glsl: move to compiler/
[mesa.git] / src / compiler / 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 "compiler/glsl_types.h"
39 #include "util/hash_table.h"
40
41 namespace {
42
43 struct assignment_entry {
44 int assignment_count;
45 ir_variable *var;
46 ir_constant *constval;
47 bool our_scope;
48 };
49
50 class ir_constant_variable_visitor : public ir_hierarchical_visitor {
51 public:
52 virtual ir_visitor_status visit_enter(ir_dereference_variable *);
53 virtual ir_visitor_status visit(ir_variable *);
54 virtual ir_visitor_status visit_enter(ir_assignment *);
55 virtual ir_visitor_status visit_enter(ir_call *);
56
57 struct hash_table *ht;
58 };
59
60 } /* unnamed namespace */
61
62 static struct assignment_entry *
63 get_assignment_entry(ir_variable *var, struct hash_table *ht)
64 {
65 struct hash_entry *hte = _mesa_hash_table_search(ht, var);
66 struct assignment_entry *entry;
67
68 if (hte) {
69 entry = (struct assignment_entry *) hte->data;
70 } else {
71 entry = (struct assignment_entry *) calloc(1, sizeof(*entry));
72 entry->var = var;
73 _mesa_hash_table_insert(ht, var, entry);
74 }
75
76 return entry;
77 }
78
79 ir_visitor_status
80 ir_constant_variable_visitor::visit(ir_variable *ir)
81 {
82 struct assignment_entry *entry = get_assignment_entry(ir, this->ht);
83 entry->our_scope = true;
84 return visit_continue;
85 }
86
87 /* Skip derefs of variables so that we can detect declarations. */
88 ir_visitor_status
89 ir_constant_variable_visitor::visit_enter(ir_dereference_variable *ir)
90 {
91 (void)ir;
92 return visit_continue_with_parent;
93 }
94
95 ir_visitor_status
96 ir_constant_variable_visitor::visit_enter(ir_assignment *ir)
97 {
98 ir_constant *constval;
99 struct assignment_entry *entry;
100
101 entry = get_assignment_entry(ir->lhs->variable_referenced(), this->ht);
102 assert(entry);
103 entry->assignment_count++;
104
105 /* If it's already constant, don't do the work. */
106 if (entry->var->constant_value)
107 return visit_continue;
108
109 /* OK, now find if we actually have all the right conditions for
110 * this to be a constant value assigned to the var.
111 */
112 if (ir->condition)
113 return visit_continue;
114
115 ir_variable *var = ir->whole_variable_written();
116 if (!var)
117 return visit_continue;
118
119 /* Ignore buffer variables, since the underlying storage is shared
120 * and we can't be sure that this variable won't be written by another
121 * thread.
122 */
123 if (var->data.mode == ir_var_shader_storage ||
124 var->data.mode == ir_var_shader_shared)
125 return visit_continue;
126
127 constval = ir->rhs->constant_expression_value();
128 if (!constval)
129 return visit_continue;
130
131 /* Mark this entry as having a constant assignment (if the
132 * assignment count doesn't go >1). do_constant_variable will fix
133 * up the variable with the constant value later.
134 */
135 entry->constval = constval;
136
137 return visit_continue;
138 }
139
140 ir_visitor_status
141 ir_constant_variable_visitor::visit_enter(ir_call *ir)
142 {
143 /* Mark any out parameters as assigned to */
144 foreach_two_lists(formal_node, &ir->callee->parameters,
145 actual_node, &ir->actual_parameters) {
146 ir_rvalue *param_rval = (ir_rvalue *) actual_node;
147 ir_variable *param = (ir_variable *) formal_node;
148
149 if (param->data.mode == ir_var_function_out ||
150 param->data.mode == ir_var_function_inout) {
151 ir_variable *var = param_rval->variable_referenced();
152 struct assignment_entry *entry;
153
154 assert(var);
155 entry = get_assignment_entry(var, this->ht);
156 entry->assignment_count++;
157 }
158 }
159
160 /* Mark the return storage as having been assigned to */
161 if (ir->return_deref != NULL) {
162 ir_variable *var = ir->return_deref->variable_referenced();
163 struct assignment_entry *entry;
164
165 assert(var);
166 entry = get_assignment_entry(var, this->ht);
167 entry->assignment_count++;
168 }
169
170 return visit_continue;
171 }
172
173 /**
174 * Does a copy propagation pass on the code present in the instruction stream.
175 */
176 bool
177 do_constant_variable(exec_list *instructions)
178 {
179 bool progress = false;
180 ir_constant_variable_visitor v;
181
182 v.ht = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
183 _mesa_key_pointer_equal);
184 v.run(instructions);
185
186 struct hash_entry *hte;
187 hash_table_foreach(v.ht, hte) {
188 struct assignment_entry *entry = (struct assignment_entry *) hte->data;
189
190 if (entry->assignment_count == 1 && entry->constval && entry->our_scope) {
191 entry->var->constant_value = entry->constval;
192 progress = true;
193 }
194 hte->data = NULL;
195 free(entry);
196 }
197 _mesa_hash_table_destroy(v.ht, NULL);
198
199 return progress;
200 }
201
202 bool
203 do_constant_variable_unlinked(exec_list *instructions)
204 {
205 bool progress = false;
206
207 foreach_in_list(ir_instruction, ir, instructions) {
208 ir_function *f = ir->as_function();
209 if (f) {
210 foreach_in_list(ir_function_signature, sig, &f->signatures) {
211 if (do_constant_variable(&sig->body))
212 progress = true;
213 }
214 }
215 }
216
217 return progress;
218 }