nir: Rename parallel_copy_copy to parallel_copy_entry and add a foreach macro
[mesa.git] / src / glsl / nir / nir_search.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 * Jason Ekstrand (jason@jlekstrand.net)
25 *
26 */
27
28 #include "nir_search.h"
29
30 struct match_state {
31 unsigned variables_seen;
32 nir_alu_src variables[NIR_SEARCH_MAX_VARIABLES];
33 };
34
35 static bool
36 match_expression(const nir_search_expression *expr, nir_alu_instr *instr,
37 unsigned num_components, const uint8_t *swizzle,
38 struct match_state *state);
39
40 static const uint8_t identity_swizzle[] = { 0, 1, 2, 3 };
41
42 static bool
43 match_value(const nir_search_value *value, nir_alu_instr *instr, unsigned src,
44 unsigned num_components, const uint8_t *swizzle,
45 struct match_state *state)
46 {
47 uint8_t new_swizzle[4];
48
49 for (int i = 0; i < num_components; ++i)
50 new_swizzle[i] = instr->src[src].swizzle[swizzle[i]];
51
52 switch (value->type) {
53 case nir_search_value_expression:
54 if (!instr->src[src].src.is_ssa)
55 return false;
56
57 if (instr->src[src].src.ssa->parent_instr->type != nir_instr_type_alu)
58 return false;
59
60 return match_expression(nir_search_value_as_expression(value),
61 nir_instr_as_alu(instr->src[src].src.ssa->parent_instr),
62 num_components, new_swizzle, state);
63
64 case nir_search_value_variable: {
65 nir_search_variable *var = nir_search_value_as_variable(value);
66
67 if (state->variables_seen & (1 << var->variable)) {
68 if (!nir_srcs_equal(state->variables[var->variable].src,
69 instr->src[src].src))
70 return false;
71
72 assert(!instr->src[src].abs && !instr->src[src].negate);
73
74 for (int i = 0; i < num_components; ++i) {
75 if (state->variables[var->variable].swizzle[i] != new_swizzle[i])
76 return false;
77 }
78
79 return true;
80 } else {
81 state->variables_seen |= (1 << var->variable);
82 state->variables[var->variable].src = instr->src[src].src;
83 state->variables[var->variable].abs = false;
84 state->variables[var->variable].negate = false;
85
86 for (int i = 0; i < 4; ++i) {
87 if (i < num_components)
88 state->variables[var->variable].swizzle[i] = new_swizzle[i];
89 else
90 state->variables[var->variable].swizzle[i] = 0;
91 }
92
93 return true;
94 }
95 }
96
97 case nir_search_value_constant: {
98 nir_search_constant *const_val = nir_search_value_as_constant(value);
99
100 if (!instr->src[src].src.is_ssa)
101 return false;
102
103 if (instr->src[src].src.ssa->parent_instr->type != nir_instr_type_load_const)
104 return false;
105
106 nir_load_const_instr *load =
107 nir_instr_as_load_const(instr->src[src].src.ssa->parent_instr);
108
109 switch (nir_op_infos[instr->op].input_types[src]) {
110 case nir_type_float:
111 for (unsigned i = 0; i < num_components; ++i) {
112 if (load->value.f[new_swizzle[i]] != const_val->data.f)
113 return false;
114 }
115 return true;
116 case nir_type_int:
117 case nir_type_unsigned:
118 case nir_type_bool:
119 for (unsigned i = 0; i < num_components; ++i) {
120 if (load->value.i[new_swizzle[i]] != const_val->data.i)
121 return false;
122 }
123 return true;
124 default:
125 unreachable("Invalid alu source type");
126 }
127 }
128
129 default:
130 unreachable("Invalid search value type");
131 }
132 }
133
134 static bool
135 match_expression(const nir_search_expression *expr, nir_alu_instr *instr,
136 unsigned num_components, const uint8_t *swizzle,
137 struct match_state *state)
138 {
139 if (instr->op != expr->opcode)
140 return false;
141
142 assert(!instr->dest.saturate);
143 assert(nir_op_infos[instr->op].num_inputs > 0);
144
145 /* If we have an explicitly sized destination, we can only handle the
146 * identity swizzle. While dot(vec3(a, b, c).zxy) is a valid
147 * expression, we don't have the information right now to propagate that
148 * swizzle through. We can only properly propagate swizzles if the
149 * instruction is vectorized.
150 */
151 if (nir_op_infos[instr->op].output_size != 0) {
152 for (unsigned i = 0; i < num_components; i++) {
153 if (swizzle[i] != i)
154 return false;
155 }
156 }
157
158 bool matched = true;
159 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
160 /* If the source is an explicitly sized source, then we need to reset
161 * both the number of components and the swizzle.
162 */
163 if (nir_op_infos[instr->op].input_sizes[i] != 0) {
164 num_components = nir_op_infos[instr->op].input_sizes[i];
165 swizzle = identity_swizzle;
166 }
167
168 if (!match_value(expr->srcs[i], instr, i, num_components,
169 swizzle, state)) {
170 matched = false;
171 break;
172 }
173 }
174
175 if (matched)
176 return true;
177
178 if (nir_op_infos[instr->op].num_inputs == 2 &&
179 (nir_op_infos[instr->op].algebraic_properties & NIR_OP_IS_COMMUTATIVE)) {
180 if (!match_value(expr->srcs[0], instr, 1, num_components,
181 swizzle, state))
182 return false;
183
184 return match_value(expr->srcs[1], instr, 0, num_components,
185 swizzle, state);
186 } else {
187 return false;
188 }
189 }
190
191 static nir_alu_src
192 construct_value(const nir_search_value *value, nir_alu_type type,
193 unsigned num_components, struct match_state *state,
194 nir_instr *instr, void *mem_ctx)
195 {
196 switch (value->type) {
197 case nir_search_value_expression: {
198 const nir_search_expression *expr = nir_search_value_as_expression(value);
199
200 if (nir_op_infos[expr->opcode].output_size != 0)
201 num_components = nir_op_infos[expr->opcode].output_size;
202
203 nir_alu_instr *alu = nir_alu_instr_create(mem_ctx, expr->opcode);
204 alu->dest.dest.is_ssa = true;
205 nir_ssa_def_init(&alu->instr, &alu->dest.dest.ssa, num_components, NULL);
206 alu->dest.write_mask = (1 << num_components) - 1;
207 alu->dest.saturate = false;
208
209 for (unsigned i = 0; i < nir_op_infos[expr->opcode].num_inputs; i++) {
210 /* If the source is an explicitly sized source, then we need to reset
211 * the number of components to match.
212 */
213 if (nir_op_infos[alu->op].input_sizes[i] != 0)
214 num_components = nir_op_infos[alu->op].input_sizes[i];
215
216 alu->src[i] = construct_value(expr->srcs[i],
217 nir_op_infos[alu->op].input_types[i],
218 num_components,
219 state, instr, mem_ctx);
220 }
221
222 nir_instr_insert_before(instr, &alu->instr);
223
224 nir_alu_src val = {
225 .src.is_ssa = true,
226 .src.ssa = &alu->dest.dest.ssa,
227 .negate = false,
228 .abs = false,
229 .swizzle = { 0, 1, 2, 3 }
230 };
231
232 return val;
233 }
234
235 case nir_search_value_variable: {
236 const nir_search_variable *var = nir_search_value_as_variable(value);
237 assert(state->variables_seen & (1 << var->variable));
238
239 nir_alu_src val = state->variables[var->variable];
240 val.src = nir_src_copy(val.src, mem_ctx);
241
242 return val;
243 }
244
245 case nir_search_value_constant: {
246 const nir_search_constant *c = nir_search_value_as_constant(value);
247 nir_load_const_instr *load = nir_load_const_instr_create(mem_ctx, 1);
248
249 switch (type) {
250 case nir_type_float:
251 load->def.name = ralloc_asprintf(mem_ctx, "%f", c->data.f);
252 load->value.f[0] = c->data.f;
253 break;
254 case nir_type_int:
255 load->def.name = ralloc_asprintf(mem_ctx, "%d", c->data.i);
256 load->value.i[0] = c->data.i;
257 break;
258 case nir_type_unsigned:
259 case nir_type_bool:
260 load->value.u[0] = c->data.u;
261 break;
262 default:
263 unreachable("Invalid alu source type");
264 }
265
266 nir_instr_insert_before(instr, &load->instr);
267
268 nir_alu_src val = {
269 .src.is_ssa = true,
270 .src.ssa = &load->def,
271 .negate = false,
272 .abs = false,
273 .swizzle = { 0, 0, 0, 0 } /* Splatted scalar */
274 };
275
276 return val;
277 }
278
279 default:
280 unreachable("Invalid search value type");
281 }
282 }
283
284 nir_alu_instr *
285 nir_replace_instr(nir_alu_instr *instr, const nir_search_expression *search,
286 const nir_search_value *replace, void *mem_ctx)
287 {
288 uint8_t swizzle[4] = { 0, 0, 0, 0 };
289
290 for (unsigned i = 0; i < instr->dest.dest.ssa.num_components; ++i)
291 swizzle[i] = i;
292
293 assert(instr->dest.dest.is_ssa);
294
295 struct match_state state;
296 state.variables_seen = 0;
297
298 if (!match_expression(search, instr, instr->dest.dest.ssa.num_components,
299 swizzle, &state))
300 return NULL;
301
302 /* Inserting a mov may be unnecessary. However, it's much easier to
303 * simply let copy propagation clean this up than to try to go through
304 * and rewrite swizzles ourselves.
305 */
306 nir_alu_instr *mov = nir_alu_instr_create(mem_ctx, nir_op_imov);
307 mov->dest.write_mask = instr->dest.write_mask;
308 mov->dest.dest.is_ssa = true;
309 nir_ssa_def_init(&mov->instr, &mov->dest.dest.ssa,
310 instr->dest.dest.ssa.num_components, NULL);
311
312 mov->src[0] = construct_value(replace, nir_op_infos[instr->op].output_type,
313 instr->dest.dest.ssa.num_components, &state,
314 &instr->instr, mem_ctx);
315 nir_instr_insert_before(&instr->instr, &mov->instr);
316
317 nir_src replace_src = {
318 .is_ssa = true,
319 .ssa = &mov->dest.dest.ssa,
320 };
321
322 nir_ssa_def_rewrite_uses(&instr->dest.dest.ssa, replace_src, mem_ctx);
323
324 /* We know this one has no more uses because we just rewrote them all,
325 * so we can remove it. The rest of the matched expression, however, we
326 * don't know so much about. We'll just let dead code clean them up.
327 */
328 nir_instr_remove(&instr->instr);
329
330 return mov;
331 }