This adds a simple match pattern to simplify
`max<max<a,b>,a>` to `max<a,b>`. Reassociation handles
this already (
r0-77700-ge969dbde29bfd396259357) but
seems like we should be able to handle this even before
reassociation.
This fixes part of PR tree-optimization/80574 but more
work is needed fix it the rest of the way. The original
testcase there is fixed but the RTL level is what fixes
it the rest of the way.
OK? Bootstrapped and tested on x86_64-linux-gnu.
gcc/ChangeLog:
* match.pd (minmax<minmax<a,b>,a>->minmax<a,b>): New
transformation.
gcc/testsuite/ChangeLog:
* gcc.dg/tree-ssa/reassoc-12.c: Disable all of
the passes that enables match-and-simplify.
* gcc.dg/tree-ssa/minmax-23.c: New test.
(for minmax (min max)
(simplify
(minmax @0 @0)
- @0))
+ @0)
+/* max(max(x,y),x) -> max(x,y) */
+ (simplify
+ (minmax:c (minmax:c@2 @0 @1) @0)
+ @2))
/* For fmin() and fmax(), skip folding when both are sNaN. */
(for minmax (FMIN_ALL FMAX_ALL)
(simplify
--- /dev/null
+/* { dg-do compile } */
+/* { dg-options "-O1 -fno-tree-reassoc -fdump-tree-optimized" } */
+
+
+#define MAX(a,b) (a)>=(b) ? (a) : (b)
+
+#define MIN(a,b) (a)<=(b) ? (a) : (b)
+
+int test1(int a, int b)
+{
+ int d = MAX(a,b);
+ return MAX(a,d);
+}
+int test2(int a, int b)
+{
+ int d = MIN(a,b);
+ return MIN(a,d);
+}
+
+/* We should be optimize these two functions even without reassociation. */
+/* { dg-final { scan-tree-dump-times "MAX_EXPR " 1 "optimized"} } */
+/* { dg-final { scan-tree-dump-times "MIN_EXPR " 1 "optimized"} } */
/* { dg-do compile } */
-/* { dg-options "-O2 -fdump-tree-reassoc1-details" } */
+/* Match-and-simplify can handle now MAX<MAX<a,b>,a>->MAX<a,b>, disable all of the passes that uses that. */
+/* { dg-options "-O1 -fdump-tree-reassoc1-details -fno-tree-ccp -fno-tree-ccp -fno-tree-forwprop -fno-tree-fre" } */
int f(int a, int b)
{
/* MAX_EXPR <a, a> should cause it to be equivalent to a. */