http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50472
Bug #: 50472 Summary: Volatile qualification in data is not enough to avoid optimization over pointer to data Classification: Unclassified Product: gcc Version: 4.6.1 Status: UNCONFIRMED Severity: major Priority: P3 Component: regression AssignedTo: unassig...@gcc.gnu.org ReportedBy: paulo.ma...@csr.com Given the following code: static const unsigned int foo = 1; unsigned int test( void ) { const volatile unsigned int *bar = &foo; return ( *bar ); } GCC46 is generating: $ local/gcc-4.6.1/bin/gcc -Os -S vol.c $ cat vol.s ... test: .LFB0: .cfi_startproc movl $1, %eax ret .cfi_endproc ... This is optimizing away bar even though the data bar points to is being qualified as volatile. The way to ensure this doesn't happen requires qualifying the pointer as a volatile too: static const unsigned int foo = 1; unsigned int test( void ) { const volatile unsigned int * volatile bar = &foo; return ( *bar ); } $ local/gcc-4.6.1/bin/gcc -Os -S vol.c $ cat vol.s ... test: .LFB0: .cfi_startproc movq $foo, -8(%rsp) movq -8(%rsp), %rax movl (%rax), %eax ret .cfi_endproc ... This is a regression to what GCC45 used to do. Qualification of the data as volatile used to be enough to block the optimization. Posting this to the mailing list revealed Ian agrees this is a regression: http://gcc.gnu.org/ml/gcc/2011-09/msg00197.html