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