Path: blob/21.2-virgl/src/compiler/nir/nir_instr_set.c
4546 views
/*1* Copyright © 2014 Connor Abbott2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#include "nir_instr_set.h"24#include "nir_vla.h"25#include "util/half_float.h"2627static bool28src_is_ssa(nir_src *src, void *data)29{30(void) data;31return src->is_ssa;32}3334static bool35dest_is_ssa(nir_dest *dest, void *data)36{37(void) data;38return dest->is_ssa;39}4041ASSERTED static inline bool42instr_each_src_and_dest_is_ssa(const nir_instr *instr)43{44if (!nir_foreach_dest((nir_instr *)instr, dest_is_ssa, NULL) ||45!nir_foreach_src((nir_instr *)instr, src_is_ssa, NULL))46return false;4748return true;49}5051/* This function determines if uses of an instruction can safely be rewritten52* to use another identical instruction instead. Note that this function must53* be kept in sync with hash_instr() and nir_instrs_equal() -- only54* instructions that pass this test will be handed on to those functions, and55* conversely they must handle everything that this function returns true for.56*/57static bool58instr_can_rewrite(const nir_instr *instr)59{60/* We only handle SSA. */61assert(instr_each_src_and_dest_is_ssa(instr));6263switch (instr->type) {64case nir_instr_type_alu:65case nir_instr_type_deref:66case nir_instr_type_tex:67case nir_instr_type_load_const:68case nir_instr_type_phi:69return true;70case nir_instr_type_intrinsic:71return nir_intrinsic_can_reorder(nir_instr_as_intrinsic(instr));72case nir_instr_type_call:73case nir_instr_type_jump:74case nir_instr_type_ssa_undef:75return false;76case nir_instr_type_parallel_copy:77default:78unreachable("Invalid instruction type");79}8081return false;82}838485#define HASH(hash, data) XXH32(&(data), sizeof(data), hash)8687static uint32_t88hash_src(uint32_t hash, const nir_src *src)89{90assert(src->is_ssa);91hash = HASH(hash, src->ssa);92return hash;93}9495static uint32_t96hash_alu_src(uint32_t hash, const nir_alu_src *src, unsigned num_components)97{98hash = HASH(hash, src->abs);99hash = HASH(hash, src->negate);100101for (unsigned i = 0; i < num_components; i++)102hash = HASH(hash, src->swizzle[i]);103104hash = hash_src(hash, &src->src);105return hash;106}107108static uint32_t109hash_alu(uint32_t hash, const nir_alu_instr *instr)110{111hash = HASH(hash, instr->op);112113/* We explicitly don't hash instr->exact. */114uint8_t flags = instr->no_signed_wrap |115instr->no_unsigned_wrap << 1;116hash = HASH(hash, flags);117118hash = HASH(hash, instr->dest.dest.ssa.num_components);119hash = HASH(hash, instr->dest.dest.ssa.bit_size);120121if (nir_op_infos[instr->op].algebraic_properties & NIR_OP_IS_2SRC_COMMUTATIVE) {122assert(nir_op_infos[instr->op].num_inputs >= 2);123124uint32_t hash0 = hash_alu_src(hash, &instr->src[0],125nir_ssa_alu_instr_src_components(instr, 0));126uint32_t hash1 = hash_alu_src(hash, &instr->src[1],127nir_ssa_alu_instr_src_components(instr, 1));128/* For commutative operations, we need some commutative way of129* combining the hashes. One option would be to XOR them but that130* means that anything with two identical sources will hash to 0 and131* that's common enough we probably don't want the guaranteed132* collision. Either addition or multiplication will also work.133*/134hash = hash0 * hash1;135136for (unsigned i = 2; i < nir_op_infos[instr->op].num_inputs; i++) {137hash = hash_alu_src(hash, &instr->src[i],138nir_ssa_alu_instr_src_components(instr, i));139}140} else {141for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {142hash = hash_alu_src(hash, &instr->src[i],143nir_ssa_alu_instr_src_components(instr, i));144}145}146147return hash;148}149150static uint32_t151hash_deref(uint32_t hash, const nir_deref_instr *instr)152{153hash = HASH(hash, instr->deref_type);154hash = HASH(hash, instr->modes);155hash = HASH(hash, instr->type);156157if (instr->deref_type == nir_deref_type_var)158return HASH(hash, instr->var);159160hash = hash_src(hash, &instr->parent);161162switch (instr->deref_type) {163case nir_deref_type_struct:164hash = HASH(hash, instr->strct.index);165break;166167case nir_deref_type_array:168case nir_deref_type_ptr_as_array:169hash = hash_src(hash, &instr->arr.index);170break;171172case nir_deref_type_cast:173hash = HASH(hash, instr->cast.ptr_stride);174hash = HASH(hash, instr->cast.align_mul);175hash = HASH(hash, instr->cast.align_offset);176break;177178case nir_deref_type_var:179case nir_deref_type_array_wildcard:180/* Nothing to do */181break;182183default:184unreachable("Invalid instruction deref type");185}186187return hash;188}189190static uint32_t191hash_load_const(uint32_t hash, const nir_load_const_instr *instr)192{193hash = HASH(hash, instr->def.num_components);194195if (instr->def.bit_size == 1) {196for (unsigned i = 0; i < instr->def.num_components; i++) {197uint8_t b = instr->value[i].b;198hash = HASH(hash, b);199}200} else {201unsigned size = instr->def.num_components * sizeof(*instr->value);202hash = XXH32(instr->value, size, hash);203}204205return hash;206}207208static int209cmp_phi_src(const void *data1, const void *data2)210{211nir_phi_src *src1 = *(nir_phi_src **)data1;212nir_phi_src *src2 = *(nir_phi_src **)data2;213return src1->pred - src2->pred;214}215216static uint32_t217hash_phi(uint32_t hash, const nir_phi_instr *instr)218{219hash = HASH(hash, instr->instr.block);220221/* sort sources by predecessor, since the order shouldn't matter */222unsigned num_preds = instr->instr.block->predecessors->entries;223NIR_VLA(nir_phi_src *, srcs, num_preds);224unsigned i = 0;225nir_foreach_phi_src(src, instr) {226srcs[i++] = src;227}228229qsort(srcs, num_preds, sizeof(nir_phi_src *), cmp_phi_src);230231for (i = 0; i < num_preds; i++) {232hash = hash_src(hash, &srcs[i]->src);233hash = HASH(hash, srcs[i]->pred);234}235236return hash;237}238239static uint32_t240hash_intrinsic(uint32_t hash, const nir_intrinsic_instr *instr)241{242const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic];243hash = HASH(hash, instr->intrinsic);244245if (info->has_dest) {246hash = HASH(hash, instr->dest.ssa.num_components);247hash = HASH(hash, instr->dest.ssa.bit_size);248}249250hash = XXH32(instr->const_index, info->num_indices * sizeof(instr->const_index[0]), hash);251252for (unsigned i = 0; i < nir_intrinsic_infos[instr->intrinsic].num_srcs; i++)253hash = hash_src(hash, &instr->src[i]);254255return hash;256}257258static uint32_t259hash_tex(uint32_t hash, const nir_tex_instr *instr)260{261hash = HASH(hash, instr->op);262hash = HASH(hash, instr->num_srcs);263264for (unsigned i = 0; i < instr->num_srcs; i++) {265hash = HASH(hash, instr->src[i].src_type);266hash = hash_src(hash, &instr->src[i].src);267}268269hash = HASH(hash, instr->coord_components);270hash = HASH(hash, instr->sampler_dim);271hash = HASH(hash, instr->is_array);272hash = HASH(hash, instr->is_shadow);273hash = HASH(hash, instr->is_new_style_shadow);274unsigned component = instr->component;275hash = HASH(hash, component);276for (unsigned i = 0; i < 4; ++i)277for (unsigned j = 0; j < 2; ++j)278hash = HASH(hash, instr->tg4_offsets[i][j]);279hash = HASH(hash, instr->texture_index);280hash = HASH(hash, instr->sampler_index);281hash = HASH(hash, instr->texture_non_uniform);282hash = HASH(hash, instr->sampler_non_uniform);283284return hash;285}286287/* Computes a hash of an instruction for use in a hash table. Note that this288* will only work for instructions where instr_can_rewrite() returns true, and289* it should return identical hashes for two instructions that are the same290* according nir_instrs_equal().291*/292293static uint32_t294hash_instr(const void *data)295{296const nir_instr *instr = data;297uint32_t hash = 0;298299switch (instr->type) {300case nir_instr_type_alu:301hash = hash_alu(hash, nir_instr_as_alu(instr));302break;303case nir_instr_type_deref:304hash = hash_deref(hash, nir_instr_as_deref(instr));305break;306case nir_instr_type_load_const:307hash = hash_load_const(hash, nir_instr_as_load_const(instr));308break;309case nir_instr_type_phi:310hash = hash_phi(hash, nir_instr_as_phi(instr));311break;312case nir_instr_type_intrinsic:313hash = hash_intrinsic(hash, nir_instr_as_intrinsic(instr));314break;315case nir_instr_type_tex:316hash = hash_tex(hash, nir_instr_as_tex(instr));317break;318default:319unreachable("Invalid instruction type");320}321322return hash;323}324325bool326nir_srcs_equal(nir_src src1, nir_src src2)327{328if (src1.is_ssa) {329if (src2.is_ssa) {330return src1.ssa == src2.ssa;331} else {332return false;333}334} else {335if (src2.is_ssa) {336return false;337} else {338if ((src1.reg.indirect == NULL) != (src2.reg.indirect == NULL))339return false;340341if (src1.reg.indirect) {342if (!nir_srcs_equal(*src1.reg.indirect, *src2.reg.indirect))343return false;344}345346return src1.reg.reg == src2.reg.reg &&347src1.reg.base_offset == src2.reg.base_offset;348}349}350}351352/**353* If the \p s is an SSA value that was generated by a negation instruction,354* that instruction is returned as a \c nir_alu_instr. Otherwise \c NULL is355* returned.356*/357static nir_alu_instr *358get_neg_instr(nir_src s)359{360nir_alu_instr *alu = nir_src_as_alu_instr(s);361362return alu != NULL && (alu->op == nir_op_fneg || alu->op == nir_op_ineg)363? alu : NULL;364}365366bool367nir_const_value_negative_equal(nir_const_value c1,368nir_const_value c2,369nir_alu_type full_type)370{371assert(nir_alu_type_get_base_type(full_type) != nir_type_invalid);372assert(nir_alu_type_get_type_size(full_type) != 0);373374switch (full_type) {375case nir_type_float16:376return _mesa_half_to_float(c1.u16) == -_mesa_half_to_float(c2.u16);377378case nir_type_float32:379return c1.f32 == -c2.f32;380381case nir_type_float64:382return c1.f64 == -c2.f64;383384case nir_type_int8:385case nir_type_uint8:386return c1.i8 == -c2.i8;387388case nir_type_int16:389case nir_type_uint16:390return c1.i16 == -c2.i16;391392case nir_type_int32:393case nir_type_uint32:394return c1.i32 == -c2.i32;395396case nir_type_int64:397case nir_type_uint64:398return c1.i64 == -c2.i64;399400default:401break;402}403404return false;405}406407/**408* Shallow compare of ALU srcs to determine if one is the negation of the other409*410* This function detects cases where \p alu1 is a constant and \p alu2 is a411* constant that is its negation. It will also detect cases where \p alu2 is412* an SSA value that is a \c nir_op_fneg applied to \p alu1 (and vice versa).413*414* This function does not detect the general case when \p alu1 and \p alu2 are415* SSA values that are the negations of each other (e.g., \p alu1 represents416* (a * b) and \p alu2 represents (-a * b)).417*418* \warning419* It is the responsibility of the caller to ensure that the component counts,420* write masks, and base types of the sources being compared are compatible.421*/422bool423nir_alu_srcs_negative_equal(const nir_alu_instr *alu1,424const nir_alu_instr *alu2,425unsigned src1, unsigned src2)426{427#ifndef NDEBUG428for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++) {429assert(nir_alu_instr_channel_used(alu1, src1, i) ==430nir_alu_instr_channel_used(alu2, src2, i));431}432433if (nir_op_infos[alu1->op].input_types[src1] == nir_type_float) {434assert(nir_op_infos[alu1->op].input_types[src1] ==435nir_op_infos[alu2->op].input_types[src2]);436} else {437assert(nir_op_infos[alu1->op].input_types[src1] == nir_type_int);438assert(nir_op_infos[alu2->op].input_types[src2] == nir_type_int);439}440#endif441442if (alu1->src[src1].abs != alu2->src[src2].abs)443return false;444445bool parity = alu1->src[src1].negate != alu2->src[src2].negate;446447/* Handling load_const instructions is tricky. */448449const nir_const_value *const const1 =450nir_src_as_const_value(alu1->src[src1].src);451452if (const1 != NULL) {453/* Assume that constant folding will eliminate source mods and unary454* ops.455*/456if (parity)457return false;458459const nir_const_value *const const2 =460nir_src_as_const_value(alu2->src[src2].src);461462if (const2 == NULL)463return false;464465if (nir_src_bit_size(alu1->src[src1].src) !=466nir_src_bit_size(alu2->src[src2].src))467return false;468469const nir_alu_type full_type = nir_op_infos[alu1->op].input_types[src1] |470nir_src_bit_size(alu1->src[src1].src);471for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++) {472if (nir_alu_instr_channel_used(alu1, src1, i) &&473!nir_const_value_negative_equal(const1[alu1->src[src1].swizzle[i]],474const2[alu2->src[src2].swizzle[i]],475full_type))476return false;477}478479return true;480}481482uint8_t alu1_swizzle[NIR_MAX_VEC_COMPONENTS] = {0};483nir_src alu1_actual_src;484nir_alu_instr *neg1 = get_neg_instr(alu1->src[src1].src);485486if (neg1) {487parity = !parity;488alu1_actual_src = neg1->src[0].src;489490for (unsigned i = 0; i < nir_ssa_alu_instr_src_components(neg1, 0); i++)491alu1_swizzle[i] = neg1->src[0].swizzle[i];492} else {493alu1_actual_src = alu1->src[src1].src;494495for (unsigned i = 0; i < nir_ssa_alu_instr_src_components(alu1, src1); i++)496alu1_swizzle[i] = i;497}498499uint8_t alu2_swizzle[NIR_MAX_VEC_COMPONENTS] = {0};500nir_src alu2_actual_src;501nir_alu_instr *neg2 = get_neg_instr(alu2->src[src2].src);502503if (neg2) {504parity = !parity;505alu2_actual_src = neg2->src[0].src;506507for (unsigned i = 0; i < nir_ssa_alu_instr_src_components(neg2, 0); i++)508alu2_swizzle[i] = neg2->src[0].swizzle[i];509} else {510alu2_actual_src = alu2->src[src2].src;511512for (unsigned i = 0; i < nir_ssa_alu_instr_src_components(alu2, src2); i++)513alu2_swizzle[i] = i;514}515516for (unsigned i = 0; i < nir_ssa_alu_instr_src_components(alu1, src1); i++) {517if (alu1_swizzle[alu1->src[src1].swizzle[i]] !=518alu2_swizzle[alu2->src[src2].swizzle[i]])519return false;520}521522return parity && nir_srcs_equal(alu1_actual_src, alu2_actual_src);523}524525bool526nir_alu_srcs_equal(const nir_alu_instr *alu1, const nir_alu_instr *alu2,527unsigned src1, unsigned src2)528{529if (alu1->src[src1].abs != alu2->src[src2].abs ||530alu1->src[src1].negate != alu2->src[src2].negate)531return false;532533for (unsigned i = 0; i < nir_ssa_alu_instr_src_components(alu1, src1); i++) {534if (alu1->src[src1].swizzle[i] != alu2->src[src2].swizzle[i])535return false;536}537538return nir_srcs_equal(alu1->src[src1].src, alu2->src[src2].src);539}540541/* Returns "true" if two instructions are equal. Note that this will only542* work for the subset of instructions defined by instr_can_rewrite(). Also,543* it should only return "true" for instructions that hash_instr() will return544* the same hash for (ignoring collisions, of course).545*/546547bool548nir_instrs_equal(const nir_instr *instr1, const nir_instr *instr2)549{550assert(instr_can_rewrite(instr1) && instr_can_rewrite(instr2));551552if (instr1->type != instr2->type)553return false;554555switch (instr1->type) {556case nir_instr_type_alu: {557nir_alu_instr *alu1 = nir_instr_as_alu(instr1);558nir_alu_instr *alu2 = nir_instr_as_alu(instr2);559560if (alu1->op != alu2->op)561return false;562563/* We explicitly don't compare instr->exact. */564565if (alu1->no_signed_wrap != alu2->no_signed_wrap)566return false;567568if (alu1->no_unsigned_wrap != alu2->no_unsigned_wrap)569return false;570571/* TODO: We can probably acutally do something more inteligent such572* as allowing different numbers and taking a maximum or something573* here */574if (alu1->dest.dest.ssa.num_components != alu2->dest.dest.ssa.num_components)575return false;576577if (alu1->dest.dest.ssa.bit_size != alu2->dest.dest.ssa.bit_size)578return false;579580if (nir_op_infos[alu1->op].algebraic_properties & NIR_OP_IS_2SRC_COMMUTATIVE) {581if ((!nir_alu_srcs_equal(alu1, alu2, 0, 0) ||582!nir_alu_srcs_equal(alu1, alu2, 1, 1)) &&583(!nir_alu_srcs_equal(alu1, alu2, 0, 1) ||584!nir_alu_srcs_equal(alu1, alu2, 1, 0)))585return false;586587for (unsigned i = 2; i < nir_op_infos[alu1->op].num_inputs; i++) {588if (!nir_alu_srcs_equal(alu1, alu2, i, i))589return false;590}591} else {592for (unsigned i = 0; i < nir_op_infos[alu1->op].num_inputs; i++) {593if (!nir_alu_srcs_equal(alu1, alu2, i, i))594return false;595}596}597return true;598}599case nir_instr_type_deref: {600nir_deref_instr *deref1 = nir_instr_as_deref(instr1);601nir_deref_instr *deref2 = nir_instr_as_deref(instr2);602603if (deref1->deref_type != deref2->deref_type ||604deref1->modes != deref2->modes ||605deref1->type != deref2->type)606return false;607608if (deref1->deref_type == nir_deref_type_var)609return deref1->var == deref2->var;610611if (!nir_srcs_equal(deref1->parent, deref2->parent))612return false;613614switch (deref1->deref_type) {615case nir_deref_type_struct:616if (deref1->strct.index != deref2->strct.index)617return false;618break;619620case nir_deref_type_array:621case nir_deref_type_ptr_as_array:622if (!nir_srcs_equal(deref1->arr.index, deref2->arr.index))623return false;624break;625626case nir_deref_type_cast:627if (deref1->cast.ptr_stride != deref2->cast.ptr_stride ||628deref1->cast.align_mul != deref2->cast.align_mul ||629deref1->cast.align_offset != deref2->cast.align_offset)630return false;631break;632633case nir_deref_type_var:634case nir_deref_type_array_wildcard:635/* Nothing to do */636break;637638default:639unreachable("Invalid instruction deref type");640}641return true;642}643case nir_instr_type_tex: {644nir_tex_instr *tex1 = nir_instr_as_tex(instr1);645nir_tex_instr *tex2 = nir_instr_as_tex(instr2);646647if (tex1->op != tex2->op)648return false;649650if (tex1->num_srcs != tex2->num_srcs)651return false;652for (unsigned i = 0; i < tex1->num_srcs; i++) {653if (tex1->src[i].src_type != tex2->src[i].src_type ||654!nir_srcs_equal(tex1->src[i].src, tex2->src[i].src)) {655return false;656}657}658659if (tex1->coord_components != tex2->coord_components ||660tex1->sampler_dim != tex2->sampler_dim ||661tex1->is_array != tex2->is_array ||662tex1->is_shadow != tex2->is_shadow ||663tex1->is_new_style_shadow != tex2->is_new_style_shadow ||664tex1->component != tex2->component ||665tex1->texture_index != tex2->texture_index ||666tex1->sampler_index != tex2->sampler_index) {667return false;668}669670if (memcmp(tex1->tg4_offsets, tex2->tg4_offsets,671sizeof(tex1->tg4_offsets)))672return false;673674return true;675}676case nir_instr_type_load_const: {677nir_load_const_instr *load1 = nir_instr_as_load_const(instr1);678nir_load_const_instr *load2 = nir_instr_as_load_const(instr2);679680if (load1->def.num_components != load2->def.num_components)681return false;682683if (load1->def.bit_size != load2->def.bit_size)684return false;685686if (load1->def.bit_size == 1) {687for (unsigned i = 0; i < load1->def.num_components; ++i) {688if (load1->value[i].b != load2->value[i].b)689return false;690}691} else {692unsigned size = load1->def.num_components * sizeof(*load1->value);693if (memcmp(load1->value, load2->value, size) != 0)694return false;695}696return true;697}698case nir_instr_type_phi: {699nir_phi_instr *phi1 = nir_instr_as_phi(instr1);700nir_phi_instr *phi2 = nir_instr_as_phi(instr2);701702if (phi1->instr.block != phi2->instr.block)703return false;704705nir_foreach_phi_src(src1, phi1) {706nir_foreach_phi_src(src2, phi2) {707if (src1->pred == src2->pred) {708if (!nir_srcs_equal(src1->src, src2->src))709return false;710711break;712}713}714}715716return true;717}718case nir_instr_type_intrinsic: {719nir_intrinsic_instr *intrinsic1 = nir_instr_as_intrinsic(instr1);720nir_intrinsic_instr *intrinsic2 = nir_instr_as_intrinsic(instr2);721const nir_intrinsic_info *info =722&nir_intrinsic_infos[intrinsic1->intrinsic];723724if (intrinsic1->intrinsic != intrinsic2->intrinsic ||725intrinsic1->num_components != intrinsic2->num_components)726return false;727728if (info->has_dest && intrinsic1->dest.ssa.num_components !=729intrinsic2->dest.ssa.num_components)730return false;731732if (info->has_dest && intrinsic1->dest.ssa.bit_size !=733intrinsic2->dest.ssa.bit_size)734return false;735736for (unsigned i = 0; i < info->num_srcs; i++) {737if (!nir_srcs_equal(intrinsic1->src[i], intrinsic2->src[i]))738return false;739}740741for (unsigned i = 0; i < info->num_indices; i++) {742if (intrinsic1->const_index[i] != intrinsic2->const_index[i])743return false;744}745746return true;747}748case nir_instr_type_call:749case nir_instr_type_jump:750case nir_instr_type_ssa_undef:751case nir_instr_type_parallel_copy:752default:753unreachable("Invalid instruction type");754}755756unreachable("All cases in the above switch should return");757}758759static nir_ssa_def *760nir_instr_get_dest_ssa_def(nir_instr *instr)761{762switch (instr->type) {763case nir_instr_type_alu:764assert(nir_instr_as_alu(instr)->dest.dest.is_ssa);765return &nir_instr_as_alu(instr)->dest.dest.ssa;766case nir_instr_type_deref:767assert(nir_instr_as_deref(instr)->dest.is_ssa);768return &nir_instr_as_deref(instr)->dest.ssa;769case nir_instr_type_load_const:770return &nir_instr_as_load_const(instr)->def;771case nir_instr_type_phi:772assert(nir_instr_as_phi(instr)->dest.is_ssa);773return &nir_instr_as_phi(instr)->dest.ssa;774case nir_instr_type_intrinsic:775assert(nir_instr_as_intrinsic(instr)->dest.is_ssa);776return &nir_instr_as_intrinsic(instr)->dest.ssa;777case nir_instr_type_tex:778assert(nir_instr_as_tex(instr)->dest.is_ssa);779return &nir_instr_as_tex(instr)->dest.ssa;780default:781unreachable("We never ask for any of these");782}783}784785static bool786cmp_func(const void *data1, const void *data2)787{788return nir_instrs_equal(data1, data2);789}790791struct set *792nir_instr_set_create(void *mem_ctx)793{794return _mesa_set_create(mem_ctx, hash_instr, cmp_func);795}796797void798nir_instr_set_destroy(struct set *instr_set)799{800_mesa_set_destroy(instr_set, NULL);801}802803bool804nir_instr_set_add_or_rewrite(struct set *instr_set, nir_instr *instr,805bool (*cond_function) (const nir_instr *a,806const nir_instr *b))807{808if (!instr_can_rewrite(instr))809return false;810811struct set_entry *e = _mesa_set_search_or_add(instr_set, instr, NULL);812nir_instr *match = (nir_instr *) e->key;813if (match == instr)814return false;815816if (!cond_function || cond_function(match, instr)) {817/* rewrite instruction if condition is matched */818nir_ssa_def *def = nir_instr_get_dest_ssa_def(instr);819nir_ssa_def *new_def = nir_instr_get_dest_ssa_def(match);820821/* It's safe to replace an exact instruction with an inexact one as822* long as we make it exact. If we got here, the two instructions are823* exactly identical in every other way so, once we've set the exact824* bit, they are the same.825*/826if (instr->type == nir_instr_type_alu && nir_instr_as_alu(instr)->exact)827nir_instr_as_alu(match)->exact = true;828829nir_ssa_def_rewrite_uses(def, new_def);830831nir_instr_remove(instr);832833return true;834} else {835/* otherwise, replace hashed instruction */836e->key = instr;837return false;838}839}840841void842nir_instr_set_remove(struct set *instr_set, nir_instr *instr)843{844if (!instr_can_rewrite(instr))845return;846847struct set_entry *entry = _mesa_set_search(instr_set, instr);848if (entry)849_mesa_set_remove(instr_set, entry);850}851852853854