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