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 brw_wm_vector_splitting.cpp
27 * If a vector 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 vectors in uniforms and varyings, which need to be
32 * accessible as vectors for their access by the GL. Also, vector
33 * results of non-variable-derefs in assignments aren't handled
34 * because to do so we would have to store the vector result to a
35 * temporary in order to unload each channel, and to do so would just
36 * loop us back to where we started. For the 965, this is exactly the
37 * behavior we want for the results of texture lookups, but probably not for
41 #include "main/core.h"
42 #include "brw_context.h"
45 #include "glsl/ir_visitor.h"
46 #include "glsl/ir_rvalue_visitor.h"
47 #include "glsl/glsl_types.h"
49 static bool debug
= false;
51 class variable_entry
: public exec_node
54 variable_entry(ir_variable
*var
)
57 this->whole_vector_access
= 0;
58 this->declaration
= false;
62 ir_variable
*var
; /* The key: the variable's pointer. */
64 /** Number of times the variable is referenced, including assignments. */
65 unsigned whole_vector_access
;
67 bool declaration
; /* If the variable had a decl in the instruction stream */
69 ir_variable
*components
[4];
71 /** ralloc_parent(this->var) -- the shader's ralloc context. */
75 class ir_vector_reference_visitor
: public ir_hierarchical_visitor
{
77 ir_vector_reference_visitor(void)
79 this->mem_ctx
= ralloc_context(NULL
);
80 this->variable_list
.make_empty();
83 ~ir_vector_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_swizzle
*);
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_vector_reference_visitor::get_variable_entry(ir_variable
*var
)
107 if (!var
->type
->is_vector())
110 switch (var
->data
.mode
) {
112 case ir_var_shader_in
:
113 case ir_var_shader_out
:
114 case ir_var_system_value
:
115 case ir_var_function_in
:
116 case ir_var_function_out
:
117 case ir_var_function_inout
:
118 /* Can't split varyings or uniforms. Function in/outs won't get split
123 case ir_var_temporary
:
127 foreach_list(node
, &this->variable_list
) {
128 variable_entry
*entry
= (variable_entry
*)node
;
129 if (entry
->var
== var
)
133 variable_entry
*entry
= new(mem_ctx
) variable_entry(var
);
134 this->variable_list
.push_tail(entry
);
140 ir_vector_reference_visitor::visit(ir_variable
*ir
)
142 variable_entry
*entry
= this->get_variable_entry(ir
);
145 entry
->declaration
= true;
147 return visit_continue
;
151 ir_vector_reference_visitor::visit(ir_dereference_variable
*ir
)
153 ir_variable
*const var
= ir
->var
;
154 variable_entry
*entry
= this->get_variable_entry(var
);
157 entry
->whole_vector_access
++;
159 return visit_continue
;
163 ir_vector_reference_visitor::visit_enter(ir_swizzle
*ir
)
165 /* Don't descend into a vector ir_dereference_variable below. */
166 if (ir
->val
->as_dereference_variable() && ir
->type
->is_scalar())
167 return visit_continue_with_parent
;
169 return visit_continue
;
173 ir_vector_reference_visitor::visit_enter(ir_assignment
*ir
)
175 if (ir
->lhs
->as_dereference_variable() &&
176 ir
->rhs
->as_dereference_variable() &&
178 /* We'll split copies of a vector to copies of channels, so don't
179 * descend to the ir_dereference_variables.
181 return visit_continue_with_parent
;
183 if (ir
->lhs
->as_dereference_variable() &&
184 is_power_of_two(ir
->write_mask
) &&
186 /* If we're writing just a channel, then channel-splitting the LHS is OK.
188 ir
->rhs
->accept(this);
189 return visit_continue_with_parent
;
191 return visit_continue
;
195 ir_vector_reference_visitor::visit_enter(ir_function_signature
*ir
)
197 /* We don't want to descend into the function parameters and
198 * split them, so just accept the body here.
200 visit_list_elements(this, &ir
->body
);
201 return visit_continue_with_parent
;
204 class ir_vector_splitting_visitor
: public ir_rvalue_visitor
{
206 ir_vector_splitting_visitor(exec_list
*vars
)
208 this->variable_list
= vars
;
211 virtual ir_visitor_status
visit_leave(ir_assignment
*);
213 void handle_rvalue(ir_rvalue
**rvalue
);
214 variable_entry
*get_splitting_entry(ir_variable
*var
);
216 exec_list
*variable_list
;
220 ir_vector_splitting_visitor::get_splitting_entry(ir_variable
*var
)
224 if (!var
->type
->is_vector())
227 foreach_list(node
, &*this->variable_list
) {
228 variable_entry
*entry
= (variable_entry
*)node
;
229 if (entry
->var
== var
) {
238 ir_vector_splitting_visitor::handle_rvalue(ir_rvalue
**rvalue
)
243 ir_swizzle
*swiz
= (*rvalue
)->as_swizzle();
244 if (!swiz
|| !swiz
->type
->is_scalar())
247 ir_dereference_variable
*deref_var
= swiz
->val
->as_dereference_variable();
251 variable_entry
*entry
= get_splitting_entry(deref_var
->var
);
255 ir_variable
*var
= entry
->components
[swiz
->mask
.x
];
256 *rvalue
= new(entry
->mem_ctx
) ir_dereference_variable(var
);
260 ir_vector_splitting_visitor::visit_leave(ir_assignment
*ir
)
262 ir_dereference_variable
*lhs_deref
= ir
->lhs
->as_dereference_variable();
263 ir_dereference_variable
*rhs_deref
= ir
->rhs
->as_dereference_variable();
264 variable_entry
*lhs
= lhs_deref
? get_splitting_entry(lhs_deref
->var
) : NULL
;
265 variable_entry
*rhs
= rhs_deref
? get_splitting_entry(rhs_deref
->var
) : NULL
;
267 if (lhs_deref
&& rhs_deref
&& (lhs
|| rhs
) && !ir
->condition
) {
268 unsigned int rhs_chan
= 0;
270 /* Straight assignment of vector variables. */
271 for (unsigned int i
= 0; i
< ir
->lhs
->type
->vector_elements
; i
++) {
272 ir_dereference
*new_lhs
;
274 void *mem_ctx
= lhs
? lhs
->mem_ctx
: rhs
->mem_ctx
;
275 unsigned int writemask
;
277 if (!(ir
->write_mask
& (1 << i
)))
281 new_lhs
= new(mem_ctx
) ir_dereference_variable(lhs
->components
[i
]);
284 new_lhs
= ir
->lhs
->clone(mem_ctx
, NULL
);
290 new(mem_ctx
) ir_dereference_variable(rhs
->components
[rhs_chan
]);
292 new_rhs
= new(mem_ctx
) ir_swizzle(ir
->rhs
->clone(mem_ctx
, NULL
),
293 rhs_chan
, 0, 0, 0, 1);
296 ir
->insert_before(new(mem_ctx
) ir_assignment(new_lhs
,
304 void *mem_ctx
= lhs
->mem_ctx
;
307 switch (ir
->write_mask
) {
322 assert(!"not reached: non-channelwise dereference of LHS.");
325 ir
->lhs
= new(mem_ctx
) ir_dereference_variable(lhs
->components
[elem
]);
326 ir
->write_mask
= (1 << 0);
328 handle_rvalue(&ir
->rhs
);
330 handle_rvalue(&ir
->rhs
);
333 handle_rvalue(&ir
->condition
);
335 return visit_continue
;
339 brw_do_vector_splitting(exec_list
*instructions
)
341 ir_vector_reference_visitor refs
;
343 visit_list_elements(&refs
, instructions
);
345 /* Trim out variables we can't split. */
346 foreach_list_safe(node
, &refs
.variable_list
) {
347 variable_entry
*entry
= (variable_entry
*)node
;
350 fprintf(stderr
, "vector %s@%p: decl %d, whole_access %d\n",
351 entry
->var
->name
, (void *) entry
->var
, entry
->declaration
,
352 entry
->whole_vector_access
);
355 if (!entry
->declaration
|| entry
->whole_vector_access
) {
360 if (refs
.variable_list
.is_empty())
363 void *mem_ctx
= ralloc_context(NULL
);
365 /* Replace the decls of the vectors to be split with their split
368 foreach_list(node
, &refs
.variable_list
) {
369 variable_entry
*entry
= (variable_entry
*)node
;
370 const struct glsl_type
*type
;
371 type
= glsl_type::get_instance(entry
->var
->type
->base_type
, 1, 1);
373 entry
->mem_ctx
= ralloc_parent(entry
->var
);
375 for (unsigned int i
= 0; i
< entry
->var
->type
->vector_elements
; i
++) {
376 const char *name
= ralloc_asprintf(mem_ctx
, "%s_%c",
380 entry
->components
[i
] = new(entry
->mem_ctx
) ir_variable(type
, name
,
382 entry
->var
->insert_before(entry
->components
[i
]);
385 entry
->var
->remove();
388 ir_vector_splitting_visitor
split(&refs
.variable_list
);
389 visit_list_elements(&split
, instructions
);
391 ralloc_free(mem_ctx
);