See lower part of my article at http://www.ba-stuttgart.de/~helbig/st80/bsearch.txt
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.
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.
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.
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???)
Fold simply drops the && (integerValue >= -1073741824) && (integerValue < 1073741824) part. Works if using 2**29 instead of 2**30.
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)
Janis, can you hunt this?
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.
This is a dup of another bug which I fixed for 4.1.0 so closing as fixed.
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)