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