The -fschedule-insns2 optimisation causes wrong code to be emitted for the following testcase. The assembly code loads a value from a stack frame which has already been deallocated.
This is similar to http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30282 for powerpc-eabi. extern int doStreamReadBlock(int *, char *, int size, int); char readStream(int *s) { char c = 0; doStreamReadBlock(s, &c, 1, *s); return c; } arm-eabi davem$ arm-eabi-gcc -v -O2 -mthumb -c test.c Using built-in specs. Target: arm-eabi Configured with: ../../gcc-4.3.2/configure --enable-languages=c,c++ --with-cpu=arm7tdmi --enable-interwork --enable-multilib --with-gcc --with-gnu-ld --with-gnu-as --disable-shared --disable-threads --disable-win32-registry --disable-nls --disable-debug --disable-libmudflap --disable-libssp --disable-libgomp --disable-libstdcxx-pch --target=arm-eabi --with-newlib --prefix=/opt/devkitpro/devkitARM --with-bugurl=http://wiki.devkitpro.org/index.php/Bug_Reports --with-pkgversion='devkitARM release 24' Thread model: single gcc version 4.3.2 (devkitARM release 24) 00000000 <readStream>: 0: b510 push {r4, lr} 2: b082 sub sp, #8 4: 466c mov r4, sp 6: 3407 adds r4, #7 8: 2300 movs r3, #0 a: 7023 strb r3, [r4, #0] c: 1c21 adds r1, r4, #0 e: 6803 ldr r3, [r0, #0] 10: 2201 movs r2, #1 12: f7ff fffe bl 0 <doStreamReadBlock> 16: b002 add sp, #8 <--- stack frame deallocated 18: 7820 ldrb r0, [r4, #0] <--- value loaded from stack frame 1a: bc10 pop {r4} 1c: bc02 pop {r1} 1e: 4708 bx r1 The same thing happens with mainline /opt/devkitpro/devkitARM_mainline/bin/arm-eabi-gcc -v -O2 -mthumb -c test.c Using built-in specs. Target: arm-eabi Configured with: ../../../gcc_mainline/configure --disable-nls --target=arm-eabi --prefix=/opt/devkitPro/devkitARM --enable-languages=c,c++ --with-cpu=arm7tdmi --enable-interwork --enable-multilib --with-gcc --with-gnu-ld --with-gnu-as --disable-shared --disable-threads --disable-win32-registry --disable-nls --disable-debug --disable-libmudflap --disable-libssp --disable-libgomp --disable-libstdcxx-pch Thread model: single gcc version 4.4.0 20081223 (experimental) (GCC) 00000000 <readStream>: 0: b510 push {r4, lr} 2: b082 sub sp, #8 4: 466c mov r4, sp 6: 3407 adds r4, #7 8: 2300 movs r3, #0 a: 7023 strb r3, [r4, #0] c: 1c21 adds r1, r4, #0 e: 6803 ldr r3, [r0, #0] 10: 2201 movs r2, #1 12: f7ff fffe bl 0 <doStreamReadBlock> 16: b002 add sp, #8 18: 7820 ldrb r0, [r4, #0] 1a: bc10 pop {r4} 1c: bc02 pop {r1} 1e: 4708 bx r1 -- Summary: Optimization flag -O1 -fschedule-insns2 causes wrong code Product: gcc Version: 4.3.2 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: davejmurphy at me dot com GCC build triplet: i386-apple-darwin9.6.0 GCC host triplet: i386-apple-darwin9.6.0 GCC target triplet: arm-none-eabi http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38644