The following example takes the "wrong_branch" when compiled with gcc -O3 -fno-inline
gcc --version gcc (GCC) 4.1.2 Copyright (C) 2006 Free Software Foundation, Inc. ===== example ===== #include <stdio.h> static int get_kind(int v) { volatile int k = v; return k; } static int some_call(void) { return 0; } static void wrong_branch(void) { printf("wrong_branch\n"); } static int example(int arg) { int kind; kind = get_kind(arg); if (kind == 9 || kind == 10 || kind == 5) { if (some_call() == 0) { int tmp; // kind = get_kind(arg); // ok if this call is inserted if (kind == 9 || kind == 10) { tmp = arg; } else { wrong_branch(); } /* if */ return 0; } } return 0; } int main(void) { int v = 10; example(v); return 0; } =================== The example prints "wrong_branch" when executed. If the second call to get_kind is uncommented, nothing is printed (which is correct). The generated assembly code is ===== example.s ====== example: save %sp, -112, %sp call get_kind, 0 mov %i0, %o0 add %o0, -9, %g1 subcc %g0, %g1, %g0 subx %g0, -1, %i0 cmp %g1, 1 bleu .LL11 cmp %o0, 5 be .LL11 nop .LL9: jmp %i7+8 restore %g0, 0, %o0 .LL11: call some_call, 0 nop cmp %o0, 0 bne .LL9 cmp %i0, 0 bne .LL9 nop call wrong_branch, 0 mov 0, %i0 jmp %i7+8 restore ======================= -- Summary: optimized code taking the wrong branch Product: gcc Version: 4.1.2 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: stephaniechc-gccbug at yahoo dot com GCC build triplet: sparc-sun-solaris2.8 GCC host triplet: sparc-sun-solaris2.8+ GCC target triplet: sparc-sun-solaris2.8+ http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31691