nir: move to compiler/
[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 "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 * List 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 (*rvalue == NULL || (*rvalue)->ir_type == ir_type_constant)
142 return;
143
144 /* Note that we visit rvalues one leaving. So if an expression has a
145 * non-constant operand, no need to go looking down it to find if it's
146 * constant. This cuts the time of this pass down drastically.
147 */
148 ir_expression *expr = (*rvalue)->as_expression();
149 if (expr) {
150 for (unsigned int i = 0; i < expr->get_num_operands(); i++) {
151 if (!expr->operands[i]->as_constant())
152 return;
153 }
154 }
155
156 /* Ditto for swizzles. */
157 ir_swizzle *swiz = (*rvalue)->as_swizzle();
158 if (swiz && !swiz->val->as_constant())
159 return;
160
161 ir_constant *constant = (*rvalue)->constant_expression_value();
162 if (constant) {
163 *rvalue = constant;
164 this->progress = true;
165 }
166 }
167
168 void
169 ir_constant_propagation_visitor::constant_propagation(ir_rvalue **rvalue) {
170
171 if (this->in_assignee || !*rvalue)
172 return;
173
174 const glsl_type *type = (*rvalue)->type;
175 if (!type->is_scalar() && !type->is_vector())
176 return;
177
178 ir_swizzle *swiz = NULL;
179 ir_dereference_variable *deref = (*rvalue)->as_dereference_variable();
180 if (!deref) {
181 swiz = (*rvalue)->as_swizzle();
182 if (!swiz)
183 return;
184
185 deref = swiz->val->as_dereference_variable();
186 if (!deref)
187 return;
188 }
189
190 ir_constant_data data;
191 memset(&data, 0, sizeof(data));
192
193 for (unsigned int i = 0; i < type->components(); i++) {
194 int channel;
195 acp_entry *found = NULL;
196
197 if (swiz) {
198 switch (i) {
199 case 0: channel = swiz->mask.x; break;
200 case 1: channel = swiz->mask.y; break;
201 case 2: channel = swiz->mask.z; break;
202 case 3: channel = swiz->mask.w; break;
203 default: assert(!"shouldn't be reached"); channel = 0; break;
204 }
205 } else {
206 channel = i;
207 }
208
209 foreach_in_list(acp_entry, entry, this->acp) {
210 if (entry->var == deref->var && entry->write_mask & (1 << channel)) {
211 found = entry;
212 break;
213 }
214 }
215
216 if (!found)
217 return;
218
219 int rhs_channel = 0;
220 for (int j = 0; j < 4; j++) {
221 if (j == channel)
222 break;
223 if (found->initial_values & (1 << j))
224 rhs_channel++;
225 }
226
227 switch (type->base_type) {
228 case GLSL_TYPE_FLOAT:
229 data.f[i] = found->constant->value.f[rhs_channel];
230 break;
231 case GLSL_TYPE_DOUBLE:
232 data.d[i] = found->constant->value.d[rhs_channel];
233 break;
234 case GLSL_TYPE_INT:
235 data.i[i] = found->constant->value.i[rhs_channel];
236 break;
237 case GLSL_TYPE_UINT:
238 data.u[i] = found->constant->value.u[rhs_channel];
239 break;
240 case GLSL_TYPE_BOOL:
241 data.b[i] = found->constant->value.b[rhs_channel];
242 break;
243 default:
244 assert(!"not reached");
245 break;
246 }
247 }
248
249 *rvalue = new(ralloc_parent(deref)) ir_constant(type, &data);
250 this->progress = true;
251 }
252
253 void
254 ir_constant_propagation_visitor::handle_rvalue(ir_rvalue **rvalue)
255 {
256 constant_propagation(rvalue);
257 constant_folding(rvalue);
258 }
259
260 ir_visitor_status
261 ir_constant_propagation_visitor::visit_enter(ir_function_signature *ir)
262 {
263 /* Treat entry into a function signature as a completely separate
264 * block. Any instructions at global scope will be shuffled into
265 * main() at link time, so they're irrelevant to us.
266 */
267 exec_list *orig_acp = this->acp;
268 hash_table *orig_kills = this->kills;
269 bool orig_killed_all = this->killed_all;
270
271 this->acp = new(mem_ctx) exec_list;
272 this->kills = _mesa_hash_table_create(mem_ctx, _mesa_hash_pointer,
273 _mesa_key_pointer_equal);
274 this->killed_all = false;
275
276 visit_list_elements(this, &ir->body);
277
278 this->kills = orig_kills;
279 this->acp = orig_acp;
280 this->killed_all = orig_killed_all;
281
282 return visit_continue_with_parent;
283 }
284
285 ir_visitor_status
286 ir_constant_propagation_visitor::visit_leave(ir_assignment *ir)
287 {
288 constant_folding(&ir->rhs);
289
290 if (this->in_assignee)
291 return visit_continue;
292
293 unsigned kill_mask = ir->write_mask;
294 if (ir->lhs->as_dereference_array()) {
295 /* The LHS of the assignment uses an array indexing operator (e.g. v[i]
296 * = ...;). Since we only try to constant propagate vectors and
297 * scalars, this means that either (a) array indexing is being used to
298 * select a vector component, or (b) the variable in question is neither
299 * a scalar or a vector, so we don't care about it. In the former case,
300 * we want to kill the whole vector, since in general we can't predict
301 * which vector component will be selected by array indexing. In the
302 * latter case, it doesn't matter what we do, so go ahead and kill the
303 * whole variable anyway.
304 *
305 * Note that if the array index is constant (e.g. v[2] = ...;), we could
306 * in principle be smarter, but we don't need to, because a future
307 * optimization pass will convert it to a simple assignment with the
308 * correct mask.
309 */
310 kill_mask = ~0;
311 }
312 kill(ir->lhs->variable_referenced(), kill_mask);
313
314 add_constant(ir);
315
316 return visit_continue;
317 }
318
319 ir_visitor_status
320 ir_constant_propagation_visitor::visit_enter(ir_function *ir)
321 {
322 (void) ir;
323 return visit_continue;
324 }
325
326 ir_visitor_status
327 ir_constant_propagation_visitor::visit_enter(ir_call *ir)
328 {
329 /* Do constant propagation on call parameters, but skip any out params */
330 foreach_two_lists(formal_node, &ir->callee->parameters,
331 actual_node, &ir->actual_parameters) {
332 ir_variable *sig_param = (ir_variable *) formal_node;
333 ir_rvalue *param = (ir_rvalue *) actual_node;
334 if (sig_param->data.mode != ir_var_function_out
335 && sig_param->data.mode != ir_var_function_inout) {
336 ir_rvalue *new_param = param;
337 handle_rvalue(&new_param);
338 if (new_param != param)
339 param->replace_with(new_param);
340 else
341 param->accept(this);
342 }
343 }
344
345 /* Since we're unlinked, we don't (necssarily) know the side effects of
346 * this call. So kill all copies.
347 */
348 acp->make_empty();
349 this->killed_all = true;
350
351 return visit_continue_with_parent;
352 }
353
354 void
355 ir_constant_propagation_visitor::handle_if_block(exec_list *instructions)
356 {
357 exec_list *orig_acp = this->acp;
358 hash_table *orig_kills = this->kills;
359 bool orig_killed_all = this->killed_all;
360
361 this->acp = new(mem_ctx) exec_list;
362 this->kills = _mesa_hash_table_create(mem_ctx, _mesa_hash_pointer,
363 _mesa_key_pointer_equal);
364 this->killed_all = false;
365
366 /* Populate the initial acp with a constant of the original */
367 foreach_in_list(acp_entry, a, orig_acp) {
368 this->acp->push_tail(new(this->mem_ctx) acp_entry(a));
369 }
370
371 visit_list_elements(this, instructions);
372
373 if (this->killed_all) {
374 orig_acp->make_empty();
375 }
376
377 hash_table *new_kills = this->kills;
378 this->kills = orig_kills;
379 this->acp = orig_acp;
380 this->killed_all = this->killed_all || orig_killed_all;
381
382 hash_entry *htk;
383 hash_table_foreach(new_kills, htk) {
384 kill_entry *k = (kill_entry *) htk->data;
385 kill(k->var, k->write_mask);
386 }
387 }
388
389 ir_visitor_status
390 ir_constant_propagation_visitor::visit_enter(ir_if *ir)
391 {
392 ir->condition->accept(this);
393 handle_rvalue(&ir->condition);
394
395 handle_if_block(&ir->then_instructions);
396 handle_if_block(&ir->else_instructions);
397
398 /* handle_if_block() already descended into the children. */
399 return visit_continue_with_parent;
400 }
401
402 ir_visitor_status
403 ir_constant_propagation_visitor::visit_enter(ir_loop *ir)
404 {
405 exec_list *orig_acp = this->acp;
406 hash_table *orig_kills = this->kills;
407 bool orig_killed_all = this->killed_all;
408
409 /* FINISHME: For now, the initial acp for loops is totally empty.
410 * We could go through once, then go through again with the acp
411 * cloned minus the killed entries after the first run through.
412 */
413 this->acp = new(mem_ctx) exec_list;
414 this->kills = _mesa_hash_table_create(mem_ctx, _mesa_hash_pointer,
415 _mesa_key_pointer_equal);
416 this->killed_all = false;
417
418 visit_list_elements(this, &ir->body_instructions);
419
420 if (this->killed_all) {
421 orig_acp->make_empty();
422 }
423
424 hash_table *new_kills = this->kills;
425 this->kills = orig_kills;
426 this->acp = orig_acp;
427 this->killed_all = this->killed_all || orig_killed_all;
428
429 hash_entry *htk;
430 hash_table_foreach(new_kills, htk) {
431 kill_entry *k = (kill_entry *) htk->data;
432 kill(k->var, k->write_mask);
433 }
434
435 /* already descended into the children. */
436 return visit_continue_with_parent;
437 }
438
439 void
440 ir_constant_propagation_visitor::kill(ir_variable *var, unsigned write_mask)
441 {
442 assert(var != NULL);
443
444 /* We don't track non-vectors. */
445 if (!var->type->is_vector() && !var->type->is_scalar())
446 return;
447
448 /* Remove any entries currently in the ACP for this kill. */
449 foreach_in_list_safe(acp_entry, entry, this->acp) {
450 if (entry->var == var) {
451 entry->write_mask &= ~write_mask;
452 if (entry->write_mask == 0)
453 entry->remove();
454 }
455 }
456
457 /* Add this writemask of the variable to the list of killed
458 * variables in this block.
459 */
460 hash_entry *kill_hash_entry = _mesa_hash_table_search(this->kills, var);
461 if (kill_hash_entry) {
462 kill_entry *entry = (kill_entry *) kill_hash_entry->data;
463 entry->write_mask |= write_mask;
464 return;
465 }
466 /* Not already in the list. Make new entry. */
467 _mesa_hash_table_insert(this->kills, var,
468 new(this->mem_ctx) kill_entry(var, write_mask));
469 }
470
471 /**
472 * Adds an entry to the available constant list if it's a plain assignment
473 * of a variable to a variable.
474 */
475 void
476 ir_constant_propagation_visitor::add_constant(ir_assignment *ir)
477 {
478 acp_entry *entry;
479
480 if (ir->condition)
481 return;
482
483 if (!ir->write_mask)
484 return;
485
486 ir_dereference_variable *deref = ir->lhs->as_dereference_variable();
487 ir_constant *constant = ir->rhs->as_constant();
488
489 if (!deref || !constant)
490 return;
491
492 /* Only do constant propagation on vectors. Constant matrices,
493 * arrays, or structures would require more work elsewhere.
494 */
495 if (!deref->var->type->is_vector() && !deref->var->type->is_scalar())
496 return;
497
498 /* We can't do copy propagation on buffer variables, since the underlying
499 * memory storage is shared across multiple threads we can't be sure that
500 * the variable value isn't modified between this assignment and the next
501 * instruction where its value is read.
502 */
503 if (deref->var->data.mode == ir_var_shader_storage ||
504 deref->var->data.mode == ir_var_shader_shared)
505 return;
506
507 entry = new(this->mem_ctx) acp_entry(deref->var, ir->write_mask, constant);
508 this->acp->push_tail(entry);
509 }
510
511 } /* unnamed namespace */
512
513 /**
514 * Does a constant propagation pass on the code present in the instruction stream.
515 */
516 bool
517 do_constant_propagation(exec_list *instructions)
518 {
519 ir_constant_propagation_visitor v;
520
521 visit_list_elements(&v, instructions);
522
523 return v.progress;
524 }