Take the following code: #include <limits.h> void abort (); void f(int i) { if (i>0) abort(); i = -i; if (i<0) return; abort (); } int main(void) { f(INT_MIN); return 0; } This should not abort but does at -O2 -fwrapv and above because VRP is folding the conditional which is not true as -INT_MIN = INT_MIN still. This causes us to mis compile Long.toString in libjava.
Note I should give credit to Andrew Haley who pointed me to the mis compiling of Long.toString.
Created attachment 9291 [details] Treat flag_wrapv and min value in a special way
(In reply to comment #2) > Created an attachment (id=9291) > Treat flag_wrapv and min value in a special way > patch cointains typo-bug. --- gcc-pr22493.patch Sun Jul 17 20:07:13 2005 +++ gcc-pr22493.patch Sun Jul 17 22:33:27 2005 @@ -53,7 +53,7 @@ - max = (vr0.min == TYPE_MIN_VALUE (TREE_TYPE (expr))) - ? TYPE_MAX_VALUE (TREE_TYPE (expr)) - : fold_unary_to_constant (code, TREE_TYPE (expr), vr0.min); -+ type = VR_ANTI_RANGE; ++ vr_type = VR_ANTI_RANGE; + max = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.max); + max = int_const_binop (MINUS_EXPR, max, one, 0); + min = int_const_binop (PLUS_EXPR, min, one, 0);
The attached patch, with the typo fixed and using int_const_binop (PLUS_EXPR, vr0.min, one, 0) fixed the optimization failures of Divide_1 in the testsuite.
Confirmed.
Created attachment 9336 [details] Updated patch This should also fix: /* { dg-do run } */ /* { dg-options "-O1 -ftree-vrp" } */ extern void abort (); extern void exit (int); int f (int a) { if (a != 2) { a = -a; if (a == 2) return 0; return 1; } return 1; } int main (int argc, char *argv[]) { if (f (-2)) abort (); exit (0); } which fails right now.
Patch posted here: <http://gcc.gnu.org/ml/gcc-patches/2005-07/msg01566.html>.
Subject: Bug 22493 CVSROOT: /cvs/gcc Module name: gcc Changes by: phython@gcc.gnu.org 2005-07-28 04:35:02 Modified files: gcc : ChangeLog tree-vrp.c gcc/testsuite : ChangeLog Added files: gcc/testsuite/gcc.c-torture/execute: pr22493-1.c pr22493-1.x vrp-1.c vrp-2.c vrp-3.c Log message: 2005-07-27 James A. Morrison <phython@gcc.gnu.org> PR tree-optimization/22493 * tree-vrp.c (extract_range_from_unary_expr): Deal with -fwrapv and VR_ANTI_RANGEs properly for NEGATE_EXPRs and ABS_EXPRs. Patches: http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&r1=2.9575&r2=2.9576 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/tree-vrp.c.diff?cvsroot=gcc&r1=2.43&r2=2.44 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.5832&r2=1.5833 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.c-torture/execute/pr22493-1.c.diff?cvsroot=gcc&r1=NONE&r2=1.1 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.c-torture/execute/pr22493-1.x.diff?cvsroot=gcc&r1=NONE&r2=1.1 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.c-torture/execute/vrp-1.c.diff?cvsroot=gcc&r1=NONE&r2=1.1 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.c-torture/execute/vrp-2.c.diff?cvsroot=gcc&r1=NONE&r2=1.1 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.c-torture/execute/vrp-3.c.diff?cvsroot=gcc&r1=NONE&r2=1.1
Fixed in the commit above.