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