This bug was detected in gcc 3.3.1 and is still here in gcc-3.4.3 and gcc-4.0.0 The bug is not in gcc-3.2.2
-O2 and -Os produces bad code. Switching optimization level to something less than -O2 produces a valid code. Using -O2 -fno-schedule-insns2 also produces valid code. ./g++-mainline-4.0 -O2 -o foo ~/bug.ii ./foo foo: /bug.cc:59: int main(int, char**): Assertion `zone1->offset == 2 * 20' failed. Aborted ./g++-mainline-4.0 -O2 -o foo ~/bug.ii -fno-schedule-insns2 ./foo works just fine. cat bug.ii # 1 "/bug.cc" # 1 "<built-in>" # 1 "<command line>" # 1 "/bug.cc" # 1 "/usr/include/assert.h" 1 3 4 # 36 "/usr/include/assert.h" 3 4 # 1 "/usr/include/features.h" 1 3 4 # 296 "/usr/include/features.h" 3 4 # 1 "/usr/include/sys/cdefs.h" 1 3 4 # 297 "/usr/include/features.h" 2 3 4 # 319 "/usr/include/features.h" 3 4 # 1 "/usr/include/gnu/stubs.h" 1 3 4 # 320 "/usr/include/features.h" 2 3 4 # 37 "/usr/include/assert.h" 2 3 4 # 65 "/usr/include/assert.h" 3 4 extern "C" { extern void __assert_fail (__const char *__assertion, __const char *__file, unsigned int __line, __const char *__function) throw () __attribute__ ((__noreturn__)); extern void __assert_perror_fail (int __errnum, __const char *__file, unsigned int __line, __const char *__function) throw () __attribute__ ((__noreturn__)); extern void __assert (const char *__assertion, const char *__file, int __line) throw () __attribute__ ((__noreturn__)); } # 2 "/bug.cc" 2 struct Type1 { long offset; }; struct Decaler { long bar; long foo; }; struct Type3 { int value; }; struct mem_block { mem_block() { m_ShMem = new char[4096]; } void* operator[](const long Offset) { return (reinterpret_cast<char *>(m_ShMem) + Offset); } void* m_ShMem; }; int main(int, char**) { mem_block memory_block; Decaler* decaler = new Decaler(); Type1* zone1 = reinterpret_cast<Type1*>(memory_block[0]); zone1->offset = 20; Type1* zone2 = reinterpret_cast<Type1*>(memory_block[20]); zone2->offset = 2 * 20; Type3* pageToUse = reinterpret_cast<Type3*>(memory_block[20]); decaler->bar = 123456; decaler->foo = zone1->offset; zone1->offset = zone2->offset; pageToUse->value = 5; (static_cast<void> (__builtin_expect (!!(zone1->offset == 2 * 20), 1) ? 0 : (__assert_fail ("zone1->offset == 2 * 20", "/bug.cc", 59, __PRETTY_FUNCTION__), 0))); return 0; } -- Summary: Bad code generation caused by -fschedule-insns2 Product: gcc Version: 3.3.1 Status: UNCONFIRMED Severity: normal Priority: P2 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: c dot pop at free dot fr CC: gcc-bugs at gcc dot gnu dot org GCC host triplet: i686-pc-linux-gnu, x86_64-unknown-linux-gnu http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20487