public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] MATCH: [PR111679] Add alternative simplification of `a | ((~a) ^ b)`
@ 2023-10-09 21:27 Andrew Pinski
  2023-10-10  9:32 ` Richard Biener
  0 siblings, 1 reply; 2+ messages in thread
From: Andrew Pinski @ 2023-10-09 21:27 UTC (permalink / raw)
  To: gcc-patches; +Cc: Andrew Pinski

So currently we have a simplification for `a | ~(a ^ b)` but
that does not match the case where we had originally `(~a) | (a ^ b)`
so we need to add a new pattern that matches that and uses bitwise_inverted_equal_p
that also catches comparisons too.

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

	PR tree-optimization/111679

gcc/ChangeLog:

	* match.pd (`a | ((~a) ^ b)`): New pattern.

gcc/testsuite/ChangeLog:

	* gcc.dg/tree-ssa/bitops-5.c: New test.
---
 gcc/match.pd                             |  8 +++++++
 gcc/testsuite/gcc.dg/tree-ssa/bitops-5.c | 27 ++++++++++++++++++++++++
 2 files changed, 35 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bitops-5.c

diff --git a/gcc/match.pd b/gcc/match.pd
index 31bfd8b6b68..49740d189a7 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -1350,6 +1350,14 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
   (bit_ior @0 (bit_xor @1 { build_one_cst (type); }))))
 
+/* a | ((~a) ^ b)  -->  a | (~b) (alt version of the above 2) */
+(simplify
+ (bit_ior:c @0 (bit_xor:cs @1 @2))
+ (with { bool wascmp; }
+ (if (bitwise_inverted_equal_p (@0, @1, wascmp)
+      && (!wascmp || element_precision (type) == 1))
+  (bit_ior @0 (bit_not @2)))))
+
 /* (a | b) | (a &^ b)  -->  a | b  */
 (for op (bit_and bit_xor)
  (simplify
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bitops-5.c b/gcc/testsuite/gcc.dg/tree-ssa/bitops-5.c
new file mode 100644
index 00000000000..990610e3002
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/bitops-5.c
@@ -0,0 +1,27 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized-raw" } */
+/* PR tree-optimization/111679 */
+
+int f1(int a, int b)
+{
+        return (~a) | (a ^ b); // ~(a & b) or (~a) | (~b)
+}
+
+_Bool fb(_Bool c, _Bool d)
+{
+        return (!c) | (c ^ d); // ~(c & d) or (~c) | (~d)
+}
+
+_Bool fb1(int x, int y)
+{
+        _Bool a = x == 10,  b = y > 100;
+        return (!a) | (a ^ b); // ~(a & b) or (~a) | (~b)
+        // or (x != 10) | (y <= 100)
+}
+
+/* { dg-final { scan-tree-dump-not   "bit_xor_expr, "   "optimized" } } */
+/* { dg-final { scan-tree-dump-times "bit_not_expr, " 2 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "bit_and_expr, " 2 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "bit_ior_expr, " 1 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "ne_expr, _\[0-9\]+, x_\[0-9\]+"      1 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "le_expr, _\[0-9\]+, y_\[0-9\]+"      1 "optimized" } } */
-- 
2.39.3


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

* Re: [PATCH] MATCH: [PR111679] Add alternative simplification of `a | ((~a) ^ b)`
  2023-10-09 21:27 [PATCH] MATCH: [PR111679] Add alternative simplification of `a | ((~a) ^ b)` Andrew Pinski
@ 2023-10-10  9:32 ` Richard Biener
  0 siblings, 0 replies; 2+ messages in thread
From: Richard Biener @ 2023-10-10  9:32 UTC (permalink / raw)
  To: Andrew Pinski; +Cc: gcc-patches

On Mon, Oct 9, 2023 at 11:28 PM Andrew Pinski <pinskia@gmail.com> wrote:
>
> So currently we have a simplification for `a | ~(a ^ b)` but
> that does not match the case where we had originally `(~a) | (a ^ b)`
> so we need to add a new pattern that matches that and uses bitwise_inverted_equal_p
> that also catches comparisons too.
>
> OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.

OK.

>         PR tree-optimization/111679
>
> gcc/ChangeLog:
>
>         * match.pd (`a | ((~a) ^ b)`): New pattern.
>
> gcc/testsuite/ChangeLog:
>
>         * gcc.dg/tree-ssa/bitops-5.c: New test.
> ---
>  gcc/match.pd                             |  8 +++++++
>  gcc/testsuite/gcc.dg/tree-ssa/bitops-5.c | 27 ++++++++++++++++++++++++
>  2 files changed, 35 insertions(+)
>  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bitops-5.c
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 31bfd8b6b68..49740d189a7 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -1350,6 +1350,14 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>        && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
>    (bit_ior @0 (bit_xor @1 { build_one_cst (type); }))))
>
> +/* a | ((~a) ^ b)  -->  a | (~b) (alt version of the above 2) */
> +(simplify
> + (bit_ior:c @0 (bit_xor:cs @1 @2))
> + (with { bool wascmp; }
> + (if (bitwise_inverted_equal_p (@0, @1, wascmp)
> +      && (!wascmp || element_precision (type) == 1))
> +  (bit_ior @0 (bit_not @2)))))
> +
>  /* (a | b) | (a &^ b)  -->  a | b  */
>  (for op (bit_and bit_xor)
>   (simplify
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bitops-5.c b/gcc/testsuite/gcc.dg/tree-ssa/bitops-5.c
> new file mode 100644
> index 00000000000..990610e3002
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/bitops-5.c
> @@ -0,0 +1,27 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -fdump-tree-optimized-raw" } */
> +/* PR tree-optimization/111679 */
> +
> +int f1(int a, int b)
> +{
> +        return (~a) | (a ^ b); // ~(a & b) or (~a) | (~b)
> +}
> +
> +_Bool fb(_Bool c, _Bool d)
> +{
> +        return (!c) | (c ^ d); // ~(c & d) or (~c) | (~d)
> +}
> +
> +_Bool fb1(int x, int y)
> +{
> +        _Bool a = x == 10,  b = y > 100;
> +        return (!a) | (a ^ b); // ~(a & b) or (~a) | (~b)
> +        // or (x != 10) | (y <= 100)
> +}
> +
> +/* { dg-final { scan-tree-dump-not   "bit_xor_expr, "   "optimized" } } */
> +/* { dg-final { scan-tree-dump-times "bit_not_expr, " 2 "optimized" } } */
> +/* { dg-final { scan-tree-dump-times "bit_and_expr, " 2 "optimized" } } */
> +/* { dg-final { scan-tree-dump-times "bit_ior_expr, " 1 "optimized" } } */
> +/* { dg-final { scan-tree-dump-times "ne_expr, _\[0-9\]+, x_\[0-9\]+"      1 "optimized" } } */
> +/* { dg-final { scan-tree-dump-times "le_expr, _\[0-9\]+, y_\[0-9\]+"      1 "optimized" } } */
> --
> 2.39.3
>

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

end of thread, other threads:[~2023-10-10  9:35 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-09 21:27 [PATCH] MATCH: [PR111679] Add alternative simplification of `a | ((~a) ^ b)` Andrew Pinski
2023-10-10  9:32 ` Richard Biener

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