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] PR 95923: More (boolean) bitop simplifications in match.pd
Date: Sun, 16 Jul 2023 19:42:47 -0700	[thread overview]
Message-ID: <20230717024247.1263484-1-apinski@marvell.com> (raw)

This adds the boolean version of some of the simplifications
that were added with r8-4395-ge268a77b59cb78.

That are the following:
(a | b) & (a == b) --> a & b
a | (a == b)       --> a | (b ^ 1)
(a & b) | (a == b) --> a == b

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

gcc/ChangeLog:

	PR tree-optimization/95923
	* match.pd ((a|b)&(a==b),a|(a==b),(a&b)|(a==b)): New transformation.

gcc/testsuite/ChangeLog:

	PR tree-optimization/95923
	* gcc.dg/tree-ssa/bitops-2.c: New test.
	* gcc.dg/tree-ssa/bool-checks-1.c: New test.
---
 gcc/match.pd                                  | 15 +++++++
 gcc/testsuite/gcc.dg/tree-ssa/bitops-2.c      | 41 +++++++++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/bool-checks-1.c | 22 ++++++++++
 3 files changed, 78 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bitops-2.c
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bool-checks-1.c

diff --git a/gcc/match.pd b/gcc/match.pd
index 351d9285e92..5e812d804d0 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -1226,11 +1226,21 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
  (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
  (bit_and @0 @1))
 
+/* (a | b) & (a == b)  -->  a & b (boolean version of the above).  */
+(simplify
+ (bit_and:c (bit_ior @0 @1) (nop_convert? (eq:c @0 @1)))
+ (bit_and @0 @1))
+
 /* a | ~(a ^ b)  -->  a | ~b  */
 (simplify
  (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
  (bit_ior @0 (bit_not @1)))
 
+/* a | (a == b)  -->  a | (b^1) (boolean version of the above). */
+(simplify
+ (bit_ior:c @0 (nop_convert? (eq:c @0 @1)))
+ (bit_ior @0 (bit_xor @1 { build_one_cst (type); })))
+
 /* (a | b) | (a &^ b)  -->  a | b  */
 (for op (bit_and bit_xor)
  (simplify
@@ -1242,6 +1252,11 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
  (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
  @2)
 
+/* (a & b) | (a == b)  -->  a == b  */
+(simplify
+ (bit_ior:c (bit_and:c @0 @1) (nop_convert?@2 (eq @0 @1)))
+ @2)
+
 /* ~(~a & b)  -->  a | ~b  */
 (simplify
  (bit_not (bit_and:cs (bit_not @0) @1))
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bitops-2.c b/gcc/testsuite/gcc.dg/tree-ssa/bitops-2.c
new file mode 100644
index 00000000000..d8128f3ca53
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/bitops-2.c
@@ -0,0 +1,41 @@
+/* { dg-do run } */
+/* { dg-options "-O -fdump-tree-optimized-raw" } */
+
+#define DECLS(n,VOL)			\
+__attribute__((noinline,noclone))	\
+_Bool h##n(_Bool A,_Bool B){			\
+    VOL _Bool C = A | B;			\
+    VOL _Bool D = A == B;			\
+    return C & D;			\
+}					\
+__attribute__((noinline,noclone))	\
+_Bool i##n(_Bool A,_Bool B){			\
+    VOL _Bool C = A == B;			\
+    return A | C;			\
+}					\
+__attribute__((noinline,noclone))	\
+_Bool k##n(_Bool A,_Bool B){			\
+    VOL _Bool C = A & B;			\
+    VOL _Bool D = A == B;			\
+    return C | D;			\
+}					\
+
+DECLS(0,)
+DECLS(1,volatile)
+
+int main(){
+    for(int A = 0; A <= 1; ++A)
+      for(int B = 0; B <= 1; ++B)
+	{
+	  if (h0 (A, B) != h1 (A, B)) __builtin_abort();
+	  if (i0 (A, B) != i1 (A, B)) __builtin_abort();
+	  if (k0 (A, B) != k1 (A, B)) __builtin_abort();
+	}
+}
+
+/* { dg-final { scan-tree-dump-times "bit_not_expr," 1 "optimized"} } */
+/* { dg-final { scan-tree-dump-times "bit_and_expr," 3 "optimized"} } */
+/* { dg-final { scan-tree-dump-times "bit_ior_expr," 4 "optimized"} } */
+/* { dg-final { scan-tree-dump-times "eq_expr,"      4 "optimized"} } */
+/* { dg-final { scan-tree-dump-times "ne_expr,"      7 "optimized"} } */
+/* { dg-final { scan-tree-dump-not   "bit_xor_expr,"   "optimized"} } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bool-checks-1.c b/gcc/testsuite/gcc.dg/tree-ssa/bool-checks-1.c
new file mode 100644
index 00000000000..370a23aad3e
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/bool-checks-1.c
@@ -0,0 +1,22 @@
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-optimized-raw" } */
+/* PR tree-optimization/95923 */
+
+_Bool f(_Bool a, _Bool b)
+{
+    if (!a && !b)
+        return 0;
+    if (!a && b)
+        return 0;
+    if (a && !b)
+        return 0;
+    return 1;
+}
+
+/* { dg-final { scan-tree-dump-times "bit_and_expr," 1 "optimized"} } */
+/* { dg-final { scan-tree-dump-not   "bit_not_expr,"   "optimized"} } */
+/* { dg-final { scan-tree-dump-not   "bit_ior_expr,"   "optimized"} } */
+/* { dg-final { scan-tree-dump-not   "bit_xor_expr,"   "optimized"} } */
+/* { dg-final { scan-tree-dump-not   "eq_expr,"    "optimized"} } */
+/* { dg-final { scan-tree-dump-not   "ne_expr,"    "optimized"} } */
+/* { dg-final { scan-tree-dump-not   "gimple_cond"    "optimized"} } */
-- 
2.31.1


             reply	other threads:[~2023-07-17  2:43 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-17  2:42 Andrew Pinski [this message]
2023-07-17  6:39 ` Richard Biener

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=20230717024247.1263484-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).