https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63375
Bug ID: 63375 Summary: reordering of reads across fences Product: gcc Version: 4.9.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: rtl-optimization Assignee: unassigned at gcc dot gnu.org Reporter: bobby.prani at gmail dot com A read within the region of code enclosed by barriers is being moved out of the regions. Test case follows: #include<stdio.h> typedef struct { int counter; } atomic_t; static inline int atomic_read(atomic_t *v) { return (*(volatile int *)&(v)->counter); } #define smp_mb() asm volatile ("mfence":::"memory") int main() { atomic_t val = {1}; int p; smp_mb(); p = atomic_read(&val); smp_mb(); printf("%d\n", p); return 0; } Generated assembly: 15: mfence 18: mfence 1b: mov $0x1,%esi 20: mov $0x0,%rdi 27: xor %eax,%eax 29: callq 2e <init_module+0x2e>