http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48793
Summary: Optimization -O2 Incorrectly Removes Logic Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: c AssignedTo: unassig...@gcc.gnu.org ReportedBy: ch...@durso.org Created attachment 24118 --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=24118 main.ii When dealing with min_int (0x80000000, a peculiar value as min_int < 0 && min_int == -1*min_int) it appears that -O2 and above on on some versions of gcc will under some circumstances optimize away particular logic for dealing with this edge case. Of the versions I've tested: gcc version 4.1.2 20080704 (Red Hat 4.1.2-48) SUCCESSFUL gcc version 4.2.1 (Apple Inc. build 5666) (dot 3) FAILS gcc version 4.4.3 (GCC) Target: i386-pc-solaris2.10 FAILS FAILURE EXAMPLE: chdurso@searchdev12:m_index_query_hash_core> uname -a ; /ebay/vendor/pkg/gcc-4.4.3/bin/g++ -v; /ebay/vendor/pkg/gcc-4.4.3/bin/g++ -Wall -Wextra -save-temps -O2 -o hello main.cpp ; echo START_OUTPUT; ./hello SunOS searchdev12.arch.ebay.com 5.10 Generic_120012-14 i86pc i386 i86pc Solaris Using built-in specs. Target: i386-pc-solaris2.10 Configured with: /ebay/vendor/Archive/gcc-4.4.3/configure --prefix=/ebay/vendor/pkg/gcc-4.4.3 --with-gnu-as --with-as=/usr/sfw/bin/gas --without-gnu-ld --with-ld=/usr/ccs/bin/ld --enable-shared --enable-languages=c,c++ Thread model: posix gcc version 4.4.3 (GCC) START_OUTPUT The following number is tricky as it is the only negative number unpaired with a positive analog in 2's compliment arithemetic. It behaves unintuitively and apparently breaks an optimization. tricky: -2147483648 abs(tricky): -2147483648 1 * tricky = -2147483648 -1 * tricky = -2147483648 tricky + 1 = -2147483647 tricky - 1 = 2147483647 test 0: tricky: -2147483648 test 1: tricky: -2147483648 Assertion failed: tricky == 0x7fffffff && "on -O2 and above this asserts apparently drops last if statement", file main.cpp, line 35 Abort (core dumped) // main.cpp // #include <iostream> #include <stdlib.h> #include <cassert> using namespace std; int main(int , const char**){ cout << "The following number is tricky as it is the only negative number " "unpaired with a positive analog in 2's compliment arithemetic. " "It behaves unintuitively and apparently breaks an optimization."<< endl << endl; int tricky = 0x80000000; cout << "tricky: " << tricky << endl << endl; cout << "abs(tricky): " << abs(tricky) << endl << " 1 * tricky = " << 1* tricky << endl << "-1 * tricky = " << -1* tricky << endl << "tricky + 1 = " << tricky + 1 << endl << "tricky - 1 = " << tricky - 1 << endl; tricky = 0x1; for(int i(0); i < 31; ++i) // can't simply assign, but a loop here will result allow failure tricky <<= 1; assert(tricky == int(0x80000000)); // tricky = 0x80000000; this unnecessary assignment will cause it work correctly cout << "test 0: tricky: " << tricky << endl; if (tricky<0) tricky = -tricky; // should always get here cout << "test 1: tricky: " << tricky << endl; if (tricky < 0) tricky = 0x7fffffff; // should always get here assert(tricky == 0x7fffffff && "on -O2 and above this asserts apparently drops last if statement"); return 0; } ATTACHED main.ii