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 
26 static void
add_src(nir_src * src,struct set * invariants)27 add_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 
36 static bool
add_src_cb(nir_src * src,void * state)37 add_src_cb(nir_src *src, void *state)
38 {
39    add_src(src, state);
40    return true;
41 }
42 
43 static bool
dest_is_invariant(nir_dest * dest,struct set * invariants)44 dest_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 
53 static void
add_cf_node(nir_cf_node * cf,struct set * invariants)54 add_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 
65 static void
add_var(nir_variable * var,struct set * invariants)66 add_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 
76 static bool
var_is_invariant(nir_variable * var,struct set * invariants)77 var_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 
86 static void
propagate_invariant_instr(nir_instr * instr,struct set * invariants)87 propagate_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    }
131 
132    case nir_instr_type_deref:
133    case nir_instr_type_jump:
134    case nir_instr_type_ssa_undef:
135    case nir_instr_type_load_const:
136       break; /* Nothing to do */
137 
138    case nir_instr_type_phi: {
139       nir_phi_instr *phi = nir_instr_as_phi(instr);
140       if (!dest_is_invariant(&phi->dest, invariants))
141          break;
142 
143       nir_foreach_phi_src(src, phi) {
144          add_src(&src->src, invariants);
145          add_cf_node(&src->pred->cf_node, invariants);
146       }
147       break;
148    }
149 
150    case nir_instr_type_call:
151       unreachable("This pass must be run after function inlining");
152 
153    case nir_instr_type_parallel_copy:
154    default:
155       unreachable("Cannot have this instruction type");
156    }
157 }
158 
159 static bool
propagate_invariant_impl(nir_function_impl * impl,struct set * invariants)160 propagate_invariant_impl(nir_function_impl *impl, struct set *invariants)
161 {
162    bool progress = false;
163 
164    while (true) {
165       uint32_t prev_entries = invariants->entries;
166 
167       nir_foreach_block_reverse(block, impl) {
168          nir_foreach_instr_reverse(instr, block)
169             propagate_invariant_instr(instr, invariants);
170       }
171 
172       /* Keep running until we make no more progress. */
173       if (invariants->entries > prev_entries) {
174          progress = true;
175          continue;
176       } else {
177          break;
178       }
179    }
180 
181    if (progress) {
182       nir_metadata_preserve(impl, nir_metadata_block_index |
183                                   nir_metadata_dominance |
184                                   nir_metadata_live_ssa_defs);
185    }
186 
187    return progress;
188 }
189 
190 bool
nir_propagate_invariant(nir_shader * shader)191 nir_propagate_invariant(nir_shader *shader)
192 {
193    /* Hash set of invariant things */
194    struct set *invariants = _mesa_pointer_set_create(NULL);
195 
196    bool progress = false;
197    nir_foreach_function(function, shader) {
198       if (function->impl && propagate_invariant_impl(function->impl, invariants))
199          progress = true;
200    }
201 
202    _mesa_set_destroy(invariants, NULL);
203 
204    return progress;
205 }
206