blob: af6a477d9b48948741d2bbab0b91c96ab25519b6 [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
37class variable_remap : public exec_node {
38public:
39 variable_remap(const ir_variable *old_var, ir_variable *new_var)
40 : old_var(old_var), new_var(new_var)
41 {
42 /* empty */
43 }
44 const ir_variable *old_var;
45 ir_variable *new_var;
46};
47
48class ir_function_cloning_visitor : public ir_visitor {
49public:
50 ir_function_cloning_visitor(ir_variable *retval)
51 : retval(retval)
52 {
53 /* empty */
54 }
55
56 virtual ~ir_function_cloning_visitor()
57 {
58 /* empty */
59 }
60
61 void remap_variable(const ir_variable *old_var, ir_variable *new_var) {
62 variable_remap *remap = new variable_remap(old_var, new_var);
63 this->remap_list.push_tail(remap);
64 }
65
66 ir_variable *get_remapped_variable(ir_variable *var) {
67 foreach_iter(exec_list_iterator, iter, this->remap_list) {
68 variable_remap *remap = (variable_remap *)iter.get();
69
70 if (var == remap->old_var)
71 return remap->new_var;
72 }
73
74 /* Not a reapped variable, so a global scoped reference, for example. */
75 return var;
76 }
77
78 /* List of variable_remap for mapping from original function body variables
79 * to inlined function body variables.
80 */
81 exec_list remap_list;
82
83 /* Return value for the inlined function. */
84 ir_variable *retval;
85
86 /**
87 * \name Visit methods
88 *
89 * As typical for the visitor pattern, there must be one \c visit method for
90 * each concrete subclass of \c ir_instruction. Virtual base classes within
91 * the hierarchy should not have \c visit methods.
92 */
93 /*@{*/
94 virtual void visit(ir_variable *);
95 virtual void visit(ir_label *);
96 virtual void visit(ir_loop *);
97 virtual void visit(ir_loop_jump *);
98 virtual void visit(ir_function_signature *);
99 virtual void visit(ir_function *);
100 virtual void visit(ir_expression *);
101 virtual void visit(ir_swizzle *);
102 virtual void visit(ir_dereference *);
103 virtual void visit(ir_assignment *);
104 virtual void visit(ir_constant *);
105 virtual void visit(ir_call *);
106 virtual void visit(ir_return *);
107 virtual void visit(ir_if *);
108 /*@}*/
109
110 ir_instruction *result;
111};
112
113void
114ir_function_cloning_visitor::visit(ir_variable *ir)
115{
116 ir_variable *new_var = ir->clone();
117
118 this->result = new_var;
119
120 this->remap_variable(ir, new_var);
121}
122
123void
124ir_function_cloning_visitor::visit(ir_label *ir)
125{
126 (void)ir;
127 this->result = NULL;
128}
129
130void
131ir_function_cloning_visitor::visit(ir_loop *ir)
132{
133 (void)ir;
134 this->result = NULL;
135}
136
137void
138ir_function_cloning_visitor::visit(ir_loop_jump *ir)
139{
140 (void) ir;
141 this->result = NULL;
142}
143
144
145void
146ir_function_cloning_visitor::visit(ir_function_signature *ir)
147{
148 (void)ir;
149 this->result = NULL;
150}
151
152
153void
154ir_function_cloning_visitor::visit(ir_function *ir)
155{
156 (void) ir;
157 this->result = NULL;
158}
159
160void
161ir_function_cloning_visitor::visit(ir_expression *ir)
162{
163 unsigned int operand;
164 ir_rvalue *op[2] = {NULL, NULL};
165
166 for (operand = 0; operand < ir->get_num_operands(); operand++) {
167 ir->operands[operand]->accept(this);
168 op[operand] = this->result->as_rvalue();
169 assert(op[operand]);
170 }
171
172 this->result = new ir_expression(ir->operation, ir->type, op[0], op[1]);
173}
174
175
176void
177ir_function_cloning_visitor::visit(ir_swizzle *ir)
178{
179 ir->val->accept(this);
180
181 this->result = new ir_swizzle(this->result->as_rvalue(), ir->mask);
182}
183
184void
185ir_function_cloning_visitor::visit(ir_dereference *ir)
186{
187 if (ir->mode == ir_dereference::ir_reference_variable) {
188 ir_variable *old_var = ir->var->as_variable();
189
190 /* If it's a deref of a real variable, then we need to remap it if
191 * it was local to the function.
192 */
193 if (old_var) {
194 ir_variable *new_var;
195
196 new_var = this->get_remapped_variable(old_var);
197
198 this->result = new ir_dereference(new_var);
199 } else {
200 ir->var->accept(this);
201
202 this->result = new ir_dereference(this->result);
203 }
Eric Anholt61924342010-04-08 13:40:52 -0700204 } else if (ir->mode == ir_dereference::ir_reference_array) {
205 ir_instruction *variable;
206 ir_rvalue *index;
207
208 ir->var->accept(this);
209 variable = this->result;
210
211 ir->selector.array_index->accept(this);
212 index = this->result->as_rvalue();
213
214 this->result = new ir_dereference(variable, index);
Eric Anholtcad97662010-04-07 11:46:26 -0700215 } else {
Eric Anholt61924342010-04-08 13:40:52 -0700216 assert(ir->mode == ir_dereference::ir_reference_record);
217 /* FINISHME: inlining of structure references */
218 assert(0);
Eric Anholtcad97662010-04-07 11:46:26 -0700219 }
220}
221
222void
223ir_function_cloning_visitor::visit(ir_assignment *ir)
224{
225 ir_rvalue *lhs, *rhs, *condition;
226
227 ir->lhs->accept(this);
228 lhs = this->result->as_rvalue();
229
230 ir->rhs->accept(this);
231 rhs = this->result->as_rvalue();
232
233 ir->condition->accept(this);
234 condition = this->result->as_rvalue();
235
236 this->result = new ir_assignment(lhs, rhs, condition);
237}
238
239
240void
241ir_function_cloning_visitor::visit(ir_constant *ir)
242{
243 this->result = ir->clone();
244}
245
246
247void
248ir_function_cloning_visitor::visit(ir_call *ir)
249{
250 exec_list parameters;
251
252 foreach_iter(exec_list_iterator, iter, *ir) {
253 ir_rvalue *param = (ir_rvalue *)iter.get();
254
255 param->accept(this);
256 parameters.push_tail(this->result);
257 }
258
259 this->result = new ir_call(ir->get_callee(), &parameters);
260}
261
262
263void
264ir_function_cloning_visitor::visit(ir_return *ir)
265{
266 ir_rvalue *rval;
267
268 assert(this->retval);
269
270 rval = ir->get_value();
271 rval->accept(this);
272 rval = this->result->as_rvalue();
273 assert(rval);
274
275 result = new ir_assignment(new ir_dereference(this->retval),
276 ir->get_value(), NULL);
277}
278
279
280void
281ir_function_cloning_visitor::visit(ir_if *ir)
282{
283 (void) ir;
284 result = NULL;
285}
286
287bool
288can_inline(ir_call *call)
289{
290 bool found_return = false;
291
292 /* FINISHME: Right now we only allow a single statement that is a return.
293 */
294 foreach_iter(exec_list_iterator, iter, call->get_callee()->body) {
295 ir_instruction *ir = (ir_instruction *)iter.get();
296 if (ir->get_next()->get_next() != NULL)
297 return false;
298
299 if (!ir->as_return())
300 return false;
301
302 found_return = true;
303 }
304
305 return found_return;
306}
307
308bool
Eric Anholt0d423212010-04-16 12:53:46 -0700309automatic_inlining_predicate(ir_instruction *ir)
310{
311 ir_call *call = ir->as_call();
312
313 if (call && can_inline(call))
314 return true;
315
316 return false;
317}
318
319bool
Eric Anholtcad97662010-04-07 11:46:26 -0700320do_function_inlining(exec_list *instructions)
321{
Eric Anholt2a7b2b22010-04-08 13:42:48 -0700322 bool progress = false;
Eric Anholtcad97662010-04-07 11:46:26 -0700323
Eric Anholt0d423212010-04-16 12:53:46 -0700324 do_expression_flattening(instructions, automatic_inlining_predicate);
325
Eric Anholtcad97662010-04-07 11:46:26 -0700326 foreach_iter(exec_list_iterator, iter, *instructions) {
327 ir_instruction *ir = (ir_instruction *)iter.get();
328 ir_assignment *assign = ir->as_assignment();
329 ir_call *call;
330
331 if (assign) {
332 call = assign->rhs->as_call();
333 if (!call || !can_inline(call))
334 continue;
335
336 /* generates the parameter setup, function body, and returns the return
337 * value of the function
338 */
339 ir_rvalue *rhs = call->generate_inline(ir);
340 assert(rhs);
341
342 assign->rhs = rhs;
343 progress = true;
344 } else if ((call = ir->as_call()) && can_inline(call)) {
345 (void)call->generate_inline(ir);
346 ir->remove();
347 progress = true;
348 } else {
349 ir_function_inlining_visitor v;
350 ir->accept(&v);
351 }
352 }
353
354 return progress;
355}
356
357ir_rvalue *
358ir_call::generate_inline(ir_instruction *next_ir)
359{
360 ir_variable **parameters;
361 int num_parameters;
362 int i;
363 ir_variable *retval = NULL;
364
365 num_parameters = 0;
366 foreach_iter(exec_list_iterator, iter_sig, this->callee->parameters)
367 num_parameters++;
368
369 parameters = new ir_variable *[num_parameters];
370
371 /* Generate storage for the return value. */
372 if (this->callee->return_type) {
373 retval = new ir_variable(this->callee->return_type, "__retval");
374 next_ir->insert_before(retval);
375 }
376
377 ir_function_cloning_visitor v = ir_function_cloning_visitor(retval);
378
379 /* Generate the declarations for the parameters to our inlined code,
380 * and set up the mapping of real function body variables to ours.
381 */
382 i = 0;
383 exec_list_iterator sig_param_iter = this->callee->parameters.iterator();
384 exec_list_iterator param_iter = this->actual_parameters.iterator();
385 for (i = 0; i < num_parameters; i++) {
386 const ir_variable *const sig_param = (ir_variable *) sig_param_iter.get();
387 ir_rvalue *param = (ir_rvalue *) param_iter.get();
388
389 /* Generate a new variable for the parameter. */
390 parameters[i] = sig_param->clone();
391 next_ir->insert_before(parameters[i]);
392
393 v.remap_variable(sig_param, parameters[i]);
394
395 /* Move the actual param into our param variable if it's an 'in' type. */
396 if (parameters[i]->mode == ir_var_in ||
397 parameters[i]->mode == ir_var_inout) {
398 ir_assignment *assign;
399
400 assign = new ir_assignment(new ir_dereference(parameters[i]),
401 param, NULL);
402 next_ir->insert_before(assign);
403 }
404
405 sig_param_iter.next();
406 param_iter.next();
407 }
408
409 /* Generate the inlined body of the function. */
410 foreach_iter(exec_list_iterator, iter, callee->body) {
411 ir_instruction *ir = (ir_instruction *)iter.get();
412
413 ir->accept(&v);
414 assert(v.result);
415 next_ir->insert_before(v.result);
416 }
417
418 /* Generate the declarations for the parameters to our inlined code,
419 * and set up the mapping of real function body variables to ours.
420 */
421 i = 0;
422 param_iter = this->actual_parameters.iterator();
423 for (i = 0; i < num_parameters; i++) {
424 ir_instruction *const param = (ir_instruction *) param_iter.get();
425
426 /* Move the actual param into our param variable if it's an 'in' type. */
427 if (parameters[i]->mode == ir_var_out ||
428 parameters[i]->mode == ir_var_inout) {
429 ir_assignment *assign;
430
431 assign = new ir_assignment(param->as_rvalue(),
432 new ir_dereference(parameters[i]),
433 NULL);
434 next_ir->insert_before(assign);
435 }
436
437 param_iter.next();
438 }
439
440 delete(parameters);
441
442 if (retval)
443 return new ir_dereference(retval);
444 else
445 return NULL;
446}
447
448void
449ir_function_inlining_visitor::visit(ir_variable *ir)
450{
451 (void) ir;
452}
453
454
455void
456ir_function_inlining_visitor::visit(ir_label *ir)
457{
458 ir->signature->accept(this);
459}
460
461void
462ir_function_inlining_visitor::visit(ir_loop *ir)
463{
464 do_function_inlining(&ir->body_instructions);
465}
466
467void
468ir_function_inlining_visitor::visit(ir_loop_jump *ir)
469{
470 (void) ir;
471}
472
473
474void
475ir_function_inlining_visitor::visit(ir_function_signature *ir)
476{
477 do_function_inlining(&ir->body);
478}
479
480
481void
482ir_function_inlining_visitor::visit(ir_function *ir)
483{
484 (void) ir;
485}
486
487void
488ir_function_inlining_visitor::visit(ir_expression *ir)
489{
490 unsigned int operand;
491
492 for (operand = 0; operand < ir->get_num_operands(); operand++) {
493 ir->operands[operand]->accept(this);
494 }
495}
496
497
498void
499ir_function_inlining_visitor::visit(ir_swizzle *ir)
500{
501 ir->val->accept(this);
502}
503
504
505void
506ir_function_inlining_visitor::visit(ir_dereference *ir)
507{
508 if (ir->mode == ir_dereference::ir_reference_array) {
509 ir->selector.array_index->accept(this);
510 }
511 ir->var->accept(this);
512}
513
514void
515ir_function_inlining_visitor::visit(ir_assignment *ir)
516{
517 ir->rhs->accept(this);
518}
519
520
521void
522ir_function_inlining_visitor::visit(ir_constant *ir)
523{
524 (void) ir;
525}
526
527
528void
529ir_function_inlining_visitor::visit(ir_call *ir)
530{
531 (void) ir;
532}
533
534
535void
536ir_function_inlining_visitor::visit(ir_return *ir)
537{
538 (void) ir;
539}
540
541
542void
543ir_function_inlining_visitor::visit(ir_if *ir)
544{
545 ir->condition->accept(this);
546
547 do_function_inlining(&ir->then_instructions);
548 do_function_inlining(&ir->else_instructions);
549}