When I compile the following code with -O1 -ftree-vrp or -O2, it prints -2147483647, instead of 1 as expected. (The intent of the code was to search for integers which, when squared and truncated, give small positive values.)
#include <stdio.h> #include <stdlib.h> #include <limits.h> int main() { int min = INT_MAX, i; for(i = 1; i > 0; ++i) { int candidate = i * i; if((candidate < min) && (candidate > 0)) min = candidate; } printf("min was %d\n", min); return 0; } If you put a printf each time a new min is found, you can see it behaves as though the candidate > 0 check is not occurring. Manually replacing candidate with i*i does not fix the problem. Initializing candidate to (i * i) & 0xffffffff does fix it. I'm running Gentoo on amd64, and the problem occurs with gcc-4.1.0-beta20060210 and gcc-beta20060127. It occurs with both -m64 and -m32. I can't trigger it with gcc-3.4.3. I haven't tested it on any other versions. $ gcc -v Using built-in specs. Target: x86_64-pc-linux-gnu Configured with: /var/tmp/portage/gcc-4.1.0_beta20060210/work/gcc-4.1-20060210/configure --prefix=/usr --bindir=/usr/x86_64-pc-linux-gnu/gcc-bin/4.1.0-beta20060210 --includedir=/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.0-beta20060210/include --datadir=/usr/share/gcc-data/x86_64-pc-linux-gnu/4.1.0-beta20060210 --mandir=/usr/share/gcc-data/x86_64-pc-linux-gnu/4.1.0-beta20060210/man --infodir=/usr/share/gcc-data/x86_64-pc-linux-gnu/4.1.0-beta20060210/info --with-gxx-include-dir=/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.0-beta20060210/include/g++-v4 --host=x86_64-pc-linux-gnu --build=x86_64-pc-linux-gnu --disable-altivec --enable-nls --without-included-gettext --with-system-zlib --disable-checking --disable-werror --disable-libunwind-exceptions --enable-multilib --disable-libmudflap --disable-libgcj --enable-languages=c,c++,fortran --enable-shared --enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu Thread model: posix gcc version 4.1.0-beta20060210 (prerelease) -- Summary: -ftree-vrp generates invalid code with this loop that overflows integer multiplication Product: gcc Version: 4.2.0 Status: UNCONFIRMED Severity: major Priority: P3 Component: tree-optimization AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: tomfelker at gmail dot com GCC build triplet: 4.1.0-beta20060210 GCC host triplet: x86_64-pc-linux-gnu GCC target triplet: x86_64-pc-linux-gnu http://gcc.gnu.org/bugzilla/show_bug.cgi?id=26358