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 ir_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"
41 static bool debug
= false;
43 // XXX using variable_entry2 here to avoid collision (MSVC multiply-defined
44 // function) with the variable_entry class seen in ir_variable_refcount.h
45 // Perhaps we can use the one in ir_variable_refcount.h and make this class
47 class variable_entry2
: public exec_node
50 variable_entry2(ir_variable
*var
)
53 this->whole_structure_access
= 0;
54 this->declaration
= false;
55 this->components
= NULL
;
59 ir_variable
*var
; /* The key: the variable's pointer. */
61 /** Number of times the variable is referenced, including assignments. */
62 unsigned whole_structure_access
;
64 bool declaration
; /* If the variable had a decl in the instruction stream */
66 ir_variable
**components
;
68 /** talloc_parent(this->var) -- the shader's talloc context. */
73 class ir_structure_reference_visitor
: public ir_hierarchical_visitor
{
75 ir_structure_reference_visitor(void)
77 this->mem_ctx
= talloc_new(NULL
);
78 this->variable_list
.make_empty();
81 ~ir_structure_reference_visitor(void)
86 virtual ir_visitor_status
visit(ir_variable
*);
87 virtual ir_visitor_status
visit(ir_dereference_variable
*);
88 virtual ir_visitor_status
visit_enter(ir_dereference_record
*);
89 virtual ir_visitor_status
visit_enter(ir_assignment
*);
90 virtual ir_visitor_status
visit_enter(ir_function_signature
*);
92 variable_entry2
*get_variable_entry2(ir_variable
*var
);
94 /* List of variable_entry */
95 exec_list variable_list
;
101 ir_structure_reference_visitor::get_variable_entry2(ir_variable
*var
)
105 if (!var
->type
->is_record() || var
->mode
== ir_var_uniform
)
108 foreach_iter(exec_list_iterator
, iter
, this->variable_list
) {
109 variable_entry2
*entry
= (variable_entry2
*)iter
.get();
110 if (entry
->var
== var
)
114 variable_entry2
*entry
= new(mem_ctx
) variable_entry2(var
);
115 this->variable_list
.push_tail(entry
);
121 ir_structure_reference_visitor::visit(ir_variable
*ir
)
123 variable_entry2
*entry
= this->get_variable_entry2(ir
);
126 entry
->declaration
= true;
128 return visit_continue
;
132 ir_structure_reference_visitor::visit(ir_dereference_variable
*ir
)
134 ir_variable
*const var
= ir
->variable_referenced();
135 variable_entry2
*entry
= this->get_variable_entry2(var
);
138 entry
->whole_structure_access
++;
140 return visit_continue
;
144 ir_structure_reference_visitor::visit_enter(ir_dereference_record
*ir
)
147 /* Don't descend into the ir_dereference_variable below. */
148 return visit_continue_with_parent
;
152 ir_structure_reference_visitor::visit_enter(ir_assignment
*ir
)
154 if (ir
->lhs
->as_dereference_variable() &&
155 ir
->rhs
->as_dereference_variable() &&
157 /* We'll split copies of a structure to copies of components, so don't
158 * descend to the ir_dereference_variables.
160 return visit_continue_with_parent
;
162 return visit_continue
;
166 ir_structure_reference_visitor::visit_enter(ir_function_signature
*ir
)
168 /* We don't want to descend into the function parameters and
169 * dead-code eliminate them, so just accept the body here.
171 visit_list_elements(this, &ir
->body
);
172 return visit_continue_with_parent
;
175 class ir_structure_splitting_visitor
: public ir_rvalue_visitor
{
177 ir_structure_splitting_visitor(exec_list
*vars
)
179 this->variable_list
= vars
;
182 virtual ~ir_structure_splitting_visitor()
186 virtual ir_visitor_status
visit_leave(ir_assignment
*);
188 void split_deref(ir_dereference
**deref
);
189 void handle_rvalue(ir_rvalue
**rvalue
);
190 variable_entry2
*get_splitting_entry(ir_variable
*var
);
192 exec_list
*variable_list
;
197 ir_structure_splitting_visitor::get_splitting_entry(ir_variable
*var
)
201 if (!var
->type
->is_record())
204 foreach_iter(exec_list_iterator
, iter
, *this->variable_list
) {
205 variable_entry2
*entry
= (variable_entry2
*)iter
.get();
206 if (entry
->var
== var
) {
215 ir_structure_splitting_visitor::split_deref(ir_dereference
**deref
)
217 if ((*deref
)->ir_type
!= ir_type_dereference_record
)
220 ir_dereference_record
*deref_record
= (ir_dereference_record
*)*deref
;
221 ir_dereference_variable
*deref_var
= deref_record
->record
->as_dereference_variable();
225 variable_entry2
*entry
= get_splitting_entry(deref_var
->var
);
230 for (i
= 0; i
< entry
->var
->type
->length
; i
++) {
231 if (strcmp(deref_record
->field
,
232 entry
->var
->type
->fields
.structure
[i
].name
) == 0)
235 assert(i
!= entry
->var
->type
->length
);
237 *deref
= new(entry
->mem_ctx
) ir_dereference_variable(entry
->components
[i
]);
241 ir_structure_splitting_visitor::handle_rvalue(ir_rvalue
**rvalue
)
246 ir_dereference
*deref
= (*rvalue
)->as_dereference();
256 ir_structure_splitting_visitor::visit_leave(ir_assignment
*ir
)
258 ir_dereference_variable
*lhs_deref
= ir
->lhs
->as_dereference_variable();
259 ir_dereference_variable
*rhs_deref
= ir
->rhs
->as_dereference_variable();
260 variable_entry2
*lhs_entry
= lhs_deref
? get_splitting_entry(lhs_deref
->var
) : NULL
;
261 variable_entry2
*rhs_entry
= rhs_deref
? get_splitting_entry(rhs_deref
->var
) : NULL
;
262 const glsl_type
*type
= ir
->rhs
->type
;
264 if ((lhs_entry
|| rhs_entry
) && !ir
->condition
) {
265 for (unsigned int i
= 0; i
< type
->length
; i
++) {
266 ir_dereference
*new_lhs
, *new_rhs
;
267 void *mem_ctx
= lhs_entry
? lhs_entry
->mem_ctx
: rhs_entry
->mem_ctx
;
270 new_lhs
= new(mem_ctx
) ir_dereference_variable(lhs_entry
->components
[i
]);
272 new_lhs
= new(mem_ctx
)
273 ir_dereference_record(ir
->lhs
->clone(mem_ctx
, NULL
),
274 type
->fields
.structure
[i
].name
);
278 new_rhs
= new(mem_ctx
) ir_dereference_variable(rhs_entry
->components
[i
]);
280 new_rhs
= new(mem_ctx
)
281 ir_dereference_record(ir
->rhs
->clone(mem_ctx
, NULL
),
282 type
->fields
.structure
[i
].name
);
285 ir
->insert_before(new(mem_ctx
) ir_assignment(new_lhs
,
291 handle_rvalue(&ir
->rhs
);
292 split_deref(&ir
->lhs
);
295 handle_rvalue(&ir
->condition
);
297 return visit_continue
;
301 do_structure_splitting(exec_list
*instructions
)
303 ir_structure_reference_visitor refs
;
305 visit_list_elements(&refs
, instructions
);
307 /* Trim out variables we can't split. */
308 foreach_iter(exec_list_iterator
, iter
, refs
.variable_list
) {
309 variable_entry2
*entry
= (variable_entry2
*)iter
.get();
312 printf("structure %s@%p: decl %d, whole_access %d\n",
313 entry
->var
->name
, (void *) entry
->var
, entry
->declaration
,
314 entry
->whole_structure_access
);
317 if (!entry
->declaration
|| entry
->whole_structure_access
) {
322 if (refs
.variable_list
.is_empty())
325 void *mem_ctx
= talloc_new(NULL
);
327 /* Replace the decls of the structures to be split with their split
330 foreach_iter(exec_list_iterator
, iter
, refs
.variable_list
) {
331 variable_entry2
*entry
= (variable_entry2
*)iter
.get();
332 const struct glsl_type
*type
= entry
->var
->type
;
334 entry
->mem_ctx
= talloc_parent(entry
->var
);
336 entry
->components
= talloc_array(mem_ctx
,
340 for (unsigned int i
= 0; i
< entry
->var
->type
->length
; i
++) {
341 const char *name
= talloc_asprintf(mem_ctx
, "%s_%s",
343 type
->fields
.structure
[i
].name
);
345 entry
->components
[i
] =
346 new(entry
->mem_ctx
) ir_variable(type
->fields
.structure
[i
].type
,
349 entry
->var
->insert_before(entry
->components
[i
]);
352 entry
->var
->remove();
355 ir_structure_splitting_visitor
split(&refs
.variable_list
);
356 visit_list_elements(&split
, instructions
);
358 talloc_free(mem_ctx
);