From patchwork Fri Apr 28 03:25:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 88442 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp672435vqo; Thu, 27 Apr 2023 20:26:05 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7EkRYDoBO+BdQsnRduUh45rIzM4Y5lT5KrELPOEOOKaHT9px595oKx+x9dOr05MlVaKPo1 X-Received: by 2002:a17:907:2d8f:b0:957:278c:fb27 with SMTP id gt15-20020a1709072d8f00b00957278cfb27mr4206725ejc.21.1682652365576; Thu, 27 Apr 2023 20:26:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682652365; cv=none; d=google.com; s=arc-20160816; b=ihIKT+qWZf4QycXmhsM5nTG7kme1DUaCedGf97A2BTUlJ3/KJUDNH21Q+8MiSPtGAY HyFPQWqkz1Vi4jhHwFqjQFVEpPVSK9fd+x9DR1leG1MBh0iWWYNznjb3UBr+8PsFO4G7 5rbi3RrdU4EbQTtvNc00IeK0JpR4s95EJykSaIHV16+rqB2xZzUZ1RXQpu++G1GT0zcP 9uajfKD8V8kk31gdbiRO+GJAhe+hgWHhM23IKVTVbt5A+ddCyU7x7g4MTdRtZPIXXOnV lwkQUPhCkz3EOM6aZ4zOQvTCuuUdUTpdGb1h/WOhGdxMXhpU7nrnc2YvHsN8WP1s5BnU +wuQ== 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=7lfHcQpRpn+EwIEBYPENvCRoctlZcxuxxKTeQ+4lcM8=; b=B4Q27O49O4wG7PB5qfImfaYiyPP3KVIlmp31PdWB0VQFyDtCEWYx9zSs1UaOhLbSty MtIxdJPY4wmKH6hoBMIMPiCSB75dSUONGeq5httTe+9bBZ26XffdGPLWX8VBC6/e0YKj XzdGHoBx6WdOCtEze+Pnov+CFCMKfxEphf+2ZRgrTxaG1NicEKp4CqJkTbiJNqunJcNO PjiCFD1f8eIRfpXHoa8IxrsvuJzRcs8g9YEtLEW7/zITIlWTG0K4rkwu7lLgRiiyKnu3 cSdmSuIBJacLa+CPg0SQ0Z2nhrfqWPFbGhcKH8zqR/AcHad2FdlEACVvmNciWHLWz5Ru q1Ew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=SfrMzhhk; 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 bm4-20020a0564020b0400b005047e14ecc4si14861891edb.224.2023.04.27.20.26.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Apr 2023 20:26:05 -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=SfrMzhhk; 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 3C8B9385843A for ; Fri, 28 Apr 2023 03:26:04 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3C8B9385843A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1682652364; bh=7lfHcQpRpn+EwIEBYPENvCRoctlZcxuxxKTeQ+4lcM8=; h=To:CC:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=SfrMzhhk8ZCNBDIhHFAh3oZCdmDZr3WKSX2NiJhw9xsTfGseQ4st37Z5R4cCiY18I 2U7mWPKqgfh4/CxvfAiFzLNF37KIhlR9SnKqotuUV//9KRIxge8s96Q3AeaekkhlUZ tmBhYATYsB6drhWp83NkmbQG5mtUjgANduEpPWw0= 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 D05BD3858D37 for ; Fri, 28 Apr 2023 03:25:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D05BD3858D37 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 33S2ATeW013128 for ; Thu, 27 Apr 2023 20:25:19 -0700 Received: from dc5-exch02.marvell.com ([199.233.59.182]) by mx0b-0016f401.pphosted.com (PPS) with ESMTPS id 3q781hwhgt-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Thu, 27 Apr 2023 20:25:19 -0700 Received: from DC5-EXCH02.marvell.com (10.69.176.39) by DC5-EXCH02.marvell.com (10.69.176.39) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Thu, 27 Apr 2023 20:25:16 -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, 27 Apr 2023 20:25:16 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id 04CE55B6930; Thu, 27 Apr 2023 20:25:15 -0700 (PDT) To: CC: Andrew Pinski Subject: [PATCHv2] MATCH: Factor out code that for min max detection with constants Date: Thu, 27 Apr 2023 20:25:06 -0700 Message-ID: <20230428032506.655667-1-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Proofpoint-GUID: 6sT_UJisxdSoD1uOCRb6bfIQOT61uLjf X-Proofpoint-ORIG-GUID: 6sT_UJisxdSoD1uOCRb6bfIQOT61uLjf 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-04-28_02,2023-04-27_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, 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?1764388886644397944?= X-GMAIL-MSGID: =?utf-8?q?1764388886644397944?= This factors out some of the code from the min/max detection from match.pd into a function so it can be reused in other places. This is mainly used to detect the conversions of >= to > which causes the integer values to be changed by one. Changes since v1: * factor out the checks for INTEGER_CSTs so it is more obvious. OK? Bootstrapped and tested on x86_64-linux-gnu. gcc/ChangeLog: * match.pd: Factor out the deciding the min/max from the "(cond (cmp (convert1? x) c1) (convert2? x) c2)" pattern to ... * fold-const.cc (minmax_from_comparison): this new function. * fold-const.h (minmax_from_comparison): New prototype. --- gcc/fold-const.cc | 44 ++++++++++++++++++++++++++++++++++++++++++++ gcc/fold-const.h | 3 +++ gcc/match.pd | 29 +---------------------------- 3 files changed, 48 insertions(+), 28 deletions(-) diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc index 3b397ae2941..7d2352dbcdd 100644 --- a/gcc/fold-const.cc +++ b/gcc/fold-const.cc @@ -150,6 +150,50 @@ static tree fold_convert_const (enum tree_code, tree, tree); static tree fold_view_convert_expr (tree, tree); static tree fold_negate_expr (location_t, tree); +/* This is a helper function to detect min/max for some operands of COND_EXPR. + The form is "(EXP0 CMP EXP1) ? EXP2 : EXP3". */ +tree_code +minmax_from_comparison (tree_code cmp, tree exp0, tree exp1, tree exp2, tree exp3) +{ + enum tree_code code = ERROR_MARK; + + if (HONOR_NANS (exp0) || HONOR_SIGNED_ZEROS (exp0)) + return ERROR_MARK; + + if (!operand_equal_p (exp0, exp2)) + return ERROR_MARK; + + if (TREE_CODE (exp3) == INTEGER_CST && TREE_CODE (exp1) == INTEGER_CST) + { + if (wi::to_widest (exp1) == (wi::to_widest (exp3) - 1)) + { + /* X <= Y - 1 equals to X < Y. */ + if (cmp == LE_EXPR) + code = LT_EXPR; + /* X > Y - 1 equals to X >= Y. */ + if (cmp == GT_EXPR) + code = GE_EXPR; + } + if (wi::to_widest (exp1) == (wi::to_widest (exp3) + 1)) + { + /* X < Y + 1 equals to X <= Y. */ + if (cmp == LT_EXPR) + code = LE_EXPR; + /* X >= Y + 1 equals to X > Y. */ + if (cmp == GE_EXPR) + code = GT_EXPR; + } + } + if (code != ERROR_MARK + || operand_equal_p (exp1, exp3)) + { + if (cmp == LT_EXPR || cmp == LE_EXPR) + code = MIN_EXPR; + if (cmp == GT_EXPR || cmp == GE_EXPR) + code = MAX_EXPR; + } + return code; +} /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION. Otherwise, return LOC. */ diff --git a/gcc/fold-const.h b/gcc/fold-const.h index 56ecaa87fc6..b828badc42f 100644 --- a/gcc/fold-const.h +++ b/gcc/fold-const.h @@ -246,6 +246,9 @@ extern tree fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE #define fold_build_pointer_plus_hwi(p,o) \ fold_build_pointer_plus_hwi_loc (UNKNOWN_LOCATION, p, o) +extern tree_code minmax_from_comparison (tree_code, tree, tree, + tree, tree); + /* In gimple-fold.cc. */ extern void clear_type_padding_in_mask (tree, unsigned char *); extern bool clear_padding_type_may_have_padding_p (tree); diff --git a/gcc/match.pd b/gcc/match.pd index c4320781f5b..fad337a9697 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -4677,34 +4677,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type))))) { if (cmp != EQ_EXPR) - { - if (wi::to_widest (@3) == (wi::to_widest (@2) - 1)) - { - /* X <= Y - 1 equals to X < Y. */ - if (cmp == LE_EXPR) - code = LT_EXPR; - /* X > Y - 1 equals to X >= Y. */ - if (cmp == GT_EXPR) - code = GE_EXPR; - } - if (wi::to_widest (@3) == (wi::to_widest (@2) + 1)) - { - /* X < Y + 1 equals to X <= Y. */ - if (cmp == LT_EXPR) - code = LE_EXPR; - /* X >= Y + 1 equals to X > Y. */ - if (cmp == GE_EXPR) - code = GT_EXPR; - } - if (code != ERROR_MARK - || wi::to_widest (@2) == wi::to_widest (@3)) - { - if (cmp == LT_EXPR || cmp == LE_EXPR) - code = MIN_EXPR; - if (cmp == GT_EXPR || cmp == GE_EXPR) - code = MAX_EXPR; - } - } + code = minmax_from_comparison (cmp, @1, @3, @1, @2); /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */ else if (int_fits_type_p (@3, from_type)) code = EQ_EXPR;