blob: 35eb2b38ea936096ce049b5917e8b3e38e2e6932 [file] [log] [blame]
Eric Anholtcad97662010-04-07 11:46:26 -07001/*
2 * Copyright © 2010 Intel Corporation
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 ir_function_inlining.cpp
26 *
27 * Replaces calls to functions with the body of the function.
28 */
29
Eric Anholt4b6fd392010-06-23 11:37:12 -070030#include <inttypes.h>
Eric Anholtcad97662010-04-07 11:46:26 -070031#include "ir.h"
32#include "ir_visitor.h"
33#include "ir_function_inlining.h"
Eric Anholt0d423212010-04-16 12:53:46 -070034#include "ir_expression_flattening.h"
Eric Anholtcad97662010-04-07 11:46:26 -070035#include "glsl_types.h"
Aras Pranckevicius31747152010-07-29 12:40:49 +030036#include "program/hash_table.h"
Eric Anholtcad97662010-04-07 11:46:26 -070037
Eric Anholt199c4412010-08-06 00:21:12 -070038static void
39do_sampler_replacement(exec_list *instructions,
40 ir_variable *sampler,
41 ir_dereference *deref);
42
Ian Romanicke668c2a2010-05-26 18:58:27 -070043class ir_function_inlining_visitor : public ir_hierarchical_visitor {
Eric Anholtbdd9b1f2010-05-05 11:45:30 -070044public:
45 ir_function_inlining_visitor()
46 {
Ian Romanicke668c2a2010-05-26 18:58:27 -070047 progress = false;
Eric Anholtbdd9b1f2010-05-05 11:45:30 -070048 }
49
50 virtual ~ir_function_inlining_visitor()
51 {
52 /* empty */
53 }
54
Ian Romanicke668c2a2010-05-26 18:58:27 -070055 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 *);
Kenneth Graunke26d74cd2010-05-26 17:42:03 -070059 virtual ir_visitor_status visit_enter(ir_texture *);
Ian Romanicke668c2a2010-05-26 18:58:27 -070060 virtual ir_visitor_status visit_enter(ir_swizzle *);
61
62 bool progress;
Eric Anholtbdd9b1f2010-05-05 11:45:30 -070063};
64
Eric Anholtcad97662010-04-07 11:46:26 -070065
Eric Anholtcad97662010-04-07 11:46:26 -070066bool
Eric Anholt0d423212010-04-16 12:53:46 -070067automatic_inlining_predicate(ir_instruction *ir)
68{
69 ir_call *call = ir->as_call();
70
71 if (call && can_inline(call))
72 return true;
73
74 return false;
75}
76
77bool
Eric Anholtcad97662010-04-07 11:46:26 -070078do_function_inlining(exec_list *instructions)
79{
Ian Romanicke668c2a2010-05-26 18:58:27 -070080 ir_function_inlining_visitor v;
Eric Anholtcad97662010-04-07 11:46:26 -070081
Eric Anholt0d423212010-04-16 12:53:46 -070082 do_expression_flattening(instructions, automatic_inlining_predicate);
83
Ian Romanicke668c2a2010-05-26 18:58:27 -070084 v.run(instructions);
Eric Anholtcad97662010-04-07 11:46:26 -070085
Ian Romanicke668c2a2010-05-26 18:58:27 -070086 return v.progress;
Eric Anholtcad97662010-04-07 11:46:26 -070087}
88
Eric Anholtf66ba4f2010-06-24 08:59:57 -070089static void
90replace_return_with_assignment(ir_instruction *ir, void *data)
91{
Eric Anholte33c1032010-06-24 15:13:03 -070092 void *ctx = talloc_parent(ir);
Eric Anholtf66ba4f2010-06-24 08:59:57 -070093 ir_variable *retval = (ir_variable *)data;
94 ir_return *ret = ir->as_return();
95
96 if (ret) {
97 if (ret->value) {
Eric Anholte33c1032010-06-24 15:13:03 -070098 ir_rvalue *lhs = new(ctx) ir_dereference_variable(retval);
Kenneth Graunkec7a18da2010-07-19 21:44:03 -070099 ret->replace_with(new(ctx) ir_assignment(lhs, ret->value, NULL));
Eric Anholtf66ba4f2010-06-24 08:59:57 -0700100 } else {
101 /* un-valued return has to be the last return, or we shouldn't
102 * have reached here. (see can_inline()).
103 */
Eric Anholt199c4412010-08-06 00:21:12 -0700104 assert(ret->next->is_tail_sentinel());
Eric Anholt42852472010-07-29 13:42:39 -0700105 ret->remove();
Eric Anholtf66ba4f2010-06-24 08:59:57 -0700106 }
107 }
108}
109
Eric Anholtcad97662010-04-07 11:46:26 -0700110ir_rvalue *
111ir_call::generate_inline(ir_instruction *next_ir)
112{
Carl Worth1660a292010-06-23 18:11:51 -0700113 void *ctx = talloc_parent(this);
Eric Anholtcad97662010-04-07 11:46:26 -0700114 ir_variable **parameters;
115 int num_parameters;
116 int i;
117 ir_variable *retval = NULL;
Eric Anholt4b6fd392010-06-23 11:37:12 -0700118 struct hash_table *ht;
119
Ian Romanickd1a1ee52010-07-06 14:49:14 -0700120 ht = hash_table_ctor(0, hash_table_pointer_hash, hash_table_pointer_compare);
Eric Anholtcad97662010-04-07 11:46:26 -0700121
122 num_parameters = 0;
123 foreach_iter(exec_list_iterator, iter_sig, this->callee->parameters)
124 num_parameters++;
125
126 parameters = new ir_variable *[num_parameters];
127
128 /* Generate storage for the return value. */
129 if (this->callee->return_type) {
Ian Romanick7e2aa912010-07-19 17:12:42 -0700130 retval = new(ctx) ir_variable(this->callee->return_type, "__retval",
131 ir_var_auto);
Eric Anholtcad97662010-04-07 11:46:26 -0700132 next_ir->insert_before(retval);
133 }
134
Eric Anholtcad97662010-04-07 11:46:26 -0700135 /* Generate the declarations for the parameters to our inlined code,
136 * and set up the mapping of real function body variables to ours.
137 */
138 i = 0;
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++) {
Eric Anholt8ec0b812010-07-22 13:52:41 -0700142 ir_variable *sig_param = (ir_variable *) sig_param_iter.get();
Eric Anholtcad97662010-04-07 11:46:26 -0700143 ir_rvalue *param = (ir_rvalue *) param_iter.get();
144
145 /* Generate a new variable for the parameter. */
Eric Anholt8ec0b812010-07-22 13:52:41 -0700146 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
Eric Anholt199c4412010-08-06 00:21:12 -0700150 * a sampler wouldn't. Fix it up below.
Eric Anholt8ec0b812010-07-22 13:52:41 -0700151 */
152 parameters[i] = NULL;
Eric Anholt8ec0b812010-07-22 13:52:41 -0700153 } else {
Eric Anholt8273bd42010-08-04 12:34:56 -0700154 parameters[i] = sig_param->clone(ctx, ht);
Eric Anholt8ec0b812010-07-22 13:52:41 -0700155 parameters[i]->mode = ir_var_auto;
156 next_ir->insert_before(parameters[i]);
157 }
Eric Anholtcad97662010-04-07 11:46:26 -0700158
Eric Anholtcad97662010-04-07 11:46:26 -0700159 /* Move the actual param into our param variable if it's an 'in' type. */
Eric Anholt8ec0b812010-07-22 13:52:41 -0700160 if (parameters[i] && (sig_param->mode == ir_var_in ||
161 sig_param->mode == ir_var_inout)) {
Eric Anholtcad97662010-04-07 11:46:26 -0700162 ir_assignment *assign;
163
Carl Worth1660a292010-06-23 18:11:51 -0700164 assign = new(ctx) ir_assignment(new(ctx) ir_dereference_variable(parameters[i]),
165 param, NULL);
Eric Anholtcad97662010-04-07 11:46:26 -0700166 next_ir->insert_before(assign);
167 }
168
169 sig_param_iter.next();
170 param_iter.next();
171 }
172
Eric Anholt199c4412010-08-06 00:21:12 -0700173 exec_list new_instructions;
174
175 /* Generate the inlined body of the function to a new list */
Eric Anholtcad97662010-04-07 11:46:26 -0700176 foreach_iter(exec_list_iterator, iter, callee->body) {
177 ir_instruction *ir = (ir_instruction *)iter.get();
Eric Anholt8273bd42010-08-04 12:34:56 -0700178 ir_instruction *new_ir = ir->clone(ctx, ht);
Eric Anholtcad97662010-04-07 11:46:26 -0700179
Eric Anholt199c4412010-08-06 00:21:12 -0700180 new_instructions.push_tail(new_ir);
Eric Anholtf66ba4f2010-06-24 08:59:57 -0700181 visit_tree(new_ir, replace_return_with_assignment, retval);
Eric Anholtcad97662010-04-07 11:46:26 -0700182 }
183
Eric Anholt199c4412010-08-06 00:21:12 -0700184 /* If any samplers were passed in, replace any deref of the sampler
185 * with a deref of the sampler argument.
186 */
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();
192
193 if (sig_param->type->base_type == GLSL_TYPE_SAMPLER) {
194 ir_dereference *deref = param->as_dereference();
195
196 assert(deref);
197 do_sampler_replacement(&new_instructions, sig_param, deref);
198 }
199 }
200
201 /* Now push those new instructions in. */
202 foreach_iter(exec_list_iterator, iter, new_instructions) {
203 ir_instruction *ir = (ir_instruction *)iter.get();
204 next_ir->insert_before(ir);
205 }
206
Kenneth Graunkec07fdae2010-04-30 23:38:50 -0700207 /* Copy back the value of any 'out' parameters from the function body
208 * variables to our own.
Eric Anholtcad97662010-04-07 11:46:26 -0700209 */
210 i = 0;
211 param_iter = this->actual_parameters.iterator();
Eric Anholt325a4972010-07-20 16:03:46 -0700212 sig_param_iter = this->callee->parameters.iterator();
Eric Anholtcad97662010-04-07 11:46:26 -0700213 for (i = 0; i < num_parameters; i++) {
214 ir_instruction *const param = (ir_instruction *) param_iter.get();
Eric Anholt325a4972010-07-20 16:03:46 -0700215 const ir_variable *const sig_param = (ir_variable *) sig_param_iter.get();
Eric Anholtcad97662010-04-07 11:46:26 -0700216
Kenneth Graunkec07fdae2010-04-30 23:38:50 -0700217 /* Move our param variable into the actual param if it's an 'out' type. */
Eric Anholt8ec0b812010-07-22 13:52:41 -0700218 if (parameters[i] && (sig_param->mode == ir_var_out ||
219 sig_param->mode == ir_var_inout)) {
Eric Anholtcad97662010-04-07 11:46:26 -0700220 ir_assignment *assign;
221
Eric Anholt8273bd42010-08-04 12:34:56 -0700222 assign = new(ctx) ir_assignment(param->clone(ctx, NULL)->as_rvalue(),
Carl Worth1660a292010-06-23 18:11:51 -0700223 new(ctx) ir_dereference_variable(parameters[i]),
224 NULL);
Eric Anholtcad97662010-04-07 11:46:26 -0700225 next_ir->insert_before(assign);
226 }
227
228 param_iter.next();
Eric Anholt325a4972010-07-20 16:03:46 -0700229 sig_param_iter.next();
Eric Anholtcad97662010-04-07 11:46:26 -0700230 }
231
Ian Romanick2f8b0432010-06-09 11:00:00 -0700232 delete [] parameters;
Eric Anholtcad97662010-04-07 11:46:26 -0700233
Eric Anholt4b6fd392010-06-23 11:37:12 -0700234 hash_table_dtor(ht);
235
Eric Anholtcad97662010-04-07 11:46:26 -0700236 if (retval)
Carl Worth1660a292010-06-23 18:11:51 -0700237 return new(ctx) ir_dereference_variable(retval);
Eric Anholtcad97662010-04-07 11:46:26 -0700238 else
239 return NULL;
240}
241
Ian Romanicke668c2a2010-05-26 18:58:27 -0700242
243ir_visitor_status
244ir_function_inlining_visitor::visit_enter(ir_expression *ir)
Eric Anholtcad97662010-04-07 11:46:26 -0700245{
246 (void) ir;
Ian Romanicke668c2a2010-05-26 18:58:27 -0700247 return visit_continue_with_parent;
Eric Anholtcad97662010-04-07 11:46:26 -0700248}
249
250
Ian Romanicke668c2a2010-05-26 18:58:27 -0700251ir_visitor_status
252ir_function_inlining_visitor::visit_enter(ir_return *ir)
Eric Anholtcad97662010-04-07 11:46:26 -0700253{
254 (void) ir;
Ian Romanicke668c2a2010-05-26 18:58:27 -0700255 return visit_continue_with_parent;
Eric Anholtcad97662010-04-07 11:46:26 -0700256}
257
258
Ian Romanicke668c2a2010-05-26 18:58:27 -0700259ir_visitor_status
Kenneth Graunke26d74cd2010-05-26 17:42:03 -0700260ir_function_inlining_visitor::visit_enter(ir_texture *ir)
261{
262 (void) ir;
263 return visit_continue_with_parent;
264}
265
266
267ir_visitor_status
Ian Romanicke668c2a2010-05-26 18:58:27 -0700268ir_function_inlining_visitor::visit_enter(ir_swizzle *ir)
Eric Anholtcad97662010-04-07 11:46:26 -0700269{
Ian Romanicke668c2a2010-05-26 18:58:27 -0700270 (void) ir;
271 return visit_continue_with_parent;
Eric Anholtcad97662010-04-07 11:46:26 -0700272}
273
274
Ian Romanicke668c2a2010-05-26 18:58:27 -0700275ir_visitor_status
276ir_function_inlining_visitor::visit_enter(ir_call *ir)
Eric Anholtcad97662010-04-07 11:46:26 -0700277{
Ian Romanicke668c2a2010-05-26 18:58:27 -0700278 if (can_inline(ir)) {
Eric Anholtd2afc872010-07-12 10:13:20 -0700279 /* If the call was part of some tree, then it should have been
280 * flattened out or we shouldn't have seen it because of a
281 * visit_continue_with_parent in this visitor.
282 */
283 assert(ir == base_ir);
284
Ian Romanicke668c2a2010-05-26 18:58:27 -0700285 (void) ir->generate_inline(ir);
286 ir->remove();
287 this->progress = true;
Kenneth Graunke9fa99f32010-04-21 12:30:22 -0700288 }
Eric Anholtcad97662010-04-07 11:46:26 -0700289
Ian Romanicke668c2a2010-05-26 18:58:27 -0700290 return visit_continue;
Eric Anholtcad97662010-04-07 11:46:26 -0700291}
292
293
Ian Romanicke668c2a2010-05-26 18:58:27 -0700294ir_visitor_status
295ir_function_inlining_visitor::visit_enter(ir_assignment *ir)
Eric Anholtcad97662010-04-07 11:46:26 -0700296{
Ian Romanicke668c2a2010-05-26 18:58:27 -0700297 ir_call *call = ir->rhs->as_call();
298 if (!call || !can_inline(call))
299 return visit_continue;
Eric Anholtcad97662010-04-07 11:46:26 -0700300
Ian Romanicke668c2a2010-05-26 18:58:27 -0700301 /* generates the parameter setup, function body, and returns the return
302 * value of the function
303 */
304 ir_rvalue *rhs = call->generate_inline(ir);
305 assert(rhs);
Eric Anholtcad97662010-04-07 11:46:26 -0700306
Ian Romanicke668c2a2010-05-26 18:58:27 -0700307 ir->rhs = rhs;
308 this->progress = true;
Ian Romanickc7b10462010-05-19 13:20:12 +0200309
Ian Romanicke668c2a2010-05-26 18:58:27 -0700310 return visit_continue;
Eric Anholtcad97662010-04-07 11:46:26 -0700311}
Eric Anholt199c4412010-08-06 00:21:12 -0700312
313/**
314 * Replaces references to the "sampler" variable with a clone of "deref."
315 *
316 * From the spec, samplers can appear in the tree as function
317 * (non-out) parameters and as the result of array indexing and
318 * structure field selection. In our builtin implementation, they
319 * also appear in the sampler field of an ir_tex instruction.
320 */
321
322class ir_sampler_replacement_visitor : public ir_hierarchical_visitor {
323public:
324 ir_sampler_replacement_visitor(ir_variable *sampler, ir_dereference *deref)
325 {
326 this->sampler = sampler;
327 this->deref = deref;
328 }
329
330 virtual ~ir_sampler_replacement_visitor()
331 {
332 }
333
334 virtual ir_visitor_status visit_leave(ir_call *);
335 virtual ir_visitor_status visit_leave(ir_dereference_array *);
336 virtual ir_visitor_status visit_leave(ir_dereference_record *);
337 virtual ir_visitor_status visit_leave(ir_texture *);
338
339 void replace_deref(ir_dereference **deref);
340 void replace_rvalue(ir_rvalue **rvalue);
341
342 ir_variable *sampler;
343 ir_dereference *deref;
344};
345
346void
347ir_sampler_replacement_visitor::replace_deref(ir_dereference **deref)
348{
349 ir_dereference_variable *deref_var = (*deref)->as_dereference_variable();
350 if (deref_var && deref_var->var == this->sampler) {
351 *deref = this->deref->clone(talloc_parent(*deref), NULL);
352 }
353}
354
355void
356ir_sampler_replacement_visitor::replace_rvalue(ir_rvalue **rvalue)
357{
358 if (!*rvalue)
359 return;
360
361 ir_dereference *deref = (*rvalue)->as_dereference();
362
363 if (!deref)
364 return;
365
366 replace_deref(&deref);
367 *rvalue = deref;
368}
369
370ir_visitor_status
371ir_sampler_replacement_visitor::visit_leave(ir_texture *ir)
372{
373 replace_deref(&ir->sampler);
374
375 return visit_continue;
376}
377
378ir_visitor_status
379ir_sampler_replacement_visitor::visit_leave(ir_dereference_array *ir)
380{
381 replace_rvalue(&ir->array);
382 return visit_continue;
383}
384
385ir_visitor_status
386ir_sampler_replacement_visitor::visit_leave(ir_dereference_record *ir)
387{
388 replace_rvalue(&ir->record);
389 return visit_continue;
390}
391
392ir_visitor_status
393ir_sampler_replacement_visitor::visit_leave(ir_call *ir)
394{
395 foreach_iter(exec_list_iterator, iter, *ir) {
396 ir_rvalue *param = (ir_rvalue *)iter.get();
397 ir_rvalue *new_param = param;
398 replace_rvalue(&new_param);
399
400 if (new_param != param) {
401 param->replace_with(new_param);
402 }
403 }
404 return visit_continue;
405}
406
407static void
408do_sampler_replacement(exec_list *instructions,
409 ir_variable *sampler,
410 ir_dereference *deref)
411{
412 ir_sampler_replacement_visitor v(sampler, deref);
413
414 visit_list_elements(&v, instructions);
415}