nir: Add a helper for getting a constant value from an SSA source
authorJason Ekstrand <jason.ekstrand@intel.com>
Tue, 9 Dec 2014 01:34:23 +0000 (17:34 -0800)
committerJason Ekstrand <jason.ekstrand@intel.com>
Thu, 15 Jan 2015 15:19:03 +0000 (07:19 -0800)
Reviewed-by: Connor Abbott <cwabbott0@gmail.com>
src/glsl/nir/nir.c
src/glsl/nir/nir.h

index b6bd78d05e669128e5c5e9c43a4abce8bffefebf..c376de1cf1947bc2b017d1f5baca780d291bc5a4 100644 (file)
@@ -1672,6 +1672,25 @@ nir_foreach_src(nir_instr *instr, nir_foreach_src_cb cb, void *state)
    return nir_foreach_dest(instr, visit_dest_indirect, &dest_state);
 }
 
+nir_const_value *
+nir_src_as_const_value(nir_src src)
+{
+   if (!src.is_ssa)
+      return NULL;
+
+   if (src.ssa->parent_instr->type != nir_instr_type_load_const)
+      return NULL;
+
+   nir_load_const_instr *load = nir_instr_as_load_const(src.ssa->parent_instr);
+
+   if (load->array_elems == 0)
+      return &load->value;
+   if (load->array_elems == 1)
+      return load->array;
+   else
+      return NULL;
+}
+
 bool
 nir_srcs_equal(nir_src src1, nir_src src2)
 {
index ff1f7964fce75cf70f55549ecbcab652376093b0..660cc4b8bb6b0b15ba0543320fd3036d1fa19c8b 100644 (file)
@@ -1326,6 +1326,7 @@ typedef bool (*nir_foreach_src_cb)(nir_src *src, void *state);
 bool nir_foreach_dest(nir_instr *instr, nir_foreach_dest_cb cb, void *state);
 bool nir_foreach_src(nir_instr *instr, nir_foreach_src_cb cb, void *state);
 
+nir_const_value *nir_src_as_const_value(nir_src src);
 bool nir_srcs_equal(nir_src src1, nir_src src2);
 void nir_instr_rewrite_src(nir_instr *instr, nir_src *src, nir_src new_src);