From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 26631 invoked by alias); 27 Oct 2015 14:28:01 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 26038 invoked by uid 89); 27 Oct 2015 14:28:01 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-3.0 required=5.0 tests=AWL,BAYES_00,RP_MATCHES_RCVD,SPF_PASS autolearn=ham version=3.3.2 X-HELO: mx2.suse.de Received: from mx2.suse.de (HELO mx2.suse.de) (195.135.220.15) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (CAMELLIA256-SHA encrypted) ESMTPS; Tue, 27 Oct 2015 14:28:00 +0000 Received: from relay1.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 79FC4AAC1 for ; Tue, 27 Oct 2015 14:28:00 +0000 (UTC) Date: Tue, 27 Oct 2015 14:31:00 -0000 From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: Re: [PATCH] Fix PR68067 In-Reply-To: Message-ID: References: User-Agent: Alpine 2.11 (LSU 23 2013-08-11) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-SW-Source: 2015-10/txt/msg02909.txt.bz2 On Tue, 27 Oct 2015, Richard Biener wrote: > > The following patch adjusts negate_expr_p to account for the fact > that we can't generally change a - (b - c) to (c - b) + a because > -INF - 0 is ok while 0 - -INF not. Similarly for a - (b + c). > While creating testcases I noticed that MULT_EXPR handling is bogus > as well as with -INF/2 * 2 neither operand can be negated safely. > > I believe the division case is also still wrong but I refrained > from touching it with this patch. Here is the division part. Bootstrap / regtest running on x86_64-unknown-linux-gnu. A related testcase went in with r202204. Richard. 2015-10-27 Richard Biener * fold-const.c (negate_expr_p): Adjust the division case to properly avoid introducing undefined overflow. (fold_negate_expr): Likewise. Index: gcc/fold-const.c =================================================================== --- gcc/fold-const.c (revision 229404) +++ gcc/fold-const.c (working copy) @@ -475,29 +488,23 @@ negate_expr_p (tree t) case TRUNC_DIV_EXPR: case ROUND_DIV_EXPR: case EXACT_DIV_EXPR: - /* In general we can't negate A / B, because if A is INT_MIN and - B is 1, we may turn this into INT_MIN / -1 which is undefined - and actually traps on some architectures. But if overflow is - undefined, we can negate, because - (INT_MIN / 1) is an - overflow. */ if (INTEGRAL_TYPE_P (TREE_TYPE (t))) { - if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))) - break; - /* If overflow is undefined then we have to be careful because - we ask whether it's ok to associate the negate with the - division which is not ok for example for - -((a - b) / c) where (-(a - b)) / c may invoke undefined - overflow because of negating INT_MIN. So do not use - negate_expr_p here but open-code the two important cases. */ - if (TREE_CODE (TREE_OPERAND (t, 0)) == NEGATE_EXPR - || (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST - && may_negate_without_overflow_p (TREE_OPERAND (t, 0)))) + if (negate_expr_p (TREE_OPERAND (t, 0))) return true; + + /* In general we can't negate B in A / B, because if A is INT_MIN and + B is 1, we may turn this into INT_MIN / -1 which is undefined + and actually traps on some architectures. */ + if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (t)) + || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST + && ! integer_onep (TREE_OPERAND (t, 1)))) + return negate_expr_p (TREE_OPERAND (t, 1)); } - else if (negate_expr_p (TREE_OPERAND (t, 0))) - return true; - return negate_expr_p (TREE_OPERAND (t, 1)); + else + return (negate_expr_p (TREE_OPERAND (t, 0)) + || negate_expr_p (TREE_OPERAND (t, 1))); + break; case NOP_EXPR: /* Negate -((double)float) as (double)(-float). */ @@ -667,40 +681,22 @@ fold_negate_expr (location_t loc, tree t case TRUNC_DIV_EXPR: case ROUND_DIV_EXPR: case EXACT_DIV_EXPR: - /* In general we can't negate A / B, because if A is INT_MIN and + /* In general we can't negate B in A / B, because if A is INT_MIN and B is 1, we may turn this into INT_MIN / -1 which is undefined - and actually traps on some architectures. But if overflow is - undefined, we can negate, because - (INT_MIN / 1) is an - overflow. */ - if (!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type)) - { - const char * const warnmsg = G_("assuming signed overflow does not " - "occur when negating a division"); - tem = TREE_OPERAND (t, 1); - if (negate_expr_p (tem)) - { - if (INTEGRAL_TYPE_P (type) - && (TREE_CODE (tem) != INTEGER_CST - || integer_onep (tem))) - fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC); - return fold_build2_loc (loc, TREE_CODE (t), type, - TREE_OPERAND (t, 0), negate_expr (tem)); - } - /* If overflow is undefined then we have to be careful because - we ask whether it's ok to associate the negate with the - division which is not ok for example for - -((a - b) / c) where (-(a - b)) / c may invoke undefined - overflow because of negating INT_MIN. So do not use - negate_expr_p here but open-code the two important cases. */ - tem = TREE_OPERAND (t, 0); - if ((INTEGRAL_TYPE_P (type) - && (TREE_CODE (tem) == NEGATE_EXPR - || (TREE_CODE (tem) == INTEGER_CST - && may_negate_without_overflow_p (tem)))) - || !INTEGRAL_TYPE_P (type)) - return fold_build2_loc (loc, TREE_CODE (t), type, - negate_expr (tem), TREE_OPERAND (t, 1)); - } + and actually traps on some architectures. */ + if ((! INTEGRAL_TYPE_P (type) + || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t)) + || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST + && ! integer_onep (TREE_OPERAND (t, 1)))) + && negate_expr_p (TREE_OPERAND (t, 1))) + return fold_build2_loc (loc, TREE_CODE (t), type, + TREE_OPERAND (t, 0), + negate_expr (TREE_OPERAND (t, 1))); + + if (negate_expr_p (TREE_OPERAND (t, 0))) + return fold_build2_loc (loc, TREE_CODE (t), type, + negate_expr (TREE_OPERAND (t, 0)), + TREE_OPERAND (t, 1)); break; case NOP_EXPR: