From patchwork Fri Apr 29 12:32:11 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Sandiford X-Patchwork-Id: 616722 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3qxCky11tpz9t3p for ; Fri, 29 Apr 2016 22:32:25 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=MuUJ1WtD; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:subject:date:message-id:mime-version:content-type; q=dns; s= default; b=r1zNTeWyKNfB1OEbT0AinZBeUdsNTqnIqRugcmdR2BOFgHbtPjGYI SXShyRT2SvAwl/QlIF6ljAezQGeKtaK3Di9zEsYNC64gruvUHsoppr5DBY58L46S Krb3x1ZWO6tenE3WUKpY6AM97gAOX2k8GriGpq5rfy8yHjVKl23g1M= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:subject:date:message-id:mime-version:content-type; s= default; bh=CO+PNHtDXvvXqtFKRCgsumWdSYs=; b=MuUJ1WtDlvuOkxKWJsOS AnAg+ue+2KuMsYl62tuekFT/47TFLlYyQ3jR7sLV4tEsYGp8OJ3MqB2U4GFRVK6X HXKjs1hmoGKPLt2cK9fEDhlzY8tVMgm7Wu14BtfJr1fv+47bBbz3HKE6IPme3l/7 INn0k+7vc/aYBIZBJyhubbg= Received: (qmail 85249 invoked by alias); 29 Apr 2016 12:32:17 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk 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 85239 invoked by uid 89); 29 Apr 2016 12:32:16 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.1 required=5.0 tests=BAYES_00, KAM_ASCII_DIVIDERS, RP_MATCHES_RCVD, SPF_PASS autolearn=ham version=3.3.2 spammy=ts, T's, t's, Ts X-HELO: foss.arm.com Received: from foss.arm.com (HELO foss.arm.com) (217.140.101.70) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 29 Apr 2016 12:32:15 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 8E7113A for ; Fri, 29 Apr 2016 05:32:15 -0700 (PDT) Received: from localhost (e105548-lin.manchester.arm.com [10.45.32.67]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id F09443F218 for ; Fri, 29 Apr 2016 05:32:12 -0700 (PDT) From: Richard Sandiford To: gcc-patches@gcc.gnu.org Mail-Followup-To: gcc-patches@gcc.gnu.org, richard.sandiford@arm.com Subject: Add a wi::to_wide helper function Date: Fri, 29 Apr 2016 13:32:11 +0100 Message-ID: <87h9ekfvec.fsf@e105548-lin.cambridge.arm.com> User-Agent: Gnus/5.130012 (Ma Gnus v0.12) Emacs/24.3 (gnu/linux) MIME-Version: 1.0 As Richard says, we ought to have a convenient way of converting an INTEGER_CST to a wide_int of a particular precision without having to extract the sign of the INTEGER_CST's type each time. This patch adds a wi::to_wide helper for that, alongside the existing wi::to_offset and wi::to_widest. Tested on x86_64-linux-gnu and aarch64-linux-gnu. OK to install? Thanks, Richard gcc/ * tree.h (wi::to_wide): New function. * expr.c (expand_expr_real_1): Use wi::to_wide. * fold-const.c (int_const_binop_1): Likewise. (extract_muldiv_1): Likewise. gcc/c-family/ * c-common.c (shorten_compare): Use wi::to_wide. Index: gcc/tree.h =================================================================== --- gcc/tree.h +++ gcc/tree.h @@ -5211,6 +5211,8 @@ namespace wi to_widest (const_tree); generic_wide_int > to_offset (const_tree); + + wide_int to_wide (const_tree, unsigned int); } inline unsigned int @@ -5240,6 +5242,16 @@ wi::to_offset (const_tree t) return t; } +/* Convert INTEGER_CST T to a wide_int of precision PREC, extending or + truncating as necessary. When extending, use sign extension if T's + type is signed and zero extension if T's type is unsigned. */ + +inline wide_int +wi::to_wide (const_tree t, unsigned int prec) +{ + return wide_int::from (t, prec, TYPE_SIGN (TREE_TYPE (t))); +} + template inline wi::extended_tree ::extended_tree (const_tree t) : m_t (t) Index: gcc/expr.c =================================================================== --- gcc/expr.c +++ gcc/expr.c @@ -9729,10 +9729,9 @@ expand_expr_real_1 (tree exp, rtx target, machine_mode tmode, GET_MODE_PRECISION (TYPE_MODE (type)), we need to extend from the former to the latter according to the signedness of the type. */ - temp = immed_wide_int_const (wide_int::from + temp = immed_wide_int_const (wi::to_wide (exp, - GET_MODE_PRECISION (TYPE_MODE (type)), - TYPE_SIGN (type)), + GET_MODE_PRECISION (TYPE_MODE (type))), TYPE_MODE (type)); return temp; Index: gcc/fold-const.c =================================================================== --- gcc/fold-const.c +++ gcc/fold-const.c @@ -963,8 +963,7 @@ int_const_binop_1 (enum tree_code code, const_tree arg1, const_tree parg2, signop sign = TYPE_SIGN (type); bool overflow = false; - wide_int arg2 = wide_int::from (parg2, TYPE_PRECISION (type), - TYPE_SIGN (TREE_TYPE (parg2))); + wide_int arg2 = wi::to_wide (parg2, TYPE_PRECISION (type)); switch (code) { @@ -6394,10 +6393,8 @@ extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type, bool overflow_mul_p; signop sign = TYPE_SIGN (ctype); unsigned prec = TYPE_PRECISION (ctype); - wide_int mul = wi::mul (wide_int::from (op1, prec, - TYPE_SIGN (TREE_TYPE (op1))), - wide_int::from (c, prec, - TYPE_SIGN (TREE_TYPE (c))), + wide_int mul = wi::mul (wi::to_wide (op1, prec), + wi::to_wide (c, prec), sign, &overflow_mul_p); overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1); if (overflow_mul_p Index: gcc/c-family/c-common.c =================================================================== --- gcc/c-family/c-common.c +++ gcc/c-family/c-common.c @@ -4012,10 +4012,9 @@ shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr, /* Convert primop1 to target type, but do not introduce additional overflow. We know primop1 is an int_cst. */ primop1 = force_fit_type (*restype_ptr, - wide_int::from - (primop1, - TYPE_PRECISION (*restype_ptr), - TYPE_SIGN (TREE_TYPE (primop1))), + wi::to_wide + (primop1, + TYPE_PRECISION (*restype_ptr)), 0, TREE_OVERFLOW (primop1)); } if (type != *restype_ptr)