Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 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; |
Ian Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame^] | 43 | referenced_count = 0; |
| 44 | assigned_count = 0; |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 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 */ |
Ian Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame^] | 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 | |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 57 | bool declaration; /* If the variable had a decl in the instruction stream */ |
| 58 | }; |
| 59 | |
Ian Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame^] | 60 | class ir_dead_code_visitor : public ir_hierarchical_visitor { |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 61 | public: |
Ian Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame^] | 62 | virtual ir_visitor_status visit(ir_variable *); |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 63 | |
Ian Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame^] | 64 | virtual ir_visitor_status visit_enter(ir_function *); |
| 65 | virtual ir_visitor_status visit_enter(ir_dereference *); |
| 66 | virtual ir_visitor_status visit_leave(ir_dereference *); |
| 67 | virtual ir_visitor_status visit_leave(ir_assignment *); |
| 68 | |
| 69 | ir_dead_code_visitor(void); |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 70 | |
| 71 | variable_entry *get_variable_entry(ir_variable *var); |
| 72 | |
| 73 | bool (*predicate)(ir_instruction *ir); |
| 74 | ir_instruction *base_ir; |
| 75 | |
| 76 | /* List of variable_entry */ |
| 77 | exec_list variable_list; |
Ian Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame^] | 78 | |
| 79 | /* Depth of derefernce stack. */ |
| 80 | int in_dereference; |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 81 | }; |
| 82 | |
Ian Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame^] | 83 | ir_dead_code_visitor::ir_dead_code_visitor(void) |
| 84 | { |
| 85 | this->in_dereference = 0; |
| 86 | } |
| 87 | |
| 88 | |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 89 | variable_entry * |
| 90 | ir_dead_code_visitor::get_variable_entry(ir_variable *var) |
| 91 | { |
| 92 | assert(var); |
| 93 | foreach_iter(exec_list_iterator, iter, this->variable_list) { |
| 94 | variable_entry *entry = (variable_entry *)iter.get(); |
| 95 | if (entry->var == var) |
| 96 | return entry; |
| 97 | } |
| 98 | |
| 99 | variable_entry *entry = new variable_entry(var); |
| 100 | this->variable_list.push_tail(entry); |
| 101 | return entry; |
| 102 | } |
| 103 | |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 104 | |
Ian Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame^] | 105 | ir_visitor_status |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 106 | ir_dead_code_visitor::visit(ir_variable *ir) |
| 107 | { |
| 108 | variable_entry *entry = this->get_variable_entry(ir); |
| 109 | if (entry) { |
Ian Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame^] | 110 | if (this->in_dereference) |
| 111 | entry->referenced_count++; |
| 112 | else |
| 113 | entry->declaration = true; |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 114 | } |
Ian Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame^] | 115 | |
| 116 | return visit_continue; |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | |
Ian Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame^] | 120 | ir_visitor_status |
| 121 | ir_dead_code_visitor::visit_enter(ir_function *ir) |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 122 | { |
| 123 | (void) ir; |
Ian Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame^] | 124 | return visit_continue_with_parent; |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | |
Ian Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame^] | 128 | ir_visitor_status |
| 129 | ir_dead_code_visitor::visit_enter(ir_dereference *ir) |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 130 | { |
| 131 | (void) ir; |
Ian Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame^] | 132 | this->in_dereference++; |
| 133 | return visit_continue; |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Ian Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame^] | 136 | |
| 137 | ir_visitor_status |
| 138 | ir_dead_code_visitor::visit_leave(ir_dereference *ir) |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 139 | { |
Ian Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame^] | 140 | (void) ir; |
| 141 | this->in_dereference--; |
| 142 | return visit_continue; |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | |
Ian Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame^] | 146 | ir_visitor_status |
| 147 | ir_dead_code_visitor::visit_leave(ir_assignment *ir) |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 148 | { |
| 149 | ir_instruction *lhs = ir->lhs; |
| 150 | |
| 151 | /* Walk through the LHS and mark references for variables used in |
| 152 | * array indices but not for the assignment dereference. |
| 153 | */ |
| 154 | while (lhs) { |
| 155 | if (lhs->as_variable()) |
| 156 | break; |
| 157 | |
| 158 | ir_dereference *deref = lhs->as_dereference(); |
| 159 | if (deref) { |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 160 | lhs = deref->var; |
| 161 | } else { |
| 162 | ir_swizzle *swiz = lhs->as_swizzle(); |
| 163 | |
| 164 | lhs = swiz->val; |
| 165 | } |
| 166 | } |
| 167 | |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 168 | |
| 169 | variable_entry *entry; |
| 170 | entry = this->get_variable_entry(lhs->as_variable()); |
| 171 | if (entry) { |
Ian Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame^] | 172 | entry->assigned_count++; |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 173 | if (entry->assign == NULL) |
| 174 | entry->assign = ir; |
| 175 | } |
Ian Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame^] | 176 | |
| 177 | return visit_continue; |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 181 | /** |
| 182 | * Do a dead code pass over instructions and everything that instructions |
| 183 | * references. |
| 184 | * |
| 185 | * Note that this will remove assignments to globals, so it is not suitable |
| 186 | * for usage on an unlinked instruction stream. |
| 187 | */ |
| 188 | bool |
| 189 | do_dead_code(exec_list *instructions) |
| 190 | { |
| 191 | ir_dead_code_visitor v; |
| 192 | bool progress = false; |
| 193 | |
Ian Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame^] | 194 | v.run(instructions); |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 195 | |
| 196 | foreach_iter(exec_list_iterator, iter, v.variable_list) { |
| 197 | variable_entry *entry = (variable_entry *)iter.get(); |
| 198 | |
Ian Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame^] | 199 | /* Since each assignment is a reference, the refereneced count must be |
| 200 | * greater than or equal to the assignment count. If they are equal, |
| 201 | * then all of the references are assignments, and the variable is |
| 202 | * dead. |
| 203 | * |
| 204 | * Note that if the variable is neither assigned nor referenced, both |
| 205 | * counts will be zero and will be caught by the equality test. |
| 206 | */ |
| 207 | assert(entry->referenced_count >= entry->assigned_count); |
| 208 | |
| 209 | if ((entry->referenced_count > entry->assigned_count) |
| 210 | || !entry->declaration) |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 211 | continue; |
| 212 | |
| 213 | if (entry->assign) { |
| 214 | /* Remove a single dead assignment to the variable we found. |
| 215 | * Don't do so if it's a shader output, though. |
| 216 | */ |
| 217 | if (!entry->var->shader_out) { |
| 218 | entry->assign->remove(); |
| 219 | progress = true; |
| 220 | } |
| 221 | } else { |
| 222 | /* If there are no assignments or references to the variable left, |
| 223 | * then we can remove its declaration. |
| 224 | */ |
| 225 | entry->var->remove(); |
| 226 | progress = true; |
| 227 | } |
| 228 | } |
| 229 | return progress; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Does a dead code pass on the functions present in the instruction stream. |
| 234 | * |
| 235 | * This is suitable for use while the program is not linked, as it will |
| 236 | * ignore variable declarations (and the assignments to them) for variables |
| 237 | * with global scope. |
| 238 | */ |
| 239 | bool |
| 240 | do_dead_code_unlinked(exec_list *instructions) |
| 241 | { |
| 242 | bool progress = false; |
| 243 | |
| 244 | foreach_iter(exec_list_iterator, iter, *instructions) { |
| 245 | ir_instruction *ir = (ir_instruction *)iter.get(); |
Kenneth Graunke | 6202cbf | 2010-04-21 16:02:15 -0700 | [diff] [blame] | 246 | ir_function *f = ir->as_function(); |
| 247 | if (f) { |
| 248 | foreach_iter(exec_list_iterator, sigiter, *f) { |
| 249 | ir_function_signature *sig = |
| 250 | (ir_function_signature *) sigiter.get(); |
| 251 | if (do_dead_code(&sig->body)) |
| 252 | progress = true; |
| 253 | } |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
| 257 | return progress; |
| 258 | } |