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" |
Eric Anholt | d72edc4 | 2010-07-30 16:05:27 -0700 | [diff] [blame] | 32 | #include "ir_variable_refcount.h" |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 33 | #include "glsl_types.h" |
| 34 | |
Eric Anholt | 3bd7e70 | 2010-08-05 12:07:23 -0700 | [diff] [blame] | 35 | static bool debug = false; |
| 36 | |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 37 | /** |
| 38 | * Do a dead code pass over instructions and everything that instructions |
| 39 | * references. |
| 40 | * |
| 41 | * Note that this will remove assignments to globals, so it is not suitable |
| 42 | * for usage on an unlinked instruction stream. |
| 43 | */ |
| 44 | bool |
Eric Anholt | 66d4c65 | 2010-07-27 11:28:26 -0700 | [diff] [blame] | 45 | do_dead_code(exec_list *instructions) |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 46 | { |
Eric Anholt | d72edc4 | 2010-07-30 16:05:27 -0700 | [diff] [blame] | 47 | ir_variable_refcount_visitor v; |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 48 | bool progress = false; |
| 49 | |
Ian Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame] | 50 | v.run(instructions); |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 51 | |
| 52 | foreach_iter(exec_list_iterator, iter, v.variable_list) { |
| 53 | variable_entry *entry = (variable_entry *)iter.get(); |
| 54 | |
Ian Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame] | 55 | /* Since each assignment is a reference, the refereneced count must be |
| 56 | * greater than or equal to the assignment count. If they are equal, |
| 57 | * then all of the references are assignments, and the variable is |
| 58 | * dead. |
| 59 | * |
| 60 | * Note that if the variable is neither assigned nor referenced, both |
| 61 | * counts will be zero and will be caught by the equality test. |
| 62 | */ |
| 63 | assert(entry->referenced_count >= entry->assigned_count); |
| 64 | |
Eric Anholt | 3bd7e70 | 2010-08-05 12:07:23 -0700 | [diff] [blame] | 65 | if (debug) { |
| 66 | printf("%s@%p: %d refs, %d assigns, %sdeclared in our scope\n", |
| 67 | entry->var->name, entry->var, |
| 68 | entry->referenced_count, entry->assigned_count, |
| 69 | entry->declaration ? "" : "not "); |
| 70 | } |
| 71 | |
Ian Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame] | 72 | if ((entry->referenced_count > entry->assigned_count) |
| 73 | || !entry->declaration) |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 74 | continue; |
| 75 | |
| 76 | if (entry->assign) { |
| 77 | /* Remove a single dead assignment to the variable we found. |
| 78 | * Don't do so if it's a shader output, though. |
| 79 | */ |
Eric Anholt | 046bef2 | 2010-08-04 20:33:57 -0700 | [diff] [blame] | 80 | if (entry->var->mode != ir_var_out && |
Eric Anholt | 9f82806 | 2010-08-05 12:10:31 -0700 | [diff] [blame^] | 81 | entry->var->mode != ir_var_inout && |
| 82 | !ir_has_call(entry->assign)) { |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 83 | entry->assign->remove(); |
| 84 | progress = true; |
Eric Anholt | 3bd7e70 | 2010-08-05 12:07:23 -0700 | [diff] [blame] | 85 | |
| 86 | if (debug) { |
| 87 | printf("Removed assignment to %s@%p\n", |
| 88 | entry->var->name, entry->var); |
| 89 | } |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 90 | } |
| 91 | } else { |
| 92 | /* If there are no assignments or references to the variable left, |
| 93 | * then we can remove its declaration. |
| 94 | */ |
| 95 | entry->var->remove(); |
| 96 | progress = true; |
Eric Anholt | 3bd7e70 | 2010-08-05 12:07:23 -0700 | [diff] [blame] | 97 | |
| 98 | if (debug) { |
| 99 | printf("Removed declaration of %s@%p\n", |
| 100 | entry->var->name, entry->var); |
| 101 | } |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 102 | } |
| 103 | } |
Eric Anholt | 66d4c65 | 2010-07-27 11:28:26 -0700 | [diff] [blame] | 104 | talloc_free(v.mem_ctx); |
| 105 | |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 106 | return progress; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Does a dead code pass on the functions present in the instruction stream. |
| 111 | * |
| 112 | * This is suitable for use while the program is not linked, as it will |
| 113 | * ignore variable declarations (and the assignments to them) for variables |
| 114 | * with global scope. |
| 115 | */ |
| 116 | bool |
Eric Anholt | 66d4c65 | 2010-07-27 11:28:26 -0700 | [diff] [blame] | 117 | do_dead_code_unlinked(exec_list *instructions) |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 118 | { |
| 119 | bool progress = false; |
| 120 | |
| 121 | foreach_iter(exec_list_iterator, iter, *instructions) { |
| 122 | ir_instruction *ir = (ir_instruction *)iter.get(); |
Kenneth Graunke | 6202cbf | 2010-04-21 16:02:15 -0700 | [diff] [blame] | 123 | ir_function *f = ir->as_function(); |
| 124 | if (f) { |
| 125 | foreach_iter(exec_list_iterator, sigiter, *f) { |
| 126 | ir_function_signature *sig = |
| 127 | (ir_function_signature *) sigiter.get(); |
Eric Anholt | 66d4c65 | 2010-07-27 11:28:26 -0700 | [diff] [blame] | 128 | if (do_dead_code(&sig->body)) |
Kenneth Graunke | 6202cbf | 2010-04-21 16:02:15 -0700 | [diff] [blame] | 129 | progress = true; |
| 130 | } |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
| 134 | return progress; |
| 135 | } |