glsl: Function signatures cannot have NULL return type
[mesa.git] / src / glsl / opt_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 opt_function_inlining.cpp
26 *
27 * Replaces calls to functions with the body of the function.
28 */
29
30 #include <inttypes.h>
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 #include "program/hash_table.h"
37
38 static void
39 do_sampler_replacement(exec_list *instructions,
40 ir_variable *sampler,
41 ir_dereference *deref);
42
43 class ir_function_inlining_visitor : public ir_hierarchical_visitor {
44 public:
45 ir_function_inlining_visitor()
46 {
47 progress = false;
48 }
49
50 virtual ~ir_function_inlining_visitor()
51 {
52 /* empty */
53 }
54
55 virtual ir_visitor_status visit_enter(ir_expression *);
56 virtual ir_visitor_status visit_enter(ir_call *);
57 virtual ir_visitor_status visit_enter(ir_assignment *);
58 virtual ir_visitor_status visit_enter(ir_return *);
59 virtual ir_visitor_status visit_enter(ir_texture *);
60 virtual ir_visitor_status visit_enter(ir_swizzle *);
61
62 bool progress;
63 };
64
65
66 bool
67 automatic_inlining_predicate(ir_instruction *ir)
68 {
69 ir_call *call = ir->as_call();
70
71 if (call && can_inline(call))
72 return true;
73
74 return false;
75 }
76
77 bool
78 do_function_inlining(exec_list *instructions)
79 {
80 ir_function_inlining_visitor v;
81
82 do_expression_flattening(instructions, automatic_inlining_predicate);
83
84 v.run(instructions);
85
86 return v.progress;
87 }
88
89 static void
90 replace_return_with_assignment(ir_instruction *ir, void *data)
91 {
92 void *ctx = ralloc_parent(ir);
93 ir_variable *retval = (ir_variable *)data;
94 ir_return *ret = ir->as_return();
95
96 if (ret) {
97 if (ret->value) {
98 ir_rvalue *lhs = new(ctx) ir_dereference_variable(retval);
99 ret->replace_with(new(ctx) ir_assignment(lhs, ret->value, NULL));
100 } else {
101 /* un-valued return has to be the last return, or we shouldn't
102 * have reached here. (see can_inline()).
103 */
104 assert(ret->next->is_tail_sentinel());
105 ret->remove();
106 }
107 }
108 }
109
110 ir_rvalue *
111 ir_call::generate_inline(ir_instruction *next_ir)
112 {
113 void *ctx = ralloc_parent(this);
114 ir_variable **parameters;
115 int num_parameters;
116 int i;
117 ir_variable *retval = NULL;
118 struct hash_table *ht;
119
120 ht = hash_table_ctor(0, hash_table_pointer_hash, hash_table_pointer_compare);
121
122 num_parameters = 0;
123 foreach_iter(exec_list_iterator, iter_sig, this->callee->parameters)
124 num_parameters++;
125
126 parameters = new ir_variable *[num_parameters];
127
128 /* Generate storage for the return value. */
129 if (!this->callee->return_type->is_void()) {
130 retval = new(ctx) ir_variable(this->callee->return_type, "_ret_val",
131 ir_var_auto);
132 next_ir->insert_before(retval);
133 }
134
135 /* Generate the declarations for the parameters to our inlined code,
136 * and set up the mapping of real function body variables to ours.
137 */
138 i = 0;
139 exec_list_iterator sig_param_iter = this->callee->parameters.iterator();
140 exec_list_iterator param_iter = this->actual_parameters.iterator();
141 for (i = 0; i < num_parameters; i++) {
142 ir_variable *sig_param = (ir_variable *) sig_param_iter.get();
143 ir_rvalue *param = (ir_rvalue *) param_iter.get();
144
145 /* Generate a new variable for the parameter. */
146 if (sig_param->type->base_type == GLSL_TYPE_SAMPLER) {
147 /* For samplers, we want the inlined sampler references
148 * referencing the passed in sampler variable, since that
149 * will have the location information, which an assignment of
150 * a sampler wouldn't. Fix it up below.
151 */
152 parameters[i] = NULL;
153 } else {
154 parameters[i] = sig_param->clone(ctx, ht);
155 parameters[i]->mode = ir_var_auto;
156
157 /* Remove the read-only decoration becuase we're going to write
158 * directly to this variable. If the cloned variable is left
159 * read-only and the inlined function is inside a loop, the loop
160 * analysis code will get confused.
161 */
162 parameters[i]->read_only = false;
163 next_ir->insert_before(parameters[i]);
164 }
165
166 /* Move the actual param into our param variable if it's an 'in' type. */
167 if (parameters[i] && (sig_param->mode == ir_var_in ||
168 sig_param->mode == ir_var_const_in ||
169 sig_param->mode == ir_var_inout)) {
170 ir_assignment *assign;
171
172 assign = new(ctx) ir_assignment(new(ctx) ir_dereference_variable(parameters[i]),
173 param, NULL);
174 next_ir->insert_before(assign);
175 }
176
177 sig_param_iter.next();
178 param_iter.next();
179 }
180
181 exec_list new_instructions;
182
183 /* Generate the inlined body of the function to a new list */
184 foreach_iter(exec_list_iterator, iter, callee->body) {
185 ir_instruction *ir = (ir_instruction *)iter.get();
186 ir_instruction *new_ir = ir->clone(ctx, ht);
187
188 new_instructions.push_tail(new_ir);
189 visit_tree(new_ir, replace_return_with_assignment, retval);
190 }
191
192 /* If any samplers were passed in, replace any deref of the sampler
193 * with a deref of the sampler argument.
194 */
195 param_iter = this->actual_parameters.iterator();
196 sig_param_iter = this->callee->parameters.iterator();
197 for (i = 0; i < num_parameters; i++) {
198 ir_instruction *const param = (ir_instruction *) param_iter.get();
199 ir_variable *sig_param = (ir_variable *) sig_param_iter.get();
200
201 if (sig_param->type->base_type == GLSL_TYPE_SAMPLER) {
202 ir_dereference *deref = param->as_dereference();
203
204 assert(deref);
205 do_sampler_replacement(&new_instructions, sig_param, deref);
206 }
207 param_iter.next();
208 sig_param_iter.next();
209 }
210
211 /* Now push those new instructions in. */
212 foreach_iter(exec_list_iterator, iter, new_instructions) {
213 ir_instruction *ir = (ir_instruction *)iter.get();
214 next_ir->insert_before(ir);
215 }
216
217 /* Copy back the value of any 'out' parameters from the function body
218 * variables to our own.
219 */
220 i = 0;
221 param_iter = this->actual_parameters.iterator();
222 sig_param_iter = this->callee->parameters.iterator();
223 for (i = 0; i < num_parameters; i++) {
224 ir_instruction *const param = (ir_instruction *) param_iter.get();
225 const ir_variable *const sig_param = (ir_variable *) sig_param_iter.get();
226
227 /* Move our param variable into the actual param if it's an 'out' type. */
228 if (parameters[i] && (sig_param->mode == ir_var_out ||
229 sig_param->mode == ir_var_inout)) {
230 ir_assignment *assign;
231
232 assign = new(ctx) ir_assignment(param->clone(ctx, NULL)->as_rvalue(),
233 new(ctx) ir_dereference_variable(parameters[i]),
234 NULL);
235 next_ir->insert_before(assign);
236 }
237
238 param_iter.next();
239 sig_param_iter.next();
240 }
241
242 delete [] parameters;
243
244 hash_table_dtor(ht);
245
246 if (retval)
247 return new(ctx) ir_dereference_variable(retval);
248 else
249 return NULL;
250 }
251
252
253 ir_visitor_status
254 ir_function_inlining_visitor::visit_enter(ir_expression *ir)
255 {
256 (void) ir;
257 return visit_continue_with_parent;
258 }
259
260
261 ir_visitor_status
262 ir_function_inlining_visitor::visit_enter(ir_return *ir)
263 {
264 (void) ir;
265 return visit_continue_with_parent;
266 }
267
268
269 ir_visitor_status
270 ir_function_inlining_visitor::visit_enter(ir_texture *ir)
271 {
272 (void) ir;
273 return visit_continue_with_parent;
274 }
275
276
277 ir_visitor_status
278 ir_function_inlining_visitor::visit_enter(ir_swizzle *ir)
279 {
280 (void) ir;
281 return visit_continue_with_parent;
282 }
283
284
285 ir_visitor_status
286 ir_function_inlining_visitor::visit_enter(ir_call *ir)
287 {
288 if (can_inline(ir)) {
289 /* If the call was part of some tree, then it should have been
290 * flattened out or we shouldn't have seen it because of a
291 * visit_continue_with_parent in this visitor.
292 */
293 assert(ir == base_ir);
294
295 (void) ir->generate_inline(ir);
296 ir->remove();
297 this->progress = true;
298 }
299
300 return visit_continue;
301 }
302
303
304 ir_visitor_status
305 ir_function_inlining_visitor::visit_enter(ir_assignment *ir)
306 {
307 ir_call *call = ir->rhs->as_call();
308 if (!call || !can_inline(call))
309 return visit_continue;
310
311 /* generates the parameter setup, function body, and returns the return
312 * value of the function
313 */
314 ir_rvalue *rhs = call->generate_inline(ir);
315 assert(rhs);
316
317 ir->rhs = rhs;
318 this->progress = true;
319
320 return visit_continue;
321 }
322
323 /**
324 * Replaces references to the "sampler" variable with a clone of "deref."
325 *
326 * From the spec, samplers can appear in the tree as function
327 * (non-out) parameters and as the result of array indexing and
328 * structure field selection. In our builtin implementation, they
329 * also appear in the sampler field of an ir_tex instruction.
330 */
331
332 class ir_sampler_replacement_visitor : public ir_hierarchical_visitor {
333 public:
334 ir_sampler_replacement_visitor(ir_variable *sampler, ir_dereference *deref)
335 {
336 this->sampler = sampler;
337 this->deref = deref;
338 }
339
340 virtual ~ir_sampler_replacement_visitor()
341 {
342 }
343
344 virtual ir_visitor_status visit_leave(ir_call *);
345 virtual ir_visitor_status visit_leave(ir_dereference_array *);
346 virtual ir_visitor_status visit_leave(ir_dereference_record *);
347 virtual ir_visitor_status visit_leave(ir_texture *);
348
349 void replace_deref(ir_dereference **deref);
350 void replace_rvalue(ir_rvalue **rvalue);
351
352 ir_variable *sampler;
353 ir_dereference *deref;
354 };
355
356 void
357 ir_sampler_replacement_visitor::replace_deref(ir_dereference **deref)
358 {
359 ir_dereference_variable *deref_var = (*deref)->as_dereference_variable();
360 if (deref_var && deref_var->var == this->sampler) {
361 *deref = this->deref->clone(ralloc_parent(*deref), NULL);
362 }
363 }
364
365 void
366 ir_sampler_replacement_visitor::replace_rvalue(ir_rvalue **rvalue)
367 {
368 if (!*rvalue)
369 return;
370
371 ir_dereference *deref = (*rvalue)->as_dereference();
372
373 if (!deref)
374 return;
375
376 replace_deref(&deref);
377 *rvalue = deref;
378 }
379
380 ir_visitor_status
381 ir_sampler_replacement_visitor::visit_leave(ir_texture *ir)
382 {
383 replace_deref(&ir->sampler);
384
385 return visit_continue;
386 }
387
388 ir_visitor_status
389 ir_sampler_replacement_visitor::visit_leave(ir_dereference_array *ir)
390 {
391 replace_rvalue(&ir->array);
392 return visit_continue;
393 }
394
395 ir_visitor_status
396 ir_sampler_replacement_visitor::visit_leave(ir_dereference_record *ir)
397 {
398 replace_rvalue(&ir->record);
399 return visit_continue;
400 }
401
402 ir_visitor_status
403 ir_sampler_replacement_visitor::visit_leave(ir_call *ir)
404 {
405 foreach_iter(exec_list_iterator, iter, *ir) {
406 ir_rvalue *param = (ir_rvalue *)iter.get();
407 ir_rvalue *new_param = param;
408 replace_rvalue(&new_param);
409
410 if (new_param != param) {
411 param->replace_with(new_param);
412 }
413 }
414 return visit_continue;
415 }
416
417 static void
418 do_sampler_replacement(exec_list *instructions,
419 ir_variable *sampler,
420 ir_dereference *deref)
421 {
422 ir_sampler_replacement_visitor v(sampler, deref);
423
424 visit_list_elements(&v, instructions);
425 }