2 * Copyright © 2010 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
25 * \file lower_if_to_cond_assign.cpp
27 * This attempts to flatten if-statements to conditional assignments for
28 * GPUs with limited or no flow control support.
30 * It can't handle other control flow being inside of its block, such
31 * as calls or loops. Hopefully loop unrolling and inlining will take
34 * Drivers for GPUs with no control flow support should simply call
36 * lower_if_to_cond_assign(instructions)
38 * to attempt to flatten all if-statements.
40 * Some GPUs (such as i965 prior to gen6) do support control flow, but have a
41 * maximum nesting depth N. Drivers for such hardware can call
43 * lower_if_to_cond_assign(instructions, N)
45 * to attempt to flatten any if-statements appearing at depth > N.
48 #include "glsl_types.h"
51 class ir_if_to_cond_assign_visitor
: public ir_hierarchical_visitor
{
53 ir_if_to_cond_assign_visitor(unsigned max_depth
)
55 this->progress
= false;
56 this->max_depth
= max_depth
;
60 ir_visitor_status
visit_enter(ir_if
*);
61 ir_visitor_status
visit_leave(ir_if
*);
69 lower_if_to_cond_assign(exec_list
*instructions
, unsigned max_depth
)
71 ir_if_to_cond_assign_visitor
v(max_depth
);
73 visit_list_elements(&v
, instructions
);
79 check_control_flow(ir_instruction
*ir
, void *data
)
81 bool *found_control_flow
= (bool *)data
;
82 switch (ir
->ir_type
) {
86 case ir_type_loop_jump
:
88 *found_control_flow
= true;
96 move_block_to_cond_assign(void *mem_ctx
,
97 ir_if
*if_ir
, ir_variable
*cond_var
, bool then
)
99 exec_list
*instructions
;
102 instructions
= &if_ir
->then_instructions
;
104 instructions
= &if_ir
->else_instructions
;
107 foreach_iter(exec_list_iterator
, iter
, *instructions
) {
108 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
110 if (ir
->ir_type
== ir_type_assignment
) {
111 ir_assignment
*assign
= (ir_assignment
*)ir
;
112 ir_rvalue
*cond_expr
;
113 ir_dereference
*deref
= new(mem_ctx
) ir_dereference_variable(cond_var
);
118 cond_expr
= new(mem_ctx
) ir_expression(ir_unop_logic_not
,
119 glsl_type::bool_type
,
124 if (!assign
->condition
) {
125 assign
->condition
= cond_expr
;
127 assign
->condition
= new(mem_ctx
) ir_expression(ir_binop_logic_and
,
128 glsl_type::bool_type
,
134 /* Now, move from the if block to the block surrounding it. */
136 if_ir
->insert_before(ir
);
141 ir_if_to_cond_assign_visitor::visit_enter(ir_if
*ir
)
145 return visit_continue
;
149 ir_if_to_cond_assign_visitor::visit_leave(ir_if
*ir
)
151 /* Only flatten when beyond the GPU's maximum supported nesting depth. */
152 if (this->depth
-- <= this->max_depth
)
153 return visit_continue
;
155 bool found_control_flow
= false;
156 ir_variable
*cond_var
;
157 ir_assignment
*assign
;
158 ir_dereference_variable
*deref
;
160 /* Check that both blocks don't contain anything we can't support. */
161 foreach_iter(exec_list_iterator
, then_iter
, ir
->then_instructions
) {
162 ir_instruction
*then_ir
= (ir_instruction
*)then_iter
.get();
163 visit_tree(then_ir
, check_control_flow
, &found_control_flow
);
165 foreach_iter(exec_list_iterator
, else_iter
, ir
->else_instructions
) {
166 ir_instruction
*else_ir
= (ir_instruction
*)else_iter
.get();
167 visit_tree(else_ir
, check_control_flow
, &found_control_flow
);
169 if (found_control_flow
)
170 return visit_continue
;
172 void *mem_ctx
= ralloc_parent(ir
);
174 /* Store the condition to a variable so the assignment conditions are
177 cond_var
= new(mem_ctx
) ir_variable(glsl_type::bool_type
,
178 "if_to_cond_assign_condition",
180 ir
->insert_before(cond_var
);
182 deref
= new(mem_ctx
) ir_dereference_variable(cond_var
);
183 assign
= new(mem_ctx
) ir_assignment(deref
,
184 ir
->condition
, NULL
);
185 ir
->insert_before(assign
);
187 /* Now, move all of the instructions out of the if blocks, putting
188 * conditions on assignments.
190 move_block_to_cond_assign(mem_ctx
, ir
, cond_var
, true);
191 move_block_to_cond_assign(mem_ctx
, ir
, cond_var
, false);
195 this->progress
= true;
197 return visit_continue
;