blob: d66eceedb86c1ce7e3387afac1fd9c7a92dd83a5 [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
Ian Romanicke668c2a2010-05-26 18:58:27 -070037class ir_function_inlining_visitor : public ir_hierarchical_visitor {
Eric Anholtbdd9b1f2010-05-05 11:45:30 -070038public:
39 ir_function_inlining_visitor()
40 {
Ian Romanicke668c2a2010-05-26 18:58:27 -070041 progress = false;
Eric Anholtbdd9b1f2010-05-05 11:45:30 -070042 }
43
44 virtual ~ir_function_inlining_visitor()
45 {
46 /* empty */
47 }
48
Ian Romanicke668c2a2010-05-26 18:58:27 -070049 virtual ir_visitor_status visit_enter(ir_expression *);
50 virtual ir_visitor_status visit_enter(ir_call *);
51 virtual ir_visitor_status visit_enter(ir_assignment *);
52 virtual ir_visitor_status visit_enter(ir_return *);
Kenneth Graunke26d74cd2010-05-26 17:42:03 -070053 virtual ir_visitor_status visit_enter(ir_texture *);
Ian Romanicke668c2a2010-05-26 18:58:27 -070054 virtual ir_visitor_status visit_enter(ir_swizzle *);
55
56 bool progress;
Eric Anholtbdd9b1f2010-05-05 11:45:30 -070057};
58
Eric Anholtcad97662010-04-07 11:46:26 -070059class variable_remap : public exec_node {
60public:
61 variable_remap(const ir_variable *old_var, ir_variable *new_var)
62 : old_var(old_var), new_var(new_var)
63 {
64 /* empty */
65 }
66 const ir_variable *old_var;
67 ir_variable *new_var;
68};
69
70class ir_function_cloning_visitor : public ir_visitor {
71public:
72 ir_function_cloning_visitor(ir_variable *retval)
73 : retval(retval)
74 {
75 /* empty */
76 }
77
78 virtual ~ir_function_cloning_visitor()
79 {
80 /* empty */
81 }
82
83 void remap_variable(const ir_variable *old_var, ir_variable *new_var) {
84 variable_remap *remap = new variable_remap(old_var, new_var);
85 this->remap_list.push_tail(remap);
86 }
87
88 ir_variable *get_remapped_variable(ir_variable *var) {
89 foreach_iter(exec_list_iterator, iter, this->remap_list) {
90 variable_remap *remap = (variable_remap *)iter.get();
91
92 if (var == remap->old_var)
93 return remap->new_var;
94 }
95
96 /* Not a reapped variable, so a global scoped reference, for example. */
97 return var;
98 }
99
100 /* List of variable_remap for mapping from original function body variables
101 * to inlined function body variables.
102 */
103 exec_list remap_list;
104
105 /* Return value for the inlined function. */
106 ir_variable *retval;
107
108 /**
109 * \name Visit methods
110 *
111 * As typical for the visitor pattern, there must be one \c visit method for
112 * each concrete subclass of \c ir_instruction. Virtual base classes within
113 * the hierarchy should not have \c visit methods.
114 */
115 /*@{*/
116 virtual void visit(ir_variable *);
Eric Anholtcad97662010-04-07 11:46:26 -0700117 virtual void visit(ir_loop *);
118 virtual void visit(ir_loop_jump *);
119 virtual void visit(ir_function_signature *);
120 virtual void visit(ir_function *);
121 virtual void visit(ir_expression *);
Kenneth Graunke26d74cd2010-05-26 17:42:03 -0700122 virtual void visit(ir_texture *);
Eric Anholtcad97662010-04-07 11:46:26 -0700123 virtual void visit(ir_swizzle *);
Ian Romanickc7b10462010-05-19 13:20:12 +0200124 virtual void visit(ir_dereference_variable *);
125 virtual void visit(ir_dereference_array *);
126 virtual void visit(ir_dereference_record *);
Eric Anholtcad97662010-04-07 11:46:26 -0700127 virtual void visit(ir_assignment *);
128 virtual void visit(ir_constant *);
129 virtual void visit(ir_call *);
130 virtual void visit(ir_return *);
131 virtual void visit(ir_if *);
132 /*@}*/
133
134 ir_instruction *result;
135};
136
137void
138ir_function_cloning_visitor::visit(ir_variable *ir)
139{
140 ir_variable *new_var = ir->clone();
141
142 this->result = new_var;
143
144 this->remap_variable(ir, new_var);
145}
146
147void
Eric Anholtcad97662010-04-07 11:46:26 -0700148ir_function_cloning_visitor::visit(ir_loop *ir)
149{
Eric Anholte8e97482010-04-22 17:52:59 -0700150 /* FINISHME: Implement loop cloning. */
151 assert(0);
152
Eric Anholtcad97662010-04-07 11:46:26 -0700153 (void)ir;
154 this->result = NULL;
155}
156
157void
158ir_function_cloning_visitor::visit(ir_loop_jump *ir)
159{
Eric Anholte8e97482010-04-22 17:52:59 -0700160 /* FINISHME: Implement loop cloning. */
161 assert(0);
162
Eric Anholtcad97662010-04-07 11:46:26 -0700163 (void) ir;
164 this->result = NULL;
165}
166
167
168void
169ir_function_cloning_visitor::visit(ir_function_signature *ir)
170{
Eric Anholte8e97482010-04-22 17:52:59 -0700171 assert(0);
Eric Anholtcad97662010-04-07 11:46:26 -0700172 (void)ir;
173 this->result = NULL;
174}
175
176
177void
178ir_function_cloning_visitor::visit(ir_function *ir)
179{
Eric Anholte8e97482010-04-22 17:52:59 -0700180 assert(0);
Eric Anholtcad97662010-04-07 11:46:26 -0700181 (void) ir;
182 this->result = NULL;
183}
184
185void
186ir_function_cloning_visitor::visit(ir_expression *ir)
187{
188 unsigned int operand;
189 ir_rvalue *op[2] = {NULL, NULL};
190
191 for (operand = 0; operand < ir->get_num_operands(); operand++) {
192 ir->operands[operand]->accept(this);
193 op[operand] = this->result->as_rvalue();
194 assert(op[operand]);
195 }
196
197 this->result = new ir_expression(ir->operation, ir->type, op[0], op[1]);
198}
199
200
201void
Kenneth Graunke26d74cd2010-05-26 17:42:03 -0700202ir_function_cloning_visitor::visit(ir_texture *ir)
203{
204 // FINISHME: Do stuff with texture lookups
205 (void) ir;
206}
207
208
209void
Eric Anholtcad97662010-04-07 11:46:26 -0700210ir_function_cloning_visitor::visit(ir_swizzle *ir)
211{
212 ir->val->accept(this);
213
214 this->result = new ir_swizzle(this->result->as_rvalue(), ir->mask);
215}
216
217void
Ian Romanickc7b10462010-05-19 13:20:12 +0200218ir_function_cloning_visitor::visit(ir_dereference_variable *ir)
Eric Anholtcad97662010-04-07 11:46:26 -0700219{
Ian Romanickc7b10462010-05-19 13:20:12 +0200220 ir_variable *var = this->get_remapped_variable(ir->variable_referenced());
221 this->result = new ir_dereference_variable(var);
222}
Ian Romanick70fe8b62010-05-19 11:37:35 +0200223
Ian Romanickc7b10462010-05-19 13:20:12 +0200224void
225ir_function_cloning_visitor::visit(ir_dereference_array *ir)
226{
Ian Romanick36ea2862010-05-19 13:52:29 +0200227 ir->array->accept(this);
Eric Anholt61924342010-04-08 13:40:52 -0700228
Ian Romanickc7b10462010-05-19 13:20:12 +0200229 ir_rvalue *var = this->result->as_rvalue();
Eric Anholt61924342010-04-08 13:40:52 -0700230
Ian Romanick36ea2862010-05-19 13:52:29 +0200231 ir->array_index->accept(this);
Ian Romanick70fe8b62010-05-19 11:37:35 +0200232
Ian Romanickc7b10462010-05-19 13:20:12 +0200233 ir_rvalue *index = this->result->as_rvalue();
Ian Romanick70fe8b62010-05-19 11:37:35 +0200234
Ian Romanickc7b10462010-05-19 13:20:12 +0200235 this->result = new ir_dereference_array(var, index);
236}
Ian Romanick70fe8b62010-05-19 11:37:35 +0200237
Ian Romanickc7b10462010-05-19 13:20:12 +0200238void
239ir_function_cloning_visitor::visit(ir_dereference_record *ir)
240{
Ian Romanick36ea2862010-05-19 13:52:29 +0200241 ir->record->accept(this);
Ian Romanick70fe8b62010-05-19 11:37:35 +0200242
Ian Romanickc7b10462010-05-19 13:20:12 +0200243 ir_rvalue *var = this->result->as_rvalue();
244
Ian Romanick36ea2862010-05-19 13:52:29 +0200245 this->result = new ir_dereference_record(var, strdup(ir->field));
Eric Anholtcad97662010-04-07 11:46:26 -0700246}
247
248void
249ir_function_cloning_visitor::visit(ir_assignment *ir)
250{
Eric Anholt22147be2010-04-22 18:41:32 -0700251 ir_rvalue *lhs, *rhs, *condition = NULL;
Eric Anholtcad97662010-04-07 11:46:26 -0700252
253 ir->lhs->accept(this);
254 lhs = this->result->as_rvalue();
255
256 ir->rhs->accept(this);
257 rhs = this->result->as_rvalue();
258
Eric Anholt22147be2010-04-22 18:41:32 -0700259 if (ir->condition) {
260 ir->condition->accept(this);
261 condition = this->result->as_rvalue();
262 }
Eric Anholtcad97662010-04-07 11:46:26 -0700263
264 this->result = new ir_assignment(lhs, rhs, condition);
265}
266
267
268void
269ir_function_cloning_visitor::visit(ir_constant *ir)
270{
271 this->result = ir->clone();
272}
273
274
275void
276ir_function_cloning_visitor::visit(ir_call *ir)
277{
278 exec_list parameters;
279
280 foreach_iter(exec_list_iterator, iter, *ir) {
281 ir_rvalue *param = (ir_rvalue *)iter.get();
282
283 param->accept(this);
284 parameters.push_tail(this->result);
285 }
286
287 this->result = new ir_call(ir->get_callee(), &parameters);
288}
289
290
291void
292ir_function_cloning_visitor::visit(ir_return *ir)
293{
294 ir_rvalue *rval;
295
296 assert(this->retval);
297
298 rval = ir->get_value();
299 rval->accept(this);
300 rval = this->result->as_rvalue();
301 assert(rval);
302
Ian Romanick70fe8b62010-05-19 11:37:35 +0200303 result = new ir_assignment(new ir_dereference_variable(this->retval), rval,
304 NULL);
Eric Anholtcad97662010-04-07 11:46:26 -0700305}
306
307
308void
309ir_function_cloning_visitor::visit(ir_if *ir)
310{
Eric Anholte8e97482010-04-22 17:52:59 -0700311 /* FINISHME: Implement if cloning. */
312 assert(0);
313
Eric Anholtcad97662010-04-07 11:46:26 -0700314 (void) ir;
315 result = NULL;
316}
317
318bool
Eric Anholt0d423212010-04-16 12:53:46 -0700319automatic_inlining_predicate(ir_instruction *ir)
320{
321 ir_call *call = ir->as_call();
322
323 if (call && can_inline(call))
324 return true;
325
326 return false;
327}
328
329bool
Eric Anholtcad97662010-04-07 11:46:26 -0700330do_function_inlining(exec_list *instructions)
331{
Ian Romanicke668c2a2010-05-26 18:58:27 -0700332 ir_function_inlining_visitor v;
Eric Anholtcad97662010-04-07 11:46:26 -0700333
Eric Anholt0d423212010-04-16 12:53:46 -0700334 do_expression_flattening(instructions, automatic_inlining_predicate);
335
Ian Romanicke668c2a2010-05-26 18:58:27 -0700336 v.run(instructions);
Eric Anholtcad97662010-04-07 11:46:26 -0700337
Ian Romanicke668c2a2010-05-26 18:58:27 -0700338 return v.progress;
Eric Anholtcad97662010-04-07 11:46:26 -0700339}
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
Ian Romanick70fe8b62010-05-19 11:37:35 +0200384 assign = new ir_assignment(new ir_dereference_variable(parameters[i]),
Eric Anholtcad97662010-04-07 11:46:26 -0700385 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
Kenneth Graunkec07fdae2010-04-30 23:38:50 -0700402 /* Copy back the value of any 'out' parameters from the function body
403 * variables to our own.
Eric Anholtcad97662010-04-07 11:46:26 -0700404 */
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
Kenneth Graunkec07fdae2010-04-30 23:38:50 -0700410 /* Move our param variable into the actual param if it's an 'out' type. */
Eric Anholtcad97662010-04-07 11:46:26 -0700411 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(),
Ian Romanick70fe8b62010-05-19 11:37:35 +0200416 new ir_dereference_variable(parameters[i]),
Eric Anholtcad97662010-04-07 11:46:26 -0700417 NULL);
418 next_ir->insert_before(assign);
419 }
420
421 param_iter.next();
422 }
423
Ian Romanick2f8b0432010-06-09 11:00:00 -0700424 delete [] parameters;
Eric Anholtcad97662010-04-07 11:46:26 -0700425
426 if (retval)
Ian Romanick70fe8b62010-05-19 11:37:35 +0200427 return new ir_dereference_variable(retval);
Eric Anholtcad97662010-04-07 11:46:26 -0700428 else
429 return NULL;
430}
431
Ian Romanicke668c2a2010-05-26 18:58:27 -0700432
433ir_visitor_status
434ir_function_inlining_visitor::visit_enter(ir_expression *ir)
Eric Anholtcad97662010-04-07 11:46:26 -0700435{
436 (void) ir;
Ian Romanicke668c2a2010-05-26 18:58:27 -0700437 return visit_continue_with_parent;
Eric Anholtcad97662010-04-07 11:46:26 -0700438}
439
440
Ian Romanicke668c2a2010-05-26 18:58:27 -0700441ir_visitor_status
442ir_function_inlining_visitor::visit_enter(ir_return *ir)
Eric Anholtcad97662010-04-07 11:46:26 -0700443{
444 (void) ir;
Ian Romanicke668c2a2010-05-26 18:58:27 -0700445 return visit_continue_with_parent;
Eric Anholtcad97662010-04-07 11:46:26 -0700446}
447
448
Ian Romanicke668c2a2010-05-26 18:58:27 -0700449ir_visitor_status
Kenneth Graunke26d74cd2010-05-26 17:42:03 -0700450ir_function_inlining_visitor::visit_enter(ir_texture *ir)
451{
452 (void) ir;
453 return visit_continue_with_parent;
454}
455
456
457ir_visitor_status
Ian Romanicke668c2a2010-05-26 18:58:27 -0700458ir_function_inlining_visitor::visit_enter(ir_swizzle *ir)
Eric Anholtcad97662010-04-07 11:46:26 -0700459{
Ian Romanicke668c2a2010-05-26 18:58:27 -0700460 (void) ir;
461 return visit_continue_with_parent;
Eric Anholtcad97662010-04-07 11:46:26 -0700462}
463
464
Ian Romanicke668c2a2010-05-26 18:58:27 -0700465ir_visitor_status
466ir_function_inlining_visitor::visit_enter(ir_call *ir)
Eric Anholtcad97662010-04-07 11:46:26 -0700467{
Ian Romanicke668c2a2010-05-26 18:58:27 -0700468 if (can_inline(ir)) {
469 (void) ir->generate_inline(ir);
470 ir->remove();
471 this->progress = true;
Kenneth Graunke9fa99f32010-04-21 12:30:22 -0700472 }
Eric Anholtcad97662010-04-07 11:46:26 -0700473
Ian Romanicke668c2a2010-05-26 18:58:27 -0700474 return visit_continue;
Eric Anholtcad97662010-04-07 11:46:26 -0700475}
476
477
Ian Romanicke668c2a2010-05-26 18:58:27 -0700478ir_visitor_status
479ir_function_inlining_visitor::visit_enter(ir_assignment *ir)
Eric Anholtcad97662010-04-07 11:46:26 -0700480{
Ian Romanicke668c2a2010-05-26 18:58:27 -0700481 ir_call *call = ir->rhs->as_call();
482 if (!call || !can_inline(call))
483 return visit_continue;
Eric Anholtcad97662010-04-07 11:46:26 -0700484
Ian Romanicke668c2a2010-05-26 18:58:27 -0700485 /* generates the parameter setup, function body, and returns the return
486 * value of the function
487 */
488 ir_rvalue *rhs = call->generate_inline(ir);
489 assert(rhs);
Eric Anholtcad97662010-04-07 11:46:26 -0700490
Ian Romanicke668c2a2010-05-26 18:58:27 -0700491 ir->rhs = rhs;
492 this->progress = true;
Ian Romanickc7b10462010-05-19 13:20:12 +0200493
Ian Romanicke668c2a2010-05-26 18:58:27 -0700494 return visit_continue;
Eric Anholtcad97662010-04-07 11:46:26 -0700495}