blob: 0d072c1b764c8cbe477a36e026da2c091e24fb2f [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);
Eric Anholt35e8e462010-04-26 15:02:40 -0700208 this->result = new ir_dereference(var, strdup(ir->selector.field));
Eric Anholtcad97662010-04-07 11:46:26 -0700209 }
210}
211
212void
213ir_function_cloning_visitor::visit(ir_assignment *ir)
214{
Eric Anholt22147be2010-04-22 18:41:32 -0700215 ir_rvalue *lhs, *rhs, *condition = NULL;
Eric Anholtcad97662010-04-07 11:46:26 -0700216
217 ir->lhs->accept(this);
218 lhs = this->result->as_rvalue();
219
220 ir->rhs->accept(this);
221 rhs = this->result->as_rvalue();
222
Eric Anholt22147be2010-04-22 18:41:32 -0700223 if (ir->condition) {
224 ir->condition->accept(this);
225 condition = this->result->as_rvalue();
226 }
Eric Anholtcad97662010-04-07 11:46:26 -0700227
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
Kenneth Graunke05ddeba2010-05-01 00:31:35 -0700267 result = new ir_assignment(new ir_dereference(this->retval), rval, NULL);
Eric Anholtcad97662010-04-07 11:46:26 -0700268}
269
270
271void
272ir_function_cloning_visitor::visit(ir_if *ir)
273{
Eric Anholte8e97482010-04-22 17:52:59 -0700274 /* FINISHME: Implement if cloning. */
275 assert(0);
276
Eric Anholtcad97662010-04-07 11:46:26 -0700277 (void) ir;
278 result = NULL;
279}
280
281bool
Eric Anholt0d423212010-04-16 12:53:46 -0700282automatic_inlining_predicate(ir_instruction *ir)
283{
284 ir_call *call = ir->as_call();
285
286 if (call && can_inline(call))
287 return true;
288
289 return false;
290}
291
292bool
Eric Anholtcad97662010-04-07 11:46:26 -0700293do_function_inlining(exec_list *instructions)
294{
Eric Anholt2a7b2b22010-04-08 13:42:48 -0700295 bool progress = false;
Eric Anholtcad97662010-04-07 11:46:26 -0700296
Eric Anholt0d423212010-04-16 12:53:46 -0700297 do_expression_flattening(instructions, automatic_inlining_predicate);
298
Eric Anholtcad97662010-04-07 11:46:26 -0700299 foreach_iter(exec_list_iterator, iter, *instructions) {
300 ir_instruction *ir = (ir_instruction *)iter.get();
301 ir_assignment *assign = ir->as_assignment();
302 ir_call *call;
303
304 if (assign) {
305 call = assign->rhs->as_call();
306 if (!call || !can_inline(call))
307 continue;
308
309 /* generates the parameter setup, function body, and returns the return
310 * value of the function
311 */
312 ir_rvalue *rhs = call->generate_inline(ir);
313 assert(rhs);
314
315 assign->rhs = rhs;
316 progress = true;
317 } else if ((call = ir->as_call()) && can_inline(call)) {
318 (void)call->generate_inline(ir);
319 ir->remove();
320 progress = true;
321 } else {
322 ir_function_inlining_visitor v;
323 ir->accept(&v);
324 }
325 }
326
327 return progress;
328}
329
330ir_rvalue *
331ir_call::generate_inline(ir_instruction *next_ir)
332{
333 ir_variable **parameters;
334 int num_parameters;
335 int i;
336 ir_variable *retval = NULL;
337
338 num_parameters = 0;
339 foreach_iter(exec_list_iterator, iter_sig, this->callee->parameters)
340 num_parameters++;
341
342 parameters = new ir_variable *[num_parameters];
343
344 /* Generate storage for the return value. */
345 if (this->callee->return_type) {
346 retval = new ir_variable(this->callee->return_type, "__retval");
347 next_ir->insert_before(retval);
348 }
349
350 ir_function_cloning_visitor v = ir_function_cloning_visitor(retval);
351
352 /* Generate the declarations for the parameters to our inlined code,
353 * and set up the mapping of real function body variables to ours.
354 */
355 i = 0;
356 exec_list_iterator sig_param_iter = this->callee->parameters.iterator();
357 exec_list_iterator param_iter = this->actual_parameters.iterator();
358 for (i = 0; i < num_parameters; i++) {
359 const ir_variable *const sig_param = (ir_variable *) sig_param_iter.get();
360 ir_rvalue *param = (ir_rvalue *) param_iter.get();
361
362 /* Generate a new variable for the parameter. */
363 parameters[i] = sig_param->clone();
364 next_ir->insert_before(parameters[i]);
365
366 v.remap_variable(sig_param, parameters[i]);
367
368 /* Move the actual param into our param variable if it's an 'in' type. */
369 if (parameters[i]->mode == ir_var_in ||
370 parameters[i]->mode == ir_var_inout) {
371 ir_assignment *assign;
372
373 assign = new ir_assignment(new ir_dereference(parameters[i]),
374 param, NULL);
375 next_ir->insert_before(assign);
376 }
377
378 sig_param_iter.next();
379 param_iter.next();
380 }
381
382 /* Generate the inlined body of the function. */
383 foreach_iter(exec_list_iterator, iter, callee->body) {
384 ir_instruction *ir = (ir_instruction *)iter.get();
385
386 ir->accept(&v);
387 assert(v.result);
388 next_ir->insert_before(v.result);
389 }
390
Kenneth Graunkec07fdae2010-04-30 23:38:50 -0700391 /* Copy back the value of any 'out' parameters from the function body
392 * variables to our own.
Eric Anholtcad97662010-04-07 11:46:26 -0700393 */
394 i = 0;
395 param_iter = this->actual_parameters.iterator();
396 for (i = 0; i < num_parameters; i++) {
397 ir_instruction *const param = (ir_instruction *) param_iter.get();
398
Kenneth Graunkec07fdae2010-04-30 23:38:50 -0700399 /* Move our param variable into the actual param if it's an 'out' type. */
Eric Anholtcad97662010-04-07 11:46:26 -0700400 if (parameters[i]->mode == ir_var_out ||
401 parameters[i]->mode == ir_var_inout) {
402 ir_assignment *assign;
403
404 assign = new ir_assignment(param->as_rvalue(),
405 new ir_dereference(parameters[i]),
406 NULL);
407 next_ir->insert_before(assign);
408 }
409
410 param_iter.next();
411 }
412
413 delete(parameters);
414
415 if (retval)
416 return new ir_dereference(retval);
417 else
418 return NULL;
419}
420
421void
422ir_function_inlining_visitor::visit(ir_variable *ir)
423{
424 (void) ir;
425}
426
427
428void
Eric Anholtcad97662010-04-07 11:46:26 -0700429ir_function_inlining_visitor::visit(ir_loop *ir)
430{
431 do_function_inlining(&ir->body_instructions);
432}
433
434void
435ir_function_inlining_visitor::visit(ir_loop_jump *ir)
436{
437 (void) ir;
438}
439
440
441void
442ir_function_inlining_visitor::visit(ir_function_signature *ir)
443{
444 do_function_inlining(&ir->body);
445}
446
447
448void
449ir_function_inlining_visitor::visit(ir_function *ir)
450{
Kenneth Graunke9fa99f32010-04-21 12:30:22 -0700451 foreach_iter(exec_list_iterator, iter, *ir) {
452 ir_function_signature *const sig = (ir_function_signature *) iter.get();
453 sig->accept(this);
454 }
Eric Anholtcad97662010-04-07 11:46:26 -0700455}
456
457void
458ir_function_inlining_visitor::visit(ir_expression *ir)
459{
460 unsigned int operand;
461
462 for (operand = 0; operand < ir->get_num_operands(); operand++) {
463 ir->operands[operand]->accept(this);
464 }
465}
466
467
468void
469ir_function_inlining_visitor::visit(ir_swizzle *ir)
470{
471 ir->val->accept(this);
472}
473
474
475void
476ir_function_inlining_visitor::visit(ir_dereference *ir)
477{
478 if (ir->mode == ir_dereference::ir_reference_array) {
479 ir->selector.array_index->accept(this);
480 }
481 ir->var->accept(this);
482}
483
484void
485ir_function_inlining_visitor::visit(ir_assignment *ir)
486{
487 ir->rhs->accept(this);
488}
489
490
491void
492ir_function_inlining_visitor::visit(ir_constant *ir)
493{
494 (void) ir;
495}
496
497
498void
499ir_function_inlining_visitor::visit(ir_call *ir)
500{
501 (void) ir;
502}
503
504
505void
506ir_function_inlining_visitor::visit(ir_return *ir)
507{
508 (void) ir;
509}
510
511
512void
513ir_function_inlining_visitor::visit(ir_if *ir)
514{
515 ir->condition->accept(this);
516
517 do_function_inlining(&ir->then_instructions);
518 do_function_inlining(&ir->else_instructions);
519}