101e04c3fSmrg/*
201e04c3fSmrg * Copyright © 2015 Red Hat
301e04c3fSmrg *
401e04c3fSmrg * Permission is hereby granted, free of charge, to any person obtaining a
501e04c3fSmrg * copy of this software and associated documentation files (the "Software"),
601e04c3fSmrg * to deal in the Software without restriction, including without limitation
701e04c3fSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
801e04c3fSmrg * and/or sell copies of the Software, and to permit persons to whom the
901e04c3fSmrg * Software is furnished to do so, subject to the following conditions:
1001e04c3fSmrg *
1101e04c3fSmrg * The above copyright notice and this permission notice (including the next
1201e04c3fSmrg * paragraph) shall be included in all copies or substantial portions of the
1301e04c3fSmrg * Software.
1401e04c3fSmrg *
1501e04c3fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1601e04c3fSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1701e04c3fSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1801e04c3fSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1901e04c3fSmrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2001e04c3fSmrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2101e04c3fSmrg * DEALINGS IN THE SOFTWARE.
2201e04c3fSmrg */
2301e04c3fSmrg
2401e04c3fSmrg/**
2501e04c3fSmrg * \file lower_subroutine.cpp
2601e04c3fSmrg *
2701e04c3fSmrg * lowers subroutines to an if ladder.
2801e04c3fSmrg */
2901e04c3fSmrg
3001e04c3fSmrg#include "compiler/glsl_types.h"
3101e04c3fSmrg#include "glsl_parser_extras.h"
3201e04c3fSmrg#include "ir.h"
3301e04c3fSmrg#include "ir_builder.h"
3401e04c3fSmrg
3501e04c3fSmrgusing namespace ir_builder;
3601e04c3fSmrgnamespace {
3701e04c3fSmrg
3801e04c3fSmrgclass lower_subroutine_visitor : public ir_hierarchical_visitor {
3901e04c3fSmrgpublic:
4001e04c3fSmrg   lower_subroutine_visitor(struct _mesa_glsl_parse_state *state)
4101e04c3fSmrg      : state(state)
4201e04c3fSmrg   {
4301e04c3fSmrg      this->progress = false;
4401e04c3fSmrg   }
4501e04c3fSmrg
4601e04c3fSmrg   ir_visitor_status visit_leave(ir_call *);
4701e04c3fSmrg   ir_call *call_clone(ir_call *call, ir_function_signature *callee);
4801e04c3fSmrg   bool progress;
4901e04c3fSmrg   struct _mesa_glsl_parse_state *state;
5001e04c3fSmrg};
5101e04c3fSmrg
5201e04c3fSmrg}
5301e04c3fSmrg
5401e04c3fSmrgbool
5501e04c3fSmrglower_subroutine(exec_list *instructions, struct _mesa_glsl_parse_state *state)
5601e04c3fSmrg{
5701e04c3fSmrg   lower_subroutine_visitor v(state);
5801e04c3fSmrg   visit_list_elements(&v, instructions);
5901e04c3fSmrg   return v.progress;
6001e04c3fSmrg}
6101e04c3fSmrg
6201e04c3fSmrgir_call *
6301e04c3fSmrglower_subroutine_visitor::call_clone(ir_call *call, ir_function_signature *callee)
6401e04c3fSmrg{
6501e04c3fSmrg   void *mem_ctx = ralloc_parent(call);
6601e04c3fSmrg   ir_dereference_variable *new_return_ref = NULL;
6701e04c3fSmrg   if (call->return_deref != NULL)
6801e04c3fSmrg      new_return_ref = call->return_deref->clone(mem_ctx, NULL);
6901e04c3fSmrg
7001e04c3fSmrg   exec_list new_parameters;
7101e04c3fSmrg
7201e04c3fSmrg   foreach_in_list(ir_instruction, ir, &call->actual_parameters) {
7301e04c3fSmrg      new_parameters.push_tail(ir->clone(mem_ctx, NULL));
7401e04c3fSmrg   }
7501e04c3fSmrg
7601e04c3fSmrg   return new(mem_ctx) ir_call(callee, new_return_ref, &new_parameters);
7701e04c3fSmrg}
7801e04c3fSmrg
7901e04c3fSmrgir_visitor_status
8001e04c3fSmrglower_subroutine_visitor::visit_leave(ir_call *ir)
8101e04c3fSmrg{
8201e04c3fSmrg   if (!ir->sub_var)
8301e04c3fSmrg      return visit_continue;
8401e04c3fSmrg
8501e04c3fSmrg   void *mem_ctx = ralloc_parent(ir);
8601e04c3fSmrg   ir_if *last_branch = NULL;
8701e04c3fSmrg
8801e04c3fSmrg   for (int s = this->state->num_subroutines - 1; s >= 0; s--) {
8901e04c3fSmrg      ir_rvalue *var;
9001e04c3fSmrg      ir_function *fn = this->state->subroutines[s];
9101e04c3fSmrg      ir_constant *lc = new(mem_ctx)ir_constant(fn->subroutine_index);
9201e04c3fSmrg
9301e04c3fSmrg      bool is_compat = false;
9401e04c3fSmrg
9501e04c3fSmrg      for (int i = 0; i < fn->num_subroutine_types; i++) {
9601e04c3fSmrg         if (ir->sub_var->type->without_array() == fn->subroutine_types[i]) {
9701e04c3fSmrg            is_compat = true;
9801e04c3fSmrg            break;
9901e04c3fSmrg         }
10001e04c3fSmrg      }
10101e04c3fSmrg      if (is_compat == false)
10201e04c3fSmrg         continue;
10301e04c3fSmrg
10401e04c3fSmrg      if (ir->array_idx != NULL)
10501e04c3fSmrg         var = ir->array_idx->clone(mem_ctx, NULL);
10601e04c3fSmrg      else
10701e04c3fSmrg         var = new(mem_ctx) ir_dereference_variable(ir->sub_var);
10801e04c3fSmrg
10901e04c3fSmrg      ir_function_signature *sub_sig =
11001e04c3fSmrg         fn->exact_matching_signature(this->state,
11101e04c3fSmrg                                      &ir->actual_parameters);
11201e04c3fSmrg
11301e04c3fSmrg      ir_call *new_call = call_clone(ir, sub_sig);
11401e04c3fSmrg      if (!last_branch)
11501e04c3fSmrg         last_branch = if_tree(equal(subr_to_int(var), lc), new_call);
11601e04c3fSmrg      else
11701e04c3fSmrg         last_branch = if_tree(equal(subr_to_int(var), lc), new_call, last_branch);
11801e04c3fSmrg   }
11901e04c3fSmrg   if (last_branch)
12001e04c3fSmrg      ir->insert_before(last_branch);
12101e04c3fSmrg   ir->remove();
12201e04c3fSmrg
12301e04c3fSmrg   return visit_continue;
12401e04c3fSmrg}
125