43 void RISCVFrameLowering::determineFrameLayout(
MachineFunction &MF)
const {
52 if (RI->needsStackRealignment(MF)) {
54 FrameSize += (MaxStackAlign - StackAlign);
55 StackAlign = MaxStackAlign;
63 FrameSize =
alignTo(FrameSize, StackAlign);
77 if (DestReg == SrcReg && Val == 0)
81 BuildMI(MBB, MBBI, DL, TII->get(RISCV::ADDI), DestReg)
94 TII->
movImm(MBB, MBBI, DL, ScratchReg, Val, Flag);
95 BuildMI(MBB, MBBI, DL, TII->get(Opc), DestReg)
125 determineFrameLayout(MF);
139 MF.
getFunction(),
"Stack pointer required, but has been reserved."});
143 if (FirstSPAdjustAmount)
144 StackSize = FirstSPAdjustAmount;
152 BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
153 .addCFIIndex(CFIIndex);
162 std::advance(MBBI, CSI.size());
166 for (
const auto &
Entry : CSI) {
170 nullptr, RI->getDwarfRegNum(Reg,
true),
Offset));
171 BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
172 .addCFIIndex(CFIIndex);
179 MF.
getFunction(),
"Frame pointer required, but has been reserved."});
181 adjustReg(MBB, MBBI, DL, FPReg, SPReg,
186 nullptr, RI->getDwarfRegNum(FPReg,
true), 0));
187 BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
188 .addCFIIndex(CFIIndex);
192 if (FirstSPAdjustAmount) {
193 uint64_t SecondSPAdjustAmount = MFI.
getStackSize() - FirstSPAdjustAmount;
194 assert(SecondSPAdjustAmount > 0 &&
195 "SecondSPAdjustAmount should be greater than zero");
196 adjustReg(MBB, MBBI, DL, SPReg, SPReg, -SecondSPAdjustAmount,
205 BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
206 .addCFIIndex(CFIIndex);
213 if (RI->needsStackRealignment(MF)) {
217 if (isInt<12>(-(
int)MaxAlignment)) {
218 BuildMI(MBB, MBBI, DL, TII->get(RISCV::ANDI), SPReg)
220 .
addImm(-(
int)MaxAlignment);
225 BuildMI(MBB, MBBI, DL, TII->get(RISCV::SRLI), VR)
228 BuildMI(MBB, MBBI, DL, TII->get(RISCV::SLLI), SPReg)
237 BuildMI(MBB, MBBI, DL, TII->get(RISCV::ADDI), BPReg)
259 if (MBBI == MBB.
end())
261 DL = MBBI->getDebugLoc();
265 if (!MBBI->isTerminator())
266 MBBI = std::next(MBBI);
275 uint64_t FPOffset = StackSize - RVFI->getVarArgsSaveSize();
281 assert(
hasFP(MF) &&
"frame pointer should not have been eliminated");
282 adjustReg(MBB, LastFrameDestroy, DL, SPReg, FPReg, -FPOffset,
287 if (FirstSPAdjustAmount) {
288 uint64_t SecondSPAdjustAmount = MFI.
getStackSize() - FirstSPAdjustAmount;
289 assert(SecondSPAdjustAmount > 0 &&
290 "SecondSPAdjustAmount should be greater than zero");
292 adjustReg(MBB, LastFrameDestroy, DL, SPReg, SPReg, SecondSPAdjustAmount,
296 if (FirstSPAdjustAmount)
297 StackSize = FirstSPAdjustAmount;
305 unsigned &FrameReg)
const {
323 MinCSFI = CSI[0].getFrameIdx();
324 MaxCSFI = CSI[CSI.size() - 1].getFrameIdx();
327 if (FI >= MinCSFI && FI <= MaxCSFI) {
328 FrameReg = RISCV::X2;
330 if (FirstSPAdjustAmount)
331 Offset += FirstSPAdjustAmount;
341 FrameReg = RISCV::X2;
346 Offset += RVFI->getVarArgsSaveSize();
360 SavedRegs.
set(RISCV::X1);
361 SavedRegs.
set(RISCV::X8);
374 static const MCPhysReg CSRegs[] = { RISCV::X1,
375 RISCV::X5, RISCV::X6, RISCV::X7,
376 RISCV::X10, RISCV::X11,
377 RISCV::X12, RISCV::X13, RISCV::X14, RISCV::X15, RISCV::X16, RISCV::X17,
378 RISCV::X28, RISCV::X29, RISCV::X30, RISCV::X31, 0
381 for (
unsigned i = 0; CSRegs[i]; ++i)
382 SavedRegs.
set(CSRegs[i]);
390 for (
unsigned i = 0; Regs[i]; ++i)
391 if (RISCV::FPR32RegClass.
contains(Regs[i]) ||
392 RISCV::FPR64RegClass.
contains(Regs[i]))
393 SavedRegs.
set(Regs[i]);
436 int64_t Amount = MI->getOperand(0).getImm();
442 if (MI->getOpcode() == RISCV::ADJCALLSTACKDOWN)
449 return MBB.
erase(MI);
476 if (!isInt<12>(StackSize) && (CSI.size() > 0)) {
483 return 2048 - StackAlign;
void movImm(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, const DebugLoc &DL, Register DstReg, uint64_t Val, MachineInstr::MIFlag Flag=MachineInstr::NoFlags) const
static Register getSPReg(const RISCVSubtarget &STI)
Diagnostic information for unsupported feature in backend.
This class represents lattice values for constants.
bool hasFP(const MachineFunction &MF) const override
hasFP - Return true if the specified function should have a dedicated frame pointer register...
Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
int getFrameIndexReference(const MachineFunction &MF, int FI, unsigned &FrameReg) const override
getFrameIndexReference - This method should return the base register and offset used to reference a f...
MachineBasicBlock::iterator eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const override
This method is called during prolog/epilog code insertion to eliminate call frame setup and destroy p...
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
LLVM_NODISCARD unsigned addFrameInst(const MCCFIInstruction &Inst)
iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
static MCCFIInstruction createOffset(MCSymbol *L, unsigned Register, int Offset)
.cfi_offset Previous value of Register is saved at offset Offset from CFA.
const RISCVRegisterInfo * getRegisterInfo() const override
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
unsigned const TargetRegisterInfo * TRI
bool adjustsStack() const
Return true if this function adjusts the stack – e.g., when calling another function.
int getOffsetOfLocalArea() const
getOffsetOfLocalArea - This method returns the offset of the local area from the stack pointer on ent...
static MCCFIInstruction createDefCfaOffset(MCSymbol *L, int Offset)
.cfi_def_cfa_offset modifies a rule for computing CFA.
unsigned getSpillSize(const TargetRegisterClass &RC) const
Return the size in bytes of the stack slot allocated to hold a spilled copy of a register from class ...
const MachineInstrBuilder & setMIFlag(MachineInstr::MIFlag Flag) const
return AArch64::GPR64RegClass contains(Reg)
virtual void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS=nullptr) const
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
unsigned countTrailingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0's from the least significant bit to the most stopping at the first 1...
unsigned getSpillAlignment(const TargetRegisterClass &RC) const
Return the minimum required alignment in bytes for a spill slot for a register of this class...
bool isFrameAddressTaken() const
This method may be called any time after instruction selection is complete to determine if there is a...
instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
const HexagonInstrInfo * TII
void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS) const override
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
int64_t getObjectOffset(int ObjectIdx) const
Return the assigned stack offset of the specified object from the incoming stack pointer.
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted...
bool hasVarSizedObjects() const
This method may be called any time after instruction selection is complete to determine if the stack ...
bool DisableFramePointerElim(const MachineFunction &MF) const
DisableFramePointerElim - This returns true if frame pointer elimination optimization should be disab...
int alignSPAdjust(int SPAdj) const
alignSPAdjust - This method aligns the stack adjustment to the correct alignment. ...
Simple integer binary arithmetic operators.
iterator getLastNonDebugInstr()
Returns an iterator to the last non-debug instruction in the basic block, or end().
Flag
These should be considered private to the implementation of the MCInstrDesc class.
void processFunctionBeforeFrameFinalized(MachineFunction &MF, RegScavenger *RS) const override
processFunctionBeforeFrameFinalized - This method is called immediately before the specified function...
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
This file declares the machine register scavenger class.
bool hasBP(const MachineFunction &MF) const
unsigned const MachineRegisterInfo * MRI
static MCCFIInstruction createDefCfa(MCSymbol *L, unsigned Register, int Offset)
.cfi_def_cfa defines a rule for computing CFA as: take address from Register and add Offset to it...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
void setStackSize(uint64_t Size)
Set the size of the stack.
bool isFixedObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a fixed stack object.
unsigned getMaxAlignment() const
Return the alignment in bytes that this function must be aligned to, which is greater than the defaul...
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
uint64_t getFirstSPAdjustAmount(const MachineFunction &MF) const
unsigned getStackAlignment() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
bool isRegisterReservedByUser(Register i) const
Align max(MaybeAlign Lhs, Align Rhs)
unsigned getMaxCallFrameSize() const
Return the maximum size of a call frame that must be allocated for an outgoing function call...
void addScavengingFrameIndex(int FI)
Add a scavenging frame index.
virtual Register getFrameRegister(const MachineFunction &MF) const =0
Debug information queries.
static Register getFPReg(const RISCVSubtarget &STI)
bool hasReservedCallFrame(const MachineFunction &MF) const override
hasReservedCallFrame - Under normal circumstances, when a frame pointer is not required, we reserve argument space for call sites in the function immediately on entry to the current function.
void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override
emitProlog/emitEpilog - These methods insert prolog and epilog code into the function.
const Function & getFunction() const
Return the LLVM function that this machine code represents.
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
uint64_t estimateStackSize(const MachineFunction &MF) const
Estimate and return the size of the stack frame.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
const std::vector< CalleeSavedInfo > & getCalleeSavedInfo() const
Returns a reference to call saved info vector for the current function.
void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override
void setMaxCallFrameSize(unsigned S)
int getOffsetAdjustment() const
Return the correction for frame offsets.
const RISCVSubtarget & STI
void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
const LLVMTargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
RISCVMachineFunctionInfo - This class is derived from MachineFunctionInfo and contains private RISCV-...
int CreateStackObject(uint64_t Size, Align Alignment, bool isSpillSlot, const AllocaInst *Alloca=nullptr, uint8_t ID=0)
Create a new statically sized stack object, returning a nonnegative identifier to represent it...
bool needsStackRealignment(const MachineFunction &MF) const
True if storage within the function requires the stack pointer to be aligned more than the normal cal...
const RISCVInstrInfo * getInstrInfo() const override
const MCPhysReg * getCalleeSavedRegs() const
Returns list of callee saved registers.
uint64_t getStackSize() const
Return the number of bytes that must be allocated to hold all of the fixed size frame objects...
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
Wrapper class representing virtual and physical registers.
bool hasCalls() const
Return true if the current function has any function calls.