2 * Copyright © 2014 Connor Abbott
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:
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
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
24 #include "nir_instr_set.h"
26 #include "util/half_float.h"
28 #define HASH(hash, data) _mesa_fnv32_1a_accumulate((hash), (data))
31 hash_src(uint32_t hash
, const nir_src
*src
)
34 hash
= HASH(hash
, src
->ssa
);
39 hash_alu_src(uint32_t hash
, const nir_alu_src
*src
, unsigned num_components
)
41 hash
= HASH(hash
, src
->abs
);
42 hash
= HASH(hash
, src
->negate
);
44 for (unsigned i
= 0; i
< num_components
; i
++)
45 hash
= HASH(hash
, src
->swizzle
[i
]);
47 hash
= hash_src(hash
, &src
->src
);
52 hash_alu(uint32_t hash
, const nir_alu_instr
*instr
)
54 hash
= HASH(hash
, instr
->op
);
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
);
61 hash
= HASH(hash
, instr
->dest
.dest
.ssa
.num_components
);
62 hash
= HASH(hash
, instr
->dest
.dest
.ssa
.bit_size
);
64 if (nir_op_infos
[instr
->op
].algebraic_properties
& NIR_OP_IS_2SRC_COMMUTATIVE
) {
65 assert(nir_op_infos
[instr
->op
].num_inputs
>= 2);
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.
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
));
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
));
94 hash_deref(uint32_t hash
, const nir_deref_instr
*instr
)
96 hash
= HASH(hash
, instr
->deref_type
);
97 hash
= HASH(hash
, instr
->mode
);
98 hash
= HASH(hash
, instr
->type
);
100 if (instr
->deref_type
== nir_deref_type_var
)
101 return HASH(hash
, instr
->var
);
103 hash
= hash_src(hash
, &instr
->parent
);
105 switch (instr
->deref_type
) {
106 case nir_deref_type_struct
:
107 hash
= HASH(hash
, instr
->strct
.index
);
110 case nir_deref_type_array
:
111 case nir_deref_type_ptr_as_array
:
112 hash
= hash_src(hash
, &instr
->arr
.index
);
115 case nir_deref_type_cast
:
116 hash
= HASH(hash
, instr
->cast
.ptr_stride
);
119 case nir_deref_type_var
:
120 case nir_deref_type_array_wildcard
:
125 unreachable("Invalid instruction deref type");
132 hash_load_const(uint32_t hash
, const nir_load_const_instr
*instr
)
134 hash
= HASH(hash
, instr
->def
.num_components
);
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
);
142 unsigned size
= instr
->def
.num_components
* sizeof(*instr
->value
);
143 hash
= _mesa_fnv32_1a_accumulate_block(hash
, instr
->value
, size
);
150 cmp_phi_src(const void *data1
, const void *data2
)
152 nir_phi_src
*src1
= *(nir_phi_src
**)data1
;
153 nir_phi_src
*src2
= *(nir_phi_src
**)data2
;
154 return src1
->pred
- src2
->pred
;
158 hash_phi(uint32_t hash
, const nir_phi_instr
*instr
)
160 hash
= HASH(hash
, instr
->instr
.block
);
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
);
166 nir_foreach_phi_src(src
, instr
) {
170 qsort(srcs
, num_preds
, sizeof(nir_phi_src
*), cmp_phi_src
);
172 for (i
= 0; i
< num_preds
; i
++) {
173 hash
= hash_src(hash
, &srcs
[i
]->src
);
174 hash
= HASH(hash
, srcs
[i
]->pred
);
181 hash_intrinsic(uint32_t hash
, const nir_intrinsic_instr
*instr
)
183 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[instr
->intrinsic
];
184 hash
= HASH(hash
, instr
->intrinsic
);
186 if (info
->has_dest
) {
187 hash
= HASH(hash
, instr
->dest
.ssa
.num_components
);
188 hash
= HASH(hash
, instr
->dest
.ssa
.bit_size
);
191 hash
= _mesa_fnv32_1a_accumulate_block(hash
, instr
->const_index
,
193 * sizeof(instr
->const_index
[0]));
198 hash_tex(uint32_t hash
, const nir_tex_instr
*instr
)
200 hash
= HASH(hash
, instr
->op
);
201 hash
= HASH(hash
, instr
->num_srcs
);
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
);
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
);
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().
232 hash_instr(const void *data
)
234 const nir_instr
*instr
= data
;
235 uint32_t hash
= _mesa_fnv32_1a_offset_bias
;
237 switch (instr
->type
) {
238 case nir_instr_type_alu
:
239 hash
= hash_alu(hash
, nir_instr_as_alu(instr
));
241 case nir_instr_type_deref
:
242 hash
= hash_deref(hash
, nir_instr_as_deref(instr
));
244 case nir_instr_type_load_const
:
245 hash
= hash_load_const(hash
, nir_instr_as_load_const(instr
));
247 case nir_instr_type_phi
:
248 hash
= hash_phi(hash
, nir_instr_as_phi(instr
));
250 case nir_instr_type_intrinsic
:
251 hash
= hash_intrinsic(hash
, nir_instr_as_intrinsic(instr
));
253 case nir_instr_type_tex
:
254 hash
= hash_tex(hash
, nir_instr_as_tex(instr
));
257 unreachable("Invalid instruction type");
264 nir_srcs_equal(nir_src src1
, nir_src src2
)
268 return src1
.ssa
== src2
.ssa
;
276 if ((src1
.reg
.indirect
== NULL
) != (src2
.reg
.indirect
== NULL
))
279 if (src1
.reg
.indirect
) {
280 if (!nir_srcs_equal(*src1
.reg
.indirect
, *src2
.reg
.indirect
))
284 return src1
.reg
.reg
== src2
.reg
.reg
&&
285 src1
.reg
.base_offset
== src2
.reg
.base_offset
;
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
295 static nir_alu_instr
*
296 get_neg_instr(nir_src s
)
298 nir_alu_instr
*alu
= nir_src_as_alu_instr(s
);
300 return alu
!= NULL
&& (alu
->op
== nir_op_fneg
|| alu
->op
== nir_op_ineg
)
305 nir_const_value_negative_equal(nir_const_value c1
,
307 nir_alu_type full_type
)
309 assert(nir_alu_type_get_base_type(full_type
) != nir_type_invalid
);
310 assert(nir_alu_type_get_type_size(full_type
) != 0);
313 case nir_type_float16
:
314 return _mesa_half_to_float(c1
.u16
) == -_mesa_half_to_float(c2
.u16
);
316 case nir_type_float32
:
317 return c1
.f32
== -c2
.f32
;
319 case nir_type_float64
:
320 return c1
.f64
== -c2
.f64
;
324 return c1
.i8
== -c2
.i8
;
327 case nir_type_uint16
:
328 return c1
.i16
== -c2
.i16
;
331 case nir_type_uint32
:
332 return c1
.i32
== -c2
.i32
;
335 case nir_type_uint64
:
336 return c1
.i64
== -c2
.i64
;
346 * Shallow compare of ALU srcs to determine if one is the negation of the other
348 * This function detects cases where \p alu1 is a constant and \p alu2 is a
349 * constant that is its negation. It will also detect cases where \p alu2 is
350 * an SSA value that is a \c nir_op_fneg applied to \p alu1 (and vice versa).
352 * This function does not detect the general case when \p alu1 and \p alu2 are
353 * SSA values that are the negations of each other (e.g., \p alu1 represents
354 * (a * b) and \p alu2 represents (-a * b)).
357 * It is the responsibility of the caller to ensure that the component counts,
358 * write masks, and base types of the sources being compared are compatible.
361 nir_alu_srcs_negative_equal(const nir_alu_instr
*alu1
,
362 const nir_alu_instr
*alu2
,
363 unsigned src1
, unsigned src2
)
366 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; i
++) {
367 assert(nir_alu_instr_channel_used(alu1
, src1
, i
) ==
368 nir_alu_instr_channel_used(alu2
, src2
, i
));
371 if (nir_op_infos
[alu1
->op
].input_types
[src1
] == nir_type_float
) {
372 assert(nir_op_infos
[alu1
->op
].input_types
[src1
] ==
373 nir_op_infos
[alu2
->op
].input_types
[src2
]);
375 assert(nir_op_infos
[alu1
->op
].input_types
[src1
] == nir_type_int
);
376 assert(nir_op_infos
[alu2
->op
].input_types
[src2
] == nir_type_int
);
380 if (alu1
->src
[src1
].abs
!= alu2
->src
[src2
].abs
)
383 bool parity
= alu1
->src
[src1
].negate
!= alu2
->src
[src2
].negate
;
385 /* Handling load_const instructions is tricky. */
387 const nir_const_value
*const const1
=
388 nir_src_as_const_value(alu1
->src
[src1
].src
);
390 if (const1
!= NULL
) {
391 /* Assume that constant folding will eliminate source mods and unary
397 const nir_const_value
*const const2
=
398 nir_src_as_const_value(alu2
->src
[src2
].src
);
403 if (nir_src_bit_size(alu1
->src
[src1
].src
) !=
404 nir_src_bit_size(alu2
->src
[src2
].src
))
407 const nir_alu_type full_type
= nir_op_infos
[alu1
->op
].input_types
[src1
] |
408 nir_src_bit_size(alu1
->src
[src1
].src
);
409 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; i
++) {
410 if (nir_alu_instr_channel_used(alu1
, src1
, i
) &&
411 !nir_const_value_negative_equal(const1
[alu1
->src
[src1
].swizzle
[i
]],
412 const2
[alu2
->src
[src2
].swizzle
[i
]],
420 uint8_t alu1_swizzle
[4] = {0};
421 nir_src alu1_actual_src
;
422 nir_alu_instr
*neg1
= get_neg_instr(alu1
->src
[src1
].src
);
426 alu1_actual_src
= neg1
->src
[0].src
;
428 for (unsigned i
= 0; i
< nir_ssa_alu_instr_src_components(neg1
, 0); i
++)
429 alu1_swizzle
[i
] = neg1
->src
[0].swizzle
[i
];
431 alu1_actual_src
= alu1
->src
[src1
].src
;
433 for (unsigned i
= 0; i
< nir_ssa_alu_instr_src_components(alu1
, src1
); i
++)
437 uint8_t alu2_swizzle
[4] = {0};
438 nir_src alu2_actual_src
;
439 nir_alu_instr
*neg2
= get_neg_instr(alu2
->src
[src2
].src
);
443 alu2_actual_src
= neg2
->src
[0].src
;
445 for (unsigned i
= 0; i
< nir_ssa_alu_instr_src_components(neg2
, 0); i
++)
446 alu2_swizzle
[i
] = neg2
->src
[0].swizzle
[i
];
448 alu2_actual_src
= alu2
->src
[src2
].src
;
450 for (unsigned i
= 0; i
< nir_ssa_alu_instr_src_components(alu2
, src2
); i
++)
454 for (unsigned i
= 0; i
< nir_ssa_alu_instr_src_components(alu1
, src1
); i
++) {
455 if (alu1_swizzle
[alu1
->src
[src1
].swizzle
[i
]] !=
456 alu2_swizzle
[alu2
->src
[src2
].swizzle
[i
]])
460 return parity
&& nir_srcs_equal(alu1_actual_src
, alu2_actual_src
);
464 nir_alu_srcs_equal(const nir_alu_instr
*alu1
, const nir_alu_instr
*alu2
,
465 unsigned src1
, unsigned src2
)
467 if (alu1
->src
[src1
].abs
!= alu2
->src
[src2
].abs
||
468 alu1
->src
[src1
].negate
!= alu2
->src
[src2
].negate
)
471 for (unsigned i
= 0; i
< nir_ssa_alu_instr_src_components(alu1
, src1
); i
++) {
472 if (alu1
->src
[src1
].swizzle
[i
] != alu2
->src
[src2
].swizzle
[i
])
476 return nir_srcs_equal(alu1
->src
[src1
].src
, alu2
->src
[src2
].src
);
479 /* Returns "true" if two instructions are equal. Note that this will only
480 * work for the subset of instructions defined by instr_can_rewrite(). Also,
481 * it should only return "true" for instructions that hash_instr() will return
482 * the same hash for (ignoring collisions, of course).
486 nir_instrs_equal(const nir_instr
*instr1
, const nir_instr
*instr2
)
488 if (instr1
->type
!= instr2
->type
)
491 switch (instr1
->type
) {
492 case nir_instr_type_alu
: {
493 nir_alu_instr
*alu1
= nir_instr_as_alu(instr1
);
494 nir_alu_instr
*alu2
= nir_instr_as_alu(instr2
);
496 if (alu1
->op
!= alu2
->op
)
499 /* We explicitly don't compare instr->exact. */
501 if (alu1
->no_signed_wrap
!= alu2
->no_signed_wrap
)
504 if (alu1
->no_unsigned_wrap
!= alu2
->no_unsigned_wrap
)
507 /* TODO: We can probably acutally do something more inteligent such
508 * as allowing different numbers and taking a maximum or something
510 if (alu1
->dest
.dest
.ssa
.num_components
!= alu2
->dest
.dest
.ssa
.num_components
)
513 if (alu1
->dest
.dest
.ssa
.bit_size
!= alu2
->dest
.dest
.ssa
.bit_size
)
516 if (nir_op_infos
[alu1
->op
].algebraic_properties
& NIR_OP_IS_2SRC_COMMUTATIVE
) {
517 if ((!nir_alu_srcs_equal(alu1
, alu2
, 0, 0) ||
518 !nir_alu_srcs_equal(alu1
, alu2
, 1, 1)) &&
519 (!nir_alu_srcs_equal(alu1
, alu2
, 0, 1) ||
520 !nir_alu_srcs_equal(alu1
, alu2
, 1, 0)))
523 for (unsigned i
= 2; i
< nir_op_infos
[alu1
->op
].num_inputs
; i
++) {
524 if (!nir_alu_srcs_equal(alu1
, alu2
, i
, i
))
528 for (unsigned i
= 0; i
< nir_op_infos
[alu1
->op
].num_inputs
; i
++) {
529 if (!nir_alu_srcs_equal(alu1
, alu2
, i
, i
))
535 case nir_instr_type_deref
: {
536 nir_deref_instr
*deref1
= nir_instr_as_deref(instr1
);
537 nir_deref_instr
*deref2
= nir_instr_as_deref(instr2
);
539 if (deref1
->deref_type
!= deref2
->deref_type
||
540 deref1
->mode
!= deref2
->mode
||
541 deref1
->type
!= deref2
->type
)
544 if (deref1
->deref_type
== nir_deref_type_var
)
545 return deref1
->var
== deref2
->var
;
547 if (!nir_srcs_equal(deref1
->parent
, deref2
->parent
))
550 switch (deref1
->deref_type
) {
551 case nir_deref_type_struct
:
552 if (deref1
->strct
.index
!= deref2
->strct
.index
)
556 case nir_deref_type_array
:
557 case nir_deref_type_ptr_as_array
:
558 if (!nir_srcs_equal(deref1
->arr
.index
, deref2
->arr
.index
))
562 case nir_deref_type_cast
:
563 if (deref1
->cast
.ptr_stride
!= deref2
->cast
.ptr_stride
)
567 case nir_deref_type_var
:
568 case nir_deref_type_array_wildcard
:
573 unreachable("Invalid instruction deref type");
577 case nir_instr_type_tex
: {
578 nir_tex_instr
*tex1
= nir_instr_as_tex(instr1
);
579 nir_tex_instr
*tex2
= nir_instr_as_tex(instr2
);
581 if (tex1
->op
!= tex2
->op
)
584 if (tex1
->num_srcs
!= tex2
->num_srcs
)
586 for (unsigned i
= 0; i
< tex1
->num_srcs
; i
++) {
587 if (tex1
->src
[i
].src_type
!= tex2
->src
[i
].src_type
||
588 !nir_srcs_equal(tex1
->src
[i
].src
, tex2
->src
[i
].src
)) {
593 if (tex1
->coord_components
!= tex2
->coord_components
||
594 tex1
->sampler_dim
!= tex2
->sampler_dim
||
595 tex1
->is_array
!= tex2
->is_array
||
596 tex1
->is_shadow
!= tex2
->is_shadow
||
597 tex1
->is_new_style_shadow
!= tex2
->is_new_style_shadow
||
598 tex1
->component
!= tex2
->component
||
599 tex1
->texture_index
!= tex2
->texture_index
||
600 tex1
->texture_array_size
!= tex2
->texture_array_size
||
601 tex1
->sampler_index
!= tex2
->sampler_index
) {
605 if (memcmp(tex1
->tg4_offsets
, tex2
->tg4_offsets
,
606 sizeof(tex1
->tg4_offsets
)))
611 case nir_instr_type_load_const
: {
612 nir_load_const_instr
*load1
= nir_instr_as_load_const(instr1
);
613 nir_load_const_instr
*load2
= nir_instr_as_load_const(instr2
);
615 if (load1
->def
.num_components
!= load2
->def
.num_components
)
618 if (load1
->def
.bit_size
!= load2
->def
.bit_size
)
621 if (load1
->def
.bit_size
== 1) {
622 for (unsigned i
= 0; i
< load1
->def
.num_components
; ++i
) {
623 if (load1
->value
[i
].b
!= load2
->value
[i
].b
)
627 unsigned size
= load1
->def
.num_components
* sizeof(*load1
->value
);
628 if (memcmp(load1
->value
, load2
->value
, size
) != 0)
633 case nir_instr_type_phi
: {
634 nir_phi_instr
*phi1
= nir_instr_as_phi(instr1
);
635 nir_phi_instr
*phi2
= nir_instr_as_phi(instr2
);
637 if (phi1
->instr
.block
!= phi2
->instr
.block
)
640 nir_foreach_phi_src(src1
, phi1
) {
641 nir_foreach_phi_src(src2
, phi2
) {
642 if (src1
->pred
== src2
->pred
) {
643 if (!nir_srcs_equal(src1
->src
, src2
->src
))
653 case nir_instr_type_intrinsic
: {
654 nir_intrinsic_instr
*intrinsic1
= nir_instr_as_intrinsic(instr1
);
655 nir_intrinsic_instr
*intrinsic2
= nir_instr_as_intrinsic(instr2
);
656 const nir_intrinsic_info
*info
=
657 &nir_intrinsic_infos
[intrinsic1
->intrinsic
];
659 if (intrinsic1
->intrinsic
!= intrinsic2
->intrinsic
||
660 intrinsic1
->num_components
!= intrinsic2
->num_components
)
663 if (info
->has_dest
&& intrinsic1
->dest
.ssa
.num_components
!=
664 intrinsic2
->dest
.ssa
.num_components
)
667 if (info
->has_dest
&& intrinsic1
->dest
.ssa
.bit_size
!=
668 intrinsic2
->dest
.ssa
.bit_size
)
671 for (unsigned i
= 0; i
< info
->num_srcs
; i
++) {
672 if (!nir_srcs_equal(intrinsic1
->src
[i
], intrinsic2
->src
[i
]))
676 for (unsigned i
= 0; i
< info
->num_indices
; i
++) {
677 if (intrinsic1
->const_index
[i
] != intrinsic2
->const_index
[i
])
683 case nir_instr_type_call
:
684 case nir_instr_type_jump
:
685 case nir_instr_type_ssa_undef
:
686 case nir_instr_type_parallel_copy
:
688 unreachable("Invalid instruction type");
691 unreachable("All cases in the above switch should return");
695 src_is_ssa(nir_src
*src
, void *data
)
702 dest_is_ssa(nir_dest
*dest
, void *data
)
709 instr_each_src_and_dest_is_ssa(nir_instr
*instr
)
711 if (!nir_foreach_dest(instr
, dest_is_ssa
, NULL
) ||
712 !nir_foreach_src(instr
, src_is_ssa
, NULL
))
718 /* This function determines if uses of an instruction can safely be rewritten
719 * to use another identical instruction instead. Note that this function must
720 * be kept in sync with hash_instr() and nir_instrs_equal() -- only
721 * instructions that pass this test will be handed on to those functions, and
722 * conversely they must handle everything that this function returns true for.
726 instr_can_rewrite(nir_instr
*instr
)
728 /* We only handle SSA. */
729 assert(instr_each_src_and_dest_is_ssa(instr
));
731 switch (instr
->type
) {
732 case nir_instr_type_alu
:
733 case nir_instr_type_deref
:
734 case nir_instr_type_tex
:
735 case nir_instr_type_load_const
:
736 case nir_instr_type_phi
:
738 case nir_instr_type_intrinsic
:
739 return nir_intrinsic_can_reorder(nir_instr_as_intrinsic(instr
));
740 case nir_instr_type_call
:
741 case nir_instr_type_jump
:
742 case nir_instr_type_ssa_undef
:
744 case nir_instr_type_parallel_copy
:
746 unreachable("Invalid instruction type");
753 nir_instr_get_dest_ssa_def(nir_instr
*instr
)
755 switch (instr
->type
) {
756 case nir_instr_type_alu
:
757 assert(nir_instr_as_alu(instr
)->dest
.dest
.is_ssa
);
758 return &nir_instr_as_alu(instr
)->dest
.dest
.ssa
;
759 case nir_instr_type_deref
:
760 assert(nir_instr_as_deref(instr
)->dest
.is_ssa
);
761 return &nir_instr_as_deref(instr
)->dest
.ssa
;
762 case nir_instr_type_load_const
:
763 return &nir_instr_as_load_const(instr
)->def
;
764 case nir_instr_type_phi
:
765 assert(nir_instr_as_phi(instr
)->dest
.is_ssa
);
766 return &nir_instr_as_phi(instr
)->dest
.ssa
;
767 case nir_instr_type_intrinsic
:
768 assert(nir_instr_as_intrinsic(instr
)->dest
.is_ssa
);
769 return &nir_instr_as_intrinsic(instr
)->dest
.ssa
;
770 case nir_instr_type_tex
:
771 assert(nir_instr_as_tex(instr
)->dest
.is_ssa
);
772 return &nir_instr_as_tex(instr
)->dest
.ssa
;
774 unreachable("We never ask for any of these");
779 cmp_func(const void *data1
, const void *data2
)
781 return nir_instrs_equal(data1
, data2
);
785 nir_instr_set_create(void *mem_ctx
)
787 return _mesa_set_create(mem_ctx
, hash_instr
, cmp_func
);
791 nir_instr_set_destroy(struct set
*instr_set
)
793 _mesa_set_destroy(instr_set
, NULL
);
797 nir_instr_set_add_or_rewrite(struct set
*instr_set
, nir_instr
*instr
)
799 if (!instr_can_rewrite(instr
))
802 struct set_entry
*e
= _mesa_set_search_or_add(instr_set
, instr
);
803 nir_instr
*match
= (nir_instr
*) e
->key
;
804 if (match
!= instr
) {
805 nir_ssa_def
*def
= nir_instr_get_dest_ssa_def(instr
);
806 nir_ssa_def
*new_def
= nir_instr_get_dest_ssa_def(match
);
808 /* It's safe to replace an exact instruction with an inexact one as
809 * long as we make it exact. If we got here, the two instructions are
810 * exactly identical in every other way so, once we've set the exact
811 * bit, they are the same.
813 if (instr
->type
== nir_instr_type_alu
&& nir_instr_as_alu(instr
)->exact
)
814 nir_instr_as_alu(match
)->exact
= true;
816 nir_ssa_def_rewrite_uses(def
, nir_src_for_ssa(new_def
));
824 nir_instr_set_remove(struct set
*instr_set
, nir_instr
*instr
)
826 if (!instr_can_rewrite(instr
))
829 struct set_entry
*entry
= _mesa_set_search(instr_set
, instr
);
831 _mesa_set_remove(instr_set
, entry
);