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_algebraic.cpp
27 * Takes advantage of association, commutivity, and other algebraic
28 * properties to simplify expressions.
32 #include "ir_visitor.h"
33 #include "ir_rvalue_visitor.h"
34 #include "ir_optimization.h"
35 #include "glsl_types.h"
38 * Visitor class for replacing expressions with ir_constant values.
41 class ir_algebraic_visitor
: public ir_rvalue_visitor
{
43 ir_algebraic_visitor()
45 this->progress
= false;
48 virtual ~ir_algebraic_visitor()
52 ir_rvalue
*handle_expression(ir_expression
*ir
);
53 void handle_rvalue(ir_rvalue
**rvalue
);
54 bool reassociate_constant(ir_expression
*ir1
,
56 ir_constant
*constant
,
58 void reassociate_operands(ir_expression
*ir1
,
66 is_vec_zero(ir_constant
*ir
)
72 if (!ir
->type
->is_scalar() &&
73 !ir
->type
->is_vector())
76 for (c
= 0; c
< ir
->type
->vector_elements
; c
++) {
77 switch (ir
->type
->base_type
) {
79 if (ir
->value
.f
[c
] != 0.0)
83 if (ir
->value
.i
[c
] != 0)
87 if (ir
->value
.u
[c
] != 0)
91 if (ir
->value
.b
[c
] != false)
95 assert(!"bad base type");
104 is_vec_one(ir_constant
*ir
)
110 if (!ir
->type
->is_scalar() &&
111 !ir
->type
->is_vector())
114 for (c
= 0; c
< ir
->type
->vector_elements
; c
++) {
115 switch (ir
->type
->base_type
) {
116 case GLSL_TYPE_FLOAT
:
117 if (ir
->value
.f
[c
] != 1.0)
121 if (ir
->value
.i
[c
] != 1)
125 if (ir
->value
.u
[c
] != 1)
129 if (ir
->value
.b
[c
] != true)
133 assert(!"bad base type");
142 update_type(ir_expression
*ir
)
144 if (ir
->operands
[0]->type
->is_vector())
145 ir
->type
= ir
->operands
[0]->type
;
147 ir
->type
= ir
->operands
[1]->type
;
151 ir_algebraic_visitor::reassociate_operands(ir_expression
*ir1
,
156 ir_rvalue
*temp
= ir2
->operands
[op2
];
157 ir2
->operands
[op2
] = ir1
->operands
[op1
];
158 ir1
->operands
[op1
] = temp
;
160 /* Update the type of ir2. The type of ir1 won't have changed --
161 * base types matched, and at least one of the operands of the 2
162 * binops is still a vector if any of them were.
166 this->progress
= true;
170 * Reassociates a constant down a tree of adds or multiplies.
172 * Consider (2 * (a * (b * 0.5))). We want to send up with a * b.
175 ir_algebraic_visitor::reassociate_constant(ir_expression
*ir1
, int const_index
,
176 ir_constant
*constant
,
179 if (!ir2
|| ir1
->operation
!= ir2
->operation
)
182 /* Don't want to even think about matrices. */
183 if (ir1
->operands
[0]->type
->is_matrix() ||
184 ir1
->operands
[0]->type
->is_matrix() ||
185 ir2
->operands
[1]->type
->is_matrix() ||
186 ir2
->operands
[1]->type
->is_matrix())
189 ir_constant
*ir2_const
[2];
190 ir2_const
[0] = ir2
->operands
[0]->constant_expression_value();
191 ir2_const
[1] = ir2
->operands
[1]->constant_expression_value();
193 if (ir2_const
[0] && ir2_const
[1])
197 reassociate_operands(ir1
, const_index
, ir2
, 1);
199 } else if (ir2_const
[1]) {
200 reassociate_operands(ir1
, const_index
, ir2
, 0);
204 if (reassociate_constant(ir1
, const_index
, constant
,
205 ir2
->operands
[0]->as_expression())) {
210 if (reassociate_constant(ir1
, const_index
, constant
,
211 ir2
->operands
[1]->as_expression())) {
220 ir_algebraic_visitor::handle_expression(ir_expression
*ir
)
222 ir_constant
*op_const
[2] = {NULL
, NULL
};
223 ir_expression
*op_expr
[2] = {NULL
, NULL
};
226 for (i
= 0; i
< ir
->get_num_operands(); i
++) {
227 if (ir
->operands
[i
]->type
->is_matrix())
230 op_const
[i
] = ir
->operands
[i
]->constant_expression_value();
231 op_expr
[i
] = ir
->operands
[i
]->as_expression();
234 switch (ir
->operation
) {
235 case ir_unop_logic_not
: {
236 enum ir_expression_operation new_op
= ir_unop_logic_not
;
238 if (op_expr
[0] == NULL
)
241 switch (op_expr
[0]->operation
) {
242 case ir_binop_less
: new_op
= ir_binop_gequal
; break;
243 case ir_binop_greater
: new_op
= ir_binop_lequal
; break;
244 case ir_binop_lequal
: new_op
= ir_binop_greater
; break;
245 case ir_binop_gequal
: new_op
= ir_binop_less
; break;
246 case ir_binop_equal
: new_op
= ir_binop_nequal
; break;
247 case ir_binop_nequal
: new_op
= ir_binop_equal
; break;
250 /* The default case handler is here to silence a warning from GCC.
255 if (new_op
!= ir_unop_logic_not
) {
256 this->progress
= true;
257 return new(ir
) ir_expression(new_op
,
259 op_expr
[0]->operands
[0],
260 op_expr
[0]->operands
[1]);
267 if (is_vec_zero(op_const
[0])) {
268 this->progress
= true;
269 return ir
->operands
[1];
271 if (is_vec_zero(op_const
[1])) {
272 this->progress
= true;
273 return ir
->operands
[0];
276 /* Reassociate addition of constants so that we can do constant
279 if (op_const
[0] && !op_const
[1])
280 reassociate_constant(ir
, 0, op_const
[0],
281 ir
->operands
[1]->as_expression());
282 if (op_const
[1] && !op_const
[0])
283 reassociate_constant(ir
, 1, op_const
[1],
284 ir
->operands
[0]->as_expression());
288 if (is_vec_zero(op_const
[0])) {
289 this->progress
= true;
290 return new(ir
) ir_expression(ir_unop_neg
,
295 if (is_vec_zero(op_const
[1])) {
296 this->progress
= true;
297 return ir
->operands
[0];
302 if (is_vec_one(op_const
[0])) {
303 this->progress
= true;
304 return ir
->operands
[1];
306 if (is_vec_one(op_const
[1])) {
307 this->progress
= true;
308 return ir
->operands
[0];
311 if (is_vec_zero(op_const
[0]) || is_vec_zero(op_const
[1])) {
312 this->progress
= true;
313 return ir_constant::zero(ir
, ir
->type
);
316 /* Reassociate multiplication of constants so that we can do
319 if (op_const
[0] && !op_const
[1])
320 reassociate_constant(ir
, 0, op_const
[0],
321 ir
->operands
[1]->as_expression());
322 if (op_const
[1] && !op_const
[0])
323 reassociate_constant(ir
, 1, op_const
[1],
324 ir
->operands
[0]->as_expression());
329 if (is_vec_one(op_const
[0]) && ir
->type
->base_type
== GLSL_TYPE_FLOAT
) {
330 this->progress
= true;
331 return new(ir
) ir_expression(ir_unop_rcp
,
336 if (is_vec_one(op_const
[1])) {
337 this->progress
= true;
338 return ir
->operands
[0];
343 if (op_expr
[0] && op_expr
[0]->operation
== ir_unop_rcp
) {
344 this->progress
= true;
345 return op_expr
[0]->operands
[0];
348 /* FINISHME: We should do rcp(rsq(x)) -> sqrt(x) for some
349 * backends, except that some backends will have done sqrt ->
350 * rcp(rsq(x)) and we don't want to undo it for them.
353 /* As far as we know, all backends are OK with rsq. */
354 if (op_expr
[0] && op_expr
[0]->operation
== ir_unop_sqrt
) {
355 this->progress
= true;
356 return new(ir
) ir_expression(ir_unop_rsq
,
358 op_expr
[0]->operands
[0],
372 ir_algebraic_visitor::handle_rvalue(ir_rvalue
**rvalue
)
377 ir_expression
*expr
= (*rvalue
)->as_expression();
381 *rvalue
= handle_expression(expr
);
385 do_algebraic(exec_list
*instructions
)
387 ir_algebraic_visitor v
;
389 visit_list_elements(&v
, instructions
);