GCC 4.4.7 - 4.10.0 for x86_64 miscompiles the following code. $ cat test.c int main (void) { long a = 0; long b = 0x7FFFFFFFFFFFFFFFL; int t = (a - 2) > (b - 1); if (t != 0) { __builtin_abort(); }; return 0; } $ x86_64-unknown-linux-gnu-gcc-4.10.0 test.c -O2 $ ./a.out Aborted (core dumped) $ x86_64-unknown-linux-gnu-gcc-4.10.0 -v Using built-in specs. COLLECT_GCC=x86_64-unknown-linux-gnu-gcc-4.10.0 COLLECT_LTO_WRAPPER=/usr/local/x86_64-tools/gcc-4.10.0/libexec/gcc/x86_64-unknown-linux-gnu/4.10.0/lto-wrapper Target: x86_64-unknown-linux-gnu Configured with: /home/hassy/gcc/configure --prefix=/usr/local/x86_64-tools/gcc-4.10.0/ --with-gmp=/usr/local/gmp-5.1.1/ --with-mpfr=/usr/local/mpfr-3.1.2/ --with-mpc=/usr/local/mpc-1.0.1/ --disable-multilib --disable-nls --enable-languages=c Thread model: posix gcc version 4.10.0 20140502 (experimental) (GCC)
Confirmed. Fold introduces signed overflow: long int a = 0; long int b = 9223372036854775807; int t = b + 1 < a;
Fails also with gcc 4.3.6, works with gcc 4.2.4 and 4.1.2.
I believe this started with addition of this folding in r117931, but didn't verify it.
According to my bisection it did start with r117931.
Mine then.
/* Put the constant on the side where it doesn't overflow and is of lower absolute value than before. */ cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1) ? MINUS_EXPR : PLUS_EXPR, const2, const1); if (!TREE_OVERFLOW (cst) && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)) const2 is -1 here and cst is 1. So that means just checking for "lower absolute value" is wrong. A sign-change is obviously not ok either.
Author: rguenth Date: Wed May 28 12:44:11 2014 New Revision: 211018 URL: http://gcc.gnu.org/viewcvs?rev=211018&root=gcc&view=rev Log: 2014-05-28 Richard Biener <rguenther@suse.de> PR middle-end/61045 * fold-const.c (fold_comparison): When folding X +- C1 CMP Y +- C2 to X CMP Y +- C2 +- C1 also ensure the sign of the remaining constant operand stays the same. * gcc.dg/pr61045.c: New testcase. Added: trunk/gcc/testsuite/gcc.dg/pr61045.c Modified: trunk/gcc/ChangeLog trunk/gcc/fold-const.c trunk/gcc/testsuite/ChangeLog
Author: rguenth Date: Wed May 28 12:46:39 2014 New Revision: 211019 URL: http://gcc.gnu.org/viewcvs?rev=211019&root=gcc&view=rev Log: 2014-05-28 Richard Biener <rguenther@suse.de> Backport from mainline 2014-05-28 Richard Biener <rguenther@suse.de> PR middle-end/61045 * fold-const.c (fold_comparison): When folding X +- C1 CMP Y +- C2 to X CMP Y +- C2 +- C1 also ensure the sign of the remaining constant operand stays the same. * gcc.dg/pr61045.c: New testcase. 2014-05-05 Richard Biener <rguenther@suse.de> PR middle-end/61010 * fold-const.c (fold_binary_loc): Consistently avoid canonicalizing X & CST away from a CST that is the mask of a mode. * gcc.dg/torture/pr61010.c: New testcase. 2014-04-28 Richard Biener <rguenther@suse.de> PR tree-optimization/60979 * graphite-scop-detection.c (scopdet_basic_block_info): Reject SCOPs that end in a block with a successor with abnormal predecessors. * gcc.dg/graphite/pr60979.c: New testcase. Added: branches/gcc-4_9-branch/gcc/testsuite/gcc.dg/graphite/pr60979.c branches/gcc-4_9-branch/gcc/testsuite/gcc.dg/pr61045.c branches/gcc-4_9-branch/gcc/testsuite/gcc.dg/torture/pr61010.c Modified: branches/gcc-4_9-branch/gcc/ChangeLog branches/gcc-4_9-branch/gcc/fold-const.c branches/gcc-4_9-branch/gcc/graphite-scop-detection.c branches/gcc-4_9-branch/gcc/testsuite/ChangeLog
The 4.7 branch is being closed, moving target milestone to 4.8.4.
Author: rguenth Date: Tue Sep 9 13:17:51 2014 New Revision: 215073 URL: https://gcc.gnu.org/viewcvs?rev=215073&root=gcc&view=rev Log: 2014-09-09 Richard Biener <rguenther@suse.de> Backport from mainline 2014-05-05 Richard Biener <rguenther@suse.de> PR middle-end/61010 * fold-const.c (fold_binary_loc): Consistently avoid canonicalizing X & CST away from a CST that is the mask of a mode. * gcc.dg/torture/pr61010.c: New testcase. 2014-05-28 Richard Biener <rguenther@suse.de> PR middle-end/61045 * fold-const.c (fold_comparison): When folding X +- C1 CMP Y +- C2 to X CMP Y +- C2 +- C1 also ensure the sign of the remaining constant operand stays the same. * gcc.dg/pr61045.c: New testcase. 2014-08-11 Richard Biener <rguenther@suse.de> PR tree-optimization/62075 * tree-vect-slp.c (vect_detect_hybrid_slp_stmts): Properly handle uses in patterns. * gcc.dg/vect/pr62075.c: New testcase. Added: branches/gcc-4_8-branch/gcc/testsuite/gcc.dg/pr61045.c branches/gcc-4_8-branch/gcc/testsuite/gcc.dg/torture/pr61010.c branches/gcc-4_8-branch/gcc/testsuite/gcc.dg/vect/pr62075.c Modified: branches/gcc-4_8-branch/gcc/ChangeLog branches/gcc-4_8-branch/gcc/fold-const.c branches/gcc-4_8-branch/gcc/testsuite/ChangeLog branches/gcc-4_8-branch/gcc/tree-vect-slp.c
Fixed.