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

Message ID 20230904002135.928111-1-apinski@marvell.com
State Accepted
Headers
Series MATCH: Add `~MAX(~X, Y)` pattern: [PR96694] |

Checks

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

Commit Message

Andrew Pinski Sept. 4, 2023, 12:21 a.m. UTC
  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.
---
 gcc/match.pd                              |  7 ++++-
 gcc/testsuite/gcc.dg/tree-ssa/minmax-24.c | 31 +++++++++++++++++++++++
 2 files changed, 37 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/minmax-24.c
  

Comments

Jeff Law Sept. 5, 2023, 7:15 a.m. UTC | #1
On 9/3/23 18:21, Andrew Pinski via Gcc-patches wrote:
> 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.
OK.
jeff
  

Patch

diff --git a/gcc/match.pd b/gcc/match.pd
index e9ce48ea7fa..604c2c2360c 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -3786,7 +3786,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)
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/minmax-24.c b/gcc/testsuite/gcc.dg/tree-ssa/minmax-24.c
new file mode 100644
index 00000000000..2b21f94eecf
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/minmax-24.c
@@ -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" } } */