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 | |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 30 | #include "ir.h" |
| 31 | #include "ir_visitor.h" |
| 32 | #include "ir_expression_flattening.h" |
| 33 | #include "glsl_types.h" |
| 34 | |
| 35 | class variable_entry : public exec_node |
| 36 | { |
| 37 | public: |
| 38 | variable_entry(ir_variable *var) |
| 39 | { |
| 40 | this->var = var; |
| 41 | assign = NULL; |
Ian Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame] | 42 | referenced_count = 0; |
| 43 | assigned_count = 0; |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 44 | 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 Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame] | 49 | |
| 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 Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 56 | bool declaration; /* If the variable had a decl in the instruction stream */ |
| 57 | }; |
| 58 | |
Ian Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame] | 59 | class ir_dead_code_visitor : public ir_hierarchical_visitor { |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 60 | public: |
Ian Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame] | 61 | virtual ir_visitor_status visit(ir_variable *); |
Ian Romanick | f3a002b | 2010-05-19 12:02:19 +0200 | [diff] [blame] | 62 | virtual ir_visitor_status visit(ir_dereference_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 *); |
Ian Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame] | 65 | virtual ir_visitor_status visit_leave(ir_assignment *); |
| 66 | |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 67 | variable_entry *get_variable_entry(ir_variable *var); |
| 68 | |
| 69 | bool (*predicate)(ir_instruction *ir); |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 70 | |
| 71 | /* List of variable_entry */ |
| 72 | exec_list variable_list; |
Eric Anholt | bda2742 | 2010-06-25 13:38:38 -0700 | [diff] [blame] | 73 | |
| 74 | void *mem_ctx; |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 75 | }; |
| 76 | |
Ian Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame] | 77 | |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 78 | variable_entry * |
| 79 | ir_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 Anholt | bda2742 | 2010-06-25 13:38:38 -0700 | [diff] [blame] | 88 | variable_entry *entry = new(mem_ctx) variable_entry(var); |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 89 | this->variable_list.push_tail(entry); |
| 90 | return entry; |
| 91 | } |
| 92 | |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 93 | |
Ian Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame] | 94 | ir_visitor_status |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 95 | ir_dead_code_visitor::visit(ir_variable *ir) |
| 96 | { |
| 97 | variable_entry *entry = this->get_variable_entry(ir); |
Ian Romanick | f3a002b | 2010-05-19 12:02:19 +0200 | [diff] [blame] | 98 | if (entry) |
| 99 | entry->declaration = true; |
| 100 | |
| 101 | return visit_continue; |
| 102 | } |
| 103 | |
| 104 | |
| 105 | ir_visitor_status |
| 106 | ir_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 Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame] | 113 | |
| 114 | return visit_continue; |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | |
Ian Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame] | 118 | ir_visitor_status |
| 119 | ir_dead_code_visitor::visit_enter(ir_function *ir) |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 120 | { |
| 121 | (void) ir; |
Ian Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame] | 122 | return visit_continue_with_parent; |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | |
Ian Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame] | 126 | ir_visitor_status |
Ian Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame] | 127 | ir_dead_code_visitor::visit_leave(ir_assignment *ir) |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 128 | { |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 129 | variable_entry *entry; |
Ian Romanick | 461c294 | 2010-05-18 13:53:20 +0200 | [diff] [blame] | 130 | entry = this->get_variable_entry(ir->lhs->variable_referenced()); |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 131 | if (entry) { |
Ian Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame] | 132 | entry->assigned_count++; |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 133 | if (entry->assign == NULL) |
| 134 | entry->assign = ir; |
| 135 | } |
Ian Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame] | 136 | |
| 137 | return visit_continue; |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 141 | /** |
| 142 | * Do a dead code pass over instructions and everything that instructions |
| 143 | * references. |
| 144 | * |
| 145 | * Note that this will remove assignments to globals, so it is not suitable |
| 146 | * for usage on an unlinked instruction stream. |
| 147 | */ |
| 148 | bool |
Eric Anholt | 66d4c65 | 2010-07-27 11:28:26 -0700 | [diff] [blame^] | 149 | do_dead_code(exec_list *instructions) |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 150 | { |
| 151 | ir_dead_code_visitor v; |
| 152 | bool progress = false; |
| 153 | |
Eric Anholt | 66d4c65 | 2010-07-27 11:28:26 -0700 | [diff] [blame^] | 154 | v.mem_ctx = talloc_new(NULL); |
Ian Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame] | 155 | v.run(instructions); |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 156 | |
| 157 | foreach_iter(exec_list_iterator, iter, v.variable_list) { |
| 158 | variable_entry *entry = (variable_entry *)iter.get(); |
| 159 | |
Ian Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame] | 160 | /* Since each assignment is a reference, the refereneced count must be |
| 161 | * greater than or equal to the assignment count. If they are equal, |
| 162 | * then all of the references are assignments, and the variable is |
| 163 | * dead. |
| 164 | * |
| 165 | * Note that if the variable is neither assigned nor referenced, both |
| 166 | * counts will be zero and will be caught by the equality test. |
| 167 | */ |
| 168 | assert(entry->referenced_count >= entry->assigned_count); |
| 169 | |
| 170 | if ((entry->referenced_count > entry->assigned_count) |
| 171 | || !entry->declaration) |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 172 | continue; |
| 173 | |
| 174 | if (entry->assign) { |
| 175 | /* Remove a single dead assignment to the variable we found. |
| 176 | * Don't do so if it's a shader output, though. |
| 177 | */ |
| 178 | if (!entry->var->shader_out) { |
| 179 | entry->assign->remove(); |
| 180 | progress = true; |
| 181 | } |
| 182 | } else { |
| 183 | /* If there are no assignments or references to the variable left, |
| 184 | * then we can remove its declaration. |
| 185 | */ |
| 186 | entry->var->remove(); |
| 187 | progress = true; |
| 188 | } |
| 189 | } |
Eric Anholt | 66d4c65 | 2010-07-27 11:28:26 -0700 | [diff] [blame^] | 190 | talloc_free(v.mem_ctx); |
| 191 | |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 192 | return progress; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Does a dead code pass on the functions present in the instruction stream. |
| 197 | * |
| 198 | * This is suitable for use while the program is not linked, as it will |
| 199 | * ignore variable declarations (and the assignments to them) for variables |
| 200 | * with global scope. |
| 201 | */ |
| 202 | bool |
Eric Anholt | 66d4c65 | 2010-07-27 11:28:26 -0700 | [diff] [blame^] | 203 | do_dead_code_unlinked(exec_list *instructions) |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 204 | { |
| 205 | bool progress = false; |
| 206 | |
| 207 | foreach_iter(exec_list_iterator, iter, *instructions) { |
| 208 | ir_instruction *ir = (ir_instruction *)iter.get(); |
Kenneth Graunke | 6202cbf | 2010-04-21 16:02:15 -0700 | [diff] [blame] | 209 | ir_function *f = ir->as_function(); |
| 210 | if (f) { |
| 211 | foreach_iter(exec_list_iterator, sigiter, *f) { |
| 212 | ir_function_signature *sig = |
| 213 | (ir_function_signature *) sigiter.get(); |
Eric Anholt | 66d4c65 | 2010-07-27 11:28:26 -0700 | [diff] [blame^] | 214 | if (do_dead_code(&sig->body)) |
Kenneth Graunke | 6202cbf | 2010-04-21 16:02:15 -0700 | [diff] [blame] | 215 | progress = true; |
| 216 | } |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
| 220 | return progress; |
| 221 | } |