From patchwork Fri Sep 1 02:26:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 137362 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c792:0:b0:3f2:4152:657d with SMTP id b18csp626947vqu; Thu, 31 Aug 2023 19:27:44 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF0L0+VMD6bvOqCrJd1YwHGwUG/zykg9iqvRZV0DhwIR7hLCl1zUjTtmsQCbeANgYdFT9rl X-Received: by 2002:aa7:d8d7:0:b0:525:6d9e:67c0 with SMTP id k23-20020aa7d8d7000000b005256d9e67c0mr949753eds.23.1693535264649; Thu, 31 Aug 2023 19:27:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693535264; cv=none; d=google.com; s=arc-20160816; b=UC2lk3JOh2GQsjQwG/v+t0jzF7KFc+JwDFCEUlAsbyE4ypDSlKOO903Kh5y4AfMOAW rDyrnW9XX960rv2N3DAAlMCM8ed3SSYyARrMCdkb3OHcAKFgbBF8IlJGHxRoIs+IIWDd fyxkZW3l0niHFPye+YrYw3vm833HzBpJxRRbngiS95DoUGM4PayxVVapetTIbtIN8Svy Dm2tbuGpGQxuR6Hc+vN9x83S3Z6Q0eazx3yVcSde5SBWMKILUSy1EEUPOaYhHNpg9OX2 GhXPtFEvQ4gla1XwKhX9oKV+2QKvGAnDFKwwsXLcmqnFD9lVVFE4KIbtj9Wxk6nuBhZR DkMw== 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:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=hqo7Ce+6eOfmMjs3PM1h5nvKIXGJuo7+RKfQnMgri7Q=; fh=XbcmRug6/SzczusyoNU1I7Lu5oY0AN2u0mWDzKoxdSs=; b=F/IoYxtg6MTOQf37IJMmgXKgmNVNNTeknBoPPkchajyh7cyZCZuwWxq0WWAd+YhdnY kyWqCV6/nhIyOoUWa57vlNp/P5C6+qVxkMGwn4MRUIR2IzKsm6Ro32mYkHGc1+eUahoP F6hdXY5DuW/OTcYfgzZq7XMd7iAbB3+apn7ZhksayfrlB0wawGZBPPb0PQrBsVzdO5Mu yHrKvw6yCeRcdUYIAO9U5x4MXU90h8yyhYFj9InHVzDhIAEmrHdBP3cgrcWSr5Na2FYC arh8hnN0So+FOFD0irp09mXQ7vbHA99K1gSeZBVom0D0RNvQ3fcXe7BXnPj07YwqE5MH NaKw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=pyscBYlT; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id b9-20020aa7c909000000b005234af90321si1813752edt.665.2023.08.31.19.27.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Aug 2023 19:27:44 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=pyscBYlT; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 85DE63858418 for ; Fri, 1 Sep 2023 02:27:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 85DE63858418 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1693535259; bh=hqo7Ce+6eOfmMjs3PM1h5nvKIXGJuo7+RKfQnMgri7Q=; h=To:CC:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=pyscBYlTT2iVkopXnm6RV+TNkOghmSojvKBa2j5yiNKWLJz0B8kajyEP+vatLejCP gB+zV6BCaPnu6fLaPeSjZ36tR0Z+BZ8XXoFlDGPFWq5e/QTTOcbQVOxr7YrIJB4ltl 9aIBdyFPwiyK2X+RKdGale+DToO4/WSyxj3cgS94= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-0016f401.pphosted.com (mx0b-0016f401.pphosted.com [67.231.156.173]) by sourceware.org (Postfix) with ESMTPS id 17E423858D20 for ; Fri, 1 Sep 2023 02:26:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 17E423858D20 Received: from pps.filterd (m0045851.ppops.net [127.0.0.1]) by mx0b-0016f401.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 37VHEO5e011758 for ; Thu, 31 Aug 2023 19:26:49 -0700 Received: from dc5-exch01.marvell.com ([199.233.59.181]) by mx0b-0016f401.pphosted.com (PPS) with ESMTPS id 3st1y680p1-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Thu, 31 Aug 2023 19:26:49 -0700 Received: from DC5-EXCH02.marvell.com (10.69.176.39) by DC5-EXCH01.marvell.com (10.69.176.38) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Thu, 31 Aug 2023 19:26:47 -0700 Received: from maili.marvell.com (10.69.176.80) by DC5-EXCH02.marvell.com (10.69.176.39) with Microsoft SMTP Server id 15.0.1497.48 via Frontend Transport; Thu, 31 Aug 2023 19:26:47 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id CEEDF3F7080; Thu, 31 Aug 2023 19:26:46 -0700 (PDT) To: CC: Andrew Pinski Subject: [PATCH] MATCH: `(nop_convert)-a` into -(nop_convert)a if the negate is single use and a is known not to be signed min value Date: Thu, 31 Aug 2023 19:26:32 -0700 Message-ID: <20230901022632.754088-1-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: e1rJk3MoRDSNJju31kFn-9c7B03xuWKA X-Proofpoint-GUID: e1rJk3MoRDSNJju31kFn-9c7B03xuWKA X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.957,Hydra:6.0.601,FMLib:17.11.176.26 definitions=2023-09-01_01,2023-08-31_01,2023-05-22_02 X-Spam-Status: No, score=-14.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_PASS, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Andrew Pinski via Gcc-patches From: Andrew Pinski Reply-To: Andrew Pinski Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1775800433320140652 X-GMAIL-MSGID: 1775800433320140652 This pushes the conversion further down the chain which allows to optimize away more conversions in many cases. OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. PR tree-optimization/107765 PR tree-optimization/107137 gcc/ChangeLog: * match.pd (`(nop_convert)-a`): New pattern. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/neg-cast-1.c: New test. * gcc.dg/tree-ssa/neg-cast-2.c: New test. * gcc.dg/tree-ssa/neg-cast-3.c: New test. --- gcc/match.pd | 31 ++++++++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/neg-cast-1.c | 17 ++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/neg-cast-2.c | 20 ++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/neg-cast-3.c | 15 +++++++++++ 4 files changed, 83 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/neg-cast-1.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/neg-cast-2.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/neg-cast-3.c diff --git a/gcc/match.pd b/gcc/match.pd index 487a7e38719..3cff9b03d92 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -959,6 +959,37 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) #endif )))) +/* (nop_cast)-var -> -(nop_cast)(var) + if -var is known to not overflow; that is does not include + the signed integer MIN. */ +(simplify + (convert (negate:s @0)) + (if (INTEGRAL_TYPE_P (type) + && tree_nop_conversion_p (type, TREE_TYPE (@0))) + (with { + /* If the top is not set, there is no overflow happening. */ + bool contains_signed_min = !wi::ges_p (tree_nonzero_bits (@0), 0); +#if GIMPLE + int_range_max vr; + if (contains_signed_min + && TREE_CODE (@0) == SSA_NAME + && get_range_query (cfun)->range_of_expr (vr, @0) + && !vr.undefined_p ()) + { + tree stype = signed_type_for (type); + auto minvalue = wi::min_value (stype); + int_range_max valid_range (TREE_TYPE (@0), minvalue, minvalue); + vr.intersect (valid_range); + /* If the range does not include min value, + then we can do this change around. */ + if (vr.undefined_p ()) + contains_signed_min = false; + } +#endif + } + (if (!contains_signed_min) + (negate (convert @0)))))) + (for op (negate abs) /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */ (for coss (COS COSH) diff --git a/gcc/testsuite/gcc.dg/tree-ssa/neg-cast-1.c b/gcc/testsuite/gcc.dg/tree-ssa/neg-cast-1.c new file mode 100644 index 00000000000..7ddf40aca29 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/neg-cast-1.c @@ -0,0 +1,17 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-evrp" } */ +/* PR tree-optimization/107765 */ + +#include + +int a(int input) +{ + if (input == INT_MIN) __builtin_unreachable(); + unsigned t = input; + int tt = -t; + return tt == -input; +} + +/* Should be able to optimize this down to just `return 1;` during evrp. */ +/* { dg-final { scan-tree-dump "return 1;" "evrp" } } */ +/* { dg-final { scan-tree-dump-not " - " "evrp" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/neg-cast-2.c b/gcc/testsuite/gcc.dg/tree-ssa/neg-cast-2.c new file mode 100644 index 00000000000..ce49079e235 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/neg-cast-2.c @@ -0,0 +1,20 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-fre3 -fdump-tree-optimized" } */ +/* part of PR tree-optimization/108397 */ + +long long +foo (unsigned char o) +{ + unsigned long long t1 = -(long long) (o == 0); + unsigned long long t2 = -(long long) (t1 != 0); + unsigned long long t3 = -(long long) (t1 <= t2); + return t3; +} + +/* Should be able to optimize this down to just `return -1;` during fre3. */ +/* { dg-final { scan-tree-dump "return -1;" "fre3" } } */ +/* FRE does not remove all dead statements */ +/* { dg-final { scan-tree-dump-not " - " "fre3" { xfail *-*-* } } } */ + +/* { dg-final { scan-tree-dump "return -1;" "optimized" } } */ +/* { dg-final { scan-tree-dump-not " - " "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/neg-cast-3.c b/gcc/testsuite/gcc.dg/tree-ssa/neg-cast-3.c new file mode 100644 index 00000000000..a26a6051bda --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/neg-cast-3.c @@ -0,0 +1,15 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-forwprop2 -fdump-tree-optimized" } */ +/* PR tree-optimization/107137 */ + +unsigned f(_Bool a) +{ + int t = a; + t = -t; + return t; +} + +/* There should be no cast to int at all. */ +/* Forwprop2 does not remove all of the statements. */ +/* { dg-final { scan-tree-dump-not "\\\(int\\\)" "forwprop2" { xfail *-*-* } } } */ +/* { dg-final { scan-tree-dump-not "\\\(int\\\)" "optimized" } } */