nir: Copy propagation between blocks
[mesa.git] / src / compiler / nir / nir_inline_functions.c
index b343eb735b19f3819fb270f6dd1874a475b6b3ea..06c90d939564860ca44ccbcd7f64a9efcfe33d0b 100644 (file)
 #include "nir.h"
 #include "nir_builder.h"
 #include "nir_control_flow.h"
-
-struct inline_functions_state {
-   struct set *inlined;
-   nir_builder builder;
-   bool progress;
-};
+#include "nir_vla.h"
 
 static bool inline_function_impl(nir_function_impl *impl, struct set *inlined);
 
 static bool
-inline_functions_block(nir_block *block, void *void_state)
+inline_functions_block(nir_block *block, nir_builder *b,
+                       struct set *inlined)
 {
-   struct inline_functions_state *state = void_state;
-
-   nir_builder *b = &state->builder;
-
+   bool progress = false;
    /* This is tricky.  We're iterating over instructions in a block but, as
     * we go, the block and its instruction list are being split into
     * pieces.  However, this *should* be safe since foreach_safe always
@@ -47,40 +40,58 @@ inline_functions_block(nir_block *block, void *void_state)
     * properly get moved to the next block when it gets split, and we
     * continue iterating there.
     */
-   nir_foreach_instr_safe(block, instr) {
+   nir_foreach_instr_safe(instr, block) {
       if (instr->type != nir_instr_type_call)
          continue;
 
-      state->progress = true;
+      progress = true;
 
       nir_call_instr *call = nir_instr_as_call(instr);
       assert(call->callee->impl);
 
-      inline_function_impl(call->callee->impl, state->inlined);
+      inline_function_impl(call->callee->impl, inlined);
 
       nir_function_impl *callee_copy =
          nir_function_impl_clone(call->callee->impl);
+      callee_copy->function = call->callee;
 
       exec_list_append(&b->impl->locals, &callee_copy->locals);
       exec_list_append(&b->impl->registers, &callee_copy->registers);
 
       b->cursor = nir_before_instr(&call->instr);
 
-      /* Add copies of all in parameters */
-      assert(call->num_params == callee_copy->num_params);
-      for (unsigned i = 0; i < callee_copy->num_params; i++) {
-         nir_variable *param = callee_copy->params[i];
-
-         /* Turn it into a local variable */
-         param->data.mode = nir_var_local;
-         exec_list_push_head(&b->impl->locals, &param->node);
-
-         /* Only in or inout parameters */
-         if (call->callee->params[i].param_type == nir_parameter_out)
-            continue;
+      /* Rewrite all of the uses of the callee's parameters to use the call
+       * instructions sources.  In order to ensure that the "load" happens
+       * here and not later (for register sources), we make sure to convert it
+       * to an SSA value first.
+       */
+      const unsigned num_params = call->num_params;
+      NIR_VLA(nir_ssa_def *, params, num_params);
+      for (unsigned i = 0; i < num_params; i++) {
+         params[i] = nir_ssa_for_src(b, call->params[i],
+                                     call->callee->params[i].num_components);
+      }
 
-         nir_copy_deref_var(b, nir_deref_var_create(b->shader, param),
-                               call->params[i]);
+      nir_foreach_block(block, callee_copy) {
+         nir_foreach_instr_safe(instr, block) {
+            if (instr->type != nir_instr_type_intrinsic)
+               continue;
+
+            nir_intrinsic_instr *load = nir_instr_as_intrinsic(instr);
+            if (load->intrinsic != nir_intrinsic_load_param)
+               continue;
+
+            unsigned param_idx = nir_intrinsic_param_idx(load);
+            assert(param_idx < num_params);
+            assert(load->dest.is_ssa);
+            nir_ssa_def_rewrite_uses(&load->dest.ssa,
+                                     nir_src_for_ssa(params[param_idx]));
+
+            /* Remove any left-over load_param intrinsics because they're soon
+             * to be in another function and therefore no longer valid.
+             */
+            nir_instr_remove(&load->instr);
+         }
       }
 
       /* Pluck the body out of the function and place it here */
@@ -88,33 +99,10 @@ inline_functions_block(nir_block *block, void *void_state)
       nir_cf_list_extract(&body, &callee_copy->body);
       nir_cf_reinsert(&body, b->cursor);
 
-      b->cursor = nir_before_instr(&call->instr);
-
-      /* Add copies of all out parameters and the return */
-      assert(call->num_params == callee_copy->num_params);
-      for (unsigned i = 0; i < callee_copy->num_params; i++) {
-         /* Only out or inout parameters */
-         if (call->callee->params[i].param_type == nir_parameter_in)
-            continue;
-
-         nir_copy_deref_var(b, call->params[i],
-                               nir_deref_var_create(b->shader,
-                                                    callee_copy->params[i]));
-      }
-      if (!glsl_type_is_void(call->callee->return_type)) {
-         /* Turn it into a local variable */
-         callee_copy->return_var->data.mode = nir_var_local;
-         exec_list_push_head(&b->impl->locals, &callee_copy->return_var->node);
-
-         nir_copy_deref_var(b, call->return_deref,
-                               nir_deref_var_create(b->shader,
-                                                    callee_copy->return_var));
-      }
-
       nir_instr_remove(&call->instr);
    }
 
-   return true;
+   return progress;
 }
 
 static bool
@@ -123,15 +111,15 @@ inline_function_impl(nir_function_impl *impl, struct set *inlined)
    if (_mesa_set_search(inlined, impl))
       return false; /* Already inlined */
 
-   struct inline_functions_state state;
+   nir_builder b;
+   nir_builder_init(&b, impl);
 
-   state.inlined = inlined;
-   state.progress = false;
-   nir_builder_init(&state.builder, impl);
-
-   nir_foreach_block(impl, inline_functions_block, &state);
+   bool progress = false;
+   nir_foreach_block_safe(block, impl) {
+      progress |= inline_functions_block(block, &b, inlined);
+   }
 
-   if (state.progress) {
+   if (progress) {
       /* SSA and register indices are completely messed up now */
       nir_index_ssa_defs(impl);
       nir_index_local_regs(impl);
@@ -141,7 +129,7 @@ inline_function_impl(nir_function_impl *impl, struct set *inlined)
 
    _mesa_set_add(inlined, impl);
 
-   return state.progress;
+   return progress;
 }
 
 bool
@@ -151,7 +139,7 @@ nir_inline_functions(nir_shader *shader)
                                           _mesa_key_pointer_equal);
    bool progress = false;
 
-   nir_foreach_function(shader, function) {
+   nir_foreach_function(function, shader) {
       if (function->impl)
          progress = inline_function_impl(function->impl, inlined) || progress;
    }