From: Richard Biener <richard.guenther@gmail.com>
To: Andrew Pinski <pinskia@gmail.com>
Cc: gcc-patches@gcc.gnu.org
Subject: Re: [PATCH] MATCH: Improve `A CMP 0 ? A : -A` set of patterns to use bitwise_equal_p.
Date: Mon, 16 Oct 2023 11:09:00 +0200 [thread overview]
Message-ID: <CAFiYyc23H9WzFfTAb_TsJfqyb65yJiiqvmsdsd=Gv4TuCaP2ew@mail.gmail.com> (raw)
In-Reply-To: <20231015215956.512326-1-pinskia@gmail.com>
On Mon, Oct 16, 2023 at 12:00 AM Andrew Pinski <pinskia@gmail.com> wrote:
>
> This improves the `A CMP 0 ? A : -A` set of match patterns to use
> bitwise_equal_p which allows an nop cast between signed and unsigned.
> This allows catching a few extra cases which were not being caught before.
>
> OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.
OK.
> gcc/ChangeLog:
>
> PR tree-optimization/101541
> * match.pd (A CMP 0 ? A : -A): Improve
> using bitwise_equal_p.
>
> gcc/testsuite/ChangeLog:
>
> PR tree-optimization/101541
> * gcc.dg/tree-ssa/phi-opt-36.c: New test.
> * gcc.dg/tree-ssa/phi-opt-37.c: New test.
> ---
> gcc/match.pd | 49 ++++++++++++---------
> gcc/testsuite/gcc.dg/tree-ssa/phi-opt-36.c | 51 ++++++++++++++++++++++
> gcc/testsuite/gcc.dg/tree-ssa/phi-opt-37.c | 24 ++++++++++
> 3 files changed, 104 insertions(+), 20 deletions(-)
> create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-36.c
> create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-37.c
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 45624f3dcb4..142e2dfbeb1 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -5668,42 +5668,51 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
> /* A == 0 ? A : -A same as -A */
> (for cmp (eq uneq)
> (simplify
> - (cnd (cmp @0 zerop) @0 (negate@1 @0))
> - (if (!HONOR_SIGNED_ZEROS (type))
> + (cnd (cmp @0 zerop) @2 (negate@1 @2))
> + (if (!HONOR_SIGNED_ZEROS (type)
> + && bitwise_equal_p (@0, @2))
> @1))
> (simplify
> - (cnd (cmp @0 zerop) zerop (negate@1 @0))
> - (if (!HONOR_SIGNED_ZEROS (type))
> + (cnd (cmp @0 zerop) zerop (negate@1 @2))
> + (if (!HONOR_SIGNED_ZEROS (type)
> + && bitwise_equal_p (@0, @2))
> @1))
> )
> /* A != 0 ? A : -A same as A */
> (for cmp (ne ltgt)
> (simplify
> - (cnd (cmp @0 zerop) @0 (negate @0))
> - (if (!HONOR_SIGNED_ZEROS (type))
> - @0))
> + (cnd (cmp @0 zerop) @1 (negate @1))
> + (if (!HONOR_SIGNED_ZEROS (type)
> + && bitwise_equal_p (@0, @1))
> + @1))
> (simplify
> - (cnd (cmp @0 zerop) @0 integer_zerop)
> - (if (!HONOR_SIGNED_ZEROS (type))
> - @0))
> + (cnd (cmp @0 zerop) @1 integer_zerop)
> + (if (!HONOR_SIGNED_ZEROS (type)
> + && bitwise_equal_p (@0, @1))
> + @1))
> )
> /* A >=/> 0 ? A : -A same as abs (A) */
> (for cmp (ge gt)
> (simplify
> - (cnd (cmp @0 zerop) @0 (negate @0))
> - (if (!HONOR_SIGNED_ZEROS (type)
> - && !TYPE_UNSIGNED (type))
> - (abs @0))))
> + (cnd (cmp @0 zerop) @1 (negate @1))
> + (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
> + && !TYPE_UNSIGNED (TREE_TYPE(@0))
> + && bitwise_equal_p (@0, @1))
> + (if (TYPE_UNSIGNED (type))
> + (absu:type @0)
> + (abs @0)))))
> /* A <=/< 0 ? A : -A same as -abs (A) */
> (for cmp (le lt)
> (simplify
> - (cnd (cmp @0 zerop) @0 (negate @0))
> - (if (!HONOR_SIGNED_ZEROS (type)
> - && !TYPE_UNSIGNED (type))
> - (if (ANY_INTEGRAL_TYPE_P (type)
> - && !TYPE_OVERFLOW_WRAPS (type))
> + (cnd (cmp @0 zerop) @1 (negate @1))
> + (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
> + && !TYPE_UNSIGNED (TREE_TYPE(@0))
> + && bitwise_equal_p (@0, @1))
> + (if ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
> + && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
> + || TYPE_UNSIGNED (type))
> (with {
> - tree utype = unsigned_type_for (type);
> + tree utype = unsigned_type_for (TREE_TYPE(@0));
> }
> (convert (negate (absu:utype @0))))
> (negate (abs @0)))))
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-36.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-36.c
> new file mode 100644
> index 00000000000..4baf9f82a22
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-36.c
> @@ -0,0 +1,51 @@
> +/* { dg-options "-O2 -fdump-tree-phiopt" } */
> +
> +unsigned f0(int A)
> +{
> + unsigned t = A;
> +// A == 0? A : -A same as -A
> + if (A == 0) return t;
> + return -t;
> +}
> +
> +unsigned f1(int A)
> +{
> + unsigned t = A;
> +// A != 0? A : -A same as A
> + if (A != 0) return t;
> + return -t;
> +}
> +unsigned f2(int A)
> +{
> + unsigned t = A;
> +// A >= 0? A : -A same as abs (A)
> + if (A >= 0) return t;
> + return -t;
> +}
> +unsigned f3(int A)
> +{
> + unsigned t = A;
> +// A > 0? A : -A same as abs (A)
> + if (A > 0) return t;
> + return -t;
> +}
> +unsigned f4(int A)
> +{
> + unsigned t = A;
> +// A <= 0? A : -A same as -abs (A)
> + if (A <= 0) return t;
> + return -t;
> +}
> +unsigned f5(int A)
> +{
> + unsigned t = A;
> +// A < 0? A : -A same as -abs (A)
> + if (A < 0) return t;
> + return -t;
> +}
> +
> +/* f4 and f5 are not allowed to be optimized in early phi-opt. */
> +/* { dg-final { scan-tree-dump-times "if " 2 "phiopt1" } } */
> +/* { dg-final { scan-tree-dump-not "if " "phiopt2" } } */
> +
> +
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-37.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-37.c
> new file mode 100644
> index 00000000000..f1ff472aaff
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-37.c
> @@ -0,0 +1,24 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O1 -fdump-tree-phiopt1" } */
> +
> +unsigned abs_with_convert0 (int x)
> +{
> + unsigned int y = x;
> +
> + if (x < 0)
> + y = -y;
> +
> + return y;
> +}
> +unsigned abs_with_convert1 (unsigned x)
> +{
> + int y = x;
> +
> + if (y < 0)
> + x = -x;
> +
> + return x;
> +}
> +
> +/* { dg-final { scan-tree-dump-times "ABSU_EXPR <" 2 "phiopt1" } } */
> +/* { dg-final { scan-tree-dump-not "if " "phiopt1" } } */
> --
> 2.39.3
>
prev parent reply other threads:[~2023-10-16 9:11 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-15 21:59 Andrew Pinski
2023-10-16 9:09 ` 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='CAFiYyc23H9WzFfTAb_TsJfqyb65yJiiqvmsdsd=Gv4TuCaP2ew@mail.gmail.com' \
--to=richard.guenther@gmail.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=pinskia@gmail.com \
/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).