1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2010 Intel Corporation 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 21bf215546Sopenharmony_ci * DEALINGS IN THE SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci/** 25bf215546Sopenharmony_ci * \file opt_dead_code.cpp 26bf215546Sopenharmony_ci * 27bf215546Sopenharmony_ci * Eliminates dead assignments and variable declarations from the code. 28bf215546Sopenharmony_ci */ 29bf215546Sopenharmony_ci 30bf215546Sopenharmony_ci#include "ir.h" 31bf215546Sopenharmony_ci#include "ir_visitor.h" 32bf215546Sopenharmony_ci#include "ir_variable_refcount.h" 33bf215546Sopenharmony_ci#include "compiler/glsl_types.h" 34bf215546Sopenharmony_ci#include "util/hash_table.h" 35bf215546Sopenharmony_ci 36bf215546Sopenharmony_cistatic bool debug = false; 37bf215546Sopenharmony_ci 38bf215546Sopenharmony_ci/** 39bf215546Sopenharmony_ci * Do a dead code pass over instructions and everything that instructions 40bf215546Sopenharmony_ci * references. 41bf215546Sopenharmony_ci * 42bf215546Sopenharmony_ci * Note that this will remove assignments to globals, so it is not suitable 43bf215546Sopenharmony_ci * for usage on an unlinked instruction stream. 44bf215546Sopenharmony_ci */ 45bf215546Sopenharmony_cibool 46bf215546Sopenharmony_cido_dead_code(exec_list *instructions) 47bf215546Sopenharmony_ci{ 48bf215546Sopenharmony_ci ir_variable_refcount_visitor v; 49bf215546Sopenharmony_ci bool progress = false; 50bf215546Sopenharmony_ci 51bf215546Sopenharmony_ci v.run(instructions); 52bf215546Sopenharmony_ci 53bf215546Sopenharmony_ci hash_table_foreach(v.ht, e) { 54bf215546Sopenharmony_ci ir_variable_refcount_entry *entry = (ir_variable_refcount_entry *)e->data; 55bf215546Sopenharmony_ci 56bf215546Sopenharmony_ci /* Since each assignment is a reference, the refereneced count must be 57bf215546Sopenharmony_ci * greater than or equal to the assignment count. If they are equal, 58bf215546Sopenharmony_ci * then all of the references are assignments, and the variable is 59bf215546Sopenharmony_ci * dead. 60bf215546Sopenharmony_ci * 61bf215546Sopenharmony_ci * Note that if the variable is neither assigned nor referenced, both 62bf215546Sopenharmony_ci * counts will be zero and will be caught by the equality test. 63bf215546Sopenharmony_ci */ 64bf215546Sopenharmony_ci assert(entry->referenced_count >= entry->assigned_count); 65bf215546Sopenharmony_ci 66bf215546Sopenharmony_ci if (debug) { 67bf215546Sopenharmony_ci printf("%s@%p: %d refs, %d assigns, %sdeclared in our scope\n", 68bf215546Sopenharmony_ci entry->var->name, (void *) entry->var, 69bf215546Sopenharmony_ci entry->referenced_count, entry->assigned_count, 70bf215546Sopenharmony_ci entry->declaration ? "" : "not "); 71bf215546Sopenharmony_ci } 72bf215546Sopenharmony_ci 73bf215546Sopenharmony_ci if ((entry->referenced_count > entry->assigned_count) 74bf215546Sopenharmony_ci || !entry->declaration) 75bf215546Sopenharmony_ci continue; 76bf215546Sopenharmony_ci 77bf215546Sopenharmony_ci /* Section 7.4.1 (Shader Interface Matching) of the OpenGL 4.5 78bf215546Sopenharmony_ci * (Core Profile) spec says: 79bf215546Sopenharmony_ci * 80bf215546Sopenharmony_ci * "With separable program objects, interfaces between shader 81bf215546Sopenharmony_ci * stages may involve the outputs from one program object and the 82bf215546Sopenharmony_ci * inputs from a second program object. For such interfaces, it is 83bf215546Sopenharmony_ci * not possible to detect mismatches at link time, because the 84bf215546Sopenharmony_ci * programs are linked separately. When each such program is 85bf215546Sopenharmony_ci * linked, all inputs or outputs interfacing with another program 86bf215546Sopenharmony_ci * stage are treated as active." 87bf215546Sopenharmony_ci */ 88bf215546Sopenharmony_ci if (entry->var->data.always_active_io) 89bf215546Sopenharmony_ci continue; 90bf215546Sopenharmony_ci 91bf215546Sopenharmony_ci if (!entry->assign_list.is_empty()) { 92bf215546Sopenharmony_ci /* Remove all the dead assignments to the variable we found. 93bf215546Sopenharmony_ci * Don't do so if it's a shader or function output, though. 94bf215546Sopenharmony_ci */ 95bf215546Sopenharmony_ci if (entry->var->data.mode != ir_var_function_out && 96bf215546Sopenharmony_ci entry->var->data.mode != ir_var_function_inout && 97bf215546Sopenharmony_ci entry->var->data.mode != ir_var_shader_out && 98bf215546Sopenharmony_ci entry->var->data.mode != ir_var_shader_storage) { 99bf215546Sopenharmony_ci 100bf215546Sopenharmony_ci while (!entry->assign_list.is_empty()) { 101bf215546Sopenharmony_ci struct assignment_entry *assignment_entry = 102bf215546Sopenharmony_ci exec_node_data(struct assignment_entry, 103bf215546Sopenharmony_ci entry->assign_list.get_head_raw(), link); 104bf215546Sopenharmony_ci 105bf215546Sopenharmony_ci assignment_entry->assign->remove(); 106bf215546Sopenharmony_ci 107bf215546Sopenharmony_ci if (debug) { 108bf215546Sopenharmony_ci printf("Removed assignment to %s@%p\n", 109bf215546Sopenharmony_ci entry->var->name, (void *) entry->var); 110bf215546Sopenharmony_ci } 111bf215546Sopenharmony_ci 112bf215546Sopenharmony_ci assignment_entry->link.remove(); 113bf215546Sopenharmony_ci free(assignment_entry); 114bf215546Sopenharmony_ci } 115bf215546Sopenharmony_ci progress = true; 116bf215546Sopenharmony_ci } 117bf215546Sopenharmony_ci } 118bf215546Sopenharmony_ci 119bf215546Sopenharmony_ci if (entry->assign_list.is_empty()) { 120bf215546Sopenharmony_ci /* If there are no assignments or references to the variable left, 121bf215546Sopenharmony_ci * then we can remove its declaration. 122bf215546Sopenharmony_ci */ 123bf215546Sopenharmony_ci 124bf215546Sopenharmony_ci /* uniform initializers are precious, and could get used by another 125bf215546Sopenharmony_ci * stage. 126bf215546Sopenharmony_ci */ 127bf215546Sopenharmony_ci if (entry->var->data.mode == ir_var_uniform || 128bf215546Sopenharmony_ci entry->var->data.mode == ir_var_shader_storage) { 129bf215546Sopenharmony_ci if (entry->var->constant_initializer) 130bf215546Sopenharmony_ci continue; 131bf215546Sopenharmony_ci 132bf215546Sopenharmony_ci /* Section 2.11.6 (Uniform Variables) of the OpenGL ES 3.0.3 spec 133bf215546Sopenharmony_ci * says: 134bf215546Sopenharmony_ci * 135bf215546Sopenharmony_ci * "All members of a named uniform block declared with a 136bf215546Sopenharmony_ci * shared or std140 layout qualifier are considered active, 137bf215546Sopenharmony_ci * even if they are not referenced in any shader in the 138bf215546Sopenharmony_ci * program. The uniform block itself is also considered 139bf215546Sopenharmony_ci * active, even if no member of the block is referenced." 140bf215546Sopenharmony_ci * 141bf215546Sopenharmony_ci * If the variable is in a uniform block with one of those 142bf215546Sopenharmony_ci * layouts, do not eliminate it. 143bf215546Sopenharmony_ci */ 144bf215546Sopenharmony_ci if (entry->var->is_in_buffer_block()) { 145bf215546Sopenharmony_ci if (entry->var->get_interface_type_packing() != 146bf215546Sopenharmony_ci GLSL_INTERFACE_PACKING_PACKED) { 147bf215546Sopenharmony_ci /* Set used to false so it doesn't get set as referenced by 148bf215546Sopenharmony_ci * the shader in the program resource list. This will also 149bf215546Sopenharmony_ci * help avoid the state being unnecessarily flushed for the 150bf215546Sopenharmony_ci * shader stage. 151bf215546Sopenharmony_ci */ 152bf215546Sopenharmony_ci entry->var->data.used = false; 153bf215546Sopenharmony_ci continue; 154bf215546Sopenharmony_ci } 155bf215546Sopenharmony_ci } 156bf215546Sopenharmony_ci 157bf215546Sopenharmony_ci if (entry->var->type->is_subroutine()) 158bf215546Sopenharmony_ci continue; 159bf215546Sopenharmony_ci } 160bf215546Sopenharmony_ci 161bf215546Sopenharmony_ci entry->var->remove(); 162bf215546Sopenharmony_ci progress = true; 163bf215546Sopenharmony_ci 164bf215546Sopenharmony_ci if (debug) { 165bf215546Sopenharmony_ci printf("Removed declaration of %s@%p\n", 166bf215546Sopenharmony_ci entry->var->name, (void *) entry->var); 167bf215546Sopenharmony_ci } 168bf215546Sopenharmony_ci } 169bf215546Sopenharmony_ci } 170bf215546Sopenharmony_ci 171bf215546Sopenharmony_ci return progress; 172bf215546Sopenharmony_ci} 173bf215546Sopenharmony_ci 174bf215546Sopenharmony_ci/** 175bf215546Sopenharmony_ci * Does a dead code pass on the functions present in the instruction stream. 176bf215546Sopenharmony_ci * 177bf215546Sopenharmony_ci * This is suitable for use while the program is not linked, as it will 178bf215546Sopenharmony_ci * ignore variable declarations (and the assignments to them) for variables 179bf215546Sopenharmony_ci * with global scope. 180bf215546Sopenharmony_ci */ 181bf215546Sopenharmony_cibool 182bf215546Sopenharmony_cido_dead_code_unlinked(exec_list *instructions) 183bf215546Sopenharmony_ci{ 184bf215546Sopenharmony_ci bool progress = false; 185bf215546Sopenharmony_ci 186bf215546Sopenharmony_ci foreach_in_list(ir_instruction, ir, instructions) { 187bf215546Sopenharmony_ci ir_function *f = ir->as_function(); 188bf215546Sopenharmony_ci if (f) { 189bf215546Sopenharmony_ci foreach_in_list(ir_function_signature, sig, &f->signatures) { 190bf215546Sopenharmony_ci if (do_dead_code(&sig->body)) 191bf215546Sopenharmony_ci progress = true; 192bf215546Sopenharmony_ci } 193bf215546Sopenharmony_ci } 194bf215546Sopenharmony_ci } 195bf215546Sopenharmony_ci 196bf215546Sopenharmony_ci return progress; 197bf215546Sopenharmony_ci} 198