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