glsl: Add using statements for standard library functions.
[mesa.git] / src / glsl / opt_dead_code.cpp
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 "glsl_types.h"
34
35 using std::printf;
36
37 static bool debug = false;
38
39 /**
40 * Do a dead code pass over instructions and everything that instructions
41 * references.
42 *
43 * Note that this will remove assignments to globals, so it is not suitable
44 * for usage on an unlinked instruction stream.
45 */
46 bool
47 do_dead_code(exec_list *instructions)
48 {
49 ir_variable_refcount_visitor v;
50 bool progress = false;
51
52 v.run(instructions);
53
54 foreach_iter(exec_list_iterator, iter, v.variable_list) {
55 variable_entry *entry = (variable_entry *)iter.get();
56
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
67 if (debug) {
68 printf("%s@%p: %d refs, %d assigns, %sdeclared in our scope\n",
69 entry->var->name, (void *) entry->var,
70 entry->referenced_count, entry->assigned_count,
71 entry->declaration ? "" : "not ");
72 }
73
74 if ((entry->referenced_count > entry->assigned_count)
75 || !entry->declaration)
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 */
82 if (entry->var->mode != ir_var_out &&
83 entry->var->mode != ir_var_inout &&
84 !ir_has_call(entry->assign)) {
85 entry->assign->remove();
86 progress = true;
87
88 if (debug) {
89 printf("Removed assignment to %s@%p\n",
90 entry->var->name, (void *) entry->var);
91 }
92 }
93 } else {
94 /* If there are no assignments or references to the variable left,
95 * then we can remove its declaration.
96 */
97
98 /* uniform initializers are precious, and could get used by another
99 * stage.
100 */
101 if (entry->var->mode == ir_var_uniform &&
102 entry->var->constant_value)
103 continue;
104
105 entry->var->remove();
106 progress = true;
107
108 if (debug) {
109 printf("Removed declaration of %s@%p\n",
110 entry->var->name, (void *) entry->var);
111 }
112 }
113 }
114
115 return progress;
116 }
117
118 /**
119 * Does a dead code pass on the functions present in the instruction stream.
120 *
121 * This is suitable for use while the program is not linked, as it will
122 * ignore variable declarations (and the assignments to them) for variables
123 * with global scope.
124 */
125 bool
126 do_dead_code_unlinked(exec_list *instructions)
127 {
128 bool progress = false;
129
130 foreach_iter(exec_list_iterator, iter, *instructions) {
131 ir_instruction *ir = (ir_instruction *)iter.get();
132 ir_function *f = ir->as_function();
133 if (f) {
134 foreach_iter(exec_list_iterator, sigiter, *f) {
135 ir_function_signature *sig =
136 (ir_function_signature *) sigiter.get();
137 if (do_dead_code(&sig->body))
138 progress = true;
139 }
140 }
141 }
142
143 return progress;
144 }