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-06-12 11:04:22 +08:00
|
|
|
#include "llvm/MC/MCDwarf.h"
|
2017-10-20 05:37:38 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
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
|
|
|
|
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.
|
[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
|
|
|
unsigned StackAlign = getStackAlignment();
|
|
|
|
if (RI->needsStackRealignment(MF)) {
|
|
|
|
unsigned MaxStackAlign = std::max(StackAlign, MFI.getMaxAlignment());
|
|
|
|
FrameSize += (MaxStackAlign - StackAlign);
|
|
|
|
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
|
|
|
|
2017-10-20 05:37:38 +08:00
|
|
|
void RISCVFrameLowering::emitPrologue(MachineFunction &MF,
|
2017-12-11 20:34:11 +08:00
|
|
|
MachineBasicBlock &MBB) const {
|
|
|
|
assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
[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) && MFI.hasVarSizedObjects()) {
|
|
|
|
report_fatal_error(
|
|
|
|
"RISC-V backend can't currently handle functions that need stack "
|
|
|
|
"realignment and have variable sized objects");
|
|
|
|
}
|
|
|
|
|
2019-08-16 22:27:50 +08:00
|
|
|
Register FPReg = getFPReg(STI);
|
|
|
|
Register SPReg = getSPReg(STI);
|
2017-12-11 20:34:11 +08:00
|
|
|
|
|
|
|
// Debug location must be unknown since the first debug location is used
|
|
|
|
// to determine the end of the prologue.
|
|
|
|
DebugLoc DL;
|
|
|
|
|
|
|
|
// Determine the correct frame layout
|
|
|
|
determineFrameLayout(MF);
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
|
|
|
// Early exit if there is no need to allocate on the stack
|
|
|
|
if (StackSize == 0 && !MFI.adjustsStack())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Allocate space on the stack if necessary.
|
|
|
|
adjustReg(MBB, MBBI, DL, SPReg, SPReg, -StackSize, MachineInstr::FrameSetup);
|
|
|
|
|
2019-06-12 11:04:22 +08:00
|
|
|
// Emit ".cfi_def_cfa_offset StackSize"
|
|
|
|
unsigned CFIIndex = MF.addFrameInst(
|
|
|
|
MCCFIInstruction::createDefCfaOffset(nullptr, -StackSize));
|
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
|
|
|
|
.addCFIIndex(CFIIndex);
|
|
|
|
|
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.
|
|
|
|
const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
|
|
|
|
std::advance(MBBI, CSI.size());
|
|
|
|
|
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) {
|
|
|
|
int64_t Offset = MFI.getObjectOffset(Entry.getFrameIdx());
|
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)) {
|
2018-01-18 19:34:02 +08:00
|
|
|
adjustReg(MBB, MBBI, DL, FPReg, SPReg,
|
|
|
|
StackSize - RVFI->getVarArgsSaveSize(), MachineInstr::FrameSetup);
|
2019-06-12 11:04:22 +08:00
|
|
|
|
|
|
|
// Emit ".cfi_def_cfa $fp, 0"
|
|
|
|
unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createDefCfa(
|
|
|
|
nullptr, RI->getDwarfRegNum(FPReg, true), 0));
|
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
|
|
|
|
.addCFIIndex(CFIIndex);
|
[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)) {
|
|
|
|
unsigned MaxAlignment = MFI.getMaxAlignment();
|
|
|
|
|
|
|
|
const RISCVInstrInfo *TII = STI.getInstrInfo();
|
|
|
|
if (isInt<12>(-(int)MaxAlignment)) {
|
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(RISCV::ANDI), SPReg)
|
|
|
|
.addReg(SPReg)
|
|
|
|
.addImm(-(int)MaxAlignment);
|
|
|
|
} else {
|
|
|
|
unsigned ShiftAmount = countTrailingZeros(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-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 {
|
|
|
|
MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
|
|
|
|
const RISCVRegisterInfo *RI = STI.getRegisterInfo();
|
|
|
|
MachineFrameInfo &MFI = MF.getFrameInfo();
|
2018-01-11 03:41:03 +08:00
|
|
|
auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
|
2017-12-11 20:34:11 +08:00
|
|
|
DebugLoc DL = MBBI->getDebugLoc();
|
2019-06-12 11:04:22 +08:00
|
|
|
const RISCVInstrInfo *TII = STI.getInstrInfo();
|
2019-08-16 22:27:50 +08:00
|
|
|
Register FPReg = getFPReg(STI);
|
|
|
|
Register SPReg = getSPReg(STI);
|
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.
|
[RISCV] Fix std::advance slowness
Summary:
It seems std::advance template is treating "-MFI.getCalleeSavedInfo().size()"
as a large unsigned value", causing slowness.
Thanks to Henrik Gustafsson for reporting the issue.
Reviewers: asb
Reviewed By: asb
Subscribers: llvm-commits, rbar, johnrusso, simoncook, sabuasal, niosHD, kito-cheng, shiva0217, zzheng, edward-jones, mgrang, rogfer01, MartinMosbeck, brucehoult, the_o, rkruppe, PkmX, jocewei, asb
Differential Revision: https://reviews.llvm.org/D51148
llvm-svn: 340669
2018-08-25 07:13:59 +08:00
|
|
|
auto LastFrameDestroy = std::prev(MBBI, MFI.getCalleeSavedInfo().size());
|
2017-12-11 20:34:11 +08:00
|
|
|
|
|
|
|
uint64_t StackSize = MFI.getStackSize();
|
2019-06-12 11:04:22 +08:00
|
|
|
uint64_t FPOffset = StackSize - 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);
|
|
|
|
}
|
|
|
|
|
2019-06-12 11:04:22 +08:00
|
|
|
if (hasFP(MF)) {
|
|
|
|
// To find the instruction restoring FP from stack.
|
|
|
|
for (auto &I = LastFrameDestroy; I != MBBI; ++I) {
|
|
|
|
if (I->mayLoad() && I->getOperand(0).isReg()) {
|
[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 DestReg = I->getOperand(0).getReg();
|
2019-06-12 11:04:22 +08:00
|
|
|
if (DestReg == FPReg) {
|
|
|
|
// If there is frame pointer, after restoring $fp registers, we
|
|
|
|
// need adjust CFA to ($sp - FPOffset).
|
|
|
|
// Emit ".cfi_def_cfa $sp, -FPOffset"
|
|
|
|
unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createDefCfa(
|
|
|
|
nullptr, RI->getDwarfRegNum(SPReg, true), -FPOffset));
|
|
|
|
BuildMI(MBB, std::next(I), DL,
|
|
|
|
TII->get(TargetOpcode::CFI_INSTRUCTION))
|
|
|
|
.addCFIIndex(CFIIndex);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add CFI directives for callee-saved registers.
|
|
|
|
const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
|
|
|
|
// Iterate over list of callee-saved registers and emit .cfi_restore
|
|
|
|
// directives.
|
|
|
|
for (const auto &Entry : CSI) {
|
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::createRestore(
|
|
|
|
nullptr, RI->getDwarfRegNum(Reg, true)));
|
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
|
|
|
|
.addCFIIndex(CFIIndex);
|
|
|
|
}
|
|
|
|
|
2017-12-11 20:34:11 +08:00
|
|
|
// Deallocate stack
|
|
|
|
adjustReg(MBB, MBBI, DL, SPReg, SPReg, StackSize, MachineInstr::FrameDestroy);
|
2019-06-12 11:04:22 +08:00
|
|
|
|
|
|
|
// After restoring $sp, we need to adjust CFA to $(sp + 0)
|
|
|
|
// Emit ".cfi_def_cfa_offset 0"
|
|
|
|
unsigned CFIIndex =
|
|
|
|
MF.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, 0));
|
|
|
|
BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
|
|
|
|
.addCFIIndex(CFIIndex);
|
2017-12-11 20:34:11 +08:00
|
|
|
}
|
2017-12-11 19:53:54 +08:00
|
|
|
|
|
|
|
int RISCVFrameLowering::getFrameIndexReference(const MachineFunction &MF,
|
|
|
|
int FI,
|
|
|
|
unsigned &FrameReg) const {
|
|
|
|
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).
|
|
|
|
const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
|
|
|
|
int MinCSFI = 0;
|
|
|
|
int MaxCSFI = -1;
|
|
|
|
|
|
|
|
int Offset = MFI.getObjectOffset(FI) - getOffsetOfLocalArea() +
|
|
|
|
MFI.getOffsetAdjustment();
|
|
|
|
|
|
|
|
if (CSI.size()) {
|
|
|
|
MinCSFI = CSI[0].getFrameIdx();
|
|
|
|
MaxCSFI = CSI[CSI.size() - 1].getFrameIdx();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FI >= MinCSFI && FI <= MaxCSFI) {
|
|
|
|
FrameReg = RISCV::X2;
|
|
|
|
Offset += MF.getFrameInfo().getStackSize();
|
[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 if (RI->needsStackRealignment(MF)) {
|
|
|
|
assert(!MFI.hasVarSizedObjects() &&
|
|
|
|
"Unexpected combination of stack realignment and varsized objects");
|
|
|
|
// If the stack was realigned, the frame pointer is set in order to allow
|
|
|
|
// SP to be restored, but we still access stack objects using SP.
|
|
|
|
FrameReg = RISCV::X2;
|
|
|
|
Offset += MF.getFrameInfo().getStackSize();
|
2018-01-11 03:41:03 +08:00
|
|
|
} else {
|
|
|
|
FrameReg = RI->getFrameRegister(MF);
|
2018-01-18 19:34:02 +08:00
|
|
|
if (hasFP(MF))
|
|
|
|
Offset += RVFI->getVarArgsSaveSize();
|
|
|
|
else
|
|
|
|
Offset += MF.getFrameInfo().getStackSize();
|
2017-12-11 19:53:54 +08:00
|
|
|
}
|
|
|
|
return Offset;
|
|
|
|
}
|
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);
|
|
|
|
}
|
[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))) {
|
|
|
|
int RegScavFI = MFI.CreateStackObject(
|
|
|
|
RegInfo->getSpillSize(*RC), RegInfo->getSpillAlignment(*RC), false);
|
|
|
|
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);
|
|
|
|
}
|