Fix ir_dead_code for function refactoring.
[mesa.git] / ir_dead_code.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.cpp
26 *
27 * Eliminates dead assignments and variable declarations from the code.
28 */
29
30 #define NULL 0
31 #include "ir.h"
32 #include "ir_visitor.h"
33 #include "ir_expression_flattening.h"
34 #include "glsl_types.h"
35
36 class variable_entry : public exec_node
37 {
38 public:
39 variable_entry(ir_variable *var)
40 {
41 this->var = var;
42 assign = NULL;
43 referenced = false;
44 declaration = false;
45 }
46
47 ir_variable *var; /* The key: the variable's pointer. */
48 ir_assignment *assign; /* An assignment to the variable, if any */
49 bool referenced; /* If the variable has ever been referenced. */
50 bool declaration; /* If the variable had a decl in the instruction stream */
51 };
52
53 class ir_dead_code_visitor : public ir_visitor {
54 public:
55
56 /**
57 * \name Visit methods
58 *
59 * As typical for the visitor pattern, there must be one \c visit method for
60 * each concrete subclass of \c ir_instruction. Virtual base classes within
61 * the hierarchy should not have \c visit methods.
62 */
63 /*@{*/
64 virtual void visit(ir_variable *);
65 virtual void visit(ir_loop *);
66 virtual void visit(ir_loop_jump *);
67 virtual void visit(ir_function_signature *);
68 virtual void visit(ir_function *);
69 virtual void visit(ir_expression *);
70 virtual void visit(ir_swizzle *);
71 virtual void visit(ir_dereference *);
72 virtual void visit(ir_assignment *);
73 virtual void visit(ir_constant *);
74 virtual void visit(ir_call *);
75 virtual void visit(ir_return *);
76 virtual void visit(ir_if *);
77 /*@}*/
78
79 variable_entry *get_variable_entry(ir_variable *var);
80
81 bool (*predicate)(ir_instruction *ir);
82 ir_instruction *base_ir;
83
84 /* List of variable_entry */
85 exec_list variable_list;
86 };
87
88 variable_entry *
89 ir_dead_code_visitor::get_variable_entry(ir_variable *var)
90 {
91 assert(var);
92 foreach_iter(exec_list_iterator, iter, this->variable_list) {
93 variable_entry *entry = (variable_entry *)iter.get();
94 if (entry->var == var)
95 return entry;
96 }
97
98 variable_entry *entry = new variable_entry(var);
99 this->variable_list.push_tail(entry);
100 return entry;
101 }
102
103 void
104 find_dead_code(exec_list *instructions, ir_dead_code_visitor *v)
105 {
106 foreach_iter(exec_list_iterator, iter, *instructions) {
107 ir_instruction *ir = (ir_instruction *)iter.get();
108
109 ir->accept(v);
110 }
111 }
112
113 void
114 ir_dead_code_visitor::visit(ir_variable *ir)
115 {
116 variable_entry *entry = this->get_variable_entry(ir);
117 if (entry) {
118 entry->declaration = true;
119 }
120 }
121
122
123 void
124 ir_dead_code_visitor::visit(ir_loop *ir)
125 {
126 find_dead_code(&ir->body_instructions, this);
127 if (ir->from)
128 ir->from->accept(this);
129 if (ir->to)
130 ir->to->accept(this);
131 if (ir->increment)
132 ir->increment->accept(this);
133 }
134
135 void
136 ir_dead_code_visitor::visit(ir_loop_jump *ir)
137 {
138 (void) ir;
139 }
140
141
142 void
143 ir_dead_code_visitor::visit(ir_function_signature *ir)
144 {
145 find_dead_code(&ir->body, this);
146 }
147
148 void
149 ir_dead_code_visitor::visit(ir_function *ir)
150 {
151 (void) ir;
152 }
153
154 void
155 ir_dead_code_visitor::visit(ir_expression *ir)
156 {
157 unsigned int operand;
158
159 for (operand = 0; operand < ir->get_num_operands(); operand++) {
160 ir->operands[operand]->accept(this);
161 }
162 }
163
164
165 void
166 ir_dead_code_visitor::visit(ir_swizzle *ir)
167 {
168 ir->val->accept(this);
169 }
170
171
172 void
173 ir_dead_code_visitor::visit(ir_dereference *ir)
174 {
175 ir_variable *var;
176
177 if (ir->mode == ir_dereference::ir_reference_array) {
178 ir->selector.array_index->accept(this);
179 }
180
181 var = ir->var->as_variable();
182 if (var) {
183 variable_entry *entry = this->get_variable_entry(var);
184 entry->referenced = true;
185 } else {
186 ir->var->accept(this);
187 }
188 }
189
190 void
191 ir_dead_code_visitor::visit(ir_assignment *ir)
192 {
193 ir_instruction *lhs = ir->lhs;
194
195 /* Walk through the LHS and mark references for variables used in
196 * array indices but not for the assignment dereference.
197 */
198 while (lhs) {
199 if (lhs->as_variable())
200 break;
201
202 ir_dereference *deref = lhs->as_dereference();
203 if (deref) {
204 if (deref->mode == ir_dereference::ir_reference_array)
205 deref->selector.array_index->accept(this);
206 lhs = deref->var;
207 } else {
208 ir_swizzle *swiz = lhs->as_swizzle();
209
210 lhs = swiz->val;
211 }
212 }
213
214 ir->rhs->accept(this);
215 if (ir->condition)
216 ir->condition->accept(this);
217
218 variable_entry *entry;
219 entry = this->get_variable_entry(lhs->as_variable());
220 if (entry) {
221 if (entry->assign == NULL)
222 entry->assign = ir;
223 }
224 }
225
226
227 void
228 ir_dead_code_visitor::visit(ir_constant *ir)
229 {
230 (void) ir;
231 }
232
233
234 void
235 ir_dead_code_visitor::visit(ir_call *ir)
236 {
237 foreach_iter(exec_list_iterator, iter, *ir) {
238 ir_rvalue *param = (ir_rvalue *)iter.get();
239
240 /* FINISHME: handle out values. */
241 param->accept(this);
242 }
243
244 /* Ignore the callee. Function bodies will get handled when they're
245 * encountered at the top level instruction stream and spawn their
246 * own dead code visitor.
247 */
248 }
249
250
251 void
252 ir_dead_code_visitor::visit(ir_return *ir)
253 {
254 ir->get_value()->accept(this);
255 }
256
257
258 void
259 ir_dead_code_visitor::visit(ir_if *ir)
260 {
261 ir->condition->accept(this);
262
263 find_dead_code(&ir->then_instructions, this);
264 find_dead_code(&ir->else_instructions, this);
265 }
266
267 /**
268 * Do a dead code pass over instructions and everything that instructions
269 * references.
270 *
271 * Note that this will remove assignments to globals, so it is not suitable
272 * for usage on an unlinked instruction stream.
273 */
274 bool
275 do_dead_code(exec_list *instructions)
276 {
277 ir_dead_code_visitor v;
278 bool progress = false;
279
280 find_dead_code(instructions, &v);
281
282 foreach_iter(exec_list_iterator, iter, v.variable_list) {
283 variable_entry *entry = (variable_entry *)iter.get();
284
285 if (entry->referenced || !entry->declaration)
286 continue;
287
288 if (entry->assign) {
289 /* Remove a single dead assignment to the variable we found.
290 * Don't do so if it's a shader output, though.
291 */
292 if (!entry->var->shader_out) {
293 entry->assign->remove();
294 progress = true;
295 }
296 } else {
297 /* If there are no assignments or references to the variable left,
298 * then we can remove its declaration.
299 */
300 entry->var->remove();
301 progress = true;
302 }
303 }
304 return progress;
305 }
306
307 /**
308 * Does a dead code pass on the functions present in the instruction stream.
309 *
310 * This is suitable for use while the program is not linked, as it will
311 * ignore variable declarations (and the assignments to them) for variables
312 * with global scope.
313 */
314 bool
315 do_dead_code_unlinked(exec_list *instructions)
316 {
317 bool progress = false;
318
319 foreach_iter(exec_list_iterator, iter, *instructions) {
320 ir_instruction *ir = (ir_instruction *)iter.get();
321 ir_function *f = ir->as_function();
322 if (f) {
323 foreach_iter(exec_list_iterator, sigiter, *f) {
324 ir_function_signature *sig =
325 (ir_function_signature *) sigiter.get();
326 if (do_dead_code(&sig->body))
327 progress = true;
328 }
329 }
330 }
331
332 return progress;
333 }