b6434b80544cad32aa599e5f9095279c275bda1e
[mesa.git] / ir_function_inlining.cpp
1 /*
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"
34 #include "glsl_types.h"
35
36 class variable_remap : public exec_node {
37 public:
38 variable_remap(const ir_variable *old_var, ir_variable *new_var)
39 : old_var(old_var), new_var(new_var)
40 {
41 /* empty */
42 }
43 const ir_variable *old_var;
44 ir_variable *new_var;
45 };
46
47 class ir_function_cloning_visitor : public ir_visitor {
48 public:
49 ir_function_cloning_visitor(ir_variable *retval)
50 : retval(retval)
51 {
52 /* empty */
53 }
54
55 virtual ~ir_function_cloning_visitor()
56 {
57 /* empty */
58 }
59
60 void remap_variable(const ir_variable *old_var, ir_variable *new_var) {
61 variable_remap *remap = new variable_remap(old_var, new_var);
62 this->remap_list.push_tail(remap);
63 }
64
65 ir_variable *get_remapped_variable(ir_variable *var) {
66 foreach_iter(exec_list_iterator, iter, this->remap_list) {
67 variable_remap *remap = (variable_remap *)iter.get();
68
69 if (var == remap->old_var)
70 return remap->new_var;
71 }
72
73 /* Not a reapped variable, so a global scoped reference, for example. */
74 return var;
75 }
76
77 /* List of variable_remap for mapping from original function body variables
78 * to inlined function body variables.
79 */
80 exec_list remap_list;
81
82 /* Return value for the inlined function. */
83 ir_variable *retval;
84
85 /**
86 * \name Visit methods
87 *
88 * As typical for the visitor pattern, there must be one \c visit method for
89 * each concrete subclass of \c ir_instruction. Virtual base classes within
90 * the hierarchy should not have \c visit methods.
91 */
92 /*@{*/
93 virtual void visit(ir_variable *);
94 virtual void visit(ir_label *);
95 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
112 void
113 ir_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
122 void
123 ir_function_cloning_visitor::visit(ir_label *ir)
124 {
125 (void)ir;
126 this->result = NULL;
127 }
128
129 void
130 ir_function_cloning_visitor::visit(ir_loop *ir)
131 {
132 (void)ir;
133 this->result = NULL;
134 }
135
136 void
137 ir_function_cloning_visitor::visit(ir_loop_jump *ir)
138 {
139 (void) ir;
140 this->result = NULL;
141 }
142
143
144 void
145 ir_function_cloning_visitor::visit(ir_function_signature *ir)
146 {
147 (void)ir;
148 this->result = NULL;
149 }
150
151
152 void
153 ir_function_cloning_visitor::visit(ir_function *ir)
154 {
155 (void) ir;
156 this->result = NULL;
157 }
158
159 void
160 ir_function_cloning_visitor::visit(ir_expression *ir)
161 {
162 unsigned int operand;
163 ir_rvalue *op[2] = {NULL, NULL};
164
165 for (operand = 0; operand < ir->get_num_operands(); operand++) {
166 ir->operands[operand]->accept(this);
167 op[operand] = this->result->as_rvalue();
168 assert(op[operand]);
169 }
170
171 this->result = new ir_expression(ir->operation, ir->type, op[0], op[1]);
172 }
173
174
175 void
176 ir_function_cloning_visitor::visit(ir_swizzle *ir)
177 {
178 ir->val->accept(this);
179
180 this->result = new ir_swizzle(this->result->as_rvalue(), ir->mask);
181 }
182
183 void
184 ir_function_cloning_visitor::visit(ir_dereference *ir)
185 {
186 if (ir->mode == ir_dereference::ir_reference_variable) {
187 ir_variable *old_var = ir->var->as_variable();
188
189 /* If it's a deref of a real variable, then we need to remap it if
190 * it was local to the function.
191 */
192 if (old_var) {
193 ir_variable *new_var;
194
195 new_var = this->get_remapped_variable(old_var);
196
197 this->result = new ir_dereference(new_var);
198 } else {
199 ir->var->accept(this);
200
201 this->result = new ir_dereference(this->result);
202 }
203 } else {
204 this->result = NULL;
205 }
206 }
207
208 void
209 ir_function_cloning_visitor::visit(ir_assignment *ir)
210 {
211 ir_rvalue *lhs, *rhs, *condition;
212
213 ir->lhs->accept(this);
214 lhs = this->result->as_rvalue();
215
216 ir->rhs->accept(this);
217 rhs = this->result->as_rvalue();
218
219 ir->condition->accept(this);
220 condition = this->result->as_rvalue();
221
222 this->result = new ir_assignment(lhs, rhs, condition);
223 }
224
225
226 void
227 ir_function_cloning_visitor::visit(ir_constant *ir)
228 {
229 this->result = ir->clone();
230 }
231
232
233 void
234 ir_function_cloning_visitor::visit(ir_call *ir)
235 {
236 exec_list parameters;
237
238 foreach_iter(exec_list_iterator, iter, *ir) {
239 ir_rvalue *param = (ir_rvalue *)iter.get();
240
241 param->accept(this);
242 parameters.push_tail(this->result);
243 }
244
245 this->result = new ir_call(ir->get_callee(), &parameters);
246 }
247
248
249 void
250 ir_function_cloning_visitor::visit(ir_return *ir)
251 {
252 ir_rvalue *rval;
253
254 assert(this->retval);
255
256 rval = ir->get_value();
257 rval->accept(this);
258 rval = this->result->as_rvalue();
259 assert(rval);
260
261 result = new ir_assignment(new ir_dereference(this->retval),
262 ir->get_value(), NULL);
263 }
264
265
266 void
267 ir_function_cloning_visitor::visit(ir_if *ir)
268 {
269 (void) ir;
270 result = NULL;
271 }
272
273 bool
274 can_inline(ir_call *call)
275 {
276 bool found_return = false;
277
278 /* FINISHME: Right now we only allow a single statement that is a return.
279 */
280 foreach_iter(exec_list_iterator, iter, call->get_callee()->body) {
281 ir_instruction *ir = (ir_instruction *)iter.get();
282 if (ir->get_next()->get_next() != NULL)
283 return false;
284
285 if (!ir->as_return())
286 return false;
287
288 found_return = true;
289 }
290
291 return found_return;
292 }
293
294 bool
295 do_function_inlining(exec_list *instructions)
296 {
297 bool progress;
298
299 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
330 ir_rvalue *
331 ir_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
391 /* Generate the declarations for the parameters to our inlined code,
392 * and set up the mapping of real function body variables to ours.
393 */
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
399 /* Move the actual param into our param variable if it's an 'in' type. */
400 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
421 void
422 ir_function_inlining_visitor::visit(ir_variable *ir)
423 {
424 (void) ir;
425 }
426
427
428 void
429 ir_function_inlining_visitor::visit(ir_label *ir)
430 {
431 ir->signature->accept(this);
432 }
433
434 void
435 ir_function_inlining_visitor::visit(ir_loop *ir)
436 {
437 do_function_inlining(&ir->body_instructions);
438 }
439
440 void
441 ir_function_inlining_visitor::visit(ir_loop_jump *ir)
442 {
443 (void) ir;
444 }
445
446
447 void
448 ir_function_inlining_visitor::visit(ir_function_signature *ir)
449 {
450 do_function_inlining(&ir->body);
451 }
452
453
454 void
455 ir_function_inlining_visitor::visit(ir_function *ir)
456 {
457 (void) ir;
458 }
459
460 void
461 ir_function_inlining_visitor::visit(ir_expression *ir)
462 {
463 unsigned int operand;
464
465 for (operand = 0; operand < ir->get_num_operands(); operand++) {
466 ir->operands[operand]->accept(this);
467 }
468 }
469
470
471 void
472 ir_function_inlining_visitor::visit(ir_swizzle *ir)
473 {
474 ir->val->accept(this);
475 }
476
477
478 void
479 ir_function_inlining_visitor::visit(ir_dereference *ir)
480 {
481 if (ir->mode == ir_dereference::ir_reference_array) {
482 ir->selector.array_index->accept(this);
483 }
484 ir->var->accept(this);
485 }
486
487 void
488 ir_function_inlining_visitor::visit(ir_assignment *ir)
489 {
490 ir->rhs->accept(this);
491 }
492
493
494 void
495 ir_function_inlining_visitor::visit(ir_constant *ir)
496 {
497 (void) ir;
498 }
499
500
501 void
502 ir_function_inlining_visitor::visit(ir_call *ir)
503 {
504 (void) ir;
505 }
506
507
508 void
509 ir_function_inlining_visitor::visit(ir_return *ir)
510 {
511 (void) ir;
512 }
513
514
515 void
516 ir_function_inlining_visitor::visit(ir_if *ir)
517 {
518 ir->condition->accept(this);
519
520 do_function_inlining(&ir->then_instructions);
521 do_function_inlining(&ir->else_instructions);
522 }