From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 16651 invoked by alias); 12 Nov 2007 13:07:50 -0000 Received: (qmail 16642 invoked by uid 22791); 12 Nov 2007 13:07:49 -0000 X-Spam-Check-By: sourceware.org Received: from mx2.suse.de (HELO mx2.suse.de) (195.135.220.15) by sourceware.org (qpsmtpd/0.31) with ESMTP; Mon, 12 Nov 2007 13:07:46 +0000 Received: from Relay1.suse.de (mail2.suse.de [195.135.221.8]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mx2.suse.de (Postfix) with ESMTP id 80B922B090 for ; Mon, 12 Nov 2007 14:07:43 +0100 (CET) Date: Mon, 12 Nov 2007 14:57:00 -0000 From: Richard Guenther To: gcc-patches@gcc.gnu.org Subject: [PATCH] Fix PR34070, wrong folding Message-ID: 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: 2007-11/txt/msg00620.txt.bz2 I'll commit this as soon as bootstrap/testing has finished. Richard. 2007-11-12 Richard Guenther PR middle-end/34070 * fold-const.c (fold_binary): If testing for non-negative operands with tree_expr_nonnegative_warnv_p make sure to use op0 which has all (sign) conversions retained. * gcc.c-torture/execute/pr34070-1.c: New testcase. * gcc.c-torture/execute/pr34070-2.c: Likewise. Index: fold-const.c =================================================================== *** fold-const.c (revision 130096) --- fold-const.c (working copy) *************** fold_binary (enum tree_code code, tree t *** 11202,11208 **** strict_overflow_p = false; if (TREE_CODE (arg1) == LSHIFT_EXPR && (TYPE_UNSIGNED (type) ! || tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))) { tree sval = TREE_OPERAND (arg1, 0); if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0) --- 11202,11208 ---- strict_overflow_p = false; if (TREE_CODE (arg1) == LSHIFT_EXPR && (TYPE_UNSIGNED (type) ! || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p))) { tree sval = TREE_OPERAND (arg1, 0); if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0) *************** fold_binary (enum tree_code code, tree t *** 11320,11326 **** strict_overflow_p = false; if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR) && (TYPE_UNSIGNED (type) ! || tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))) { tree c = arg1; /* Also optimize A % (C << N) where C is a power of 2, --- 11320,11326 ---- strict_overflow_p = false; if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR) && (TYPE_UNSIGNED (type) ! || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p))) { tree c = arg1; /* Also optimize A % (C << N) where C is a power of 2, Index: testsuite/gcc.c-torture/execute/pr34070-1.c =================================================================== *** testsuite/gcc.c-torture/execute/pr34070-1.c (revision 0) --- testsuite/gcc.c-torture/execute/pr34070-1.c (revision 0) *************** *** 0 **** --- 1,13 ---- + extern void abort (void); + + int f(unsigned int x) + { + return ((int)x) % 4; + } + + int main() + { + if (f(-1) != -1) + abort (); + return 0; + } Index: testsuite/gcc.c-torture/execute/pr34070-2.c =================================================================== *** testsuite/gcc.c-torture/execute/pr34070-2.c (revision 0) --- testsuite/gcc.c-torture/execute/pr34070-2.c (revision 0) *************** *** 0 **** --- 1,13 ---- + extern void abort (void); + + int f(unsigned int x, int n) + { + return ((int)x) / (1 << n); + } + + int main() + { + if (f(-1, 1) != 0) + abort (); + return 0; + }