2 * Copyright © 2010 Intel Corporation
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 ir_function_inlining.cpp
27 * Replaces calls to functions with the body of the function.
32 #include "ir_visitor.h"
33 #include "ir_function_inlining.h"
34 #include "ir_expression_flattening.h"
35 #include "glsl_types.h"
36 #include "hash_table.h"
38 class ir_function_inlining_visitor
: public ir_hierarchical_visitor
{
40 ir_function_inlining_visitor()
45 virtual ~ir_function_inlining_visitor()
50 virtual ir_visitor_status
visit_enter(ir_expression
*);
51 virtual ir_visitor_status
visit_enter(ir_call
*);
52 virtual ir_visitor_status
visit_enter(ir_assignment
*);
53 virtual ir_visitor_status
visit_enter(ir_return
*);
54 virtual ir_visitor_status
visit_enter(ir_texture
*);
55 virtual ir_visitor_status
visit_enter(ir_swizzle
*);
61 unsigned int hash_func(const void *key
)
63 return (unsigned int)(uintptr_t)key
;
66 int hash_compare_func(const void *key1
, const void *key2
)
68 return key1
== key2
? 0 : 1;
72 automatic_inlining_predicate(ir_instruction
*ir
)
74 ir_call
*call
= ir
->as_call();
76 if (call
&& can_inline(call
))
83 do_function_inlining(exec_list
*instructions
)
85 ir_function_inlining_visitor v
;
87 do_expression_flattening(instructions
, automatic_inlining_predicate
);
95 replace_return_with_assignment(ir_instruction
*ir
, void *data
)
97 ir_variable
*retval
= (ir_variable
*)data
;
98 ir_return
*ret
= ir
->as_return();
102 ir_rvalue
*lhs
= new ir_dereference_variable(retval
);
103 ret
->insert_before(new ir_assignment(lhs
, ret
->value
, NULL
));
106 /* un-valued return has to be the last return, or we shouldn't
107 * have reached here. (see can_inline()).
109 assert(!ret
->next
->is_tail_sentinal());
115 ir_call::generate_inline(ir_instruction
*next_ir
)
117 ir_variable
**parameters
;
120 ir_variable
*retval
= NULL
;
121 struct hash_table
*ht
;
123 ht
= hash_table_ctor(0, hash_func
, hash_compare_func
);
126 foreach_iter(exec_list_iterator
, iter_sig
, this->callee
->parameters
)
129 parameters
= new ir_variable
*[num_parameters
];
131 /* Generate storage for the return value. */
132 if (this->callee
->return_type
) {
133 retval
= new ir_variable(this->callee
->return_type
, "__retval");
134 next_ir
->insert_before(retval
);
137 /* Generate the declarations for the parameters to our inlined code,
138 * and set up the mapping of real function body variables to ours.
141 exec_list_iterator sig_param_iter
= this->callee
->parameters
.iterator();
142 exec_list_iterator param_iter
= this->actual_parameters
.iterator();
143 for (i
= 0; i
< num_parameters
; i
++) {
144 const ir_variable
*const sig_param
= (ir_variable
*) sig_param_iter
.get();
145 ir_rvalue
*param
= (ir_rvalue
*) param_iter
.get();
147 /* Generate a new variable for the parameter. */
148 parameters
[i
] = (ir_variable
*)sig_param
->clone(ht
);
149 next_ir
->insert_before(parameters
[i
]);
151 /* Move the actual param into our param variable if it's an 'in' type. */
152 if (parameters
[i
]->mode
== ir_var_in
||
153 parameters
[i
]->mode
== ir_var_inout
) {
154 ir_assignment
*assign
;
156 assign
= new ir_assignment(new ir_dereference_variable(parameters
[i
]),
158 next_ir
->insert_before(assign
);
161 sig_param_iter
.next();
165 /* Generate the inlined body of the function. */
166 foreach_iter(exec_list_iterator
, iter
, callee
->body
) {
167 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
168 ir_instruction
*new_ir
= ir
->clone(ht
);
170 next_ir
->insert_before(new_ir
);
171 visit_tree(new_ir
, replace_return_with_assignment
, retval
);
174 /* Copy back the value of any 'out' parameters from the function body
175 * variables to our own.
178 param_iter
= this->actual_parameters
.iterator();
179 for (i
= 0; i
< num_parameters
; i
++) {
180 ir_instruction
*const param
= (ir_instruction
*) param_iter
.get();
182 /* Move our param variable into the actual param if it's an 'out' type. */
183 if (parameters
[i
]->mode
== ir_var_out
||
184 parameters
[i
]->mode
== ir_var_inout
) {
185 ir_assignment
*assign
;
187 assign
= new ir_assignment(param
->as_rvalue(),
188 new ir_dereference_variable(parameters
[i
]),
190 next_ir
->insert_before(assign
);
196 delete [] parameters
;
201 return new ir_dereference_variable(retval
);
208 ir_function_inlining_visitor::visit_enter(ir_expression
*ir
)
211 return visit_continue_with_parent
;
216 ir_function_inlining_visitor::visit_enter(ir_return
*ir
)
219 return visit_continue_with_parent
;
224 ir_function_inlining_visitor::visit_enter(ir_texture
*ir
)
227 return visit_continue_with_parent
;
232 ir_function_inlining_visitor::visit_enter(ir_swizzle
*ir
)
235 return visit_continue_with_parent
;
240 ir_function_inlining_visitor::visit_enter(ir_call
*ir
)
242 if (can_inline(ir
)) {
243 (void) ir
->generate_inline(ir
);
245 this->progress
= true;
248 return visit_continue
;
253 ir_function_inlining_visitor::visit_enter(ir_assignment
*ir
)
255 ir_call
*call
= ir
->rhs
->as_call();
256 if (!call
|| !can_inline(call
))
257 return visit_continue
;
259 /* generates the parameter setup, function body, and returns the return
260 * value of the function
262 ir_rvalue
*rhs
= call
->generate_inline(ir
);
266 this->progress
= true;
268 return visit_continue
;