Include stdio.h and stdlib.h everywhere, and don't cook our own #define NULL.
[mesa.git] / ir_dead_code_local.cpp
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
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:
10 *
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
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 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.
22 */
23
24 /**
25 * \file ir_dead_code_local.cpp
26 *
27 * Eliminates local dead assignments from the code.
28 *
29 * This operates on basic blocks, tracking assignments and finding if
30 * they're used before the variable is completely reassigned.
31 *
32 * Compare this to ir_dead_code.cpp, which operates globally looking
33 * for assignments to variables that are never read.
34 */
35
36 #include "ir.h"
37 #include "ir_print_visitor.h"
38 #include "ir_basic_block.h"
39 #include "ir_optimization.h"
40 #include "glsl_types.h"
41
42 static bool debug = false;
43
44 class assignment_entry : public exec_node
45 {
46 public:
47 assignment_entry(ir_variable *lhs, ir_instruction *ir)
48 {
49 assert(lhs);
50 assert(ir);
51 this->lhs = lhs;
52 this->ir = ir;
53 }
54
55 ir_variable *lhs;
56 ir_instruction *ir;
57 };
58
59 class kill_for_derefs_visitor : public ir_hierarchical_visitor {
60 public:
61 kill_for_derefs_visitor(exec_list *assignments)
62 {
63 this->assignments = assignments;
64 }
65
66 virtual ir_visitor_status visit(ir_dereference_variable *ir)
67 {
68 ir_variable *const var = ir->variable_referenced();
69
70 foreach_iter(exec_list_iterator, iter, *this->assignments) {
71 assignment_entry *entry = (assignment_entry *)iter.get();
72
73 if (entry->lhs == var) {
74 if (debug)
75 printf("kill %s\n", entry->lhs->name);
76 entry->remove();
77 }
78 }
79
80 return visit_continue;
81 }
82
83 private:
84 exec_list *assignments;
85 };
86
87 class array_index_visit : public ir_hierarchical_visitor {
88 public:
89 array_index_visit(ir_hierarchical_visitor *v)
90 {
91 this->visitor = v;
92 }
93
94 virtual ir_visitor_status visit_enter(class ir_dereference_array *ir)
95 {
96 ir->array_index->accept(visitor);
97 return visit_continue;
98 }
99
100 static void run(ir_instruction *ir, ir_hierarchical_visitor *v)
101 {
102 array_index_visit top_visit(v);
103 ir->accept(& top_visit);
104 }
105
106 ir_hierarchical_visitor *visitor;
107 };
108
109
110 /**
111 * Adds an entry to the available copy list if it's a plain assignment
112 * of a variable to a variable.
113 */
114 static bool
115 process_assignment(ir_assignment *ir, exec_list *assignments)
116 {
117 ir_variable *var = NULL;
118 bool progress = false;
119 kill_for_derefs_visitor v(assignments);
120
121 /* Kill assignment entries for things used to produce this assignment. */
122 ir->rhs->accept(&v);
123 if (ir->condition) {
124 ir->condition->accept(&v);
125 }
126
127 /* Kill assignment enties used as array indices.
128 */
129 array_index_visit::run(ir->lhs, &v);
130 var = ir->lhs->variable_referenced();
131 assert(var);
132
133 bool always_assign = true;
134 if (ir->condition) {
135 ir_constant *condition = ir->condition->as_constant();
136 if (!condition || !condition->value.b[0])
137 always_assign = false;
138 }
139
140 /* Now, check if we did a whole-variable assignment. */
141 if (always_assign && (ir->lhs->whole_variable_referenced() != NULL)) {
142 /* We did a whole-variable assignment. So, any instruction in
143 * the assignment list with the same LHS is dead.
144 */
145 if (debug)
146 printf("looking for %s to remove\n", var->name);
147 foreach_iter(exec_list_iterator, iter, *assignments) {
148 assignment_entry *entry = (assignment_entry *)iter.get();
149
150 if (entry->lhs == var) {
151 if (debug)
152 printf("removing %s\n", var->name);
153 entry->ir->remove();
154 entry->remove();
155 progress = true;
156 }
157 }
158 }
159
160 /* Add this instruction to the assignment list. */
161 assignment_entry *entry = new assignment_entry(var, ir);
162 assignments->push_tail(entry);
163
164 if (debug) {
165 printf("add %s\n", var->name);
166
167 printf("current entries\n");
168 foreach_iter(exec_list_iterator, iter, *assignments) {
169 assignment_entry *entry = (assignment_entry *)iter.get();
170
171 printf(" %s\n", entry->lhs->name);
172 }
173 }
174
175 return progress;
176 }
177
178 static void
179 dead_code_local_basic_block(ir_instruction *first,
180 ir_instruction *last,
181 void *data)
182 {
183 ir_instruction *ir, *ir_next;
184 /* List of avaialble_copy */
185 exec_list assignments;
186 bool *out_progress = (bool *)data;
187 bool progress = false;
188
189 /* Safe looping, since process_assignment */
190 for (ir = first, ir_next = (ir_instruction *)first->next;;
191 ir = ir_next, ir_next = (ir_instruction *)ir->next) {
192 ir_assignment *ir_assign = ir->as_assignment();
193
194 if (debug) {
195 ir_print_visitor v;
196 ir->accept(&v);
197 printf("\n");
198 }
199
200 if (ir_assign) {
201 progress = process_assignment(ir_assign, &assignments) || progress;
202 } else {
203 kill_for_derefs_visitor kill(&assignments);
204 ir->accept(&kill);
205 }
206
207 if (ir == last)
208 break;
209 }
210 *out_progress = progress;
211 }
212
213 /**
214 * Does a copy propagation pass on the code present in the instruction stream.
215 */
216 bool
217 do_dead_code_local(exec_list *instructions)
218 {
219 bool progress = false;
220
221 call_for_basic_blocks(instructions, dead_code_local_basic_block, &progress);
222
223 return progress;
224 }