The Extended Asm manual https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html says the following about the "memory" clobber:
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.
I am confused about the decision to flush to memory. Before the asm code, how would GCC know if a register serves as a cache for a memory location, and thus needs to be flushed to memory? And is this part of cache coherency (I thought cache coherency was a hardware behavior)? After the asm code, how does GCC distinguish a register as a cache and, next time the register is read, decide instead to read from memory as the cache may be old?