i965/vec4: split VEC4_OPCODE_FROM_DOUBLE into one opcode per destination's type
[mesa.git] / src / compiler / nir / nir_lower_to_source_mods.c
index b5f165002d154956d76ce777912a2a87e8ad8da2..077ca53704f28d0b5063279cfa2c2a8438d5cba3 100644 (file)
@@ -36,6 +36,8 @@
 static bool
 nir_lower_to_source_mods_block(nir_block *block)
 {
+   bool progress = false;
+
    nir_foreach_instr(instr, block) {
       if (instr->type != nir_instr_type_alu)
          continue;
@@ -91,6 +93,8 @@ nir_lower_to_source_mods_block(nir_block *block)
          if (list_empty(&parent->dest.dest.ssa.uses) &&
              list_empty(&parent->dest.dest.ssa.if_uses))
             nir_instr_remove(&parent->instr);
+
+         progress = true;
       }
 
       switch (alu->op) {
@@ -136,7 +140,7 @@ nir_lower_to_source_mods_block(nir_block *block)
          continue;
 
       bool all_children_are_sat = true;
-      nir_foreach_use(&alu->dest.dest.ssa, child_src) {
+      nir_foreach_use(child_src, &alu->dest.dest.ssa) {
          assert(child_src->is_ssa);
          nir_instr *child = child_src->parent_instr;
          if (child->type != nir_instr_type_alu) {
@@ -161,12 +165,11 @@ nir_lower_to_source_mods_block(nir_block *block)
          continue;
 
       alu->dest.saturate = true;
+      progress = true;
 
-      nir_foreach_use(&alu->dest.dest.ssa, child_src) {
+      nir_foreach_use(child_src, &alu->dest.dest.ssa) {
          assert(child_src->is_ssa);
-         nir_instr *child = child_src->parent_instr;
-         assert(child->type == nir_instr_type_alu);
-         nir_alu_instr *child_alu = nir_instr_as_alu(child);
+         nir_alu_instr *child_alu = nir_instr_as_alu(child_src->parent_instr);
 
          child_alu->op = nir_op_fmov;
          child_alu->dest.saturate = false;
@@ -178,17 +181,35 @@ nir_lower_to_source_mods_block(nir_block *block)
       }
    }
 
-   return true;
+   return progress;
 }
 
-void
+static bool
+nir_lower_to_source_mods_impl(nir_function_impl *impl)
+{
+   bool progress = false;
+
+   nir_foreach_block(block, impl) {
+      progress |= nir_lower_to_source_mods_block(block);
+   }
+
+   if (progress)
+      nir_metadata_preserve(impl, nir_metadata_block_index |
+                                  nir_metadata_dominance);
+
+   return progress;
+}
+
+bool
 nir_lower_to_source_mods(nir_shader *shader)
 {
+   bool progress = false;
+
    nir_foreach_function(function, shader) {
       if (function->impl) {
-         nir_foreach_block(block, function->impl) {
-            nir_lower_to_source_mods_block(block);
-         }
+         progress |= nir_lower_to_source_mods_impl(function->impl);
       }
    }
+
+   return progress;
 }