nir: Remove fake edges in the CF handling code
[mesa.git] / src / compiler / nir / nir_opt_remove_phis.c
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 (cwabbott0@gmail.com)
25 *
26 */
27
28 #include "nir.h"
29
30 static nir_alu_instr *
31 get_parent_mov(nir_ssa_def *ssa)
32 {
33 if (ssa->parent_instr->type != nir_instr_type_alu)
34 return NULL;
35
36 nir_alu_instr *alu = nir_instr_as_alu(ssa->parent_instr);
37 return (alu->op == nir_op_imov || alu->op == nir_op_fmov) ? alu : NULL;
38 }
39
40 static bool
41 matching_mov(nir_alu_instr *mov1, nir_ssa_def *ssa)
42 {
43 if (!mov1)
44 return false;
45
46 nir_alu_instr *mov2 = get_parent_mov(ssa);
47 return mov2 && nir_alu_srcs_equal(mov1, mov2, 0, 0);
48 }
49
50 /*
51 * This is a pass for removing phi nodes that look like:
52 * a = phi(b, b, b, ...)
53 *
54 * Note that we can't ignore undef sources here, or else we may create a
55 * situation where the definition of b isn't dominated by its uses. We're
56 * allowed to do this since the definition of b must dominate all of the
57 * phi node's predecessors, which means it must dominate the phi node as well
58 * as all of the phi node's uses. In essence, the phi node acts as a copy
59 * instruction. b can't be another phi node in the same block, since the only
60 * time when phi nodes can source other phi nodes defined in the same block is
61 * at the loop header, and in that case one of the sources of the phi has to
62 * be from before the loop and that source can't be b.
63 */
64
65 static bool
66 remove_phis_block(nir_block *block)
67 {
68 bool progress = false;
69
70 nir_foreach_instr_safe(instr, block) {
71 if (instr->type != nir_instr_type_phi)
72 break;
73
74 nir_phi_instr *phi = nir_instr_as_phi(instr);
75
76 nir_ssa_def *def = NULL;
77 nir_alu_instr *mov = NULL;
78 bool srcs_same = true;
79
80 nir_foreach_phi_src(src, phi) {
81 assert(src->src.is_ssa);
82
83 /* For phi nodes at the beginning of loops, we may encounter some
84 * sources from backedges that point back to the destination of the
85 * same phi, i.e. something like:
86 *
87 * a = phi(a, b, ...)
88 *
89 * We can safely ignore these sources, since if all of the normal
90 * sources point to the same definition, then that definition must
91 * still dominate the phi node, and the phi will still always take
92 * the value of that definition.
93 */
94 if (src->src.ssa == &phi->dest.ssa)
95 continue;
96
97 if (def == NULL) {
98 def = src->src.ssa;
99 mov = get_parent_mov(def);
100 } else {
101 if (src->src.ssa != def && !matching_mov(mov, src->src.ssa)) {
102 srcs_same = false;
103 break;
104 }
105 }
106 }
107
108 if (!srcs_same)
109 continue;
110
111 /* We must have found at least one definition, since there must be at
112 * least one forward edge.
113 */
114 assert(def != NULL);
115
116 assert(phi->dest.is_ssa);
117 nir_ssa_def_rewrite_uses(&phi->dest.ssa, nir_src_for_ssa(def));
118 nir_instr_remove(instr);
119
120 progress = true;
121 }
122
123 return progress;
124 }
125
126 static bool
127 remove_phis_impl(nir_function_impl *impl)
128 {
129 bool progress = false;
130
131 nir_foreach_block(block, impl) {
132 progress |= remove_phis_block(block);
133 }
134
135 if (progress) {
136 nir_metadata_preserve(impl, nir_metadata_block_index |
137 nir_metadata_dominance);
138 }
139
140 return progress;
141 }
142
143 bool
144 nir_opt_remove_phis(nir_shader *shader)
145 {
146 bool progress = false;
147
148 nir_foreach_function(function, shader)
149 if (function->impl)
150 progress = remove_phis_impl(function->impl) || progress;
151
152 return progress;
153 }
154