LLVM AArch64 backend using stack pointer to reference local variables

Hi all,

I am wondering whether there is any way
(i.e., compile option or different version of backend code that can be referred to)
to make the LLVM AArch64 backend use the frame pointer instead of the stack pointer
when it references (or locates) local variables in a stack frame.

When I generate assembly code using LLVM AArch64 backend,
it seems to use a stack pointer to locate the local variables in a stack frame.

For example,
  str wzr, [sp, #12] // e.g., local variable a
  str w8, [sp, #8] // e.g., local variable b
  str w9, [sp, #4] // e.g., local variable c
  str w10, [sp]

(It calculates offsets from a stack pointer and accesses the local variable using the stack pointer and the offsets.)

The weird thing is all the other compiler tools are using a frame pointer rather than a stack pointer.
I tried out "gcc", "clang", "LLVM x86 backend" and they are all referencing local variables using a frame pointer and its offsets.

Is there any reasons that the only LLVM AArch64 uses a stack pointer?
And, is there any way to make it use a frame pinter?

I'd really appreciate for any comment or answer! :slight_smile:


Hi Yonghae,

Could you provide a short example for this? Perhaps using https://godbolt.org/

(there is now a “ARM 64-BIT CLANG” compilers section)

I see some details about the frame pointer in:

Though I haven’t read it properly yet, it seems like it’s not a simple case of not using it at all. It depends on circumstance.

David Spickett.

Using the stack pointer frees up the register, which would have been used as a frame pointer,
for general allocation.

When accessing the stack frame, offsets from SP are positive, whereas offsets from a frame
pointer are negative.
When you use SP, you can use LDR with a scaled positive offset, with range from 0
up to 16380 (or 32760 for 64-bit accesses).
If you use FP, you would use, LDUR with a negative offset, with a range just [-256, 255].

What downsides of using SP do you see?