From patchwork Mon Jul 7 06:55:20 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kugan Vivekanandarajah X-Patchwork-Id: 33128 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-qa0-f71.google.com (mail-qa0-f71.google.com [209.85.216.71]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 1BBD520C88 for ; Mon, 7 Jul 2014 06:55:47 +0000 (UTC) Received: by mail-qa0-f71.google.com with SMTP id m5sf12743521qaj.10 for ; Sun, 06 Jul 2014 23:55:47 -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:message-id:date:from:user-agent:mime-version:to:cc :subject:references:in-reply-to:x-original-sender :x-original-authentication-results:content-type; bh=87iHr28pPgT9e/qwsmSlzKJbKjrFc4LmQaT43exdi0I=; b=AGHdZfYFMxUv+LZqENS9oLzTDSSO7TIsBCA939QCg2TNuUUxpgwz6E6jELr56UzIyr vPT+VVcLqnCyMDTKAsABXT4XtC6fnmcMREbpd6ur7O9Opf0UlTWb5V5QJFOg+zwQMoHH dfuCj0GrIjY8Y4T15j7LauOxYBDJZukfOBPHICt0x2qyWL/lC5E0bvd+FJcCU4wQp2P1 Jzyl8usGsTbhYzWywLbK+hAhSHzS9oEOXYgTna08K1jOYPz51kFBOeAzwkv285Qnennc L0rdTre2+FQj1e9zmkysopzNvuDWBAzj35+NoZwfHWyjl62447WEdiqKHHtipvFH9Sfu Y84w== X-Gm-Message-State: ALoCoQnB1eAODpVJD75QyboPJkjYMtuHT4z261VbhiEWTh7+adSuuP0qlSHfhGWAC+08BWwNiJrN X-Received: by 10.224.103.135 with SMTP id k7mr13075645qao.6.1404716147348; Sun, 06 Jul 2014 23:55:47 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.107.68 with SMTP id g62ls681645qgf.75.gmail; Sun, 06 Jul 2014 23:55:47 -0700 (PDT) X-Received: by 10.220.136.8 with SMTP id p8mr2420105vct.42.1404716147240; Sun, 06 Jul 2014 23:55:47 -0700 (PDT) Received: from mail-ve0-x22a.google.com (mail-ve0-x22a.google.com [2607:f8b0:400c:c01::22a]) by mx.google.com with ESMTPS id ya3si18446705vec.105.2014.07.06.23.55.47 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sun, 06 Jul 2014 23:55:47 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 2607:f8b0:400c:c01::22a as permitted sender) client-ip=2607:f8b0:400c:c01::22a; Received: by mail-ve0-f170.google.com with SMTP id i13so3664087veh.1 for ; Sun, 06 Jul 2014 23:55:47 -0700 (PDT) X-Received: by 10.220.166.9 with SMTP id k9mr25871535vcy.20.1404716147126; Sun, 06 Jul 2014 23:55:47 -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.221.37.5 with SMTP id tc5csp644048vcb; Sun, 6 Jul 2014 23:55:46 -0700 (PDT) X-Received: by 10.66.142.73 with SMTP id ru9mr27224313pab.41.1404716145822; Sun, 06 Jul 2014 23:55:45 -0700 (PDT) Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id lp7si40364898pab.189.2014.07.06.23.55.45 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 06 Jul 2014 23:55:45 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-return-371946-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Received: (qmail 14660 invoked by alias); 7 Jul 2014 06:55:33 -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 14644 invoked by uid 89); 7 Jul 2014 06:55:32 -0000 X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.3 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-pa0-f46.google.com Received: from mail-pa0-f46.google.com (HELO mail-pa0-f46.google.com) (209.85.220.46) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Mon, 07 Jul 2014 06:55:28 +0000 Received: by mail-pa0-f46.google.com with SMTP id eu11so4886298pac.5 for ; Sun, 06 Jul 2014 23:55:26 -0700 (PDT) X-Received: by 10.66.66.14 with SMTP id b14mr6053512pat.9.1404716126697; Sun, 06 Jul 2014 23:55:26 -0700 (PDT) Received: from [10.1.1.4] (58-6-183-210.dyn.iinet.net.au. [58.6.183.210]) by mx.google.com with ESMTPSA id oc3sm18931619pdb.45.2014.07.06.23.55.24 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sun, 06 Jul 2014 23:55:25 -0700 (PDT) Message-ID: <53BA4458.30804@linaro.org> Date: Mon, 07 Jul 2014 16:55:20 +1000 From: Kugan User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 MIME-Version: 1.0 To: Jakub Jelinek CC: "gcc-patches@gcc.gnu.org" Subject: Re: [PATCH 2/2] Enable elimination of zext/sext References: <53A9658F.2070304@linaro.org> <53A966BF.30806@linaro.org> <20140624122101.GX31640@tucnak.redhat.com> <53AA8501.809@linaro.org> <20140625083618.GZ31640@tucnak.redhat.com> In-Reply-To: <20140625083618.GZ31640@tucnak.redhat.com> 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 2607:f8b0:400c:c01::22a as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org; dkim=pass header.i=@gcc.gnu.org X-Google-Group-Id: 836684582541 > For -fwrapv I don't see why you'd get into trouble ever, the VRP computation > should be well aware of the -fwrapv semantics and the value ranges should > reflect that. > > For -fno-strict-overflow, I have no idea since it is very weirdly defined. > > In any case, for your example above, the loop is always well defined, > because for char/short a++ is performed as: > a = (short) ((int) a + 1) > So, if the patch turns it into infinite loop, with -Os -fno-strict-overflow > or -Os, it is simply a problem with the patch. VR [1, 32768] looks correct, > a++ is performed only if a is >= 0, therefore before addition [0, 32767]. > But from VR [1, 32768] you can't optimize away the sign extension, make sure > you don't have there off-by-one? > > It would be nice if the patch contained some testcases, it is easy > to construct testcases where you have arbitrary VRs on some SSA_NAMEs, > you just need something to stick the VR on, so you can do something like: > type foo (type a) > { > if (a < VR_min + 1 || a > VR_max + 1) return; // If VR_min is type minimum or VR_max type maximum this needs to be adjusted of course. > a = a + 1; > // now you can try some cast that your optimization would try to optimize > return a; > } > Or void bar (type a) { a = (a & mask) + bias; (or similarly) } > Make sure to cover the boundary cases, where VR minimum or maximum still > allow optimizing away zero and/or sign extensions, and another case where > they are +- 1 and already don't allow it. Hi Jakub, For -fwrapv, it is due to how PROMOTE_MODE is defined in arm back-end. In the test-case, a function (which has signed char return type) returns -1 in one of the paths. ARM PROMOTE_MODE changes that to 255 and relies on zero/sign extension generated by RTL again for the correct value. I saw some other targets also defining similar think. I am therefore skipping removing zero/sign extension if the ssa variable can be set to negative integer constants. As for the -fno-strict-overflow case, if the variables overflows, in VRP dumps, I see +INF(OVF), but the value range stored in ssa has TYPE_MAX. We therefore should limit the comparison to (TYPE_MIN < VR_MIN && VR_MAX < TYPE_MAX) instead of (TYPE_MIN <= VR_MIN && VR_MAX <= TYPE_MAX) when checking to be sure that this is not the overflowing case. Attached patch changes this. I have bootstrapped on x86_64-unknown-linux-gnu and regression tested for x86_64-unknown-linux-gnu, arm-none-linux-gnueabi (using qemu), aarch64_be-none-elf (Foundation model), aarch64-none-elf --with-abi=ilp32 (Foundation model) and s390x-ibm-linux (64bit, using qemu) with no new regression. Is this OK? Thanks, Kugan gcc/ 2014-07-07 Kugan Vivekanandarajah * calls.c (precompute_arguments): Check is_promoted_for_type and set the promoted mode. (is_promoted_for_type): New function. (expand_expr_real_1): Check is_promoted_for_type and set the promoted mode. * expr.h (is_promoted_for_type): New function definition. * cfgexpand.c (expand_gimple_stmt_1): Call emit_move_insn if SUBREG is promoted with SRP_SIGNED_AND_UNSIGNED. gcc/testsuite 2014-07-07 Kugan Vivekanandarajah * gcc.dg/zero_sign_ext_test.c: New test. diff --git a/gcc/calls.c b/gcc/calls.c index a3e6faa..eac512f 100644 --- a/gcc/calls.c +++ b/gcc/calls.c @@ -1484,7 +1484,10 @@ precompute_arguments (int num_actuals, struct arg_data *args) args[i].initial_value = gen_lowpart_SUBREG (mode, args[i].value); SUBREG_PROMOTED_VAR_P (args[i].initial_value) = 1; - SUBREG_PROMOTED_SET (args[i].initial_value, args[i].unsignedp); + if (is_promoted_for_type (args[i].tree_value, mode, !args[i].unsignedp)) + SUBREG_PROMOTED_SET (args[i].initial_value, SRP_SIGNED_AND_UNSIGNED); + else + SUBREG_PROMOTED_SET (args[i].initial_value, args[i].unsignedp); } } } diff --git a/gcc/cfgexpand.c b/gcc/cfgexpand.c index b7a34a2..ac6776d 100644 --- a/gcc/cfgexpand.c +++ b/gcc/cfgexpand.c @@ -3309,7 +3309,13 @@ expand_gimple_stmt_1 (gimple stmt) GET_MODE (target), temp, unsignedp); } - convert_move (SUBREG_REG (target), temp, unsignedp); + if ((SUBREG_PROMOTED_GET (target) == SRP_SIGNED_AND_UNSIGNED) + && (GET_CODE (temp) == SUBREG) + && (GET_MODE (target) == GET_MODE (temp)) + && (GET_MODE (SUBREG_REG (target)) == GET_MODE (SUBREG_REG (temp)))) + emit_move_insn (SUBREG_REG (target), SUBREG_REG (temp)); + else + convert_move (SUBREG_REG (target), temp, unsignedp); } else if (nontemporal && emit_storent_insn (target, temp)) ; diff --git a/gcc/expr.c b/gcc/expr.c index 10f4a96..68708c1 100644 --- a/gcc/expr.c +++ b/gcc/expr.c @@ -68,6 +68,7 @@ along with GCC; see the file COPYING3. If not see #include "tree-ssa-address.h" #include "cfgexpand.h" #include "builtins.h" +#include "tree-ssa.h" #ifndef STACK_PUSH_CODE #ifdef STACK_GROWS_DOWNWARD @@ -9210,6 +9211,88 @@ expand_expr_real_2 (sepops ops, rtx target, enum machine_mode tmode, } #undef REDUCE_BIT_FIELD +/* Return TRUE if value in SSA is already zero/sign extended for lhs type + (type here is the combination of LHS_MODE and LHS_UNS) using value range + information stored. Return FALSE otherwise. */ +bool +is_promoted_for_type (tree ssa, enum machine_mode lhs_mode, bool lhs_uns) +{ + wide_int type_min, type_max; + wide_int min, max, limit; + unsigned int prec; + tree lhs_type; + bool rhs_uns; + gimple stmt; + + if (ssa == NULL_TREE + || TREE_CODE (ssa) != SSA_NAME + || !INTEGRAL_TYPE_P (TREE_TYPE (ssa))) + return false; + + /* Return FALSE if value_range is not recorded for SSA. */ + if (get_range_info (ssa, &min, &max) != VR_RANGE) + return false; + stmt = SSA_NAME_DEF_STMT (ssa); + + /* In some architectures, negative integer constants are truncated and + sign changed with target defined PROMOTE_MODE macro. This will impact + the value range seen here and produce wrong code if zero/sign extensions + are eliminated. Therefore, return false if this SSA can have negative + integers. */ + if (is_gimple_assign (stmt) + && (TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_unary)) + { + tree rhs1 = gimple_assign_rhs1 (stmt); + if (TREE_CODE (rhs1) == INTEGER_CST + && !TYPE_UNSIGNED (TREE_TYPE (ssa)) + && tree_int_cst_compare (rhs1, integer_zero_node) == -1) + return false; + } + else if (gimple_code (stmt) == GIMPLE_PHI) + { + unsigned int i; + for (i = 0; i < gimple_phi_num_args (stmt); ++i) + { + tree arg = gimple_phi_arg_def (stmt, i); + if (TREE_CODE (arg) == INTEGER_CST + && !TYPE_UNSIGNED (TREE_TYPE (ssa)) + && tree_int_cst_compare (arg, integer_zero_node) == -1) + return false; + } + } + + lhs_type = lang_hooks.types.type_for_mode (lhs_mode, lhs_uns); + rhs_uns = TYPE_UNSIGNED (TREE_TYPE (ssa)); + prec = min.get_precision (); + + /* Signed maximum value. */ + limit = wide_int::from (TYPE_MAX_VALUE (TREE_TYPE (ssa)), prec, SIGNED); + + /* Signedness of LHS and RHS differs but values in range. */ + if ((rhs_uns != lhs_uns) + && ((!lhs_uns && !wi::neg_p (min, TYPE_SIGN (lhs_type))) + || (lhs_uns && (wi::cmp (max, limit, TYPE_SIGN (TREE_TYPE (ssa))) == -1)))) + lhs_uns = !lhs_uns; + + /* Signedness of LHS and RHS should match. */ + if (rhs_uns != lhs_uns) + return false; + + type_min = wide_int::from (TYPE_MIN_VALUE (lhs_type), prec, + TYPE_SIGN (TREE_TYPE (ssa))); + type_max = wide_int::from (TYPE_MAX_VALUE (lhs_type), prec, + TYPE_SIGN (TREE_TYPE (ssa))); + + /* Check if values lies in-between the type range. */ + if ((wi::neg_p (max, TYPE_SIGN (TREE_TYPE (ssa))) + || (wi::cmp (max, type_max, TYPE_SIGN (TREE_TYPE (ssa))) == -1)) + && (!wi::neg_p (min, TYPE_SIGN (TREE_TYPE (ssa))) + || (wi::cmp (min, 0, TYPE_SIGN (TREE_TYPE (ssa))) == 0) + || (wi::cmp (type_min, min, TYPE_SIGN (TREE_TYPE (ssa))) == -1))) + return true; + + return false; +} /* Return TRUE if expression STMT is suitable for replacement. Never consider memory loads as replaceable, because those don't ever lead @@ -9513,7 +9596,10 @@ expand_expr_real_1 (tree exp, rtx target, enum machine_mode tmode, temp = gen_lowpart_SUBREG (mode, decl_rtl); SUBREG_PROMOTED_VAR_P (temp) = 1; - SUBREG_PROMOTED_SET (temp, unsignedp); + if (is_promoted_for_type (ssa_name, mode, !unsignedp)) + SUBREG_PROMOTED_SET (temp, SRP_SIGNED_AND_UNSIGNED); + else + SUBREG_PROMOTED_SET (temp, unsignedp); return temp; } diff --git a/gcc/expr.h b/gcc/expr.h index 6a1d3ab..e99d000 100644 --- a/gcc/expr.h +++ b/gcc/expr.h @@ -440,6 +440,7 @@ extern rtx expand_expr_real_1 (tree, rtx, enum machine_mode, enum expand_modifier, rtx *, bool); extern rtx expand_expr_real_2 (sepops, rtx, enum machine_mode, enum expand_modifier); +extern bool is_promoted_for_type (tree, enum machine_mode, bool); /* Generate code for computing expression EXP. An rtx for the computed value is returned. The value is never null. diff --git a/gcc/testsuite/gcc.dg/zero_sign_ext_test.c b/gcc/testsuite/gcc.dg/zero_sign_ext_test.c index e69de29..ef37612 100644 --- a/gcc/testsuite/gcc.dg/zero_sign_ext_test.c +++ b/gcc/testsuite/gcc.dg/zero_sign_ext_test.c @@ -0,0 +1,135 @@ +extern void abort (void); + +/* { dg-options "-O2" } */ +/* { dg-do run } */ + +#define TYPE_MAX(type, sign) \ + ((!sign) ? ((1 << (sizeof (type) * 8 - 1)) - 1) : \ + ((1 << (sizeof (type) * 8)) - 1)) +#define TYPE_MIN(type, sign) \ + ((!sign) ? -(1 << (sizeof (type) * 8 - 1)) : 0) + +#define TEST_FN(NAME, ARG_TYPE, RET_TYPE, CAST_TYPE, VAL, VR_MIN, VR_MAX)\ + __attribute__((noinline, noclone)) RET_TYPE \ + NAME (ARG_TYPE arg){ \ + RET_TYPE ret = VAL; \ + if (arg + 1 < VR_MIN || arg + 1 > VR_MAX) return ret; \ + /* Value Range of arg at this point will be [VR_min, VR_max]. */\ + arg = arg + VAL; \ + ret = (CAST_TYPE)arg; \ + return arg; \ + } + +/* Signed to signed conversion with value in-range. */ +TEST_FN (foo1, short, short, char, 1, TYPE_MIN (char, 0), TYPE_MAX (char, 0)); +TEST_FN (foo2, short, short, char, 1, TYPE_MIN (char, 0) + 1,\ + TYPE_MAX (char, 0) - 1); + +/* Signed to signed conversion with value not in-range. */ +TEST_FN (foo3, short, short, char, -1, TYPE_MIN (short, 0) + 1, 100); +TEST_FN (foo4, short, short, char, 1, 12, TYPE_MAX (short, 0) + 1); + +/* Unsigned to unsigned conversion with value in-range. */ +TEST_FN (foo5, unsigned short, unsigned short, unsigned char, 1,\ + TYPE_MIN (char, 1) + 1, TYPE_MAX (char, 1) - 1); +TEST_FN (foo6, unsigned short, unsigned short, unsigned char, 1,\ + TYPE_MIN (char, 1), TYPE_MAX (char, 1)); + +/* Unsigned to unsigned conversion with value not in-range. */ +TEST_FN (foo7, unsigned short, unsigned short, unsigned char, 1,\ + TYPE_MIN (short, 1) + 1, TYPE_MAX (short, 1) - 1); +TEST_FN (foo8, unsigned short, unsigned short, unsigned char, 1,\ + TYPE_MIN (short, 1), TYPE_MAX (short, 1)); + +/* Signed to unsigned conversion with value range positive. */ +TEST_FN (foo9, short, short, unsigned char, -1, 1,\ + TYPE_MAX (char, 1) - 1); +TEST_FN (foo10, short, short, unsigned char, 1, 0,\ + TYPE_MAX (char, 1)); + +/* Signed to unsigned conversion with value range negative. */ +TEST_FN (foo11, short, short, unsigned char, 1,\ + TYPE_MIN (char, 0) + 1, TYPE_MAX (char, 0) - 1); +TEST_FN (foo12, short, short, unsigned char, 1,\ + TYPE_MIN (char, 0), TYPE_MAX (char, 0)); + +/* Unsigned to Signed conversion with value range in signed equiv range */ +TEST_FN (foo13, unsigned short, unsigned short, char, 1,\ + TYPE_MIN (char, 1) + 1, TYPE_MAX (char, 0) - 1); +TEST_FN (foo14, unsigned short, unsigned short, char, 1,\ + TYPE_MIN (char, 1), TYPE_MAX (char, 0)); + +/* Unsigned to Signed conversion with value range not-in signed range */ +TEST_FN (foo15, unsigned short, unsigned short, char, 1,\ + TYPE_MIN (char, 1) + 1, TYPE_MAX (char, 1) - 1); +TEST_FN (foo16, unsigned short, unsigned short, char, 1,\ + TYPE_MIN (char, 1), TYPE_MAX (char, 1)); + +int main () +{ + /* Signed to signed conversion with value in-range. */ + /* arg + 1 */ + if (foo1 (-32) != -31) + abort (); + /* arg + 1 */ + if (foo2 (32) != 33) + abort (); + + /* Signed to signed conversion with value not in-range. */ + /* arg - 1 */ + if (foo3 (-512) != -513) + abort (); + /* arg + 1 */ + if (foo4 (512) != 513) + abort (); + + /* Unsigned to unsigned conversion with value in-range. */ + /* arg + 1 */ + if (foo5 (64) != 65) + abort (); + /* arg + 1 */ + if (foo6 (64) != 65) + abort (); + + /* Unigned to unsigned conversion with value not in-range. */ + /* arg + 1 */ + if (foo7 (512) != 513) + abort (); + /* arg + 1 */ + if (foo8 (512) != 513) + abort (); + + /* Signed to unsigned conversion with value range positive. */ + /* arg - 1 */ + if (foo9 (2) != 1) + abort (); + /* arg + 1 */ + if (foo10 (2) != 3) + abort (); + + /* Signed to unsigned conversion with value range negative. */ + /* arg + 1 */ + if (foo11 (-125) != -124) + abort (); + /* arg + 1 */ + if (foo12 (-125) != -124) + abort (); + + /* Unsigned to Signed conversion with value range in signed equiv range */ + /* arg + 1 */ + if (foo13 (125) != 126) + abort (); + /* arg + 1 */ + if (foo14 (125) != 126) + abort (); + + /* Unsigned to Signed conversion with value range not-in signed range */ + /* arg + 1 */ + if (foo15 (250) != 251) + abort (); + /* arg + 1 */ + if (foo16 (250) != 251) + abort (); + + return 0; +}