blob: c0e77b427304875ca287a8a4700c3bae3963e9e5 [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 *);
Eric Anholtcad97662010-04-07 11:46:26 -070095 virtual void visit(ir_loop *);
96 virtual void visit(ir_loop_jump *);
97 virtual void visit(ir_function_signature *);
98 virtual void visit(ir_function *);
99 virtual void visit(ir_expression *);
100 virtual void visit(ir_swizzle *);
101 virtual void visit(ir_dereference *);
102 virtual void visit(ir_assignment *);
103 virtual void visit(ir_constant *);
104 virtual void visit(ir_call *);
105 virtual void visit(ir_return *);
106 virtual void visit(ir_if *);
107 /*@}*/
108
109 ir_instruction *result;
110};
111
112void
113ir_function_cloning_visitor::visit(ir_variable *ir)
114{
115 ir_variable *new_var = ir->clone();
116
117 this->result = new_var;
118
119 this->remap_variable(ir, new_var);
120}
121
122void
Eric Anholtcad97662010-04-07 11:46:26 -0700123ir_function_cloning_visitor::visit(ir_loop *ir)
124{
125 (void)ir;
126 this->result = NULL;
127}
128
129void
130ir_function_cloning_visitor::visit(ir_loop_jump *ir)
131{
132 (void) ir;
133 this->result = NULL;
134}
135
136
137void
138ir_function_cloning_visitor::visit(ir_function_signature *ir)
139{
140 (void)ir;
141 this->result = NULL;
142}
143
144
145void
146ir_function_cloning_visitor::visit(ir_function *ir)
147{
148 (void) ir;
149 this->result = NULL;
150}
151
152void
153ir_function_cloning_visitor::visit(ir_expression *ir)
154{
155 unsigned int operand;
156 ir_rvalue *op[2] = {NULL, NULL};
157
158 for (operand = 0; operand < ir->get_num_operands(); operand++) {
159 ir->operands[operand]->accept(this);
160 op[operand] = this->result->as_rvalue();
161 assert(op[operand]);
162 }
163
164 this->result = new ir_expression(ir->operation, ir->type, op[0], op[1]);
165}
166
167
168void
169ir_function_cloning_visitor::visit(ir_swizzle *ir)
170{
171 ir->val->accept(this);
172
173 this->result = new ir_swizzle(this->result->as_rvalue(), ir->mask);
174}
175
176void
177ir_function_cloning_visitor::visit(ir_dereference *ir)
178{
179 if (ir->mode == ir_dereference::ir_reference_variable) {
180 ir_variable *old_var = ir->var->as_variable();
181
182 /* If it's a deref of a real variable, then we need to remap it if
183 * it was local to the function.
184 */
185 if (old_var) {
186 ir_variable *new_var;
187
188 new_var = this->get_remapped_variable(old_var);
189
190 this->result = new ir_dereference(new_var);
191 } else {
192 ir->var->accept(this);
193
194 this->result = new ir_dereference(this->result);
195 }
Eric Anholt61924342010-04-08 13:40:52 -0700196 } else if (ir->mode == ir_dereference::ir_reference_array) {
197 ir_instruction *variable;
198 ir_rvalue *index;
199
200 ir->var->accept(this);
201 variable = this->result;
202
203 ir->selector.array_index->accept(this);
204 index = this->result->as_rvalue();
205
206 this->result = new ir_dereference(variable, index);
Eric Anholtcad97662010-04-07 11:46:26 -0700207 } else {
Eric Anholt61924342010-04-08 13:40:52 -0700208 assert(ir->mode == ir_dereference::ir_reference_record);
209 /* FINISHME: inlining of structure references */
210 assert(0);
Eric Anholtcad97662010-04-07 11:46:26 -0700211 }
212}
213
214void
215ir_function_cloning_visitor::visit(ir_assignment *ir)
216{
217 ir_rvalue *lhs, *rhs, *condition;
218
219 ir->lhs->accept(this);
220 lhs = this->result->as_rvalue();
221
222 ir->rhs->accept(this);
223 rhs = this->result->as_rvalue();
224
225 ir->condition->accept(this);
226 condition = this->result->as_rvalue();
227
228 this->result = new ir_assignment(lhs, rhs, condition);
229}
230
231
232void
233ir_function_cloning_visitor::visit(ir_constant *ir)
234{
235 this->result = ir->clone();
236}
237
238
239void
240ir_function_cloning_visitor::visit(ir_call *ir)
241{
242 exec_list parameters;
243
244 foreach_iter(exec_list_iterator, iter, *ir) {
245 ir_rvalue *param = (ir_rvalue *)iter.get();
246
247 param->accept(this);
248 parameters.push_tail(this->result);
249 }
250
251 this->result = new ir_call(ir->get_callee(), &parameters);
252}
253
254
255void
256ir_function_cloning_visitor::visit(ir_return *ir)
257{
258 ir_rvalue *rval;
259
260 assert(this->retval);
261
262 rval = ir->get_value();
263 rval->accept(this);
264 rval = this->result->as_rvalue();
265 assert(rval);
266
267 result = new ir_assignment(new ir_dereference(this->retval),
268 ir->get_value(), NULL);
269}
270
271
272void
273ir_function_cloning_visitor::visit(ir_if *ir)
274{
275 (void) ir;
276 result = NULL;
277}
278
279bool
280can_inline(ir_call *call)
281{
282 bool found_return = false;
283
284 /* FINISHME: Right now we only allow a single statement that is a return.
285 */
286 foreach_iter(exec_list_iterator, iter, call->get_callee()->body) {
287 ir_instruction *ir = (ir_instruction *)iter.get();
288 if (ir->get_next()->get_next() != NULL)
289 return false;
290
291 if (!ir->as_return())
292 return false;
293
294 found_return = true;
295 }
296
297 return found_return;
298}
299
300bool
Eric Anholt0d423212010-04-16 12:53:46 -0700301automatic_inlining_predicate(ir_instruction *ir)
302{
303 ir_call *call = ir->as_call();
304
305 if (call && can_inline(call))
306 return true;
307
308 return false;
309}
310
311bool
Eric Anholtcad97662010-04-07 11:46:26 -0700312do_function_inlining(exec_list *instructions)
313{
Eric Anholt2a7b2b22010-04-08 13:42:48 -0700314 bool progress = false;
Eric Anholtcad97662010-04-07 11:46:26 -0700315
Eric Anholt0d423212010-04-16 12:53:46 -0700316 do_expression_flattening(instructions, automatic_inlining_predicate);
317
Eric Anholtcad97662010-04-07 11:46:26 -0700318 foreach_iter(exec_list_iterator, iter, *instructions) {
319 ir_instruction *ir = (ir_instruction *)iter.get();
320 ir_assignment *assign = ir->as_assignment();
321 ir_call *call;
322
323 if (assign) {
324 call = assign->rhs->as_call();
325 if (!call || !can_inline(call))
326 continue;
327
328 /* generates the parameter setup, function body, and returns the return
329 * value of the function
330 */
331 ir_rvalue *rhs = call->generate_inline(ir);
332 assert(rhs);
333
334 assign->rhs = rhs;
335 progress = true;
336 } else if ((call = ir->as_call()) && can_inline(call)) {
337 (void)call->generate_inline(ir);
338 ir->remove();
339 progress = true;
340 } else {
341 ir_function_inlining_visitor v;
342 ir->accept(&v);
343 }
344 }
345
346 return progress;
347}
348
349ir_rvalue *
350ir_call::generate_inline(ir_instruction *next_ir)
351{
352 ir_variable **parameters;
353 int num_parameters;
354 int i;
355 ir_variable *retval = NULL;
356
357 num_parameters = 0;
358 foreach_iter(exec_list_iterator, iter_sig, this->callee->parameters)
359 num_parameters++;
360
361 parameters = new ir_variable *[num_parameters];
362
363 /* Generate storage for the return value. */
364 if (this->callee->return_type) {
365 retval = new ir_variable(this->callee->return_type, "__retval");
366 next_ir->insert_before(retval);
367 }
368
369 ir_function_cloning_visitor v = ir_function_cloning_visitor(retval);
370
371 /* Generate the declarations for the parameters to our inlined code,
372 * and set up the mapping of real function body variables to ours.
373 */
374 i = 0;
375 exec_list_iterator sig_param_iter = this->callee->parameters.iterator();
376 exec_list_iterator param_iter = this->actual_parameters.iterator();
377 for (i = 0; i < num_parameters; i++) {
378 const ir_variable *const sig_param = (ir_variable *) sig_param_iter.get();
379 ir_rvalue *param = (ir_rvalue *) param_iter.get();
380
381 /* Generate a new variable for the parameter. */
382 parameters[i] = sig_param->clone();
383 next_ir->insert_before(parameters[i]);
384
385 v.remap_variable(sig_param, parameters[i]);
386
387 /* Move the actual param into our param variable if it's an 'in' type. */
388 if (parameters[i]->mode == ir_var_in ||
389 parameters[i]->mode == ir_var_inout) {
390 ir_assignment *assign;
391
392 assign = new ir_assignment(new ir_dereference(parameters[i]),
393 param, NULL);
394 next_ir->insert_before(assign);
395 }
396
397 sig_param_iter.next();
398 param_iter.next();
399 }
400
401 /* Generate the inlined body of the function. */
402 foreach_iter(exec_list_iterator, iter, callee->body) {
403 ir_instruction *ir = (ir_instruction *)iter.get();
404
405 ir->accept(&v);
406 assert(v.result);
407 next_ir->insert_before(v.result);
408 }
409
410 /* Generate the declarations for the parameters to our inlined code,
411 * and set up the mapping of real function body variables to ours.
412 */
413 i = 0;
414 param_iter = this->actual_parameters.iterator();
415 for (i = 0; i < num_parameters; i++) {
416 ir_instruction *const param = (ir_instruction *) param_iter.get();
417
418 /* Move the actual param into our param variable if it's an 'in' type. */
419 if (parameters[i]->mode == ir_var_out ||
420 parameters[i]->mode == ir_var_inout) {
421 ir_assignment *assign;
422
423 assign = new ir_assignment(param->as_rvalue(),
424 new ir_dereference(parameters[i]),
425 NULL);
426 next_ir->insert_before(assign);
427 }
428
429 param_iter.next();
430 }
431
432 delete(parameters);
433
434 if (retval)
435 return new ir_dereference(retval);
436 else
437 return NULL;
438}
439
440void
441ir_function_inlining_visitor::visit(ir_variable *ir)
442{
443 (void) ir;
444}
445
446
447void
Eric Anholtcad97662010-04-07 11:46:26 -0700448ir_function_inlining_visitor::visit(ir_loop *ir)
449{
450 do_function_inlining(&ir->body_instructions);
451}
452
453void
454ir_function_inlining_visitor::visit(ir_loop_jump *ir)
455{
456 (void) ir;
457}
458
459
460void
461ir_function_inlining_visitor::visit(ir_function_signature *ir)
462{
463 do_function_inlining(&ir->body);
464}
465
466
467void
468ir_function_inlining_visitor::visit(ir_function *ir)
469{
Kenneth Graunke9fa99f32010-04-21 12:30:22 -0700470 foreach_iter(exec_list_iterator, iter, *ir) {
471 ir_function_signature *const sig = (ir_function_signature *) iter.get();
472 sig->accept(this);
473 }
Eric Anholtcad97662010-04-07 11:46:26 -0700474}
475
476void
477ir_function_inlining_visitor::visit(ir_expression *ir)
478{
479 unsigned int operand;
480
481 for (operand = 0; operand < ir->get_num_operands(); operand++) {
482 ir->operands[operand]->accept(this);
483 }
484}
485
486
487void
488ir_function_inlining_visitor::visit(ir_swizzle *ir)
489{
490 ir->val->accept(this);
491}
492
493
494void
495ir_function_inlining_visitor::visit(ir_dereference *ir)
496{
497 if (ir->mode == ir_dereference::ir_reference_array) {
498 ir->selector.array_index->accept(this);
499 }
500 ir->var->accept(this);
501}
502
503void
504ir_function_inlining_visitor::visit(ir_assignment *ir)
505{
506 ir->rhs->accept(this);
507}
508
509
510void
511ir_function_inlining_visitor::visit(ir_constant *ir)
512{
513 (void) ir;
514}
515
516
517void
518ir_function_inlining_visitor::visit(ir_call *ir)
519{
520 (void) ir;
521}
522
523
524void
525ir_function_inlining_visitor::visit(ir_return *ir)
526{
527 (void) ir;
528}
529
530
531void
532ir_function_inlining_visitor::visit(ir_if *ir)
533{
534 ir->condition->accept(this);
535
536 do_function_inlining(&ir->then_instructions);
537 do_function_inlining(&ir->else_instructions);
538}