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(bool native_integers
,
49 const struct gl_shader_compiler_options
*options
)
52 this->progress
= false;
54 this->native_integers
= native_integers
;
57 virtual ~ir_algebraic_visitor()
61 ir_rvalue
*handle_expression(ir_expression
*ir
);
62 void handle_rvalue(ir_rvalue
**rvalue
);
63 bool reassociate_constant(ir_expression
*ir1
,
65 ir_constant
*constant
,
67 void reassociate_operands(ir_expression
*ir1
,
71 ir_rvalue
*swizzle_if_required(ir_expression
*expr
,
74 const struct gl_shader_compiler_options
*options
;
81 } /* unnamed namespace */
84 is_vec_zero(ir_constant
*ir
)
86 return (ir
== NULL
) ? false : ir
->is_zero();
90 is_vec_one(ir_constant
*ir
)
92 return (ir
== NULL
) ? false : ir
->is_one();
96 is_vec_two(ir_constant
*ir
)
98 return (ir
== NULL
) ? false : ir
->is_value(2.0, 2);
102 is_vec_four(ir_constant
*ir
)
104 return (ir
== NULL
) ? false : ir
->is_value(4.0, 4);
108 is_vec_negative_one(ir_constant
*ir
)
110 return (ir
== NULL
) ? false : ir
->is_negative_one();
114 is_valid_vec_const(ir_constant
*ir
)
119 if (!ir
->type
->is_scalar() && !ir
->type
->is_vector())
126 is_less_than_one(ir_constant
*ir
)
128 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
);
130 if (!is_valid_vec_const(ir
))
133 unsigned component
= 0;
134 for (int c
= 0; c
< ir
->type
->vector_elements
; c
++) {
135 if (ir
->get_float_component(c
) < 1.0f
)
139 return (component
== ir
->type
->vector_elements
);
143 is_greater_than_zero(ir_constant
*ir
)
145 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
);
147 if (!is_valid_vec_const(ir
))
150 unsigned component
= 0;
151 for (int c
= 0; c
< ir
->type
->vector_elements
; c
++) {
152 if (ir
->get_float_component(c
) > 0.0f
)
156 return (component
== ir
->type
->vector_elements
);
160 update_type(ir_expression
*ir
)
162 if (ir
->operands
[0]->type
->is_vector())
163 ir
->type
= ir
->operands
[0]->type
;
165 ir
->type
= ir
->operands
[1]->type
;
168 /* Recognize (v.x + v.y) + (v.z + v.w) as dot(v, 1.0) */
169 static ir_expression
*
170 try_replace_with_dot(ir_expression
*expr0
, ir_expression
*expr1
, void *mem_ctx
)
172 if (expr0
&& expr0
->operation
== ir_binop_add
&&
173 expr0
->type
->is_float() &&
174 expr1
&& expr1
->operation
== ir_binop_add
&&
175 expr1
->type
->is_float()) {
176 ir_swizzle
*x
= expr0
->operands
[0]->as_swizzle();
177 ir_swizzle
*y
= expr0
->operands
[1]->as_swizzle();
178 ir_swizzle
*z
= expr1
->operands
[0]->as_swizzle();
179 ir_swizzle
*w
= expr1
->operands
[1]->as_swizzle();
181 if (!x
|| x
->mask
.num_components
!= 1 ||
182 !y
|| y
->mask
.num_components
!= 1 ||
183 !z
|| z
->mask
.num_components
!= 1 ||
184 !w
|| w
->mask
.num_components
!= 1) {
188 bool swiz_seen
[4] = {false, false, false, false};
189 swiz_seen
[x
->mask
.x
] = true;
190 swiz_seen
[y
->mask
.x
] = true;
191 swiz_seen
[z
->mask
.x
] = true;
192 swiz_seen
[w
->mask
.x
] = true;
194 if (!swiz_seen
[0] || !swiz_seen
[1] ||
195 !swiz_seen
[2] || !swiz_seen
[3]) {
199 if (x
->val
->equals(y
->val
) &&
200 x
->val
->equals(z
->val
) &&
201 x
->val
->equals(w
->val
)) {
202 return dot(x
->val
, new(mem_ctx
) ir_constant(1.0f
, 4));
209 ir_algebraic_visitor::reassociate_operands(ir_expression
*ir1
,
214 ir_rvalue
*temp
= ir2
->operands
[op2
];
215 ir2
->operands
[op2
] = ir1
->operands
[op1
];
216 ir1
->operands
[op1
] = temp
;
218 /* Update the type of ir2. The type of ir1 won't have changed --
219 * base types matched, and at least one of the operands of the 2
220 * binops is still a vector if any of them were.
224 this->progress
= true;
228 * Reassociates a constant down a tree of adds or multiplies.
230 * Consider (2 * (a * (b * 0.5))). We want to send up with a * b.
233 ir_algebraic_visitor::reassociate_constant(ir_expression
*ir1
, int const_index
,
234 ir_constant
*constant
,
237 if (!ir2
|| ir1
->operation
!= ir2
->operation
)
240 /* Don't want to even think about matrices. */
241 if (ir1
->operands
[0]->type
->is_matrix() ||
242 ir1
->operands
[1]->type
->is_matrix() ||
243 ir2
->operands
[0]->type
->is_matrix() ||
244 ir2
->operands
[1]->type
->is_matrix())
247 ir_constant
*ir2_const
[2];
248 ir2_const
[0] = ir2
->operands
[0]->constant_expression_value();
249 ir2_const
[1] = ir2
->operands
[1]->constant_expression_value();
251 if (ir2_const
[0] && ir2_const
[1])
255 reassociate_operands(ir1
, const_index
, ir2
, 1);
257 } else if (ir2_const
[1]) {
258 reassociate_operands(ir1
, const_index
, ir2
, 0);
262 if (reassociate_constant(ir1
, const_index
, constant
,
263 ir2
->operands
[0]->as_expression())) {
268 if (reassociate_constant(ir1
, const_index
, constant
,
269 ir2
->operands
[1]->as_expression())) {
277 /* When eliminating an expression and just returning one of its operands,
278 * we may need to swizzle that operand out to a vector if the expression was
282 ir_algebraic_visitor::swizzle_if_required(ir_expression
*expr
,
285 if (expr
->type
->is_vector() && operand
->type
->is_scalar()) {
286 return new(mem_ctx
) ir_swizzle(operand
, 0, 0, 0, 0,
287 expr
->type
->vector_elements
);
293 ir_algebraic_visitor::handle_expression(ir_expression
*ir
)
295 ir_constant
*op_const
[4] = {NULL
, NULL
, NULL
, NULL
};
296 ir_expression
*op_expr
[4] = {NULL
, NULL
, NULL
, NULL
};
299 if (ir
->operation
== ir_binop_mul
&&
300 ir
->operands
[0]->type
->is_matrix() &&
301 ir
->operands
[1]->type
->is_vector()) {
302 ir_expression
*matrix_mul
= ir
->operands
[0]->as_expression();
304 if (matrix_mul
&& matrix_mul
->operation
== ir_binop_mul
&&
305 matrix_mul
->operands
[0]->type
->is_matrix() &&
306 matrix_mul
->operands
[1]->type
->is_matrix()) {
308 return mul(matrix_mul
->operands
[0],
309 mul(matrix_mul
->operands
[1], ir
->operands
[1]));
313 assert(ir
->get_num_operands() <= 4);
314 for (i
= 0; i
< ir
->get_num_operands(); i
++) {
315 if (ir
->operands
[i
]->type
->is_matrix())
318 op_const
[i
] = ir
->operands
[i
]->constant_expression_value();
319 op_expr
[i
] = ir
->operands
[i
]->as_expression();
322 if (this->mem_ctx
== NULL
)
323 this->mem_ctx
= ralloc_parent(ir
);
325 switch (ir
->operation
) {
326 case ir_unop_bit_not
:
327 if (op_expr
[0] && op_expr
[0]->operation
== ir_unop_bit_not
)
328 return op_expr
[0]->operands
[0];
332 if (op_expr
[0] == NULL
)
335 switch (op_expr
[0]->operation
) {
338 return abs(op_expr
[0]->operands
[0]);
345 if (op_expr
[0] == NULL
)
348 if (op_expr
[0]->operation
== ir_unop_neg
) {
349 return op_expr
[0]->operands
[0];
354 if (op_expr
[0] == NULL
)
357 if (op_expr
[0]->operation
== ir_unop_log
) {
358 return op_expr
[0]->operands
[0];
363 if (op_expr
[0] == NULL
)
366 if (op_expr
[0]->operation
== ir_unop_exp
) {
367 return op_expr
[0]->operands
[0];
372 if (op_expr
[0] == NULL
)
375 if (op_expr
[0]->operation
== ir_unop_log2
) {
376 return op_expr
[0]->operands
[0];
379 if (!options
->EmitNoPow
&& op_expr
[0]->operation
== ir_binop_mul
) {
380 for (int log2_pos
= 0; log2_pos
< 2; log2_pos
++) {
381 ir_expression
*log2_expr
=
382 op_expr
[0]->operands
[log2_pos
]->as_expression();
384 if (log2_expr
&& log2_expr
->operation
== ir_unop_log2
) {
385 return new(mem_ctx
) ir_expression(ir_binop_pow
,
387 log2_expr
->operands
[0],
388 op_expr
[0]->operands
[1 - log2_pos
]);
395 if (op_expr
[0] == NULL
)
398 if (op_expr
[0]->operation
== ir_unop_exp2
) {
399 return op_expr
[0]->operands
[0];
405 if (op_expr
[0] && op_expr
[0]->operation
== ir_unop_trunc
) {
406 return new(mem_ctx
) ir_expression(ir
->operation
,
408 op_expr
[0]->operands
[0]);
412 case ir_unop_logic_not
: {
413 enum ir_expression_operation new_op
= ir_unop_logic_not
;
415 if (op_expr
[0] == NULL
)
418 switch (op_expr
[0]->operation
) {
419 case ir_binop_less
: new_op
= ir_binop_gequal
; break;
420 case ir_binop_greater
: new_op
= ir_binop_lequal
; break;
421 case ir_binop_lequal
: new_op
= ir_binop_greater
; break;
422 case ir_binop_gequal
: new_op
= ir_binop_less
; break;
423 case ir_binop_equal
: new_op
= ir_binop_nequal
; break;
424 case ir_binop_nequal
: new_op
= ir_binop_equal
; break;
425 case ir_binop_all_equal
: new_op
= ir_binop_any_nequal
; break;
426 case ir_binop_any_nequal
: new_op
= ir_binop_all_equal
; break;
429 /* The default case handler is here to silence a warning from GCC.
434 if (new_op
!= ir_unop_logic_not
) {
435 return new(mem_ctx
) ir_expression(new_op
,
437 op_expr
[0]->operands
[0],
438 op_expr
[0]->operands
[1]);
444 case ir_unop_saturate
:
445 if (op_expr
[0] && op_expr
[0]->operation
== ir_binop_add
) {
446 ir_expression
*b2f_0
= op_expr
[0]->operands
[0]->as_expression();
447 ir_expression
*b2f_1
= op_expr
[0]->operands
[1]->as_expression();
449 if (b2f_0
&& b2f_0
->operation
== ir_unop_b2f
&&
450 b2f_1
&& b2f_1
->operation
== ir_unop_b2f
) {
451 return b2f(logic_or(b2f_0
->operands
[0], b2f_1
->operands
[0]));
457 if (is_vec_zero(op_const
[0]))
458 return ir
->operands
[1];
459 if (is_vec_zero(op_const
[1]))
460 return ir
->operands
[0];
462 /* Reassociate addition of constants so that we can do constant
465 if (op_const
[0] && !op_const
[1])
466 reassociate_constant(ir
, 0, op_const
[0], op_expr
[1]);
467 if (op_const
[1] && !op_const
[0])
468 reassociate_constant(ir
, 1, op_const
[1], op_expr
[0]);
470 /* Recognize (v.x + v.y) + (v.z + v.w) as dot(v, 1.0) */
471 if (options
->OptimizeForAOS
) {
472 ir_expression
*expr
= try_replace_with_dot(op_expr
[0], op_expr
[1],
478 /* Replace (-x + y) * a + x and commutative variations with lrp(x, y, a).
481 * (x * -a) + (y * a) + x
482 * x + (x * -a) + (y * a)
483 * x * (1 - a) + y * a
486 for (int mul_pos
= 0; mul_pos
< 2; mul_pos
++) {
487 ir_expression
*mul
= op_expr
[mul_pos
];
489 if (!mul
|| mul
->operation
!= ir_binop_mul
)
492 /* Multiply found on one of the operands. Now check for an
493 * inner addition operation.
495 for (int inner_add_pos
= 0; inner_add_pos
< 2; inner_add_pos
++) {
496 ir_expression
*inner_add
=
497 mul
->operands
[inner_add_pos
]->as_expression();
499 if (!inner_add
|| inner_add
->operation
!= ir_binop_add
)
502 /* Inner addition found on one of the operands. Now check for
503 * one of the operands of the inner addition to be the negative
506 for (int neg_pos
= 0; neg_pos
< 2; neg_pos
++) {
508 inner_add
->operands
[neg_pos
]->as_expression();
510 if (!neg
|| neg
->operation
!= ir_unop_neg
)
513 ir_rvalue
*x_operand
= ir
->operands
[1 - mul_pos
];
515 if (!neg
->operands
[0]->equals(x_operand
))
518 ir_rvalue
*y_operand
= inner_add
->operands
[1 - neg_pos
];
519 ir_rvalue
*a_operand
= mul
->operands
[1 - inner_add_pos
];
521 if (x_operand
->type
!= y_operand
->type
||
522 x_operand
->type
!= a_operand
->type
)
525 return lrp(x_operand
, y_operand
, a_operand
);
533 if (is_vec_zero(op_const
[0]))
534 return neg(ir
->operands
[1]);
535 if (is_vec_zero(op_const
[1]))
536 return ir
->operands
[0];
540 if (is_vec_one(op_const
[0]))
541 return ir
->operands
[1];
542 if (is_vec_one(op_const
[1]))
543 return ir
->operands
[0];
545 if (is_vec_zero(op_const
[0]) || is_vec_zero(op_const
[1]))
546 return ir_constant::zero(ir
, ir
->type
);
548 if (is_vec_negative_one(op_const
[0]))
549 return neg(ir
->operands
[1]);
550 if (is_vec_negative_one(op_const
[1]))
551 return neg(ir
->operands
[0]);
553 if (op_expr
[0] && op_expr
[0]->operation
== ir_unop_b2f
&&
554 op_expr
[1] && op_expr
[1]->operation
== ir_unop_b2f
) {
555 return b2f(logic_and(op_expr
[0]->operands
[0], op_expr
[1]->operands
[0]));
558 /* Reassociate multiplication of constants so that we can do
561 if (op_const
[0] && !op_const
[1])
562 reassociate_constant(ir
, 0, op_const
[0], op_expr
[1]);
563 if (op_const
[1] && !op_const
[0])
564 reassociate_constant(ir
, 1, op_const
[1], op_expr
[0]);
568 * (mul (floor (add (abs x) 0.5) (sign x)))
572 * (trunc (add x (mul (sign x) 0.5)))
574 for (int i
= 0; i
< 2; i
++) {
575 ir_expression
*sign_expr
= ir
->operands
[i
]->as_expression();
576 ir_expression
*floor_expr
= ir
->operands
[1 - i
]->as_expression();
578 if (!sign_expr
|| sign_expr
->operation
!= ir_unop_sign
||
579 !floor_expr
|| floor_expr
->operation
!= ir_unop_floor
)
582 ir_expression
*add_expr
= floor_expr
->operands
[0]->as_expression();
583 if (!add_expr
|| add_expr
->operation
!= ir_binop_add
)
586 for (int j
= 0; j
< 2; j
++) {
587 ir_expression
*abs_expr
= add_expr
->operands
[j
]->as_expression();
588 if (!abs_expr
|| abs_expr
->operation
!= ir_unop_abs
)
591 ir_constant
*point_five
= add_expr
->operands
[1 - j
]->as_constant();
592 if (!point_five
|| !point_five
->is_value(0.5, 0))
595 if (abs_expr
->operands
[0]->equals(sign_expr
->operands
[0])) {
596 return trunc(add(abs_expr
->operands
[0],
597 mul(sign_expr
, point_five
)));
604 if (is_vec_one(op_const
[0]) && (
605 ir
->type
->base_type
== GLSL_TYPE_FLOAT
||
606 ir
->type
->base_type
== GLSL_TYPE_DOUBLE
)) {
607 return new(mem_ctx
) ir_expression(ir_unop_rcp
,
608 ir
->operands
[1]->type
,
612 if (is_vec_one(op_const
[1]))
613 return ir
->operands
[0];
617 if (is_vec_zero(op_const
[0]) || is_vec_zero(op_const
[1]))
618 return ir_constant::zero(mem_ctx
, ir
->type
);
620 for (int i
= 0; i
< 2; i
++) {
624 unsigned components
[4] = { 0 }, count
= 0;
626 for (unsigned c
= 0; c
< op_const
[i
]->type
->vector_elements
; c
++) {
627 if (op_const
[i
]->is_zero())
630 components
[count
] = c
;
634 /* No channels had zero values; bail. */
635 if (count
>= op_const
[i
]->type
->vector_elements
)
638 ir_expression_operation op
= count
== 1 ?
639 ir_binop_mul
: ir_binop_dot
;
641 /* Swizzle both operands to remove the channels that were zero. */
643 ir_expression(op
, ir
->type
,
644 new(mem_ctx
) ir_swizzle(ir
->operands
[0],
646 new(mem_ctx
) ir_swizzle(ir
->operands
[1],
652 case ir_binop_lequal
:
653 case ir_binop_greater
:
654 case ir_binop_gequal
:
656 case ir_binop_nequal
:
657 for (int add_pos
= 0; add_pos
< 2; add_pos
++) {
658 ir_expression
*add
= op_expr
[add_pos
];
660 if (!add
|| add
->operation
!= ir_binop_add
)
663 ir_constant
*zero
= op_const
[1 - add_pos
];
664 if (!is_vec_zero(zero
))
667 /* Depending of the zero position we want to optimize
668 * (0 cmp x+y) into (-x cmp y) or (x+y cmp 0) into (x cmp -y)
671 return new(mem_ctx
) ir_expression(ir
->operation
,
672 neg(add
->operands
[0]),
675 return new(mem_ctx
) ir_expression(ir
->operation
,
677 neg(add
->operands
[1]));
682 case ir_binop_all_equal
:
683 case ir_binop_any_nequal
:
684 if (ir
->operands
[0]->type
->is_scalar() &&
685 ir
->operands
[1]->type
->is_scalar())
686 return new(mem_ctx
) ir_expression(ir
->operation
== ir_binop_all_equal
687 ? ir_binop_equal
: ir_binop_nequal
,
692 case ir_binop_rshift
:
693 case ir_binop_lshift
:
695 if (is_vec_zero(op_const
[0]))
696 return ir
->operands
[0];
698 if (is_vec_zero(op_const
[1]))
699 return ir
->operands
[0];
702 case ir_binop_logic_and
:
703 if (is_vec_one(op_const
[0])) {
704 return ir
->operands
[1];
705 } else if (is_vec_one(op_const
[1])) {
706 return ir
->operands
[0];
707 } else if (is_vec_zero(op_const
[0]) || is_vec_zero(op_const
[1])) {
708 return ir_constant::zero(mem_ctx
, ir
->type
);
709 } else if (op_expr
[0] && op_expr
[0]->operation
== ir_unop_logic_not
&&
710 op_expr
[1] && op_expr
[1]->operation
== ir_unop_logic_not
) {
712 * (not A) and (not B) === not (A or B)
714 return logic_not(logic_or(op_expr
[0]->operands
[0],
715 op_expr
[1]->operands
[0]));
716 } else if (ir
->operands
[0]->equals(ir
->operands
[1])) {
718 return ir
->operands
[0];
722 case ir_binop_logic_xor
:
723 if (is_vec_zero(op_const
[0])) {
724 return ir
->operands
[1];
725 } else if (is_vec_zero(op_const
[1])) {
726 return ir
->operands
[0];
727 } else if (is_vec_one(op_const
[0])) {
728 return logic_not(ir
->operands
[1]);
729 } else if (is_vec_one(op_const
[1])) {
730 return logic_not(ir
->operands
[0]);
731 } else if (ir
->operands
[0]->equals(ir
->operands
[1])) {
732 /* (a ^^ a) == false */
733 return ir_constant::zero(mem_ctx
, ir
->type
);
737 case ir_binop_logic_or
:
738 if (is_vec_zero(op_const
[0])) {
739 return ir
->operands
[1];
740 } else if (is_vec_zero(op_const
[1])) {
741 return ir
->operands
[0];
742 } else if (is_vec_one(op_const
[0]) || is_vec_one(op_const
[1])) {
743 ir_constant_data data
;
745 for (unsigned i
= 0; i
< 16; i
++)
748 return new(mem_ctx
) ir_constant(ir
->type
, &data
);
749 } else if (op_expr
[0] && op_expr
[0]->operation
== ir_unop_logic_not
&&
750 op_expr
[1] && op_expr
[1]->operation
== ir_unop_logic_not
) {
752 * (not A) or (not B) === not (A and B)
754 return logic_not(logic_and(op_expr
[0]->operands
[0],
755 op_expr
[1]->operands
[0]));
756 } else if (ir
->operands
[0]->equals(ir
->operands
[1])) {
758 return ir
->operands
[0];
764 if (is_vec_one(op_const
[0]))
768 if (is_vec_one(op_const
[1]))
769 return ir
->operands
[0];
771 /* pow(2,x) == exp2(x) */
772 if (is_vec_two(op_const
[0]))
773 return expr(ir_unop_exp2
, ir
->operands
[1]);
775 if (is_vec_two(op_const
[1])) {
776 ir_variable
*x
= new(ir
) ir_variable(ir
->operands
[1]->type
, "x",
778 base_ir
->insert_before(x
);
779 base_ir
->insert_before(assign(x
, ir
->operands
[0]));
783 if (is_vec_four(op_const
[1])) {
784 ir_variable
*x
= new(ir
) ir_variable(ir
->operands
[1]->type
, "x",
786 base_ir
->insert_before(x
);
787 base_ir
->insert_before(assign(x
, ir
->operands
[0]));
789 ir_variable
*squared
= new(ir
) ir_variable(ir
->operands
[1]->type
,
792 base_ir
->insert_before(squared
);
793 base_ir
->insert_before(assign(squared
, mul(x
, x
)));
794 return mul(squared
, squared
);
801 if (ir
->type
->base_type
!= GLSL_TYPE_FLOAT
|| options
->EmitNoSat
)
804 /* Replace min(max) operations and its commutative combinations with
805 * a saturate operation
807 for (int op
= 0; op
< 2; op
++) {
808 ir_expression
*inner_expr
= op_expr
[op
];
809 ir_constant
*outer_const
= op_const
[1 - op
];
810 ir_expression_operation op_cond
= (ir
->operation
== ir_binop_max
) ?
811 ir_binop_min
: ir_binop_max
;
813 if (!inner_expr
|| !outer_const
|| (inner_expr
->operation
!= op_cond
))
816 /* One of these has to be a constant */
817 if (!inner_expr
->operands
[0]->as_constant() &&
818 !inner_expr
->operands
[1]->as_constant())
821 /* Found a min(max) combination. Now try to see if its operands
822 * meet our conditions that we can do just a single saturate operation
824 for (int minmax_op
= 0; minmax_op
< 2; minmax_op
++) {
825 ir_rvalue
*x
= inner_expr
->operands
[minmax_op
];
826 ir_rvalue
*y
= inner_expr
->operands
[1 - minmax_op
];
828 ir_constant
*inner_const
= y
->as_constant();
832 /* min(max(x, 0.0), 1.0) is sat(x) */
833 if (ir
->operation
== ir_binop_min
&&
834 inner_const
->is_zero() &&
835 outer_const
->is_one())
838 /* max(min(x, 1.0), 0.0) is sat(x) */
839 if (ir
->operation
== ir_binop_max
&&
840 inner_const
->is_one() &&
841 outer_const
->is_zero())
844 /* min(max(x, 0.0), b) where b < 1.0 is sat(min(x, b)) */
845 if (ir
->operation
== ir_binop_min
&&
846 inner_const
->is_zero() &&
847 is_less_than_one(outer_const
))
848 return saturate(expr(ir_binop_min
, x
, outer_const
));
850 /* max(min(x, b), 0.0) where b < 1.0 is sat(min(x, b)) */
851 if (ir
->operation
== ir_binop_max
&&
852 is_less_than_one(inner_const
) &&
853 outer_const
->is_zero())
854 return saturate(expr(ir_binop_min
, x
, inner_const
));
856 /* max(min(x, 1.0), b) where b > 0.0 is sat(max(x, b)) */
857 if (ir
->operation
== ir_binop_max
&&
858 inner_const
->is_one() &&
859 is_greater_than_zero(outer_const
))
860 return saturate(expr(ir_binop_max
, x
, outer_const
));
862 /* min(max(x, b), 1.0) where b > 0.0 is sat(max(x, b)) */
863 if (ir
->operation
== ir_binop_min
&&
864 is_greater_than_zero(inner_const
) &&
865 outer_const
->is_one())
866 return saturate(expr(ir_binop_max
, x
, inner_const
));
873 if (op_expr
[0] && op_expr
[0]->operation
== ir_unop_rcp
)
874 return op_expr
[0]->operands
[0];
876 if (op_expr
[0] && (op_expr
[0]->operation
== ir_unop_exp2
||
877 op_expr
[0]->operation
== ir_unop_exp
)) {
878 return new(mem_ctx
) ir_expression(op_expr
[0]->operation
, ir
->type
,
879 neg(op_expr
[0]->operands
[0]));
882 /* While ir_to_mesa.cpp will lower sqrt(x) to rcp(rsq(x)), it does so at
883 * its IR level, so we can always apply this transformation.
885 if (op_expr
[0] && op_expr
[0]->operation
== ir_unop_rsq
)
886 return sqrt(op_expr
[0]->operands
[0]);
888 /* As far as we know, all backends are OK with rsq. */
889 if (op_expr
[0] && op_expr
[0]->operation
== ir_unop_sqrt
) {
890 return rsq(op_expr
[0]->operands
[0]);
896 /* Operands are op0 * op1 + op2. */
897 if (is_vec_zero(op_const
[0]) || is_vec_zero(op_const
[1])) {
898 return ir
->operands
[2];
899 } else if (is_vec_zero(op_const
[2])) {
900 return mul(ir
->operands
[0], ir
->operands
[1]);
901 } else if (is_vec_one(op_const
[0])) {
902 return add(ir
->operands
[1], ir
->operands
[2]);
903 } else if (is_vec_one(op_const
[1])) {
904 return add(ir
->operands
[0], ir
->operands
[2]);
909 /* Operands are (x, y, a). */
910 if (is_vec_zero(op_const
[2])) {
911 return ir
->operands
[0];
912 } else if (is_vec_one(op_const
[2])) {
913 return ir
->operands
[1];
914 } else if (ir
->operands
[0]->equals(ir
->operands
[1])) {
915 return ir
->operands
[0];
916 } else if (is_vec_zero(op_const
[0])) {
917 return mul(ir
->operands
[1], ir
->operands
[2]);
918 } else if (is_vec_zero(op_const
[1])) {
919 unsigned op2_components
= ir
->operands
[2]->type
->vector_elements
;
922 switch (ir
->type
->base_type
) {
923 case GLSL_TYPE_FLOAT
:
924 one
= new(mem_ctx
) ir_constant(1.0f
, op2_components
);
926 case GLSL_TYPE_DOUBLE
:
927 one
= new(mem_ctx
) ir_constant(1.0, op2_components
);
931 unreachable("unexpected type");
934 return mul(ir
->operands
[0], add(one
, neg(ir
->operands
[2])));
939 if (is_vec_one(op_const
[0]))
940 return ir
->operands
[1];
941 if (is_vec_zero(op_const
[0]))
942 return ir
->operands
[2];
953 ir_algebraic_visitor::handle_rvalue(ir_rvalue
**rvalue
)
958 ir_expression
*expr
= (*rvalue
)->as_expression();
959 if (!expr
|| expr
->operation
== ir_quadop_vector
)
962 ir_rvalue
*new_rvalue
= handle_expression(expr
);
963 if (new_rvalue
== *rvalue
)
966 /* If the expr used to be some vec OP scalar returning a vector, and the
967 * optimization gave us back a scalar, we still need to turn it into a
970 *rvalue
= swizzle_if_required(expr
, new_rvalue
);
972 this->progress
= true;
976 do_algebraic(exec_list
*instructions
, bool native_integers
,
977 const struct gl_shader_compiler_options
*options
)
979 ir_algebraic_visitor
v(native_integers
, options
);
981 visit_list_elements(&v
, instructions
);