blob: 20b791e94ac76085dbc2110e0d45b973f5034cdf [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;
Ian Romanickb5a7cf92010-05-14 12:41:22 -070043 referenced_count = 0;
44 assigned_count = 0;
Eric Anholt7d211042010-04-16 16:43:47 -070045 declaration = false;
46 }
47
48 ir_variable *var; /* The key: the variable's pointer. */
49 ir_assignment *assign; /* An assignment to the variable, if any */
Ian Romanickb5a7cf92010-05-14 12:41:22 -070050
51 /** Number of times the variable is referenced, including assignments. */
52 unsigned referenced_count;
53
54 /** Number of times the variable is assignmened. */
55 unsigned assigned_count;
56
Eric Anholt7d211042010-04-16 16:43:47 -070057 bool declaration; /* If the variable had a decl in the instruction stream */
58};
59
Ian Romanickb5a7cf92010-05-14 12:41:22 -070060class ir_dead_code_visitor : public ir_hierarchical_visitor {
Eric Anholt7d211042010-04-16 16:43:47 -070061public:
Ian Romanickb5a7cf92010-05-14 12:41:22 -070062 virtual ir_visitor_status visit(ir_variable *);
Eric Anholt7d211042010-04-16 16:43:47 -070063
Ian Romanickb5a7cf92010-05-14 12:41:22 -070064 virtual ir_visitor_status visit_enter(ir_function *);
65 virtual ir_visitor_status visit_enter(ir_dereference *);
66 virtual ir_visitor_status visit_leave(ir_dereference *);
67 virtual ir_visitor_status visit_leave(ir_assignment *);
68
69 ir_dead_code_visitor(void);
Eric Anholt7d211042010-04-16 16:43:47 -070070
71 variable_entry *get_variable_entry(ir_variable *var);
72
73 bool (*predicate)(ir_instruction *ir);
74 ir_instruction *base_ir;
75
76 /* List of variable_entry */
77 exec_list variable_list;
Ian Romanickb5a7cf92010-05-14 12:41:22 -070078
79 /* Depth of derefernce stack. */
80 int in_dereference;
Eric Anholt7d211042010-04-16 16:43:47 -070081};
82
Ian Romanickb5a7cf92010-05-14 12:41:22 -070083ir_dead_code_visitor::ir_dead_code_visitor(void)
84{
85 this->in_dereference = 0;
86}
87
88
Eric Anholt7d211042010-04-16 16:43:47 -070089variable_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
Eric Anholt7d211042010-04-16 16:43:47 -0700104
Ian Romanickb5a7cf92010-05-14 12:41:22 -0700105ir_visitor_status
Eric Anholt7d211042010-04-16 16:43:47 -0700106ir_dead_code_visitor::visit(ir_variable *ir)
107{
108 variable_entry *entry = this->get_variable_entry(ir);
109 if (entry) {
Ian Romanickb5a7cf92010-05-14 12:41:22 -0700110 if (this->in_dereference)
111 entry->referenced_count++;
112 else
113 entry->declaration = true;
Eric Anholt7d211042010-04-16 16:43:47 -0700114 }
Ian Romanickb5a7cf92010-05-14 12:41:22 -0700115
116 return visit_continue;
Eric Anholt7d211042010-04-16 16:43:47 -0700117}
118
119
Ian Romanickb5a7cf92010-05-14 12:41:22 -0700120ir_visitor_status
121ir_dead_code_visitor::visit_enter(ir_function *ir)
Eric Anholt7d211042010-04-16 16:43:47 -0700122{
123 (void) ir;
Ian Romanickb5a7cf92010-05-14 12:41:22 -0700124 return visit_continue_with_parent;
Eric Anholt7d211042010-04-16 16:43:47 -0700125}
126
127
Ian Romanickb5a7cf92010-05-14 12:41:22 -0700128ir_visitor_status
129ir_dead_code_visitor::visit_enter(ir_dereference *ir)
Eric Anholt7d211042010-04-16 16:43:47 -0700130{
131 (void) ir;
Ian Romanickb5a7cf92010-05-14 12:41:22 -0700132 this->in_dereference++;
133 return visit_continue;
Eric Anholt7d211042010-04-16 16:43:47 -0700134}
135
Ian Romanickb5a7cf92010-05-14 12:41:22 -0700136
137ir_visitor_status
138ir_dead_code_visitor::visit_leave(ir_dereference *ir)
Eric Anholt7d211042010-04-16 16:43:47 -0700139{
Ian Romanickb5a7cf92010-05-14 12:41:22 -0700140 (void) ir;
141 this->in_dereference--;
142 return visit_continue;
Eric Anholt7d211042010-04-16 16:43:47 -0700143}
144
145
Ian Romanickb5a7cf92010-05-14 12:41:22 -0700146ir_visitor_status
147ir_dead_code_visitor::visit_leave(ir_assignment *ir)
Eric Anholt7d211042010-04-16 16:43:47 -0700148{
149 ir_instruction *lhs = ir->lhs;
150
151 /* Walk through the LHS and mark references for variables used in
152 * array indices but not for the assignment dereference.
153 */
154 while (lhs) {
155 if (lhs->as_variable())
156 break;
157
158 ir_dereference *deref = lhs->as_dereference();
159 if (deref) {
Eric Anholt7d211042010-04-16 16:43:47 -0700160 lhs = deref->var;
161 } else {
162 ir_swizzle *swiz = lhs->as_swizzle();
163
164 lhs = swiz->val;
165 }
166 }
167
Eric Anholt7d211042010-04-16 16:43:47 -0700168
169 variable_entry *entry;
170 entry = this->get_variable_entry(lhs->as_variable());
171 if (entry) {
Ian Romanickb5a7cf92010-05-14 12:41:22 -0700172 entry->assigned_count++;
Eric Anholt7d211042010-04-16 16:43:47 -0700173 if (entry->assign == NULL)
174 entry->assign = ir;
175 }
Ian Romanickb5a7cf92010-05-14 12:41:22 -0700176
177 return visit_continue;
Eric Anholt7d211042010-04-16 16:43:47 -0700178}
179
180
Eric Anholt7d211042010-04-16 16:43:47 -0700181/**
182 * Do a dead code pass over instructions and everything that instructions
183 * references.
184 *
185 * Note that this will remove assignments to globals, so it is not suitable
186 * for usage on an unlinked instruction stream.
187 */
188bool
189do_dead_code(exec_list *instructions)
190{
191 ir_dead_code_visitor v;
192 bool progress = false;
193
Ian Romanickb5a7cf92010-05-14 12:41:22 -0700194 v.run(instructions);
Eric Anholt7d211042010-04-16 16:43:47 -0700195
196 foreach_iter(exec_list_iterator, iter, v.variable_list) {
197 variable_entry *entry = (variable_entry *)iter.get();
198
Ian Romanickb5a7cf92010-05-14 12:41:22 -0700199 /* Since each assignment is a reference, the refereneced count must be
200 * greater than or equal to the assignment count. If they are equal,
201 * then all of the references are assignments, and the variable is
202 * dead.
203 *
204 * Note that if the variable is neither assigned nor referenced, both
205 * counts will be zero and will be caught by the equality test.
206 */
207 assert(entry->referenced_count >= entry->assigned_count);
208
209 if ((entry->referenced_count > entry->assigned_count)
210 || !entry->declaration)
Eric Anholt7d211042010-04-16 16:43:47 -0700211 continue;
212
213 if (entry->assign) {
214 /* Remove a single dead assignment to the variable we found.
215 * Don't do so if it's a shader output, though.
216 */
217 if (!entry->var->shader_out) {
218 entry->assign->remove();
219 progress = true;
220 }
221 } else {
222 /* If there are no assignments or references to the variable left,
223 * then we can remove its declaration.
224 */
225 entry->var->remove();
226 progress = true;
227 }
228 }
229 return progress;
230}
231
232/**
233 * Does a dead code pass on the functions present in the instruction stream.
234 *
235 * This is suitable for use while the program is not linked, as it will
236 * ignore variable declarations (and the assignments to them) for variables
237 * with global scope.
238 */
239bool
240do_dead_code_unlinked(exec_list *instructions)
241{
242 bool progress = false;
243
244 foreach_iter(exec_list_iterator, iter, *instructions) {
245 ir_instruction *ir = (ir_instruction *)iter.get();
Kenneth Graunke6202cbf2010-04-21 16:02:15 -0700246 ir_function *f = ir->as_function();
247 if (f) {
248 foreach_iter(exec_list_iterator, sigiter, *f) {
249 ir_function_signature *sig =
250 (ir_function_signature *) sigiter.get();
251 if (do_dead_code(&sig->body))
252 progress = true;
253 }
Eric Anholt7d211042010-04-16 16:43:47 -0700254 }
255 }
256
257 return progress;
258}