blob: 9109ce698b234e98ddc9b0cf5c324cdd47591731 [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
Eric Anholtbdd9b1f2010-05-05 11:45:30 -070037class ir_function_inlining_visitor : public ir_visitor {
38public:
39 ir_function_inlining_visitor()
40 {
41 /* empty */
42 }
43
44 virtual ~ir_function_inlining_visitor()
45 {
46 /* empty */
47 }
48
49 /**
50 * \name Visit methods
51 *
52 * As typical for the visitor pattern, there must be one \c visit method for
53 * each concrete subclass of \c ir_instruction. Virtual base classes within
54 * the hierarchy should not have \c visit methods.
55 */
56 /*@{*/
57 virtual void visit(ir_variable *);
58 virtual void visit(ir_loop *);
59 virtual void visit(ir_loop_jump *);
60 virtual void visit(ir_function_signature *);
61 virtual void visit(ir_function *);
62 virtual void visit(ir_expression *);
63 virtual void visit(ir_swizzle *);
Ian Romanickc7b10462010-05-19 13:20:12 +020064 virtual void visit(ir_dereference_variable *);
65 virtual void visit(ir_dereference_array *);
66 virtual void visit(ir_dereference_record *);
Eric Anholtbdd9b1f2010-05-05 11:45:30 -070067 virtual void visit(ir_assignment *);
68 virtual void visit(ir_constant *);
69 virtual void visit(ir_call *);
70 virtual void visit(ir_return *);
71 virtual void visit(ir_if *);
72 /*@}*/
73};
74
Eric Anholtcad97662010-04-07 11:46:26 -070075class variable_remap : public exec_node {
76public:
77 variable_remap(const ir_variable *old_var, ir_variable *new_var)
78 : old_var(old_var), new_var(new_var)
79 {
80 /* empty */
81 }
82 const ir_variable *old_var;
83 ir_variable *new_var;
84};
85
86class ir_function_cloning_visitor : public ir_visitor {
87public:
88 ir_function_cloning_visitor(ir_variable *retval)
89 : retval(retval)
90 {
91 /* empty */
92 }
93
94 virtual ~ir_function_cloning_visitor()
95 {
96 /* empty */
97 }
98
99 void remap_variable(const ir_variable *old_var, ir_variable *new_var) {
100 variable_remap *remap = new variable_remap(old_var, new_var);
101 this->remap_list.push_tail(remap);
102 }
103
104 ir_variable *get_remapped_variable(ir_variable *var) {
105 foreach_iter(exec_list_iterator, iter, this->remap_list) {
106 variable_remap *remap = (variable_remap *)iter.get();
107
108 if (var == remap->old_var)
109 return remap->new_var;
110 }
111
112 /* Not a reapped variable, so a global scoped reference, for example. */
113 return var;
114 }
115
116 /* List of variable_remap for mapping from original function body variables
117 * to inlined function body variables.
118 */
119 exec_list remap_list;
120
121 /* Return value for the inlined function. */
122 ir_variable *retval;
123
124 /**
125 * \name Visit methods
126 *
127 * As typical for the visitor pattern, there must be one \c visit method for
128 * each concrete subclass of \c ir_instruction. Virtual base classes within
129 * the hierarchy should not have \c visit methods.
130 */
131 /*@{*/
132 virtual void visit(ir_variable *);
Eric Anholtcad97662010-04-07 11:46:26 -0700133 virtual void visit(ir_loop *);
134 virtual void visit(ir_loop_jump *);
135 virtual void visit(ir_function_signature *);
136 virtual void visit(ir_function *);
137 virtual void visit(ir_expression *);
138 virtual void visit(ir_swizzle *);
Ian Romanickc7b10462010-05-19 13:20:12 +0200139 virtual void visit(ir_dereference_variable *);
140 virtual void visit(ir_dereference_array *);
141 virtual void visit(ir_dereference_record *);
Eric Anholtcad97662010-04-07 11:46:26 -0700142 virtual void visit(ir_assignment *);
143 virtual void visit(ir_constant *);
144 virtual void visit(ir_call *);
145 virtual void visit(ir_return *);
146 virtual void visit(ir_if *);
147 /*@}*/
148
149 ir_instruction *result;
150};
151
152void
153ir_function_cloning_visitor::visit(ir_variable *ir)
154{
155 ir_variable *new_var = ir->clone();
156
157 this->result = new_var;
158
159 this->remap_variable(ir, new_var);
160}
161
162void
Eric Anholtcad97662010-04-07 11:46:26 -0700163ir_function_cloning_visitor::visit(ir_loop *ir)
164{
Eric Anholte8e97482010-04-22 17:52:59 -0700165 /* FINISHME: Implement loop cloning. */
166 assert(0);
167
Eric Anholtcad97662010-04-07 11:46:26 -0700168 (void)ir;
169 this->result = NULL;
170}
171
172void
173ir_function_cloning_visitor::visit(ir_loop_jump *ir)
174{
Eric Anholte8e97482010-04-22 17:52:59 -0700175 /* FINISHME: Implement loop cloning. */
176 assert(0);
177
Eric Anholtcad97662010-04-07 11:46:26 -0700178 (void) ir;
179 this->result = NULL;
180}
181
182
183void
184ir_function_cloning_visitor::visit(ir_function_signature *ir)
185{
Eric Anholte8e97482010-04-22 17:52:59 -0700186 assert(0);
Eric Anholtcad97662010-04-07 11:46:26 -0700187 (void)ir;
188 this->result = NULL;
189}
190
191
192void
193ir_function_cloning_visitor::visit(ir_function *ir)
194{
Eric Anholte8e97482010-04-22 17:52:59 -0700195 assert(0);
Eric Anholtcad97662010-04-07 11:46:26 -0700196 (void) ir;
197 this->result = NULL;
198}
199
200void
201ir_function_cloning_visitor::visit(ir_expression *ir)
202{
203 unsigned int operand;
204 ir_rvalue *op[2] = {NULL, NULL};
205
206 for (operand = 0; operand < ir->get_num_operands(); operand++) {
207 ir->operands[operand]->accept(this);
208 op[operand] = this->result->as_rvalue();
209 assert(op[operand]);
210 }
211
212 this->result = new ir_expression(ir->operation, ir->type, op[0], op[1]);
213}
214
215
216void
217ir_function_cloning_visitor::visit(ir_swizzle *ir)
218{
219 ir->val->accept(this);
220
221 this->result = new ir_swizzle(this->result->as_rvalue(), ir->mask);
222}
223
224void
Ian Romanickc7b10462010-05-19 13:20:12 +0200225ir_function_cloning_visitor::visit(ir_dereference_variable *ir)
Eric Anholtcad97662010-04-07 11:46:26 -0700226{
Ian Romanickc7b10462010-05-19 13:20:12 +0200227 ir_variable *var = this->get_remapped_variable(ir->variable_referenced());
228 this->result = new ir_dereference_variable(var);
229}
Ian Romanick70fe8b62010-05-19 11:37:35 +0200230
Ian Romanickc7b10462010-05-19 13:20:12 +0200231void
232ir_function_cloning_visitor::visit(ir_dereference_array *ir)
233{
Ian Romanick36ea2862010-05-19 13:52:29 +0200234 ir->array->accept(this);
Eric Anholt61924342010-04-08 13:40:52 -0700235
Ian Romanickc7b10462010-05-19 13:20:12 +0200236 ir_rvalue *var = this->result->as_rvalue();
Eric Anholt61924342010-04-08 13:40:52 -0700237
Ian Romanick36ea2862010-05-19 13:52:29 +0200238 ir->array_index->accept(this);
Ian Romanick70fe8b62010-05-19 11:37:35 +0200239
Ian Romanickc7b10462010-05-19 13:20:12 +0200240 ir_rvalue *index = this->result->as_rvalue();
Ian Romanick70fe8b62010-05-19 11:37:35 +0200241
Ian Romanickc7b10462010-05-19 13:20:12 +0200242 this->result = new ir_dereference_array(var, index);
243}
Ian Romanick70fe8b62010-05-19 11:37:35 +0200244
Ian Romanickc7b10462010-05-19 13:20:12 +0200245void
246ir_function_cloning_visitor::visit(ir_dereference_record *ir)
247{
Ian Romanick36ea2862010-05-19 13:52:29 +0200248 ir->record->accept(this);
Ian Romanick70fe8b62010-05-19 11:37:35 +0200249
Ian Romanickc7b10462010-05-19 13:20:12 +0200250 ir_rvalue *var = this->result->as_rvalue();
251
Ian Romanick36ea2862010-05-19 13:52:29 +0200252 this->result = new ir_dereference_record(var, strdup(ir->field));
Eric Anholtcad97662010-04-07 11:46:26 -0700253}
254
255void
256ir_function_cloning_visitor::visit(ir_assignment *ir)
257{
Eric Anholt22147be2010-04-22 18:41:32 -0700258 ir_rvalue *lhs, *rhs, *condition = NULL;
Eric Anholtcad97662010-04-07 11:46:26 -0700259
260 ir->lhs->accept(this);
261 lhs = this->result->as_rvalue();
262
263 ir->rhs->accept(this);
264 rhs = this->result->as_rvalue();
265
Eric Anholt22147be2010-04-22 18:41:32 -0700266 if (ir->condition) {
267 ir->condition->accept(this);
268 condition = this->result->as_rvalue();
269 }
Eric Anholtcad97662010-04-07 11:46:26 -0700270
271 this->result = new ir_assignment(lhs, rhs, condition);
272}
273
274
275void
276ir_function_cloning_visitor::visit(ir_constant *ir)
277{
278 this->result = ir->clone();
279}
280
281
282void
283ir_function_cloning_visitor::visit(ir_call *ir)
284{
285 exec_list parameters;
286
287 foreach_iter(exec_list_iterator, iter, *ir) {
288 ir_rvalue *param = (ir_rvalue *)iter.get();
289
290 param->accept(this);
291 parameters.push_tail(this->result);
292 }
293
294 this->result = new ir_call(ir->get_callee(), &parameters);
295}
296
297
298void
299ir_function_cloning_visitor::visit(ir_return *ir)
300{
301 ir_rvalue *rval;
302
303 assert(this->retval);
304
305 rval = ir->get_value();
306 rval->accept(this);
307 rval = this->result->as_rvalue();
308 assert(rval);
309
Ian Romanick70fe8b62010-05-19 11:37:35 +0200310 result = new ir_assignment(new ir_dereference_variable(this->retval), rval,
311 NULL);
Eric Anholtcad97662010-04-07 11:46:26 -0700312}
313
314
315void
316ir_function_cloning_visitor::visit(ir_if *ir)
317{
Eric Anholte8e97482010-04-22 17:52:59 -0700318 /* FINISHME: Implement if cloning. */
319 assert(0);
320
Eric Anholtcad97662010-04-07 11:46:26 -0700321 (void) ir;
322 result = NULL;
323}
324
325bool
Eric Anholt0d423212010-04-16 12:53:46 -0700326automatic_inlining_predicate(ir_instruction *ir)
327{
328 ir_call *call = ir->as_call();
329
330 if (call && can_inline(call))
331 return true;
332
333 return false;
334}
335
336bool
Eric Anholtcad97662010-04-07 11:46:26 -0700337do_function_inlining(exec_list *instructions)
338{
Eric Anholt2a7b2b22010-04-08 13:42:48 -0700339 bool progress = false;
Eric Anholtcad97662010-04-07 11:46:26 -0700340
Eric Anholt0d423212010-04-16 12:53:46 -0700341 do_expression_flattening(instructions, automatic_inlining_predicate);
342
Eric Anholtcad97662010-04-07 11:46:26 -0700343 foreach_iter(exec_list_iterator, iter, *instructions) {
344 ir_instruction *ir = (ir_instruction *)iter.get();
345 ir_assignment *assign = ir->as_assignment();
346 ir_call *call;
347
348 if (assign) {
349 call = assign->rhs->as_call();
350 if (!call || !can_inline(call))
351 continue;
352
353 /* generates the parameter setup, function body, and returns the return
354 * value of the function
355 */
356 ir_rvalue *rhs = call->generate_inline(ir);
357 assert(rhs);
358
359 assign->rhs = rhs;
360 progress = true;
361 } else if ((call = ir->as_call()) && can_inline(call)) {
362 (void)call->generate_inline(ir);
363 ir->remove();
364 progress = true;
365 } else {
366 ir_function_inlining_visitor v;
367 ir->accept(&v);
368 }
369 }
370
371 return progress;
372}
373
374ir_rvalue *
375ir_call::generate_inline(ir_instruction *next_ir)
376{
377 ir_variable **parameters;
378 int num_parameters;
379 int i;
380 ir_variable *retval = NULL;
381
382 num_parameters = 0;
383 foreach_iter(exec_list_iterator, iter_sig, this->callee->parameters)
384 num_parameters++;
385
386 parameters = new ir_variable *[num_parameters];
387
388 /* Generate storage for the return value. */
389 if (this->callee->return_type) {
390 retval = new ir_variable(this->callee->return_type, "__retval");
391 next_ir->insert_before(retval);
392 }
393
394 ir_function_cloning_visitor v = ir_function_cloning_visitor(retval);
395
396 /* Generate the declarations for the parameters to our inlined code,
397 * and set up the mapping of real function body variables to ours.
398 */
399 i = 0;
400 exec_list_iterator sig_param_iter = this->callee->parameters.iterator();
401 exec_list_iterator param_iter = this->actual_parameters.iterator();
402 for (i = 0; i < num_parameters; i++) {
403 const ir_variable *const sig_param = (ir_variable *) sig_param_iter.get();
404 ir_rvalue *param = (ir_rvalue *) param_iter.get();
405
406 /* Generate a new variable for the parameter. */
407 parameters[i] = sig_param->clone();
408 next_ir->insert_before(parameters[i]);
409
410 v.remap_variable(sig_param, parameters[i]);
411
412 /* Move the actual param into our param variable if it's an 'in' type. */
413 if (parameters[i]->mode == ir_var_in ||
414 parameters[i]->mode == ir_var_inout) {
415 ir_assignment *assign;
416
Ian Romanick70fe8b62010-05-19 11:37:35 +0200417 assign = new ir_assignment(new ir_dereference_variable(parameters[i]),
Eric Anholtcad97662010-04-07 11:46:26 -0700418 param, NULL);
419 next_ir->insert_before(assign);
420 }
421
422 sig_param_iter.next();
423 param_iter.next();
424 }
425
426 /* Generate the inlined body of the function. */
427 foreach_iter(exec_list_iterator, iter, callee->body) {
428 ir_instruction *ir = (ir_instruction *)iter.get();
429
430 ir->accept(&v);
431 assert(v.result);
432 next_ir->insert_before(v.result);
433 }
434
Kenneth Graunkec07fdae2010-04-30 23:38:50 -0700435 /* Copy back the value of any 'out' parameters from the function body
436 * variables to our own.
Eric Anholtcad97662010-04-07 11:46:26 -0700437 */
438 i = 0;
439 param_iter = this->actual_parameters.iterator();
440 for (i = 0; i < num_parameters; i++) {
441 ir_instruction *const param = (ir_instruction *) param_iter.get();
442
Kenneth Graunkec07fdae2010-04-30 23:38:50 -0700443 /* Move our param variable into the actual param if it's an 'out' type. */
Eric Anholtcad97662010-04-07 11:46:26 -0700444 if (parameters[i]->mode == ir_var_out ||
445 parameters[i]->mode == ir_var_inout) {
446 ir_assignment *assign;
447
448 assign = new ir_assignment(param->as_rvalue(),
Ian Romanick70fe8b62010-05-19 11:37:35 +0200449 new ir_dereference_variable(parameters[i]),
Eric Anholtcad97662010-04-07 11:46:26 -0700450 NULL);
451 next_ir->insert_before(assign);
452 }
453
454 param_iter.next();
455 }
456
457 delete(parameters);
458
459 if (retval)
Ian Romanick70fe8b62010-05-19 11:37:35 +0200460 return new ir_dereference_variable(retval);
Eric Anholtcad97662010-04-07 11:46:26 -0700461 else
462 return NULL;
463}
464
465void
466ir_function_inlining_visitor::visit(ir_variable *ir)
467{
468 (void) ir;
469}
470
471
472void
Eric Anholtcad97662010-04-07 11:46:26 -0700473ir_function_inlining_visitor::visit(ir_loop *ir)
474{
475 do_function_inlining(&ir->body_instructions);
476}
477
478void
479ir_function_inlining_visitor::visit(ir_loop_jump *ir)
480{
481 (void) ir;
482}
483
484
485void
486ir_function_inlining_visitor::visit(ir_function_signature *ir)
487{
488 do_function_inlining(&ir->body);
489}
490
491
492void
493ir_function_inlining_visitor::visit(ir_function *ir)
494{
Kenneth Graunke9fa99f32010-04-21 12:30:22 -0700495 foreach_iter(exec_list_iterator, iter, *ir) {
496 ir_function_signature *const sig = (ir_function_signature *) iter.get();
497 sig->accept(this);
498 }
Eric Anholtcad97662010-04-07 11:46:26 -0700499}
500
501void
502ir_function_inlining_visitor::visit(ir_expression *ir)
503{
504 unsigned int operand;
505
506 for (operand = 0; operand < ir->get_num_operands(); operand++) {
507 ir->operands[operand]->accept(this);
508 }
509}
510
511
512void
513ir_function_inlining_visitor::visit(ir_swizzle *ir)
514{
515 ir->val->accept(this);
516}
517
518
519void
Ian Romanickc7b10462010-05-19 13:20:12 +0200520ir_function_inlining_visitor::visit(ir_dereference_variable *ir)
Eric Anholtcad97662010-04-07 11:46:26 -0700521{
Ian Romanickc7b10462010-05-19 13:20:12 +0200522 ir->var->accept(this);
523}
524
525void
526ir_function_inlining_visitor::visit(ir_dereference_array *ir)
527{
Ian Romanick36ea2862010-05-19 13:52:29 +0200528 ir->array_index->accept(this);
529 ir->array->accept(this);
Ian Romanickc7b10462010-05-19 13:20:12 +0200530}
531
532void
533ir_function_inlining_visitor::visit(ir_dereference_record *ir)
534{
Ian Romanick36ea2862010-05-19 13:52:29 +0200535 ir->record->accept(this);
Eric Anholtcad97662010-04-07 11:46:26 -0700536}
537
538void
539ir_function_inlining_visitor::visit(ir_assignment *ir)
540{
541 ir->rhs->accept(this);
542}
543
544
545void
546ir_function_inlining_visitor::visit(ir_constant *ir)
547{
548 (void) ir;
549}
550
551
552void
553ir_function_inlining_visitor::visit(ir_call *ir)
554{
555 (void) ir;
556}
557
558
559void
560ir_function_inlining_visitor::visit(ir_return *ir)
561{
562 (void) ir;
563}
564
565
566void
567ir_function_inlining_visitor::visit(ir_if *ir)
568{
569 ir->condition->accept(this);
570
571 do_function_inlining(&ir->then_instructions);
572 do_function_inlining(&ir->else_instructions);
573}