On 9/25/24 2:30 AM, Eikansh Gupta wrote:
This patch simplify `min(a,b) op max(a,b)` to `a op b`. This optimization
will work for all the binary commutative operations. So, the `op` here can
be one of {plus, mult, bit_and, bit_xor, bit_ior, eq, ne, min, max}.
PR tree-optimization/109878
PR 109401
gcc/ChangeLog:
* match.pd (min(a,b) op max(a,b) -> a op b): New pattern.
gcc/testsuite/ChangeLog:
* gcc.dg/tree-ssa/pr109401.c: New test.
* gcc.dg/tree-ssa/pr109401-1.c: New test.
---
gcc/match.pd | 7 +++
gcc/testsuite/gcc.dg/tree-ssa/pr109401-1.c | 35 +++++++++++++
gcc/testsuite/gcc.dg/tree-ssa/pr109401.c | 61 ++++++++++++++++++++++
3 files changed, 103 insertions(+)
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr109401-1.c
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr109401.c
diff --git a/gcc/match.pd b/gcc/match.pd
index 940292d0d49..4f0453344d6 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -4463,6 +4463,13 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
@0
@2)))
+/* min (a, b) op max (a, b) -> a op b */
+(for op (plus mult bit_and bit_xor bit_ior eq ne min max)
+ (simplify
+ (op:c (min:c @0 @1) (max:c @0 @1))
+ (if (!HONOR_NANS (@0))
+ (op @0 @1))))
+
Does it work in the other order ie max (a, b) op (min (a, b) -> a op b?
What about other OP cases such as subtraction, div/mod?
Jeff