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 opt_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 "ir_builder.h"
36 #include "glsl_types.h"
38 using namespace ir_builder
;
43 * Visitor class for replacing expressions with ir_constant values.
46 class ir_algebraic_visitor
: public ir_rvalue_visitor
{
48 ir_algebraic_visitor()
50 this->progress
= false;
54 virtual ~ir_algebraic_visitor()
58 ir_rvalue
*handle_expression(ir_expression
*ir
);
59 void handle_rvalue(ir_rvalue
**rvalue
);
60 bool reassociate_constant(ir_expression
*ir1
,
62 ir_constant
*constant
,
64 void reassociate_operands(ir_expression
*ir1
,
68 ir_rvalue
*swizzle_if_required(ir_expression
*expr
,
76 } /* unnamed namespace */
79 is_vec_zero(ir_constant
*ir
)
81 return (ir
== NULL
) ? false : ir
->is_zero();
85 is_vec_one(ir_constant
*ir
)
87 return (ir
== NULL
) ? false : ir
->is_one();
91 is_vec_two(ir_constant
*ir
)
93 return (ir
== NULL
) ? false : ir
->is_value(2.0, 2);
97 is_vec_negative_one(ir_constant
*ir
)
99 return (ir
== NULL
) ? false : ir
->is_negative_one();
103 is_vec_basis(ir_constant
*ir
)
105 return (ir
== NULL
) ? false : ir
->is_basis();
109 update_type(ir_expression
*ir
)
111 if (ir
->operands
[0]->type
->is_vector())
112 ir
->type
= ir
->operands
[0]->type
;
114 ir
->type
= ir
->operands
[1]->type
;
118 ir_algebraic_visitor::reassociate_operands(ir_expression
*ir1
,
123 ir_rvalue
*temp
= ir2
->operands
[op2
];
124 ir2
->operands
[op2
] = ir1
->operands
[op1
];
125 ir1
->operands
[op1
] = temp
;
127 /* Update the type of ir2. The type of ir1 won't have changed --
128 * base types matched, and at least one of the operands of the 2
129 * binops is still a vector if any of them were.
133 this->progress
= true;
137 * Reassociates a constant down a tree of adds or multiplies.
139 * Consider (2 * (a * (b * 0.5))). We want to send up with a * b.
142 ir_algebraic_visitor::reassociate_constant(ir_expression
*ir1
, int const_index
,
143 ir_constant
*constant
,
146 if (!ir2
|| ir1
->operation
!= ir2
->operation
)
149 /* Don't want to even think about matrices. */
150 if (ir1
->operands
[0]->type
->is_matrix() ||
151 ir1
->operands
[1]->type
->is_matrix() ||
152 ir2
->operands
[0]->type
->is_matrix() ||
153 ir2
->operands
[1]->type
->is_matrix())
156 ir_constant
*ir2_const
[2];
157 ir2_const
[0] = ir2
->operands
[0]->constant_expression_value();
158 ir2_const
[1] = ir2
->operands
[1]->constant_expression_value();
160 if (ir2_const
[0] && ir2_const
[1])
164 reassociate_operands(ir1
, const_index
, ir2
, 1);
166 } else if (ir2_const
[1]) {
167 reassociate_operands(ir1
, const_index
, ir2
, 0);
171 if (reassociate_constant(ir1
, const_index
, constant
,
172 ir2
->operands
[0]->as_expression())) {
177 if (reassociate_constant(ir1
, const_index
, constant
,
178 ir2
->operands
[1]->as_expression())) {
186 /* When eliminating an expression and just returning one of its operands,
187 * we may need to swizzle that operand out to a vector if the expression was
191 ir_algebraic_visitor::swizzle_if_required(ir_expression
*expr
,
194 if (expr
->type
->is_vector() && operand
->type
->is_scalar()) {
195 return new(mem_ctx
) ir_swizzle(operand
, 0, 0, 0, 0,
196 expr
->type
->vector_elements
);
202 ir_algebraic_visitor::handle_expression(ir_expression
*ir
)
204 ir_constant
*op_const
[4] = {NULL
, NULL
, NULL
, NULL
};
205 ir_expression
*op_expr
[4] = {NULL
, NULL
, NULL
, NULL
};
208 assert(ir
->get_num_operands() <= 4);
209 for (i
= 0; i
< ir
->get_num_operands(); i
++) {
210 if (ir
->operands
[i
]->type
->is_matrix())
213 op_const
[i
] = ir
->operands
[i
]->constant_expression_value();
214 op_expr
[i
] = ir
->operands
[i
]->as_expression();
217 if (this->mem_ctx
== NULL
)
218 this->mem_ctx
= ralloc_parent(ir
);
220 switch (ir
->operation
) {
221 case ir_unop_bit_not
:
222 if (op_expr
[0] && op_expr
[0]->operation
== ir_unop_bit_not
)
223 return op_expr
[0]->operands
[0];
227 if (op_expr
[0] == NULL
)
230 switch (op_expr
[0]->operation
) {
233 return abs(op_expr
[0]->operands
[0]);
240 if (op_expr
[0] == NULL
)
243 if (op_expr
[0]->operation
== ir_unop_neg
) {
244 return op_expr
[0]->operands
[0];
249 if (op_expr
[0] == NULL
)
252 if (op_expr
[0]->operation
== ir_unop_log
) {
253 return op_expr
[0]->operands
[0];
258 if (op_expr
[0] == NULL
)
261 if (op_expr
[0]->operation
== ir_unop_exp
) {
262 return op_expr
[0]->operands
[0];
267 if (op_expr
[0] == NULL
)
270 if (op_expr
[0]->operation
== ir_unop_log2
) {
271 return op_expr
[0]->operands
[0];
276 if (op_expr
[0] == NULL
)
279 if (op_expr
[0]->operation
== ir_unop_exp2
) {
280 return op_expr
[0]->operands
[0];
284 case ir_unop_logic_not
: {
285 enum ir_expression_operation new_op
= ir_unop_logic_not
;
287 if (op_expr
[0] == NULL
)
290 switch (op_expr
[0]->operation
) {
291 case ir_binop_less
: new_op
= ir_binop_gequal
; break;
292 case ir_binop_greater
: new_op
= ir_binop_lequal
; break;
293 case ir_binop_lequal
: new_op
= ir_binop_greater
; break;
294 case ir_binop_gequal
: new_op
= ir_binop_less
; break;
295 case ir_binop_equal
: new_op
= ir_binop_nequal
; break;
296 case ir_binop_nequal
: new_op
= ir_binop_equal
; break;
297 case ir_binop_all_equal
: new_op
= ir_binop_any_nequal
; break;
298 case ir_binop_any_nequal
: new_op
= ir_binop_all_equal
; break;
301 /* The default case handler is here to silence a warning from GCC.
306 if (new_op
!= ir_unop_logic_not
) {
307 return new(mem_ctx
) ir_expression(new_op
,
309 op_expr
[0]->operands
[0],
310 op_expr
[0]->operands
[1]);
317 if (is_vec_zero(op_const
[0]))
318 return ir
->operands
[1];
319 if (is_vec_zero(op_const
[1]))
320 return ir
->operands
[0];
322 /* Reassociate addition of constants so that we can do constant
325 if (op_const
[0] && !op_const
[1])
326 reassociate_constant(ir
, 0, op_const
[0], op_expr
[1]);
327 if (op_const
[1] && !op_const
[0])
328 reassociate_constant(ir
, 1, op_const
[1], op_expr
[0]);
330 /* Replace (-x + y) * a + x and commutative variations with lrp(x, y, a).
333 * (x * -a) + (y * a) + x
334 * x + (x * -a) + (y * a)
335 * x * (1 - a) + y * a
338 for (int mul_pos
= 0; mul_pos
< 2; mul_pos
++) {
339 ir_expression
*mul
= op_expr
[mul_pos
];
341 if (!mul
|| mul
->operation
!= ir_binop_mul
)
344 /* Multiply found on one of the operands. Now check for an
345 * inner addition operation.
347 for (int inner_add_pos
= 0; inner_add_pos
< 2; inner_add_pos
++) {
348 ir_expression
*inner_add
=
349 mul
->operands
[inner_add_pos
]->as_expression();
351 if (!inner_add
|| inner_add
->operation
!= ir_binop_add
)
354 /* Inner addition found on one of the operands. Now check for
355 * one of the operands of the inner addition to be the negative
358 for (int neg_pos
= 0; neg_pos
< 2; neg_pos
++) {
360 inner_add
->operands
[neg_pos
]->as_expression();
362 if (!neg
|| neg
->operation
!= ir_unop_neg
)
365 ir_rvalue
*x_operand
= ir
->operands
[1 - mul_pos
];
367 if (!neg
->operands
[0]->equals(x_operand
))
370 ir_rvalue
*y_operand
= inner_add
->operands
[1 - neg_pos
];
371 ir_rvalue
*a_operand
= mul
->operands
[1 - inner_add_pos
];
373 if (x_operand
->type
!= y_operand
->type
||
374 x_operand
->type
!= a_operand
->type
)
377 return lrp(x_operand
, y_operand
, a_operand
);
384 if (is_vec_zero(op_const
[0]))
385 return neg(ir
->operands
[1]);
386 if (is_vec_zero(op_const
[1]))
387 return ir
->operands
[0];
391 if (is_vec_one(op_const
[0]))
392 return ir
->operands
[1];
393 if (is_vec_one(op_const
[1]))
394 return ir
->operands
[0];
396 if (is_vec_zero(op_const
[0]) || is_vec_zero(op_const
[1]))
397 return ir_constant::zero(ir
, ir
->type
);
399 if (is_vec_negative_one(op_const
[0]))
400 return neg(ir
->operands
[1]);
401 if (is_vec_negative_one(op_const
[1]))
402 return neg(ir
->operands
[0]);
405 /* Reassociate multiplication of constants so that we can do
408 if (op_const
[0] && !op_const
[1])
409 reassociate_constant(ir
, 0, op_const
[0], op_expr
[1]);
410 if (op_const
[1] && !op_const
[0])
411 reassociate_constant(ir
, 1, op_const
[1], op_expr
[0]);
416 if (is_vec_one(op_const
[0]) && ir
->type
->base_type
== GLSL_TYPE_FLOAT
) {
417 return new(mem_ctx
) ir_expression(ir_unop_rcp
,
418 ir
->operands
[1]->type
,
422 if (is_vec_one(op_const
[1]))
423 return ir
->operands
[0];
427 if (is_vec_zero(op_const
[0]) || is_vec_zero(op_const
[1]))
428 return ir_constant::zero(mem_ctx
, ir
->type
);
430 if (is_vec_basis(op_const
[0])) {
431 unsigned component
= 0;
432 for (unsigned c
= 0; c
< op_const
[0]->type
->vector_elements
; c
++) {
433 if (op_const
[0]->value
.f
[c
] == 1.0)
436 return new(mem_ctx
) ir_swizzle(ir
->operands
[1], component
, 0, 0, 0, 1);
438 if (is_vec_basis(op_const
[1])) {
439 unsigned component
= 0;
440 for (unsigned c
= 0; c
< op_const
[1]->type
->vector_elements
; c
++) {
441 if (op_const
[1]->value
.f
[c
] == 1.0)
444 return new(mem_ctx
) ir_swizzle(ir
->operands
[0], component
, 0, 0, 0, 1);
448 case ir_binop_rshift
:
449 case ir_binop_lshift
:
451 if (is_vec_zero(op_const
[0]))
452 return ir
->operands
[0];
454 if (is_vec_zero(op_const
[1]))
455 return ir
->operands
[0];
458 case ir_binop_logic_and
:
459 if (is_vec_one(op_const
[0])) {
460 return ir
->operands
[1];
461 } else if (is_vec_one(op_const
[1])) {
462 return ir
->operands
[0];
463 } else if (is_vec_zero(op_const
[0]) || is_vec_zero(op_const
[1])) {
464 return ir_constant::zero(mem_ctx
, ir
->type
);
465 } else if (op_expr
[0] && op_expr
[0]->operation
== ir_unop_logic_not
&&
466 op_expr
[1] && op_expr
[1]->operation
== ir_unop_logic_not
) {
468 * (not A) and (not B) === not (A or B)
470 return logic_not(logic_or(op_expr
[0]->operands
[0],
471 op_expr
[1]->operands
[0]));
472 } else if (ir
->operands
[0]->equals(ir
->operands
[1])) {
474 return ir
->operands
[0];
478 case ir_binop_logic_xor
:
479 if (is_vec_zero(op_const
[0])) {
480 return ir
->operands
[1];
481 } else if (is_vec_zero(op_const
[1])) {
482 return ir
->operands
[0];
483 } else if (is_vec_one(op_const
[0])) {
484 return logic_not(ir
->operands
[1]);
485 } else if (is_vec_one(op_const
[1])) {
486 return logic_not(ir
->operands
[0]);
487 } else if (ir
->operands
[0]->equals(ir
->operands
[1])) {
488 /* (a ^^ a) == false */
489 return ir_constant::zero(mem_ctx
, ir
->type
);
493 case ir_binop_logic_or
:
494 if (is_vec_zero(op_const
[0])) {
495 return ir
->operands
[1];
496 } else if (is_vec_zero(op_const
[1])) {
497 return ir
->operands
[0];
498 } else if (is_vec_one(op_const
[0]) || is_vec_one(op_const
[1])) {
499 ir_constant_data data
;
501 for (unsigned i
= 0; i
< 16; i
++)
504 return new(mem_ctx
) ir_constant(ir
->type
, &data
);
505 } else if (op_expr
[0] && op_expr
[0]->operation
== ir_unop_logic_not
&&
506 op_expr
[1] && op_expr
[1]->operation
== ir_unop_logic_not
) {
508 * (not A) or (not B) === not (A and B)
510 return logic_not(logic_and(op_expr
[0]->operands
[0],
511 op_expr
[1]->operands
[0]));
512 } else if (ir
->operands
[0]->equals(ir
->operands
[1])) {
514 return ir
->operands
[0];
520 if (is_vec_one(op_const
[0]))
524 if (is_vec_one(op_const
[1]))
525 return ir
->operands
[0];
527 /* pow(2,x) == exp2(x) */
528 if (is_vec_two(op_const
[0]))
529 return expr(ir_unop_exp2
, ir
->operands
[1]);
534 if (op_expr
[0] && op_expr
[0]->operation
== ir_unop_rcp
)
535 return op_expr
[0]->operands
[0];
537 /* While ir_to_mesa.cpp will lower sqrt(x) to rcp(rsq(x)), it does so at
538 * its IR level, so we can always apply this transformation.
540 if (op_expr
[0] && op_expr
[0]->operation
== ir_unop_rsq
)
541 return sqrt(op_expr
[0]->operands
[0]);
543 /* As far as we know, all backends are OK with rsq. */
544 if (op_expr
[0] && op_expr
[0]->operation
== ir_unop_sqrt
) {
545 return rsq(op_expr
[0]->operands
[0]);
551 /* Operands are op0 * op1 + op2. */
552 if (is_vec_zero(op_const
[0]) || is_vec_zero(op_const
[1])) {
553 return ir
->operands
[2];
554 } else if (is_vec_zero(op_const
[2])) {
555 return mul(ir
->operands
[0], ir
->operands
[1]);
556 } else if (is_vec_one(op_const
[0])) {
557 return add(ir
->operands
[1], ir
->operands
[2]);
558 } else if (is_vec_one(op_const
[1])) {
559 return add(ir
->operands
[0], ir
->operands
[2]);
564 /* Operands are (x, y, a). */
565 if (is_vec_zero(op_const
[2])) {
566 return ir
->operands
[0];
567 } else if (is_vec_one(op_const
[2])) {
568 return ir
->operands
[1];
569 } else if (ir
->operands
[0]->equals(ir
->operands
[1])) {
570 return ir
->operands
[0];
571 } else if (is_vec_zero(op_const
[0])) {
572 return mul(ir
->operands
[1], ir
->operands
[2]);
573 } else if (is_vec_zero(op_const
[1])) {
574 unsigned op2_components
= ir
->operands
[2]->type
->vector_elements
;
575 ir_constant
*one
= new(mem_ctx
) ir_constant(1.0f
, op2_components
);
576 return mul(ir
->operands
[0], add(one
, neg(ir
->operands
[2])));
581 if (is_vec_one(op_const
[0]))
582 return ir
->operands
[1];
583 if (is_vec_zero(op_const
[0]))
584 return ir
->operands
[2];
595 ir_algebraic_visitor::handle_rvalue(ir_rvalue
**rvalue
)
600 ir_expression
*expr
= (*rvalue
)->as_expression();
601 if (!expr
|| expr
->operation
== ir_quadop_vector
)
604 ir_rvalue
*new_rvalue
= handle_expression(expr
);
605 if (new_rvalue
== *rvalue
)
608 /* If the expr used to be some vec OP scalar returning a vector, and the
609 * optimization gave us back a scalar, we still need to turn it into a
612 *rvalue
= swizzle_if_required(expr
, new_rvalue
);
614 this->progress
= true;
618 do_algebraic(exec_list
*instructions
)
620 ir_algebraic_visitor v
;
622 visit_list_elements(&v
, instructions
);