public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Richard Biener <richard.guenther@gmail.com>
To: Andrew Pinski <apinski@marvell.com>
Cc: gcc-patches@gcc.gnu.org
Subject: Re: [PATCH] PR 95923: More (boolean) bitop simplifications in match.pd
Date: Mon, 17 Jul 2023 08:39:36 +0200	[thread overview]
Message-ID: <CAFiYyc0VKOOmmEqmWW5x1dv-u+Op8P02hFA0jnsFYGcW3M66FQ@mail.gmail.com> (raw)
In-Reply-To: <20230717024247.1263484-1-apinski@marvell.com>

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
>

      reply	other threads:[~2023-07-17  6:40 UTC|newest]

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

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=CAFiYyc0VKOOmmEqmWW5x1dv-u+Op8P02hFA0jnsFYGcW3M66FQ@mail.gmail.com \
    --to=richard.guenther@gmail.com \
    --cc=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).