When an Armv6m function dynamically re-aligns the stack, access to incoming stack arguments (and to stack area, allocated for register varargs)
is done via SP, which is incorrect. For example, compiling:
void h(int, int *); void f(int n, ...) { __builtin_va_list ap; __builtin_va_start(ap, n); __attribute__((aligned(16))) int v[4]; h(n, v); }
with clang -target arm-eabi -mcpu=cortex-m0 -O2 yields the following assembly:
f: sub sp, #12 push {r4, r6, r7, lr} add r7, sp, #8 sub sp, #20 mov r4, sp lsrs r4, r4, #4 lsls r4, r4, #4 mov sp, r4 str r3, [sp, #44] str r2, [sp, #40] str r1, [sp, #36] ...
where incoming register varargs are stored using the SP after alignment.
This patch fixes it, by making access to "fixed" frame objects be done via FP when the function needs stack re-alignment.
It also changes the access to "fixed" frame objects be done via FP (instead of using R6/BP) also for the case when the stack frame contains variable sized objects. This should allow more objects to fit within the immediate offset of the load instruction.
All of the above via a small refactoring to reuse the existing ARMFrameLowering::ResolveFrameIndexReference.
Please add a comment explaining which objects must be referenced some particular way, and which objects you prefer to reference one way or the other for the sake of optimization.