From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 20679 invoked by alias); 7 Jul 2011 13:16:59 -0000 Received: (qmail 20671 invoked by uid 22791); 7 Jul 2011 13:16:59 -0000 X-SWARE-Spam-Status: No, hits=-3.3 required=5.0 tests=AWL,BAYES_00,T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from cantor2.suse.de (HELO mx2.suse.de) (195.135.220.15) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 07 Jul 2011 13:16:42 +0000 Received: from relay1.suse.de (charybdis-ext.suse.de [195.135.221.2]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mx2.suse.de (Postfix) with ESMTP id 2DD3E8C5DF for ; Thu, 7 Jul 2011 15:16:41 +0200 (CEST) Date: Thu, 07 Jul 2011 13:22:00 -0000 From: Richard Guenther To: gcc-patches@gcc.gnu.org Subject: [PATCH] Fix folding of -(unsigned)(a * -b) Message-ID: User-Agent: Alpine 2.00 (LNX 1167 2008-08-23) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII 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 X-SW-Source: 2011-07/txt/msg00477.txt.bz2 Folding of $subject is currently broken (noticed that when playing with types in pointer_int_sum). We happily ignore the fact that the negate operates on an unsigned type and change it to operate on a signed one - which may cause new undefined overflow. Seen with the testcase below which aborts with current trunk. The fix is to not strip sign-changing conversions as already done for ABS_EXPR. Bootstrapped on x86_64-unknown-linux-gnu, testing in progress. Richard. 2011-07-07 Richard Guenther * fold-const.c (fold_unary_loc): Do not strip sign-changes for NEGATE_EXPR. * gcc.dg/ftrapv-3.c: New testcase. Index: gcc/fold-const.c =================================================================== --- gcc/fold-const.c (revision 175962) +++ gcc/fold-const.c (working copy) @@ -7561,7 +7561,7 @@ fold_unary_loc (location_t loc, enum tre if (arg0) { if (CONVERT_EXPR_CODE_P (code) - || code == FLOAT_EXPR || code == ABS_EXPR) + || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR) { /* Don't use STRIP_NOPS, because signedness of argument type matters. */ Index: gcc/testsuite/gcc.dg/ftrapv-3.c =================================================================== --- gcc/testsuite/gcc.dg/ftrapv-3.c (revision 0) +++ gcc/testsuite/gcc.dg/ftrapv-3.c (revision 0) @@ -0,0 +1,16 @@ +/* { dg-do run } */ +/* { dg-options "-ftrapv" } */ + +extern void abort (void); +unsigned long +foo (long i, long j) +{ + /* We may not fold this to (unsigned long)(i * j). */ + return -(unsigned long)(i * -j); +} +int main() +{ + if (foo (-__LONG_MAX__ - 1, -1) != -(unsigned long)(-__LONG_MAX__ - 1)) + abort (); + return 0; +}