]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
MATCH: Add Max<Max<a,b>,a> -> Max<a,b> simplifcation
authorAndrew Pinski <apinski@marvell.com>
Fri, 21 Jul 2023 00:36:29 +0000 (17:36 -0700)
committerAndrew Pinski <apinski@marvell.com>
Fri, 21 Jul 2023 07:33:39 +0000 (00:33 -0700)
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.

gcc/match.pd
gcc/testsuite/gcc.dg/tree-ssa/minmax-23.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/reassoc-12.c

index 4dfe92623f7d2592e49f3074fbc3960e88387abb..bfd15d6cd4a00a147158e23a5e470d0052704997 100644 (file)
@@ -3503,7 +3503,11 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 (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
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/minmax-23.c b/gcc/testsuite/gcc.dg/tree-ssa/minmax-23.c
new file mode 100644 (file)
index 0000000..0b7e51b
--- /dev/null
@@ -0,0 +1,22 @@
+/* { 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"} } */
index 9a138ebcf70909493e59a30774f70837ab0820ff..2238147de19ca5da28facd87e931db550b7fd551 100644 (file)
@@ -1,5 +1,6 @@
 /* { 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.  */