From patchwork Mon Apr 24 21:30:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 87171 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp3016826vqo; Mon, 24 Apr 2023 14:34:15 -0700 (PDT) X-Google-Smtp-Source: AKy350ZvocbsRLuDrisJ2vsDqCPvWlt5rEiQL6RBvIDc+W4UQljqb1CE4w2W9mVLqnTGclKw9nU+ X-Received: by 2002:aa7:db85:0:b0:506:983f:1f61 with SMTP id u5-20020aa7db85000000b00506983f1f61mr11445743edt.3.1682372055065; Mon, 24 Apr 2023 14:34:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682372055; cv=none; d=google.com; s=arc-20160816; b=LOKRLDfEyVrEgFRHWzk2T7t0Ir3/zVSb9kAJvUk6ZnJbTrKjGBuGjxR7+7hHNRX96H Bk8uw41S6s5QOBVaeOyVHAiM9GAjzKwDQKvOAMKxTtkFLVqwFkPmlIUfhrgKyEq9XGkm qr0ftczQGVxKmnZ3JAjeSqmxxnx74JkckPTACY0nJAhCOmlVEnxcqZF6/z+gIT6O9BmR t3CZEiuAGwdKLw3omi3G5TCgZt82E/Adtbq+KkAL+Cy+KHVA/Qr069rAjyYlo1bLi7cL PbY1zpZGs93YM7sbMgCKsZR2Sfuh04Wo3xGxDyNVkN8B+pUi5bQOmmrgc7TNsyLJepUc 8z6w== 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:references:in-reply-to :message-id:date:subject:cc:to:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=z8504nwQnTFU43pLDIaRcZ8HHbxZLfh/WpXZQd8HX1o=; b=K2o3RW5STVwfCI0ZxUFtEc3Z96pQHC/hHjiw/2CMdWRdrPPZKZAxiT1xCUGyTOn33q hJklK6UEM51UuJmxoTYu2UgCXaN86q6eB9vXUsAVJ408uc8P4CrClByLFGSVPBZO4m8n h/IoNOPWIq01bs7yHBDvYLC9uTKUU1TCRtXCFbOLg6Buk90TZUu7pjKqZi3FZgc/7tAZ DeRpCz3gDhzAeCJnPO0HvzDp7cQfpCZs5HDB12GKmO0xhZ+LYpv0ThLQ3Y9MBvvQlAII ZAwfJafYMybuEKOdohRmw80yI3Ra4lbeFS3avGc/T+1oB0SfiR2crxNFnIltFF0oJAUW +7Dg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=VcagIox+; 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 ba12-20020a0564021acc00b004fd298d2f99si8662302edb.492.2023.04.24.14.34.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Apr 2023 14:34:15 -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=VcagIox+; 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 1E1CB3881D18 for ; Mon, 24 Apr 2023 21:32:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1E1CB3881D18 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1682371943; bh=z8504nwQnTFU43pLDIaRcZ8HHbxZLfh/WpXZQd8HX1o=; h=To:CC:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=VcagIox+vVA6e+8DmDP/QJslNR9x/B9GkxTEYK4W1NqlNYJysxujyPBdS1wHbnUY4 KXrUUAqMmxnprjnEb9uO5aI1DbQdIMSa8wEdPek9SCiyyz4zz68w+d31kBohoTCT3K j1XRd5UAmdfo1XSYQFDUVRyJHcjOeuRsYKsW4oSY= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-0016f401.pphosted.com (mx0a-0016f401.pphosted.com [67.231.148.174]) by sourceware.org (Postfix) with ESMTPS id 916BA3858C53 for ; Mon, 24 Apr 2023 21:30:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 916BA3858C53 Received: from pps.filterd (m0045849.ppops.net [127.0.0.1]) by mx0a-0016f401.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 33OGXia0028418 for ; Mon, 24 Apr 2023 14:30:23 -0700 Received: from dc5-exch01.marvell.com ([199.233.59.181]) by mx0a-0016f401.pphosted.com (PPS) with ESMTPS id 3q5nfb3jvs-3 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Mon, 24 Apr 2023 14:30:23 -0700 Received: from DC5-EXCH01.marvell.com (10.69.176.38) by DC5-EXCH01.marvell.com (10.69.176.38) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Mon, 24 Apr 2023 14:30:21 -0700 Received: from maili.marvell.com (10.69.176.80) by DC5-EXCH01.marvell.com (10.69.176.38) with Microsoft SMTP Server id 15.0.1497.48 via Frontend Transport; Mon, 24 Apr 2023 14:30:21 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id A19D33F70B5; Mon, 24 Apr 2023 14:30:21 -0700 (PDT) To: CC: Andrew Pinski Subject: [PATCH 6/7] MATCH: Factor out code that for min max detection with constants Date: Mon, 24 Apr 2023 14:30:10 -0700 Message-ID: <20230424213011.528181-7-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230424213011.528181-1-apinski@marvell.com> References: <20230424213011.528181-1-apinski@marvell.com> MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: Hb50ApMdqxH0VqNvu_vZmfv_vtZCAbcJ X-Proofpoint-GUID: Hb50ApMdqxH0VqNvu_vZmfv_vtZCAbcJ 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-24_11,2023-04-21_01,2023-02-09_01 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_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?1764094960218202254?= X-GMAIL-MSGID: =?utf-8?q?1764094960218202254?= 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. 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 | 43 +++++++++++++++++++++++++++++++++++++++++++ gcc/fold-const.h | 3 +++ gcc/match.pd | 29 +---------------------------- 3 files changed, 47 insertions(+), 28 deletions(-) diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc index 3b397ae2941..b8add24f874 100644 --- a/gcc/fold-const.cc +++ b/gcc/fold-const.cc @@ -150,6 +150,49 @@ 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 (exp1) == INTEGER_CST && TREE_CODE (exp1) == INTEGER_CST + && 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 (TREE_CODE (exp3) == INTEGER_CST && TREE_CODE (exp1) == INTEGER_CST + && 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 e89ba57e30b..6d3aaf45a93 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;