glsl: Improve the local dead code optimization to eliminate unused channels.
[mesa.git] / src / glsl / opt_dead_code_local.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 opt_dead_code_local.cpp
26 *
27 * Eliminates local dead assignments from the code.
28 *
29 * This operates on basic blocks, tracking assignments and finding if
30 * they're used before the variable is completely reassigned.
31 *
32 * Compare this to ir_dead_code.cpp, which operates globally looking
33 * for assignments to variables that are never read.
34 */
35
36 #include "ir.h"
37 #include "ir_basic_block.h"
38 #include "ir_optimization.h"
39 #include "glsl_types.h"
40
41 static bool debug = false;
42
43 class assignment_entry : public exec_node
44 {
45 public:
46 assignment_entry(ir_variable *lhs, ir_assignment *ir)
47 {
48 assert(lhs);
49 assert(ir);
50 this->lhs = lhs;
51 this->ir = ir;
52 this->available = ir->write_mask;
53 }
54
55 ir_variable *lhs;
56 ir_assignment *ir;
57
58 /* bitmask of xyzw channels written that haven't been used so far. */
59 int available;
60 };
61
62 class kill_for_derefs_visitor : public ir_hierarchical_visitor {
63 public:
64 kill_for_derefs_visitor(exec_list *assignments)
65 {
66 this->assignments = assignments;
67 }
68
69 void kill_channels(ir_variable *const var, int used)
70 {
71 foreach_iter(exec_list_iterator, iter, *this->assignments) {
72 assignment_entry *entry = (assignment_entry *)iter.get();
73
74 if (entry->lhs == var) {
75 if (var->type->is_scalar() || var->type->is_vector()) {
76 if (debug)
77 printf("kill %s (0x%01x - 0x%01x)\n", entry->lhs->name,
78 entry->available, used);
79 entry->available &= ~used;
80 if (!entry->available)
81 entry->remove();
82 } else {
83 if (debug)
84 printf("kill %s\n", entry->lhs->name);
85 entry->remove();
86 }
87 }
88 }
89 }
90
91 virtual ir_visitor_status visit(ir_dereference_variable *ir)
92 {
93 kill_channels(ir->var, ~0);
94
95 return visit_continue;
96 }
97
98 virtual ir_visitor_status visit(ir_swizzle *ir)
99 {
100 ir_dereference_variable *deref = ir->val->as_dereference_variable();
101 if (!deref)
102 return visit_continue;
103
104 int used = 0;
105 used |= 1 << ir->mask.x;
106 used |= 1 << ir->mask.y;
107 used |= 1 << ir->mask.z;
108 used |= 1 << ir->mask.w;
109
110 kill_channels(deref->var, used);
111
112 return visit_continue_with_parent;
113 }
114
115 private:
116 exec_list *assignments;
117 };
118
119 class array_index_visit : public ir_hierarchical_visitor {
120 public:
121 array_index_visit(ir_hierarchical_visitor *v)
122 {
123 this->visitor = v;
124 }
125
126 virtual ir_visitor_status visit_enter(class ir_dereference_array *ir)
127 {
128 ir->array_index->accept(visitor);
129 return visit_continue;
130 }
131
132 static void run(ir_instruction *ir, ir_hierarchical_visitor *v)
133 {
134 array_index_visit top_visit(v);
135 ir->accept(& top_visit);
136 }
137
138 ir_hierarchical_visitor *visitor;
139 };
140
141
142 /**
143 * Adds an entry to the available copy list if it's a plain assignment
144 * of a variable to a variable.
145 */
146 static bool
147 process_assignment(void *ctx, ir_assignment *ir, exec_list *assignments)
148 {
149 ir_variable *var = NULL;
150 bool progress = false;
151 kill_for_derefs_visitor v(assignments);
152
153 /* Kill assignment entries for things used to produce this assignment. */
154 ir->rhs->accept(&v);
155 if (ir->condition) {
156 ir->condition->accept(&v);
157 }
158
159 /* Kill assignment enties used as array indices.
160 */
161 array_index_visit::run(ir->lhs, &v);
162 var = ir->lhs->variable_referenced();
163 assert(var);
164
165 /* Now, check if we did a whole-variable assignment. */
166 if (!ir->condition) {
167 ir_dereference_variable *deref_var = ir->lhs->as_dereference_variable();
168
169 /* If it's a vector type, we can do per-channel elimination of
170 * use of the RHS.
171 */
172 if (deref_var && (deref_var->var->type->is_scalar() ||
173 deref_var->var->type->is_vector())) {
174
175 if (debug)
176 printf("looking for %s.0x%01x to remove\n", var->name,
177 ir->write_mask);
178
179 foreach_iter(exec_list_iterator, iter, *assignments) {
180 assignment_entry *entry = (assignment_entry *)iter.get();
181
182 if (entry->lhs != var)
183 continue;
184
185 int remove = entry->available & ir->write_mask;
186 if (debug) {
187 printf("%s 0x%01x - 0x%01x = 0x%01x\n",
188 var->name,
189 entry->ir->write_mask,
190 remove, entry->ir->write_mask & ~remove);
191 }
192 if (remove) {
193 progress = true;
194
195 if (debug) {
196 printf("rewriting:\n ");
197 entry->ir->print();
198 printf("\n");
199 }
200
201 entry->ir->write_mask &= ~remove;
202 entry->available &= ~remove;
203 if (entry->ir->write_mask == 0) {
204 /* Delete the dead assignment. */
205 entry->ir->remove();
206 entry->remove();
207 } else {
208 void *mem_ctx = ralloc_parent(entry->ir);
209 /* Reswizzle the RHS arguments according to the new
210 * write_mask.
211 */
212 unsigned components[4];
213 unsigned channels = 0;
214 unsigned next = 0;
215
216 for (int i = 0; i < 4; i++) {
217 if ((entry->ir->write_mask | remove) & (1 << i)) {
218 if (!(remove & (1 << i)))
219 components[channels++] = next;
220 next++;
221 }
222 }
223
224 entry->ir->rhs = new(mem_ctx) ir_swizzle(entry->ir->rhs,
225 components,
226 channels);
227 if (debug) {
228 printf("to:\n ");
229 entry->ir->print();
230 printf("\n");
231 }
232 }
233 }
234 }
235 } else if (ir->whole_variable_written() != NULL) {
236 /* We did a whole-variable assignment. So, any instruction in
237 * the assignment list with the same LHS is dead.
238 */
239 if (debug)
240 printf("looking for %s to remove\n", var->name);
241 foreach_iter(exec_list_iterator, iter, *assignments) {
242 assignment_entry *entry = (assignment_entry *)iter.get();
243
244 if (entry->lhs == var) {
245 if (debug)
246 printf("removing %s\n", var->name);
247 entry->ir->remove();
248 entry->remove();
249 progress = true;
250 }
251 }
252 }
253 }
254
255 /* Add this instruction to the assignment list available to be removed. */
256 assignment_entry *entry = new(ctx) assignment_entry(var, ir);
257 assignments->push_tail(entry);
258
259 if (debug) {
260 printf("add %s\n", var->name);
261
262 printf("current entries\n");
263 foreach_iter(exec_list_iterator, iter, *assignments) {
264 assignment_entry *entry = (assignment_entry *)iter.get();
265
266 printf(" %s (0x%01x)\n", entry->lhs->name, entry->available);
267 }
268 }
269
270 return progress;
271 }
272
273 static void
274 dead_code_local_basic_block(ir_instruction *first,
275 ir_instruction *last,
276 void *data)
277 {
278 ir_instruction *ir, *ir_next;
279 /* List of avaialble_copy */
280 exec_list assignments;
281 bool *out_progress = (bool *)data;
282 bool progress = false;
283
284 void *ctx = ralloc_context(NULL);
285 /* Safe looping, since process_assignment */
286 for (ir = first, ir_next = (ir_instruction *)first->next;;
287 ir = ir_next, ir_next = (ir_instruction *)ir->next) {
288 ir_assignment *ir_assign = ir->as_assignment();
289
290 if (debug) {
291 ir->print();
292 printf("\n");
293 }
294
295 if (ir_assign) {
296 progress = process_assignment(ctx, ir_assign, &assignments) || progress;
297 } else {
298 kill_for_derefs_visitor kill(&assignments);
299 ir->accept(&kill);
300 }
301
302 if (ir == last)
303 break;
304 }
305 *out_progress = progress;
306 ralloc_free(ctx);
307 }
308
309 /**
310 * Does a copy propagation pass on the code present in the instruction stream.
311 */
312 bool
313 do_dead_code_local(exec_list *instructions)
314 {
315 bool progress = false;
316
317 call_for_basic_blocks(instructions, dead_code_local_basic_block, &progress);
318
319 return progress;
320 }