nir/opt_undef: Handle a couple more normal store intrinsics.
[mesa.git] / src / compiler / nir / nir_opt_undef.c
index ce18c2df043b1904a8a676313cd34b7c0ffca73c..e03e5b88efc6bb2629fddcd63c2b799f05d58291 100644 (file)
@@ -22,6 +22,7 @@
  */
 
 #include "nir.h"
+#include "nir_builder.h"
 
 /** @file nir_opt_undef.c
  *
@@ -62,7 +63,7 @@ opt_undef_csel(nir_alu_instr *instr)
       memset(&empty_src, 0, sizeof(empty_src));
       nir_instr_rewrite_src(&instr->instr, &instr->src[1].src, empty_src);
       nir_instr_rewrite_src(&instr->instr, &instr->src[2].src, empty_src);
-      instr->op = nir_op_imov;
+      instr->op = nir_op_mov;
 
       return true;
    }
@@ -70,27 +71,94 @@ opt_undef_csel(nir_alu_instr *instr)
    return false;
 }
 
+/**
+ * Replace vecN(undef, undef, ...) with a single undef.
+ */
+static bool
+opt_undef_vecN(nir_builder *b, nir_alu_instr *alu)
+{
+   if (!nir_op_is_vec(alu->op))
+      return false;
+
+   assert(alu->dest.dest.is_ssa);
+
+   for (unsigned i = 0; i < nir_op_infos[alu->op].num_inputs; i++) {
+      if (!alu->src[i].src.is_ssa ||
+          alu->src[i].src.ssa->parent_instr->type != nir_instr_type_ssa_undef)
+         return false;
+   }
+
+   b->cursor = nir_before_instr(&alu->instr);
+   nir_ssa_def *undef = nir_ssa_undef(b, alu->dest.dest.ssa.num_components,
+                                      nir_dest_bit_size(alu->dest.dest));
+   nir_ssa_def_rewrite_uses(&alu->dest.dest.ssa, nir_src_for_ssa(undef));
+
+   return true;
+}
+
+/**
+ * Remove any store intrinsics whose value is undefined (the existing
+ * value is a fine representation of "undefined").
+ */
+static bool
+opt_undef_store(nir_intrinsic_instr *intrin)
+{
+   int arg_index;
+   switch (intrin->intrinsic) {
+   case nir_intrinsic_store_deref:
+      arg_index = 1;
+      break;
+   case nir_intrinsic_store_output:
+   case nir_intrinsic_store_per_vertex_output:
+   case nir_intrinsic_store_ssbo:
+   case nir_intrinsic_store_shared:
+   case nir_intrinsic_store_global:
+   case nir_intrinsic_store_scratch:
+      arg_index =  0;
+      break;
+   default:
+      return false;
+   }
+
+   if (!intrin->src[arg_index].is_ssa ||
+       intrin->src[arg_index].ssa->parent_instr->type != nir_instr_type_ssa_undef)
+      return false;
+
+   nir_instr_remove(&intrin->instr);
+
+   return true;
+}
+
 bool
 nir_opt_undef(nir_shader *shader)
 {
+   nir_builder b;
    bool progress = false;
 
    nir_foreach_function(function, shader) {
       if (function->impl) {
+         nir_builder_init(&b, function->impl);
          nir_foreach_block(block, function->impl) {
             nir_foreach_instr_safe(instr, block) {
                if (instr->type == nir_instr_type_alu) {
                   nir_alu_instr *alu = nir_instr_as_alu(instr);
 
                   progress = opt_undef_csel(alu) || progress;
+                  progress = opt_undef_vecN(&b, alu) || progress;
+               } else if (instr->type == nir_instr_type_intrinsic) {
+                  nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
+                  progress = opt_undef_store(intrin) || progress;
                }
             }
          }
 
-         if (progress)
+         if (progress) {
             nir_metadata_preserve(function->impl,
                                   nir_metadata_block_index |
                                   nir_metadata_dominance);
+         } else {
+            nir_metadata_preserve(function->impl, nir_metadata_all);
+         }
       }
    }