Merge branch 'fixes'
[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 /* FINISHME: Implement loop cloning. */
126 assert(0);
127
128 (void)ir;
129 this->result = NULL;
130 }
131
132 void
133 ir_function_cloning_visitor::visit(ir_loop_jump *ir)
134 {
135 /* FINISHME: Implement loop cloning. */
136 assert(0);
137
138 (void) ir;
139 this->result = NULL;
140 }
141
142
143 void
144 ir_function_cloning_visitor::visit(ir_function_signature *ir)
145 {
146 assert(0);
147 (void)ir;
148 this->result = NULL;
149 }
150
151
152 void
153 ir_function_cloning_visitor::visit(ir_function *ir)
154 {
155 assert(0);
156 (void) ir;
157 this->result = NULL;
158 }
159
160 void
161 ir_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
176 void
177 ir_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
184 void
185 ir_function_cloning_visitor::visit(ir_dereference *ir)
186 {
187 ir_variable *old_var = ir->var->as_variable();
188 ir_instruction *var;
189
190 if (old_var)
191 var = this->get_remapped_variable(old_var);
192 else {
193 ir->var->accept(this);
194 var = this->result;
195 }
196
197 if (ir->mode == ir_dereference::ir_reference_variable) {
198 this->result = new ir_dereference(var);
199 } else if (ir->mode == ir_dereference::ir_reference_array) {
200 ir_rvalue *index;
201
202 ir->selector.array_index->accept(this);
203 index = this->result->as_rvalue();
204
205 this->result = new ir_dereference(var, index);
206 } else {
207 assert(ir->mode == ir_dereference::ir_reference_record);
208 this->result = new ir_dereference(var, strdup(ir->selector.field));
209 }
210 }
211
212 void
213 ir_function_cloning_visitor::visit(ir_assignment *ir)
214 {
215 ir_rvalue *lhs, *rhs, *condition = NULL;
216
217 ir->lhs->accept(this);
218 lhs = this->result->as_rvalue();
219
220 ir->rhs->accept(this);
221 rhs = this->result->as_rvalue();
222
223 if (ir->condition) {
224 ir->condition->accept(this);
225 condition = this->result->as_rvalue();
226 }
227
228 this->result = new ir_assignment(lhs, rhs, condition);
229 }
230
231
232 void
233 ir_function_cloning_visitor::visit(ir_constant *ir)
234 {
235 this->result = ir->clone();
236 }
237
238
239 void
240 ir_function_cloning_visitor::visit(ir_call *ir)
241 {
242 exec_list parameters;
243
244 foreach_iter(exec_list_iterator, iter, *ir) {
245 ir_rvalue *param = (ir_rvalue *)iter.get();
246
247 param->accept(this);
248 parameters.push_tail(this->result);
249 }
250
251 this->result = new ir_call(ir->get_callee(), &parameters);
252 }
253
254
255 void
256 ir_function_cloning_visitor::visit(ir_return *ir)
257 {
258 ir_rvalue *rval;
259
260 assert(this->retval);
261
262 rval = ir->get_value();
263 rval->accept(this);
264 rval = this->result->as_rvalue();
265 assert(rval);
266
267 result = new ir_assignment(new ir_dereference(this->retval), rval, NULL);
268 }
269
270
271 void
272 ir_function_cloning_visitor::visit(ir_if *ir)
273 {
274 /* FINISHME: Implement if cloning. */
275 assert(0);
276
277 (void) ir;
278 result = NULL;
279 }
280
281 bool
282 automatic_inlining_predicate(ir_instruction *ir)
283 {
284 ir_call *call = ir->as_call();
285
286 if (call && can_inline(call))
287 return true;
288
289 return false;
290 }
291
292 bool
293 do_function_inlining(exec_list *instructions)
294 {
295 bool progress = false;
296
297 do_expression_flattening(instructions, automatic_inlining_predicate);
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 /* Copy back the value of any 'out' parameters from the function body
392 * variables to our own.
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 our param variable into the actual param if it's an 'out' 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_loop *ir)
430 {
431 do_function_inlining(&ir->body_instructions);
432 }
433
434 void
435 ir_function_inlining_visitor::visit(ir_loop_jump *ir)
436 {
437 (void) ir;
438 }
439
440
441 void
442 ir_function_inlining_visitor::visit(ir_function_signature *ir)
443 {
444 do_function_inlining(&ir->body);
445 }
446
447
448 void
449 ir_function_inlining_visitor::visit(ir_function *ir)
450 {
451 foreach_iter(exec_list_iterator, iter, *ir) {
452 ir_function_signature *const sig = (ir_function_signature *) iter.get();
453 sig->accept(this);
454 }
455 }
456
457 void
458 ir_function_inlining_visitor::visit(ir_expression *ir)
459 {
460 unsigned int operand;
461
462 for (operand = 0; operand < ir->get_num_operands(); operand++) {
463 ir->operands[operand]->accept(this);
464 }
465 }
466
467
468 void
469 ir_function_inlining_visitor::visit(ir_swizzle *ir)
470 {
471 ir->val->accept(this);
472 }
473
474
475 void
476 ir_function_inlining_visitor::visit(ir_dereference *ir)
477 {
478 if (ir->mode == ir_dereference::ir_reference_array) {
479 ir->selector.array_index->accept(this);
480 }
481 ir->var->accept(this);
482 }
483
484 void
485 ir_function_inlining_visitor::visit(ir_assignment *ir)
486 {
487 ir->rhs->accept(this);
488 }
489
490
491 void
492 ir_function_inlining_visitor::visit(ir_constant *ir)
493 {
494 (void) ir;
495 }
496
497
498 void
499 ir_function_inlining_visitor::visit(ir_call *ir)
500 {
501 (void) ir;
502 }
503
504
505 void
506 ir_function_inlining_visitor::visit(ir_return *ir)
507 {
508 (void) ir;
509 }
510
511
512 void
513 ir_function_inlining_visitor::visit(ir_if *ir)
514 {
515 ir->condition->accept(this);
516
517 do_function_inlining(&ir->then_instructions);
518 do_function_inlining(&ir->else_instructions);
519 }