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_vector.cpp
26 * IR lowering pass to remove some types of ir_quadop_vector
28 * \author Ian Romanick <ian.d.romanick@intel.com>
32 #include "ir_rvalue_visitor.h"
36 class lower_vector_visitor
: public ir_rvalue_visitor
{
38 lower_vector_visitor() : dont_lower_swz(false), progress(false)
43 void handle_rvalue(ir_rvalue
**rvalue
);
46 * Should SWZ-like expressions be lowered?
53 } /* anonymous namespace */
56 * Determine if an IR expression tree looks like an extended swizzle
58 * Extended swizzles consist of access of a single vector source (with possible
59 * per component negation) and the constants -1, 0, or 1.
62 is_extended_swizzle(ir_expression
*ir
)
64 /* Track any variables that are accessed by this expression.
66 ir_variable
*var
= NULL
;
68 assert(ir
->operation
== ir_quadop_vector
);
70 for (unsigned i
= 0; i
< ir
->type
->vector_elements
; i
++) {
71 ir_rvalue
*op
= ir
->operands
[i
];
74 switch (op
->ir_type
) {
75 case ir_type_constant
: {
76 const ir_constant
*const c
= op
->as_constant();
78 if (!c
->is_one() && !c
->is_zero() && !c
->is_negative_one())
85 case ir_type_dereference_variable
: {
86 ir_dereference_variable
*const d
= (ir_dereference_variable
*) op
;
88 if ((var
!= NULL
) && (var
!= d
->var
))
96 case ir_type_expression
: {
97 ir_expression
*const ex
= (ir_expression
*) op
;
99 if (ex
->operation
!= ir_unop_neg
)
102 op
= ex
->operands
[0];
106 case ir_type_swizzle
:
107 op
= ((ir_swizzle
*) op
)->val
;
120 lower_vector_visitor::handle_rvalue(ir_rvalue
**rvalue
)
125 ir_expression
*expr
= (*rvalue
)->as_expression();
126 if ((expr
== NULL
) || (expr
->operation
!= ir_quadop_vector
))
129 if (this->dont_lower_swz
&& is_extended_swizzle(expr
))
132 /* FINISHME: Is this the right thing to use for the ralloc context?
134 void *const mem_ctx
= expr
;
136 assert(expr
->type
->vector_elements
== expr
->num_operands
);
138 /* Generate a temporary with the same type as the ir_quadop_operation.
140 ir_variable
*const temp
=
141 new(mem_ctx
) ir_variable(expr
->type
, "vecop_tmp", ir_var_temporary
);
143 this->base_ir
->insert_before(temp
);
145 /* Counter of the number of components collected so far.
149 /* Write-mask in the destination that receives counted by 'assigned'.
154 /* Generate upto four assignments to that variable. Try to group component
155 * assignments together:
157 * - All constant components can be assigned at once.
158 * - All assigments of components from a single variable with the same
159 * unary operator can be assigned at once.
161 ir_constant_data d
= { { 0 } };
165 for (unsigned i
= 0; i
< expr
->type
->vector_elements
; i
++) {
166 const ir_constant
*const c
= expr
->operands
[i
]->as_constant();
171 switch (expr
->type
->base_type
) {
172 case GLSL_TYPE_UINT
: d
.u
[assigned
] = c
->value
.u
[0]; break;
173 case GLSL_TYPE_INT
: d
.i
[assigned
] = c
->value
.i
[0]; break;
174 case GLSL_TYPE_FLOAT
: d
.f
[assigned
] = c
->value
.f
[0]; break;
175 case GLSL_TYPE_BOOL
: d
.b
[assigned
] = c
->value
.b
[0]; break;
176 default: assert(!"Should not get here."); break;
179 write_mask
|= (1U << i
);
183 assert((write_mask
== 0) == (assigned
== 0));
185 /* If there were constant values, generate an assignment.
188 ir_constant
*const c
=
189 new(mem_ctx
) ir_constant(glsl_type::get_instance(expr
->type
->base_type
,
192 ir_dereference
*const lhs
= new(mem_ctx
) ir_dereference_variable(temp
);
193 ir_assignment
*const assign
=
194 new(mem_ctx
) ir_assignment(lhs
, c
, NULL
, write_mask
);
196 this->base_ir
->insert_before(assign
);
199 /* FINISHME: This should try to coalesce assignments.
201 for (unsigned i
= 0; i
< expr
->type
->vector_elements
; i
++) {
202 if (expr
->operands
[i
]->ir_type
== ir_type_constant
)
205 ir_dereference
*const lhs
= new(mem_ctx
) ir_dereference_variable(temp
);
206 ir_assignment
*const assign
=
207 new(mem_ctx
) ir_assignment(lhs
, expr
->operands
[i
], NULL
, (1U << i
));
209 this->base_ir
->insert_before(assign
);
213 assert(assigned
== expr
->type
->vector_elements
);
215 *rvalue
= new(mem_ctx
) ir_dereference_variable(temp
);
216 this->progress
= true;
220 lower_quadop_vector(exec_list
*instructions
, bool dont_lower_swz
)
222 lower_vector_visitor v
;
224 v
.dont_lower_swz
= dont_lower_swz
;
225 visit_list_elements(&v
, instructions
);