This is an archive of the discontinued LLVM Phabricator instance.

[ARM] Generate consistent frame records for Thumb2
ClosedPublic

Authored by olista01 on Aug 15 2016, 9:09 AM.

Details

Summary

There is not an official documented ABI for frame pointers in Thumb2,
but we should try to emit something which is useful.

We use r7 as the frame pointer for Thumb code, which currently means
that if a function needs to save a high register (r8-r11), it will get
pushed to the stack between the frame pointer (r7) and link register
(r14). This means that while a stack unwinder can follow the chain of
frame pointers up the stack, it cannot know the offset to lr, so does
not know which functions correspond to the stack frames.

To fix this, we need to push the callee-saved registers in two batches,
with the first push saving the low registers, fp and lr, and the second
push saving the high registers. This is already implemented, but
previously only used for iOS. This patch turns it on for all Thumb2
targets when frame pointers are required by the ABI, and the frame
pointer is r7 (Windows uses r11, so this isn't a problem there). If
frame pointer elimination is enabled we still emit a single push/pop
even if we need a frame pointer for other reasons, to avoid increasing
code size.

We must also ensure that lr is pushed to the stack when using a frame
pointer, so that we end up with a complete frame record. Situations that
could cause this were rare, because we already push lr in most
situations so that we can return using the pop instruction.

@Tim, or anyone who knows about the Apple ARM calling convention:
There is existing code to force a frame pointer and enable split push/pop when
using the Apple ABI (documented at
https://developer.apple.com/library/ios/documentation/Xcode/Conceptual/iPhoneOSABIReference/Articles/ARMv6FunctionCallingConventions.html).
However, the code to do this is spread across several functions, and the exact
conditions under which is enabled are different. I've found these different
conditions:

  • isTargetIOS() || isTargetWatchOS() // In ARMFrameLowering::hasFP
  • isTargetMachO() // In ARMSubtarget::splitFramePushPop
  • isTargetDarwin() // In ARMBaseRegisterInfo::getCalleeSavedRegs

Which of these conditions is correct? I've left these as they are for now, but
we should probably clear this up. I found one test, ifcvt-iter-indbr.ll, which
used a thumbv7s-apple-darwin triple, which currently (before and after this
patch) causes the split push/pop to be used, but does still allow the frame
pointer to be optimised out.

Diff Detail

Repository
rL LLVM

Event Timeline

olista01 updated this revision to Diff 68044.Aug 15 2016, 9:09 AM
olista01 retitled this revision from to [ARM] Generate consistent frame records for Thumb2.
olista01 updated this object.
olista01 added reviewers: t.p.northover, rengolin.
olista01 set the repository for this revision to rL LLVM.
olista01 added a subscriber: llvm-commits.

I've found these different conditions:

  • isTargetIOS() || isTargetWatchOS() // In ARMFrameLowering::hasFP
  • isTargetMachO() // In ARMSubtarget::splitFramePushPop
  • isTargetDarwin() // In ARMBaseRegisterInfo::getCalleeSavedRegs

Oh dear, that is a bit of a mess! I think we might be trying too hard to support the current situation.

The iOS special snowflake code seems to be basically a hack to ignore -fomit-frame-pointer (I spoke to Jim about this, and the best we could work out was that it might have been a workaround for an llvm-gcc limitation, never revisited since Clang came along).

With that removed, we seem to be left with something that's basically a duplicate of noFramePointerElim (just above your hasABIFP). If you simplify this, I'll put together a Clang patch to ignore and warn about -fomit-frame-pointer on iOS & watchOS.

I can remove the iOS || WatchOS check in ARMFrameLowering::hasFP (and add the "no-frame-pointer-elim" attribute to all the iOS/WatchOS tests).

However, what about the checks that enable the split push/pop, in ARMSubtarget::splitFramePushPop and ARMBaseRegisterInfo::getCalleeSavedRegs? These currently check a mixture of isMachO and isDarwin, but the comments and name of the callee-saved reg list mention iOS. Which of these is correct?

olista01 updated this revision to Diff 68879.Aug 22 2016, 10:05 AM
olista01 edited edge metadata.
olista01 removed rL LLVM as the repository for this revision.
  • Removed hasABIFP, use DisableFramePointerElim instead.
  • Removed special cases for iOS and WatchOS, the "no-frame-pointer-elim" attribute should be set by the frontend instead.
  • R7 is now the frame pointer for Darwin, rather than MachO, and split stacks are enabled iff the frame pointer is r7.
  • Fix up a lot of tests affected by the iOS/WatchOS change. Most of these were just a case of adding the "no-frame-pointer-elim" attribute, but there were a few cases testing the codegen differences with/without frame pointers, which needed changing.
  • Rolled D23517 into this patch, as that was the original behaviour on iOS/WatchOS, and it wouldn't be possible to replicate the original iOS behaviour without D23517.
t.p.northover accepted this revision.Aug 22 2016, 10:22 AM
t.p.northover edited edge metadata.

Thanks Oliver. Looks nice and neat to me now! I'll work on making Clang ignore -fomit-frame-pointer for Darwin.

This revision is now accepted and ready to land.Aug 22 2016, 10:22 AM
This revision was automatically updated to reflect the committed changes.