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