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