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 "glsl_types.h"
41 static bool debug
= false;
45 namespace opt_array_splitting
{
47 class variable_entry
: public exec_node
50 variable_entry(ir_variable
*var
)
54 this->declaration
= false;
55 this->components
= NULL
;
57 if (var
->type
->is_array())
58 this->size
= var
->type
->length
;
60 this->size
= var
->type
->matrix_columns
;
63 ir_variable
*var
; /* The key: the variable's pointer. */
64 unsigned size
; /* array length or matrix columns */
66 /** Whether this array should be split or not. */
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.
75 ir_variable
**components
;
77 /** ralloc_parent(this->var) -- the shader's talloc context. */
83 using namespace opt_array_splitting
;
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.
90 class ir_array_reference_visitor
: public ir_hierarchical_visitor
{
92 ir_array_reference_visitor(void)
94 this->mem_ctx
= ralloc_context(NULL
);
95 this->variable_list
.make_empty();
98 ~ir_array_reference_visitor(void)
100 ralloc_free(mem_ctx
);
103 bool get_split_list(exec_list
*instructions
, bool linked
);
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
*);
110 variable_entry
*get_variable_entry(ir_variable
*var
);
112 /* List of variable_entry */
113 exec_list variable_list
;
121 ir_array_reference_visitor::get_variable_entry(ir_variable
*var
)
125 if (var
->data
.mode
!= ir_var_auto
&&
126 var
->data
.mode
!= ir_var_temporary
)
129 if (!(var
->type
->is_array() || var
->type
->is_matrix()))
132 /* If the array hasn't been sized yet, we can't split it. After
133 * linking, this should be resolved.
135 if (var
->type
->is_unsized_array())
138 foreach_iter(exec_list_iterator
, iter
, this->variable_list
) {
139 variable_entry
*entry
= (variable_entry
*)iter
.get();
140 if (entry
->var
== var
)
144 variable_entry
*entry
= new(mem_ctx
) variable_entry(var
);
145 this->variable_list
.push_tail(entry
);
151 ir_array_reference_visitor::visit(ir_variable
*ir
)
153 variable_entry
*entry
= this->get_variable_entry(ir
);
156 entry
->declaration
= true;
158 return visit_continue
;
162 ir_array_reference_visitor::visit(ir_dereference_variable
*ir
)
164 variable_entry
*entry
= this->get_variable_entry(ir
->var
);
166 /* If we made it to here without seeing an ir_dereference_array,
167 * then the dereference of this array didn't have a constant index
168 * (see the visit_continue_with_parent below), so we can't split
172 entry
->split
= false;
174 return visit_continue
;
178 ir_array_reference_visitor::visit_enter(ir_dereference_array
*ir
)
180 ir_dereference_variable
*deref
= ir
->array
->as_dereference_variable();
182 return visit_continue
;
184 variable_entry
*entry
= this->get_variable_entry(deref
->var
);
186 /* If the access to the array has a variable index, we wouldn't
187 * know which split variable this dereference should go to.
189 if (entry
&& !ir
->array_index
->as_constant())
190 entry
->split
= false;
192 return visit_continue_with_parent
;
196 ir_array_reference_visitor::visit_enter(ir_function_signature
*ir
)
198 /* We don't have logic for array-splitting function arguments,
199 * so just look at the body instructions and not the parameter
202 visit_list_elements(this, &ir
->body
);
203 return visit_continue_with_parent
;
207 ir_array_reference_visitor::get_split_list(exec_list
*instructions
,
210 visit_list_elements(this, instructions
);
212 /* If the shaders aren't linked yet, we can't mess with global
213 * declarations, which need to be matched by name across shaders.
216 foreach_list(node
, instructions
) {
217 ir_variable
*var
= ((ir_instruction
*)node
)->as_variable();
219 variable_entry
*entry
= get_variable_entry(var
);
226 /* Trim out variables we found that we can't split. */
227 foreach_iter(exec_list_iterator
, iter
, variable_list
) {
228 variable_entry
*entry
= (variable_entry
*)iter
.get();
231 printf("array %s@%p: decl %d, split %d\n",
232 entry
->var
->name
, (void *) entry
->var
, entry
->declaration
,
236 if (!(entry
->declaration
&& entry
->split
)) {
241 return !variable_list
.is_empty();
245 * This class rewrites the dereferences of arrays that have been split
246 * to use the newly created ir_variables for each component.
248 class ir_array_splitting_visitor
: public ir_rvalue_visitor
{
250 ir_array_splitting_visitor(exec_list
*vars
)
252 this->variable_list
= vars
;
255 virtual ~ir_array_splitting_visitor()
259 virtual ir_visitor_status
visit_leave(ir_assignment
*);
261 void split_deref(ir_dereference
**deref
);
262 void handle_rvalue(ir_rvalue
**rvalue
);
263 variable_entry
*get_splitting_entry(ir_variable
*var
);
265 exec_list
*variable_list
;
269 ir_array_splitting_visitor::get_splitting_entry(ir_variable
*var
)
273 foreach_iter(exec_list_iterator
, iter
, *this->variable_list
) {
274 variable_entry
*entry
= (variable_entry
*)iter
.get();
275 if (entry
->var
== var
) {
284 ir_array_splitting_visitor::split_deref(ir_dereference
**deref
)
286 ir_dereference_array
*deref_array
= (*deref
)->as_dereference_array();
290 ir_dereference_variable
*deref_var
= deref_array
->array
->as_dereference_variable();
293 ir_variable
*var
= deref_var
->var
;
295 variable_entry
*entry
= get_splitting_entry(var
);
299 ir_constant
*constant
= deref_array
->array_index
->as_constant();
302 if (constant
->value
.i
[0] < (int)entry
->size
) {
303 *deref
= new(entry
->mem_ctx
)
304 ir_dereference_variable(entry
->components
[constant
->value
.i
[0]]);
306 /* There was a constant array access beyond the end of the
307 * array. This might have happened due to constant folding
308 * after the initial parse. This produces an undefined value,
309 * but shouldn't crash. Just give them an uninitialized
312 ir_variable
*temp
= new(entry
->mem_ctx
) ir_variable(deref_array
->type
,
315 entry
->components
[0]->insert_before(temp
);
316 *deref
= new(entry
->mem_ctx
) ir_dereference_variable(temp
);
321 ir_array_splitting_visitor::handle_rvalue(ir_rvalue
**rvalue
)
326 ir_dereference
*deref
= (*rvalue
)->as_dereference();
336 ir_array_splitting_visitor::visit_leave(ir_assignment
*ir
)
338 /* The normal rvalue visitor skips the LHS of assignments, but we
339 * need to process those just the same.
341 ir_rvalue
*lhs
= ir
->lhs
;
344 ir
->lhs
= lhs
->as_dereference();
346 ir
->lhs
->accept(this);
348 handle_rvalue(&ir
->rhs
);
349 ir
->rhs
->accept(this);
352 handle_rvalue(&ir
->condition
);
353 ir
->condition
->accept(this);
356 return visit_continue
;
360 optimize_split_arrays(exec_list
*instructions
, bool linked
)
362 ir_array_reference_visitor refs
;
363 if (!refs
.get_split_list(instructions
, linked
))
366 void *mem_ctx
= ralloc_context(NULL
);
368 /* Replace the decls of the arrays to be split with their split
371 foreach_iter(exec_list_iterator
, iter
, refs
.variable_list
) {
372 variable_entry
*entry
= (variable_entry
*)iter
.get();
373 const struct glsl_type
*type
= entry
->var
->type
;
374 const struct glsl_type
*subtype
;
376 if (type
->is_matrix())
377 subtype
= type
->column_type();
379 subtype
= type
->fields
.array
;
381 entry
->mem_ctx
= ralloc_parent(entry
->var
);
383 entry
->components
= ralloc_array(mem_ctx
,
387 for (unsigned int i
= 0; i
< entry
->size
; i
++) {
388 const char *name
= ralloc_asprintf(mem_ctx
, "%s_%d",
389 entry
->var
->name
, i
);
391 entry
->components
[i
] =
392 new(entry
->mem_ctx
) ir_variable(subtype
, name
, ir_var_temporary
);
393 entry
->var
->insert_before(entry
->components
[i
]);
396 entry
->var
->remove();
399 ir_array_splitting_visitor
split(&refs
.variable_list
);
400 visit_list_elements(&split
, instructions
);
403 _mesa_print_ir(instructions
, NULL
);
405 ralloc_free(mem_ctx
);