Using -Os and volatile results in functions, sometimes the optimizer 'forgets' the volatile property of the function and makes it pure. This has a major impact in some cases. Test case 1 gives buggy code where test case 2 & 3 works. The command line to compile this was arm-elf-gcc -c test.c -Os
/* Test case 1 */ struct test_a { volatile int a; }; int func_a(void) __attribute__ ((noinline)); int func_a(void) { return ((struct test_a *)0)->a; } void a(void) { while(func_a() == 0); } /* Test case 2 */ struct test_b { volatile int a;}; int func_b(void) __attribute__ ((noinline)); int func_b(void) { volatile struct test_b *t= 0; return t->a; } void b(void) { while(func_b() == 0); } /* Test case 3 */ struct test_c { volatile int a; }; int func_c(void) __attribute__ ((noinline)); int func_c(void) { return ((volatile struct test_c *)0)->a; } void c(void) { while(func_c() == 0); } The result from this is: 00000000 <func_a>: 0: e3a03000 mov r3, #0 ; 0x0 4: e5930000 ldr r0, [r3] 8: e12fff1e bx lr 0000000c <a>: c: e52de004 str lr, [sp, #-4]! 10: ebfffffe bl 0 <func_a> 14: e3500000 cmp r0, #0 ; 0x0 18: 0a000003 beq 14 <a+0x8> <--- looping back to the cmp!?! 1c: e49df004 ldr pc, [sp], #4 00000020 <func_b>: 20: e3a03000 mov r3, #0 ; 0x0 24: e5930000 ldr r0, [r3] 28: e12fff1e bx lr 0000002c <b>: 2c: e52de004 str lr, [sp, #-4]! 30: ebfffffe bl 20 <func_b> 34: e3500000 cmp r0, #0 ; 0x0 38: 0a00000a beq 30 <b+0x4> <--- looping back to the functon call 3c: e49df004 ldr pc, [sp], #4 00000040 <func_c>: 40: e3a03000 mov r3, #0 ; 0x0 44: e5930000 ldr r0, [r3] 48: e12fff1e bx lr 0000004c <c>: 4c: e52de004 str lr, [sp, #-4]! 50: ebfffffe bl 40 <func_c> 54: e3500000 cmp r0, #0 ; 0x0 58: 0a000012 beq 50 <c+0x4> <--- looping back to the functon call 5c: e49df004 ldr pc, [sp], #4 -- Summary: volatile problem in optimized functions Product: gcc Version: 4.1.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: sb at anoto dot com GCC build triplet: x86-pc-cygwin GCC host triplet: x86-pc-cygwin GCC target triplet: arm-elf-gcc http://gcc.gnu.org/bugzilla/show_bug.cgi?id=29964