blob: eab459b920fe3a2f1e39c99dac5ff0eabca1c0f3 [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
Eric Anholt7d211042010-04-16 16:43:47 -070030#include "ir.h"
31#include "ir_visitor.h"
32#include "ir_expression_flattening.h"
33#include "glsl_types.h"
34
35class variable_entry : public exec_node
36{
37public:
38 variable_entry(ir_variable *var)
39 {
40 this->var = var;
41 assign = NULL;
Ian Romanickb5a7cf92010-05-14 12:41:22 -070042 referenced_count = 0;
43 assigned_count = 0;
Eric Anholt7d211042010-04-16 16:43:47 -070044 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 */
Ian Romanickb5a7cf92010-05-14 12:41:22 -070049
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
Eric Anholt7d211042010-04-16 16:43:47 -070056 bool declaration; /* If the variable had a decl in the instruction stream */
57};
58
Ian Romanickb5a7cf92010-05-14 12:41:22 -070059class ir_dead_code_visitor : public ir_hierarchical_visitor {
Eric Anholt7d211042010-04-16 16:43:47 -070060public:
Ian Romanickb5a7cf92010-05-14 12:41:22 -070061 virtual ir_visitor_status visit(ir_variable *);
Ian Romanickf3a002b2010-05-19 12:02:19 +020062 virtual ir_visitor_status visit(ir_dereference_variable *);
Eric Anholt7d211042010-04-16 16:43:47 -070063
Eric Anholt5532c4c2010-07-27 11:29:17 -070064 virtual ir_visitor_status visit_enter(ir_function_signature *);
Ian Romanickb5a7cf92010-05-14 12:41:22 -070065 virtual ir_visitor_status visit_leave(ir_assignment *);
66
Eric Anholt7d211042010-04-16 16:43:47 -070067 variable_entry *get_variable_entry(ir_variable *var);
68
69 bool (*predicate)(ir_instruction *ir);
Eric Anholt7d211042010-04-16 16:43:47 -070070
71 /* List of variable_entry */
72 exec_list variable_list;
Eric Anholtbda27422010-06-25 13:38:38 -070073
74 void *mem_ctx;
Eric Anholt7d211042010-04-16 16:43:47 -070075};
76
Ian Romanickb5a7cf92010-05-14 12:41:22 -070077
Eric Anholt7d211042010-04-16 16:43:47 -070078variable_entry *
79ir_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
Eric Anholtbda27422010-06-25 13:38:38 -070088 variable_entry *entry = new(mem_ctx) variable_entry(var);
Eric Anholt7d211042010-04-16 16:43:47 -070089 this->variable_list.push_tail(entry);
90 return entry;
91}
92
Eric Anholt7d211042010-04-16 16:43:47 -070093
Ian Romanickb5a7cf92010-05-14 12:41:22 -070094ir_visitor_status
Eric Anholt7d211042010-04-16 16:43:47 -070095ir_dead_code_visitor::visit(ir_variable *ir)
96{
97 variable_entry *entry = this->get_variable_entry(ir);
Ian Romanickf3a002b2010-05-19 12:02:19 +020098 if (entry)
99 entry->declaration = true;
100
101 return visit_continue;
102}
103
104
105ir_visitor_status
106ir_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++;
Ian Romanickb5a7cf92010-05-14 12:41:22 -0700113
114 return visit_continue;
Eric Anholt7d211042010-04-16 16:43:47 -0700115}
116
117
Ian Romanickb5a7cf92010-05-14 12:41:22 -0700118ir_visitor_status
Eric Anholt5532c4c2010-07-27 11:29:17 -0700119ir_dead_code_visitor::visit_enter(ir_function_signature *ir)
Eric Anholt7d211042010-04-16 16:43:47 -0700120{
Eric Anholt5532c4c2010-07-27 11:29:17 -0700121 /* We don't want to descend into the function parameters and
122 * dead-code eliminate them, so just accept the body here.
123 */
124 visit_list_elements(this, &ir->body);
Ian Romanickb5a7cf92010-05-14 12:41:22 -0700125 return visit_continue_with_parent;
Eric Anholt7d211042010-04-16 16:43:47 -0700126}
127
128
Ian Romanickb5a7cf92010-05-14 12:41:22 -0700129ir_visitor_status
Ian Romanickb5a7cf92010-05-14 12:41:22 -0700130ir_dead_code_visitor::visit_leave(ir_assignment *ir)
Eric Anholt7d211042010-04-16 16:43:47 -0700131{
Eric Anholt7d211042010-04-16 16:43:47 -0700132 variable_entry *entry;
Ian Romanick461c2942010-05-18 13:53:20 +0200133 entry = this->get_variable_entry(ir->lhs->variable_referenced());
Eric Anholt7d211042010-04-16 16:43:47 -0700134 if (entry) {
Ian Romanickb5a7cf92010-05-14 12:41:22 -0700135 entry->assigned_count++;
Eric Anholt7d211042010-04-16 16:43:47 -0700136 if (entry->assign == NULL)
137 entry->assign = ir;
138 }
Ian Romanickb5a7cf92010-05-14 12:41:22 -0700139
140 return visit_continue;
Eric Anholt7d211042010-04-16 16:43:47 -0700141}
142
143
Eric Anholt7d211042010-04-16 16:43:47 -0700144/**
145 * Do a dead code pass over instructions and everything that instructions
146 * references.
147 *
148 * Note that this will remove assignments to globals, so it is not suitable
149 * for usage on an unlinked instruction stream.
150 */
151bool
Eric Anholt66d4c652010-07-27 11:28:26 -0700152do_dead_code(exec_list *instructions)
Eric Anholt7d211042010-04-16 16:43:47 -0700153{
154 ir_dead_code_visitor v;
155 bool progress = false;
156
Eric Anholt66d4c652010-07-27 11:28:26 -0700157 v.mem_ctx = talloc_new(NULL);
Ian Romanickb5a7cf92010-05-14 12:41:22 -0700158 v.run(instructions);
Eric Anholt7d211042010-04-16 16:43:47 -0700159
160 foreach_iter(exec_list_iterator, iter, v.variable_list) {
161 variable_entry *entry = (variable_entry *)iter.get();
162
Ian Romanickb5a7cf92010-05-14 12:41:22 -0700163 /* Since each assignment is a reference, the refereneced count must be
164 * greater than or equal to the assignment count. If they are equal,
165 * then all of the references are assignments, and the variable is
166 * dead.
167 *
168 * Note that if the variable is neither assigned nor referenced, both
169 * counts will be zero and will be caught by the equality test.
170 */
171 assert(entry->referenced_count >= entry->assigned_count);
172
173 if ((entry->referenced_count > entry->assigned_count)
174 || !entry->declaration)
Eric Anholt7d211042010-04-16 16:43:47 -0700175 continue;
176
177 if (entry->assign) {
178 /* Remove a single dead assignment to the variable we found.
179 * Don't do so if it's a shader output, though.
180 */
181 if (!entry->var->shader_out) {
182 entry->assign->remove();
183 progress = true;
184 }
185 } else {
186 /* If there are no assignments or references to the variable left,
187 * then we can remove its declaration.
188 */
189 entry->var->remove();
190 progress = true;
191 }
192 }
Eric Anholt66d4c652010-07-27 11:28:26 -0700193 talloc_free(v.mem_ctx);
194
Eric Anholt7d211042010-04-16 16:43:47 -0700195 return progress;
196}
197
198/**
199 * Does a dead code pass on the functions present in the instruction stream.
200 *
201 * This is suitable for use while the program is not linked, as it will
202 * ignore variable declarations (and the assignments to them) for variables
203 * with global scope.
204 */
205bool
Eric Anholt66d4c652010-07-27 11:28:26 -0700206do_dead_code_unlinked(exec_list *instructions)
Eric Anholt7d211042010-04-16 16:43:47 -0700207{
208 bool progress = false;
209
210 foreach_iter(exec_list_iterator, iter, *instructions) {
211 ir_instruction *ir = (ir_instruction *)iter.get();
Kenneth Graunke6202cbf2010-04-21 16:02:15 -0700212 ir_function *f = ir->as_function();
213 if (f) {
214 foreach_iter(exec_list_iterator, sigiter, *f) {
215 ir_function_signature *sig =
216 (ir_function_signature *) sigiter.get();
Eric Anholt66d4c652010-07-27 11:28:26 -0700217 if (do_dead_code(&sig->body))
Kenneth Graunke6202cbf2010-04-21 16:02:15 -0700218 progress = true;
219 }
Eric Anholt7d211042010-04-16 16:43:47 -0700220 }
221 }
222
223 return progress;
224}