MATCH: Add Max<Max<a,b>,a> -> Max<a,b> simplifcation

Message ID 20230721040534.1511819-1-apinski@marvell.com
State Accepted
Headers
Series MATCH: Add Max<Max<a,b>,a> -> Max<a,b> simplifcation |

Checks

Context Check Description
snail/gcc-patch-check success Github commit url

Commit Message

Andrew Pinski July 21, 2023, 4:05 a.m. UTC
  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                               |  6 +++++-
 gcc/testsuite/gcc.dg/tree-ssa/minmax-23.c  | 22 ++++++++++++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/reassoc-12.c |  3 ++-
 3 files changed, 29 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/minmax-23.c
  

Comments

Richard Biener July 21, 2023, 6:30 a.m. UTC | #1
On Fri, Jul 21, 2023 at 6:06 AM Andrew Pinski via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> 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.

maybe turn it into a GIMPLE testcase instead ...

In any case OK.

Thanks,
Richard.

>         * gcc.dg/tree-ssa/minmax-23.c: New test.
> ---
>  gcc/match.pd                               |  6 +++++-
>  gcc/testsuite/gcc.dg/tree-ssa/minmax-23.c  | 22 ++++++++++++++++++++++
>  gcc/testsuite/gcc.dg/tree-ssa/reassoc-12.c |  3 ++-
>  3 files changed, 29 insertions(+), 2 deletions(-)
>  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/minmax-23.c
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 4dfe92623f7..bfd15d6cd4a 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -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
> index 00000000000..0b7e51bb97e
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/minmax-23.c
> @@ -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"} } */
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/reassoc-12.c b/gcc/testsuite/gcc.dg/tree-ssa/reassoc-12.c
> index 9a138ebcf70..2238147de19 100644
> --- a/gcc/testsuite/gcc.dg/tree-ssa/reassoc-12.c
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/reassoc-12.c
> @@ -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.  */
> --
> 2.31.1
>
  

Patch

diff --git a/gcc/match.pd b/gcc/match.pd
index 4dfe92623f7..bfd15d6cd4a 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -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
index 00000000000..0b7e51bb97e
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/minmax-23.c
@@ -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"} } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/reassoc-12.c b/gcc/testsuite/gcc.dg/tree-ssa/reassoc-12.c
index 9a138ebcf70..2238147de19 100644
--- a/gcc/testsuite/gcc.dg/tree-ssa/reassoc-12.c
+++ b/gcc/testsuite/gcc.dg/tree-ssa/reassoc-12.c
@@ -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.  */