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 "compiler/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 virtual ir_visitor_status
visit_enter(ir_assignment
*ir
);
63 ir_rvalue
*handle_expression(ir_expression
*ir
);
64 void handle_rvalue(ir_rvalue
**rvalue
);
65 bool reassociate_constant(ir_expression
*ir1
,
67 ir_constant
*constant
,
69 void reassociate_operands(ir_expression
*ir1
,
73 ir_rvalue
*swizzle_if_required(ir_expression
*expr
,
76 const struct gl_shader_compiler_options
*options
;
83 } /* unnamed namespace */
86 ir_algebraic_visitor::visit_enter(ir_assignment
*ir
)
88 ir_variable
*var
= ir
->lhs
->variable_referenced();
89 if (var
->data
.invariant
|| var
->data
.precise
) {
90 /* If we're assigning to an invariant or precise variable, just bail.
91 * Most of the algebraic optimizations aren't precision-safe.
93 * FINISHME: Find out which optimizations are precision-safe and enable
94 * then only for invariant or precise trees.
96 return visit_continue_with_parent
;
98 return visit_continue
;
103 is_vec_zero(ir_constant
*ir
)
105 return (ir
== NULL
) ? false : ir
->is_zero();
109 is_vec_one(ir_constant
*ir
)
111 return (ir
== NULL
) ? false : ir
->is_one();
115 is_vec_two(ir_constant
*ir
)
117 return (ir
== NULL
) ? false : ir
->is_value(2.0, 2);
121 is_vec_four(ir_constant
*ir
)
123 return (ir
== NULL
) ? false : ir
->is_value(4.0, 4);
127 is_vec_negative_one(ir_constant
*ir
)
129 return (ir
== NULL
) ? false : ir
->is_negative_one();
133 is_valid_vec_const(ir_constant
*ir
)
138 if (!ir
->type
->is_scalar() && !ir
->type
->is_vector())
145 is_less_than_one(ir_constant
*ir
)
147 assert(ir
->type
->is_float());
149 if (!is_valid_vec_const(ir
))
152 unsigned component
= 0;
153 for (int c
= 0; c
< ir
->type
->vector_elements
; c
++) {
154 if (ir
->get_float_component(c
) < 1.0f
)
158 return (component
== ir
->type
->vector_elements
);
162 is_greater_than_zero(ir_constant
*ir
)
164 assert(ir
->type
->is_float());
166 if (!is_valid_vec_const(ir
))
169 unsigned component
= 0;
170 for (int c
= 0; c
< ir
->type
->vector_elements
; c
++) {
171 if (ir
->get_float_component(c
) > 0.0f
)
175 return (component
== ir
->type
->vector_elements
);
179 update_type(ir_expression
*ir
)
181 if (ir
->operands
[0]->type
->is_vector())
182 ir
->type
= ir
->operands
[0]->type
;
184 ir
->type
= ir
->operands
[1]->type
;
187 /* Recognize (v.x + v.y) + (v.z + v.w) as dot(v, 1.0) */
188 static ir_expression
*
189 try_replace_with_dot(ir_expression
*expr0
, ir_expression
*expr1
, void *mem_ctx
)
191 if (expr0
&& expr0
->operation
== ir_binop_add
&&
192 expr0
->type
->is_float() &&
193 expr1
&& expr1
->operation
== ir_binop_add
&&
194 expr1
->type
->is_float()) {
195 ir_swizzle
*x
= expr0
->operands
[0]->as_swizzle();
196 ir_swizzle
*y
= expr0
->operands
[1]->as_swizzle();
197 ir_swizzle
*z
= expr1
->operands
[0]->as_swizzle();
198 ir_swizzle
*w
= expr1
->operands
[1]->as_swizzle();
200 if (!x
|| x
->mask
.num_components
!= 1 ||
201 !y
|| y
->mask
.num_components
!= 1 ||
202 !z
|| z
->mask
.num_components
!= 1 ||
203 !w
|| w
->mask
.num_components
!= 1) {
207 bool swiz_seen
[4] = {false, false, false, false};
208 swiz_seen
[x
->mask
.x
] = true;
209 swiz_seen
[y
->mask
.x
] = true;
210 swiz_seen
[z
->mask
.x
] = true;
211 swiz_seen
[w
->mask
.x
] = true;
213 if (!swiz_seen
[0] || !swiz_seen
[1] ||
214 !swiz_seen
[2] || !swiz_seen
[3]) {
218 if (x
->val
->equals(y
->val
) &&
219 x
->val
->equals(z
->val
) &&
220 x
->val
->equals(w
->val
)) {
221 return dot(x
->val
, new(mem_ctx
) ir_constant(1.0f
, 4));
228 ir_algebraic_visitor::reassociate_operands(ir_expression
*ir1
,
233 ir_rvalue
*temp
= ir2
->operands
[op2
];
234 ir2
->operands
[op2
] = ir1
->operands
[op1
];
235 ir1
->operands
[op1
] = temp
;
237 /* Update the type of ir2. The type of ir1 won't have changed --
238 * base types matched, and at least one of the operands of the 2
239 * binops is still a vector if any of them were.
243 this->progress
= true;
247 * Reassociates a constant down a tree of adds or multiplies.
249 * Consider (2 * (a * (b * 0.5))). We want to end up with a * b.
252 ir_algebraic_visitor::reassociate_constant(ir_expression
*ir1
, int const_index
,
253 ir_constant
*constant
,
256 if (!ir2
|| ir1
->operation
!= ir2
->operation
)
259 /* Don't want to even think about matrices. */
260 if (ir1
->operands
[0]->type
->is_matrix() ||
261 ir1
->operands
[1]->type
->is_matrix() ||
262 ir2
->operands
[0]->type
->is_matrix() ||
263 ir2
->operands
[1]->type
->is_matrix())
266 void *mem_ctx
= ralloc_parent(ir2
);
268 ir_constant
*ir2_const
[2];
269 ir2_const
[0] = ir2
->operands
[0]->constant_expression_value(mem_ctx
);
270 ir2_const
[1] = ir2
->operands
[1]->constant_expression_value(mem_ctx
);
272 if (ir2_const
[0] && ir2_const
[1])
276 reassociate_operands(ir1
, const_index
, ir2
, 1);
278 } else if (ir2_const
[1]) {
279 reassociate_operands(ir1
, const_index
, ir2
, 0);
283 if (reassociate_constant(ir1
, const_index
, constant
,
284 ir2
->operands
[0]->as_expression())) {
289 if (reassociate_constant(ir1
, const_index
, constant
,
290 ir2
->operands
[1]->as_expression())) {
298 /* When eliminating an expression and just returning one of its operands,
299 * we may need to swizzle that operand out to a vector if the expression was
303 ir_algebraic_visitor::swizzle_if_required(ir_expression
*expr
,
306 if (expr
->type
->is_vector() && operand
->type
->is_scalar()) {
307 return new(mem_ctx
) ir_swizzle(operand
, 0, 0, 0, 0,
308 expr
->type
->vector_elements
);
314 ir_algebraic_visitor::handle_expression(ir_expression
*ir
)
316 ir_constant
*op_const
[4] = {NULL
, NULL
, NULL
, NULL
};
317 ir_expression
*op_expr
[4] = {NULL
, NULL
, NULL
, NULL
};
319 if (ir
->operation
== ir_binop_mul
&&
320 ir
->operands
[0]->type
->is_matrix() &&
321 ir
->operands
[1]->type
->is_vector()) {
322 ir_expression
*matrix_mul
= ir
->operands
[0]->as_expression();
324 if (matrix_mul
&& matrix_mul
->operation
== ir_binop_mul
&&
325 matrix_mul
->operands
[0]->type
->is_matrix() &&
326 matrix_mul
->operands
[1]->type
->is_matrix()) {
328 return mul(matrix_mul
->operands
[0],
329 mul(matrix_mul
->operands
[1], ir
->operands
[1]));
333 assert(ir
->num_operands
<= 4);
334 for (unsigned i
= 0; i
< ir
->num_operands
; i
++) {
335 if (ir
->operands
[i
]->type
->is_matrix())
339 ir
->operands
[i
]->constant_expression_value(ralloc_parent(ir
));
340 op_expr
[i
] = ir
->operands
[i
]->as_expression();
343 if (this->mem_ctx
== NULL
)
344 this->mem_ctx
= ralloc_parent(ir
);
346 switch (ir
->operation
) {
347 case ir_unop_bit_not
:
348 if (op_expr
[0] && op_expr
[0]->operation
== ir_unop_bit_not
)
349 return op_expr
[0]->operands
[0];
353 if (op_expr
[0] == NULL
)
356 switch (op_expr
[0]->operation
) {
359 return abs(op_expr
[0]->operands
[0]);
366 if (op_expr
[0] == NULL
)
369 if (op_expr
[0]->operation
== ir_unop_neg
) {
370 return op_expr
[0]->operands
[0];
375 if (op_expr
[0] == NULL
)
378 if (op_expr
[0]->operation
== ir_unop_log
) {
379 return op_expr
[0]->operands
[0];
384 if (op_expr
[0] == NULL
)
387 if (op_expr
[0]->operation
== ir_unop_exp
) {
388 return op_expr
[0]->operands
[0];
393 if (op_expr
[0] == NULL
)
396 if (op_expr
[0]->operation
== ir_unop_log2
) {
397 return op_expr
[0]->operands
[0];
400 if (!options
->EmitNoPow
&& op_expr
[0]->operation
== ir_binop_mul
) {
401 for (int log2_pos
= 0; log2_pos
< 2; log2_pos
++) {
402 ir_expression
*log2_expr
=
403 op_expr
[0]->operands
[log2_pos
]->as_expression();
405 if (log2_expr
&& log2_expr
->operation
== ir_unop_log2
) {
406 return new(mem_ctx
) ir_expression(ir_binop_pow
,
408 log2_expr
->operands
[0],
409 op_expr
[0]->operands
[1 - log2_pos
]);
416 if (op_expr
[0] == NULL
)
419 if (op_expr
[0]->operation
== ir_unop_exp2
) {
420 return op_expr
[0]->operands
[0];
426 if (op_expr
[0] && op_expr
[0]->operation
== ir_unop_trunc
) {
427 return new(mem_ctx
) ir_expression(ir
->operation
,
429 op_expr
[0]->operands
[0]);
433 case ir_unop_logic_not
: {
434 enum ir_expression_operation new_op
= ir_unop_logic_not
;
436 if (op_expr
[0] == NULL
)
439 switch (op_expr
[0]->operation
) {
440 case ir_binop_less
: new_op
= ir_binop_gequal
; break;
441 case ir_binop_greater
: new_op
= ir_binop_lequal
; break;
442 case ir_binop_lequal
: new_op
= ir_binop_greater
; break;
443 case ir_binop_gequal
: new_op
= ir_binop_less
; break;
444 case ir_binop_equal
: new_op
= ir_binop_nequal
; break;
445 case ir_binop_nequal
: new_op
= ir_binop_equal
; break;
446 case ir_binop_all_equal
: new_op
= ir_binop_any_nequal
; break;
447 case ir_binop_any_nequal
: new_op
= ir_binop_all_equal
; break;
450 /* The default case handler is here to silence a warning from GCC.
455 if (new_op
!= ir_unop_logic_not
) {
456 return new(mem_ctx
) ir_expression(new_op
,
458 op_expr
[0]->operands
[0],
459 op_expr
[0]->operands
[1]);
465 case ir_unop_saturate
:
466 if (op_expr
[0] && op_expr
[0]->operation
== ir_binop_add
) {
467 ir_expression
*b2f_0
= op_expr
[0]->operands
[0]->as_expression();
468 ir_expression
*b2f_1
= op_expr
[0]->operands
[1]->as_expression();
470 if (b2f_0
&& b2f_0
->operation
== ir_unop_b2f
&&
471 b2f_1
&& b2f_1
->operation
== ir_unop_b2f
) {
472 return b2f(logic_or(b2f_0
->operands
[0], b2f_1
->operands
[0]));
477 /* This macro CANNOT use the do { } while(true) mechanism because
478 * then the breaks apply to the loop instead of the switch!
480 #define HANDLE_PACK_UNPACK_INVERSE(inverse_operation) \
482 ir_expression *const op = ir->operands[0]->as_expression(); \
485 if (op->operation == (inverse_operation)) \
486 return op->operands[0]; \
490 case ir_unop_unpack_uint_2x32
:
491 HANDLE_PACK_UNPACK_INVERSE(ir_unop_pack_uint_2x32
);
492 case ir_unop_pack_uint_2x32
:
493 HANDLE_PACK_UNPACK_INVERSE(ir_unop_unpack_uint_2x32
);
494 case ir_unop_unpack_int_2x32
:
495 HANDLE_PACK_UNPACK_INVERSE(ir_unop_pack_int_2x32
);
496 case ir_unop_pack_int_2x32
:
497 HANDLE_PACK_UNPACK_INVERSE(ir_unop_unpack_int_2x32
);
498 case ir_unop_unpack_double_2x32
:
499 HANDLE_PACK_UNPACK_INVERSE(ir_unop_pack_double_2x32
);
500 case ir_unop_pack_double_2x32
:
501 HANDLE_PACK_UNPACK_INVERSE(ir_unop_unpack_double_2x32
);
503 #undef HANDLE_PACK_UNPACK_INVERSE
506 if (is_vec_zero(op_const
[0]))
507 return ir
->operands
[1];
508 if (is_vec_zero(op_const
[1]))
509 return ir
->operands
[0];
511 /* Reassociate addition of constants so that we can do constant
514 if (op_const
[0] && !op_const
[1])
515 reassociate_constant(ir
, 0, op_const
[0], op_expr
[1]);
516 if (op_const
[1] && !op_const
[0])
517 reassociate_constant(ir
, 1, op_const
[1], op_expr
[0]);
519 /* Recognize (v.x + v.y) + (v.z + v.w) as dot(v, 1.0) */
520 if (options
->OptimizeForAOS
) {
521 ir_expression
*expr
= try_replace_with_dot(op_expr
[0], op_expr
[1],
527 /* Replace (-x + y) * a + x and commutative variations with lrp(x, y, a).
530 * (x * -a) + (y * a) + x
531 * x + (x * -a) + (y * a)
532 * x * (1 - a) + y * a
535 for (int mul_pos
= 0; mul_pos
< 2; mul_pos
++) {
536 ir_expression
*mul
= op_expr
[mul_pos
];
538 if (!mul
|| mul
->operation
!= ir_binop_mul
)
541 /* Multiply found on one of the operands. Now check for an
542 * inner addition operation.
544 for (int inner_add_pos
= 0; inner_add_pos
< 2; inner_add_pos
++) {
545 ir_expression
*inner_add
=
546 mul
->operands
[inner_add_pos
]->as_expression();
548 if (!inner_add
|| inner_add
->operation
!= ir_binop_add
)
551 /* Inner addition found on one of the operands. Now check for
552 * one of the operands of the inner addition to be the negative
555 for (int neg_pos
= 0; neg_pos
< 2; neg_pos
++) {
557 inner_add
->operands
[neg_pos
]->as_expression();
559 if (!neg
|| neg
->operation
!= ir_unop_neg
)
562 ir_rvalue
*x_operand
= ir
->operands
[1 - mul_pos
];
564 if (!neg
->operands
[0]->equals(x_operand
))
567 ir_rvalue
*y_operand
= inner_add
->operands
[1 - neg_pos
];
568 ir_rvalue
*a_operand
= mul
->operands
[1 - inner_add_pos
];
570 if (x_operand
->type
!= y_operand
->type
||
571 x_operand
->type
!= a_operand
->type
)
574 return lrp(x_operand
, y_operand
, a_operand
);
582 if (is_vec_zero(op_const
[0]))
583 return neg(ir
->operands
[1]);
584 if (is_vec_zero(op_const
[1]))
585 return ir
->operands
[0];
589 if (is_vec_one(op_const
[0]))
590 return ir
->operands
[1];
591 if (is_vec_one(op_const
[1]))
592 return ir
->operands
[0];
594 if (is_vec_zero(op_const
[0]) || is_vec_zero(op_const
[1]))
595 return ir_constant::zero(ir
, ir
->type
);
597 if (is_vec_negative_one(op_const
[0]))
598 return neg(ir
->operands
[1]);
599 if (is_vec_negative_one(op_const
[1]))
600 return neg(ir
->operands
[0]);
602 if (op_expr
[0] && op_expr
[0]->operation
== ir_unop_b2f
&&
603 op_expr
[1] && op_expr
[1]->operation
== ir_unop_b2f
) {
604 return b2f(logic_and(op_expr
[0]->operands
[0], op_expr
[1]->operands
[0]));
607 /* Reassociate multiplication of constants so that we can do
610 if (op_const
[0] && !op_const
[1])
611 reassociate_constant(ir
, 0, op_const
[0], op_expr
[1]);
612 if (op_const
[1] && !op_const
[0])
613 reassociate_constant(ir
, 1, op_const
[1], op_expr
[0]);
617 * (mul (floor (add (abs x) 0.5) (sign x)))
621 * (trunc (add x (mul (sign x) 0.5)))
623 for (int i
= 0; i
< 2; i
++) {
624 ir_expression
*sign_expr
= ir
->operands
[i
]->as_expression();
625 ir_expression
*floor_expr
= ir
->operands
[1 - i
]->as_expression();
627 if (!sign_expr
|| sign_expr
->operation
!= ir_unop_sign
||
628 !floor_expr
|| floor_expr
->operation
!= ir_unop_floor
)
631 ir_expression
*add_expr
= floor_expr
->operands
[0]->as_expression();
632 if (!add_expr
|| add_expr
->operation
!= ir_binop_add
)
635 for (int j
= 0; j
< 2; j
++) {
636 ir_expression
*abs_expr
= add_expr
->operands
[j
]->as_expression();
637 if (!abs_expr
|| abs_expr
->operation
!= ir_unop_abs
)
640 ir_constant
*point_five
= add_expr
->operands
[1 - j
]->as_constant();
641 if (!point_five
|| !point_five
->is_value(0.5, 0))
644 if (abs_expr
->operands
[0]->equals(sign_expr
->operands
[0])) {
645 return trunc(add(abs_expr
->operands
[0],
646 mul(sign_expr
, point_five
)));
653 if (is_vec_one(op_const
[0]) && (
654 ir
->type
->is_float() || ir
->type
->is_double())) {
655 return new(mem_ctx
) ir_expression(ir_unop_rcp
,
656 ir
->operands
[1]->type
,
660 if (is_vec_one(op_const
[1]))
661 return ir
->operands
[0];
665 if (is_vec_zero(op_const
[0]) || is_vec_zero(op_const
[1]))
666 return ir_constant::zero(mem_ctx
, ir
->type
);
668 for (int i
= 0; i
< 2; i
++) {
672 unsigned components
[4] = { 0 }, count
= 0;
674 for (unsigned c
= 0; c
< op_const
[i
]->type
->vector_elements
; c
++) {
675 if (op_const
[i
]->is_zero())
678 components
[count
] = c
;
682 /* No channels had zero values; bail. */
683 if (count
>= op_const
[i
]->type
->vector_elements
)
686 ir_expression_operation op
= count
== 1 ?
687 ir_binop_mul
: ir_binop_dot
;
689 /* Swizzle both operands to remove the channels that were zero. */
691 ir_expression(op
, ir
->type
,
692 new(mem_ctx
) ir_swizzle(ir
->operands
[0],
694 new(mem_ctx
) ir_swizzle(ir
->operands
[1],
700 case ir_binop_lequal
:
701 case ir_binop_greater
:
702 case ir_binop_gequal
:
704 case ir_binop_nequal
:
705 for (int add_pos
= 0; add_pos
< 2; add_pos
++) {
706 ir_expression
*add
= op_expr
[add_pos
];
708 if (!add
|| add
->operation
!= ir_binop_add
)
711 ir_constant
*zero
= op_const
[1 - add_pos
];
712 if (!is_vec_zero(zero
))
715 /* Depending of the zero position we want to optimize
716 * (0 cmp x+y) into (-x cmp y) or (x+y cmp 0) into (x cmp -y)
719 return new(mem_ctx
) ir_expression(ir
->operation
,
720 neg(add
->operands
[0]),
723 return new(mem_ctx
) ir_expression(ir
->operation
,
725 neg(add
->operands
[1]));
730 case ir_binop_all_equal
:
731 case ir_binop_any_nequal
:
732 if (ir
->operands
[0]->type
->is_scalar() &&
733 ir
->operands
[1]->type
->is_scalar())
734 return new(mem_ctx
) ir_expression(ir
->operation
== ir_binop_all_equal
735 ? ir_binop_equal
: ir_binop_nequal
,
740 case ir_binop_rshift
:
741 case ir_binop_lshift
:
743 if (is_vec_zero(op_const
[0]))
744 return ir
->operands
[0];
746 if (is_vec_zero(op_const
[1]))
747 return ir
->operands
[0];
750 case ir_binop_logic_and
:
751 if (is_vec_one(op_const
[0])) {
752 return ir
->operands
[1];
753 } else if (is_vec_one(op_const
[1])) {
754 return ir
->operands
[0];
755 } else if (is_vec_zero(op_const
[0]) || is_vec_zero(op_const
[1])) {
756 return ir_constant::zero(mem_ctx
, ir
->type
);
757 } else if (op_expr
[0] && op_expr
[0]->operation
== ir_unop_logic_not
&&
758 op_expr
[1] && op_expr
[1]->operation
== ir_unop_logic_not
) {
760 * (not A) and (not B) === not (A or B)
762 return logic_not(logic_or(op_expr
[0]->operands
[0],
763 op_expr
[1]->operands
[0]));
764 } else if (ir
->operands
[0]->equals(ir
->operands
[1])) {
766 return ir
->operands
[0];
770 case ir_binop_logic_xor
:
771 if (is_vec_zero(op_const
[0])) {
772 return ir
->operands
[1];
773 } else if (is_vec_zero(op_const
[1])) {
774 return ir
->operands
[0];
775 } else if (is_vec_one(op_const
[0])) {
776 return logic_not(ir
->operands
[1]);
777 } else if (is_vec_one(op_const
[1])) {
778 return logic_not(ir
->operands
[0]);
779 } else if (ir
->operands
[0]->equals(ir
->operands
[1])) {
780 /* (a ^^ a) == false */
781 return ir_constant::zero(mem_ctx
, ir
->type
);
785 case ir_binop_logic_or
:
786 if (is_vec_zero(op_const
[0])) {
787 return ir
->operands
[1];
788 } else if (is_vec_zero(op_const
[1])) {
789 return ir
->operands
[0];
790 } else if (is_vec_one(op_const
[0]) || is_vec_one(op_const
[1])) {
791 ir_constant_data data
;
793 for (unsigned i
= 0; i
< 16; i
++)
796 return new(mem_ctx
) ir_constant(ir
->type
, &data
);
797 } else if (op_expr
[0] && op_expr
[0]->operation
== ir_unop_logic_not
&&
798 op_expr
[1] && op_expr
[1]->operation
== ir_unop_logic_not
) {
800 * (not A) or (not B) === not (A and B)
802 return logic_not(logic_and(op_expr
[0]->operands
[0],
803 op_expr
[1]->operands
[0]));
804 } else if (ir
->operands
[0]->equals(ir
->operands
[1])) {
806 return ir
->operands
[0];
812 if (is_vec_one(op_const
[0]))
816 if (is_vec_one(op_const
[1]))
817 return ir
->operands
[0];
819 /* pow(2,x) == exp2(x) */
820 if (is_vec_two(op_const
[0]))
821 return expr(ir_unop_exp2
, ir
->operands
[1]);
823 if (is_vec_two(op_const
[1])) {
824 ir_variable
*x
= new(ir
) ir_variable(ir
->operands
[1]->type
, "x",
826 base_ir
->insert_before(x
);
827 base_ir
->insert_before(assign(x
, ir
->operands
[0]));
831 if (is_vec_four(op_const
[1])) {
832 ir_variable
*x
= new(ir
) ir_variable(ir
->operands
[1]->type
, "x",
834 base_ir
->insert_before(x
);
835 base_ir
->insert_before(assign(x
, ir
->operands
[0]));
837 ir_variable
*squared
= new(ir
) ir_variable(ir
->operands
[1]->type
,
840 base_ir
->insert_before(squared
);
841 base_ir
->insert_before(assign(squared
, mul(x
, x
)));
842 return mul(squared
, squared
);
849 if (!ir
->type
->is_float() || options
->EmitNoSat
)
852 /* Replace min(max) operations and its commutative combinations with
853 * a saturate operation
855 for (int op
= 0; op
< 2; op
++) {
856 ir_expression
*inner_expr
= op_expr
[op
];
857 ir_constant
*outer_const
= op_const
[1 - op
];
858 ir_expression_operation op_cond
= (ir
->operation
== ir_binop_max
) ?
859 ir_binop_min
: ir_binop_max
;
861 if (!inner_expr
|| !outer_const
|| (inner_expr
->operation
!= op_cond
))
864 /* One of these has to be a constant */
865 if (!inner_expr
->operands
[0]->as_constant() &&
866 !inner_expr
->operands
[1]->as_constant())
869 /* Found a min(max) combination. Now try to see if its operands
870 * meet our conditions that we can do just a single saturate operation
872 for (int minmax_op
= 0; minmax_op
< 2; minmax_op
++) {
873 ir_rvalue
*x
= inner_expr
->operands
[minmax_op
];
874 ir_rvalue
*y
= inner_expr
->operands
[1 - minmax_op
];
876 ir_constant
*inner_const
= y
->as_constant();
880 /* min(max(x, 0.0), 1.0) is sat(x) */
881 if (ir
->operation
== ir_binop_min
&&
882 inner_const
->is_zero() &&
883 outer_const
->is_one())
886 /* max(min(x, 1.0), 0.0) is sat(x) */
887 if (ir
->operation
== ir_binop_max
&&
888 inner_const
->is_one() &&
889 outer_const
->is_zero())
892 /* min(max(x, 0.0), b) where b < 1.0 is sat(min(x, b)) */
893 if (ir
->operation
== ir_binop_min
&&
894 inner_const
->is_zero() &&
895 is_less_than_one(outer_const
))
896 return saturate(expr(ir_binop_min
, x
, outer_const
));
898 /* max(min(x, b), 0.0) where b < 1.0 is sat(min(x, b)) */
899 if (ir
->operation
== ir_binop_max
&&
900 is_less_than_one(inner_const
) &&
901 outer_const
->is_zero())
902 return saturate(expr(ir_binop_min
, x
, inner_const
));
904 /* max(min(x, 1.0), b) where b > 0.0 is sat(max(x, b)) */
905 if (ir
->operation
== ir_binop_max
&&
906 inner_const
->is_one() &&
907 is_greater_than_zero(outer_const
))
908 return saturate(expr(ir_binop_max
, x
, outer_const
));
910 /* min(max(x, b), 1.0) where b > 0.0 is sat(max(x, b)) */
911 if (ir
->operation
== ir_binop_min
&&
912 is_greater_than_zero(inner_const
) &&
913 outer_const
->is_one())
914 return saturate(expr(ir_binop_max
, x
, inner_const
));
921 if (op_expr
[0] && op_expr
[0]->operation
== ir_unop_rcp
)
922 return op_expr
[0]->operands
[0];
924 if (op_expr
[0] && (op_expr
[0]->operation
== ir_unop_exp2
||
925 op_expr
[0]->operation
== ir_unop_exp
)) {
926 return new(mem_ctx
) ir_expression(op_expr
[0]->operation
, ir
->type
,
927 neg(op_expr
[0]->operands
[0]));
930 /* While ir_to_mesa.cpp will lower sqrt(x) to rcp(rsq(x)), it does so at
931 * its IR level, so we can always apply this transformation.
933 if (op_expr
[0] && op_expr
[0]->operation
== ir_unop_rsq
)
934 return sqrt(op_expr
[0]->operands
[0]);
936 /* As far as we know, all backends are OK with rsq. */
937 if (op_expr
[0] && op_expr
[0]->operation
== ir_unop_sqrt
) {
938 return rsq(op_expr
[0]->operands
[0]);
944 /* Operands are op0 * op1 + op2. */
945 if (is_vec_zero(op_const
[0]) || is_vec_zero(op_const
[1])) {
946 return ir
->operands
[2];
947 } else if (is_vec_zero(op_const
[2])) {
948 return mul(ir
->operands
[0], ir
->operands
[1]);
949 } else if (is_vec_one(op_const
[0])) {
950 return add(ir
->operands
[1], ir
->operands
[2]);
951 } else if (is_vec_one(op_const
[1])) {
952 return add(ir
->operands
[0], ir
->operands
[2]);
957 /* Operands are (x, y, a). */
958 if (is_vec_zero(op_const
[2])) {
959 return ir
->operands
[0];
960 } else if (is_vec_one(op_const
[2])) {
961 return ir
->operands
[1];
962 } else if (ir
->operands
[0]->equals(ir
->operands
[1])) {
963 return ir
->operands
[0];
964 } else if (is_vec_zero(op_const
[0])) {
965 return mul(ir
->operands
[1], ir
->operands
[2]);
966 } else if (is_vec_zero(op_const
[1])) {
967 unsigned op2_components
= ir
->operands
[2]->type
->vector_elements
;
970 switch (ir
->type
->base_type
) {
971 case GLSL_TYPE_FLOAT
:
972 one
= new(mem_ctx
) ir_constant(1.0f
, op2_components
);
974 case GLSL_TYPE_DOUBLE
:
975 one
= new(mem_ctx
) ir_constant(1.0, op2_components
);
979 unreachable("unexpected type");
982 return mul(ir
->operands
[0], add(one
, neg(ir
->operands
[2])));
987 if (is_vec_one(op_const
[0]))
988 return ir
->operands
[1];
989 if (is_vec_zero(op_const
[0]))
990 return ir
->operands
[2];
993 /* Remove interpolateAt* instructions for demoted inputs. They are
994 * assigned a constant expression to facilitate this.
996 case ir_unop_interpolate_at_centroid
:
997 case ir_binop_interpolate_at_offset
:
998 case ir_binop_interpolate_at_sample
:
1000 return ir
->operands
[0];
1011 ir_algebraic_visitor::handle_rvalue(ir_rvalue
**rvalue
)
1016 ir_expression
*expr
= (*rvalue
)->as_expression();
1017 if (!expr
|| expr
->operation
== ir_quadop_vector
)
1020 ir_rvalue
*new_rvalue
= handle_expression(expr
);
1021 if (new_rvalue
== *rvalue
)
1024 /* If the expr used to be some vec OP scalar returning a vector, and the
1025 * optimization gave us back a scalar, we still need to turn it into a
1028 *rvalue
= swizzle_if_required(expr
, new_rvalue
);
1030 this->progress
= true;
1034 do_algebraic(exec_list
*instructions
, bool native_integers
,
1035 const struct gl_shader_compiler_options
*options
)
1037 ir_algebraic_visitor
v(native_integers
, options
);
1039 visit_list_elements(&v
, instructions
);