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 using ir_hierarchical_visitor::visit;
53 using ir_hierarchical_visitor::visit_enter;
54
55 virtual ir_visitor_status visit_enter(ir_dereference_variable *);
56 virtual ir_visitor_status visit(ir_variable *);
57 virtual ir_visitor_status visit_enter(ir_assignment *);
58 virtual ir_visitor_status visit_enter(ir_call *);
59
60 struct hash_table *ht;
61 };
62
63 } /* unnamed namespace */
64
65 static struct assignment_entry *
get_assignment_entry(ir_variable * var,struct hash_table * ht)66 get_assignment_entry(ir_variable *var, struct hash_table *ht)
67 {
68 struct hash_entry *hte = _mesa_hash_table_search(ht, var);
69 struct assignment_entry *entry;
70
71 if (hte) {
72 entry = (struct assignment_entry *) hte->data;
73 } else {
74 entry = (struct assignment_entry *) calloc(1, sizeof(*entry));
75 entry->var = var;
76 _mesa_hash_table_insert(ht, var, entry);
77 }
78
79 return entry;
80 }
81
82 ir_visitor_status
visit(ir_variable * ir)83 ir_constant_variable_visitor::visit(ir_variable *ir)
84 {
85 struct assignment_entry *entry = get_assignment_entry(ir, this->ht);
86 entry->our_scope = true;
87 return visit_continue;
88 }
89
90 /* Skip derefs of variables so that we can detect declarations. */
91 ir_visitor_status
visit_enter(ir_dereference_variable * ir)92 ir_constant_variable_visitor::visit_enter(ir_dereference_variable *ir)
93 {
94 (void)ir;
95 return visit_continue_with_parent;
96 }
97
98 ir_visitor_status
visit_enter(ir_assignment * ir)99 ir_constant_variable_visitor::visit_enter(ir_assignment *ir)
100 {
101 ir_constant *constval;
102 struct assignment_entry *entry;
103
104 entry = get_assignment_entry(ir->lhs->variable_referenced(), this->ht);
105 assert(entry);
106 entry->assignment_count++;
107
108 /* If there's more than one assignment, don't bother - we won't do anything
109 * with this variable anyway, and continuing just wastes memory cloning
110 * constant expressions.
111 */
112 if (entry->assignment_count > 1)
113 return visit_continue;
114
115 /* If it's already constant, don't do the work. */
116 if (entry->var->constant_value)
117 return visit_continue;
118
119 ir_variable *var = ir->whole_variable_written();
120 if (!var)
121 return visit_continue;
122
123 /* Ignore buffer variables, since the underlying storage is shared
124 * and we can't be sure that this variable won't be written by another
125 * thread.
126 */
127 if (var->data.mode == ir_var_shader_storage ||
128 var->data.mode == ir_var_shader_shared)
129 return visit_continue;
130
131 constval = ir->rhs->constant_expression_value(ralloc_parent(ir));
132 if (!constval)
133 return visit_continue;
134
135 /* Mark this entry as having a constant assignment (if the
136 * assignment count doesn't go >1). do_constant_variable will fix
137 * up the variable with the constant value later.
138 */
139 entry->constval = constval;
140
141 return visit_continue;
142 }
143
144 ir_visitor_status
visit_enter(ir_call * ir)145 ir_constant_variable_visitor::visit_enter(ir_call *ir)
146 {
147 /* Mark any out parameters as assigned to */
148 foreach_two_lists(formal_node, &ir->callee->parameters,
149 actual_node, &ir->actual_parameters) {
150 ir_rvalue *param_rval = (ir_rvalue *) actual_node;
151 ir_variable *param = (ir_variable *) formal_node;
152
153 if (param->data.mode == ir_var_function_out ||
154 param->data.mode == ir_var_function_inout) {
155 ir_variable *var = param_rval->variable_referenced();
156 struct assignment_entry *entry;
157
158 assert(var);
159 entry = get_assignment_entry(var, this->ht);
160 entry->assignment_count++;
161 }
162
163 /* We don't know if the variable passed to this function has been
164 * assigned a value or if it is undefined, so for now we always assume
165 * it has been assigned a value. Once functions have been inlined any
166 * further potential optimisations will be taken care of.
167 */
168 struct assignment_entry *entry;
169 entry = get_assignment_entry(param, this->ht);
170 entry->assignment_count++;
171 }
172
173 /* Mark the return storage as having been assigned to */
174 if (ir->return_deref != NULL) {
175 ir_variable *var = ir->return_deref->variable_referenced();
176 struct assignment_entry *entry;
177
178 assert(var);
179 entry = get_assignment_entry(var, this->ht);
180 entry->assignment_count++;
181 }
182
183 return visit_continue;
184 }
185
186 /**
187 * Does a copy propagation pass on the code present in the instruction stream.
188 */
189 bool
do_constant_variable(exec_list * instructions)190 do_constant_variable(exec_list *instructions)
191 {
192 bool progress = false;
193 ir_constant_variable_visitor v;
194
195 v.ht = _mesa_pointer_hash_table_create(NULL);
196 v.run(instructions);
197
198 hash_table_foreach(v.ht, hte) {
199 struct assignment_entry *entry = (struct assignment_entry *) hte->data;
200
201 if (entry->assignment_count == 1 && entry->constval && entry->our_scope) {
202 entry->var->constant_value = entry->constval;
203 progress = true;
204 }
205 hte->data = NULL;
206 free(entry);
207 }
208 _mesa_hash_table_destroy(v.ht, NULL);
209
210 return progress;
211 }
212
213 bool
do_constant_variable_unlinked(exec_list * instructions)214 do_constant_variable_unlinked(exec_list *instructions)
215 {
216 bool progress = false;
217
218 foreach_in_list(ir_instruction, ir, instructions) {
219 ir_function *f = ir->as_function();
220 if (f) {
221 foreach_in_list(ir_function_signature, sig, &f->signatures) {
222 if (do_constant_variable(&sig->body))
223 progress = true;
224 }
225 }
226 }
227
228 return progress;
229 }
230