public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] MATCH: [PR111432] Simplify `a & (x | CST)` to a when we know that (a & ~CST) == 0
@ 2023-10-14  0:57 Andrew Pinski
  2023-10-17  9:24 ` Richard Biener
  0 siblings, 1 reply; 2+ messages in thread
From: Andrew Pinski @ 2023-10-14  0:57 UTC (permalink / raw)
  To: gcc-patches; +Cc: Andrew Pinski

This adds the simplification `a & (x | CST)` to a when we know that
`(a & ~CST) == 0`. In a similar fashion as `a & CST` is handle.

I looked into handling `a | (x & CST)` but that I don't see any decent
simplifications happening.

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

	PR tree-optimization/111432

gcc/ChangeLog:

	* match.pd (`a & (x | CST)`): New pattern.

gcc/testsuite/ChangeLog:

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

diff --git a/gcc/match.pd b/gcc/match.pd
index 51e5065d086..45624f3dcb4 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -1550,6 +1550,14 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
   @0))
+
+/* `a & (x | CST)` -> a if we know that (a & ~CST) == 0   */
+(simplify
+ (bit_and:c SSA_NAME@0 (bit_ior @1 INTEGER_CST@2))
+ (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
+      && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@2)) == 0)
+  @0))
+
 /* x | C -> C if we know that x & ~C == 0.  */
 (simplify
  (bit_ior SSA_NAME@0 INTEGER_CST@1)
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bitops-7.c b/gcc/testsuite/gcc.dg/tree-ssa/bitops-7.c
new file mode 100644
index 00000000000..7fb18db3a11
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/bitops-7.c
@@ -0,0 +1,24 @@
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-optimized-raw" } */
+/* PR tree-optimization/111432 */
+
+int
+foo3(int c, int bb)
+{
+  if ((bb & ~3)!=0) __builtin_unreachable();
+  return (bb & (c|3));
+}
+
+int
+foo_bool(int c, _Bool bb)
+{
+  return (bb & (c|7));
+}
+
+/* Both of these functions should be able to remove the `IOR` and `AND`
+   as the only bits that are non-zero for bb is set on the other side
+   of the `AND`.
+ */
+
+/* { dg-final { scan-tree-dump-not   "bit_ior_expr, "   "optimized" } } */
+/* { dg-final { scan-tree-dump-not   "bit_and_expr, "   "optimized" } } */
-- 
2.39.3


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

* Re: [PATCH] MATCH: [PR111432] Simplify `a & (x | CST)` to a when we know that (a & ~CST) == 0
  2023-10-14  0:57 [PATCH] MATCH: [PR111432] Simplify `a & (x | CST)` to a when we know that (a & ~CST) == 0 Andrew Pinski
@ 2023-10-17  9:24 ` Richard Biener
  0 siblings, 0 replies; 2+ messages in thread
From: Richard Biener @ 2023-10-17  9:24 UTC (permalink / raw)
  To: Andrew Pinski; +Cc: gcc-patches

On Sat, Oct 14, 2023 at 2:57 AM Andrew Pinski <pinskia@gmail.com> wrote:
>
> This adds the simplification `a & (x | CST)` to a when we know that
> `(a & ~CST) == 0`. In a similar fashion as `a & CST` is handle.
>
> I looked into handling `a | (x & CST)` but that I don't see any decent
> simplifications happening.
>
> OK? Bootstrapped and tested on x86_linux-gnu with no regressions.

OK.

>         PR tree-optimization/111432
>
> gcc/ChangeLog:
>
>         * match.pd (`a & (x | CST)`): New pattern.
>
> gcc/testsuite/ChangeLog:
>
>         * gcc.dg/tree-ssa/bitops-7.c: New test.
> ---
>  gcc/match.pd                             |  8 ++++++++
>  gcc/testsuite/gcc.dg/tree-ssa/bitops-7.c | 24 ++++++++++++++++++++++++
>  2 files changed, 32 insertions(+)
>  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bitops-7.c
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 51e5065d086..45624f3dcb4 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -1550,6 +1550,14 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
>        && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
>    @0))
> +
> +/* `a & (x | CST)` -> a if we know that (a & ~CST) == 0   */
> +(simplify
> + (bit_and:c SSA_NAME@0 (bit_ior @1 INTEGER_CST@2))
> + (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
> +      && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@2)) == 0)
> +  @0))
> +
>  /* x | C -> C if we know that x & ~C == 0.  */
>  (simplify
>   (bit_ior SSA_NAME@0 INTEGER_CST@1)
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bitops-7.c b/gcc/testsuite/gcc.dg/tree-ssa/bitops-7.c
> new file mode 100644
> index 00000000000..7fb18db3a11
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/bitops-7.c
> @@ -0,0 +1,24 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O1 -fdump-tree-optimized-raw" } */
> +/* PR tree-optimization/111432 */
> +
> +int
> +foo3(int c, int bb)
> +{
> +  if ((bb & ~3)!=0) __builtin_unreachable();
> +  return (bb & (c|3));
> +}
> +
> +int
> +foo_bool(int c, _Bool bb)
> +{
> +  return (bb & (c|7));
> +}
> +
> +/* Both of these functions should be able to remove the `IOR` and `AND`
> +   as the only bits that are non-zero for bb is set on the other side
> +   of the `AND`.
> + */
> +
> +/* { dg-final { scan-tree-dump-not   "bit_ior_expr, "   "optimized" } } */
> +/* { dg-final { scan-tree-dump-not   "bit_and_expr, "   "optimized" } } */
> --
> 2.39.3
>

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

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

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-14  0:57 [PATCH] MATCH: [PR111432] Simplify `a & (x | CST)` to a when we know that (a & ~CST) == 0 Andrew Pinski
2023-10-17  9:24 ` 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).