52e3937bb11ecfe685dcae6828dfae2f83dd767a
[mesa.git] / src / compiler / 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 "compiler/glsl_types.h"
43 #include "util/hash_table.h"
44
45 namespace {
46
47 class acp_entry : public exec_node
48 {
49 public:
50 /* override operator new from exec_node */
51 DECLARE_LINEAR_ZALLOC_CXX_OPERATORS(acp_entry)
52
53 acp_entry(ir_variable *var, unsigned write_mask, ir_constant *constant)
54 {
55 assert(var);
56 assert(constant);
57 this->var = var;
58 this->write_mask = write_mask;
59 this->constant = constant;
60 this->initial_values = write_mask;
61 }
62
63 acp_entry(const acp_entry *src)
64 {
65 this->var = src->var;
66 this->write_mask = src->write_mask;
67 this->constant = src->constant;
68 this->initial_values = src->initial_values;
69 }
70
71 ir_variable *var;
72 ir_constant *constant;
73 unsigned write_mask;
74
75 /** Mask of values initially available in the constant. */
76 unsigned initial_values;
77 };
78
79
80 class kill_entry : public exec_node
81 {
82 public:
83 /* override operator new from exec_node */
84 DECLARE_LINEAR_ZALLOC_CXX_OPERATORS(kill_entry)
85
86 kill_entry(ir_variable *var, unsigned write_mask)
87 {
88 assert(var);
89 this->var = var;
90 this->write_mask = write_mask;
91 }
92
93 ir_variable *var;
94 unsigned write_mask;
95 };
96
97 class ir_constant_propagation_visitor : public ir_rvalue_visitor {
98 public:
99 ir_constant_propagation_visitor()
100 {
101 progress = false;
102 killed_all = false;
103 mem_ctx = ralloc_context(0);
104 this->lin_ctx = linear_alloc_parent(this->mem_ctx, 0);
105 this->acp = new(mem_ctx) exec_list;
106 this->kills = _mesa_hash_table_create(mem_ctx, _mesa_hash_pointer,
107 _mesa_key_pointer_equal);
108 }
109 ~ir_constant_propagation_visitor()
110 {
111 ralloc_free(mem_ctx);
112 }
113
114 virtual ir_visitor_status visit_enter(class ir_loop *);
115 virtual ir_visitor_status visit_enter(class ir_function_signature *);
116 virtual ir_visitor_status visit_enter(class ir_function *);
117 virtual ir_visitor_status visit_leave(class ir_assignment *);
118 virtual ir_visitor_status visit_enter(class ir_call *);
119 virtual ir_visitor_status visit_enter(class ir_if *);
120
121 void add_constant(ir_assignment *ir);
122 void constant_folding(ir_rvalue **rvalue);
123 void constant_propagation(ir_rvalue **rvalue);
124 void kill(ir_variable *ir, unsigned write_mask);
125 void handle_if_block(exec_list *instructions);
126 void handle_rvalue(ir_rvalue **rvalue);
127
128 /** List of acp_entry: The available constants to propagate */
129 exec_list *acp;
130
131 /**
132 * Hash table of kill_entry: The masks of variables whose values were
133 * killed in this block.
134 */
135 hash_table *kills;
136
137 bool progress;
138
139 bool killed_all;
140
141 void *mem_ctx;
142 void *lin_ctx;
143 };
144
145
146 void
147 ir_constant_propagation_visitor::constant_folding(ir_rvalue **rvalue)
148 {
149 if (this->in_assignee || *rvalue == NULL)
150 return;
151
152 if (ir_constant_fold(rvalue))
153 this->progress = true;
154
155 ir_dereference_variable *var_ref = (*rvalue)->as_dereference_variable();
156 if (var_ref && !var_ref->type->is_array()) {
157 ir_constant *constant =
158 var_ref->constant_expression_value(ralloc_parent(var_ref));
159 if (constant) {
160 *rvalue = constant;
161 this->progress = true;
162 }
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 hash_table *orig_kills = this->kills;
267 bool orig_killed_all = this->killed_all;
268
269 this->acp = new(mem_ctx) exec_list;
270 this->kills = _mesa_hash_table_create(mem_ctx, _mesa_hash_pointer,
271 _mesa_key_pointer_equal);
272 this->killed_all = false;
273
274 visit_list_elements(this, &ir->body);
275
276 this->kills = orig_kills;
277 this->acp = orig_acp;
278 this->killed_all = orig_killed_all;
279
280 return visit_continue_with_parent;
281 }
282
283 ir_visitor_status
284 ir_constant_propagation_visitor::visit_leave(ir_assignment *ir)
285 {
286 constant_folding(&ir->rhs);
287
288 if (this->in_assignee)
289 return visit_continue;
290
291 unsigned kill_mask = ir->write_mask;
292 if (ir->lhs->as_dereference_array()) {
293 /* The LHS of the assignment uses an array indexing operator (e.g. v[i]
294 * = ...;). Since we only try to constant propagate vectors and
295 * scalars, this means that either (a) array indexing is being used to
296 * select a vector component, or (b) the variable in question is neither
297 * a scalar or a vector, so we don't care about it. In the former case,
298 * we want to kill the whole vector, since in general we can't predict
299 * which vector component will be selected by array indexing. In the
300 * latter case, it doesn't matter what we do, so go ahead and kill the
301 * whole variable anyway.
302 *
303 * Note that if the array index is constant (e.g. v[2] = ...;), we could
304 * in principle be smarter, but we don't need to, because a future
305 * optimization pass will convert it to a simple assignment with the
306 * correct mask.
307 */
308 kill_mask = ~0;
309 }
310 kill(ir->lhs->variable_referenced(), kill_mask);
311
312 add_constant(ir);
313
314 return visit_continue;
315 }
316
317 ir_visitor_status
318 ir_constant_propagation_visitor::visit_enter(ir_function *ir)
319 {
320 (void) ir;
321 return visit_continue;
322 }
323
324 ir_visitor_status
325 ir_constant_propagation_visitor::visit_enter(ir_call *ir)
326 {
327 /* Do constant propagation on call parameters, but skip any out params */
328 foreach_two_lists(formal_node, &ir->callee->parameters,
329 actual_node, &ir->actual_parameters) {
330 ir_variable *sig_param = (ir_variable *) formal_node;
331 ir_rvalue *param = (ir_rvalue *) actual_node;
332 if (sig_param->data.mode != ir_var_function_out
333 && sig_param->data.mode != ir_var_function_inout) {
334 ir_rvalue *new_param = param;
335 handle_rvalue(&new_param);
336 if (new_param != param)
337 param->replace_with(new_param);
338 else
339 param->accept(this);
340 }
341 }
342
343 /* Since we're unlinked, we don't (necssarily) know the side effects of
344 * this call. So kill all copies.
345 */
346 acp->make_empty();
347 this->killed_all = true;
348
349 return visit_continue_with_parent;
350 }
351
352 void
353 ir_constant_propagation_visitor::handle_if_block(exec_list *instructions)
354 {
355 exec_list *orig_acp = this->acp;
356 hash_table *orig_kills = this->kills;
357 bool orig_killed_all = this->killed_all;
358
359 this->acp = new(mem_ctx) exec_list;
360 this->kills = _mesa_hash_table_create(mem_ctx, _mesa_hash_pointer,
361 _mesa_key_pointer_equal);
362 this->killed_all = false;
363
364 /* Populate the initial acp with a constant of the original */
365 foreach_in_list(acp_entry, a, orig_acp) {
366 this->acp->push_tail(new(this->lin_ctx) acp_entry(a));
367 }
368
369 visit_list_elements(this, instructions);
370
371 if (this->killed_all) {
372 orig_acp->make_empty();
373 }
374
375 hash_table *new_kills = this->kills;
376 this->kills = orig_kills;
377 this->acp = orig_acp;
378 this->killed_all = this->killed_all || orig_killed_all;
379
380 hash_entry *htk;
381 hash_table_foreach(new_kills, htk) {
382 kill_entry *k = (kill_entry *) htk->data;
383 kill(k->var, k->write_mask);
384 }
385 }
386
387 ir_visitor_status
388 ir_constant_propagation_visitor::visit_enter(ir_if *ir)
389 {
390 ir->condition->accept(this);
391 handle_rvalue(&ir->condition);
392
393 handle_if_block(&ir->then_instructions);
394 handle_if_block(&ir->else_instructions);
395
396 /* handle_if_block() already descended into the children. */
397 return visit_continue_with_parent;
398 }
399
400 ir_visitor_status
401 ir_constant_propagation_visitor::visit_enter(ir_loop *ir)
402 {
403 exec_list *orig_acp = this->acp;
404 hash_table *orig_kills = this->kills;
405 bool orig_killed_all = this->killed_all;
406
407 /* FINISHME: For now, the initial acp for loops is totally empty.
408 * We could go through once, then go through again with the acp
409 * cloned minus the killed entries after the first run through.
410 */
411 this->acp = new(mem_ctx) exec_list;
412 this->kills = _mesa_hash_table_create(mem_ctx, _mesa_hash_pointer,
413 _mesa_key_pointer_equal);
414 this->killed_all = false;
415
416 visit_list_elements(this, &ir->body_instructions);
417
418 if (this->killed_all) {
419 orig_acp->make_empty();
420 }
421
422 hash_table *new_kills = this->kills;
423 this->kills = orig_kills;
424 this->acp = orig_acp;
425 this->killed_all = this->killed_all || orig_killed_all;
426
427 hash_entry *htk;
428 hash_table_foreach(new_kills, htk) {
429 kill_entry *k = (kill_entry *) htk->data;
430 kill(k->var, k->write_mask);
431 }
432
433 /* already descended into the children. */
434 return visit_continue_with_parent;
435 }
436
437 void
438 ir_constant_propagation_visitor::kill(ir_variable *var, unsigned write_mask)
439 {
440 assert(var != NULL);
441
442 /* We don't track non-vectors. */
443 if (!var->type->is_vector() && !var->type->is_scalar())
444 return;
445
446 /* Remove any entries currently in the ACP for this kill. */
447 foreach_in_list_safe(acp_entry, entry, this->acp) {
448 if (entry->var == var) {
449 entry->write_mask &= ~write_mask;
450 if (entry->write_mask == 0)
451 entry->remove();
452 }
453 }
454
455 /* Add this writemask of the variable to the hash table of killed
456 * variables in this block.
457 */
458 hash_entry *kill_hash_entry = _mesa_hash_table_search(this->kills, var);
459 if (kill_hash_entry) {
460 kill_entry *entry = (kill_entry *) kill_hash_entry->data;
461 entry->write_mask |= write_mask;
462 return;
463 }
464 /* Not already in the hash table. Make new entry. */
465 _mesa_hash_table_insert(this->kills, var,
466 new(this->lin_ctx) kill_entry(var, write_mask));
467 }
468
469 /**
470 * Adds an entry to the available constant list if it's a plain assignment
471 * of a variable to a variable.
472 */
473 void
474 ir_constant_propagation_visitor::add_constant(ir_assignment *ir)
475 {
476 acp_entry *entry;
477
478 if (ir->condition)
479 return;
480
481 if (!ir->write_mask)
482 return;
483
484 ir_dereference_variable *deref = ir->lhs->as_dereference_variable();
485 ir_constant *constant = ir->rhs->as_constant();
486
487 if (!deref || !constant)
488 return;
489
490 /* Only do constant propagation on vectors. Constant matrices,
491 * arrays, or structures would require more work elsewhere.
492 */
493 if (!deref->var->type->is_vector() && !deref->var->type->is_scalar())
494 return;
495
496 /* We can't do copy propagation on buffer variables, since the underlying
497 * memory storage is shared across multiple threads we can't be sure that
498 * the variable value isn't modified between this assignment and the next
499 * instruction where its value is read.
500 */
501 if (deref->var->data.mode == ir_var_shader_storage ||
502 deref->var->data.mode == ir_var_shader_shared)
503 return;
504
505 entry = new(this->lin_ctx) acp_entry(deref->var, ir->write_mask, constant);
506 this->acp->push_tail(entry);
507 }
508
509 } /* unnamed namespace */
510
511 /**
512 * Does a constant propagation pass on the code present in the instruction stream.
513 */
514 bool
515 do_constant_propagation(exec_list *instructions)
516 {
517 ir_constant_propagation_visitor v;
518
519 visit_list_elements(&v, instructions);
520
521 return v.progress;
522 }