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] MATCH: Add simplifications of `(a == CST) & a`
Date: Mon, 18 Sep 2023 09:37:02 +0200	[thread overview]
Message-ID: <CAFiYyc0fekQKywc4D6EJZWPV49AHJNumY==36KHmAZ2tHVUe8w@mail.gmail.com> (raw)
In-Reply-To: <20230916155924.1679335-1-apinski@marvell.com>

On Sat, Sep 16, 2023 at 6:00 PM Andrew Pinski via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> `(a == CST) & a` can be either simplified to simplying `a == CST`
> or 0 depending on the first bit of the CST.
> This is an extension of the already pattern of `X & !X` and allows
> us to remove the 2 xfails on gcc.dg/binop-notand1a.c and gcc.dg/binop-notand4a.c.
>
> OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.

OK.

>         PR tree-optimization/111431
>
> gcc/ChangeLog:
>
>         * match.pd (`(a == CST) & a`): New pattern.
>
> gcc/testsuite/ChangeLog:
>
>         * gcc.dg/binop-notand1a.c: Remove xfail.
>         * gcc.dg/binop-notand4a.c: Likewise.
>         * gcc.c-torture/execute/pr111431-1.c: New test.
>         * gcc.dg/binop-andeq1.c: New test.
>         * gcc.dg/binop-andeq2.c: New test.
>         * gcc.dg/binop-notand7.c: New test.
>         * gcc.dg/binop-notand7a.c: New test.
> ---
>  gcc/match.pd                                  |  8 ++++
>  .../gcc.c-torture/execute/pr111431-1.c        | 39 +++++++++++++++++++
>  gcc/testsuite/gcc.dg/binop-andeq1.c           | 12 ++++++
>  gcc/testsuite/gcc.dg/binop-andeq2.c           | 14 +++++++
>  gcc/testsuite/gcc.dg/binop-notand1a.c         |  4 +-
>  gcc/testsuite/gcc.dg/binop-notand4a.c         |  4 +-
>  gcc/testsuite/gcc.dg/binop-notand7.c          | 12 ++++++
>  gcc/testsuite/gcc.dg/binop-notand7a.c         | 12 ++++++
>  8 files changed, 99 insertions(+), 6 deletions(-)
>  create mode 100644 gcc/testsuite/gcc.c-torture/execute/pr111431-1.c
>  create mode 100644 gcc/testsuite/gcc.dg/binop-andeq1.c
>  create mode 100644 gcc/testsuite/gcc.dg/binop-andeq2.c
>  create mode 100644 gcc/testsuite/gcc.dg/binop-notand7.c
>  create mode 100644 gcc/testsuite/gcc.dg/binop-notand7a.c
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index ebb50ee0581..65960a1701e 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -5172,6 +5172,14 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>   )
>  )
>
> +/* `(a == CST) & a` can be simplified to `0` or `(a == CST)` depending
> +   on the first bit of the CST.  */
> +(simplify
> + (bit_and:c (convert@2 (eq @0 INTEGER_CST@1)) (convert? @0))
> + (if ((wi::to_wide (@1) & 1) != 0)
> +  @2
> +  { build_zero_cst (type); }))
> +
>  /* Optimize
>     # x_5 in range [cst1, cst2] where cst2 = cst1 + 1
>     x_5 ? cstN ? cst4 : cst3
> diff --git a/gcc/testsuite/gcc.c-torture/execute/pr111431-1.c b/gcc/testsuite/gcc.c-torture/execute/pr111431-1.c
> new file mode 100644
> index 00000000000..a96dbadf2b5
> --- /dev/null
> +++ b/gcc/testsuite/gcc.c-torture/execute/pr111431-1.c
> @@ -0,0 +1,39 @@
> +int
> +foo (int a)
> +{
> +  int b = a == 0;
> +  return (a & b);
> +}
> +
> +#define function(vol,cst) \
> +__attribute__((noipa)) \
> +_Bool func_##cst##_##vol(vol int a) \
> +{ \
> +  vol int b = a == cst; \
> +  return (a & b); \
> +}
> +
> +#define funcdefs(cst) \
> +function(,cst) \
> +function(volatile,cst)
> +
> +#define funcs(f) \
> +f(0) \
> +f(1) \
> +f(5)
> +
> +funcs(funcdefs)
> +
> +#define test(cst) \
> +do { \
> + if(func_##cst##_(a) != func_##cst##_volatile(a))\
> +   __builtin_abort(); \
> +} while(0);
> +int main(void)
> +{
> +  for(int a = -10; a <= 10; a++)
> +   {
> +     funcs(test)
> +   }
> +}
> +
> diff --git a/gcc/testsuite/gcc.dg/binop-andeq1.c b/gcc/testsuite/gcc.dg/binop-andeq1.c
> new file mode 100644
> index 00000000000..2a92b8f95df
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/binop-andeq1.c
> @@ -0,0 +1,12 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -fdump-tree-optimized" } */
> +/* PR tree-optimization/111431 */
> +
> +int
> +foo (int a)
> +{
> +  int b = a == 2;
> +  return (a & b);
> +}
> +
> +/* { dg-final { scan-tree-dump-times "return 0" 1 "optimized" } } */
> diff --git a/gcc/testsuite/gcc.dg/binop-andeq2.c b/gcc/testsuite/gcc.dg/binop-andeq2.c
> new file mode 100644
> index 00000000000..895262fc17e
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/binop-andeq2.c
> @@ -0,0 +1,14 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -fdump-tree-optimized" } */
> +/* PR tree-optimization/111431 */
> +
> +int
> +foo (int a)
> +{
> +  int b = a == 1025;
> +  return (a & b);
> +}
> +
> +/* { dg-final { scan-tree-dump-not "return 0"  "optimized" } } */
> +/* { dg-final { scan-tree-dump-not " & "  "optimized" } } */
> +/* { dg-final { scan-tree-dump-times " == 1025;" 1  "optimized" } } */
> diff --git a/gcc/testsuite/gcc.dg/binop-notand1a.c b/gcc/testsuite/gcc.dg/binop-notand1a.c
> index c7e932b2638..d94685eb4ce 100644
> --- a/gcc/testsuite/gcc.dg/binop-notand1a.c
> +++ b/gcc/testsuite/gcc.dg/binop-notand1a.c
> @@ -7,6 +7,4 @@ foo (char a, unsigned short b)
>    return (a & !a) | (b & !b);
>  }
>
> -/* As long as comparisons aren't boolified and casts from boolean-types
> -   aren't preserved, the folding of  X & !X to zero fails.  */
> -/* { dg-final { scan-tree-dump-times "return 0" 1 "optimized" { xfail *-*-* } } } */
> +/* { dg-final { scan-tree-dump-times "return 0" 1 "optimized"  } } */
> diff --git a/gcc/testsuite/gcc.dg/binop-notand4a.c b/gcc/testsuite/gcc.dg/binop-notand4a.c
> index dce6a5c7eb5..bd9c7cce638 100644
> --- a/gcc/testsuite/gcc.dg/binop-notand4a.c
> +++ b/gcc/testsuite/gcc.dg/binop-notand4a.c
> @@ -7,6 +7,4 @@ foo (unsigned char a, _Bool b)
>    return (!a & a) | (b & !b);
>  }
>
> -/* As long as comparisons aren't boolified and casts from boolean-types
> -   aren't preserved, the folding of  X & !X to zero fails.  */
> -/* { dg-final { scan-tree-dump-times "return 0" 1 "optimized" { xfail *-*-* } } } */
> +/* { dg-final { scan-tree-dump-times "return 0" 1 "optimized" } } */
> diff --git a/gcc/testsuite/gcc.dg/binop-notand7.c b/gcc/testsuite/gcc.dg/binop-notand7.c
> new file mode 100644
> index 00000000000..c2bb6a0449d
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/binop-notand7.c
> @@ -0,0 +1,12 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -fdump-tree-optimized" } */
> +/* PR tree-optimization/111431 */
> +
> +int
> +foo (int a)
> +{
> +  int b = !a;
> +  return (a & b);
> +}
> +
> +/* { dg-final { scan-tree-dump-times "return 0" 1 "optimized" } } */
> diff --git a/gcc/testsuite/gcc.dg/binop-notand7a.c b/gcc/testsuite/gcc.dg/binop-notand7a.c
> new file mode 100644
> index 00000000000..dd50916da61
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/binop-notand7a.c
> @@ -0,0 +1,12 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -fdump-tree-optimized" } */
> +/* PR tree-optimization/111431 */
> +
> +unsigned
> +foo (int a)
> +{
> +  int b = !a;
> +  return (a & b);
> +}
> +
> +/* { dg-final { scan-tree-dump-times "return 0" 1 "optimized" } } */
> --
> 2.31.1
>

      reply	other threads:[~2023-09-18  7:37 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-16 15:59 Andrew Pinski
2023-09-18  7:37 ` 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='CAFiYyc0fekQKywc4D6EJZWPV49AHJNumY==36KHmAZ2tHVUe8w@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).