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 "program/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
*);
62 automatic_inlining_predicate(ir_instruction
*ir
)
64 ir_call
*call
= ir
->as_call();
66 if (call
&& can_inline(call
))
73 do_function_inlining(exec_list
*instructions
)
75 ir_function_inlining_visitor v
;
77 do_expression_flattening(instructions
, automatic_inlining_predicate
);
85 replace_return_with_assignment(ir_instruction
*ir
, void *data
)
87 void *ctx
= talloc_parent(ir
);
88 ir_variable
*retval
= (ir_variable
*)data
;
89 ir_return
*ret
= ir
->as_return();
93 ir_rvalue
*lhs
= new(ctx
) ir_dereference_variable(retval
);
94 ret
->replace_with(new(ctx
) ir_assignment(lhs
, ret
->value
, NULL
));
96 /* un-valued return has to be the last return, or we shouldn't
97 * have reached here. (see can_inline()).
99 assert(!ret
->next
->is_tail_sentinel());
106 ir_call::generate_inline(ir_instruction
*next_ir
)
108 void *ctx
= talloc_parent(this);
109 ir_variable
**parameters
;
112 ir_variable
*retval
= NULL
;
113 struct hash_table
*ht
;
115 ht
= hash_table_ctor(0, hash_table_pointer_hash
, hash_table_pointer_compare
);
118 foreach_iter(exec_list_iterator
, iter_sig
, this->callee
->parameters
)
121 parameters
= new ir_variable
*[num_parameters
];
123 /* Generate storage for the return value. */
124 if (this->callee
->return_type
) {
125 retval
= new(ctx
) ir_variable(this->callee
->return_type
, "__retval",
127 next_ir
->insert_before(retval
);
130 /* Generate the declarations for the parameters to our inlined code,
131 * and set up the mapping of real function body variables to ours.
134 exec_list_iterator sig_param_iter
= this->callee
->parameters
.iterator();
135 exec_list_iterator param_iter
= this->actual_parameters
.iterator();
136 for (i
= 0; i
< num_parameters
; i
++) {
137 ir_variable
*sig_param
= (ir_variable
*) sig_param_iter
.get();
138 ir_rvalue
*param
= (ir_rvalue
*) param_iter
.get();
140 /* Generate a new variable for the parameter. */
141 if (sig_param
->type
->base_type
== GLSL_TYPE_SAMPLER
) {
142 /* For samplers, we want the inlined sampler references
143 * referencing the passed in sampler variable, since that
144 * will have the location information, which an assignment of
145 * a sampler wouldn't.
147 parameters
[i
] = NULL
;
148 hash_table_insert(ht
, param
->variable_referenced(), sig_param
);
150 parameters
[i
] = sig_param
->clone(ctx
, ht
);
151 parameters
[i
]->mode
= ir_var_auto
;
152 next_ir
->insert_before(parameters
[i
]);
155 /* Move the actual param into our param variable if it's an 'in' type. */
156 if (parameters
[i
] && (sig_param
->mode
== ir_var_in
||
157 sig_param
->mode
== ir_var_inout
)) {
158 ir_assignment
*assign
;
160 assign
= new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(parameters
[i
]),
162 next_ir
->insert_before(assign
);
165 sig_param_iter
.next();
169 /* Generate the inlined body of the function. */
170 foreach_iter(exec_list_iterator
, iter
, callee
->body
) {
171 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
172 ir_instruction
*new_ir
= ir
->clone(ctx
, ht
);
174 next_ir
->insert_before(new_ir
);
175 visit_tree(new_ir
, replace_return_with_assignment
, retval
);
178 /* Copy back the value of any 'out' parameters from the function body
179 * variables to our own.
182 param_iter
= this->actual_parameters
.iterator();
183 sig_param_iter
= this->callee
->parameters
.iterator();
184 for (i
= 0; i
< num_parameters
; i
++) {
185 ir_instruction
*const param
= (ir_instruction
*) param_iter
.get();
186 const ir_variable
*const sig_param
= (ir_variable
*) sig_param_iter
.get();
188 /* Move our param variable into the actual param if it's an 'out' type. */
189 if (parameters
[i
] && (sig_param
->mode
== ir_var_out
||
190 sig_param
->mode
== ir_var_inout
)) {
191 ir_assignment
*assign
;
193 assign
= new(ctx
) ir_assignment(param
->clone(ctx
, NULL
)->as_rvalue(),
194 new(ctx
) ir_dereference_variable(parameters
[i
]),
196 next_ir
->insert_before(assign
);
200 sig_param_iter
.next();
203 delete [] parameters
;
208 return new(ctx
) ir_dereference_variable(retval
);
215 ir_function_inlining_visitor::visit_enter(ir_expression
*ir
)
218 return visit_continue_with_parent
;
223 ir_function_inlining_visitor::visit_enter(ir_return
*ir
)
226 return visit_continue_with_parent
;
231 ir_function_inlining_visitor::visit_enter(ir_texture
*ir
)
234 return visit_continue_with_parent
;
239 ir_function_inlining_visitor::visit_enter(ir_swizzle
*ir
)
242 return visit_continue_with_parent
;
247 ir_function_inlining_visitor::visit_enter(ir_call
*ir
)
249 if (can_inline(ir
)) {
250 /* If the call was part of some tree, then it should have been
251 * flattened out or we shouldn't have seen it because of a
252 * visit_continue_with_parent in this visitor.
254 assert(ir
== base_ir
);
256 (void) ir
->generate_inline(ir
);
258 this->progress
= true;
261 return visit_continue
;
266 ir_function_inlining_visitor::visit_enter(ir_assignment
*ir
)
268 ir_call
*call
= ir
->rhs
->as_call();
269 if (!call
|| !can_inline(call
))
270 return visit_continue
;
272 /* generates the parameter setup, function body, and returns the return
273 * value of the function
275 ir_rvalue
*rhs
= call
->generate_inline(ir
);
279 this->progress
= true;
281 return visit_continue
;