Replace builtin_types.h generation with the generated output.
[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 "ir_expression_flattening.h"
35 #include "glsl_types.h"
36
37 class variable_remap : public exec_node {
38 public:
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
48 class ir_function_cloning_visitor : public ir_visitor {
49 public:
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 *);
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_loop *ir)
124 {
125 (void)ir;
126 this->result = NULL;
127 }
128
129 void
130 ir_function_cloning_visitor::visit(ir_loop_jump *ir)
131 {
132 (void) ir;
133 this->result = NULL;
134 }
135
136
137 void
138 ir_function_cloning_visitor::visit(ir_function_signature *ir)
139 {
140 (void)ir;
141 this->result = NULL;
142 }
143
144
145 void
146 ir_function_cloning_visitor::visit(ir_function *ir)
147 {
148 (void) ir;
149 this->result = NULL;
150 }
151
152 void
153 ir_function_cloning_visitor::visit(ir_expression *ir)
154 {
155 unsigned int operand;
156 ir_rvalue *op[2] = {NULL, NULL};
157
158 for (operand = 0; operand < ir->get_num_operands(); operand++) {
159 ir->operands[operand]->accept(this);
160 op[operand] = this->result->as_rvalue();
161 assert(op[operand]);
162 }
163
164 this->result = new ir_expression(ir->operation, ir->type, op[0], op[1]);
165 }
166
167
168 void
169 ir_function_cloning_visitor::visit(ir_swizzle *ir)
170 {
171 ir->val->accept(this);
172
173 this->result = new ir_swizzle(this->result->as_rvalue(), ir->mask);
174 }
175
176 void
177 ir_function_cloning_visitor::visit(ir_dereference *ir)
178 {
179 if (ir->mode == ir_dereference::ir_reference_variable) {
180 ir_variable *old_var = ir->var->as_variable();
181
182 /* If it's a deref of a real variable, then we need to remap it if
183 * it was local to the function.
184 */
185 if (old_var) {
186 ir_variable *new_var;
187
188 new_var = this->get_remapped_variable(old_var);
189
190 this->result = new ir_dereference(new_var);
191 } else {
192 ir->var->accept(this);
193
194 this->result = new ir_dereference(this->result);
195 }
196 } else if (ir->mode == ir_dereference::ir_reference_array) {
197 ir_instruction *variable;
198 ir_rvalue *index;
199
200 ir->var->accept(this);
201 variable = this->result;
202
203 ir->selector.array_index->accept(this);
204 index = this->result->as_rvalue();
205
206 this->result = new ir_dereference(variable, index);
207 } else {
208 assert(ir->mode == ir_dereference::ir_reference_record);
209 /* FINISHME: inlining of structure references */
210 assert(0);
211 }
212 }
213
214 void
215 ir_function_cloning_visitor::visit(ir_assignment *ir)
216 {
217 ir_rvalue *lhs, *rhs, *condition = NULL;
218
219 ir->lhs->accept(this);
220 lhs = this->result->as_rvalue();
221
222 ir->rhs->accept(this);
223 rhs = this->result->as_rvalue();
224
225 if (ir->condition) {
226 ir->condition->accept(this);
227 condition = this->result->as_rvalue();
228 }
229
230 this->result = new ir_assignment(lhs, rhs, condition);
231 }
232
233
234 void
235 ir_function_cloning_visitor::visit(ir_constant *ir)
236 {
237 this->result = ir->clone();
238 }
239
240
241 void
242 ir_function_cloning_visitor::visit(ir_call *ir)
243 {
244 exec_list parameters;
245
246 foreach_iter(exec_list_iterator, iter, *ir) {
247 ir_rvalue *param = (ir_rvalue *)iter.get();
248
249 param->accept(this);
250 parameters.push_tail(this->result);
251 }
252
253 this->result = new ir_call(ir->get_callee(), &parameters);
254 }
255
256
257 void
258 ir_function_cloning_visitor::visit(ir_return *ir)
259 {
260 ir_rvalue *rval;
261
262 assert(this->retval);
263
264 rval = ir->get_value();
265 rval->accept(this);
266 rval = this->result->as_rvalue();
267 assert(rval);
268
269 result = new ir_assignment(new ir_dereference(this->retval),
270 ir->get_value(), NULL);
271 }
272
273
274 void
275 ir_function_cloning_visitor::visit(ir_if *ir)
276 {
277 (void) ir;
278 result = NULL;
279 }
280
281 bool
282 can_inline(ir_call *call)
283 {
284 bool found_return = false;
285
286 /* FINISHME: Right now we only allow a single statement that is a return.
287 */
288 foreach_iter(exec_list_iterator, iter, call->get_callee()->body) {
289 ir_instruction *ir = (ir_instruction *)iter.get();
290 if (ir->get_next()->get_next() != NULL)
291 return false;
292
293 if (!ir->as_return())
294 return false;
295
296 found_return = true;
297 }
298
299 return found_return;
300 }
301
302 bool
303 automatic_inlining_predicate(ir_instruction *ir)
304 {
305 ir_call *call = ir->as_call();
306
307 if (call && can_inline(call))
308 return true;
309
310 return false;
311 }
312
313 bool
314 do_function_inlining(exec_list *instructions)
315 {
316 bool progress = false;
317
318 do_expression_flattening(instructions, automatic_inlining_predicate);
319
320 foreach_iter(exec_list_iterator, iter, *instructions) {
321 ir_instruction *ir = (ir_instruction *)iter.get();
322 ir_assignment *assign = ir->as_assignment();
323 ir_call *call;
324
325 if (assign) {
326 call = assign->rhs->as_call();
327 if (!call || !can_inline(call))
328 continue;
329
330 /* generates the parameter setup, function body, and returns the return
331 * value of the function
332 */
333 ir_rvalue *rhs = call->generate_inline(ir);
334 assert(rhs);
335
336 assign->rhs = rhs;
337 progress = true;
338 } else if ((call = ir->as_call()) && can_inline(call)) {
339 (void)call->generate_inline(ir);
340 ir->remove();
341 progress = true;
342 } else {
343 ir_function_inlining_visitor v;
344 ir->accept(&v);
345 }
346 }
347
348 return progress;
349 }
350
351 ir_rvalue *
352 ir_call::generate_inline(ir_instruction *next_ir)
353 {
354 ir_variable **parameters;
355 int num_parameters;
356 int i;
357 ir_variable *retval = NULL;
358
359 num_parameters = 0;
360 foreach_iter(exec_list_iterator, iter_sig, this->callee->parameters)
361 num_parameters++;
362
363 parameters = new ir_variable *[num_parameters];
364
365 /* Generate storage for the return value. */
366 if (this->callee->return_type) {
367 retval = new ir_variable(this->callee->return_type, "__retval");
368 next_ir->insert_before(retval);
369 }
370
371 ir_function_cloning_visitor v = ir_function_cloning_visitor(retval);
372
373 /* Generate the declarations for the parameters to our inlined code,
374 * and set up the mapping of real function body variables to ours.
375 */
376 i = 0;
377 exec_list_iterator sig_param_iter = this->callee->parameters.iterator();
378 exec_list_iterator param_iter = this->actual_parameters.iterator();
379 for (i = 0; i < num_parameters; i++) {
380 const ir_variable *const sig_param = (ir_variable *) sig_param_iter.get();
381 ir_rvalue *param = (ir_rvalue *) param_iter.get();
382
383 /* Generate a new variable for the parameter. */
384 parameters[i] = sig_param->clone();
385 next_ir->insert_before(parameters[i]);
386
387 v.remap_variable(sig_param, parameters[i]);
388
389 /* Move the actual param into our param variable if it's an 'in' type. */
390 if (parameters[i]->mode == ir_var_in ||
391 parameters[i]->mode == ir_var_inout) {
392 ir_assignment *assign;
393
394 assign = new ir_assignment(new ir_dereference(parameters[i]),
395 param, NULL);
396 next_ir->insert_before(assign);
397 }
398
399 sig_param_iter.next();
400 param_iter.next();
401 }
402
403 /* Generate the inlined body of the function. */
404 foreach_iter(exec_list_iterator, iter, callee->body) {
405 ir_instruction *ir = (ir_instruction *)iter.get();
406
407 ir->accept(&v);
408 assert(v.result);
409 next_ir->insert_before(v.result);
410 }
411
412 /* Generate the declarations for the parameters to our inlined code,
413 * and set up the mapping of real function body variables to ours.
414 */
415 i = 0;
416 param_iter = this->actual_parameters.iterator();
417 for (i = 0; i < num_parameters; i++) {
418 ir_instruction *const param = (ir_instruction *) param_iter.get();
419
420 /* Move the actual param into our param variable if it's an 'in' type. */
421 if (parameters[i]->mode == ir_var_out ||
422 parameters[i]->mode == ir_var_inout) {
423 ir_assignment *assign;
424
425 assign = new ir_assignment(param->as_rvalue(),
426 new ir_dereference(parameters[i]),
427 NULL);
428 next_ir->insert_before(assign);
429 }
430
431 param_iter.next();
432 }
433
434 delete(parameters);
435
436 if (retval)
437 return new ir_dereference(retval);
438 else
439 return NULL;
440 }
441
442 void
443 ir_function_inlining_visitor::visit(ir_variable *ir)
444 {
445 (void) ir;
446 }
447
448
449 void
450 ir_function_inlining_visitor::visit(ir_loop *ir)
451 {
452 do_function_inlining(&ir->body_instructions);
453 }
454
455 void
456 ir_function_inlining_visitor::visit(ir_loop_jump *ir)
457 {
458 (void) ir;
459 }
460
461
462 void
463 ir_function_inlining_visitor::visit(ir_function_signature *ir)
464 {
465 do_function_inlining(&ir->body);
466 }
467
468
469 void
470 ir_function_inlining_visitor::visit(ir_function *ir)
471 {
472 foreach_iter(exec_list_iterator, iter, *ir) {
473 ir_function_signature *const sig = (ir_function_signature *) iter.get();
474 sig->accept(this);
475 }
476 }
477
478 void
479 ir_function_inlining_visitor::visit(ir_expression *ir)
480 {
481 unsigned int operand;
482
483 for (operand = 0; operand < ir->get_num_operands(); operand++) {
484 ir->operands[operand]->accept(this);
485 }
486 }
487
488
489 void
490 ir_function_inlining_visitor::visit(ir_swizzle *ir)
491 {
492 ir->val->accept(this);
493 }
494
495
496 void
497 ir_function_inlining_visitor::visit(ir_dereference *ir)
498 {
499 if (ir->mode == ir_dereference::ir_reference_array) {
500 ir->selector.array_index->accept(this);
501 }
502 ir->var->accept(this);
503 }
504
505 void
506 ir_function_inlining_visitor::visit(ir_assignment *ir)
507 {
508 ir->rhs->accept(this);
509 }
510
511
512 void
513 ir_function_inlining_visitor::visit(ir_constant *ir)
514 {
515 (void) ir;
516 }
517
518
519 void
520 ir_function_inlining_visitor::visit(ir_call *ir)
521 {
522 (void) ir;
523 }
524
525
526 void
527 ir_function_inlining_visitor::visit(ir_return *ir)
528 {
529 (void) ir;
530 }
531
532
533 void
534 ir_function_inlining_visitor::visit(ir_if *ir)
535 {
536 ir->condition->accept(this);
537
538 do_function_inlining(&ir->then_instructions);
539 do_function_inlining(&ir->else_instructions);
540 }