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 707EB382C156 for ; Wed, 13 Jul 2022 19:25:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 707EB382C156 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-663--VWSGWzbPnyMJ9iOArijuQ-1; Wed, 13 Jul 2022 15:25:15 -0400 X-MC-Unique: -VWSGWzbPnyMJ9iOArijuQ-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id ECCDB858F11 for ; Wed, 13 Jul 2022 19:25:14 +0000 (UTC) Received: from sfeifer.remote.csb (unknown [10.22.18.121]) by smtp.corp.redhat.com (Postfix) with ESMTP id CF47E18ECB for ; Wed, 13 Jul 2022 19:25:14 +0000 (UTC) From: Sam Feifer To: gcc-patches@gcc.gnu.org Subject: [PATCH] match.pd: Add new abs pattern [PR94290] Date: Wed, 13 Jul 2022 15:24:20 -0400 Message-Id: <20220713192420.3126654-1-sfeifer@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-13.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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 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:25:18 -0000 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. 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