Search code examples
compilationclangllvmllvm-clang

Ensure that register is pushed to the stack at function entry


Typically, a C function is translated by clang for an ARM architecture in a way such that the LR register is pushed on the stack. However, if the function does not contain any function calls itself, LR is not pushed, i.e. the compiler assumes that LR won't be modified.

Is there a way to make the clang/llc always push LR at the beginning of a function, regardless of whether it calls a function or not?


Solution

  • Copied from the comments above.

    Try sticking asm volatile ( "" : : : "lr" ); at the beginning of the body of the function. This tells LLVM that lr gets clobbered. If LLVM finds that a register is clobbered anywhere in a function, it has to save that register at the beginning of the function.

    (The somewhat cryptic inline asm syntax is documented here: https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html)