From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 2032 invoked by alias); 4 Jan 2007 09:16:47 -0000 Received: (qmail 1998 invoked by uid 48); 4 Jan 2007 09:16:37 -0000 Date: Thu, 04 Jan 2007 09:16:00 -0000 Subject: [Bug c/30364] New: Wrong variable ranges due to constant folding X-Bugzilla-Reason: CC Message-ID: Reply-To: gcc-bugzilla@gcc.gnu.org To: gcc-bugs@gcc.gnu.org From: "guillaume dot melquiond at ens-lyon dot fr" Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org X-SW-Source: 2007-01/txt/msg00161.txt.bz2 Testcase: (compiled with -O2 at least) int f(int a, int b) { if (a > 0x7FFFFFF0) return 0; if (b > 0x7FFFFFF0) return 0; int c = (a - 20) + (b - 20); return c > 0x7FFFFFF0; } GCC 4.1.2 and 4.3.0 (snapshot from 2006-12-17) optimizes the whole function to a single "return 0;". This would be correct if the function was actually written with "c = a + b - 40" under a non-overflow assumption. GCC could indeed deduce that c is no bigger than 0x7FFFFFFF - 40. But as the function was originally written, this property does not hold any longer. For example, a = 0x7FFFFFF0 and b = 41 will not cause any overflow during computations, and the last conditional shall hence evaluate to true. The problem is that GCC performs VRP with C language semantic (undefined behavior on overflow) on code that is no longer the input as written by the user; so this semantic is not valid at that point. The user input should not have undergone a transformation based on associativity. Tested with Debian packages. GCC 3.3.6, 3.4.6, and 4.0.4 generate correct code. GCC 4.1.2 and 4.3.0 generates wrong code. As the expression "a + b - 40" is generated early, I suppose any GCC with VRP would produce wrong code for this testcase. -- Summary: Wrong variable ranges due to constant folding Product: gcc Version: 4.1.2 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: guillaume dot melquiond at ens-lyon dot fr http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30364