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