2012-02-18 20:03:15 +08:00
|
|
|
//===-- Thumb1FrameLowering.cpp - Thumb1 Frame Information ----------------===//
|
2010-11-15 08:06:54 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2011-01-10 20:39:04 +08:00
|
|
|
// This file contains the Thumb1 implementation of TargetFrameLowering class.
|
2010-11-15 08:06:54 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-01-10 20:39:04 +08:00
|
|
|
#include "Thumb1FrameLowering.h"
|
2010-11-15 08:06:54 +08:00
|
|
|
#include "ARMMachineFunctionInfo.h"
|
2015-07-21 05:42:14 +08:00
|
|
|
#include "llvm/CodeGen/LivePhysRegs.h"
|
2010-11-15 08:06:54 +08:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2014-02-15 01:19:07 +08:00
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
Fix epilogue codegen to avoid leaving the stack pointer in an invalid
state. Previously Thumb2 would restore sp from fp like this:
mov sp, r7
sub, sp, #4
If an interrupt is taken after the 'mov' but before the 'sub', callee-saved
registers might be clobbered by the interrupt handler. Instead, try
restoring directly from sp:
add sp, #4
Or, if necessary (with VLA, etc.) use a scratch register to compute sp and
then restore it:
sub.w r4, r7, #8
mov sp, r7
rdar://8465407
llvm-svn: 119977
2010-11-23 02:12:04 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2010-11-15 08:06:54 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-06-27 03:29:59 +08:00
|
|
|
Thumb1FrameLowering::Thumb1FrameLowering(const ARMSubtarget &sti)
|
|
|
|
: ARMFrameLowering(sti) {}
|
|
|
|
|
2011-09-14 04:30:37 +08:00
|
|
|
bool Thumb1FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const{
|
2010-11-19 05:19:35 +08:00
|
|
|
const MachineFrameInfo *FFI = MF.getFrameInfo();
|
|
|
|
unsigned CFSize = FFI->getMaxCallFrameSize();
|
|
|
|
// It's not always a good idea to include the call frame as part of the
|
|
|
|
// stack frame. ARM (especially Thumb) has small immediate offset to
|
|
|
|
// address the stack frame. So a large call frame can cause poor codegen
|
|
|
|
// and may even makes it impossible to scavenge a register.
|
|
|
|
if (CFSize >= ((1 << 8) - 1) * 4 / 2) // Half of imm8 * 4
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return !MF.getFrameInfo()->hasVarSizedObjects();
|
|
|
|
}
|
|
|
|
|
2011-03-06 02:43:50 +08:00
|
|
|
static void
|
|
|
|
emitSPUpdate(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator &MBBI,
|
|
|
|
const TargetInstrInfo &TII, DebugLoc dl,
|
2015-03-13 06:48:50 +08:00
|
|
|
const ThumbRegisterInfo &MRI,
|
2011-03-06 02:43:50 +08:00
|
|
|
int NumBytes, unsigned MIFlags = MachineInstr::NoFlags) {
|
2011-03-06 02:43:32 +08:00
|
|
|
emitThumbRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes, TII,
|
2011-03-06 02:43:50 +08:00
|
|
|
MRI, MIFlags);
|
2010-11-15 08:06:54 +08:00
|
|
|
}
|
|
|
|
|
2013-02-22 04:05:00 +08:00
|
|
|
|
|
|
|
void Thumb1FrameLowering::
|
|
|
|
eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator I) const {
|
|
|
|
const Thumb1InstrInfo &TII =
|
2015-01-29 08:19:33 +08:00
|
|
|
*static_cast<const Thumb1InstrInfo *>(STI.getInstrInfo());
|
2015-03-13 06:48:50 +08:00
|
|
|
const ThumbRegisterInfo *RegInfo =
|
|
|
|
static_cast<const ThumbRegisterInfo *>(STI.getRegisterInfo());
|
2013-02-22 04:05:00 +08:00
|
|
|
if (!hasReservedCallFrame(MF)) {
|
|
|
|
// If we have alloca, convert as follows:
|
|
|
|
// ADJCALLSTACKDOWN -> sub, sp, sp, amount
|
|
|
|
// ADJCALLSTACKUP -> add, sp, sp, amount
|
|
|
|
MachineInstr *Old = I;
|
|
|
|
DebugLoc dl = Old->getDebugLoc();
|
|
|
|
unsigned Amount = Old->getOperand(0).getImm();
|
|
|
|
if (Amount != 0) {
|
|
|
|
// We need to keep the stack aligned properly. To do this, we round the
|
|
|
|
// amount of space needed for the outgoing arguments up to the next
|
|
|
|
// alignment boundary.
|
|
|
|
unsigned Align = getStackAlignment();
|
|
|
|
Amount = (Amount+Align-1)/Align*Align;
|
|
|
|
|
|
|
|
// Replace the pseudo instruction with a new instruction...
|
|
|
|
unsigned Opc = Old->getOpcode();
|
|
|
|
if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) {
|
|
|
|
emitSPUpdate(MBB, I, TII, dl, *RegInfo, -Amount);
|
|
|
|
} else {
|
|
|
|
assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP);
|
|
|
|
emitSPUpdate(MBB, I, TII, dl, *RegInfo, Amount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MBB.erase(I);
|
|
|
|
}
|
|
|
|
|
[ShrinkWrap] Add (a simplified version) of shrink-wrapping.
This patch introduces a new pass that computes the safe point to insert the
prologue and epilogue of the function.
The interest is to find safe points that are cheaper than the entry and exits
blocks.
As an example and to avoid regressions to be introduce, this patch also
implements the required bits to enable the shrink-wrapping pass for AArch64.
** Context **
Currently we insert the prologue and epilogue of the method/function in the
entry and exits blocks. Although this is correct, we can do a better job when
those are not immediately required and insert them at less frequently executed
places.
The job of the shrink-wrapping pass is to identify such places.
** Motivating example **
Let us consider the following function that perform a call only in one branch of
a if:
define i32 @f(i32 %a, i32 %b) {
%tmp = alloca i32, align 4
%tmp2 = icmp slt i32 %a, %b
br i1 %tmp2, label %true, label %false
true:
store i32 %a, i32* %tmp, align 4
%tmp4 = call i32 @doSomething(i32 0, i32* %tmp)
br label %false
false:
%tmp.0 = phi i32 [ %tmp4, %true ], [ %a, %0 ]
ret i32 %tmp.0
}
On AArch64 this code generates (removing the cfi directives to ease
readabilities):
_f: ; @f
; BB#0:
stp x29, x30, [sp, #-16]!
mov x29, sp
sub sp, sp, #16 ; =16
cmp w0, w1
b.ge LBB0_2
; BB#1: ; %true
stur w0, [x29, #-4]
sub x1, x29, #4 ; =4
mov w0, wzr
bl _doSomething
LBB0_2: ; %false
mov sp, x29
ldp x29, x30, [sp], #16
ret
With shrink-wrapping we could generate:
_f: ; @f
; BB#0:
cmp w0, w1
b.ge LBB0_2
; BB#1: ; %true
stp x29, x30, [sp, #-16]!
mov x29, sp
sub sp, sp, #16 ; =16
stur w0, [x29, #-4]
sub x1, x29, #4 ; =4
mov w0, wzr
bl _doSomething
add sp, x29, #16 ; =16
ldp x29, x30, [sp], #16
LBB0_2: ; %false
ret
Therefore, we would pay the overhead of setting up/destroying the frame only if
we actually do the call.
** Proposed Solution **
This patch introduces a new machine pass that perform the shrink-wrapping
analysis (See the comments at the beginning of ShrinkWrap.cpp for more details).
It then stores the safe save and restore point into the MachineFrameInfo
attached to the MachineFunction.
This information is then used by the PrologEpilogInserter (PEI) to place the
related code at the right place. This pass runs right before the PEI.
Unlike the original paper of Chow from PLDI’88, this implementation of
shrink-wrapping does not use expensive data-flow analysis and does not need hack
to properly avoid frequently executed point. Instead, it relies on dominance and
loop properties.
The pass is off by default and each target can opt-in by setting the
EnableShrinkWrap boolean to true in their derived class of TargetPassConfig.
This setting can also be overwritten on the command line by using
-enable-shrink-wrap.
Before you try out the pass for your target, make sure you properly fix your
emitProlog/emitEpilog/adjustForXXX method to cope with basic blocks that are not
necessarily the entry block.
** Design Decisions **
1. ShrinkWrap is its own pass right now. It could frankly be merged into PEI but
for debugging and clarity I thought it was best to have its own file.
2. Right now, we only support one save point and one restore point. At some
point we can expand this to several save point and restore point, the impacted
component would then be:
- The pass itself: New algorithm needed.
- MachineFrameInfo: Hold a list or set of Save/Restore point instead of one
pointer.
- PEI: Should loop over the save point and restore point.
Anyhow, at least for this first iteration, I do not believe this is interesting
to support the complex cases. We should revisit that when we motivating
examples.
Differential Revision: http://reviews.llvm.org/D9210
<rdar://problem/3201744>
llvm-svn: 236507
2015-05-06 01:38:16 +08:00
|
|
|
void Thumb1FrameLowering::emitPrologue(MachineFunction &MF,
|
|
|
|
MachineBasicBlock &MBB) const {
|
|
|
|
assert(&MBB == &MF.front() && "Shrink-wrapping not yet implemented");
|
2010-11-15 08:06:54 +08:00
|
|
|
MachineBasicBlock::iterator MBBI = MBB.begin();
|
|
|
|
MachineFrameInfo *MFI = MF.getFrameInfo();
|
|
|
|
ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
|
2014-02-15 01:19:07 +08:00
|
|
|
MachineModuleInfo &MMI = MF.getMMI();
|
|
|
|
const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
|
2015-03-13 06:48:50 +08:00
|
|
|
const ThumbRegisterInfo *RegInfo =
|
|
|
|
static_cast<const ThumbRegisterInfo *>(STI.getRegisterInfo());
|
2010-11-15 08:06:54 +08:00
|
|
|
const Thumb1InstrInfo &TII =
|
2015-01-29 08:19:33 +08:00
|
|
|
*static_cast<const Thumb1InstrInfo *>(STI.getInstrInfo());
|
2010-11-15 08:06:54 +08:00
|
|
|
|
2015-03-12 02:54:22 +08:00
|
|
|
unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize();
|
2010-11-15 08:06:54 +08:00
|
|
|
unsigned NumBytes = MFI->getStackSize();
|
2014-03-05 23:25:27 +08:00
|
|
|
assert(NumBytes >= ArgRegsSaveSize &&
|
|
|
|
"ArgRegsSaveSize is included in NumBytes");
|
2010-11-15 08:06:54 +08:00
|
|
|
const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
|
|
|
|
DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
|
|
|
|
unsigned FramePtr = RegInfo->getFrameRegister(MF);
|
|
|
|
unsigned BasePtr = RegInfo->getBaseRegister();
|
2014-02-15 01:19:07 +08:00
|
|
|
int CFAOffset = 0;
|
2010-11-15 08:06:54 +08:00
|
|
|
|
|
|
|
// Thumb add/sub sp, imm8 instructions implicitly multiply the offset by 4.
|
|
|
|
NumBytes = (NumBytes + 3) & ~3;
|
|
|
|
MFI->setStackSize(NumBytes);
|
|
|
|
|
|
|
|
// Determine the sizes of each callee-save spill areas and record which frame
|
|
|
|
// belongs to which callee-save spill areas.
|
|
|
|
unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
|
|
|
|
int FramePtrSpillFI = 0;
|
|
|
|
|
2014-02-15 01:19:07 +08:00
|
|
|
if (ArgRegsSaveSize) {
|
2013-04-30 15:19:58 +08:00
|
|
|
emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -ArgRegsSaveSize,
|
2011-03-06 02:43:50 +08:00
|
|
|
MachineInstr::FrameSetup);
|
2014-02-15 01:19:07 +08:00
|
|
|
CFAOffset -= ArgRegsSaveSize;
|
2014-03-07 14:08:31 +08:00
|
|
|
unsigned CFIIndex = MMI.addFrameInst(
|
|
|
|
MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
|
|
|
|
BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
|
2014-12-23 07:09:14 +08:00
|
|
|
.addCFIIndex(CFIIndex)
|
|
|
|
.setMIFlags(MachineInstr::FrameSetup);
|
2014-02-15 01:19:07 +08:00
|
|
|
}
|
2010-11-15 08:06:54 +08:00
|
|
|
|
|
|
|
if (!AFI->hasStackFrame()) {
|
2014-03-05 23:25:27 +08:00
|
|
|
if (NumBytes - ArgRegsSaveSize != 0) {
|
|
|
|
emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -(NumBytes - ArgRegsSaveSize),
|
2011-03-06 02:43:50 +08:00
|
|
|
MachineInstr::FrameSetup);
|
2014-03-05 23:25:27 +08:00
|
|
|
CFAOffset -= NumBytes - ArgRegsSaveSize;
|
2014-03-07 14:08:31 +08:00
|
|
|
unsigned CFIIndex = MMI.addFrameInst(
|
|
|
|
MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
|
|
|
|
BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
|
2014-12-23 07:09:14 +08:00
|
|
|
.addCFIIndex(CFIIndex)
|
|
|
|
.setMIFlags(MachineInstr::FrameSetup);
|
2014-02-15 01:19:07 +08:00
|
|
|
}
|
2010-11-15 08:06:54 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
|
|
|
|
unsigned Reg = CSI[i].getReg();
|
|
|
|
int FI = CSI[i].getFrameIdx();
|
|
|
|
switch (Reg) {
|
2014-02-15 01:19:07 +08:00
|
|
|
case ARM::R8:
|
|
|
|
case ARM::R9:
|
|
|
|
case ARM::R10:
|
|
|
|
case ARM::R11:
|
|
|
|
if (STI.isTargetMachO()) {
|
|
|
|
GPRCS2Size += 4;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// fallthrough
|
2010-11-15 08:06:54 +08:00
|
|
|
case ARM::R4:
|
|
|
|
case ARM::R5:
|
|
|
|
case ARM::R6:
|
|
|
|
case ARM::R7:
|
|
|
|
case ARM::LR:
|
|
|
|
if (Reg == FramePtr)
|
|
|
|
FramePtrSpillFI = FI;
|
|
|
|
GPRCS1Size += 4;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DPRCSSize += 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tPUSH) {
|
|
|
|
++MBBI;
|
|
|
|
if (MBBI != MBB.end())
|
|
|
|
dl = MBBI->getDebugLoc();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine starting offsets of spill areas.
|
2014-03-05 23:25:27 +08:00
|
|
|
unsigned DPRCSOffset = NumBytes - ArgRegsSaveSize - (GPRCS1Size + GPRCS2Size + DPRCSSize);
|
2010-11-15 08:06:54 +08:00
|
|
|
unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
|
|
|
|
unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size;
|
2013-02-20 20:21:33 +08:00
|
|
|
bool HasFP = hasFP(MF);
|
|
|
|
if (HasFP)
|
|
|
|
AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) +
|
|
|
|
NumBytes);
|
2010-11-15 08:06:54 +08:00
|
|
|
AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
|
|
|
|
AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
|
|
|
|
AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
|
|
|
|
NumBytes = DPRCSOffset;
|
Fix epilogue codegen to avoid leaving the stack pointer in an invalid
state. Previously Thumb2 would restore sp from fp like this:
mov sp, r7
sub, sp, #4
If an interrupt is taken after the 'mov' but before the 'sub', callee-saved
registers might be clobbered by the interrupt handler. Instead, try
restoring directly from sp:
add sp, #4
Or, if necessary (with VLA, etc.) use a scratch register to compute sp and
then restore it:
sub.w r4, r7, #8
mov sp, r7
rdar://8465407
llvm-svn: 119977
2010-11-23 02:12:04 +08:00
|
|
|
|
2013-11-09 01:18:07 +08:00
|
|
|
int FramePtrOffsetInBlock = 0;
|
2014-02-15 01:19:07 +08:00
|
|
|
unsigned adjustedGPRCS1Size = GPRCS1Size;
|
2014-03-02 20:27:27 +08:00
|
|
|
if (tryFoldSPUpdateIntoPushPop(STI, MF, std::prev(MBBI), NumBytes)) {
|
2013-11-09 01:18:07 +08:00
|
|
|
FramePtrOffsetInBlock = NumBytes;
|
2014-02-15 01:19:07 +08:00
|
|
|
adjustedGPRCS1Size += NumBytes;
|
2013-11-09 01:18:07 +08:00
|
|
|
NumBytes = 0;
|
|
|
|
}
|
|
|
|
|
2014-02-15 01:19:07 +08:00
|
|
|
if (adjustedGPRCS1Size) {
|
|
|
|
CFAOffset -= adjustedGPRCS1Size;
|
2014-03-07 14:08:31 +08:00
|
|
|
unsigned CFIIndex = MMI.addFrameInst(
|
|
|
|
MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
|
|
|
|
BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
|
2014-12-23 07:09:14 +08:00
|
|
|
.addCFIIndex(CFIIndex)
|
|
|
|
.setMIFlags(MachineInstr::FrameSetup);
|
2014-02-15 01:19:07 +08:00
|
|
|
}
|
|
|
|
for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
|
|
|
|
E = CSI.end(); I != E; ++I) {
|
|
|
|
unsigned Reg = I->getReg();
|
|
|
|
int FI = I->getFrameIdx();
|
|
|
|
switch (Reg) {
|
|
|
|
case ARM::R8:
|
|
|
|
case ARM::R9:
|
|
|
|
case ARM::R10:
|
|
|
|
case ARM::R11:
|
|
|
|
case ARM::R12:
|
|
|
|
if (STI.isTargetMachO())
|
|
|
|
break;
|
|
|
|
// fallthough
|
|
|
|
case ARM::R0:
|
|
|
|
case ARM::R1:
|
|
|
|
case ARM::R2:
|
|
|
|
case ARM::R3:
|
|
|
|
case ARM::R4:
|
|
|
|
case ARM::R5:
|
|
|
|
case ARM::R6:
|
|
|
|
case ARM::R7:
|
|
|
|
case ARM::LR:
|
2014-03-07 14:08:31 +08:00
|
|
|
unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
|
|
|
|
nullptr, MRI->getDwarfRegNum(Reg, true), MFI->getObjectOffset(FI)));
|
|
|
|
BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
|
2014-12-23 07:09:14 +08:00
|
|
|
.addCFIIndex(CFIIndex)
|
|
|
|
.setMIFlags(MachineInstr::FrameSetup);
|
2014-02-15 01:19:07 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
Fix epilogue codegen to avoid leaving the stack pointer in an invalid
state. Previously Thumb2 would restore sp from fp like this:
mov sp, r7
sub, sp, #4
If an interrupt is taken after the 'mov' but before the 'sub', callee-saved
registers might be clobbered by the interrupt handler. Instead, try
restoring directly from sp:
add sp, #4
Or, if necessary (with VLA, etc.) use a scratch register to compute sp and
then restore it:
sub.w r4, r7, #8
mov sp, r7
rdar://8465407
llvm-svn: 119977
2010-11-23 02:12:04 +08:00
|
|
|
// Adjust FP so it point to the stack slot that contains the previous FP.
|
2013-02-20 20:21:33 +08:00
|
|
|
if (HasFP) {
|
2014-03-05 23:25:27 +08:00
|
|
|
FramePtrOffsetInBlock += MFI->getObjectOffset(FramePtrSpillFI)
|
|
|
|
+ GPRCS1Size + ArgRegsSaveSize;
|
2011-08-25 01:46:13 +08:00
|
|
|
AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tADDrSPi), FramePtr)
|
2013-11-09 01:18:07 +08:00
|
|
|
.addReg(ARM::SP).addImm(FramePtrOffsetInBlock / 4)
|
2011-08-25 01:46:13 +08:00
|
|
|
.setMIFlags(MachineInstr::FrameSetup));
|
2014-02-15 01:19:07 +08:00
|
|
|
if(FramePtrOffsetInBlock) {
|
|
|
|
CFAOffset += FramePtrOffsetInBlock;
|
2014-03-07 14:08:31 +08:00
|
|
|
unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createDefCfa(
|
|
|
|
nullptr, MRI->getDwarfRegNum(FramePtr, true), CFAOffset));
|
|
|
|
BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
|
2014-12-23 07:09:14 +08:00
|
|
|
.addCFIIndex(CFIIndex)
|
|
|
|
.setMIFlags(MachineInstr::FrameSetup);
|
2014-03-07 14:08:31 +08:00
|
|
|
} else {
|
|
|
|
unsigned CFIIndex =
|
|
|
|
MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(
|
|
|
|
nullptr, MRI->getDwarfRegNum(FramePtr, true)));
|
|
|
|
BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
|
2014-12-23 07:09:14 +08:00
|
|
|
.addCFIIndex(CFIIndex)
|
|
|
|
.setMIFlags(MachineInstr::FrameSetup);
|
2014-03-07 14:08:31 +08:00
|
|
|
}
|
2011-06-14 05:18:25 +08:00
|
|
|
if (NumBytes > 508)
|
|
|
|
// If offset is > 508 then sp cannot be adjusted in a single instruction,
|
Fix epilogue codegen to avoid leaving the stack pointer in an invalid
state. Previously Thumb2 would restore sp from fp like this:
mov sp, r7
sub, sp, #4
If an interrupt is taken after the 'mov' but before the 'sub', callee-saved
registers might be clobbered by the interrupt handler. Instead, try
restoring directly from sp:
add sp, #4
Or, if necessary (with VLA, etc.) use a scratch register to compute sp and
then restore it:
sub.w r4, r7, #8
mov sp, r7
rdar://8465407
llvm-svn: 119977
2010-11-23 02:12:04 +08:00
|
|
|
// try restoring from fp instead.
|
|
|
|
AFI->setShouldRestoreSPFromFP(true);
|
|
|
|
}
|
|
|
|
|
2014-02-15 01:19:07 +08:00
|
|
|
if (NumBytes) {
|
2010-11-15 08:06:54 +08:00
|
|
|
// Insert it after all the callee-save spills.
|
2011-03-06 02:43:50 +08:00
|
|
|
emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -NumBytes,
|
|
|
|
MachineInstr::FrameSetup);
|
2014-02-15 01:19:07 +08:00
|
|
|
if (!HasFP) {
|
|
|
|
CFAOffset -= NumBytes;
|
2014-03-07 14:08:31 +08:00
|
|
|
unsigned CFIIndex = MMI.addFrameInst(
|
|
|
|
MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
|
|
|
|
BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
|
2014-12-23 07:09:14 +08:00
|
|
|
.addCFIIndex(CFIIndex)
|
|
|
|
.setMIFlags(MachineInstr::FrameSetup);
|
2014-02-15 01:19:07 +08:00
|
|
|
}
|
|
|
|
}
|
2010-11-15 08:06:54 +08:00
|
|
|
|
2013-02-20 20:21:33 +08:00
|
|
|
if (STI.isTargetELF() && HasFP)
|
2010-11-15 08:06:54 +08:00
|
|
|
MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() -
|
|
|
|
AFI->getFramePtrSpillOffset());
|
|
|
|
|
|
|
|
AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
|
|
|
|
AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
|
|
|
|
AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
|
|
|
|
|
2011-10-20 08:07:12 +08:00
|
|
|
// Thumb1 does not currently support dynamic stack realignment. Report a
|
|
|
|
// fatal error rather then silently generate bad code.
|
|
|
|
if (RegInfo->needsStackRealignment(MF))
|
|
|
|
report_fatal_error("Dynamic stack realignment not supported for thumb1.");
|
2011-10-15 08:28:24 +08:00
|
|
|
|
2010-11-15 08:06:54 +08:00
|
|
|
// If we need a base pointer, set it up here. It's whatever the value
|
|
|
|
// of the stack pointer is at this point. Any variable size objects
|
|
|
|
// will be allocated after this, so we can still use the base pointer
|
|
|
|
// to reference locals.
|
|
|
|
if (RegInfo->hasBasePointer(MF))
|
2011-07-01 07:38:17 +08:00
|
|
|
AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), BasePtr)
|
2011-07-01 06:10:46 +08:00
|
|
|
.addReg(ARM::SP));
|
2011-03-06 02:43:50 +08:00
|
|
|
|
2011-01-11 08:16:04 +08:00
|
|
|
// If the frame has variable sized objects then the epilogue must restore
|
|
|
|
// the sp from fp. We can assume there's an FP here since hasFP already
|
|
|
|
// checks for hasVarSizedObjects.
|
|
|
|
if (MFI->hasVarSizedObjects())
|
|
|
|
AFI->setShouldRestoreSPFromFP(true);
|
2010-11-15 08:06:54 +08:00
|
|
|
}
|
|
|
|
|
2014-04-04 13:16:06 +08:00
|
|
|
static bool isCSRestore(MachineInstr *MI, const MCPhysReg *CSRegs) {
|
2011-06-30 04:26:39 +08:00
|
|
|
if (MI->getOpcode() == ARM::tLDRspi &&
|
2010-11-15 08:06:54 +08:00
|
|
|
MI->getOperand(1).isFI() &&
|
|
|
|
isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs))
|
|
|
|
return true;
|
|
|
|
else if (MI->getOpcode() == ARM::tPOP) {
|
|
|
|
// The first two operands are predicates. The last two are
|
|
|
|
// imp-def and imp-use of SP. Check everything in between.
|
|
|
|
for (int i = 2, e = MI->getNumOperands() - 2; i != e; ++i)
|
|
|
|
if (!isCalleeSavedRegister(MI->getOperand(i).getReg(), CSRegs))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-01-10 20:39:04 +08:00
|
|
|
void Thumb1FrameLowering::emitEpilogue(MachineFunction &MF,
|
2010-11-15 08:06:54 +08:00
|
|
|
MachineBasicBlock &MBB) const {
|
2015-07-21 05:42:14 +08:00
|
|
|
MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
|
|
|
|
DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
|
2010-11-15 08:06:54 +08:00
|
|
|
MachineFrameInfo *MFI = MF.getFrameInfo();
|
|
|
|
ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
|
2015-03-13 06:48:50 +08:00
|
|
|
const ThumbRegisterInfo *RegInfo =
|
|
|
|
static_cast<const ThumbRegisterInfo *>(STI.getRegisterInfo());
|
2010-11-15 08:06:54 +08:00
|
|
|
const Thumb1InstrInfo &TII =
|
2015-01-29 08:19:33 +08:00
|
|
|
*static_cast<const Thumb1InstrInfo *>(STI.getInstrInfo());
|
2010-11-15 08:06:54 +08:00
|
|
|
|
2015-03-12 02:54:22 +08:00
|
|
|
unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize();
|
2010-11-15 08:06:54 +08:00
|
|
|
int NumBytes = (int)MFI->getStackSize();
|
2014-03-06 02:53:36 +08:00
|
|
|
assert((unsigned)NumBytes >= ArgRegsSaveSize &&
|
2014-03-05 23:25:27 +08:00
|
|
|
"ArgRegsSaveSize is included in NumBytes");
|
2015-03-12 05:41:28 +08:00
|
|
|
const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF);
|
2010-11-15 08:06:54 +08:00
|
|
|
unsigned FramePtr = RegInfo->getFrameRegister(MF);
|
|
|
|
|
|
|
|
if (!AFI->hasStackFrame()) {
|
2014-03-05 23:25:27 +08:00
|
|
|
if (NumBytes - ArgRegsSaveSize != 0)
|
|
|
|
emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, NumBytes - ArgRegsSaveSize);
|
2010-11-15 08:06:54 +08:00
|
|
|
} else {
|
|
|
|
// Unwind MBBI to point to first LDR / VLDRD.
|
|
|
|
if (MBBI != MBB.begin()) {
|
|
|
|
do
|
|
|
|
--MBBI;
|
|
|
|
while (MBBI != MBB.begin() && isCSRestore(MBBI, CSRegs));
|
|
|
|
if (!isCSRestore(MBBI, CSRegs))
|
|
|
|
++MBBI;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move SP to start of FP callee save spill area.
|
|
|
|
NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
|
|
|
|
AFI->getGPRCalleeSavedArea2Size() +
|
2014-03-05 23:25:27 +08:00
|
|
|
AFI->getDPRCalleeSavedAreaSize() +
|
|
|
|
ArgRegsSaveSize);
|
2010-11-15 08:06:54 +08:00
|
|
|
|
|
|
|
if (AFI->shouldRestoreSPFromFP()) {
|
|
|
|
NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
|
|
|
|
// Reset SP based on frame pointer only if the stack frame extends beyond
|
2011-01-11 08:16:04 +08:00
|
|
|
// frame pointer stack slot, the target is ELF and the function has FP, or
|
|
|
|
// the target uses var sized objects.
|
Fix epilogue codegen to avoid leaving the stack pointer in an invalid
state. Previously Thumb2 would restore sp from fp like this:
mov sp, r7
sub, sp, #4
If an interrupt is taken after the 'mov' but before the 'sub', callee-saved
registers might be clobbered by the interrupt handler. Instead, try
restoring directly from sp:
add sp, #4
Or, if necessary (with VLA, etc.) use a scratch register to compute sp and
then restore it:
sub.w r4, r7, #8
mov sp, r7
rdar://8465407
llvm-svn: 119977
2010-11-23 02:12:04 +08:00
|
|
|
if (NumBytes) {
|
2015-07-15 01:17:13 +08:00
|
|
|
assert(!MFI->getPristineRegs(MF).test(ARM::R4) &&
|
Fix epilogue codegen to avoid leaving the stack pointer in an invalid
state. Previously Thumb2 would restore sp from fp like this:
mov sp, r7
sub, sp, #4
If an interrupt is taken after the 'mov' but before the 'sub', callee-saved
registers might be clobbered by the interrupt handler. Instead, try
restoring directly from sp:
add sp, #4
Or, if necessary (with VLA, etc.) use a scratch register to compute sp and
then restore it:
sub.w r4, r7, #8
mov sp, r7
rdar://8465407
llvm-svn: 119977
2010-11-23 02:12:04 +08:00
|
|
|
"No scratch register to restore SP from FP!");
|
2011-03-06 02:43:32 +08:00
|
|
|
emitThumbRegPlusImmediate(MBB, MBBI, dl, ARM::R4, FramePtr, -NumBytes,
|
|
|
|
TII, *RegInfo);
|
2011-07-01 07:38:17 +08:00
|
|
|
AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
|
2011-07-01 06:10:46 +08:00
|
|
|
ARM::SP)
|
|
|
|
.addReg(ARM::R4));
|
Fix epilogue codegen to avoid leaving the stack pointer in an invalid
state. Previously Thumb2 would restore sp from fp like this:
mov sp, r7
sub, sp, #4
If an interrupt is taken after the 'mov' but before the 'sub', callee-saved
registers might be clobbered by the interrupt handler. Instead, try
restoring directly from sp:
add sp, #4
Or, if necessary (with VLA, etc.) use a scratch register to compute sp and
then restore it:
sub.w r4, r7, #8
mov sp, r7
rdar://8465407
llvm-svn: 119977
2010-11-23 02:12:04 +08:00
|
|
|
} else
|
2011-07-01 07:38:17 +08:00
|
|
|
AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
|
2011-07-01 06:10:46 +08:00
|
|
|
ARM::SP)
|
|
|
|
.addReg(FramePtr));
|
2010-11-15 08:06:54 +08:00
|
|
|
} else {
|
2015-07-21 05:42:14 +08:00
|
|
|
if (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tBX_RET &&
|
|
|
|
&MBB.front() != MBBI && std::prev(MBBI)->getOpcode() == ARM::tPOP) {
|
2014-03-02 20:27:27 +08:00
|
|
|
MachineBasicBlock::iterator PMBBI = std::prev(MBBI);
|
2013-12-02 22:46:26 +08:00
|
|
|
if (!tryFoldSPUpdateIntoPushPop(STI, MF, PMBBI, NumBytes))
|
2013-11-09 01:18:07 +08:00
|
|
|
emitSPUpdate(MBB, PMBBI, TII, dl, *RegInfo, NumBytes);
|
2013-12-02 22:46:26 +08:00
|
|
|
} else if (!tryFoldSPUpdateIntoPushPop(STI, MF, MBBI, NumBytes))
|
2010-11-15 08:06:54 +08:00
|
|
|
emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, NumBytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-06 05:32:21 +08:00
|
|
|
bool IsV4PopReturn = false;
|
|
|
|
for (const CalleeSavedInfo &CSI : MFI->getCalleeSavedInfo())
|
|
|
|
if (CSI.getReg() == ARM::LR)
|
|
|
|
IsV4PopReturn = true;
|
|
|
|
IsV4PopReturn &= STI.hasV4TOps() && !STI.hasV5TOps();
|
|
|
|
|
|
|
|
// Unlike T2 and ARM mode, the T1 pop instruction cannot restore
|
|
|
|
// to LR, and we can't pop the value directly to the PC since
|
|
|
|
// we need to update the SP after popping the value. So instead
|
|
|
|
// we have to emit:
|
|
|
|
// POP {r3}
|
|
|
|
// ADD sp, #offset
|
|
|
|
// BX r3
|
|
|
|
// If this would clobber a return value, then generate this sequence instead:
|
|
|
|
// MOV ip, r3
|
|
|
|
// POP {r3}
|
|
|
|
// ADD sp, #offset
|
|
|
|
// MOV lr, r3
|
|
|
|
// MOV r3, ip
|
|
|
|
// BX lr
|
|
|
|
if (ArgRegsSaveSize || IsV4PopReturn) {
|
2015-07-21 05:42:14 +08:00
|
|
|
// If MBBI is a return instruction, we may be able to directly restore
|
|
|
|
// LR in the PC.
|
|
|
|
// This is possible if we do not need to emit any SP update.
|
|
|
|
// Otherwise, we need a temporary register to pop the value
|
|
|
|
// and copy that value into LR.
|
|
|
|
MBBI = MBB.getFirstTerminator();
|
|
|
|
if (!ArgRegsSaveSize && MBBI != MBB.end() &&
|
|
|
|
MBBI->getOpcode() == ARM::tBX_RET) {
|
2014-08-06 05:32:21 +08:00
|
|
|
MachineInstrBuilder MIB =
|
2015-07-21 05:42:14 +08:00
|
|
|
AddDefaultPred(
|
|
|
|
BuildMI(MBB, MBBI, MBBI->getDebugLoc(), TII.get(ARM::tPOP_RET)))
|
|
|
|
.addReg(ARM::PC, RegState::Define);
|
2014-08-06 05:32:21 +08:00
|
|
|
MIB.copyImplicitOps(&*MBBI);
|
|
|
|
// erase the old tBX_RET instruction
|
|
|
|
MBB.erase(MBBI);
|
2015-07-21 05:42:14 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look for a temporary register to use.
|
|
|
|
// First, compute the liveness information.
|
|
|
|
LivePhysRegs UsedRegs(STI.getRegisterInfo());
|
|
|
|
UsedRegs.addLiveOuts(&MBB, /*AddPristines*/ true);
|
|
|
|
// The semantic of pristines changed recently and now,
|
|
|
|
// the callee-saved registers that are touched in the function
|
|
|
|
// are not part of the pristines set anymore.
|
|
|
|
// Add those callee-saved now.
|
|
|
|
const TargetRegisterInfo *TRI = STI.getRegisterInfo();
|
|
|
|
const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF);
|
|
|
|
for (unsigned i = 0; CSRegs[i]; ++i)
|
|
|
|
UsedRegs.addReg(CSRegs[i]);
|
|
|
|
|
|
|
|
DebugLoc dl = DebugLoc();
|
|
|
|
if (MBBI != MBB.end()) {
|
|
|
|
dl = MBBI->getDebugLoc();
|
|
|
|
auto InstUpToMBBI = MBB.end();
|
|
|
|
// The post-decrement is on purpose here.
|
|
|
|
// We want to have the liveness right before MBBI.
|
|
|
|
while (InstUpToMBBI-- != MBBI)
|
|
|
|
UsedRegs.stepBackward(*InstUpToMBBI);
|
|
|
|
}
|
2014-08-06 05:32:21 +08:00
|
|
|
|
2015-07-21 05:42:14 +08:00
|
|
|
// Look for a register that can be directly use in the POP.
|
|
|
|
unsigned PopReg = 0;
|
|
|
|
// And some temporary register, just in case.
|
|
|
|
unsigned TemporaryReg = 0;
|
|
|
|
BitVector PopFriendly =
|
|
|
|
TRI->getAllocatableSet(MF, TRI->getRegClass(ARM::tGPRRegClassID));
|
|
|
|
assert(PopFriendly.any() && "No allocatable pop-friendly register?!");
|
|
|
|
// Rebuild the GPRs from the high registers because they are removed
|
|
|
|
// form the GPR reg class for thumb1.
|
|
|
|
BitVector GPRsNoLRSP =
|
|
|
|
TRI->getAllocatableSet(MF, TRI->getRegClass(ARM::hGPRRegClassID));
|
|
|
|
GPRsNoLRSP |= PopFriendly;
|
|
|
|
GPRsNoLRSP.reset(ARM::LR);
|
|
|
|
GPRsNoLRSP.reset(ARM::SP);
|
|
|
|
GPRsNoLRSP.reset(ARM::PC);
|
|
|
|
for (int Register = GPRsNoLRSP.find_first(); Register != -1;
|
|
|
|
Register = GPRsNoLRSP.find_next(Register)) {
|
|
|
|
if (!UsedRegs.contains(Register)) {
|
|
|
|
// Remember the first pop-friendly register and exit.
|
|
|
|
if (PopFriendly.test(Register)) {
|
|
|
|
PopReg = Register;
|
|
|
|
TemporaryReg = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Otherwise, remember that the register will be available to
|
|
|
|
// save a pop-friendly register.
|
|
|
|
TemporaryReg = Register;
|
|
|
|
}
|
|
|
|
}
|
2014-08-06 05:32:21 +08:00
|
|
|
|
2015-07-21 05:42:14 +08:00
|
|
|
assert((PopReg || TemporaryReg) && "Cannot get LR");
|
2014-08-06 05:32:21 +08:00
|
|
|
|
2015-07-21 05:42:14 +08:00
|
|
|
if (TemporaryReg) {
|
|
|
|
assert(!PopReg && "Unnecessary MOV is about to be inserted");
|
|
|
|
PopReg = PopFriendly.find_first();
|
2014-08-06 05:32:21 +08:00
|
|
|
AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr))
|
2015-07-21 05:42:14 +08:00
|
|
|
.addReg(TemporaryReg, RegState::Define)
|
|
|
|
.addReg(PopReg, RegState::Kill));
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(PopReg && "Do not know how to get LR");
|
|
|
|
AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tPOP)))
|
|
|
|
.addReg(PopReg, RegState::Define);
|
|
|
|
|
|
|
|
emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, ArgRegsSaveSize);
|
|
|
|
|
|
|
|
if (!TemporaryReg && MBBI != MBB.end() &&
|
|
|
|
MBBI->getOpcode() == ARM::tBX_RET) {
|
|
|
|
MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII.get(ARM::tBX))
|
|
|
|
.addReg(PopReg, RegState::Kill);
|
|
|
|
AddDefaultPred(MIB);
|
|
|
|
MIB.copyImplicitOps(&*MBBI);
|
|
|
|
// erase the old tBX_RET instruction
|
|
|
|
MBB.erase(MBBI);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr))
|
|
|
|
.addReg(ARM::LR, RegState::Define)
|
|
|
|
.addReg(PopReg, RegState::Kill));
|
2014-08-06 05:32:21 +08:00
|
|
|
|
2015-07-21 05:42:14 +08:00
|
|
|
if (TemporaryReg) {
|
2014-08-06 05:32:21 +08:00
|
|
|
AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr))
|
2015-07-21 05:42:14 +08:00
|
|
|
.addReg(PopReg, RegState::Define)
|
|
|
|
.addReg(TemporaryReg, RegState::Kill));
|
2014-08-06 05:32:21 +08:00
|
|
|
}
|
2010-11-15 08:06:54 +08:00
|
|
|
}
|
|
|
|
}
|
2010-11-28 07:05:03 +08:00
|
|
|
|
2011-01-10 20:39:04 +08:00
|
|
|
bool Thumb1FrameLowering::
|
2010-11-28 07:05:03 +08:00
|
|
|
spillCalleeSavedRegisters(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MI,
|
|
|
|
const std::vector<CalleeSavedInfo> &CSI,
|
|
|
|
const TargetRegisterInfo *TRI) const {
|
|
|
|
if (CSI.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
DebugLoc DL;
|
2015-01-29 08:19:33 +08:00
|
|
|
const TargetInstrInfo &TII = *STI.getInstrInfo();
|
2010-11-28 07:05:03 +08:00
|
|
|
|
|
|
|
if (MI != MBB.end()) DL = MI->getDebugLoc();
|
|
|
|
|
|
|
|
MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(ARM::tPUSH));
|
|
|
|
AddDefaultPred(MIB);
|
|
|
|
for (unsigned i = CSI.size(); i != 0; --i) {
|
|
|
|
unsigned Reg = CSI[i-1].getReg();
|
|
|
|
bool isKill = true;
|
|
|
|
|
|
|
|
// Add the callee-saved register as live-in unless it's LR and
|
|
|
|
// @llvm.returnaddress is called. If LR is returned for @llvm.returnaddress
|
|
|
|
// then it's already added to the function and entry block live-in sets.
|
|
|
|
if (Reg == ARM::LR) {
|
|
|
|
MachineFunction &MF = *MBB.getParent();
|
|
|
|
if (MF.getFrameInfo()->isReturnAddressTaken() &&
|
|
|
|
MF.getRegInfo().isLiveIn(Reg))
|
|
|
|
isKill = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isKill)
|
|
|
|
MBB.addLiveIn(Reg);
|
|
|
|
|
|
|
|
MIB.addReg(Reg, getKillRegState(isKill));
|
|
|
|
}
|
2011-03-06 02:43:50 +08:00
|
|
|
MIB.setMIFlags(MachineInstr::FrameSetup);
|
2010-11-28 07:05:03 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-01-10 20:39:04 +08:00
|
|
|
bool Thumb1FrameLowering::
|
2010-11-28 07:05:03 +08:00
|
|
|
restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MI,
|
|
|
|
const std::vector<CalleeSavedInfo> &CSI,
|
|
|
|
const TargetRegisterInfo *TRI) const {
|
|
|
|
if (CSI.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
MachineFunction &MF = *MBB.getParent();
|
|
|
|
ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
|
2015-01-29 08:19:33 +08:00
|
|
|
const TargetInstrInfo &TII = *STI.getInstrInfo();
|
2010-11-28 07:05:03 +08:00
|
|
|
|
2013-04-30 15:19:58 +08:00
|
|
|
bool isVarArg = AFI->getArgRegsSaveSize() > 0;
|
2010-11-28 07:05:03 +08:00
|
|
|
DebugLoc DL = MI->getDebugLoc();
|
|
|
|
MachineInstrBuilder MIB = BuildMI(MF, DL, TII.get(ARM::tPOP));
|
|
|
|
AddDefaultPred(MIB);
|
|
|
|
|
|
|
|
bool NumRegs = false;
|
|
|
|
for (unsigned i = CSI.size(); i != 0; --i) {
|
|
|
|
unsigned Reg = CSI[i-1].getReg();
|
2015-07-21 05:42:14 +08:00
|
|
|
if (Reg == ARM::LR && MBB.succ_empty()) {
|
2010-11-28 07:05:03 +08:00
|
|
|
// Special epilogue for vararg functions. See emitEpilogue
|
|
|
|
if (isVarArg)
|
|
|
|
continue;
|
2014-08-06 05:32:21 +08:00
|
|
|
// ARMv4T requires BX, see emitEpilogue
|
|
|
|
if (STI.hasV4TOps() && !STI.hasV5TOps())
|
|
|
|
continue;
|
2010-11-28 07:05:03 +08:00
|
|
|
Reg = ARM::PC;
|
|
|
|
(*MIB).setDesc(TII.get(ARM::tPOP_RET));
|
2015-07-21 05:42:14 +08:00
|
|
|
if (MI != MBB.end())
|
|
|
|
MIB.copyImplicitOps(&*MI);
|
2010-11-28 07:05:03 +08:00
|
|
|
MI = MBB.erase(MI);
|
|
|
|
}
|
|
|
|
MIB.addReg(Reg, getDefRegState(true));
|
|
|
|
NumRegs = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// It's illegal to emit pop instruction without operands.
|
|
|
|
if (NumRegs)
|
|
|
|
MBB.insert(MI, &*MIB);
|
|
|
|
else
|
|
|
|
MF.DeleteMachineInstr(MIB);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|