From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qv1-xf34.google.com (mail-qv1-xf34.google.com [IPv6:2607:f8b0:4864:20::f34]) by sourceware.org (Postfix) with ESMTPS id 9DB7B3856DCA for ; Wed, 13 Jul 2022 19:36:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 9DB7B3856DCA Received: by mail-qv1-xf34.google.com with SMTP id r12so5584901qvm.3 for ; Wed, 13 Jul 2022 12:36:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=1/AuNq9ACvFI7ChxDPKHw7oZT4Q6/LlKhk5MuIX7COY=; b=pNq78QuN46VyKB3q8rxJMqTIsXkpsOAM8ISHHQartUHwVpVdOvbPj7lyeyJ61TgOQW jTYhKhqza/H2isizDph+CxXm5w1YOtdOM89eK6NHvYzEhHdwSsq6QG53xv8PA3ivyKf1 707dpUloH8OAsAx8Rx/t1xR4ut3wA6J8YacMwQEdhpMDxvto1K2lkfu0kd3wQNV6Dacw MYJXs2X8v/eoFb1EdeOMVSoYJLrc3etDn9zUFiLOk3zNlMniysw1n0naiYWiSqWaJ7oI Rb62MyRtREbg6BB07+QGXUK5oymBSgRUveQgJoO0bz33xOZAs72nJ1YiPBmrWD40Iqp2 l1eQ== X-Gm-Message-State: AJIora94eomilimgJ3zE+IShDbWV6smlLPL0lyYnvkA/y/0lSRiNBdtv rN6RSwMFYnUXRP6h3IQS5/VtEW30AMrK+e14j9w= X-Google-Smtp-Source: AGRyM1vTRN2WngGOd1zaTV3YGkLdnsYGKKi70m77xHcTmRMf9AU1NTgueHuWXOZsgfpEg3BnvJUEGJ2kFx8qE9JaRnU= X-Received: by 2002:ad4:594e:0:b0:472:f6bc:c7fb with SMTP id eo14-20020ad4594e000000b00472f6bcc7fbmr4804698qvb.96.1657740966853; Wed, 13 Jul 2022 12:36:06 -0700 (PDT) MIME-Version: 1.0 References: <20220713192420.3126654-1-sfeifer@redhat.com> In-Reply-To: <20220713192420.3126654-1-sfeifer@redhat.com> From: Andrew Pinski Date: Wed, 13 Jul 2022 12:35:53 -0700 Message-ID: Subject: Re: [PATCH] match.pd: Add new abs pattern [PR94290] To: Sam Feifer Cc: GCC Patches Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-8.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 13 Jul 2022 19:36:09 -0000 On Wed, Jul 13, 2022 at 12:26 PM Sam Feifer via Gcc-patches wrote: > > This patch is intended to fix a missed optimization in match.pd. It optim= izes (x >=3D 0 ? x : 0) + (x <=3D 0 ? -x : 0) to just abs(x). I had to writ= e a second simplification in match.pd to handle the commutative property as= the match was not ocurring otherwise. Additionally, the pattern (x <=3D 0 = ? -x : 0) now gets optimized to max(-x, 0), which helps with the other simp= lification 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, 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 >=3D 0 ? x : 0) + (x <=3D 0 ? -x : 0): New simplifi= cation. > * match.pd (x <=3D 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 >=3D 0 ? x : 0) + (x <=3D 0 ? -x : 0) -> abs x. */ > +(simplify > + (plus (max @0 integer_zerop) (max (negate @0) integer_zerop)) > + (abs @0)) > + > +/* (x <=3D 0 ? -x : 0) + (x >=3D 0 ? x : 0) -> abs x. */ > +(simplify > + (plus (max (negate @0) integer_zerop) (max @0 integer_zerop) ) > + (abs @0)) > + > +/* (x <=3D 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/testsu= ite/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) !=3D 0 > + || foo(-42) !=3D 42 > + || foo(42) !=3D 42 > + || baz(-10) !=3D 10 > + || baz(-10) !=3D 10) { > + __builtin_abort(); > + } > + > + return 0; > +} > diff --git a/gcc/testsuite/gcc.dg/pr94290-2.c b/gcc/testsuite/gcc.dg/pr94= 290-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 <=3D 0 ? -x : 0; > +} > + > +/* Changed order. */ > +__attribute__((noipa)) unsigned int bar(int x) { > + return 0 >=3D 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/pr9429= 0.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 >=3D 0 ? x : 0) + (x <=3D 0 ? -x : 0); > +} > + > +/* Signed function. */ > +__attribute__((noipa)) int bar(int x) { > + return (x >=3D 0 ? x : 0) + (x <=3D 0 ? -x : 0); > +} > + > +/* Commutative property. */ > +__attribute__((noipa)) unsigned int baz(int x) { > + return (x <=3D 0 ? -x : 0) + (x >=3D 0 ? x : 0); > +} > + > +/* Flipped order for max expressions. */ > +__attribute__((noipa)) unsigned int quux(int x) { > + return (0 <=3D x ? x : 0) + (0 >=3D x ? -x : 0); > +} > + > +/* Not zero so should not optimize. */ > +__attribute__((noipa)) unsigned int waldo(int x) { > + return (x >=3D 4 ? x : 4) + (x <=3D 4 ? -x : 4); > +} > + > +/* Not zero so should not optimize. */ > +__attribute__((noipa)) unsigned int fred(int x) { > + return (x >=3D -4 ? x : -4) + (x <=3D -4 ? -x : -4); > +} > + > +/* Incorrect pattern. */ > +__attribute__((noipa)) unsigned int goo(int x) { > + return (x <=3D 0 ? x : 0) + (x >=3D 0 ? -x : 0); > +} > + > +/* Incorrect pattern. */ > +__attribute__((noipa)) int qux(int x) { > + return (x >=3D 0 ? x : 0) + (x >=3D 0 ? x : 0); > +} > + > +/* { dg-final {scan-tree-dump-times " ABS_EXPR " 4 "optimized" } } */ > > base-commit: 6af530f914801f5e561057da55c41480f28751f7 > -- > 2.31.1 >