nir/algebraic: Make algebraic_parser_test.sh executable.
[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 <main/imports.h>
30
31 /**
32 * SSA-based copy propagation
33 */
34
35 static bool is_move(nir_alu_instr *instr)
36 {
37 if (instr->op != nir_op_fmov &&
38 instr->op != nir_op_imov)
39 return false;
40
41 if (instr->dest.saturate)
42 return false;
43
44 /* we handle modifiers in a separate pass */
45
46 if (instr->src[0].abs || instr->src[0].negate)
47 return false;
48
49 if (!instr->src[0].src.is_ssa)
50 return false;
51
52 return true;
53
54 }
55
56 static bool is_vec(nir_alu_instr *instr)
57 {
58 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
59 if (!instr->src[i].src.is_ssa)
60 return false;
61
62 /* we handle modifiers in a separate pass */
63 if (instr->src[i].abs || instr->src[i].negate)
64 return false;
65 }
66
67 return instr->op == nir_op_vec2 ||
68 instr->op == nir_op_vec3 ||
69 instr->op == nir_op_vec4;
70 }
71
72 static bool
73 is_swizzleless_move(nir_alu_instr *instr)
74 {
75 if (is_move(instr)) {
76 for (unsigned i = 0; i < 4; i++) {
77 if (!((instr->dest.write_mask >> i) & 1))
78 break;
79 if (instr->src[0].swizzle[i] != i)
80 return false;
81 }
82 return true;
83 } else if (is_vec(instr)) {
84 nir_ssa_def *def = NULL;
85 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
86 if (instr->src[i].swizzle[0] != i)
87 return false;
88
89 if (def == NULL) {
90 def = instr->src[i].src.ssa;
91 } else if (instr->src[i].src.ssa != def) {
92 return false;
93 }
94 }
95 return true;
96 } else {
97 return false;
98 }
99 }
100
101 static bool
102 is_trivial_deref_cast(nir_deref_instr *cast)
103 {
104 if (cast->deref_type != nir_deref_type_cast)
105 return false;
106
107 nir_deref_instr *parent = nir_src_as_deref(cast->parent);
108 if (!parent)
109 return false;
110
111 return cast->mode == parent->mode &&
112 cast->type == parent->type &&
113 cast->dest.ssa.num_components == parent->dest.ssa.num_components &&
114 cast->dest.ssa.bit_size == parent->dest.ssa.bit_size;
115 }
116
117 static bool
118 copy_prop_src(nir_src *src, nir_instr *parent_instr, nir_if *parent_if,
119 unsigned num_components)
120 {
121 if (!src->is_ssa) {
122 if (src->reg.indirect)
123 return copy_prop_src(src->reg.indirect, parent_instr, parent_if, 1);
124 return false;
125 }
126
127 nir_instr *src_instr = src->ssa->parent_instr;
128 nir_ssa_def *copy_def;
129 if (src_instr->type == nir_instr_type_alu) {
130 nir_alu_instr *alu_instr = nir_instr_as_alu(src_instr);
131 if (!is_swizzleless_move(alu_instr))
132 return false;
133
134 if (alu_instr->src[0].src.ssa->num_components != num_components)
135 return false;
136
137 copy_def= alu_instr->src[0].src.ssa;
138 } else if (src_instr->type == nir_instr_type_deref) {
139 nir_deref_instr *deref_instr = nir_instr_as_deref(src_instr);
140 if (!is_trivial_deref_cast(deref_instr))
141 return false;
142
143 copy_def = deref_instr->parent.ssa;
144 } else {
145 return false;
146 }
147
148 if (parent_instr) {
149 nir_instr_rewrite_src(parent_instr, src, nir_src_for_ssa(copy_def));
150 } else {
151 assert(src == &parent_if->condition);
152 nir_if_rewrite_condition(parent_if, nir_src_for_ssa(copy_def));
153 }
154
155 return true;
156 }
157
158 static bool
159 copy_prop_alu_src(nir_alu_instr *parent_alu_instr, unsigned index)
160 {
161 nir_alu_src *src = &parent_alu_instr->src[index];
162 if (!src->src.is_ssa) {
163 if (src->src.reg.indirect)
164 return copy_prop_src(src->src.reg.indirect, &parent_alu_instr->instr,
165 NULL, 1);
166 return false;
167 }
168
169 nir_instr *src_instr = src->src.ssa->parent_instr;
170 if (src_instr->type != nir_instr_type_alu)
171 return false;
172
173 nir_alu_instr *alu_instr = nir_instr_as_alu(src_instr);
174 if (!is_move(alu_instr) && !is_vec(alu_instr))
175 return false;
176
177 nir_ssa_def *def;
178 unsigned new_swizzle[4] = {0, 0, 0, 0};
179
180 if (alu_instr->op == nir_op_fmov ||
181 alu_instr->op == nir_op_imov) {
182 for (unsigned i = 0; i < 4; i++)
183 new_swizzle[i] = alu_instr->src[0].swizzle[src->swizzle[i]];
184 def = alu_instr->src[0].src.ssa;
185 } else {
186 def = NULL;
187
188 for (unsigned i = 0; i < 4; i++) {
189 if (!nir_alu_instr_channel_used(parent_alu_instr, index, i))
190 continue;
191
192 nir_ssa_def *new_def = alu_instr->src[src->swizzle[i]].src.ssa;
193 if (def == NULL)
194 def = new_def;
195 else {
196 if (def != new_def)
197 return false;
198 }
199 new_swizzle[i] = alu_instr->src[src->swizzle[i]].swizzle[0];
200 }
201 }
202
203 for (unsigned i = 0; i < 4; i++)
204 src->swizzle[i] = new_swizzle[i];
205
206 nir_instr_rewrite_src(&parent_alu_instr->instr, &src->src,
207 nir_src_for_ssa(def));
208
209 return true;
210 }
211
212 static bool
213 copy_prop_dest(nir_dest *dest, nir_instr *instr)
214 {
215 if (!dest->is_ssa && dest->reg.indirect)
216 return copy_prop_src(dest->reg.indirect, instr, NULL, 1);
217
218 return false;
219 }
220
221 static bool
222 copy_prop_instr(nir_instr *instr)
223 {
224 bool progress = false;
225 switch (instr->type) {
226 case nir_instr_type_alu: {
227 nir_alu_instr *alu_instr = nir_instr_as_alu(instr);
228
229 for (unsigned i = 0; i < nir_op_infos[alu_instr->op].num_inputs; i++)
230 while (copy_prop_alu_src(alu_instr, i))
231 progress = true;
232
233 while (copy_prop_dest(&alu_instr->dest.dest, instr))
234 progress = true;
235
236 return progress;
237 }
238
239 case nir_instr_type_deref: {
240 nir_deref_instr *deref = nir_instr_as_deref(instr);
241
242 if (deref->deref_type != nir_deref_type_var) {
243 assert(deref->dest.is_ssa);
244 const unsigned comps = deref->dest.ssa.num_components;
245 while (copy_prop_src(&deref->parent, instr, NULL, comps))
246 progress = true;
247 }
248
249 if (deref->deref_type == nir_deref_type_array) {
250 while (copy_prop_src(&deref->arr.index, instr, NULL, 1))
251 progress = true;
252 }
253
254 return progress;
255 }
256
257 case nir_instr_type_tex: {
258 nir_tex_instr *tex = nir_instr_as_tex(instr);
259 for (unsigned i = 0; i < tex->num_srcs; i++) {
260 unsigned num_components = nir_tex_instr_src_size(tex, i);
261 while (copy_prop_src(&tex->src[i].src, instr, NULL, num_components))
262 progress = true;
263 }
264
265 while (copy_prop_dest(&tex->dest, instr))
266 progress = true;
267
268 return progress;
269 }
270
271 case nir_instr_type_intrinsic: {
272 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
273 for (unsigned i = 0;
274 i < nir_intrinsic_infos[intrin->intrinsic].num_srcs; i++) {
275 unsigned num_components = nir_intrinsic_src_components(intrin, i);
276
277 while (copy_prop_src(&intrin->src[i], instr, NULL, num_components))
278 progress = true;
279 }
280
281 if (nir_intrinsic_infos[intrin->intrinsic].has_dest) {
282 while (copy_prop_dest(&intrin->dest, instr))
283 progress = true;
284 }
285
286 return progress;
287 }
288
289 case nir_instr_type_phi: {
290 nir_phi_instr *phi = nir_instr_as_phi(instr);
291 assert(phi->dest.is_ssa);
292 unsigned num_components = phi->dest.ssa.num_components;
293 nir_foreach_phi_src(src, phi) {
294 while (copy_prop_src(&src->src, instr, NULL, num_components))
295 progress = true;
296 }
297
298 return progress;
299 }
300
301 default:
302 return false;
303 }
304 }
305
306 static bool
307 copy_prop_if(nir_if *if_stmt)
308 {
309 return copy_prop_src(&if_stmt->condition, NULL, if_stmt, 1);
310 }
311
312 static bool
313 nir_copy_prop_impl(nir_function_impl *impl)
314 {
315 bool progress = false;
316
317 nir_foreach_block(block, impl) {
318 nir_foreach_instr(instr, block) {
319 if (copy_prop_instr(instr))
320 progress = true;
321 }
322
323 nir_if *if_stmt = nir_block_get_following_if(block);
324 if (if_stmt && copy_prop_if(if_stmt))
325 progress = true;
326 }
327
328 if (progress) {
329 nir_metadata_preserve(impl, nir_metadata_block_index |
330 nir_metadata_dominance);
331 }
332
333 return progress;
334 }
335
336 bool
337 nir_copy_prop(nir_shader *shader)
338 {
339 bool progress = false;
340
341 nir_foreach_function(function, shader) {
342 if (function->impl && nir_copy_prop_impl(function->impl))
343 progress = true;
344 }
345
346 return progress;
347 }