ir_variable: Add query to get number of slots used by a variable
[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 #include "ir.h"
31 #include "ir_visitor.h"
32 #include "ir_expression_flattening.h"
33 #include "glsl_types.h"
34
35 class variable_entry : public exec_node
36 {
37 public:
38 variable_entry(ir_variable *var)
39 {
40 this->var = var;
41 assign = NULL;
42 referenced_count = 0;
43 assigned_count = 0;
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
50 /** Number of times the variable is referenced, including assignments. */
51 unsigned referenced_count;
52
53 /** Number of times the variable is assignmened. */
54 unsigned assigned_count;
55
56 bool declaration; /* If the variable had a decl in the instruction stream */
57 };
58
59 class ir_dead_code_visitor : public ir_hierarchical_visitor {
60 public:
61 virtual ir_visitor_status visit(ir_variable *);
62 virtual ir_visitor_status visit(ir_dereference_variable *);
63
64 virtual ir_visitor_status visit_enter(ir_function *);
65 virtual ir_visitor_status visit_leave(ir_assignment *);
66
67 variable_entry *get_variable_entry(ir_variable *var);
68
69 bool (*predicate)(ir_instruction *ir);
70 ir_instruction *base_ir;
71
72 /* List of variable_entry */
73 exec_list variable_list;
74 };
75
76
77 variable_entry *
78 ir_dead_code_visitor::get_variable_entry(ir_variable *var)
79 {
80 assert(var);
81 foreach_iter(exec_list_iterator, iter, this->variable_list) {
82 variable_entry *entry = (variable_entry *)iter.get();
83 if (entry->var == var)
84 return entry;
85 }
86
87 variable_entry *entry = new variable_entry(var);
88 this->variable_list.push_tail(entry);
89 return entry;
90 }
91
92
93 ir_visitor_status
94 ir_dead_code_visitor::visit(ir_variable *ir)
95 {
96 variable_entry *entry = this->get_variable_entry(ir);
97 if (entry)
98 entry->declaration = true;
99
100 return visit_continue;
101 }
102
103
104 ir_visitor_status
105 ir_dead_code_visitor::visit(ir_dereference_variable *ir)
106 {
107 ir_variable *const var = ir->variable_referenced();
108 variable_entry *entry = this->get_variable_entry(var);
109
110 if (entry)
111 entry->referenced_count++;
112
113 return visit_continue;
114 }
115
116
117 ir_visitor_status
118 ir_dead_code_visitor::visit_enter(ir_function *ir)
119 {
120 (void) ir;
121 return visit_continue_with_parent;
122 }
123
124
125 ir_visitor_status
126 ir_dead_code_visitor::visit_leave(ir_assignment *ir)
127 {
128 variable_entry *entry;
129 entry = this->get_variable_entry(ir->lhs->variable_referenced());
130 if (entry) {
131 entry->assigned_count++;
132 if (entry->assign == NULL)
133 entry->assign = ir;
134 }
135
136 return visit_continue;
137 }
138
139
140 /**
141 * Do a dead code pass over instructions and everything that instructions
142 * references.
143 *
144 * Note that this will remove assignments to globals, so it is not suitable
145 * for usage on an unlinked instruction stream.
146 */
147 bool
148 do_dead_code(exec_list *instructions)
149 {
150 ir_dead_code_visitor v;
151 bool progress = false;
152
153 v.run(instructions);
154
155 foreach_iter(exec_list_iterator, iter, v.variable_list) {
156 variable_entry *entry = (variable_entry *)iter.get();
157
158 /* Since each assignment is a reference, the refereneced count must be
159 * greater than or equal to the assignment count. If they are equal,
160 * then all of the references are assignments, and the variable is
161 * dead.
162 *
163 * Note that if the variable is neither assigned nor referenced, both
164 * counts will be zero and will be caught by the equality test.
165 */
166 assert(entry->referenced_count >= entry->assigned_count);
167
168 if ((entry->referenced_count > entry->assigned_count)
169 || !entry->declaration)
170 continue;
171
172 if (entry->assign) {
173 /* Remove a single dead assignment to the variable we found.
174 * Don't do so if it's a shader output, though.
175 */
176 if (!entry->var->shader_out) {
177 entry->assign->remove();
178 progress = true;
179 }
180 } else {
181 /* If there are no assignments or references to the variable left,
182 * then we can remove its declaration.
183 */
184 entry->var->remove();
185 progress = true;
186 }
187 }
188 return progress;
189 }
190
191 /**
192 * Does a dead code pass on the functions present in the instruction stream.
193 *
194 * This is suitable for use while the program is not linked, as it will
195 * ignore variable declarations (and the assignments to them) for variables
196 * with global scope.
197 */
198 bool
199 do_dead_code_unlinked(exec_list *instructions)
200 {
201 bool progress = false;
202
203 foreach_iter(exec_list_iterator, iter, *instructions) {
204 ir_instruction *ir = (ir_instruction *)iter.get();
205 ir_function *f = ir->as_function();
206 if (f) {
207 foreach_iter(exec_list_iterator, sigiter, *f) {
208 ir_function_signature *sig =
209 (ir_function_signature *) sigiter.get();
210 if (do_dead_code(&sig->body))
211 progress = true;
212 }
213 }
214 }
215
216 return progress;
217 }