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