From patchwork Thu Nov 10 20:52:39 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dominik Vogt X-Patchwork-Id: 693478 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 3tFFcZ6V0Wz9t1b for ; Fri, 11 Nov 2016 07:53:01 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="yoY3T18o"; 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:date :from:to:cc:subject:reply-to:references:mime-version :content-type:in-reply-to:message-id; q=dns; s=default; b=n4vChV DH7Z/h0WD6ui+MLYpb4v9Odsurl+1MOC2lxf5Q9e3HhyDyfVv8VPAD91O/mjVXeg 88KlDmSqUXY4XIBES41ZtsA4PEd6I3mEDH7u66DD/JO9CmQbXnfIkUHUJb49TJfl x/PJFqdMkp65t6OCQMbpn/ccNHtw4cg2qAX8c= 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:date :from:to:cc:subject:reply-to:references:mime-version :content-type:in-reply-to:message-id; s=default; bh=FSJnetisPT2c TcBwxIi815ym75k=; b=yoY3T18oWf86T7Cm7fEkDdHNAuHwkLbiMsPnOZy4rf4J o9PyY9BVVh/Fk5acxYFKWxO6hgvgrnNQTuaZTJnx6B3oOll3/9z5VNjgnHXAAZF6 R+aMk3wscnGxZI3AUEkgHbVJTToURRmryh28QsXSEsgjImg1i5Ro9YDdNq1nmDs= Received: (qmail 118426 invoked by alias); 10 Nov 2016 20:52:53 -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 118409 invoked by uid 89); 10 Nov 2016 20:52:52 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.1 required=5.0 tests=BAYES_00, KAM_LAZY_DOMAIN_SECURITY, RCVD_IN_DNSWL_LOW, RCVD_IN_SEMBACKSCATTER autolearn=no version=3.3.2 spammy=robin, Robin, massage, patched X-HELO: mx0a-001b2d01.pphosted.com Received: from mx0a-001b2d01.pphosted.com (HELO mx0a-001b2d01.pphosted.com) (148.163.156.1) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 10 Nov 2016 20:52:50 +0000 Received: from pps.filterd (m0098396.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.17/8.16.0.17) with SMTP id uAAKmmVs041437 for ; Thu, 10 Nov 2016 15:52:49 -0500 Received: from e06smtp13.uk.ibm.com (e06smtp13.uk.ibm.com [195.75.94.109]) by mx0a-001b2d01.pphosted.com with ESMTP id 26mschrtsn-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Thu, 10 Nov 2016 15:52:49 -0500 Received: from localhost by e06smtp13.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 10 Nov 2016 20:52:46 -0000 Received: from d06dlp03.portsmouth.uk.ibm.com (9.149.20.15) by e06smtp13.uk.ibm.com (192.168.101.143) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Thu, 10 Nov 2016 20:52:44 -0000 Received: from b06cxnps3074.portsmouth.uk.ibm.com (d06relay09.portsmouth.uk.ibm.com [9.149.109.194]) by d06dlp03.portsmouth.uk.ibm.com (Postfix) with ESMTP id 6C37A1B08061 for ; Thu, 10 Nov 2016 20:54:57 +0000 (GMT) Received: from d06av07.portsmouth.uk.ibm.com (d06av07.portsmouth.uk.ibm.com [9.149.37.248]) by b06cxnps3074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id uAAKqi0343384928 for ; Thu, 10 Nov 2016 20:52:44 GMT Received: from d06av07.portsmouth.uk.ibm.com (localhost [127.0.0.1]) by d06av07.portsmouth.uk.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id uAAKqhdG028630 for ; Thu, 10 Nov 2016 15:52:43 -0500 Received: from oc5510024614.ibm.com (sig-9-145-12-217.uk.ibm.com [9.145.12.217]) by d06av07.portsmouth.uk.ibm.com (8.14.4/8.14.4/NCO v10.0 AVin) with ESMTP id uAAKqhQw028621; Thu, 10 Nov 2016 15:52:43 -0500 Received: by oc5510024614.ibm.com (Postfix, from userid 500) id 4E5C218F60; Thu, 10 Nov 2016 21:52:40 +0100 (CET) Date: Thu, 10 Nov 2016 21:52:39 +0100 From: Dominik Vogt To: Richard Biener Cc: GCC Patches , Ulrich Weigand , Andreas Krebbel Subject: Re: [RFC] Check number of uses in simplify_cond_using_ranges(). Reply-To: vogt@linux.vnet.ibm.com Mail-Followup-To: vogt@linux.vnet.ibm.com, Richard Biener , GCC Patches , Ulrich Weigand , Andreas Krebbel References: <20161103150322.GA3523@linux.vnet.ibm.com> <20161104110822.GA23310@linux.vnet.ibm.com> <20161108143125.GA16341@linux.vnet.ibm.com> <20161109143053.GA21900@linux.vnet.ibm.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.20 (2009-12-10) X-TM-AS-MML: disable X-Content-Scanned: Fidelis XPS MAILER x-cbid: 16111020-0012-0000-0000-0000048907C0 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 16111020-0013-0000-0000-0000162B29D1 Message-Id: <20161110205239.GA9225@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2016-11-10_09:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=0 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1609300000 definitions=main-1611100352 On Wed, Nov 09, 2016 at 03:46:38PM +0100, Richard Biener wrote: > On Wed, Nov 9, 2016 at 3:30 PM, Dominik Vogt wrote: > > Something like the attached patch? Robin and me have spent quite > > some time to figure out the new pattern. Two questions: > > > > 1) In the match expression you cannot just use SSA_NAME@0 because > > then the "case SSA_NAME:" is added to a switch for another > > pattern that already has that label. Thus we made that "proxy" > > predicate "ssa_name_p" that forces the code for the new pattern > > out of the old switch and into a separate code block. We > > couldn't figure out whether this joining of case labels is a > > feature in the matching language. So, is this the right way to > > deal with the conflicting labels? > > No, just do not match SSA_NAME. And instead of > > + (with { gimple *def_stmt = SSA_NAME_DEF_STMT (@0); } > + (if (is_gimple_assign (def_stmt) > + && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))) > > you instead want to change the pattern to > > (simpify > (cmp (convert @0) INTEGER_CST@1) > > @0 will then be your innerop > > note that you can't use get_value_range but you have to use the > get_range_info interface instead. I suppose a helper function > somewhere that computes whether an expression fits a type > would be helpful (see expr_not_equal_to for sth related, > thus expr_fits_type_p (@0, TREE_TYPE (@1))) All right, I think we got that (new patch attached). > Likewise the overflow_infinity checks do not translate to match.pd > (or rahter the range info you get). Can you give us another hint here, please? The overflow check should probably go into expr_fits_type_p, but with only the min and max values from get_range_info, how can the overflow TREE_OVERFLOW_P flag be retrieved from @1, to duplicate the logic from is_{nega,posi}tive_overflow_infinity? Is it availably somewhere, or is it necessary to somehow re-calculate it from the expression? (This is really necessary so that cases like this don't start folding with the patch: --- signed char foo3uu (unsigned char a) { unsigned char d; unsigned long un; d = (a & 63) + 200; un = d; if (un >= 12) ubar(un); return d; } -- ) Ciao Dominik ^_^ ^_^ diff --git a/gcc/fold-const.c b/gcc/fold-const.c index 593ea16..082ad46 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -9100,6 +9100,70 @@ expr_not_equal_to (tree t, const wide_int &w) } } +/* Return true if the value range of T is known not fit into TYPE. */ + +bool +expr_fits_type_p (tree t, tree type) +{ + wide_int min, max; + value_range_type rtype; + signop sign; + signop tsign; + bool has_range = false; + + if (!INTEGRAL_TYPE_P (type)) + return false; + tsign = TYPE_SIGN (type); + sign = TYPE_SIGN (TREE_TYPE (t)); + switch (TREE_CODE (t)) + { + case INTEGER_CST: + min = t; + max = t; + has_range = true; + break; + + case SSA_NAME: + if (!INTEGRAL_TYPE_P (TREE_TYPE (t))) + return false; + rtype = get_range_info (t, &min, &max); + if (rtype == VR_RANGE) + has_range = true; + else if (rtype == VR_ANTI_RANGE) + { + /* As ANTI_RANGEs always wrap around, just check if T's whole type's + value range fits into TYPE. */ + min = TYPE_MIN_VALUE (TREE_TYPE (t)); + max = TYPE_MAX_VALUE (TREE_TYPE (t)); + has_range = true; + } + break; + } + if (has_range) + { + if (sign == tsign) + { + if (wi::le_p (max, TYPE_MAX_VALUE (type), sign) + && wi::ge_p (min, TYPE_MIN_VALUE (type), sign)) + return true; + } + else if (sign == SIGNED && tsign == UNSIGNED) + { + if (wi::ge_p (min, 0, SIGNED) + && wi::le_p (max, TYPE_MAX_VALUE (type), UNSIGNED) + && wi::ge_p (min, TYPE_MIN_VALUE (type), UNSIGNED)) + return true; + } + else if (sign == UNSIGNED && tsign == SIGNED) + { + if (wi::le_p (max, TYPE_MAX_VALUE (type), UNSIGNED)) + return true; + } + } + + return false; +} + /* Fold a binary expression of code CODE and type TYPE with operands OP0 and OP1. LOC is the location of the resulting expression. Return the folded expression if folding is successful. Otherwise, diff --git a/gcc/fold-const.h b/gcc/fold-const.h index ae37142..46b0213 100644 --- a/gcc/fold-const.h +++ b/gcc/fold-const.h @@ -180,6 +180,7 @@ extern bool merge_ranges (int *, tree *, tree *, int, tree, tree, int, extern tree sign_bit_p (tree, const_tree); extern tree exact_inverse (tree, tree); extern bool expr_not_equal_to (tree t, const wide_int &); +extern bool expr_fits_type_p (tree t, tree type); extern tree const_unop (enum tree_code, tree, tree); extern tree const_binop (enum tree_code, tree, tree, tree); extern bool negate_mathfn_p (combined_fn); diff --git a/gcc/match.pd b/gcc/match.pd index 48f7351..7f0fd08 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -3501,3 +3501,31 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) { CONSTRUCTOR_ELT (ctor, idx / k)->value; }) (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / k)->value; } @1 { bitsize_int ((idx % k) * width); }))))))))) + +#if GIMPLE +/* If we have a comparison of an SSA_NAME (OP0) against a constant, + see if OP0 was set by a type conversion where the source of + the conversion is another SSA_NAME with a range that fits + into the range of OP0's type. + + If so, the conversion is redundant as the earlier SSA_NAME can be + used for the comparison directly if we just massage the constant in the + comparison. */ + +(for cmp (eq ne gt ge lt le) + (simplify + (cmp (convert@0 @1) INTEGER_CST@2) + (if (TREE_CODE (@1) == SSA_NAME + && !POINTER_TYPE_P (TREE_TYPE (@1)) + && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (@1) + && desired_pro_or_demotion_p (TREE_TYPE (@1), TREE_TYPE (@0))) + (if (expr_fits_type_p (@1, TREE_TYPE (@0)) + && int_fits_type_p (@2, TREE_TYPE (@1)) + /*!!! overflow conditions*/ + /* If the only use of @1 is the cast to @0, and @0 is also + used in other places, folding would introduce new uses + of the otherwise dead @1 without eliminating @1, so do + not. */ + && (!has_single_use (@1) || has_single_use (@0))) + (cmp { @1; } (convert @2)))))) +#endif diff --git a/gcc/tree-vrp.c b/gcc/tree-vrp.c index 68fe2ac..548b91e 100644 --- a/gcc/tree-vrp.c +++ b/gcc/tree-vrp.c @@ -9590,72 +9590,6 @@ simplify_cond_using_ranges (gcond *stmt) } } - /* If we have a comparison of an SSA_NAME (OP0) against a constant, - see if OP0 was set by a type conversion where the source of - the conversion is another SSA_NAME with a range that fits - into the range of OP0's type. - - If so, the conversion is redundant as the earlier SSA_NAME can be - used for the comparison directly if we just massage the constant in the - comparison. */ - if (TREE_CODE (op0) == SSA_NAME - && TREE_CODE (op1) == INTEGER_CST) - { - gimple *def_stmt = SSA_NAME_DEF_STMT (op0); - tree innerop; - - if (!is_gimple_assign (def_stmt) - || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))) - return false; - - innerop = gimple_assign_rhs1 (def_stmt); - - if (TREE_CODE (innerop) == SSA_NAME - && !POINTER_TYPE_P (TREE_TYPE (innerop)) - && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop) - && desired_pro_or_demotion_p (TREE_TYPE (innerop), TREE_TYPE (op0))) - { - value_range *vr = get_value_range (innerop); - - if (range_int_cst_p (vr) - && range_fits_type_p (vr, - TYPE_PRECISION (TREE_TYPE (op0)), - TYPE_SIGN (TREE_TYPE (op0))) - && int_fits_type_p (op1, TREE_TYPE (innerop)) - /* The range must not have overflowed, or if it did overflow - we must not be wrapping/trapping overflow and optimizing - with strict overflow semantics. */ - && ((!is_negative_overflow_infinity (vr->min) - && !is_positive_overflow_infinity (vr->max)) - || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop)))) - { - /* If the range overflowed and the user has asked for warnings - when strict overflow semantics were used to optimize code, - issue an appropriate warning. */ - if (cond_code != EQ_EXPR && cond_code != NE_EXPR - && (is_negative_overflow_infinity (vr->min) - || is_positive_overflow_infinity (vr->max)) - && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL)) - { - location_t location; - - if (!gimple_has_location (stmt)) - location = input_location; - else - location = gimple_location (stmt); - warning_at (location, OPT_Wstrict_overflow, - "assuming signed overflow does not occur when " - "simplifying conditional"); - } - - tree newconst = fold_convert (TREE_TYPE (innerop), op1); - gimple_cond_set_lhs (stmt, innerop); - gimple_cond_set_rhs (stmt, newconst); - return true; - } - } - } - return false; }