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 flattens if-statements to conditional assignments if:
28 *
29 * - the GPU has limited or no flow control support
30 *   (controlled by max_depth)
31 *
32 * - small conditional branches are more expensive than conditional assignments
33 *   (controlled by min_branch_cost, that's the cost for a branch to be
34 *    preserved)
35 *
36 * It can't handle other control flow being inside of its block, such
37 * as calls or loops.  Hopefully loop unrolling and inlining will take
38 * care of those.
39 *
40 * Drivers for GPUs with no control flow support should simply call
41 *
42 *    lower_if_to_cond_assign(instructions)
43 *
44 * to attempt to flatten all if-statements.
45 *
46 * Some GPUs (such as i965 prior to gen6) do support control flow, but have a
47 * maximum nesting depth N.  Drivers for such hardware can call
48 *
49 *    lower_if_to_cond_assign(instructions, N)
50 *
51 * to attempt to flatten any if-statements appearing at depth > N.
52 */
53
54#include "compiler/glsl_types.h"
55#include "ir.h"
56#include "util/set.h"
57#include "util/hash_table.h" /* Needed for the hashing functions */
58#include "main/macros.h" /* for MAX2 */
59
60namespace {
61
62class ir_if_to_cond_assign_visitor : public ir_hierarchical_visitor {
63public:
64   ir_if_to_cond_assign_visitor(gl_shader_stage stage,
65                                unsigned max_depth,
66                                unsigned min_branch_cost)
67   {
68      this->progress = false;
69      this->stage = stage;
70      this->max_depth = max_depth;
71      this->min_branch_cost = min_branch_cost;
72      this->depth = 0;
73
74      this->condition_variables = _mesa_pointer_set_create(NULL);
75   }
76
77   ~ir_if_to_cond_assign_visitor()
78   {
79      _mesa_set_destroy(this->condition_variables, NULL);
80   }
81
82   ir_visitor_status visit_enter(ir_if *);
83   ir_visitor_status visit_leave(ir_if *);
84
85   bool found_unsupported_op;
86   bool found_expensive_op;
87   bool found_dynamic_arrayref;
88   bool is_then;
89   bool progress;
90   gl_shader_stage stage;
91   unsigned then_cost;
92   unsigned else_cost;
93   unsigned min_branch_cost;
94   unsigned max_depth;
95   unsigned depth;
96
97   struct set *condition_variables;
98};
99
100} /* anonymous namespace */
101
102bool
103lower_if_to_cond_assign(gl_shader_stage stage, exec_list *instructions,
104                        unsigned max_depth, unsigned min_branch_cost)
105{
106   if (max_depth == UINT_MAX)
107      return false;
108
109   ir_if_to_cond_assign_visitor v(stage, max_depth, min_branch_cost);
110
111   visit_list_elements(&v, instructions);
112
113   return v.progress;
114}
115
116static void
117check_ir_node(ir_instruction *ir, void *data)
118{
119   ir_if_to_cond_assign_visitor *v = (ir_if_to_cond_assign_visitor *)data;
120
121   switch (ir->ir_type) {
122   case ir_type_call:
123   case ir_type_discard:
124   case ir_type_loop:
125   case ir_type_loop_jump:
126   case ir_type_return:
127   case ir_type_emit_vertex:
128   case ir_type_end_primitive:
129   case ir_type_barrier:
130      v->found_unsupported_op = true;
131      break;
132
133   case ir_type_dereference_variable: {
134      ir_variable *var = ir->as_dereference_variable()->variable_referenced();
135
136      /* Lowering branches with TCS output accesses breaks many piglit tests,
137       * so don't touch them for now.
138       */
139      if (v->stage == MESA_SHADER_TESS_CTRL &&
140          var->data.mode == ir_var_shader_out)
141         v->found_unsupported_op = true;
142      break;
143   }
144
145   /* SSBO, images, atomic counters are handled by ir_type_call */
146   case ir_type_texture:
147      v->found_expensive_op = true;
148      break;
149
150   case ir_type_dereference_array: {
151      ir_dereference_array *deref = ir->as_dereference_array();
152
153      if (deref->array_index->ir_type != ir_type_constant)
154         v->found_dynamic_arrayref = true;
155   } /* fall-through */
156   case ir_type_expression:
157   case ir_type_dereference_record:
158      if (v->is_then)
159         v->then_cost++;
160      else
161         v->else_cost++;
162      break;
163
164   default:
165      break;
166   }
167}
168
169static void
170move_block_to_cond_assign(void *mem_ctx,
171                          ir_if *if_ir, ir_rvalue *cond_expr,
172                          exec_list *instructions,
173                          struct set *set)
174{
175   foreach_in_list_safe(ir_instruction, ir, instructions) {
176      if (ir->ir_type == ir_type_assignment) {
177         ir_assignment *assign = (ir_assignment *)ir;
178
179         if (_mesa_set_search(set, assign) == NULL) {
180            _mesa_set_add(set, assign);
181
182            /* If the LHS of the assignment is a condition variable that was
183             * previously added, insert an additional assignment of false to
184             * the variable.
185             */
186            const bool assign_to_cv =
187               _mesa_set_search(
188                  set, assign->lhs->variable_referenced()) != NULL;
189
190            if (!assign->condition) {
191               if (assign_to_cv) {
192                  assign->rhs =
193                     new(mem_ctx) ir_expression(ir_binop_logic_and,
194                                                glsl_type::bool_type,
195                                                cond_expr->clone(mem_ctx, NULL),
196                                                assign->rhs);
197               } else {
198                  assign->condition = cond_expr->clone(mem_ctx, NULL);
199               }
200            } else {
201               assign->condition =
202                  new(mem_ctx) ir_expression(ir_binop_logic_and,
203                                             glsl_type::bool_type,
204                                             cond_expr->clone(mem_ctx, NULL),
205                                             assign->condition);
206            }
207         }
208      }
209
210      /* Now, move from the if block to the block surrounding it. */
211      ir->remove();
212      if_ir->insert_before(ir);
213   }
214}
215
216ir_visitor_status
217ir_if_to_cond_assign_visitor::visit_enter(ir_if *)
218{
219   this->depth++;
220
221   return visit_continue;
222}
223
224ir_visitor_status
225ir_if_to_cond_assign_visitor::visit_leave(ir_if *ir)
226{
227   bool must_lower = this->depth-- > this->max_depth;
228
229   /* Only flatten when beyond the GPU's maximum supported nesting depth. */
230   if (!must_lower && this->min_branch_cost == 0)
231      return visit_continue;
232
233   this->found_unsupported_op = false;
234   this->found_expensive_op = false;
235   this->found_dynamic_arrayref = false;
236   this->then_cost = 0;
237   this->else_cost = 0;
238
239   ir_assignment *assign;
240
241   /* Check that both blocks don't contain anything we can't support. */
242   this->is_then = true;
243   foreach_in_list(ir_instruction, then_ir, &ir->then_instructions) {
244      visit_tree(then_ir, check_ir_node, this);
245   }
246
247   this->is_then = false;
248   foreach_in_list(ir_instruction, else_ir, &ir->else_instructions) {
249      visit_tree(else_ir, check_ir_node, this);
250   }
251
252   if (this->found_unsupported_op)
253      return visit_continue; /* can't handle inner unsupported opcodes */
254
255   /* Skip if the branch cost is high enough or if there's an expensive op.
256    *
257    * Also skip if non-constant array indices were encountered, since those
258    * can be out-of-bounds for a not-taken branch, and so generating an
259    * assignment would be incorrect. In the case of must_lower, it's up to the
260    * backend to deal with any potential fall-out (perhaps by translating the
261    * assignments to hardware-predicated moves).
262    */
263   if (!must_lower &&
264       (this->found_expensive_op ||
265        this->found_dynamic_arrayref ||
266        MAX2(this->then_cost, this->else_cost) >= this->min_branch_cost))
267      return visit_continue;
268
269   void *mem_ctx = ralloc_parent(ir);
270
271   /* Store the condition to a variable.  Move all of the instructions from
272    * the then-clause of the if-statement.  Use the condition variable as a
273    * condition for all assignments.
274    */
275   ir_variable *const then_var =
276      new(mem_ctx) ir_variable(glsl_type::bool_type,
277                               "if_to_cond_assign_then",
278                               ir_var_temporary);
279   ir->insert_before(then_var);
280
281   ir_dereference_variable *then_cond =
282      new(mem_ctx) ir_dereference_variable(then_var);
283
284   assign = new(mem_ctx) ir_assignment(then_cond, ir->condition);
285   ir->insert_before(assign);
286
287   move_block_to_cond_assign(mem_ctx, ir, then_cond,
288                             &ir->then_instructions,
289                             this->condition_variables);
290
291   /* Add the new condition variable to the hash table.  This allows us to
292    * find this variable when lowering other (enclosing) if-statements.
293    */
294   _mesa_set_add(this->condition_variables, then_var);
295
296   /* If there are instructions in the else-clause, store the inverse of the
297    * condition to a variable.  Move all of the instructions from the
298    * else-clause if the if-statement.  Use the (inverse) condition variable
299    * as a condition for all assignments.
300    */
301   if (!ir->else_instructions.is_empty()) {
302      ir_variable *const else_var =
303         new(mem_ctx) ir_variable(glsl_type::bool_type,
304                                  "if_to_cond_assign_else",
305                                  ir_var_temporary);
306      ir->insert_before(else_var);
307
308      ir_dereference_variable *else_cond =
309         new(mem_ctx) ir_dereference_variable(else_var);
310
311      ir_rvalue *inverse =
312         new(mem_ctx) ir_expression(ir_unop_logic_not,
313                                    then_cond->clone(mem_ctx, NULL));
314
315      assign = new(mem_ctx) ir_assignment(else_cond, inverse);
316      ir->insert_before(assign);
317
318      move_block_to_cond_assign(mem_ctx, ir, else_cond,
319                                &ir->else_instructions,
320                                this->condition_variables);
321
322      /* Add the new condition variable to the hash table.  This allows us to
323       * find this variable when lowering other (enclosing) if-statements.
324       */
325      _mesa_set_add(this->condition_variables, else_var);
326   }
327
328   ir->remove();
329
330   this->progress = true;
331
332   return visit_continue;
333}
334