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: Move `(x | y) & (~x ^ y)` over to use bitwise_inverted_equal_p
Date: Tue, 29 Aug 2023 09:32:51 +0200	[thread overview]
Message-ID: <CAFiYyc3cKSE=hQPSBm=EDWQByF6xv5SN-wNP=ngHqBMys+8qXw@mail.gmail.com> (raw)
In-Reply-To: <20230828201402.3786409-1-apinski@marvell.com>

On Mon, Aug 28, 2023 at 10:15 PM Andrew Pinski via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> This moves the match pattern `(x | y) & (~x ^ y)` over to use bitwise_inverted_equal_p.
> This now also allows to optmize comparisons and also catches the missed `(~x | y) & (x ^ y)`
> transformation into `~x & y`.
>
> OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.

OK

> gcc/ChangeLog:
>
>         PR tree-optmization/111147
>         * match.pd (`(x | y) & (~x ^ y)`) Use bitwise_inverted_equal_p
>         instead of matching bit_not.
>
> gcc/testsuite/ChangeLog:
>
>         PR tree-optmization/111147
>         * gcc.dg/tree-ssa/cmpbit-4.c: New test.
> ---
>  gcc/match.pd                             |  7 +++-
>  gcc/testsuite/gcc.dg/tree-ssa/cmpbit-4.c | 47 ++++++++++++++++++++++++
>  2 files changed, 52 insertions(+), 2 deletions(-)
>  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/cmpbit-4.c
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index e6bdc3149b6..47d2733211a 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -1616,8 +1616,11 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>
>  /* (x | y) & (~x ^ y) -> x & y */
>  (simplify
> - (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
> - (bit_and @0 @1))
> + (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 @2))
> + (with { bool wascmp; }
> +  (if (bitwise_inverted_equal_p (@0, @2, wascmp)
> +       && (!wascmp || element_precision (type) == 1))
> +   (bit_and @0 @1))))
>
>  /* (~x | y) & (x | ~y) -> ~(x ^ y) */
>  (simplify
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/cmpbit-4.c b/gcc/testsuite/gcc.dg/tree-ssa/cmpbit-4.c
> new file mode 100644
> index 00000000000..cdba5d623af
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/cmpbit-4.c
> @@ -0,0 +1,47 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -fdump-tree-optimized-raw" } */
> +
> +int g(int x, int y)
> +{
> +  int xp = ~x;
> +  return (x | y) & (xp ^ y); // x & y
> +}
> +int g0(int x, int y)
> +{
> +  int xp = ~x;
> +  return (xp | y) & (x ^ y); // ~x & y
> +}
> +
> +_Bool gb(_Bool x, _Bool y)
> +{
> +  _Bool xp = !x;
> +  return (x | y) & (xp ^ y); // x & y
> +}
> +_Bool gb0(_Bool x, _Bool y)
> +{
> +  _Bool xp = !x;
> +  return (xp | y) & (x ^ y); // !x & y
> +}
> +
> +
> +_Bool gbi(int a, int b)
> +{
> +  _Bool x = a < 2;
> +  _Bool y = b < 3;
> +  _Bool xp = !x;
> +  return (x | y) & (xp ^ y); // x & y
> +}
> +_Bool gbi0(int a, int b)
> +{
> +  _Bool x = a < 2;
> +  _Bool y = b < 3;
> +  _Bool xp = !x;
> +  return (xp | y) & (x ^ y); // !x & y
> +}
> +
> +/* All of these should be optimized to `x & y` or `~x & y` */
> +/* { dg-final { scan-tree-dump-times "le_expr, " 3 "optimized" } } */
> +/* { dg-final { scan-tree-dump-times "gt_expr, " 1 "optimized" } } */
> +/* { dg-final { scan-tree-dump-not "bit_xor_expr, " "optimized" } } */
> +/* { dg-final { scan-tree-dump-times "bit_and_expr, " 6 "optimized" } } */
> +/* { dg-final { scan-tree-dump-times "bit_not_expr, " 2 "optimized" } } */
> --
> 2.31.1
>

      reply	other threads:[~2023-08-29  7:34 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-28 20:14 Andrew Pinski
2023-08-29  7:32 ` 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='CAFiYyc3cKSE=hQPSBm=EDWQByF6xv5SN-wNP=ngHqBMys+8qXw@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).