From: Andrew Pinski <apinski@marvell.com>
To: <gcc-patches@gcc.gnu.org>
Cc: Andrew Pinski <apinski@marvell.com>
Subject: [PATCH] MATCH: Add `(x | c) & ~(y | c)` and `x & ~(y | x)` patterns [PR98710]
Date: Sun, 3 Sep 2023 18:25:36 -0700 [thread overview]
Message-ID: <20230904012536.930677-1-apinski@marvell.com> (raw)
Adding some more simple bit_and/bit_ior patterns.
How often these show up, I have no idea.
This was tested on top of
https://gcc.gnu.org/pipermail/gcc-patches/2023-September/629174.html .
OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.
gcc/ChangeLog:
PR tree-optimization/98710
* match.pd (`(x | c) & ~(y | c)`, `(x & c) | ~(y & c)`): New pattern.
(`x & ~(y | x)`, `x | ~(y & x)`): New patterns.
gcc/testsuite/ChangeLog:
PR tree-optimization/98710
* gcc.dg/tree-ssa/andor-7.c: New test.
* gcc.dg/tree-ssa/andor-8.c: New test.
---
gcc/match.pd | 14 +++++++++++++-
gcc/testsuite/gcc.dg/tree-ssa/andor-7.c | 16 ++++++++++++++++
gcc/testsuite/gcc.dg/tree-ssa/andor-8.c | 19 +++++++++++++++++++
3 files changed, 48 insertions(+), 1 deletion(-)
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/andor-7.c
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/andor-8.c
diff --git a/gcc/match.pd b/gcc/match.pd
index 3495f9451d1..a3f507a1e2e 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -1995,7 +1995,19 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
/* (x & y) | (x | z) -> (x | z) */
(simplify
(bitop:c (rbitop:c @0 @1) (bitop:c@3 @0 @2))
- @3))
+ @3)
+ /* (x | c) & ~(y | c) -> x & ~(y | c) */
+ /* (x & c) | ~(y & c) -> x | ~(y & c) */
+ (simplify
+ (bitop:c (rbitop:c @0 @1) (bit_not@3 (rbitop:c @1 @2)))
+ (bitop @0 @3))
+ /* x & ~(y | x) -> 0 */
+ /* x | ~(y & x) -> -1 */
+ (simplify
+ (bitop:c @0 (bit_not (rbitop:c @0 @1)))
+ (if (bitop == BIT_AND_EXPR)
+ { build_zero_cst (type); }
+ { build_minus_one_cst (type); })))
/* ((x | y) & z) | x -> (z & y) | x
((x ^ y) & z) | x -> (z & y) | x */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/andor-7.c b/gcc/testsuite/gcc.dg/tree-ssa/andor-7.c
new file mode 100644
index 00000000000..63b70fa7888
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/andor-7.c
@@ -0,0 +1,16 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-original" } */
+/* PR tree-optimization/98710 */
+
+signed foo(signed x, signed y, signed z)
+{
+ return (x | z) & ~(y | z); // x & ~(y | z);
+}
+// Note . here is `(` or `)`
+/* { dg-final { scan-tree-dump "return x \& ~.y \\| z.;|return ~.y \\| z. \& x;" "original" } } */
+
+signed foo_or(signed a, signed b, signed c)
+{
+ return (a & c) | ~(b & c); // a | ~(b & c);
+}
+/* { dg-final { scan-tree-dump "return a \\| ~.b \& c.;|return ~.b \& c. \\| a;" "original" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/andor-8.c b/gcc/testsuite/gcc.dg/tree-ssa/andor-8.c
new file mode 100644
index 00000000000..0c2eb4c1a00
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/andor-8.c
@@ -0,0 +1,19 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-original" } */
+/* PR tree-optimization/98710 */
+
+signed foo2(signed a, signed b, signed c)
+{
+ return (a & ~(b | a)) & c; // 0
+}
+/* { dg-final { scan-tree-dump "return 0;" "original" } } */
+signed foo2_or(signed x, signed y, signed z)
+{
+ return (x | ~(y & x)) & z; // -1 & z -> z
+}
+
+/* { dg-final { scan-tree-dump "return z;" "original" } } */
+/* All | and & should have been removed. */
+/* { dg-final { scan-tree-dump-not "~" "original" } } */
+/* { dg-final { scan-tree-dump-not " \& " "original" } } */
+/* { dg-final { scan-tree-dump-not " \\| " "original" } } */
--
2.31.1
next reply other threads:[~2023-09-04 1:25 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-09-04 1:25 Andrew Pinski [this message]
2023-09-05 7:24 ` Jeff Law
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230904012536.930677-1-apinski@marvell.com \
--to=apinski@marvell.com \
--cc=gcc-patches@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).