glsl: Add an array splitting pass.
[mesa.git] / src / glsl / opt_array_splitting.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_array_splitting.cpp
26 *
27 * If an array is always dereferenced with a constant index, then
28 * split it apart into its elements, making it more amenable to other
29 * optimization passes.
30 *
31 * This skips uniform/varying arrays, which would need careful
32 * handling due to their ir->location fields tying them to the GL API
33 * and other shader stages.
34 */
35
36 #include "ir.h"
37 #include "ir_visitor.h"
38 #include "ir_rvalue_visitor.h"
39 #include "ir_print_visitor.h"
40 #include "glsl_types.h"
41
42 static bool debug = false;
43
44 namespace opt_array_splitting {
45
46 class variable_entry : public exec_node
47 {
48 public:
49 variable_entry(ir_variable *var)
50 {
51 this->var = var;
52 this->whole_array_access = 0;
53 this->declaration = false;
54 this->components = NULL;
55 this->mem_ctx = NULL;
56 }
57
58 ir_variable *var; /* The key: the variable's pointer. */
59
60 /** Number of times the variable is referenced, including assignments. */
61 unsigned whole_array_access;
62
63 bool declaration; /* If the variable had a decl in the instruction stream */
64
65 ir_variable **components;
66
67 /** ralloc_parent(this->var) -- the shader's talloc context. */
68 void *mem_ctx;
69 };
70
71 } /* namespace */
72 using namespace opt_array_splitting;
73
74 /**
75 * This class does a walk over the tree, coming up with the set of
76 * variables that could be split by looking to see if they are arrays
77 * that are only ever constant-index dereferenced.
78 */
79 class ir_array_reference_visitor : public ir_hierarchical_visitor {
80 public:
81 ir_array_reference_visitor(void)
82 {
83 this->mem_ctx = ralloc_context(NULL);
84 this->variable_list.make_empty();
85 }
86
87 ~ir_array_reference_visitor(void)
88 {
89 ralloc_free(mem_ctx);
90 }
91
92 bool get_split_list(exec_list *instructions, bool linked);
93
94 virtual ir_visitor_status visit(ir_variable *);
95 virtual ir_visitor_status visit(ir_dereference_variable *);
96 virtual ir_visitor_status visit_enter(ir_dereference_array *);
97
98 variable_entry *get_variable_entry(ir_variable *var);
99
100 /* List of variable_entry */
101 exec_list variable_list;
102
103 void *mem_ctx;
104 };
105
106 variable_entry *
107 ir_array_reference_visitor::get_variable_entry(ir_variable *var)
108 {
109 assert(var);
110
111 if (var->mode != ir_var_auto &&
112 var->mode != ir_var_temporary)
113 return NULL;
114
115 if (!var->type->is_array())
116 return NULL;
117
118 /* If the array hasn't been sized yet, we can't split it. After
119 * linking, this should be resolved.
120 */
121 if (var->type->length == 0)
122 return NULL;
123
124 foreach_iter(exec_list_iterator, iter, this->variable_list) {
125 variable_entry *entry = (variable_entry *)iter.get();
126 if (entry->var == var)
127 return entry;
128 }
129
130 variable_entry *entry = new(mem_ctx) variable_entry(var);
131 this->variable_list.push_tail(entry);
132 return entry;
133 }
134
135
136 ir_visitor_status
137 ir_array_reference_visitor::visit(ir_variable *ir)
138 {
139 variable_entry *entry = this->get_variable_entry(ir);
140
141 if (entry)
142 entry->declaration = true;
143
144 return visit_continue;
145 }
146
147 ir_visitor_status
148 ir_array_reference_visitor::visit(ir_dereference_variable *ir)
149 {
150 variable_entry *entry = this->get_variable_entry(ir->var);
151
152 /* If we made it to here, then the dereference of this array didn't
153 * have a constant index (see the visit_continue_with_parent
154 * below), so we can't split the variable.
155 */
156 if (entry)
157 entry->whole_array_access++;
158
159 return visit_continue;
160 }
161
162 ir_visitor_status
163 ir_array_reference_visitor::visit_enter(ir_dereference_array *ir)
164 {
165 ir_dereference_variable *deref = ir->array->as_dereference_variable();
166 if (!deref)
167 return visit_continue;
168
169 variable_entry *entry = this->get_variable_entry(deref->var);
170
171 if (entry && !ir->array_index->as_constant())
172 entry->whole_array_access++;
173
174 return visit_continue_with_parent;
175 }
176
177 bool
178 ir_array_reference_visitor::get_split_list(exec_list *instructions,
179 bool linked)
180 {
181 visit_list_elements(this, instructions);
182
183 /* If the shaders aren't linked yet, we can't mess with global
184 * declarations, which need to be matched by name across shaders.
185 */
186 if (!linked) {
187 foreach_list(node, instructions) {
188 ir_variable *var = ((ir_instruction *)node)->as_variable();
189 if (var) {
190 variable_entry *entry = get_variable_entry(var);
191 if (entry)
192 entry->remove();
193 }
194 }
195 }
196
197 /* Trim out variables we found that we can't split. */
198 foreach_iter(exec_list_iterator, iter, variable_list) {
199 variable_entry *entry = (variable_entry *)iter.get();
200
201 if (debug) {
202 printf("array %s@%p: decl %d, whole_access %d\n",
203 entry->var->name, (void *) entry->var, entry->declaration,
204 entry->whole_array_access);
205 }
206
207 if (!entry->declaration || entry->whole_array_access) {
208 entry->remove();
209 }
210 }
211
212 return !variable_list.is_empty();
213 }
214
215 /** This is the class that does the actual work of splitting. */
216 class ir_array_splitting_visitor : public ir_rvalue_visitor {
217 public:
218 ir_array_splitting_visitor(exec_list *vars)
219 {
220 this->variable_list = vars;
221 }
222
223 virtual ~ir_array_splitting_visitor()
224 {
225 }
226
227 virtual ir_visitor_status visit_leave(ir_assignment *);
228
229 void split_deref(ir_dereference **deref);
230 void handle_rvalue(ir_rvalue **rvalue);
231 variable_entry *get_splitting_entry(ir_variable *var);
232
233 exec_list *variable_list;
234 void *mem_ctx;
235 };
236
237 variable_entry *
238 ir_array_splitting_visitor::get_splitting_entry(ir_variable *var)
239 {
240 assert(var);
241
242 if (!var->type->is_array())
243 return NULL;
244
245 foreach_iter(exec_list_iterator, iter, *this->variable_list) {
246 variable_entry *entry = (variable_entry *)iter.get();
247 if (entry->var == var) {
248 return entry;
249 }
250 }
251
252 return NULL;
253 }
254
255 void
256 ir_array_splitting_visitor::split_deref(ir_dereference **deref)
257 {
258 ir_dereference_array *deref_array = (*deref)->as_dereference_array();
259 if (!deref_array)
260 return;
261
262 ir_dereference_variable *deref_var = deref_array->array->as_dereference_variable();
263 if (!deref_var)
264 return;
265 ir_variable *var = deref_var->var;
266
267 variable_entry *entry = get_splitting_entry(var);
268 if (!entry)
269 return;
270
271 ir_constant *constant = deref_array->array_index->as_constant();
272 assert(constant);
273
274 if (constant->value.i[0] < (int)var->type->length) {
275 *deref = new(entry->mem_ctx)
276 ir_dereference_variable(entry->components[constant->value.i[0]]);
277 } else {
278 /* There was a constant array access beyond the end of the
279 * array. This might have happened due to constant folding
280 * after the initial parse. This produces an undefined value,
281 * but shouldn't crash. Just give them an uninitialized
282 * variable.
283 */
284 ir_variable *temp = new(entry->mem_ctx) ir_variable(deref_array->type,
285 "undef",
286 ir_var_temporary);
287 entry->components[0]->insert_before(temp);
288 *deref = new(entry->mem_ctx) ir_dereference_variable(temp);
289 }
290 }
291
292 void
293 ir_array_splitting_visitor::handle_rvalue(ir_rvalue **rvalue)
294 {
295 if (!*rvalue)
296 return;
297
298 ir_dereference *deref = (*rvalue)->as_dereference();
299
300 if (!deref)
301 return;
302
303 split_deref(&deref);
304 *rvalue = deref;
305 }
306
307 ir_visitor_status
308 ir_array_splitting_visitor::visit_leave(ir_assignment *ir)
309 {
310 /* The normal rvalue visitor skips the LHS of assignments, but we
311 * need to process those just the same.
312 */
313 ir_rvalue *lhs = ir->lhs;
314
315 handle_rvalue(&lhs);
316 ir->lhs = lhs->as_dereference();
317
318 ir->lhs->accept(this);
319
320 handle_rvalue(&ir->rhs);
321 ir->rhs->accept(this);
322
323 if (ir->condition) {
324 handle_rvalue(&ir->condition);
325 ir->condition->accept(this);
326 }
327
328 return visit_continue;
329 }
330
331 bool
332 optimize_split_arrays(exec_list *instructions, bool linked)
333 {
334 ir_array_reference_visitor refs;
335 if (!refs.get_split_list(instructions, linked))
336 return false;
337
338 void *mem_ctx = ralloc_context(NULL);
339
340 /* Replace the decls of the arrays to be split with their split
341 * components.
342 */
343 foreach_iter(exec_list_iterator, iter, refs.variable_list) {
344 variable_entry *entry = (variable_entry *)iter.get();
345 const struct glsl_type *type = entry->var->type;
346
347 entry->mem_ctx = ralloc_parent(entry->var);
348
349 entry->components = ralloc_array(mem_ctx,
350 ir_variable *,
351 type->length);
352
353 for (unsigned int i = 0; i < type->length; i++) {
354 const char *name = ralloc_asprintf(mem_ctx, "%s_%d",
355 entry->var->name, i);
356
357 entry->components[i] =
358 new(entry->mem_ctx) ir_variable(type->fields.array,
359 name,
360 ir_var_temporary);
361 entry->var->insert_before(entry->components[i]);
362 }
363
364 entry->var->remove();
365 }
366
367 ir_array_splitting_visitor split(&refs.variable_list);
368 visit_list_elements(&split, instructions);
369
370 if (debug)
371 _mesa_print_ir(instructions, NULL);
372
373 ralloc_free(mem_ctx);
374
375 return true;
376
377 }