ralloc: Add missing va_end following va_copy.
[mesa.git] / src / glsl / opt_constant_variable.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_constant_variable.cpp
26 *
27 * Marks variables assigned a single constant value over the course
28 * of the program as constant.
29 *
30 * The goal here is to trigger further constant folding and then dead
31 * code elimination. This is common with vector/matrix constructors
32 * and calls to builtin functions.
33 */
34
35 #include "ir.h"
36 #include "ir_visitor.h"
37 #include "ir_optimization.h"
38 #include "glsl_types.h"
39
40 using std::calloc;
41 using std::free;
42
43 struct assignment_entry {
44 exec_node link;
45 int assignment_count;
46 ir_variable *var;
47 ir_constant *constval;
48 bool our_scope;
49 };
50
51 class ir_constant_variable_visitor : public ir_hierarchical_visitor {
52 public:
53 virtual ir_visitor_status visit_enter(ir_dereference_variable *);
54 virtual ir_visitor_status visit(ir_variable *);
55 virtual ir_visitor_status visit_enter(ir_assignment *);
56 virtual ir_visitor_status visit_enter(ir_call *);
57
58 exec_list list;
59 };
60
61 static struct assignment_entry *
62 get_assignment_entry(ir_variable *var, exec_list *list)
63 {
64 struct assignment_entry *entry;
65
66 foreach_list_typed(struct assignment_entry, entry, link, list) {
67 if (entry->var == var)
68 return entry;
69 }
70
71 entry = (struct assignment_entry *)calloc(1, sizeof(*entry));
72 entry->var = var;
73 list->push_head(&entry->link);
74 return entry;
75 }
76
77 ir_visitor_status
78 ir_constant_variable_visitor::visit(ir_variable *ir)
79 {
80 struct assignment_entry *entry = get_assignment_entry(ir, &this->list);
81 entry->our_scope = true;
82 return visit_continue;
83 }
84
85 /* Skip derefs of variables so that we can detect declarations. */
86 ir_visitor_status
87 ir_constant_variable_visitor::visit_enter(ir_dereference_variable *ir)
88 {
89 (void)ir;
90 return visit_continue_with_parent;
91 }
92
93 ir_visitor_status
94 ir_constant_variable_visitor::visit_enter(ir_assignment *ir)
95 {
96 ir_constant *constval;
97 struct assignment_entry *entry;
98
99 entry = get_assignment_entry(ir->lhs->variable_referenced(), &this->list);
100 assert(entry);
101 entry->assignment_count++;
102
103 /* If it's already constant, don't do the work. */
104 if (entry->var->constant_value)
105 return visit_continue;
106
107 /* OK, now find if we actually have all the right conditions for
108 * this to be a constant value assigned to the var.
109 */
110 if (ir->condition) {
111 constval = ir->condition->constant_expression_value();
112 if (!constval || !constval->value.b[0])
113 return visit_continue;
114 }
115
116 ir_variable *var = ir->whole_variable_written();
117 if (!var)
118 return visit_continue;
119
120 constval = ir->rhs->constant_expression_value();
121 if (!constval)
122 return visit_continue;
123
124 /* Mark this entry as having a constant assignment (if the
125 * assignment count doesn't go >1). do_constant_variable will fix
126 * up the variable with the constant value later.
127 */
128 entry->constval = constval;
129
130 return visit_continue;
131 }
132
133 ir_visitor_status
134 ir_constant_variable_visitor::visit_enter(ir_call *ir)
135 {
136 exec_list_iterator sig_iter = ir->get_callee()->parameters.iterator();
137 foreach_iter(exec_list_iterator, iter, *ir) {
138 ir_rvalue *param_rval = (ir_rvalue *)iter.get();
139 ir_variable *param = (ir_variable *)sig_iter.get();
140
141 if (param->mode == ir_var_out ||
142 param->mode == ir_var_inout) {
143 ir_variable *var = param_rval->variable_referenced();
144 struct assignment_entry *entry;
145
146 assert(var);
147 entry = get_assignment_entry(var, &this->list);
148 entry->assignment_count++;
149 }
150 sig_iter.next();
151 }
152 return visit_continue;
153 }
154
155 /**
156 * Does a copy propagation pass on the code present in the instruction stream.
157 */
158 bool
159 do_constant_variable(exec_list *instructions)
160 {
161 bool progress = false;
162 ir_constant_variable_visitor v;
163
164 v.run(instructions);
165
166 while (!v.list.is_empty()) {
167
168 struct assignment_entry *entry;
169 entry = exec_node_data(struct assignment_entry, v.list.head, link);
170
171 if (entry->assignment_count == 1 && entry->constval && entry->our_scope) {
172 entry->var->constant_value = entry->constval;
173 progress = true;
174 }
175 entry->link.remove();
176 free(entry);
177 }
178
179 return progress;
180 }
181
182 bool
183 do_constant_variable_unlinked(exec_list *instructions)
184 {
185 bool progress = false;
186
187 foreach_iter(exec_list_iterator, iter, *instructions) {
188 ir_instruction *ir = (ir_instruction *)iter.get();
189 ir_function *f = ir->as_function();
190 if (f) {
191 foreach_iter(exec_list_iterator, sigiter, *f) {
192 ir_function_signature *sig =
193 (ir_function_signature *) sigiter.get();
194 if (do_constant_variable(&sig->body))
195 progress = true;
196 }
197 }
198 }
199
200 return progress;
201 }