2 * Copyright © 2010 Intel Corporation
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:
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
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.
25 * \file opt_constant_propagation.cpp
27 * Tracks assignments of constants to channels of variables, and
28 * usage of those constant channels with direct usage of the constants.
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.
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"
44 class acp_entry : public exec_node
47 acp_entry(ir_variable *var, unsigned write_mask, ir_constant *constant)
52 this->write_mask = write_mask;
53 this->constant = constant;
54 this->initial_values = write_mask;
57 acp_entry(const acp_entry *src)
60 this->write_mask = src->write_mask;
61 this->constant = src->constant;
62 this->initial_values = src->initial_values;
66 ir_constant *constant;
69 /** Mask of values initially available in the constant. */
70 unsigned initial_values;
74 class kill_entry : public exec_node
77 kill_entry(ir_variable *var, unsigned write_mask)
81 this->write_mask = write_mask;
88 class ir_constant_propagation_visitor : public ir_rvalue_visitor {
90 ir_constant_propagation_visitor()
93 mem_ctx = ralloc_context(0);
94 this->acp = new(mem_ctx) exec_list;
95 this->kills = new(mem_ctx) exec_list;
97 ~ir_constant_propagation_visitor()
102 virtual ir_visitor_status visit_enter(class ir_loop *);
103 virtual ir_visitor_status visit_enter(class ir_function_signature *);
104 virtual ir_visitor_status visit_enter(class ir_function *);
105 virtual ir_visitor_status visit_leave(class ir_assignment *);
106 virtual ir_visitor_status visit_enter(class ir_call *);
107 virtual ir_visitor_status visit_enter(class ir_if *);
109 void add_constant(ir_assignment *ir);
110 void kill(ir_variable *ir, unsigned write_mask);
111 void handle_if_block(exec_list *instructions);
112 void handle_rvalue(ir_rvalue **rvalue);
114 /** List of acp_entry: The available constants to propagate */
118 * List of kill_entry: The masks of variables whose values were
119 * killed in this block.
132 ir_constant_propagation_visitor::handle_rvalue(ir_rvalue **rvalue)
134 if (this->in_assignee || !*rvalue)
137 const glsl_type *type = (*rvalue)->type;
138 if (!type->is_scalar() && !type->is_vector())
141 ir_swizzle *swiz = NULL;
142 ir_dereference_variable *deref = (*rvalue)->as_dereference_variable();
144 swiz = (*rvalue)->as_swizzle();
148 deref = swiz->val->as_dereference_variable();
153 ir_constant_data data;
154 memset(&data, 0, sizeof(data));
156 for (unsigned int i = 0; i < type->components(); i++) {
158 acp_entry *found = NULL;
162 case 0: channel = swiz->mask.x; break;
163 case 1: channel = swiz->mask.y; break;
164 case 2: channel = swiz->mask.z; break;
165 case 3: channel = swiz->mask.w; break;
166 default: assert(!"shouldn't be reached"); channel = 0; break;
172 foreach_iter(exec_list_iterator, iter, *this->acp) {
173 acp_entry *entry = (acp_entry *)iter.get();
174 if (entry->var == deref->var && entry->write_mask & (1 << channel)) {
184 for (int j = 0; j < 4; j++) {
187 if (found->initial_values & (1 << j))
191 switch (type->base_type) {
192 case GLSL_TYPE_FLOAT:
193 data.f[i] = found->constant->value.f[rhs_channel];
196 data.i[i] = found->constant->value.i[rhs_channel];
199 data.u[i] = found->constant->value.u[rhs_channel];
202 data.b[i] = found->constant->value.b[rhs_channel];
205 assert(!"not reached");
210 *rvalue = new(ralloc_parent(deref)) ir_constant(type, &data);
211 this->progress = true;
215 ir_constant_propagation_visitor::visit_enter(ir_function_signature *ir)
217 /* Treat entry into a function signature as a completely separate
218 * block. Any instructions at global scope will be shuffled into
219 * main() at link time, so they're irrelevant to us.
221 exec_list *orig_acp = this->acp;
222 exec_list *orig_kills = this->kills;
223 bool orig_killed_all = this->killed_all;
225 this->acp = new(mem_ctx) exec_list;
226 this->kills = new(mem_ctx) exec_list;
227 this->killed_all = false;
229 visit_list_elements(this, &ir->body);
231 this->kills = orig_kills;
232 this->acp = orig_acp;
233 this->killed_all = orig_killed_all;
235 return visit_continue_with_parent;
239 ir_constant_propagation_visitor::visit_leave(ir_assignment *ir)
241 if (this->in_assignee)
242 return visit_continue;
244 unsigned kill_mask = ir->write_mask;
245 if (ir->lhs->as_dereference_array()) {
246 /* The LHS of the assignment uses an array indexing operator (e.g. v[i]
247 * = ...;). Since we only try to constant propagate vectors and
248 * scalars, this means that either (a) array indexing is being used to
249 * select a vector component, or (b) the variable in question is neither
250 * a scalar or a vector, so we don't care about it. In the former case,
251 * we want to kill the whole vector, since in general we can't predict
252 * which vector component will be selected by array indexing. In the
253 * latter case, it doesn't matter what we do, so go ahead and kill the
254 * whole variable anyway.
256 * Note that if the array index is constant (e.g. v[2] = ...;), we could
257 * in principle be smarter, but we don't need to, because a future
258 * optimization pass will convert it to a simple assignment with the
263 kill(ir->lhs->variable_referenced(), kill_mask);
267 return visit_continue;
271 ir_constant_propagation_visitor::visit_enter(ir_function *ir)
274 return visit_continue;
278 ir_constant_propagation_visitor::visit_enter(ir_call *ir)
280 /* Do constant propagation on call parameters, but skip any out params */
281 exec_list_iterator sig_param_iter = ir->callee->parameters.iterator();
282 foreach_iter(exec_list_iterator, iter, ir->actual_parameters) {
283 ir_variable *sig_param = (ir_variable *)sig_param_iter.get();
284 ir_rvalue *param = (ir_rvalue *)iter.get();
285 if (sig_param->mode != ir_var_out && sig_param->mode != ir_var_inout) {
286 ir_rvalue *new_param = param;
287 handle_rvalue(&new_param);
288 if (new_param != param)
289 param->replace_with(new_param);
293 sig_param_iter.next();
296 /* Since we're unlinked, we don't (necssarily) know the side effects of
297 * this call. So kill all copies.
300 this->killed_all = true;
302 return visit_continue_with_parent;
306 ir_constant_propagation_visitor::handle_if_block(exec_list *instructions)
308 exec_list *orig_acp = this->acp;
309 exec_list *orig_kills = this->kills;
310 bool orig_killed_all = this->killed_all;
312 this->acp = new(mem_ctx) exec_list;
313 this->kills = new(mem_ctx) exec_list;
314 this->killed_all = false;
316 /* Populate the initial acp with a constant of the original */
317 foreach_iter(exec_list_iterator, iter, *orig_acp) {
318 acp_entry *a = (acp_entry *)iter.get();
319 this->acp->push_tail(new(this->mem_ctx) acp_entry(a));
322 visit_list_elements(this, instructions);
324 if (this->killed_all) {
325 orig_acp->make_empty();
328 exec_list *new_kills = this->kills;
329 this->kills = orig_kills;
330 this->acp = orig_acp;
331 this->killed_all = this->killed_all || orig_killed_all;
333 foreach_iter(exec_list_iterator, iter, *new_kills) {
334 kill_entry *k = (kill_entry *)iter.get();
335 kill(k->var, k->write_mask);
340 ir_constant_propagation_visitor::visit_enter(ir_if *ir)
342 ir->condition->accept(this);
343 handle_rvalue(&ir->condition);
345 handle_if_block(&ir->then_instructions);
346 handle_if_block(&ir->else_instructions);
348 /* handle_if_block() already descended into the children. */
349 return visit_continue_with_parent;
353 ir_constant_propagation_visitor::visit_enter(ir_loop *ir)
355 exec_list *orig_acp = this->acp;
356 exec_list *orig_kills = this->kills;
357 bool orig_killed_all = this->killed_all;
359 /* FINISHME: For now, the initial acp for loops is totally empty.
360 * We could go through once, then go through again with the acp
361 * cloned minus the killed entries after the first run through.
363 this->acp = new(mem_ctx) exec_list;
364 this->kills = new(mem_ctx) exec_list;
365 this->killed_all = false;
367 visit_list_elements(this, &ir->body_instructions);
369 if (this->killed_all) {
370 orig_acp->make_empty();
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;
378 foreach_iter(exec_list_iterator, iter, *new_kills) {
379 kill_entry *k = (kill_entry *)iter.get();
380 kill(k->var, k->write_mask);
383 /* already descended into the children. */
384 return visit_continue_with_parent;
388 ir_constant_propagation_visitor::kill(ir_variable *var, unsigned write_mask)
392 /* We don't track non-vectors. */
393 if (!var->type->is_vector() && !var->type->is_scalar())
396 /* Remove any entries currently in the ACP for this kill. */
397 foreach_iter(exec_list_iterator, iter, *this->acp) {
398 acp_entry *entry = (acp_entry *)iter.get();
400 if (entry->var == var) {
401 entry->write_mask &= ~write_mask;
402 if (entry->write_mask == 0)
407 /* Add this writemask of the variable to the list of killed
408 * variables in this block.
410 foreach_iter(exec_list_iterator, iter, *this->kills) {
411 kill_entry *entry = (kill_entry *)iter.get();
413 if (entry->var == var) {
414 entry->write_mask |= write_mask;
418 /* Not already in the list. Make new entry. */
419 this->kills->push_tail(new(this->mem_ctx) kill_entry(var, write_mask));
423 * Adds an entry to the available constant list if it's a plain assignment
424 * of a variable to a variable.
427 ir_constant_propagation_visitor::add_constant(ir_assignment *ir)
437 ir_dereference_variable *deref = ir->lhs->as_dereference_variable();
438 ir_constant *constant = ir->rhs->as_constant();
440 if (!deref || !constant)
443 /* Only do constant propagation on vectors. Constant matrices,
444 * arrays, or structures would require more work elsewhere.
446 if (!deref->var->type->is_vector() && !deref->var->type->is_scalar())
449 entry = new(this->mem_ctx) acp_entry(deref->var, ir->write_mask, constant);
450 this->acp->push_tail(entry);
454 * Does a constant propagation pass on the code present in the instruction stream.
457 do_constant_propagation(exec_list *instructions)
459 ir_constant_propagation_visitor v;
461 visit_list_elements(&v, instructions);