i965/vec4: split VEC4_OPCODE_FROM_DOUBLE into one opcode per destination's type
[mesa.git] / src / compiler / nir / nir_inline_functions.c
index 4a08dcc96e0cab90df10a8c7b1be2fd0e66683ef..b91e7bc86da8f0192eeb429f15b7e89febde4d01 100644 (file)
 #include "nir_builder.h"
 #include "nir_control_flow.h"
 
-struct inline_functions_state {
-   struct set *inlined;
-   nir_builder builder;
-   bool progress;
-};
-
 static bool inline_function_impl(nir_function_impl *impl, struct set *inlined);
 
-static bool
-rewrite_param_derefs_block(nir_block *block, void *void_state)
+static void
+convert_deref_to_param_deref(nir_instr *instr, nir_deref_var **deref,
+                             nir_call_instr *call)
 {
-   nir_call_instr *call = void_state;
+   /* This isn't a parameter, just return the deref */
+   if ((*deref)->var->data.mode != nir_var_param)
+      return;
 
-   nir_foreach_instr_safe(block, instr) {
-      if (instr->type != nir_instr_type_intrinsic)
-         continue;
+   int param_idx = (*deref)->var->data.location;
+
+   nir_deref_var *call_deref;
+   if (param_idx >= 0) {
+      assert(param_idx < call->callee->num_params);
+      call_deref = call->params[param_idx];
+   } else {
+      call_deref = call->return_deref;
+   }
+   assert(call_deref);
 
+   /* Now we make a new deref by concatenating the deref in the call's
+    * parameter with the deref we were given.
+    */
+   nir_deref_var *new_deref = nir_deref_var_clone(call_deref, instr);
+   nir_deref *new_tail = nir_deref_tail(&new_deref->deref);
+   new_tail->child = (*deref)->deref.child;
+   ralloc_steal(new_tail, new_tail->child);
+   *deref = new_deref;
+}
+
+static void
+rewrite_param_derefs(nir_instr *instr, nir_call_instr *call)
+{
+   switch (instr->type) {
+   case nir_instr_type_intrinsic: {
       nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
 
       for (unsigned i = 0;
            i < nir_intrinsic_infos[intrin->intrinsic].num_variables; i++) {
-         if (intrin->variables[i]->var->data.mode != nir_var_param)
-            continue;
-
-         int param_idx = intrin->variables[i]->var->data.location;
-
-         nir_deref_var *call_deref;
-         if (param_idx >= 0) {
-            assert(param_idx < call->callee->num_params);
-            call_deref = call->params[param_idx];
-         } else {
-            call_deref = call->return_deref;
-         }
-         assert(call_deref);
-
-         nir_deref_var *new_deref = nir_deref_as_var(nir_copy_deref(intrin, &call_deref->deref));
-         nir_deref *new_tail = nir_deref_tail(&new_deref->deref);
-         new_tail->child = intrin->variables[i]->deref.child;
-         ralloc_steal(new_tail, new_tail->child);
-         intrin->variables[i] = new_deref;
+         convert_deref_to_param_deref(instr, &intrin->variables[i], call);
       }
+      break;
    }
 
-   return true;
+   case nir_instr_type_tex: {
+      nir_tex_instr *tex = nir_instr_as_tex(instr);
+      if (tex->texture)
+         convert_deref_to_param_deref(&tex->instr, &tex->texture, call);
+      if (tex->sampler)
+         convert_deref_to_param_deref(&tex->instr, &tex->sampler, call);
+      break;
+   }
+
+   default:
+      break; /* Nothing else has derefs */
+   }
 }
 
 static void
@@ -95,11 +108,9 @@ lower_param_to_local(nir_variable *param, nir_function_impl *impl, bool write)
 }
 
 static bool
-lower_params_to_locals_block(nir_block *block, void *void_state)
+lower_params_to_locals_block(nir_block *block, nir_function_impl *impl)
 {
-   nir_function_impl *impl = void_state;
-
-   nir_foreach_instr_safe(block, instr) {
+   nir_foreach_instr(instr, block) {
       if (instr->type != nir_instr_type_intrinsic)
          continue;
 
@@ -134,12 +145,10 @@ lower_params_to_locals_block(nir_block *block, void *void_state)
 }
 
 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
@@ -147,16 +156,16 @@ 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);
@@ -181,7 +190,10 @@ inline_functions_block(nir_block *block, void *void_state)
        */
 
       /* Figure out when we need to lower to a shadow local */
-      nir_foreach_block(callee_copy, lower_params_to_locals_block, callee_copy);
+      nir_foreach_block(block, callee_copy) {
+         lower_params_to_locals_block(block, callee_copy);
+      }
+
       for (unsigned i = 0; i < callee_copy->num_params; i++) {
          nir_variable *param = callee_copy->params[i];
 
@@ -192,7 +204,10 @@ inline_functions_block(nir_block *block, void *void_state)
          }
       }
 
-      nir_foreach_block(callee_copy, rewrite_param_derefs_block, call);
+      nir_foreach_block(block, callee_copy) {
+         nir_foreach_instr(instr, block)
+            rewrite_param_derefs(instr, call);
+      }
 
       /* Pluck the body out of the function and place it here */
       nir_cf_list body;
@@ -222,7 +237,7 @@ inline_functions_block(nir_block *block, void *void_state)
       nir_instr_remove(&call->instr);
    }
 
-   return true;
+   return progress;
 }
 
 static bool
@@ -231,15 +246,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);
@@ -249,7 +264,7 @@ inline_function_impl(nir_function_impl *impl, struct set *inlined)
 
    _mesa_set_add(inlined, impl);
 
-   return state.progress;
+   return progress;
 }
 
 bool
@@ -259,7 +274,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;
    }