MATCH: Add ~MAX(~X, Y) pattern: [PR96694]

This adds `~MAX(~X, Y)` and `~MIN(~X, Y)` patterns
that are like the `~(~a & b)` and `~(~a | b)` patterns
and allows to reduce the number of ~ by 1.

OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.

	PR tree-optimization/96694

gcc/ChangeLog:

	* match.pd (`~MAX(~X, Y)`, `~MIN(~X, Y)`): New patterns.

gcc/testsuite/ChangeLog:

	* gcc.dg/tree-ssa/minmax-24.c: New test.
This commit is contained in:
Andrew Pinski 2023-09-03 18:37:51 +00:00
parent b34f373635
commit 244d132134
2 changed files with 37 additions and 1 deletions

View file

@ -3864,7 +3864,12 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
maxmin (max min)
(simplify
(minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
(bit_not (maxmin @0 @1))))
(bit_not (maxmin @0 @1)))
/* ~MAX(~X, Y) --> MIN(X, ~Y) */
/* ~MIN(~X, Y) --> MAX(X, ~Y) */
(simplify
(bit_not (minmax:cs (bit_not @0) @1))
(maxmin @0 (bit_not @1))))
/* MIN (X, Y) == X -> X <= Y */
(for minmax (min min max max)

View file

@ -0,0 +1,31 @@
/* { dg-do compile } */
/* { dg-options "-O2 -fdump-tree-optimized" } */
/* PR tree-optimization/96694 */
static inline int min(int a, int b)
{
return a < b ? a : b;
}
static inline int max(int a, int b)
{
return a > b ? a : b;
}
int max_not(int x, int y)
{
return ~max(~x, y); // min (x, ~y)
}
/* { dg-final { scan-tree-dump "~y_\[0-9\]+.D.;" "optimized" } } */
/* { dg-final { scan-tree-dump-not "~x_\[0-9\]+.D.;" "optimized" } } */
/* { dg-final { scan-tree-dump "MIN_EXPR <x_\[0-9\]+.D., _\[0-9\]+>|MIN_EXPR <_\[0-9\]+, x_\[0-9\]+.D.>" "optimized" } } */
int min_not(int c, int d)
{
return ~min(~c, d); // max (c, ~d)
}
/* { dg-final { scan-tree-dump "~d_\[0-9\]+.D.;" "optimized" } } */
/* { dg-final { scan-tree-dump-not "~c_\[0-9\]+.D.;" "optimized" } } */
/* { dg-final { scan-tree-dump "MAX_EXPR <c_\[0-9\]+.D., _\[0-9\]+>|MIN_EXPR <_\[0-9\]+, c_\[0-9\]+.D.>" "optimized" } } */
/* { dg-final { scan-tree-dump-times "~" 2 "optimized" } } */