blob: 0cc8a2650f0402d51e7450a298ee4aafad5aee92 [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 *);
Eric Anholt7d211042010-04-16 16:43:47 -070065 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
88variable_entry *
89ir_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
Eric Anholt7d211042010-04-16 16:43:47 -0700103
104void
105ir_dead_code_visitor::visit(ir_variable *ir)
106{
107 variable_entry *entry = this->get_variable_entry(ir);
108 if (entry) {
109 entry->declaration = true;
110 }
111}
112
113
114void
Eric Anholt7d211042010-04-16 16:43:47 -0700115ir_dead_code_visitor::visit(ir_loop *ir)
116{
Ian Romanick86790dd2010-05-10 17:44:57 -0700117 visit_exec_list(&ir->body_instructions, this);
Eric Anholt7d211042010-04-16 16:43:47 -0700118 if (ir->from)
119 ir->from->accept(this);
120 if (ir->to)
121 ir->to->accept(this);
122 if (ir->increment)
123 ir->increment->accept(this);
124}
125
126void
127ir_dead_code_visitor::visit(ir_loop_jump *ir)
128{
129 (void) ir;
130}
131
132
133void
134ir_dead_code_visitor::visit(ir_function_signature *ir)
135{
Ian Romanick86790dd2010-05-10 17:44:57 -0700136 visit_exec_list(&ir->body, this);
Eric Anholt7d211042010-04-16 16:43:47 -0700137}
138
139void
140ir_dead_code_visitor::visit(ir_function *ir)
141{
142 (void) ir;
143}
144
145void
146ir_dead_code_visitor::visit(ir_expression *ir)
147{
148 unsigned int operand;
149
150 for (operand = 0; operand < ir->get_num_operands(); operand++) {
151 ir->operands[operand]->accept(this);
152 }
153}
154
155
156void
157ir_dead_code_visitor::visit(ir_swizzle *ir)
158{
159 ir->val->accept(this);
160}
161
162
163void
164ir_dead_code_visitor::visit(ir_dereference *ir)
165{
166 ir_variable *var;
167
168 if (ir->mode == ir_dereference::ir_reference_array) {
169 ir->selector.array_index->accept(this);
170 }
171
172 var = ir->var->as_variable();
173 if (var) {
174 variable_entry *entry = this->get_variable_entry(var);
175 entry->referenced = true;
176 } else {
177 ir->var->accept(this);
178 }
179}
180
181void
182ir_dead_code_visitor::visit(ir_assignment *ir)
183{
184 ir_instruction *lhs = ir->lhs;
185
186 /* Walk through the LHS and mark references for variables used in
187 * array indices but not for the assignment dereference.
188 */
189 while (lhs) {
190 if (lhs->as_variable())
191 break;
192
193 ir_dereference *deref = lhs->as_dereference();
194 if (deref) {
195 if (deref->mode == ir_dereference::ir_reference_array)
196 deref->selector.array_index->accept(this);
197 lhs = deref->var;
198 } else {
199 ir_swizzle *swiz = lhs->as_swizzle();
200
201 lhs = swiz->val;
202 }
203 }
204
205 ir->rhs->accept(this);
206 if (ir->condition)
207 ir->condition->accept(this);
208
209 variable_entry *entry;
210 entry = this->get_variable_entry(lhs->as_variable());
211 if (entry) {
212 if (entry->assign == NULL)
213 entry->assign = ir;
214 }
215}
216
217
218void
219ir_dead_code_visitor::visit(ir_constant *ir)
220{
221 (void) ir;
222}
223
224
225void
226ir_dead_code_visitor::visit(ir_call *ir)
227{
228 foreach_iter(exec_list_iterator, iter, *ir) {
229 ir_rvalue *param = (ir_rvalue *)iter.get();
230
231 /* FINISHME: handle out values. */
232 param->accept(this);
233 }
234
235 /* Ignore the callee. Function bodies will get handled when they're
236 * encountered at the top level instruction stream and spawn their
237 * own dead code visitor.
238 */
239}
240
241
242void
243ir_dead_code_visitor::visit(ir_return *ir)
244{
Eric Anholt438f38c2010-04-29 13:54:19 -0700245 ir_rvalue *val = ir->get_value();
246
247 if (val)
248 val->accept(this);
Eric Anholt7d211042010-04-16 16:43:47 -0700249}
250
251
252void
253ir_dead_code_visitor::visit(ir_if *ir)
254{
255 ir->condition->accept(this);
256
Ian Romanick86790dd2010-05-10 17:44:57 -0700257 visit_exec_list(&ir->then_instructions, this);
258 visit_exec_list(&ir->else_instructions, this);
Eric Anholt7d211042010-04-16 16:43:47 -0700259}
260
261/**
262 * Do a dead code pass over instructions and everything that instructions
263 * references.
264 *
265 * Note that this will remove assignments to globals, so it is not suitable
266 * for usage on an unlinked instruction stream.
267 */
268bool
269do_dead_code(exec_list *instructions)
270{
271 ir_dead_code_visitor v;
272 bool progress = false;
273
Ian Romanick86790dd2010-05-10 17:44:57 -0700274 visit_exec_list(instructions, &v);
Eric Anholt7d211042010-04-16 16:43:47 -0700275
276 foreach_iter(exec_list_iterator, iter, v.variable_list) {
277 variable_entry *entry = (variable_entry *)iter.get();
278
279 if (entry->referenced || !entry->declaration)
280 continue;
281
282 if (entry->assign) {
283 /* Remove a single dead assignment to the variable we found.
284 * Don't do so if it's a shader output, though.
285 */
286 if (!entry->var->shader_out) {
287 entry->assign->remove();
288 progress = true;
289 }
290 } else {
291 /* If there are no assignments or references to the variable left,
292 * then we can remove its declaration.
293 */
294 entry->var->remove();
295 progress = true;
296 }
297 }
298 return progress;
299}
300
301/**
302 * Does a dead code pass on the functions present in the instruction stream.
303 *
304 * This is suitable for use while the program is not linked, as it will
305 * ignore variable declarations (and the assignments to them) for variables
306 * with global scope.
307 */
308bool
309do_dead_code_unlinked(exec_list *instructions)
310{
311 bool progress = false;
312
313 foreach_iter(exec_list_iterator, iter, *instructions) {
314 ir_instruction *ir = (ir_instruction *)iter.get();
Kenneth Graunke6202cbf2010-04-21 16:02:15 -0700315 ir_function *f = ir->as_function();
316 if (f) {
317 foreach_iter(exec_list_iterator, sigiter, *f) {
318 ir_function_signature *sig =
319 (ir_function_signature *) sigiter.get();
320 if (do_dead_code(&sig->body))
321 progress = true;
322 }
Eric Anholt7d211042010-04-16 16:43:47 -0700323 }
324 }
325
326 return progress;
327}