Bug 61045 - [4.8 Regression] Wrong constant folding
Summary: [4.8 Regression] Wrong constant folding
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 5.0
: P3 normal
Target Milestone: 4.8.4
Assignee: Richard Biener
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2014-05-03 15:41 UTC by Ishiura Lab Compiler Team
Modified: 2014-09-09 13:19 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work: 4.8.4, 4.9.1, 5.0
Known to fail: 4.8.3, 4.9.0
Last reconfirmed: 2014-05-03 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ishiura Lab Compiler Team 2014-05-03 15:41:33 UTC
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)
Comment 1 Marek Polacek 2014-05-03 15:57:28 UTC
Confirmed.  Fold introduces signed overflow:
    long int a = 0;
    long int b = 9223372036854775807;
    int t = b + 1 < a;
Comment 2 Mikael Pettersson 2014-05-03 16:00:48 UTC
Fails also with gcc 4.3.6, works with gcc 4.2.4 and 4.1.2.
Comment 3 Marek Polacek 2014-05-03 17:02:23 UTC
I believe this started with addition of this folding in r117931, but didn't verify it.
Comment 4 Mikael Pettersson 2014-05-03 19:31:20 UTC
According to my bisection it did start with r117931.
Comment 5 Richard Biener 2014-05-05 09:11:35 UTC
Mine then.
Comment 6 Richard Biener 2014-05-28 11:10:22 UTC
      /* 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.
Comment 7 Richard Biener 2014-05-28 12:44:43 UTC
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
Comment 8 Richard Biener 2014-05-28 12:47:11 UTC
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
Comment 9 Richard Biener 2014-06-12 13:43:40 UTC
The 4.7 branch is being closed, moving target milestone to 4.8.4.
Comment 10 Richard Biener 2014-09-09 13:18:23 UTC
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
Comment 11 Richard Biener 2014-09-09 13:19:02 UTC
Fixed.