From patchwork Wed Oct 7 23:40:16 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kugan Vivekanandarajah X-Patchwork-Id: 54619 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wi0-f199.google.com (mail-wi0-f199.google.com [209.85.212.199]) by patches.linaro.org (Postfix) with ESMTPS id 803DF22FEF for ; Wed, 7 Oct 2015 23:40:44 +0000 (UTC) Received: by wicuu12 with SMTP id uu12sf912371wic.2 for ; Wed, 07 Oct 2015 16:40:43 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:mailing-list:precedence:list-id :list-unsubscribe:list-archive:list-post:list-help:sender :delivered-to:subject:to:references:cc:from:message-id:date :user-agent:mime-version:in-reply-to:content-type:x-original-sender :x-original-authentication-results; bh=ltf8S27vyRvqp2h7Dn9WFsg8mssN/tqxKKeoImW+Kt8=; b=Nz4oRTQ6MAnuThZFz2j34y8Ga3XfHyRSanRxPTZ2AiICajfDoQEZJc8uuWteWNadzG XtxrdkClI/L+8O/PY4WDcSMJS9ok8OSprkt6B+MvXtHNwkX3Oa9JmD+pkoNF1aGz7pPE L/lpfBzFS93j1OdQP5H6PkLvbgpidn4HRmhU8iZWk+dc1uWyFu5GDwCpUKHRSredgXbX kZrkR00GBNI3OS7v+xZ86WadHxtZN8SguuFliWY4MkG2mTWXbE+haSm9hNDRSHPq4TGs s2/r0Dclrxaq25ZzR2KaDL+we6d9XTerDXlWt0r3qp3Xh8DXCykhVtg1a71XXRC/1ugx aCbg== X-Gm-Message-State: ALoCoQnxuZRh826ebciwUuumpyYOo20kilBBqqdXifFAsVYOzqwb3T5bu+NYAQUM/SaTG5+MBpjR X-Received: by 10.112.158.202 with SMTP id ww10mr761933lbb.13.1444261243792; Wed, 07 Oct 2015 16:40:43 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.25.160.13 with SMTP id j13ls123781lfe.67.gmail; Wed, 07 Oct 2015 16:40:43 -0700 (PDT) X-Received: by 10.112.167.8 with SMTP id zk8mr1978414lbb.15.1444261243662; Wed, 07 Oct 2015 16:40:43 -0700 (PDT) Received: from mail-lb0-x22e.google.com (mail-lb0-x22e.google.com. [2a00:1450:4010:c04::22e]) by mx.google.com with ESMTPS id h77si27349272lfi.33.2015.10.07.16.40.43 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 07 Oct 2015 16:40:43 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 2a00:1450:4010:c04::22e as permitted sender) client-ip=2a00:1450:4010:c04::22e; Received: by lbos8 with SMTP id s8so28419638lbo.0 for ; Wed, 07 Oct 2015 16:40:43 -0700 (PDT) X-Received: by 10.112.139.201 with SMTP id ra9mr1973763lbb.29.1444261243493; Wed, 07 Oct 2015 16:40:43 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.59.35 with SMTP id w3csp252540lbq; Wed, 7 Oct 2015 16:40:42 -0700 (PDT) X-Received: by 10.107.170.223 with SMTP id g92mr4815784ioj.79.1444261242000; Wed, 07 Oct 2015 16:40:42 -0700 (PDT) Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id 71si29419552iom.188.2015.10.07.16.40.41 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 07 Oct 2015 16:40:41 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-return-409545-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Received: (qmail 93465 invoked by alias); 7 Oct 2015 23:40:28 -0000 Mailing-List: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: , List-Help: , Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 93450 invoked by uid 89); 7 Oct 2015 23:40:27 -0000 X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.4 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-pa0-f45.google.com Received: from mail-pa0-f45.google.com (HELO mail-pa0-f45.google.com) (209.85.220.45) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Wed, 07 Oct 2015 23:40:25 +0000 Received: by pacfv12 with SMTP id fv12so35618807pac.2 for ; Wed, 07 Oct 2015 16:40:23 -0700 (PDT) X-Received: by 10.68.65.70 with SMTP id v6mr4306948pbs.23.1444261223664; Wed, 07 Oct 2015 16:40:23 -0700 (PDT) Received: from [10.1.1.2] (58-6-183-210.dyn.iinet.net.au. [58.6.183.210]) by smtp.googlemail.com with ESMTPSA id su3sm41509476pbc.83.2015.10.07.16.40.21 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 07 Oct 2015 16:40:22 -0700 (PDT) Subject: Re: [3/7] Optimize ZEXT_EXPR with tree-vrp To: Richard Biener References: <55ECFC2A.7050908@linaro.org> <55ECFD57.5060507@linaro.org> <5614556E.5020908@linaro.org> Cc: "gcc-patches@gcc.gnu.org" From: Kugan Message-ID: <5615AD60.6040904@linaro.org> Date: Thu, 8 Oct 2015 10:40:16 +1100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 MIME-Version: 1.0 In-Reply-To: X-IsSubscribed: yes X-Original-Sender: kugan.vivekanandarajah@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 2a00:1450:4010:c04::22e as permitted sender) smtp.mailfrom=patch+caf_=patchwork-forward=linaro.org@linaro.org; dkim=pass header.i=@gcc.gnu.org X-Google-Group-Id: 836684582541 On 07/10/15 19:20, Richard Biener wrote: > On Wed, Oct 7, 2015 at 1:12 AM, kugan wrote: >> >> Hi Richard, >> >> Thanks for the review. >> >> On 15/09/15 23:08, Richard Biener wrote: >>> >>> On Mon, Sep 7, 2015 at 4:58 AM, Kugan >>> wrote: >>>> >>>> This patch tree-vrp handling and optimization for ZEXT_EXPR. >>> >>> >>> + else if (code == SEXT_EXPR) >>> + { >>> + gcc_assert (range_int_cst_p (&vr1)); >>> + unsigned int prec = tree_to_uhwi (vr1.min); >>> + type = vr0.type; >>> + wide_int tmin, tmax; >>> + wide_int type_min, type_max; >>> + wide_int may_be_nonzero, must_be_nonzero; >>> + >>> + gcc_assert (!TYPE_UNSIGNED (expr_type)); >>> >>> hmm, I don't think we should restrict SEXT_EXPR this way. SEXT_EXPR >>> should operate on both signed and unsigned types and the result type >>> should be the same as the type of operand 0. >>> >>> + type_min = wi::shwi (1 << (prec - 1), >>> + TYPE_PRECISION (TREE_TYPE (vr0.min))); >>> + type_max = wi::shwi (((1 << (prec - 1)) - 1), >>> + TYPE_PRECISION (TREE_TYPE (vr0.max))); >>> >>> there is wi::min_value and max_value for this. >> >> >> As of now, SEXT_EXPR in gimple is of the form: x = y sext 8 and types of all >> the operand and results are of the wider type. Therefore we cant use the >> wi::min_value. Or do you want to convert this precision (in this case 8) to >> a type and use wi::min_value? > > I don't understand - wi::min/max_value get a precision and sign, not a type. > your 1 << (prec - 1) is even wrong for prec > 32 (it's an integer type > expression). > Thus > > type_min = wi::min_value (prec, SIGNED); > type_max = wi::max_value (prec, SIGNED); > Thanks for the comments. Is the attached patch looks better. It is based on the above. I am still assuming the position of sign-bit in SEXT_EXPR will be less than 64bit (for calculating sign_bit in wide_int format). I think this will always be the case but please let me know if this is not OK. Thanks, Kugan > >> Please find the patch that addresses the other comments. >From 963e5ed4576bd7f82e83b21f35c58e9962dbbc74 Mon Sep 17 00:00:00 2001 From: Kugan Vivekanandarajah Date: Mon, 17 Aug 2015 13:45:52 +1000 Subject: [PATCH 3/7] Optimize ZEXT_EXPR with tree-vrp --- gcc/tree-vrp.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/gcc/tree-vrp.c b/gcc/tree-vrp.c index 2cd71a2..ada1c9f 100644 --- a/gcc/tree-vrp.c +++ b/gcc/tree-vrp.c @@ -2317,6 +2317,7 @@ extract_range_from_binary_expr_1 (value_range_t *vr, && code != LSHIFT_EXPR && code != MIN_EXPR && code != MAX_EXPR + && code != SEXT_EXPR && code != BIT_AND_EXPR && code != BIT_IOR_EXPR && code != BIT_XOR_EXPR) @@ -2877,6 +2878,51 @@ extract_range_from_binary_expr_1 (value_range_t *vr, extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1); return; } + else if (code == SEXT_EXPR) + { + gcc_assert (range_int_cst_p (&vr1)); + unsigned int prec = tree_to_uhwi (vr1.min); + type = vr0.type; + wide_int tmin, tmax; + wide_int may_be_nonzero, must_be_nonzero; + + wide_int type_min = wi::min_value (prec, SIGNED); + wide_int type_max = wi::max_value (prec, SIGNED); + type_min = wide_int_to_tree (expr_type, type_min); + type_max = wide_int_to_tree (expr_type, type_max); + wide_int sign_bit = wi::shwi (1ULL << (prec - 1), + TYPE_PRECISION (TREE_TYPE (vr0.min))); + if (zero_nonzero_bits_from_vr (expr_type, &vr0, + &may_be_nonzero, + &must_be_nonzero)) + { + if (wi::bit_and (must_be_nonzero, sign_bit) == sign_bit) + { + /* If to-be-extended sign bit is one. */ + tmin = type_min; + tmax = may_be_nonzero; + } + else if (wi::bit_and (may_be_nonzero, sign_bit) + != sign_bit) + { + /* If to-be-extended sign bit is zero. */ + tmin = must_be_nonzero; + tmax = may_be_nonzero; + } + else + { + tmin = type_min; + tmax = type_max; + } + } + else + { + tmin = type_min; + tmax = type_max; + } + min = wide_int_to_tree (expr_type, tmin); + max = wide_int_to_tree (expr_type, tmax); + } else if (code == RSHIFT_EXPR || code == LSHIFT_EXPR) { @@ -9244,6 +9290,28 @@ simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt) break; } break; + case SEXT_EXPR: + { + unsigned int prec = tree_to_uhwi (op1); + wide_int sign_bit = wi::shwi (1ULL << (prec - 1), + TYPE_PRECISION (TREE_TYPE (vr0.min))); + wide_int mask = wi::shwi (((1ULL << (prec - 1)) - 1), + TYPE_PRECISION (TREE_TYPE (vr0.max))); + mask = wi::bit_not (mask); + if (wi::bit_and (must_be_nonzero0, sign_bit) == sign_bit) + { + /* If to-be-extended sign bit is one. */ + if (wi::bit_and (must_be_nonzero0, mask) == mask) + op = op0; + } + else if (wi::bit_and (may_be_nonzero0, sign_bit) != sign_bit) + { + /* If to-be-extended sign bit is zero. */ + if (wi::bit_and (may_be_nonzero0, mask) == 0) + op = op0; + } + } + break; default: gcc_unreachable (); } @@ -9946,6 +10014,7 @@ simplify_stmt_using_ranges (gimple_stmt_iterator *gsi) case BIT_AND_EXPR: case BIT_IOR_EXPR: + case SEXT_EXPR: /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR if all the bits being cleared are already cleared or all the bits being set are already set. */ -- 1.9.1