> I agree - the compiler removes unnessecary code here, but the reason is > the incomplete asm() instruction. The asm() instruction has 3 parts: the > instruction itself (here "swpb %0"), the destination of the operand > (here "+g" (v)) and the source(s). The sources have been omitted here. > > asm("<instruction>" : <destination> : <sources>); > > Therefore the compiler can't see a source and removes the statements.
Sorry, no. This is incorrect and misleading. The "+" in the "+g" declaration says that the variable v is a source as well as a destination. Further, an asm that does not delcare a source is perfectly legal, and often used for things like reading special registers on processors that have them. E.g. asm("rdtsc" : "=a" (lo), "=d" (hi)); on x86. (See the discussion of "voltaile" with "asm" in the GCC manual for some details I'm not going into here.)