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