blob: 5b1b3cb8b0b7fa13797636299fd82b12cbf71d78 [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{
Eric Anholte8e97482010-04-22 17:52:59 -0700125 /* FINISHME: Implement loop cloning. */
126 assert(0);
127
Eric Anholtcad97662010-04-07 11:46:26 -0700128 (void)ir;
129 this->result = NULL;
130}
131
132void
133ir_function_cloning_visitor::visit(ir_loop_jump *ir)
134{
Eric Anholte8e97482010-04-22 17:52:59 -0700135 /* FINISHME: Implement loop cloning. */
136 assert(0);
137
Eric Anholtcad97662010-04-07 11:46:26 -0700138 (void) ir;
139 this->result = NULL;
140}
141
142
143void
144ir_function_cloning_visitor::visit(ir_function_signature *ir)
145{
Eric Anholte8e97482010-04-22 17:52:59 -0700146 assert(0);
Eric Anholtcad97662010-04-07 11:46:26 -0700147 (void)ir;
148 this->result = NULL;
149}
150
151
152void
153ir_function_cloning_visitor::visit(ir_function *ir)
154{
Eric Anholte8e97482010-04-22 17:52:59 -0700155 assert(0);
Eric Anholtcad97662010-04-07 11:46:26 -0700156 (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{
Eric Anholtc0bfe872010-04-26 15:01:50 -0700187 ir_variable *old_var = ir->var->as_variable();
188 ir_instruction *var;
Eric Anholtcad97662010-04-07 11:46:26 -0700189
Eric Anholtc0bfe872010-04-26 15:01:50 -0700190 if (old_var)
191 var = this->get_remapped_variable(old_var);
192 else {
Eric Anholt61924342010-04-08 13:40:52 -0700193 ir->var->accept(this);
Eric Anholtc0bfe872010-04-26 15:01:50 -0700194 var = this->result;
195 }
196
197 if (ir->mode == ir_dereference::ir_reference_variable) {
198 this->result = new ir_dereference(var);
199 } else if (ir->mode == ir_dereference::ir_reference_array) {
200 ir_rvalue *index;
Eric Anholt61924342010-04-08 13:40:52 -0700201
202 ir->selector.array_index->accept(this);
203 index = this->result->as_rvalue();
204
Eric Anholtc0bfe872010-04-26 15:01:50 -0700205 this->result = new ir_dereference(var, index);
Eric Anholtcad97662010-04-07 11:46:26 -0700206 } else {
Eric Anholt61924342010-04-08 13:40:52 -0700207 assert(ir->mode == ir_dereference::ir_reference_record);
208 /* FINISHME: inlining of structure references */
209 assert(0);
Eric Anholtcad97662010-04-07 11:46:26 -0700210 }
211}
212
213void
214ir_function_cloning_visitor::visit(ir_assignment *ir)
215{
Eric Anholt22147be2010-04-22 18:41:32 -0700216 ir_rvalue *lhs, *rhs, *condition = NULL;
Eric Anholtcad97662010-04-07 11:46:26 -0700217
218 ir->lhs->accept(this);
219 lhs = this->result->as_rvalue();
220
221 ir->rhs->accept(this);
222 rhs = this->result->as_rvalue();
223
Eric Anholt22147be2010-04-22 18:41:32 -0700224 if (ir->condition) {
225 ir->condition->accept(this);
226 condition = this->result->as_rvalue();
227 }
Eric Anholtcad97662010-04-07 11:46:26 -0700228
229 this->result = new ir_assignment(lhs, rhs, condition);
230}
231
232
233void
234ir_function_cloning_visitor::visit(ir_constant *ir)
235{
236 this->result = ir->clone();
237}
238
239
240void
241ir_function_cloning_visitor::visit(ir_call *ir)
242{
243 exec_list parameters;
244
245 foreach_iter(exec_list_iterator, iter, *ir) {
246 ir_rvalue *param = (ir_rvalue *)iter.get();
247
248 param->accept(this);
249 parameters.push_tail(this->result);
250 }
251
252 this->result = new ir_call(ir->get_callee(), &parameters);
253}
254
255
256void
257ir_function_cloning_visitor::visit(ir_return *ir)
258{
259 ir_rvalue *rval;
260
261 assert(this->retval);
262
263 rval = ir->get_value();
264 rval->accept(this);
265 rval = this->result->as_rvalue();
266 assert(rval);
267
268 result = new ir_assignment(new ir_dereference(this->retval),
269 ir->get_value(), NULL);
270}
271
272
273void
274ir_function_cloning_visitor::visit(ir_if *ir)
275{
Eric Anholte8e97482010-04-22 17:52:59 -0700276 /* FINISHME: Implement if cloning. */
277 assert(0);
278
Eric Anholtcad97662010-04-07 11:46:26 -0700279 (void) ir;
280 result = NULL;
281}
282
283bool
Eric Anholt0d423212010-04-16 12:53:46 -0700284automatic_inlining_predicate(ir_instruction *ir)
285{
286 ir_call *call = ir->as_call();
287
288 if (call && can_inline(call))
289 return true;
290
291 return false;
292}
293
294bool
Eric Anholtcad97662010-04-07 11:46:26 -0700295do_function_inlining(exec_list *instructions)
296{
Eric Anholt2a7b2b22010-04-08 13:42:48 -0700297 bool progress = false;
Eric Anholtcad97662010-04-07 11:46:26 -0700298
Eric Anholt0d423212010-04-16 12:53:46 -0700299 do_expression_flattening(instructions, automatic_inlining_predicate);
300
Eric Anholtcad97662010-04-07 11:46:26 -0700301 foreach_iter(exec_list_iterator, iter, *instructions) {
302 ir_instruction *ir = (ir_instruction *)iter.get();
303 ir_assignment *assign = ir->as_assignment();
304 ir_call *call;
305
306 if (assign) {
307 call = assign->rhs->as_call();
308 if (!call || !can_inline(call))
309 continue;
310
311 /* generates the parameter setup, function body, and returns the return
312 * value of the function
313 */
314 ir_rvalue *rhs = call->generate_inline(ir);
315 assert(rhs);
316
317 assign->rhs = rhs;
318 progress = true;
319 } else if ((call = ir->as_call()) && can_inline(call)) {
320 (void)call->generate_inline(ir);
321 ir->remove();
322 progress = true;
323 } else {
324 ir_function_inlining_visitor v;
325 ir->accept(&v);
326 }
327 }
328
329 return progress;
330}
331
332ir_rvalue *
333ir_call::generate_inline(ir_instruction *next_ir)
334{
335 ir_variable **parameters;
336 int num_parameters;
337 int i;
338 ir_variable *retval = NULL;
339
340 num_parameters = 0;
341 foreach_iter(exec_list_iterator, iter_sig, this->callee->parameters)
342 num_parameters++;
343
344 parameters = new ir_variable *[num_parameters];
345
346 /* Generate storage for the return value. */
347 if (this->callee->return_type) {
348 retval = new ir_variable(this->callee->return_type, "__retval");
349 next_ir->insert_before(retval);
350 }
351
352 ir_function_cloning_visitor v = ir_function_cloning_visitor(retval);
353
354 /* Generate the declarations for the parameters to our inlined code,
355 * and set up the mapping of real function body variables to ours.
356 */
357 i = 0;
358 exec_list_iterator sig_param_iter = this->callee->parameters.iterator();
359 exec_list_iterator param_iter = this->actual_parameters.iterator();
360 for (i = 0; i < num_parameters; i++) {
361 const ir_variable *const sig_param = (ir_variable *) sig_param_iter.get();
362 ir_rvalue *param = (ir_rvalue *) param_iter.get();
363
364 /* Generate a new variable for the parameter. */
365 parameters[i] = sig_param->clone();
366 next_ir->insert_before(parameters[i]);
367
368 v.remap_variable(sig_param, parameters[i]);
369
370 /* Move the actual param into our param variable if it's an 'in' type. */
371 if (parameters[i]->mode == ir_var_in ||
372 parameters[i]->mode == ir_var_inout) {
373 ir_assignment *assign;
374
375 assign = new ir_assignment(new ir_dereference(parameters[i]),
376 param, NULL);
377 next_ir->insert_before(assign);
378 }
379
380 sig_param_iter.next();
381 param_iter.next();
382 }
383
384 /* Generate the inlined body of the function. */
385 foreach_iter(exec_list_iterator, iter, callee->body) {
386 ir_instruction *ir = (ir_instruction *)iter.get();
387
388 ir->accept(&v);
389 assert(v.result);
390 next_ir->insert_before(v.result);
391 }
392
393 /* Generate the declarations for the parameters to our inlined code,
394 * and set up the mapping of real function body variables to ours.
395 */
396 i = 0;
397 param_iter = this->actual_parameters.iterator();
398 for (i = 0; i < num_parameters; i++) {
399 ir_instruction *const param = (ir_instruction *) param_iter.get();
400
401 /* Move the actual param into our param variable if it's an 'in' type. */
402 if (parameters[i]->mode == ir_var_out ||
403 parameters[i]->mode == ir_var_inout) {
404 ir_assignment *assign;
405
406 assign = new ir_assignment(param->as_rvalue(),
407 new ir_dereference(parameters[i]),
408 NULL);
409 next_ir->insert_before(assign);
410 }
411
412 param_iter.next();
413 }
414
415 delete(parameters);
416
417 if (retval)
418 return new ir_dereference(retval);
419 else
420 return NULL;
421}
422
423void
424ir_function_inlining_visitor::visit(ir_variable *ir)
425{
426 (void) ir;
427}
428
429
430void
Eric Anholtcad97662010-04-07 11:46:26 -0700431ir_function_inlining_visitor::visit(ir_loop *ir)
432{
433 do_function_inlining(&ir->body_instructions);
434}
435
436void
437ir_function_inlining_visitor::visit(ir_loop_jump *ir)
438{
439 (void) ir;
440}
441
442
443void
444ir_function_inlining_visitor::visit(ir_function_signature *ir)
445{
446 do_function_inlining(&ir->body);
447}
448
449
450void
451ir_function_inlining_visitor::visit(ir_function *ir)
452{
Kenneth Graunke9fa99f32010-04-21 12:30:22 -0700453 foreach_iter(exec_list_iterator, iter, *ir) {
454 ir_function_signature *const sig = (ir_function_signature *) iter.get();
455 sig->accept(this);
456 }
Eric Anholtcad97662010-04-07 11:46:26 -0700457}
458
459void
460ir_function_inlining_visitor::visit(ir_expression *ir)
461{
462 unsigned int operand;
463
464 for (operand = 0; operand < ir->get_num_operands(); operand++) {
465 ir->operands[operand]->accept(this);
466 }
467}
468
469
470void
471ir_function_inlining_visitor::visit(ir_swizzle *ir)
472{
473 ir->val->accept(this);
474}
475
476
477void
478ir_function_inlining_visitor::visit(ir_dereference *ir)
479{
480 if (ir->mode == ir_dereference::ir_reference_array) {
481 ir->selector.array_index->accept(this);
482 }
483 ir->var->accept(this);
484}
485
486void
487ir_function_inlining_visitor::visit(ir_assignment *ir)
488{
489 ir->rhs->accept(this);
490}
491
492
493void
494ir_function_inlining_visitor::visit(ir_constant *ir)
495{
496 (void) ir;
497}
498
499
500void
501ir_function_inlining_visitor::visit(ir_call *ir)
502{
503 (void) ir;
504}
505
506
507void
508ir_function_inlining_visitor::visit(ir_return *ir)
509{
510 (void) ir;
511}
512
513
514void
515ir_function_inlining_visitor::visit(ir_if *ir)
516{
517 ir->condition->accept(this);
518
519 do_function_inlining(&ir->then_instructions);
520 do_function_inlining(&ir->else_instructions);
521}