http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60747
Bug ID: 60747 Summary: the tree-vrp compilation flag produce wrong assembly Product: gcc Version: 4.4.7 Status: UNCONFIRMED Severity: major Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: techsgin at gmail dot com Hi At optimization level O2 the results of return value of short if are miss calculated i.e wrong results. see example Solution: Don't use the tree-vrp compilation flag i.e add -fno-tree-vrp Example code: #include <stdio.h> #include <string.h> int hash_it(const char* d, const int prime) __attribute__((noinline)); int hash_it(const char* d, const int prime) { int n = strlen(d); int h = n; for (int i = 0; i < n; i++, d++) h = (777*h) + (*d); return ((h >= 0) ? (h % prime) : (-h % prime)); } int main () { int lret = 0; const char* lsdata = "ABCD"; lret = hash_it(lsdata, 10); printf("%d, %s\n", lret, lsdata); return 0; } Results: correct result (i.e NO tree vrp) : $ g++ -O2 -fno-tree-vrp main.cpp -o main_2_no_tree_vrp $ ./main_2_no_tree_vrp 2, ABCD incorrect result (i.e tree vrp in use) : $ g++ -O2 main.cpp -o main_o2_tree_vrp $ ./main_o2_tree_vrp -2, ABCD Version details: $ cat /etc/*-release CentOS release 6.5 (Final) LSB_VERSION=base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch CentOS release 6.5 (Final) CentOS release 6.5 (Final) $ uname -a Linux mastercos6 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux $ gcc -v Using built-in specs. Target: x86_64-redhat-linux Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-ppl --with-cloog --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux Thread model: posix gcc version 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)