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_structure_splitting.cpp
27 * If a structure is only ever referenced by its components, then
28 * split those components out to individual variables so they can be
29 * handled normally by other optimization passes.
31 * This skips structures like uniforms, which need to be accessible as
32 * structures for their access by the GL.
36 #include "ir_visitor.h"
37 #include "ir_print_visitor.h"
38 #include "ir_rvalue_visitor.h"
39 #include "glsl_types.h"
43 static bool debug
= false;
45 class variable_entry
: public exec_node
48 variable_entry(ir_variable
*var
)
51 this->whole_structure_access
= 0;
52 this->declaration
= false;
53 this->components
= NULL
;
57 ir_variable
*var
; /* The key: the variable's pointer. */
59 /** Number of times the variable is referenced, including assignments. */
60 unsigned whole_structure_access
;
62 /* If the variable had a decl we can work with in the instruction
63 * stream. We can't do splitting on function arguments, which
64 * don't get this variable set.
68 ir_variable
**components
;
70 /** ralloc_parent(this->var) -- the shader's ralloc context. */
75 class ir_structure_reference_visitor
: public ir_hierarchical_visitor
{
77 ir_structure_reference_visitor(void)
79 this->mem_ctx
= ralloc_context(NULL
);
80 this->variable_list
.make_empty();
83 ~ir_structure_reference_visitor(void)
88 virtual ir_visitor_status
visit(ir_variable
*);
89 virtual ir_visitor_status
visit(ir_dereference_variable
*);
90 virtual ir_visitor_status
visit_enter(ir_dereference_record
*);
91 virtual ir_visitor_status
visit_enter(ir_assignment
*);
92 virtual ir_visitor_status
visit_enter(ir_function_signature
*);
94 variable_entry
*get_variable_entry(ir_variable
*var
);
96 /* List of variable_entry */
97 exec_list variable_list
;
103 ir_structure_reference_visitor::get_variable_entry(ir_variable
*var
)
107 if (!var
->type
->is_record() || var
->mode
== ir_var_uniform
108 || var
->mode
== ir_var_shader_in
|| var
->mode
== ir_var_shader_out
)
111 foreach_iter(exec_list_iterator
, iter
, this->variable_list
) {
112 variable_entry
*entry
= (variable_entry
*)iter
.get();
113 if (entry
->var
== var
)
117 variable_entry
*entry
= new(mem_ctx
) variable_entry(var
);
118 this->variable_list
.push_tail(entry
);
124 ir_structure_reference_visitor::visit(ir_variable
*ir
)
126 variable_entry
*entry
= this->get_variable_entry(ir
);
129 entry
->declaration
= true;
131 return visit_continue
;
135 ir_structure_reference_visitor::visit(ir_dereference_variable
*ir
)
137 ir_variable
*const var
= ir
->variable_referenced();
138 variable_entry
*entry
= this->get_variable_entry(var
);
141 entry
->whole_structure_access
++;
143 return visit_continue
;
147 ir_structure_reference_visitor::visit_enter(ir_dereference_record
*ir
)
150 /* Don't descend into the ir_dereference_variable below. */
151 return visit_continue_with_parent
;
155 ir_structure_reference_visitor::visit_enter(ir_assignment
*ir
)
157 /* If there are no structure references yet, no need to bother with
158 * processing the expression tree.
160 if (this->variable_list
.is_empty())
161 return visit_continue_with_parent
;
163 if (ir
->lhs
->as_dereference_variable() &&
164 ir
->rhs
->as_dereference_variable() &&
166 /* We'll split copies of a structure to copies of components, so don't
167 * descend to the ir_dereference_variables.
169 return visit_continue_with_parent
;
171 return visit_continue
;
175 ir_structure_reference_visitor::visit_enter(ir_function_signature
*ir
)
177 /* We don't have logic for structure-splitting function arguments,
178 * so just look at the body instructions and not the parameter
181 visit_list_elements(this, &ir
->body
);
182 return visit_continue_with_parent
;
185 class ir_structure_splitting_visitor
: public ir_rvalue_visitor
{
187 ir_structure_splitting_visitor(exec_list
*vars
)
189 this->variable_list
= vars
;
192 virtual ~ir_structure_splitting_visitor()
196 virtual ir_visitor_status
visit_leave(ir_assignment
*);
198 void split_deref(ir_dereference
**deref
);
199 void handle_rvalue(ir_rvalue
**rvalue
);
200 variable_entry
*get_splitting_entry(ir_variable
*var
);
202 exec_list
*variable_list
;
206 ir_structure_splitting_visitor::get_splitting_entry(ir_variable
*var
)
210 if (!var
->type
->is_record())
213 foreach_iter(exec_list_iterator
, iter
, *this->variable_list
) {
214 variable_entry
*entry
= (variable_entry
*)iter
.get();
215 if (entry
->var
== var
) {
224 ir_structure_splitting_visitor::split_deref(ir_dereference
**deref
)
226 if ((*deref
)->ir_type
!= ir_type_dereference_record
)
229 ir_dereference_record
*deref_record
= (ir_dereference_record
*)*deref
;
230 ir_dereference_variable
*deref_var
= deref_record
->record
->as_dereference_variable();
234 variable_entry
*entry
= get_splitting_entry(deref_var
->var
);
239 for (i
= 0; i
< entry
->var
->type
->length
; i
++) {
240 if (strcmp(deref_record
->field
,
241 entry
->var
->type
->fields
.structure
[i
].name
) == 0)
244 assert(i
!= entry
->var
->type
->length
);
246 *deref
= new(entry
->mem_ctx
) ir_dereference_variable(entry
->components
[i
]);
250 ir_structure_splitting_visitor::handle_rvalue(ir_rvalue
**rvalue
)
255 ir_dereference
*deref
= (*rvalue
)->as_dereference();
265 ir_structure_splitting_visitor::visit_leave(ir_assignment
*ir
)
267 ir_dereference_variable
*lhs_deref
= ir
->lhs
->as_dereference_variable();
268 ir_dereference_variable
*rhs_deref
= ir
->rhs
->as_dereference_variable();
269 variable_entry
*lhs_entry
= lhs_deref
? get_splitting_entry(lhs_deref
->var
) : NULL
;
270 variable_entry
*rhs_entry
= rhs_deref
? get_splitting_entry(rhs_deref
->var
) : NULL
;
271 const glsl_type
*type
= ir
->rhs
->type
;
273 if ((lhs_entry
|| rhs_entry
) && !ir
->condition
) {
274 for (unsigned int i
= 0; i
< type
->length
; i
++) {
275 ir_dereference
*new_lhs
, *new_rhs
;
276 void *mem_ctx
= lhs_entry
? lhs_entry
->mem_ctx
: rhs_entry
->mem_ctx
;
279 new_lhs
= new(mem_ctx
) ir_dereference_variable(lhs_entry
->components
[i
]);
281 new_lhs
= new(mem_ctx
)
282 ir_dereference_record(ir
->lhs
->clone(mem_ctx
, NULL
),
283 type
->fields
.structure
[i
].name
);
287 new_rhs
= new(mem_ctx
) ir_dereference_variable(rhs_entry
->components
[i
]);
289 new_rhs
= new(mem_ctx
)
290 ir_dereference_record(ir
->rhs
->clone(mem_ctx
, NULL
),
291 type
->fields
.structure
[i
].name
);
294 ir
->insert_before(new(mem_ctx
) ir_assignment(new_lhs
,
300 handle_rvalue(&ir
->rhs
);
301 split_deref(&ir
->lhs
);
304 handle_rvalue(&ir
->condition
);
306 return visit_continue
;
309 } /* unnamed namespace */
312 do_structure_splitting(exec_list
*instructions
)
314 ir_structure_reference_visitor refs
;
316 visit_list_elements(&refs
, instructions
);
318 /* Trim out variables we can't split. */
319 foreach_iter(exec_list_iterator
, iter
, refs
.variable_list
) {
320 variable_entry
*entry
= (variable_entry
*)iter
.get();
323 printf("structure %s@%p: decl %d, whole_access %d\n",
324 entry
->var
->name
, (void *) entry
->var
, entry
->declaration
,
325 entry
->whole_structure_access
);
328 if (!entry
->declaration
|| entry
->whole_structure_access
) {
333 if (refs
.variable_list
.is_empty())
336 void *mem_ctx
= ralloc_context(NULL
);
338 /* Replace the decls of the structures to be split with their split
341 foreach_iter(exec_list_iterator
, iter
, refs
.variable_list
) {
342 variable_entry
*entry
= (variable_entry
*)iter
.get();
343 const struct glsl_type
*type
= entry
->var
->type
;
345 entry
->mem_ctx
= ralloc_parent(entry
->var
);
347 entry
->components
= ralloc_array(mem_ctx
,
351 for (unsigned int i
= 0; i
< entry
->var
->type
->length
; i
++) {
352 const char *name
= ralloc_asprintf(mem_ctx
, "%s_%s",
354 type
->fields
.structure
[i
].name
);
356 entry
->components
[i
] =
357 new(entry
->mem_ctx
) ir_variable(type
->fields
.structure
[i
].type
,
360 entry
->var
->insert_before(entry
->components
[i
]);
363 entry
->var
->remove();
366 ir_structure_splitting_visitor
split(&refs
.variable_list
);
367 visit_list_elements(&split
, instructions
);
369 ralloc_free(mem_ctx
);