xref: /aosp_15_r20/external/mesa3d/src/compiler/nir/nir_opt_remove_phis.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2015 Connor Abbott
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 DEALINGS
21  * IN THE SOFTWARE.
22  *
23  * Authors:
24  *    Connor Abbott ([email protected])
25  *
26  */
27 
28 #include "nir.h"
29 #include "nir_builder.h"
30 
31 static nir_alu_instr *
get_parent_mov(nir_def * ssa)32 get_parent_mov(nir_def *ssa)
33 {
34    if (ssa->parent_instr->type != nir_instr_type_alu)
35       return NULL;
36 
37    nir_alu_instr *alu = nir_instr_as_alu(ssa->parent_instr);
38    return (alu->op == nir_op_mov) ? alu : NULL;
39 }
40 
41 static bool
matching_mov(nir_alu_instr * mov1,nir_def * ssa)42 matching_mov(nir_alu_instr *mov1, nir_def *ssa)
43 {
44    if (!mov1)
45       return false;
46 
47    nir_alu_instr *mov2 = get_parent_mov(ssa);
48    return mov2 && nir_alu_srcs_equal(mov1, mov2, 0, 0);
49 }
50 
51 /*
52  * This is a pass for removing phi nodes that look like:
53  * a = phi(b, b, b, ...)
54  *
55  * Note that we can't always ignore undef sources here, or else we may create a
56  * situation where the definition of b isn't dominated by its uses. We're
57  * allowed to do this since the definition of b must dominate all of the
58  * phi node's predecessors, which means it must dominate the phi node as well
59  * as all of the phi node's uses. In essence, the phi node acts as a copy
60  * instruction. b can't be another phi node in the same block, since the only
61  * time when phi nodes can source other phi nodes defined in the same block is
62  * at the loop header, and in that case one of the sources of the phi has to
63  * be from before the loop and that source can't be b.
64  */
65 
66 static bool
remove_phis_block(nir_block * block,nir_builder * b)67 remove_phis_block(nir_block *block, nir_builder *b)
68 {
69    bool progress = false;
70 
71    nir_foreach_phi_safe(phi, block) {
72       nir_def *def = NULL;
73       nir_alu_instr *mov = NULL;
74       bool srcs_same = true;
75 
76       nir_foreach_phi_src(src, phi) {
77          /* For phi nodes at the beginning of loops, we may encounter some
78           * sources from backedges that point back to the destination of the
79           * same phi, i.e. something like:
80           *
81           * a = phi(a, b, ...)
82           *
83           * We can safely ignore these sources, since if all of the normal
84           * sources point to the same definition, then that definition must
85           * still dominate the phi node, and the phi will still always take
86           * the value of that definition.
87           */
88          if (src->src.ssa == &phi->def)
89             continue;
90 
91          if (def == NULL) {
92             def = src->src.ssa;
93             mov = get_parent_mov(def);
94          } else if (nir_src_is_undef(src->src) &&
95                     nir_block_dominates(def->parent_instr->block, src->pred)) {
96             /* Ignore this undef source. */
97          } else {
98             if (src->src.ssa != def && !matching_mov(mov, src->src.ssa)) {
99                srcs_same = false;
100                break;
101             }
102          }
103       }
104 
105       if (!srcs_same)
106          continue;
107 
108       if (!def) {
109          /* In this case, the phi had no sources. So turn it into an undef. */
110 
111          b->cursor = nir_after_phis(block);
112          def = nir_undef(b, phi->def.num_components,
113                          phi->def.bit_size);
114       } else if (mov) {
115          /* If the sources were all movs from the same source with the same
116           * swizzle, then we can't just pick a random move because it may not
117           * dominate the phi node. Instead, we need to emit our own move after
118           * the phi which uses the shared source, and rewrite uses of the phi
119           * to use the move instead. This is ok, because while the movs may
120           * not all dominate the phi node, their shared source does.
121           */
122 
123          b->cursor = nir_after_phis(block);
124          def = nir_mov_alu(b, mov->src[0], def->num_components);
125       }
126 
127       nir_def_replace(&phi->def, def);
128 
129       progress = true;
130    }
131 
132    return progress;
133 }
134 
135 bool
nir_opt_remove_phis_block(nir_block * block)136 nir_opt_remove_phis_block(nir_block *block)
137 {
138    nir_builder b = nir_builder_create(nir_cf_node_get_function(&block->cf_node));
139    return remove_phis_block(block, &b);
140 }
141 
142 static bool
nir_opt_remove_phis_impl(nir_function_impl * impl)143 nir_opt_remove_phis_impl(nir_function_impl *impl)
144 {
145    bool progress = false;
146    nir_builder bld = nir_builder_create(impl);
147 
148    nir_metadata_require(impl, nir_metadata_dominance);
149 
150    nir_foreach_block(block, impl) {
151       progress |= remove_phis_block(block, &bld);
152    }
153 
154    if (progress) {
155       nir_metadata_preserve(impl, nir_metadata_control_flow);
156    } else {
157       nir_metadata_preserve(impl, nir_metadata_all);
158    }
159 
160    return progress;
161 }
162 
163 bool
nir_opt_remove_phis(nir_shader * shader)164 nir_opt_remove_phis(nir_shader *shader)
165 {
166    bool progress = false;
167 
168    nir_foreach_function_impl(impl, shader)
169       progress = nir_opt_remove_phis_impl(impl) || progress;
170 
171    return progress;
172 }
173