• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2015 Red Hat
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 /**
25  * \file lower_subroutine.cpp
26  *
27  * lowers subroutines to an if ladder.
28  */
29 
30 #include "compiler/glsl_types.h"
31 #include "glsl_parser_extras.h"
32 #include "ir.h"
33 #include "ir_builder.h"
34 
35 using namespace ir_builder;
36 namespace {
37 
38 class lower_subroutine_visitor : public ir_hierarchical_visitor {
39 public:
lower_subroutine_visitor(struct _mesa_glsl_parse_state * state)40    lower_subroutine_visitor(struct _mesa_glsl_parse_state *state)
41       : state(state)
42    {
43       this->progress = false;
44    }
45 
46    ir_visitor_status visit_leave(ir_call *);
47    ir_call *call_clone(ir_call *call, ir_function_signature *callee);
48    bool progress;
49    struct _mesa_glsl_parse_state *state;
50 };
51 
52 }
53 
54 bool
lower_subroutine(exec_list * instructions,struct _mesa_glsl_parse_state * state)55 lower_subroutine(exec_list *instructions, struct _mesa_glsl_parse_state *state)
56 {
57    lower_subroutine_visitor v(state);
58    visit_list_elements(&v, instructions);
59    return v.progress;
60 }
61 
62 ir_call *
call_clone(ir_call * call,ir_function_signature * callee)63 lower_subroutine_visitor::call_clone(ir_call *call, ir_function_signature *callee)
64 {
65    void *mem_ctx = ralloc_parent(call);
66    ir_dereference_variable *new_return_ref = NULL;
67    if (call->return_deref != NULL)
68       new_return_ref = call->return_deref->clone(mem_ctx, NULL);
69 
70    exec_list new_parameters;
71 
72    foreach_in_list(ir_instruction, ir, &call->actual_parameters) {
73       new_parameters.push_tail(ir->clone(mem_ctx, NULL));
74    }
75 
76    return new(mem_ctx) ir_call(callee, new_return_ref, &new_parameters);
77 }
78 
79 ir_visitor_status
visit_leave(ir_call * ir)80 lower_subroutine_visitor::visit_leave(ir_call *ir)
81 {
82    if (!ir->sub_var)
83       return visit_continue;
84 
85    void *mem_ctx = ralloc_parent(ir);
86    ir_if *last_branch = NULL;
87 
88    for (int s = this->state->num_subroutines - 1; s >= 0; s--) {
89       ir_rvalue *var;
90       ir_function *fn = this->state->subroutines[s];
91       ir_constant *lc = new(mem_ctx)ir_constant(fn->subroutine_index);
92 
93       bool is_compat = false;
94 
95       for (int i = 0; i < fn->num_subroutine_types; i++) {
96          if (ir->sub_var->type->without_array() == fn->subroutine_types[i]) {
97             is_compat = true;
98             break;
99          }
100       }
101       if (is_compat == false)
102          continue;
103 
104       if (ir->array_idx != NULL)
105          var = ir->array_idx->clone(mem_ctx, NULL);
106       else
107          var = new(mem_ctx) ir_dereference_variable(ir->sub_var);
108 
109       ir_function_signature *sub_sig =
110          fn->exact_matching_signature(this->state,
111                                       &ir->actual_parameters);
112 
113       ir_call *new_call = call_clone(ir, sub_sig);
114       if (!last_branch)
115          last_branch = if_tree(equal(subr_to_int(var), lc), new_call);
116       else
117          last_branch = if_tree(equal(subr_to_int(var), lc), new_call, last_branch);
118    }
119    if (last_branch)
120       ir->insert_before(last_branch);
121    ir->remove();
122 
123    return visit_continue;
124 }
125