From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 54250 invoked by alias); 18 Jun 2015 13:09:24 -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 54231 invoked by uid 89); 18 Jun 2015 13:09:23 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.3 required=5.0 tests=AWL,BAYES_00,KAM_ASCII_DIVIDERS,KAM_LAZY_DOMAIN_SECURITY,RP_MATCHES_RCVD autolearn=no version=3.3.2 X-HELO: mx2.suse.de Received: from cantor2.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; Thu, 18 Jun 2015 13:09:17 +0000 Received: from relay1.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 9CFF2AAB2 for ; Thu, 18 Jun 2015 13:09:14 +0000 (UTC) Date: Thu, 18 Jun 2015 13:15:00 -0000 From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] Backport PR56917 to 4.8 Message-ID: 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-06/txt/msg01275.txt.bz2 I am currently bootstrapping and regtesting the following for backport to the 4.8 branch and the testcase for 4.9 to trunk. Richard. 2015-06-18 Richard Biener Backport from mainline 2014-12-04 Marek Polacek PR middle-end/56917 * fold-const.c (fold_unary_loc): Perform the negation in A's type when transforming ~ (A - 1) or ~ (A + -1) to -A. * g++.dg/other/const4.C: New testcase. Index: gcc/fold-const.c =================================================================== --- gcc/fold-const.c (revision 221345) +++ gcc/fold-const.c (revision 221346) @@ -8324,9 +8324,14 @@ fold_unary_loc (location_t loc, enum tre && integer_onep (TREE_OPERAND (arg0, 1))) || (TREE_CODE (arg0) == PLUS_EXPR && integer_all_onesp (TREE_OPERAND (arg0, 1))))) - return fold_build1_loc (loc, NEGATE_EXPR, type, - fold_convert_loc (loc, type, - TREE_OPERAND (arg0, 0))); + { + /* Perform the negation in ARG0's type and only then convert + to TYPE as to avoid introducing undefined behavior. */ + tree t = fold_build1_loc (loc, NEGATE_EXPR, + TREE_TYPE (TREE_OPERAND (arg0, 0)), + TREE_OPERAND (arg0, 0)); + return fold_convert_loc (loc, type, t); + } /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */ else if (TREE_CODE (arg0) == BIT_XOR_EXPR && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type, Index: gcc/testsuite/g++.dg/other/const4.C =================================================================== *** gcc/testsuite/g++.dg/other/const4.C (revision 0) --- gcc/testsuite/g++.dg/other/const4.C (working copy) *************** *** 0 **** --- 1,10 ---- + // { dg-do compile } + + int lValue; + int main() + { + switch (lValue) + { + case -(int)((2U << (8 * sizeof(int) - 2)) - 1) - 1:; + } + }