public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] MATCH: Add `~MAX(~X, Y)` pattern: [PR96694]
@ 2023-09-04  0:21 Andrew Pinski
  2023-09-05  7:15 ` Jeff Law
  0 siblings, 1 reply; 2+ messages in thread
From: Andrew Pinski @ 2023-09-04  0:21 UTC (permalink / raw)
  To: gcc-patches; +Cc: Andrew Pinski

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

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" } } */
-- 
2.31.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] MATCH: Add `~MAX(~X, Y)` pattern: [PR96694]
  2023-09-04  0:21 [PATCH] MATCH: Add `~MAX(~X, Y)` pattern: [PR96694] Andrew Pinski
@ 2023-09-05  7:15 ` Jeff Law
  0 siblings, 0 replies; 2+ messages in thread
From: Jeff Law @ 2023-09-05  7:15 UTC (permalink / raw)
  To: Andrew Pinski, gcc-patches



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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-09-05  7:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-04  0:21 [PATCH] MATCH: Add `~MAX(~X, Y)` pattern: [PR96694] Andrew Pinski
2023-09-05  7:15 ` Jeff Law

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).