blob: 0578f1737b050becd8405eddbcc58f8bc16ffe05 [file] [log] [blame]
Eric Anholt7d211042010-04-16 16:43:47 -07001/*
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 Versacedf883eb2010-11-17 10:43:10 -080025 * \file opt_dead_code.cpp
Eric Anholt7d211042010-04-16 16:43:47 -070026 *
27 * Eliminates dead assignments and variable declarations from the code.
28 */
29
Eric Anholt7d211042010-04-16 16:43:47 -070030#include "ir.h"
31#include "ir_visitor.h"
Eric Anholtd72edc42010-07-30 16:05:27 -070032#include "ir_variable_refcount.h"
Eric Anholt7d211042010-04-16 16:43:47 -070033#include "glsl_types.h"
34
Eric Anholt3bd7e702010-08-05 12:07:23 -070035static bool debug = false;
36
Eric Anholt7d211042010-04-16 16:43:47 -070037/**
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 */
44bool
Ian Romanick1d5d67f2011-10-21 11:17:39 -070045do_dead_code(exec_list *instructions, bool uniform_locations_assigned)
Eric Anholt7d211042010-04-16 16:43:47 -070046{
Eric Anholtd72edc42010-07-30 16:05:27 -070047 ir_variable_refcount_visitor v;
Eric Anholt7d211042010-04-16 16:43:47 -070048 bool progress = false;
49
Ian Romanickb5a7cf92010-05-14 12:41:22 -070050 v.run(instructions);
Eric Anholt7d211042010-04-16 16:43:47 -070051
52 foreach_iter(exec_list_iterator, iter, v.variable_list) {
Kenneth Graunkee3b52002012-01-28 16:58:37 -080053 ir_variable_refcount_entry *entry = (ir_variable_refcount_entry *)iter.get();
Eric Anholt7d211042010-04-16 16:43:47 -070054
Ian Romanickb5a7cf92010-05-14 12:41:22 -070055 /* 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 Anholt3bd7e702010-08-05 12:07:23 -070065 if (debug) {
66 printf("%s@%p: %d refs, %d assigns, %sdeclared in our scope\n",
Brian Paul9f9386d2010-08-11 14:04:51 -060067 entry->var->name, (void *) entry->var,
Eric Anholt3bd7e702010-08-05 12:07:23 -070068 entry->referenced_count, entry->assigned_count,
69 entry->declaration ? "" : "not ");
70 }
71
Ian Romanickb5a7cf92010-05-14 12:41:22 -070072 if ((entry->referenced_count > entry->assigned_count)
73 || !entry->declaration)
Eric Anholt7d211042010-04-16 16:43:47 -070074 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 Anholt046bef22010-08-04 20:33:57 -070080 if (entry->var->mode != ir_var_out &&
Kenneth Graunked884f602012-03-20 15:56:37 -070081 entry->var->mode != ir_var_inout) {
Eric Anholt7d211042010-04-16 16:43:47 -070082 entry->assign->remove();
83 progress = true;
Eric Anholt3bd7e702010-08-05 12:07:23 -070084
85 if (debug) {
86 printf("Removed assignment to %s@%p\n",
Brian Paul9f9386d2010-08-11 14:04:51 -060087 entry->var->name, (void *) entry->var);
Eric Anholt3bd7e702010-08-05 12:07:23 -070088 }
Eric Anholt7d211042010-04-16 16:43:47 -070089 }
90 } else {
91 /* If there are no assignments or references to the variable left,
92 * then we can remove its declaration.
93 */
Eric Anholt7de4d8f2010-08-24 15:30:42 -070094
95 /* uniform initializers are precious, and could get used by another
Ian Romanick1d5d67f2011-10-21 11:17:39 -070096 * stage. Also, once uniform locations have been assigned, the
97 * declaration cannot be deleted.
Eric Anholt7de4d8f2010-08-24 15:30:42 -070098 */
99 if (entry->var->mode == ir_var_uniform &&
Ian Romanick1d5d67f2011-10-21 11:17:39 -0700100 (uniform_locations_assigned || entry->var->constant_value))
Eric Anholt7de4d8f2010-08-24 15:30:42 -0700101 continue;
102
Eric Anholt7d211042010-04-16 16:43:47 -0700103 entry->var->remove();
104 progress = true;
Eric Anholt3bd7e702010-08-05 12:07:23 -0700105
106 if (debug) {
107 printf("Removed declaration of %s@%p\n",
Brian Paul9f9386d2010-08-11 14:04:51 -0600108 entry->var->name, (void *) entry->var);
Eric Anholt3bd7e702010-08-05 12:07:23 -0700109 }
Eric Anholt7d211042010-04-16 16:43:47 -0700110 }
111 }
Eric Anholt66d4c652010-07-27 11:28:26 -0700112
Eric Anholt7d211042010-04-16 16:43:47 -0700113 return progress;
114}
115
116/**
117 * Does a dead code pass on the functions present in the instruction stream.
118 *
119 * This is suitable for use while the program is not linked, as it will
120 * ignore variable declarations (and the assignments to them) for variables
121 * with global scope.
122 */
123bool
Eric Anholt66d4c652010-07-27 11:28:26 -0700124do_dead_code_unlinked(exec_list *instructions)
Eric Anholt7d211042010-04-16 16:43:47 -0700125{
126 bool progress = false;
127
128 foreach_iter(exec_list_iterator, iter, *instructions) {
129 ir_instruction *ir = (ir_instruction *)iter.get();
Kenneth Graunke6202cbf2010-04-21 16:02:15 -0700130 ir_function *f = ir->as_function();
131 if (f) {
132 foreach_iter(exec_list_iterator, sigiter, *f) {
133 ir_function_signature *sig =
134 (ir_function_signature *) sigiter.get();
Ian Romanick1d5d67f2011-10-21 11:17:39 -0700135 /* The setting of the uniform_locations_assigned flag here is
136 * irrelevent. If there is a uniform declaration encountered
137 * inside the body of the function, something has already gone
138 * terribly, terribly wrong.
139 */
140 if (do_dead_code(&sig->body, false))
Kenneth Graunke6202cbf2010-04-21 16:02:15 -0700141 progress = true;
142 }
Eric Anholt7d211042010-04-16 16:43:47 -0700143 }
144 }
145
146 return progress;
147}