Merge remote-tracking branch 'mesa-public/master' into vulkan
[mesa.git] / src / glsl / opt_constant_propagation.cpp
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * constant 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, constant, 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 constantright 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 CONSTANTRIGHT 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_constant_propagation.cpp
26 *
27 * Tracks assignments of constants to channels of variables, and
28 * usage of those constant channels with direct usage of the constants.
29 *
30 * This can lead to constant folding and algebraic optimizations in
31 * those later expressions, while causing no increase in instruction
32 * count (due to constants being generally free to load from a
33 * constant push buffer or as instruction immediate values) and
34 * possibly reducing register pressure.
35 */
36
37 #include "ir.h"
38 #include "ir_visitor.h"
39 #include "ir_rvalue_visitor.h"
40 #include "ir_basic_block.h"
41 #include "ir_optimization.h"
42 #include "glsl_types.h"
43
44 namespace {
45
46 class acp_entry : public exec_node
47 {
48 public:
49 acp_entry(ir_variable *var, unsigned write_mask, ir_constant *constant)
50 {
51 assert(var);
52 assert(constant);
53 this->var = var;
54 this->write_mask = write_mask;
55 this->constant = constant;
56 this->initial_values = write_mask;
57 }
58
59 acp_entry(const acp_entry *src)
60 {
61 this->var = src->var;
62 this->write_mask = src->write_mask;
63 this->constant = src->constant;
64 this->initial_values = src->initial_values;
65 }
66
67 ir_variable *var;
68 ir_constant *constant;
69 unsigned write_mask;
70
71 /** Mask of values initially available in the constant. */
72 unsigned initial_values;
73 };
74
75
76 class kill_entry : public exec_node
77 {
78 public:
79 kill_entry(ir_variable *var, unsigned write_mask)
80 {
81 assert(var);
82 this->var = var;
83 this->write_mask = write_mask;
84 }
85
86 ir_variable *var;
87 unsigned write_mask;
88 };
89
90 class ir_constant_propagation_visitor : public ir_rvalue_visitor {
91 public:
92 ir_constant_propagation_visitor()
93 {
94 progress = false;
95 killed_all = false;
96 mem_ctx = ralloc_context(0);
97 this->acp = new(mem_ctx) exec_list;
98 this->kills = new(mem_ctx) exec_list;
99 }
100 ~ir_constant_propagation_visitor()
101 {
102 ralloc_free(mem_ctx);
103 }
104
105 virtual ir_visitor_status visit_enter(class ir_loop *);
106 virtual ir_visitor_status visit_enter(class ir_function_signature *);
107 virtual ir_visitor_status visit_enter(class ir_function *);
108 virtual ir_visitor_status visit_leave(class ir_assignment *);
109 virtual ir_visitor_status visit_enter(class ir_call *);
110 virtual ir_visitor_status visit_enter(class ir_if *);
111
112 void add_constant(ir_assignment *ir);
113 void constant_folding(ir_rvalue **rvalue);
114 void constant_propagation(ir_rvalue **rvalue);
115 void kill(ir_variable *ir, unsigned write_mask);
116 void handle_if_block(exec_list *instructions);
117 void handle_rvalue(ir_rvalue **rvalue);
118
119 /** List of acp_entry: The available constants to propagate */
120 exec_list *acp;
121
122 /**
123 * List of kill_entry: The masks of variables whose values were
124 * killed in this block.
125 */
126 exec_list *kills;
127
128 bool progress;
129
130 bool killed_all;
131
132 void *mem_ctx;
133 };
134
135
136 void
137 ir_constant_propagation_visitor::constant_folding(ir_rvalue **rvalue) {
138
139 if (*rvalue == NULL || (*rvalue)->ir_type == ir_type_constant)
140 return;
141
142 /* Note that we visit rvalues one leaving. So if an expression has a
143 * non-constant operand, no need to go looking down it to find if it's
144 * constant. This cuts the time of this pass down drastically.
145 */
146 ir_expression *expr = (*rvalue)->as_expression();
147 if (expr) {
148 for (unsigned int i = 0; i < expr->get_num_operands(); i++) {
149 if (!expr->operands[i]->as_constant())
150 return;
151 }
152 }
153
154 /* Ditto for swizzles. */
155 ir_swizzle *swiz = (*rvalue)->as_swizzle();
156 if (swiz && !swiz->val->as_constant())
157 return;
158
159 ir_constant *constant = (*rvalue)->constant_expression_value();
160 if (constant) {
161 *rvalue = constant;
162 this->progress = true;
163 }
164 }
165
166 void
167 ir_constant_propagation_visitor::constant_propagation(ir_rvalue **rvalue) {
168
169 if (this->in_assignee || !*rvalue)
170 return;
171
172 const glsl_type *type = (*rvalue)->type;
173 if (!type->is_scalar() && !type->is_vector())
174 return;
175
176 ir_swizzle *swiz = NULL;
177 ir_dereference_variable *deref = (*rvalue)->as_dereference_variable();
178 if (!deref) {
179 swiz = (*rvalue)->as_swizzle();
180 if (!swiz)
181 return;
182
183 deref = swiz->val->as_dereference_variable();
184 if (!deref)
185 return;
186 }
187
188 ir_constant_data data;
189 memset(&data, 0, sizeof(data));
190
191 for (unsigned int i = 0; i < type->components(); i++) {
192 int channel;
193 acp_entry *found = NULL;
194
195 if (swiz) {
196 switch (i) {
197 case 0: channel = swiz->mask.x; break;
198 case 1: channel = swiz->mask.y; break;
199 case 2: channel = swiz->mask.z; break;
200 case 3: channel = swiz->mask.w; break;
201 default: assert(!"shouldn't be reached"); channel = 0; break;
202 }
203 } else {
204 channel = i;
205 }
206
207 foreach_in_list(acp_entry, entry, this->acp) {
208 if (entry->var == deref->var && entry->write_mask & (1 << channel)) {
209 found = entry;
210 break;
211 }
212 }
213
214 if (!found)
215 return;
216
217 int rhs_channel = 0;
218 for (int j = 0; j < 4; j++) {
219 if (j == channel)
220 break;
221 if (found->initial_values & (1 << j))
222 rhs_channel++;
223 }
224
225 switch (type->base_type) {
226 case GLSL_TYPE_FLOAT:
227 data.f[i] = found->constant->value.f[rhs_channel];
228 break;
229 case GLSL_TYPE_DOUBLE:
230 data.d[i] = found->constant->value.d[rhs_channel];
231 break;
232 case GLSL_TYPE_INT:
233 data.i[i] = found->constant->value.i[rhs_channel];
234 break;
235 case GLSL_TYPE_UINT:
236 data.u[i] = found->constant->value.u[rhs_channel];
237 break;
238 case GLSL_TYPE_BOOL:
239 data.b[i] = found->constant->value.b[rhs_channel];
240 break;
241 default:
242 assert(!"not reached");
243 break;
244 }
245 }
246
247 *rvalue = new(ralloc_parent(deref)) ir_constant(type, &data);
248 this->progress = true;
249 }
250
251 void
252 ir_constant_propagation_visitor::handle_rvalue(ir_rvalue **rvalue)
253 {
254 constant_propagation(rvalue);
255 constant_folding(rvalue);
256 }
257
258 ir_visitor_status
259 ir_constant_propagation_visitor::visit_enter(ir_function_signature *ir)
260 {
261 /* Treat entry into a function signature as a completely separate
262 * block. Any instructions at global scope will be shuffled into
263 * main() at link time, so they're irrelevant to us.
264 */
265 exec_list *orig_acp = this->acp;
266 exec_list *orig_kills = this->kills;
267 bool orig_killed_all = this->killed_all;
268
269 this->acp = new(mem_ctx) exec_list;
270 this->kills = new(mem_ctx) exec_list;
271 this->killed_all = false;
272
273 visit_list_elements(this, &ir->body);
274
275 this->kills = orig_kills;
276 this->acp = orig_acp;
277 this->killed_all = orig_killed_all;
278
279 return visit_continue_with_parent;
280 }
281
282 ir_visitor_status
283 ir_constant_propagation_visitor::visit_leave(ir_assignment *ir)
284 {
285 constant_folding(&ir->rhs);
286
287 if (this->in_assignee)
288 return visit_continue;
289
290 unsigned kill_mask = ir->write_mask;
291 if (ir->lhs->as_dereference_array()) {
292 /* The LHS of the assignment uses an array indexing operator (e.g. v[i]
293 * = ...;). Since we only try to constant propagate vectors and
294 * scalars, this means that either (a) array indexing is being used to
295 * select a vector component, or (b) the variable in question is neither
296 * a scalar or a vector, so we don't care about it. In the former case,
297 * we want to kill the whole vector, since in general we can't predict
298 * which vector component will be selected by array indexing. In the
299 * latter case, it doesn't matter what we do, so go ahead and kill the
300 * whole variable anyway.
301 *
302 * Note that if the array index is constant (e.g. v[2] = ...;), we could
303 * in principle be smarter, but we don't need to, because a future
304 * optimization pass will convert it to a simple assignment with the
305 * correct mask.
306 */
307 kill_mask = ~0;
308 }
309 kill(ir->lhs->variable_referenced(), kill_mask);
310
311 add_constant(ir);
312
313 return visit_continue;
314 }
315
316 ir_visitor_status
317 ir_constant_propagation_visitor::visit_enter(ir_function *ir)
318 {
319 (void) ir;
320 return visit_continue;
321 }
322
323 ir_visitor_status
324 ir_constant_propagation_visitor::visit_enter(ir_call *ir)
325 {
326 /* Do constant propagation on call parameters, but skip any out params */
327 foreach_two_lists(formal_node, &ir->callee->parameters,
328 actual_node, &ir->actual_parameters) {
329 ir_variable *sig_param = (ir_variable *) formal_node;
330 ir_rvalue *param = (ir_rvalue *) actual_node;
331 if (sig_param->data.mode != ir_var_function_out
332 && sig_param->data.mode != ir_var_function_inout) {
333 ir_rvalue *new_param = param;
334 handle_rvalue(&new_param);
335 if (new_param != param)
336 param->replace_with(new_param);
337 else
338 param->accept(this);
339 }
340 }
341
342 /* Since we're unlinked, we don't (necssarily) know the side effects of
343 * this call. So kill all copies.
344 */
345 acp->make_empty();
346 this->killed_all = true;
347
348 return visit_continue_with_parent;
349 }
350
351 void
352 ir_constant_propagation_visitor::handle_if_block(exec_list *instructions)
353 {
354 exec_list *orig_acp = this->acp;
355 exec_list *orig_kills = this->kills;
356 bool orig_killed_all = this->killed_all;
357
358 this->acp = new(mem_ctx) exec_list;
359 this->kills = new(mem_ctx) exec_list;
360 this->killed_all = false;
361
362 /* Populate the initial acp with a constant of the original */
363 foreach_in_list(acp_entry, a, orig_acp) {
364 this->acp->push_tail(new(this->mem_ctx) acp_entry(a));
365 }
366
367 visit_list_elements(this, instructions);
368
369 if (this->killed_all) {
370 orig_acp->make_empty();
371 }
372
373 exec_list *new_kills = this->kills;
374 this->kills = orig_kills;
375 this->acp = orig_acp;
376 this->killed_all = this->killed_all || orig_killed_all;
377
378 foreach_in_list(kill_entry, k, new_kills) {
379 kill(k->var, k->write_mask);
380 }
381 }
382
383 ir_visitor_status
384 ir_constant_propagation_visitor::visit_enter(ir_if *ir)
385 {
386 ir->condition->accept(this);
387 handle_rvalue(&ir->condition);
388
389 handle_if_block(&ir->then_instructions);
390 handle_if_block(&ir->else_instructions);
391
392 /* handle_if_block() already descended into the children. */
393 return visit_continue_with_parent;
394 }
395
396 ir_visitor_status
397 ir_constant_propagation_visitor::visit_enter(ir_loop *ir)
398 {
399 exec_list *orig_acp = this->acp;
400 exec_list *orig_kills = this->kills;
401 bool orig_killed_all = this->killed_all;
402
403 /* FINISHME: For now, the initial acp for loops is totally empty.
404 * We could go through once, then go through again with the acp
405 * cloned minus the killed entries after the first run through.
406 */
407 this->acp = new(mem_ctx) exec_list;
408 this->kills = new(mem_ctx) exec_list;
409 this->killed_all = false;
410
411 visit_list_elements(this, &ir->body_instructions);
412
413 if (this->killed_all) {
414 orig_acp->make_empty();
415 }
416
417 exec_list *new_kills = this->kills;
418 this->kills = orig_kills;
419 this->acp = orig_acp;
420 this->killed_all = this->killed_all || orig_killed_all;
421
422 foreach_in_list(kill_entry, k, new_kills) {
423 kill(k->var, k->write_mask);
424 }
425
426 /* already descended into the children. */
427 return visit_continue_with_parent;
428 }
429
430 void
431 ir_constant_propagation_visitor::kill(ir_variable *var, unsigned write_mask)
432 {
433 assert(var != NULL);
434
435 /* We don't track non-vectors. */
436 if (!var->type->is_vector() && !var->type->is_scalar())
437 return;
438
439 /* Remove any entries currently in the ACP for this kill. */
440 foreach_in_list_safe(acp_entry, entry, this->acp) {
441 if (entry->var == var) {
442 entry->write_mask &= ~write_mask;
443 if (entry->write_mask == 0)
444 entry->remove();
445 }
446 }
447
448 /* Add this writemask of the variable to the list of killed
449 * variables in this block.
450 */
451 foreach_in_list(kill_entry, entry, this->kills) {
452 if (entry->var == var) {
453 entry->write_mask |= write_mask;
454 return;
455 }
456 }
457 /* Not already in the list. Make new entry. */
458 this->kills->push_tail(new(this->mem_ctx) kill_entry(var, write_mask));
459 }
460
461 /**
462 * Adds an entry to the available constant list if it's a plain assignment
463 * of a variable to a variable.
464 */
465 void
466 ir_constant_propagation_visitor::add_constant(ir_assignment *ir)
467 {
468 acp_entry *entry;
469
470 if (ir->condition)
471 return;
472
473 if (!ir->write_mask)
474 return;
475
476 ir_dereference_variable *deref = ir->lhs->as_dereference_variable();
477 ir_constant *constant = ir->rhs->as_constant();
478
479 if (!deref || !constant)
480 return;
481
482 /* Only do constant propagation on vectors. Constant matrices,
483 * arrays, or structures would require more work elsewhere.
484 */
485 if (!deref->var->type->is_vector() && !deref->var->type->is_scalar())
486 return;
487
488 /* We can't do copy propagation on buffer variables, since the underlying
489 * memory storage is shared across multiple threads we can't be sure that
490 * the variable value isn't modified between this assignment and the next
491 * instruction where its value is read.
492 */
493 if (deref->var->data.mode == ir_var_shader_storage)
494 return;
495
496 entry = new(this->mem_ctx) acp_entry(deref->var, ir->write_mask, constant);
497 this->acp->push_tail(entry);
498 }
499
500 } /* unnamed namespace */
501
502 /**
503 * Does a constant propagation pass on the code present in the instruction stream.
504 */
505 bool
506 do_constant_propagation(exec_list *instructions)
507 {
508 ir_constant_propagation_visitor v;
509
510 visit_list_elements(&v, instructions);
511
512 return v.progress;
513 }