glsl: Fix handling of function calls inside nested loops.
[mesa.git] / src / glsl / lower_if_to_cond_assign.cpp
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 */
23
24 /**
25 * \file lower_if_to_cond_assign.cpp
26 *
27 * This attempts to flatten if-statements to conditional assignments for
28 * GPUs with limited or no flow control support.
29 *
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
32 * care of those.
33 *
34 * Drivers for GPUs with no control flow support should simply call
35 *
36 * lower_if_to_cond_assign(instructions)
37 *
38 * to attempt to flatten all if-statements.
39 *
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
42 *
43 * lower_if_to_cond_assign(instructions, N)
44 *
45 * to attempt to flatten any if-statements appearing at depth > N.
46 */
47
48 #include "glsl_types.h"
49 #include "ir.h"
50 #include "program/hash_table.h"
51
52 namespace {
53
54 class ir_if_to_cond_assign_visitor : public ir_hierarchical_visitor {
55 public:
56 ir_if_to_cond_assign_visitor(unsigned max_depth)
57 {
58 this->progress = false;
59 this->max_depth = max_depth;
60 this->depth = 0;
61
62 this->condition_variables = hash_table_ctor(0, hash_table_pointer_hash,
63 hash_table_pointer_compare);
64 }
65
66 ~ir_if_to_cond_assign_visitor()
67 {
68 hash_table_dtor(this->condition_variables);
69 }
70
71 ir_visitor_status visit_enter(ir_if *);
72 ir_visitor_status visit_leave(ir_if *);
73
74 bool progress;
75 unsigned max_depth;
76 unsigned depth;
77
78 struct hash_table *condition_variables;
79 };
80
81 } /* anonymous namespace */
82
83 bool
84 lower_if_to_cond_assign(exec_list *instructions, unsigned max_depth)
85 {
86 if (max_depth == UINT_MAX)
87 return false;
88
89 ir_if_to_cond_assign_visitor v(max_depth);
90
91 visit_list_elements(&v, instructions);
92
93 return v.progress;
94 }
95
96 void
97 check_control_flow(ir_instruction *ir, void *data)
98 {
99 bool *found_control_flow = (bool *)data;
100 switch (ir->ir_type) {
101 case ir_type_call:
102 case ir_type_discard:
103 case ir_type_loop:
104 case ir_type_loop_jump:
105 case ir_type_return:
106 *found_control_flow = true;
107 break;
108 default:
109 break;
110 }
111 }
112
113 void
114 move_block_to_cond_assign(void *mem_ctx,
115 ir_if *if_ir, ir_rvalue *cond_expr,
116 exec_list *instructions,
117 struct hash_table *ht)
118 {
119 foreach_list_safe(node, instructions) {
120 ir_instruction *ir = (ir_instruction *) node;
121
122 if (ir->ir_type == ir_type_assignment) {
123 ir_assignment *assign = (ir_assignment *)ir;
124
125 if (hash_table_find(ht, assign) == NULL) {
126 hash_table_insert(ht, assign, assign);
127
128 /* If the LHS of the assignment is a condition variable that was
129 * previously added, insert an additional assignment of false to
130 * the variable.
131 */
132 const bool assign_to_cv =
133 hash_table_find(ht, assign->lhs->variable_referenced()) != NULL;
134
135 if (!assign->condition) {
136 if (assign_to_cv) {
137 assign->rhs =
138 new(mem_ctx) ir_expression(ir_binop_logic_and,
139 glsl_type::bool_type,
140 cond_expr->clone(mem_ctx, NULL),
141 assign->rhs);
142 } else {
143 assign->condition = cond_expr->clone(mem_ctx, NULL);
144 }
145 } else {
146 assign->condition =
147 new(mem_ctx) ir_expression(ir_binop_logic_and,
148 glsl_type::bool_type,
149 cond_expr->clone(mem_ctx, NULL),
150 assign->condition);
151 }
152 }
153 }
154
155 /* Now, move from the if block to the block surrounding it. */
156 ir->remove();
157 if_ir->insert_before(ir);
158 }
159 }
160
161 ir_visitor_status
162 ir_if_to_cond_assign_visitor::visit_enter(ir_if *ir)
163 {
164 (void) ir;
165 this->depth++;
166
167 return visit_continue;
168 }
169
170 ir_visitor_status
171 ir_if_to_cond_assign_visitor::visit_leave(ir_if *ir)
172 {
173 /* Only flatten when beyond the GPU's maximum supported nesting depth. */
174 if (this->depth-- <= this->max_depth)
175 return visit_continue;
176
177 bool found_control_flow = false;
178 ir_assignment *assign;
179
180 /* Check that both blocks don't contain anything we can't support. */
181 foreach_iter(exec_list_iterator, then_iter, ir->then_instructions) {
182 ir_instruction *then_ir = (ir_instruction *)then_iter.get();
183 visit_tree(then_ir, check_control_flow, &found_control_flow);
184 }
185 foreach_iter(exec_list_iterator, else_iter, ir->else_instructions) {
186 ir_instruction *else_ir = (ir_instruction *)else_iter.get();
187 visit_tree(else_ir, check_control_flow, &found_control_flow);
188 }
189 if (found_control_flow)
190 return visit_continue;
191
192 void *mem_ctx = ralloc_parent(ir);
193
194 /* Store the condition to a variable. Move all of the instructions from
195 * the then-clause of the if-statement. Use the condition variable as a
196 * condition for all assignments.
197 */
198 ir_variable *const then_var =
199 new(mem_ctx) ir_variable(glsl_type::bool_type,
200 "if_to_cond_assign_then",
201 ir_var_temporary);
202 ir->insert_before(then_var);
203
204 ir_dereference_variable *then_cond =
205 new(mem_ctx) ir_dereference_variable(then_var);
206
207 assign = new(mem_ctx) ir_assignment(then_cond, ir->condition);
208 ir->insert_before(assign);
209
210 move_block_to_cond_assign(mem_ctx, ir, then_cond,
211 &ir->then_instructions,
212 this->condition_variables);
213
214 /* Add the new condition variable to the hash table. This allows us to
215 * find this variable when lowering other (enclosing) if-statements.
216 */
217 hash_table_insert(this->condition_variables, then_var, then_var);
218
219 /* If there are instructions in the else-clause, store the inverse of the
220 * condition to a variable. Move all of the instructions from the
221 * else-clause if the if-statement. Use the (inverse) condition variable
222 * as a condition for all assignments.
223 */
224 if (!ir->else_instructions.is_empty()) {
225 ir_variable *const else_var =
226 new(mem_ctx) ir_variable(glsl_type::bool_type,
227 "if_to_cond_assign_else",
228 ir_var_temporary);
229 ir->insert_before(else_var);
230
231 ir_dereference_variable *else_cond =
232 new(mem_ctx) ir_dereference_variable(else_var);
233
234 ir_rvalue *inverse =
235 new(mem_ctx) ir_expression(ir_unop_logic_not,
236 then_cond->clone(mem_ctx, NULL));
237
238 assign = new(mem_ctx) ir_assignment(else_cond, inverse);
239 ir->insert_before(assign);
240
241 move_block_to_cond_assign(mem_ctx, ir, else_cond,
242 &ir->else_instructions,
243 this->condition_variables);
244
245 /* Add the new condition variable to the hash table. This allows us to
246 * find this variable when lowering other (enclosing) if-statements.
247 */
248 hash_table_insert(this->condition_variables, else_var, else_var);
249 }
250
251 ir->remove();
252
253 this->progress = true;
254
255 return visit_continue;
256 }