2014-05-24 20:50:23 +08:00
|
|
|
//===- AArch64FrameLowering.cpp - AArch64 Frame Lowering -------*- C++ -*-====//
|
2014-03-29 18:18:08 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2014-03-29 18:18:08 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
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 |
|
|
|
|
// | |
|
|
|
|
// |-----------------------------------|
|
|
|
|
// | |
|
2017-07-14 01:03:12 +08:00
|
|
|
// | (Win64 only) varargs from reg |
|
|
|
|
// | |
|
|
|
|
// |-----------------------------------|
|
|
|
|
// | |
|
2015-04-09 16:49:47 +08:00
|
|
|
// | 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"
|
2017-01-25 08:29:26 +08:00
|
|
|
#include "AArch64RegisterInfo.h"
|
2014-05-24 20:50:23 +08:00
|
|
|
#include "AArch64Subtarget.h"
|
|
|
|
#include "AArch64TargetMachine.h"
|
2017-12-20 14:51:45 +08:00
|
|
|
#include "MCTargetDesc/AArch64AddressingModes.h"
|
2018-08-17 20:53:22 +08:00
|
|
|
#include "llvm/ADT/ScopeExit.h"
|
2017-01-25 08:29:26 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2014-03-29 18:18:08 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2016-07-07 05:31:27 +08:00
|
|
|
#include "llvm/CodeGen/LivePhysRegs.h"
|
2017-01-25 08:29:26 +08:00
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
2014-03-29 18:18:08 +08:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2017-01-25 08:29:26 +08:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2014-03-29 18:18:08 +08:00
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2017-01-25 08:29:26 +08:00
|
|
|
#include "llvm/CodeGen/MachineMemOperand.h"
|
2014-03-29 18:18:08 +08:00
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
2017-01-25 08:29:26 +08:00
|
|
|
#include "llvm/CodeGen/MachineOperand.h"
|
2014-03-29 18:18:08 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/RegisterScavenging.h"
|
2017-11-08 09:01:31 +08:00
|
|
|
#include "llvm/CodeGen/TargetInstrInfo.h"
|
2017-11-17 09:07:10 +08:00
|
|
|
#include "llvm/CodeGen/TargetRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/TargetSubtargetInfo.h"
|
2018-10-31 17:27:01 +08:00
|
|
|
#include "llvm/CodeGen/WinEHFuncInfo.h"
|
2017-01-25 08:29:26 +08:00
|
|
|
#include "llvm/IR/Attributes.h"
|
|
|
|
#include "llvm/IR/CallingConv.h"
|
2014-07-25 19:42:14 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2017-01-25 08:29:26 +08:00
|
|
|
#include "llvm/IR/DebugLoc.h"
|
2014-07-25 19:42:14 +08:00
|
|
|
#include "llvm/IR/Function.h"
|
2018-10-31 17:27:01 +08:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2017-01-25 08:29:26 +08:00
|
|
|
#include "llvm/MC/MCDwarf.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"
|
2017-01-25 08:29:26 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/MathExtras.h"
|
2014-03-29 18:18:08 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-01-25 08:29:26 +08:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/TargetOptions.h"
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <iterator>
|
|
|
|
#include <vector>
|
2014-03-29 18:18:08 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
[AArch64] Emit CSR loads in the same order as stores
Optionally allow the order of restoring the callee-saved registers in the
epilogue to be reversed.
The flag -reverse-csr-restore-seq generates the following code:
```
stp x26, x25, [sp, #-64]!
stp x24, x23, [sp, #16]
stp x22, x21, [sp, #32]
stp x20, x19, [sp, #48]
; [..]
ldp x24, x23, [sp, #16]
ldp x22, x21, [sp, #32]
ldp x20, x19, [sp, #48]
ldp x26, x25, [sp], #64
ret
```
Note how the CSRs are restored in the same order as they are saved.
One exception to this rule is the last `ldp`, which allows us to merge
the stack adjustment and the ldp into a post-index ldp. This is done by
first generating:
ldp x26, x27, [sp]
add sp, sp, #64
which gets merged by the arm64 load store optimizer into
ldp x26, x25, [sp], #64
The flag is disabled by default.
llvm-svn: 327569
2018-03-15 04:34:03 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
ReverseCSRRestoreSeq("reverse-csr-restore-seq",
|
|
|
|
cl::desc("reverse the CSR restore sequence"),
|
|
|
|
cl::init(false), cl::Hidden);
|
|
|
|
|
2014-03-29 18:18:08 +08:00
|
|
|
STATISTIC(NumRedZoneFunctions, "Number of functions using red zone");
|
|
|
|
|
2018-01-19 11:16:36 +08:00
|
|
|
/// This is the biggest offset to the stack pointer we can encode in aarch64
|
|
|
|
/// instructions (without using a separate calculation and a temp register).
|
|
|
|
/// Note that the exception here are vector stores/loads which cannot encode any
|
|
|
|
/// displacements (see estimateRSStackSizeLimit(), isAArch64FrameOffsetLegal()).
|
|
|
|
static const unsigned DefaultSafeSPDisplacement = 255;
|
|
|
|
|
2017-05-30 14:58:41 +08:00
|
|
|
/// 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.
|
|
|
|
static unsigned estimateRSStackSizeLimit(MachineFunction &MF) {
|
|
|
|
// FIXME: 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.
|
|
|
|
for (MachineBasicBlock &MBB : MF) {
|
|
|
|
for (MachineInstr &MI : MBB) {
|
2018-05-09 10:42:00 +08:00
|
|
|
if (MI.isDebugInstr() || MI.isPseudo() ||
|
2017-05-30 14:58:41 +08:00
|
|
|
MI.getOpcode() == AArch64::ADDXri ||
|
|
|
|
MI.getOpcode() == AArch64::ADDSXri)
|
|
|
|
continue;
|
|
|
|
|
2017-10-31 06:00:06 +08:00
|
|
|
for (const MachineOperand &MO : MI.operands()) {
|
|
|
|
if (!MO.isFI())
|
2017-05-30 14:58:41 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
int Offset = 0;
|
|
|
|
if (isAArch64FrameOffsetLegal(MI, Offset, nullptr, nullptr, nullptr) ==
|
|
|
|
AArch64FrameOffsetCannotUpdate)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-19 11:16:36 +08:00
|
|
|
return DefaultSafeSPDisplacement;
|
2017-05-30 14:58:41 +08:00
|
|
|
}
|
|
|
|
|
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.
|
2017-12-16 06:22:58 +08:00
|
|
|
if (MF.getFunction().hasFnAttribute(Attribute::NoRedZone))
|
2014-03-29 18:18:08 +08:00
|
|
|
return false;
|
|
|
|
|
2016-07-29 02:40:00 +08:00
|
|
|
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-07-29 02:40:00 +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 {
|
2016-07-29 02:40:00 +08:00
|
|
|
const MachineFrameInfo &MFI = MF.getFrameInfo();
|
2014-08-05 10:39:49 +08:00
|
|
|
const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
|
2018-11-10 07:33:30 +08:00
|
|
|
// Win64 EH requires a frame pointer if funclets are present, as the locals
|
|
|
|
// are accessed off the frame pointer in both the parent function and the
|
|
|
|
// funclets.
|
|
|
|
if (MF.hasEHFunclets())
|
|
|
|
return true;
|
2016-03-03 01:58:31 +08:00
|
|
|
// Retain behavior of always omitting the FP for leaf functions when possible.
|
2018-01-19 11:16:36 +08:00
|
|
|
if (MFI.hasCalls() && MF.getTarget().Options.DisableFramePointerElim(MF))
|
|
|
|
return true;
|
|
|
|
if (MFI.hasVarSizedObjects() || MFI.isFrameAddressTaken() ||
|
|
|
|
MFI.hasStackMap() || MFI.hasPatchPoint() ||
|
|
|
|
RegInfo->needsStackRealignment(MF))
|
|
|
|
return true;
|
|
|
|
// With large callframes around we may need to use FP to access the scavenging
|
|
|
|
// emergency spillslot.
|
|
|
|
//
|
|
|
|
// Unfortunately some calls to hasFP() like machine verifier ->
|
|
|
|
// getReservedReg() -> hasFP in the middle of global isel are too early
|
|
|
|
// to know the max call frame size. Hopefully conservatively returning "true"
|
|
|
|
// in those cases is fine.
|
|
|
|
// DefaultSafeSPDisplacement is fine as we only emergency spill GP regs.
|
|
|
|
if (!MFI.isMaxCallFrameSizeComputed() ||
|
|
|
|
MFI.getMaxCallFrameSize() > DefaultSafeSPDisplacement)
|
|
|
|
return true;
|
|
|
|
|
[COFF, ARM64] Implement support for SEH extensions __try/__except/__finally
Summary:
This patch supports MS SEH extensions __try/__except/__finally. The intrinsics localescape and localrecover are responsible for communicating escaped static allocas from the try block to the handler.
We need to preserve frame pointers for SEH. So we create a new function/property HasLocalEscape.
Reviewers: rnk, compnerd, mstorsjo, TomTan, efriedma, ssijaric
Reviewed By: rnk, efriedma
Subscribers: smeenai, jrmuizel, alex, majnemer, ssijaric, ehsan, dmajor, kristina, javed.absar, kristof.beyls, chrib, llvm-commits
Differential Revision: https://reviews.llvm.org/D53540
llvm-svn: 351370
2019-01-17 03:52:59 +08:00
|
|
|
// Win64 SEH requires frame pointer if funclets are present.
|
|
|
|
if (MF.hasLocalEscape())
|
|
|
|
return true;
|
|
|
|
|
2018-01-19 11:16:36 +08:00
|
|
|
return false;
|
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 {
|
2016-07-29 02:40:00 +08:00
|
|
|
return !MF.getFrameInfo().hasVarSizedObjects();
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-08-17 20:53:22 +08:00
|
|
|
static bool ShouldSignReturnAddress(MachineFunction &MF) {
|
|
|
|
// The function should be signed in the following situations:
|
|
|
|
// - sign-return-address=all
|
|
|
|
// - sign-return-address=non-leaf and the functions spills the LR
|
|
|
|
|
|
|
|
const Function &F = MF.getFunction();
|
|
|
|
if (!F.hasFnAttribute("sign-return-address"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
StringRef Scope = F.getFnAttribute("sign-return-address").getValueAsString();
|
|
|
|
if (Scope.equals("none"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (Scope.equals("all"))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
assert(Scope.equals("non-leaf") && "Expected all, none or non-leaf");
|
|
|
|
|
|
|
|
for (const auto &Info : MF.getFrameInfo().getCalleeSavedInfo())
|
|
|
|
if (Info.getReg() == AArch64::LR)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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();
|
2016-07-29 02:40:00 +08:00
|
|
|
MachineFrameInfo &MFI = MF.getFrameInfo();
|
2016-12-01 07:48:42 +08:00
|
|
|
const TargetSubtargetInfo &STI = MF.getSubtarget();
|
|
|
|
const MCRegisterInfo *MRI = STI.getRegisterInfo();
|
|
|
|
const TargetInstrInfo *TII = STI.getInstrInfo();
|
2014-03-29 18:18:08 +08:00
|
|
|
DebugLoc DL = MBB.findDebugLoc(MBBI);
|
|
|
|
|
|
|
|
// Add callee saved registers to move list.
|
2016-07-29 02:40:00 +08:00
|
|
|
const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
|
2014-03-29 18:18:08 +08:00
|
|
|
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 =
|
2016-07-29 02:40:00 +08:00
|
|
|
MFI.getObjectOffset(Info.getFrameIdx()) - getOffsetOfLocalArea();
|
2014-03-29 18:18:08 +08:00
|
|
|
unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
|
2016-12-01 07:48:42 +08:00
|
|
|
unsigned CFIIndex = MF.addFrameInst(
|
2016-02-26 00:36:08 +08:00
|
|
|
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;
|
|
|
|
|
2017-04-01 07:12:27 +08:00
|
|
|
const AArch64Subtarget &Subtarget = MF->getSubtarget<AArch64Subtarget>();
|
2017-05-27 05:51:00 +08:00
|
|
|
const AArch64RegisterInfo &TRI = *Subtarget.getRegisterInfo();
|
2017-04-01 07:12:27 +08:00
|
|
|
LivePhysRegs LiveRegs(TRI);
|
2016-07-07 05:31:27 +08:00
|
|
|
LiveRegs.addLiveIns(*MBB);
|
2016-02-20 02:27:32 +08:00
|
|
|
|
2016-07-07 05:31:27 +08:00
|
|
|
// Mark callee saved registers as used so we will not choose them.
|
2018-09-23 06:17:50 +08:00
|
|
|
const MCPhysReg *CSRegs = MF->getRegInfo().getCalleeSavedRegs();
|
2016-02-20 02:27:32 +08:00
|
|
|
for (unsigned i = 0; CSRegs[i]; ++i)
|
2016-07-07 05:31:27 +08:00
|
|
|
LiveRegs.addReg(CSRegs[i]);
|
2016-02-20 02:27:32 +08:00
|
|
|
|
2016-07-07 05:31:27 +08:00
|
|
|
// Prefer X9 since it was historically used for the prologue scratch reg.
|
|
|
|
const MachineRegisterInfo &MRI = MF->getRegInfo();
|
|
|
|
if (LiveRegs.available(MRI, AArch64::X9))
|
|
|
|
return AArch64::X9;
|
2016-02-20 02:27:32 +08:00
|
|
|
|
2016-07-07 05:31:27 +08:00
|
|
|
for (unsigned Reg : AArch64::GPR64RegClass) {
|
|
|
|
if (LiveRegs.available(MRI, Reg))
|
|
|
|
return Reg;
|
|
|
|
}
|
2016-02-20 02:27:32 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-12-20 14:51:45 +08:00
|
|
|
static bool windowsRequiresStackProbe(MachineFunction &MF,
|
|
|
|
unsigned StackSizeInBytes) {
|
|
|
|
const AArch64Subtarget &Subtarget = MF.getSubtarget<AArch64Subtarget>();
|
|
|
|
if (!Subtarget.isTargetWindows())
|
|
|
|
return false;
|
|
|
|
const Function &F = MF.getFunction();
|
|
|
|
// TODO: When implementing stack protectors, take that into account
|
|
|
|
// for the probe threshold.
|
|
|
|
unsigned StackProbeSize = 4096;
|
|
|
|
if (F.hasFnAttribute("stack-probe-size"))
|
|
|
|
F.getFnAttribute("stack-probe-size")
|
|
|
|
.getValueAsString()
|
|
|
|
.getAsInteger(0, StackProbeSize);
|
2018-02-23 21:46:25 +08:00
|
|
|
return (StackSizeInBytes >= StackProbeSize) &&
|
|
|
|
!F.hasFnAttribute("no-stack-arg-probe");
|
2017-12-20 14:51:45 +08:00
|
|
|
}
|
|
|
|
|
2016-05-07 00:34:59 +08:00
|
|
|
bool AArch64FrameLowering::shouldCombineCSRLocalStackBump(
|
|
|
|
MachineFunction &MF, unsigned StackBumpBytes) const {
|
|
|
|
AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
|
2016-07-29 02:40:00 +08:00
|
|
|
const MachineFrameInfo &MFI = MF.getFrameInfo();
|
2016-05-07 00:34:59 +08:00
|
|
|
const AArch64Subtarget &Subtarget = MF.getSubtarget<AArch64Subtarget>();
|
|
|
|
const AArch64RegisterInfo *RegInfo = Subtarget.getRegisterInfo();
|
|
|
|
|
|
|
|
if (AFI->getLocalStackSize() == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// 512 is the maximum immediate for stp/ldp that will be used for
|
|
|
|
// callee-save save/restores
|
2017-12-20 14:51:45 +08:00
|
|
|
if (StackBumpBytes >= 512 || windowsRequiresStackProbe(MF, StackBumpBytes))
|
2016-05-07 00:34:59 +08:00
|
|
|
return false;
|
|
|
|
|
2016-07-29 02:40:00 +08:00
|
|
|
if (MFI.hasVarSizedObjects())
|
2016-05-07 00:34:59 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (RegInfo->needsStackRealignment(MF))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// This isn't strictly necessary, but it simplifies things a bit since the
|
|
|
|
// current RedZone handling code assumes the SP is adjusted by the
|
|
|
|
// callee-save save/restore code.
|
|
|
|
if (canUseRedZone(MF))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-31 17:27:01 +08:00
|
|
|
// Given a load or a store instruction, generate an appropriate unwinding SEH
|
|
|
|
// code on Windows.
|
|
|
|
static MachineBasicBlock::iterator InsertSEH(MachineBasicBlock::iterator MBBI,
|
|
|
|
const TargetInstrInfo &TII,
|
|
|
|
MachineInstr::MIFlag Flag) {
|
|
|
|
unsigned Opc = MBBI->getOpcode();
|
|
|
|
MachineBasicBlock *MBB = MBBI->getParent();
|
|
|
|
MachineFunction &MF = *MBB->getParent();
|
|
|
|
DebugLoc DL = MBBI->getDebugLoc();
|
|
|
|
unsigned ImmIdx = MBBI->getNumOperands() - 1;
|
|
|
|
int Imm = MBBI->getOperand(ImmIdx).getImm();
|
|
|
|
MachineInstrBuilder MIB;
|
|
|
|
const AArch64Subtarget &Subtarget = MF.getSubtarget<AArch64Subtarget>();
|
|
|
|
const AArch64RegisterInfo *RegInfo = Subtarget.getRegisterInfo();
|
|
|
|
|
|
|
|
switch (Opc) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("No SEH Opcode for this instruction");
|
|
|
|
case AArch64::LDPDpost:
|
|
|
|
Imm = -Imm;
|
|
|
|
LLVM_FALLTHROUGH;
|
|
|
|
case AArch64::STPDpre: {
|
|
|
|
unsigned Reg0 = RegInfo->getSEHRegNum(MBBI->getOperand(1).getReg());
|
|
|
|
unsigned Reg1 = RegInfo->getSEHRegNum(MBBI->getOperand(2).getReg());
|
|
|
|
MIB = BuildMI(MF, DL, TII.get(AArch64::SEH_SaveFRegP_X))
|
|
|
|
.addImm(Reg0)
|
|
|
|
.addImm(Reg1)
|
|
|
|
.addImm(Imm * 8)
|
|
|
|
.setMIFlag(Flag);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case AArch64::LDPXpost:
|
|
|
|
Imm = -Imm;
|
|
|
|
LLVM_FALLTHROUGH;
|
|
|
|
case AArch64::STPXpre: {
|
|
|
|
unsigned Reg0 = MBBI->getOperand(1).getReg();
|
|
|
|
unsigned Reg1 = MBBI->getOperand(2).getReg();
|
|
|
|
if (Reg0 == AArch64::FP && Reg1 == AArch64::LR)
|
|
|
|
MIB = BuildMI(MF, DL, TII.get(AArch64::SEH_SaveFPLR_X))
|
|
|
|
.addImm(Imm * 8)
|
|
|
|
.setMIFlag(Flag);
|
|
|
|
else
|
|
|
|
MIB = BuildMI(MF, DL, TII.get(AArch64::SEH_SaveRegP_X))
|
|
|
|
.addImm(RegInfo->getSEHRegNum(Reg0))
|
|
|
|
.addImm(RegInfo->getSEHRegNum(Reg1))
|
|
|
|
.addImm(Imm * 8)
|
|
|
|
.setMIFlag(Flag);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case AArch64::LDRDpost:
|
|
|
|
Imm = -Imm;
|
|
|
|
LLVM_FALLTHROUGH;
|
|
|
|
case AArch64::STRDpre: {
|
|
|
|
unsigned Reg = RegInfo->getSEHRegNum(MBBI->getOperand(1).getReg());
|
|
|
|
MIB = BuildMI(MF, DL, TII.get(AArch64::SEH_SaveFReg_X))
|
|
|
|
.addImm(Reg)
|
|
|
|
.addImm(Imm)
|
|
|
|
.setMIFlag(Flag);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case AArch64::LDRXpost:
|
|
|
|
Imm = -Imm;
|
|
|
|
LLVM_FALLTHROUGH;
|
|
|
|
case AArch64::STRXpre: {
|
|
|
|
unsigned Reg = RegInfo->getSEHRegNum(MBBI->getOperand(1).getReg());
|
|
|
|
MIB = BuildMI(MF, DL, TII.get(AArch64::SEH_SaveReg_X))
|
|
|
|
.addImm(Reg)
|
|
|
|
.addImm(Imm)
|
|
|
|
.setMIFlag(Flag);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case AArch64::STPDi:
|
|
|
|
case AArch64::LDPDi: {
|
|
|
|
unsigned Reg0 = RegInfo->getSEHRegNum(MBBI->getOperand(0).getReg());
|
|
|
|
unsigned Reg1 = RegInfo->getSEHRegNum(MBBI->getOperand(1).getReg());
|
|
|
|
MIB = BuildMI(MF, DL, TII.get(AArch64::SEH_SaveFRegP))
|
|
|
|
.addImm(Reg0)
|
|
|
|
.addImm(Reg1)
|
|
|
|
.addImm(Imm * 8)
|
|
|
|
.setMIFlag(Flag);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case AArch64::STPXi:
|
|
|
|
case AArch64::LDPXi: {
|
|
|
|
unsigned Reg0 = MBBI->getOperand(0).getReg();
|
|
|
|
unsigned Reg1 = MBBI->getOperand(1).getReg();
|
|
|
|
if (Reg0 == AArch64::FP && Reg1 == AArch64::LR)
|
|
|
|
MIB = BuildMI(MF, DL, TII.get(AArch64::SEH_SaveFPLR))
|
|
|
|
.addImm(Imm * 8)
|
|
|
|
.setMIFlag(Flag);
|
|
|
|
else
|
|
|
|
MIB = BuildMI(MF, DL, TII.get(AArch64::SEH_SaveRegP))
|
|
|
|
.addImm(RegInfo->getSEHRegNum(Reg0))
|
|
|
|
.addImm(RegInfo->getSEHRegNum(Reg1))
|
|
|
|
.addImm(Imm * 8)
|
|
|
|
.setMIFlag(Flag);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case AArch64::STRXui:
|
|
|
|
case AArch64::LDRXui: {
|
|
|
|
int Reg = RegInfo->getSEHRegNum(MBBI->getOperand(0).getReg());
|
|
|
|
MIB = BuildMI(MF, DL, TII.get(AArch64::SEH_SaveReg))
|
|
|
|
.addImm(Reg)
|
|
|
|
.addImm(Imm * 8)
|
|
|
|
.setMIFlag(Flag);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case AArch64::STRDui:
|
|
|
|
case AArch64::LDRDui: {
|
|
|
|
unsigned Reg = RegInfo->getSEHRegNum(MBBI->getOperand(0).getReg());
|
|
|
|
MIB = BuildMI(MF, DL, TII.get(AArch64::SEH_SaveFReg))
|
|
|
|
.addImm(Reg)
|
|
|
|
.addImm(Imm * 8)
|
|
|
|
.setMIFlag(Flag);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto I = MBB->insertAfter(MBBI, MIB);
|
|
|
|
return I;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fix up the SEH opcode associated with the save/restore instruction.
|
|
|
|
static void fixupSEHOpcode(MachineBasicBlock::iterator MBBI,
|
|
|
|
unsigned LocalStackSize) {
|
|
|
|
MachineOperand *ImmOpnd = nullptr;
|
|
|
|
unsigned ImmIdx = MBBI->getNumOperands() - 1;
|
|
|
|
switch (MBBI->getOpcode()) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Fix the offset in the SEH instruction");
|
|
|
|
case AArch64::SEH_SaveFPLR:
|
|
|
|
case AArch64::SEH_SaveRegP:
|
|
|
|
case AArch64::SEH_SaveReg:
|
|
|
|
case AArch64::SEH_SaveFRegP:
|
|
|
|
case AArch64::SEH_SaveFReg:
|
|
|
|
ImmOpnd = &MBBI->getOperand(ImmIdx);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ImmOpnd)
|
|
|
|
ImmOpnd->setImm(ImmOpnd->getImm() + LocalStackSize);
|
|
|
|
}
|
|
|
|
|
2016-05-07 00:34:59 +08:00
|
|
|
// Convert callee-save register save/restore instruction to do stack pointer
|
|
|
|
// decrement/increment to allocate/deallocate the callee-save stack area by
|
|
|
|
// converting store/load to use pre/post increment version.
|
|
|
|
static MachineBasicBlock::iterator convertCalleeSaveRestoreToSPPrePostIncDec(
|
2016-06-12 23:39:02 +08:00
|
|
|
MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
|
2018-10-31 17:27:01 +08:00
|
|
|
const DebugLoc &DL, const TargetInstrInfo *TII, int CSStackSizeInc,
|
|
|
|
bool NeedsWinCFI, bool InProlog = true) {
|
2018-04-05 05:55:44 +08:00
|
|
|
// Ignore instructions that do not operate on SP, i.e. shadow call stack
|
2018-11-17 04:08:54 +08:00
|
|
|
// instructions and associated CFI instruction.
|
2018-04-05 05:55:44 +08:00
|
|
|
while (MBBI->getOpcode() == AArch64::STRXpost ||
|
2018-11-17 04:08:54 +08:00
|
|
|
MBBI->getOpcode() == AArch64::LDRXpre ||
|
|
|
|
MBBI->getOpcode() == AArch64::CFI_INSTRUCTION) {
|
|
|
|
if (MBBI->getOpcode() != AArch64::CFI_INSTRUCTION)
|
|
|
|
assert(MBBI->getOperand(0).getReg() != AArch64::SP);
|
2018-04-05 05:55:44 +08:00
|
|
|
++MBBI;
|
|
|
|
}
|
2016-05-07 00:34:59 +08:00
|
|
|
unsigned NewOpc;
|
2018-09-12 17:44:46 +08:00
|
|
|
int Scale = 1;
|
2016-05-07 00:34:59 +08:00
|
|
|
switch (MBBI->getOpcode()) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unexpected callee-save save/restore opcode!");
|
|
|
|
case AArch64::STPXi:
|
|
|
|
NewOpc = AArch64::STPXpre;
|
2018-09-12 17:44:46 +08:00
|
|
|
Scale = 8;
|
2016-05-07 00:34:59 +08:00
|
|
|
break;
|
|
|
|
case AArch64::STPDi:
|
|
|
|
NewOpc = AArch64::STPDpre;
|
2018-09-12 17:44:46 +08:00
|
|
|
Scale = 8;
|
2016-05-07 00:34:59 +08:00
|
|
|
break;
|
2018-09-12 20:10:22 +08:00
|
|
|
case AArch64::STPQi:
|
|
|
|
NewOpc = AArch64::STPQpre;
|
|
|
|
Scale = 16;
|
|
|
|
break;
|
2016-05-07 00:34:59 +08:00
|
|
|
case AArch64::STRXui:
|
|
|
|
NewOpc = AArch64::STRXpre;
|
|
|
|
break;
|
|
|
|
case AArch64::STRDui:
|
|
|
|
NewOpc = AArch64::STRDpre;
|
|
|
|
break;
|
2018-09-12 20:10:22 +08:00
|
|
|
case AArch64::STRQui:
|
|
|
|
NewOpc = AArch64::STRQpre;
|
|
|
|
break;
|
2016-05-07 00:34:59 +08:00
|
|
|
case AArch64::LDPXi:
|
|
|
|
NewOpc = AArch64::LDPXpost;
|
2018-09-12 17:44:46 +08:00
|
|
|
Scale = 8;
|
2016-05-07 00:34:59 +08:00
|
|
|
break;
|
|
|
|
case AArch64::LDPDi:
|
|
|
|
NewOpc = AArch64::LDPDpost;
|
2018-09-12 17:44:46 +08:00
|
|
|
Scale = 8;
|
2016-05-07 00:34:59 +08:00
|
|
|
break;
|
2018-09-12 20:10:22 +08:00
|
|
|
case AArch64::LDPQi:
|
|
|
|
NewOpc = AArch64::LDPQpost;
|
|
|
|
Scale = 16;
|
|
|
|
break;
|
2016-05-07 00:34:59 +08:00
|
|
|
case AArch64::LDRXui:
|
|
|
|
NewOpc = AArch64::LDRXpost;
|
|
|
|
break;
|
|
|
|
case AArch64::LDRDui:
|
|
|
|
NewOpc = AArch64::LDRDpost;
|
|
|
|
break;
|
2018-09-12 20:10:22 +08:00
|
|
|
case AArch64::LDRQui:
|
|
|
|
NewOpc = AArch64::LDRQpost;
|
|
|
|
break;
|
2016-05-07 00:34:59 +08:00
|
|
|
}
|
2018-10-31 17:27:01 +08:00
|
|
|
// Get rid of the SEH code associated with the old instruction.
|
|
|
|
if (NeedsWinCFI) {
|
|
|
|
auto SEH = std::next(MBBI);
|
|
|
|
if (AArch64InstrInfo::isSEHInstruction(*SEH))
|
|
|
|
SEH->eraseFromParent();
|
|
|
|
}
|
2016-05-07 00:34:59 +08:00
|
|
|
|
|
|
|
MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(NewOpc));
|
|
|
|
MIB.addReg(AArch64::SP, RegState::Define);
|
|
|
|
|
|
|
|
// Copy all operands other than the immediate offset.
|
|
|
|
unsigned OpndIdx = 0;
|
|
|
|
for (unsigned OpndEnd = MBBI->getNumOperands() - 1; OpndIdx < OpndEnd;
|
|
|
|
++OpndIdx)
|
2017-01-13 17:58:52 +08:00
|
|
|
MIB.add(MBBI->getOperand(OpndIdx));
|
2016-05-07 00:34:59 +08:00
|
|
|
|
|
|
|
assert(MBBI->getOperand(OpndIdx).getImm() == 0 &&
|
|
|
|
"Unexpected immediate offset in first/last callee-save save/restore "
|
|
|
|
"instruction!");
|
|
|
|
assert(MBBI->getOperand(OpndIdx - 1).getReg() == AArch64::SP &&
|
|
|
|
"Unexpected base register in callee-save save/restore instruction!");
|
2018-09-12 17:44:46 +08:00
|
|
|
assert(CSStackSizeInc % Scale == 0);
|
|
|
|
MIB.addImm(CSStackSizeInc / Scale);
|
2016-05-07 00:34:59 +08:00
|
|
|
|
|
|
|
MIB.setMIFlags(MBBI->getFlags());
|
2018-08-17 05:30:05 +08:00
|
|
|
MIB.setMemRefs(MBBI->memoperands());
|
2016-05-07 00:34:59 +08:00
|
|
|
|
2018-10-31 17:27:01 +08:00
|
|
|
// Generate a new SEH code that corresponds to the new instruction.
|
|
|
|
if (NeedsWinCFI)
|
|
|
|
InsertSEH(*MIB, *TII,
|
|
|
|
InProlog ? MachineInstr::FrameSetup : MachineInstr::FrameDestroy);
|
|
|
|
|
2016-05-07 00:34:59 +08:00
|
|
|
return std::prev(MBB.erase(MBBI));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fixup callee-save register save/restore instructions to take into account
|
|
|
|
// combined SP bump by adding the local stack size to the stack offsets.
|
2016-07-09 04:29:42 +08:00
|
|
|
static void fixupCalleeSaveRestoreStackOffset(MachineInstr &MI,
|
2018-10-31 17:27:01 +08:00
|
|
|
unsigned LocalStackSize,
|
|
|
|
bool NeedsWinCFI) {
|
|
|
|
if (AArch64InstrInfo::isSEHInstruction(MI))
|
|
|
|
return;
|
|
|
|
|
2016-07-09 04:29:42 +08:00
|
|
|
unsigned Opc = MI.getOpcode();
|
2018-04-05 05:55:44 +08:00
|
|
|
|
|
|
|
// Ignore instructions that do not operate on SP, i.e. shadow call stack
|
2018-11-17 04:08:54 +08:00
|
|
|
// instructions and associated CFI instruction.
|
|
|
|
if (Opc == AArch64::STRXpost || Opc == AArch64::LDRXpre ||
|
|
|
|
Opc == AArch64::CFI_INSTRUCTION) {
|
|
|
|
if (Opc != AArch64::CFI_INSTRUCTION)
|
|
|
|
assert(MI.getOperand(0).getReg() != AArch64::SP);
|
2018-04-05 05:55:44 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:44:46 +08:00
|
|
|
unsigned Scale;
|
|
|
|
switch (Opc) {
|
|
|
|
case AArch64::STPXi:
|
|
|
|
case AArch64::STRXui:
|
|
|
|
case AArch64::STPDi:
|
|
|
|
case AArch64::STRDui:
|
|
|
|
case AArch64::LDPXi:
|
|
|
|
case AArch64::LDRXui:
|
|
|
|
case AArch64::LDPDi:
|
|
|
|
case AArch64::LDRDui:
|
|
|
|
Scale = 8;
|
|
|
|
break;
|
2018-09-12 20:10:22 +08:00
|
|
|
case AArch64::STPQi:
|
|
|
|
case AArch64::STRQui:
|
|
|
|
case AArch64::LDPQi:
|
|
|
|
case AArch64::LDRQui:
|
|
|
|
Scale = 16;
|
|
|
|
break;
|
2018-09-12 17:44:46 +08:00
|
|
|
default:
|
|
|
|
llvm_unreachable("Unexpected callee-save save/restore opcode!");
|
|
|
|
}
|
2016-05-07 00:34:59 +08:00
|
|
|
|
2016-07-09 04:29:42 +08:00
|
|
|
unsigned OffsetIdx = MI.getNumExplicitOperands() - 1;
|
|
|
|
assert(MI.getOperand(OffsetIdx - 1).getReg() == AArch64::SP &&
|
2016-05-07 00:34:59 +08:00
|
|
|
"Unexpected base register in callee-save save/restore instruction!");
|
|
|
|
// Last operand is immediate offset that needs fixing.
|
2016-07-09 04:29:42 +08:00
|
|
|
MachineOperand &OffsetOpnd = MI.getOperand(OffsetIdx);
|
2016-05-07 00:34:59 +08:00
|
|
|
// All generated opcodes have scaled offsets.
|
2018-09-12 20:10:22 +08:00
|
|
|
assert(LocalStackSize % Scale == 0);
|
2018-09-12 17:44:46 +08:00
|
|
|
OffsetOpnd.setImm(OffsetOpnd.getImm() + LocalStackSize / Scale);
|
2018-10-31 17:27:01 +08:00
|
|
|
|
|
|
|
if (NeedsWinCFI) {
|
|
|
|
auto MBBI = std::next(MachineBasicBlock::iterator(MI));
|
|
|
|
assert(MBBI != MI.getParent()->end() && "Expecting a valid instruction");
|
|
|
|
assert(AArch64InstrInfo::isSEHInstruction(*MBBI) &&
|
|
|
|
"Expecting a SEH instruction");
|
|
|
|
fixupSEHOpcode(MBBI, LocalStackSize);
|
|
|
|
}
|
2016-05-07 00:34:59 +08:00
|
|
|
}
|
|
|
|
|
2018-04-27 23:30:54 +08:00
|
|
|
static void adaptForLdStOpt(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator FirstSPPopI,
|
|
|
|
MachineBasicBlock::iterator LastPopI) {
|
|
|
|
// Sometimes (when we restore in the same order as we save), we can end up
|
|
|
|
// with code like this:
|
|
|
|
//
|
|
|
|
// ldp x26, x25, [sp]
|
|
|
|
// ldp x24, x23, [sp, #16]
|
|
|
|
// ldp x22, x21, [sp, #32]
|
|
|
|
// ldp x20, x19, [sp, #48]
|
|
|
|
// add sp, sp, #64
|
|
|
|
//
|
|
|
|
// In this case, it is always better to put the first ldp at the end, so
|
|
|
|
// that the load-store optimizer can run and merge the ldp and the add into
|
|
|
|
// a post-index ldp.
|
|
|
|
// If we managed to grab the first pop instruction, move it to the end.
|
|
|
|
if (ReverseCSRRestoreSeq)
|
|
|
|
MBB.splice(FirstSPPopI, &MBB, LastPopI);
|
|
|
|
// We should end up with something like this now:
|
|
|
|
//
|
|
|
|
// ldp x24, x23, [sp, #16]
|
|
|
|
// ldp x22, x21, [sp, #32]
|
|
|
|
// ldp x20, x19, [sp, #48]
|
|
|
|
// ldp x26, x25, [sp]
|
|
|
|
// add sp, sp, #64
|
|
|
|
//
|
|
|
|
// and the load-store optimizer can merge the last two instructions into:
|
|
|
|
//
|
|
|
|
// ldp x26, x25, [sp], #64
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
2018-10-30 00:26:58 +08:00
|
|
|
static bool ShouldSignWithAKey(MachineFunction &MF) {
|
|
|
|
const Function &F = MF.getFunction();
|
|
|
|
if (!F.hasFnAttribute("sign-return-address-key"))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
const StringRef Key =
|
|
|
|
F.getFnAttribute("sign-return-address-key").getValueAsString();
|
|
|
|
assert(Key.equals_lower("a_key") || Key.equals_lower("b_key"));
|
|
|
|
return Key.equals_lower("a_key");
|
|
|
|
}
|
|
|
|
|
2018-11-10 07:33:30 +08:00
|
|
|
static bool needsWinCFI(const MachineFunction &MF) {
|
|
|
|
const Function &F = MF.getFunction();
|
|
|
|
return MF.getTarget().getMCAsmInfo()->usesWindowsCFI() &&
|
|
|
|
F.needsUnwindTableEntry();
|
|
|
|
}
|
|
|
|
|
[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();
|
2016-07-29 02:40:00 +08:00
|
|
|
const MachineFrameInfo &MFI = MF.getFrameInfo();
|
2017-12-16 06:22:58 +08:00
|
|
|
const Function &F = 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>();
|
2018-10-31 17:27:01 +08:00
|
|
|
bool needsFrameMoves = (MMI.hasDebugInfo() || F.needsUnwindTableEntry()) &&
|
|
|
|
!MF.getTarget().getMCAsmInfo()->usesWindowsCFI();
|
2015-11-06 05:54:58 +08:00
|
|
|
bool HasFP = hasFP(MF);
|
2018-11-10 07:33:30 +08:00
|
|
|
bool NeedsWinCFI = needsWinCFI(MF);
|
2018-10-31 17:27:01 +08:00
|
|
|
MF.setHasWinCFI(NeedsWinCFI);
|
2018-11-10 07:33:30 +08:00
|
|
|
bool IsFunclet = MBB.isEHFuncletEntry();
|
|
|
|
|
2018-04-13 00:16:18 +08:00
|
|
|
// At this point, we're going to decide whether or not the function uses a
|
|
|
|
// redzone. In most cases, the function doesn't have a redzone so let's
|
|
|
|
// assume that's false and set it to true in the case that there's a redzone.
|
|
|
|
AFI->setHasRedZone(false);
|
|
|
|
|
2015-11-06 05:54:58 +08:00
|
|
|
// Debug location must be unknown since the first debug location is used
|
|
|
|
// to determine the end of the prologue.
|
|
|
|
DebugLoc DL;
|
|
|
|
|
2018-08-17 20:53:22 +08:00
|
|
|
if (ShouldSignReturnAddress(MF)) {
|
2018-12-21 18:45:08 +08:00
|
|
|
if (ShouldSignWithAKey(MF))
|
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(AArch64::PACIASP))
|
|
|
|
.setMIFlag(MachineInstr::FrameSetup);
|
|
|
|
else {
|
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(AArch64::EMITBKEY))
|
|
|
|
.setMIFlag(MachineInstr::FrameSetup);
|
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(AArch64::PACIBSP))
|
|
|
|
.setMIFlag(MachineInstr::FrameSetup);
|
|
|
|
}
|
2018-12-18 18:37:42 +08:00
|
|
|
|
|
|
|
unsigned CFIIndex =
|
|
|
|
MF.addFrameInst(MCCFIInstruction::createNegateRAState(nullptr));
|
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
|
|
|
|
.addCFIIndex(CFIIndex)
|
|
|
|
.setMIFlags(MachineInstr::FrameSetup);
|
2018-08-17 20:53:22 +08:00
|
|
|
}
|
|
|
|
|
2015-11-06 05:54:58 +08:00
|
|
|
// All calls are tail calls in GHC calling conv, and functions have no
|
|
|
|
// prologue/epilogue.
|
2017-12-16 06:22:58 +08:00
|
|
|
if (MF.getFunction().getCallingConv() == CallingConv::GHC)
|
2015-01-20 01:40:05 +08:00
|
|
|
return;
|
|
|
|
|
2018-11-10 07:33:30 +08:00
|
|
|
// getStackSize() includes all the locals in its size calculation. We don't
|
|
|
|
// include these locals when computing the stack size of a funclet, as they
|
|
|
|
// are allocated in the parent's stack frame and accessed via the frame
|
|
|
|
// pointer from the funclet. We only save the callee saved registers in the
|
|
|
|
// funclet, which are really the callee saved registers of the parent
|
|
|
|
// function, including the funclet.
|
|
|
|
int NumBytes = IsFunclet ? (int)getWinEHFuncletFrameSize(MF)
|
|
|
|
: (int)MFI.getStackSize();
|
2017-12-20 14:51:45 +08:00
|
|
|
if (!AFI->hasStackFrame() && !windowsRequiresStackProbe(MF, NumBytes)) {
|
2014-03-29 18:18:08 +08:00
|
|
|
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.
|
2018-04-04 05:56:10 +08:00
|
|
|
if (canUseRedZone(MF)) {
|
|
|
|
AFI->setHasRedZone(true);
|
2016-03-15 02:24:34 +08:00
|
|
|
++NumRedZoneFunctions;
|
2018-04-04 05:56:10 +08:00
|
|
|
} else {
|
2014-05-24 20:50:23 +08:00
|
|
|
emitFrameOffset(MBB, MBBI, DL, AArch64::SP, AArch64::SP, -NumBytes, TII,
|
2018-10-31 17:27:01 +08:00
|
|
|
MachineInstr::FrameSetup, false, NeedsWinCFI);
|
|
|
|
if (!NeedsWinCFI) {
|
|
|
|
// Label used to tie together the PROLOG_LABEL and the MachineMoves.
|
|
|
|
MCSymbol *FrameLabel = MMI.getContext().createTempSymbol();
|
|
|
|
// Encode the stack size of the leaf function.
|
|
|
|
unsigned CFIIndex = MF.addFrameInst(
|
|
|
|
MCCFIInstruction::createDefCfaOffset(FrameLabel, -NumBytes));
|
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
|
|
|
|
.addCFIIndex(CFIIndex)
|
|
|
|
.setMIFlags(MachineInstr::FrameSetup);
|
|
|
|
}
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
2018-10-31 17:27:01 +08:00
|
|
|
|
|
|
|
if (NeedsWinCFI)
|
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_PrologEnd))
|
|
|
|
.setMIFlag(MachineInstr::FrameSetup);
|
|
|
|
|
2014-03-29 18:18:08 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-02 05:13:54 +08:00
|
|
|
bool IsWin64 =
|
2017-12-16 06:22:58 +08:00
|
|
|
Subtarget.isCallingConvWin64(MF.getFunction().getCallingConv());
|
2018-11-10 07:33:30 +08:00
|
|
|
// Var args are accounted for in the containing function, so don't
|
|
|
|
// include them for funclets.
|
|
|
|
unsigned FixedObject = (IsWin64 && !IsFunclet) ?
|
|
|
|
alignTo(AFI->getVarArgsGPRSize(), 16) : 0;
|
2017-08-02 05:13:54 +08:00
|
|
|
|
|
|
|
auto PrologueSaveSize = AFI->getCalleeSavedStackSize() + FixedObject;
|
2016-03-15 02:24:34 +08:00
|
|
|
// All of the remaining stack allocations are for locals.
|
2017-08-02 05:13:54 +08:00
|
|
|
AFI->setLocalStackSize(NumBytes - PrologueSaveSize);
|
2016-05-07 00:34:59 +08:00
|
|
|
bool CombineSPBump = shouldCombineCSRLocalStackBump(MF, NumBytes);
|
|
|
|
if (CombineSPBump) {
|
|
|
|
emitFrameOffset(MBB, MBBI, DL, AArch64::SP, AArch64::SP, -NumBytes, TII,
|
2018-10-31 17:27:01 +08:00
|
|
|
MachineInstr::FrameSetup, false, NeedsWinCFI);
|
2016-05-07 00:34:59 +08:00
|
|
|
NumBytes = 0;
|
2017-08-02 05:13:54 +08:00
|
|
|
} else if (PrologueSaveSize != 0) {
|
2018-10-31 17:27:01 +08:00
|
|
|
MBBI = convertCalleeSaveRestoreToSPPrePostIncDec(
|
|
|
|
MBB, MBBI, DL, TII, -PrologueSaveSize, NeedsWinCFI);
|
2017-08-02 05:13:54 +08:00
|
|
|
NumBytes -= PrologueSaveSize;
|
2016-05-07 00:34:59 +08:00
|
|
|
}
|
|
|
|
assert(NumBytes >= 0 && "Negative stack allocation size!?");
|
2014-03-29 18:18:08 +08:00
|
|
|
|
2016-05-07 00:34:59 +08:00
|
|
|
// Move past the saves of the callee-saved registers, fixing up the offsets
|
|
|
|
// and pre-inc if we decided to combine the callee-save and local stack
|
|
|
|
// pointer bump above.
|
2016-02-02 00:29:19 +08:00
|
|
|
MachineBasicBlock::iterator End = MBB.end();
|
2016-05-07 00:34:59 +08:00
|
|
|
while (MBBI != End && MBBI->getFlag(MachineInstr::FrameSetup)) {
|
|
|
|
if (CombineSPBump)
|
2018-10-31 17:27:01 +08:00
|
|
|
fixupCalleeSaveRestoreStackOffset(*MBBI, AFI->getLocalStackSize(),
|
|
|
|
NeedsWinCFI);
|
2014-03-29 18:18:08 +08:00
|
|
|
++MBBI;
|
2016-05-07 00:34:59 +08:00
|
|
|
}
|
2018-10-31 17:27:01 +08:00
|
|
|
|
2018-11-10 07:33:30 +08:00
|
|
|
// The code below is not applicable to funclets. We have emitted all the SEH
|
|
|
|
// opcodes that we needed to emit. The FP and BP belong to the containing
|
|
|
|
// function.
|
|
|
|
if (IsFunclet) {
|
|
|
|
if (NeedsWinCFI)
|
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_PrologEnd))
|
|
|
|
.setMIFlag(MachineInstr::FrameSetup);
|
2019-01-17 08:24:38 +08:00
|
|
|
|
|
|
|
// SEH funclets are passed the frame pointer in X1. If the parent
|
|
|
|
// function uses the base register, then the base register is used
|
|
|
|
// directly, and is not retrieved from X1.
|
|
|
|
if (F.hasPersonalityFn()) {
|
|
|
|
EHPersonality Per = classifyEHPersonality(F.getPersonalityFn());
|
2019-01-18 04:24:14 +08:00
|
|
|
if (isAsynchronousEHPersonality(Per)) {
|
2019-01-17 08:24:38 +08:00
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::COPY), AArch64::FP)
|
|
|
|
.addReg(AArch64::X1).setMIFlag(MachineInstr::FrameSetup);
|
2019-01-18 04:24:14 +08:00
|
|
|
MBB.addLiveIn(AArch64::X1);
|
|
|
|
}
|
2019-01-17 08:24:38 +08:00
|
|
|
}
|
|
|
|
|
2018-11-10 07:33:30 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-29 18:18:08 +08:00
|
|
|
if (HasFP) {
|
2017-08-02 05:13:54 +08:00
|
|
|
// Only set up FP if we actually need to. Frame pointer is fp =
|
|
|
|
// sp - fixedobject - 16.
|
|
|
|
int FPOffset = AFI->getCalleeSavedStackSize() - 16;
|
2016-05-07 00:34:59 +08:00
|
|
|
if (CombineSPBump)
|
|
|
|
FPOffset += AFI->getLocalStackSize();
|
2016-03-15 02:24:34 +08:00
|
|
|
|
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,
|
2018-10-31 17:27:01 +08:00
|
|
|
MachineInstr::FrameSetup, false, NeedsWinCFI);
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
|
|
|
|
2017-12-20 14:51:45 +08:00
|
|
|
if (windowsRequiresStackProbe(MF, NumBytes)) {
|
|
|
|
uint32_t NumWords = NumBytes >> 4;
|
2018-10-31 17:27:01 +08:00
|
|
|
if (NeedsWinCFI) {
|
|
|
|
// alloc_l can hold at most 256MB, so assume that NumBytes doesn't
|
|
|
|
// exceed this amount. We need to move at most 2^24 - 1 into x15.
|
|
|
|
// This is at most two instructions, MOVZ follwed by MOVK.
|
|
|
|
// TODO: Fix to use multiple stack alloc unwind codes for stacks
|
|
|
|
// exceeding 256MB in size.
|
|
|
|
if (NumBytes >= (1 << 28))
|
|
|
|
report_fatal_error("Stack size cannot exceed 256MB for stack "
|
|
|
|
"unwinding purposes");
|
|
|
|
|
|
|
|
uint32_t LowNumWords = NumWords & 0xFFFF;
|
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(AArch64::MOVZXi), AArch64::X15)
|
|
|
|
.addImm(LowNumWords)
|
|
|
|
.addImm(AArch64_AM::getShifterImm(AArch64_AM::LSL, 0))
|
|
|
|
.setMIFlag(MachineInstr::FrameSetup);
|
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_Nop))
|
|
|
|
.setMIFlag(MachineInstr::FrameSetup);
|
|
|
|
if ((NumWords & 0xFFFF0000) != 0) {
|
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(AArch64::MOVKXi), AArch64::X15)
|
|
|
|
.addReg(AArch64::X15)
|
|
|
|
.addImm((NumWords & 0xFFFF0000) >> 16) // High half
|
|
|
|
.addImm(AArch64_AM::getShifterImm(AArch64_AM::LSL, 16))
|
|
|
|
.setMIFlag(MachineInstr::FrameSetup);
|
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_Nop))
|
|
|
|
.setMIFlag(MachineInstr::FrameSetup);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(AArch64::MOVi64imm), AArch64::X15)
|
|
|
|
.addImm(NumWords)
|
|
|
|
.setMIFlags(MachineInstr::FrameSetup);
|
|
|
|
}
|
2017-12-20 14:51:45 +08:00
|
|
|
|
|
|
|
switch (MF.getTarget().getCodeModel()) {
|
2018-08-22 19:31:39 +08:00
|
|
|
case CodeModel::Tiny:
|
2017-12-20 14:51:45 +08:00
|
|
|
case CodeModel::Small:
|
|
|
|
case CodeModel::Medium:
|
|
|
|
case CodeModel::Kernel:
|
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(AArch64::BL))
|
|
|
|
.addExternalSymbol("__chkstk")
|
|
|
|
.addReg(AArch64::X15, RegState::Implicit)
|
2018-10-31 16:14:09 +08:00
|
|
|
.addReg(AArch64::X16, RegState::Implicit | RegState::Define | RegState::Dead)
|
|
|
|
.addReg(AArch64::X17, RegState::Implicit | RegState::Define | RegState::Dead)
|
|
|
|
.addReg(AArch64::NZCV, RegState::Implicit | RegState::Define | RegState::Dead)
|
2017-12-20 14:51:45 +08:00
|
|
|
.setMIFlags(MachineInstr::FrameSetup);
|
2018-10-31 17:27:01 +08:00
|
|
|
if (NeedsWinCFI)
|
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_Nop))
|
|
|
|
.setMIFlag(MachineInstr::FrameSetup);
|
2017-12-20 14:51:45 +08:00
|
|
|
break;
|
|
|
|
case CodeModel::Large:
|
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(AArch64::MOVaddrEXT))
|
|
|
|
.addReg(AArch64::X16, RegState::Define)
|
|
|
|
.addExternalSymbol("__chkstk")
|
|
|
|
.addExternalSymbol("__chkstk")
|
|
|
|
.setMIFlags(MachineInstr::FrameSetup);
|
2018-10-31 17:27:01 +08:00
|
|
|
if (NeedsWinCFI)
|
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_Nop))
|
|
|
|
.setMIFlag(MachineInstr::FrameSetup);
|
2017-12-20 14:51:45 +08:00
|
|
|
|
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(AArch64::BLR))
|
|
|
|
.addReg(AArch64::X16, RegState::Kill)
|
|
|
|
.addReg(AArch64::X15, RegState::Implicit | RegState::Define)
|
2018-10-31 16:14:09 +08:00
|
|
|
.addReg(AArch64::X16, RegState::Implicit | RegState::Define | RegState::Dead)
|
|
|
|
.addReg(AArch64::X17, RegState::Implicit | RegState::Define | RegState::Dead)
|
|
|
|
.addReg(AArch64::NZCV, RegState::Implicit | RegState::Define | RegState::Dead)
|
2017-12-20 14:51:45 +08:00
|
|
|
.setMIFlags(MachineInstr::FrameSetup);
|
2018-10-31 17:27:01 +08:00
|
|
|
if (NeedsWinCFI)
|
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_Nop))
|
|
|
|
.setMIFlag(MachineInstr::FrameSetup);
|
2017-12-20 14:51:45 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(AArch64::SUBXrx64), AArch64::SP)
|
|
|
|
.addReg(AArch64::SP, RegState::Kill)
|
|
|
|
.addReg(AArch64::X15, RegState::Kill)
|
|
|
|
.addImm(AArch64_AM::getArithExtendImm(AArch64_AM::UXTX, 4))
|
|
|
|
.setMIFlags(MachineInstr::FrameSetup);
|
2018-10-31 17:27:01 +08:00
|
|
|
if (NeedsWinCFI)
|
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_StackAlloc))
|
|
|
|
.addImm(NumBytes)
|
|
|
|
.setMIFlag(MachineInstr::FrameSetup);
|
2017-12-20 14:51:45 +08:00
|
|
|
NumBytes = 0;
|
|
|
|
}
|
|
|
|
|
2014-03-29 18:18:08 +08:00
|
|
|
// 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,
|
2018-10-31 17:27:01 +08:00
|
|
|
MachineInstr::FrameSetup, false, NeedsWinCFI);
|
2015-04-09 16:49:47 +08:00
|
|
|
|
2016-03-15 02:24:34 +08:00
|
|
|
if (NeedsRealignment) {
|
2016-07-29 02:40:00 +08:00
|
|
|
const unsigned Alignment = MFI.getMaxAlignment();
|
2016-03-15 02:24:34 +08:00
|
|
|
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);
|
2018-10-31 17:27:01 +08:00
|
|
|
if (NeedsWinCFI)
|
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_StackAlloc))
|
|
|
|
.addImm(NumBytes & andMaskEncoded)
|
|
|
|
.setMIFlag(MachineInstr::FrameSetup);
|
2016-03-15 02:24:34 +08:00
|
|
|
}
|
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);
|
2018-10-31 17:27:01 +08:00
|
|
|
if (NeedsWinCFI)
|
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_Nop))
|
|
|
|
.setMIFlag(MachineInstr::FrameSetup);
|
2015-04-09 16:49:47 +08:00
|
|
|
}
|
2014-03-29 18:18:08 +08:00
|
|
|
|
2018-10-31 17:27:01 +08:00
|
|
|
// The very last FrameSetup instruction indicates the end of prologue. Emit a
|
|
|
|
// SEH opcode indicating the prologue end.
|
|
|
|
if (NeedsWinCFI)
|
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_PrologEnd))
|
|
|
|
.setMIFlag(MachineInstr::FrameSetup);
|
|
|
|
|
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);
|
2017-08-02 05:13:54 +08:00
|
|
|
unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createDefCfa(
|
|
|
|
nullptr, Reg, 2 * StackGrowth - FixedObject));
|
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
|
|
|
} else {
|
|
|
|
// Encode the stack size of the leaf function.
|
2016-12-01 07:48:42 +08:00
|
|
|
unsigned CFIIndex = MF.addFrameInst(
|
2016-07-29 02:40:00 +08:00
|
|
|
MCCFIInstruction::createDefCfaOffset(nullptr, -MFI.getStackSize()));
|
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-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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-17 20:53:22 +08:00
|
|
|
static void InsertReturnAddressAuth(MachineFunction &MF,
|
|
|
|
MachineBasicBlock &MBB) {
|
|
|
|
if (!ShouldSignReturnAddress(MF))
|
|
|
|
return;
|
|
|
|
const AArch64Subtarget &Subtarget = MF.getSubtarget<AArch64Subtarget>();
|
|
|
|
const TargetInstrInfo *TII = Subtarget.getInstrInfo();
|
|
|
|
|
|
|
|
MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
|
|
|
|
DebugLoc DL;
|
|
|
|
if (MBBI != MBB.end())
|
|
|
|
DL = MBBI->getDebugLoc();
|
|
|
|
|
|
|
|
// The AUTIASP instruction assembles to a hint instruction before v8.3a so
|
|
|
|
// this instruction can safely used for any v8a architecture.
|
|
|
|
// From v8.3a onwards there are optimised authenticate LR and return
|
|
|
|
// instructions, namely RETA{A,B}, that can be used instead.
|
|
|
|
if (Subtarget.hasV8_3aOps() && MBBI != MBB.end() &&
|
|
|
|
MBBI->getOpcode() == AArch64::RET_ReallyLR) {
|
2018-10-30 00:26:58 +08:00
|
|
|
BuildMI(MBB, MBBI, DL,
|
|
|
|
TII->get(ShouldSignWithAKey(MF) ? AArch64::RETAA : AArch64::RETAB))
|
|
|
|
.copyImplicitOps(*MBBI);
|
2018-08-17 20:53:22 +08:00
|
|
|
MBB.erase(MBBI);
|
|
|
|
} else {
|
2018-10-30 00:26:58 +08:00
|
|
|
BuildMI(
|
|
|
|
MBB, MBBI, DL,
|
|
|
|
TII->get(ShouldSignWithAKey(MF) ? AArch64::AUTIASP : AArch64::AUTIBSP))
|
2018-08-17 20:53:22 +08:00
|
|
|
.setMIFlag(MachineInstr::FrameDestroy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-10 07:33:30 +08:00
|
|
|
static bool isFuncletReturnInstr(const MachineInstr &MI) {
|
|
|
|
switch (MI.getOpcode()) {
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
case AArch64::CATCHRET:
|
|
|
|
case AArch64::CLEANUPRET:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
2016-07-29 02:40:00 +08:00
|
|
|
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;
|
2018-11-10 07:33:30 +08:00
|
|
|
bool NeedsWinCFI = needsWinCFI(MF);
|
|
|
|
bool IsFunclet = false;
|
2018-10-31 17:27:01 +08:00
|
|
|
|
[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 (MBB.end() != MBBI) {
|
|
|
|
DL = MBBI->getDebugLoc();
|
|
|
|
unsigned RetOpcode = MBBI->getOpcode();
|
|
|
|
IsTailCallReturn = RetOpcode == AArch64::TCRETURNdi ||
|
[AArch64][v8.5A] Restrict indirect tail calls to use x16/17 only when using BTI
When branch target identification is enabled, all indirectly-callable
functions start with a BTI C instruction. this instruction can only be
the target of certain indirect branches (direct branches and
fall-through are not affected):
- A BLR instruction, in either a protected or unprotected page.
- A BR instruction in a protected page, using x16 or x17.
- A BR instruction in an unprotected page, using any register.
Without BTI, we can use any non call-preserved register to hold the
address for an indirect tail call. However, when BTI is enabled, then
the code being compiled might be loaded into a BTI-protected page, where
only x16 and x17 can be used for indirect tail calls.
Legacy code withiout this restriction can still indirectly tail-call
BTI-protected functions, because they will be loaded into an unprotected
page, so any register is allowed.
Differential revision: https://reviews.llvm.org/D52868
llvm-svn: 343968
2018-10-08 22:09:15 +08:00
|
|
|
RetOpcode == AArch64::TCRETURNri ||
|
|
|
|
RetOpcode == AArch64::TCRETURNriBTI;
|
2018-11-10 07:33:30 +08:00
|
|
|
IsFunclet = isFuncletReturnInstr(*MBBI);
|
[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
|
|
|
}
|
2018-10-31 17:27:01 +08:00
|
|
|
|
2018-11-10 07:33:30 +08:00
|
|
|
int NumBytes = IsFunclet ? (int)getWinEHFuncletFrameSize(MF)
|
|
|
|
: MFI.getStackSize();
|
2018-10-31 17:27:01 +08:00
|
|
|
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.
|
2017-12-16 06:22:58 +08:00
|
|
|
if (MF.getFunction().getCallingConv() == CallingConv::GHC)
|
2015-01-20 01:40:05 +08:00
|
|
|
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
|
|
|
|
2018-08-17 20:53:22 +08:00
|
|
|
auto Cleanup = make_scope_exit([&] { InsertReturnAddressAuth(MF, MBB); });
|
|
|
|
|
2017-08-02 05:13:54 +08:00
|
|
|
bool IsWin64 =
|
2017-12-16 06:22:58 +08:00
|
|
|
Subtarget.isCallingConvWin64(MF.getFunction().getCallingConv());
|
2018-12-04 08:54:52 +08:00
|
|
|
// Var args are accounted for in the containing function, so don't
|
|
|
|
// include them for funclets.
|
|
|
|
unsigned FixedObject =
|
|
|
|
(IsWin64 && !IsFunclet) ? alignTo(AFI->getVarArgsGPRSize(), 16) : 0;
|
2017-08-02 05:13:54 +08:00
|
|
|
|
[AArch64] Emit CSR loads in the same order as stores
Optionally allow the order of restoring the callee-saved registers in the
epilogue to be reversed.
The flag -reverse-csr-restore-seq generates the following code:
```
stp x26, x25, [sp, #-64]!
stp x24, x23, [sp, #16]
stp x22, x21, [sp, #32]
stp x20, x19, [sp, #48]
; [..]
ldp x24, x23, [sp, #16]
ldp x22, x21, [sp, #32]
ldp x20, x19, [sp, #48]
ldp x26, x25, [sp], #64
ret
```
Note how the CSRs are restored in the same order as they are saved.
One exception to this rule is the last `ldp`, which allows us to merge
the stack adjustment and the ldp into a post-index ldp. This is done by
first generating:
ldp x26, x27, [sp]
add sp, sp, #64
which gets merged by the arm64 load store optimizer into
ldp x26, x25, [sp], #64
The flag is disabled by default.
llvm-svn: 327569
2018-03-15 04:34:03 +08:00
|
|
|
uint64_t AfterCSRPopSize = ArgumentPopSize;
|
2017-08-02 05:13:54 +08:00
|
|
|
auto PrologueSaveSize = AFI->getCalleeSavedStackSize() + FixedObject;
|
2018-12-04 08:54:52 +08:00
|
|
|
// We cannot rely on the local stack size set in emitPrologue if the function
|
|
|
|
// has funclets, as funclets have different local stack size requirements, and
|
|
|
|
// the current value set in emitPrologue may be that of the containing
|
|
|
|
// function.
|
2018-11-10 07:33:30 +08:00
|
|
|
if (MF.hasEHFunclets())
|
|
|
|
AFI->setLocalStackSize(NumBytes - PrologueSaveSize);
|
2016-05-07 00:34:59 +08:00
|
|
|
bool CombineSPBump = shouldCombineCSRLocalStackBump(MF, NumBytes);
|
[AArch64] Emit CSR loads in the same order as stores
Optionally allow the order of restoring the callee-saved registers in the
epilogue to be reversed.
The flag -reverse-csr-restore-seq generates the following code:
```
stp x26, x25, [sp, #-64]!
stp x24, x23, [sp, #16]
stp x22, x21, [sp, #32]
stp x20, x19, [sp, #48]
; [..]
ldp x24, x23, [sp, #16]
ldp x22, x21, [sp, #32]
ldp x20, x19, [sp, #48]
ldp x26, x25, [sp], #64
ret
```
Note how the CSRs are restored in the same order as they are saved.
One exception to this rule is the last `ldp`, which allows us to merge
the stack adjustment and the ldp into a post-index ldp. This is done by
first generating:
ldp x26, x27, [sp]
add sp, sp, #64
which gets merged by the arm64 load store optimizer into
ldp x26, x25, [sp], #64
The flag is disabled by default.
llvm-svn: 327569
2018-03-15 04:34:03 +08:00
|
|
|
// Assume we can't combine the last pop with the sp restore.
|
|
|
|
|
|
|
|
if (!CombineSPBump && PrologueSaveSize != 0) {
|
|
|
|
MachineBasicBlock::iterator Pop = std::prev(MBB.getFirstTerminator());
|
2018-10-31 17:27:01 +08:00
|
|
|
while (AArch64InstrInfo::isSEHInstruction(*Pop))
|
|
|
|
Pop = std::prev(Pop);
|
[AArch64] Emit CSR loads in the same order as stores
Optionally allow the order of restoring the callee-saved registers in the
epilogue to be reversed.
The flag -reverse-csr-restore-seq generates the following code:
```
stp x26, x25, [sp, #-64]!
stp x24, x23, [sp, #16]
stp x22, x21, [sp, #32]
stp x20, x19, [sp, #48]
; [..]
ldp x24, x23, [sp, #16]
ldp x22, x21, [sp, #32]
ldp x20, x19, [sp, #48]
ldp x26, x25, [sp], #64
ret
```
Note how the CSRs are restored in the same order as they are saved.
One exception to this rule is the last `ldp`, which allows us to merge
the stack adjustment and the ldp into a post-index ldp. This is done by
first generating:
ldp x26, x27, [sp]
add sp, sp, #64
which gets merged by the arm64 load store optimizer into
ldp x26, x25, [sp], #64
The flag is disabled by default.
llvm-svn: 327569
2018-03-15 04:34:03 +08:00
|
|
|
// Converting the last ldp to a post-index ldp is valid only if the last
|
|
|
|
// ldp's offset is 0.
|
|
|
|
const MachineOperand &OffsetOp = Pop->getOperand(Pop->getNumOperands() - 1);
|
|
|
|
// If the offset is 0, convert it to a post-index ldp.
|
2018-10-31 17:27:01 +08:00
|
|
|
if (OffsetOp.getImm() == 0)
|
|
|
|
convertCalleeSaveRestoreToSPPrePostIncDec(
|
|
|
|
MBB, Pop, DL, TII, PrologueSaveSize, NeedsWinCFI, false);
|
|
|
|
else {
|
[AArch64] Emit CSR loads in the same order as stores
Optionally allow the order of restoring the callee-saved registers in the
epilogue to be reversed.
The flag -reverse-csr-restore-seq generates the following code:
```
stp x26, x25, [sp, #-64]!
stp x24, x23, [sp, #16]
stp x22, x21, [sp, #32]
stp x20, x19, [sp, #48]
; [..]
ldp x24, x23, [sp, #16]
ldp x22, x21, [sp, #32]
ldp x20, x19, [sp, #48]
ldp x26, x25, [sp], #64
ret
```
Note how the CSRs are restored in the same order as they are saved.
One exception to this rule is the last `ldp`, which allows us to merge
the stack adjustment and the ldp into a post-index ldp. This is done by
first generating:
ldp x26, x27, [sp]
add sp, sp, #64
which gets merged by the arm64 load store optimizer into
ldp x26, x25, [sp], #64
The flag is disabled by default.
llvm-svn: 327569
2018-03-15 04:34:03 +08:00
|
|
|
// If not, make sure to emit an add after the last ldp.
|
|
|
|
// We're doing this by transfering the size to be restored from the
|
|
|
|
// adjustment *before* the CSR pops to the adjustment *after* the CSR
|
|
|
|
// pops.
|
|
|
|
AfterCSRPopSize += PrologueSaveSize;
|
|
|
|
}
|
|
|
|
}
|
2016-05-07 00:34:59 +08:00
|
|
|
|
2014-03-29 18:18:08 +08:00
|
|
|
// Move past the restores of the callee-saved registers.
|
[AArch64] Emit CSR loads in the same order as stores
Optionally allow the order of restoring the callee-saved registers in the
epilogue to be reversed.
The flag -reverse-csr-restore-seq generates the following code:
```
stp x26, x25, [sp, #-64]!
stp x24, x23, [sp, #16]
stp x22, x21, [sp, #32]
stp x20, x19, [sp, #48]
; [..]
ldp x24, x23, [sp, #16]
ldp x22, x21, [sp, #32]
ldp x20, x19, [sp, #48]
ldp x26, x25, [sp], #64
ret
```
Note how the CSRs are restored in the same order as they are saved.
One exception to this rule is the last `ldp`, which allows us to merge
the stack adjustment and the ldp into a post-index ldp. This is done by
first generating:
ldp x26, x27, [sp]
add sp, sp, #64
which gets merged by the arm64 load store optimizer into
ldp x26, x25, [sp], #64
The flag is disabled by default.
llvm-svn: 327569
2018-03-15 04:34:03 +08:00
|
|
|
// If we plan on combining the sp bump of the local stack size and the callee
|
|
|
|
// save stack size, we might need to adjust the CSR save and restore offsets.
|
[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;
|
2016-05-07 00:34:59 +08:00
|
|
|
} else if (CombineSPBump)
|
2018-10-31 17:27:01 +08:00
|
|
|
fixupCalleeSaveRestoreStackOffset(*LastPopI, AFI->getLocalStackSize(),
|
|
|
|
NeedsWinCFI);
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
2016-05-07 00:34:59 +08:00
|
|
|
|
2018-10-31 17:27:01 +08:00
|
|
|
if (NeedsWinCFI)
|
|
|
|
BuildMI(MBB, LastPopI, DL, TII->get(AArch64::SEH_EpilogStart))
|
|
|
|
.setMIFlag(MachineInstr::FrameDestroy);
|
|
|
|
|
2016-05-07 00:34:59 +08:00
|
|
|
// If there is a single SP update, insert it before the ret and we're done.
|
|
|
|
if (CombineSPBump) {
|
|
|
|
emitFrameOffset(MBB, MBB.getFirstTerminator(), DL, AArch64::SP, AArch64::SP,
|
2018-10-31 17:27:01 +08:00
|
|
|
NumBytes + AfterCSRPopSize, TII, MachineInstr::FrameDestroy,
|
|
|
|
false, NeedsWinCFI);
|
|
|
|
if (NeedsWinCFI)
|
|
|
|
BuildMI(MBB, MBB.getFirstTerminator(), DL,
|
|
|
|
TII->get(AArch64::SEH_EpilogEnd))
|
|
|
|
.setMIFlag(MachineInstr::FrameDestroy);
|
2016-05-07 00:34:59 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-02 05:13:54 +08:00
|
|
|
NumBytes -= PrologueSaveSize;
|
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).
|
[AArch64] Emit CSR loads in the same order as stores
Optionally allow the order of restoring the callee-saved registers in the
epilogue to be reversed.
The flag -reverse-csr-restore-seq generates the following code:
```
stp x26, x25, [sp, #-64]!
stp x24, x23, [sp, #16]
stp x22, x21, [sp, #32]
stp x20, x19, [sp, #48]
; [..]
ldp x24, x23, [sp, #16]
ldp x22, x21, [sp, #32]
ldp x20, x19, [sp, #48]
ldp x26, x25, [sp], #64
ret
```
Note how the CSRs are restored in the same order as they are saved.
One exception to this rule is the last `ldp`, which allows us to merge
the stack adjustment and the ldp into a post-index ldp. This is done by
first generating:
ldp x26, x27, [sp]
add sp, sp, #64
which gets merged by the arm64 load store optimizer into
ldp x26, x25, [sp], #64
The flag is disabled by default.
llvm-svn: 327569
2018-03-15 04:34:03 +08:00
|
|
|
if (RedZone && AfterCSRPopSize == 0)
|
2016-02-24 00:54:36 +08:00
|
|
|
return;
|
|
|
|
|
2017-08-02 05:13:54 +08:00
|
|
|
bool NoCalleeSaveRestore = PrologueSaveSize == 0;
|
2016-02-24 00:54:36 +08:00
|
|
|
int StackRestoreBytes = RedZone ? 0 : NumBytes;
|
|
|
|
if (NoCalleeSaveRestore)
|
[AArch64] Emit CSR loads in the same order as stores
Optionally allow the order of restoring the callee-saved registers in the
epilogue to be reversed.
The flag -reverse-csr-restore-seq generates the following code:
```
stp x26, x25, [sp, #-64]!
stp x24, x23, [sp, #16]
stp x22, x21, [sp, #32]
stp x20, x19, [sp, #48]
; [..]
ldp x24, x23, [sp, #16]
ldp x22, x21, [sp, #32]
ldp x20, x19, [sp, #48]
ldp x26, x25, [sp], #64
ret
```
Note how the CSRs are restored in the same order as they are saved.
One exception to this rule is the last `ldp`, which allows us to merge
the stack adjustment and the ldp into a post-index ldp. This is done by
first generating:
ldp x26, x27, [sp]
add sp, sp, #64
which gets merged by the arm64 load store optimizer into
ldp x26, x25, [sp], #64
The flag is disabled by default.
llvm-svn: 327569
2018-03-15 04:34:03 +08:00
|
|
|
StackRestoreBytes += AfterCSRPopSize;
|
2018-04-27 23:30:54 +08:00
|
|
|
|
2016-02-24 00:54:36 +08:00
|
|
|
// If we were able to combine the local stack pop with the argument pop,
|
|
|
|
// then we're done.
|
2018-04-27 23:30:54 +08:00
|
|
|
bool Done = NoCalleeSaveRestore || AfterCSRPopSize == 0;
|
|
|
|
|
|
|
|
// If we're done after this, make sure to help the load store optimizer.
|
|
|
|
if (Done)
|
|
|
|
adaptForLdStOpt(MBB, MBB.getFirstTerminator(), LastPopI);
|
|
|
|
|
|
|
|
emitFrameOffset(MBB, LastPopI, DL, AArch64::SP, AArch64::SP,
|
2018-10-31 17:27:01 +08:00
|
|
|
StackRestoreBytes, TII, MachineInstr::FrameDestroy, false,
|
|
|
|
NeedsWinCFI);
|
|
|
|
if (Done) {
|
|
|
|
if (NeedsWinCFI)
|
|
|
|
BuildMI(MBB, MBB.getFirstTerminator(), DL,
|
|
|
|
TII->get(AArch64::SEH_EpilogEnd))
|
|
|
|
.setMIFlag(MachineInstr::FrameDestroy);
|
2016-02-24 00:54:36 +08:00
|
|
|
return;
|
2018-10-31 17:27:01 +08:00
|
|
|
}
|
2018-04-27 23:30:54 +08:00
|
|
|
|
2016-02-24 00:54:36 +08:00
|
|
|
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.
|
2018-11-10 07:33:30 +08:00
|
|
|
if (!IsFunclet && (MFI.hasVarSizedObjects() || AFI->isStackRealigned()))
|
2014-05-24 20:50:23 +08:00
|
|
|
emitFrameOffset(MBB, LastPopI, DL, AArch64::SP, AArch64::FP,
|
2017-08-02 05:13:54 +08:00
|
|
|
-AFI->getCalleeSavedStackSize() + 16, TII,
|
2018-10-31 17:27:01 +08:00
|
|
|
MachineInstr::FrameDestroy, false, NeedsWinCFI);
|
2016-03-15 02:17:41 +08:00
|
|
|
else if (NumBytes)
|
|
|
|
emitFrameOffset(MBB, LastPopI, DL, AArch64::SP, AArch64::SP, NumBytes, TII,
|
2018-10-31 17:27:01 +08:00
|
|
|
MachineInstr::FrameDestroy, false, NeedsWinCFI);
|
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.
|
[AArch64] Emit CSR loads in the same order as stores
Optionally allow the order of restoring the callee-saved registers in the
epilogue to be reversed.
The flag -reverse-csr-restore-seq generates the following code:
```
stp x26, x25, [sp, #-64]!
stp x24, x23, [sp, #16]
stp x22, x21, [sp, #32]
stp x20, x19, [sp, #48]
; [..]
ldp x24, x23, [sp, #16]
ldp x22, x21, [sp, #32]
ldp x20, x19, [sp, #48]
ldp x26, x25, [sp], #64
ret
```
Note how the CSRs are restored in the same order as they are saved.
One exception to this rule is the last `ldp`, which allows us to merge
the stack adjustment and the ldp into a post-index ldp. This is done by
first generating:
ldp x26, x27, [sp]
add sp, sp, #64
which gets merged by the arm64 load store optimizer into
ldp x26, x25, [sp], #64
The flag is disabled by default.
llvm-svn: 327569
2018-03-15 04:34:03 +08:00
|
|
|
if (AfterCSRPopSize) {
|
2018-04-05 05:55:44 +08:00
|
|
|
// Find an insertion point for the first ldp so that it goes before the
|
|
|
|
// shadow call stack epilog instruction. This ensures that the restore of
|
|
|
|
// lr from x18 is placed after the restore from sp.
|
|
|
|
auto FirstSPPopI = MBB.getFirstTerminator();
|
|
|
|
while (FirstSPPopI != Begin) {
|
|
|
|
auto Prev = std::prev(FirstSPPopI);
|
|
|
|
if (Prev->getOpcode() != AArch64::LDRXpre ||
|
|
|
|
Prev->getOperand(0).getReg() == AArch64::SP)
|
|
|
|
break;
|
|
|
|
FirstSPPopI = Prev;
|
|
|
|
}
|
|
|
|
|
2018-04-27 23:30:54 +08:00
|
|
|
adaptForLdStOpt(MBB, FirstSPPopI, LastPopI);
|
|
|
|
|
2018-04-05 05:55:44 +08:00
|
|
|
emitFrameOffset(MBB, FirstSPPopI, DL, AArch64::SP, AArch64::SP,
|
2018-10-31 17:27:01 +08:00
|
|
|
AfterCSRPopSize, TII, MachineInstr::FrameDestroy, false,
|
|
|
|
NeedsWinCFI);
|
[AArch64] Emit CSR loads in the same order as stores
Optionally allow the order of restoring the callee-saved registers in the
epilogue to be reversed.
The flag -reverse-csr-restore-seq generates the following code:
```
stp x26, x25, [sp, #-64]!
stp x24, x23, [sp, #16]
stp x22, x21, [sp, #32]
stp x20, x19, [sp, #48]
; [..]
ldp x24, x23, [sp, #16]
ldp x22, x21, [sp, #32]
ldp x20, x19, [sp, #48]
ldp x26, x25, [sp], #64
ret
```
Note how the CSRs are restored in the same order as they are saved.
One exception to this rule is the last `ldp`, which allows us to merge
the stack adjustment and the ldp into a post-index ldp. This is done by
first generating:
ldp x26, x27, [sp]
add sp, sp, #64
which gets merged by the arm64 load store optimizer into
ldp x26, x25, [sp], #64
The flag is disabled by default.
llvm-svn: 327569
2018-03-15 04:34:03 +08:00
|
|
|
}
|
2018-10-31 17:27:01 +08:00
|
|
|
if (NeedsWinCFI)
|
|
|
|
BuildMI(MBB, MBB.getFirstTerminator(), DL, TII->get(AArch64::SEH_EpilogEnd))
|
|
|
|
.setMIFlag(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 {
|
2016-07-29 02:40:00 +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>();
|
2017-08-02 05:13:54 +08:00
|
|
|
const AArch64Subtarget &Subtarget = MF.getSubtarget<AArch64Subtarget>();
|
|
|
|
bool IsWin64 =
|
2017-12-16 06:22:58 +08:00
|
|
|
Subtarget.isCallingConvWin64(MF.getFunction().getCallingConv());
|
2017-08-02 05:13:54 +08:00
|
|
|
unsigned FixedObject = IsWin64 ? alignTo(AFI->getVarArgsGPRSize(), 16) : 0;
|
|
|
|
int FPOffset = MFI.getObjectOffset(FI) + FixedObject + 16;
|
2016-07-29 02:40:00 +08:00
|
|
|
int Offset = MFI.getObjectOffset(FI) + MFI.getStackSize();
|
|
|
|
bool isFixed = MFI.isFixedObjectIndex(FI);
|
2018-04-27 02:50:45 +08:00
|
|
|
bool isCSR = !isFixed && MFI.getObjectOffset(FI) >=
|
|
|
|
-((int)AFI->getCalleeSavedStackSize());
|
2014-03-29 18:18:08 +08:00
|
|
|
|
|
|
|
// 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);
|
2018-04-27 02:50:45 +08:00
|
|
|
} else if (isCSR && RegInfo->needsStackRealignment(MF)) {
|
|
|
|
// References to the CSR area must use FP if we're re-aligning the stack
|
|
|
|
// since the dynamically-sized alignment padding is between the SP/BP and
|
|
|
|
// the CSR area.
|
|
|
|
assert(hasFP(MF) && "Re-aligned stack must have frame pointer");
|
|
|
|
UseFP = true;
|
2018-04-10 19:29:40 +08:00
|
|
|
} else if (hasFP(MF) && !RegInfo->needsStackRealignment(MF)) {
|
2014-03-29 18:18:08 +08:00
|
|
|
// If the FPOffset is negative, we have to keep in mind that the
|
|
|
|
// available offset range for negative offsets is smaller than for
|
2018-04-10 19:29:40 +08:00
|
|
|
// positive ones. If an offset is
|
2014-03-29 18:18:08 +08:00
|
|
|
// available via the FP and the SP, use whichever is closest.
|
2018-04-10 19:29:40 +08:00
|
|
|
bool FPOffsetFits = FPOffset >= -256;
|
|
|
|
PreferFP |= Offset > -FPOffset;
|
|
|
|
|
|
|
|
if (MFI.hasVarSizedObjects()) {
|
|
|
|
// If we have variable sized objects, we can use either FP or BP, as the
|
|
|
|
// SP offset is unknown. We can use the base pointer if we have one and
|
|
|
|
// FP is not preferred. If not, we're stuck with using FP.
|
|
|
|
bool CanUseBP = RegInfo->hasBasePointer(MF);
|
|
|
|
if (FPOffsetFits && CanUseBP) // Both are ok. Pick the best.
|
|
|
|
UseFP = PreferFP;
|
|
|
|
else if (!CanUseBP) // Can't use BP. Forced to use FP.
|
|
|
|
UseFP = true;
|
|
|
|
// else we can use BP and FP, but the offset from FP won't fit.
|
|
|
|
// That will make us scavenge registers which we can probably avoid by
|
|
|
|
// using BP. If it won't fit for BP either, we'll scavenge anyway.
|
2018-04-11 20:36:55 +08:00
|
|
|
} else if (FPOffset >= 0) {
|
2018-04-10 19:29:40 +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.
|
2014-03-29 18:18:08 +08:00
|
|
|
UseFP = true;
|
2018-11-10 07:33:30 +08:00
|
|
|
} else if (MF.hasEHFunclets() && !RegInfo->hasBasePointer(MF)) {
|
|
|
|
// Funclets access the locals contained in the parent's stack frame
|
|
|
|
// via the frame pointer, so we have to use the FP in the parent
|
|
|
|
// function.
|
|
|
|
assert(
|
|
|
|
Subtarget.isCallingConvWin64(MF.getFunction().getCallingConv()) &&
|
|
|
|
"Funclets should only be present on Win64");
|
|
|
|
UseFP = true;
|
2018-04-10 19:29:40 +08:00
|
|
|
} else {
|
|
|
|
// We have the choice between FP and (SP or BP).
|
|
|
|
if (FPOffsetFits && PreferFP) // If FP is the best fit, use it.
|
|
|
|
UseFP = true;
|
|
|
|
}
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-27 02:50:45 +08:00
|
|
|
assert(((isFixed || isCSR) || !RegInfo->needsStackRealignment(MF) || !UseFP) &&
|
2015-04-09 16:49:47 +08:00
|
|
|
"In the presence of dynamic stack pointer realignment, "
|
2018-04-27 02:50:45 +08:00
|
|
|
"non-argument/CSR objects cannot be accessed through the frame pointer");
|
2015-04-09 16:49:47 +08:00
|
|
|
|
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 {
|
2018-04-10 19:29:40 +08:00
|
|
|
assert(!MFI.hasVarSizedObjects() &&
|
|
|
|
"Can't use SP when we have var sized objects.");
|
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) {
|
2016-04-14 05:43:16 +08:00
|
|
|
// Do not set a kill flag on values that are also marked as live-in. This
|
|
|
|
// happens with the @llvm-returnaddress intrinsic and with arguments passed in
|
|
|
|
// callee saved registers.
|
|
|
|
// Omitting the kill flags is conservatively correct even if the live-in
|
|
|
|
// is not used after all.
|
|
|
|
bool IsLiveIn = MF.getRegInfo().isLiveIn(Reg);
|
|
|
|
return getKillRegState(!IsLiveIn);
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
|
|
|
|
2016-04-12 05:08:06 +08:00
|
|
|
static bool produceCompactUnwindFrame(MachineFunction &MF) {
|
|
|
|
const AArch64Subtarget &Subtarget = MF.getSubtarget<AArch64Subtarget>();
|
2017-12-16 06:22:58 +08:00
|
|
|
AttributeList Attrs = MF.getFunction().getAttributes();
|
2016-04-12 05:08:06 +08:00
|
|
|
return Subtarget.isTargetMachO() &&
|
|
|
|
!(Subtarget.getTargetLowering()->supportSwiftError() &&
|
|
|
|
Attrs.hasAttrSomewhere(Attribute::SwiftError));
|
|
|
|
}
|
|
|
|
|
2018-10-31 17:27:01 +08:00
|
|
|
static bool invalidateWindowsRegisterPairing(unsigned Reg1, unsigned Reg2,
|
|
|
|
bool NeedsWinCFI) {
|
|
|
|
// If we are generating register pairs for a Windows function that requires
|
|
|
|
// EH support, then pair consecutive registers only. There are no unwind
|
|
|
|
// opcodes for saves/restores of non-consectuve register pairs.
|
|
|
|
// The unwind opcodes are save_regp, save_regp_x, save_fregp, save_frepg_x.
|
|
|
|
// https://docs.microsoft.com/en-us/cpp/build/arm64-exception-handling
|
|
|
|
|
|
|
|
// TODO: LR can be paired with any register. We don't support this yet in
|
|
|
|
// the MCLayer. We need to add support for the save_lrpair unwind code.
|
|
|
|
if (!NeedsWinCFI)
|
|
|
|
return false;
|
|
|
|
if (Reg2 == Reg1 + 1)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-06 19:13:10 +08:00
|
|
|
namespace {
|
2017-01-25 08:29:26 +08:00
|
|
|
|
2016-02-02 03:07:06 +08:00
|
|
|
struct RegPairInfo {
|
2017-01-25 08:29:26 +08:00
|
|
|
unsigned Reg1 = AArch64::NoRegister;
|
|
|
|
unsigned Reg2 = AArch64::NoRegister;
|
2016-02-02 03:07:06 +08:00
|
|
|
int FrameIdx;
|
|
|
|
int Offset;
|
2018-09-12 20:10:22 +08:00
|
|
|
enum RegType { GPR, FPR64, FPR128 } Type;
|
2017-01-25 08:29:26 +08:00
|
|
|
|
|
|
|
RegPairInfo() = default;
|
|
|
|
|
2016-02-13 00:31:41 +08:00
|
|
|
bool isPaired() const { return Reg2 != AArch64::NoRegister; }
|
2016-02-02 03:07:06 +08:00
|
|
|
};
|
2017-01-25 08:29:26 +08:00
|
|
|
|
2016-08-06 19:13:10 +08:00
|
|
|
} // end anonymous namespace
|
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,
|
2018-04-05 05:55:44 +08:00
|
|
|
const TargetRegisterInfo *TRI, SmallVectorImpl<RegPairInfo> &RegPairs,
|
|
|
|
bool &NeedShadowCallStackProlog) {
|
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
|
|
|
|
2018-11-10 07:33:30 +08:00
|
|
|
bool NeedsWinCFI = needsWinCFI(MF);
|
2016-02-13 00:31:41 +08:00
|
|
|
AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
|
2016-07-29 02:40:00 +08:00
|
|
|
MachineFrameInfo &MFI = MF.getFrameInfo();
|
2017-12-16 06:22:58 +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.
|
2016-04-12 05:08:06 +08:00
|
|
|
assert((!produceCompactUnwindFrame(MF) ||
|
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!");
|
2017-07-14 01:03:12 +08:00
|
|
|
int Offset = AFI->getCalleeSavedStackSize();
|
2018-10-31 17:27:01 +08:00
|
|
|
// On Linux, we will have either one or zero non-paired register. On Windows
|
|
|
|
// with CFI, we can have multiple unpaired registers in order to utilize the
|
|
|
|
// available unwind codes. This flag assures that the alignment fixup is done
|
|
|
|
// only once, as intened.
|
|
|
|
bool FixupDone = false;
|
2016-02-13 00:31:41 +08:00
|
|
|
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();
|
|
|
|
|
2018-09-12 17:44:46 +08:00
|
|
|
if (AArch64::GPR64RegClass.contains(RPI.Reg1))
|
|
|
|
RPI.Type = RegPairInfo::GPR;
|
|
|
|
else if (AArch64::FPR64RegClass.contains(RPI.Reg1))
|
|
|
|
RPI.Type = RegPairInfo::FPR64;
|
2018-09-12 20:10:22 +08:00
|
|
|
else if (AArch64::FPR128RegClass.contains(RPI.Reg1))
|
|
|
|
RPI.Type = RegPairInfo::FPR128;
|
2018-09-12 17:44:46 +08:00
|
|
|
else
|
|
|
|
llvm_unreachable("Unsupported register class.");
|
2016-02-13 00:31:41 +08:00
|
|
|
|
|
|
|
// 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();
|
2018-09-12 17:44:46 +08:00
|
|
|
switch (RPI.Type) {
|
|
|
|
case RegPairInfo::GPR:
|
2018-10-31 17:27:01 +08:00
|
|
|
if (AArch64::GPR64RegClass.contains(NextReg) &&
|
|
|
|
!invalidateWindowsRegisterPairing(RPI.Reg1, NextReg, NeedsWinCFI))
|
2018-09-12 17:44:46 +08:00
|
|
|
RPI.Reg2 = NextReg;
|
|
|
|
break;
|
|
|
|
case RegPairInfo::FPR64:
|
2018-10-31 17:27:01 +08:00
|
|
|
if (AArch64::FPR64RegClass.contains(NextReg) &&
|
|
|
|
!invalidateWindowsRegisterPairing(RPI.Reg1, NextReg, NeedsWinCFI))
|
2018-09-12 17:44:46 +08:00
|
|
|
RPI.Reg2 = NextReg;
|
|
|
|
break;
|
2018-09-12 20:10:22 +08:00
|
|
|
case RegPairInfo::FPR128:
|
|
|
|
if (AArch64::FPR128RegClass.contains(NextReg))
|
|
|
|
RPI.Reg2 = NextReg;
|
|
|
|
break;
|
2018-09-12 17:44:46 +08:00
|
|
|
}
|
2016-02-13 00:31:41 +08:00
|
|
|
}
|
2016-02-02 03:07:06 +08:00
|
|
|
|
2018-04-05 05:55:44 +08:00
|
|
|
// If either of the registers to be saved is the lr register, it means that
|
|
|
|
// we also need to save lr in the shadow call stack.
|
|
|
|
if ((RPI.Reg1 == AArch64::LR || RPI.Reg2 == AArch64::LR) &&
|
|
|
|
MF.getFunction().hasFnAttribute(Attribute::ShadowCallStack)) {
|
[AArch64] Support reserving x1-7 registers.
Summary:
Reserving registers x1-7 is used to support CONFIG_ARM64_LSE_ATOMICS in Linux kernel. This change adds support for reserving registers x1 through x7.
Reviewers: javed.absar, phosek, srhines, nickdesaulniers, efriedma
Reviewed By: nickdesaulniers, efriedma
Subscribers: niravd, jfb, manojgupta, nickdesaulniers, jyknight, efriedma, kristof.beyls, llvm-commits
Differential Revision: https://reviews.llvm.org/D48580
llvm-svn: 341706
2018-09-08 04:58:57 +08:00
|
|
|
if (!MF.getSubtarget<AArch64Subtarget>().isXRegisterReserved(18))
|
2018-04-05 05:55:44 +08:00
|
|
|
report_fatal_error("Must reserve x18 to use shadow call stack");
|
|
|
|
NeedShadowCallStackProlog = true;
|
|
|
|
}
|
|
|
|
|
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.
|
2016-04-12 05:08:06 +08:00
|
|
|
assert((!produceCompactUnwindFrame(MF) ||
|
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();
|
|
|
|
|
2018-09-12 20:10:22 +08:00
|
|
|
int Scale = RPI.Type == RegPairInfo::FPR128 ? 16 : 8;
|
|
|
|
Offset -= RPI.isPaired() ? 2 * Scale : Scale;
|
|
|
|
|
|
|
|
// Round up size of non-pair to pair size if we need to pad the
|
|
|
|
// callee-save area to ensure 16-byte alignment.
|
2018-10-31 17:27:01 +08:00
|
|
|
if (AFI->hasCalleeSaveStackFreeSpace() && !FixupDone &&
|
2018-09-12 20:10:22 +08:00
|
|
|
RPI.Type != RegPairInfo::FPR128 && !RPI.isPaired()) {
|
2018-10-31 17:27:01 +08:00
|
|
|
FixupDone = true;
|
2018-09-12 20:10:22 +08:00
|
|
|
Offset -= 8;
|
|
|
|
assert(Offset % 16 == 0);
|
2016-07-29 02:40:00 +08:00
|
|
|
assert(MFI.getObjectAlignment(RPI.FrameIdx) <= 16);
|
|
|
|
MFI.setObjectAlignment(RPI.FrameIdx, 16);
|
2018-09-12 20:10:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(Offset % Scale == 0);
|
|
|
|
RPI.Offset = Offset / Scale;
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2018-11-10 07:33:30 +08:00
|
|
|
bool NeedsWinCFI = needsWinCFI(MF);
|
2016-02-02 03:07:06 +08:00
|
|
|
DebugLoc DL;
|
|
|
|
SmallVector<RegPairInfo, 8> RegPairs;
|
|
|
|
|
2018-04-05 05:55:44 +08:00
|
|
|
bool NeedShadowCallStackProlog = false;
|
|
|
|
computeCalleeSaveRegisterPairs(MF, CSI, TRI, RegPairs,
|
|
|
|
NeedShadowCallStackProlog);
|
2017-05-27 11:38:02 +08:00
|
|
|
const MachineRegisterInfo &MRI = MF.getRegInfo();
|
2016-02-02 03:07:06 +08:00
|
|
|
|
2018-04-05 05:55:44 +08:00
|
|
|
if (NeedShadowCallStackProlog) {
|
|
|
|
// Shadow call stack prolog: str x30, [x18], #8
|
|
|
|
BuildMI(MBB, MI, DL, TII.get(AArch64::STRXpost))
|
|
|
|
.addReg(AArch64::X18, RegState::Define)
|
|
|
|
.addReg(AArch64::LR)
|
|
|
|
.addReg(AArch64::X18)
|
|
|
|
.addImm(8)
|
|
|
|
.setMIFlag(MachineInstr::FrameSetup);
|
|
|
|
|
2018-10-31 17:27:01 +08:00
|
|
|
if (NeedsWinCFI)
|
|
|
|
BuildMI(MBB, MI, DL, TII.get(AArch64::SEH_Nop))
|
|
|
|
.setMIFlag(MachineInstr::FrameSetup);
|
2018-11-17 04:08:54 +08:00
|
|
|
|
2018-12-01 05:04:25 +08:00
|
|
|
if (!MF.getFunction().hasFnAttribute(Attribute::NoUnwind)) {
|
|
|
|
// Emit a CFI instruction that causes 8 to be subtracted from the value of
|
|
|
|
// x18 when unwinding past this frame.
|
|
|
|
static const char CFIInst[] = {
|
|
|
|
dwarf::DW_CFA_val_expression,
|
|
|
|
18, // register
|
|
|
|
2, // length
|
|
|
|
static_cast<char>(unsigned(dwarf::DW_OP_breg18)),
|
|
|
|
static_cast<char>(-8) & 0x7f, // addend (sleb128)
|
|
|
|
};
|
2019-01-23 22:51:21 +08:00
|
|
|
unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createEscape(
|
|
|
|
nullptr, StringRef(CFIInst, sizeof(CFIInst))));
|
2018-12-01 05:04:25 +08:00
|
|
|
BuildMI(MBB, MI, DL, TII.get(AArch64::CFI_INSTRUCTION))
|
|
|
|
.addCFIIndex(CFIIndex)
|
|
|
|
.setMIFlag(MachineInstr::FrameSetup);
|
|
|
|
}
|
2018-10-31 17:27:01 +08:00
|
|
|
|
2018-04-05 05:55:44 +08:00
|
|
|
// This instruction also makes x18 live-in to the entry block.
|
|
|
|
MBB.addLiveIn(AArch64::X18);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2016-05-07 00:34:59 +08:00
|
|
|
// Issue sequence of spills for cs regs. The first spill may be converted
|
|
|
|
// to a pre-decrement store later by emitPrologue if the callee-save stack
|
|
|
|
// area allocation can't be combined with the local stack area allocation.
|
2014-03-29 18:18:08 +08:00
|
|
|
// For example:
|
2016-05-07 00:34:59 +08:00
|
|
|
// stp x22, x21, [sp, #0] // addImm(+0)
|
2014-03-29 18:18:08 +08:00
|
|
|
// 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.
|
2018-09-12 17:44:46 +08:00
|
|
|
unsigned Size, Align;
|
|
|
|
switch (RPI.Type) {
|
|
|
|
case RegPairInfo::GPR:
|
|
|
|
StrOpc = RPI.isPaired() ? AArch64::STPXi : AArch64::STRXui;
|
|
|
|
Size = 8;
|
|
|
|
Align = 8;
|
|
|
|
break;
|
|
|
|
case RegPairInfo::FPR64:
|
|
|
|
StrOpc = RPI.isPaired() ? AArch64::STPDi : AArch64::STRDui;
|
|
|
|
Size = 8;
|
|
|
|
Align = 8;
|
|
|
|
break;
|
2018-09-12 20:10:22 +08:00
|
|
|
case RegPairInfo::FPR128:
|
|
|
|
StrOpc = RPI.isPaired() ? AArch64::STPQi : AArch64::STRQui;
|
|
|
|
Size = 16;
|
|
|
|
Align = 16;
|
|
|
|
break;
|
2018-09-12 17:44:46 +08:00
|
|
|
}
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "CSR spill: (" << printReg(Reg1, TRI);
|
|
|
|
if (RPI.isPaired()) dbgs() << ", " << printReg(Reg2, TRI);
|
|
|
|
dbgs() << ") -> fi#(" << RPI.FrameIdx;
|
|
|
|
if (RPI.isPaired()) dbgs() << ", " << RPI.FrameIdx + 1;
|
|
|
|
dbgs() << ")\n");
|
2016-02-02 03:07:06 +08:00
|
|
|
|
2018-10-31 17:27:01 +08:00
|
|
|
assert((!NeedsWinCFI || !(Reg1 == AArch64::LR && Reg2 == AArch64::FP)) &&
|
|
|
|
"Windows unwdinding requires a consecutive (FP,LR) pair");
|
|
|
|
// Windows unwind codes require consecutive registers if registers are
|
|
|
|
// paired. Make the switch here, so that the code below will save (x,x+1)
|
|
|
|
// and not (x+1,x).
|
|
|
|
unsigned FrameIdxReg1 = RPI.FrameIdx;
|
|
|
|
unsigned FrameIdxReg2 = RPI.FrameIdx + 1;
|
|
|
|
if (NeedsWinCFI && RPI.isPaired()) {
|
|
|
|
std::swap(Reg1, Reg2);
|
|
|
|
std::swap(FrameIdxReg1, FrameIdxReg2);
|
|
|
|
}
|
2014-05-22 19:56:20 +08:00
|
|
|
MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(StrOpc));
|
2017-05-27 11:38:02 +08:00
|
|
|
if (!MRI.isReserved(Reg1))
|
|
|
|
MBB.addLiveIn(Reg1);
|
2016-02-13 00:31:41 +08:00
|
|
|
if (RPI.isPaired()) {
|
2017-05-27 11:38:02 +08:00
|
|
|
if (!MRI.isReserved(Reg2))
|
|
|
|
MBB.addLiveIn(Reg2);
|
2016-05-07 00:34:59 +08:00
|
|
|
MIB.addReg(Reg2, getPrologueDeath(MF, Reg2));
|
2016-04-15 23:16:19 +08:00
|
|
|
MIB.addMemOperand(MF.getMachineMemOperand(
|
2018-10-31 17:27:01 +08:00
|
|
|
MachinePointerInfo::getFixedStack(MF, FrameIdxReg2),
|
2018-09-12 17:44:46 +08:00
|
|
|
MachineMemOperand::MOStore, Size, Align));
|
2016-05-07 00:34:59 +08:00
|
|
|
}
|
|
|
|
MIB.addReg(Reg1, getPrologueDeath(MF, Reg1))
|
2016-02-13 00:31:41 +08:00
|
|
|
.addReg(AArch64::SP)
|
2018-09-12 17:44:46 +08:00
|
|
|
.addImm(RPI.Offset) // [sp, #offset*scale],
|
|
|
|
// where factor*scale is implicit
|
2016-02-13 00:31:41 +08:00
|
|
|
.setMIFlag(MachineInstr::FrameSetup);
|
2016-04-15 23:16:19 +08:00
|
|
|
MIB.addMemOperand(MF.getMachineMemOperand(
|
2018-10-31 17:27:01 +08:00
|
|
|
MachinePointerInfo::getFixedStack(MF,FrameIdxReg1),
|
2018-09-12 17:44:46 +08:00
|
|
|
MachineMemOperand::MOStore, Size, Align));
|
2018-10-31 17:27:01 +08:00
|
|
|
if (NeedsWinCFI)
|
|
|
|
InsertSEH(MIB, TII, 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,
|
2017-08-11 00:17:32 +08:00
|
|
|
std::vector<CalleeSavedInfo> &CSI,
|
2014-03-29 18:18:08 +08:00
|
|
|
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;
|
2018-11-10 07:33:30 +08:00
|
|
|
bool NeedsWinCFI = needsWinCFI(MF);
|
2014-03-29 18:18:08 +08:00
|
|
|
|
|
|
|
if (MI != MBB.end())
|
|
|
|
DL = MI->getDebugLoc();
|
|
|
|
|
2018-04-05 05:55:44 +08:00
|
|
|
bool NeedShadowCallStackProlog = false;
|
|
|
|
computeCalleeSaveRegisterPairs(MF, CSI, TRI, RegPairs,
|
|
|
|
NeedShadowCallStackProlog);
|
2016-02-02 03:07:06 +08:00
|
|
|
|
[AArch64] Emit CSR loads in the same order as stores
Optionally allow the order of restoring the callee-saved registers in the
epilogue to be reversed.
The flag -reverse-csr-restore-seq generates the following code:
```
stp x26, x25, [sp, #-64]!
stp x24, x23, [sp, #16]
stp x22, x21, [sp, #32]
stp x20, x19, [sp, #48]
; [..]
ldp x24, x23, [sp, #16]
ldp x22, x21, [sp, #32]
ldp x20, x19, [sp, #48]
ldp x26, x25, [sp], #64
ret
```
Note how the CSRs are restored in the same order as they are saved.
One exception to this rule is the last `ldp`, which allows us to merge
the stack adjustment and the ldp into a post-index ldp. This is done by
first generating:
ldp x26, x27, [sp]
add sp, sp, #64
which gets merged by the arm64 load store optimizer into
ldp x26, x25, [sp], #64
The flag is disabled by default.
llvm-svn: 327569
2018-03-15 04:34:03 +08:00
|
|
|
auto EmitMI = [&](const RegPairInfo &RPI) {
|
2016-02-02 03:07:06 +08:00
|
|
|
unsigned Reg1 = RPI.Reg1;
|
|
|
|
unsigned Reg2 = RPI.Reg2;
|
|
|
|
|
2016-05-07 00:34:59 +08:00
|
|
|
// Issue sequence of restores for cs regs. The last restore may be converted
|
|
|
|
// to a post-increment load later by emitEpilogue if the callee-save stack
|
|
|
|
// area allocation can't be combined with the local stack area allocation.
|
2014-03-29 18:18:08 +08:00
|
|
|
// For example:
|
|
|
|
// ldp fp, lr, [sp, #32] // addImm(+4)
|
|
|
|
// ldp x20, x19, [sp, #16] // addImm(+2)
|
2016-05-07 00:34:59 +08:00
|
|
|
// ldp x22, x21, [sp, #0] // addImm(+0)
|
2014-03-29 18:18:08 +08:00
|
|
|
// Note: see comment in spillCalleeSavedRegisters()
|
|
|
|
unsigned LdrOpc;
|
2018-09-12 17:44:46 +08:00
|
|
|
unsigned Size, Align;
|
|
|
|
switch (RPI.Type) {
|
|
|
|
case RegPairInfo::GPR:
|
|
|
|
LdrOpc = RPI.isPaired() ? AArch64::LDPXi : AArch64::LDRXui;
|
|
|
|
Size = 8;
|
|
|
|
Align = 8;
|
|
|
|
break;
|
|
|
|
case RegPairInfo::FPR64:
|
|
|
|
LdrOpc = RPI.isPaired() ? AArch64::LDPDi : AArch64::LDRDui;
|
|
|
|
Size = 8;
|
|
|
|
Align = 8;
|
|
|
|
break;
|
2018-09-12 20:10:22 +08:00
|
|
|
case RegPairInfo::FPR128:
|
|
|
|
LdrOpc = RPI.isPaired() ? AArch64::LDPQi : AArch64::LDRQui;
|
|
|
|
Size = 16;
|
|
|
|
Align = 16;
|
|
|
|
break;
|
2018-09-12 17:44:46 +08:00
|
|
|
}
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "CSR restore: (" << printReg(Reg1, TRI);
|
|
|
|
if (RPI.isPaired()) dbgs() << ", " << printReg(Reg2, TRI);
|
|
|
|
dbgs() << ") -> fi#(" << RPI.FrameIdx;
|
|
|
|
if (RPI.isPaired()) dbgs() << ", " << RPI.FrameIdx + 1;
|
|
|
|
dbgs() << ")\n");
|
2016-02-02 03:07:06 +08:00
|
|
|
|
2018-10-31 17:27:01 +08:00
|
|
|
// Windows unwind codes require consecutive registers if registers are
|
|
|
|
// paired. Make the switch here, so that the code below will save (x,x+1)
|
|
|
|
// and not (x+1,x).
|
|
|
|
unsigned FrameIdxReg1 = RPI.FrameIdx;
|
|
|
|
unsigned FrameIdxReg2 = RPI.FrameIdx + 1;
|
|
|
|
if (NeedsWinCFI && RPI.isPaired()) {
|
|
|
|
std::swap(Reg1, Reg2);
|
|
|
|
std::swap(FrameIdxReg1, FrameIdxReg2);
|
|
|
|
}
|
2014-05-22 19:56:20 +08:00
|
|
|
MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(LdrOpc));
|
2016-04-15 23:16:19 +08:00
|
|
|
if (RPI.isPaired()) {
|
2016-05-07 00:34:59 +08:00
|
|
|
MIB.addReg(Reg2, getDefRegState(true));
|
2016-04-15 23:16:19 +08:00
|
|
|
MIB.addMemOperand(MF.getMachineMemOperand(
|
2018-10-31 17:27:01 +08:00
|
|
|
MachinePointerInfo::getFixedStack(MF, FrameIdxReg2),
|
2018-09-12 17:44:46 +08:00
|
|
|
MachineMemOperand::MOLoad, Size, Align));
|
2016-05-07 00:34:59 +08:00
|
|
|
}
|
|
|
|
MIB.addReg(Reg1, getDefRegState(true))
|
2016-02-13 00:31:41 +08:00
|
|
|
.addReg(AArch64::SP)
|
2018-09-12 17:44:46 +08:00
|
|
|
.addImm(RPI.Offset) // [sp, #offset*scale]
|
|
|
|
// where factor*scale is implicit
|
2016-02-13 00:31:41 +08:00
|
|
|
.setMIFlag(MachineInstr::FrameDestroy);
|
2016-04-15 23:16:19 +08:00
|
|
|
MIB.addMemOperand(MF.getMachineMemOperand(
|
2018-10-31 17:27:01 +08:00
|
|
|
MachinePointerInfo::getFixedStack(MF, FrameIdxReg1),
|
2018-09-12 17:44:46 +08:00
|
|
|
MachineMemOperand::MOLoad, Size, Align));
|
2018-10-31 17:27:01 +08:00
|
|
|
if (NeedsWinCFI)
|
|
|
|
InsertSEH(MIB, TII, MachineInstr::FrameDestroy);
|
[AArch64] Emit CSR loads in the same order as stores
Optionally allow the order of restoring the callee-saved registers in the
epilogue to be reversed.
The flag -reverse-csr-restore-seq generates the following code:
```
stp x26, x25, [sp, #-64]!
stp x24, x23, [sp, #16]
stp x22, x21, [sp, #32]
stp x20, x19, [sp, #48]
; [..]
ldp x24, x23, [sp, #16]
ldp x22, x21, [sp, #32]
ldp x20, x19, [sp, #48]
ldp x26, x25, [sp], #64
ret
```
Note how the CSRs are restored in the same order as they are saved.
One exception to this rule is the last `ldp`, which allows us to merge
the stack adjustment and the ldp into a post-index ldp. This is done by
first generating:
ldp x26, x27, [sp]
add sp, sp, #64
which gets merged by the arm64 load store optimizer into
ldp x26, x25, [sp], #64
The flag is disabled by default.
llvm-svn: 327569
2018-03-15 04:34:03 +08:00
|
|
|
};
|
|
|
|
if (ReverseCSRRestoreSeq)
|
|
|
|
for (const RegPairInfo &RPI : reverse(RegPairs))
|
|
|
|
EmitMI(RPI);
|
|
|
|
else
|
|
|
|
for (const RegPairInfo &RPI : RegPairs)
|
|
|
|
EmitMI(RPI);
|
2018-04-05 05:55:44 +08:00
|
|
|
|
|
|
|
if (NeedShadowCallStackProlog) {
|
|
|
|
// Shadow call stack epilog: ldr x30, [x18, #-8]!
|
|
|
|
BuildMI(MBB, MI, DL, TII.get(AArch64::LDRXpre))
|
|
|
|
.addReg(AArch64::X18, RegState::Define)
|
|
|
|
.addReg(AArch64::LR, RegState::Define)
|
|
|
|
.addReg(AArch64::X18)
|
|
|
|
.addImm(-8)
|
|
|
|
.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.
|
2017-12-16 06:22:58 +08:00
|
|
|
if (MF.getFunction().getCallingConv() == CallingConv::GHC)
|
2015-07-15 01:17:13 +08:00
|
|
|
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
|
|
|
unsigned UnspilledCSGPR = AArch64::NoRegister;
|
|
|
|
unsigned UnspilledCSGPRPaired = AArch64::NoRegister;
|
2014-03-29 18:18:08 +08:00
|
|
|
|
2017-12-20 14:51:45 +08:00
|
|
|
MachineFrameInfo &MFI = MF.getFrameInfo();
|
2018-09-23 06:17:50 +08:00
|
|
|
const MCPhysReg *CSRegs = MF.getRegInfo().getCalleeSavedRegs();
|
2017-12-20 14:51:45 +08:00
|
|
|
|
|
|
|
unsigned BasePointerReg = RegInfo->hasBasePointer(MF)
|
|
|
|
? RegInfo->getBaseRegister()
|
|
|
|
: (unsigned)AArch64::NoRegister;
|
|
|
|
|
2017-04-22 06:42:08 +08:00
|
|
|
unsigned ExtraCSSpill = 0;
|
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.
|
2018-09-12 20:10:22 +08:00
|
|
|
if (produceCompactUnwindFrame(MF) && PairedReg != AArch64::NoRegister &&
|
|
|
|
!SavedRegs.test(PairedReg)) {
|
2016-02-13 00:31:41 +08:00
|
|
|
SavedRegs.set(PairedReg);
|
2016-05-17 04:52:28 +08:00
|
|
|
if (AArch64::GPR64RegClass.contains(PairedReg) &&
|
|
|
|
!RegInfo->isReservedReg(MF, PairedReg))
|
2017-04-22 06:42:08 +08:00
|
|
|
ExtraCSSpill = PairedReg;
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
2016-02-13 00:31:41 +08:00
|
|
|
}
|
2014-03-29 18:18:08 +08:00
|
|
|
|
2018-09-12 20:10:22 +08:00
|
|
|
// Calculates the callee saved stack size.
|
|
|
|
unsigned CSStackSize = 0;
|
|
|
|
const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
|
|
|
|
const MachineRegisterInfo &MRI = MF.getRegInfo();
|
|
|
|
for (unsigned Reg : SavedRegs.set_bits())
|
|
|
|
CSStackSize += TRI->getRegSizeInBits(Reg, MRI) / 8;
|
|
|
|
|
|
|
|
// Save number of saved regs, so we can easily update CSStackSize later.
|
|
|
|
unsigned NumSavedRegs = SavedRegs.count();
|
|
|
|
|
|
|
|
// The frame record needs to be created by saving the appropriate registers
|
|
|
|
unsigned EstimatedStackSize = MFI.estimateStackSize(MF);
|
|
|
|
if (hasFP(MF) ||
|
|
|
|
windowsRequiresStackProbe(MF, EstimatedStackSize + CSStackSize + 16)) {
|
|
|
|
SavedRegs.set(AArch64::FP);
|
|
|
|
SavedRegs.set(AArch64::LR);
|
|
|
|
}
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "*** determineCalleeSaves\nUsed CSRs:";
|
|
|
|
for (unsigned Reg
|
|
|
|
: SavedRegs.set_bits()) 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.
|
2018-09-12 20:10:22 +08:00
|
|
|
bool CanEliminateFrame = SavedRegs.count() == 0;
|
2014-03-29 18:18:08 +08:00
|
|
|
|
|
|
|
// The CSR spill slots have not been allocated yet, so estimateStackSize
|
|
|
|
// won't include them.
|
2017-05-30 14:58:41 +08:00
|
|
|
unsigned EstimatedStackSizeLimit = estimateRSStackSizeLimit(MF);
|
2018-09-12 20:10:22 +08:00
|
|
|
bool BigStack = (EstimatedStackSize + CSStackSize) > EstimatedStackSizeLimit;
|
2014-03-29 18:18:08 +08:00
|
|
|
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.
|
2017-04-22 06:42:08 +08:00
|
|
|
if (BigStack) {
|
|
|
|
if (!ExtraCSSpill && UnspilledCSGPR != AArch64::NoRegister) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Spilling " << printReg(UnspilledCSGPR, RegInfo)
|
|
|
|
<< " to get a scratch register.\n");
|
2016-02-13 00:31:41 +08:00
|
|
|
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.
|
2016-04-12 05:08:06 +08:00
|
|
|
if (produceCompactUnwindFrame(MF))
|
2016-02-13 00:31:41 +08:00
|
|
|
SavedRegs.set(UnspilledCSGPRPaired);
|
2017-04-22 06:42:08 +08:00
|
|
|
ExtraCSSpill = UnspilledCSGPRPaired;
|
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.
|
2017-04-22 06:42:08 +08:00
|
|
|
if (!ExtraCSSpill || MF.getRegInfo().isPhysRegUsed(ExtraCSSpill)) {
|
2017-04-25 02:55:33 +08:00
|
|
|
const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
|
|
|
|
const TargetRegisterClass &RC = AArch64::GPR64RegClass;
|
|
|
|
unsigned Size = TRI->getSpillSize(RC);
|
|
|
|
unsigned Align = TRI->getSpillAlignment(RC);
|
|
|
|
int FI = MFI.CreateStackObject(Size, Align, false);
|
2014-03-29 18:18:08 +08:00
|
|
|
RS->addScavengingFrameIndex(FI);
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "No available CS registers, allocated fi#" << FI
|
|
|
|
<< " as the emergency spill slot.\n");
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
|
|
|
}
|
2016-02-02 00:29:19 +08:00
|
|
|
|
2018-09-12 20:10:22 +08:00
|
|
|
// Adding the size of additional 64bit GPR saves.
|
|
|
|
CSStackSize += 8 * (SavedRegs.count() - NumSavedRegs);
|
|
|
|
unsigned AlignedCSStackSize = alignTo(CSStackSize, 16);
|
|
|
|
LLVM_DEBUG(dbgs() << "Estimated stack frame size: "
|
|
|
|
<< EstimatedStackSize + AlignedCSStackSize
|
|
|
|
<< " bytes.\n");
|
|
|
|
|
2016-02-13 00:31:41 +08:00
|
|
|
// Round up to register pair alignment to avoid additional SP adjustment
|
|
|
|
// instructions.
|
2018-09-12 20:10:22 +08:00
|
|
|
AFI->setCalleeSavedStackSize(AlignedCSStackSize);
|
|
|
|
AFI->setCalleeSaveStackHasFreeSpace(AlignedCSStackSize != CSStackSize);
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
2016-06-03 00:22:07 +08:00
|
|
|
|
|
|
|
bool AArch64FrameLowering::enableStackSlotScavenging(
|
|
|
|
const MachineFunction &MF) const {
|
|
|
|
const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
|
|
|
|
return AFI->hasCalleeSaveStackFreeSpace();
|
|
|
|
}
|
2018-11-10 07:33:30 +08:00
|
|
|
|
|
|
|
void AArch64FrameLowering::processFunctionBeforeFrameFinalized(
|
|
|
|
MachineFunction &MF, RegScavenger *RS) const {
|
|
|
|
// If this function isn't doing Win64-style C++ EH, we don't need to do
|
|
|
|
// anything.
|
|
|
|
if (!MF.hasEHFunclets())
|
|
|
|
return;
|
|
|
|
const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
|
|
|
|
MachineFrameInfo &MFI = MF.getFrameInfo();
|
|
|
|
WinEHFuncInfo &EHInfo = *MF.getWinEHFuncInfo();
|
|
|
|
|
|
|
|
MachineBasicBlock &MBB = MF.front();
|
|
|
|
auto MBBI = MBB.begin();
|
|
|
|
while (MBBI != MBB.end() && MBBI->getFlag(MachineInstr::FrameSetup))
|
|
|
|
++MBBI;
|
|
|
|
|
|
|
|
if (MBBI->isTerminator())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Create an UnwindHelp object.
|
|
|
|
int UnwindHelpFI =
|
|
|
|
MFI.CreateStackObject(/*size*/8, /*alignment*/16, false);
|
|
|
|
EHInfo.UnwindHelpFrameIdx = UnwindHelpFI;
|
|
|
|
// We need to store -2 into the UnwindHelp object at the start of the
|
|
|
|
// function.
|
|
|
|
DebugLoc DL;
|
|
|
|
RS->enterBasicBlock(MBB);
|
|
|
|
unsigned DstReg = RS->scavengeRegister(&AArch64::GPR64RegClass, MBBI, 0);
|
|
|
|
BuildMI(MBB, MBBI, DL, TII.get(AArch64::MOVi64imm), DstReg).addImm(-2);
|
|
|
|
BuildMI(MBB, MBBI, DL, TII.get(AArch64::STURXi))
|
|
|
|
.addReg(DstReg, getKillRegState(true))
|
|
|
|
.addFrameIndex(UnwindHelpFI)
|
|
|
|
.addImm(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// For Win64 AArch64 EH, the offset to the Unwind object is from the SP before
|
|
|
|
/// the update. This is easily retrieved as it is exactly the offset that is set
|
|
|
|
/// in processFunctionBeforeFrameFinalized.
|
|
|
|
int AArch64FrameLowering::getFrameIndexReferencePreferSP(
|
|
|
|
const MachineFunction &MF, int FI, unsigned &FrameReg,
|
|
|
|
bool IgnoreSPUpdates) const {
|
|
|
|
const MachineFrameInfo &MFI = MF.getFrameInfo();
|
|
|
|
LLVM_DEBUG(dbgs() << "Offset from the SP for " << FI << " is "
|
|
|
|
<< MFI.getObjectOffset(FI) << "\n");
|
|
|
|
FrameReg = AArch64::SP;
|
|
|
|
return MFI.getObjectOffset(FI);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The parent frame offset (aka dispFrame) is only used on X86_64 to retrieve
|
|
|
|
/// the parent's frame pointer
|
|
|
|
unsigned AArch64FrameLowering::getWinEHParentFrameOffset(
|
|
|
|
const MachineFunction &MF) const {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Funclets only need to account for space for the callee saved registers,
|
|
|
|
/// as the locals are accounted for in the parent's stack frame.
|
|
|
|
unsigned AArch64FrameLowering::getWinEHFuncletFrameSize(
|
|
|
|
const MachineFunction &MF) const {
|
|
|
|
// This is the size of the pushed CSRs.
|
|
|
|
unsigned CSSize =
|
|
|
|
MF.getInfo<AArch64FunctionInfo>()->getCalleeSavedStackSize();
|
|
|
|
// This is the amount of stack a funclet needs to allocate.
|
|
|
|
return alignTo(CSSize + MF.getFrameInfo().getMaxCallFrameSize(),
|
|
|
|
getStackAlignment());
|
|
|
|
}
|