nir/lower_system_values: Refactor and use the builder.
authorJason Ekstrand <jason.ekstrand@intel.com>
Tue, 15 Dec 2015 02:56:59 +0000 (18:56 -0800)
committerJason Ekstrand <jason.ekstrand@intel.com>
Tue, 15 Dec 2015 22:12:31 +0000 (14:12 -0800)
Now that we have a helper in the builder for system values and a helper in
core NIR to get the intrinsic opcode, there's really no point in having
things split out into a helper function.  This commit "modernizes" this
pass to use helpers better and look more like newer passes.

Reviewed-by: Eric Anholt <eric@anholt.net>
src/glsl/nir/nir_lower_system_values.c

index 03a98147ba953d828c04082d36a4b24a2e2987f9..402f98e319c6f8f9cc435a46266f663936aa815b 100644 (file)
  */
 
 #include "nir.h"
-#include "main/mtypes.h"
+#include "nir_builder.h"
+
+struct lower_system_values_state {
+   nir_builder builder;
+   bool progress;
+};
 
 static bool
-convert_instr(nir_intrinsic_instr *instr)
+convert_block(nir_block *block, void *void_state)
 {
-   if (instr->intrinsic != nir_intrinsic_load_var)
-      return false;
+   struct lower_system_values_state *state = void_state;
 
-   nir_variable *var = instr->variables[0]->var;
-   if (var->data.mode != nir_var_system_value)
-      return false;
+   nir_builder *b = &state->builder;
 
-   void *mem_ctx = ralloc_parent(instr);
+   nir_foreach_instr_safe(block, instr) {
+      if (instr->type != nir_instr_type_intrinsic)
+         continue;
 
-   assert(instr->dest.is_ssa);
+      nir_intrinsic_instr *load_var = nir_instr_as_intrinsic(instr);
 
-   nir_intrinsic_op op = nir_intrinsic_from_system_value(var->data.location);
-   nir_intrinsic_instr *new_instr = nir_intrinsic_instr_create(mem_ctx, op);
+      if (load_var->intrinsic != nir_intrinsic_load_var)
+         continue;
 
-   nir_ssa_dest_init(&new_instr->instr, &new_instr->dest,
-                     instr->dest.ssa.num_components, NULL);
-   nir_ssa_def_rewrite_uses(&instr->dest.ssa,
-                            nir_src_for_ssa(&new_instr->dest.ssa));
+      nir_variable *var = load_var->variables[0]->var;
+      if (var->data.mode != nir_var_system_value)
+         continue;
 
-   nir_instr_insert_before(&instr->instr, &new_instr->instr);
-   nir_instr_remove(&instr->instr);
+      b->cursor = nir_after_instr(&load_var->instr);
 
-   return true;
-}
+      nir_intrinsic_op sysval_op =
+         nir_intrinsic_from_system_value(var->data.location);
+      nir_ssa_def *sysval = nir_load_system_value(b, sysval_op, 0);
 
-static bool
-convert_block(nir_block *block, void *state)
-{
-   bool *progress = state;
+      nir_ssa_def_rewrite_uses(&load_var->dest.ssa, nir_src_for_ssa(sysval));
+      nir_instr_remove(&load_var->instr);
 
-   nir_foreach_instr_safe(block, instr) {
-      if (instr->type == nir_instr_type_intrinsic)
-         *progress = convert_instr(nir_instr_as_intrinsic(instr)) || *progress;
+      state->progress = true;
    }
 
    return true;
@@ -72,12 +71,15 @@ convert_block(nir_block *block, void *state)
 static bool
 convert_impl(nir_function_impl *impl)
 {
-   bool progress = false;
+   struct lower_system_values_state state;
 
-   nir_foreach_block(impl, convert_block, &progress);
+   state.progress = false;
+   nir_builder_init(&state.builder, impl);
+
+   nir_foreach_block(impl, convert_block, &state);
    nir_metadata_preserve(impl, nir_metadata_block_index |
                                nir_metadata_dominance);
-   return progress;
+   return state.progress;
 }
 
 bool