nir: Use pointers for nir_src_copy and nir_dest_copy
[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 nir_ssa_dest_init(&alu->instr, &alu->dest.dest, num_components, NULL);
205 alu->dest.write_mask = (1 << num_components) - 1;
206 alu->dest.saturate = false;
207
208 for (unsigned i = 0; i < nir_op_infos[expr->opcode].num_inputs; i++) {
209 /* If the source is an explicitly sized source, then we need to reset
210 * the number of components to match.
211 */
212 if (nir_op_infos[alu->op].input_sizes[i] != 0)
213 num_components = nir_op_infos[alu->op].input_sizes[i];
214
215 alu->src[i] = construct_value(expr->srcs[i],
216 nir_op_infos[alu->op].input_types[i],
217 num_components,
218 state, instr, mem_ctx);
219 }
220
221 nir_instr_insert_before(instr, &alu->instr);
222
223 nir_alu_src val;
224 val.src = nir_src_for_ssa(&alu->dest.dest.ssa);
225 val.negate = false;
226 val.abs = false,
227 memcpy(val.swizzle, identity_swizzle, sizeof val.swizzle);
228
229 return val;
230 }
231
232 case nir_search_value_variable: {
233 const nir_search_variable *var = nir_search_value_as_variable(value);
234 assert(state->variables_seen & (1 << var->variable));
235
236 nir_alu_src val;
237 nir_alu_src_copy(&val, &state->variables[var->variable], mem_ctx);
238
239 return val;
240 }
241
242 case nir_search_value_constant: {
243 const nir_search_constant *c = nir_search_value_as_constant(value);
244 nir_load_const_instr *load = nir_load_const_instr_create(mem_ctx, 1);
245
246 switch (type) {
247 case nir_type_float:
248 load->def.name = ralloc_asprintf(mem_ctx, "%f", c->data.f);
249 load->value.f[0] = c->data.f;
250 break;
251 case nir_type_int:
252 load->def.name = ralloc_asprintf(mem_ctx, "%d", c->data.i);
253 load->value.i[0] = c->data.i;
254 break;
255 case nir_type_unsigned:
256 case nir_type_bool:
257 load->value.u[0] = c->data.u;
258 break;
259 default:
260 unreachable("Invalid alu source type");
261 }
262
263 nir_instr_insert_before(instr, &load->instr);
264
265 nir_alu_src val;
266 val.src = nir_src_for_ssa(&load->def);
267 val.negate = false;
268 val.abs = false,
269 memset(val.swizzle, 0, sizeof val.swizzle);
270
271 return val;
272 }
273
274 default:
275 unreachable("Invalid search value type");
276 }
277 }
278
279 nir_alu_instr *
280 nir_replace_instr(nir_alu_instr *instr, const nir_search_expression *search,
281 const nir_search_value *replace, void *mem_ctx)
282 {
283 uint8_t swizzle[4] = { 0, 0, 0, 0 };
284
285 for (unsigned i = 0; i < instr->dest.dest.ssa.num_components; ++i)
286 swizzle[i] = i;
287
288 assert(instr->dest.dest.is_ssa);
289
290 struct match_state state;
291 state.variables_seen = 0;
292
293 if (!match_expression(search, instr, instr->dest.dest.ssa.num_components,
294 swizzle, &state))
295 return NULL;
296
297 /* Inserting a mov may be unnecessary. However, it's much easier to
298 * simply let copy propagation clean this up than to try to go through
299 * and rewrite swizzles ourselves.
300 */
301 nir_alu_instr *mov = nir_alu_instr_create(mem_ctx, nir_op_imov);
302 mov->dest.write_mask = instr->dest.write_mask;
303 nir_ssa_dest_init(&mov->instr, &mov->dest.dest,
304 instr->dest.dest.ssa.num_components, NULL);
305
306 mov->src[0] = construct_value(replace, nir_op_infos[instr->op].output_type,
307 instr->dest.dest.ssa.num_components, &state,
308 &instr->instr, mem_ctx);
309 nir_instr_insert_before(&instr->instr, &mov->instr);
310
311 nir_ssa_def_rewrite_uses(&instr->dest.dest.ssa,
312 nir_src_for_ssa(&mov->dest.dest.ssa), mem_ctx);
313
314 /* We know this one has no more uses because we just rewrote them all,
315 * so we can remove it. The rest of the matched expression, however, we
316 * don't know so much about. We'll just let dead code clean them up.
317 */
318 nir_instr_remove(&instr->instr);
319
320 return mov;
321 }