Bug 28042 - optimizer (-O2) changes the semantics of my programs
Summary: optimizer (-O2) changes the semantics of my programs
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 2.95.1
: P3 normal
Target Milestone: 4.1.0
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2006-06-15 13:31 UTC by Wolfgang Helbig
Modified: 2006-06-16 18:59 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build: ?
Known to work: 4.1.2 4.2.0
Known to fail: 4.0.4
Last reconfirmed: 2006-06-16 10:13:28


Attachments
Test case (cleaned for -Wall) from Wolfgang's page (211 bytes, text/plain)
2006-06-16 09:07 UTC, Tony Garnock-Jones
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Wolfgang Helbig 2006-06-15 13:31:37 UTC
See lower part of my article at http://www.ba-stuttgart.de/~helbig/st80/bsearch.txt
Comment 1 Andrew Pinski 2006-06-15 14:15:04 UTC
The tesstcase works in "4.2.0 20060608" but fails with Apple's "gcc version 4.0.1 (Apple Computer, Inc. build 5341)".  I bet I have seen this bug before.
Comment 2 Tony Garnock-Jones 2006-06-16 09:07:01 UTC
Created attachment 11678 [details]
Test case (cleaned for -Wall) from Wolfgang's page

Identical (and still failing) test case to that given by Wolfgang Helbig, but for changes I made to make it compile clean (and still fail) with -Wall: rearranged procedures to avoid missing-prototype warning; fill in prototype for main; return value from main; header files.
Comment 3 Tony Garnock-Jones 2006-06-16 09:07:57 UTC
Jun 16 10:07:17 tonyg@shortstop
~$ gcc -Wall -O0 -o is_small is_small.c

Jun 16 10:07:29 tonyg@shortstop
~$ ./is_small 
0

Jun 16 10:07:32 tonyg@shortstop
~$ gcc -Wall -O1 -o is_small is_small.c

Jun 16 10:07:37 tonyg@shortstop
~$ ./is_small 
ivalue: 1073741824
1

Jun 16 10:07:38 tonyg@shortstop
~$ gcc --version
gcc (GCC) 4.0.4 20060507 (prerelease) (Debian 4.0.3-3)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Comment 4 Tony Garnock-Jones 2006-06-16 09:28:38 UTC
Even better, if you remove the redundant parens, so that the program reads 

...
    if ((integerValue >= 0)
        && (integerValue >= -1073741824) && (integerValue < 1073741824)) {
...


then the optimizer generates code correctly! (How on earth can paren placement affect code generation???)
Comment 5 Richard Biener 2006-06-16 10:13:28 UTC
Fold simply drops the && (integerValue >= -1073741824) && (integerValue < 1073741824) part.  Works if using 2**29 instead of 2**30.
Comment 6 Richard Biener 2006-06-16 10:24:40 UTC
So, we fold

int foo(int i)
{
  return i>=0 && (i>=-1073741824 && i<1073741824);
}

into return i>=0.  Or more precise, we fold

int foo(int i)
{
  return i>=0 && i - -1073741824 >= 0;
}

to return i>=0 (4.2 does this as well),
as we fold
  (i>=-1073741824 && i<1073741824)
to 
  i - -1073741824 >= 0  (wrong, this overflows for i == 1073741824)
first;  4.2 folds that to
  (int)((unsigned)i - 0c0000000) >= 0.   (ok)

(the overflow flag on 0c0000000 is still wrong)
Comment 7 Richard Biener 2006-06-16 10:37:47 UTC
Janis, can you hunt this?
Comment 8 Richard Biener 2006-06-16 10:47:27 UTC
Otherwise, this is not a regression, so possibly just RESOLVED FIXED with a target milestone of 4.1.x.  Up to the RM of 4.0.
Comment 9 Andrew Pinski 2006-06-16 17:55:23 UTC
This is a dup of another bug which I fixed for 4.1.0 so closing as fixed.
Comment 10 Janis Johnson 2006-06-16 18:59:28 UTC
The regression hunt to find when the testcase starts passing identified this mainline patch:

    http://gcc.gnu.org/viewcvs?view=rev&rev=106400

    r106400 | rth | 2005-11-02 21:44:17 +0000 (Wed, 02 Nov 2005)