2 * Copyright © 2010 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy 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, copy, 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 copyright 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 COPYRIGHT 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 ir_dead_code_local.cpp
27 * Eliminates local dead assignments from the code.
29 * This operates on basic blocks, tracking assignments and finding if
30 * they're used before the variable is completely reassigned.
32 * Compare this to ir_dead_code.cpp, which operates globally looking
33 * for assignments to variables that are never read.
38 #include "ir_print_visitor.h"
39 #include "ir_basic_block.h"
40 #include "ir_optimization.h"
41 #include "glsl_types.h"
43 static bool debug
= false;
45 class assignment_entry
: public exec_node
48 assignment_entry(ir_variable
*lhs
, ir_instruction
*ir
)
60 class kill_for_derefs_visitor
: public ir_hierarchical_visitor
{
62 kill_for_derefs_visitor(exec_list
*assignments
)
64 this->assignments
= assignments
;
67 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
69 ir_variable
*const var
= ir
->variable_referenced();
71 foreach_iter(exec_list_iterator
, iter
, *this->assignments
) {
72 assignment_entry
*entry
= (assignment_entry
*)iter
.get();
74 if (entry
->lhs
== var
) {
76 printf("kill %s\n", entry
->lhs
->name
);
81 return visit_continue
;
85 exec_list
*assignments
;
88 class array_index_visit
: public ir_hierarchical_visitor
{
90 array_index_visit(ir_hierarchical_visitor
*v
)
95 virtual ir_visitor_status
visit_enter(class ir_dereference_array
*ir
)
97 ir
->array_index
->accept(visitor
);
98 return visit_continue
;
101 static void run(ir_instruction
*ir
, ir_hierarchical_visitor
*v
)
103 array_index_visit
top_visit(v
);
104 ir
->accept(& top_visit
);
107 ir_hierarchical_visitor
*visitor
;
112 * Adds an entry to the available copy list if it's a plain assignment
113 * of a variable to a variable.
116 process_assignment(ir_assignment
*ir
, exec_list
*assignments
)
118 ir_variable
*var
= NULL
;
119 bool progress
= false;
120 kill_for_derefs_visitor
v(assignments
);
122 /* Kill assignment entries for things used to produce this assignment. */
125 ir
->condition
->accept(&v
);
128 /* Kill assignment enties used as array indices.
130 array_index_visit::run(ir
->lhs
, &v
);
131 var
= ir
->lhs
->variable_referenced();
134 bool always_assign
= true;
136 ir_constant
*condition
= ir
->condition
->as_constant();
137 if (!condition
|| !condition
->value
.b
[0])
138 always_assign
= false;
141 /* Now, check if we did a whole-variable assignment. */
142 if (always_assign
&& (ir
->lhs
->whole_variable_referenced() != NULL
)) {
143 /* We did a whole-variable assignment. So, any instruction in
144 * the assignment list with the same LHS is dead.
147 printf("looking for %s to remove\n", var
->name
);
148 foreach_iter(exec_list_iterator
, iter
, *assignments
) {
149 assignment_entry
*entry
= (assignment_entry
*)iter
.get();
151 if (entry
->lhs
== var
) {
153 printf("removing %s\n", var
->name
);
161 /* Add this instruction to the assignment list. */
162 assignment_entry
*entry
= new assignment_entry(var
, ir
);
163 assignments
->push_tail(entry
);
166 printf("add %s\n", var
->name
);
168 printf("current entries\n");
169 foreach_iter(exec_list_iterator
, iter
, *assignments
) {
170 assignment_entry
*entry
= (assignment_entry
*)iter
.get();
172 printf(" %s\n", entry
->lhs
->name
);
180 dead_code_local_basic_block(ir_instruction
*first
,
181 ir_instruction
*last
,
184 ir_instruction
*ir
, *ir_next
;
185 /* List of avaialble_copy */
186 exec_list assignments
;
187 bool *out_progress
= (bool *)data
;
188 bool progress
= false;
190 /* Safe looping, since process_assignment */
191 for (ir
= first
, ir_next
= (ir_instruction
*)first
->next
;;
192 ir
= ir_next
, ir_next
= (ir_instruction
*)ir
->next
) {
193 ir_assignment
*ir_assign
= ir
->as_assignment();
202 progress
= process_assignment(ir_assign
, &assignments
) || progress
;
204 kill_for_derefs_visitor
kill(&assignments
);
211 *out_progress
= progress
;
215 * Does a copy propagation pass on the code present in the instruction stream.
218 do_dead_code_local(exec_list
*instructions
)
220 bool progress
= false;
222 call_for_basic_blocks(instructions
, dead_code_local_basic_block
, &progress
);