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"
39 do_sampler_replacement(exec_list
*instructions
,
41 ir_dereference
*deref
);
43 class ir_function_inlining_visitor
: public ir_hierarchical_visitor
{
45 ir_function_inlining_visitor()
50 virtual ~ir_function_inlining_visitor()
55 virtual ir_visitor_status
visit_enter(ir_expression
*);
56 virtual ir_visitor_status
visit_enter(ir_call
*);
57 virtual ir_visitor_status
visit_enter(ir_assignment
*);
58 virtual ir_visitor_status
visit_enter(ir_return
*);
59 virtual ir_visitor_status
visit_enter(ir_texture
*);
60 virtual ir_visitor_status
visit_enter(ir_swizzle
*);
67 automatic_inlining_predicate(ir_instruction
*ir
)
69 ir_call
*call
= ir
->as_call();
71 if (call
&& can_inline(call
))
78 do_function_inlining(exec_list
*instructions
)
80 ir_function_inlining_visitor v
;
82 do_expression_flattening(instructions
, automatic_inlining_predicate
);
90 replace_return_with_assignment(ir_instruction
*ir
, void *data
)
92 void *ctx
= talloc_parent(ir
);
93 ir_variable
*retval
= (ir_variable
*)data
;
94 ir_return
*ret
= ir
->as_return();
98 ir_rvalue
*lhs
= new(ctx
) ir_dereference_variable(retval
);
99 ret
->replace_with(new(ctx
) ir_assignment(lhs
, ret
->value
, NULL
));
101 /* un-valued return has to be the last return, or we shouldn't
102 * have reached here. (see can_inline()).
104 assert(ret
->next
->is_tail_sentinel());
111 ir_call::generate_inline(ir_instruction
*next_ir
)
113 void *ctx
= talloc_parent(this);
114 ir_variable
**parameters
;
117 ir_variable
*retval
= NULL
;
118 struct hash_table
*ht
;
120 ht
= hash_table_ctor(0, hash_table_pointer_hash
, hash_table_pointer_compare
);
123 foreach_iter(exec_list_iterator
, iter_sig
, this->callee
->parameters
)
126 parameters
= new ir_variable
*[num_parameters
];
128 /* Generate storage for the return value. */
129 if (this->callee
->return_type
) {
130 retval
= new(ctx
) ir_variable(this->callee
->return_type
, "__retval",
132 next_ir
->insert_before(retval
);
135 /* Generate the declarations for the parameters to our inlined code,
136 * and set up the mapping of real function body variables to ours.
139 exec_list_iterator sig_param_iter
= this->callee
->parameters
.iterator();
140 exec_list_iterator param_iter
= this->actual_parameters
.iterator();
141 for (i
= 0; i
< num_parameters
; i
++) {
142 ir_variable
*sig_param
= (ir_variable
*) sig_param_iter
.get();
143 ir_rvalue
*param
= (ir_rvalue
*) param_iter
.get();
145 /* Generate a new variable for the parameter. */
146 if (sig_param
->type
->base_type
== GLSL_TYPE_SAMPLER
) {
147 /* For samplers, we want the inlined sampler references
148 * referencing the passed in sampler variable, since that
149 * will have the location information, which an assignment of
150 * a sampler wouldn't. Fix it up below.
152 parameters
[i
] = NULL
;
154 parameters
[i
] = sig_param
->clone(ctx
, ht
);
155 parameters
[i
]->mode
= ir_var_auto
;
156 next_ir
->insert_before(parameters
[i
]);
159 /* Move the actual param into our param variable if it's an 'in' type. */
160 if (parameters
[i
] && (sig_param
->mode
== ir_var_in
||
161 sig_param
->mode
== ir_var_inout
)) {
162 ir_assignment
*assign
;
164 assign
= new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(parameters
[i
]),
166 next_ir
->insert_before(assign
);
169 sig_param_iter
.next();
173 exec_list new_instructions
;
175 /* Generate the inlined body of the function to a new list */
176 foreach_iter(exec_list_iterator
, iter
, callee
->body
) {
177 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
178 ir_instruction
*new_ir
= ir
->clone(ctx
, ht
);
180 new_instructions
.push_tail(new_ir
);
181 visit_tree(new_ir
, replace_return_with_assignment
, retval
);
184 /* If any samplers were passed in, replace any deref of the sampler
185 * with a deref of the sampler argument.
187 param_iter
= this->actual_parameters
.iterator();
188 sig_param_iter
= this->callee
->parameters
.iterator();
189 for (i
= 0; i
< num_parameters
; i
++) {
190 ir_instruction
*const param
= (ir_instruction
*) param_iter
.get();
191 ir_variable
*sig_param
= (ir_variable
*) sig_param_iter
.get();
193 if (sig_param
->type
->base_type
== GLSL_TYPE_SAMPLER
) {
194 ir_dereference
*deref
= param
->as_dereference();
197 do_sampler_replacement(&new_instructions
, sig_param
, deref
);
200 sig_param_iter
.next();
203 /* Now push those new instructions in. */
204 foreach_iter(exec_list_iterator
, iter
, new_instructions
) {
205 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
206 next_ir
->insert_before(ir
);
209 /* Copy back the value of any 'out' parameters from the function body
210 * variables to our own.
213 param_iter
= this->actual_parameters
.iterator();
214 sig_param_iter
= this->callee
->parameters
.iterator();
215 for (i
= 0; i
< num_parameters
; i
++) {
216 ir_instruction
*const param
= (ir_instruction
*) param_iter
.get();
217 const ir_variable
*const sig_param
= (ir_variable
*) sig_param_iter
.get();
219 /* Move our param variable into the actual param if it's an 'out' type. */
220 if (parameters
[i
] && (sig_param
->mode
== ir_var_out
||
221 sig_param
->mode
== ir_var_inout
)) {
222 ir_assignment
*assign
;
224 assign
= new(ctx
) ir_assignment(param
->clone(ctx
, NULL
)->as_rvalue(),
225 new(ctx
) ir_dereference_variable(parameters
[i
]),
227 next_ir
->insert_before(assign
);
231 sig_param_iter
.next();
234 delete [] parameters
;
239 return new(ctx
) ir_dereference_variable(retval
);
246 ir_function_inlining_visitor::visit_enter(ir_expression
*ir
)
249 return visit_continue_with_parent
;
254 ir_function_inlining_visitor::visit_enter(ir_return
*ir
)
257 return visit_continue_with_parent
;
262 ir_function_inlining_visitor::visit_enter(ir_texture
*ir
)
265 return visit_continue_with_parent
;
270 ir_function_inlining_visitor::visit_enter(ir_swizzle
*ir
)
273 return visit_continue_with_parent
;
278 ir_function_inlining_visitor::visit_enter(ir_call
*ir
)
280 if (can_inline(ir
)) {
281 /* If the call was part of some tree, then it should have been
282 * flattened out or we shouldn't have seen it because of a
283 * visit_continue_with_parent in this visitor.
285 assert(ir
== base_ir
);
287 (void) ir
->generate_inline(ir
);
289 this->progress
= true;
292 return visit_continue
;
297 ir_function_inlining_visitor::visit_enter(ir_assignment
*ir
)
299 ir_call
*call
= ir
->rhs
->as_call();
300 if (!call
|| !can_inline(call
))
301 return visit_continue
;
303 /* generates the parameter setup, function body, and returns the return
304 * value of the function
306 ir_rvalue
*rhs
= call
->generate_inline(ir
);
310 this->progress
= true;
312 return visit_continue
;
316 * Replaces references to the "sampler" variable with a clone of "deref."
318 * From the spec, samplers can appear in the tree as function
319 * (non-out) parameters and as the result of array indexing and
320 * structure field selection. In our builtin implementation, they
321 * also appear in the sampler field of an ir_tex instruction.
324 class ir_sampler_replacement_visitor
: public ir_hierarchical_visitor
{
326 ir_sampler_replacement_visitor(ir_variable
*sampler
, ir_dereference
*deref
)
328 this->sampler
= sampler
;
332 virtual ~ir_sampler_replacement_visitor()
336 virtual ir_visitor_status
visit_leave(ir_call
*);
337 virtual ir_visitor_status
visit_leave(ir_dereference_array
*);
338 virtual ir_visitor_status
visit_leave(ir_dereference_record
*);
339 virtual ir_visitor_status
visit_leave(ir_texture
*);
341 void replace_deref(ir_dereference
**deref
);
342 void replace_rvalue(ir_rvalue
**rvalue
);
344 ir_variable
*sampler
;
345 ir_dereference
*deref
;
349 ir_sampler_replacement_visitor::replace_deref(ir_dereference
**deref
)
351 ir_dereference_variable
*deref_var
= (*deref
)->as_dereference_variable();
352 if (deref_var
&& deref_var
->var
== this->sampler
) {
353 *deref
= this->deref
->clone(talloc_parent(*deref
), NULL
);
358 ir_sampler_replacement_visitor::replace_rvalue(ir_rvalue
**rvalue
)
363 ir_dereference
*deref
= (*rvalue
)->as_dereference();
368 replace_deref(&deref
);
373 ir_sampler_replacement_visitor::visit_leave(ir_texture
*ir
)
375 replace_deref(&ir
->sampler
);
377 return visit_continue
;
381 ir_sampler_replacement_visitor::visit_leave(ir_dereference_array
*ir
)
383 replace_rvalue(&ir
->array
);
384 return visit_continue
;
388 ir_sampler_replacement_visitor::visit_leave(ir_dereference_record
*ir
)
390 replace_rvalue(&ir
->record
);
391 return visit_continue
;
395 ir_sampler_replacement_visitor::visit_leave(ir_call
*ir
)
397 foreach_iter(exec_list_iterator
, iter
, *ir
) {
398 ir_rvalue
*param
= (ir_rvalue
*)iter
.get();
399 ir_rvalue
*new_param
= param
;
400 replace_rvalue(&new_param
);
402 if (new_param
!= param
) {
403 param
->replace_with(new_param
);
406 return visit_continue
;
410 do_sampler_replacement(exec_list
*instructions
,
411 ir_variable
*sampler
,
412 ir_dereference
*deref
)
414 ir_sampler_replacement_visitor
v(sampler
, deref
);
416 visit_list_elements(&v
, instructions
);