public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Andrew Pinski <apinski@marvell.com>
To: <gcc-patches@gcc.gnu.org>
Cc: Andrew Pinski <apinski@marvell.com>
Subject: [PATCH] MATCH: Add pattern for `(x | y) & (x & z)`
Date: Sun, 3 Sep 2023 13:49:47 -0700	[thread overview]
Message-ID: <20230903204947.918766-1-apinski@marvell.com> (raw)

Like the pattern already there for `(x | y) & x`,
this adds a simple pattern to optimize `(x | y) & (x & z)`
to just `x & z`.

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

gcc/ChangeLog:

	PR tree-optimization/103536
	* match.pd (`(x | y) & (x & z)`,
	`(x & y) | (x | z)`): New patterns.

gcc/testsuite/ChangeLog:

	PR tree-optimization/103536
	* gcc.dg/tree-ssa/andor-6.c: New test.
	* gcc.dg/tree-ssa/andor-bool-1.c: New test.
---
 gcc/match.pd                                 |  7 ++++++-
 gcc/testsuite/gcc.dg/tree-ssa/andor-6.c      | 19 +++++++++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/andor-bool-1.c | 13 +++++++++++++
 3 files changed, 38 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/andor-6.c
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/andor-bool-1.c

diff --git a/gcc/match.pd b/gcc/match.pd
index 3efc971f7f6..3495f9451d1 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -1990,7 +1990,12 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
   (with { bool wascmp; }
    (if (bitwise_inverted_equal_p (@0, @2, wascmp)
 	&& (!wascmp || element_precision (type) == 1))
-    (bitop @0 @1)))))
+    (bitop @0 @1))))
+  /* (x | y) & (x & z) -> (x & z) */
+  /* (x & y) | (x | z) -> (x | z) */
+ (simplify
+  (bitop:c (rbitop:c @0 @1) (bitop:c@3 @0 @2))
+  @3))
 
 /* ((x | y) & z) | x -> (z & y) | x
    ((x ^ y) & z) | x -> (z & y) | x  */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/andor-6.c b/gcc/testsuite/gcc.dg/tree-ssa/andor-6.c
new file mode 100644
index 00000000000..32e11730f98
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/andor-6.c
@@ -0,0 +1,19 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-original" } */
+/* PR tree-optimization/103536 */
+
+int
+orand(int a, int b, int c)
+{
+    return (a | b) & (a & c); // a & c
+}
+
+/* { dg-final { scan-tree-dump "return a \& c;" "original" } } */
+
+int
+andor(int d, int e, int f)
+{
+    return (d & e) | (d | f); // d | f
+}
+
+/* { dg-final { scan-tree-dump "return d \\| f;" "original" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/andor-bool-1.c b/gcc/testsuite/gcc.dg/tree-ssa/andor-bool-1.c
new file mode 100644
index 00000000000..a1b974f3859
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/andor-bool-1.c
@@ -0,0 +1,13 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* PR tree-optimization/103536 */
+
+_Bool
+src_1 (_Bool a, _Bool b)
+{
+    return (a || b) && (a && b);
+}
+
+/* { dg-final { scan-tree-dump "a_\[0-9\]+.D. \& b_\[0-9\]+.D." "optimized" } } */
+/* { dg-final { scan-tree-dump-not "a_\[0-9\]+.D. \\\| b_\[0-9\]+.D." "optimized" } } */
+/* { dg-final { scan-tree-dump-not "if " "optimized" } } */
-- 
2.31.1


             reply	other threads:[~2023-09-03 20:49 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-03 20:49 Andrew Pinski [this message]
2023-09-05  7:21 ` 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=20230903204947.918766-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).