nir: Switch to using 1-bit Booleans for almost everything
[mesa.git] / src / compiler / nir / nir_opt_undef.c
index a7bb887353e95f5bb635cbe2dd41f33c208083ac..c26158dab7ebed001cfebe6d5ecd58454af362cd 100644 (file)
@@ -79,27 +79,58 @@ opt_undef_vecN(nir_builder *b, nir_alu_instr *alu)
 {
    if (alu->op != nir_op_vec2 &&
        alu->op != nir_op_vec3 &&
-       alu->op != nir_op_vec4)
+       alu->op != nir_op_vec4 &&
+       alu->op != nir_op_fmov &&
+       alu->op != nir_op_imov)
       return false;
 
    assert(alu->dest.dest.is_ssa);
 
-   unsigned num_components = nir_op_infos[alu->op].num_inputs;
-
-   for (unsigned i = 0; i < num_components; i++) {
+   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, num_components, nir_dest_bit_size(alu->dest.dest));
+   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:
+      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)
 {
@@ -116,6 +147,9 @@ nir_opt_undef(nir_shader *shader)
 
                   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;
                }
             }
          }