2 * Copyright © 2015 Red Hat
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:
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
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.
25 * \file lower_subroutine.cpp
27 * lowers subroutines to an if ladder.
30 #include "compiler/glsl_types.h"
31 #include "glsl_parser_extras.h"
33 #include "ir_builder.h"
35 using namespace ir_builder
;
38 class lower_subroutine_visitor
: public ir_hierarchical_visitor
{
40 lower_subroutine_visitor(struct _mesa_glsl_parse_state
*state
)
43 this->progress
= false;
46 ir_visitor_status
visit_leave(ir_call
*);
47 ir_call
*call_clone(ir_call
*call
, ir_function_signature
*callee
);
49 struct _mesa_glsl_parse_state
*state
;
55 lower_subroutine(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
)
57 lower_subroutine_visitor
v(state
);
58 visit_list_elements(&v
, instructions
);
63 lower_subroutine_visitor::call_clone(ir_call
*call
, ir_function_signature
*callee
)
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
);
70 exec_list new_parameters
;
72 foreach_in_list(ir_instruction
, ir
, &call
->actual_parameters
) {
73 new_parameters
.push_tail(ir
->clone(mem_ctx
, NULL
));
76 return new(mem_ctx
) ir_call(callee
, new_return_ref
, &new_parameters
);
80 lower_subroutine_visitor::visit_leave(ir_call
*ir
)
83 return visit_continue
;
85 void *mem_ctx
= ralloc_parent(ir
);
86 ir_if
*last_branch
= NULL
;
88 for (int s
= this->state
->num_subroutines
- 1; s
>= 0; s
--) {
90 ir_function
*fn
= this->state
->subroutines
[s
];
91 ir_constant
*lc
= new(mem_ctx
)ir_constant(fn
->subroutine_index
);
93 bool is_compat
= false;
95 for (int i
= 0; i
< fn
->num_subroutine_types
; i
++) {
96 if (ir
->sub_var
->type
->without_array() == fn
->subroutine_types
[i
]) {
101 if (is_compat
== false)
104 if (ir
->array_idx
!= NULL
)
105 var
= ir
->array_idx
->clone(mem_ctx
, NULL
);
107 var
= new(mem_ctx
) ir_dereference_variable(ir
->sub_var
);
109 ir_function_signature
*sub_sig
=
110 fn
->exact_matching_signature(this->state
,
111 &ir
->actual_parameters
);
113 ir_call
*new_call
= call_clone(ir
, sub_sig
);
115 last_branch
= if_tree(equal(subr_to_int(var
), lc
), new_call
);
117 last_branch
= if_tree(equal(subr_to_int(var
), lc
), new_call
, last_branch
);
120 ir
->insert_before(last_branch
);
123 return visit_continue
;