Complain and exit if the given shader file doesn't exist.
[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 ir_function_inlining_visitor : public ir_hierarchical_visitor {
38 public:
39 ir_function_inlining_visitor()
40 {
41 progress = false;
42 }
43
44 virtual ~ir_function_inlining_visitor()
45 {
46 /* empty */
47 }
48
49 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_texture *);
54 virtual ir_visitor_status visit_enter(ir_swizzle *);
55
56 bool progress;
57 };
58
59 class variable_remap : public exec_node {
60 public:
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
70 class ir_function_cloning_visitor : public ir_visitor {
71 public:
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 *);
117 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 *);
122 virtual void visit(ir_texture *);
123 virtual void visit(ir_swizzle *);
124 virtual void visit(ir_dereference_variable *);
125 virtual void visit(ir_dereference_array *);
126 virtual void visit(ir_dereference_record *);
127 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
137 void
138 ir_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
147 void
148 ir_function_cloning_visitor::visit(ir_loop *ir)
149 {
150 /* FINISHME: Implement loop cloning. */
151 assert(0);
152
153 (void)ir;
154 this->result = NULL;
155 }
156
157 void
158 ir_function_cloning_visitor::visit(ir_loop_jump *ir)
159 {
160 /* FINISHME: Implement loop cloning. */
161 assert(0);
162
163 (void) ir;
164 this->result = NULL;
165 }
166
167
168 void
169 ir_function_cloning_visitor::visit(ir_function_signature *ir)
170 {
171 assert(0);
172 (void)ir;
173 this->result = NULL;
174 }
175
176
177 void
178 ir_function_cloning_visitor::visit(ir_function *ir)
179 {
180 assert(0);
181 (void) ir;
182 this->result = NULL;
183 }
184
185 void
186 ir_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
201 void
202 ir_function_cloning_visitor::visit(ir_texture *ir)
203 {
204 ir_texture *tex = new ir_texture(ir->op);
205
206 ir->sampler->accept(this);
207 tex->set_sampler(this->result->as_dereference());
208
209 ir->coordinate->accept(this);
210 tex->coordinate = this->result->as_rvalue();
211
212 if (ir->projector != NULL) {
213 ir->projector->accept(this);
214 tex->projector = this->result->as_rvalue();
215 }
216
217 if (ir->shadow_comparitor != NULL) {
218 ir->shadow_comparitor->accept(this);
219 tex->shadow_comparitor = this->result->as_rvalue();
220 }
221
222 for (int i = 0; i < 3; i++)
223 tex->offsets[i] = ir->offsets[i];
224
225 tex->lod_info = ir->lod_info;
226 }
227
228
229 void
230 ir_function_cloning_visitor::visit(ir_swizzle *ir)
231 {
232 ir->val->accept(this);
233
234 this->result = new ir_swizzle(this->result->as_rvalue(), ir->mask);
235 }
236
237 void
238 ir_function_cloning_visitor::visit(ir_dereference_variable *ir)
239 {
240 ir_variable *var = this->get_remapped_variable(ir->variable_referenced());
241 this->result = new ir_dereference_variable(var);
242 }
243
244 void
245 ir_function_cloning_visitor::visit(ir_dereference_array *ir)
246 {
247 ir->array->accept(this);
248
249 ir_rvalue *var = this->result->as_rvalue();
250
251 ir->array_index->accept(this);
252
253 ir_rvalue *index = this->result->as_rvalue();
254
255 this->result = new ir_dereference_array(var, index);
256 }
257
258 void
259 ir_function_cloning_visitor::visit(ir_dereference_record *ir)
260 {
261 ir->record->accept(this);
262
263 ir_rvalue *var = this->result->as_rvalue();
264
265 this->result = new ir_dereference_record(var, strdup(ir->field));
266 }
267
268 void
269 ir_function_cloning_visitor::visit(ir_assignment *ir)
270 {
271 ir_rvalue *lhs, *rhs, *condition = NULL;
272
273 ir->lhs->accept(this);
274 lhs = this->result->as_rvalue();
275
276 ir->rhs->accept(this);
277 rhs = this->result->as_rvalue();
278
279 if (ir->condition) {
280 ir->condition->accept(this);
281 condition = this->result->as_rvalue();
282 }
283
284 this->result = new ir_assignment(lhs, rhs, condition);
285 }
286
287
288 void
289 ir_function_cloning_visitor::visit(ir_constant *ir)
290 {
291 this->result = ir->clone();
292 }
293
294
295 void
296 ir_function_cloning_visitor::visit(ir_call *ir)
297 {
298 exec_list parameters;
299
300 foreach_iter(exec_list_iterator, iter, *ir) {
301 ir_rvalue *param = (ir_rvalue *)iter.get();
302
303 param->accept(this);
304 parameters.push_tail(this->result);
305 }
306
307 this->result = new ir_call(ir->get_callee(), &parameters);
308 }
309
310
311 void
312 ir_function_cloning_visitor::visit(ir_return *ir)
313 {
314 ir_rvalue *rval;
315
316 assert(this->retval);
317
318 rval = ir->get_value();
319 rval->accept(this);
320 rval = this->result->as_rvalue();
321 assert(rval);
322
323 result = new ir_assignment(new ir_dereference_variable(this->retval), rval,
324 NULL);
325 }
326
327
328 void
329 ir_function_cloning_visitor::visit(ir_if *ir)
330 {
331 /* FINISHME: Implement if cloning. */
332 assert(0);
333
334 (void) ir;
335 result = NULL;
336 }
337
338 bool
339 automatic_inlining_predicate(ir_instruction *ir)
340 {
341 ir_call *call = ir->as_call();
342
343 if (call && can_inline(call))
344 return true;
345
346 return false;
347 }
348
349 bool
350 do_function_inlining(exec_list *instructions)
351 {
352 ir_function_inlining_visitor v;
353
354 do_expression_flattening(instructions, automatic_inlining_predicate);
355
356 v.run(instructions);
357
358 return v.progress;
359 }
360
361 ir_rvalue *
362 ir_call::generate_inline(ir_instruction *next_ir)
363 {
364 ir_variable **parameters;
365 int num_parameters;
366 int i;
367 ir_variable *retval = NULL;
368
369 num_parameters = 0;
370 foreach_iter(exec_list_iterator, iter_sig, this->callee->parameters)
371 num_parameters++;
372
373 parameters = new ir_variable *[num_parameters];
374
375 /* Generate storage for the return value. */
376 if (this->callee->return_type) {
377 retval = new ir_variable(this->callee->return_type, "__retval");
378 next_ir->insert_before(retval);
379 }
380
381 ir_function_cloning_visitor v = ir_function_cloning_visitor(retval);
382
383 /* Generate the declarations for the parameters to our inlined code,
384 * and set up the mapping of real function body variables to ours.
385 */
386 i = 0;
387 exec_list_iterator sig_param_iter = this->callee->parameters.iterator();
388 exec_list_iterator param_iter = this->actual_parameters.iterator();
389 for (i = 0; i < num_parameters; i++) {
390 const ir_variable *const sig_param = (ir_variable *) sig_param_iter.get();
391 ir_rvalue *param = (ir_rvalue *) param_iter.get();
392
393 /* Generate a new variable for the parameter. */
394 parameters[i] = sig_param->clone();
395 next_ir->insert_before(parameters[i]);
396
397 v.remap_variable(sig_param, parameters[i]);
398
399 /* Move the actual param into our param variable if it's an 'in' type. */
400 if (parameters[i]->mode == ir_var_in ||
401 parameters[i]->mode == ir_var_inout) {
402 ir_assignment *assign;
403
404 assign = new ir_assignment(new ir_dereference_variable(parameters[i]),
405 param, NULL);
406 next_ir->insert_before(assign);
407 }
408
409 sig_param_iter.next();
410 param_iter.next();
411 }
412
413 /* Generate the inlined body of the function. */
414 foreach_iter(exec_list_iterator, iter, callee->body) {
415 ir_instruction *ir = (ir_instruction *)iter.get();
416
417 ir->accept(&v);
418 assert(v.result);
419 next_ir->insert_before(v.result);
420 }
421
422 /* Copy back the value of any 'out' parameters from the function body
423 * variables to our own.
424 */
425 i = 0;
426 param_iter = this->actual_parameters.iterator();
427 for (i = 0; i < num_parameters; i++) {
428 ir_instruction *const param = (ir_instruction *) param_iter.get();
429
430 /* Move our param variable into the actual param if it's an 'out' type. */
431 if (parameters[i]->mode == ir_var_out ||
432 parameters[i]->mode == ir_var_inout) {
433 ir_assignment *assign;
434
435 assign = new ir_assignment(param->as_rvalue(),
436 new ir_dereference_variable(parameters[i]),
437 NULL);
438 next_ir->insert_before(assign);
439 }
440
441 param_iter.next();
442 }
443
444 delete [] parameters;
445
446 if (retval)
447 return new ir_dereference_variable(retval);
448 else
449 return NULL;
450 }
451
452
453 ir_visitor_status
454 ir_function_inlining_visitor::visit_enter(ir_expression *ir)
455 {
456 (void) ir;
457 return visit_continue_with_parent;
458 }
459
460
461 ir_visitor_status
462 ir_function_inlining_visitor::visit_enter(ir_return *ir)
463 {
464 (void) ir;
465 return visit_continue_with_parent;
466 }
467
468
469 ir_visitor_status
470 ir_function_inlining_visitor::visit_enter(ir_texture *ir)
471 {
472 (void) ir;
473 return visit_continue_with_parent;
474 }
475
476
477 ir_visitor_status
478 ir_function_inlining_visitor::visit_enter(ir_swizzle *ir)
479 {
480 (void) ir;
481 return visit_continue_with_parent;
482 }
483
484
485 ir_visitor_status
486 ir_function_inlining_visitor::visit_enter(ir_call *ir)
487 {
488 if (can_inline(ir)) {
489 (void) ir->generate_inline(ir);
490 ir->remove();
491 this->progress = true;
492 }
493
494 return visit_continue;
495 }
496
497
498 ir_visitor_status
499 ir_function_inlining_visitor::visit_enter(ir_assignment *ir)
500 {
501 ir_call *call = ir->rhs->as_call();
502 if (!call || !can_inline(call))
503 return visit_continue;
504
505 /* generates the parameter setup, function body, and returns the return
506 * value of the function
507 */
508 ir_rvalue *rhs = call->generate_inline(ir);
509 assert(rhs);
510
511 ir->rhs = rhs;
512 this->progress = true;
513
514 return visit_continue;
515 }