From: Andrew Pinski Date: Fri, 21 Jul 2023 00:36:29 +0000 (-0700) Subject: MATCH: Add Max,a> -> Max simplifcation X-Git-Tag: basepoints/gcc-15~7441 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6d449531a60b56ed0f4aeb640aa9e46e4ec35208;p=thirdparty%2Fgcc.git MATCH: Add Max,a> -> Max simplifcation This adds a simple match pattern to simplify `max,a>` to `max`. 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,a>->minmax): 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. --- diff --git a/gcc/match.pd b/gcc/match.pd index 4dfe92623f7d..bfd15d6cd4a0 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 000000000000..0b7e51bb97ee --- /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 9a138ebcf709..2238147de19c 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,a>->MAX, 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 should cause it to be equivalent to a. */