On 12/04/11 09:18, David Brown wrote: > For example, if you have code like this: > > static int x; > void test(void) { > x = 1; > asm volatile ("" : : : "memory"); > x = 2; > } > > The variable "x" is not volatile - can the compiler remove the > assignment "x = 1"? Perhaps with aggressive optimisation, the compiler > will figure out how and when x is used, and discover that it doesn't > need to store it in memory at all, but can keep it in a register > (perhaps all uses have ended up inlined inside the same function). Then > "x" is no longer in memory - will it still be affected by the memory > clobber? I don't think either assignment can be removed, at least not when the program is compiled with threads enabled. Even though x is local to this translation unit it is not local to this thread. The compiler must respect memory barriers in this case: if it did not, POSIX threads support would not work. There was a fantastically long thread about this "Optimization of conditional access to globals: thread-unsafe?" in Oct 2007. Andrew.