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