4a98a2812c33f0bb5a24143b600d65bd0c787043
[mesa.git] / src / compiler / nir / nir_opt_copy_propagate.c
1 /*
2 * Copyright © 2014 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 DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Connor Abbott (cwabbott0@gmail.com)
25 *
26 */
27
28 #include "nir.h"
29 #include <util/imports.h>
30
31 /**
32 * SSA-based copy propagation
33 */
34
35 static bool is_move(nir_alu_instr *instr)
36 {
37 assert(instr->src[0].src.is_ssa);
38
39 if (instr->op != nir_op_mov)
40 return false;
41
42 if (instr->dest.saturate)
43 return false;
44
45 /* we handle modifiers in a separate pass */
46
47 if (instr->src[0].abs || instr->src[0].negate)
48 return false;
49
50 return true;
51
52 }
53
54 static bool is_vec(nir_alu_instr *instr)
55 {
56 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
57 assert(instr->src[i].src.is_ssa);
58
59 /* we handle modifiers in a separate pass */
60 if (instr->src[i].abs || instr->src[i].negate)
61 return false;
62 }
63
64 assert(instr->dest.dest.is_ssa);
65 return nir_op_is_vec(instr->op);
66 }
67
68 static bool
69 is_swizzleless_move(nir_alu_instr *instr)
70 {
71 if (is_move(instr)) {
72 for (unsigned i = 0; i < 4; i++) {
73 if (!((instr->dest.write_mask >> i) & 1))
74 break;
75 if (instr->src[0].swizzle[i] != i)
76 return false;
77 }
78 return true;
79 } else if (is_vec(instr)) {
80 nir_ssa_def *def = NULL;
81 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
82 if (instr->src[i].swizzle[0] != i)
83 return false;
84
85 if (def == NULL) {
86 def = instr->src[i].src.ssa;
87 } else if (instr->src[i].src.ssa != def) {
88 return false;
89 }
90 }
91 return true;
92 } else {
93 return false;
94 }
95 }
96
97 static bool
98 copy_prop_src(nir_src *src, nir_instr *parent_instr, nir_if *parent_if,
99 unsigned num_components)
100 {
101 assert(src->is_ssa);
102
103 nir_instr *src_instr = src->ssa->parent_instr;
104 nir_ssa_def *copy_def;
105 if (src_instr->type == nir_instr_type_alu) {
106 nir_alu_instr *alu_instr = nir_instr_as_alu(src_instr);
107 if (!is_swizzleless_move(alu_instr))
108 return false;
109
110 if (alu_instr->src[0].src.ssa->num_components != num_components)
111 return false;
112
113 copy_def= alu_instr->src[0].src.ssa;
114 } else {
115 return false;
116 }
117
118 if (parent_instr) {
119 nir_instr_rewrite_src(parent_instr, src, nir_src_for_ssa(copy_def));
120 } else {
121 assert(src == &parent_if->condition);
122 nir_if_rewrite_condition(parent_if, nir_src_for_ssa(copy_def));
123 }
124
125 return true;
126 }
127
128 static bool
129 copy_prop_alu_src(nir_alu_instr *parent_alu_instr, unsigned index)
130 {
131 nir_alu_src *src = &parent_alu_instr->src[index];
132 assert(src->src.is_ssa);
133
134 nir_instr *src_instr = src->src.ssa->parent_instr;
135 if (src_instr->type != nir_instr_type_alu)
136 return false;
137
138 nir_alu_instr *alu_instr = nir_instr_as_alu(src_instr);
139 if (!is_move(alu_instr) && !is_vec(alu_instr))
140 return false;
141
142 nir_ssa_def *def;
143 unsigned new_swizzle[NIR_MAX_VEC_COMPONENTS] = {0, 0, 0, 0};
144
145 if (alu_instr->op == nir_op_mov) {
146 for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++)
147 new_swizzle[i] = alu_instr->src[0].swizzle[src->swizzle[i]];
148 def = alu_instr->src[0].src.ssa;
149 } else {
150 def = NULL;
151
152 for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++) {
153 if (!nir_alu_instr_channel_used(parent_alu_instr, index, i))
154 continue;
155
156 nir_ssa_def *new_def = alu_instr->src[src->swizzle[i]].src.ssa;
157 if (def == NULL)
158 def = new_def;
159 else {
160 if (def != new_def)
161 return false;
162 }
163 new_swizzle[i] = alu_instr->src[src->swizzle[i]].swizzle[0];
164 }
165 }
166
167 for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++)
168 src->swizzle[i] = new_swizzle[i];
169
170 nir_instr_rewrite_src(&parent_alu_instr->instr, &src->src,
171 nir_src_for_ssa(def));
172
173 return true;
174 }
175
176 static bool
177 copy_prop_instr(nir_instr *instr)
178 {
179 bool progress = false;
180 switch (instr->type) {
181 case nir_instr_type_alu: {
182 nir_alu_instr *alu_instr = nir_instr_as_alu(instr);
183
184 for (unsigned i = 0; i < nir_op_infos[alu_instr->op].num_inputs; i++)
185 while (copy_prop_alu_src(alu_instr, i))
186 progress = true;
187
188 return progress;
189 }
190
191 case nir_instr_type_deref: {
192 nir_deref_instr *deref = nir_instr_as_deref(instr);
193
194 if (deref->deref_type != nir_deref_type_var) {
195 assert(deref->dest.is_ssa);
196 const unsigned comps = deref->dest.ssa.num_components;
197 while (copy_prop_src(&deref->parent, instr, NULL, comps))
198 progress = true;
199 }
200
201 if (deref->deref_type == nir_deref_type_array ||
202 deref->deref_type == nir_deref_type_ptr_as_array) {
203 while (copy_prop_src(&deref->arr.index, instr, NULL, 1))
204 progress = true;
205 }
206
207 return progress;
208 }
209
210 case nir_instr_type_tex: {
211 nir_tex_instr *tex = nir_instr_as_tex(instr);
212 for (unsigned i = 0; i < tex->num_srcs; i++) {
213 unsigned num_components = nir_tex_instr_src_size(tex, i);
214 while (copy_prop_src(&tex->src[i].src, instr, NULL, num_components))
215 progress = true;
216 }
217
218 return progress;
219 }
220
221 case nir_instr_type_intrinsic: {
222 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
223 for (unsigned i = 0;
224 i < nir_intrinsic_infos[intrin->intrinsic].num_srcs; i++) {
225 unsigned num_components = nir_intrinsic_src_components(intrin, i);
226
227 while (copy_prop_src(&intrin->src[i], instr, NULL, num_components))
228 progress = true;
229 }
230
231 return progress;
232 }
233
234 case nir_instr_type_phi: {
235 nir_phi_instr *phi = nir_instr_as_phi(instr);
236 assert(phi->dest.is_ssa);
237 unsigned num_components = phi->dest.ssa.num_components;
238 nir_foreach_phi_src(src, phi) {
239 while (copy_prop_src(&src->src, instr, NULL, num_components))
240 progress = true;
241 }
242
243 return progress;
244 }
245
246 default:
247 return false;
248 }
249 }
250
251 static bool
252 copy_prop_if(nir_if *if_stmt)
253 {
254 return copy_prop_src(&if_stmt->condition, NULL, if_stmt, 1);
255 }
256
257 static bool
258 nir_copy_prop_impl(nir_function_impl *impl)
259 {
260 bool progress = false;
261
262 nir_foreach_block(block, impl) {
263 nir_foreach_instr(instr, block) {
264 if (copy_prop_instr(instr))
265 progress = true;
266 }
267
268 nir_if *if_stmt = nir_block_get_following_if(block);
269 if (if_stmt && copy_prop_if(if_stmt))
270 progress = true;
271 }
272
273 if (progress) {
274 nir_metadata_preserve(impl, nir_metadata_block_index |
275 nir_metadata_dominance);
276 } else {
277 #ifndef NDEBUG
278 impl->valid_metadata &= ~nir_metadata_not_properly_reset;
279 #endif
280 }
281
282 return progress;
283 }
284
285 bool
286 nir_copy_prop(nir_shader *shader)
287 {
288 bool progress = false;
289
290 nir_foreach_function(function, shader) {
291 if (function->impl && nir_copy_prop_impl(function->impl))
292 progress = true;
293 }
294
295 return progress;
296 }