Merge remote-tracking branch 'mesa-public/master' into vulkan
[mesa.git] / src / glsl / lower_subroutine.cpp
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 "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:
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 bool progress;
48 struct _mesa_glsl_parse_state *state;
49 };
50
51 }
52
53 bool
54 lower_subroutine(exec_list *instructions, struct _mesa_glsl_parse_state *state)
55 {
56 lower_subroutine_visitor v(state);
57 visit_list_elements(&v, instructions);
58 return v.progress;
59 }
60
61 ir_visitor_status
62 lower_subroutine_visitor::visit_leave(ir_call *ir)
63 {
64 if (!ir->sub_var)
65 return visit_continue;
66
67 void *mem_ctx = ralloc_parent(ir);
68 ir_if *last_branch = NULL;
69 ir_dereference_variable *return_deref = ir->return_deref;
70
71 for (int s = this->state->num_subroutines - 1; s >= 0; s--) {
72 ir_rvalue *var;
73 ir_constant *lc = new(mem_ctx)ir_constant(s);
74 ir_function *fn = this->state->subroutines[s];
75 bool is_compat = false;
76
77 for (int i = 0; i < fn->num_subroutine_types; i++) {
78 if (ir->sub_var->type->without_array() == fn->subroutine_types[i]) {
79 is_compat = true;
80 break;
81 }
82 }
83 if (is_compat == false)
84 continue;
85
86 if (ir->array_idx != NULL)
87 var = new(mem_ctx) ir_dereference_array(ir->sub_var, ir->array_idx->clone(mem_ctx, NULL));
88 else
89 var = new(mem_ctx) ir_dereference_variable(ir->sub_var);
90
91 ir_function_signature *sub_sig =
92 fn->exact_matching_signature(this->state,
93 &ir->actual_parameters);
94
95 ir_call *new_call = new(mem_ctx) ir_call(sub_sig, return_deref, &ir->actual_parameters);
96 if (!last_branch)
97 last_branch = if_tree(equal(subr_to_int(var), lc), new_call);
98 else
99 last_branch = if_tree(equal(subr_to_int(var), lc), new_call, last_branch);
100
101 if (return_deref && s > 0)
102 return_deref = return_deref->clone(mem_ctx, NULL);
103 }
104 if (last_branch)
105 ir->insert_before(last_branch);
106 ir->remove();
107
108 return visit_continue;
109 }