From patchwork Thu Jul 21 21:28:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Li, Pan2 via Gcc-patches" X-Patchwork-Id: 112 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e252:0:0:0:0:0 with SMTP id bl18csp1648475wrb; Thu, 21 Jul 2022 14:29:28 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vfFjXB3a+36linCrRsrm+5WXsyHdEzp6UkfnF2YYh56RQ1ZX9DG/+omofYvG0/g/5QQeof X-Received: by 2002:a17:906:106:b0:722:e997:a365 with SMTP id 6-20020a170906010600b00722e997a365mr447801eje.169.1658438968628; Thu, 21 Jul 2022 14:29:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658438968; cv=none; d=google.com; s=arc-20160816; b=CFTAvAcelvSNvp8Da3TQ0+22i9JO3DsssUMT0RrIoTtbKs88aL3EclcDGzjj5vQuLK nM3wNwA+mL7xNnRxX6OIR1Zw6QeUXhdb/iiB/+Frs6l2Fe5Bq1dFT5l1ied8mZgbOhaS 5R4noglAmgMwYj9/ctF3HoMu3J6/RmMwS+5O9wOXNtV+J0zndmfvtJ5eoLXh9fL8EA70 8a5jELqrk6xlyzZpWkoSQq/HoCaYPrPSbcJavjMOMJYnY6YzVB2woYuJ7YvcD7EyC1cX pUH4HLAj2+i2ebZE/MmbZiOTQdfmJIi2C/4sD0dkLdTH4Msq+rZHHxBmIVeONkWb47CH nIrw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=Ve8wUB3Nwa1yvL3bHvu1p13TbH06hjmaLlMb5V0qGf8=; b=lK2aVXjRUuv/48fxp3ggc/AYugG+PpxWdCgKC3M95K90QRj7okeFb9eXvBgYCRsw1t YIA+/7YBpyLhJo+m83kUvLesI7C5GWPuuGOnHyyieuZhnFKRAjLxTdV4vdNy8QycFlk6 Ll/Q+T89MiCq0AdaE6M+QeGARaLYAA9kcDHmoU5w7W0hesTXtEJs3mqxOEjfYMyzSJMR ZXcPczxQgcOu5qFpovyKF5bUIiXo0EIFeSf3u0KjRm9ierqnKuYn26bKTjKCYJlOYiJs 3Ay/JwbzhYrIQNvTk/x9cGJivppbq6s8C4l9aNTO0OBqZNVUkn6UnK78QxZP38MwAzn+ hLMw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=u664DjVb; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id du11-20020a17090772cb00b0070d08e9f4fbsi3187987ejc.615.2022.07.21.14.29.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Jul 2022 14:29:28 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=u664DjVb; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4E2E73835687 for ; Thu, 21 Jul 2022 21:29:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4E2E73835687 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1658438965; bh=Ve8wUB3Nwa1yvL3bHvu1p13TbH06hjmaLlMb5V0qGf8=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=u664DjVbwxJfqL2d6erpZOwkvvqpCIlT6WVSyCvLqQAmmQugtypgldEQgwau2V2XO zruqtwIas12l/UYCJUk07Arze7JajElL5NAOIHBM/Ch11V5GVi5yuXZAk8uE7qtDui ILY49cgNcNlB2vz8D+30aD06/QffFxZ64D+q0ylw= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id D9D743835680 for ; Thu, 21 Jul 2022 21:28:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D9D743835680 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-626-CONsZv-qMm-WeMycCLdYCg-1; Thu, 21 Jul 2022 17:28:41 -0400 X-MC-Unique: CONsZv-qMm-WeMycCLdYCg-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 4E85B85A584 for ; Thu, 21 Jul 2022 21:28:41 +0000 (UTC) Received: from sfeifer.remote.csb (unknown [10.22.18.132]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2FFF340CF916 for ; Thu, 21 Jul 2022 21:28:41 +0000 (UTC) To: GCC Patches Subject: [pushed] match.pd: Add new abs pattern [PR94920] Date: Thu, 21 Jul 2022 17:28:34 -0400 Message-Id: <20220721212834.174429-1-sfeifer@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.4 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_NONE, SPF_HELO_NONE, SPF_NONE, TXREP 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-Patchwork-Original-From: Sam Feifer via Gcc-patches From: "Li, Pan2 via Gcc-patches" Reply-To: Sam Feifer Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1738999299954462141?= X-GMAIL-MSGID: =?utf-8?q?1738999299954462141?= 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). 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/94920 gcc/ChangeLog: * match.pd (x >= 0 ? x : 0) + (x <= 0 ? -x : 0): New simplification. (x <= 0 ? -x : 0): New simplification. gcc/testsuite/ChangeLog: * g++.dg/pr94920-1.C: New test. * g++.dg/pr94920.C: New test. * gcc.dg/pr94920-2.c: New test. --- gcc/match.pd | 10 +++++ gcc/testsuite/g++.dg/pr94920-1.C | 17 +++++++++ gcc/testsuite/g++.dg/pr94920.C | 63 ++++++++++++++++++++++++++++++++ gcc/testsuite/gcc.dg/pr94920-2.c | 15 ++++++++ 4 files changed, 105 insertions(+) create mode 100644 gcc/testsuite/g++.dg/pr94920-1.C create mode 100644 gcc/testsuite/g++.dg/pr94920.C create mode 100644 gcc/testsuite/gcc.dg/pr94920-2.c base-commit: 28be481cf47d52af8b11972d2394226bbaf87867 diff --git a/gcc/match.pd b/gcc/match.pd index 88a1a5aa9cc..9736393061a 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -339,6 +339,11 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0))) (COPYSIGN_ALL (negate @0) @1))) +/* (x >= 0 ? x : 0) + (x <= 0 ? -x : 0) -> abs x. */ +(simplify + (plus:c (max @0 integer_zerop) (max (negate @0) integer_zerop)) + (abs @0)) + /* X * 1, X / 1 -> X. */ (for op (mult trunc_div ceil_div floor_div round_div exact_div) (simplify @@ -3425,6 +3430,11 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) && (GIMPLE || !TREE_SIDE_EFFECTS (@1))) (cond (convert:boolean_type_node @2) @1 @0))) +/* (x <= 0 ? -x : 0) -> max(-x, 0). */ +(simplify + (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1) + (max @2 @1)) + /* Simplifications of shift and rotates. */ (for rotate (lrotate rrotate) diff --git a/gcc/testsuite/g++.dg/pr94920-1.C b/gcc/testsuite/g++.dg/pr94920-1.C new file mode 100644 index 00000000000..6c6483eab2d --- /dev/null +++ b/gcc/testsuite/g++.dg/pr94920-1.C @@ -0,0 +1,17 @@ +/* PR tree-optimization/94920 */ +/* { dg-do run } */ + +#include "pr94920.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/g++.dg/pr94920.C b/gcc/testsuite/g++.dg/pr94920.C new file mode 100644 index 00000000000..925ec4f42f1 --- /dev/null +++ b/gcc/testsuite/g++.dg/pr94920.C @@ -0,0 +1,63 @@ +/* PR tree-optimization/94920 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +typedef int __attribute__((vector_size(4*sizeof(int)))) vint; + +/* Same form as PR. */ +__attribute__((noipa)) unsigned int foo(int x) { + return (x >= 0 ? x : 0) + (x <= 0 ? -x : 0); +} + +/* Test for forward propogation. */ +__attribute__((noipa)) unsigned int corge(int x) { + int w = (x >= 0 ? x : 0); + int y = -x; + int z = (y >= 0 ? y : 0); + return w + z; +} + +/* Vector case. */ +__attribute__((noipa)) vint thud(vint x) { + vint t = (x >= 0 ? x : 0) ; + vint xx = -x; + vint t1 = (xx >= 0 ? xx : 0); + return t + t1; +} + +/* 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 " 6 "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/pr94920-2.c b/gcc/testsuite/gcc.dg/pr94920-2.c new file mode 100644 index 00000000000..a2d23324cfa --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr94920-2.c @@ -0,0 +1,15 @@ +/* PR tree-optimization/94920 */ +/* { 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" } } */