2014-05-24 20:50:23 +08:00
|
|
|
//===- AArch64FrameLowering.cpp - AArch64 Frame Lowering -------*- C++ -*-====//
|
2014-03-29 18:18:08 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2014-05-24 20:50:23 +08:00
|
|
|
// This file contains the AArch64 implementation of TargetFrameLowering class.
|
2014-03-29 18:18:08 +08:00
|
|
|
//
|
2015-04-09 16:49:47 +08:00
|
|
|
// On AArch64, stack frames are structured as follows:
|
|
|
|
//
|
|
|
|
// The stack grows downward.
|
|
|
|
//
|
|
|
|
// All of the individual frame areas on the frame below are optional, i.e. it's
|
|
|
|
// possible to create a function so that the particular area isn't present
|
|
|
|
// in the frame.
|
|
|
|
//
|
|
|
|
// At function entry, the "frame" looks as follows:
|
|
|
|
//
|
|
|
|
// | | Higher address
|
|
|
|
// |-----------------------------------|
|
|
|
|
// | |
|
|
|
|
// | arguments passed on the stack |
|
|
|
|
// | |
|
|
|
|
// |-----------------------------------| <- sp
|
|
|
|
// | | Lower address
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// After the prologue has run, the frame has the following general structure.
|
|
|
|
// Note that this doesn't depict the case where a red-zone is used. Also,
|
|
|
|
// technically the last frame area (VLAs) doesn't get created until in the
|
|
|
|
// main function body, after the prologue is run. However, it's depicted here
|
|
|
|
// for completeness.
|
|
|
|
//
|
|
|
|
// | | Higher address
|
|
|
|
// |-----------------------------------|
|
|
|
|
// | |
|
|
|
|
// | arguments passed on the stack |
|
|
|
|
// | |
|
|
|
|
// |-----------------------------------|
|
|
|
|
// | |
|
|
|
|
// | prev_fp, prev_lr |
|
|
|
|
// | (a.k.a. "frame record") |
|
|
|
|
// |-----------------------------------| <- fp(=x29)
|
|
|
|
// | |
|
|
|
|
// | other callee-saved registers |
|
|
|
|
// | |
|
|
|
|
// |-----------------------------------|
|
|
|
|
// |.empty.space.to.make.part.below....|
|
|
|
|
// |.aligned.in.case.it.needs.more.than| (size of this area is unknown at
|
|
|
|
// |.the.standard.16-byte.alignment....| compile time; if present)
|
|
|
|
// |-----------------------------------|
|
|
|
|
// | |
|
|
|
|
// | local variables of fixed size |
|
|
|
|
// | including spill slots |
|
|
|
|
// |-----------------------------------| <- bp(not defined by ABI,
|
|
|
|
// |.variable-sized.local.variables....| LLVM chooses X19)
|
|
|
|
// |.(VLAs)............................| (size of this area is unknown at
|
|
|
|
// |...................................| compile time)
|
|
|
|
// |-----------------------------------| <- sp
|
|
|
|
// | | Lower address
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// To access the data in a frame, at-compile time, a constant offset must be
|
|
|
|
// computable from one of the pointers (fp, bp, sp) to access it. The size
|
|
|
|
// of the areas with a dotted background cannot be computed at compile-time
|
|
|
|
// if they are present, making it required to have all three of fp, bp and
|
|
|
|
// sp to be set up to be able to access all contents in the frame areas,
|
|
|
|
// assuming all of the frame areas are non-empty.
|
|
|
|
//
|
|
|
|
// For most functions, some of the frame areas are empty. For those functions,
|
|
|
|
// it may not be necessary to set up fp or bp:
|
2015-08-09 02:27:36 +08:00
|
|
|
// * A base pointer is definitely needed when there are both VLAs and local
|
2015-04-09 16:49:47 +08:00
|
|
|
// variables with more-than-default alignment requirements.
|
2015-08-09 02:27:36 +08:00
|
|
|
// * A frame pointer is definitely needed when there are local variables with
|
2015-04-09 16:49:47 +08:00
|
|
|
// more-than-default alignment requirements.
|
|
|
|
//
|
|
|
|
// In some cases when a base pointer is not strictly needed, it is generated
|
|
|
|
// anyway when offsets from the frame pointer to access local variables become
|
|
|
|
// so large that the offset can't be encoded in the immediate fields of loads
|
|
|
|
// or stores.
|
|
|
|
//
|
|
|
|
// FIXME: also explain the redzone concept.
|
|
|
|
// FIXME: also explain the concept of reserved call frames.
|
|
|
|
//
|
2014-03-29 18:18:08 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-05-24 20:50:23 +08:00
|
|
|
#include "AArch64FrameLowering.h"
|
|
|
|
#include "AArch64InstrInfo.h"
|
|
|
|
#include "AArch64MachineFunctionInfo.h"
|
|
|
|
#include "AArch64Subtarget.h"
|
|
|
|
#include "AArch64TargetMachine.h"
|
2014-03-29 18:18:08 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/RegisterScavenging.h"
|
2014-07-25 19:42:14 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
2014-03-29 18:18:08 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2014-07-25 19:42:14 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2014-03-29 18:18:08 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:41:26 +08:00
|
|
|
#define DEBUG_TYPE "frame-info"
|
|
|
|
|
2014-05-24 20:50:23 +08:00
|
|
|
static cl::opt<bool> EnableRedZone("aarch64-redzone",
|
|
|
|
cl::desc("enable use of redzone on AArch64"),
|
2014-03-29 18:18:08 +08:00
|
|
|
cl::init(false), cl::Hidden);
|
|
|
|
|
|
|
|
STATISTIC(NumRedZoneFunctions, "Number of functions using red zone");
|
|
|
|
|
2014-05-24 20:50:23 +08:00
|
|
|
bool AArch64FrameLowering::canUseRedZone(const MachineFunction &MF) const {
|
2014-03-29 18:18:08 +08:00
|
|
|
if (!EnableRedZone)
|
|
|
|
return false;
|
|
|
|
// Don't use the red zone if the function explicitly asks us not to.
|
|
|
|
// This is typically used for kernel code.
|
2015-02-14 10:09:06 +08:00
|
|
|
if (MF.getFunction()->hasFnAttribute(Attribute::NoRedZone))
|
2014-03-29 18:18:08 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
const MachineFrameInfo *MFI = MF.getFrameInfo();
|
2014-05-24 20:50:23 +08:00
|
|
|
const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
|
2014-03-29 18:18:08 +08:00
|
|
|
unsigned NumBytes = AFI->getLocalStackSize();
|
|
|
|
|
2016-03-01 06:50:49 +08:00
|
|
|
return !(MFI->hasCalls() || hasFP(MF) || NumBytes > 128);
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// hasFP - Return true if the specified function should have a dedicated frame
|
|
|
|
/// pointer register.
|
2014-05-24 20:50:23 +08:00
|
|
|
bool AArch64FrameLowering::hasFP(const MachineFunction &MF) const {
|
2014-03-29 18:18:08 +08:00
|
|
|
const MachineFrameInfo *MFI = MF.getFrameInfo();
|
2014-08-05 10:39:49 +08:00
|
|
|
const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
|
2016-03-03 01:58:31 +08:00
|
|
|
// Retain behavior of always omitting the FP for leaf functions when possible.
|
|
|
|
return (MFI->hasCalls() &&
|
|
|
|
MF.getTarget().Options.DisableFramePointerElim(MF)) ||
|
|
|
|
MFI->hasVarSizedObjects() || MFI->isFrameAddressTaken() ||
|
|
|
|
MFI->hasStackMap() || MFI->hasPatchPoint() ||
|
|
|
|
RegInfo->needsStackRealignment(MF);
|
2014-03-29 18:18:08 +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.
|
2014-05-24 20:50:23 +08:00
|
|
|
bool
|
|
|
|
AArch64FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
|
2014-03-29 18:18:08 +08:00
|
|
|
return !MF.getFrameInfo()->hasVarSizedObjects();
|
|
|
|
}
|
|
|
|
|
2016-04-01 02:33:38 +08:00
|
|
|
MachineBasicBlock::iterator AArch64FrameLowering::eliminateCallFramePseudoInstr(
|
2014-03-29 18:18:08 +08:00
|
|
|
MachineFunction &MF, MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator I) const {
|
2014-08-05 10:39:49 +08:00
|
|
|
const AArch64InstrInfo *TII =
|
|
|
|
static_cast<const AArch64InstrInfo *>(MF.getSubtarget().getInstrInfo());
|
2014-05-15 09:33:17 +08:00
|
|
|
DebugLoc DL = I->getDebugLoc();
|
2015-05-19 04:27:55 +08:00
|
|
|
unsigned Opc = I->getOpcode();
|
2014-05-15 09:33:17 +08:00
|
|
|
bool IsDestroy = Opc == TII->getCallFrameDestroyOpcode();
|
|
|
|
uint64_t CalleePopAmount = IsDestroy ? I->getOperand(1).getImm() : 0;
|
|
|
|
|
2014-08-05 10:39:49 +08:00
|
|
|
const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
|
2014-03-29 18:18:08 +08:00
|
|
|
if (!TFI->hasReservedCallFrame(MF)) {
|
2014-05-15 09:33:17 +08:00
|
|
|
unsigned Align = getStackAlignment();
|
|
|
|
|
|
|
|
int64_t Amount = I->getOperand(0).getImm();
|
2016-01-15 05:06:47 +08:00
|
|
|
Amount = alignTo(Amount, Align);
|
2014-05-15 09:33:17 +08:00
|
|
|
if (!IsDestroy)
|
|
|
|
Amount = -Amount;
|
|
|
|
|
|
|
|
// N.b. if CalleePopAmount is valid but zero (i.e. callee would pop, but it
|
|
|
|
// doesn't have to pop anything), then the first operand will be zero too so
|
|
|
|
// this adjustment is a no-op.
|
|
|
|
if (CalleePopAmount == 0) {
|
|
|
|
// FIXME: in-function stack adjustment for calls is limited to 24-bits
|
|
|
|
// because there's no guaranteed temporary register available.
|
|
|
|
//
|
2014-08-12 02:04:46 +08:00
|
|
|
// ADD/SUB (immediate) has only LSL #0 and LSL #12 available.
|
2014-05-15 09:33:17 +08:00
|
|
|
// 1) For offset <= 12-bit, we use LSL #0
|
|
|
|
// 2) For 12-bit <= offset <= 24-bit, we use two instructions. One uses
|
|
|
|
// LSL #0, and the other uses LSL #12.
|
|
|
|
//
|
2016-01-20 00:50:45 +08:00
|
|
|
// Most call frames will be allocated at the start of a function so
|
2014-05-15 09:33:17 +08:00
|
|
|
// this is OK, but it is a limitation that needs dealing with.
|
|
|
|
assert(Amount > -0xffffff && Amount < 0xffffff && "call frame too large");
|
2014-05-24 20:50:23 +08:00
|
|
|
emitFrameOffset(MBB, I, DL, AArch64::SP, AArch64::SP, Amount, TII);
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
2014-05-15 09:33:17 +08:00
|
|
|
} else if (CalleePopAmount != 0) {
|
|
|
|
// If the calling convention demands that the callee pops arguments from the
|
|
|
|
// stack, we want to add it back if we have a reserved call frame.
|
|
|
|
assert(CalleePopAmount < 0xffffff && "call frame too large");
|
2014-05-24 20:50:23 +08:00
|
|
|
emitFrameOffset(MBB, I, DL, AArch64::SP, AArch64::SP, -CalleePopAmount,
|
|
|
|
TII);
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
2016-04-01 02:33:38 +08:00
|
|
|
return MBB.erase(I);
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
|
|
|
|
2014-05-24 20:50:23 +08:00
|
|
|
void AArch64FrameLowering::emitCalleeSavedFrameMoves(
|
2016-02-26 00:36:08 +08:00
|
|
|
MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI) const {
|
2014-03-29 18:18:08 +08:00
|
|
|
MachineFunction &MF = *MBB.getParent();
|
|
|
|
MachineFrameInfo *MFI = MF.getFrameInfo();
|
|
|
|
MachineModuleInfo &MMI = MF.getMMI();
|
|
|
|
const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
|
2014-08-05 10:39:49 +08:00
|
|
|
const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
|
2014-03-29 18:18:08 +08:00
|
|
|
DebugLoc DL = MBB.findDebugLoc(MBBI);
|
|
|
|
|
|
|
|
// Add callee saved registers to move list.
|
|
|
|
const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
|
|
|
|
if (CSI.empty())
|
|
|
|
return;
|
|
|
|
|
2014-04-03 02:00:49 +08:00
|
|
|
for (const auto &Info : CSI) {
|
|
|
|
unsigned Reg = Info.getReg();
|
2016-02-26 00:36:08 +08:00
|
|
|
int64_t Offset =
|
|
|
|
MFI->getObjectOffset(Info.getFrameIdx()) - getOffsetOfLocalArea();
|
2014-03-29 18:18:08 +08:00
|
|
|
unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
|
2016-02-26 00:36:08 +08:00
|
|
|
unsigned CFIIndex = MMI.addFrameInst(
|
|
|
|
MCCFIInstruction::createOffset(nullptr, DwarfReg, Offset));
|
2014-03-29 18:18:08 +08:00
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
|
2014-12-16 08:20:49 +08:00
|
|
|
.addCFIIndex(CFIIndex)
|
|
|
|
.setMIFlags(MachineInstr::FrameSetup);
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-20 02:27:32 +08:00
|
|
|
// Find a scratch register that we can use at the start of the prologue to
|
|
|
|
// re-align the stack pointer. We avoid using callee-save registers since they
|
|
|
|
// may appear to be free when this is called from canUseAsPrologue (during
|
|
|
|
// shrink wrapping), but then no longer be free when this is called from
|
|
|
|
// emitPrologue.
|
|
|
|
//
|
|
|
|
// FIXME: This is a bit conservative, since in the above case we could use one
|
|
|
|
// of the callee-save registers as a scratch temp to re-align the stack pointer,
|
|
|
|
// but we would then have to make sure that we were in fact saving at least one
|
|
|
|
// callee-save register in the prologue, which is additional complexity that
|
|
|
|
// doesn't seem worth the benefit.
|
|
|
|
static unsigned findScratchNonCalleeSaveRegister(MachineBasicBlock *MBB) {
|
|
|
|
MachineFunction *MF = MBB->getParent();
|
|
|
|
|
|
|
|
// If MBB is an entry block, use X9 as the scratch register
|
|
|
|
if (&MF->front() == MBB)
|
|
|
|
return AArch64::X9;
|
|
|
|
|
|
|
|
RegScavenger RS;
|
|
|
|
RS.enterBasicBlock(MBB);
|
|
|
|
|
|
|
|
// Prefer X9 since it was historically used for the prologue scratch reg.
|
|
|
|
if (!RS.isRegUsed(AArch64::X9))
|
|
|
|
return AArch64::X9;
|
|
|
|
|
|
|
|
// Find a free non callee-save reg.
|
|
|
|
const AArch64Subtarget &Subtarget = MF->getSubtarget<AArch64Subtarget>();
|
|
|
|
const AArch64RegisterInfo *RegInfo = Subtarget.getRegisterInfo();
|
|
|
|
const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(MF);
|
|
|
|
BitVector CalleeSaveRegs(RegInfo->getNumRegs());
|
|
|
|
for (unsigned i = 0; CSRegs[i]; ++i)
|
|
|
|
CalleeSaveRegs.set(CSRegs[i]);
|
|
|
|
|
|
|
|
BitVector Available = RS.getRegsAvailable(&AArch64::GPR64RegClass);
|
|
|
|
for (int AvailReg = Available.find_first(); AvailReg != -1;
|
|
|
|
AvailReg = Available.find_next(AvailReg))
|
|
|
|
if (!CalleeSaveRegs.test(AvailReg))
|
|
|
|
return AvailReg;
|
|
|
|
|
|
|
|
return AArch64::NoRegister;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AArch64FrameLowering::canUseAsPrologue(
|
|
|
|
const MachineBasicBlock &MBB) const {
|
|
|
|
const MachineFunction *MF = MBB.getParent();
|
|
|
|
MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB);
|
|
|
|
const AArch64Subtarget &Subtarget = MF->getSubtarget<AArch64Subtarget>();
|
|
|
|
const AArch64RegisterInfo *RegInfo = Subtarget.getRegisterInfo();
|
|
|
|
|
|
|
|
// Don't need a scratch register if we're not going to re-align the stack.
|
|
|
|
if (!RegInfo->needsStackRealignment(*MF))
|
|
|
|
return true;
|
|
|
|
// Otherwise, we can use any block as long as it has a scratch register
|
|
|
|
// available.
|
|
|
|
return findScratchNonCalleeSaveRegister(TmpMBB) != AArch64::NoRegister;
|
|
|
|
}
|
|
|
|
|
[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 AArch64FrameLowering::emitPrologue(MachineFunction &MF,
|
|
|
|
MachineBasicBlock &MBB) const {
|
2014-03-29 18:18:08 +08:00
|
|
|
MachineBasicBlock::iterator MBBI = MBB.begin();
|
|
|
|
const MachineFrameInfo *MFI = MF.getFrameInfo();
|
|
|
|
const Function *Fn = MF.getFunction();
|
2015-12-17 06:54:06 +08:00
|
|
|
const AArch64Subtarget &Subtarget = MF.getSubtarget<AArch64Subtarget>();
|
|
|
|
const AArch64RegisterInfo *RegInfo = Subtarget.getRegisterInfo();
|
|
|
|
const TargetInstrInfo *TII = Subtarget.getInstrInfo();
|
2014-03-29 18:18:08 +08:00
|
|
|
MachineModuleInfo &MMI = MF.getMMI();
|
2015-11-06 05:54:58 +08:00
|
|
|
AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
|
|
|
|
bool needsFrameMoves = MMI.hasDebugInfo() || Fn->needsUnwindTableEntry();
|
|
|
|
bool HasFP = hasFP(MF);
|
|
|
|
|
|
|
|
// Debug location must be unknown since the first debug location is used
|
|
|
|
// to determine the end of the prologue.
|
|
|
|
DebugLoc DL;
|
|
|
|
|
|
|
|
// All calls are tail calls in GHC calling conv, and functions have no
|
|
|
|
// prologue/epilogue.
|
2015-01-20 01:40:05 +08:00
|
|
|
if (MF.getFunction()->getCallingConv() == CallingConv::GHC)
|
|
|
|
return;
|
|
|
|
|
2014-03-29 18:18:08 +08:00
|
|
|
int NumBytes = (int)MFI->getStackSize();
|
|
|
|
if (!AFI->hasStackFrame()) {
|
|
|
|
assert(!HasFP && "unexpected function without stack frame but with FP");
|
|
|
|
|
|
|
|
// All of the stack allocation is for locals.
|
|
|
|
AFI->setLocalStackSize(NumBytes);
|
|
|
|
|
2016-03-15 02:24:34 +08:00
|
|
|
if (!NumBytes)
|
|
|
|
return;
|
2014-03-29 18:18:08 +08:00
|
|
|
// REDZONE: If the stack size is less than 128 bytes, we don't need
|
|
|
|
// to actually allocate.
|
2016-03-15 02:24:34 +08:00
|
|
|
if (canUseRedZone(MF))
|
|
|
|
++NumRedZoneFunctions;
|
|
|
|
else {
|
2014-05-24 20:50:23 +08:00
|
|
|
emitFrameOffset(MBB, MBBI, DL, AArch64::SP, AArch64::SP, -NumBytes, TII,
|
2014-03-29 18:18:08 +08:00
|
|
|
MachineInstr::FrameSetup);
|
|
|
|
|
2016-03-15 02:24:34 +08:00
|
|
|
// Label used to tie together the PROLOG_LABEL and the MachineMoves.
|
|
|
|
MCSymbol *FrameLabel = MMI.getContext().createTempSymbol();
|
2014-03-29 18:18:08 +08:00
|
|
|
// Encode the stack size of the leaf function.
|
|
|
|
unsigned CFIIndex = MMI.addFrameInst(
|
|
|
|
MCCFIInstruction::createDefCfaOffset(FrameLabel, -NumBytes));
|
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
|
2014-12-16 08:20:49 +08:00
|
|
|
.addCFIIndex(CFIIndex)
|
|
|
|
.setMIFlags(MachineInstr::FrameSetup);
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-15 02:24:34 +08:00
|
|
|
NumBytes -= AFI->getCalleeSavedStackSize();
|
|
|
|
assert(NumBytes >= 0 && "Negative stack allocation size!?");
|
|
|
|
// All of the remaining stack allocations are for locals.
|
|
|
|
AFI->setLocalStackSize(NumBytes);
|
2014-03-29 18:18:08 +08:00
|
|
|
|
|
|
|
// Move past the saves of the callee-saved registers.
|
2016-02-02 00:29:19 +08:00
|
|
|
MachineBasicBlock::iterator End = MBB.end();
|
|
|
|
while (MBBI != End && MBBI->getFlag(MachineInstr::FrameSetup))
|
2014-03-29 18:18:08 +08:00
|
|
|
++MBBI;
|
|
|
|
if (HasFP) {
|
2016-03-15 02:24:34 +08:00
|
|
|
// Only set up FP if we actually need to. Frame pointer is fp = sp - 16.
|
|
|
|
int FPOffset = AFI->getCalleeSavedStackSize() - 16;
|
|
|
|
|
2014-03-29 18:18:08 +08:00
|
|
|
// Issue sub fp, sp, FPOffset or
|
|
|
|
// mov fp,sp when FPOffset is zero.
|
|
|
|
// Note: All stores of callee-saved registers are marked as "FrameSetup".
|
|
|
|
// This code marks the instruction(s) that set the FP also.
|
2014-05-24 20:50:23 +08:00
|
|
|
emitFrameOffset(MBB, MBBI, DL, AArch64::FP, AArch64::SP, FPOffset, TII,
|
2014-03-29 18:18:08 +08:00
|
|
|
MachineInstr::FrameSetup);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate space for the rest of the frame.
|
2016-03-15 02:24:34 +08:00
|
|
|
if (NumBytes) {
|
|
|
|
const bool NeedsRealignment = RegInfo->needsStackRealignment(MF);
|
|
|
|
unsigned scratchSPReg = AArch64::SP;
|
2015-04-09 16:49:47 +08:00
|
|
|
|
2016-03-15 02:24:34 +08:00
|
|
|
if (NeedsRealignment) {
|
|
|
|
scratchSPReg = findScratchNonCalleeSaveRegister(&MBB);
|
|
|
|
assert(scratchSPReg != AArch64::NoRegister);
|
|
|
|
}
|
2015-04-09 16:49:47 +08:00
|
|
|
|
2016-03-15 02:24:34 +08:00
|
|
|
// If we're a leaf function, try using the red zone.
|
|
|
|
if (!canUseRedZone(MF))
|
|
|
|
// FIXME: in the case of dynamic re-alignment, NumBytes doesn't have
|
|
|
|
// the correct value here, as NumBytes also includes padding bytes,
|
|
|
|
// which shouldn't be counted here.
|
|
|
|
emitFrameOffset(MBB, MBBI, DL, scratchSPReg, AArch64::SP, -NumBytes, TII,
|
|
|
|
MachineInstr::FrameSetup);
|
2015-04-09 16:49:47 +08:00
|
|
|
|
2016-03-15 02:24:34 +08:00
|
|
|
if (NeedsRealignment) {
|
|
|
|
const unsigned Alignment = MFI->getMaxAlignment();
|
|
|
|
const unsigned NrBitsToZero = countTrailingZeros(Alignment);
|
|
|
|
assert(NrBitsToZero > 1);
|
|
|
|
assert(scratchSPReg != AArch64::SP);
|
|
|
|
|
|
|
|
// SUB X9, SP, NumBytes
|
|
|
|
// -- X9 is temporary register, so shouldn't contain any live data here,
|
|
|
|
// -- free to use. This is already produced by emitFrameOffset above.
|
|
|
|
// AND SP, X9, 0b11111...0000
|
|
|
|
// The logical immediates have a non-trivial encoding. The following
|
|
|
|
// formula computes the encoded immediate with all ones but
|
|
|
|
// NrBitsToZero zero bits as least significant bits.
|
|
|
|
uint32_t andMaskEncoded = (1 << 12) // = N
|
|
|
|
| ((64 - NrBitsToZero) << 6) // immr
|
|
|
|
| ((64 - NrBitsToZero - 1) << 0); // imms
|
|
|
|
|
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(AArch64::ANDXri), AArch64::SP)
|
|
|
|
.addReg(scratchSPReg, RegState::Kill)
|
|
|
|
.addImm(andMaskEncoded);
|
|
|
|
AFI->setStackRealigned(true);
|
|
|
|
}
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we need a base pointer, set it up here. It's whatever the value of the
|
|
|
|
// stack pointer is at this point. Any variable size objects will be allocated
|
|
|
|
// after this, so we can still use the base pointer to reference locals.
|
|
|
|
//
|
|
|
|
// FIXME: Clarify FrameSetup flags here.
|
|
|
|
// Note: Use emitFrameOffset() like above for FP if the FrameSetup flag is
|
|
|
|
// needed.
|
2015-04-09 16:49:47 +08:00
|
|
|
if (RegInfo->hasBasePointer(MF)) {
|
|
|
|
TII->copyPhysReg(MBB, MBBI, DL, RegInfo->getBaseRegister(), AArch64::SP,
|
|
|
|
false);
|
|
|
|
}
|
2014-03-29 18:18:08 +08:00
|
|
|
|
|
|
|
if (needsFrameMoves) {
|
2015-07-16 14:11:10 +08:00
|
|
|
const DataLayout &TD = MF.getDataLayout();
|
|
|
|
const int StackGrowth = -TD.getPointerSize(0);
|
2014-03-29 18:18:08 +08:00
|
|
|
unsigned FramePtr = RegInfo->getFrameRegister(MF);
|
|
|
|
// An example of the prologue:
|
|
|
|
//
|
|
|
|
// .globl __foo
|
|
|
|
// .align 2
|
|
|
|
// __foo:
|
|
|
|
// Ltmp0:
|
|
|
|
// .cfi_startproc
|
|
|
|
// .cfi_personality 155, ___gxx_personality_v0
|
|
|
|
// Leh_func_begin:
|
|
|
|
// .cfi_lsda 16, Lexception33
|
|
|
|
//
|
|
|
|
// stp xa,bx, [sp, -#offset]!
|
|
|
|
// ...
|
|
|
|
// stp x28, x27, [sp, #offset-32]
|
|
|
|
// stp fp, lr, [sp, #offset-16]
|
|
|
|
// add fp, sp, #offset - 16
|
|
|
|
// sub sp, sp, #1360
|
|
|
|
//
|
|
|
|
// The Stack:
|
|
|
|
// +-------------------------------------------+
|
|
|
|
// 10000 | ........ | ........ | ........ | ........ |
|
|
|
|
// 10004 | ........ | ........ | ........ | ........ |
|
|
|
|
// +-------------------------------------------+
|
|
|
|
// 10008 | ........ | ........ | ........ | ........ |
|
|
|
|
// 1000c | ........ | ........ | ........ | ........ |
|
|
|
|
// +===========================================+
|
|
|
|
// 10010 | X28 Register |
|
|
|
|
// 10014 | X28 Register |
|
|
|
|
// +-------------------------------------------+
|
|
|
|
// 10018 | X27 Register |
|
|
|
|
// 1001c | X27 Register |
|
|
|
|
// +===========================================+
|
|
|
|
// 10020 | Frame Pointer |
|
|
|
|
// 10024 | Frame Pointer |
|
|
|
|
// +-------------------------------------------+
|
|
|
|
// 10028 | Link Register |
|
|
|
|
// 1002c | Link Register |
|
|
|
|
// +===========================================+
|
|
|
|
// 10030 | ........ | ........ | ........ | ........ |
|
|
|
|
// 10034 | ........ | ........ | ........ | ........ |
|
|
|
|
// +-------------------------------------------+
|
|
|
|
// 10038 | ........ | ........ | ........ | ........ |
|
|
|
|
// 1003c | ........ | ........ | ........ | ........ |
|
|
|
|
// +-------------------------------------------+
|
|
|
|
//
|
|
|
|
// [sp] = 10030 :: >>initial value<<
|
|
|
|
// sp = 10020 :: stp fp, lr, [sp, #-16]!
|
|
|
|
// fp = sp == 10020 :: mov fp, sp
|
|
|
|
// [sp] == 10020 :: stp x28, x27, [sp, #-16]!
|
|
|
|
// sp == 10010 :: >>final value<<
|
|
|
|
//
|
|
|
|
// The frame pointer (w29) points to address 10020. If we use an offset of
|
|
|
|
// '16' from 'w29', we get the CFI offsets of -8 for w30, -16 for w29, -24
|
|
|
|
// for w27, and -32 for w28:
|
|
|
|
//
|
|
|
|
// Ltmp1:
|
|
|
|
// .cfi_def_cfa w29, 16
|
|
|
|
// Ltmp2:
|
|
|
|
// .cfi_offset w30, -8
|
|
|
|
// Ltmp3:
|
|
|
|
// .cfi_offset w29, -16
|
|
|
|
// Ltmp4:
|
|
|
|
// .cfi_offset w27, -24
|
|
|
|
// Ltmp5:
|
|
|
|
// .cfi_offset w28, -32
|
|
|
|
|
|
|
|
if (HasFP) {
|
|
|
|
// Define the current CFA rule to use the provided FP.
|
|
|
|
unsigned Reg = RegInfo->getDwarfRegNum(FramePtr, true);
|
|
|
|
unsigned CFIIndex = MMI.addFrameInst(
|
|
|
|
MCCFIInstruction::createDefCfa(nullptr, Reg, 2 * StackGrowth));
|
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
|
2014-12-16 08:20:49 +08:00
|
|
|
.addCFIIndex(CFIIndex)
|
|
|
|
.setMIFlags(MachineInstr::FrameSetup);
|
2014-03-29 18:18:08 +08:00
|
|
|
} else {
|
|
|
|
// Encode the stack size of the leaf function.
|
|
|
|
unsigned CFIIndex = MMI.addFrameInst(
|
|
|
|
MCCFIInstruction::createDefCfaOffset(nullptr, -MFI->getStackSize()));
|
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
|
2014-12-16 08:20:49 +08:00
|
|
|
.addCFIIndex(CFIIndex)
|
|
|
|
.setMIFlags(MachineInstr::FrameSetup);
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
|
|
|
|
2016-02-26 00:36:08 +08:00
|
|
|
// Now emit the moves for whatever callee saved regs we have (including FP,
|
|
|
|
// LR if those are saved).
|
|
|
|
emitCalleeSavedFrameMoves(MBB, MBBI);
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-24 20:50:23 +08:00
|
|
|
void AArch64FrameLowering::emitEpilogue(MachineFunction &MF,
|
|
|
|
MachineBasicBlock &MBB) const {
|
2014-03-29 18:18:08 +08:00
|
|
|
MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
|
|
|
|
MachineFrameInfo *MFI = MF.getFrameInfo();
|
2015-12-17 06:54:06 +08:00
|
|
|
const AArch64Subtarget &Subtarget = MF.getSubtarget<AArch64Subtarget>();
|
|
|
|
const TargetInstrInfo *TII = Subtarget.getInstrInfo();
|
[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
|
|
|
DebugLoc DL;
|
|
|
|
bool IsTailCallReturn = false;
|
|
|
|
if (MBB.end() != MBBI) {
|
|
|
|
DL = MBBI->getDebugLoc();
|
|
|
|
unsigned RetOpcode = MBBI->getOpcode();
|
|
|
|
IsTailCallReturn = RetOpcode == AArch64::TCRETURNdi ||
|
|
|
|
RetOpcode == AArch64::TCRETURNri;
|
|
|
|
}
|
2014-03-31 18:21:36 +08:00
|
|
|
int NumBytes = MFI->getStackSize();
|
2014-05-24 20:50:23 +08:00
|
|
|
const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
|
2014-05-15 09:33:17 +08:00
|
|
|
|
2015-01-20 01:40:05 +08:00
|
|
|
// All calls are tail calls in GHC calling conv, and functions have no
|
|
|
|
// prologue/epilogue.
|
|
|
|
if (MF.getFunction()->getCallingConv() == CallingConv::GHC)
|
|
|
|
return;
|
|
|
|
|
2015-04-09 16:49:47 +08:00
|
|
|
// Initial and residual are named for consistency with the prologue. Note that
|
2014-05-15 09:33:17 +08:00
|
|
|
// in the epilogue, the residual adjustment is executed first.
|
|
|
|
uint64_t ArgumentPopSize = 0;
|
[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
|
|
|
if (IsTailCallReturn) {
|
2014-05-15 09:33:17 +08:00
|
|
|
MachineOperand &StackAdjust = MBBI->getOperand(1);
|
|
|
|
|
|
|
|
// For a tail-call in a callee-pops-arguments environment, some or all of
|
|
|
|
// the stack may actually be in use for the call's arguments, this is
|
|
|
|
// calculated during LowerCall and consumed here...
|
|
|
|
ArgumentPopSize = StackAdjust.getImm();
|
|
|
|
} else {
|
|
|
|
// ... otherwise the amount to pop is *all* of the argument space,
|
|
|
|
// conveniently stored in the MachineFunctionInfo by
|
|
|
|
// LowerFormalArguments. This will, of course, be zero for the C calling
|
|
|
|
// convention.
|
|
|
|
ArgumentPopSize = AFI->getArgumentStackToRestore();
|
|
|
|
}
|
|
|
|
|
|
|
|
// The stack frame should be like below,
|
|
|
|
//
|
|
|
|
// ---------------------- ---
|
|
|
|
// | | |
|
|
|
|
// | BytesInStackArgArea| CalleeArgStackSize
|
|
|
|
// | (NumReusableBytes) | (of tail call)
|
|
|
|
// | | ---
|
|
|
|
// | | |
|
|
|
|
// ---------------------| --- |
|
|
|
|
// | | | |
|
|
|
|
// | CalleeSavedReg | | |
|
2016-02-02 00:29:19 +08:00
|
|
|
// | (CalleeSavedStackSize)| | |
|
2014-05-15 09:33:17 +08:00
|
|
|
// | | | |
|
|
|
|
// ---------------------| | NumBytes
|
|
|
|
// | | StackSize (StackAdjustUp)
|
|
|
|
// | LocalStackSize | | |
|
|
|
|
// | (covering callee | | |
|
|
|
|
// | args) | | |
|
|
|
|
// | | | |
|
|
|
|
// ---------------------- --- ---
|
|
|
|
//
|
|
|
|
// So NumBytes = StackSize + BytesInStackArgArea - CalleeArgStackSize
|
|
|
|
// = StackSize + ArgumentPopSize
|
|
|
|
//
|
2014-05-24 20:50:23 +08:00
|
|
|
// AArch64TargetLowering::LowerCall figures out ArgumentPopSize and keeps
|
|
|
|
// it as the 2nd argument of AArch64ISD::TC_RETURN.
|
2014-05-15 09:33:17 +08:00
|
|
|
|
2014-03-29 18:18:08 +08:00
|
|
|
// Move past the restores of the callee-saved registers.
|
[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
|
|
|
MachineBasicBlock::iterator LastPopI = MBB.getFirstTerminator();
|
2015-12-17 11:18:47 +08:00
|
|
|
MachineBasicBlock::iterator Begin = MBB.begin();
|
|
|
|
while (LastPopI != Begin) {
|
|
|
|
--LastPopI;
|
2016-02-02 00:29:19 +08:00
|
|
|
if (!LastPopI->getFlag(MachineInstr::FrameDestroy)) {
|
2014-03-29 18:18:08 +08:00
|
|
|
++LastPopI;
|
2015-12-17 11:18:47 +08:00
|
|
|
break;
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
|
|
|
}
|
2016-02-02 00:29:19 +08:00
|
|
|
NumBytes -= AFI->getCalleeSavedStackSize();
|
2014-03-29 18:18:08 +08:00
|
|
|
assert(NumBytes >= 0 && "Negative stack allocation size!?");
|
|
|
|
|
|
|
|
if (!hasFP(MF)) {
|
2016-02-24 00:54:36 +08:00
|
|
|
bool RedZone = canUseRedZone(MF);
|
2014-03-29 18:18:08 +08:00
|
|
|
// If this was a redzone leaf function, we don't need to restore the
|
2016-02-24 00:54:36 +08:00
|
|
|
// stack pointer (but we may need to pop stack args for fastcc).
|
|
|
|
if (RedZone && ArgumentPopSize == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
bool NoCalleeSaveRestore = AFI->getCalleeSavedStackSize() == 0;
|
|
|
|
int StackRestoreBytes = RedZone ? 0 : NumBytes;
|
|
|
|
if (NoCalleeSaveRestore)
|
|
|
|
StackRestoreBytes += ArgumentPopSize;
|
|
|
|
emitFrameOffset(MBB, LastPopI, DL, AArch64::SP, AArch64::SP,
|
|
|
|
StackRestoreBytes, TII, MachineInstr::FrameDestroy);
|
|
|
|
// If we were able to combine the local stack pop with the argument pop,
|
|
|
|
// then we're done.
|
|
|
|
if (NoCalleeSaveRestore || ArgumentPopSize == 0)
|
|
|
|
return;
|
|
|
|
NumBytes = 0;
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Restore the original stack pointer.
|
|
|
|
// FIXME: Rather than doing the math here, we should instead just use
|
|
|
|
// non-post-indexed loads for the restores if we aren't actually going to
|
|
|
|
// be able to save any instructions.
|
2016-03-15 02:17:41 +08:00
|
|
|
if (MFI->hasVarSizedObjects() || AFI->isStackRealigned())
|
2014-05-24 20:50:23 +08:00
|
|
|
emitFrameOffset(MBB, LastPopI, DL, AArch64::SP, AArch64::FP,
|
2016-02-02 00:29:19 +08:00
|
|
|
-AFI->getCalleeSavedStackSize() + 16, TII,
|
|
|
|
MachineInstr::FrameDestroy);
|
2016-03-15 02:17:41 +08:00
|
|
|
else if (NumBytes)
|
|
|
|
emitFrameOffset(MBB, LastPopI, DL, AArch64::SP, AArch64::SP, NumBytes, TII,
|
|
|
|
MachineInstr::FrameDestroy);
|
2016-02-24 00:54:36 +08:00
|
|
|
|
|
|
|
// This must be placed after the callee-save restore code because that code
|
|
|
|
// assumes the SP is at the same location as it was after the callee-save save
|
|
|
|
// code in the prologue.
|
|
|
|
if (ArgumentPopSize)
|
|
|
|
emitFrameOffset(MBB, MBB.getFirstTerminator(), DL, AArch64::SP, AArch64::SP,
|
|
|
|
ArgumentPopSize, TII, MachineInstr::FrameDestroy);
|
2014-03-29 18:18:08 +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.
|
2014-05-24 20:50:23 +08:00
|
|
|
int AArch64FrameLowering::getFrameIndexReference(const MachineFunction &MF,
|
|
|
|
int FI,
|
|
|
|
unsigned &FrameReg) const {
|
2014-03-29 18:18:08 +08:00
|
|
|
return resolveFrameIndexReference(MF, FI, FrameReg);
|
|
|
|
}
|
|
|
|
|
2014-05-24 20:50:23 +08:00
|
|
|
int AArch64FrameLowering::resolveFrameIndexReference(const MachineFunction &MF,
|
|
|
|
int FI, unsigned &FrameReg,
|
|
|
|
bool PreferFP) const {
|
2014-03-29 18:18:08 +08:00
|
|
|
const MachineFrameInfo *MFI = MF.getFrameInfo();
|
2014-05-24 20:50:23 +08:00
|
|
|
const AArch64RegisterInfo *RegInfo = static_cast<const AArch64RegisterInfo *>(
|
2014-08-05 10:39:49 +08:00
|
|
|
MF.getSubtarget().getRegisterInfo());
|
2014-05-24 20:50:23 +08:00
|
|
|
const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
|
2014-03-29 18:18:08 +08:00
|
|
|
int FPOffset = MFI->getObjectOffset(FI) + 16;
|
|
|
|
int Offset = MFI->getObjectOffset(FI) + MFI->getStackSize();
|
|
|
|
bool isFixed = MFI->isFixedObjectIndex(FI);
|
|
|
|
|
|
|
|
// Use frame pointer to reference fixed objects. Use it for locals if
|
2015-04-09 16:49:47 +08:00
|
|
|
// there are VLAs or a dynamically realigned SP (and thus the SP isn't
|
|
|
|
// reliable as a base). Make sure useFPForScavengingIndex() does the
|
|
|
|
// right thing for the emergency spill slot.
|
2014-03-29 18:18:08 +08:00
|
|
|
bool UseFP = false;
|
|
|
|
if (AFI->hasStackFrame()) {
|
|
|
|
// Note: Keeping the following as multiple 'if' statements rather than
|
|
|
|
// merging to a single expression for readability.
|
|
|
|
//
|
|
|
|
// Argument access should always use the FP.
|
|
|
|
if (isFixed) {
|
|
|
|
UseFP = hasFP(MF);
|
2015-04-09 16:49:47 +08:00
|
|
|
} else if (hasFP(MF) && !RegInfo->hasBasePointer(MF) &&
|
|
|
|
!RegInfo->needsStackRealignment(MF)) {
|
2014-03-29 18:18:08 +08:00
|
|
|
// Use SP or FP, whichever gives us the best chance of the offset
|
|
|
|
// being in range for direct access. If the FPOffset is positive,
|
|
|
|
// that'll always be best, as the SP will be even further away.
|
|
|
|
// If the FPOffset is negative, we have to keep in mind that the
|
|
|
|
// available offset range for negative offsets is smaller than for
|
|
|
|
// positive ones. If we have variable sized objects, we're stuck with
|
|
|
|
// using the FP regardless, though, as the SP offset is unknown
|
|
|
|
// and we don't have a base pointer available. If an offset is
|
|
|
|
// available via the FP and the SP, use whichever is closest.
|
|
|
|
if (PreferFP || MFI->hasVarSizedObjects() || FPOffset >= 0 ||
|
|
|
|
(FPOffset >= -256 && Offset > -FPOffset))
|
|
|
|
UseFP = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-09 16:49:47 +08:00
|
|
|
assert((isFixed || !RegInfo->needsStackRealignment(MF) || !UseFP) &&
|
|
|
|
"In the presence of dynamic stack pointer realignment, "
|
|
|
|
"non-argument objects cannot be accessed through the frame pointer");
|
|
|
|
|
2014-03-29 18:18:08 +08:00
|
|
|
if (UseFP) {
|
|
|
|
FrameReg = RegInfo->getFrameRegister(MF);
|
|
|
|
return FPOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use the base pointer if we have one.
|
|
|
|
if (RegInfo->hasBasePointer(MF))
|
|
|
|
FrameReg = RegInfo->getBaseRegister();
|
|
|
|
else {
|
2014-05-24 20:50:23 +08:00
|
|
|
FrameReg = AArch64::SP;
|
2014-03-29 18:18:08 +08:00
|
|
|
// If we're using the red zone for this function, the SP won't actually
|
|
|
|
// be adjusted, so the offsets will be negative. They're also all
|
|
|
|
// within range of the signed 9-bit immediate instructions.
|
|
|
|
if (canUseRedZone(MF))
|
|
|
|
Offset -= AFI->getLocalStackSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned getPrologueDeath(MachineFunction &MF, unsigned Reg) {
|
2014-05-24 20:50:23 +08:00
|
|
|
if (Reg != AArch64::LR)
|
2014-03-29 18:18:08 +08:00
|
|
|
return getKillRegState(true);
|
|
|
|
|
|
|
|
// LR maybe referred to later by an @llvm.returnaddress intrinsic.
|
2014-05-24 20:50:23 +08:00
|
|
|
bool LRLiveIn = MF.getRegInfo().isLiveIn(AArch64::LR);
|
2014-03-29 18:18:08 +08:00
|
|
|
bool LRKill = !(LRLiveIn && MF.getFrameInfo()->isReturnAddressTaken());
|
|
|
|
return getKillRegState(LRKill);
|
|
|
|
}
|
|
|
|
|
2016-02-02 03:07:06 +08:00
|
|
|
struct RegPairInfo {
|
|
|
|
RegPairInfo() : Reg1(AArch64::NoRegister), Reg2(AArch64::NoRegister) {}
|
|
|
|
unsigned Reg1;
|
|
|
|
unsigned Reg2;
|
|
|
|
int FrameIdx;
|
|
|
|
int Offset;
|
|
|
|
bool IsGPR;
|
2016-02-13 00:31:41 +08:00
|
|
|
bool isPaired() const { return Reg2 != AArch64::NoRegister; }
|
2016-02-02 03:07:06 +08:00
|
|
|
};
|
|
|
|
|
2016-02-13 00:31:41 +08:00
|
|
|
static void computeCalleeSaveRegisterPairs(
|
|
|
|
MachineFunction &MF, const std::vector<CalleeSavedInfo> &CSI,
|
|
|
|
const TargetRegisterInfo *TRI, SmallVectorImpl<RegPairInfo> &RegPairs) {
|
2016-02-02 03:07:06 +08:00
|
|
|
|
2016-02-13 00:31:41 +08:00
|
|
|
if (CSI.empty())
|
|
|
|
return;
|
2015-11-06 05:54:58 +08:00
|
|
|
|
2016-02-13 00:31:41 +08:00
|
|
|
AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
|
|
|
|
MachineFrameInfo *MFI = MF.getFrameInfo();
|
2016-03-10 12:35:09 +08:00
|
|
|
CallingConv::ID CC = MF.getFunction()->getCallingConv();
|
2016-02-13 00:31:41 +08:00
|
|
|
unsigned Count = CSI.size();
|
2016-03-10 12:35:09 +08:00
|
|
|
(void)CC;
|
2016-02-13 00:31:41 +08:00
|
|
|
// MachO's compact unwind format relies on all registers being stored in
|
|
|
|
// pairs.
|
|
|
|
assert((!MF.getSubtarget<AArch64Subtarget>().isTargetMachO() ||
|
2016-03-10 12:35:09 +08:00
|
|
|
CC == CallingConv::PreserveMost ||
|
2016-02-13 00:31:41 +08:00
|
|
|
(Count & 1) == 0) &&
|
|
|
|
"Odd number of callee-saved regs to spill!");
|
|
|
|
unsigned Offset = AFI->getCalleeSavedStackSize();
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < Count; ++i) {
|
2016-02-02 03:07:06 +08:00
|
|
|
RegPairInfo RPI;
|
2016-02-13 00:31:41 +08:00
|
|
|
RPI.Reg1 = CSI[i].getReg();
|
|
|
|
|
|
|
|
assert(AArch64::GPR64RegClass.contains(RPI.Reg1) ||
|
|
|
|
AArch64::FPR64RegClass.contains(RPI.Reg1));
|
|
|
|
RPI.IsGPR = AArch64::GPR64RegClass.contains(RPI.Reg1);
|
|
|
|
|
|
|
|
// Add the next reg to the pair if it is in the same register class.
|
|
|
|
if (i + 1 < Count) {
|
|
|
|
unsigned NextReg = CSI[i + 1].getReg();
|
|
|
|
if ((RPI.IsGPR && AArch64::GPR64RegClass.contains(NextReg)) ||
|
|
|
|
(!RPI.IsGPR && AArch64::FPR64RegClass.contains(NextReg)))
|
|
|
|
RPI.Reg2 = NextReg;
|
|
|
|
}
|
2016-02-02 03:07:06 +08:00
|
|
|
|
2014-03-29 18:18:08 +08:00
|
|
|
// GPRs and FPRs are saved in pairs of 64-bit regs. We expect the CSI
|
|
|
|
// list to come in sorted by frame index so that we can issue the store
|
|
|
|
// pair instructions directly. Assert if we see anything otherwise.
|
|
|
|
//
|
|
|
|
// The order of the registers in the list is controlled by
|
|
|
|
// getCalleeSavedRegs(), so they will always be in-order, as well.
|
2016-02-13 00:31:41 +08:00
|
|
|
assert((!RPI.isPaired() ||
|
|
|
|
(CSI[i].getFrameIdx() + 1 == CSI[i + 1].getFrameIdx())) &&
|
2014-03-29 18:18:08 +08:00
|
|
|
"Out of order callee saved regs!");
|
2016-02-13 00:31:41 +08:00
|
|
|
|
|
|
|
// MachO's compact unwind format relies on all registers being stored in
|
|
|
|
// adjacent register pairs.
|
|
|
|
assert((!MF.getSubtarget<AArch64Subtarget>().isTargetMachO() ||
|
2016-03-10 12:35:09 +08:00
|
|
|
CC == CallingConv::PreserveMost ||
|
2016-02-13 00:31:41 +08:00
|
|
|
(RPI.isPaired() &&
|
|
|
|
((RPI.Reg1 == AArch64::LR && RPI.Reg2 == AArch64::FP) ||
|
|
|
|
RPI.Reg1 + 1 == RPI.Reg2))) &&
|
|
|
|
"Callee-save registers not saved as adjacent register pair!");
|
|
|
|
|
|
|
|
RPI.FrameIdx = CSI[i].getFrameIdx();
|
|
|
|
|
|
|
|
if (Count * 8 != AFI->getCalleeSavedStackSize() && !RPI.isPaired()) {
|
|
|
|
// Round up size of non-pair to pair size if we need to pad the
|
|
|
|
// callee-save area to ensure 16-byte alignment.
|
|
|
|
Offset -= 16;
|
|
|
|
assert(MFI->getObjectAlignment(RPI.FrameIdx) <= 16);
|
|
|
|
MFI->setObjectSize(RPI.FrameIdx, 16);
|
|
|
|
} else
|
|
|
|
Offset -= RPI.isPaired() ? 16 : 8;
|
|
|
|
assert(Offset % 8 == 0);
|
|
|
|
RPI.Offset = Offset / 8;
|
2016-02-02 03:07:06 +08:00
|
|
|
assert((RPI.Offset >= -64 && RPI.Offset <= 63) &&
|
|
|
|
"Offset out of bounds for LDP/STP immediate");
|
|
|
|
|
|
|
|
RegPairs.push_back(RPI);
|
2016-02-13 00:31:41 +08:00
|
|
|
if (RPI.isPaired())
|
|
|
|
++i;
|
2016-02-02 03:07:06 +08:00
|
|
|
}
|
2016-02-13 00:31:41 +08:00
|
|
|
|
|
|
|
// Align first offset to even 16-byte boundary to avoid additional SP
|
|
|
|
// adjustment instructions.
|
|
|
|
// Last pair offset is size of whole callee-save region for SP
|
|
|
|
// pre-dec/post-inc.
|
|
|
|
RegPairInfo &LastPair = RegPairs.back();
|
|
|
|
assert(AFI->getCalleeSavedStackSize() % 8 == 0);
|
|
|
|
LastPair.Offset = AFI->getCalleeSavedStackSize() / 8;
|
2016-02-02 03:07:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AArch64FrameLowering::spillCalleeSavedRegisters(
|
|
|
|
MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
|
|
|
|
const std::vector<CalleeSavedInfo> &CSI,
|
|
|
|
const TargetRegisterInfo *TRI) const {
|
|
|
|
MachineFunction &MF = *MBB.getParent();
|
|
|
|
const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
|
|
|
|
DebugLoc DL;
|
|
|
|
SmallVector<RegPairInfo, 8> RegPairs;
|
|
|
|
|
2016-02-13 00:31:41 +08:00
|
|
|
computeCalleeSaveRegisterPairs(MF, CSI, TRI, RegPairs);
|
2016-02-02 03:07:06 +08:00
|
|
|
|
2016-02-13 00:31:41 +08:00
|
|
|
for (auto RPII = RegPairs.rbegin(), RPIE = RegPairs.rend(); RPII != RPIE;
|
2016-02-02 03:07:06 +08:00
|
|
|
++RPII) {
|
|
|
|
RegPairInfo RPI = *RPII;
|
|
|
|
unsigned Reg1 = RPI.Reg1;
|
|
|
|
unsigned Reg2 = RPI.Reg2;
|
|
|
|
unsigned StrOpc;
|
|
|
|
|
2014-03-29 18:18:08 +08:00
|
|
|
// Issue sequence of non-sp increment and pi sp spills for cs regs. The
|
|
|
|
// first spill is a pre-increment that allocates the stack.
|
|
|
|
// For example:
|
|
|
|
// stp x22, x21, [sp, #-48]! // addImm(-6)
|
|
|
|
// stp x20, x19, [sp, #16] // addImm(+2)
|
|
|
|
// stp fp, lr, [sp, #32] // addImm(+4)
|
|
|
|
// Rationale: This sequence saves uop updates compared to a sequence of
|
|
|
|
// pre-increment spills like stp xi,xj,[sp,#-16]!
|
2016-02-02 03:07:06 +08:00
|
|
|
// Note: Similar rationale and sequence for restores in epilog.
|
2016-02-13 00:31:41 +08:00
|
|
|
bool BumpSP = RPII == RegPairs.rbegin();
|
2016-02-02 03:07:06 +08:00
|
|
|
if (RPI.IsGPR) {
|
2014-03-29 18:18:08 +08:00
|
|
|
// For first spill use pre-increment store.
|
2016-02-02 03:07:06 +08:00
|
|
|
if (BumpSP)
|
2016-02-13 00:31:41 +08:00
|
|
|
StrOpc = RPI.isPaired() ? AArch64::STPXpre : AArch64::STRXpre;
|
2014-03-29 18:18:08 +08:00
|
|
|
else
|
2016-02-13 00:31:41 +08:00
|
|
|
StrOpc = RPI.isPaired() ? AArch64::STPXi : AArch64::STRXui;
|
2016-02-02 03:07:06 +08:00
|
|
|
} else {
|
2014-03-29 18:18:08 +08:00
|
|
|
// For first spill use pre-increment store.
|
2016-02-02 03:07:06 +08:00
|
|
|
if (BumpSP)
|
2016-02-13 00:31:41 +08:00
|
|
|
StrOpc = RPI.isPaired() ? AArch64::STPDpre : AArch64::STRDpre;
|
2014-03-29 18:18:08 +08:00
|
|
|
else
|
2016-02-13 00:31:41 +08:00
|
|
|
StrOpc = RPI.isPaired() ? AArch64::STPDi : AArch64::STRDui;
|
2016-02-02 03:07:06 +08:00
|
|
|
}
|
2016-02-13 00:31:41 +08:00
|
|
|
DEBUG(dbgs() << "CSR spill: (" << TRI->getName(Reg1);
|
|
|
|
if (RPI.isPaired())
|
|
|
|
dbgs() << ", " << TRI->getName(Reg2);
|
|
|
|
dbgs() << ") -> fi#(" << RPI.FrameIdx;
|
|
|
|
if (RPI.isPaired())
|
|
|
|
dbgs() << ", " << RPI.FrameIdx+1;
|
|
|
|
dbgs() << ")\n");
|
2016-02-02 03:07:06 +08:00
|
|
|
|
|
|
|
const int Offset = BumpSP ? -RPI.Offset : RPI.Offset;
|
2014-05-22 19:56:20 +08:00
|
|
|
MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(StrOpc));
|
2016-02-02 03:07:06 +08:00
|
|
|
if (BumpSP)
|
2014-05-24 20:50:23 +08:00
|
|
|
MIB.addReg(AArch64::SP, RegState::Define);
|
2014-05-22 19:56:20 +08:00
|
|
|
|
2016-02-13 00:31:41 +08:00
|
|
|
if (RPI.isPaired()) {
|
|
|
|
MBB.addLiveIn(Reg1);
|
|
|
|
MBB.addLiveIn(Reg2);
|
|
|
|
MIB.addReg(Reg2, getPrologueDeath(MF, Reg2))
|
2014-03-29 18:18:08 +08:00
|
|
|
.addReg(Reg1, getPrologueDeath(MF, Reg1))
|
2014-05-24 20:50:23 +08:00
|
|
|
.addReg(AArch64::SP)
|
2014-03-29 18:18:08 +08:00
|
|
|
.addImm(Offset) // [sp, #offset * 8], where factor * 8 is implicit
|
|
|
|
.setMIFlag(MachineInstr::FrameSetup);
|
2016-02-13 00:31:41 +08:00
|
|
|
} else {
|
|
|
|
MBB.addLiveIn(Reg1);
|
|
|
|
MIB.addReg(Reg1, getPrologueDeath(MF, Reg1))
|
|
|
|
.addReg(AArch64::SP)
|
|
|
|
.addImm(BumpSP ? Offset * 8 : Offset) // pre-inc version is unscaled
|
|
|
|
.setMIFlag(MachineInstr::FrameSetup);
|
|
|
|
}
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-24 20:50:23 +08:00
|
|
|
bool AArch64FrameLowering::restoreCalleeSavedRegisters(
|
2014-03-29 18:18:08 +08:00
|
|
|
MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
|
|
|
|
const std::vector<CalleeSavedInfo> &CSI,
|
|
|
|
const TargetRegisterInfo *TRI) const {
|
|
|
|
MachineFunction &MF = *MBB.getParent();
|
2014-08-05 10:39:49 +08:00
|
|
|
const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
|
2014-03-29 18:18:08 +08:00
|
|
|
DebugLoc DL;
|
2016-02-02 03:07:06 +08:00
|
|
|
SmallVector<RegPairInfo, 8> RegPairs;
|
2014-03-29 18:18:08 +08:00
|
|
|
|
|
|
|
if (MI != MBB.end())
|
|
|
|
DL = MI->getDebugLoc();
|
|
|
|
|
2016-02-13 00:31:41 +08:00
|
|
|
computeCalleeSaveRegisterPairs(MF, CSI, TRI, RegPairs);
|
2016-02-02 03:07:06 +08:00
|
|
|
|
2016-02-13 00:31:41 +08:00
|
|
|
for (auto RPII = RegPairs.begin(), RPIE = RegPairs.end(); RPII != RPIE;
|
2016-02-02 03:07:06 +08:00
|
|
|
++RPII) {
|
|
|
|
RegPairInfo RPI = *RPII;
|
|
|
|
unsigned Reg1 = RPI.Reg1;
|
|
|
|
unsigned Reg2 = RPI.Reg2;
|
|
|
|
|
2014-03-29 18:18:08 +08:00
|
|
|
// Issue sequence of non-sp increment and sp-pi restores for cs regs. Only
|
|
|
|
// the last load is sp-pi post-increment and de-allocates the stack:
|
|
|
|
// For example:
|
|
|
|
// ldp fp, lr, [sp, #32] // addImm(+4)
|
|
|
|
// ldp x20, x19, [sp, #16] // addImm(+2)
|
|
|
|
// ldp x22, x21, [sp], #48 // addImm(+6)
|
|
|
|
// Note: see comment in spillCalleeSavedRegisters()
|
|
|
|
unsigned LdrOpc;
|
2016-02-13 00:31:41 +08:00
|
|
|
bool BumpSP = RPII == std::prev(RegPairs.end());
|
2016-02-02 03:07:06 +08:00
|
|
|
if (RPI.IsGPR) {
|
|
|
|
if (BumpSP)
|
2016-02-13 00:31:41 +08:00
|
|
|
LdrOpc = RPI.isPaired() ? AArch64::LDPXpost : AArch64::LDRXpost;
|
2014-03-29 18:18:08 +08:00
|
|
|
else
|
2016-02-13 00:31:41 +08:00
|
|
|
LdrOpc = RPI.isPaired() ? AArch64::LDPXi : AArch64::LDRXui;
|
2016-02-02 03:07:06 +08:00
|
|
|
} else {
|
|
|
|
if (BumpSP)
|
2016-02-13 00:31:41 +08:00
|
|
|
LdrOpc = RPI.isPaired() ? AArch64::LDPDpost : AArch64::LDRDpost;
|
2014-03-29 18:18:08 +08:00
|
|
|
else
|
2016-02-13 00:31:41 +08:00
|
|
|
LdrOpc = RPI.isPaired() ? AArch64::LDPDi : AArch64::LDRDui;
|
2016-02-02 03:07:06 +08:00
|
|
|
}
|
2016-02-13 00:31:41 +08:00
|
|
|
DEBUG(dbgs() << "CSR restore: (" << TRI->getName(Reg1);
|
|
|
|
if (RPI.isPaired())
|
|
|
|
dbgs() << ", " << TRI->getName(Reg2);
|
|
|
|
dbgs() << ") -> fi#(" << RPI.FrameIdx;
|
|
|
|
if (RPI.isPaired())
|
|
|
|
dbgs() << ", " << RPI.FrameIdx+1;
|
|
|
|
dbgs() << ")\n");
|
2016-02-02 03:07:06 +08:00
|
|
|
|
|
|
|
const int Offset = RPI.Offset;
|
2014-05-22 19:56:20 +08:00
|
|
|
MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(LdrOpc));
|
2016-02-02 03:07:06 +08:00
|
|
|
if (BumpSP)
|
2014-05-24 20:50:23 +08:00
|
|
|
MIB.addReg(AArch64::SP, RegState::Define);
|
2014-05-22 19:56:20 +08:00
|
|
|
|
2016-02-13 00:31:41 +08:00
|
|
|
if (RPI.isPaired())
|
|
|
|
MIB.addReg(Reg2, getDefRegState(true))
|
2014-03-29 18:18:08 +08:00
|
|
|
.addReg(Reg1, getDefRegState(true))
|
2014-05-24 20:50:23 +08:00
|
|
|
.addReg(AArch64::SP)
|
2016-02-02 00:29:19 +08:00
|
|
|
.addImm(Offset) // [sp], #offset * 8 or [sp, #offset * 8]
|
|
|
|
// where the factor * 8 is implicit
|
|
|
|
.setMIFlag(MachineInstr::FrameDestroy);
|
2016-02-13 00:31:41 +08:00
|
|
|
else
|
|
|
|
MIB.addReg(Reg1, getDefRegState(true))
|
|
|
|
.addReg(AArch64::SP)
|
|
|
|
.addImm(BumpSP ? Offset * 8 : Offset) // post-dec version is unscaled
|
|
|
|
.setMIFlag(MachineInstr::FrameDestroy);
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-15 01:17:13 +08:00
|
|
|
void AArch64FrameLowering::determineCalleeSaves(MachineFunction &MF,
|
|
|
|
BitVector &SavedRegs,
|
|
|
|
RegScavenger *RS) const {
|
|
|
|
// All calls are tail calls in GHC calling conv, and functions have no
|
|
|
|
// prologue/epilogue.
|
|
|
|
if (MF.getFunction()->getCallingConv() == CallingConv::GHC)
|
|
|
|
return;
|
|
|
|
|
|
|
|
TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
|
2014-05-24 20:50:23 +08:00
|
|
|
const AArch64RegisterInfo *RegInfo = static_cast<const AArch64RegisterInfo *>(
|
2014-08-05 10:39:49 +08:00
|
|
|
MF.getSubtarget().getRegisterInfo());
|
2014-05-24 20:50:23 +08:00
|
|
|
AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
|
2016-02-13 00:31:41 +08:00
|
|
|
const AArch64Subtarget &Subtarget = MF.getSubtarget<AArch64Subtarget>();
|
|
|
|
unsigned UnspilledCSGPR = AArch64::NoRegister;
|
|
|
|
unsigned UnspilledCSGPRPaired = AArch64::NoRegister;
|
2014-03-29 18:18:08 +08:00
|
|
|
|
|
|
|
// The frame record needs to be created by saving the appropriate registers
|
|
|
|
if (hasFP(MF)) {
|
2015-07-15 01:17:13 +08:00
|
|
|
SavedRegs.set(AArch64::FP);
|
|
|
|
SavedRegs.set(AArch64::LR);
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
|
|
|
|
2016-02-13 00:31:41 +08:00
|
|
|
unsigned BasePointerReg = AArch64::NoRegister;
|
2014-03-29 18:18:08 +08:00
|
|
|
if (RegInfo->hasBasePointer(MF))
|
2016-02-13 00:31:41 +08:00
|
|
|
BasePointerReg = RegInfo->getBaseRegister();
|
2014-03-29 18:18:08 +08:00
|
|
|
|
|
|
|
bool ExtraCSSpill = false;
|
2014-04-04 13:16:06 +08:00
|
|
|
const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF);
|
2016-02-13 00:31:41 +08:00
|
|
|
// Figure out which callee-saved registers to save/restore.
|
|
|
|
for (unsigned i = 0; CSRegs[i]; ++i) {
|
|
|
|
const unsigned Reg = CSRegs[i];
|
|
|
|
|
2016-02-20 02:27:32 +08:00
|
|
|
// Add the base pointer register to SavedRegs if it is callee-save.
|
|
|
|
if (Reg == BasePointerReg)
|
2016-02-13 00:31:41 +08:00
|
|
|
SavedRegs.set(Reg);
|
2014-03-29 18:18:08 +08:00
|
|
|
|
2016-02-13 00:31:41 +08:00
|
|
|
bool RegUsed = SavedRegs.test(Reg);
|
|
|
|
unsigned PairedReg = CSRegs[i ^ 1];
|
|
|
|
if (!RegUsed) {
|
|
|
|
if (AArch64::GPR64RegClass.contains(Reg) &&
|
|
|
|
!RegInfo->isReservedReg(MF, Reg)) {
|
|
|
|
UnspilledCSGPR = Reg;
|
|
|
|
UnspilledCSGPRPaired = PairedReg;
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-02-13 00:31:41 +08:00
|
|
|
// MachO's compact unwind format relies on all registers being stored in
|
|
|
|
// pairs.
|
|
|
|
// FIXME: the usual format is actually better if unwinding isn't needed.
|
|
|
|
if (Subtarget.isTargetMachO() && !SavedRegs.test(PairedReg)) {
|
|
|
|
SavedRegs.set(PairedReg);
|
|
|
|
ExtraCSSpill = true;
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
2016-02-13 00:31:41 +08:00
|
|
|
}
|
2014-03-29 18:18:08 +08:00
|
|
|
|
2016-02-13 00:31:41 +08:00
|
|
|
DEBUG(dbgs() << "*** determineCalleeSaves\nUsed CSRs:";
|
|
|
|
for (int Reg = SavedRegs.find_first(); Reg != -1;
|
|
|
|
Reg = SavedRegs.find_next(Reg))
|
|
|
|
dbgs() << ' ' << PrintReg(Reg, RegInfo);
|
|
|
|
dbgs() << "\n";);
|
2014-03-29 18:18:08 +08:00
|
|
|
|
2016-02-13 00:31:41 +08:00
|
|
|
// If any callee-saved registers are used, the frame cannot be eliminated.
|
|
|
|
unsigned NumRegsSpilled = SavedRegs.count();
|
|
|
|
bool CanEliminateFrame = NumRegsSpilled == 0;
|
2014-03-29 18:18:08 +08:00
|
|
|
|
|
|
|
// FIXME: Set BigStack if any stack slot references may be out of range.
|
|
|
|
// For now, just conservatively guestimate based on unscaled indexing
|
|
|
|
// range. We'll end up allocating an unnecessary spill slot a lot, but
|
|
|
|
// realistically that's not a big deal at this stage of the game.
|
|
|
|
// The CSR spill slots have not been allocated yet, so estimateStackSize
|
|
|
|
// won't include them.
|
|
|
|
MachineFrameInfo *MFI = MF.getFrameInfo();
|
2016-02-13 00:31:41 +08:00
|
|
|
unsigned CFSize = MFI->estimateStackSize(MF) + 8 * NumRegsSpilled;
|
2014-03-29 18:18:08 +08:00
|
|
|
DEBUG(dbgs() << "Estimated stack frame size: " << CFSize << " bytes.\n");
|
|
|
|
bool BigStack = (CFSize >= 256);
|
|
|
|
if (BigStack || !CanEliminateFrame || RegInfo->cannotEliminateFrame(MF))
|
|
|
|
AFI->setHasStackFrame(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. If we already spilled an extra callee-saved register
|
|
|
|
// above to keep the number of spills even, we don't need to do anything else
|
|
|
|
// here.
|
|
|
|
if (BigStack && !ExtraCSSpill) {
|
2016-02-13 00:31:41 +08:00
|
|
|
if (UnspilledCSGPR != AArch64::NoRegister) {
|
|
|
|
DEBUG(dbgs() << "Spilling " << PrintReg(UnspilledCSGPR, RegInfo)
|
|
|
|
<< " to get a scratch register.\n");
|
|
|
|
SavedRegs.set(UnspilledCSGPR);
|
|
|
|
// MachO's compact unwind format relies on all registers being stored in
|
|
|
|
// pairs, so if we need to spill one extra for BigStack, then we need to
|
|
|
|
// store the pair.
|
|
|
|
if (Subtarget.isTargetMachO())
|
|
|
|
SavedRegs.set(UnspilledCSGPRPaired);
|
2014-03-29 18:18:08 +08:00
|
|
|
ExtraCSSpill = true;
|
2016-02-13 00:31:41 +08:00
|
|
|
NumRegsSpilled = SavedRegs.count();
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we didn't find an extra callee-saved register to spill, create
|
|
|
|
// an emergency spill slot.
|
|
|
|
if (!ExtraCSSpill) {
|
2014-05-24 20:50:23 +08:00
|
|
|
const TargetRegisterClass *RC = &AArch64::GPR64RegClass;
|
2014-03-29 18:18:08 +08:00
|
|
|
int FI = MFI->CreateStackObject(RC->getSize(), RC->getAlignment(), false);
|
|
|
|
RS->addScavengingFrameIndex(FI);
|
|
|
|
DEBUG(dbgs() << "No available CS registers, allocated fi#" << FI
|
|
|
|
<< " as the emergency spill slot.\n");
|
|
|
|
}
|
|
|
|
}
|
2016-02-02 00:29:19 +08:00
|
|
|
|
2016-02-13 00:31:41 +08:00
|
|
|
// Round up to register pair alignment to avoid additional SP adjustment
|
|
|
|
// instructions.
|
|
|
|
AFI->setCalleeSavedStackSize(alignTo(8 * NumRegsSpilled, 16));
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|