15

Clang TargetInfo has a method called getClobbers:

Returns a string of target-specific clobbers, in LLVM format.

So, what is a clobber?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
sdgfsdh
  • 33,689
  • 26
  • 132
  • 245

1 Answers1

25

A clobbered register is a register which is trashed i.e. modified in unpredictable way by inline assembler. This usually happens when you need a temp. register or use particular instruction which happens to modify some register as a by-product.

Usually programmer explicitly declares registers which are clobbered by his inline asm code but some may be considered to be trashed by default and that's where getClobbers come into play.

getClobbers returns empty result for most targets. On MIPS GCC has historically not used $1 in generated code so most programmers didn't bother to declare it as clobbered. To reduce portability costs, LLVM considers $1 to be always clobbered in inline asm. Another example is arithmetic flags register (cc) which is considered to be always clobbered by inline asm by GCC on i386 and x86_64 targets.

yugr
  • 19,769
  • 3
  • 51
  • 96
  • 9
    Might be worth adding that "trashed" here means the assembler code needs to use the registers (a register could be seen as a very fast temporary variable built into the CPU instead of being on a separate RAM chip if you're unfamiliar with assembler), and so will not preserve the values the caller put into them. – uliwitness Jan 27 '17 at 18:30
  • @uliwitness Thanks, added. – yugr Jan 27 '17 at 19:04
  • 1
    i386 and amd64 gcc (and clang I'm pretty sure) implicitly clobber condition codes, `"cc"`, in asm statements because many integer instructions unavoidably write them. I know you're not trying to make an exhaustive list, but that's one for a major ISA. – Peter Cordes Dec 02 '20 at 08:58
  • @PeterCordes Thank you, that's a good addition to the answer. I haven't yet found code in Clang which does so mentioned only GCC. – yugr Dec 02 '20 at 19:14