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 lower_vec_index_to_cond_assign.cpp
27 * Turns indexing into vector types to a series of conditional moves
28 * of each channel's swizzle into a temporary.
30 * Most GPUs don't have a native way to do this operation, and this
31 * works around that. For drivers using both this pass and
32 * ir_vec_index_to_swizzle, there's a risk that this pass will happen
33 * before sufficient constant folding to find that the array index is
34 * constant. However, we hope that other optimization passes,
35 * particularly constant folding of assignment conditions and copy
36 * propagation, will result in the same code in the end.
40 #include "ir_visitor.h"
41 #include "ir_optimization.h"
42 #include "glsl_types.h"
47 * Visitor class for replacing expressions with ir_constant values.
50 class ir_vec_index_to_cond_assign_visitor
: public ir_hierarchical_visitor
{
52 ir_vec_index_to_cond_assign_visitor()
57 ir_rvalue
*convert_vec_index_to_cond_assign(void *mem_ctx
,
58 ir_rvalue
*orig_vector
,
59 ir_rvalue
*orig_index
,
60 const glsl_type
*type
);
62 ir_rvalue
*convert_vector_extract_to_cond_assign(ir_rvalue
*ir
);
64 virtual ir_visitor_status
visit_enter(ir_expression
*);
65 virtual ir_visitor_status
visit_enter(ir_swizzle
*);
66 virtual ir_visitor_status
visit_leave(ir_assignment
*);
67 virtual ir_visitor_status
visit_enter(ir_return
*);
68 virtual ir_visitor_status
visit_enter(ir_call
*);
69 virtual ir_visitor_status
visit_enter(ir_if
*);
74 } /* anonymous namespace */
77 ir_vec_index_to_cond_assign_visitor::convert_vec_index_to_cond_assign(void *mem_ctx
,
78 ir_rvalue
*orig_vector
,
79 ir_rvalue
*orig_index
,
80 const glsl_type
*type
)
82 ir_assignment
*assign
, *value_assign
;
83 ir_variable
*index
, *var
, *value
;
84 ir_dereference
*deref
, *deref_value
;
90 /* Store the index to a temporary to avoid reusing its tree. */
91 index
= new(base_ir
) ir_variable(glsl_type::int_type
,
94 list
.push_tail(index
);
95 deref
= new(base_ir
) ir_dereference_variable(index
);
96 assign
= new(base_ir
) ir_assignment(deref
, orig_index
, NULL
);
97 list
.push_tail(assign
);
99 /* Store the value inside a temp, thus avoiding matrixes duplication */
100 value
= new(base_ir
) ir_variable(orig_vector
->type
, "vec_value_tmp",
102 list
.push_tail(value
);
103 deref_value
= new(base_ir
) ir_dereference_variable(value
);
104 value_assign
= new(base_ir
) ir_assignment(deref_value
, orig_vector
);
105 list
.push_tail(value_assign
);
107 /* Temporary where we store whichever value we swizzle out. */
108 var
= new(base_ir
) ir_variable(type
, "vec_index_tmp_v",
112 /* Generate a single comparison condition "mask" for all of the components
115 ir_rvalue
*const cond_deref
=
116 compare_index_block(&list
, index
, 0,
117 orig_vector
->type
->vector_elements
,
120 /* Generate a conditional move of each vector element to the temp. */
121 for (i
= 0; i
< orig_vector
->type
->vector_elements
; i
++) {
122 ir_rvalue
*condition_swizzle
=
123 new(base_ir
) ir_swizzle(cond_deref
->clone(mem_ctx
, NULL
),
126 /* Just clone the rest of the deref chain when trying to get at the
127 * underlying variable.
130 new(base_ir
) ir_swizzle(deref_value
->clone(mem_ctx
, NULL
),
133 deref
= new(base_ir
) ir_dereference_variable(var
);
134 assign
= new(base_ir
) ir_assignment(deref
, swizzle
, condition_swizzle
);
135 list
.push_tail(assign
);
138 /* Put all of the new instructions in the IR stream before the old
141 base_ir
->insert_before(&list
);
143 this->progress
= true;
144 return new(base_ir
) ir_dereference_variable(var
);
148 ir_vec_index_to_cond_assign_visitor::convert_vector_extract_to_cond_assign(ir_rvalue
*ir
)
150 ir_expression
*const expr
= ir
->as_expression();
152 if (expr
== NULL
|| expr
->operation
!= ir_binop_vector_extract
)
155 return convert_vec_index_to_cond_assign(ralloc_parent(ir
),
162 ir_vec_index_to_cond_assign_visitor::visit_enter(ir_expression
*ir
)
166 for (i
= 0; i
< ir
->get_num_operands(); i
++) {
167 ir
->operands
[i
] = convert_vector_extract_to_cond_assign(ir
->operands
[i
]);
170 return visit_continue
;
174 ir_vec_index_to_cond_assign_visitor::visit_enter(ir_swizzle
*ir
)
176 /* Can't be hit from normal GLSL, since you can't swizzle a scalar (which
177 * the result of indexing a vector is. But maybe at some point we'll end up
178 * using swizzling of scalars for vector construction.
180 ir
->val
= convert_vector_extract_to_cond_assign(ir
->val
);
182 return visit_continue
;
186 ir_vec_index_to_cond_assign_visitor::visit_leave(ir_assignment
*ir
)
188 ir
->rhs
= convert_vector_extract_to_cond_assign(ir
->rhs
);
191 ir
->condition
= convert_vector_extract_to_cond_assign(ir
->condition
);
194 return visit_continue
;
198 ir_vec_index_to_cond_assign_visitor::visit_enter(ir_call
*ir
)
200 foreach_list_safe(n
, &ir
->actual_parameters
) {
201 ir_rvalue
*param
= (ir_rvalue
*) n
;
202 ir_rvalue
*new_param
= convert_vector_extract_to_cond_assign(param
);
204 if (new_param
!= param
) {
205 param
->replace_with(new_param
);
209 return visit_continue
;
213 ir_vec_index_to_cond_assign_visitor::visit_enter(ir_return
*ir
)
216 ir
->value
= convert_vector_extract_to_cond_assign(ir
->value
);
219 return visit_continue
;
223 ir_vec_index_to_cond_assign_visitor::visit_enter(ir_if
*ir
)
225 ir
->condition
= convert_vector_extract_to_cond_assign(ir
->condition
);
227 return visit_continue
;
231 do_vec_index_to_cond_assign(exec_list
*instructions
)
233 ir_vec_index_to_cond_assign_visitor v
;
235 visit_list_elements(&v
, instructions
);