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