From patchwork Mon May 8 05:26:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 90936 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1926660vqo; Sun, 7 May 2023 22:27:44 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ53oHi4l2501WXshdaJUoYBmUSw5CBWKM//kOypVRyaHb4IGr0tEVMCdt6jt0c1xYbv1mwj X-Received: by 2002:a17:907:7212:b0:966:61b3:f630 with SMTP id dr18-20020a170907721200b0096661b3f630mr2177097ejc.9.1683523664362; Sun, 07 May 2023 22:27:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683523664; cv=none; d=google.com; s=arc-20160816; b=q7bL1as/RWaddG0YJEMvzOzG/vOUAkPFA+BCfuTYjZipYiw6SkrlLJ2qBXj5sokH46 pHWUa8JbxDisntiPBUH0bQrlJ9+sSE4fSxbvfc6wI2nVCS0asNnmWiW+5F1ikmmk1Fde 1N7fDFh+r41qgE/40xELwmLKqsMQtMNO+s1SurI+H5TkvbOIJobmhqX7xkoOwMgk/FLj v7Z/xywx/VEgzxVxtF2ZaTf5IdSdSSSBp2ZrRdKNcJQnjPdFJ569MOZXPKp36to7ClpV BhDBXMvMMk0ApJl9Bbe42s1uoJ41C1wnxQvUs5nAQJiSgU+faEJQAhPQYfk2NzY5jHU8 VIJw== 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=YmVQrQBPp68TkHOYEpfoqmKXMS/AnfQ3EfA7XbdH7F0=; b=c63mWzJS+pSKwLh/UY0kiu+kNT5zVkbeEav8hdLqmrNOaAeQeRJUBdiN2ftR0hy0BF /pTYFym+lMwj8jK544TRD3LapkkCYkLpENtfIBYi4KPyGbfxYhBKoh47Klobok69UrZr 5wTF40OkR1u2aKo7HkCsCpE44hAPA7Fi/MTvQvRb+HGRHUxDqpqco3Y5x1UbkzkfGFm9 VYfmpB4gQO+1gdn561295jYSDK8AtJ7ZAj+6L++IQmEM31CRS8jt3yH3pyYkpHD/wdgS b4gLryfidaa8cD2dI+MDJrcS8NfTfcgnAQJeqDSdiSdCstuuMbVqxfVaPlzzSR+JylLw irSQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=K6L2bGR+; 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 az7-20020a170907904700b0094eda184e61si615460ejc.186.2023.05.07.22.27.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 07 May 2023 22:27:44 -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=K6L2bGR+; 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 E42413857705 for ; Mon, 8 May 2023 05:27:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E42413857705 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1683523661; bh=YmVQrQBPp68TkHOYEpfoqmKXMS/AnfQ3EfA7XbdH7F0=; h=To:CC:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=K6L2bGR+rlny4U2dFXRjO2BRQnIjFfHn0m38aBltJi9HPcUjkS6EjZeHT415Dqbuq /XdMwwHkr21loS0YIzrK0KPjvHcFbFbEEPbpeVoRl58j6HVxo/wOyh1mXYfYu6Wrqt xlVZrKJFN2SFBF4YEmopBZa26hV5wqrEQrmPArkI= 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 50A643858D32 for ; Mon, 8 May 2023 05:26:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 50A643858D32 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 3483uDGx025172 for ; Sun, 7 May 2023 22:26:57 -0700 Received: from dc5-exch01.marvell.com ([199.233.59.181]) by mx0b-0016f401.pphosted.com (PPS) with ESMTPS id 3qdpdpkhhy-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Sun, 07 May 2023 22:26:57 -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; Sun, 7 May 2023 22:26:55 -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; Sun, 7 May 2023 22:26:54 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id 25D405B6ADB; Sun, 7 May 2023 22:26:53 -0700 (PDT) To: CC: Andrew Pinski Subject: [PATCH] Add a != MIN/MAX_VALUE_CST ? CST-+1 : a to minmax_from_comparison Date: Sun, 7 May 2023 22:26:25 -0700 Message-ID: <20230508052625.1185434-1-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Proofpoint-GUID: smUXSFLobxjjBIdPbFHtCFyIMO4WHIvg X-Proofpoint-ORIG-GUID: smUXSFLobxjjBIdPbFHtCFyIMO4WHIvg X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-05-08_02,2023-05-05_01,2023-02-09_01 X-Spam-Status: No, score=-14.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, PLING_QUERY, 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-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: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765302509792327927?= X-GMAIL-MSGID: =?utf-8?q?1765302509792327927?= This patch adds the support for match that was implemented for PR 87913 in phiopt. It implements it by adding support to minmax_from_comparison for the check. It uses the range information if available which allows to produce MIN/MAX expression when comparing against the lower/upper bound of the range instead of lower/upper of the type. minmax-20.c is the new testcase which tests the ranges part. OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. gcc/ChangeLog: * fold-const.cc (minmax_from_comparison): Add support for NE_EXPR. * match.pd ((cond (cmp (convert1? x) c1) (convert2? x) c2) pattern): Add ne as a possible cmp. ((a CMP b) ? minmax : minmax pattern): Likewise. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/minmax-20.c: New test. --- gcc/fold-const.cc | 26 +++++++++++++++++++++++ gcc/match.pd | 4 ++-- gcc/testsuite/gcc.dg/tree-ssa/minmax-20.c | 12 +++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/minmax-20.c diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc index db54bfc5662..d90671b9975 100644 --- a/gcc/fold-const.cc +++ b/gcc/fold-const.cc @@ -173,6 +173,19 @@ minmax_from_comparison (tree_code cmp, tree exp0, tree exp1, tree exp2, tree exp /* X > Y - 1 equals to X >= Y. */ if (cmp == GT_EXPR) code = GE_EXPR; + /* a != MIN_RANGE ? a : MIN_RANGE+1 -> MAX_EXPR+1, a> */ + if (cmp == NE_EXPR && TREE_CODE (exp0) == SSA_NAME) + { + value_range r; + get_range_query (cfun)->range_of_expr (r, exp0); + if (r.undefined_p ()) + r.set_varying (TREE_TYPE (exp0)); + + widest_int min = widest_int::from (r.lower_bound (), + TYPE_SIGN (TREE_TYPE (exp0))); + if (min == wi::to_widest (exp1)) + code = MAX_EXPR; + } } if (wi::to_widest (exp1) == (wi::to_widest (exp3) + 1)) { @@ -182,6 +195,19 @@ minmax_from_comparison (tree_code cmp, tree exp0, tree exp1, tree exp2, tree exp /* X >= Y + 1 equals to X > Y. */ if (cmp == GE_EXPR) code = GT_EXPR; + /* a != MAX_RANGE ? a : MAX_RANGE-1 -> MIN_EXPR-1, a> */ + if (cmp == NE_EXPR && TREE_CODE (exp0) == SSA_NAME) + { + value_range r; + get_range_query (cfun)->range_of_expr (r, exp0); + if (r.undefined_p ()) + r.set_varying (TREE_TYPE (exp0)); + + widest_int max = widest_int::from (r.upper_bound (), + TYPE_SIGN (TREE_TYPE (exp0))); + if (max == wi::to_widest (exp1)) + code = MIN_EXPR; + } } } if (code != ERROR_MARK diff --git a/gcc/match.pd b/gcc/match.pd index a55ede838cd..95f7e9a6abc 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -4751,7 +4751,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) Case 2) (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */ -(for cmp (lt le gt ge eq) +(for cmp (lt le gt ge eq ne) (simplify (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2) (with @@ -4942,7 +4942,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* Optimize (a CMP b) ? minmax : minmax to minmax, c> */ (for minmax (min max) - (for cmp (lt le gt ge) + (for cmp (lt le gt ge ne) (simplify (cond (cmp @1 @3) (minmax:c @1 @4) (minmax:c @2 @4)) (with diff --git a/gcc/testsuite/gcc.dg/tree-ssa/minmax-20.c b/gcc/testsuite/gcc.dg/tree-ssa/minmax-20.c new file mode 100644 index 00000000000..481c375f5f9 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/minmax-20.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-phiopt2" } */ + +int f(int num) +{ + if (num < 3) __builtin_unreachable(); + return num != 3 ? num : 4; +} + +/* In phiopt2 with the range information, this should be turned into + a MAX_EXPR. */ +/* { dg-final { scan-tree-dump-times "MAX_EXPR" 1 "phiopt2" } } */