2 * Copyright © 2010 Intel Corporation
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:
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
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.
25 * \file opt_array_splitting.cpp
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.
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.
37 #include "ir_visitor.h"
38 #include "ir_rvalue_visitor.h"
39 #include "ir_print_visitor.h"
40 #include "glsl_types.h"
42 static bool debug
= false;
44 namespace opt_array_splitting
{
46 class variable_entry
: public exec_node
49 variable_entry(ir_variable
*var
)
53 this->declaration
= false;
54 this->components
= NULL
;
56 if (var
->type
->is_array())
57 this->size
= var
->type
->length
;
59 this->size
= var
->type
->matrix_columns
;
62 ir_variable
*var
; /* The key: the variable's pointer. */
63 unsigned size
; /* array length or matrix columns */
65 /** Whether this array should be split or not. */
68 bool declaration
; /* If the variable had a decl in the instruction stream */
70 ir_variable
**components
;
72 /** ralloc_parent(this->var) -- the shader's talloc context. */
77 using namespace opt_array_splitting
;
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.
84 class ir_array_reference_visitor
: public ir_hierarchical_visitor
{
86 ir_array_reference_visitor(void)
88 this->mem_ctx
= ralloc_context(NULL
);
89 this->variable_list
.make_empty();
92 ~ir_array_reference_visitor(void)
97 bool get_split_list(exec_list
*instructions
, bool linked
);
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
*);
103 variable_entry
*get_variable_entry(ir_variable
*var
);
105 /* List of variable_entry */
106 exec_list variable_list
;
112 ir_array_reference_visitor::get_variable_entry(ir_variable
*var
)
116 if (var
->mode
!= ir_var_auto
&&
117 var
->mode
!= ir_var_temporary
)
120 if (!(var
->type
->is_array() || var
->type
->is_matrix()))
123 /* If the array hasn't been sized yet, we can't split it. After
124 * linking, this should be resolved.
126 if (var
->type
->is_array() && var
->type
->length
== 0)
129 foreach_iter(exec_list_iterator
, iter
, this->variable_list
) {
130 variable_entry
*entry
= (variable_entry
*)iter
.get();
131 if (entry
->var
== var
)
135 variable_entry
*entry
= new(mem_ctx
) variable_entry(var
);
136 this->variable_list
.push_tail(entry
);
142 ir_array_reference_visitor::visit(ir_variable
*ir
)
144 variable_entry
*entry
= this->get_variable_entry(ir
);
147 entry
->declaration
= true;
149 return visit_continue
;
153 ir_array_reference_visitor::visit(ir_dereference_variable
*ir
)
155 variable_entry
*entry
= this->get_variable_entry(ir
->var
);
157 /* If we made it to here without seeing an ir_dereference_array,
158 * then the dereference of this array didn't have a constant index
159 * (see the visit_continue_with_parent below), so we can't split
163 entry
->split
= false;
165 return visit_continue
;
169 ir_array_reference_visitor::visit_enter(ir_dereference_array
*ir
)
171 ir_dereference_variable
*deref
= ir
->array
->as_dereference_variable();
173 return visit_continue
;
175 variable_entry
*entry
= this->get_variable_entry(deref
->var
);
177 /* If the access to the array has a variable index, we wouldn't
178 * know which split variable this dereference should go to.
180 if (entry
&& !ir
->array_index
->as_constant())
181 entry
->split
= false;
183 return visit_continue_with_parent
;
187 ir_array_reference_visitor::get_split_list(exec_list
*instructions
,
190 visit_list_elements(this, instructions
);
192 /* If the shaders aren't linked yet, we can't mess with global
193 * declarations, which need to be matched by name across shaders.
196 foreach_list(node
, instructions
) {
197 ir_variable
*var
= ((ir_instruction
*)node
)->as_variable();
199 variable_entry
*entry
= get_variable_entry(var
);
206 /* Trim out variables we found that we can't split. */
207 foreach_iter(exec_list_iterator
, iter
, variable_list
) {
208 variable_entry
*entry
= (variable_entry
*)iter
.get();
211 printf("array %s@%p: decl %d, split %d\n",
212 entry
->var
->name
, (void *) entry
->var
, entry
->declaration
,
216 if (!(entry
->declaration
&& entry
->split
)) {
221 return !variable_list
.is_empty();
225 * This class rewrites the dereferences of arrays that have been split
226 * to use the newly created ir_variables for each component.
228 class ir_array_splitting_visitor
: public ir_rvalue_visitor
{
230 ir_array_splitting_visitor(exec_list
*vars
)
232 this->variable_list
= vars
;
235 virtual ~ir_array_splitting_visitor()
239 virtual ir_visitor_status
visit_leave(ir_assignment
*);
241 void split_deref(ir_dereference
**deref
);
242 void handle_rvalue(ir_rvalue
**rvalue
);
243 variable_entry
*get_splitting_entry(ir_variable
*var
);
245 exec_list
*variable_list
;
249 ir_array_splitting_visitor::get_splitting_entry(ir_variable
*var
)
253 foreach_iter(exec_list_iterator
, iter
, *this->variable_list
) {
254 variable_entry
*entry
= (variable_entry
*)iter
.get();
255 if (entry
->var
== var
) {
264 ir_array_splitting_visitor::split_deref(ir_dereference
**deref
)
266 ir_dereference_array
*deref_array
= (*deref
)->as_dereference_array();
270 ir_dereference_variable
*deref_var
= deref_array
->array
->as_dereference_variable();
273 ir_variable
*var
= deref_var
->var
;
275 variable_entry
*entry
= get_splitting_entry(var
);
279 ir_constant
*constant
= deref_array
->array_index
->as_constant();
282 if (constant
->value
.i
[0] < (int)entry
->size
) {
283 *deref
= new(entry
->mem_ctx
)
284 ir_dereference_variable(entry
->components
[constant
->value
.i
[0]]);
286 /* There was a constant array access beyond the end of the
287 * array. This might have happened due to constant folding
288 * after the initial parse. This produces an undefined value,
289 * but shouldn't crash. Just give them an uninitialized
292 ir_variable
*temp
= new(entry
->mem_ctx
) ir_variable(deref_array
->type
,
295 entry
->components
[0]->insert_before(temp
);
296 *deref
= new(entry
->mem_ctx
) ir_dereference_variable(temp
);
301 ir_array_splitting_visitor::handle_rvalue(ir_rvalue
**rvalue
)
306 ir_dereference
*deref
= (*rvalue
)->as_dereference();
316 ir_array_splitting_visitor::visit_leave(ir_assignment
*ir
)
318 /* The normal rvalue visitor skips the LHS of assignments, but we
319 * need to process those just the same.
321 ir_rvalue
*lhs
= ir
->lhs
;
324 ir
->lhs
= lhs
->as_dereference();
326 ir
->lhs
->accept(this);
328 handle_rvalue(&ir
->rhs
);
329 ir
->rhs
->accept(this);
332 handle_rvalue(&ir
->condition
);
333 ir
->condition
->accept(this);
336 return visit_continue
;
340 optimize_split_arrays(exec_list
*instructions
, bool linked
)
342 ir_array_reference_visitor refs
;
343 if (!refs
.get_split_list(instructions
, linked
))
346 void *mem_ctx
= ralloc_context(NULL
);
348 /* Replace the decls of the arrays to be split with their split
351 foreach_iter(exec_list_iterator
, iter
, refs
.variable_list
) {
352 variable_entry
*entry
= (variable_entry
*)iter
.get();
353 const struct glsl_type
*type
= entry
->var
->type
;
354 const struct glsl_type
*subtype
;
356 if (type
->is_matrix())
357 subtype
= type
->column_type();
359 subtype
= type
->fields
.array
;
361 entry
->mem_ctx
= ralloc_parent(entry
->var
);
363 entry
->components
= ralloc_array(mem_ctx
,
367 for (unsigned int i
= 0; i
< entry
->size
; i
++) {
368 const char *name
= ralloc_asprintf(mem_ctx
, "%s_%d",
369 entry
->var
->name
, i
);
371 entry
->components
[i
] =
372 new(entry
->mem_ctx
) ir_variable(subtype
, name
, ir_var_temporary
);
373 entry
->var
->insert_before(entry
->components
[i
]);
376 entry
->var
->remove();
379 ir_array_splitting_visitor
split(&refs
.variable_list
);
380 visit_list_elements(&split
, instructions
);
383 _mesa_print_ir(instructions
, NULL
);
385 ralloc_free(mem_ctx
);