From patchwork Fri Apr 28 03:30:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 88443 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp674147vqo; Thu, 27 Apr 2023 20:31:39 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5WpVluTnciDEhuUazPiE3qX71vT4UAAETKzob/20UpxLcmlUeVwuY7hrPKZlHLL/D9A/mP X-Received: by 2002:a17:907:7207:b0:94e:c8c:42ec with SMTP id dr7-20020a170907720700b0094e0c8c42ecmr3639924ejc.20.1682652699039; Thu, 27 Apr 2023 20:31:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682652699; cv=none; d=google.com; s=arc-20160816; b=Fyyp9RIWD+w2sZkTJIE/QtjZMmGrhJ2vxY//Zn7obSoszKU8o6Olhvi74a4Hf49HGk 17dmWfJ7Kipp0UEtXLVITUWq1rW8ho0iUHI2yp/bglcd7ehFN98fBGXsZqmmq0hHfN59 fXrFOCtOgDjWCS3uL7WgyuKI9ovBF6eF9mf5pqPNY2OpVb5yjXEpVx6Zomkd8rqhHRWW c0az21ugNPHqpRKeAT6CFNqU5d4Up19hl7nRo70BNxuR3zhR7FYiL2hiucOMS8jQsaJF YZ42Cs/Jk5tlikxur9842Ad/Z95qKURdHRvlXSpHn6Wq7kOnjvF/wRqeYiFhDtZ3sAaM 9mCg== 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=FSHqyCdYiceHee9Hsj4xfU4aJHlHgMaZ5dF6q8PD3B8=; b=pp1WNnZhepW/oIzV5j+RScoB+FI6cVdBiLvD/q4fbXcPBtlMgcdsq4XQqkq/+VCDk4 p8yjnqQSdmOrhrxfzYEKas0egNYWBt5uZdr6jldJWgjQaTmV5oELMh0nycj12O5hzzRo Zya+89tC6x8Jjfx1brMMx6s1QE4iDwRGoaMQKzDPPf+LN4XaFMXR/bBX/O7zlAgeqGg7 YrVIi6IiyUQrYs7R1WBpy8cql/jYsqu+cjYUGVYGV/80y1MjDHSU/sajwDWpXMWlpxIg ehryQQIF6QWCSJEHVLMRzj1+Xq4qOM+ZI+OMnXY21mzkNUqPFhD033DNeCFfznxeLcf7 VKPg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=iL9kh1qT; 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 sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id cs13-20020a170906dc8d00b009501e0d8869si19049950ejc.59.2023.04.27.20.31.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Apr 2023 20:31:39 -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=iL9kh1qT; 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 D963D38582BD for ; Fri, 28 Apr 2023 03:31:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D963D38582BD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1682652697; bh=FSHqyCdYiceHee9Hsj4xfU4aJHlHgMaZ5dF6q8PD3B8=; h=To:CC:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=iL9kh1qToHzNWI2Abx/ZiY/i76+ZUD3LejTd6sb0emTnTVTNNP5nNiKJjShtR4nst 9RdCmxgclmwdSJsDkGtbObvMChosRT527p7/NSHPPCJLouIFzbeY9lC5T7bCCOu0RV 6nJP+5LSB+MQV4YA0Qbre59YjJctC3Ii56B0Flh8= 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 43A273858D37 for ; Fri, 28 Apr 2023 03:30:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 43A273858D37 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 33S2B45J014173 for ; Thu, 27 Apr 2023 20:30:52 -0700 Received: from dc5-exch02.marvell.com ([199.233.59.182]) by mx0b-0016f401.pphosted.com (PPS) with ESMTPS id 3q781hwj0f-3 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Thu, 27 Apr 2023 20:30:52 -0700 Received: from DC5-EXCH01.marvell.com (10.69.176.38) by DC5-EXCH02.marvell.com (10.69.176.39) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Thu, 27 Apr 2023 20:30:51 -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; Thu, 27 Apr 2023 20:30:50 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id 98A375B692C; Thu, 27 Apr 2023 20:30:50 -0700 (PDT) To: CC: Andrew Pinski Subject: [PATCH] PHIOPT: Move two_value_replacement to match.pd Date: Thu, 27 Apr 2023 20:30:45 -0700 Message-ID: <20230428033045.655785-1-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Proofpoint-GUID: m20nvCdd-jQUTYr56O52mK6eGYtijz3n X-Proofpoint-ORIG-GUID: m20nvCdd-jQUTYr56O52mK6eGYtijz3n 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?1764389236332099150?= X-GMAIL-MSGID: =?utf-8?q?1764389236332099150?= This patch converts two_value_replacement function into a match.pd pattern. It is a direct translation with only one minor change, does not check for the {0,+-1} case as that is handled before in match.pd so there is no reason to do the extra check for it. OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. gcc/ChangeLog: PR tree-optimization/100958 * tree-ssa-phiopt.cc (two_value_replacement): Remove. (pass_phiopt::execute): Don't call two_value_replacement. * match.pd (a !=/== CST1 ? CST2 : CST3): Add pattern to handle what two_value_replacement did. --- gcc/match.pd | 94 ++++++++++++++++++++++++ gcc/tree-ssa-phiopt.cc | 157 +---------------------------------------- 2 files changed, 96 insertions(+), 155 deletions(-) diff --git a/gcc/match.pd b/gcc/match.pd index 31fe5093218..e17597ead26 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -4632,6 +4632,100 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) ) ) ) + +/* Optimize + # x_5 in range [cst1, cst2] where cst2 = cst1 + 1 + x_5 ? cstN ? cst4 : cst3 + # op is == or != and N is 1 or 2 + to r_6 = x_5 + (min (cst3, cst4) - cst1) or + r_6 = (min (cst3, cst4) + cst1) - x_5 depending on op, N and which + of cst3 and cst4 is smaller. + This was originally done by two_value_replacement in phiopt (PR 88676). */ +(for eqne (ne eq) + (simplify + (cond (eqne SSA_NAME@0 INTEGER_CST@1) INTEGER_CST@2 INTEGER_CST@3) + (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) + && INTEGRAL_TYPE_P (type) + && (wi::to_widest (@2) + 1 == wi::to_widest (@3) + || wi::to_widest (@2) == wi::to_widest (@3) + 1)) + (with { + value_range r; + get_range_query (cfun)->range_of_expr (r, @0); + if (r.undefined_p ()) + r.set_varying (TREE_TYPE (@0)); + + wide_int min = r.lower_bound (); + wide_int max = r.upper_bound (); + } + (if (min + 1 == max + && (wi::to_wide (@1) == min + || wi::to_wide (@1) == max)) + (with { + tree arg0 = @2, arg1 = @3; + tree type1; + if ((eqne == EQ_EXPR) ^ (wi::to_wide (@1) == min)) + std::swap (arg0, arg1); + if (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type)) + { + /* Avoid performing the arithmetics in bool type which has different + semantics, otherwise prefer unsigned types from the two with + the same precision. */ + if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE + || !TYPE_UNSIGNED (type)) + type1 = TREE_TYPE (@0); + else + type1 = TREE_TYPE (arg0); + } + else if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type)) + type1 = TREE_TYPE (@0); + else + type1 = type; + min = wide_int::from (min, TYPE_PRECISION (type1), + TYPE_SIGN (TREE_TYPE (@0))); + wide_int a = wide_int::from (wi::to_wide (arg0), TYPE_PRECISION (type1), + TYPE_SIGN (type)); + enum tree_code code; + wi::overflow_type ovf; + if (tree_int_cst_lt (arg0, arg1)) + { + code = PLUS_EXPR; + a -= min; + if (!TYPE_UNSIGNED (type1)) + { + /* lhs is known to be in range [min, min+1] and we want to add a + to it. Check if that operation can overflow for those 2 values + and if yes, force unsigned type. */ + wi::add (min + (wi::neg_p (a) ? 0 : 1), a, SIGNED, &ovf); + if (ovf) + type1 = unsigned_type_for (type1); + } + } + else + { + code = MINUS_EXPR; + a += min; + if (!TYPE_UNSIGNED (type1)) + { + /* lhs is known to be in range [min, min+1] and we want to subtract + it from a. Check if that operation can overflow for those 2 + values and if yes, force unsigned type. */ + wi::sub (a, min + (wi::neg_p (min) ? 0 : 1), SIGNED, &ovf); + if (ovf) + type1 = unsigned_type_for (type1); + } + } + tree arg = wide_int_to_tree (type1, a); + } + (if (code == PLUS_EXPR) + (convert (plus (convert:type1 @0) { arg; })) + (convert (minus { arg; } (convert:type1 @0))) + ) + ) + ) + ) + ) + ) +) #endif (simplify diff --git a/gcc/tree-ssa-phiopt.cc b/gcc/tree-ssa-phiopt.cc index 7fc6ac17b4a..4b43f1abdbc 100644 --- a/gcc/tree-ssa-phiopt.cc +++ b/gcc/tree-ssa-phiopt.cc @@ -373,155 +373,6 @@ factor_out_conditional_conversion (edge e0, edge e1, gphi *phi, return newphi; } -/* Optimize - # x_5 in range [cst1, cst2] where cst2 = cst1 + 1 - if (x_5 op cstN) # where op is == or != and N is 1 or 2 - goto bb3; - else - goto bb4; - bb3: - bb4: - # r_6 = PHI # where cst3 == cst4 + 1 or cst4 == cst3 + 1 - - to r_6 = x_5 + (min (cst3, cst4) - cst1) or - r_6 = (min (cst3, cst4) + cst1) - x_5 depending on op, N and which - of cst3 and cst4 is smaller. */ - -static bool -two_value_replacement (basic_block cond_bb, basic_block middle_bb, - edge e1, gphi *phi, tree arg0, tree arg1) -{ - /* Only look for adjacent integer constants. */ - if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0)) - || !INTEGRAL_TYPE_P (TREE_TYPE (arg1)) - || TREE_CODE (arg0) != INTEGER_CST - || TREE_CODE (arg1) != INTEGER_CST - || (tree_int_cst_lt (arg0, arg1) - ? wi::to_widest (arg0) + 1 != wi::to_widest (arg1) - : wi::to_widest (arg1) + 1 != wi::to_widest (arg0))) - return false; - - if (!empty_block_p (middle_bb)) - return false; - - gcond *stmt = as_a (*gsi_last_bb (cond_bb)); - tree lhs = gimple_cond_lhs (stmt); - tree rhs = gimple_cond_rhs (stmt); - - if (TREE_CODE (lhs) != SSA_NAME - || !INTEGRAL_TYPE_P (TREE_TYPE (lhs)) - || TREE_CODE (rhs) != INTEGER_CST) - return false; - - switch (gimple_cond_code (stmt)) - { - case EQ_EXPR: - case NE_EXPR: - break; - default: - return false; - } - - /* Defer boolean x ? 0 : {1,-1} or x ? {1,-1} : 0 to - match_simplify_replacement. */ - if (TREE_CODE (TREE_TYPE (lhs)) == BOOLEAN_TYPE - && (integer_zerop (arg0) - || integer_zerop (arg1) - || TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE - || (TYPE_PRECISION (TREE_TYPE (arg0)) - <= TYPE_PRECISION (TREE_TYPE (lhs))))) - return false; - - value_range r; - get_range_query (cfun)->range_of_expr (r, lhs); - if (r.undefined_p ()) - r.set_varying (TREE_TYPE (lhs)); - wide_int min = r.lower_bound (); - wide_int max = r.upper_bound (); - - if (min + 1 != max - || (wi::to_wide (rhs) != min - && wi::to_wide (rhs) != max)) - return false; - - /* We need to know which is the true edge and which is the false - edge so that we know when to invert the condition below. */ - edge true_edge, false_edge; - extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge); - if ((gimple_cond_code (stmt) == EQ_EXPR) - ^ (wi::to_wide (rhs) == max) - ^ (e1 == false_edge)) - std::swap (arg0, arg1); - - tree type; - if (TYPE_PRECISION (TREE_TYPE (lhs)) == TYPE_PRECISION (TREE_TYPE (arg0))) - { - /* Avoid performing the arithmetics in bool type which has different - semantics, otherwise prefer unsigned types from the two with - the same precision. */ - if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE - || !TYPE_UNSIGNED (TREE_TYPE (arg0))) - type = TREE_TYPE (lhs); - else - type = TREE_TYPE (arg0); - } - else if (TYPE_PRECISION (TREE_TYPE (lhs)) > TYPE_PRECISION (TREE_TYPE (arg0))) - type = TREE_TYPE (lhs); - else - type = TREE_TYPE (arg0); - - min = wide_int::from (min, TYPE_PRECISION (type), - TYPE_SIGN (TREE_TYPE (lhs))); - wide_int a = wide_int::from (wi::to_wide (arg0), TYPE_PRECISION (type), - TYPE_SIGN (TREE_TYPE (arg0))); - enum tree_code code; - wi::overflow_type ovf; - if (tree_int_cst_lt (arg0, arg1)) - { - code = PLUS_EXPR; - a -= min; - if (!TYPE_UNSIGNED (type)) - { - /* lhs is known to be in range [min, min+1] and we want to add a - to it. Check if that operation can overflow for those 2 values - and if yes, force unsigned type. */ - wi::add (min + (wi::neg_p (a) ? 0 : 1), a, SIGNED, &ovf); - if (ovf) - type = unsigned_type_for (type); - } - } - else - { - code = MINUS_EXPR; - a += min; - if (!TYPE_UNSIGNED (type)) - { - /* lhs is known to be in range [min, min+1] and we want to subtract - it from a. Check if that operation can overflow for those 2 - values and if yes, force unsigned type. */ - wi::sub (a, min + (wi::neg_p (min) ? 0 : 1), SIGNED, &ovf); - if (ovf) - type = unsigned_type_for (type); - } - } - - tree arg = wide_int_to_tree (type, a); - gimple_seq stmts = NULL; - lhs = gimple_convert (&stmts, type, lhs); - tree new_rhs; - if (code == PLUS_EXPR) - new_rhs = gimple_build (&stmts, PLUS_EXPR, type, lhs, arg); - else - new_rhs = gimple_build (&stmts, MINUS_EXPR, type, arg, lhs); - new_rhs = gimple_convert (&stmts, TREE_TYPE (arg0), new_rhs); - gimple_stmt_iterator gsi = gsi_for_stmt (stmt); - gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT); - - replace_phi_edge_with_variable (cond_bb, e1, phi, new_rhs); - - /* Note that we optimized this PHI. */ - return true; -} /* Return TRUE if SEQ/OP pair should be allowed during early phiopt. Currently this is to allow MIN/MAX and ABS/NEGATE and constants. */ @@ -4170,12 +4021,8 @@ pass_phiopt::execute (function *) } /* Do the replacement of conditional if it can be done. */ - if (!early_p - && !diamond_p - && two_value_replacement (bb, bb1, e2, phi, arg0, arg1)) - cfgchanged = true; - else if (match_simplify_replacement (bb, bb1, bb2, e1, e2, phi, - arg0, arg1, early_p, diamond_p)) + if (match_simplify_replacement (bb, bb1, bb2, e1, e2, phi, + arg0, arg1, early_p, diamond_p)) cfgchanged = true; else if (!early_p && !diamond_p