blob: a501c813fb8395278c9a8e9e060845a8b705a45f [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
30#define NULL 0
31#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"
36
Ian Romanicke668c2a2010-05-26 18:58:27 -070037class ir_function_inlining_visitor : public ir_hierarchical_visitor {
Eric Anholtbdd9b1f2010-05-05 11:45:30 -070038public:
39 ir_function_inlining_visitor()
40 {
Ian Romanicke668c2a2010-05-26 18:58:27 -070041 progress = false;
Eric Anholtbdd9b1f2010-05-05 11:45:30 -070042 }
43
44 virtual ~ir_function_inlining_visitor()
45 {
46 /* empty */
47 }
48
Ian Romanicke668c2a2010-05-26 18:58:27 -070049 virtual ir_visitor_status visit_enter(ir_expression *);
50 virtual ir_visitor_status visit_enter(ir_call *);
51 virtual ir_visitor_status visit_enter(ir_assignment *);
52 virtual ir_visitor_status visit_enter(ir_return *);
Kenneth Graunke26d74cd2010-05-26 17:42:03 -070053 virtual ir_visitor_status visit_enter(ir_texture *);
Ian Romanicke668c2a2010-05-26 18:58:27 -070054 virtual ir_visitor_status visit_enter(ir_swizzle *);
55
56 bool progress;
Eric Anholtbdd9b1f2010-05-05 11:45:30 -070057};
58
Eric Anholtcad97662010-04-07 11:46:26 -070059class variable_remap : public exec_node {
60public:
61 variable_remap(const ir_variable *old_var, ir_variable *new_var)
62 : old_var(old_var), new_var(new_var)
63 {
64 /* empty */
65 }
66 const ir_variable *old_var;
67 ir_variable *new_var;
68};
69
70class ir_function_cloning_visitor : public ir_visitor {
71public:
72 ir_function_cloning_visitor(ir_variable *retval)
73 : retval(retval)
74 {
75 /* empty */
76 }
77
78 virtual ~ir_function_cloning_visitor()
79 {
80 /* empty */
81 }
82
83 void remap_variable(const ir_variable *old_var, ir_variable *new_var) {
84 variable_remap *remap = new variable_remap(old_var, new_var);
85 this->remap_list.push_tail(remap);
86 }
87
88 ir_variable *get_remapped_variable(ir_variable *var) {
89 foreach_iter(exec_list_iterator, iter, this->remap_list) {
90 variable_remap *remap = (variable_remap *)iter.get();
91
92 if (var == remap->old_var)
93 return remap->new_var;
94 }
95
96 /* Not a reapped variable, so a global scoped reference, for example. */
97 return var;
98 }
99
100 /* List of variable_remap for mapping from original function body variables
101 * to inlined function body variables.
102 */
103 exec_list remap_list;
104
105 /* Return value for the inlined function. */
106 ir_variable *retval;
107
108 /**
109 * \name Visit methods
110 *
111 * As typical for the visitor pattern, there must be one \c visit method for
112 * each concrete subclass of \c ir_instruction. Virtual base classes within
113 * the hierarchy should not have \c visit methods.
114 */
115 /*@{*/
116 virtual void visit(ir_variable *);
Eric Anholtcad97662010-04-07 11:46:26 -0700117 virtual void visit(ir_loop *);
118 virtual void visit(ir_loop_jump *);
119 virtual void visit(ir_function_signature *);
120 virtual void visit(ir_function *);
121 virtual void visit(ir_expression *);
Kenneth Graunke26d74cd2010-05-26 17:42:03 -0700122 virtual void visit(ir_texture *);
Eric Anholtcad97662010-04-07 11:46:26 -0700123 virtual void visit(ir_swizzle *);
Ian Romanickc7b10462010-05-19 13:20:12 +0200124 virtual void visit(ir_dereference_variable *);
125 virtual void visit(ir_dereference_array *);
126 virtual void visit(ir_dereference_record *);
Eric Anholtcad97662010-04-07 11:46:26 -0700127 virtual void visit(ir_assignment *);
128 virtual void visit(ir_constant *);
129 virtual void visit(ir_call *);
130 virtual void visit(ir_return *);
131 virtual void visit(ir_if *);
132 /*@}*/
133
134 ir_instruction *result;
135};
136
137void
138ir_function_cloning_visitor::visit(ir_variable *ir)
139{
140 ir_variable *new_var = ir->clone();
141
142 this->result = new_var;
143
144 this->remap_variable(ir, new_var);
145}
146
147void
Eric Anholtcad97662010-04-07 11:46:26 -0700148ir_function_cloning_visitor::visit(ir_loop *ir)
149{
Eric Anholte8e97482010-04-22 17:52:59 -0700150 /* FINISHME: Implement loop cloning. */
151 assert(0);
152
Eric Anholtcad97662010-04-07 11:46:26 -0700153 (void)ir;
154 this->result = NULL;
155}
156
157void
158ir_function_cloning_visitor::visit(ir_loop_jump *ir)
159{
Eric Anholte8e97482010-04-22 17:52:59 -0700160 /* FINISHME: Implement loop cloning. */
161 assert(0);
162
Eric Anholtcad97662010-04-07 11:46:26 -0700163 (void) ir;
164 this->result = NULL;
165}
166
167
168void
169ir_function_cloning_visitor::visit(ir_function_signature *ir)
170{
Eric Anholte8e97482010-04-22 17:52:59 -0700171 assert(0);
Eric Anholtcad97662010-04-07 11:46:26 -0700172 (void)ir;
173 this->result = NULL;
174}
175
176
177void
178ir_function_cloning_visitor::visit(ir_function *ir)
179{
Eric Anholte8e97482010-04-22 17:52:59 -0700180 assert(0);
Eric Anholtcad97662010-04-07 11:46:26 -0700181 (void) ir;
182 this->result = NULL;
183}
184
185void
186ir_function_cloning_visitor::visit(ir_expression *ir)
187{
188 unsigned int operand;
189 ir_rvalue *op[2] = {NULL, NULL};
190
191 for (operand = 0; operand < ir->get_num_operands(); operand++) {
192 ir->operands[operand]->accept(this);
193 op[operand] = this->result->as_rvalue();
194 assert(op[operand]);
195 }
196
197 this->result = new ir_expression(ir->operation, ir->type, op[0], op[1]);
198}
199
200
201void
Kenneth Graunke26d74cd2010-05-26 17:42:03 -0700202ir_function_cloning_visitor::visit(ir_texture *ir)
203{
Kenneth Graunkeb97efa52010-06-09 11:07:53 -0700204 ir_texture *tex = new ir_texture(ir->op);
205
206 ir->sampler->accept(this);
207 tex->set_sampler(this->result->as_dereference());
208
209 ir->coordinate->accept(this);
210 tex->coordinate = this->result->as_rvalue();
211
212 if (ir->projector != NULL) {
213 ir->projector->accept(this);
214 tex->projector = this->result->as_rvalue();
215 }
216
217 if (ir->shadow_comparitor != NULL) {
218 ir->shadow_comparitor->accept(this);
219 tex->shadow_comparitor = this->result->as_rvalue();
220 }
221
222 for (int i = 0; i < 3; i++)
223 tex->offsets[i] = ir->offsets[i];
224
225 tex->lod_info = ir->lod_info;
Kenneth Graunke26d74cd2010-05-26 17:42:03 -0700226}
227
228
229void
Eric Anholtcad97662010-04-07 11:46:26 -0700230ir_function_cloning_visitor::visit(ir_swizzle *ir)
231{
232 ir->val->accept(this);
233
234 this->result = new ir_swizzle(this->result->as_rvalue(), ir->mask);
235}
236
237void
Ian Romanickc7b10462010-05-19 13:20:12 +0200238ir_function_cloning_visitor::visit(ir_dereference_variable *ir)
Eric Anholtcad97662010-04-07 11:46:26 -0700239{
Ian Romanickc7b10462010-05-19 13:20:12 +0200240 ir_variable *var = this->get_remapped_variable(ir->variable_referenced());
241 this->result = new ir_dereference_variable(var);
242}
Ian Romanick70fe8b62010-05-19 11:37:35 +0200243
Ian Romanickc7b10462010-05-19 13:20:12 +0200244void
245ir_function_cloning_visitor::visit(ir_dereference_array *ir)
246{
Ian Romanick36ea2862010-05-19 13:52:29 +0200247 ir->array->accept(this);
Eric Anholt61924342010-04-08 13:40:52 -0700248
Ian Romanickc7b10462010-05-19 13:20:12 +0200249 ir_rvalue *var = this->result->as_rvalue();
Eric Anholt61924342010-04-08 13:40:52 -0700250
Ian Romanick36ea2862010-05-19 13:52:29 +0200251 ir->array_index->accept(this);
Ian Romanick70fe8b62010-05-19 11:37:35 +0200252
Ian Romanickc7b10462010-05-19 13:20:12 +0200253 ir_rvalue *index = this->result->as_rvalue();
Ian Romanick70fe8b62010-05-19 11:37:35 +0200254
Ian Romanickc7b10462010-05-19 13:20:12 +0200255 this->result = new ir_dereference_array(var, index);
256}
Ian Romanick70fe8b62010-05-19 11:37:35 +0200257
Ian Romanickc7b10462010-05-19 13:20:12 +0200258void
259ir_function_cloning_visitor::visit(ir_dereference_record *ir)
260{
Ian Romanick36ea2862010-05-19 13:52:29 +0200261 ir->record->accept(this);
Ian Romanick70fe8b62010-05-19 11:37:35 +0200262
Ian Romanickc7b10462010-05-19 13:20:12 +0200263 ir_rvalue *var = this->result->as_rvalue();
264
Ian Romanick36ea2862010-05-19 13:52:29 +0200265 this->result = new ir_dereference_record(var, strdup(ir->field));
Eric Anholtcad97662010-04-07 11:46:26 -0700266}
267
268void
269ir_function_cloning_visitor::visit(ir_assignment *ir)
270{
Eric Anholt22147be2010-04-22 18:41:32 -0700271 ir_rvalue *lhs, *rhs, *condition = NULL;
Eric Anholtcad97662010-04-07 11:46:26 -0700272
273 ir->lhs->accept(this);
274 lhs = this->result->as_rvalue();
275
276 ir->rhs->accept(this);
277 rhs = this->result->as_rvalue();
278
Eric Anholt22147be2010-04-22 18:41:32 -0700279 if (ir->condition) {
280 ir->condition->accept(this);
281 condition = this->result->as_rvalue();
282 }
Eric Anholtcad97662010-04-07 11:46:26 -0700283
284 this->result = new ir_assignment(lhs, rhs, condition);
285}
286
287
288void
289ir_function_cloning_visitor::visit(ir_constant *ir)
290{
291 this->result = ir->clone();
292}
293
294
295void
296ir_function_cloning_visitor::visit(ir_call *ir)
297{
298 exec_list parameters;
299
300 foreach_iter(exec_list_iterator, iter, *ir) {
301 ir_rvalue *param = (ir_rvalue *)iter.get();
302
303 param->accept(this);
304 parameters.push_tail(this->result);
305 }
306
307 this->result = new ir_call(ir->get_callee(), &parameters);
308}
309
310
311void
312ir_function_cloning_visitor::visit(ir_return *ir)
313{
314 ir_rvalue *rval;
315
316 assert(this->retval);
317
318 rval = ir->get_value();
319 rval->accept(this);
320 rval = this->result->as_rvalue();
321 assert(rval);
322
Ian Romanick70fe8b62010-05-19 11:37:35 +0200323 result = new ir_assignment(new ir_dereference_variable(this->retval), rval,
324 NULL);
Eric Anholtcad97662010-04-07 11:46:26 -0700325}
326
327
328void
329ir_function_cloning_visitor::visit(ir_if *ir)
330{
Eric Anholte8e97482010-04-22 17:52:59 -0700331 /* FINISHME: Implement if cloning. */
332 assert(0);
333
Eric Anholtcad97662010-04-07 11:46:26 -0700334 (void) ir;
335 result = NULL;
336}
337
338bool
Eric Anholt0d423212010-04-16 12:53:46 -0700339automatic_inlining_predicate(ir_instruction *ir)
340{
341 ir_call *call = ir->as_call();
342
343 if (call && can_inline(call))
344 return true;
345
346 return false;
347}
348
349bool
Eric Anholtcad97662010-04-07 11:46:26 -0700350do_function_inlining(exec_list *instructions)
351{
Ian Romanicke668c2a2010-05-26 18:58:27 -0700352 ir_function_inlining_visitor v;
Eric Anholtcad97662010-04-07 11:46:26 -0700353
Eric Anholt0d423212010-04-16 12:53:46 -0700354 do_expression_flattening(instructions, automatic_inlining_predicate);
355
Ian Romanicke668c2a2010-05-26 18:58:27 -0700356 v.run(instructions);
Eric Anholtcad97662010-04-07 11:46:26 -0700357
Ian Romanicke668c2a2010-05-26 18:58:27 -0700358 return v.progress;
Eric Anholtcad97662010-04-07 11:46:26 -0700359}
360
361ir_rvalue *
362ir_call::generate_inline(ir_instruction *next_ir)
363{
364 ir_variable **parameters;
365 int num_parameters;
366 int i;
367 ir_variable *retval = NULL;
368
369 num_parameters = 0;
370 foreach_iter(exec_list_iterator, iter_sig, this->callee->parameters)
371 num_parameters++;
372
373 parameters = new ir_variable *[num_parameters];
374
375 /* Generate storage for the return value. */
376 if (this->callee->return_type) {
377 retval = new ir_variable(this->callee->return_type, "__retval");
378 next_ir->insert_before(retval);
379 }
380
381 ir_function_cloning_visitor v = ir_function_cloning_visitor(retval);
382
383 /* Generate the declarations for the parameters to our inlined code,
384 * and set up the mapping of real function body variables to ours.
385 */
386 i = 0;
387 exec_list_iterator sig_param_iter = this->callee->parameters.iterator();
388 exec_list_iterator param_iter = this->actual_parameters.iterator();
389 for (i = 0; i < num_parameters; i++) {
390 const ir_variable *const sig_param = (ir_variable *) sig_param_iter.get();
391 ir_rvalue *param = (ir_rvalue *) param_iter.get();
392
393 /* Generate a new variable for the parameter. */
394 parameters[i] = sig_param->clone();
395 next_ir->insert_before(parameters[i]);
396
397 v.remap_variable(sig_param, parameters[i]);
398
399 /* Move the actual param into our param variable if it's an 'in' type. */
400 if (parameters[i]->mode == ir_var_in ||
401 parameters[i]->mode == ir_var_inout) {
402 ir_assignment *assign;
403
Ian Romanick70fe8b62010-05-19 11:37:35 +0200404 assign = new ir_assignment(new ir_dereference_variable(parameters[i]),
Eric Anholtcad97662010-04-07 11:46:26 -0700405 param, NULL);
406 next_ir->insert_before(assign);
407 }
408
409 sig_param_iter.next();
410 param_iter.next();
411 }
412
413 /* Generate the inlined body of the function. */
414 foreach_iter(exec_list_iterator, iter, callee->body) {
415 ir_instruction *ir = (ir_instruction *)iter.get();
416
417 ir->accept(&v);
418 assert(v.result);
419 next_ir->insert_before(v.result);
420 }
421
Kenneth Graunkec07fdae2010-04-30 23:38:50 -0700422 /* Copy back the value of any 'out' parameters from the function body
423 * variables to our own.
Eric Anholtcad97662010-04-07 11:46:26 -0700424 */
425 i = 0;
426 param_iter = this->actual_parameters.iterator();
427 for (i = 0; i < num_parameters; i++) {
428 ir_instruction *const param = (ir_instruction *) param_iter.get();
429
Kenneth Graunkec07fdae2010-04-30 23:38:50 -0700430 /* Move our param variable into the actual param if it's an 'out' type. */
Eric Anholtcad97662010-04-07 11:46:26 -0700431 if (parameters[i]->mode == ir_var_out ||
432 parameters[i]->mode == ir_var_inout) {
433 ir_assignment *assign;
434
435 assign = new ir_assignment(param->as_rvalue(),
Ian Romanick70fe8b62010-05-19 11:37:35 +0200436 new ir_dereference_variable(parameters[i]),
Eric Anholtcad97662010-04-07 11:46:26 -0700437 NULL);
438 next_ir->insert_before(assign);
439 }
440
441 param_iter.next();
442 }
443
Ian Romanick2f8b0432010-06-09 11:00:00 -0700444 delete [] parameters;
Eric Anholtcad97662010-04-07 11:46:26 -0700445
446 if (retval)
Ian Romanick70fe8b62010-05-19 11:37:35 +0200447 return new ir_dereference_variable(retval);
Eric Anholtcad97662010-04-07 11:46:26 -0700448 else
449 return NULL;
450}
451
Ian Romanicke668c2a2010-05-26 18:58:27 -0700452
453ir_visitor_status
454ir_function_inlining_visitor::visit_enter(ir_expression *ir)
Eric Anholtcad97662010-04-07 11:46:26 -0700455{
456 (void) ir;
Ian Romanicke668c2a2010-05-26 18:58:27 -0700457 return visit_continue_with_parent;
Eric Anholtcad97662010-04-07 11:46:26 -0700458}
459
460
Ian Romanicke668c2a2010-05-26 18:58:27 -0700461ir_visitor_status
462ir_function_inlining_visitor::visit_enter(ir_return *ir)
Eric Anholtcad97662010-04-07 11:46:26 -0700463{
464 (void) ir;
Ian Romanicke668c2a2010-05-26 18:58:27 -0700465 return visit_continue_with_parent;
Eric Anholtcad97662010-04-07 11:46:26 -0700466}
467
468
Ian Romanicke668c2a2010-05-26 18:58:27 -0700469ir_visitor_status
Kenneth Graunke26d74cd2010-05-26 17:42:03 -0700470ir_function_inlining_visitor::visit_enter(ir_texture *ir)
471{
472 (void) ir;
473 return visit_continue_with_parent;
474}
475
476
477ir_visitor_status
Ian Romanicke668c2a2010-05-26 18:58:27 -0700478ir_function_inlining_visitor::visit_enter(ir_swizzle *ir)
Eric Anholtcad97662010-04-07 11:46:26 -0700479{
Ian Romanicke668c2a2010-05-26 18:58:27 -0700480 (void) ir;
481 return visit_continue_with_parent;
Eric Anholtcad97662010-04-07 11:46:26 -0700482}
483
484
Ian Romanicke668c2a2010-05-26 18:58:27 -0700485ir_visitor_status
486ir_function_inlining_visitor::visit_enter(ir_call *ir)
Eric Anholtcad97662010-04-07 11:46:26 -0700487{
Ian Romanicke668c2a2010-05-26 18:58:27 -0700488 if (can_inline(ir)) {
489 (void) ir->generate_inline(ir);
490 ir->remove();
491 this->progress = true;
Kenneth Graunke9fa99f32010-04-21 12:30:22 -0700492 }
Eric Anholtcad97662010-04-07 11:46:26 -0700493
Ian Romanicke668c2a2010-05-26 18:58:27 -0700494 return visit_continue;
Eric Anholtcad97662010-04-07 11:46:26 -0700495}
496
497
Ian Romanicke668c2a2010-05-26 18:58:27 -0700498ir_visitor_status
499ir_function_inlining_visitor::visit_enter(ir_assignment *ir)
Eric Anholtcad97662010-04-07 11:46:26 -0700500{
Ian Romanicke668c2a2010-05-26 18:58:27 -0700501 ir_call *call = ir->rhs->as_call();
502 if (!call || !can_inline(call))
503 return visit_continue;
Eric Anholtcad97662010-04-07 11:46:26 -0700504
Ian Romanicke668c2a2010-05-26 18:58:27 -0700505 /* generates the parameter setup, function body, and returns the return
506 * value of the function
507 */
508 ir_rvalue *rhs = call->generate_inline(ir);
509 assert(rhs);
Eric Anholtcad97662010-04-07 11:46:26 -0700510
Ian Romanicke668c2a2010-05-26 18:58:27 -0700511 ir->rhs = rhs;
512 this->progress = true;
Ian Romanickc7b10462010-05-19 13:20:12 +0200513
Ian Romanicke668c2a2010-05-26 18:58:27 -0700514 return visit_continue;
Eric Anholtcad97662010-04-07 11:46:26 -0700515}