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 | /** |
Chad Versace | df883eb | 2010-11-17 10:43:10 -0800 | [diff] [blame] | 25 | * \file opt_dead_code.cpp |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 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" |
Eric Anholt | 1ddc021 | 2012-10-17 15:20:09 -0700 | [diff] [blame^] | 34 | #include "main/hash_table.h" |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 35 | |
Eric Anholt | 3bd7e70 | 2010-08-05 12:07:23 -0700 | [diff] [blame] | 36 | static bool debug = false; |
| 37 | |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 38 | /** |
| 39 | * Do a dead code pass over instructions and everything that instructions |
| 40 | * references. |
| 41 | * |
| 42 | * Note that this will remove assignments to globals, so it is not suitable |
| 43 | * for usage on an unlinked instruction stream. |
| 44 | */ |
| 45 | bool |
Ian Romanick | 1d5d67f | 2011-10-21 11:17:39 -0700 | [diff] [blame] | 46 | do_dead_code(exec_list *instructions, bool uniform_locations_assigned) |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 47 | { |
Eric Anholt | d72edc4 | 2010-07-30 16:05:27 -0700 | [diff] [blame] | 48 | ir_variable_refcount_visitor v; |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 49 | bool progress = false; |
| 50 | |
Ian Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame] | 51 | v.run(instructions); |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 52 | |
Eric Anholt | 1ddc021 | 2012-10-17 15:20:09 -0700 | [diff] [blame^] | 53 | struct hash_entry *e; |
| 54 | hash_table_foreach(v.ht, e) { |
| 55 | ir_variable_refcount_entry *entry = (ir_variable_refcount_entry *)e->data; |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 56 | |
Ian Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame] | 57 | /* Since each assignment is a reference, the refereneced count must be |
| 58 | * greater than or equal to the assignment count. If they are equal, |
| 59 | * then all of the references are assignments, and the variable is |
| 60 | * dead. |
| 61 | * |
| 62 | * Note that if the variable is neither assigned nor referenced, both |
| 63 | * counts will be zero and will be caught by the equality test. |
| 64 | */ |
| 65 | assert(entry->referenced_count >= entry->assigned_count); |
| 66 | |
Eric Anholt | 3bd7e70 | 2010-08-05 12:07:23 -0700 | [diff] [blame] | 67 | if (debug) { |
| 68 | printf("%s@%p: %d refs, %d assigns, %sdeclared in our scope\n", |
Brian Paul | 9f9386d | 2010-08-11 14:04:51 -0600 | [diff] [blame] | 69 | entry->var->name, (void *) entry->var, |
Eric Anholt | 3bd7e70 | 2010-08-05 12:07:23 -0700 | [diff] [blame] | 70 | entry->referenced_count, entry->assigned_count, |
| 71 | entry->declaration ? "" : "not "); |
| 72 | } |
| 73 | |
Ian Romanick | b5a7cf9 | 2010-05-14 12:41:22 -0700 | [diff] [blame] | 74 | if ((entry->referenced_count > entry->assigned_count) |
| 75 | || !entry->declaration) |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 76 | continue; |
| 77 | |
| 78 | if (entry->assign) { |
| 79 | /* Remove a single dead assignment to the variable we found. |
| 80 | * Don't do so if it's a shader output, though. |
| 81 | */ |
Eric Anholt | 046bef2 | 2010-08-04 20:33:57 -0700 | [diff] [blame] | 82 | if (entry->var->mode != ir_var_out && |
Kenneth Graunke | d884f60 | 2012-03-20 15:56:37 -0700 | [diff] [blame] | 83 | entry->var->mode != ir_var_inout) { |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 84 | entry->assign->remove(); |
| 85 | progress = true; |
Eric Anholt | 3bd7e70 | 2010-08-05 12:07:23 -0700 | [diff] [blame] | 86 | |
| 87 | if (debug) { |
| 88 | printf("Removed assignment to %s@%p\n", |
Brian Paul | 9f9386d | 2010-08-11 14:04:51 -0600 | [diff] [blame] | 89 | entry->var->name, (void *) entry->var); |
Eric Anholt | 3bd7e70 | 2010-08-05 12:07:23 -0700 | [diff] [blame] | 90 | } |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 91 | } |
| 92 | } else { |
| 93 | /* If there are no assignments or references to the variable left, |
| 94 | * then we can remove its declaration. |
| 95 | */ |
Eric Anholt | 7de4d8f | 2010-08-24 15:30:42 -0700 | [diff] [blame] | 96 | |
| 97 | /* uniform initializers are precious, and could get used by another |
Ian Romanick | 1d5d67f | 2011-10-21 11:17:39 -0700 | [diff] [blame] | 98 | * stage. Also, once uniform locations have been assigned, the |
| 99 | * declaration cannot be deleted. |
Eric Anholt | 0cea8a5 | 2012-05-01 14:26:09 -0700 | [diff] [blame] | 100 | * |
| 101 | * Also, GL_ARB_uniform_buffer_object says that std140 |
| 102 | * uniforms will not be eliminated. Since we always do |
| 103 | * std140, just don't eliminate uniforms in UBOs. |
Eric Anholt | 7de4d8f | 2010-08-24 15:30:42 -0700 | [diff] [blame] | 104 | */ |
| 105 | if (entry->var->mode == ir_var_uniform && |
Eric Anholt | 0cea8a5 | 2012-05-01 14:26:09 -0700 | [diff] [blame] | 106 | (uniform_locations_assigned || |
| 107 | entry->var->constant_value || |
| 108 | entry->var->uniform_block != -1)) |
Eric Anholt | 7de4d8f | 2010-08-24 15:30:42 -0700 | [diff] [blame] | 109 | continue; |
| 110 | |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 111 | entry->var->remove(); |
| 112 | progress = true; |
Eric Anholt | 3bd7e70 | 2010-08-05 12:07:23 -0700 | [diff] [blame] | 113 | |
| 114 | if (debug) { |
| 115 | printf("Removed declaration of %s@%p\n", |
Brian Paul | 9f9386d | 2010-08-11 14:04:51 -0600 | [diff] [blame] | 116 | entry->var->name, (void *) entry->var); |
Eric Anholt | 3bd7e70 | 2010-08-05 12:07:23 -0700 | [diff] [blame] | 117 | } |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 118 | } |
| 119 | } |
Eric Anholt | 66d4c65 | 2010-07-27 11:28:26 -0700 | [diff] [blame] | 120 | |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 121 | return progress; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Does a dead code pass on the functions present in the instruction stream. |
| 126 | * |
| 127 | * This is suitable for use while the program is not linked, as it will |
| 128 | * ignore variable declarations (and the assignments to them) for variables |
| 129 | * with global scope. |
| 130 | */ |
| 131 | bool |
Eric Anholt | 66d4c65 | 2010-07-27 11:28:26 -0700 | [diff] [blame] | 132 | do_dead_code_unlinked(exec_list *instructions) |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 133 | { |
| 134 | bool progress = false; |
| 135 | |
| 136 | foreach_iter(exec_list_iterator, iter, *instructions) { |
| 137 | ir_instruction *ir = (ir_instruction *)iter.get(); |
Kenneth Graunke | 6202cbf | 2010-04-21 16:02:15 -0700 | [diff] [blame] | 138 | ir_function *f = ir->as_function(); |
| 139 | if (f) { |
| 140 | foreach_iter(exec_list_iterator, sigiter, *f) { |
| 141 | ir_function_signature *sig = |
| 142 | (ir_function_signature *) sigiter.get(); |
Ian Romanick | 1d5d67f | 2011-10-21 11:17:39 -0700 | [diff] [blame] | 143 | /* The setting of the uniform_locations_assigned flag here is |
| 144 | * irrelevent. If there is a uniform declaration encountered |
| 145 | * inside the body of the function, something has already gone |
| 146 | * terribly, terribly wrong. |
| 147 | */ |
| 148 | if (do_dead_code(&sig->body, false)) |
Kenneth Graunke | 6202cbf | 2010-04-21 16:02:15 -0700 | [diff] [blame] | 149 | progress = true; |
| 150 | } |
Eric Anholt | 7d21104 | 2010-04-16 16:43:47 -0700 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
| 154 | return progress; |
| 155 | } |