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