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