2017-10-20 05:37:38 +08:00
|
|
|
//===-- RISCVFrameLowering.cpp - RISCV Frame Information ------------------===//
|
|
|
|
//
|
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
|
2017-10-20 05:37:38 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains the RISCV implementation of TargetFrameLowering class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "RISCVFrameLowering.h"
|
2018-01-11 03:41:03 +08:00
|
|
|
#include "RISCVMachineFunctionInfo.h"
|
2017-10-20 05:37:38 +08:00
|
|
|
#include "RISCVSubtarget.h"
|
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2018-01-11 19:17:19 +08:00
|
|
|
#include "llvm/CodeGen/RegisterScavenging.h"
|
2019-10-23 04:25:01 +08:00
|
|
|
#include "llvm/IR/DiagnosticInfo.h"
|
2019-06-12 11:04:22 +08:00
|
|
|
#include "llvm/MC/MCDwarf.h"
|
2017-10-20 05:37:38 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2020-03-27 13:09:31 +08:00
|
|
|
// For now we use x18, a.k.a s2, as pointer to shadow call stack.
|
|
|
|
// User should explicitly set -ffixed-x18 and not use x18 in their asm.
|
|
|
|
static void emitSCSPrologue(MachineFunction &MF, MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MI,
|
|
|
|
const DebugLoc &DL) {
|
|
|
|
if (!MF.getFunction().hasFnAttribute(Attribute::ShadowCallStack))
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto &STI = MF.getSubtarget<RISCVSubtarget>();
|
|
|
|
Register RAReg = STI.getRegisterInfo()->getRARegister();
|
|
|
|
|
|
|
|
// Do not save RA to the SCS if it's not saved to the regular stack,
|
|
|
|
// i.e. RA is not at risk of being overwritten.
|
|
|
|
std::vector<CalleeSavedInfo> &CSI = MF.getFrameInfo().getCalleeSavedInfo();
|
|
|
|
if (std::none_of(CSI.begin(), CSI.end(),
|
|
|
|
[&](CalleeSavedInfo &CSR) { return CSR.getReg() == RAReg; }))
|
|
|
|
return;
|
|
|
|
|
|
|
|
Register SCSPReg = RISCVABI::getSCSPReg();
|
|
|
|
|
|
|
|
auto &Ctx = MF.getFunction().getContext();
|
|
|
|
if (!STI.isRegisterReservedByUser(SCSPReg)) {
|
|
|
|
Ctx.diagnose(DiagnosticInfoUnsupported{
|
|
|
|
MF.getFunction(), "x18 not reserved by user for Shadow Call Stack."});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
|
|
|
|
if (RVFI->useSaveRestoreLibCalls(MF)) {
|
|
|
|
Ctx.diagnose(DiagnosticInfoUnsupported{
|
|
|
|
MF.getFunction(),
|
|
|
|
"Shadow Call Stack cannot be combined with Save/Restore LibCalls."});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const RISCVInstrInfo *TII = STI.getInstrInfo();
|
|
|
|
bool IsRV64 = STI.hasFeature(RISCV::Feature64Bit);
|
|
|
|
int64_t SlotSize = STI.getXLen() / 8;
|
|
|
|
// Store return address to shadow call stack
|
|
|
|
// s[w|d] ra, 0(s2)
|
|
|
|
// addi s2, s2, [4|8]
|
|
|
|
BuildMI(MBB, MI, DL, TII->get(IsRV64 ? RISCV::SD : RISCV::SW))
|
|
|
|
.addReg(RAReg)
|
|
|
|
.addReg(SCSPReg)
|
|
|
|
.addImm(0);
|
|
|
|
BuildMI(MBB, MI, DL, TII->get(RISCV::ADDI))
|
|
|
|
.addReg(SCSPReg, RegState::Define)
|
|
|
|
.addReg(SCSPReg)
|
|
|
|
.addImm(SlotSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void emitSCSEpilogue(MachineFunction &MF, MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MI,
|
|
|
|
const DebugLoc &DL) {
|
|
|
|
if (!MF.getFunction().hasFnAttribute(Attribute::ShadowCallStack))
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto &STI = MF.getSubtarget<RISCVSubtarget>();
|
|
|
|
Register RAReg = STI.getRegisterInfo()->getRARegister();
|
|
|
|
|
|
|
|
// See emitSCSPrologue() above.
|
|
|
|
std::vector<CalleeSavedInfo> &CSI = MF.getFrameInfo().getCalleeSavedInfo();
|
|
|
|
if (std::none_of(CSI.begin(), CSI.end(),
|
|
|
|
[&](CalleeSavedInfo &CSR) { return CSR.getReg() == RAReg; }))
|
|
|
|
return;
|
|
|
|
|
|
|
|
Register SCSPReg = RISCVABI::getSCSPReg();
|
|
|
|
|
|
|
|
auto &Ctx = MF.getFunction().getContext();
|
|
|
|
if (!STI.isRegisterReservedByUser(SCSPReg)) {
|
|
|
|
Ctx.diagnose(DiagnosticInfoUnsupported{
|
|
|
|
MF.getFunction(), "x18 not reserved by user for Shadow Call Stack."});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
|
|
|
|
if (RVFI->useSaveRestoreLibCalls(MF)) {
|
|
|
|
Ctx.diagnose(DiagnosticInfoUnsupported{
|
|
|
|
MF.getFunction(),
|
|
|
|
"Shadow Call Stack cannot be combined with Save/Restore LibCalls."});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const RISCVInstrInfo *TII = STI.getInstrInfo();
|
|
|
|
bool IsRV64 = STI.hasFeature(RISCV::Feature64Bit);
|
|
|
|
int64_t SlotSize = STI.getXLen() / 8;
|
|
|
|
// Load return address from shadow call stack
|
|
|
|
// l[w|d] ra, -[4|8](s2)
|
|
|
|
// addi s2, s2, -[4|8]
|
|
|
|
BuildMI(MBB, MI, DL, TII->get(IsRV64 ? RISCV::LD : RISCV::LW))
|
|
|
|
.addReg(RAReg, RegState::Define)
|
|
|
|
.addReg(SCSPReg)
|
|
|
|
.addImm(-SlotSize);
|
|
|
|
BuildMI(MBB, MI, DL, TII->get(RISCV::ADDI))
|
|
|
|
.addReg(SCSPReg, RegState::Define)
|
|
|
|
.addReg(SCSPReg)
|
|
|
|
.addImm(-SlotSize);
|
|
|
|
}
|
|
|
|
|
2020-02-12 05:23:03 +08:00
|
|
|
// Get the ID of the libcall used for spilling and restoring callee saved
|
|
|
|
// registers. The ID is representative of the number of registers saved or
|
|
|
|
// restored by the libcall, except it is zero-indexed - ID 0 corresponds to a
|
|
|
|
// single register.
|
|
|
|
static int getLibCallID(const MachineFunction &MF,
|
|
|
|
const std::vector<CalleeSavedInfo> &CSI) {
|
|
|
|
const auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
|
|
|
|
|
2020-07-01 03:41:03 +08:00
|
|
|
if (CSI.empty() || !RVFI->useSaveRestoreLibCalls(MF))
|
2020-02-12 05:23:03 +08:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
Register MaxReg = RISCV::NoRegister;
|
|
|
|
for (auto &CS : CSI)
|
|
|
|
// RISCVRegisterInfo::hasReservedSpillSlot assigns negative frame indexes to
|
|
|
|
// registers which can be saved by libcall.
|
|
|
|
if (CS.getFrameIdx() < 0)
|
|
|
|
MaxReg = std::max(MaxReg.id(), CS.getReg());
|
|
|
|
|
|
|
|
if (MaxReg == RISCV::NoRegister)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
switch (MaxReg) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Something has gone wrong!");
|
|
|
|
case /*s11*/ RISCV::X27: return 12;
|
|
|
|
case /*s10*/ RISCV::X26: return 11;
|
|
|
|
case /*s9*/ RISCV::X25: return 10;
|
|
|
|
case /*s8*/ RISCV::X24: return 9;
|
|
|
|
case /*s7*/ RISCV::X23: return 8;
|
|
|
|
case /*s6*/ RISCV::X22: return 7;
|
|
|
|
case /*s5*/ RISCV::X21: return 6;
|
|
|
|
case /*s4*/ RISCV::X20: return 5;
|
|
|
|
case /*s3*/ RISCV::X19: return 4;
|
|
|
|
case /*s2*/ RISCV::X18: return 3;
|
|
|
|
case /*s1*/ RISCV::X9: return 2;
|
|
|
|
case /*s0*/ RISCV::X8: return 1;
|
|
|
|
case /*ra*/ RISCV::X1: return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the name of the libcall used for spilling callee saved registers.
|
|
|
|
// If this function will not use save/restore libcalls, then return a nullptr.
|
|
|
|
static const char *
|
|
|
|
getSpillLibCallName(const MachineFunction &MF,
|
|
|
|
const std::vector<CalleeSavedInfo> &CSI) {
|
|
|
|
static const char *const SpillLibCalls[] = {
|
|
|
|
"__riscv_save_0",
|
|
|
|
"__riscv_save_1",
|
|
|
|
"__riscv_save_2",
|
|
|
|
"__riscv_save_3",
|
|
|
|
"__riscv_save_4",
|
|
|
|
"__riscv_save_5",
|
|
|
|
"__riscv_save_6",
|
|
|
|
"__riscv_save_7",
|
|
|
|
"__riscv_save_8",
|
|
|
|
"__riscv_save_9",
|
|
|
|
"__riscv_save_10",
|
|
|
|
"__riscv_save_11",
|
|
|
|
"__riscv_save_12"
|
|
|
|
};
|
|
|
|
|
|
|
|
int LibCallID = getLibCallID(MF, CSI);
|
|
|
|
if (LibCallID == -1)
|
|
|
|
return nullptr;
|
|
|
|
return SpillLibCalls[LibCallID];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the name of the libcall used for restoring callee saved registers.
|
|
|
|
// If this function will not use save/restore libcalls, then return a nullptr.
|
|
|
|
static const char *
|
|
|
|
getRestoreLibCallName(const MachineFunction &MF,
|
|
|
|
const std::vector<CalleeSavedInfo> &CSI) {
|
|
|
|
static const char *const RestoreLibCalls[] = {
|
|
|
|
"__riscv_restore_0",
|
|
|
|
"__riscv_restore_1",
|
|
|
|
"__riscv_restore_2",
|
|
|
|
"__riscv_restore_3",
|
|
|
|
"__riscv_restore_4",
|
|
|
|
"__riscv_restore_5",
|
|
|
|
"__riscv_restore_6",
|
|
|
|
"__riscv_restore_7",
|
|
|
|
"__riscv_restore_8",
|
|
|
|
"__riscv_restore_9",
|
|
|
|
"__riscv_restore_10",
|
|
|
|
"__riscv_restore_11",
|
|
|
|
"__riscv_restore_12"
|
|
|
|
};
|
|
|
|
|
|
|
|
int LibCallID = getLibCallID(MF, CSI);
|
|
|
|
if (LibCallID == -1)
|
|
|
|
return nullptr;
|
|
|
|
return RestoreLibCalls[LibCallID];
|
|
|
|
}
|
|
|
|
|
2018-01-18 19:34:02 +08:00
|
|
|
bool RISCVFrameLowering::hasFP(const MachineFunction &MF) const {
|
|
|
|
const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
|
|
|
|
|
|
|
|
const MachineFrameInfo &MFI = MF.getFrameInfo();
|
|
|
|
return MF.getTarget().Options.DisableFramePointerElim(MF) ||
|
|
|
|
RegInfo->needsStackRealignment(MF) || MFI.hasVarSizedObjects() ||
|
|
|
|
MFI.isFrameAddressTaken();
|
|
|
|
}
|
2017-10-20 05:37:38 +08:00
|
|
|
|
2019-10-15 15:11:35 +08:00
|
|
|
bool RISCVFrameLowering::hasBP(const MachineFunction &MF) const {
|
|
|
|
const MachineFrameInfo &MFI = MF.getFrameInfo();
|
|
|
|
const TargetRegisterInfo *TRI = STI.getRegisterInfo();
|
|
|
|
|
|
|
|
return MFI.hasVarSizedObjects() && TRI->needsStackRealignment(MF);
|
|
|
|
}
|
|
|
|
|
2017-12-11 20:34:11 +08:00
|
|
|
// Determines the size of the frame and maximum call frame size.
|
|
|
|
void RISCVFrameLowering::determineFrameLayout(MachineFunction &MF) const {
|
|
|
|
MachineFrameInfo &MFI = MF.getFrameInfo();
|
|
|
|
const RISCVRegisterInfo *RI = STI.getRegisterInfo();
|
|
|
|
|
|
|
|
// Get the number of bytes to allocate from the FrameInfo.
|
|
|
|
uint64_t FrameSize = MFI.getStackSize();
|
|
|
|
|
|
|
|
// Get the alignment.
|
[Alignment][NFC] Deprecate getMaxAlignment
Summary:
This is patch is part of a series to introduce an Alignment type.
See this thread for context: http://lists.llvm.org/pipermail/llvm-dev/2019-July/133851.html
See this patch for the introduction of the type: https://reviews.llvm.org/D64790
Reviewers: courbet
Subscribers: jholewinski, arsenm, dschuff, jyknight, sdardis, nemanjai, jvesely, nhaehnle, sbc100, jgravelle-google, hiraditya, aheejin, kbarton, fedor.sergeev, asb, rbar, johnrusso, simoncook, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, PkmX, jocewei, Jim, lenary, s.egerton, pzheng, sameer.abuasal, apazos, luismarques, kerbowa, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D76348
2020-03-18 17:50:38 +08:00
|
|
|
Align StackAlign = getStackAlign();
|
[RISCV] Minimal stack realignment support
Summary:
Currently the RISC-V backend does not realign the stack. This can be an issue even for the RV32I/RV64I ABIs (where the stack is 16-byte aligned), though is rare. It will be much more comment with RV32E (though the alignment requirements for common data types remain under-documented...).
This patch adds minimal support for stack realignment. It should cope with large realignments. It will error out if the stack needs realignment and variable sized objects are present.
It feels like a lot of the code like getFrameIndexReference and determineFrameLayout could be refactored somehow, as right now it feels fiddly and brittle. We also seem to allocate a lot more memory than GCC does for equivalent C code.
Reviewers: asb
Reviewed By: asb
Subscribers: wwei, jrtc27, s.egerton, MaskRay, Jim, lenary, hiraditya, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, kito-cheng, shiva0217, zzheng, edward-jones, rogfer01, MartinMosbeck, brucehoult, the_o, rkruppe, PkmX, jocewei, psnobl, benna, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D62007
llvm-svn: 368300
2019-08-08 22:40:54 +08:00
|
|
|
if (RI->needsStackRealignment(MF)) {
|
[Alignment][NFC] Deprecate getMaxAlignment
Summary:
This is patch is part of a series to introduce an Alignment type.
See this thread for context: http://lists.llvm.org/pipermail/llvm-dev/2019-July/133851.html
See this patch for the introduction of the type: https://reviews.llvm.org/D64790
Reviewers: courbet
Subscribers: jholewinski, arsenm, dschuff, jyknight, sdardis, nemanjai, jvesely, nhaehnle, sbc100, jgravelle-google, hiraditya, aheejin, kbarton, fedor.sergeev, asb, rbar, johnrusso, simoncook, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, PkmX, jocewei, Jim, lenary, s.egerton, pzheng, sameer.abuasal, apazos, luismarques, kerbowa, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D76348
2020-03-18 17:50:38 +08:00
|
|
|
Align MaxStackAlign = std::max(StackAlign, MFI.getMaxAlign());
|
|
|
|
FrameSize += (MaxStackAlign.value() - StackAlign.value());
|
[RISCV] Minimal stack realignment support
Summary:
Currently the RISC-V backend does not realign the stack. This can be an issue even for the RV32I/RV64I ABIs (where the stack is 16-byte aligned), though is rare. It will be much more comment with RV32E (though the alignment requirements for common data types remain under-documented...).
This patch adds minimal support for stack realignment. It should cope with large realignments. It will error out if the stack needs realignment and variable sized objects are present.
It feels like a lot of the code like getFrameIndexReference and determineFrameLayout could be refactored somehow, as right now it feels fiddly and brittle. We also seem to allocate a lot more memory than GCC does for equivalent C code.
Reviewers: asb
Reviewed By: asb
Subscribers: wwei, jrtc27, s.egerton, MaskRay, Jim, lenary, hiraditya, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, kito-cheng, shiva0217, zzheng, edward-jones, rogfer01, MartinMosbeck, brucehoult, the_o, rkruppe, PkmX, jocewei, psnobl, benna, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D62007
llvm-svn: 368300
2019-08-08 22:40:54 +08:00
|
|
|
StackAlign = MaxStackAlign;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set Max Call Frame Size
|
|
|
|
uint64_t MaxCallSize = alignTo(MFI.getMaxCallFrameSize(), StackAlign);
|
|
|
|
MFI.setMaxCallFrameSize(MaxCallSize);
|
2017-12-11 20:34:11 +08:00
|
|
|
|
|
|
|
// Make sure the frame is aligned.
|
|
|
|
FrameSize = alignTo(FrameSize, StackAlign);
|
|
|
|
|
|
|
|
// Update frame info.
|
|
|
|
MFI.setStackSize(FrameSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RISCVFrameLowering::adjustReg(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MBBI,
|
2019-08-16 22:27:50 +08:00
|
|
|
const DebugLoc &DL, Register DestReg,
|
|
|
|
Register SrcReg, int64_t Val,
|
2017-12-11 20:34:11 +08:00
|
|
|
MachineInstr::MIFlag Flag) const {
|
2018-01-11 03:53:46 +08:00
|
|
|
MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
|
2017-12-11 20:34:11 +08:00
|
|
|
const RISCVInstrInfo *TII = STI.getInstrInfo();
|
|
|
|
|
|
|
|
if (DestReg == SrcReg && Val == 0)
|
|
|
|
return;
|
|
|
|
|
2018-01-11 03:53:46 +08:00
|
|
|
if (isInt<12>(Val)) {
|
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(RISCV::ADDI), DestReg)
|
|
|
|
.addReg(SrcReg)
|
|
|
|
.addImm(Val)
|
|
|
|
.setMIFlag(Flag);
|
2019-09-13 12:03:32 +08:00
|
|
|
} else {
|
2018-01-11 03:53:46 +08:00
|
|
|
unsigned Opc = RISCV::ADD;
|
|
|
|
bool isSub = Val < 0;
|
|
|
|
if (isSub) {
|
|
|
|
Val = -Val;
|
|
|
|
Opc = RISCV::SUB;
|
|
|
|
}
|
|
|
|
|
[risc-v] Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Depends on D65919
Reviewers: lenary
Subscribers: jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision for full review was: https://reviews.llvm.org/D65962
llvm-svn: 368629
2019-08-13 06:41:02 +08:00
|
|
|
Register ScratchReg = MRI.createVirtualRegister(&RISCV::GPRRegClass);
|
2019-09-13 12:03:32 +08:00
|
|
|
TII->movImm(MBB, MBBI, DL, ScratchReg, Val, Flag);
|
2018-01-11 03:53:46 +08:00
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(Opc), DestReg)
|
|
|
|
.addReg(SrcReg)
|
|
|
|
.addReg(ScratchReg, RegState::Kill)
|
|
|
|
.setMIFlag(Flag);
|
|
|
|
}
|
2017-12-11 20:34:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the register used to hold the frame pointer.
|
2019-08-16 22:27:50 +08:00
|
|
|
static Register getFPReg(const RISCVSubtarget &STI) { return RISCV::X8; }
|
2017-12-11 20:34:11 +08:00
|
|
|
|
|
|
|
// Returns the register used to hold the stack pointer.
|
2019-08-16 22:27:50 +08:00
|
|
|
static Register getSPReg(const RISCVSubtarget &STI) { return RISCV::X2; }
|
2017-12-11 20:34:11 +08:00
|
|
|
|
2020-02-12 05:23:03 +08:00
|
|
|
static SmallVector<CalleeSavedInfo, 8>
|
|
|
|
getNonLibcallCSI(const std::vector<CalleeSavedInfo> &CSI) {
|
|
|
|
SmallVector<CalleeSavedInfo, 8> NonLibcallCSI;
|
|
|
|
|
|
|
|
for (auto &CS : CSI)
|
|
|
|
if (CS.getFrameIdx() >= 0)
|
|
|
|
NonLibcallCSI.push_back(CS);
|
|
|
|
|
|
|
|
return NonLibcallCSI;
|
|
|
|
}
|
|
|
|
|
2017-10-20 05:37:38 +08:00
|
|
|
void RISCVFrameLowering::emitPrologue(MachineFunction &MF,
|
2017-12-11 20:34:11 +08:00
|
|
|
MachineBasicBlock &MBB) const {
|
|
|
|
MachineFrameInfo &MFI = MF.getFrameInfo();
|
2018-01-11 03:41:03 +08:00
|
|
|
auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
|
2019-06-12 11:04:22 +08:00
|
|
|
const RISCVRegisterInfo *RI = STI.getRegisterInfo();
|
|
|
|
const RISCVInstrInfo *TII = STI.getInstrInfo();
|
2017-12-11 20:34:11 +08:00
|
|
|
MachineBasicBlock::iterator MBBI = MBB.begin();
|
|
|
|
|
2019-08-16 22:27:50 +08:00
|
|
|
Register FPReg = getFPReg(STI);
|
|
|
|
Register SPReg = getSPReg(STI);
|
2019-10-15 15:11:35 +08:00
|
|
|
Register BPReg = RISCVABI::getBPReg();
|
2017-12-11 20:34:11 +08:00
|
|
|
|
2020-03-27 13:09:31 +08:00
|
|
|
// Debug location must be unknown since the first debug location is used
|
|
|
|
// to determine the end of the prologue.
|
|
|
|
DebugLoc DL;
|
|
|
|
|
|
|
|
// Emit prologue for shadow call stack.
|
|
|
|
emitSCSPrologue(MF, MBB, MBBI, DL);
|
|
|
|
|
2020-02-12 05:23:03 +08:00
|
|
|
// Since spillCalleeSavedRegisters may have inserted a libcall, skip past
|
|
|
|
// any instructions marked as FrameSetup
|
|
|
|
while (MBBI != MBB.end() && MBBI->getFlag(MachineInstr::FrameSetup))
|
|
|
|
++MBBI;
|
|
|
|
|
2017-12-11 20:34:11 +08:00
|
|
|
// Determine the correct frame layout
|
|
|
|
determineFrameLayout(MF);
|
|
|
|
|
2020-02-12 05:23:03 +08:00
|
|
|
// If libcalls are used to spill and restore callee-saved registers, the frame
|
|
|
|
// has two sections; the opaque section managed by the libcalls, and the
|
|
|
|
// section managed by MachineFrameInfo which can also hold callee saved
|
|
|
|
// registers in fixed stack slots, both of which have negative frame indices.
|
|
|
|
// This gets even more complicated when incoming arguments are passed via the
|
|
|
|
// stack, as these too have negative frame indices. An example is detailed
|
|
|
|
// below:
|
|
|
|
//
|
|
|
|
// | incoming arg | <- FI[-3]
|
|
|
|
// | libcallspill |
|
|
|
|
// | calleespill | <- FI[-2]
|
|
|
|
// | calleespill | <- FI[-1]
|
|
|
|
// | this_frame | <- FI[0]
|
|
|
|
//
|
|
|
|
// For negative frame indices, the offset from the frame pointer will differ
|
|
|
|
// depending on which of these groups the frame index applies to.
|
|
|
|
// The following calculates the correct offset knowing the number of callee
|
|
|
|
// saved registers spilt by the two methods.
|
|
|
|
if (int LibCallRegs = getLibCallID(MF, MFI.getCalleeSavedInfo()) + 1) {
|
|
|
|
// Calculate the size of the frame managed by the libcall. The libcalls are
|
|
|
|
// implemented such that the stack will always be 16 byte aligned.
|
|
|
|
unsigned LibCallFrameSize = alignTo((STI.getXLen() / 8) * LibCallRegs, 16);
|
|
|
|
RVFI->setLibCallStackSize(LibCallFrameSize);
|
|
|
|
}
|
|
|
|
|
2017-12-11 20:34:11 +08:00
|
|
|
// FIXME (note copied from Lanai): This appears to be overallocating. Needs
|
|
|
|
// investigation. Get the number of bytes to allocate from the FrameInfo.
|
|
|
|
uint64_t StackSize = MFI.getStackSize();
|
2020-02-12 05:23:03 +08:00
|
|
|
uint64_t RealStackSize = StackSize + RVFI->getLibCallStackSize();
|
2017-12-11 20:34:11 +08:00
|
|
|
|
|
|
|
// Early exit if there is no need to allocate on the stack
|
2020-02-12 05:23:03 +08:00
|
|
|
if (RealStackSize == 0 && !MFI.adjustsStack())
|
2017-12-11 20:34:11 +08:00
|
|
|
return;
|
|
|
|
|
2019-10-23 04:25:01 +08:00
|
|
|
// If the stack pointer has been marked as reserved, then produce an error if
|
|
|
|
// the frame requires stack allocation
|
|
|
|
if (STI.isRegisterReservedByUser(SPReg))
|
|
|
|
MF.getFunction().getContext().diagnose(DiagnosticInfoUnsupported{
|
|
|
|
MF.getFunction(), "Stack pointer required, but has been reserved."});
|
|
|
|
|
[RISCV] Split SP adjustment to reduce the offset of callee saved register spill and restore
We would like to split the SP adjustment to reduce the instructions in
prologue and epilogue as the following case. In this way, the offset of
the callee saved register could fit in a single store.
add sp,sp,-2032
sw ra,2028(sp)
sw s0,2024(sp)
sw s1,2020(sp)
sw s3,2012(sp)
sw s4,2008(sp)
add sp,sp,-64
Differential Revision: https://reviews.llvm.org/D68011
llvm-svn: 373688
2019-10-04 10:00:57 +08:00
|
|
|
uint64_t FirstSPAdjustAmount = getFirstSPAdjustAmount(MF);
|
|
|
|
// Split the SP adjustment to reduce the offsets of callee saved spill.
|
2020-02-12 05:23:03 +08:00
|
|
|
if (FirstSPAdjustAmount) {
|
[RISCV] Split SP adjustment to reduce the offset of callee saved register spill and restore
We would like to split the SP adjustment to reduce the instructions in
prologue and epilogue as the following case. In this way, the offset of
the callee saved register could fit in a single store.
add sp,sp,-2032
sw ra,2028(sp)
sw s0,2024(sp)
sw s1,2020(sp)
sw s3,2012(sp)
sw s4,2008(sp)
add sp,sp,-64
Differential Revision: https://reviews.llvm.org/D68011
llvm-svn: 373688
2019-10-04 10:00:57 +08:00
|
|
|
StackSize = FirstSPAdjustAmount;
|
2020-02-12 05:23:03 +08:00
|
|
|
RealStackSize = FirstSPAdjustAmount;
|
|
|
|
}
|
[RISCV] Split SP adjustment to reduce the offset of callee saved register spill and restore
We would like to split the SP adjustment to reduce the instructions in
prologue and epilogue as the following case. In this way, the offset of
the callee saved register could fit in a single store.
add sp,sp,-2032
sw ra,2028(sp)
sw s0,2024(sp)
sw s1,2020(sp)
sw s3,2012(sp)
sw s4,2008(sp)
add sp,sp,-64
Differential Revision: https://reviews.llvm.org/D68011
llvm-svn: 373688
2019-10-04 10:00:57 +08:00
|
|
|
|
2017-12-11 20:34:11 +08:00
|
|
|
// Allocate space on the stack if necessary.
|
|
|
|
adjustReg(MBB, MBBI, DL, SPReg, SPReg, -StackSize, MachineInstr::FrameSetup);
|
|
|
|
|
2020-02-12 05:23:03 +08:00
|
|
|
// Emit ".cfi_def_cfa_offset RealStackSize"
|
2019-06-12 11:04:22 +08:00
|
|
|
unsigned CFIIndex = MF.addFrameInst(
|
2020-05-23 06:51:24 +08:00
|
|
|
MCCFIInstruction::cfiDefCfaOffset(nullptr, RealStackSize));
|
2019-06-12 11:04:22 +08:00
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
|
|
|
|
.addCFIIndex(CFIIndex);
|
|
|
|
|
2020-02-12 05:23:03 +08:00
|
|
|
const auto &CSI = MFI.getCalleeSavedInfo();
|
|
|
|
|
2017-12-11 20:34:11 +08:00
|
|
|
// The frame pointer is callee-saved, and code has been generated for us to
|
|
|
|
// save it to the stack. We need to skip over the storing of callee-saved
|
|
|
|
// registers as the frame pointer must be modified after it has been saved
|
|
|
|
// to the stack, not before.
|
|
|
|
// FIXME: assumes exactly one instruction is used to save each callee-saved
|
|
|
|
// register.
|
2020-02-12 05:23:03 +08:00
|
|
|
std::advance(MBBI, getNonLibcallCSI(CSI).size());
|
2017-12-11 20:34:11 +08:00
|
|
|
|
2019-06-12 11:04:22 +08:00
|
|
|
// Iterate over list of callee-saved registers and emit .cfi_offset
|
|
|
|
// directives.
|
|
|
|
for (const auto &Entry : CSI) {
|
2020-02-12 05:23:03 +08:00
|
|
|
int FrameIdx = Entry.getFrameIdx();
|
|
|
|
int64_t Offset;
|
|
|
|
// Offsets for objects with fixed locations (IE: those saved by libcall) are
|
|
|
|
// simply calculated from the frame index.
|
|
|
|
if (FrameIdx < 0)
|
|
|
|
Offset = FrameIdx * (int64_t) STI.getXLen() / 8;
|
|
|
|
else
|
|
|
|
Offset = MFI.getObjectOffset(Entry.getFrameIdx()) -
|
|
|
|
RVFI->getLibCallStackSize();
|
2019-08-16 22:27:50 +08:00
|
|
|
Register Reg = Entry.getReg();
|
2019-06-12 11:04:22 +08:00
|
|
|
unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
|
|
|
|
nullptr, RI->getDwarfRegNum(Reg, true), Offset));
|
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
|
|
|
|
.addCFIIndex(CFIIndex);
|
|
|
|
}
|
|
|
|
|
2017-12-11 20:34:11 +08:00
|
|
|
// Generate new FP.
|
2019-06-12 11:04:22 +08:00
|
|
|
if (hasFP(MF)) {
|
2019-10-23 04:25:01 +08:00
|
|
|
if (STI.isRegisterReservedByUser(FPReg))
|
|
|
|
MF.getFunction().getContext().diagnose(DiagnosticInfoUnsupported{
|
|
|
|
MF.getFunction(), "Frame pointer required, but has been reserved."});
|
|
|
|
|
2018-01-18 19:34:02 +08:00
|
|
|
adjustReg(MBB, MBBI, DL, FPReg, SPReg,
|
2020-02-12 05:23:03 +08:00
|
|
|
RealStackSize - RVFI->getVarArgsSaveSize(),
|
|
|
|
MachineInstr::FrameSetup);
|
2019-06-12 11:04:22 +08:00
|
|
|
|
2020-05-23 06:18:05 +08:00
|
|
|
// Emit ".cfi_def_cfa $fp, RVFI->getVarArgsSaveSize()"
|
|
|
|
unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::cfiDefCfa(
|
|
|
|
nullptr, RI->getDwarfRegNum(FPReg, true), RVFI->getVarArgsSaveSize()));
|
2019-06-12 11:04:22 +08:00
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
|
|
|
|
.addCFIIndex(CFIIndex);
|
[RISCV] Split SP adjustment to reduce the offset of callee saved register spill and restore
We would like to split the SP adjustment to reduce the instructions in
prologue and epilogue as the following case. In this way, the offset of
the callee saved register could fit in a single store.
add sp,sp,-2032
sw ra,2028(sp)
sw s0,2024(sp)
sw s1,2020(sp)
sw s3,2012(sp)
sw s4,2008(sp)
add sp,sp,-64
Differential Revision: https://reviews.llvm.org/D68011
llvm-svn: 373688
2019-10-04 10:00:57 +08:00
|
|
|
}
|
[RISCV] Minimal stack realignment support
Summary:
Currently the RISC-V backend does not realign the stack. This can be an issue even for the RV32I/RV64I ABIs (where the stack is 16-byte aligned), though is rare. It will be much more comment with RV32E (though the alignment requirements for common data types remain under-documented...).
This patch adds minimal support for stack realignment. It should cope with large realignments. It will error out if the stack needs realignment and variable sized objects are present.
It feels like a lot of the code like getFrameIndexReference and determineFrameLayout could be refactored somehow, as right now it feels fiddly and brittle. We also seem to allocate a lot more memory than GCC does for equivalent C code.
Reviewers: asb
Reviewed By: asb
Subscribers: wwei, jrtc27, s.egerton, MaskRay, Jim, lenary, hiraditya, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, kito-cheng, shiva0217, zzheng, edward-jones, rogfer01, MartinMosbeck, brucehoult, the_o, rkruppe, PkmX, jocewei, psnobl, benna, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D62007
llvm-svn: 368300
2019-08-08 22:40:54 +08:00
|
|
|
|
[RISCV] Split SP adjustment to reduce the offset of callee saved register spill and restore
We would like to split the SP adjustment to reduce the instructions in
prologue and epilogue as the following case. In this way, the offset of
the callee saved register could fit in a single store.
add sp,sp,-2032
sw ra,2028(sp)
sw s0,2024(sp)
sw s1,2020(sp)
sw s3,2012(sp)
sw s4,2008(sp)
add sp,sp,-64
Differential Revision: https://reviews.llvm.org/D68011
llvm-svn: 373688
2019-10-04 10:00:57 +08:00
|
|
|
// Emit the second SP adjustment after saving callee saved registers.
|
|
|
|
if (FirstSPAdjustAmount) {
|
|
|
|
uint64_t SecondSPAdjustAmount = MFI.getStackSize() - FirstSPAdjustAmount;
|
|
|
|
assert(SecondSPAdjustAmount > 0 &&
|
|
|
|
"SecondSPAdjustAmount should be greater than zero");
|
|
|
|
adjustReg(MBB, MBBI, DL, SPReg, SPReg, -SecondSPAdjustAmount,
|
|
|
|
MachineInstr::FrameSetup);
|
2019-11-11 00:04:43 +08:00
|
|
|
|
|
|
|
// If we are using a frame-pointer, and thus emitted ".cfi_def_cfa fp, 0",
|
|
|
|
// don't emit an sp-based .cfi_def_cfa_offset
|
|
|
|
if (!hasFP(MF)) {
|
|
|
|
// Emit ".cfi_def_cfa_offset StackSize"
|
|
|
|
unsigned CFIIndex = MF.addFrameInst(
|
2020-05-23 06:51:24 +08:00
|
|
|
MCCFIInstruction::cfiDefCfaOffset(nullptr, MFI.getStackSize()));
|
2019-11-11 00:04:43 +08:00
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
|
|
|
|
.addCFIIndex(CFIIndex);
|
|
|
|
}
|
[RISCV] Split SP adjustment to reduce the offset of callee saved register spill and restore
We would like to split the SP adjustment to reduce the instructions in
prologue and epilogue as the following case. In this way, the offset of
the callee saved register could fit in a single store.
add sp,sp,-2032
sw ra,2028(sp)
sw s0,2024(sp)
sw s1,2020(sp)
sw s3,2012(sp)
sw s4,2008(sp)
add sp,sp,-64
Differential Revision: https://reviews.llvm.org/D68011
llvm-svn: 373688
2019-10-04 10:00:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (hasFP(MF)) {
|
[RISCV] Minimal stack realignment support
Summary:
Currently the RISC-V backend does not realign the stack. This can be an issue even for the RV32I/RV64I ABIs (where the stack is 16-byte aligned), though is rare. It will be much more comment with RV32E (though the alignment requirements for common data types remain under-documented...).
This patch adds minimal support for stack realignment. It should cope with large realignments. It will error out if the stack needs realignment and variable sized objects are present.
It feels like a lot of the code like getFrameIndexReference and determineFrameLayout could be refactored somehow, as right now it feels fiddly and brittle. We also seem to allocate a lot more memory than GCC does for equivalent C code.
Reviewers: asb
Reviewed By: asb
Subscribers: wwei, jrtc27, s.egerton, MaskRay, Jim, lenary, hiraditya, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, kito-cheng, shiva0217, zzheng, edward-jones, rogfer01, MartinMosbeck, brucehoult, the_o, rkruppe, PkmX, jocewei, psnobl, benna, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D62007
llvm-svn: 368300
2019-08-08 22:40:54 +08:00
|
|
|
// Realign Stack
|
|
|
|
const RISCVRegisterInfo *RI = STI.getRegisterInfo();
|
|
|
|
if (RI->needsStackRealignment(MF)) {
|
[Alignment][NFC] Deprecate getMaxAlignment
Summary:
This is patch is part of a series to introduce an Alignment type.
See this thread for context: http://lists.llvm.org/pipermail/llvm-dev/2019-July/133851.html
See this patch for the introduction of the type: https://reviews.llvm.org/D64790
Reviewers: courbet
Subscribers: jholewinski, arsenm, dschuff, jyknight, sdardis, nemanjai, jvesely, nhaehnle, sbc100, jgravelle-google, hiraditya, aheejin, kbarton, fedor.sergeev, asb, rbar, johnrusso, simoncook, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, PkmX, jocewei, Jim, lenary, s.egerton, pzheng, sameer.abuasal, apazos, luismarques, kerbowa, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D76348
2020-03-18 17:50:38 +08:00
|
|
|
Align MaxAlignment = MFI.getMaxAlign();
|
[RISCV] Minimal stack realignment support
Summary:
Currently the RISC-V backend does not realign the stack. This can be an issue even for the RV32I/RV64I ABIs (where the stack is 16-byte aligned), though is rare. It will be much more comment with RV32E (though the alignment requirements for common data types remain under-documented...).
This patch adds minimal support for stack realignment. It should cope with large realignments. It will error out if the stack needs realignment and variable sized objects are present.
It feels like a lot of the code like getFrameIndexReference and determineFrameLayout could be refactored somehow, as right now it feels fiddly and brittle. We also seem to allocate a lot more memory than GCC does for equivalent C code.
Reviewers: asb
Reviewed By: asb
Subscribers: wwei, jrtc27, s.egerton, MaskRay, Jim, lenary, hiraditya, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, kito-cheng, shiva0217, zzheng, edward-jones, rogfer01, MartinMosbeck, brucehoult, the_o, rkruppe, PkmX, jocewei, psnobl, benna, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D62007
llvm-svn: 368300
2019-08-08 22:40:54 +08:00
|
|
|
|
|
|
|
const RISCVInstrInfo *TII = STI.getInstrInfo();
|
[Alignment][NFC] Deprecate getMaxAlignment
Summary:
This is patch is part of a series to introduce an Alignment type.
See this thread for context: http://lists.llvm.org/pipermail/llvm-dev/2019-July/133851.html
See this patch for the introduction of the type: https://reviews.llvm.org/D64790
Reviewers: courbet
Subscribers: jholewinski, arsenm, dschuff, jyknight, sdardis, nemanjai, jvesely, nhaehnle, sbc100, jgravelle-google, hiraditya, aheejin, kbarton, fedor.sergeev, asb, rbar, johnrusso, simoncook, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, PkmX, jocewei, Jim, lenary, s.egerton, pzheng, sameer.abuasal, apazos, luismarques, kerbowa, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D76348
2020-03-18 17:50:38 +08:00
|
|
|
if (isInt<12>(-(int)MaxAlignment.value())) {
|
[RISCV] Minimal stack realignment support
Summary:
Currently the RISC-V backend does not realign the stack. This can be an issue even for the RV32I/RV64I ABIs (where the stack is 16-byte aligned), though is rare. It will be much more comment with RV32E (though the alignment requirements for common data types remain under-documented...).
This patch adds minimal support for stack realignment. It should cope with large realignments. It will error out if the stack needs realignment and variable sized objects are present.
It feels like a lot of the code like getFrameIndexReference and determineFrameLayout could be refactored somehow, as right now it feels fiddly and brittle. We also seem to allocate a lot more memory than GCC does for equivalent C code.
Reviewers: asb
Reviewed By: asb
Subscribers: wwei, jrtc27, s.egerton, MaskRay, Jim, lenary, hiraditya, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, kito-cheng, shiva0217, zzheng, edward-jones, rogfer01, MartinMosbeck, brucehoult, the_o, rkruppe, PkmX, jocewei, psnobl, benna, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D62007
llvm-svn: 368300
2019-08-08 22:40:54 +08:00
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(RISCV::ANDI), SPReg)
|
|
|
|
.addReg(SPReg)
|
[Alignment][NFC] Deprecate getMaxAlignment
Summary:
This is patch is part of a series to introduce an Alignment type.
See this thread for context: http://lists.llvm.org/pipermail/llvm-dev/2019-July/133851.html
See this patch for the introduction of the type: https://reviews.llvm.org/D64790
Reviewers: courbet
Subscribers: jholewinski, arsenm, dschuff, jyknight, sdardis, nemanjai, jvesely, nhaehnle, sbc100, jgravelle-google, hiraditya, aheejin, kbarton, fedor.sergeev, asb, rbar, johnrusso, simoncook, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, PkmX, jocewei, Jim, lenary, s.egerton, pzheng, sameer.abuasal, apazos, luismarques, kerbowa, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D76348
2020-03-18 17:50:38 +08:00
|
|
|
.addImm(-(int)MaxAlignment.value());
|
[RISCV] Minimal stack realignment support
Summary:
Currently the RISC-V backend does not realign the stack. This can be an issue even for the RV32I/RV64I ABIs (where the stack is 16-byte aligned), though is rare. It will be much more comment with RV32E (though the alignment requirements for common data types remain under-documented...).
This patch adds minimal support for stack realignment. It should cope with large realignments. It will error out if the stack needs realignment and variable sized objects are present.
It feels like a lot of the code like getFrameIndexReference and determineFrameLayout could be refactored somehow, as right now it feels fiddly and brittle. We also seem to allocate a lot more memory than GCC does for equivalent C code.
Reviewers: asb
Reviewed By: asb
Subscribers: wwei, jrtc27, s.egerton, MaskRay, Jim, lenary, hiraditya, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, kito-cheng, shiva0217, zzheng, edward-jones, rogfer01, MartinMosbeck, brucehoult, the_o, rkruppe, PkmX, jocewei, psnobl, benna, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D62007
llvm-svn: 368300
2019-08-08 22:40:54 +08:00
|
|
|
} else {
|
[Alignment][NFC] Deprecate getMaxAlignment
Summary:
This is patch is part of a series to introduce an Alignment type.
See this thread for context: http://lists.llvm.org/pipermail/llvm-dev/2019-July/133851.html
See this patch for the introduction of the type: https://reviews.llvm.org/D64790
Reviewers: courbet
Subscribers: jholewinski, arsenm, dschuff, jyknight, sdardis, nemanjai, jvesely, nhaehnle, sbc100, jgravelle-google, hiraditya, aheejin, kbarton, fedor.sergeev, asb, rbar, johnrusso, simoncook, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, PkmX, jocewei, Jim, lenary, s.egerton, pzheng, sameer.abuasal, apazos, luismarques, kerbowa, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D76348
2020-03-18 17:50:38 +08:00
|
|
|
unsigned ShiftAmount = Log2(MaxAlignment);
|
2019-08-16 22:27:50 +08:00
|
|
|
Register VR =
|
[RISCV] Minimal stack realignment support
Summary:
Currently the RISC-V backend does not realign the stack. This can be an issue even for the RV32I/RV64I ABIs (where the stack is 16-byte aligned), though is rare. It will be much more comment with RV32E (though the alignment requirements for common data types remain under-documented...).
This patch adds minimal support for stack realignment. It should cope with large realignments. It will error out if the stack needs realignment and variable sized objects are present.
It feels like a lot of the code like getFrameIndexReference and determineFrameLayout could be refactored somehow, as right now it feels fiddly and brittle. We also seem to allocate a lot more memory than GCC does for equivalent C code.
Reviewers: asb
Reviewed By: asb
Subscribers: wwei, jrtc27, s.egerton, MaskRay, Jim, lenary, hiraditya, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, kito-cheng, shiva0217, zzheng, edward-jones, rogfer01, MartinMosbeck, brucehoult, the_o, rkruppe, PkmX, jocewei, psnobl, benna, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D62007
llvm-svn: 368300
2019-08-08 22:40:54 +08:00
|
|
|
MF.getRegInfo().createVirtualRegister(&RISCV::GPRRegClass);
|
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(RISCV::SRLI), VR)
|
|
|
|
.addReg(SPReg)
|
|
|
|
.addImm(ShiftAmount);
|
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(RISCV::SLLI), SPReg)
|
|
|
|
.addReg(VR)
|
|
|
|
.addImm(ShiftAmount);
|
|
|
|
}
|
2019-10-15 15:11:35 +08:00
|
|
|
// FP will be used to restore the frame in the epilogue, so we need
|
|
|
|
// another base register BP to record SP after re-alignment. SP will
|
|
|
|
// track the current stack after allocating variable sized objects.
|
|
|
|
if (hasBP(MF)) {
|
|
|
|
// move BP, SP
|
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(RISCV::ADDI), BPReg)
|
|
|
|
.addReg(SPReg)
|
|
|
|
.addImm(0);
|
|
|
|
}
|
[RISCV] Minimal stack realignment support
Summary:
Currently the RISC-V backend does not realign the stack. This can be an issue even for the RV32I/RV64I ABIs (where the stack is 16-byte aligned), though is rare. It will be much more comment with RV32E (though the alignment requirements for common data types remain under-documented...).
This patch adds minimal support for stack realignment. It should cope with large realignments. It will error out if the stack needs realignment and variable sized objects are present.
It feels like a lot of the code like getFrameIndexReference and determineFrameLayout could be refactored somehow, as right now it feels fiddly and brittle. We also seem to allocate a lot more memory than GCC does for equivalent C code.
Reviewers: asb
Reviewed By: asb
Subscribers: wwei, jrtc27, s.egerton, MaskRay, Jim, lenary, hiraditya, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, kito-cheng, shiva0217, zzheng, edward-jones, rogfer01, MartinMosbeck, brucehoult, the_o, rkruppe, PkmX, jocewei, psnobl, benna, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D62007
llvm-svn: 368300
2019-08-08 22:40:54 +08:00
|
|
|
}
|
2019-06-12 11:04:22 +08:00
|
|
|
}
|
2017-12-11 20:34:11 +08:00
|
|
|
}
|
2017-10-20 05:37:38 +08:00
|
|
|
|
|
|
|
void RISCVFrameLowering::emitEpilogue(MachineFunction &MF,
|
2017-12-11 20:34:11 +08:00
|
|
|
MachineBasicBlock &MBB) const {
|
|
|
|
const RISCVRegisterInfo *RI = STI.getRegisterInfo();
|
|
|
|
MachineFrameInfo &MFI = MF.getFrameInfo();
|
2018-01-11 03:41:03 +08:00
|
|
|
auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
|
2019-08-16 22:27:50 +08:00
|
|
|
Register FPReg = getFPReg(STI);
|
|
|
|
Register SPReg = getSPReg(STI);
|
2017-12-11 20:34:11 +08:00
|
|
|
|
2020-01-15 02:59:11 +08:00
|
|
|
// Get the insert location for the epilogue. If there were no terminators in
|
|
|
|
// the block, get the last instruction.
|
|
|
|
MachineBasicBlock::iterator MBBI = MBB.end();
|
|
|
|
DebugLoc DL;
|
|
|
|
if (!MBB.empty()) {
|
|
|
|
MBBI = MBB.getFirstTerminator();
|
|
|
|
if (MBBI == MBB.end())
|
|
|
|
MBBI = MBB.getLastNonDebugInstr();
|
|
|
|
DL = MBBI->getDebugLoc();
|
|
|
|
|
|
|
|
// If this is not a terminator, the actual insert location should be after the
|
|
|
|
// last instruction.
|
|
|
|
if (!MBBI->isTerminator())
|
|
|
|
MBBI = std::next(MBBI);
|
2020-02-12 05:23:03 +08:00
|
|
|
|
|
|
|
// If callee-saved registers are saved via libcall, place stack adjustment
|
|
|
|
// before this call.
|
|
|
|
while (MBBI != MBB.begin() &&
|
|
|
|
std::prev(MBBI)->getFlag(MachineInstr::FrameDestroy))
|
|
|
|
--MBBI;
|
2020-01-15 02:59:11 +08:00
|
|
|
}
|
|
|
|
|
2020-02-12 05:23:03 +08:00
|
|
|
const auto &CSI = getNonLibcallCSI(MFI.getCalleeSavedInfo());
|
|
|
|
|
2017-12-11 20:34:11 +08:00
|
|
|
// Skip to before the restores of callee-saved registers
|
|
|
|
// FIXME: assumes exactly one instruction is used to restore each
|
|
|
|
// callee-saved register.
|
2020-02-12 05:23:03 +08:00
|
|
|
auto LastFrameDestroy = MBBI;
|
|
|
|
if (!CSI.empty())
|
|
|
|
LastFrameDestroy = std::prev(MBBI, CSI.size());
|
2017-12-11 20:34:11 +08:00
|
|
|
|
|
|
|
uint64_t StackSize = MFI.getStackSize();
|
2020-02-12 05:23:03 +08:00
|
|
|
uint64_t RealStackSize = StackSize + RVFI->getLibCallStackSize();
|
|
|
|
uint64_t FPOffset = RealStackSize - RVFI->getVarArgsSaveSize();
|
2017-12-11 20:34:11 +08:00
|
|
|
|
|
|
|
// Restore the stack pointer using the value of the frame pointer. Only
|
|
|
|
// necessary if the stack pointer was modified, meaning the stack size is
|
|
|
|
// unknown.
|
|
|
|
if (RI->needsStackRealignment(MF) || MFI.hasVarSizedObjects()) {
|
2018-01-18 19:34:02 +08:00
|
|
|
assert(hasFP(MF) && "frame pointer should not have been eliminated");
|
2019-06-12 11:04:22 +08:00
|
|
|
adjustReg(MBB, LastFrameDestroy, DL, SPReg, FPReg, -FPOffset,
|
2017-12-11 20:34:11 +08:00
|
|
|
MachineInstr::FrameDestroy);
|
|
|
|
}
|
|
|
|
|
[RISCV] Split SP adjustment to reduce the offset of callee saved register spill and restore
We would like to split the SP adjustment to reduce the instructions in
prologue and epilogue as the following case. In this way, the offset of
the callee saved register could fit in a single store.
add sp,sp,-2032
sw ra,2028(sp)
sw s0,2024(sp)
sw s1,2020(sp)
sw s3,2012(sp)
sw s4,2008(sp)
add sp,sp,-64
Differential Revision: https://reviews.llvm.org/D68011
llvm-svn: 373688
2019-10-04 10:00:57 +08:00
|
|
|
uint64_t FirstSPAdjustAmount = getFirstSPAdjustAmount(MF);
|
|
|
|
if (FirstSPAdjustAmount) {
|
|
|
|
uint64_t SecondSPAdjustAmount = MFI.getStackSize() - FirstSPAdjustAmount;
|
|
|
|
assert(SecondSPAdjustAmount > 0 &&
|
|
|
|
"SecondSPAdjustAmount should be greater than zero");
|
|
|
|
|
|
|
|
adjustReg(MBB, LastFrameDestroy, DL, SPReg, SPReg, SecondSPAdjustAmount,
|
|
|
|
MachineInstr::FrameDestroy);
|
2019-06-12 11:04:22 +08:00
|
|
|
}
|
|
|
|
|
[RISCV] Split SP adjustment to reduce the offset of callee saved register spill and restore
We would like to split the SP adjustment to reduce the instructions in
prologue and epilogue as the following case. In this way, the offset of
the callee saved register could fit in a single store.
add sp,sp,-2032
sw ra,2028(sp)
sw s0,2024(sp)
sw s1,2020(sp)
sw s3,2012(sp)
sw s4,2008(sp)
add sp,sp,-64
Differential Revision: https://reviews.llvm.org/D68011
llvm-svn: 373688
2019-10-04 10:00:57 +08:00
|
|
|
if (FirstSPAdjustAmount)
|
|
|
|
StackSize = FirstSPAdjustAmount;
|
|
|
|
|
2017-12-11 20:34:11 +08:00
|
|
|
// Deallocate stack
|
|
|
|
adjustReg(MBB, MBBI, DL, SPReg, SPReg, StackSize, MachineInstr::FrameDestroy);
|
2020-03-27 13:09:31 +08:00
|
|
|
|
|
|
|
// Emit epilogue for shadow call stack.
|
|
|
|
emitSCSEpilogue(MF, MBB, MBBI, DL);
|
2017-12-11 20:34:11 +08:00
|
|
|
}
|
2017-12-11 19:53:54 +08:00
|
|
|
|
2020-11-04 16:56:54 +08:00
|
|
|
StackOffset
|
|
|
|
RISCVFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
|
|
|
|
Register &FrameReg) const {
|
2017-12-11 19:53:54 +08:00
|
|
|
const MachineFrameInfo &MFI = MF.getFrameInfo();
|
|
|
|
const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
|
2018-01-11 03:41:03 +08:00
|
|
|
const auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
|
2017-12-11 19:53:54 +08:00
|
|
|
|
|
|
|
// Callee-saved registers should be referenced relative to the stack
|
|
|
|
// pointer (positive offset), otherwise use the frame pointer (negative
|
|
|
|
// offset).
|
2020-02-12 05:23:03 +08:00
|
|
|
const auto &CSI = getNonLibcallCSI(MFI.getCalleeSavedInfo());
|
2017-12-11 19:53:54 +08:00
|
|
|
int MinCSFI = 0;
|
|
|
|
int MaxCSFI = -1;
|
|
|
|
|
|
|
|
int Offset = MFI.getObjectOffset(FI) - getOffsetOfLocalArea() +
|
|
|
|
MFI.getOffsetAdjustment();
|
|
|
|
|
[RISCV] Split SP adjustment to reduce the offset of callee saved register spill and restore
We would like to split the SP adjustment to reduce the instructions in
prologue and epilogue as the following case. In this way, the offset of
the callee saved register could fit in a single store.
add sp,sp,-2032
sw ra,2028(sp)
sw s0,2024(sp)
sw s1,2020(sp)
sw s3,2012(sp)
sw s4,2008(sp)
add sp,sp,-64
Differential Revision: https://reviews.llvm.org/D68011
llvm-svn: 373688
2019-10-04 10:00:57 +08:00
|
|
|
uint64_t FirstSPAdjustAmount = getFirstSPAdjustAmount(MF);
|
|
|
|
|
2017-12-11 19:53:54 +08:00
|
|
|
if (CSI.size()) {
|
|
|
|
MinCSFI = CSI[0].getFrameIdx();
|
|
|
|
MaxCSFI = CSI[CSI.size() - 1].getFrameIdx();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FI >= MinCSFI && FI <= MaxCSFI) {
|
|
|
|
FrameReg = RISCV::X2;
|
[RISCV] Split SP adjustment to reduce the offset of callee saved register spill and restore
We would like to split the SP adjustment to reduce the instructions in
prologue and epilogue as the following case. In this way, the offset of
the callee saved register could fit in a single store.
add sp,sp,-2032
sw ra,2028(sp)
sw s0,2024(sp)
sw s1,2020(sp)
sw s3,2012(sp)
sw s4,2008(sp)
add sp,sp,-64
Differential Revision: https://reviews.llvm.org/D68011
llvm-svn: 373688
2019-10-04 10:00:57 +08:00
|
|
|
|
|
|
|
if (FirstSPAdjustAmount)
|
|
|
|
Offset += FirstSPAdjustAmount;
|
|
|
|
else
|
2020-02-12 05:23:03 +08:00
|
|
|
Offset += MFI.getStackSize();
|
2019-10-15 15:11:35 +08:00
|
|
|
} else if (RI->needsStackRealignment(MF) && !MFI.isFixedObjectIndex(FI)) {
|
[RISCV] Minimal stack realignment support
Summary:
Currently the RISC-V backend does not realign the stack. This can be an issue even for the RV32I/RV64I ABIs (where the stack is 16-byte aligned), though is rare. It will be much more comment with RV32E (though the alignment requirements for common data types remain under-documented...).
This patch adds minimal support for stack realignment. It should cope with large realignments. It will error out if the stack needs realignment and variable sized objects are present.
It feels like a lot of the code like getFrameIndexReference and determineFrameLayout could be refactored somehow, as right now it feels fiddly and brittle. We also seem to allocate a lot more memory than GCC does for equivalent C code.
Reviewers: asb
Reviewed By: asb
Subscribers: wwei, jrtc27, s.egerton, MaskRay, Jim, lenary, hiraditya, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, kito-cheng, shiva0217, zzheng, edward-jones, rogfer01, MartinMosbeck, brucehoult, the_o, rkruppe, PkmX, jocewei, psnobl, benna, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D62007
llvm-svn: 368300
2019-08-08 22:40:54 +08:00
|
|
|
// If the stack was realigned, the frame pointer is set in order to allow
|
2019-10-15 15:11:35 +08:00
|
|
|
// SP to be restored, so we need another base register to record the stack
|
|
|
|
// after realignment.
|
|
|
|
if (hasBP(MF))
|
|
|
|
FrameReg = RISCVABI::getBPReg();
|
|
|
|
else
|
|
|
|
FrameReg = RISCV::X2;
|
2020-02-12 05:23:03 +08:00
|
|
|
Offset += MFI.getStackSize();
|
|
|
|
if (FI < 0)
|
|
|
|
Offset += RVFI->getLibCallStackSize();
|
2018-01-11 03:41:03 +08:00
|
|
|
} else {
|
|
|
|
FrameReg = RI->getFrameRegister(MF);
|
2020-02-12 05:23:03 +08:00
|
|
|
if (hasFP(MF)) {
|
2018-01-18 19:34:02 +08:00
|
|
|
Offset += RVFI->getVarArgsSaveSize();
|
2020-02-12 05:23:03 +08:00
|
|
|
if (FI >= 0)
|
|
|
|
Offset -= RVFI->getLibCallStackSize();
|
|
|
|
} else {
|
|
|
|
Offset += MFI.getStackSize();
|
|
|
|
if (FI < 0)
|
|
|
|
Offset += RVFI->getLibCallStackSize();
|
|
|
|
}
|
2017-12-11 19:53:54 +08:00
|
|
|
}
|
2020-11-04 16:56:54 +08:00
|
|
|
return StackOffset::getFixed(Offset);
|
2017-12-11 19:53:54 +08:00
|
|
|
}
|
2017-12-11 20:34:11 +08:00
|
|
|
|
|
|
|
void RISCVFrameLowering::determineCalleeSaves(MachineFunction &MF,
|
|
|
|
BitVector &SavedRegs,
|
|
|
|
RegScavenger *RS) const {
|
|
|
|
TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
|
2018-01-18 19:34:02 +08:00
|
|
|
// Unconditionally spill RA and FP only if the function uses a frame
|
|
|
|
// pointer.
|
|
|
|
if (hasFP(MF)) {
|
|
|
|
SavedRegs.set(RISCV::X1);
|
|
|
|
SavedRegs.set(RISCV::X8);
|
|
|
|
}
|
2019-10-15 15:11:35 +08:00
|
|
|
// Mark BP as used if function has dedicated base pointer.
|
|
|
|
if (hasBP(MF))
|
|
|
|
SavedRegs.set(RISCVABI::getBPReg());
|
[RISCV] Add support for _interrupt attribute
- Save/restore only registers that are used.
This includes Callee saved registers and Caller saved registers
(arguments and temporaries) for integer and FP registers.
- If there is a call in the interrupt handler, save/restore all
Caller saved registers (arguments and temporaries) and all FP registers.
- Emit special return instructions depending on "interrupt"
attribute type.
Based on initial patch by Zhaoshi Zheng.
Reviewers: asb
Reviewed By: asb
Subscribers: rkruppe, the_o, MartinMosbeck, brucehoult, rbar, johnrusso, simoncook, sabuasal, niosHD, kito-cheng, shiva0217, zzheng, edward-jones, mgrang, rogfer01, llvm-commits
Differential Revision: https://reviews.llvm.org/D48411
llvm-svn: 338047
2018-07-27 01:49:43 +08:00
|
|
|
|
|
|
|
// If interrupt is enabled and there are calls in the handler,
|
|
|
|
// unconditionally save all Caller-saved registers and
|
|
|
|
// all FP registers, regardless whether they are used.
|
|
|
|
MachineFrameInfo &MFI = MF.getFrameInfo();
|
|
|
|
|
|
|
|
if (MF.getFunction().hasFnAttribute("interrupt") && MFI.hasCalls()) {
|
|
|
|
|
|
|
|
static const MCPhysReg CSRegs[] = { RISCV::X1, /* ra */
|
|
|
|
RISCV::X5, RISCV::X6, RISCV::X7, /* t0-t2 */
|
|
|
|
RISCV::X10, RISCV::X11, /* a0-a1, a2-a7 */
|
|
|
|
RISCV::X12, RISCV::X13, RISCV::X14, RISCV::X15, RISCV::X16, RISCV::X17,
|
|
|
|
RISCV::X28, RISCV::X29, RISCV::X30, RISCV::X31, 0 /* t3-t6 */
|
|
|
|
};
|
|
|
|
|
|
|
|
for (unsigned i = 0; CSRegs[i]; ++i)
|
|
|
|
SavedRegs.set(CSRegs[i]);
|
|
|
|
|
|
|
|
if (MF.getSubtarget<RISCVSubtarget>().hasStdExtD() ||
|
|
|
|
MF.getSubtarget<RISCVSubtarget>().hasStdExtF()) {
|
|
|
|
|
|
|
|
// If interrupt is enabled, this list contains all FP registers.
|
|
|
|
const MCPhysReg * Regs = MF.getRegInfo().getCalleeSavedRegs();
|
|
|
|
|
|
|
|
for (unsigned i = 0; Regs[i]; ++i)
|
|
|
|
if (RISCV::FPR32RegClass.contains(Regs[i]) ||
|
|
|
|
RISCV::FPR64RegClass.contains(Regs[i]))
|
|
|
|
SavedRegs.set(Regs[i]);
|
|
|
|
}
|
|
|
|
}
|
2017-12-11 20:34:11 +08:00
|
|
|
}
|
2018-01-11 19:17:19 +08:00
|
|
|
|
|
|
|
void RISCVFrameLowering::processFunctionBeforeFrameFinalized(
|
|
|
|
MachineFunction &MF, RegScavenger *RS) const {
|
|
|
|
const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
|
|
|
|
MachineFrameInfo &MFI = MF.getFrameInfo();
|
|
|
|
const TargetRegisterClass *RC = &RISCV::GPRRegClass;
|
|
|
|
// estimateStackSize has been observed to under-estimate the final stack
|
|
|
|
// size, so give ourselves wiggle-room by checking for stack size
|
|
|
|
// representable an 11-bit signed field rather than 12-bits.
|
|
|
|
// FIXME: It may be possible to craft a function with a small stack that
|
|
|
|
// still needs an emergency spill slot for branch relaxation. This case
|
|
|
|
// would currently be missed.
|
|
|
|
if (!isInt<11>(MFI.estimateStackSize(MF))) {
|
2020-07-01 15:28:11 +08:00
|
|
|
int RegScavFI = MFI.CreateStackObject(RegInfo->getSpillSize(*RC),
|
|
|
|
RegInfo->getSpillAlign(*RC), false);
|
2018-01-11 19:17:19 +08:00
|
|
|
RS->addScavengingFrameIndex(RegScavFI);
|
|
|
|
}
|
|
|
|
}
|
2018-03-20 09:39:17 +08:00
|
|
|
|
|
|
|
// Not preserve stack space within prologue for outgoing variables when the
|
|
|
|
// function contains variable size objects and let eliminateCallFramePseudoInstr
|
|
|
|
// preserve stack space for it.
|
|
|
|
bool RISCVFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
|
|
|
|
return !MF.getFrameInfo().hasVarSizedObjects();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Eliminate ADJCALLSTACKDOWN, ADJCALLSTACKUP pseudo instructions.
|
|
|
|
MachineBasicBlock::iterator RISCVFrameLowering::eliminateCallFramePseudoInstr(
|
|
|
|
MachineFunction &MF, MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MI) const {
|
2019-08-16 22:27:50 +08:00
|
|
|
Register SPReg = RISCV::X2;
|
2018-03-20 09:39:17 +08:00
|
|
|
DebugLoc DL = MI->getDebugLoc();
|
|
|
|
|
|
|
|
if (!hasReservedCallFrame(MF)) {
|
|
|
|
// If space has not been reserved for a call frame, ADJCALLSTACKDOWN and
|
|
|
|
// ADJCALLSTACKUP must be converted to instructions manipulating the stack
|
|
|
|
// pointer. This is necessary when there is a variable length stack
|
|
|
|
// allocation (e.g. alloca), which means it's not possible to allocate
|
|
|
|
// space for outgoing arguments from within the function prologue.
|
|
|
|
int64_t Amount = MI->getOperand(0).getImm();
|
|
|
|
|
|
|
|
if (Amount != 0) {
|
|
|
|
// Ensure the stack remains aligned after adjustment.
|
|
|
|
Amount = alignSPAdjust(Amount);
|
|
|
|
|
|
|
|
if (MI->getOpcode() == RISCV::ADJCALLSTACKDOWN)
|
|
|
|
Amount = -Amount;
|
|
|
|
|
|
|
|
adjustReg(MBB, MI, DL, SPReg, SPReg, Amount, MachineInstr::NoFlags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return MBB.erase(MI);
|
|
|
|
}
|
[RISCV] Split SP adjustment to reduce the offset of callee saved register spill and restore
We would like to split the SP adjustment to reduce the instructions in
prologue and epilogue as the following case. In this way, the offset of
the callee saved register could fit in a single store.
add sp,sp,-2032
sw ra,2028(sp)
sw s0,2024(sp)
sw s1,2020(sp)
sw s3,2012(sp)
sw s4,2008(sp)
add sp,sp,-64
Differential Revision: https://reviews.llvm.org/D68011
llvm-svn: 373688
2019-10-04 10:00:57 +08:00
|
|
|
|
|
|
|
// We would like to split the SP adjustment to reduce prologue/epilogue
|
|
|
|
// as following instructions. In this way, the offset of the callee saved
|
|
|
|
// register could fit in a single store.
|
|
|
|
// add sp,sp,-2032
|
|
|
|
// sw ra,2028(sp)
|
|
|
|
// sw s0,2024(sp)
|
|
|
|
// sw s1,2020(sp)
|
|
|
|
// sw s3,2012(sp)
|
|
|
|
// sw s4,2008(sp)
|
|
|
|
// add sp,sp,-64
|
|
|
|
uint64_t
|
|
|
|
RISCVFrameLowering::getFirstSPAdjustAmount(const MachineFunction &MF) const {
|
2020-02-12 05:23:03 +08:00
|
|
|
const auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
|
[RISCV] Split SP adjustment to reduce the offset of callee saved register spill and restore
We would like to split the SP adjustment to reduce the instructions in
prologue and epilogue as the following case. In this way, the offset of
the callee saved register could fit in a single store.
add sp,sp,-2032
sw ra,2028(sp)
sw s0,2024(sp)
sw s1,2020(sp)
sw s3,2012(sp)
sw s4,2008(sp)
add sp,sp,-64
Differential Revision: https://reviews.llvm.org/D68011
llvm-svn: 373688
2019-10-04 10:00:57 +08:00
|
|
|
const MachineFrameInfo &MFI = MF.getFrameInfo();
|
|
|
|
const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
|
|
|
|
uint64_t StackSize = MFI.getStackSize();
|
|
|
|
|
2020-02-12 05:23:03 +08:00
|
|
|
// Disable SplitSPAdjust if save-restore libcall used. The callee saved
|
|
|
|
// registers will be pushed by the save-restore libcalls, so we don't have to
|
|
|
|
// split the SP adjustment in this case.
|
|
|
|
if (RVFI->getLibCallStackSize())
|
|
|
|
return 0;
|
|
|
|
|
[RISCV] Split SP adjustment to reduce the offset of callee saved register spill and restore
We would like to split the SP adjustment to reduce the instructions in
prologue and epilogue as the following case. In this way, the offset of
the callee saved register could fit in a single store.
add sp,sp,-2032
sw ra,2028(sp)
sw s0,2024(sp)
sw s1,2020(sp)
sw s3,2012(sp)
sw s4,2008(sp)
add sp,sp,-64
Differential Revision: https://reviews.llvm.org/D68011
llvm-svn: 373688
2019-10-04 10:00:57 +08:00
|
|
|
// Return the FirstSPAdjustAmount if the StackSize can not fit in signed
|
|
|
|
// 12-bit and there exists a callee saved register need to be pushed.
|
|
|
|
if (!isInt<12>(StackSize) && (CSI.size() > 0)) {
|
|
|
|
// FirstSPAdjustAmount is choosed as (2048 - StackAlign)
|
|
|
|
// because 2048 will cause sp = sp + 2048 in epilogue split into
|
|
|
|
// multi-instructions. The offset smaller than 2048 can fit in signle
|
|
|
|
// load/store instruction and we have to stick with the stack alignment.
|
|
|
|
// 2048 is 16-byte alignment. The stack alignment for RV32 and RV64 is 16,
|
|
|
|
// for RV32E is 4. So (2048 - StackAlign) will satisfy the stack alignment.
|
[Alignment][NFC] Use TFL::getStackAlign()
Summary:
This is patch is part of a series to introduce an Alignment type.
See this thread for context: http://lists.llvm.org/pipermail/llvm-dev/2019-July/133851.html
See this patch for the introduction of the type: https://reviews.llvm.org/D64790
Reviewers: courbet
Subscribers: dylanmckay, sdardis, nemanjai, hiraditya, kbarton, asb, rbar, johnrusso, simoncook, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, PkmX, jocewei, Jim, lenary, s.egerton, pzheng, sameer.abuasal, apazos, luismarques, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D76551
2020-03-22 04:41:33 +08:00
|
|
|
return 2048 - getStackAlign().value();
|
[RISCV] Split SP adjustment to reduce the offset of callee saved register spill and restore
We would like to split the SP adjustment to reduce the instructions in
prologue and epilogue as the following case. In this way, the offset of
the callee saved register could fit in a single store.
add sp,sp,-2032
sw ra,2028(sp)
sw s0,2024(sp)
sw s1,2020(sp)
sw s3,2012(sp)
sw s4,2008(sp)
add sp,sp,-64
Differential Revision: https://reviews.llvm.org/D68011
llvm-svn: 373688
2019-10-04 10:00:57 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2020-02-12 05:23:03 +08:00
|
|
|
|
|
|
|
bool RISCVFrameLowering::spillCalleeSavedRegisters(
|
|
|
|
MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
|
|
|
|
ArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
|
|
|
|
if (CSI.empty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
MachineFunction *MF = MBB.getParent();
|
|
|
|
const TargetInstrInfo &TII = *MF->getSubtarget().getInstrInfo();
|
|
|
|
DebugLoc DL;
|
|
|
|
if (MI != MBB.end() && !MI->isDebugInstr())
|
|
|
|
DL = MI->getDebugLoc();
|
|
|
|
|
|
|
|
const char *SpillLibCall = getSpillLibCallName(*MF, CSI);
|
|
|
|
if (SpillLibCall) {
|
|
|
|
// Add spill libcall via non-callee-saved register t0.
|
|
|
|
BuildMI(MBB, MI, DL, TII.get(RISCV::PseudoCALLReg), RISCV::X5)
|
|
|
|
.addExternalSymbol(SpillLibCall, RISCVII::MO_CALL)
|
|
|
|
.setMIFlag(MachineInstr::FrameSetup);
|
|
|
|
|
|
|
|
// Add registers spilled in libcall as liveins.
|
|
|
|
for (auto &CS : CSI)
|
|
|
|
MBB.addLiveIn(CS.getReg());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Manually spill values not spilled by libcall.
|
|
|
|
const auto &NonLibcallCSI = getNonLibcallCSI(CSI);
|
|
|
|
for (auto &CS : NonLibcallCSI) {
|
|
|
|
// Insert the spill to the stack frame.
|
|
|
|
Register Reg = CS.getReg();
|
|
|
|
const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
|
|
|
|
TII.storeRegToStackSlot(MBB, MI, Reg, true, CS.getFrameIdx(), RC, TRI);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RISCVFrameLowering::restoreCalleeSavedRegisters(
|
|
|
|
MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
|
2020-02-29 16:50:23 +08:00
|
|
|
MutableArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
|
2020-02-12 05:23:03 +08:00
|
|
|
if (CSI.empty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
MachineFunction *MF = MBB.getParent();
|
|
|
|
const TargetInstrInfo &TII = *MF->getSubtarget().getInstrInfo();
|
|
|
|
DebugLoc DL;
|
|
|
|
if (MI != MBB.end() && !MI->isDebugInstr())
|
|
|
|
DL = MI->getDebugLoc();
|
|
|
|
|
|
|
|
// Manually restore values not restored by libcall. Insert in reverse order.
|
|
|
|
// loadRegFromStackSlot can insert multiple instructions.
|
|
|
|
const auto &NonLibcallCSI = getNonLibcallCSI(CSI);
|
|
|
|
for (auto &CS : reverse(NonLibcallCSI)) {
|
|
|
|
Register Reg = CS.getReg();
|
|
|
|
const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
|
|
|
|
TII.loadRegFromStackSlot(MBB, MI, Reg, CS.getFrameIdx(), RC, TRI);
|
|
|
|
assert(MI != MBB.begin() && "loadRegFromStackSlot didn't insert any code!");
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *RestoreLibCall = getRestoreLibCallName(*MF, CSI);
|
|
|
|
if (RestoreLibCall) {
|
|
|
|
// Add restore libcall via tail call.
|
|
|
|
MachineBasicBlock::iterator NewMI =
|
|
|
|
BuildMI(MBB, MI, DL, TII.get(RISCV::PseudoTAIL))
|
|
|
|
.addExternalSymbol(RestoreLibCall, RISCVII::MO_CALL)
|
|
|
|
.setMIFlag(MachineInstr::FrameDestroy);
|
|
|
|
|
|
|
|
// Remove trailing returns, since the terminator is now a tail call to the
|
|
|
|
// restore function.
|
|
|
|
if (MI != MBB.end() && MI->getOpcode() == RISCV::PseudoRET) {
|
|
|
|
NewMI->copyImplicitOps(*MF, *MI);
|
|
|
|
MI->eraseFromParent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RISCVFrameLowering::canUseAsPrologue(const MachineBasicBlock &MBB) const {
|
|
|
|
MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB);
|
2020-07-01 03:41:03 +08:00
|
|
|
const MachineFunction *MF = MBB.getParent();
|
|
|
|
const auto *RVFI = MF->getInfo<RISCVMachineFunctionInfo>();
|
2020-02-12 05:23:03 +08:00
|
|
|
|
2020-07-01 03:41:03 +08:00
|
|
|
if (!RVFI->useSaveRestoreLibCalls(*MF))
|
2020-02-12 05:23:03 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Inserting a call to a __riscv_save libcall requires the use of the register
|
|
|
|
// t0 (X5) to hold the return address. Therefore if this register is already
|
|
|
|
// used we can't insert the call.
|
|
|
|
|
|
|
|
RegScavenger RS;
|
|
|
|
RS.enterBasicBlock(*TmpMBB);
|
|
|
|
return !RS.isRegUsed(RISCV::X5);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RISCVFrameLowering::canUseAsEpilogue(const MachineBasicBlock &MBB) const {
|
2020-07-01 03:41:03 +08:00
|
|
|
const MachineFunction *MF = MBB.getParent();
|
2020-02-12 05:23:03 +08:00
|
|
|
MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB);
|
2020-07-01 03:41:03 +08:00
|
|
|
const auto *RVFI = MF->getInfo<RISCVMachineFunctionInfo>();
|
2020-02-12 05:23:03 +08:00
|
|
|
|
2020-07-01 03:41:03 +08:00
|
|
|
if (!RVFI->useSaveRestoreLibCalls(*MF))
|
2020-02-12 05:23:03 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Using the __riscv_restore libcalls to restore CSRs requires a tail call.
|
|
|
|
// This means if we still need to continue executing code within this function
|
|
|
|
// the restore cannot take place in this basic block.
|
|
|
|
|
|
|
|
if (MBB.succ_size() > 1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
MachineBasicBlock *SuccMBB =
|
|
|
|
MBB.succ_empty() ? TmpMBB->getFallThrough() : *MBB.succ_begin();
|
|
|
|
|
|
|
|
// Doing a tail call should be safe if there are no successors, because either
|
|
|
|
// we have a returning block or the end of the block is unreachable, so the
|
|
|
|
// restore will be eliminated regardless.
|
|
|
|
if (!SuccMBB)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// The successor can only contain a return, since we would effectively be
|
|
|
|
// replacing the successor with our own tail return at the end of our block.
|
|
|
|
return SuccMBB->isReturnBlock() && SuccMBB->size() == 1;
|
|
|
|
}
|