blob: 7ac74ca03b6235775162b76d1531a8169f10dd56 [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 *);
53 virtual ir_visitor_status visit_enter(ir_swizzle *);
54
55 bool progress;
Eric Anholtbdd9b1f2010-05-05 11:45:30 -070056};
57
Eric Anholtcad97662010-04-07 11:46:26 -070058class variable_remap : public exec_node {
59public:
60 variable_remap(const ir_variable *old_var, ir_variable *new_var)
61 : old_var(old_var), new_var(new_var)
62 {
63 /* empty */
64 }
65 const ir_variable *old_var;
66 ir_variable *new_var;
67};
68
69class ir_function_cloning_visitor : public ir_visitor {
70public:
71 ir_function_cloning_visitor(ir_variable *retval)
72 : retval(retval)
73 {
74 /* empty */
75 }
76
77 virtual ~ir_function_cloning_visitor()
78 {
79 /* empty */
80 }
81
82 void remap_variable(const ir_variable *old_var, ir_variable *new_var) {
83 variable_remap *remap = new variable_remap(old_var, new_var);
84 this->remap_list.push_tail(remap);
85 }
86
87 ir_variable *get_remapped_variable(ir_variable *var) {
88 foreach_iter(exec_list_iterator, iter, this->remap_list) {
89 variable_remap *remap = (variable_remap *)iter.get();
90
91 if (var == remap->old_var)
92 return remap->new_var;
93 }
94
95 /* Not a reapped variable, so a global scoped reference, for example. */
96 return var;
97 }
98
99 /* List of variable_remap for mapping from original function body variables
100 * to inlined function body variables.
101 */
102 exec_list remap_list;
103
104 /* Return value for the inlined function. */
105 ir_variable *retval;
106
107 /**
108 * \name Visit methods
109 *
110 * As typical for the visitor pattern, there must be one \c visit method for
111 * each concrete subclass of \c ir_instruction. Virtual base classes within
112 * the hierarchy should not have \c visit methods.
113 */
114 /*@{*/
115 virtual void visit(ir_variable *);
Eric Anholtcad97662010-04-07 11:46:26 -0700116 virtual void visit(ir_loop *);
117 virtual void visit(ir_loop_jump *);
118 virtual void visit(ir_function_signature *);
119 virtual void visit(ir_function *);
120 virtual void visit(ir_expression *);
121 virtual void visit(ir_swizzle *);
Ian Romanickc7b10462010-05-19 13:20:12 +0200122 virtual void visit(ir_dereference_variable *);
123 virtual void visit(ir_dereference_array *);
124 virtual void visit(ir_dereference_record *);
Eric Anholtcad97662010-04-07 11:46:26 -0700125 virtual void visit(ir_assignment *);
126 virtual void visit(ir_constant *);
127 virtual void visit(ir_call *);
128 virtual void visit(ir_return *);
129 virtual void visit(ir_if *);
130 /*@}*/
131
132 ir_instruction *result;
133};
134
135void
136ir_function_cloning_visitor::visit(ir_variable *ir)
137{
138 ir_variable *new_var = ir->clone();
139
140 this->result = new_var;
141
142 this->remap_variable(ir, new_var);
143}
144
145void
Eric Anholtcad97662010-04-07 11:46:26 -0700146ir_function_cloning_visitor::visit(ir_loop *ir)
147{
Eric Anholte8e97482010-04-22 17:52:59 -0700148 /* FINISHME: Implement loop cloning. */
149 assert(0);
150
Eric Anholtcad97662010-04-07 11:46:26 -0700151 (void)ir;
152 this->result = NULL;
153}
154
155void
156ir_function_cloning_visitor::visit(ir_loop_jump *ir)
157{
Eric Anholte8e97482010-04-22 17:52:59 -0700158 /* FINISHME: Implement loop cloning. */
159 assert(0);
160
Eric Anholtcad97662010-04-07 11:46:26 -0700161 (void) ir;
162 this->result = NULL;
163}
164
165
166void
167ir_function_cloning_visitor::visit(ir_function_signature *ir)
168{
Eric Anholte8e97482010-04-22 17:52:59 -0700169 assert(0);
Eric Anholtcad97662010-04-07 11:46:26 -0700170 (void)ir;
171 this->result = NULL;
172}
173
174
175void
176ir_function_cloning_visitor::visit(ir_function *ir)
177{
Eric Anholte8e97482010-04-22 17:52:59 -0700178 assert(0);
Eric Anholtcad97662010-04-07 11:46:26 -0700179 (void) ir;
180 this->result = NULL;
181}
182
183void
184ir_function_cloning_visitor::visit(ir_expression *ir)
185{
186 unsigned int operand;
187 ir_rvalue *op[2] = {NULL, NULL};
188
189 for (operand = 0; operand < ir->get_num_operands(); operand++) {
190 ir->operands[operand]->accept(this);
191 op[operand] = this->result->as_rvalue();
192 assert(op[operand]);
193 }
194
195 this->result = new ir_expression(ir->operation, ir->type, op[0], op[1]);
196}
197
198
199void
200ir_function_cloning_visitor::visit(ir_swizzle *ir)
201{
202 ir->val->accept(this);
203
204 this->result = new ir_swizzle(this->result->as_rvalue(), ir->mask);
205}
206
207void
Ian Romanickc7b10462010-05-19 13:20:12 +0200208ir_function_cloning_visitor::visit(ir_dereference_variable *ir)
Eric Anholtcad97662010-04-07 11:46:26 -0700209{
Ian Romanickc7b10462010-05-19 13:20:12 +0200210 ir_variable *var = this->get_remapped_variable(ir->variable_referenced());
211 this->result = new ir_dereference_variable(var);
212}
Ian Romanick70fe8b62010-05-19 11:37:35 +0200213
Ian Romanickc7b10462010-05-19 13:20:12 +0200214void
215ir_function_cloning_visitor::visit(ir_dereference_array *ir)
216{
Ian Romanick36ea2862010-05-19 13:52:29 +0200217 ir->array->accept(this);
Eric Anholt61924342010-04-08 13:40:52 -0700218
Ian Romanickc7b10462010-05-19 13:20:12 +0200219 ir_rvalue *var = this->result->as_rvalue();
Eric Anholt61924342010-04-08 13:40:52 -0700220
Ian Romanick36ea2862010-05-19 13:52:29 +0200221 ir->array_index->accept(this);
Ian Romanick70fe8b62010-05-19 11:37:35 +0200222
Ian Romanickc7b10462010-05-19 13:20:12 +0200223 ir_rvalue *index = this->result->as_rvalue();
Ian Romanick70fe8b62010-05-19 11:37:35 +0200224
Ian Romanickc7b10462010-05-19 13:20:12 +0200225 this->result = new ir_dereference_array(var, index);
226}
Ian Romanick70fe8b62010-05-19 11:37:35 +0200227
Ian Romanickc7b10462010-05-19 13:20:12 +0200228void
229ir_function_cloning_visitor::visit(ir_dereference_record *ir)
230{
Ian Romanick36ea2862010-05-19 13:52:29 +0200231 ir->record->accept(this);
Ian Romanick70fe8b62010-05-19 11:37:35 +0200232
Ian Romanickc7b10462010-05-19 13:20:12 +0200233 ir_rvalue *var = this->result->as_rvalue();
234
Ian Romanick36ea2862010-05-19 13:52:29 +0200235 this->result = new ir_dereference_record(var, strdup(ir->field));
Eric Anholtcad97662010-04-07 11:46:26 -0700236}
237
238void
239ir_function_cloning_visitor::visit(ir_assignment *ir)
240{
Eric Anholt22147be2010-04-22 18:41:32 -0700241 ir_rvalue *lhs, *rhs, *condition = NULL;
Eric Anholtcad97662010-04-07 11:46:26 -0700242
243 ir->lhs->accept(this);
244 lhs = this->result->as_rvalue();
245
246 ir->rhs->accept(this);
247 rhs = this->result->as_rvalue();
248
Eric Anholt22147be2010-04-22 18:41:32 -0700249 if (ir->condition) {
250 ir->condition->accept(this);
251 condition = this->result->as_rvalue();
252 }
Eric Anholtcad97662010-04-07 11:46:26 -0700253
254 this->result = new ir_assignment(lhs, rhs, condition);
255}
256
257
258void
259ir_function_cloning_visitor::visit(ir_constant *ir)
260{
261 this->result = ir->clone();
262}
263
264
265void
266ir_function_cloning_visitor::visit(ir_call *ir)
267{
268 exec_list parameters;
269
270 foreach_iter(exec_list_iterator, iter, *ir) {
271 ir_rvalue *param = (ir_rvalue *)iter.get();
272
273 param->accept(this);
274 parameters.push_tail(this->result);
275 }
276
277 this->result = new ir_call(ir->get_callee(), &parameters);
278}
279
280
281void
282ir_function_cloning_visitor::visit(ir_return *ir)
283{
284 ir_rvalue *rval;
285
286 assert(this->retval);
287
288 rval = ir->get_value();
289 rval->accept(this);
290 rval = this->result->as_rvalue();
291 assert(rval);
292
Ian Romanick70fe8b62010-05-19 11:37:35 +0200293 result = new ir_assignment(new ir_dereference_variable(this->retval), rval,
294 NULL);
Eric Anholtcad97662010-04-07 11:46:26 -0700295}
296
297
298void
299ir_function_cloning_visitor::visit(ir_if *ir)
300{
Eric Anholte8e97482010-04-22 17:52:59 -0700301 /* FINISHME: Implement if cloning. */
302 assert(0);
303
Eric Anholtcad97662010-04-07 11:46:26 -0700304 (void) ir;
305 result = NULL;
306}
307
308bool
Eric Anholt0d423212010-04-16 12:53:46 -0700309automatic_inlining_predicate(ir_instruction *ir)
310{
311 ir_call *call = ir->as_call();
312
313 if (call && can_inline(call))
314 return true;
315
316 return false;
317}
318
319bool
Eric Anholtcad97662010-04-07 11:46:26 -0700320do_function_inlining(exec_list *instructions)
321{
Ian Romanicke668c2a2010-05-26 18:58:27 -0700322 ir_function_inlining_visitor v;
Eric Anholtcad97662010-04-07 11:46:26 -0700323
Eric Anholt0d423212010-04-16 12:53:46 -0700324 do_expression_flattening(instructions, automatic_inlining_predicate);
325
Ian Romanicke668c2a2010-05-26 18:58:27 -0700326 v.run(instructions);
Eric Anholtcad97662010-04-07 11:46:26 -0700327
Ian Romanicke668c2a2010-05-26 18:58:27 -0700328 return v.progress;
Eric Anholtcad97662010-04-07 11:46:26 -0700329}
330
331ir_rvalue *
332ir_call::generate_inline(ir_instruction *next_ir)
333{
334 ir_variable **parameters;
335 int num_parameters;
336 int i;
337 ir_variable *retval = NULL;
338
339 num_parameters = 0;
340 foreach_iter(exec_list_iterator, iter_sig, this->callee->parameters)
341 num_parameters++;
342
343 parameters = new ir_variable *[num_parameters];
344
345 /* Generate storage for the return value. */
346 if (this->callee->return_type) {
347 retval = new ir_variable(this->callee->return_type, "__retval");
348 next_ir->insert_before(retval);
349 }
350
351 ir_function_cloning_visitor v = ir_function_cloning_visitor(retval);
352
353 /* Generate the declarations for the parameters to our inlined code,
354 * and set up the mapping of real function body variables to ours.
355 */
356 i = 0;
357 exec_list_iterator sig_param_iter = this->callee->parameters.iterator();
358 exec_list_iterator param_iter = this->actual_parameters.iterator();
359 for (i = 0; i < num_parameters; i++) {
360 const ir_variable *const sig_param = (ir_variable *) sig_param_iter.get();
361 ir_rvalue *param = (ir_rvalue *) param_iter.get();
362
363 /* Generate a new variable for the parameter. */
364 parameters[i] = sig_param->clone();
365 next_ir->insert_before(parameters[i]);
366
367 v.remap_variable(sig_param, parameters[i]);
368
369 /* Move the actual param into our param variable if it's an 'in' type. */
370 if (parameters[i]->mode == ir_var_in ||
371 parameters[i]->mode == ir_var_inout) {
372 ir_assignment *assign;
373
Ian Romanick70fe8b62010-05-19 11:37:35 +0200374 assign = new ir_assignment(new ir_dereference_variable(parameters[i]),
Eric Anholtcad97662010-04-07 11:46:26 -0700375 param, NULL);
376 next_ir->insert_before(assign);
377 }
378
379 sig_param_iter.next();
380 param_iter.next();
381 }
382
383 /* Generate the inlined body of the function. */
384 foreach_iter(exec_list_iterator, iter, callee->body) {
385 ir_instruction *ir = (ir_instruction *)iter.get();
386
387 ir->accept(&v);
388 assert(v.result);
389 next_ir->insert_before(v.result);
390 }
391
Kenneth Graunkec07fdae2010-04-30 23:38:50 -0700392 /* Copy back the value of any 'out' parameters from the function body
393 * variables to our own.
Eric Anholtcad97662010-04-07 11:46:26 -0700394 */
395 i = 0;
396 param_iter = this->actual_parameters.iterator();
397 for (i = 0; i < num_parameters; i++) {
398 ir_instruction *const param = (ir_instruction *) param_iter.get();
399
Kenneth Graunkec07fdae2010-04-30 23:38:50 -0700400 /* Move our param variable into the actual param if it's an 'out' type. */
Eric Anholtcad97662010-04-07 11:46:26 -0700401 if (parameters[i]->mode == ir_var_out ||
402 parameters[i]->mode == ir_var_inout) {
403 ir_assignment *assign;
404
405 assign = new ir_assignment(param->as_rvalue(),
Ian Romanick70fe8b62010-05-19 11:37:35 +0200406 new ir_dereference_variable(parameters[i]),
Eric Anholtcad97662010-04-07 11:46:26 -0700407 NULL);
408 next_ir->insert_before(assign);
409 }
410
411 param_iter.next();
412 }
413
414 delete(parameters);
415
416 if (retval)
Ian Romanick70fe8b62010-05-19 11:37:35 +0200417 return new ir_dereference_variable(retval);
Eric Anholtcad97662010-04-07 11:46:26 -0700418 else
419 return NULL;
420}
421
Ian Romanicke668c2a2010-05-26 18:58:27 -0700422
423ir_visitor_status
424ir_function_inlining_visitor::visit_enter(ir_expression *ir)
Eric Anholtcad97662010-04-07 11:46:26 -0700425{
426 (void) ir;
Ian Romanicke668c2a2010-05-26 18:58:27 -0700427 return visit_continue_with_parent;
Eric Anholtcad97662010-04-07 11:46:26 -0700428}
429
430
Ian Romanicke668c2a2010-05-26 18:58:27 -0700431ir_visitor_status
432ir_function_inlining_visitor::visit_enter(ir_return *ir)
Eric Anholtcad97662010-04-07 11:46:26 -0700433{
434 (void) ir;
Ian Romanicke668c2a2010-05-26 18:58:27 -0700435 return visit_continue_with_parent;
Eric Anholtcad97662010-04-07 11:46:26 -0700436}
437
438
Ian Romanicke668c2a2010-05-26 18:58:27 -0700439ir_visitor_status
440ir_function_inlining_visitor::visit_enter(ir_swizzle *ir)
Eric Anholtcad97662010-04-07 11:46:26 -0700441{
Ian Romanicke668c2a2010-05-26 18:58:27 -0700442 (void) ir;
443 return visit_continue_with_parent;
Eric Anholtcad97662010-04-07 11:46:26 -0700444}
445
446
Ian Romanicke668c2a2010-05-26 18:58:27 -0700447ir_visitor_status
448ir_function_inlining_visitor::visit_enter(ir_call *ir)
Eric Anholtcad97662010-04-07 11:46:26 -0700449{
Ian Romanicke668c2a2010-05-26 18:58:27 -0700450 if (can_inline(ir)) {
451 (void) ir->generate_inline(ir);
452 ir->remove();
453 this->progress = true;
Kenneth Graunke9fa99f32010-04-21 12:30:22 -0700454 }
Eric Anholtcad97662010-04-07 11:46:26 -0700455
Ian Romanicke668c2a2010-05-26 18:58:27 -0700456 return visit_continue;
Eric Anholtcad97662010-04-07 11:46:26 -0700457}
458
459
Ian Romanicke668c2a2010-05-26 18:58:27 -0700460ir_visitor_status
461ir_function_inlining_visitor::visit_enter(ir_assignment *ir)
Eric Anholtcad97662010-04-07 11:46:26 -0700462{
Ian Romanicke668c2a2010-05-26 18:58:27 -0700463 ir_call *call = ir->rhs->as_call();
464 if (!call || !can_inline(call))
465 return visit_continue;
Eric Anholtcad97662010-04-07 11:46:26 -0700466
Ian Romanicke668c2a2010-05-26 18:58:27 -0700467 /* generates the parameter setup, function body, and returns the return
468 * value of the function
469 */
470 ir_rvalue *rhs = call->generate_inline(ir);
471 assert(rhs);
Eric Anholtcad97662010-04-07 11:46:26 -0700472
Ian Romanicke668c2a2010-05-26 18:58:27 -0700473 ir->rhs = rhs;
474 this->progress = true;
Ian Romanickc7b10462010-05-19 13:20:12 +0200475
Ian Romanicke668c2a2010-05-26 18:58:27 -0700476 return visit_continue;
Eric Anholtcad97662010-04-07 11:46:26 -0700477}