Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / libs / mesa / src / glsl / opt_dead_code_local.cpp
blob39962bd602ef8ba89a2ad34eba903d82cdbdc437
1 /*
2 * Copyright © 2010 Intel Corporation
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:
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.
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.
24 /**
25 * \file opt_dead_code_local.cpp
27 * Eliminates local dead assignments from the code.
29 * This operates on basic blocks, tracking assignments and finding if
30 * they're used before the variable is completely reassigned.
32 * Compare this to ir_dead_code.cpp, which operates globally looking
33 * for assignments to variables that are never read.
36 #include "ir.h"
37 #include "ir_basic_block.h"
38 #include "ir_optimization.h"
39 #include "glsl_types.h"
41 static bool debug = false;
43 class assignment_entry : public exec_node
45 public:
46 assignment_entry(ir_variable *lhs, ir_instruction *ir)
48 assert(lhs);
49 assert(ir);
50 this->lhs = lhs;
51 this->ir = ir;
54 ir_variable *lhs;
55 ir_instruction *ir;
58 class kill_for_derefs_visitor : public ir_hierarchical_visitor {
59 public:
60 kill_for_derefs_visitor(exec_list *assignments)
62 this->assignments = assignments;
65 virtual ir_visitor_status visit(ir_dereference_variable *ir)
67 ir_variable *const var = ir->variable_referenced();
69 foreach_iter(exec_list_iterator, iter, *this->assignments) {
70 assignment_entry *entry = (assignment_entry *)iter.get();
72 if (entry->lhs == var) {
73 if (debug)
74 printf("kill %s\n", entry->lhs->name);
75 entry->remove();
79 return visit_continue;
82 private:
83 exec_list *assignments;
86 class array_index_visit : public ir_hierarchical_visitor {
87 public:
88 array_index_visit(ir_hierarchical_visitor *v)
90 this->visitor = v;
93 virtual ir_visitor_status visit_enter(class ir_dereference_array *ir)
95 ir->array_index->accept(visitor);
96 return visit_continue;
99 static void run(ir_instruction *ir, ir_hierarchical_visitor *v)
101 array_index_visit top_visit(v);
102 ir->accept(& top_visit);
105 ir_hierarchical_visitor *visitor;
110 * Adds an entry to the available copy list if it's a plain assignment
111 * of a variable to a variable.
113 static bool
114 process_assignment(void *ctx, ir_assignment *ir, exec_list *assignments)
116 ir_variable *var = NULL;
117 bool progress = false;
118 kill_for_derefs_visitor v(assignments);
120 /* Kill assignment entries for things used to produce this assignment. */
121 ir->rhs->accept(&v);
122 if (ir->condition) {
123 ir->condition->accept(&v);
126 /* Kill assignment enties used as array indices.
128 array_index_visit::run(ir->lhs, &v);
129 var = ir->lhs->variable_referenced();
130 assert(var);
132 /* Now, check if we did a whole-variable assignment. */
133 if (!ir->condition && (ir->whole_variable_written() != NULL)) {
134 /* We did a whole-variable assignment. So, any instruction in
135 * the assignment list with the same LHS is dead.
137 if (debug)
138 printf("looking for %s to remove\n", var->name);
139 foreach_iter(exec_list_iterator, iter, *assignments) {
140 assignment_entry *entry = (assignment_entry *)iter.get();
142 if (entry->lhs == var) {
143 if (debug)
144 printf("removing %s\n", var->name);
145 entry->ir->remove();
146 entry->remove();
147 progress = true;
152 /* Add this instruction to the assignment list available to be removed.
153 * But not if the assignment has other side effects.
155 if (ir_has_call(ir))
156 return progress;
158 assignment_entry *entry = new(ctx) assignment_entry(var, ir);
159 assignments->push_tail(entry);
161 if (debug) {
162 printf("add %s\n", var->name);
164 printf("current entries\n");
165 foreach_iter(exec_list_iterator, iter, *assignments) {
166 assignment_entry *entry = (assignment_entry *)iter.get();
168 printf(" %s\n", entry->lhs->name);
172 return progress;
175 static void
176 dead_code_local_basic_block(ir_instruction *first,
177 ir_instruction *last,
178 void *data)
180 ir_instruction *ir, *ir_next;
181 /* List of avaialble_copy */
182 exec_list assignments;
183 bool *out_progress = (bool *)data;
184 bool progress = false;
186 void *ctx = ralloc_context(NULL);
187 /* Safe looping, since process_assignment */
188 for (ir = first, ir_next = (ir_instruction *)first->next;;
189 ir = ir_next, ir_next = (ir_instruction *)ir->next) {
190 ir_assignment *ir_assign = ir->as_assignment();
192 if (debug) {
193 ir->print();
194 printf("\n");
197 if (ir_assign) {
198 progress = process_assignment(ctx, ir_assign, &assignments) || progress;
199 } else {
200 kill_for_derefs_visitor kill(&assignments);
201 ir->accept(&kill);
204 if (ir == last)
205 break;
207 *out_progress = progress;
208 ralloc_free(ctx);
212 * Does a copy propagation pass on the code present in the instruction stream.
214 bool
215 do_dead_code_local(exec_list *instructions)
217 bool progress = false;
219 call_for_basic_blocks(instructions, dead_code_local_basic_block, &progress);
221 return progress;