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"
43 #include "util/hash_table.h"
47 class acp_entry
: public exec_node
50 acp_entry(ir_variable
*var
, unsigned write_mask
, ir_constant
*constant
)
55 this->write_mask
= write_mask
;
56 this->constant
= constant
;
57 this->initial_values
= write_mask
;
60 acp_entry(const acp_entry
*src
)
63 this->write_mask
= src
->write_mask
;
64 this->constant
= src
->constant
;
65 this->initial_values
= src
->initial_values
;
69 ir_constant
*constant
;
72 /** Mask of values initially available in the constant. */
73 unsigned initial_values
;
77 class kill_entry
: public exec_node
80 kill_entry(ir_variable
*var
, unsigned write_mask
)
84 this->write_mask
= write_mask
;
91 class ir_constant_propagation_visitor
: public ir_rvalue_visitor
{
93 ir_constant_propagation_visitor()
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
);
102 ~ir_constant_propagation_visitor()
104 ralloc_free(mem_ctx
);
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
*);
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
);
121 /** List of acp_entry: The available constants to propagate */
125 * List of kill_entry: The masks of variables whose values were
126 * killed in this block.
139 ir_constant_propagation_visitor::constant_folding(ir_rvalue
**rvalue
) {
141 if (*rvalue
== NULL
|| (*rvalue
)->ir_type
== ir_type_constant
)
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.
148 ir_expression
*expr
= (*rvalue
)->as_expression();
150 for (unsigned int i
= 0; i
< expr
->get_num_operands(); i
++) {
151 if (!expr
->operands
[i
]->as_constant())
156 /* Ditto for swizzles. */
157 ir_swizzle
*swiz
= (*rvalue
)->as_swizzle();
158 if (swiz
&& !swiz
->val
->as_constant())
161 ir_constant
*constant
= (*rvalue
)->constant_expression_value();
164 this->progress
= true;
169 ir_constant_propagation_visitor::constant_propagation(ir_rvalue
**rvalue
) {
171 if (this->in_assignee
|| !*rvalue
)
174 const glsl_type
*type
= (*rvalue
)->type
;
175 if (!type
->is_scalar() && !type
->is_vector())
178 ir_swizzle
*swiz
= NULL
;
179 ir_dereference_variable
*deref
= (*rvalue
)->as_dereference_variable();
181 swiz
= (*rvalue
)->as_swizzle();
185 deref
= swiz
->val
->as_dereference_variable();
190 ir_constant_data data
;
191 memset(&data
, 0, sizeof(data
));
193 for (unsigned int i
= 0; i
< type
->components(); i
++) {
195 acp_entry
*found
= NULL
;
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;
209 foreach_in_list(acp_entry
, entry
, this->acp
) {
210 if (entry
->var
== deref
->var
&& entry
->write_mask
& (1 << channel
)) {
220 for (int j
= 0; j
< 4; j
++) {
223 if (found
->initial_values
& (1 << j
))
227 switch (type
->base_type
) {
228 case GLSL_TYPE_FLOAT
:
229 data
.f
[i
] = found
->constant
->value
.f
[rhs_channel
];
231 case GLSL_TYPE_DOUBLE
:
232 data
.d
[i
] = found
->constant
->value
.d
[rhs_channel
];
235 data
.i
[i
] = found
->constant
->value
.i
[rhs_channel
];
238 data
.u
[i
] = found
->constant
->value
.u
[rhs_channel
];
241 data
.b
[i
] = found
->constant
->value
.b
[rhs_channel
];
244 assert(!"not reached");
249 *rvalue
= new(ralloc_parent(deref
)) ir_constant(type
, &data
);
250 this->progress
= true;
254 ir_constant_propagation_visitor::handle_rvalue(ir_rvalue
**rvalue
)
256 constant_propagation(rvalue
);
257 constant_folding(rvalue
);
261 ir_constant_propagation_visitor::visit_enter(ir_function_signature
*ir
)
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.
267 exec_list
*orig_acp
= this->acp
;
268 hash_table
*orig_kills
= this->kills
;
269 bool orig_killed_all
= this->killed_all
;
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;
276 visit_list_elements(this, &ir
->body
);
278 this->kills
= orig_kills
;
279 this->acp
= orig_acp
;
280 this->killed_all
= orig_killed_all
;
282 return visit_continue_with_parent
;
286 ir_constant_propagation_visitor::visit_leave(ir_assignment
*ir
)
288 constant_folding(&ir
->rhs
);
290 if (this->in_assignee
)
291 return visit_continue
;
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.
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
312 kill(ir
->lhs
->variable_referenced(), kill_mask
);
316 return visit_continue
;
320 ir_constant_propagation_visitor::visit_enter(ir_function
*ir
)
323 return visit_continue
;
327 ir_constant_propagation_visitor::visit_enter(ir_call
*ir
)
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
);
345 /* Since we're unlinked, we don't (necssarily) know the side effects of
346 * this call. So kill all copies.
349 this->killed_all
= true;
351 return visit_continue_with_parent
;
355 ir_constant_propagation_visitor::handle_if_block(exec_list
*instructions
)
357 exec_list
*orig_acp
= this->acp
;
358 hash_table
*orig_kills
= this->kills
;
359 bool orig_killed_all
= this->killed_all
;
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;
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
));
371 visit_list_elements(this, instructions
);
373 if (this->killed_all
) {
374 orig_acp
->make_empty();
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
;
383 hash_table_foreach(new_kills
, htk
) {
384 kill_entry
*k
= (kill_entry
*) htk
->data
;
385 kill(k
->var
, k
->write_mask
);
390 ir_constant_propagation_visitor::visit_enter(ir_if
*ir
)
392 ir
->condition
->accept(this);
393 handle_rvalue(&ir
->condition
);
395 handle_if_block(&ir
->then_instructions
);
396 handle_if_block(&ir
->else_instructions
);
398 /* handle_if_block() already descended into the children. */
399 return visit_continue_with_parent
;
403 ir_constant_propagation_visitor::visit_enter(ir_loop
*ir
)
405 exec_list
*orig_acp
= this->acp
;
406 hash_table
*orig_kills
= this->kills
;
407 bool orig_killed_all
= this->killed_all
;
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.
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;
418 visit_list_elements(this, &ir
->body_instructions
);
420 if (this->killed_all
) {
421 orig_acp
->make_empty();
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
;
430 hash_table_foreach(new_kills
, htk
) {
431 kill_entry
*k
= (kill_entry
*) htk
->data
;
432 kill(k
->var
, k
->write_mask
);
435 /* already descended into the children. */
436 return visit_continue_with_parent
;
440 ir_constant_propagation_visitor::kill(ir_variable
*var
, unsigned write_mask
)
444 /* We don't track non-vectors. */
445 if (!var
->type
->is_vector() && !var
->type
->is_scalar())
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)
457 /* Add this writemask of the variable to the list of killed
458 * variables in this block.
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
;
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
));
472 * Adds an entry to the available constant list if it's a plain assignment
473 * of a variable to a variable.
476 ir_constant_propagation_visitor::add_constant(ir_assignment
*ir
)
486 ir_dereference_variable
*deref
= ir
->lhs
->as_dereference_variable();
487 ir_constant
*constant
= ir
->rhs
->as_constant();
489 if (!deref
|| !constant
)
492 /* Only do constant propagation on vectors. Constant matrices,
493 * arrays, or structures would require more work elsewhere.
495 if (!deref
->var
->type
->is_vector() && !deref
->var
->type
->is_scalar())
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.
503 if (deref
->var
->data
.mode
== ir_var_shader_storage
||
504 deref
->var
->data
.mode
== ir_var_shader_shared
)
507 entry
= new(this->mem_ctx
) acp_entry(deref
->var
, ir
->write_mask
, constant
);
508 this->acp
->push_tail(entry
);
511 } /* unnamed namespace */
514 * Does a constant propagation pass on the code present in the instruction stream.
517 do_constant_propagation(exec_list
*instructions
)
519 ir_constant_propagation_visitor v
;
521 visit_list_elements(&v
, instructions
);