When to use earlyclobber constraint in extended GCC inline assembly?

By default, the compiler assumes all inputs will be consumed before any output registers are written to, so that it’s allowed to use the same registers for both. This leads to better code when possible, but if the assumption is wrong, things will fail catastrophically. The “early clobber” marker is a way to tell the compiler that this output will be written before all the input has been consumed, so it cannot share a register with any input.

GNU C inline asm syntax was designed to wrap a single instruction as efficiently as possible. You can put multiple instructions in an asm template, but the defaults (assuming that all inputs are read before any outputs are written) are designed around wrapping a single instruction.

It’s the same constraint syntax as GCC uses in its machine-description files that teach the compiler what instructions are available in an ISA.

Leave a Comment