public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Sam Feifer <sfeifer@redhat.com>
To: Andrew Pinski <pinskia@gmail.com>
Cc: GCC Patches <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH] match.pd: Add new abs pattern [PR94290]
Date: Thu, 14 Jul 2022 10:09:27 -0400	[thread overview]
Message-ID: <CAJKdq8RKGsUdj8qL-M3UVeu1ZnZwLXt3xCibzSMoe5EaJ+Q3Fw@mail.gmail.com> (raw)
In-Reply-To: <CA+=Sn1nNw_boibUQ0ij54MHTNP62FmVvXSUPq=-q_Q_AyoafUw@mail.gmail.com>

On Wed, Jul 13, 2022 at 3:36 PM Andrew Pinski <pinskia@gmail.com> wrote:

> On Wed, Jul 13, 2022 at 12:26 PM Sam Feifer via Gcc-patches
> <gcc-patches@gcc.gnu.org> wrote:
> >
> > This patch is intended to fix a missed optimization in match.pd. It
> optimizes (x >= 0 ? x : 0) + (x <= 0 ? -x : 0) to just abs(x). I had to
> write a second simplification in match.pd to handle the commutative
> property as the match was not ocurring otherwise. Additionally, the pattern
> (x <= 0 ? -x : 0) now gets optimized to max(-x, 0), which helps with the
> other simplification rule.
>
> You could use :c for the commutative property instead and that should
> simplify things.
> That is:
>
> (simplify
>   (plus:c (max @0 integer_zerop) (max (negate @0) integer_zerop))
>   (abs @0))
>
> Also since integer_zerop works on vectors, it seems like you should
> add a testcase or two for the vector case.
> Also would be useful if you write a testcase that uses different
> statements rather than one big one so it gets exercised in the
> forwprop case.
> Note also if either of the max are used more than just in this
> simplification, it could increase the lifetime of @0, maybe you need
> to add :s to the max expressions.
>
>
Thanks for the feedback. I'm not quite sure what a vector test case would
look like for this. Could I get some guidance on that?

Thanks
-Sam


> Thanks,
> Andrew
>
> >
> > Tests are also included to be added to the testsuite.
> >
> > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
> >
> >         PR tree-optimization/94290
> >
> > gcc/ChangeLog:
> >
> >         * match.pd (x >= 0 ? x : 0) + (x <= 0 ? -x : 0): New
> simplification.
> >         * match.pd (x <= 0 ? -x : 0): New Simplification.
> >
> > gcc/testsuite/ChangeLog:
> >
> >         * gcc.c-torture/execute/pr94290-1.c: New test.
> >         * gcc.dg/pr94290-2.c: New test.
> >         * gcc.dg/pr94290.c: New test.
> > ---
> >  gcc/match.pd                                  | 15 ++++++
> >  .../gcc.c-torture/execute/pr94290-1.c         | 16 +++++++
> >  gcc/testsuite/gcc.dg/pr94290-2.c              | 15 ++++++
> >  gcc/testsuite/gcc.dg/pr94290.c                | 46 +++++++++++++++++++
> >  4 files changed, 92 insertions(+)
> >  create mode 100644 gcc/testsuite/gcc.c-torture/execute/pr94290-1.c
> >  create mode 100644 gcc/testsuite/gcc.dg/pr94290-2.c
> >  create mode 100644 gcc/testsuite/gcc.dg/pr94290.c
> >
> > diff --git a/gcc/match.pd b/gcc/match.pd
> > index 45aefd96688..55ca79d7ac9 100644
> > --- a/gcc/match.pd
> > +++ b/gcc/match.pd
> > @@ -7848,3 +7848,18 @@ and,
> >        (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
> >          (bit_and @0 @1)
> >        (cond (le @0 @1) @0 (bit_and @0 @1))))))
> > +
> > +/* (x >= 0 ? x : 0) + (x <= 0 ? -x : 0) -> abs x.  */
> > +(simplify
> > +  (plus (max @0 integer_zerop) (max (negate @0) integer_zerop))
> > +  (abs @0))
> > +
> > +/* (x <= 0 ? -x : 0) + (x >= 0 ? x : 0) -> abs x.  */
> > +(simplify
> > +  (plus (max (negate @0) integer_zerop) (max @0 integer_zerop) )
> > +  (abs @0))
> > +
> > +/* (x <= 0 ? -x : 0) -> max(-x, 0).  */
> > +(simplify
> > + (cond (le @0 integer_zerop@1) (negate @0) integer_zerop@1)
> > + (max (negate @0) @1))
> > diff --git a/gcc/testsuite/gcc.c-torture/execute/pr94290-1.c
> b/gcc/testsuite/gcc.c-torture/execute/pr94290-1.c
> > new file mode 100644
> > index 00000000000..93b80d569aa
> > --- /dev/null
> > +++ b/gcc/testsuite/gcc.c-torture/execute/pr94290-1.c
> > @@ -0,0 +1,16 @@
> > +/* PR tree-optimization/94290 */
> > +
> > +#include "../../gcc.dg/pr94290.c"
> > +
> > +int main() {
> > +
> > +    if (foo(0) != 0
> > +        || foo(-42) != 42
> > +        || foo(42) != 42
> > +        || baz(-10) != 10
> > +        || baz(-10) != 10) {
> > +            __builtin_abort();
> > +        }
> > +
> > +    return 0;
> > +}
> > diff --git a/gcc/testsuite/gcc.dg/pr94290-2.c
> b/gcc/testsuite/gcc.dg/pr94290-2.c
> > new file mode 100644
> > index 00000000000..ea6e55755f5
> > --- /dev/null
> > +++ b/gcc/testsuite/gcc.dg/pr94290-2.c
> > @@ -0,0 +1,15 @@
> > +/* PR tree-optimization/94290 */
> > +/* { dg-do compile } */
> > +/* { dg-options "-O2 -fdump-tree-optimized" } */
> > +
> > +/* Form from PR.  */
> > +__attribute__((noipa)) unsigned int foo(int x) {
> > +    return x <= 0 ? -x : 0;
> > +}
> > +
> > +/* Changed order.  */
> > +__attribute__((noipa)) unsigned int bar(int x) {
> > +    return 0 >= x ? -x : 0;
> > +}
> > +
> > +/* { dg-final {scan-tree-dump-times " MAX_EXPR " 2 "optimized" } } */
> > diff --git a/gcc/testsuite/gcc.dg/pr94290.c
> b/gcc/testsuite/gcc.dg/pr94290.c
> > new file mode 100644
> > index 00000000000..47617c36c02
> > --- /dev/null
> > +++ b/gcc/testsuite/gcc.dg/pr94290.c
> > @@ -0,0 +1,46 @@
> > +/* PR tree-optimization/94290 */
> > +/* { dg-do compile } */
> > +/* { dg-options "-O2 -fdump-tree-optimized" } */
> > +
> > +
> > +/* Same form as PR.  */
> > +__attribute__((noipa)) unsigned int foo(int x) {
> > +    return (x >= 0 ? x : 0) + (x <= 0 ? -x : 0);
> > +}
> > +
> > +/* Signed function.  */
> > +__attribute__((noipa)) int bar(int x) {
> > +    return (x >= 0 ? x : 0) + (x <= 0 ? -x : 0);
> > +}
> > +
> > +/* Commutative property.  */
> > +__attribute__((noipa)) unsigned int baz(int x) {
> > +    return (x <= 0 ? -x : 0) + (x >= 0 ? x : 0);
> > +}
> > +
> > +/* Flipped order for max expressions.  */
> > +__attribute__((noipa)) unsigned int quux(int x) {
> > +    return (0 <= x ? x : 0) + (0 >= x ? -x : 0);
> > +}
> > +
> > +/* Not zero so should not optimize.  */
> > +__attribute__((noipa)) unsigned int waldo(int x) {
> > +    return (x >= 4 ? x : 4) + (x <= 4 ? -x : 4);
> > +}
> > +
> > +/* Not zero so should not optimize.  */
> > +__attribute__((noipa)) unsigned int fred(int x) {
> > +    return (x >= -4 ? x : -4) + (x <= -4 ? -x : -4);
> > +}
> > +
> > +/* Incorrect pattern.  */
> > +__attribute__((noipa)) unsigned int goo(int x) {
> > +    return (x <= 0 ? x : 0) + (x >= 0 ? -x : 0);
> > +}
> > +
> > +/* Incorrect pattern.  */
> > +__attribute__((noipa)) int qux(int x) {
> > +    return (x >= 0 ? x : 0) + (x >= 0 ? x : 0);
> > +}
> > +
> > +/* { dg-final {scan-tree-dump-times " ABS_EXPR " 4 "optimized" } } */
> >
> > base-commit: 6af530f914801f5e561057da55c41480f28751f7
> > --
> > 2.31.1
> >
>
>

  parent reply	other threads:[~2022-07-14 14:09 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-13 19:24 Sam Feifer
2022-07-13 19:35 ` Andrew Pinski
2022-07-14  6:52   ` Richard Biener
2022-07-14 14:09   ` Sam Feifer [this message]
2022-07-14 17:24     ` Andrew Pinski
2022-07-14 19:38       ` Sam Feifer
2022-07-14 19:53         ` Andrew Pinski
2022-07-18 13:07           ` Sam Feifer
2022-07-18 17:30             ` Sam Feifer
2022-07-19  6:36               ` Richard Biener
2022-07-19 13:48                 ` Sam Feifer

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=CAJKdq8RKGsUdj8qL-M3UVeu1ZnZwLXt3xCibzSMoe5EaJ+Q3Fw@mail.gmail.com \
    --to=sfeifer@redhat.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).