nir: add nir_alu_src_is_trivial_ssa()
Signed-off-by: Rhys Perry <pendingchaos02@gmail.com>
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/7426>
diff --git a/src/compiler/nir/nir.c b/src/compiler/nir/nir.c
index 66bfb39..bf1315a 100644
--- a/src/compiler/nir/nir.c
+++ b/src/compiler/nir/nir.c
@@ -332,6 +332,20 @@
dest->saturate = src->saturate;
}
+bool
+nir_alu_src_is_trivial_ssa(const nir_alu_instr *alu, unsigned srcn)
+{
+ static uint8_t trivial_swizzle[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
+ STATIC_ASSERT(ARRAY_SIZE(trivial_swizzle) == NIR_MAX_VEC_COMPONENTS);
+
+ const nir_alu_src *src = &alu->src[srcn];
+ unsigned num_components = nir_ssa_alu_instr_src_components(alu, srcn);
+
+ return src->src.is_ssa && (src->src.ssa->num_components == num_components) &&
+ !src->abs && !src->negate &&
+ (memcmp(src->swizzle, trivial_swizzle, num_components) == 0);
+}
+
static void
cf_init(nir_cf_node *node, nir_cf_node_type type)
diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h
index eb706a1..ba835cd 100644
--- a/src/compiler/nir/nir.h
+++ b/src/compiler/nir/nir.h
@@ -1432,6 +1432,8 @@
const nir_alu_instr *alu2,
unsigned src1, unsigned src2);
+bool nir_alu_src_is_trivial_ssa(const nir_alu_instr *alu, unsigned srcn);
+
typedef enum {
nir_deref_type_var,
nir_deref_type_array,