diff mbox series

PR 95923: More (boolean) bitop simplifications in match.pd

Message ID 20230717024247.1263484-1-apinski@marvell.com
State New
Headers show
Series PR 95923: More (boolean) bitop simplifications in match.pd | expand

Commit Message

Andrew Pinski July 17, 2023, 2:42 a.m. UTC
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

Comments

Richard Biener July 17, 2023, 6:39 a.m. UTC | #1
On Mon, Jul 17, 2023 at 4:43 AM Andrew Pinski via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> 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.

OK.

Richard.

> 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
>
diff mbox series

Patch

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