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"
46 class acp_entry
: public exec_node
49 acp_entry(ir_variable
*var
, unsigned write_mask
, ir_constant
*constant
)
54 this->write_mask
= write_mask
;
55 this->constant
= constant
;
56 this->initial_values
= write_mask
;
59 acp_entry(const acp_entry
*src
)
62 this->write_mask
= src
->write_mask
;
63 this->constant
= src
->constant
;
64 this->initial_values
= src
->initial_values
;
68 ir_constant
*constant
;
71 /** Mask of values initially available in the constant. */
72 unsigned initial_values
;
76 class kill_entry
: public exec_node
79 kill_entry(ir_variable
*var
, unsigned write_mask
)
83 this->write_mask
= write_mask
;
90 class ir_constant_propagation_visitor
: public ir_rvalue_visitor
{
92 ir_constant_propagation_visitor()
96 mem_ctx
= ralloc_context(0);
97 this->acp
= new(mem_ctx
) exec_list
;
98 this->kills
= new(mem_ctx
) exec_list
;
100 ~ir_constant_propagation_visitor()
102 ralloc_free(mem_ctx
);
105 virtual ir_visitor_status
visit_enter(class ir_loop
*);
106 virtual ir_visitor_status
visit_enter(class ir_function_signature
*);
107 virtual ir_visitor_status
visit_enter(class ir_function
*);
108 virtual ir_visitor_status
visit_leave(class ir_assignment
*);
109 virtual ir_visitor_status
visit_enter(class ir_call
*);
110 virtual ir_visitor_status
visit_enter(class ir_if
*);
112 void add_constant(ir_assignment
*ir
);
113 void kill(ir_variable
*ir
, unsigned write_mask
);
114 void handle_if_block(exec_list
*instructions
);
115 void handle_rvalue(ir_rvalue
**rvalue
);
117 /** List of acp_entry: The available constants to propagate */
121 * List of kill_entry: The masks of variables whose values were
122 * killed in this block.
135 ir_constant_propagation_visitor::handle_rvalue(ir_rvalue
**rvalue
)
137 if (this->in_assignee
|| !*rvalue
)
140 const glsl_type
*type
= (*rvalue
)->type
;
141 if (!type
->is_scalar() && !type
->is_vector())
144 ir_swizzle
*swiz
= NULL
;
145 ir_dereference_variable
*deref
= (*rvalue
)->as_dereference_variable();
147 swiz
= (*rvalue
)->as_swizzle();
151 deref
= swiz
->val
->as_dereference_variable();
156 ir_constant_data data
;
157 memset(&data
, 0, sizeof(data
));
159 for (unsigned int i
= 0; i
< type
->components(); i
++) {
161 acp_entry
*found
= NULL
;
165 case 0: channel
= swiz
->mask
.x
; break;
166 case 1: channel
= swiz
->mask
.y
; break;
167 case 2: channel
= swiz
->mask
.z
; break;
168 case 3: channel
= swiz
->mask
.w
; break;
169 default: assert(!"shouldn't be reached"); channel
= 0; break;
175 foreach_iter(exec_list_iterator
, iter
, *this->acp
) {
176 acp_entry
*entry
= (acp_entry
*)iter
.get();
177 if (entry
->var
== deref
->var
&& entry
->write_mask
& (1 << channel
)) {
187 for (int j
= 0; j
< 4; j
++) {
190 if (found
->initial_values
& (1 << j
))
194 switch (type
->base_type
) {
195 case GLSL_TYPE_FLOAT
:
196 data
.f
[i
] = found
->constant
->value
.f
[rhs_channel
];
199 data
.i
[i
] = found
->constant
->value
.i
[rhs_channel
];
202 data
.u
[i
] = found
->constant
->value
.u
[rhs_channel
];
205 data
.b
[i
] = found
->constant
->value
.b
[rhs_channel
];
208 assert(!"not reached");
213 *rvalue
= new(ralloc_parent(deref
)) ir_constant(type
, &data
);
214 this->progress
= true;
218 ir_constant_propagation_visitor::visit_enter(ir_function_signature
*ir
)
220 /* Treat entry into a function signature as a completely separate
221 * block. Any instructions at global scope will be shuffled into
222 * main() at link time, so they're irrelevant to us.
224 exec_list
*orig_acp
= this->acp
;
225 exec_list
*orig_kills
= this->kills
;
226 bool orig_killed_all
= this->killed_all
;
228 this->acp
= new(mem_ctx
) exec_list
;
229 this->kills
= new(mem_ctx
) exec_list
;
230 this->killed_all
= false;
232 visit_list_elements(this, &ir
->body
);
234 this->kills
= orig_kills
;
235 this->acp
= orig_acp
;
236 this->killed_all
= orig_killed_all
;
238 return visit_continue_with_parent
;
242 ir_constant_propagation_visitor::visit_leave(ir_assignment
*ir
)
244 if (this->in_assignee
)
245 return visit_continue
;
247 unsigned kill_mask
= ir
->write_mask
;
248 if (ir
->lhs
->as_dereference_array()) {
249 /* The LHS of the assignment uses an array indexing operator (e.g. v[i]
250 * = ...;). Since we only try to constant propagate vectors and
251 * scalars, this means that either (a) array indexing is being used to
252 * select a vector component, or (b) the variable in question is neither
253 * a scalar or a vector, so we don't care about it. In the former case,
254 * we want to kill the whole vector, since in general we can't predict
255 * which vector component will be selected by array indexing. In the
256 * latter case, it doesn't matter what we do, so go ahead and kill the
257 * whole variable anyway.
259 * Note that if the array index is constant (e.g. v[2] = ...;), we could
260 * in principle be smarter, but we don't need to, because a future
261 * optimization pass will convert it to a simple assignment with the
266 kill(ir
->lhs
->variable_referenced(), kill_mask
);
270 return visit_continue
;
274 ir_constant_propagation_visitor::visit_enter(ir_function
*ir
)
277 return visit_continue
;
281 ir_constant_propagation_visitor::visit_enter(ir_call
*ir
)
283 /* Do constant propagation on call parameters, but skip any out params */
284 exec_list_iterator sig_param_iter
= ir
->callee
->parameters
.iterator();
285 foreach_iter(exec_list_iterator
, iter
, ir
->actual_parameters
) {
286 ir_variable
*sig_param
= (ir_variable
*)sig_param_iter
.get();
287 ir_rvalue
*param
= (ir_rvalue
*)iter
.get();
288 if (sig_param
->mode
!= ir_var_function_out
289 && sig_param
->mode
!= ir_var_function_inout
) {
290 ir_rvalue
*new_param
= param
;
291 handle_rvalue(&new_param
);
292 if (new_param
!= param
)
293 param
->replace_with(new_param
);
297 sig_param_iter
.next();
300 /* Since we're unlinked, we don't (necssarily) know the side effects of
301 * this call. So kill all copies.
304 this->killed_all
= true;
306 return visit_continue_with_parent
;
310 ir_constant_propagation_visitor::handle_if_block(exec_list
*instructions
)
312 exec_list
*orig_acp
= this->acp
;
313 exec_list
*orig_kills
= this->kills
;
314 bool orig_killed_all
= this->killed_all
;
316 this->acp
= new(mem_ctx
) exec_list
;
317 this->kills
= new(mem_ctx
) exec_list
;
318 this->killed_all
= false;
320 /* Populate the initial acp with a constant of the original */
321 foreach_iter(exec_list_iterator
, iter
, *orig_acp
) {
322 acp_entry
*a
= (acp_entry
*)iter
.get();
323 this->acp
->push_tail(new(this->mem_ctx
) acp_entry(a
));
326 visit_list_elements(this, instructions
);
328 if (this->killed_all
) {
329 orig_acp
->make_empty();
332 exec_list
*new_kills
= this->kills
;
333 this->kills
= orig_kills
;
334 this->acp
= orig_acp
;
335 this->killed_all
= this->killed_all
|| orig_killed_all
;
337 foreach_iter(exec_list_iterator
, iter
, *new_kills
) {
338 kill_entry
*k
= (kill_entry
*)iter
.get();
339 kill(k
->var
, k
->write_mask
);
344 ir_constant_propagation_visitor::visit_enter(ir_if
*ir
)
346 ir
->condition
->accept(this);
347 handle_rvalue(&ir
->condition
);
349 handle_if_block(&ir
->then_instructions
);
350 handle_if_block(&ir
->else_instructions
);
352 /* handle_if_block() already descended into the children. */
353 return visit_continue_with_parent
;
357 ir_constant_propagation_visitor::visit_enter(ir_loop
*ir
)
359 exec_list
*orig_acp
= this->acp
;
360 exec_list
*orig_kills
= this->kills
;
361 bool orig_killed_all
= this->killed_all
;
363 /* FINISHME: For now, the initial acp for loops is totally empty.
364 * We could go through once, then go through again with the acp
365 * cloned minus the killed entries after the first run through.
367 this->acp
= new(mem_ctx
) exec_list
;
368 this->kills
= new(mem_ctx
) exec_list
;
369 this->killed_all
= false;
371 visit_list_elements(this, &ir
->body_instructions
);
373 if (this->killed_all
) {
374 orig_acp
->make_empty();
377 exec_list
*new_kills
= this->kills
;
378 this->kills
= orig_kills
;
379 this->acp
= orig_acp
;
380 this->killed_all
= this->killed_all
|| orig_killed_all
;
382 foreach_iter(exec_list_iterator
, iter
, *new_kills
) {
383 kill_entry
*k
= (kill_entry
*)iter
.get();
384 kill(k
->var
, k
->write_mask
);
387 /* already descended into the children. */
388 return visit_continue_with_parent
;
392 ir_constant_propagation_visitor::kill(ir_variable
*var
, unsigned write_mask
)
396 /* We don't track non-vectors. */
397 if (!var
->type
->is_vector() && !var
->type
->is_scalar())
400 /* Remove any entries currently in the ACP for this kill. */
401 foreach_iter(exec_list_iterator
, iter
, *this->acp
) {
402 acp_entry
*entry
= (acp_entry
*)iter
.get();
404 if (entry
->var
== var
) {
405 entry
->write_mask
&= ~write_mask
;
406 if (entry
->write_mask
== 0)
411 /* Add this writemask of the variable to the list of killed
412 * variables in this block.
414 foreach_iter(exec_list_iterator
, iter
, *this->kills
) {
415 kill_entry
*entry
= (kill_entry
*)iter
.get();
417 if (entry
->var
== var
) {
418 entry
->write_mask
|= write_mask
;
422 /* Not already in the list. Make new entry. */
423 this->kills
->push_tail(new(this->mem_ctx
) kill_entry(var
, write_mask
));
427 * Adds an entry to the available constant list if it's a plain assignment
428 * of a variable to a variable.
431 ir_constant_propagation_visitor::add_constant(ir_assignment
*ir
)
441 ir_dereference_variable
*deref
= ir
->lhs
->as_dereference_variable();
442 ir_constant
*constant
= ir
->rhs
->as_constant();
444 if (!deref
|| !constant
)
447 /* Only do constant propagation on vectors. Constant matrices,
448 * arrays, or structures would require more work elsewhere.
450 if (!deref
->var
->type
->is_vector() && !deref
->var
->type
->is_scalar())
453 entry
= new(this->mem_ctx
) acp_entry(deref
->var
, ir
->write_mask
, constant
);
454 this->acp
->push_tail(entry
);
457 } /* unnamed namespace */
460 * Does a constant propagation pass on the code present in the instruction stream.
463 do_constant_propagation(exec_list
*instructions
)
465 ir_constant_propagation_visitor v
;
467 visit_list_elements(&v
, instructions
);