Move compiler.h and imports.h/c from src/mesa/main into src/util
[mesa.git] / src / compiler / nir / nir_opt_copy_propagate.c
index 189d544979b179256e08bf00348b734d1f68e9e6..00c78f384510df9024b21932b4e0616fd09dc91e 100644 (file)
@@ -26,7 +26,7 @@
  */
 
 #include "nir.h"
-#include <main/imports.h>
+#include <util/imports.h>
 
 /**
  * SSA-based copy propagation
@@ -34,8 +34,9 @@
 
 static bool is_move(nir_alu_instr *instr)
 {
-   if (instr->op != nir_op_fmov &&
-       instr->op != nir_op_imov)
+   assert(instr->src[0].src.is_ssa);
+
+   if (instr->op != nir_op_mov)
       return false;
 
    if (instr->dest.saturate)
@@ -46,9 +47,6 @@ static bool is_move(nir_alu_instr *instr)
    if (instr->src[0].abs || instr->src[0].negate)
       return false;
 
-   if (!instr->src[0].src.is_ssa)
-      return false;
-
    return true;
 
 }
@@ -56,8 +54,7 @@ static bool is_move(nir_alu_instr *instr)
 static bool is_vec(nir_alu_instr *instr)
 {
    for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
-      if (!instr->src[i].src.is_ssa)
-         return false;
+      assert(instr->src[i].src.is_ssa);
 
       /* we handle modifiers in a separate pass */
       if (instr->src[i].abs || instr->src[i].negate)
@@ -98,31 +95,11 @@ is_swizzleless_move(nir_alu_instr *instr)
    }
 }
 
-static bool
-is_trivial_deref_cast(nir_deref_instr *cast)
-{
-   if (cast->deref_type != nir_deref_type_cast)
-      return false;
-
-   nir_deref_instr *parent = nir_src_as_deref(cast->parent);
-   if (!parent)
-      return false;
-
-   return cast->mode == parent->mode &&
-          cast->type == parent->type &&
-          cast->dest.ssa.num_components == parent->dest.ssa.num_components &&
-          cast->dest.ssa.bit_size == parent->dest.ssa.bit_size;
-}
-
 static bool
 copy_prop_src(nir_src *src, nir_instr *parent_instr, nir_if *parent_if,
               unsigned num_components)
 {
-   if (!src->is_ssa) {
-      if (src->reg.indirect)
-         return copy_prop_src(src->reg.indirect, parent_instr, parent_if, 1);
-      return false;
-   }
+   assert(src->is_ssa);
 
    nir_instr *src_instr = src->ssa->parent_instr;
    nir_ssa_def *copy_def;
@@ -135,12 +112,6 @@ copy_prop_src(nir_src *src, nir_instr *parent_instr, nir_if *parent_if,
          return false;
 
       copy_def= alu_instr->src[0].src.ssa;
-   } else if (src_instr->type == nir_instr_type_deref) {
-      nir_deref_instr *deref_instr = nir_instr_as_deref(src_instr);
-      if (!is_trivial_deref_cast(deref_instr))
-         return false;
-
-      copy_def = deref_instr->parent.ssa;
    } else {
       return false;
    }
@@ -159,12 +130,7 @@ static bool
 copy_prop_alu_src(nir_alu_instr *parent_alu_instr, unsigned index)
 {
    nir_alu_src *src = &parent_alu_instr->src[index];
-   if (!src->src.is_ssa) {
-      if (src->src.reg.indirect)
-         return copy_prop_src(src->src.reg.indirect, &parent_alu_instr->instr,
-                              NULL, 1);
-      return false;
-   }
+   assert(src->src.is_ssa);
 
    nir_instr *src_instr =  src->src.ssa->parent_instr;
    if (src_instr->type != nir_instr_type_alu)
@@ -175,17 +141,16 @@ copy_prop_alu_src(nir_alu_instr *parent_alu_instr, unsigned index)
       return false;
 
    nir_ssa_def *def;
-   unsigned new_swizzle[4] = {0, 0, 0, 0};
+   unsigned new_swizzle[NIR_MAX_VEC_COMPONENTS] = {0, 0, 0, 0};
 
-   if (alu_instr->op == nir_op_fmov ||
-       alu_instr->op == nir_op_imov) {
-      for (unsigned i = 0; i < 4; i++)
+   if (alu_instr->op == nir_op_mov) {
+      for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++)
          new_swizzle[i] = alu_instr->src[0].swizzle[src->swizzle[i]];
       def = alu_instr->src[0].src.ssa;
    } else {
       def = NULL;
 
-      for (unsigned i = 0; i < 4; i++) {
+      for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++) {
          if (!nir_alu_instr_channel_used(parent_alu_instr, index, i))
             continue;
 
@@ -200,7 +165,7 @@ copy_prop_alu_src(nir_alu_instr *parent_alu_instr, unsigned index)
       }
    }
 
-   for (unsigned i = 0; i < 4; i++)
+   for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++)
       src->swizzle[i] = new_swizzle[i];
 
    nir_instr_rewrite_src(&parent_alu_instr->instr, &src->src,
@@ -209,15 +174,6 @@ copy_prop_alu_src(nir_alu_instr *parent_alu_instr, unsigned index)
    return true;
 }
 
-static bool
-copy_prop_dest(nir_dest *dest, nir_instr *instr)
-{
-   if (!dest->is_ssa && dest->reg.indirect)
-      return copy_prop_src(dest->reg.indirect, instr, NULL, 1);
-
-   return false;
-}
-
 static bool
 copy_prop_instr(nir_instr *instr)
 {
@@ -230,9 +186,6 @@ copy_prop_instr(nir_instr *instr)
          while (copy_prop_alu_src(alu_instr, i))
             progress = true;
 
-      while (copy_prop_dest(&alu_instr->dest.dest, instr))
-         progress = true;
-
       return progress;
    }
 
@@ -246,7 +199,8 @@ copy_prop_instr(nir_instr *instr)
             progress = true;
       }
 
-      if (deref->deref_type == nir_deref_type_array) {
+      if (deref->deref_type == nir_deref_type_array ||
+          deref->deref_type == nir_deref_type_ptr_as_array) {
          while (copy_prop_src(&deref->arr.index, instr, NULL, 1))
             progress = true;
       }
@@ -262,9 +216,6 @@ copy_prop_instr(nir_instr *instr)
             progress = true;
       }
 
-      while (copy_prop_dest(&tex->dest, instr))
-         progress = true;
-
       return progress;
    }
 
@@ -278,11 +229,6 @@ copy_prop_instr(nir_instr *instr)
             progress = true;
       }
 
-      if (nir_intrinsic_infos[intrin->intrinsic].has_dest) {
-         while (copy_prop_dest(&intrin->dest, instr))
-            progress = true;
-      }
-
       return progress;
    }
 
@@ -328,6 +274,10 @@ nir_copy_prop_impl(nir_function_impl *impl)
    if (progress) {
       nir_metadata_preserve(impl, nir_metadata_block_index |
                                   nir_metadata_dominance);
+   } else {
+#ifndef NDEBUG
+      impl->valid_metadata &= ~nir_metadata_not_properly_reset;
+#endif
    }
 
    return progress;