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
)
52 this->whole_array_access
= 0;
53 this->declaration
= false;
54 this->components
= NULL
;
58 ir_variable
*var
; /* The key: the variable's pointer. */
60 /** Number of times the variable is referenced, including assignments. */
61 unsigned whole_array_access
;
63 bool declaration
; /* If the variable had a decl in the instruction stream */
65 ir_variable
**components
;
67 /** ralloc_parent(this->var) -- the shader's talloc context. */
72 using namespace opt_array_splitting
;
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.
79 class ir_array_reference_visitor
: public ir_hierarchical_visitor
{
81 ir_array_reference_visitor(void)
83 this->mem_ctx
= ralloc_context(NULL
);
84 this->variable_list
.make_empty();
87 ~ir_array_reference_visitor(void)
92 bool get_split_list(exec_list
*instructions
, bool linked
);
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
*);
98 variable_entry
*get_variable_entry(ir_variable
*var
);
100 /* List of variable_entry */
101 exec_list variable_list
;
107 ir_array_reference_visitor::get_variable_entry(ir_variable
*var
)
111 if (var
->mode
!= ir_var_auto
&&
112 var
->mode
!= ir_var_temporary
)
115 if (!var
->type
->is_array())
118 /* If the array hasn't been sized yet, we can't split it. After
119 * linking, this should be resolved.
121 if (var
->type
->length
== 0)
124 foreach_iter(exec_list_iterator
, iter
, this->variable_list
) {
125 variable_entry
*entry
= (variable_entry
*)iter
.get();
126 if (entry
->var
== var
)
130 variable_entry
*entry
= new(mem_ctx
) variable_entry(var
);
131 this->variable_list
.push_tail(entry
);
137 ir_array_reference_visitor::visit(ir_variable
*ir
)
139 variable_entry
*entry
= this->get_variable_entry(ir
);
142 entry
->declaration
= true;
144 return visit_continue
;
148 ir_array_reference_visitor::visit(ir_dereference_variable
*ir
)
150 variable_entry
*entry
= this->get_variable_entry(ir
->var
);
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.
157 entry
->whole_array_access
++;
159 return visit_continue
;
163 ir_array_reference_visitor::visit_enter(ir_dereference_array
*ir
)
165 ir_dereference_variable
*deref
= ir
->array
->as_dereference_variable();
167 return visit_continue
;
169 variable_entry
*entry
= this->get_variable_entry(deref
->var
);
171 if (entry
&& !ir
->array_index
->as_constant())
172 entry
->whole_array_access
++;
174 return visit_continue_with_parent
;
178 ir_array_reference_visitor::get_split_list(exec_list
*instructions
,
181 visit_list_elements(this, instructions
);
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.
187 foreach_list(node
, instructions
) {
188 ir_variable
*var
= ((ir_instruction
*)node
)->as_variable();
190 variable_entry
*entry
= get_variable_entry(var
);
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();
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
);
207 if (!entry
->declaration
|| entry
->whole_array_access
) {
212 return !variable_list
.is_empty();
215 /** This is the class that does the actual work of splitting. */
216 class ir_array_splitting_visitor
: public ir_rvalue_visitor
{
218 ir_array_splitting_visitor(exec_list
*vars
)
220 this->variable_list
= vars
;
223 virtual ~ir_array_splitting_visitor()
227 virtual ir_visitor_status
visit_leave(ir_assignment
*);
229 void split_deref(ir_dereference
**deref
);
230 void handle_rvalue(ir_rvalue
**rvalue
);
231 variable_entry
*get_splitting_entry(ir_variable
*var
);
233 exec_list
*variable_list
;
238 ir_array_splitting_visitor::get_splitting_entry(ir_variable
*var
)
242 if (!var
->type
->is_array())
245 foreach_iter(exec_list_iterator
, iter
, *this->variable_list
) {
246 variable_entry
*entry
= (variable_entry
*)iter
.get();
247 if (entry
->var
== var
) {
256 ir_array_splitting_visitor::split_deref(ir_dereference
**deref
)
258 ir_dereference_array
*deref_array
= (*deref
)->as_dereference_array();
262 ir_dereference_variable
*deref_var
= deref_array
->array
->as_dereference_variable();
265 ir_variable
*var
= deref_var
->var
;
267 variable_entry
*entry
= get_splitting_entry(var
);
271 ir_constant
*constant
= deref_array
->array_index
->as_constant();
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]]);
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
284 ir_variable
*temp
= new(entry
->mem_ctx
) ir_variable(deref_array
->type
,
287 entry
->components
[0]->insert_before(temp
);
288 *deref
= new(entry
->mem_ctx
) ir_dereference_variable(temp
);
293 ir_array_splitting_visitor::handle_rvalue(ir_rvalue
**rvalue
)
298 ir_dereference
*deref
= (*rvalue
)->as_dereference();
308 ir_array_splitting_visitor::visit_leave(ir_assignment
*ir
)
310 /* The normal rvalue visitor skips the LHS of assignments, but we
311 * need to process those just the same.
313 ir_rvalue
*lhs
= ir
->lhs
;
316 ir
->lhs
= lhs
->as_dereference();
318 ir
->lhs
->accept(this);
320 handle_rvalue(&ir
->rhs
);
321 ir
->rhs
->accept(this);
324 handle_rvalue(&ir
->condition
);
325 ir
->condition
->accept(this);
328 return visit_continue
;
332 optimize_split_arrays(exec_list
*instructions
, bool linked
)
334 ir_array_reference_visitor refs
;
335 if (!refs
.get_split_list(instructions
, linked
))
338 void *mem_ctx
= ralloc_context(NULL
);
340 /* Replace the decls of the arrays to be split with their split
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
;
347 entry
->mem_ctx
= ralloc_parent(entry
->var
);
349 entry
->components
= ralloc_array(mem_ctx
,
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
);
357 entry
->components
[i
] =
358 new(entry
->mem_ctx
) ir_variable(type
->fields
.array
,
361 entry
->var
->insert_before(entry
->components
[i
]);
364 entry
->var
->remove();
367 ir_array_splitting_visitor
split(&refs
.variable_list
);
368 visit_list_elements(&split
, instructions
);
371 _mesa_print_ir(instructions
, NULL
);
373 ralloc_free(mem_ctx
);