glsl/opt_array_splitting: Fix indentation
[mesa.git] / src / compiler / 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 "compiler/glsl_types.h"
40
41 static bool debug = false;
42
43 namespace {
44
45 namespace opt_array_splitting {
46
47 class variable_entry : public exec_node
48 {
49 public:
50 variable_entry(ir_variable *var)
51 {
52 this->var = var;
53 this->split = true;
54 this->declaration = false;
55 this->components = NULL;
56 this->mem_ctx = NULL;
57 if (var->type->is_array())
58 this->size = var->type->length;
59 else
60 this->size = var->type->matrix_columns;
61 }
62
63 ir_variable *var; /* The key: the variable's pointer. */
64 unsigned size; /* array length or matrix columns */
65
66 /** Whether this array should be split or not. */
67 bool split;
68
69 /* If the variable had a decl we can work with in the instruction
70 * stream. We can't do splitting on function arguments, which
71 * don't get this variable set.
72 */
73 bool declaration;
74
75 ir_variable **components;
76
77 /** ralloc_parent(this->var) -- the shader's talloc context. */
78 void *mem_ctx;
79 };
80
81 } /* namespace */
82
83 using namespace opt_array_splitting;
84
85 /**
86 * This class does a walk over the tree, coming up with the set of
87 * variables that could be split by looking to see if they are arrays
88 * that are only ever constant-index dereferenced.
89 */
90 class ir_array_reference_visitor : public ir_hierarchical_visitor {
91 public:
92 ir_array_reference_visitor(void)
93 {
94 this->mem_ctx = ralloc_context(NULL);
95 this->variable_list.make_empty();
96 }
97
98 ~ir_array_reference_visitor(void)
99 {
100 ralloc_free(mem_ctx);
101 }
102
103 bool get_split_list(exec_list *instructions, bool linked);
104
105 virtual ir_visitor_status visit(ir_variable *);
106 virtual ir_visitor_status visit(ir_dereference_variable *);
107 virtual ir_visitor_status visit_enter(ir_dereference_array *);
108 virtual ir_visitor_status visit_enter(ir_function_signature *);
109
110 variable_entry *get_variable_entry(ir_variable *var);
111
112 /* List of variable_entry */
113 exec_list variable_list;
114
115 void *mem_ctx;
116 };
117
118 } /* namespace */
119
120 variable_entry *
121 ir_array_reference_visitor::get_variable_entry(ir_variable *var)
122 {
123 assert(var);
124
125 if (var->data.mode != ir_var_auto &&
126 var->data.mode != ir_var_temporary)
127 return NULL;
128
129 if (!(var->type->is_array() || var->type->is_matrix()))
130 return NULL;
131
132 /* If the array hasn't been sized yet, we can't split it. After
133 * linking, this should be resolved.
134 */
135 if (var->type->is_unsized_array())
136 return NULL;
137
138 foreach_in_list(variable_entry, entry, &this->variable_list) {
139 if (entry->var == var)
140 return entry;
141 }
142
143 variable_entry *entry = new(mem_ctx) variable_entry(var);
144 this->variable_list.push_tail(entry);
145 return entry;
146 }
147
148
149 ir_visitor_status
150 ir_array_reference_visitor::visit(ir_variable *ir)
151 {
152 variable_entry *entry = this->get_variable_entry(ir);
153
154 if (entry)
155 entry->declaration = true;
156
157 return visit_continue;
158 }
159
160 ir_visitor_status
161 ir_array_reference_visitor::visit(ir_dereference_variable *ir)
162 {
163 variable_entry *entry = this->get_variable_entry(ir->var);
164
165 /* If we made it to here without seeing an ir_dereference_array,
166 * then the dereference of this array didn't have a constant index
167 * (see the visit_continue_with_parent below), so we can't split
168 * the variable.
169 */
170 if (entry)
171 entry->split = false;
172
173 return visit_continue;
174 }
175
176 ir_visitor_status
177 ir_array_reference_visitor::visit_enter(ir_dereference_array *ir)
178 {
179 ir_dereference_variable *deref = ir->array->as_dereference_variable();
180 if (!deref)
181 return visit_continue;
182
183 variable_entry *entry = this->get_variable_entry(deref->var);
184
185 /* If the access to the array has a variable index, we wouldn't
186 * know which split variable this dereference should go to.
187 */
188 if (!ir->array_index->as_constant()) {
189 if (entry)
190 entry->split = false;
191 /* This variable indexing could come from a different array dereference
192 * that also has variable indexing, that is, something like a[b[a[b[0]]]].
193 * If we return visit_continue_with_parent here for the first appearence
194 * of a, then we can miss that b also has indirect indexing (if this is
195 * the only place in the program where such indirect indexing into b
196 * happens), so keep going.
197 */
198 return visit_continue;
199 }
200
201 /* If the index is also array dereference, visit index. */
202 if (ir->array_index->as_dereference_array())
203 visit_enter(ir->array_index->as_dereference_array());
204
205 return visit_continue_with_parent;
206 }
207
208 ir_visitor_status
209 ir_array_reference_visitor::visit_enter(ir_function_signature *ir)
210 {
211 /* We don't have logic for array-splitting function arguments,
212 * so just look at the body instructions and not the parameter
213 * declarations.
214 */
215 visit_list_elements(this, &ir->body);
216 return visit_continue_with_parent;
217 }
218
219 bool
220 ir_array_reference_visitor::get_split_list(exec_list *instructions,
221 bool linked)
222 {
223 visit_list_elements(this, instructions);
224
225 /* If the shaders aren't linked yet, we can't mess with global
226 * declarations, which need to be matched by name across shaders.
227 */
228 if (!linked) {
229 foreach_in_list(ir_instruction, node, instructions) {
230 ir_variable *var = node->as_variable();
231 if (var) {
232 variable_entry *entry = get_variable_entry(var);
233 if (entry)
234 entry->remove();
235 }
236 }
237 }
238
239 /* Trim out variables we found that we can't split. */
240 foreach_in_list_safe(variable_entry, entry, &variable_list) {
241 if (debug) {
242 printf("array %s@%p: decl %d, split %d\n",
243 entry->var->name, (void *) entry->var, entry->declaration,
244 entry->split);
245 }
246
247 if (!(entry->declaration && entry->split)) {
248 entry->remove();
249 }
250 }
251
252 return !variable_list.is_empty();
253 }
254
255 /**
256 * This class rewrites the dereferences of arrays that have been split
257 * to use the newly created ir_variables for each component.
258 */
259 class ir_array_splitting_visitor : public ir_rvalue_visitor {
260 public:
261 ir_array_splitting_visitor(exec_list *vars)
262 {
263 this->variable_list = vars;
264 }
265
266 virtual ~ir_array_splitting_visitor()
267 {
268 }
269
270 virtual ir_visitor_status visit_leave(ir_assignment *);
271
272 void split_deref(ir_dereference **deref);
273 void handle_rvalue(ir_rvalue **rvalue);
274 variable_entry *get_splitting_entry(ir_variable *var);
275
276 exec_list *variable_list;
277 };
278
279 variable_entry *
280 ir_array_splitting_visitor::get_splitting_entry(ir_variable *var)
281 {
282 assert(var);
283
284 foreach_in_list(variable_entry, entry, this->variable_list) {
285 if (entry->var == var) {
286 return entry;
287 }
288 }
289
290 return NULL;
291 }
292
293 void
294 ir_array_splitting_visitor::split_deref(ir_dereference **deref)
295 {
296 ir_dereference_array *deref_array = (*deref)->as_dereference_array();
297 if (!deref_array)
298 return;
299
300 ir_dereference_variable *deref_var = deref_array->array->as_dereference_variable();
301 if (!deref_var)
302 return;
303 ir_variable *var = deref_var->var;
304
305 variable_entry *entry = get_splitting_entry(var);
306 if (!entry)
307 return;
308
309 ir_constant *constant = deref_array->array_index->as_constant();
310 assert(constant);
311
312 if (constant->value.i[0] >= 0 && constant->value.i[0] < (int)entry->size) {
313 *deref = new(entry->mem_ctx)
314 ir_dereference_variable(entry->components[constant->value.i[0]]);
315 } else {
316 /* There was a constant array access beyond the end of the
317 * array. This might have happened due to constant folding
318 * after the initial parse. This produces an undefined value,
319 * but shouldn't crash. Just give them an uninitialized
320 * variable.
321 */
322 ir_variable *temp = new(entry->mem_ctx) ir_variable(deref_array->type,
323 "undef",
324 ir_var_temporary);
325 entry->components[0]->insert_before(temp);
326 *deref = new(entry->mem_ctx) ir_dereference_variable(temp);
327 }
328 }
329
330 void
331 ir_array_splitting_visitor::handle_rvalue(ir_rvalue **rvalue)
332 {
333 if (!*rvalue)
334 return;
335
336 ir_dereference *deref = (*rvalue)->as_dereference();
337
338 if (!deref)
339 return;
340
341 split_deref(&deref);
342 *rvalue = deref;
343 }
344
345 ir_visitor_status
346 ir_array_splitting_visitor::visit_leave(ir_assignment *ir)
347 {
348 /* The normal rvalue visitor skips the LHS of assignments, but we
349 * need to process those just the same.
350 */
351 ir_rvalue *lhs = ir->lhs;
352
353 handle_rvalue(&lhs);
354 ir->lhs = lhs->as_dereference();
355
356 ir->lhs->accept(this);
357
358 handle_rvalue(&ir->rhs);
359 ir->rhs->accept(this);
360
361 if (ir->condition) {
362 handle_rvalue(&ir->condition);
363 ir->condition->accept(this);
364 }
365
366 return visit_continue;
367 }
368
369 bool
370 optimize_split_arrays(exec_list *instructions, bool linked)
371 {
372 ir_array_reference_visitor refs;
373 if (!refs.get_split_list(instructions, linked))
374 return false;
375
376 void *mem_ctx = ralloc_context(NULL);
377
378 /* Replace the decls of the arrays to be split with their split
379 * components.
380 */
381 foreach_in_list(variable_entry, entry, &refs.variable_list) {
382 const struct glsl_type *type = entry->var->type;
383 const struct glsl_type *subtype;
384
385 if (type->is_matrix())
386 subtype = type->column_type();
387 else
388 subtype = type->fields.array;
389
390 entry->mem_ctx = ralloc_parent(entry->var);
391
392 entry->components = ralloc_array(mem_ctx, ir_variable *, entry->size);
393
394 for (unsigned int i = 0; i < entry->size; i++) {
395 const char *name = ralloc_asprintf(mem_ctx, "%s_%d",
396 entry->var->name, i);
397
398 entry->components[i] =
399 new(entry->mem_ctx) ir_variable(subtype, name, ir_var_temporary);
400 entry->var->insert_before(entry->components[i]);
401 }
402
403 entry->var->remove();
404 }
405
406 ir_array_splitting_visitor split(&refs.variable_list);
407 visit_list_elements(&split, instructions);
408
409 if (debug)
410 _mesa_print_ir(stdout, instructions, NULL);
411
412 ralloc_free(mem_ctx);
413
414 return true;
415
416 }