nir: Pass fully qualified type to nir_const_value_negative_equal
[mesa.git] / src / compiler / nir / nir_instr_set.c
1 /*
2 * Copyright © 2014 Connor Abbott
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
24 #include "nir_instr_set.h"
25 #include "nir_vla.h"
26 #include "util/half_float.h"
27
28 #define HASH(hash, data) _mesa_fnv32_1a_accumulate((hash), (data))
29
30 static uint32_t
31 hash_src(uint32_t hash, const nir_src *src)
32 {
33 assert(src->is_ssa);
34 hash = HASH(hash, src->ssa);
35 return hash;
36 }
37
38 static uint32_t
39 hash_alu_src(uint32_t hash, const nir_alu_src *src, unsigned num_components)
40 {
41 hash = HASH(hash, src->abs);
42 hash = HASH(hash, src->negate);
43
44 for (unsigned i = 0; i < num_components; i++)
45 hash = HASH(hash, src->swizzle[i]);
46
47 hash = hash_src(hash, &src->src);
48 return hash;
49 }
50
51 static uint32_t
52 hash_alu(uint32_t hash, const nir_alu_instr *instr)
53 {
54 hash = HASH(hash, instr->op);
55
56 /* We explicitly don't hash instr->exact. */
57 uint8_t flags = instr->no_signed_wrap |
58 instr->no_unsigned_wrap << 1;
59 hash = HASH(hash, flags);
60
61 hash = HASH(hash, instr->dest.dest.ssa.num_components);
62 hash = HASH(hash, instr->dest.dest.ssa.bit_size);
63
64 if (nir_op_infos[instr->op].algebraic_properties & NIR_OP_IS_2SRC_COMMUTATIVE) {
65 assert(nir_op_infos[instr->op].num_inputs >= 2);
66
67 uint32_t hash0 = hash_alu_src(hash, &instr->src[0],
68 nir_ssa_alu_instr_src_components(instr, 0));
69 uint32_t hash1 = hash_alu_src(hash, &instr->src[1],
70 nir_ssa_alu_instr_src_components(instr, 1));
71 /* For commutative operations, we need some commutative way of
72 * combining the hashes. One option would be to XOR them but that
73 * means that anything with two identical sources will hash to 0 and
74 * that's common enough we probably don't want the guaranteed
75 * collision. Either addition or multiplication will also work.
76 */
77 hash = hash0 * hash1;
78
79 for (unsigned i = 2; i < nir_op_infos[instr->op].num_inputs; i++) {
80 hash = hash_alu_src(hash, &instr->src[i],
81 nir_ssa_alu_instr_src_components(instr, i));
82 }
83 } else {
84 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
85 hash = hash_alu_src(hash, &instr->src[i],
86 nir_ssa_alu_instr_src_components(instr, i));
87 }
88 }
89
90 return hash;
91 }
92
93 static uint32_t
94 hash_deref(uint32_t hash, const nir_deref_instr *instr)
95 {
96 hash = HASH(hash, instr->deref_type);
97 hash = HASH(hash, instr->mode);
98 hash = HASH(hash, instr->type);
99
100 if (instr->deref_type == nir_deref_type_var)
101 return HASH(hash, instr->var);
102
103 hash = hash_src(hash, &instr->parent);
104
105 switch (instr->deref_type) {
106 case nir_deref_type_struct:
107 hash = HASH(hash, instr->strct.index);
108 break;
109
110 case nir_deref_type_array:
111 case nir_deref_type_ptr_as_array:
112 hash = hash_src(hash, &instr->arr.index);
113 break;
114
115 case nir_deref_type_cast:
116 hash = HASH(hash, instr->cast.ptr_stride);
117 break;
118
119 case nir_deref_type_var:
120 case nir_deref_type_array_wildcard:
121 /* Nothing to do */
122 break;
123
124 default:
125 unreachable("Invalid instruction deref type");
126 }
127
128 return hash;
129 }
130
131 static uint32_t
132 hash_load_const(uint32_t hash, const nir_load_const_instr *instr)
133 {
134 hash = HASH(hash, instr->def.num_components);
135
136 if (instr->def.bit_size == 1) {
137 for (unsigned i = 0; i < instr->def.num_components; i++) {
138 uint8_t b = instr->value[i].b;
139 hash = HASH(hash, b);
140 }
141 } else {
142 unsigned size = instr->def.num_components * sizeof(*instr->value);
143 hash = _mesa_fnv32_1a_accumulate_block(hash, instr->value, size);
144 }
145
146 return hash;
147 }
148
149 static int
150 cmp_phi_src(const void *data1, const void *data2)
151 {
152 nir_phi_src *src1 = *(nir_phi_src **)data1;
153 nir_phi_src *src2 = *(nir_phi_src **)data2;
154 return src1->pred - src2->pred;
155 }
156
157 static uint32_t
158 hash_phi(uint32_t hash, const nir_phi_instr *instr)
159 {
160 hash = HASH(hash, instr->instr.block);
161
162 /* sort sources by predecessor, since the order shouldn't matter */
163 unsigned num_preds = instr->instr.block->predecessors->entries;
164 NIR_VLA(nir_phi_src *, srcs, num_preds);
165 unsigned i = 0;
166 nir_foreach_phi_src(src, instr) {
167 srcs[i++] = src;
168 }
169
170 qsort(srcs, num_preds, sizeof(nir_phi_src *), cmp_phi_src);
171
172 for (i = 0; i < num_preds; i++) {
173 hash = hash_src(hash, &srcs[i]->src);
174 hash = HASH(hash, srcs[i]->pred);
175 }
176
177 return hash;
178 }
179
180 static uint32_t
181 hash_intrinsic(uint32_t hash, const nir_intrinsic_instr *instr)
182 {
183 const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic];
184 hash = HASH(hash, instr->intrinsic);
185
186 if (info->has_dest) {
187 hash = HASH(hash, instr->dest.ssa.num_components);
188 hash = HASH(hash, instr->dest.ssa.bit_size);
189 }
190
191 hash = _mesa_fnv32_1a_accumulate_block(hash, instr->const_index,
192 info->num_indices
193 * sizeof(instr->const_index[0]));
194 return hash;
195 }
196
197 static uint32_t
198 hash_tex(uint32_t hash, const nir_tex_instr *instr)
199 {
200 hash = HASH(hash, instr->op);
201 hash = HASH(hash, instr->num_srcs);
202
203 for (unsigned i = 0; i < instr->num_srcs; i++) {
204 hash = HASH(hash, instr->src[i].src_type);
205 hash = hash_src(hash, &instr->src[i].src);
206 }
207
208 hash = HASH(hash, instr->coord_components);
209 hash = HASH(hash, instr->sampler_dim);
210 hash = HASH(hash, instr->is_array);
211 hash = HASH(hash, instr->is_shadow);
212 hash = HASH(hash, instr->is_new_style_shadow);
213 unsigned component = instr->component;
214 hash = HASH(hash, component);
215 for (unsigned i = 0; i < 4; ++i)
216 for (unsigned j = 0; j < 2; ++j)
217 hash = HASH(hash, instr->tg4_offsets[i][j]);
218 hash = HASH(hash, instr->texture_index);
219 hash = HASH(hash, instr->texture_array_size);
220 hash = HASH(hash, instr->sampler_index);
221
222 return hash;
223 }
224
225 /* Computes a hash of an instruction for use in a hash table. Note that this
226 * will only work for instructions where instr_can_rewrite() returns true, and
227 * it should return identical hashes for two instructions that are the same
228 * according nir_instrs_equal().
229 */
230
231 static uint32_t
232 hash_instr(const void *data)
233 {
234 const nir_instr *instr = data;
235 uint32_t hash = _mesa_fnv32_1a_offset_bias;
236
237 switch (instr->type) {
238 case nir_instr_type_alu:
239 hash = hash_alu(hash, nir_instr_as_alu(instr));
240 break;
241 case nir_instr_type_deref:
242 hash = hash_deref(hash, nir_instr_as_deref(instr));
243 break;
244 case nir_instr_type_load_const:
245 hash = hash_load_const(hash, nir_instr_as_load_const(instr));
246 break;
247 case nir_instr_type_phi:
248 hash = hash_phi(hash, nir_instr_as_phi(instr));
249 break;
250 case nir_instr_type_intrinsic:
251 hash = hash_intrinsic(hash, nir_instr_as_intrinsic(instr));
252 break;
253 case nir_instr_type_tex:
254 hash = hash_tex(hash, nir_instr_as_tex(instr));
255 break;
256 default:
257 unreachable("Invalid instruction type");
258 }
259
260 return hash;
261 }
262
263 bool
264 nir_srcs_equal(nir_src src1, nir_src src2)
265 {
266 if (src1.is_ssa) {
267 if (src2.is_ssa) {
268 return src1.ssa == src2.ssa;
269 } else {
270 return false;
271 }
272 } else {
273 if (src2.is_ssa) {
274 return false;
275 } else {
276 if ((src1.reg.indirect == NULL) != (src2.reg.indirect == NULL))
277 return false;
278
279 if (src1.reg.indirect) {
280 if (!nir_srcs_equal(*src1.reg.indirect, *src2.reg.indirect))
281 return false;
282 }
283
284 return src1.reg.reg == src2.reg.reg &&
285 src1.reg.base_offset == src2.reg.base_offset;
286 }
287 }
288 }
289
290 /**
291 * If the \p s is an SSA value that was generated by a negation instruction,
292 * that instruction is returned as a \c nir_alu_instr. Otherwise \c NULL is
293 * returned.
294 */
295 static nir_alu_instr *
296 get_neg_instr(nir_src s)
297 {
298 nir_alu_instr *alu = nir_src_as_alu_instr(s);
299
300 return alu != NULL && (alu->op == nir_op_fneg || alu->op == nir_op_ineg)
301 ? alu : NULL;
302 }
303
304 bool
305 nir_const_value_negative_equal(const nir_const_value *c1,
306 const nir_const_value *c2,
307 unsigned components,
308 nir_alu_type full_type)
309 {
310 assert(nir_alu_type_get_base_type(full_type) != nir_type_invalid);
311 assert(nir_alu_type_get_type_size(full_type) != 0);
312
313 switch (full_type) {
314 case nir_type_float16:
315 for (unsigned i = 0; i < components; i++) {
316 if (_mesa_half_to_float(c1[i].u16) !=
317 -_mesa_half_to_float(c2[i].u16)) {
318 return false;
319 }
320 }
321
322 return true;
323
324 case nir_type_float32:
325 for (unsigned i = 0; i < components; i++) {
326 if (c1[i].f32 != -c2[i].f32)
327 return false;
328 }
329
330 return true;
331
332 case nir_type_float64:
333 for (unsigned i = 0; i < components; i++) {
334 if (c1[i].f64 != -c2[i].f64)
335 return false;
336 }
337
338 return true;
339
340 case nir_type_int8:
341 case nir_type_uint8:
342 for (unsigned i = 0; i < components; i++) {
343 if (c1[i].i8 != -c2[i].i8)
344 return false;
345 }
346
347 return true;
348
349 case nir_type_int16:
350 case nir_type_uint16:
351 for (unsigned i = 0; i < components; i++) {
352 if (c1[i].i16 != -c2[i].i16)
353 return false;
354 }
355
356 return true;
357
358 case nir_type_int32:
359 case nir_type_uint32:
360 for (unsigned i = 0; i < components; i++) {
361 if (c1[i].i32 != -c2[i].i32)
362 return false;
363 }
364
365 return true;
366
367 case nir_type_int64:
368 case nir_type_uint64:
369 for (unsigned i = 0; i < components; i++) {
370 if (c1[i].i64 != -c2[i].i64)
371 return false;
372 }
373
374 return true;
375
376 default:
377 break;
378 }
379
380 return false;
381 }
382
383 /**
384 * Shallow compare of ALU srcs to determine if one is the negation of the other
385 *
386 * This function detects cases where \p alu1 is a constant and \p alu2 is a
387 * constant that is its negation. It will also detect cases where \p alu2 is
388 * an SSA value that is a \c nir_op_fneg applied to \p alu1 (and vice versa).
389 *
390 * This function does not detect the general case when \p alu1 and \p alu2 are
391 * SSA values that are the negations of each other (e.g., \p alu1 represents
392 * (a * b) and \p alu2 represents (-a * b)).
393 */
394 bool
395 nir_alu_srcs_negative_equal(const nir_alu_instr *alu1,
396 const nir_alu_instr *alu2,
397 unsigned src1, unsigned src2)
398 {
399 if (alu1->src[src1].abs != alu2->src[src2].abs)
400 return false;
401
402 bool parity = alu1->src[src1].negate != alu2->src[src2].negate;
403
404 /* Handling load_const instructions is tricky. */
405
406 const nir_const_value *const const1 =
407 nir_src_as_const_value(alu1->src[src1].src);
408
409 if (const1 != NULL) {
410 /* Assume that constant folding will eliminate source mods and unary
411 * ops.
412 */
413 if (parity)
414 return false;
415
416 const nir_const_value *const const2 =
417 nir_src_as_const_value(alu2->src[src2].src);
418
419 if (const2 == NULL)
420 return false;
421
422 if (nir_src_bit_size(alu1->src[src1].src) !=
423 nir_src_bit_size(alu2->src[src2].src))
424 return false;
425
426 /* FINISHME: Apply the swizzle? */
427 return nir_const_value_negative_equal(const1,
428 const2,
429 nir_ssa_alu_instr_src_components(alu1, src1),
430 nir_op_infos[alu1->op].input_types[src1] |
431 nir_src_bit_size(alu1->src[src1].src));
432 }
433
434 uint8_t alu1_swizzle[4] = {0};
435 nir_src alu1_actual_src;
436 nir_alu_instr *neg1 = get_neg_instr(alu1->src[src1].src);
437
438 if (neg1) {
439 parity = !parity;
440 alu1_actual_src = neg1->src[0].src;
441
442 for (unsigned i = 0; i < nir_ssa_alu_instr_src_components(neg1, 0); i++)
443 alu1_swizzle[i] = neg1->src[0].swizzle[i];
444 } else {
445 alu1_actual_src = alu1->src[src1].src;
446
447 for (unsigned i = 0; i < nir_ssa_alu_instr_src_components(alu1, src1); i++)
448 alu1_swizzle[i] = i;
449 }
450
451 uint8_t alu2_swizzle[4] = {0};
452 nir_src alu2_actual_src;
453 nir_alu_instr *neg2 = get_neg_instr(alu2->src[src2].src);
454
455 if (neg2) {
456 parity = !parity;
457 alu2_actual_src = neg2->src[0].src;
458
459 for (unsigned i = 0; i < nir_ssa_alu_instr_src_components(neg2, 0); i++)
460 alu2_swizzle[i] = neg2->src[0].swizzle[i];
461 } else {
462 alu2_actual_src = alu2->src[src2].src;
463
464 for (unsigned i = 0; i < nir_ssa_alu_instr_src_components(alu2, src2); i++)
465 alu2_swizzle[i] = i;
466 }
467
468 for (unsigned i = 0; i < nir_ssa_alu_instr_src_components(alu1, src1); i++) {
469 if (alu1_swizzle[alu1->src[src1].swizzle[i]] !=
470 alu2_swizzle[alu2->src[src2].swizzle[i]])
471 return false;
472 }
473
474 return parity && nir_srcs_equal(alu1_actual_src, alu2_actual_src);
475 }
476
477 bool
478 nir_alu_srcs_equal(const nir_alu_instr *alu1, const nir_alu_instr *alu2,
479 unsigned src1, unsigned src2)
480 {
481 if (alu1->src[src1].abs != alu2->src[src2].abs ||
482 alu1->src[src1].negate != alu2->src[src2].negate)
483 return false;
484
485 for (unsigned i = 0; i < nir_ssa_alu_instr_src_components(alu1, src1); i++) {
486 if (alu1->src[src1].swizzle[i] != alu2->src[src2].swizzle[i])
487 return false;
488 }
489
490 return nir_srcs_equal(alu1->src[src1].src, alu2->src[src2].src);
491 }
492
493 /* Returns "true" if two instructions are equal. Note that this will only
494 * work for the subset of instructions defined by instr_can_rewrite(). Also,
495 * it should only return "true" for instructions that hash_instr() will return
496 * the same hash for (ignoring collisions, of course).
497 */
498
499 static bool
500 nir_instrs_equal(const nir_instr *instr1, const nir_instr *instr2)
501 {
502 if (instr1->type != instr2->type)
503 return false;
504
505 switch (instr1->type) {
506 case nir_instr_type_alu: {
507 nir_alu_instr *alu1 = nir_instr_as_alu(instr1);
508 nir_alu_instr *alu2 = nir_instr_as_alu(instr2);
509
510 if (alu1->op != alu2->op)
511 return false;
512
513 /* We explicitly don't compare instr->exact. */
514
515 if (alu1->no_signed_wrap != alu2->no_signed_wrap)
516 return false;
517
518 if (alu1->no_unsigned_wrap != alu2->no_unsigned_wrap)
519 return false;
520
521 /* TODO: We can probably acutally do something more inteligent such
522 * as allowing different numbers and taking a maximum or something
523 * here */
524 if (alu1->dest.dest.ssa.num_components != alu2->dest.dest.ssa.num_components)
525 return false;
526
527 if (alu1->dest.dest.ssa.bit_size != alu2->dest.dest.ssa.bit_size)
528 return false;
529
530 if (nir_op_infos[alu1->op].algebraic_properties & NIR_OP_IS_2SRC_COMMUTATIVE) {
531 if ((!nir_alu_srcs_equal(alu1, alu2, 0, 0) ||
532 !nir_alu_srcs_equal(alu1, alu2, 1, 1)) &&
533 (!nir_alu_srcs_equal(alu1, alu2, 0, 1) ||
534 !nir_alu_srcs_equal(alu1, alu2, 1, 0)))
535 return false;
536
537 for (unsigned i = 2; i < nir_op_infos[alu1->op].num_inputs; i++) {
538 if (!nir_alu_srcs_equal(alu1, alu2, i, i))
539 return false;
540 }
541 } else {
542 for (unsigned i = 0; i < nir_op_infos[alu1->op].num_inputs; i++) {
543 if (!nir_alu_srcs_equal(alu1, alu2, i, i))
544 return false;
545 }
546 }
547 return true;
548 }
549 case nir_instr_type_deref: {
550 nir_deref_instr *deref1 = nir_instr_as_deref(instr1);
551 nir_deref_instr *deref2 = nir_instr_as_deref(instr2);
552
553 if (deref1->deref_type != deref2->deref_type ||
554 deref1->mode != deref2->mode ||
555 deref1->type != deref2->type)
556 return false;
557
558 if (deref1->deref_type == nir_deref_type_var)
559 return deref1->var == deref2->var;
560
561 if (!nir_srcs_equal(deref1->parent, deref2->parent))
562 return false;
563
564 switch (deref1->deref_type) {
565 case nir_deref_type_struct:
566 if (deref1->strct.index != deref2->strct.index)
567 return false;
568 break;
569
570 case nir_deref_type_array:
571 case nir_deref_type_ptr_as_array:
572 if (!nir_srcs_equal(deref1->arr.index, deref2->arr.index))
573 return false;
574 break;
575
576 case nir_deref_type_cast:
577 if (deref1->cast.ptr_stride != deref2->cast.ptr_stride)
578 return false;
579 break;
580
581 case nir_deref_type_var:
582 case nir_deref_type_array_wildcard:
583 /* Nothing to do */
584 break;
585
586 default:
587 unreachable("Invalid instruction deref type");
588 }
589 return true;
590 }
591 case nir_instr_type_tex: {
592 nir_tex_instr *tex1 = nir_instr_as_tex(instr1);
593 nir_tex_instr *tex2 = nir_instr_as_tex(instr2);
594
595 if (tex1->op != tex2->op)
596 return false;
597
598 if (tex1->num_srcs != tex2->num_srcs)
599 return false;
600 for (unsigned i = 0; i < tex1->num_srcs; i++) {
601 if (tex1->src[i].src_type != tex2->src[i].src_type ||
602 !nir_srcs_equal(tex1->src[i].src, tex2->src[i].src)) {
603 return false;
604 }
605 }
606
607 if (tex1->coord_components != tex2->coord_components ||
608 tex1->sampler_dim != tex2->sampler_dim ||
609 tex1->is_array != tex2->is_array ||
610 tex1->is_shadow != tex2->is_shadow ||
611 tex1->is_new_style_shadow != tex2->is_new_style_shadow ||
612 tex1->component != tex2->component ||
613 tex1->texture_index != tex2->texture_index ||
614 tex1->texture_array_size != tex2->texture_array_size ||
615 tex1->sampler_index != tex2->sampler_index) {
616 return false;
617 }
618
619 if (memcmp(tex1->tg4_offsets, tex2->tg4_offsets,
620 sizeof(tex1->tg4_offsets)))
621 return false;
622
623 return true;
624 }
625 case nir_instr_type_load_const: {
626 nir_load_const_instr *load1 = nir_instr_as_load_const(instr1);
627 nir_load_const_instr *load2 = nir_instr_as_load_const(instr2);
628
629 if (load1->def.num_components != load2->def.num_components)
630 return false;
631
632 if (load1->def.bit_size != load2->def.bit_size)
633 return false;
634
635 if (load1->def.bit_size == 1) {
636 for (unsigned i = 0; i < load1->def.num_components; ++i) {
637 if (load1->value[i].b != load2->value[i].b)
638 return false;
639 }
640 } else {
641 unsigned size = load1->def.num_components * sizeof(*load1->value);
642 if (memcmp(load1->value, load2->value, size) != 0)
643 return false;
644 }
645 return true;
646 }
647 case nir_instr_type_phi: {
648 nir_phi_instr *phi1 = nir_instr_as_phi(instr1);
649 nir_phi_instr *phi2 = nir_instr_as_phi(instr2);
650
651 if (phi1->instr.block != phi2->instr.block)
652 return false;
653
654 nir_foreach_phi_src(src1, phi1) {
655 nir_foreach_phi_src(src2, phi2) {
656 if (src1->pred == src2->pred) {
657 if (!nir_srcs_equal(src1->src, src2->src))
658 return false;
659
660 break;
661 }
662 }
663 }
664
665 return true;
666 }
667 case nir_instr_type_intrinsic: {
668 nir_intrinsic_instr *intrinsic1 = nir_instr_as_intrinsic(instr1);
669 nir_intrinsic_instr *intrinsic2 = nir_instr_as_intrinsic(instr2);
670 const nir_intrinsic_info *info =
671 &nir_intrinsic_infos[intrinsic1->intrinsic];
672
673 if (intrinsic1->intrinsic != intrinsic2->intrinsic ||
674 intrinsic1->num_components != intrinsic2->num_components)
675 return false;
676
677 if (info->has_dest && intrinsic1->dest.ssa.num_components !=
678 intrinsic2->dest.ssa.num_components)
679 return false;
680
681 if (info->has_dest && intrinsic1->dest.ssa.bit_size !=
682 intrinsic2->dest.ssa.bit_size)
683 return false;
684
685 for (unsigned i = 0; i < info->num_srcs; i++) {
686 if (!nir_srcs_equal(intrinsic1->src[i], intrinsic2->src[i]))
687 return false;
688 }
689
690 for (unsigned i = 0; i < info->num_indices; i++) {
691 if (intrinsic1->const_index[i] != intrinsic2->const_index[i])
692 return false;
693 }
694
695 return true;
696 }
697 case nir_instr_type_call:
698 case nir_instr_type_jump:
699 case nir_instr_type_ssa_undef:
700 case nir_instr_type_parallel_copy:
701 default:
702 unreachable("Invalid instruction type");
703 }
704
705 unreachable("All cases in the above switch should return");
706 }
707
708 static bool
709 src_is_ssa(nir_src *src, void *data)
710 {
711 (void) data;
712 return src->is_ssa;
713 }
714
715 static bool
716 dest_is_ssa(nir_dest *dest, void *data)
717 {
718 (void) data;
719 return dest->is_ssa;
720 }
721
722 static inline bool
723 instr_each_src_and_dest_is_ssa(nir_instr *instr)
724 {
725 if (!nir_foreach_dest(instr, dest_is_ssa, NULL) ||
726 !nir_foreach_src(instr, src_is_ssa, NULL))
727 return false;
728
729 return true;
730 }
731
732 /* This function determines if uses of an instruction can safely be rewritten
733 * to use another identical instruction instead. Note that this function must
734 * be kept in sync with hash_instr() and nir_instrs_equal() -- only
735 * instructions that pass this test will be handed on to those functions, and
736 * conversely they must handle everything that this function returns true for.
737 */
738
739 static bool
740 instr_can_rewrite(nir_instr *instr)
741 {
742 /* We only handle SSA. */
743 assert(instr_each_src_and_dest_is_ssa(instr));
744
745 switch (instr->type) {
746 case nir_instr_type_alu:
747 case nir_instr_type_deref:
748 case nir_instr_type_tex:
749 case nir_instr_type_load_const:
750 case nir_instr_type_phi:
751 return true;
752 case nir_instr_type_intrinsic:
753 return nir_intrinsic_can_reorder(nir_instr_as_intrinsic(instr));
754 case nir_instr_type_call:
755 case nir_instr_type_jump:
756 case nir_instr_type_ssa_undef:
757 return false;
758 case nir_instr_type_parallel_copy:
759 default:
760 unreachable("Invalid instruction type");
761 }
762
763 return false;
764 }
765
766 static nir_ssa_def *
767 nir_instr_get_dest_ssa_def(nir_instr *instr)
768 {
769 switch (instr->type) {
770 case nir_instr_type_alu:
771 assert(nir_instr_as_alu(instr)->dest.dest.is_ssa);
772 return &nir_instr_as_alu(instr)->dest.dest.ssa;
773 case nir_instr_type_deref:
774 assert(nir_instr_as_deref(instr)->dest.is_ssa);
775 return &nir_instr_as_deref(instr)->dest.ssa;
776 case nir_instr_type_load_const:
777 return &nir_instr_as_load_const(instr)->def;
778 case nir_instr_type_phi:
779 assert(nir_instr_as_phi(instr)->dest.is_ssa);
780 return &nir_instr_as_phi(instr)->dest.ssa;
781 case nir_instr_type_intrinsic:
782 assert(nir_instr_as_intrinsic(instr)->dest.is_ssa);
783 return &nir_instr_as_intrinsic(instr)->dest.ssa;
784 case nir_instr_type_tex:
785 assert(nir_instr_as_tex(instr)->dest.is_ssa);
786 return &nir_instr_as_tex(instr)->dest.ssa;
787 default:
788 unreachable("We never ask for any of these");
789 }
790 }
791
792 static bool
793 cmp_func(const void *data1, const void *data2)
794 {
795 return nir_instrs_equal(data1, data2);
796 }
797
798 struct set *
799 nir_instr_set_create(void *mem_ctx)
800 {
801 return _mesa_set_create(mem_ctx, hash_instr, cmp_func);
802 }
803
804 void
805 nir_instr_set_destroy(struct set *instr_set)
806 {
807 _mesa_set_destroy(instr_set, NULL);
808 }
809
810 bool
811 nir_instr_set_add_or_rewrite(struct set *instr_set, nir_instr *instr)
812 {
813 if (!instr_can_rewrite(instr))
814 return false;
815
816 struct set_entry *e = _mesa_set_search_or_add(instr_set, instr);
817 nir_instr *match = (nir_instr *) e->key;
818 if (match != instr) {
819 nir_ssa_def *def = nir_instr_get_dest_ssa_def(instr);
820 nir_ssa_def *new_def = nir_instr_get_dest_ssa_def(match);
821
822 /* It's safe to replace an exact instruction with an inexact one as
823 * long as we make it exact. If we got here, the two instructions are
824 * exactly identical in every other way so, once we've set the exact
825 * bit, they are the same.
826 */
827 if (instr->type == nir_instr_type_alu && nir_instr_as_alu(instr)->exact)
828 nir_instr_as_alu(match)->exact = true;
829
830 nir_ssa_def_rewrite_uses(def, nir_src_for_ssa(new_def));
831 return true;
832 }
833
834 return false;
835 }
836
837 void
838 nir_instr_set_remove(struct set *instr_set, nir_instr *instr)
839 {
840 if (!instr_can_rewrite(instr))
841 return;
842
843 struct set_entry *entry = _mesa_set_search(instr_set, instr);
844 if (entry)
845 _mesa_set_remove(instr_set, entry);
846 }
847