xref: /aosp_15_r20/external/mesa3d/src/compiler/glsl/opt_dead_code.cpp (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 opt_dead_code.cpp
26  *
27  * Eliminates dead assignments and variable declarations from the code.
28  */
29 
30 #include "ir.h"
31 #include "ir_visitor.h"
32 #include "ir_variable_refcount.h"
33 #include "compiler/glsl_types.h"
34 #include "util/hash_table.h"
35 
36 static bool debug = false;
37 
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
do_dead_code(exec_list * instructions)46 do_dead_code(exec_list *instructions)
47 {
48    ir_variable_refcount_visitor v;
49    bool progress = false;
50 
51    v.run(instructions);
52 
53    hash_table_foreach(v.ht, e) {
54       ir_variable_refcount_entry *entry = (ir_variable_refcount_entry *)e->data;
55 
56       /* Since each assignment is a reference, the refereneced count must be
57        * greater than or equal to the assignment count.  If they are equal,
58        * then all of the references are assignments, and the variable is
59        * dead.
60        *
61        * Note that if the variable is neither assigned nor referenced, both
62        * counts will be zero and will be caught by the equality test.
63        */
64       assert(entry->referenced_count >= entry->assigned_count);
65 
66       if (debug) {
67 	 printf("%s@%p: %d refs, %d assigns, %sdeclared in our scope\n",
68 		entry->var->name, (void *) entry->var,
69 		entry->referenced_count, entry->assigned_count,
70 		entry->declaration ? "" : "not ");
71       }
72 
73       if ((entry->referenced_count > entry->assigned_count)
74 	  || !entry->declaration)
75 	 continue;
76 
77       if (!entry->assign_list.is_empty()) {
78 	 /* Remove all the dead assignments to the variable we found.
79 	  * Don't do so if it's a shader or function output, though.
80 	  */
81 	 if (entry->var->data.mode != ir_var_function_out &&
82 	     entry->var->data.mode != ir_var_function_inout &&
83              entry->var->data.mode != ir_var_shader_out &&
84              entry->var->data.mode != ir_var_shader_storage) {
85 
86             while (!entry->assign_list.is_empty()) {
87                struct assignment_entry *assignment_entry =
88                   exec_node_data(struct assignment_entry,
89                                  entry->assign_list.get_head_raw(), link);
90 
91 	       assignment_entry->assign->remove();
92 
93 	       if (debug) {
94 	          printf("Removed assignment to %s@%p\n",
95 		         entry->var->name, (void *) entry->var);
96                }
97 
98                assignment_entry->link.remove();
99                free(assignment_entry);
100             }
101             progress = true;
102 	 }
103       }
104 
105       if (entry->assign_list.is_empty()) {
106 	 /* If there are no assignments or references to the variable left,
107 	  * then we can remove its declaration.
108 	  */
109 
110 	 /* uniform initializers are precious, and could get used by another
111 	  * stage.
112 	  */
113          if (entry->var->data.mode == ir_var_uniform ||
114              entry->var->data.mode == ir_var_shader_storage) {
115             if (entry->var->constant_initializer)
116                continue;
117 
118             /* Section 2.11.6 (Uniform Variables) of the OpenGL ES 3.0.3 spec
119              * says:
120              *
121              *     "All members of a named uniform block declared with a
122              *     shared or std140 layout qualifier are considered active,
123              *     even if they are not referenced in any shader in the
124              *     program. The uniform block itself is also considered
125              *     active, even if no member of the block is referenced."
126              *
127              * If the variable is in a uniform block with one of those
128              * layouts, do not eliminate it.
129              */
130             if (entry->var->is_in_buffer_block()) {
131                if (entry->var->get_interface_type_packing() !=
132                    GLSL_INTERFACE_PACKING_PACKED) {
133                   /* Set used to false so it doesn't get set as referenced by
134                    * the shader in the program resource list. This will also
135                    * help avoid the state being unnecessarily flushed for the
136                    * shader stage.
137                    */
138                   entry->var->data.used = false;
139                   continue;
140                }
141             }
142 
143             if (glsl_type_is_subroutine(entry->var->type))
144                continue;
145          }
146 
147 	 entry->var->remove();
148 	 progress = true;
149 
150 	 if (debug) {
151 	    printf("Removed declaration of %s@%p\n",
152 		   entry->var->name, (void *) entry->var);
153 	 }
154       }
155    }
156 
157    return progress;
158 }
159 
160 /**
161  * Does a dead code pass on the functions present in the instruction stream.
162  *
163  * This is suitable for use while the program is not linked, as it will
164  * ignore variable declarations (and the assignments to them) for variables
165  * with global scope.
166  */
167 bool
do_dead_code_unlinked(exec_list * instructions)168 do_dead_code_unlinked(exec_list *instructions)
169 {
170    bool progress = false;
171 
172    foreach_in_list(ir_instruction, ir, instructions) {
173       ir_function *f = ir->as_function();
174       if (f) {
175 	 foreach_in_list(ir_function_signature, sig, &f->signatures) {
176 	    if (do_dead_code(&sig->body))
177 	       progress = true;
178 	 }
179       }
180    }
181 
182    return progress;
183 }
184