The following fixes folding of x + CST != CST with -fwrapv. Even with -fwrapv overflow is signalled by int_const_binop with TREE_OVERFLOW but of course we cannot do any undefined overflow optimizations when overflow wraps.
bootstrapped and tested on x86_64-unknown-linux-gnu, applied. Richard. 2014-10-28 Richard Biener <rguent...@suse.de> PR middle-end/63665 * fold-const.c (fold_comparison): Properly guard simplifying against INT_MAX/INT_MIN with !TYPE_OVERFLOW_WRAPS. * gcc.dg/pr63665.c: New testcase. Index: gcc/fold-const.c =================================================================== --- gcc/fold-const.c (revision 216771) +++ gcc/fold-const.c (working copy) @@ -8751,7 +8770,8 @@ fold_comparison (location_t loc, enum tr /* If the constant operation overflowed this can be simplified as a comparison against INT_MAX/INT_MIN. */ - if (TREE_OVERFLOW (new_const)) + if (TREE_OVERFLOW (new_const) + && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))) { int const1_sgn = tree_int_cst_sgn (const1); enum tree_code code2 = code; Index: gcc/testsuite/gcc.dg/pr63665.c =================================================================== --- gcc/testsuite/gcc.dg/pr63665.c (revision 0) +++ gcc/testsuite/gcc.dg/pr63665.c (working copy) @@ -0,0 +1,18 @@ +/* { dg-do run } */ +/* { dg-require-effective-target int32plus } */ +/* { dg-options "-O -fno-tree-ccp -fno-tree-fre -fno-tree-copy-prop -fwrapv" } */ + +static inline int +test5 (int x) +{ + int y = 0x80000000; + return x + y; +} + +int +main () +{ + if (test5 (0x80000000) != 0) + __builtin_abort (); + return 0; +}