From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id EDD5B3858D37 for ; Thu, 14 Jul 2022 19:38:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org EDD5B3858D37 Received: from mail-pg1-f197.google.com (mail-pg1-f197.google.com [209.85.215.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-41-ySu8q1TOMAaSTsjOpghmfg-1; Thu, 14 Jul 2022 15:38:56 -0400 X-MC-Unique: ySu8q1TOMAaSTsjOpghmfg-1 Received: by mail-pg1-f197.google.com with SMTP id h185-20020a636cc2000000b00419b8e7df69so1337090pgc.18 for ; Thu, 14 Jul 2022 12:38:55 -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; bh=u87F10ypjarzHBXRCiK0kK4DUOyrlsg41CdiLr+iMJw=; b=VRqs2D1QurCR5rUprJ/urkU04782pus7YnRtH0/F8ndNs5S9IczHmwE/8HSpsdVdtW Iz2rBLsYGU6FKcaSPPUz67/AlGJ8ZU2s/zK40Bi+7KWQlcXzdYEs9plR+WE6s73qhZ+R vEzsG4NjP5e5IhTHK00iaoE6PNe+PYh+2NNDhbFIuHEd8GQDXe/cJPAgymftg8a/8cGA lGSqiVvV0tMV6vTouH1DNMPLmySBu1LCCE8FqQnRaPRquzCcsY57yoZ/YrEnYmV6mthZ nVu21Euy3Q4eKi7QTv7gYzAGxGKfXeyLRR9ssSC1LfeXQRw9y0FClGCvuJPKQ+MjTRzc JaaA== X-Gm-Message-State: AJIora9QC7sR9/wjM5/HQE0Qfvu43+FvKntViJ1nymAQZYr/hT37HzR4 fZRZNg+iKNYD+ajY0MzN46SvGSJ7cLNjTtxIkSZVF88Y9TRzafa1nCnbbZGX58JoiLjjGy5ZxK2 aimqiuSOsk1ov6qlu56Hby4mGIO3Eps9PNQ== X-Received: by 2002:a63:3fcc:0:b0:408:c856:dd6d with SMTP id m195-20020a633fcc000000b00408c856dd6dmr8992918pga.354.1657827534848; Thu, 14 Jul 2022 12:38:54 -0700 (PDT) X-Google-Smtp-Source: AGRyM1t1otoyFg1bCMsY1AcVOivXvFw5SB5y0mr6M/0JrLUdTCdKcVjKu6L24TfdtNXu6HtteHhtoDtuXp0hX2eXndI= X-Received: by 2002:a63:3fcc:0:b0:408:c856:dd6d with SMTP id m195-20020a633fcc000000b00408c856dd6dmr8992899pga.354.1657827534514; Thu, 14 Jul 2022 12:38:54 -0700 (PDT) MIME-Version: 1.0 References: <20220713192420.3126654-1-sfeifer@redhat.com> In-Reply-To: From: Sam Feifer Date: Thu, 14 Jul 2022 15:38:43 -0400 Message-ID: Subject: Re: [PATCH] match.pd: Add new abs pattern [PR94290] To: Andrew Pinski Cc: GCC Patches X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-22.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, HTML_MESSAGE, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, 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 Content-Type: text/plain; charset="UTF-8" X-Content-Filtered-By: Mailman/MimeDel 2.1.29 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: Thu, 14 Jul 2022 19:39:00 -0000 On Thu, Jul 14, 2022 at 1:24 PM Andrew Pinski wrote: > On Thu, Jul 14, 2022 at 7:09 AM Sam Feifer wrote: > > > > > > On Wed, Jul 13, 2022 at 3:36 PM Andrew Pinski wrote: > >> > >> 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 > 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? > > Yes this should produce the pattern at forwprop1 time (with the C++ > front-end, the C front-end does not support vector selects): > typedef int __attribute__((vector_size(4*sizeof(int)))) vint; > > vint foo(vint x) { > vint t = (x >= 0 ? x : 0) ; > vint xx = -x; > vint t1 = (xx >= 0 ? xx : 0); > return t + t1; > } > > int foo(int x) { > int t = (x >= 0 ? x : 0) ; > int xx = -x; > int t1 = (xx >= 0 ? xx : 0); > return t + t1; > } > > Thanks, > Andrew Pinski > > Thanks for the help. I'm still having trouble with the vector test, though. When I try to compile, I get an error saying "used vector type where scalar is required", referring to the max expressions. How do I use the max expression with two vectors as the inputs? Thanks -Sam > > > > > 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 > >> > > >> > >