blob: ba556a84992c482a7dd5eb8b63552d9b00044687 [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{
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{
Eric Anholt22147be2010-04-22 18:41:32 -0700225 ir_rvalue *lhs, *rhs, *condition = NULL;
Eric Anholtcad97662010-04-07 11:46:26 -0700226
227 ir->lhs->accept(this);
228 lhs = this->result->as_rvalue();
229
230 ir->rhs->accept(this);
231 rhs = this->result->as_rvalue();
232
Eric Anholt22147be2010-04-22 18:41:32 -0700233 if (ir->condition) {
234 ir->condition->accept(this);
235 condition = this->result->as_rvalue();
236 }
Eric Anholtcad97662010-04-07 11:46:26 -0700237
238 this->result = new ir_assignment(lhs, rhs, condition);
239}
240
241
242void
243ir_function_cloning_visitor::visit(ir_constant *ir)
244{
245 this->result = ir->clone();
246}
247
248
249void
250ir_function_cloning_visitor::visit(ir_call *ir)
251{
252 exec_list parameters;
253
254 foreach_iter(exec_list_iterator, iter, *ir) {
255 ir_rvalue *param = (ir_rvalue *)iter.get();
256
257 param->accept(this);
258 parameters.push_tail(this->result);
259 }
260
261 this->result = new ir_call(ir->get_callee(), &parameters);
262}
263
264
265void
266ir_function_cloning_visitor::visit(ir_return *ir)
267{
268 ir_rvalue *rval;
269
270 assert(this->retval);
271
272 rval = ir->get_value();
273 rval->accept(this);
274 rval = this->result->as_rvalue();
275 assert(rval);
276
277 result = new ir_assignment(new ir_dereference(this->retval),
278 ir->get_value(), NULL);
279}
280
281
282void
283ir_function_cloning_visitor::visit(ir_if *ir)
284{
Eric Anholte8e97482010-04-22 17:52:59 -0700285 /* FINISHME: Implement if cloning. */
286 assert(0);
287
Eric Anholtcad97662010-04-07 11:46:26 -0700288 (void) ir;
289 result = NULL;
290}
291
292bool
Eric Anholt0d423212010-04-16 12:53:46 -0700293automatic_inlining_predicate(ir_instruction *ir)
294{
295 ir_call *call = ir->as_call();
296
297 if (call && can_inline(call))
298 return true;
299
300 return false;
301}
302
303bool
Eric Anholtcad97662010-04-07 11:46:26 -0700304do_function_inlining(exec_list *instructions)
305{
Eric Anholt2a7b2b22010-04-08 13:42:48 -0700306 bool progress = false;
Eric Anholtcad97662010-04-07 11:46:26 -0700307
Eric Anholt0d423212010-04-16 12:53:46 -0700308 do_expression_flattening(instructions, automatic_inlining_predicate);
309
Eric Anholtcad97662010-04-07 11:46:26 -0700310 foreach_iter(exec_list_iterator, iter, *instructions) {
311 ir_instruction *ir = (ir_instruction *)iter.get();
312 ir_assignment *assign = ir->as_assignment();
313 ir_call *call;
314
315 if (assign) {
316 call = assign->rhs->as_call();
317 if (!call || !can_inline(call))
318 continue;
319
320 /* generates the parameter setup, function body, and returns the return
321 * value of the function
322 */
323 ir_rvalue *rhs = call->generate_inline(ir);
324 assert(rhs);
325
326 assign->rhs = rhs;
327 progress = true;
328 } else if ((call = ir->as_call()) && can_inline(call)) {
329 (void)call->generate_inline(ir);
330 ir->remove();
331 progress = true;
332 } else {
333 ir_function_inlining_visitor v;
334 ir->accept(&v);
335 }
336 }
337
338 return progress;
339}
340
341ir_rvalue *
342ir_call::generate_inline(ir_instruction *next_ir)
343{
344 ir_variable **parameters;
345 int num_parameters;
346 int i;
347 ir_variable *retval = NULL;
348
349 num_parameters = 0;
350 foreach_iter(exec_list_iterator, iter_sig, this->callee->parameters)
351 num_parameters++;
352
353 parameters = new ir_variable *[num_parameters];
354
355 /* Generate storage for the return value. */
356 if (this->callee->return_type) {
357 retval = new ir_variable(this->callee->return_type, "__retval");
358 next_ir->insert_before(retval);
359 }
360
361 ir_function_cloning_visitor v = ir_function_cloning_visitor(retval);
362
363 /* Generate the declarations for the parameters to our inlined code,
364 * and set up the mapping of real function body variables to ours.
365 */
366 i = 0;
367 exec_list_iterator sig_param_iter = this->callee->parameters.iterator();
368 exec_list_iterator param_iter = this->actual_parameters.iterator();
369 for (i = 0; i < num_parameters; i++) {
370 const ir_variable *const sig_param = (ir_variable *) sig_param_iter.get();
371 ir_rvalue *param = (ir_rvalue *) param_iter.get();
372
373 /* Generate a new variable for the parameter. */
374 parameters[i] = sig_param->clone();
375 next_ir->insert_before(parameters[i]);
376
377 v.remap_variable(sig_param, parameters[i]);
378
379 /* Move the actual param into our param variable if it's an 'in' type. */
380 if (parameters[i]->mode == ir_var_in ||
381 parameters[i]->mode == ir_var_inout) {
382 ir_assignment *assign;
383
384 assign = new ir_assignment(new ir_dereference(parameters[i]),
385 param, NULL);
386 next_ir->insert_before(assign);
387 }
388
389 sig_param_iter.next();
390 param_iter.next();
391 }
392
393 /* Generate the inlined body of the function. */
394 foreach_iter(exec_list_iterator, iter, callee->body) {
395 ir_instruction *ir = (ir_instruction *)iter.get();
396
397 ir->accept(&v);
398 assert(v.result);
399 next_ir->insert_before(v.result);
400 }
401
402 /* Generate the declarations for the parameters to our inlined code,
403 * and set up the mapping of real function body variables to ours.
404 */
405 i = 0;
406 param_iter = this->actual_parameters.iterator();
407 for (i = 0; i < num_parameters; i++) {
408 ir_instruction *const param = (ir_instruction *) param_iter.get();
409
410 /* Move the actual param into our param variable if it's an 'in' type. */
411 if (parameters[i]->mode == ir_var_out ||
412 parameters[i]->mode == ir_var_inout) {
413 ir_assignment *assign;
414
415 assign = new ir_assignment(param->as_rvalue(),
416 new ir_dereference(parameters[i]),
417 NULL);
418 next_ir->insert_before(assign);
419 }
420
421 param_iter.next();
422 }
423
424 delete(parameters);
425
426 if (retval)
427 return new ir_dereference(retval);
428 else
429 return NULL;
430}
431
432void
433ir_function_inlining_visitor::visit(ir_variable *ir)
434{
435 (void) ir;
436}
437
438
439void
Eric Anholtcad97662010-04-07 11:46:26 -0700440ir_function_inlining_visitor::visit(ir_loop *ir)
441{
442 do_function_inlining(&ir->body_instructions);
443}
444
445void
446ir_function_inlining_visitor::visit(ir_loop_jump *ir)
447{
448 (void) ir;
449}
450
451
452void
453ir_function_inlining_visitor::visit(ir_function_signature *ir)
454{
455 do_function_inlining(&ir->body);
456}
457
458
459void
460ir_function_inlining_visitor::visit(ir_function *ir)
461{
Kenneth Graunke9fa99f32010-04-21 12:30:22 -0700462 foreach_iter(exec_list_iterator, iter, *ir) {
463 ir_function_signature *const sig = (ir_function_signature *) iter.get();
464 sig->accept(this);
465 }
Eric Anholtcad97662010-04-07 11:46:26 -0700466}
467
468void
469ir_function_inlining_visitor::visit(ir_expression *ir)
470{
471 unsigned int operand;
472
473 for (operand = 0; operand < ir->get_num_operands(); operand++) {
474 ir->operands[operand]->accept(this);
475 }
476}
477
478
479void
480ir_function_inlining_visitor::visit(ir_swizzle *ir)
481{
482 ir->val->accept(this);
483}
484
485
486void
487ir_function_inlining_visitor::visit(ir_dereference *ir)
488{
489 if (ir->mode == ir_dereference::ir_reference_array) {
490 ir->selector.array_index->accept(this);
491 }
492 ir->var->accept(this);
493}
494
495void
496ir_function_inlining_visitor::visit(ir_assignment *ir)
497{
498 ir->rhs->accept(this);
499}
500
501
502void
503ir_function_inlining_visitor::visit(ir_constant *ir)
504{
505 (void) ir;
506}
507
508
509void
510ir_function_inlining_visitor::visit(ir_call *ir)
511{
512 (void) ir;
513}
514
515
516void
517ir_function_inlining_visitor::visit(ir_return *ir)
518{
519 (void) ir;
520}
521
522
523void
524ir_function_inlining_visitor::visit(ir_if *ir)
525{
526 ir->condition->accept(this);
527
528 do_function_inlining(&ir->then_instructions);
529 do_function_inlining(&ir->else_instructions);
530}