2012-02-18 20:03:15 +08:00
|
|
|
//===-- ARMFrameLowering.cpp - ARM 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 ARM implementation of TargetFrameLowering class.
|
2010-11-15 08:06:54 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-01-10 20:39:04 +08:00
|
|
|
#include "ARMFrameLowering.h"
|
2010-11-15 08:06:54 +08:00
|
|
|
#include "ARMBaseInstrInfo.h"
|
2011-01-12 05:46:47 +08:00
|
|
|
#include "ARMBaseRegisterInfo.h"
|
2014-04-03 00:10:33 +08:00
|
|
|
#include "ARMConstantPoolValue.h"
|
2010-11-15 08:06:54 +08:00
|
|
|
#include "ARMMachineFunctionInfo.h"
|
2011-07-21 07:34:39 +08:00
|
|
|
#include "MCTargetDesc/ARMAddressingModes.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-28 07:05:25 +08:00
|
|
|
#include "llvm/CodeGen/RegisterScavenging.h"
|
2015-10-29 06:46:43 +08:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/CallingConv.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
2014-02-15 01:19:07 +08:00
|
|
|
#include "llvm/MC/MCContext.h"
|
2011-12-23 08:36:18 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Target/TargetOptions.h"
|
2010-11-15 08:06:54 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2012-02-25 06:09:25 +08:00
|
|
|
static cl::opt<bool>
|
2012-01-07 06:19:37 +08:00
|
|
|
SpillAlignedNEONRegs("align-neon-spills", cl::Hidden, cl::init(true),
|
2011-12-23 08:36:18 +08:00
|
|
|
cl::desc("Align ARM NEON spills in prolog and epilog"));
|
|
|
|
|
|
|
|
static MachineBasicBlock::iterator
|
|
|
|
skipAlignedDPRCS2Spills(MachineBasicBlock::iterator MI,
|
|
|
|
unsigned NumAlignedDPRCS2Regs);
|
|
|
|
|
2014-06-27 03:29:59 +08:00
|
|
|
ARMFrameLowering::ARMFrameLowering(const ARMSubtarget &sti)
|
|
|
|
: TargetFrameLowering(StackGrowsDown, sti.getStackAlignment(), 0, 4),
|
|
|
|
STI(sti) {}
|
|
|
|
|
2015-05-23 09:14:08 +08:00
|
|
|
bool ARMFrameLowering::noFramePointerElim(const MachineFunction &MF) const {
|
|
|
|
// iOS always has a FP for backtracking, force other targets to keep their FP
|
|
|
|
// when doing FastISel. The emitted code is currently superior, and in cases
|
|
|
|
// like test-suite's lencod FastISel isn't quite correct when FP is eliminated.
|
|
|
|
return TargetFrameLowering::noFramePointerElim(MF) ||
|
|
|
|
MF.getSubtarget<ARMSubtarget>().useFastISel();
|
|
|
|
}
|
|
|
|
|
2010-11-19 05:19:35 +08:00
|
|
|
/// hasFP - Return true if the specified function should have a dedicated frame
|
|
|
|
/// pointer register. This is true if the function has variable sized allocas
|
|
|
|
/// or if frame pointer elimination is disabled.
|
2011-01-10 20:39:04 +08:00
|
|
|
bool ARMFrameLowering::hasFP(const MachineFunction &MF) const {
|
2014-08-05 10:39:49 +08:00
|
|
|
const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
|
2010-11-19 05:19:35 +08:00
|
|
|
|
2012-01-04 09:55:04 +08:00
|
|
|
// iOS requires FP not to be clobbered for backtracing purpose.
|
2015-10-29 06:46:43 +08:00
|
|
|
if (STI.isTargetIOS() || STI.isTargetWatchOS())
|
2010-11-19 05:19:35 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
const MachineFrameInfo *MFI = MF.getFrameInfo();
|
|
|
|
// Always eliminate non-leaf frame pointers.
|
2011-12-03 06:16:29 +08:00
|
|
|
return ((MF.getTarget().Options.DisableFramePointerElim(MF) &&
|
|
|
|
MFI->hasCalls()) ||
|
2010-11-19 05:19:35 +08:00
|
|
|
RegInfo->needsStackRealignment(MF) ||
|
|
|
|
MFI->hasVarSizedObjects() ||
|
|
|
|
MFI->isFrameAddressTaken());
|
|
|
|
}
|
|
|
|
|
2011-01-14 05:10:12 +08:00
|
|
|
/// hasReservedCallFrame - Under normal circumstances, when a frame pointer is
|
|
|
|
/// not required, we reserve argument space for call sites in the function
|
|
|
|
/// immediately on entry to the current function. This eliminates the need for
|
|
|
|
/// add/sub sp brackets around call sites. Returns true if the call frame is
|
|
|
|
/// included as part of the stack frame.
|
2011-01-10 20:39:04 +08:00
|
|
|
bool ARMFrameLowering::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 << 12) - 1) / 2) // Half of imm12
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return !MF.getFrameInfo()->hasVarSizedObjects();
|
|
|
|
}
|
|
|
|
|
2011-01-14 05:10:12 +08:00
|
|
|
/// canSimplifyCallFramePseudos - If there is a reserved call frame, the
|
|
|
|
/// call frame pseudos can be simplified. Unlike most targets, having a FP
|
|
|
|
/// is not sufficient here since we still may reference some objects via SP
|
|
|
|
/// even when FP is available in Thumb2 mode.
|
|
|
|
bool
|
|
|
|
ARMFrameLowering::canSimplifyCallFramePseudos(const MachineFunction &MF) const {
|
2010-11-19 05:19:35 +08:00
|
|
|
return hasReservedCallFrame(MF) || MF.getFrameInfo()->hasVarSizedObjects();
|
|
|
|
}
|
|
|
|
|
2010-11-15 08:06:54 +08:00
|
|
|
static bool isCSRestore(MachineInstr *MI,
|
|
|
|
const ARMBaseInstrInfo &TII,
|
2014-04-04 13:16:06 +08:00
|
|
|
const MCPhysReg *CSRegs) {
|
2010-11-19 03:40:05 +08:00
|
|
|
// Integer spill area is handled with "pop".
|
2013-11-09 01:18:07 +08:00
|
|
|
if (isPopOpcode(MI->getOpcode())) {
|
2010-11-19 03:40:05 +08:00
|
|
|
// The first two operands are predicates. The last two are
|
|
|
|
// imp-def and imp-use of SP. Check everything in between.
|
|
|
|
for (int i = 5, e = MI->getNumOperands(); i != e; ++i)
|
|
|
|
if (!isCalleeSavedRegister(MI->getOperand(i).getReg(), CSRegs))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2011-07-27 04:54:26 +08:00
|
|
|
if ((MI->getOpcode() == ARM::LDR_POST_IMM ||
|
|
|
|
MI->getOpcode() == ARM::LDR_POST_REG ||
|
2010-12-11 02:41:15 +08:00
|
|
|
MI->getOpcode() == ARM::t2LDR_POST) &&
|
|
|
|
isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs) &&
|
|
|
|
MI->getOperand(1).getReg() == ARM::SP)
|
|
|
|
return true;
|
2010-11-19 03:40:05 +08:00
|
|
|
|
|
|
|
return false;
|
2010-11-15 08:06:54 +08:00
|
|
|
}
|
|
|
|
|
2013-11-05 07:04:15 +08:00
|
|
|
static void emitRegPlusImmediate(bool isARM, MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator &MBBI, DebugLoc dl,
|
|
|
|
const ARMBaseInstrInfo &TII, unsigned DestReg,
|
|
|
|
unsigned SrcReg, int NumBytes,
|
|
|
|
unsigned MIFlags = MachineInstr::NoFlags,
|
|
|
|
ARMCC::CondCodes Pred = ARMCC::AL,
|
|
|
|
unsigned PredReg = 0) {
|
2010-11-15 08:06:54 +08:00
|
|
|
if (isARM)
|
2013-11-05 07:04:15 +08:00
|
|
|
emitARMRegPlusImmediate(MBB, MBBI, dl, DestReg, SrcReg, NumBytes,
|
2013-02-22 04:05:00 +08:00
|
|
|
Pred, PredReg, TII, MIFlags);
|
2010-11-15 08:06:54 +08:00
|
|
|
else
|
2013-11-05 07:04:15 +08:00
|
|
|
emitT2RegPlusImmediate(MBB, MBBI, dl, DestReg, SrcReg, NumBytes,
|
2013-02-22 04:05:00 +08:00
|
|
|
Pred, PredReg, TII, MIFlags);
|
2010-11-15 08:06:54 +08:00
|
|
|
}
|
|
|
|
|
2013-11-05 07:04:15 +08:00
|
|
|
static void emitSPUpdate(bool isARM, MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator &MBBI, DebugLoc dl,
|
|
|
|
const ARMBaseInstrInfo &TII, int NumBytes,
|
|
|
|
unsigned MIFlags = MachineInstr::NoFlags,
|
|
|
|
ARMCC::CondCodes Pred = ARMCC::AL,
|
|
|
|
unsigned PredReg = 0) {
|
|
|
|
emitRegPlusImmediate(isARM, MBB, MBBI, dl, TII, ARM::SP, ARM::SP, NumBytes,
|
|
|
|
MIFlags, Pred, PredReg);
|
|
|
|
}
|
|
|
|
|
2014-02-15 01:19:07 +08:00
|
|
|
static int sizeOfSPAdjustment(const MachineInstr *MI) {
|
2014-11-15 06:45:33 +08:00
|
|
|
int RegSize;
|
|
|
|
switch (MI->getOpcode()) {
|
|
|
|
case ARM::VSTMDDB_UPD:
|
|
|
|
RegSize = 8;
|
|
|
|
break;
|
|
|
|
case ARM::STMDB_UPD:
|
|
|
|
case ARM::t2STMDB_UPD:
|
|
|
|
RegSize = 4;
|
|
|
|
break;
|
|
|
|
case ARM::t2STR_PRE:
|
|
|
|
case ARM::STR_PRE_IMM:
|
|
|
|
return 4;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unknown push or pop like instruction");
|
|
|
|
}
|
|
|
|
|
2014-02-15 01:19:07 +08:00
|
|
|
int count = 0;
|
|
|
|
// ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+
|
|
|
|
// pred) so the list starts at 4.
|
|
|
|
for (int i = MI->getNumOperands() - 1; i >= 4; --i)
|
2014-11-15 06:45:33 +08:00
|
|
|
count += RegSize;
|
2014-02-15 01:19:07 +08:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2014-04-30 15:05:07 +08:00
|
|
|
static bool WindowsRequiresStackProbe(const MachineFunction &MF,
|
|
|
|
size_t StackSizeInBytes) {
|
|
|
|
const MachineFrameInfo *MFI = MF.getFrameInfo();
|
2015-01-31 10:26:37 +08:00
|
|
|
const Function *F = MF.getFunction();
|
|
|
|
unsigned StackProbeSize = (MFI->getStackProtectorIndex() > 0) ? 4080 : 4096;
|
|
|
|
if (F->hasFnAttribute("stack-probe-size"))
|
|
|
|
F->getFnAttribute("stack-probe-size")
|
|
|
|
.getValueAsString()
|
|
|
|
.getAsInteger(0, StackProbeSize);
|
|
|
|
return StackSizeInBytes >= StackProbeSize;
|
2014-04-30 15:05:07 +08:00
|
|
|
}
|
|
|
|
|
2014-11-15 06:45:33 +08:00
|
|
|
namespace {
|
|
|
|
struct StackAdjustingInsts {
|
|
|
|
struct InstInfo {
|
|
|
|
MachineBasicBlock::iterator I;
|
|
|
|
unsigned SPAdjust;
|
|
|
|
bool BeforeFPSet;
|
|
|
|
};
|
|
|
|
|
|
|
|
SmallVector<InstInfo, 4> Insts;
|
|
|
|
|
|
|
|
void addInst(MachineBasicBlock::iterator I, unsigned SPAdjust,
|
|
|
|
bool BeforeFPSet = false) {
|
|
|
|
InstInfo Info = {I, SPAdjust, BeforeFPSet};
|
|
|
|
Insts.push_back(Info);
|
|
|
|
}
|
|
|
|
|
|
|
|
void addExtraBytes(const MachineBasicBlock::iterator I, unsigned ExtraBytes) {
|
|
|
|
auto Info = std::find_if(Insts.begin(), Insts.end(),
|
|
|
|
[&](InstInfo &Info) { return Info.I == I; });
|
|
|
|
assert(Info != Insts.end() && "invalid sp adjusting instruction");
|
|
|
|
Info->SPAdjust += ExtraBytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
void emitDefCFAOffsets(MachineModuleInfo &MMI, MachineBasicBlock &MBB,
|
|
|
|
DebugLoc dl, const ARMBaseInstrInfo &TII, bool HasFP) {
|
|
|
|
unsigned CFAOffset = 0;
|
|
|
|
for (auto &Info : Insts) {
|
|
|
|
if (HasFP && !Info.BeforeFPSet)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CFAOffset -= Info.SPAdjust;
|
|
|
|
unsigned CFIIndex = MMI.addFrameInst(
|
|
|
|
MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
|
|
|
|
BuildMI(MBB, std::next(Info.I), dl,
|
2014-12-16 08:20:49 +08:00
|
|
|
TII.get(TargetOpcode::CFI_INSTRUCTION))
|
|
|
|
.addCFIIndex(CFIIndex)
|
|
|
|
.setMIFlags(MachineInstr::FrameSetup);
|
2014-11-15 06:45:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2014-11-15 06:45:33 +08:00
|
|
|
|
2015-01-08 23:09:14 +08:00
|
|
|
/// Emit an instruction sequence that will align the address in
|
|
|
|
/// register Reg by zero-ing out the lower bits. For versions of the
|
|
|
|
/// architecture that support Neon, this must be done in a single
|
|
|
|
/// instruction, since skipAlignedDPRCS2Spills assumes it is done in a
|
|
|
|
/// single instruction. That function only gets called when optimizing
|
|
|
|
/// spilling of D registers on a core with the Neon instruction set
|
|
|
|
/// present.
|
|
|
|
static void emitAligningInstructions(MachineFunction &MF, ARMFunctionInfo *AFI,
|
|
|
|
const TargetInstrInfo &TII,
|
|
|
|
MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MBBI,
|
|
|
|
DebugLoc DL, const unsigned Reg,
|
|
|
|
const unsigned Alignment,
|
|
|
|
const bool MustBeSingleInstruction) {
|
2015-01-29 08:19:33 +08:00
|
|
|
const ARMSubtarget &AST =
|
|
|
|
static_cast<const ARMSubtarget &>(MF.getSubtarget());
|
2015-01-08 23:09:14 +08:00
|
|
|
const bool CanUseBFC = AST.hasV6T2Ops() || AST.hasV7Ops();
|
|
|
|
const unsigned AlignMask = Alignment - 1;
|
|
|
|
const unsigned NrBitsToZero = countTrailingZeros(Alignment);
|
|
|
|
assert(!AFI->isThumb1OnlyFunction() && "Thumb1 not supported");
|
|
|
|
if (!AFI->isThumbFunction()) {
|
|
|
|
// if the BFC instruction is available, use that to zero the lower
|
|
|
|
// bits:
|
|
|
|
// bfc Reg, #0, log2(Alignment)
|
|
|
|
// otherwise use BIC, if the mask to zero the required number of bits
|
|
|
|
// can be encoded in the bic immediate field
|
|
|
|
// bic Reg, Reg, Alignment-1
|
|
|
|
// otherwise, emit
|
|
|
|
// lsr Reg, Reg, log2(Alignment)
|
|
|
|
// lsl Reg, Reg, log2(Alignment)
|
|
|
|
if (CanUseBFC) {
|
|
|
|
AddDefaultPred(BuildMI(MBB, MBBI, DL, TII.get(ARM::BFC), Reg)
|
|
|
|
.addReg(Reg, RegState::Kill)
|
|
|
|
.addImm(~AlignMask));
|
|
|
|
} else if (AlignMask <= 255) {
|
|
|
|
AddDefaultCC(
|
|
|
|
AddDefaultPred(BuildMI(MBB, MBBI, DL, TII.get(ARM::BICri), Reg)
|
|
|
|
.addReg(Reg, RegState::Kill)
|
|
|
|
.addImm(AlignMask)));
|
|
|
|
} else {
|
|
|
|
assert(!MustBeSingleInstruction &&
|
|
|
|
"Shouldn't call emitAligningInstructions demanding a single "
|
|
|
|
"instruction to be emitted for large stack alignment for a target "
|
|
|
|
"without BFC.");
|
|
|
|
AddDefaultCC(AddDefaultPred(
|
|
|
|
BuildMI(MBB, MBBI, DL, TII.get(ARM::MOVsi), Reg)
|
|
|
|
.addReg(Reg, RegState::Kill)
|
|
|
|
.addImm(ARM_AM::getSORegOpc(ARM_AM::lsr, NrBitsToZero))));
|
|
|
|
AddDefaultCC(AddDefaultPred(
|
|
|
|
BuildMI(MBB, MBBI, DL, TII.get(ARM::MOVsi), Reg)
|
|
|
|
.addReg(Reg, RegState::Kill)
|
|
|
|
.addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, NrBitsToZero))));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Since this is only reached for Thumb-2 targets, the BFC instruction
|
|
|
|
// should always be available.
|
|
|
|
assert(CanUseBFC);
|
|
|
|
AddDefaultPred(BuildMI(MBB, MBBI, DL, TII.get(ARM::t2BFC), Reg)
|
|
|
|
.addReg(Reg, RegState::Kill)
|
|
|
|
.addImm(~AlignMask));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[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 ARMFrameLowering::emitPrologue(MachineFunction &MF,
|
|
|
|
MachineBasicBlock &MBB) const {
|
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();
|
|
|
|
MCContext &Context = MMI.getContext();
|
2014-04-30 15:05:07 +08:00
|
|
|
const TargetMachine &TM = MF.getTarget();
|
2014-02-15 01:19:07 +08:00
|
|
|
const MCRegisterInfo *MRI = Context.getRegisterInfo();
|
2015-01-29 08:19:33 +08:00
|
|
|
const ARMBaseRegisterInfo *RegInfo = STI.getRegisterInfo();
|
|
|
|
const ARMBaseInstrInfo &TII = *STI.getInstrInfo();
|
2010-11-15 08:06:54 +08:00
|
|
|
assert(!AFI->isThumb1OnlyFunction() &&
|
|
|
|
"This emitPrologue does not support Thumb1!");
|
|
|
|
bool isARM = !AFI->isThumbFunction();
|
2015-01-29 08:19:33 +08:00
|
|
|
unsigned Align = STI.getFrameLowering()->getStackAlignment();
|
2015-11-06 05:54:58 +08:00
|
|
|
unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize();
|
|
|
|
unsigned NumBytes = MFI->getStackSize();
|
|
|
|
const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
|
|
|
|
|
|
|
|
// Debug location must be unknown since the first debug location is used
|
|
|
|
// to determine the end of the prologue.
|
|
|
|
DebugLoc dl;
|
|
|
|
|
|
|
|
unsigned FramePtr = RegInfo->getFrameRegister(MF);
|
|
|
|
|
|
|
|
// Determine the sizes of each callee-save spill areas and record which frame
|
2010-11-15 08:06:54 +08:00
|
|
|
// belongs to which callee-save spill areas.
|
|
|
|
unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
|
|
|
|
int FramePtrSpillFI = 0;
|
2011-12-23 08:36:18 +08:00
|
|
|
int D8SpillFI = 0;
|
2010-11-15 08:06:54 +08:00
|
|
|
|
2012-10-27 05:46:57 +08:00
|
|
|
// All calls are tail calls in GHC calling conv, and functions have no
|
|
|
|
// prologue/epilogue.
|
2012-08-03 08:05:53 +08:00
|
|
|
if (MF.getFunction()->getCallingConv() == CallingConv::GHC)
|
|
|
|
return;
|
|
|
|
|
2014-11-15 06:45:33 +08:00
|
|
|
StackAdjustingInsts DefCFAOffsetCandidates;
|
2015-04-08 18:10:12 +08:00
|
|
|
bool HasFP = hasFP(MF);
|
2014-11-15 06:45:33 +08:00
|
|
|
|
2014-03-05 23:25:27 +08:00
|
|
|
// Allocate the vararg register save area.
|
2014-02-15 01:19:07 +08:00
|
|
|
if (ArgRegsSaveSize) {
|
2013-04-30 15:19:58 +08:00
|
|
|
emitSPUpdate(isARM, MBB, MBBI, dl, TII, -ArgRegsSaveSize,
|
2011-03-06 02:43:32 +08:00
|
|
|
MachineInstr::FrameSetup);
|
2014-11-15 06:45:33 +08:00
|
|
|
DefCFAOffsetCandidates.addInst(std::prev(MBBI), ArgRegsSaveSize, true);
|
2014-02-15 01:19:07 +08:00
|
|
|
}
|
2010-11-15 08:06:54 +08:00
|
|
|
|
2014-04-30 15:05:07 +08:00
|
|
|
if (!AFI->hasStackFrame() &&
|
|
|
|
(!STI.isTargetWindows() || !WindowsRequiresStackProbe(MF, NumBytes))) {
|
2014-03-05 23:25:27 +08:00
|
|
|
if (NumBytes - ArgRegsSaveSize != 0) {
|
|
|
|
emitSPUpdate(isARM, MBB, MBBI, dl, TII, -(NumBytes - ArgRegsSaveSize),
|
2011-03-06 02:43:32 +08:00
|
|
|
MachineInstr::FrameSetup);
|
2014-11-15 06:45:33 +08:00
|
|
|
DefCFAOffsetCandidates.addInst(std::prev(MBBI),
|
|
|
|
NumBytes - ArgRegsSaveSize, true);
|
2014-02-15 01:19:07 +08:00
|
|
|
}
|
2015-04-08 18:10:12 +08:00
|
|
|
DefCFAOffsetCandidates.emitDefCFAOffsets(MMI, MBB, dl, TII, HasFP);
|
2010-11-15 08:06:54 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-15 01:19:07 +08:00
|
|
|
// Determine spill area sizes.
|
2010-11-15 08:06:54 +08:00
|
|
|
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:
|
|
|
|
case ARM::R12:
|
2014-05-30 21:23:06 +08:00
|
|
|
if (STI.isTargetDarwin()) {
|
2014-02-15 01:19:07 +08:00
|
|
|
GPRCS2Size += 4;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// fallthrough
|
2013-10-01 22:33:28 +08:00
|
|
|
case ARM::R0:
|
|
|
|
case ARM::R1:
|
|
|
|
case ARM::R2:
|
|
|
|
case ARM::R3:
|
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:
|
2011-12-23 08:36:18 +08:00
|
|
|
// This is a DPR. Exclude the aligned DPRCS2 spills.
|
|
|
|
if (Reg == ARM::D8)
|
|
|
|
D8SpillFI = FI;
|
2013-11-05 07:04:15 +08:00
|
|
|
if (Reg < ARM::D8 || Reg >= ARM::D8 + AFI->getNumAlignedDPRCS2Regs())
|
2011-12-23 08:36:18 +08:00
|
|
|
DPRCSSize += 8;
|
2010-11-15 08:06:54 +08:00
|
|
|
}
|
|
|
|
}
|
2010-11-28 07:05:03 +08:00
|
|
|
|
2010-11-19 03:40:05 +08:00
|
|
|
// Move past area 1.
|
2014-11-15 06:45:33 +08:00
|
|
|
MachineBasicBlock::iterator LastPush = MBB.end(), GPRCS1Push, GPRCS2Push;
|
|
|
|
if (GPRCS1Size > 0) {
|
2014-02-15 01:19:07 +08:00
|
|
|
GPRCS1Push = LastPush = MBBI++;
|
2014-11-15 06:45:33 +08:00
|
|
|
DefCFAOffsetCandidates.addInst(LastPush, GPRCS1Size, true);
|
|
|
|
}
|
2010-11-28 07:05:03 +08:00
|
|
|
|
2010-11-15 08:06:54 +08:00
|
|
|
// Determine starting offsets of spill areas.
|
2014-11-05 08:27:13 +08:00
|
|
|
unsigned GPRCS1Offset = NumBytes - ArgRegsSaveSize - GPRCS1Size;
|
|
|
|
unsigned GPRCS2Offset = GPRCS1Offset - GPRCS2Size;
|
|
|
|
unsigned DPRAlign = DPRCSSize ? std::min(8U, Align) : 4U;
|
|
|
|
unsigned DPRGapSize = (GPRCS1Size + GPRCS2Size + ArgRegsSaveSize) % DPRAlign;
|
|
|
|
unsigned DPRCSOffset = GPRCS2Offset - DPRGapSize - DPRCSSize;
|
2013-11-09 01:18:07 +08:00
|
|
|
int FramePtrOffsetInPush = 0;
|
|
|
|
if (HasFP) {
|
2014-11-15 06:45:33 +08:00
|
|
|
FramePtrOffsetInPush =
|
|
|
|
MFI->getObjectOffset(FramePtrSpillFI) + ArgRegsSaveSize;
|
2010-11-15 08:06:54 +08:00
|
|
|
AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) +
|
|
|
|
NumBytes);
|
2013-11-09 01:18:07 +08:00
|
|
|
}
|
2010-11-15 08:06:54 +08:00
|
|
|
AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
|
|
|
|
AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
|
|
|
|
AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
|
|
|
|
|
2013-11-05 07:04:15 +08:00
|
|
|
// Move past area 2.
|
2014-11-15 06:45:33 +08:00
|
|
|
if (GPRCS2Size > 0) {
|
2014-02-15 01:19:07 +08:00
|
|
|
GPRCS2Push = LastPush = MBBI++;
|
2014-11-15 06:45:33 +08:00
|
|
|
DefCFAOffsetCandidates.addInst(LastPush, GPRCS2Size);
|
|
|
|
}
|
2013-11-05 07:04:15 +08:00
|
|
|
|
2014-11-05 08:27:13 +08:00
|
|
|
// Prolog/epilog inserter assumes we correctly align DPRs on the stack, so our
|
|
|
|
// .cfi_offset operations will reflect that.
|
|
|
|
if (DPRGapSize) {
|
|
|
|
assert(DPRGapSize == 4 && "unexpected alignment requirements for DPRs");
|
2014-11-15 06:45:33 +08:00
|
|
|
if (tryFoldSPUpdateIntoPushPop(STI, MF, LastPush, DPRGapSize))
|
|
|
|
DefCFAOffsetCandidates.addExtraBytes(LastPush, DPRGapSize);
|
|
|
|
else {
|
2014-11-05 08:27:13 +08:00
|
|
|
emitSPUpdate(isARM, MBB, MBBI, dl, TII, -DPRGapSize,
|
|
|
|
MachineInstr::FrameSetup);
|
2014-11-15 06:45:33 +08:00
|
|
|
DefCFAOffsetCandidates.addInst(std::prev(MBBI), DPRGapSize);
|
|
|
|
}
|
2014-11-05 08:27:13 +08:00
|
|
|
}
|
|
|
|
|
2010-11-19 03:40:05 +08:00
|
|
|
// Move past area 3.
|
2011-02-25 08:24:46 +08:00
|
|
|
if (DPRCSSize > 0) {
|
|
|
|
// Since vpush register list cannot have gaps, there may be multiple vpush
|
2011-02-25 09:29:29 +08:00
|
|
|
// instructions in the prologue.
|
2014-11-15 06:45:33 +08:00
|
|
|
while (MBBI->getOpcode() == ARM::VSTMDDB_UPD) {
|
|
|
|
DefCFAOffsetCandidates.addInst(MBBI, sizeOfSPAdjustment(MBBI));
|
2013-11-09 01:18:07 +08:00
|
|
|
LastPush = MBBI++;
|
2014-11-15 06:45:33 +08:00
|
|
|
}
|
2011-02-25 08:24:46 +08:00
|
|
|
}
|
2010-11-28 07:05:03 +08:00
|
|
|
|
2011-12-23 08:36:18 +08:00
|
|
|
// Move past the aligned DPRCS2 area.
|
|
|
|
if (AFI->getNumAlignedDPRCS2Regs() > 0) {
|
|
|
|
MBBI = skipAlignedDPRCS2Spills(MBBI, AFI->getNumAlignedDPRCS2Regs());
|
|
|
|
// The code inserted by emitAlignedDPRCS2Spills realigns the stack, and
|
|
|
|
// leaves the stack pointer pointing to the DPRCS2 area.
|
|
|
|
//
|
|
|
|
// Adjust NumBytes to represent the stack slots below the DPRCS2 area.
|
|
|
|
NumBytes += MFI->getObjectOffset(D8SpillFI);
|
|
|
|
} else
|
|
|
|
NumBytes = DPRCSOffset;
|
|
|
|
|
2014-04-30 15:05:07 +08:00
|
|
|
if (STI.isTargetWindows() && WindowsRequiresStackProbe(MF, NumBytes)) {
|
|
|
|
uint32_t NumWords = NumBytes >> 2;
|
|
|
|
|
|
|
|
if (NumWords < 65536)
|
|
|
|
AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi16), ARM::R4)
|
2014-05-07 11:03:31 +08:00
|
|
|
.addImm(NumWords)
|
|
|
|
.setMIFlags(MachineInstr::FrameSetup));
|
2014-04-30 15:05:07 +08:00
|
|
|
else
|
|
|
|
BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi32imm), ARM::R4)
|
2014-05-07 11:03:31 +08:00
|
|
|
.addImm(NumWords)
|
|
|
|
.setMIFlags(MachineInstr::FrameSetup);
|
2014-04-30 15:05:07 +08:00
|
|
|
|
|
|
|
switch (TM.getCodeModel()) {
|
|
|
|
case CodeModel::Small:
|
|
|
|
case CodeModel::Medium:
|
|
|
|
case CodeModel::Default:
|
|
|
|
case CodeModel::Kernel:
|
|
|
|
BuildMI(MBB, MBBI, dl, TII.get(ARM::tBL))
|
|
|
|
.addImm((unsigned)ARMCC::AL).addReg(0)
|
|
|
|
.addExternalSymbol("__chkstk")
|
2014-05-07 11:03:31 +08:00
|
|
|
.addReg(ARM::R4, RegState::Implicit)
|
|
|
|
.setMIFlags(MachineInstr::FrameSetup);
|
2014-04-30 15:05:07 +08:00
|
|
|
break;
|
|
|
|
case CodeModel::Large:
|
2014-05-01 12:19:59 +08:00
|
|
|
case CodeModel::JITDefault:
|
2014-04-30 15:05:07 +08:00
|
|
|
BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi32imm), ARM::R12)
|
2014-05-07 11:03:31 +08:00
|
|
|
.addExternalSymbol("__chkstk")
|
|
|
|
.setMIFlags(MachineInstr::FrameSetup);
|
2014-05-01 12:19:59 +08:00
|
|
|
|
2014-05-07 11:03:27 +08:00
|
|
|
BuildMI(MBB, MBBI, dl, TII.get(ARM::tBLXr))
|
|
|
|
.addImm((unsigned)ARMCC::AL).addReg(0)
|
2014-04-30 15:05:07 +08:00
|
|
|
.addReg(ARM::R12, RegState::Kill)
|
2014-05-07 11:03:31 +08:00
|
|
|
.addReg(ARM::R4, RegState::Implicit)
|
|
|
|
.setMIFlags(MachineInstr::FrameSetup);
|
2014-04-30 15:05:07 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::t2SUBrr),
|
|
|
|
ARM::SP)
|
|
|
|
.addReg(ARM::SP, RegState::Define)
|
|
|
|
.addReg(ARM::R4, RegState::Kill)
|
|
|
|
.setMIFlags(MachineInstr::FrameSetup)));
|
|
|
|
NumBytes = 0;
|
|
|
|
}
|
|
|
|
|
2010-11-15 08:06:54 +08:00
|
|
|
if (NumBytes) {
|
|
|
|
// Adjust SP after all the callee-save spills.
|
2015-09-24 06:21:09 +08:00
|
|
|
if (AFI->getNumAlignedDPRCS2Regs() == 0 &&
|
|
|
|
tryFoldSPUpdateIntoPushPop(STI, MF, LastPush, NumBytes))
|
2014-11-15 06:45:33 +08:00
|
|
|
DefCFAOffsetCandidates.addExtraBytes(LastPush, NumBytes);
|
|
|
|
else {
|
2013-11-09 01:18:07 +08:00
|
|
|
emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes,
|
|
|
|
MachineInstr::FrameSetup);
|
2014-11-15 06:45:33 +08:00
|
|
|
DefCFAOffsetCandidates.addInst(std::prev(MBBI), NumBytes);
|
|
|
|
}
|
2013-11-09 01:18:07 +08:00
|
|
|
|
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 (HasFP && isARM)
|
|
|
|
// Restore from fp only in ARM mode: e.g. sub sp, r7, #24
|
|
|
|
// Note it's not safe to do this in Thumb2 mode because it would have
|
|
|
|
// taken two instructions:
|
|
|
|
// mov sp, r7
|
|
|
|
// sub sp, #24
|
|
|
|
// If an interrupt is taken between the two instructions, then sp is in
|
|
|
|
// an inconsistent state (pointing to the middle of callee-saved area).
|
|
|
|
// The interrupt handler can end up clobbering the registers.
|
2010-11-15 08:06:54 +08:00
|
|
|
AFI->setShouldRestoreSPFromFP(true);
|
|
|
|
}
|
|
|
|
|
2014-11-15 06:45:33 +08:00
|
|
|
// Set FP to point to the stack slot that contains the previous FP.
|
|
|
|
// For iOS, FP is R7, which has now been stored in spill area 1.
|
|
|
|
// Otherwise, if this is not iOS, all the callee-saved registers go
|
|
|
|
// into spill area 1, including the FP in R11. In either case, it
|
|
|
|
// is in area one and the adjustment needs to take place just after
|
|
|
|
// that push.
|
|
|
|
if (HasFP) {
|
|
|
|
MachineBasicBlock::iterator AfterPush = std::next(GPRCS1Push);
|
|
|
|
unsigned PushSize = sizeOfSPAdjustment(GPRCS1Push);
|
|
|
|
emitRegPlusImmediate(!AFI->isThumbFunction(), MBB, AfterPush,
|
|
|
|
dl, TII, FramePtr, ARM::SP,
|
|
|
|
PushSize + FramePtrOffsetInPush,
|
|
|
|
MachineInstr::FrameSetup);
|
|
|
|
if (FramePtrOffsetInPush + PushSize != 0) {
|
|
|
|
unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createDefCfa(
|
|
|
|
nullptr, MRI->getDwarfRegNum(FramePtr, true),
|
|
|
|
-(ArgRegsSaveSize - FramePtrOffsetInPush)));
|
|
|
|
BuildMI(MBB, AfterPush, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
|
2014-12-16 08:20:49 +08:00
|
|
|
.addCFIIndex(CFIIndex)
|
|
|
|
.setMIFlags(MachineInstr::FrameSetup);
|
2014-11-15 06:45:33 +08:00
|
|
|
} else {
|
|
|
|
unsigned CFIIndex =
|
|
|
|
MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(
|
|
|
|
nullptr, MRI->getDwarfRegNum(FramePtr, true)));
|
|
|
|
BuildMI(MBB, AfterPush, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
|
2014-12-16 08:20:49 +08:00
|
|
|
.addCFIIndex(CFIIndex)
|
|
|
|
.setMIFlags(MachineInstr::FrameSetup);
|
2014-11-15 06:45:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that the prologue's actual instructions are finalised, we can insert
|
|
|
|
// the necessary DWARF cf instructions to describe the situation. Start by
|
|
|
|
// recording where each register ended up:
|
|
|
|
if (GPRCS1Size > 0) {
|
|
|
|
MachineBasicBlock::iterator Pos = std::next(GPRCS1Push);
|
|
|
|
int CFIIndex;
|
2014-04-04 10:10:55 +08:00
|
|
|
for (const auto &Entry : CSI) {
|
|
|
|
unsigned Reg = Entry.getReg();
|
|
|
|
int FI = Entry.getFrameIdx();
|
2014-02-15 01:19:07 +08:00
|
|
|
switch (Reg) {
|
|
|
|
case ARM::R8:
|
|
|
|
case ARM::R9:
|
|
|
|
case ARM::R10:
|
|
|
|
case ARM::R11:
|
|
|
|
case ARM::R12:
|
2014-05-30 21:23:06 +08:00
|
|
|
if (STI.isTargetDarwin())
|
2014-02-15 01:19:07 +08:00
|
|
|
break;
|
|
|
|
// fallthrough
|
|
|
|
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
|
|
|
CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
|
|
|
|
nullptr, MRI->getDwarfRegNum(Reg, true), MFI->getObjectOffset(FI)));
|
|
|
|
BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
|
2014-12-16 08:20:49 +08:00
|
|
|
.addCFIIndex(CFIIndex)
|
|
|
|
.setMIFlags(MachineInstr::FrameSetup);
|
2014-02-15 01:19:07 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GPRCS2Size > 0) {
|
2014-11-15 06:45:33 +08:00
|
|
|
MachineBasicBlock::iterator Pos = std::next(GPRCS2Push);
|
2014-04-04 10:10:55 +08:00
|
|
|
for (const auto &Entry : CSI) {
|
|
|
|
unsigned Reg = Entry.getReg();
|
|
|
|
int FI = Entry.getFrameIdx();
|
2014-02-15 01:19:07 +08:00
|
|
|
switch (Reg) {
|
|
|
|
case ARM::R8:
|
|
|
|
case ARM::R9:
|
|
|
|
case ARM::R10:
|
|
|
|
case ARM::R11:
|
|
|
|
case ARM::R12:
|
2014-05-30 21:23:06 +08:00
|
|
|
if (STI.isTargetDarwin()) {
|
2014-02-15 01:19:07 +08:00
|
|
|
unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
|
2014-03-05 23:25:27 +08:00
|
|
|
unsigned Offset = MFI->getObjectOffset(FI);
|
2014-03-07 14:08:31 +08:00
|
|
|
unsigned CFIIndex = MMI.addFrameInst(
|
|
|
|
MCCFIInstruction::createOffset(nullptr, DwarfReg, Offset));
|
|
|
|
BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
|
2014-12-16 08:20:49 +08:00
|
|
|
.addCFIIndex(CFIIndex)
|
|
|
|
.setMIFlags(MachineInstr::FrameSetup);
|
2014-02-15 01:19:07 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-09 01:18:07 +08:00
|
|
|
|
2014-02-15 01:19:07 +08:00
|
|
|
if (DPRCSSize > 0) {
|
|
|
|
// Since vpush register list cannot have gaps, there may be multiple vpush
|
|
|
|
// instructions in the prologue.
|
2014-11-15 06:45:33 +08:00
|
|
|
MachineBasicBlock::iterator Pos = std::next(LastPush);
|
2014-04-04 10:10:55 +08:00
|
|
|
for (const auto &Entry : CSI) {
|
|
|
|
unsigned Reg = Entry.getReg();
|
|
|
|
int FI = Entry.getFrameIdx();
|
2014-02-15 01:19:07 +08:00
|
|
|
if ((Reg >= ARM::D0 && Reg <= ARM::D31) &&
|
|
|
|
(Reg < ARM::D8 || Reg >= ARM::D8 + AFI->getNumAlignedDPRCS2Regs())) {
|
|
|
|
unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
|
|
|
|
unsigned Offset = MFI->getObjectOffset(FI);
|
2014-03-07 14:08:31 +08:00
|
|
|
unsigned CFIIndex = MMI.addFrameInst(
|
|
|
|
MCCFIInstruction::createOffset(nullptr, DwarfReg, Offset));
|
2014-11-15 06:45:33 +08:00
|
|
|
BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
|
2014-12-16 08:20:49 +08:00
|
|
|
.addCFIIndex(CFIIndex)
|
|
|
|
.setMIFlags(MachineInstr::FrameSetup);
|
2014-02-15 01:19:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-15 06:45:33 +08:00
|
|
|
// Now we can emit descriptions of where the canonical frame address was
|
|
|
|
// throughout the process. If we have a frame pointer, it takes over the job
|
|
|
|
// half-way through, so only the first few .cfi_def_cfa_offset instructions
|
|
|
|
// actually get emitted.
|
|
|
|
DefCFAOffsetCandidates.emitDefCFAOffsets(MMI, MBB, dl, TII, HasFP);
|
2013-11-09 01:18:07 +08:00
|
|
|
|
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 (STI.isTargetELF() && hasFP(MF))
|
2010-11-15 08:06:54 +08:00
|
|
|
MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() -
|
|
|
|
AFI->getFramePtrSpillOffset());
|
|
|
|
|
|
|
|
AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
|
|
|
|
AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
|
2014-11-05 08:27:13 +08:00
|
|
|
AFI->setDPRCalleeSavedGapSize(DPRGapSize);
|
2010-11-15 08:06:54 +08:00
|
|
|
AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
|
|
|
|
|
|
|
|
// If we need dynamic stack realignment, do it here. Be paranoid and make
|
|
|
|
// sure if we also have VLAs, we have a base pointer for frame access.
|
2011-12-24 12:17:01 +08:00
|
|
|
// If aligned NEON registers were spilled, the stack has already been
|
2011-12-23 08:36:18 +08:00
|
|
|
// realigned.
|
|
|
|
if (!AFI->getNumAlignedDPRCS2Regs() && RegInfo->needsStackRealignment(MF)) {
|
2010-11-15 08:06:54 +08:00
|
|
|
unsigned MaxAlign = MFI->getMaxAlignment();
|
2015-01-08 23:09:14 +08:00
|
|
|
assert(!AFI->isThumb1OnlyFunction());
|
2010-11-15 08:06:54 +08:00
|
|
|
if (!AFI->isThumbFunction()) {
|
2015-01-08 23:09:14 +08:00
|
|
|
emitAligningInstructions(MF, AFI, TII, MBB, MBBI, dl, ARM::SP, MaxAlign,
|
|
|
|
false);
|
2010-11-15 08:06:54 +08:00
|
|
|
} else {
|
2015-01-08 23:09:14 +08:00
|
|
|
// We cannot use sp as source/dest register here, thus we're using r4 to
|
|
|
|
// perform the calculations. We're emitting the following sequence:
|
2010-11-15 08:06:54 +08:00
|
|
|
// mov r4, sp
|
2015-01-08 23:09:14 +08:00
|
|
|
// -- use emitAligningInstructions to produce best sequence to zero
|
|
|
|
// -- out lower bits in r4
|
2010-11-15 08:06:54 +08:00
|
|
|
// mov sp, r4
|
|
|
|
// FIXME: It will be better just to find spare register here.
|
2011-07-01 07:38:17 +08:00
|
|
|
AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::R4)
|
2015-01-08 23:09:14 +08:00
|
|
|
.addReg(ARM::SP, RegState::Kill));
|
|
|
|
emitAligningInstructions(MF, AFI, TII, MBB, MBBI, dl, ARM::R4, MaxAlign,
|
|
|
|
false);
|
2011-07-01 07:38:17 +08:00
|
|
|
AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP)
|
2015-01-08 23:09:14 +08:00
|
|
|
.addReg(ARM::R4, RegState::Kill));
|
2010-11-15 08:06:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
AFI->setShouldRestoreSPFromFP(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2011-03-06 02:43:32 +08:00
|
|
|
// FIXME: Clarify FrameSetup flags here.
|
2010-11-15 08:06:54 +08:00
|
|
|
if (RegInfo->hasBasePointer(MF)) {
|
|
|
|
if (isARM)
|
|
|
|
BuildMI(MBB, MBBI, dl,
|
|
|
|
TII.get(ARM::MOVr), RegInfo->getBaseRegister())
|
|
|
|
.addReg(ARM::SP)
|
|
|
|
.addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
|
|
|
|
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
|
|
|
RegInfo->getBaseRegister())
|
|
|
|
.addReg(ARM::SP));
|
2010-11-15 08:06:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the frame has variable sized objects then the epilogue must restore
|
2011-01-11 07:10:59 +08:00
|
|
|
// the sp from fp. We can assume there's an FP here since hasFP already
|
|
|
|
// checks for hasVarSizedObjects.
|
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 (MFI->hasVarSizedObjects())
|
2010-11-15 08:06:54 +08:00
|
|
|
AFI->setShouldRestoreSPFromFP(true);
|
|
|
|
}
|
|
|
|
|
2011-01-10 20:39:04 +08:00
|
|
|
void ARMFrameLowering::emitEpilogue(MachineFunction &MF,
|
2011-01-14 05:10:12 +08:00
|
|
|
MachineBasicBlock &MBB) const {
|
2010-11-15 08:06:54 +08:00
|
|
|
MachineFrameInfo *MFI = MF.getFrameInfo();
|
|
|
|
ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
|
2014-08-05 10:39:49 +08:00
|
|
|
const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
|
2010-11-15 08:06:54 +08:00
|
|
|
const ARMBaseInstrInfo &TII =
|
2014-08-05 10:39:49 +08:00
|
|
|
*static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
|
2010-11-15 08:06:54 +08:00
|
|
|
assert(!AFI->isThumb1OnlyFunction() &&
|
|
|
|
"This emitEpilogue does not support Thumb1!");
|
|
|
|
bool isARM = !AFI->isThumbFunction();
|
|
|
|
|
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();
|
|
|
|
unsigned FramePtr = RegInfo->getFrameRegister(MF);
|
|
|
|
|
2012-10-27 05:46:57 +08:00
|
|
|
// All calls are tail calls in GHC calling conv, and functions have no
|
|
|
|
// prologue/epilogue.
|
2015-07-21 05:42:14 +08:00
|
|
|
if (MF.getFunction()->getCallingConv() == CallingConv::GHC)
|
2012-08-03 08:05:53 +08:00
|
|
|
return;
|
2015-07-21 05:42:14 +08:00
|
|
|
|
|
|
|
// First put ourselves on the first (from top) terminator instructions.
|
|
|
|
MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
|
|
|
|
DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
|
2012-08-03 08:05:53 +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(isARM, MBB, MBBI, dl, TII, NumBytes - ArgRegsSaveSize);
|
2010-11-15 08:06:54 +08:00
|
|
|
} else {
|
|
|
|
// Unwind MBBI to point to first LDR / VLDRD.
|
2014-04-04 13:16:06 +08:00
|
|
|
const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF);
|
2010-11-15 08:06:54 +08:00
|
|
|
if (MBBI != MBB.begin()) {
|
2013-11-09 01:18:07 +08:00
|
|
|
do {
|
2010-11-15 08:06:54 +08:00
|
|
|
--MBBI;
|
2013-11-09 01:18:07 +08:00
|
|
|
} while (MBBI != MBB.begin() && isCSRestore(MBBI, TII, CSRegs));
|
2010-11-15 08:06:54 +08:00
|
|
|
if (!isCSRestore(MBBI, TII, CSRegs))
|
|
|
|
++MBBI;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move SP to start of FP callee save spill area.
|
2014-03-05 23:25:27 +08:00
|
|
|
NumBytes -= (ArgRegsSaveSize +
|
|
|
|
AFI->getGPRCalleeSavedArea1Size() +
|
2010-11-15 08:06:54 +08:00
|
|
|
AFI->getGPRCalleeSavedArea2Size() +
|
2014-11-05 08:27:13 +08:00
|
|
|
AFI->getDPRCalleeSavedGapSize() +
|
2010-11-15 08:06:54 +08:00
|
|
|
AFI->getDPRCalleeSavedAreaSize());
|
|
|
|
|
|
|
|
// Reset SP based on frame pointer only if the stack frame extends beyond
|
|
|
|
// frame pointer stack slot or target is ELF and the function has FP.
|
|
|
|
if (AFI->shouldRestoreSPFromFP()) {
|
|
|
|
NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
|
|
|
|
if (NumBytes) {
|
|
|
|
if (isARM)
|
|
|
|
emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes,
|
|
|
|
ARMCC::AL, 0, TII);
|
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 {
|
|
|
|
// It's not possible to restore SP from FP in a single instruction.
|
2012-01-04 09:55:04 +08:00
|
|
|
// For iOS, this looks like:
|
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
|
|
|
// mov sp, r7
|
|
|
|
// sub sp, #24
|
|
|
|
// This is bad, if an interrupt is taken after the mov, sp is in an
|
|
|
|
// inconsistent state.
|
|
|
|
// Use the first callee-saved register as a scratch register.
|
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!");
|
|
|
|
emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::R4, FramePtr, -NumBytes,
|
2010-11-15 08:06:54 +08:00
|
|
|
ARMCC::AL, 0, TII);
|
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
|
|
|
}
|
2010-11-15 08:06:54 +08:00
|
|
|
} else {
|
|
|
|
// Thumb2 or ARM.
|
|
|
|
if (isARM)
|
|
|
|
BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), ARM::SP)
|
|
|
|
.addReg(FramePtr).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
|
|
|
|
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
|
|
|
}
|
2013-12-02 22:46:26 +08:00
|
|
|
} else if (NumBytes &&
|
2013-12-05 19:02:02 +08:00
|
|
|
!tryFoldSPUpdateIntoPushPop(STI, MF, MBBI, NumBytes))
|
2013-11-09 01:18:07 +08:00
|
|
|
emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
|
2010-11-15 08:06:54 +08:00
|
|
|
|
2010-11-19 03:40:05 +08:00
|
|
|
// Increment past our save areas.
|
2011-02-25 08:24:46 +08:00
|
|
|
if (AFI->getDPRCalleeSavedAreaSize()) {
|
|
|
|
MBBI++;
|
|
|
|
// Since vpop register list cannot have gaps, there may be multiple vpop
|
|
|
|
// instructions in the epilogue.
|
|
|
|
while (MBBI->getOpcode() == ARM::VLDMDIA_UPD)
|
|
|
|
MBBI++;
|
|
|
|
}
|
2014-11-05 08:27:13 +08:00
|
|
|
if (AFI->getDPRCalleeSavedGapSize()) {
|
|
|
|
assert(AFI->getDPRCalleeSavedGapSize() == 4 &&
|
|
|
|
"unexpected DPR alignment gap");
|
|
|
|
emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getDPRCalleeSavedGapSize());
|
|
|
|
}
|
|
|
|
|
2010-11-19 03:40:05 +08:00
|
|
|
if (AFI->getGPRCalleeSavedArea2Size()) MBBI++;
|
|
|
|
if (AFI->getGPRCalleeSavedArea1Size()) MBBI++;
|
2010-11-15 08:06:54 +08:00
|
|
|
}
|
|
|
|
|
2013-04-30 15:19:58 +08:00
|
|
|
if (ArgRegsSaveSize)
|
|
|
|
emitSPUpdate(isARM, MBB, MBBI, dl, TII, ArgRegsSaveSize);
|
2010-11-15 08:06:54 +08:00
|
|
|
}
|
2010-11-20 23:59:32 +08:00
|
|
|
|
2011-01-14 05:10:12 +08:00
|
|
|
/// getFrameIndexReference - Provide a base+offset reference to an FI slot for
|
|
|
|
/// debug info. It's the same as what we use for resolving the code-gen
|
|
|
|
/// references for now. FIXME: This can go wrong when references are
|
|
|
|
/// SP-relative and simple call frames aren't used.
|
2010-11-20 23:59:32 +08:00
|
|
|
int
|
2011-01-10 20:39:04 +08:00
|
|
|
ARMFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
|
2011-01-14 05:10:12 +08:00
|
|
|
unsigned &FrameReg) const {
|
2010-11-20 23:59:32 +08:00
|
|
|
return ResolveFrameIndexReference(MF, FI, FrameReg, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2011-01-10 20:39:04 +08:00
|
|
|
ARMFrameLowering::ResolveFrameIndexReference(const MachineFunction &MF,
|
2011-04-22 09:42:52 +08:00
|
|
|
int FI, unsigned &FrameReg,
|
2011-01-14 05:10:12 +08:00
|
|
|
int SPAdj) const {
|
2010-11-20 23:59:32 +08:00
|
|
|
const MachineFrameInfo *MFI = MF.getFrameInfo();
|
2014-08-05 05:25:23 +08:00
|
|
|
const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>(
|
2014-08-05 10:39:49 +08:00
|
|
|
MF.getSubtarget().getRegisterInfo());
|
2010-11-20 23:59:32 +08:00
|
|
|
const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
|
|
|
|
int Offset = MFI->getObjectOffset(FI) + MFI->getStackSize();
|
|
|
|
int FPOffset = Offset - AFI->getFramePtrSpillOffset();
|
|
|
|
bool isFixed = MFI->isFixedObjectIndex(FI);
|
|
|
|
|
|
|
|
FrameReg = ARM::SP;
|
|
|
|
Offset += SPAdj;
|
|
|
|
|
2012-02-28 09:15:01 +08:00
|
|
|
// SP can move around if there are allocas. We may also lose track of SP
|
|
|
|
// when emergency spilling inside a non-reserved call frame setup.
|
2012-03-21 03:28:22 +08:00
|
|
|
bool hasMovingSP = !hasReservedCallFrame(MF);
|
2012-02-28 09:15:01 +08:00
|
|
|
|
2010-11-20 23:59:32 +08:00
|
|
|
// When dynamically realigning the stack, use the frame pointer for
|
|
|
|
// parameters, and the stack/base pointer for locals.
|
|
|
|
if (RegInfo->needsStackRealignment(MF)) {
|
|
|
|
assert (hasFP(MF) && "dynamic stack realignment without a FP!");
|
|
|
|
if (isFixed) {
|
|
|
|
FrameReg = RegInfo->getFrameRegister(MF);
|
|
|
|
Offset = FPOffset;
|
2012-02-28 09:15:01 +08:00
|
|
|
} else if (hasMovingSP) {
|
2010-11-20 23:59:32 +08:00
|
|
|
assert(RegInfo->hasBasePointer(MF) &&
|
|
|
|
"VLAs and dynamic stack alignment, but missing base pointer!");
|
|
|
|
FrameReg = RegInfo->getBaseRegister();
|
|
|
|
}
|
|
|
|
return Offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is a frame pointer, use it when we can.
|
|
|
|
if (hasFP(MF) && AFI->hasStackFrame()) {
|
|
|
|
// Use frame pointer to reference fixed objects. Use it for locals if
|
|
|
|
// there are VLAs (and thus the SP isn't reliable as a base).
|
2012-02-28 09:15:01 +08:00
|
|
|
if (isFixed || (hasMovingSP && !RegInfo->hasBasePointer(MF))) {
|
2010-11-20 23:59:32 +08:00
|
|
|
FrameReg = RegInfo->getFrameRegister(MF);
|
|
|
|
return FPOffset;
|
2012-02-28 09:15:01 +08:00
|
|
|
} else if (hasMovingSP) {
|
2010-11-20 23:59:32 +08:00
|
|
|
assert(RegInfo->hasBasePointer(MF) && "missing base pointer!");
|
|
|
|
if (AFI->isThumb2Function()) {
|
2011-04-22 09:42:52 +08:00
|
|
|
// Try to use the frame pointer if we can, else use the base pointer
|
|
|
|
// since it's available. This is handy for the emergency spill slot, in
|
|
|
|
// particular.
|
2010-11-20 23:59:32 +08:00
|
|
|
if (FPOffset >= -255 && FPOffset < 0) {
|
|
|
|
FrameReg = RegInfo->getFrameRegister(MF);
|
|
|
|
return FPOffset;
|
|
|
|
}
|
2011-04-22 09:42:52 +08:00
|
|
|
}
|
2010-11-20 23:59:32 +08:00
|
|
|
} else if (AFI->isThumb2Function()) {
|
2011-08-26 01:40:54 +08:00
|
|
|
// Use add <rd>, sp, #<imm8>
|
2011-04-22 09:42:52 +08:00
|
|
|
// ldr <rd>, [sp, #<imm8>]
|
|
|
|
// if at all possible to save space.
|
|
|
|
if (Offset >= 0 && (Offset & 3) == 0 && Offset <= 1020)
|
|
|
|
return Offset;
|
2010-11-20 23:59:32 +08:00
|
|
|
// In Thumb2 mode, the negative offset is very limited. Try to avoid
|
2011-04-22 09:42:52 +08:00
|
|
|
// out of range references. ldr <rt>,[<rn>, #-<imm8>]
|
2010-11-20 23:59:32 +08:00
|
|
|
if (FPOffset >= -255 && FPOffset < 0) {
|
|
|
|
FrameReg = RegInfo->getFrameRegister(MF);
|
|
|
|
return FPOffset;
|
|
|
|
}
|
|
|
|
} else if (Offset > (FPOffset < 0 ? -FPOffset : FPOffset)) {
|
|
|
|
// Otherwise, use SP or FP, whichever is closer to the stack slot.
|
|
|
|
FrameReg = RegInfo->getFrameRegister(MF);
|
|
|
|
return FPOffset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Use the base pointer if we have one.
|
|
|
|
if (RegInfo->hasBasePointer(MF))
|
|
|
|
FrameReg = RegInfo->getBaseRegister();
|
|
|
|
return Offset;
|
|
|
|
}
|
|
|
|
|
2011-01-10 20:39:04 +08:00
|
|
|
void ARMFrameLowering::emitPushInst(MachineBasicBlock &MBB,
|
2011-01-14 05:10:12 +08:00
|
|
|
MachineBasicBlock::iterator MI,
|
|
|
|
const std::vector<CalleeSavedInfo> &CSI,
|
|
|
|
unsigned StmOpc, unsigned StrOpc,
|
|
|
|
bool NoGap,
|
2011-03-06 02:43:32 +08:00
|
|
|
bool(*Func)(unsigned, bool),
|
2011-12-23 08:36:18 +08:00
|
|
|
unsigned NumAlignedDPRCS2Regs,
|
2011-03-06 02:43:32 +08:00
|
|
|
unsigned MIFlags) const {
|
2010-11-28 07:05:03 +08:00
|
|
|
MachineFunction &MF = *MBB.getParent();
|
2015-11-06 05:54:58 +08:00
|
|
|
const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
|
|
|
|
|
|
|
|
DebugLoc DL;
|
|
|
|
|
|
|
|
SmallVector<std::pair<unsigned,bool>, 4> Regs;
|
|
|
|
unsigned i = CSI.size();
|
2010-12-08 07:08:38 +08:00
|
|
|
while (i != 0) {
|
|
|
|
unsigned LastReg = 0;
|
|
|
|
for (; i != 0; --i) {
|
|
|
|
unsigned Reg = CSI[i-1].getReg();
|
2014-05-30 21:23:06 +08:00
|
|
|
if (!(Func)(Reg, STI.isTargetDarwin())) continue;
|
2010-12-08 07:08:38 +08:00
|
|
|
|
2011-12-23 08:36:18 +08:00
|
|
|
// D-registers in the aligned area DPRCS2 are NOT spilled here.
|
|
|
|
if (Reg >= ARM::D8 && Reg < ARM::D8 + NumAlignedDPRCS2Regs)
|
|
|
|
continue;
|
|
|
|
|
2010-12-08 07:08:38 +08:00
|
|
|
// Add the callee-saved register as live-in unless it's LR and
|
2010-12-10 00:14:46 +08:00
|
|
|
// @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.
|
2010-12-08 07:08:38 +08:00
|
|
|
bool isKill = true;
|
|
|
|
if (Reg == ARM::LR) {
|
|
|
|
if (MF.getFrameInfo()->isReturnAddressTaken() &&
|
|
|
|
MF.getRegInfo().isLiveIn(Reg))
|
|
|
|
isKill = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isKill)
|
|
|
|
MBB.addLiveIn(Reg);
|
2010-11-28 07:05:03 +08:00
|
|
|
|
2010-12-09 09:57:45 +08:00
|
|
|
// If NoGap is true, push consecutive registers and then leave the rest
|
2010-12-08 14:29:02 +08:00
|
|
|
// for other instructions. e.g.
|
2010-12-09 09:57:45 +08:00
|
|
|
// vpush {d8, d10, d11} -> vpush {d8}, vpush {d10, d11}
|
2010-12-08 14:29:02 +08:00
|
|
|
if (NoGap && LastReg && LastReg != Reg-1)
|
|
|
|
break;
|
2010-12-08 07:08:38 +08:00
|
|
|
LastReg = Reg;
|
|
|
|
Regs.push_back(std::make_pair(Reg, isKill));
|
|
|
|
}
|
2010-11-28 07:05:03 +08:00
|
|
|
|
2010-12-10 02:31:13 +08:00
|
|
|
if (Regs.empty())
|
|
|
|
continue;
|
|
|
|
if (Regs.size() > 1 || StrOpc== 0) {
|
2010-12-08 07:08:38 +08:00
|
|
|
MachineInstrBuilder MIB =
|
2010-12-10 02:31:13 +08:00
|
|
|
AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(StmOpc), ARM::SP)
|
2011-03-06 02:43:32 +08:00
|
|
|
.addReg(ARM::SP).setMIFlags(MIFlags));
|
2010-12-08 07:08:38 +08:00
|
|
|
for (unsigned i = 0, e = Regs.size(); i < e; ++i)
|
|
|
|
MIB.addReg(Regs[i].first, getKillRegState(Regs[i].second));
|
2010-12-10 02:31:13 +08:00
|
|
|
} else if (Regs.size() == 1) {
|
|
|
|
MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(StrOpc),
|
|
|
|
ARM::SP)
|
|
|
|
.addReg(Regs[0].first, getKillRegState(Regs[0].second))
|
2011-08-06 04:35:44 +08:00
|
|
|
.addReg(ARM::SP).setMIFlags(MIFlags)
|
|
|
|
.addImm(-4);
|
2010-12-10 02:31:13 +08:00
|
|
|
AddDefaultPred(MIB);
|
2010-12-08 07:08:38 +08:00
|
|
|
}
|
2010-12-10 02:31:13 +08:00
|
|
|
Regs.clear();
|
2014-03-12 19:29:23 +08:00
|
|
|
|
|
|
|
// Put any subsequent vpush instructions before this one: they will refer to
|
|
|
|
// higher register numbers so need to be pushed first in order to preserve
|
|
|
|
// monotonicity.
|
2015-07-21 05:42:14 +08:00
|
|
|
if (MI != MBB.begin())
|
|
|
|
--MI;
|
2010-11-28 07:05:03 +08:00
|
|
|
}
|
2010-12-08 07:08:38 +08:00
|
|
|
}
|
|
|
|
|
2011-01-10 20:39:04 +08:00
|
|
|
void ARMFrameLowering::emitPopInst(MachineBasicBlock &MBB,
|
2011-01-14 05:10:12 +08:00
|
|
|
MachineBasicBlock::iterator MI,
|
|
|
|
const std::vector<CalleeSavedInfo> &CSI,
|
|
|
|
unsigned LdmOpc, unsigned LdrOpc,
|
|
|
|
bool isVarArg, bool NoGap,
|
2011-12-23 08:36:18 +08:00
|
|
|
bool(*Func)(unsigned, bool),
|
|
|
|
unsigned NumAlignedDPRCS2Regs) const {
|
2010-12-08 07:08:38 +08:00
|
|
|
MachineFunction &MF = *MBB.getParent();
|
2014-08-05 10:39:49 +08:00
|
|
|
const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
|
2010-12-08 07:08:38 +08:00
|
|
|
ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
|
2015-07-21 05:42:14 +08:00
|
|
|
DebugLoc DL;
|
|
|
|
bool isTailCall = false;
|
|
|
|
bool isInterrupt = false;
|
2015-10-24 01:17:59 +08:00
|
|
|
bool isTrap = false;
|
2015-07-21 05:42:14 +08:00
|
|
|
if (MBB.end() != MI) {
|
|
|
|
DL = MI->getDebugLoc();
|
|
|
|
unsigned RetOpcode = MI->getOpcode();
|
|
|
|
isTailCall = (RetOpcode == ARM::TCRETURNdi || RetOpcode == ARM::TCRETURNri);
|
|
|
|
isInterrupt =
|
|
|
|
RetOpcode == ARM::SUBS_PC_LR || RetOpcode == ARM::t2SUBS_PC_LR;
|
2015-10-24 01:17:59 +08:00
|
|
|
isTrap =
|
|
|
|
RetOpcode == ARM::TRAP || RetOpcode == ARM::TRAPNaCl ||
|
|
|
|
RetOpcode == ARM::tTRAP;
|
2015-07-21 05:42:14 +08:00
|
|
|
}
|
2010-12-08 07:08:38 +08:00
|
|
|
|
|
|
|
SmallVector<unsigned, 4> Regs;
|
|
|
|
unsigned i = CSI.size();
|
|
|
|
while (i != 0) {
|
|
|
|
unsigned LastReg = 0;
|
|
|
|
bool DeleteRet = false;
|
|
|
|
for (; i != 0; --i) {
|
|
|
|
unsigned Reg = CSI[i-1].getReg();
|
2014-05-30 21:23:06 +08:00
|
|
|
if (!(Func)(Reg, STI.isTargetDarwin())) continue;
|
2010-12-08 07:08:38 +08:00
|
|
|
|
2011-12-23 08:36:18 +08:00
|
|
|
// The aligned reloads from area DPRCS2 are not inserted here.
|
|
|
|
if (Reg >= ARM::D8 && Reg < ARM::D8 + NumAlignedDPRCS2Regs)
|
|
|
|
continue;
|
|
|
|
|
2013-10-01 22:33:28 +08:00
|
|
|
if (Reg == ARM::LR && !isTailCall && !isVarArg && !isInterrupt &&
|
2015-10-24 01:17:59 +08:00
|
|
|
!isTrap && STI.hasV5TOps()) {
|
2015-07-21 05:42:14 +08:00
|
|
|
if (MBB.succ_empty()) {
|
|
|
|
Reg = ARM::PC;
|
|
|
|
DeleteRet = true;
|
|
|
|
LdmOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_RET : ARM::LDMIA_RET;
|
|
|
|
} else
|
|
|
|
LdmOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_UPD : ARM::LDMIA_UPD;
|
2010-12-08 07:08:38 +08:00
|
|
|
// Fold the return instruction into the LDM.
|
|
|
|
}
|
|
|
|
|
2010-12-08 14:29:02 +08:00
|
|
|
// If NoGap is true, pop consecutive registers and then leave the rest
|
|
|
|
// for other instructions. e.g.
|
|
|
|
// vpop {d8, d10, d11} -> vpop {d8}, vpop {d10, d11}
|
|
|
|
if (NoGap && LastReg && LastReg != Reg-1)
|
|
|
|
break;
|
|
|
|
|
2010-12-08 07:08:38 +08:00
|
|
|
LastReg = Reg;
|
|
|
|
Regs.push_back(Reg);
|
|
|
|
}
|
2010-11-28 07:05:03 +08:00
|
|
|
|
2010-12-10 02:31:13 +08:00
|
|
|
if (Regs.empty())
|
|
|
|
continue;
|
|
|
|
if (Regs.size() > 1 || LdrOpc == 0) {
|
2010-12-08 07:08:38 +08:00
|
|
|
MachineInstrBuilder MIB =
|
2010-12-10 02:31:13 +08:00
|
|
|
AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(LdmOpc), ARM::SP)
|
2010-12-08 07:08:38 +08:00
|
|
|
.addReg(ARM::SP));
|
|
|
|
for (unsigned i = 0, e = Regs.size(); i < e; ++i)
|
|
|
|
MIB.addReg(Regs[i], getDefRegState(true));
|
2015-07-21 05:42:14 +08:00
|
|
|
if (DeleteRet && MI != MBB.end()) {
|
2016-02-28 04:01:33 +08:00
|
|
|
MIB.copyImplicitOps(*MI);
|
2010-12-08 07:08:38 +08:00
|
|
|
MI->eraseFromParent();
|
2011-08-26 01:50:53 +08:00
|
|
|
}
|
2010-12-08 07:08:38 +08:00
|
|
|
MI = MIB;
|
2010-12-10 02:31:13 +08:00
|
|
|
} else if (Regs.size() == 1) {
|
|
|
|
// If we adjusted the reg to PC from LR above, switch it back here. We
|
|
|
|
// only do that for LDM.
|
|
|
|
if (Regs[0] == ARM::PC)
|
|
|
|
Regs[0] = ARM::LR;
|
|
|
|
MachineInstrBuilder MIB =
|
|
|
|
BuildMI(MBB, MI, DL, TII.get(LdrOpc), Regs[0])
|
|
|
|
.addReg(ARM::SP, RegState::Define)
|
|
|
|
.addReg(ARM::SP);
|
|
|
|
// ARM mode needs an extra reg0 here due to addrmode2. Will go away once
|
|
|
|
// that refactoring is complete (eventually).
|
2011-07-27 04:54:26 +08:00
|
|
|
if (LdrOpc == ARM::LDR_POST_REG || LdrOpc == ARM::LDR_POST_IMM) {
|
2010-12-10 02:31:13 +08:00
|
|
|
MIB.addReg(0);
|
|
|
|
MIB.addImm(ARM_AM::getAM2Opc(ARM_AM::add, 4, ARM_AM::no_shift));
|
|
|
|
} else
|
|
|
|
MIB.addImm(4);
|
|
|
|
AddDefaultPred(MIB);
|
2010-12-08 07:08:38 +08:00
|
|
|
}
|
2010-12-10 02:31:13 +08:00
|
|
|
Regs.clear();
|
2014-03-12 19:29:23 +08:00
|
|
|
|
|
|
|
// Put any subsequent vpop instructions after this one: they will refer to
|
|
|
|
// higher register numbers so need to be popped afterwards.
|
2015-07-21 05:42:14 +08:00
|
|
|
if (MI != MBB.end())
|
|
|
|
++MI;
|
2010-12-08 03:59:34 +08:00
|
|
|
}
|
2010-11-28 07:05:03 +08:00
|
|
|
}
|
|
|
|
|
2011-12-23 08:36:18 +08:00
|
|
|
/// Emit aligned spill instructions for NumAlignedDPRCS2Regs D-registers
|
2011-12-24 12:17:01 +08:00
|
|
|
/// starting from d8. Also insert stack realignment code and leave the stack
|
|
|
|
/// pointer pointing to the d8 spill slot.
|
2011-12-23 08:36:18 +08:00
|
|
|
static void emitAlignedDPRCS2Spills(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MI,
|
|
|
|
unsigned NumAlignedDPRCS2Regs,
|
|
|
|
const std::vector<CalleeSavedInfo> &CSI,
|
|
|
|
const TargetRegisterInfo *TRI) {
|
|
|
|
MachineFunction &MF = *MBB.getParent();
|
|
|
|
ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
|
2015-10-15 08:41:26 +08:00
|
|
|
DebugLoc DL = MI != MBB.end() ? MI->getDebugLoc() : DebugLoc();
|
2014-08-05 10:39:49 +08:00
|
|
|
const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
|
2011-12-23 08:36:18 +08:00
|
|
|
MachineFrameInfo &MFI = *MF.getFrameInfo();
|
|
|
|
|
|
|
|
// Mark the D-register spill slots as properly aligned. Since MFI computes
|
|
|
|
// stack slot layout backwards, this can actually mean that the d-reg stack
|
|
|
|
// slot offsets can be wrong. The offset for d8 will always be correct.
|
|
|
|
for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
|
|
|
|
unsigned DNum = CSI[i].getReg() - ARM::D8;
|
2015-10-29 06:46:43 +08:00
|
|
|
if (DNum > NumAlignedDPRCS2Regs - 1)
|
2011-12-23 08:36:18 +08:00
|
|
|
continue;
|
|
|
|
int FI = CSI[i].getFrameIdx();
|
|
|
|
// The even-numbered registers will be 16-byte aligned, the odd-numbered
|
|
|
|
// registers will be 8-byte aligned.
|
|
|
|
MFI.setObjectAlignment(FI, DNum % 2 ? 8 : 16);
|
|
|
|
|
|
|
|
// The stack slot for D8 needs to be maximally aligned because this is
|
|
|
|
// actually the point where we align the stack pointer. MachineFrameInfo
|
|
|
|
// computes all offsets relative to the incoming stack pointer which is a
|
|
|
|
// bit weird when realigning the stack. Any extra padding for this
|
|
|
|
// over-alignment is not realized because the code inserted below adjusts
|
|
|
|
// the stack pointer by numregs * 8 before aligning the stack pointer.
|
|
|
|
if (DNum == 0)
|
|
|
|
MFI.setObjectAlignment(FI, MFI.getMaxAlignment());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move the stack pointer to the d8 spill slot, and align it at the same
|
|
|
|
// time. Leave the stack slot address in the scratch register r4.
|
|
|
|
//
|
|
|
|
// sub r4, sp, #numregs * 8
|
|
|
|
// bic r4, r4, #align - 1
|
|
|
|
// mov sp, r4
|
|
|
|
//
|
|
|
|
bool isThumb = AFI->isThumbFunction();
|
|
|
|
assert(!AFI->isThumb1OnlyFunction() && "Can't realign stack for thumb1");
|
|
|
|
AFI->setShouldRestoreSPFromFP(true);
|
|
|
|
|
|
|
|
// sub r4, sp, #numregs * 8
|
|
|
|
// The immediate is <= 64, so it doesn't need any special encoding.
|
|
|
|
unsigned Opc = isThumb ? ARM::t2SUBri : ARM::SUBri;
|
|
|
|
AddDefaultCC(AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4)
|
2015-01-08 23:09:14 +08:00
|
|
|
.addReg(ARM::SP)
|
|
|
|
.addImm(8 * NumAlignedDPRCS2Regs)));
|
2011-12-23 08:36:18 +08:00
|
|
|
|
|
|
|
unsigned MaxAlign = MF.getFrameInfo()->getMaxAlignment();
|
2015-01-08 23:09:14 +08:00
|
|
|
// We must set parameter MustBeSingleInstruction to true, since
|
|
|
|
// skipAlignedDPRCS2Spills expects exactly 3 instructions to perform
|
|
|
|
// stack alignment. Luckily, this can always be done since all ARM
|
|
|
|
// architecture versions that support Neon also support the BFC
|
|
|
|
// instruction.
|
|
|
|
emitAligningInstructions(MF, AFI, TII, MBB, MI, DL, ARM::R4, MaxAlign, true);
|
2011-12-23 08:36:18 +08:00
|
|
|
|
|
|
|
// mov sp, r4
|
|
|
|
// The stack pointer must be adjusted before spilling anything, otherwise
|
|
|
|
// the stack slots could be clobbered by an interrupt handler.
|
|
|
|
// Leave r4 live, it is used below.
|
|
|
|
Opc = isThumb ? ARM::tMOVr : ARM::MOVr;
|
|
|
|
MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(Opc), ARM::SP)
|
|
|
|
.addReg(ARM::R4);
|
|
|
|
MIB = AddDefaultPred(MIB);
|
|
|
|
if (!isThumb)
|
|
|
|
AddDefaultCC(MIB);
|
|
|
|
|
|
|
|
// Now spill NumAlignedDPRCS2Regs registers starting from d8.
|
|
|
|
// r4 holds the stack slot address.
|
|
|
|
unsigned NextReg = ARM::D8;
|
|
|
|
|
|
|
|
// 16-byte aligned vst1.64 with 4 d-regs and address writeback.
|
|
|
|
// The writeback is only needed when emitting two vst1.64 instructions.
|
|
|
|
if (NumAlignedDPRCS2Regs >= 6) {
|
|
|
|
unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
|
2012-04-20 15:30:17 +08:00
|
|
|
&ARM::QQPRRegClass);
|
2011-12-23 08:36:18 +08:00
|
|
|
MBB.addLiveIn(SupReg);
|
|
|
|
AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1d64Qwb_fixed),
|
|
|
|
ARM::R4)
|
|
|
|
.addReg(ARM::R4, RegState::Kill).addImm(16)
|
|
|
|
.addReg(NextReg)
|
|
|
|
.addReg(SupReg, RegState::ImplicitKill));
|
|
|
|
NextReg += 4;
|
|
|
|
NumAlignedDPRCS2Regs -= 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We won't modify r4 beyond this point. It currently points to the next
|
|
|
|
// register to be spilled.
|
|
|
|
unsigned R4BaseReg = NextReg;
|
|
|
|
|
|
|
|
// 16-byte aligned vst1.64 with 4 d-regs, no writeback.
|
|
|
|
if (NumAlignedDPRCS2Regs >= 4) {
|
|
|
|
unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
|
2012-04-20 15:30:17 +08:00
|
|
|
&ARM::QQPRRegClass);
|
2011-12-23 08:36:18 +08:00
|
|
|
MBB.addLiveIn(SupReg);
|
|
|
|
AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1d64Q))
|
|
|
|
.addReg(ARM::R4).addImm(16).addReg(NextReg)
|
|
|
|
.addReg(SupReg, RegState::ImplicitKill));
|
|
|
|
NextReg += 4;
|
|
|
|
NumAlignedDPRCS2Regs -= 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 16-byte aligned vst1.64 with 2 d-regs.
|
|
|
|
if (NumAlignedDPRCS2Regs >= 2) {
|
|
|
|
unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
|
2012-04-20 15:30:17 +08:00
|
|
|
&ARM::QPRRegClass);
|
2011-12-23 08:36:18 +08:00
|
|
|
MBB.addLiveIn(SupReg);
|
|
|
|
AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1q64))
|
2012-03-06 03:33:30 +08:00
|
|
|
.addReg(ARM::R4).addImm(16).addReg(SupReg));
|
2011-12-23 08:36:18 +08:00
|
|
|
NextReg += 2;
|
|
|
|
NumAlignedDPRCS2Regs -= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, use a vanilla vstr.64 for the odd last register.
|
|
|
|
if (NumAlignedDPRCS2Regs) {
|
|
|
|
MBB.addLiveIn(NextReg);
|
|
|
|
// vstr.64 uses addrmode5 which has an offset scale of 4.
|
|
|
|
AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VSTRD))
|
|
|
|
.addReg(NextReg)
|
|
|
|
.addReg(ARM::R4).addImm((NextReg-R4BaseReg)*2));
|
|
|
|
}
|
|
|
|
|
|
|
|
// The last spill instruction inserted should kill the scratch register r4.
|
2014-03-02 20:27:27 +08:00
|
|
|
std::prev(MI)->addRegisterKilled(ARM::R4, TRI);
|
2011-12-23 08:36:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Skip past the code inserted by emitAlignedDPRCS2Spills, and return an
|
|
|
|
/// iterator to the following instruction.
|
|
|
|
static MachineBasicBlock::iterator
|
|
|
|
skipAlignedDPRCS2Spills(MachineBasicBlock::iterator MI,
|
|
|
|
unsigned NumAlignedDPRCS2Regs) {
|
|
|
|
// sub r4, sp, #numregs * 8
|
|
|
|
// bic r4, r4, #align - 1
|
|
|
|
// mov sp, r4
|
|
|
|
++MI; ++MI; ++MI;
|
|
|
|
assert(MI->mayStore() && "Expecting spill instruction");
|
|
|
|
|
|
|
|
// These switches all fall through.
|
|
|
|
switch(NumAlignedDPRCS2Regs) {
|
|
|
|
case 7:
|
|
|
|
++MI;
|
|
|
|
assert(MI->mayStore() && "Expecting spill instruction");
|
|
|
|
default:
|
|
|
|
++MI;
|
|
|
|
assert(MI->mayStore() && "Expecting spill instruction");
|
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
case 4:
|
|
|
|
assert(MI->killsRegister(ARM::R4) && "Missed kill flag");
|
|
|
|
++MI;
|
|
|
|
}
|
|
|
|
return MI;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit aligned reload instructions for NumAlignedDPRCS2Regs D-registers
|
|
|
|
/// starting from d8. These instructions are assumed to execute while the
|
|
|
|
/// stack is still aligned, unlike the code inserted by emitPopInst.
|
|
|
|
static void emitAlignedDPRCS2Restores(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MI,
|
|
|
|
unsigned NumAlignedDPRCS2Regs,
|
|
|
|
const std::vector<CalleeSavedInfo> &CSI,
|
|
|
|
const TargetRegisterInfo *TRI) {
|
|
|
|
MachineFunction &MF = *MBB.getParent();
|
|
|
|
ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
|
2015-10-15 08:41:26 +08:00
|
|
|
DebugLoc DL = MI != MBB.end() ? MI->getDebugLoc() : DebugLoc();
|
2014-08-05 10:39:49 +08:00
|
|
|
const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
|
2011-12-23 08:36:18 +08:00
|
|
|
|
|
|
|
// Find the frame index assigned to d8.
|
|
|
|
int D8SpillFI = 0;
|
|
|
|
for (unsigned i = 0, e = CSI.size(); i != e; ++i)
|
|
|
|
if (CSI[i].getReg() == ARM::D8) {
|
|
|
|
D8SpillFI = CSI[i].getFrameIdx();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Materialize the address of the d8 spill slot into the scratch register r4.
|
|
|
|
// This can be fairly complicated if the stack frame is large, so just use
|
|
|
|
// the normal frame index elimination mechanism to do it. This code runs as
|
|
|
|
// the initial part of the epilog where the stack and base pointers haven't
|
|
|
|
// been changed yet.
|
|
|
|
bool isThumb = AFI->isThumbFunction();
|
|
|
|
assert(!AFI->isThumb1OnlyFunction() && "Can't realign stack for thumb1");
|
|
|
|
|
|
|
|
unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
|
|
|
|
AddDefaultCC(AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4)
|
|
|
|
.addFrameIndex(D8SpillFI).addImm(0)));
|
|
|
|
|
|
|
|
// Now restore NumAlignedDPRCS2Regs registers starting from d8.
|
|
|
|
unsigned NextReg = ARM::D8;
|
|
|
|
|
|
|
|
// 16-byte aligned vld1.64 with 4 d-regs and writeback.
|
|
|
|
if (NumAlignedDPRCS2Regs >= 6) {
|
|
|
|
unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
|
2012-04-20 15:30:17 +08:00
|
|
|
&ARM::QQPRRegClass);
|
2011-12-23 08:36:18 +08:00
|
|
|
AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1d64Qwb_fixed), NextReg)
|
|
|
|
.addReg(ARM::R4, RegState::Define)
|
|
|
|
.addReg(ARM::R4, RegState::Kill).addImm(16)
|
|
|
|
.addReg(SupReg, RegState::ImplicitDefine));
|
|
|
|
NextReg += 4;
|
|
|
|
NumAlignedDPRCS2Regs -= 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We won't modify r4 beyond this point. It currently points to the next
|
|
|
|
// register to be spilled.
|
|
|
|
unsigned R4BaseReg = NextReg;
|
|
|
|
|
|
|
|
// 16-byte aligned vld1.64 with 4 d-regs, no writeback.
|
|
|
|
if (NumAlignedDPRCS2Regs >= 4) {
|
|
|
|
unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
|
2012-04-20 15:30:17 +08:00
|
|
|
&ARM::QQPRRegClass);
|
2011-12-23 08:36:18 +08:00
|
|
|
AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1d64Q), NextReg)
|
|
|
|
.addReg(ARM::R4).addImm(16)
|
|
|
|
.addReg(SupReg, RegState::ImplicitDefine));
|
|
|
|
NextReg += 4;
|
|
|
|
NumAlignedDPRCS2Regs -= 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 16-byte aligned vld1.64 with 2 d-regs.
|
|
|
|
if (NumAlignedDPRCS2Regs >= 2) {
|
|
|
|
unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
|
2012-04-20 15:30:17 +08:00
|
|
|
&ARM::QPRRegClass);
|
2012-03-06 03:33:30 +08:00
|
|
|
AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1q64), SupReg)
|
|
|
|
.addReg(ARM::R4).addImm(16));
|
2011-12-23 08:36:18 +08:00
|
|
|
NextReg += 2;
|
|
|
|
NumAlignedDPRCS2Regs -= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, use a vanilla vldr.64 for the remaining odd register.
|
|
|
|
if (NumAlignedDPRCS2Regs)
|
|
|
|
AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLDRD), NextReg)
|
|
|
|
.addReg(ARM::R4).addImm(2*(NextReg-R4BaseReg)));
|
|
|
|
|
|
|
|
// Last store kills r4.
|
2014-03-02 20:27:27 +08:00
|
|
|
std::prev(MI)->addRegisterKilled(ARM::R4, TRI);
|
2011-12-23 08:36:18 +08:00
|
|
|
}
|
|
|
|
|
2011-01-10 20:39:04 +08:00
|
|
|
bool ARMFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
|
2011-01-14 05:10:12 +08:00
|
|
|
MachineBasicBlock::iterator MI,
|
|
|
|
const std::vector<CalleeSavedInfo> &CSI,
|
|
|
|
const TargetRegisterInfo *TRI) const {
|
2010-11-28 07:05:03 +08:00
|
|
|
if (CSI.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
MachineFunction &MF = *MBB.getParent();
|
|
|
|
ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
|
|
|
|
|
|
|
|
unsigned PushOpc = AFI->isThumbFunction() ? ARM::t2STMDB_UPD : ARM::STMDB_UPD;
|
2011-09-03 02:46:15 +08:00
|
|
|
unsigned PushOneOpc = AFI->isThumbFunction() ?
|
|
|
|
ARM::t2STR_PRE : ARM::STR_PRE_IMM;
|
2010-11-28 07:05:03 +08:00
|
|
|
unsigned FltOpc = ARM::VSTMDDB_UPD;
|
2011-12-23 08:36:18 +08:00
|
|
|
unsigned NumAlignedDPRCS2Regs = AFI->getNumAlignedDPRCS2Regs();
|
|
|
|
emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea1Register, 0,
|
2011-03-06 02:43:32 +08:00
|
|
|
MachineInstr::FrameSetup);
|
2011-12-23 08:36:18 +08:00
|
|
|
emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea2Register, 0,
|
2011-03-06 02:43:32 +08:00
|
|
|
MachineInstr::FrameSetup);
|
|
|
|
emitPushInst(MBB, MI, CSI, FltOpc, 0, true, &isARMArea3Register,
|
2011-12-23 08:36:18 +08:00
|
|
|
NumAlignedDPRCS2Regs, MachineInstr::FrameSetup);
|
|
|
|
|
|
|
|
// The code above does not insert spill code for the aligned DPRCS2 registers.
|
|
|
|
// The stack realignment code will be inserted between the push instructions
|
|
|
|
// and these spills.
|
|
|
|
if (NumAlignedDPRCS2Regs)
|
|
|
|
emitAlignedDPRCS2Spills(MBB, MI, NumAlignedDPRCS2Regs, CSI, TRI);
|
2010-11-28 07:05:03 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-01-10 20:39:04 +08:00
|
|
|
bool ARMFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
|
2011-01-14 05:10:12 +08:00
|
|
|
MachineBasicBlock::iterator MI,
|
|
|
|
const std::vector<CalleeSavedInfo> &CSI,
|
|
|
|
const TargetRegisterInfo *TRI) const {
|
2010-11-28 07:05:03 +08:00
|
|
|
if (CSI.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
MachineFunction &MF = *MBB.getParent();
|
|
|
|
ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
|
2013-04-30 15:19:58 +08:00
|
|
|
bool isVarArg = AFI->getArgRegsSaveSize() > 0;
|
2011-12-23 08:36:18 +08:00
|
|
|
unsigned NumAlignedDPRCS2Regs = AFI->getNumAlignedDPRCS2Regs();
|
|
|
|
|
|
|
|
// The emitPopInst calls below do not insert reloads for the aligned DPRCS2
|
|
|
|
// registers. Do that here instead.
|
|
|
|
if (NumAlignedDPRCS2Regs)
|
|
|
|
emitAlignedDPRCS2Restores(MBB, MI, NumAlignedDPRCS2Regs, CSI, TRI);
|
2010-11-28 07:05:03 +08:00
|
|
|
|
|
|
|
unsigned PopOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_UPD : ARM::LDMIA_UPD;
|
2011-09-03 02:46:15 +08:00
|
|
|
unsigned LdrOpc = AFI->isThumbFunction() ? ARM::t2LDR_POST :ARM::LDR_POST_IMM;
|
2010-11-28 07:05:03 +08:00
|
|
|
unsigned FltOpc = ARM::VLDMDIA_UPD;
|
2011-12-23 08:36:18 +08:00
|
|
|
emitPopInst(MBB, MI, CSI, FltOpc, 0, isVarArg, true, &isARMArea3Register,
|
|
|
|
NumAlignedDPRCS2Regs);
|
2010-12-10 02:31:13 +08:00
|
|
|
emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false,
|
2011-12-23 08:36:18 +08:00
|
|
|
&isARMArea2Register, 0);
|
2010-12-10 02:31:13 +08:00
|
|
|
emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false,
|
2011-12-23 08:36:18 +08:00
|
|
|
&isARMArea1Register, 0);
|
2010-11-28 07:05:03 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2010-11-28 07:05:25 +08:00
|
|
|
|
|
|
|
// FIXME: Make generic?
|
|
|
|
static unsigned GetFunctionSizeInBytes(const MachineFunction &MF,
|
|
|
|
const ARMBaseInstrInfo &TII) {
|
|
|
|
unsigned FnSize = 0;
|
2014-04-04 10:10:55 +08:00
|
|
|
for (auto &MBB : MF) {
|
|
|
|
for (auto &MI : MBB)
|
|
|
|
FnSize += TII.GetInstSizeInBytes(&MI);
|
2010-11-28 07:05:25 +08:00
|
|
|
}
|
|
|
|
return FnSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// estimateRSStackSizeLimit - Look at each instruction that references stack
|
|
|
|
/// frames and return the stack size limit beyond which some of these
|
|
|
|
/// instructions will require a scratch register during their expansion later.
|
|
|
|
// FIXME: Move to TII?
|
|
|
|
static unsigned estimateRSStackSizeLimit(MachineFunction &MF,
|
2011-01-10 20:39:04 +08:00
|
|
|
const TargetFrameLowering *TFI) {
|
2010-11-28 07:05:25 +08:00
|
|
|
const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
|
|
|
|
unsigned Limit = (1 << 12) - 1;
|
2014-04-04 10:10:55 +08:00
|
|
|
for (auto &MBB : MF) {
|
|
|
|
for (auto &MI : MBB) {
|
|
|
|
for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
|
|
|
|
if (!MI.getOperand(i).isFI())
|
|
|
|
continue;
|
2010-11-28 07:05:25 +08:00
|
|
|
|
|
|
|
// When using ADDri to get the address of a stack object, 255 is the
|
|
|
|
// largest offset guaranteed to fit in the immediate offset.
|
2014-04-04 10:10:55 +08:00
|
|
|
if (MI.getOpcode() == ARM::ADDri) {
|
2010-11-28 07:05:25 +08:00
|
|
|
Limit = std::min(Limit, (1U << 8) - 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise check the addressing mode.
|
2014-04-04 10:10:55 +08:00
|
|
|
switch (MI.getDesc().TSFlags & ARMII::AddrModeMask) {
|
2010-11-28 07:05:25 +08:00
|
|
|
case ARMII::AddrMode3:
|
|
|
|
case ARMII::AddrModeT2_i8:
|
|
|
|
Limit = std::min(Limit, (1U << 8) - 1);
|
|
|
|
break;
|
|
|
|
case ARMII::AddrMode5:
|
|
|
|
case ARMII::AddrModeT2_i8s4:
|
|
|
|
Limit = std::min(Limit, ((1U << 8) - 1) * 4);
|
|
|
|
break;
|
|
|
|
case ARMII::AddrModeT2_i12:
|
|
|
|
// i12 supports only positive offset so these will be converted to
|
|
|
|
// i8 opcodes. See llvm::rewriteT2FrameIndex.
|
|
|
|
if (TFI->hasFP(MF) && AFI->hasStackFrame())
|
|
|
|
Limit = std::min(Limit, (1U << 8) - 1);
|
|
|
|
break;
|
|
|
|
case ARMII::AddrMode4:
|
|
|
|
case ARMII::AddrMode6:
|
|
|
|
// Addressing modes 4 & 6 (load/store) instructions can't encode an
|
|
|
|
// immediate offset for stack references.
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break; // At most one FI per instruction
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Limit;
|
|
|
|
}
|
|
|
|
|
2011-12-23 08:36:18 +08:00
|
|
|
// In functions that realign the stack, it can be an advantage to spill the
|
|
|
|
// callee-saved vector registers after realigning the stack. The vst1 and vld1
|
|
|
|
// instructions take alignment hints that can improve performance.
|
|
|
|
//
|
2015-07-15 01:17:13 +08:00
|
|
|
static void
|
|
|
|
checkNumAlignedDPRCS2Regs(MachineFunction &MF, BitVector &SavedRegs) {
|
2011-12-23 08:36:18 +08:00
|
|
|
MF.getInfo<ARMFunctionInfo>()->setNumAlignedDPRCS2Regs(0);
|
|
|
|
if (!SpillAlignedNEONRegs)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Naked functions don't spill callee-saved registers.
|
2015-02-14 10:24:44 +08:00
|
|
|
if (MF.getFunction()->hasFnAttribute(Attribute::Naked))
|
2011-12-23 08:36:18 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// We are planning to use NEON instructions vst1 / vld1.
|
2015-01-29 08:19:33 +08:00
|
|
|
if (!static_cast<const ARMSubtarget &>(MF.getSubtarget()).hasNEON())
|
2011-12-23 08:36:18 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Don't bother if the default stack alignment is sufficiently high.
|
2015-01-29 08:19:33 +08:00
|
|
|
if (MF.getSubtarget().getFrameLowering()->getStackAlignment() >= 8)
|
2011-12-23 08:36:18 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Aligned spills require stack realignment.
|
2015-01-29 08:19:33 +08:00
|
|
|
if (!static_cast<const ARMBaseRegisterInfo *>(
|
|
|
|
MF.getSubtarget().getRegisterInfo())->canRealignStack(MF))
|
2011-12-23 08:36:18 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// We always spill contiguous d-registers starting from d8. Count how many
|
|
|
|
// needs spilling. The register allocator will almost always use the
|
|
|
|
// callee-saved registers in order, but it can happen that there are holes in
|
|
|
|
// the range. Registers above the hole will be spilled to the standard DPRCS
|
|
|
|
// area.
|
|
|
|
unsigned NumSpills = 0;
|
|
|
|
for (; NumSpills < 8; ++NumSpills)
|
2015-07-15 01:17:13 +08:00
|
|
|
if (!SavedRegs.test(ARM::D8 + NumSpills))
|
2011-12-23 08:36:18 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
// Don't do this for just one d-register. It's not worth it.
|
|
|
|
if (NumSpills < 2)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Spill the first NumSpills D-registers after realigning the stack.
|
|
|
|
MF.getInfo<ARMFunctionInfo>()->setNumAlignedDPRCS2Regs(NumSpills);
|
|
|
|
|
|
|
|
// A scratch register is required for the vst1 / vld1 instructions.
|
2015-07-15 01:17:13 +08:00
|
|
|
SavedRegs.set(ARM::R4);
|
2011-12-23 08:36:18 +08:00
|
|
|
}
|
|
|
|
|
2015-07-15 01:17:13 +08:00
|
|
|
void ARMFrameLowering::determineCalleeSaves(MachineFunction &MF,
|
|
|
|
BitVector &SavedRegs,
|
|
|
|
RegScavenger *RS) const {
|
|
|
|
TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
|
2010-11-28 07:05:25 +08:00
|
|
|
// This tells PEI to spill the FP as if it is any other callee-save register
|
|
|
|
// to take advantage the eliminateFrameIndex machinery. This also ensures it
|
|
|
|
// is spilled in the order specified by getCalleeSavedRegs() to make it easier
|
|
|
|
// to combine multiple loads / stores.
|
|
|
|
bool CanEliminateFrame = true;
|
|
|
|
bool CS1Spilled = false;
|
|
|
|
bool LRSpilled = false;
|
|
|
|
unsigned NumGPRSpills = 0;
|
|
|
|
SmallVector<unsigned, 4> UnspilledCS1GPRs;
|
|
|
|
SmallVector<unsigned, 4> UnspilledCS2GPRs;
|
2014-08-05 05:25:23 +08:00
|
|
|
const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>(
|
2014-08-05 10:39:49 +08:00
|
|
|
MF.getSubtarget().getRegisterInfo());
|
2010-11-28 07:05:25 +08:00
|
|
|
const ARMBaseInstrInfo &TII =
|
2014-08-05 10:39:49 +08:00
|
|
|
*static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
|
2010-11-28 07:05:25 +08:00
|
|
|
ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
|
|
|
|
MachineFrameInfo *MFI = MF.getFrameInfo();
|
2012-10-27 05:43:05 +08:00
|
|
|
MachineRegisterInfo &MRI = MF.getRegInfo();
|
2010-11-28 07:05:25 +08:00
|
|
|
unsigned FramePtr = RegInfo->getFrameRegister(MF);
|
|
|
|
|
|
|
|
// Spill R4 if Thumb2 function requires stack realignment - it will be used as
|
|
|
|
// scratch register. Also spill R4 if Thumb2 function has varsized objects,
|
2011-01-16 13:14:33 +08:00
|
|
|
// since it's not always possible to restore sp from fp in a single
|
|
|
|
// instruction.
|
2010-11-28 07:05:25 +08:00
|
|
|
// FIXME: It will be better just to find spare register here.
|
|
|
|
if (AFI->isThumb2Function() &&
|
|
|
|
(MFI->hasVarSizedObjects() || RegInfo->needsStackRealignment(MF)))
|
2015-07-15 01:17:13 +08:00
|
|
|
SavedRegs.set(ARM::R4);
|
2010-11-28 07:05:25 +08:00
|
|
|
|
2011-01-16 13:14:33 +08:00
|
|
|
if (AFI->isThumb1OnlyFunction()) {
|
|
|
|
// Spill LR if Thumb1 function uses variable length argument lists.
|
2013-04-30 15:19:58 +08:00
|
|
|
if (AFI->getArgRegsSaveSize() > 0)
|
2015-07-15 01:17:13 +08:00
|
|
|
SavedRegs.set(ARM::LR);
|
2011-01-16 13:14:33 +08:00
|
|
|
|
2011-06-14 05:18:25 +08:00
|
|
|
// Spill R4 if Thumb1 epilogue has to restore SP from FP. We don't know
|
|
|
|
// for sure what the stack size will be, but for this, an estimate is good
|
|
|
|
// enough. If there anything changes it, it'll be a spill, which implies
|
|
|
|
// we've used all the registers and so R4 is already used, so not marking
|
2011-10-20 08:07:12 +08:00
|
|
|
// it here will be OK.
|
2011-01-16 13:14:33 +08:00
|
|
|
// FIXME: It will be better just to find spare register here.
|
2013-03-15 05:15:20 +08:00
|
|
|
unsigned StackSize = MFI->estimateStackSize(MF);
|
2011-10-20 08:07:12 +08:00
|
|
|
if (MFI->hasVarSizedObjects() || StackSize > 508)
|
2015-07-15 01:17:13 +08:00
|
|
|
SavedRegs.set(ARM::R4);
|
2011-01-16 13:14:33 +08:00
|
|
|
}
|
2010-11-28 07:05:25 +08:00
|
|
|
|
2011-12-23 08:36:18 +08:00
|
|
|
// See if we can spill vector registers to aligned stack.
|
2015-07-15 01:17:13 +08:00
|
|
|
checkNumAlignedDPRCS2Regs(MF, SavedRegs);
|
2011-12-23 08:36:18 +08:00
|
|
|
|
2010-11-28 07:05:25 +08:00
|
|
|
// Spill the BasePtr if it's used.
|
|
|
|
if (RegInfo->hasBasePointer(MF))
|
2015-07-15 01:17:13 +08:00
|
|
|
SavedRegs.set(RegInfo->getBaseRegister());
|
2010-11-28 07:05:25 +08:00
|
|
|
|
|
|
|
// Don't spill FP if the frame can be eliminated. This is determined
|
2015-07-15 01:17:13 +08:00
|
|
|
// by scanning the callee-save registers to see if any is modified.
|
2014-04-04 13:16:06 +08:00
|
|
|
const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF);
|
2010-11-28 07:05:25 +08:00
|
|
|
for (unsigned i = 0; CSRegs[i]; ++i) {
|
|
|
|
unsigned Reg = CSRegs[i];
|
|
|
|
bool Spilled = false;
|
2015-07-15 01:17:13 +08:00
|
|
|
if (SavedRegs.test(Reg)) {
|
2010-11-28 07:05:25 +08:00
|
|
|
Spilled = true;
|
|
|
|
CanEliminateFrame = false;
|
|
|
|
}
|
|
|
|
|
2012-04-20 15:30:17 +08:00
|
|
|
if (!ARM::GPRRegClass.contains(Reg))
|
2010-11-28 07:05:25 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (Spilled) {
|
|
|
|
NumGPRSpills++;
|
|
|
|
|
2014-05-30 21:23:06 +08:00
|
|
|
if (!STI.isTargetDarwin()) {
|
2010-11-28 07:05:25 +08:00
|
|
|
if (Reg == ARM::LR)
|
|
|
|
LRSpilled = true;
|
|
|
|
CS1Spilled = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep track if LR and any of R4, R5, R6, and R7 is spilled.
|
|
|
|
switch (Reg) {
|
|
|
|
case ARM::LR:
|
|
|
|
LRSpilled = true;
|
|
|
|
// Fallthrough
|
2013-10-01 22:33:28 +08:00
|
|
|
case ARM::R0: case ARM::R1:
|
|
|
|
case ARM::R2: case ARM::R3:
|
2010-11-28 07:05:25 +08:00
|
|
|
case ARM::R4: case ARM::R5:
|
|
|
|
case ARM::R6: case ARM::R7:
|
|
|
|
CS1Spilled = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2014-05-30 21:23:06 +08:00
|
|
|
if (!STI.isTargetDarwin()) {
|
2010-11-28 07:05:25 +08:00
|
|
|
UnspilledCS1GPRs.push_back(Reg);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (Reg) {
|
2013-10-01 22:33:28 +08:00
|
|
|
case ARM::R0: case ARM::R1:
|
|
|
|
case ARM::R2: case ARM::R3:
|
2010-11-28 07:05:25 +08:00
|
|
|
case ARM::R4: case ARM::R5:
|
|
|
|
case ARM::R6: case ARM::R7:
|
|
|
|
case ARM::LR:
|
|
|
|
UnspilledCS1GPRs.push_back(Reg);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UnspilledCS2GPRs.push_back(Reg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ForceLRSpill = false;
|
|
|
|
if (!LRSpilled && AFI->isThumb1OnlyFunction()) {
|
|
|
|
unsigned FnSize = GetFunctionSizeInBytes(MF, TII);
|
|
|
|
// Force LR to be spilled if the Thumb function size is > 2048. This enables
|
|
|
|
// use of BL to implement far jump. If it turns out that it's not needed
|
|
|
|
// then the branch fix up path will undo it.
|
|
|
|
if (FnSize >= (1 << 11)) {
|
|
|
|
CanEliminateFrame = false;
|
|
|
|
ForceLRSpill = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If any of the stack slot references may be out of range of an immediate
|
|
|
|
// offset, make sure a register (or a spill slot) is available for the
|
|
|
|
// register scavenger. Note that if we're indexing off the frame pointer, the
|
|
|
|
// effective stack size is 4 bytes larger since the FP points to the stack
|
|
|
|
// slot of the previous FP. Also, if we have variable sized objects in the
|
|
|
|
// function, stack slot references will often be negative, and some of
|
|
|
|
// our instructions are positive-offset only, so conservatively consider
|
|
|
|
// that case to want a spill slot (or register) as well. Similarly, if
|
|
|
|
// the function adjusts the stack pointer during execution and the
|
|
|
|
// adjustments aren't already part of our stack size estimate, our offset
|
|
|
|
// calculations may be off, so be conservative.
|
|
|
|
// FIXME: We could add logic to be more precise about negative offsets
|
|
|
|
// and which instructions will need a scratch register for them. Is it
|
|
|
|
// worth the effort and added fragility?
|
2015-10-09 11:19:03 +08:00
|
|
|
bool BigStack = (RS && (MFI->estimateStackSize(MF) +
|
|
|
|
((hasFP(MF) && AFI->hasStackFrame()) ? 4 : 0) >=
|
|
|
|
estimateRSStackSizeLimit(MF, this))) ||
|
|
|
|
MFI->hasVarSizedObjects() ||
|
|
|
|
(MFI->adjustsStack() && !canSimplifyCallFramePseudos(MF));
|
2010-11-28 07:05:25 +08:00
|
|
|
|
|
|
|
bool ExtraCSSpill = false;
|
|
|
|
if (BigStack || !CanEliminateFrame || RegInfo->cannotEliminateFrame(MF)) {
|
|
|
|
AFI->setHasStackFrame(true);
|
|
|
|
|
|
|
|
// If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled.
|
|
|
|
// Spill LR as well so we can fold BX_RET to the registers restore (LDM).
|
|
|
|
if (!LRSpilled && CS1Spilled) {
|
2015-07-15 01:17:13 +08:00
|
|
|
SavedRegs.set(ARM::LR);
|
2010-11-28 07:05:25 +08:00
|
|
|
NumGPRSpills++;
|
2013-10-01 22:33:28 +08:00
|
|
|
SmallVectorImpl<unsigned>::iterator LRPos;
|
|
|
|
LRPos = std::find(UnspilledCS1GPRs.begin(), UnspilledCS1GPRs.end(),
|
|
|
|
(unsigned)ARM::LR);
|
|
|
|
if (LRPos != UnspilledCS1GPRs.end())
|
|
|
|
UnspilledCS1GPRs.erase(LRPos);
|
|
|
|
|
2010-11-28 07:05:25 +08:00
|
|
|
ForceLRSpill = false;
|
|
|
|
ExtraCSSpill = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasFP(MF)) {
|
2015-07-15 01:17:13 +08:00
|
|
|
SavedRegs.set(FramePtr);
|
2014-05-07 04:43:01 +08:00
|
|
|
auto FPPos = std::find(UnspilledCS1GPRs.begin(), UnspilledCS1GPRs.end(),
|
|
|
|
FramePtr);
|
|
|
|
if (FPPos != UnspilledCS1GPRs.end())
|
|
|
|
UnspilledCS1GPRs.erase(FPPos);
|
2010-11-28 07:05:25 +08:00
|
|
|
NumGPRSpills++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If stack and double are 8-byte aligned and we are spilling an odd number
|
|
|
|
// of GPRs, spill one extra callee save GPR so we won't have to pad between
|
|
|
|
// the integer and double callee save areas.
|
2011-01-10 20:39:04 +08:00
|
|
|
unsigned TargetAlign = getStackAlignment();
|
2014-11-05 08:27:20 +08:00
|
|
|
if (TargetAlign >= 8 && (NumGPRSpills & 1)) {
|
2010-11-28 07:05:25 +08:00
|
|
|
if (CS1Spilled && !UnspilledCS1GPRs.empty()) {
|
|
|
|
for (unsigned i = 0, e = UnspilledCS1GPRs.size(); i != e; ++i) {
|
|
|
|
unsigned Reg = UnspilledCS1GPRs[i];
|
2015-10-09 11:19:03 +08:00
|
|
|
// Don't spill high register if the function is thumb. In the case of
|
|
|
|
// Windows on ARM, accept R11 (frame pointer)
|
2015-04-24 04:31:26 +08:00
|
|
|
if (!AFI->isThumbFunction() ||
|
2015-10-09 11:19:03 +08:00
|
|
|
(STI.isTargetWindows() && Reg == ARM::R11) ||
|
2010-11-28 07:05:25 +08:00
|
|
|
isARMLowRegister(Reg) || Reg == ARM::LR) {
|
2015-07-15 01:17:13 +08:00
|
|
|
SavedRegs.set(Reg);
|
2012-10-27 05:43:05 +08:00
|
|
|
if (!MRI.isReserved(Reg))
|
2010-11-28 07:05:25 +08:00
|
|
|
ExtraCSSpill = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (!UnspilledCS2GPRs.empty() && !AFI->isThumb1OnlyFunction()) {
|
|
|
|
unsigned Reg = UnspilledCS2GPRs.front();
|
2015-07-15 01:17:13 +08:00
|
|
|
SavedRegs.set(Reg);
|
2012-10-27 05:43:05 +08:00
|
|
|
if (!MRI.isReserved(Reg))
|
2010-11-28 07:05:25 +08:00
|
|
|
ExtraCSSpill = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Estimate if we might need to scavenge a register at some point in order
|
|
|
|
// to materialize a stack offset. If so, either spill one additional
|
|
|
|
// callee-saved register or reserve a special spill slot to facilitate
|
|
|
|
// register scavenging. Thumb1 needs a spill slot for stack pointer
|
|
|
|
// adjustments also, even when the frame itself is small.
|
|
|
|
if (BigStack && !ExtraCSSpill) {
|
|
|
|
// If any non-reserved CS register isn't spilled, just spill one or two
|
|
|
|
// extra. That should take care of it!
|
|
|
|
unsigned NumExtras = TargetAlign / 4;
|
|
|
|
SmallVector<unsigned, 2> Extras;
|
|
|
|
while (NumExtras && !UnspilledCS1GPRs.empty()) {
|
|
|
|
unsigned Reg = UnspilledCS1GPRs.back();
|
|
|
|
UnspilledCS1GPRs.pop_back();
|
2012-10-27 05:43:05 +08:00
|
|
|
if (!MRI.isReserved(Reg) &&
|
2010-11-28 07:05:25 +08:00
|
|
|
(!AFI->isThumb1OnlyFunction() || isARMLowRegister(Reg) ||
|
|
|
|
Reg == ARM::LR)) {
|
|
|
|
Extras.push_back(Reg);
|
|
|
|
NumExtras--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// For non-Thumb1 functions, also check for hi-reg CS registers
|
|
|
|
if (!AFI->isThumb1OnlyFunction()) {
|
|
|
|
while (NumExtras && !UnspilledCS2GPRs.empty()) {
|
|
|
|
unsigned Reg = UnspilledCS2GPRs.back();
|
|
|
|
UnspilledCS2GPRs.pop_back();
|
2012-10-27 05:43:05 +08:00
|
|
|
if (!MRI.isReserved(Reg)) {
|
2010-11-28 07:05:25 +08:00
|
|
|
Extras.push_back(Reg);
|
|
|
|
NumExtras--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Extras.size() && NumExtras == 0) {
|
|
|
|
for (unsigned i = 0, e = Extras.size(); i != e; ++i) {
|
2015-07-15 01:17:13 +08:00
|
|
|
SavedRegs.set(Extras[i]);
|
2010-11-28 07:05:25 +08:00
|
|
|
}
|
|
|
|
} else if (!AFI->isThumb1OnlyFunction()) {
|
|
|
|
// note: Thumb1 functions spill to R12, not the stack. Reserve a slot
|
|
|
|
// closest to SP or frame pointer.
|
2012-04-20 15:30:17 +08:00
|
|
|
const TargetRegisterClass *RC = &ARM::GPRRegClass;
|
2013-03-23 07:32:27 +08:00
|
|
|
RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
|
2010-11-28 07:05:25 +08:00
|
|
|
RC->getAlignment(),
|
|
|
|
false));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ForceLRSpill) {
|
2015-07-15 01:17:13 +08:00
|
|
|
SavedRegs.set(ARM::LR);
|
2010-11-28 07:05:25 +08:00
|
|
|
AFI->setLRIsSpilledForFarJump(true);
|
|
|
|
}
|
|
|
|
}
|
2013-02-22 04:05:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
void ARMFrameLowering::
|
|
|
|
eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator I) const {
|
|
|
|
const ARMBaseInstrInfo &TII =
|
2014-08-05 10:39:49 +08:00
|
|
|
*static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
|
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.
|
2015-08-18 06:36:27 +08:00
|
|
|
Amount = alignSPAdjust(Amount);
|
2013-02-22 04:05:00 +08:00
|
|
|
|
|
|
|
ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
|
|
|
|
assert(!AFI->isThumb1OnlyFunction() &&
|
|
|
|
"This eliminateCallFramePseudoInstr does not support Thumb1!");
|
|
|
|
bool isARM = !AFI->isThumbFunction();
|
|
|
|
|
|
|
|
// Replace the pseudo instruction with a new instruction...
|
|
|
|
unsigned Opc = Old->getOpcode();
|
|
|
|
int PIdx = Old->findFirstPredOperandIdx();
|
|
|
|
ARMCC::CondCodes Pred = (PIdx == -1)
|
|
|
|
? ARMCC::AL : (ARMCC::CondCodes)Old->getOperand(PIdx).getImm();
|
|
|
|
if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) {
|
|
|
|
// Note: PredReg is operand 2 for ADJCALLSTACKDOWN.
|
|
|
|
unsigned PredReg = Old->getOperand(2).getReg();
|
|
|
|
emitSPUpdate(isARM, MBB, I, dl, TII, -Amount, MachineInstr::NoFlags,
|
|
|
|
Pred, PredReg);
|
|
|
|
} else {
|
|
|
|
// Note: PredReg is operand 3 for ADJCALLSTACKUP.
|
|
|
|
unsigned PredReg = Old->getOperand(3).getReg();
|
|
|
|
assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP);
|
|
|
|
emitSPUpdate(isARM, MBB, I, dl, TII, Amount, MachineInstr::NoFlags,
|
|
|
|
Pred, PredReg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MBB.erase(I);
|
|
|
|
}
|
|
|
|
|
2014-04-03 00:10:33 +08:00
|
|
|
/// Get the minimum constant for ARM that is greater than or equal to the
|
|
|
|
/// argument. In ARM, constants can have any value that can be produced by
|
|
|
|
/// rotating an 8-bit value to the right by an even number of bits within a
|
|
|
|
/// 32-bit word.
|
|
|
|
static uint32_t alignToARMConstant(uint32_t Value) {
|
|
|
|
unsigned Shifted = 0;
|
|
|
|
|
|
|
|
if (Value == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
while (!(Value & 0xC0000000)) {
|
|
|
|
Value = Value << 2;
|
|
|
|
Shifted += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Carry = (Value & 0x00FFFFFF);
|
|
|
|
Value = ((Value & 0xFF000000) >> 24) + Carry;
|
|
|
|
|
|
|
|
if (Value & 0x0000100)
|
|
|
|
Value = Value & 0x000001FC;
|
|
|
|
|
|
|
|
if (Shifted > 24)
|
|
|
|
Value = Value >> (Shifted - 24);
|
|
|
|
else
|
|
|
|
Value = Value << (24 - Shifted);
|
|
|
|
|
|
|
|
return Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The stack limit in the TCB is set to this many bytes above the actual
|
|
|
|
// stack limit.
|
|
|
|
static const uint64_t kSplitStackAvailable = 256;
|
|
|
|
|
|
|
|
// Adjust the function prologue to enable split stacks. This currently only
|
|
|
|
// supports android and linux.
|
|
|
|
//
|
|
|
|
// The ABI of the segmented stack prologue is a little arbitrarily chosen, but
|
|
|
|
// must be well defined in order to allow for consistent implementations of the
|
|
|
|
// __morestack helper function. The ABI is also not a normal ABI in that it
|
|
|
|
// doesn't follow the normal calling conventions because this allows the
|
|
|
|
// prologue of each function to be optimized further.
|
|
|
|
//
|
|
|
|
// Currently, the ABI looks like (when calling __morestack)
|
|
|
|
//
|
|
|
|
// * r4 holds the minimum stack size requested for this function call
|
|
|
|
// * r5 holds the stack size of the arguments to the function
|
|
|
|
// * the beginning of the function is 3 instructions after the call to
|
|
|
|
// __morestack
|
|
|
|
//
|
|
|
|
// Implementations of __morestack should use r4 to allocate a new stack, r5 to
|
|
|
|
// place the arguments on to the new stack, and the 3-instruction knowledge to
|
|
|
|
// jump directly to the body of the function when working on the new stack.
|
|
|
|
//
|
|
|
|
// An old (and possibly no longer compatible) implementation of __morestack for
|
|
|
|
// ARM can be found at [1].
|
|
|
|
//
|
|
|
|
// [1] - https://github.com/mozilla/rust/blob/86efd9/src/rt/arch/arm/morestack.S
|
[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 ARMFrameLowering::adjustForSegmentedStacks(
|
|
|
|
MachineFunction &MF, MachineBasicBlock &PrologueMBB) const {
|
2014-04-03 00:10:33 +08:00
|
|
|
unsigned Opcode;
|
|
|
|
unsigned CFIIndex;
|
2015-02-20 16:24:37 +08:00
|
|
|
const ARMSubtarget *ST = &MF.getSubtarget<ARMSubtarget>();
|
2014-04-03 00:10:33 +08:00
|
|
|
bool Thumb = ST->isThumb();
|
|
|
|
|
|
|
|
// Sadly, this currently doesn't support varargs, platforms other than
|
|
|
|
// android/linux. Note that thumb1/thumb2 are support for android/linux.
|
|
|
|
if (MF.getFunction()->isVarArg())
|
|
|
|
report_fatal_error("Segmented stacks do not support vararg functions.");
|
|
|
|
if (!ST->isTargetAndroid() && !ST->isTargetLinux())
|
2014-04-09 22:47:27 +08:00
|
|
|
report_fatal_error("Segmented stacks not supported on this platform.");
|
2014-04-03 00:10:33 +08:00
|
|
|
|
|
|
|
MachineFrameInfo *MFI = MF.getFrameInfo();
|
|
|
|
MachineModuleInfo &MMI = MF.getMMI();
|
|
|
|
MCContext &Context = MMI.getContext();
|
|
|
|
const MCRegisterInfo *MRI = Context.getRegisterInfo();
|
|
|
|
const ARMBaseInstrInfo &TII =
|
2014-08-05 10:39:49 +08:00
|
|
|
*static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
|
2014-04-03 00:10:33 +08:00
|
|
|
ARMFunctionInfo *ARMFI = MF.getInfo<ARMFunctionInfo>();
|
|
|
|
DebugLoc DL;
|
|
|
|
|
2014-05-22 21:03:43 +08:00
|
|
|
uint64_t StackSize = MFI->getStackSize();
|
|
|
|
|
|
|
|
// Do not generate a prologue for functions with a stack of size zero
|
|
|
|
if (StackSize == 0)
|
|
|
|
return;
|
|
|
|
|
2014-04-03 00:10:33 +08:00
|
|
|
// Use R4 and R5 as scratch registers.
|
|
|
|
// We save R4 and R5 before use and restore them before leaving the function.
|
|
|
|
unsigned ScratchReg0 = ARM::R4;
|
|
|
|
unsigned ScratchReg1 = ARM::R5;
|
|
|
|
uint64_t AlignedStackSize;
|
|
|
|
|
|
|
|
MachineBasicBlock *PrevStackMBB = MF.CreateMachineBasicBlock();
|
|
|
|
MachineBasicBlock *PostStackMBB = MF.CreateMachineBasicBlock();
|
|
|
|
MachineBasicBlock *AllocMBB = MF.CreateMachineBasicBlock();
|
|
|
|
MachineBasicBlock *GetMBB = MF.CreateMachineBasicBlock();
|
|
|
|
MachineBasicBlock *McrMBB = MF.CreateMachineBasicBlock();
|
|
|
|
|
2015-07-21 05:42:14 +08:00
|
|
|
// Grab everything that reaches PrologueMBB to update there liveness as well.
|
|
|
|
SmallPtrSet<MachineBasicBlock *, 8> BeforePrologueRegion;
|
|
|
|
SmallVector<MachineBasicBlock *, 2> WalkList;
|
|
|
|
WalkList.push_back(&PrologueMBB);
|
|
|
|
|
|
|
|
do {
|
|
|
|
MachineBasicBlock *CurMBB = WalkList.pop_back_val();
|
|
|
|
for (MachineBasicBlock *PredBB : CurMBB->predecessors()) {
|
|
|
|
if (BeforePrologueRegion.insert(PredBB).second)
|
|
|
|
WalkList.push_back(PredBB);
|
|
|
|
}
|
|
|
|
} while (!WalkList.empty());
|
|
|
|
|
|
|
|
// The order in that list is important.
|
|
|
|
// The blocks will all be inserted before PrologueMBB using that order.
|
|
|
|
// Therefore the block that should appear first in the CFG should appear
|
|
|
|
// first in the list.
|
|
|
|
MachineBasicBlock *AddedBlocks[] = {PrevStackMBB, McrMBB, GetMBB, AllocMBB,
|
|
|
|
PostStackMBB};
|
|
|
|
|
2015-12-01 14:13:01 +08:00
|
|
|
for (MachineBasicBlock *B : AddedBlocks)
|
|
|
|
BeforePrologueRegion.insert(B);
|
2015-07-21 05:42:14 +08:00
|
|
|
|
2015-09-10 02:08:03 +08:00
|
|
|
for (const auto &LI : PrologueMBB.liveins()) {
|
2015-07-21 05:42:14 +08:00
|
|
|
for (MachineBasicBlock *PredBB : BeforePrologueRegion)
|
2015-08-25 06:59:52 +08:00
|
|
|
PredBB->addLiveIn(LI);
|
2014-04-03 00:10:33 +08:00
|
|
|
}
|
|
|
|
|
2015-07-21 05:42:14 +08:00
|
|
|
// Remove the newly added blocks from the list, since we know
|
|
|
|
// we do not have to do the following updates for them.
|
2015-12-01 14:13:01 +08:00
|
|
|
for (MachineBasicBlock *B : AddedBlocks) {
|
|
|
|
BeforePrologueRegion.erase(B);
|
|
|
|
MF.insert(PrologueMBB.getIterator(), B);
|
2015-07-21 05:42:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (MachineBasicBlock *MBB : BeforePrologueRegion) {
|
|
|
|
// Make sure the LiveIns are still sorted and unique.
|
|
|
|
MBB->sortUniqueLiveIns();
|
|
|
|
// Replace the edges to PrologueMBB by edges to the sequences
|
|
|
|
// we are about to add.
|
|
|
|
MBB->ReplaceUsesOfBlockWith(&PrologueMBB, AddedBlocks[0]);
|
|
|
|
}
|
2014-04-03 00:10:33 +08:00
|
|
|
|
|
|
|
// The required stack size that is aligned to ARM constant criterion.
|
|
|
|
AlignedStackSize = alignToARMConstant(StackSize);
|
|
|
|
|
|
|
|
// When the frame size is less than 256 we just compare the stack
|
|
|
|
// boundary directly to the value of the stack pointer, per gcc.
|
|
|
|
bool CompareStackPointer = AlignedStackSize < kSplitStackAvailable;
|
|
|
|
|
|
|
|
// We will use two of the callee save registers as scratch registers so we
|
|
|
|
// need to save those registers onto the stack.
|
|
|
|
// We will use SR0 to hold stack limit and SR1 to hold the stack size
|
|
|
|
// requested and arguments for __morestack().
|
|
|
|
// SR0: Scratch Register #0
|
|
|
|
// SR1: Scratch Register #1
|
|
|
|
// push {SR0, SR1}
|
|
|
|
if (Thumb) {
|
|
|
|
AddDefaultPred(BuildMI(PrevStackMBB, DL, TII.get(ARM::tPUSH)))
|
|
|
|
.addReg(ScratchReg0).addReg(ScratchReg1);
|
|
|
|
} else {
|
|
|
|
AddDefaultPred(BuildMI(PrevStackMBB, DL, TII.get(ARM::STMDB_UPD))
|
|
|
|
.addReg(ARM::SP, RegState::Define).addReg(ARM::SP))
|
|
|
|
.addReg(ScratchReg0).addReg(ScratchReg1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit the relevant DWARF information about the change in stack pointer as
|
|
|
|
// well as where to find both r4 and r5 (the callee-save registers)
|
|
|
|
CFIIndex =
|
|
|
|
MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, -8));
|
|
|
|
BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
|
|
|
|
.addCFIIndex(CFIIndex);
|
|
|
|
CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
|
|
|
|
nullptr, MRI->getDwarfRegNum(ScratchReg1, true), -4));
|
|
|
|
BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
|
|
|
|
.addCFIIndex(CFIIndex);
|
|
|
|
CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
|
|
|
|
nullptr, MRI->getDwarfRegNum(ScratchReg0, true), -8));
|
|
|
|
BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
|
|
|
|
.addCFIIndex(CFIIndex);
|
|
|
|
|
|
|
|
// mov SR1, sp
|
|
|
|
if (Thumb) {
|
|
|
|
AddDefaultPred(BuildMI(McrMBB, DL, TII.get(ARM::tMOVr), ScratchReg1)
|
|
|
|
.addReg(ARM::SP));
|
|
|
|
} else if (CompareStackPointer) {
|
|
|
|
AddDefaultPred(BuildMI(McrMBB, DL, TII.get(ARM::MOVr), ScratchReg1)
|
|
|
|
.addReg(ARM::SP)).addReg(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// sub SR1, sp, #StackSize
|
|
|
|
if (!CompareStackPointer && Thumb) {
|
|
|
|
AddDefaultPred(
|
|
|
|
AddDefaultCC(BuildMI(McrMBB, DL, TII.get(ARM::tSUBi8), ScratchReg1))
|
|
|
|
.addReg(ScratchReg1).addImm(AlignedStackSize));
|
|
|
|
} else if (!CompareStackPointer) {
|
|
|
|
AddDefaultPred(BuildMI(McrMBB, DL, TII.get(ARM::SUBri), ScratchReg1)
|
|
|
|
.addReg(ARM::SP).addImm(AlignedStackSize)).addReg(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Thumb && ST->isThumb1Only()) {
|
|
|
|
unsigned PCLabelId = ARMFI->createPICLabelUId();
|
|
|
|
ARMConstantPoolValue *NewCPV = ARMConstantPoolSymbol::Create(
|
2014-04-03 16:45:16 +08:00
|
|
|
MF.getFunction()->getContext(), "__STACK_LIMIT", PCLabelId, 0);
|
2014-04-03 00:10:33 +08:00
|
|
|
MachineConstantPool *MCP = MF.getConstantPool();
|
2015-10-03 02:07:13 +08:00
|
|
|
unsigned CPI = MCP->getConstantPoolIndex(NewCPV, 4);
|
2014-04-03 00:10:33 +08:00
|
|
|
|
|
|
|
// ldr SR0, [pc, offset(STACK_LIMIT)]
|
|
|
|
AddDefaultPred(BuildMI(GetMBB, DL, TII.get(ARM::tLDRpci), ScratchReg0)
|
|
|
|
.addConstantPoolIndex(CPI));
|
|
|
|
|
|
|
|
// ldr SR0, [SR0]
|
|
|
|
AddDefaultPred(BuildMI(GetMBB, DL, TII.get(ARM::tLDRi), ScratchReg0)
|
|
|
|
.addReg(ScratchReg0).addImm(0));
|
|
|
|
} else {
|
|
|
|
// Get TLS base address from the coprocessor
|
|
|
|
// mrc p15, #0, SR0, c13, c0, #3
|
|
|
|
AddDefaultPred(BuildMI(McrMBB, DL, TII.get(ARM::MRC), ScratchReg0)
|
|
|
|
.addImm(15)
|
|
|
|
.addImm(0)
|
|
|
|
.addImm(13)
|
|
|
|
.addImm(0)
|
|
|
|
.addImm(3));
|
|
|
|
|
|
|
|
// Use the last tls slot on android and a private field of the TCP on linux.
|
|
|
|
assert(ST->isTargetAndroid() || ST->isTargetLinux());
|
|
|
|
unsigned TlsOffset = ST->isTargetAndroid() ? 63 : 1;
|
|
|
|
|
|
|
|
// Get the stack limit from the right offset
|
|
|
|
// ldr SR0, [sr0, #4 * TlsOffset]
|
|
|
|
AddDefaultPred(BuildMI(GetMBB, DL, TII.get(ARM::LDRi12), ScratchReg0)
|
|
|
|
.addReg(ScratchReg0).addImm(4 * TlsOffset));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare stack limit with stack size requested.
|
|
|
|
// cmp SR0, SR1
|
|
|
|
Opcode = Thumb ? ARM::tCMPr : ARM::CMPrr;
|
|
|
|
AddDefaultPred(BuildMI(GetMBB, DL, TII.get(Opcode))
|
|
|
|
.addReg(ScratchReg0)
|
|
|
|
.addReg(ScratchReg1));
|
|
|
|
|
|
|
|
// This jump is taken if StackLimit < SP - stack required.
|
|
|
|
Opcode = Thumb ? ARM::tBcc : ARM::Bcc;
|
|
|
|
BuildMI(GetMBB, DL, TII.get(Opcode)).addMBB(PostStackMBB)
|
|
|
|
.addImm(ARMCC::LO)
|
|
|
|
.addReg(ARM::CPSR);
|
|
|
|
|
|
|
|
|
|
|
|
// Calling __morestack(StackSize, Size of stack arguments).
|
|
|
|
// __morestack knows that the stack size requested is in SR0(r4)
|
|
|
|
// and amount size of stack arguments is in SR1(r5).
|
|
|
|
|
|
|
|
// Pass first argument for the __morestack by Scratch Register #0.
|
|
|
|
// The amount size of stack required
|
|
|
|
if (Thumb) {
|
|
|
|
AddDefaultPred(AddDefaultCC(BuildMI(AllocMBB, DL, TII.get(ARM::tMOVi8),
|
|
|
|
ScratchReg0)).addImm(AlignedStackSize));
|
|
|
|
} else {
|
|
|
|
AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::MOVi), ScratchReg0)
|
|
|
|
.addImm(AlignedStackSize)).addReg(0);
|
|
|
|
}
|
|
|
|
// Pass second argument for the __morestack by Scratch Register #1.
|
|
|
|
// The amount size of stack consumed to save function arguments.
|
|
|
|
if (Thumb) {
|
|
|
|
AddDefaultPred(
|
|
|
|
AddDefaultCC(BuildMI(AllocMBB, DL, TII.get(ARM::tMOVi8), ScratchReg1))
|
|
|
|
.addImm(alignToARMConstant(ARMFI->getArgumentStackSize())));
|
|
|
|
} else {
|
|
|
|
AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::MOVi), ScratchReg1)
|
|
|
|
.addImm(alignToARMConstant(ARMFI->getArgumentStackSize())))
|
|
|
|
.addReg(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// push {lr} - Save return address of this function.
|
|
|
|
if (Thumb) {
|
|
|
|
AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tPUSH)))
|
|
|
|
.addReg(ARM::LR);
|
|
|
|
} else {
|
|
|
|
AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::STMDB_UPD))
|
|
|
|
.addReg(ARM::SP, RegState::Define)
|
|
|
|
.addReg(ARM::SP))
|
|
|
|
.addReg(ARM::LR);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit the DWARF info about the change in stack as well as where to find the
|
|
|
|
// previous link register
|
|
|
|
CFIIndex =
|
|
|
|
MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, -12));
|
|
|
|
BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
|
|
|
|
.addCFIIndex(CFIIndex);
|
|
|
|
CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
|
|
|
|
nullptr, MRI->getDwarfRegNum(ARM::LR, true), -12));
|
|
|
|
BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
|
|
|
|
.addCFIIndex(CFIIndex);
|
|
|
|
|
|
|
|
// Call __morestack().
|
|
|
|
if (Thumb) {
|
|
|
|
AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tBL)))
|
|
|
|
.addExternalSymbol("__morestack");
|
|
|
|
} else {
|
|
|
|
BuildMI(AllocMBB, DL, TII.get(ARM::BL))
|
|
|
|
.addExternalSymbol("__morestack");
|
|
|
|
}
|
|
|
|
|
|
|
|
// pop {lr} - Restore return address of this original function.
|
|
|
|
if (Thumb) {
|
|
|
|
if (ST->isThumb1Only()) {
|
|
|
|
AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tPOP)))
|
|
|
|
.addReg(ScratchReg0);
|
|
|
|
AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tMOVr), ARM::LR)
|
|
|
|
.addReg(ScratchReg0));
|
|
|
|
} else {
|
|
|
|
AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::t2LDR_POST))
|
|
|
|
.addReg(ARM::LR, RegState::Define)
|
|
|
|
.addReg(ARM::SP, RegState::Define)
|
|
|
|
.addReg(ARM::SP)
|
|
|
|
.addImm(4));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::LDMIA_UPD))
|
|
|
|
.addReg(ARM::SP, RegState::Define)
|
|
|
|
.addReg(ARM::SP))
|
|
|
|
.addReg(ARM::LR);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore SR0 and SR1 in case of __morestack() was called.
|
|
|
|
// __morestack() will skip PostStackMBB block so we need to restore
|
|
|
|
// scratch registers from here.
|
|
|
|
// pop {SR0, SR1}
|
|
|
|
if (Thumb) {
|
|
|
|
AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tPOP)))
|
|
|
|
.addReg(ScratchReg0)
|
|
|
|
.addReg(ScratchReg1);
|
|
|
|
} else {
|
|
|
|
AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::LDMIA_UPD))
|
|
|
|
.addReg(ARM::SP, RegState::Define)
|
|
|
|
.addReg(ARM::SP))
|
|
|
|
.addReg(ScratchReg0)
|
|
|
|
.addReg(ScratchReg1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the CFA offset now that we've popped
|
|
|
|
CFIIndex = MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, 0));
|
|
|
|
BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
|
|
|
|
.addCFIIndex(CFIIndex);
|
|
|
|
|
|
|
|
// bx lr - Return from this function.
|
|
|
|
Opcode = Thumb ? ARM::tBX_RET : ARM::BX_RET;
|
|
|
|
AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(Opcode)));
|
|
|
|
|
|
|
|
// Restore SR0 and SR1 in case of __morestack() was not called.
|
|
|
|
// pop {SR0, SR1}
|
|
|
|
if (Thumb) {
|
|
|
|
AddDefaultPred(BuildMI(PostStackMBB, DL, TII.get(ARM::tPOP)))
|
|
|
|
.addReg(ScratchReg0)
|
|
|
|
.addReg(ScratchReg1);
|
|
|
|
} else {
|
|
|
|
AddDefaultPred(BuildMI(PostStackMBB, DL, TII.get(ARM::LDMIA_UPD))
|
|
|
|
.addReg(ARM::SP, RegState::Define)
|
|
|
|
.addReg(ARM::SP))
|
|
|
|
.addReg(ScratchReg0)
|
|
|
|
.addReg(ScratchReg1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the CFA offset now that we've popped
|
|
|
|
CFIIndex = MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, 0));
|
|
|
|
BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
|
|
|
|
.addCFIIndex(CFIIndex);
|
|
|
|
|
|
|
|
// Tell debuggers that r4 and r5 are now the same as they were in the
|
|
|
|
// previous function, that they're the "Same Value".
|
|
|
|
CFIIndex = MMI.addFrameInst(MCCFIInstruction::createSameValue(
|
|
|
|
nullptr, MRI->getDwarfRegNum(ScratchReg0, true)));
|
|
|
|
BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
|
|
|
|
.addCFIIndex(CFIIndex);
|
|
|
|
CFIIndex = MMI.addFrameInst(MCCFIInstruction::createSameValue(
|
|
|
|
nullptr, MRI->getDwarfRegNum(ScratchReg1, true)));
|
|
|
|
BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
|
|
|
|
.addCFIIndex(CFIIndex);
|
|
|
|
|
|
|
|
// Organizing MBB lists
|
[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
|
|
|
PostStackMBB->addSuccessor(&PrologueMBB);
|
2014-04-03 00:10:33 +08:00
|
|
|
|
|
|
|
AllocMBB->addSuccessor(PostStackMBB);
|
|
|
|
|
|
|
|
GetMBB->addSuccessor(PostStackMBB);
|
|
|
|
GetMBB->addSuccessor(AllocMBB);
|
|
|
|
|
|
|
|
McrMBB->addSuccessor(GetMBB);
|
|
|
|
|
|
|
|
PrevStackMBB->addSuccessor(McrMBB);
|
|
|
|
|
|
|
|
#ifdef XDEBUG
|
|
|
|
MF.verify();
|
|
|
|
#endif
|
|
|
|
}
|