1/*
2 * Copyright © 2016 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#include "nir.h"
25
26static void
27add_src(nir_src *src, struct set *invariants)
28{
29   if (src->is_ssa) {
30      _mesa_set_add(invariants, src->ssa);
31   } else {
32      _mesa_set_add(invariants, src->reg.reg);
33   }
34}
35
36static bool
37add_src_cb(nir_src *src, void *state)
38{
39   add_src(src, state);
40   return true;
41}
42
43static bool
44dest_is_invariant(nir_dest *dest, struct set *invariants)
45{
46   if (dest->is_ssa) {
47      return _mesa_set_search(invariants, &dest->ssa);
48   } else {
49      return _mesa_set_search(invariants, dest->reg.reg);
50   }
51}
52
53static void
54add_cf_node(nir_cf_node *cf, struct set *invariants)
55{
56   if (cf->type == nir_cf_node_if) {
57      nir_if *if_stmt = nir_cf_node_as_if(cf);
58      add_src(&if_stmt->condition, invariants);
59   }
60
61   if (cf->parent)
62      add_cf_node(cf->parent, invariants);
63}
64
65static void
66add_var(nir_variable *var, struct set *invariants)
67{
68   /* Because we pass the result of nir_intrinsic_get_var directly to this
69    * function, it's possible for var to be NULL if, for instance, there's a
70    * cast somewhere in the chain.
71    */
72   if (var != NULL)
73      _mesa_set_add(invariants, var);
74}
75
76static bool
77var_is_invariant(nir_variable *var, struct set * invariants)
78{
79   /* Because we pass the result of nir_intrinsic_get_var directly to this
80    * function, it's possible for var to be NULL if, for instance, there's a
81    * cast somewhere in the chain.
82    */
83   return var && (var->data.invariant || _mesa_set_search(invariants, var));
84}
85
86static void
87propagate_invariant_instr(nir_instr *instr, struct set *invariants)
88{
89   switch (instr->type) {
90   case nir_instr_type_alu: {
91      nir_alu_instr *alu = nir_instr_as_alu(instr);
92      if (!dest_is_invariant(&alu->dest.dest, invariants))
93         break;
94
95      alu->exact = true;
96      nir_foreach_src(instr, add_src_cb, invariants);
97      break;
98   }
99
100   case nir_instr_type_tex: {
101      nir_tex_instr *tex = nir_instr_as_tex(instr);
102      if (dest_is_invariant(&tex->dest, invariants))
103         nir_foreach_src(instr, add_src_cb, invariants);
104      break;
105   }
106
107   case nir_instr_type_intrinsic: {
108      nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
109      switch (intrin->intrinsic) {
110      case nir_intrinsic_copy_deref:
111         /* If the destination is invariant then so is the source */
112         if (var_is_invariant(nir_intrinsic_get_var(intrin, 0), invariants))
113            add_var(nir_intrinsic_get_var(intrin, 1), invariants);
114         break;
115
116      case nir_intrinsic_load_deref:
117         if (dest_is_invariant(&intrin->dest, invariants))
118            add_var(nir_intrinsic_get_var(intrin, 0), invariants);
119         break;
120
121      case nir_intrinsic_store_deref:
122         if (var_is_invariant(nir_intrinsic_get_var(intrin, 0), invariants))
123            add_src(&intrin->src[1], invariants);
124         break;
125
126      default:
127         /* Nothing to do */
128         break;
129      }
130      FALLTHROUGH;
131   }
132
133   case nir_instr_type_deref:
134   case nir_instr_type_jump:
135   case nir_instr_type_ssa_undef:
136   case nir_instr_type_load_const:
137      break; /* Nothing to do */
138
139   case nir_instr_type_phi: {
140      nir_phi_instr *phi = nir_instr_as_phi(instr);
141      if (!dest_is_invariant(&phi->dest, invariants))
142         break;
143
144      nir_foreach_phi_src(src, phi) {
145         add_src(&src->src, invariants);
146         add_cf_node(&src->pred->cf_node, invariants);
147      }
148      break;
149   }
150
151   case nir_instr_type_call:
152      unreachable("This pass must be run after function inlining");
153
154   case nir_instr_type_parallel_copy:
155   default:
156      unreachable("Cannot have this instruction type");
157   }
158}
159
160static bool
161propagate_invariant_impl(nir_function_impl *impl, struct set *invariants)
162{
163   bool progress = false;
164
165   while (true) {
166      uint32_t prev_entries = invariants->entries;
167
168      nir_foreach_block_reverse(block, impl) {
169         nir_foreach_instr_reverse(instr, block)
170            propagate_invariant_instr(instr, invariants);
171      }
172
173      /* Keep running until we make no more progress. */
174      if (invariants->entries > prev_entries) {
175         progress = true;
176         continue;
177      } else {
178         break;
179      }
180   }
181
182   if (progress) {
183      nir_metadata_preserve(impl, nir_metadata_block_index |
184                                  nir_metadata_dominance |
185                                  nir_metadata_live_ssa_defs);
186   } else {
187      nir_metadata_preserve(impl, nir_metadata_all);
188   }
189
190   return progress;
191}
192
193/* If invariant_prim=true, this pass considers all geometry-affecting
194 * outputs as invariant. Doing this works around a common class of application
195 * bugs appearing as flickering.
196 */
197bool
198nir_propagate_invariant(nir_shader *shader, bool invariant_prim)
199{
200   /* Hash set of invariant things */
201   struct set *invariants = _mesa_pointer_set_create(NULL);
202
203   if (shader->info.stage != MESA_SHADER_FRAGMENT && invariant_prim) {
204      nir_foreach_shader_out_variable(var, shader) {
205         switch (var->data.location) {
206         case VARYING_SLOT_POS:
207         case VARYING_SLOT_PSIZ:
208         case VARYING_SLOT_CLIP_DIST0:
209         case VARYING_SLOT_CLIP_DIST1:
210         case VARYING_SLOT_CULL_DIST0:
211         case VARYING_SLOT_CULL_DIST1:
212         case VARYING_SLOT_TESS_LEVEL_OUTER:
213         case VARYING_SLOT_TESS_LEVEL_INNER:
214            if (!var->data.invariant)
215               _mesa_set_add(invariants, var);
216            break;
217         default:
218            break;
219         }
220      }
221   }
222
223   bool progress = false;
224   nir_foreach_function(function, shader) {
225      if (function->impl && propagate_invariant_impl(function->impl, invariants))
226         progress = true;
227   }
228
229   _mesa_set_destroy(invariants, NULL);
230
231   return progress;
232}
233