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 ir_if_to_cond_assign.cpp
27 * This attempts to flatten all if statements to conditional
28 * assignments for GPUs that don't do control flow.
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
35 #include "glsl_types.h"
38 class ir_if_to_cond_assign_visitor
: public ir_hierarchical_visitor
{
40 ir_if_to_cond_assign_visitor()
42 this->progress
= false;
45 ir_visitor_status
visit_leave(ir_if
*);
51 do_if_to_cond_assign(exec_list
*instructions
)
53 ir_if_to_cond_assign_visitor v
;
55 visit_list_elements(&v
, instructions
);
61 check_control_flow(ir_instruction
*ir
, void *data
)
63 bool *found_control_flow
= (bool *)data
;
64 switch (ir
->ir_type
) {
68 case ir_type_loop_jump
:
70 *found_control_flow
= true;
78 move_block_to_cond_assign(void *mem_ctx
,
79 ir_if
*if_ir
, ir_variable
*cond_var
, bool then
)
81 exec_list
*instructions
;
84 instructions
= &if_ir
->then_instructions
;
86 instructions
= &if_ir
->else_instructions
;
89 foreach_iter(exec_list_iterator
, iter
, *instructions
) {
90 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
92 if (ir
->ir_type
== ir_type_assignment
) {
93 ir_assignment
*assign
= (ir_assignment
*)ir
;
95 ir_dereference
*deref
= new(mem_ctx
) ir_dereference_variable(cond_var
);
100 cond_expr
= new(mem_ctx
) ir_expression(ir_unop_logic_not
,
101 glsl_type::bool_type
,
106 if (!assign
->condition
) {
107 assign
->condition
= cond_expr
;
109 assign
->condition
= new(mem_ctx
) ir_expression(ir_binop_logic_and
,
110 glsl_type::bool_type
,
116 /* Now, move from the if block to the block surrounding it. */
118 if_ir
->insert_before(ir
);
123 ir_if_to_cond_assign_visitor::visit_leave(ir_if
*ir
)
125 bool found_control_flow
= false;
126 ir_variable
*cond_var
;
127 ir_assignment
*assign
;
128 ir_dereference_variable
*deref
;
130 /* Check that both blocks don't contain anything we can't support. */
131 foreach_iter(exec_list_iterator
, then_iter
, ir
->then_instructions
) {
132 ir_instruction
*then_ir
= (ir_instruction
*)then_iter
.get();
133 visit_tree(then_ir
, check_control_flow
, &found_control_flow
);
135 foreach_iter(exec_list_iterator
, else_iter
, ir
->else_instructions
) {
136 ir_instruction
*else_ir
= (ir_instruction
*)else_iter
.get();
137 visit_tree(else_ir
, check_control_flow
, &found_control_flow
);
139 if (found_control_flow
)
140 return visit_continue
;
142 void *mem_ctx
= talloc_parent(ir
);
144 /* Store the condition to a variable so the assignment conditions are
147 cond_var
= new(mem_ctx
) ir_variable(glsl_type::bool_type
,
148 "if_to_cond_assign_condition",
150 ir
->insert_before(cond_var
);
152 deref
= new(mem_ctx
) ir_dereference_variable(cond_var
);
153 assign
= new(mem_ctx
) ir_assignment(deref
,
154 ir
->condition
, NULL
);
155 ir
->insert_before(assign
);
157 /* Now, move all of the instructions out of the if blocks, putting
158 * conditions on assignments.
160 move_block_to_cond_assign(mem_ctx
, ir
, cond_var
, true);
161 move_block_to_cond_assign(mem_ctx
, ir
, cond_var
, false);
165 this->progress
= true;
167 return visit_continue
;