I was looking into the side effects/run time overhead of using compiler barrier ( in gcc ) in x86 env.
Compiler barrier: asm volatile( ::: "memory" )
GCC documentation tells something interesting ( https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html )
Excerpt:
The "memory" clobber tells the compiler that the assembly code performs memory reads or writes to items other than those listed in the input and output operands (for example, accessing the memory pointed to by one of the input parameters). To ensure memory contains correct values, GCC may need to flush specific register values to memory before executing the asm. Further, the compiler does not assume that any values read from memory before an asm remain unchanged after that asm; it reloads them as needed. Using the "memory" clobber effectively forms a read/write memory barrier for the compiler.
Question:
1) What register values are flushed ?
2) Why it needs to be flushed ?
3) Example ?
4) Is there any other overhead apart from register flushing ?