1 //===-- RISCVFrameLowering.cpp - RISCV Frame Information ------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains the RISCV implementation of TargetFrameLowering class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "RISCVFrameLowering.h"
14 #include "RISCVMachineFunctionInfo.h"
15 #include "RISCVSubtarget.h"
16 #include "llvm/CodeGen/MachineFrameInfo.h"
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/CodeGen/MachineInstrBuilder.h"
19 #include "llvm/CodeGen/MachineRegisterInfo.h"
20 #include "llvm/CodeGen/RegisterScavenging.h"
21 #include "llvm/IR/DiagnosticInfo.h"
22 #include "llvm/MC/MCDwarf.h"
23 
24 using namespace llvm;
25 
26 // For now we use x18, a.k.a s2, as pointer to shadow call stack.
27 // User should explicitly set -ffixed-x18 and not use x18 in their asm.
28 static void emitSCSPrologue(MachineFunction &MF, MachineBasicBlock &MBB,
29                             MachineBasicBlock::iterator MI,
30                             const DebugLoc &DL) {
31   if (!MF.getFunction().hasFnAttribute(Attribute::ShadowCallStack))
32     return;
33 
34   const auto &STI = MF.getSubtarget<RISCVSubtarget>();
35   Register RAReg = STI.getRegisterInfo()->getRARegister();
36 
37   // Do not save RA to the SCS if it's not saved to the regular stack,
38   // i.e. RA is not at risk of being overwritten.
39   std::vector<CalleeSavedInfo> &CSI = MF.getFrameInfo().getCalleeSavedInfo();
40   if (std::none_of(CSI.begin(), CSI.end(),
41                    [&](CalleeSavedInfo &CSR) { return CSR.getReg() == RAReg; }))
42     return;
43 
44   Register SCSPReg = RISCVABI::getSCSPReg();
45 
46   auto &Ctx = MF.getFunction().getContext();
47   if (!STI.isRegisterReservedByUser(SCSPReg)) {
48     Ctx.diagnose(DiagnosticInfoUnsupported{
49         MF.getFunction(), "x18 not reserved by user for Shadow Call Stack."});
50     return;
51   }
52 
53   const auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
54   if (RVFI->useSaveRestoreLibCalls(MF)) {
55     Ctx.diagnose(DiagnosticInfoUnsupported{
56         MF.getFunction(),
57         "Shadow Call Stack cannot be combined with Save/Restore LibCalls."});
58     return;
59   }
60 
61   const RISCVInstrInfo *TII = STI.getInstrInfo();
62   bool IsRV64 = STI.hasFeature(RISCV::Feature64Bit);
63   int64_t SlotSize = STI.getXLen() / 8;
64   // Store return address to shadow call stack
65   // s[w|d]  ra, 0(s2)
66   // addi    s2, s2, [4|8]
67   BuildMI(MBB, MI, DL, TII->get(IsRV64 ? RISCV::SD : RISCV::SW))
68       .addReg(RAReg)
69       .addReg(SCSPReg)
70       .addImm(0)
71       .setMIFlag(MachineInstr::FrameSetup);
72   BuildMI(MBB, MI, DL, TII->get(RISCV::ADDI))
73       .addReg(SCSPReg, RegState::Define)
74       .addReg(SCSPReg)
75       .addImm(SlotSize)
76       .setMIFlag(MachineInstr::FrameSetup);
77 }
78 
79 static void emitSCSEpilogue(MachineFunction &MF, MachineBasicBlock &MBB,
80                             MachineBasicBlock::iterator MI,
81                             const DebugLoc &DL) {
82   if (!MF.getFunction().hasFnAttribute(Attribute::ShadowCallStack))
83     return;
84 
85   const auto &STI = MF.getSubtarget<RISCVSubtarget>();
86   Register RAReg = STI.getRegisterInfo()->getRARegister();
87 
88   // See emitSCSPrologue() above.
89   std::vector<CalleeSavedInfo> &CSI = MF.getFrameInfo().getCalleeSavedInfo();
90   if (std::none_of(CSI.begin(), CSI.end(),
91                    [&](CalleeSavedInfo &CSR) { return CSR.getReg() == RAReg; }))
92     return;
93 
94   Register SCSPReg = RISCVABI::getSCSPReg();
95 
96   auto &Ctx = MF.getFunction().getContext();
97   if (!STI.isRegisterReservedByUser(SCSPReg)) {
98     Ctx.diagnose(DiagnosticInfoUnsupported{
99         MF.getFunction(), "x18 not reserved by user for Shadow Call Stack."});
100     return;
101   }
102 
103   const auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
104   if (RVFI->useSaveRestoreLibCalls(MF)) {
105     Ctx.diagnose(DiagnosticInfoUnsupported{
106         MF.getFunction(),
107         "Shadow Call Stack cannot be combined with Save/Restore LibCalls."});
108     return;
109   }
110 
111   const RISCVInstrInfo *TII = STI.getInstrInfo();
112   bool IsRV64 = STI.hasFeature(RISCV::Feature64Bit);
113   int64_t SlotSize = STI.getXLen() / 8;
114   // Load return address from shadow call stack
115   // l[w|d]  ra, -[4|8](s2)
116   // addi    s2, s2, -[4|8]
117   BuildMI(MBB, MI, DL, TII->get(IsRV64 ? RISCV::LD : RISCV::LW))
118       .addReg(RAReg, RegState::Define)
119       .addReg(SCSPReg)
120       .addImm(-SlotSize)
121       .setMIFlag(MachineInstr::FrameDestroy);
122   BuildMI(MBB, MI, DL, TII->get(RISCV::ADDI))
123       .addReg(SCSPReg, RegState::Define)
124       .addReg(SCSPReg)
125       .addImm(-SlotSize)
126       .setMIFlag(MachineInstr::FrameDestroy);
127 }
128 
129 // Get the ID of the libcall used for spilling and restoring callee saved
130 // registers. The ID is representative of the number of registers saved or
131 // restored by the libcall, except it is zero-indexed - ID 0 corresponds to a
132 // single register.
133 static int getLibCallID(const MachineFunction &MF,
134                         const std::vector<CalleeSavedInfo> &CSI) {
135   const auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
136 
137   if (CSI.empty() || !RVFI->useSaveRestoreLibCalls(MF))
138     return -1;
139 
140   Register MaxReg = RISCV::NoRegister;
141   for (auto &CS : CSI)
142     // RISCVRegisterInfo::hasReservedSpillSlot assigns negative frame indexes to
143     // registers which can be saved by libcall.
144     if (CS.getFrameIdx() < 0)
145       MaxReg = std::max(MaxReg.id(), CS.getReg().id());
146 
147   if (MaxReg == RISCV::NoRegister)
148     return -1;
149 
150   switch (MaxReg) {
151   default:
152     llvm_unreachable("Something has gone wrong!");
153   case /*s11*/ RISCV::X27: return 12;
154   case /*s10*/ RISCV::X26: return 11;
155   case /*s9*/  RISCV::X25: return 10;
156   case /*s8*/  RISCV::X24: return 9;
157   case /*s7*/  RISCV::X23: return 8;
158   case /*s6*/  RISCV::X22: return 7;
159   case /*s5*/  RISCV::X21: return 6;
160   case /*s4*/  RISCV::X20: return 5;
161   case /*s3*/  RISCV::X19: return 4;
162   case /*s2*/  RISCV::X18: return 3;
163   case /*s1*/  RISCV::X9:  return 2;
164   case /*s0*/  RISCV::X8:  return 1;
165   case /*ra*/  RISCV::X1:  return 0;
166   }
167 }
168 
169 // Get the name of the libcall used for spilling callee saved registers.
170 // If this function will not use save/restore libcalls, then return a nullptr.
171 static const char *
172 getSpillLibCallName(const MachineFunction &MF,
173                     const std::vector<CalleeSavedInfo> &CSI) {
174   static const char *const SpillLibCalls[] = {
175     "__riscv_save_0",
176     "__riscv_save_1",
177     "__riscv_save_2",
178     "__riscv_save_3",
179     "__riscv_save_4",
180     "__riscv_save_5",
181     "__riscv_save_6",
182     "__riscv_save_7",
183     "__riscv_save_8",
184     "__riscv_save_9",
185     "__riscv_save_10",
186     "__riscv_save_11",
187     "__riscv_save_12"
188   };
189 
190   int LibCallID = getLibCallID(MF, CSI);
191   if (LibCallID == -1)
192     return nullptr;
193   return SpillLibCalls[LibCallID];
194 }
195 
196 // Get the name of the libcall used for restoring callee saved registers.
197 // If this function will not use save/restore libcalls, then return a nullptr.
198 static const char *
199 getRestoreLibCallName(const MachineFunction &MF,
200                       const std::vector<CalleeSavedInfo> &CSI) {
201   static const char *const RestoreLibCalls[] = {
202     "__riscv_restore_0",
203     "__riscv_restore_1",
204     "__riscv_restore_2",
205     "__riscv_restore_3",
206     "__riscv_restore_4",
207     "__riscv_restore_5",
208     "__riscv_restore_6",
209     "__riscv_restore_7",
210     "__riscv_restore_8",
211     "__riscv_restore_9",
212     "__riscv_restore_10",
213     "__riscv_restore_11",
214     "__riscv_restore_12"
215   };
216 
217   int LibCallID = getLibCallID(MF, CSI);
218   if (LibCallID == -1)
219     return nullptr;
220   return RestoreLibCalls[LibCallID];
221 }
222 
223 // Return true if the specified function should have a dedicated frame
224 // pointer register.  This is true if frame pointer elimination is
225 // disabled, if it needs dynamic stack realignment, if the function has
226 // variable sized allocas, or if the frame address is taken.
227 bool RISCVFrameLowering::hasFP(const MachineFunction &MF) const {
228   const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
229 
230   const MachineFrameInfo &MFI = MF.getFrameInfo();
231   return MF.getTarget().Options.DisableFramePointerElim(MF) ||
232          RegInfo->hasStackRealignment(MF) || MFI.hasVarSizedObjects() ||
233          MFI.isFrameAddressTaken();
234 }
235 
236 bool RISCVFrameLowering::hasBP(const MachineFunction &MF) const {
237   const MachineFrameInfo &MFI = MF.getFrameInfo();
238   const TargetRegisterInfo *TRI = STI.getRegisterInfo();
239 
240   // If we do not reserve stack space for outgoing arguments in prologue,
241   // we will adjust the stack pointer before call instruction. After the
242   // adjustment, we can not use SP to access the stack objects for the
243   // arguments. Instead, use BP to access these stack objects.
244   return (MFI.hasVarSizedObjects() ||
245           (!hasReservedCallFrame(MF) && (!MFI.isMaxCallFrameSizeComputed() ||
246                                          MFI.getMaxCallFrameSize() != 0))) &&
247          TRI->hasStackRealignment(MF);
248 }
249 
250 // Determines the size of the frame and maximum call frame size.
251 void RISCVFrameLowering::determineFrameLayout(MachineFunction &MF) const {
252   MachineFrameInfo &MFI = MF.getFrameInfo();
253   auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
254 
255   // Get the number of bytes to allocate from the FrameInfo.
256   uint64_t FrameSize = MFI.getStackSize();
257 
258   // Get the alignment.
259   Align StackAlign = getStackAlign();
260 
261   // Make sure the frame is aligned.
262   FrameSize = alignTo(FrameSize, StackAlign);
263 
264   // Update frame info.
265   MFI.setStackSize(FrameSize);
266 
267   // When using SP or BP to access stack objects, we may require extra padding
268   // to ensure the bottom of the RVV stack is correctly aligned within the main
269   // stack. We calculate this as the amount required to align the scalar local
270   // variable section up to the RVV alignment.
271   const TargetRegisterInfo *TRI = STI.getRegisterInfo();
272   if (RVFI->getRVVStackSize() && (!hasFP(MF) || TRI->hasStackRealignment(MF))) {
273     int ScalarLocalVarSize = FrameSize - RVFI->getCalleeSavedStackSize() -
274                              RVFI->getVarArgsSaveSize();
275     if (auto RVVPadding =
276             offsetToAlignment(ScalarLocalVarSize, RVFI->getRVVStackAlign()))
277       RVFI->setRVVPadding(RVVPadding);
278   }
279 }
280 
281 // Returns the stack size including RVV padding (when required), rounded back
282 // up to the required stack alignment.
283 uint64_t RISCVFrameLowering::getStackSizeWithRVVPadding(
284     const MachineFunction &MF) const {
285   const MachineFrameInfo &MFI = MF.getFrameInfo();
286   auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
287   return alignTo(MFI.getStackSize() + RVFI->getRVVPadding(), getStackAlign());
288 }
289 
290 void RISCVFrameLowering::adjustReg(MachineBasicBlock &MBB,
291                                    MachineBasicBlock::iterator MBBI,
292                                    const DebugLoc &DL, Register DestReg,
293                                    Register SrcReg, int64_t Val,
294                                    MachineInstr::MIFlag Flag) const {
295   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
296   const RISCVInstrInfo *TII = STI.getInstrInfo();
297 
298   if (DestReg == SrcReg && Val == 0)
299     return;
300 
301   if (isInt<12>(Val)) {
302     BuildMI(MBB, MBBI, DL, TII->get(RISCV::ADDI), DestReg)
303         .addReg(SrcReg)
304         .addImm(Val)
305         .setMIFlag(Flag);
306     return;
307   }
308 
309   // Try to split the offset across two ADDIs. We need to keep the stack pointer
310   // aligned after each ADDI. We need to determine the maximum value we can put
311   // in each ADDI. In the negative direction, we can use -2048 which is always
312   // sufficiently aligned. In the positive direction, we need to find the
313   // largest 12-bit immediate that is aligned. Exclude -4096 since it can be
314   // created with LUI.
315   assert(getStackAlign().value() < 2048 && "Stack alignment too large");
316   int64_t MaxPosAdjStep = 2048 - getStackAlign().value();
317   if (Val > -4096 && Val <= (2 * MaxPosAdjStep)) {
318     int64_t FirstAdj = Val < 0 ? -2048 : MaxPosAdjStep;
319     Val -= FirstAdj;
320     BuildMI(MBB, MBBI, DL, TII->get(RISCV::ADDI), DestReg)
321         .addReg(SrcReg)
322         .addImm(FirstAdj)
323         .setMIFlag(Flag);
324     BuildMI(MBB, MBBI, DL, TII->get(RISCV::ADDI), DestReg)
325         .addReg(DestReg, RegState::Kill)
326         .addImm(Val)
327         .setMIFlag(Flag);
328     return;
329   }
330 
331   unsigned Opc = RISCV::ADD;
332   if (Val < 0) {
333     Val = -Val;
334     Opc = RISCV::SUB;
335   }
336 
337   Register ScratchReg = MRI.createVirtualRegister(&RISCV::GPRRegClass);
338   TII->movImm(MBB, MBBI, DL, ScratchReg, Val, Flag);
339   BuildMI(MBB, MBBI, DL, TII->get(Opc), DestReg)
340       .addReg(SrcReg)
341       .addReg(ScratchReg, RegState::Kill)
342       .setMIFlag(Flag);
343 }
344 
345 // Returns the register used to hold the frame pointer.
346 static Register getFPReg(const RISCVSubtarget &STI) { return RISCV::X8; }
347 
348 // Returns the register used to hold the stack pointer.
349 static Register getSPReg(const RISCVSubtarget &STI) { return RISCV::X2; }
350 
351 static SmallVector<CalleeSavedInfo, 8>
352 getNonLibcallCSI(const MachineFunction &MF,
353                  const std::vector<CalleeSavedInfo> &CSI) {
354   const MachineFrameInfo &MFI = MF.getFrameInfo();
355   SmallVector<CalleeSavedInfo, 8> NonLibcallCSI;
356 
357   for (auto &CS : CSI) {
358     int FI = CS.getFrameIdx();
359     if (FI >= 0 && MFI.getStackID(FI) == TargetStackID::Default)
360       NonLibcallCSI.push_back(CS);
361   }
362 
363   return NonLibcallCSI;
364 }
365 
366 void RISCVFrameLowering::adjustStackForRVV(MachineFunction &MF,
367                                            MachineBasicBlock &MBB,
368                                            MachineBasicBlock::iterator MBBI,
369                                            const DebugLoc &DL, int64_t Amount,
370                                            MachineInstr::MIFlag Flag) const {
371   assert(Amount != 0 && "Did not need to adjust stack pointer for RVV.");
372 
373   const RISCVInstrInfo *TII = STI.getInstrInfo();
374   Register SPReg = getSPReg(STI);
375   unsigned Opc = RISCV::ADD;
376   if (Amount < 0) {
377     Amount = -Amount;
378     Opc = RISCV::SUB;
379   }
380   // 1. Multiply the number of v-slots to the length of registers
381   Register FactorRegister =
382       TII->getVLENFactoredAmount(MF, MBB, MBBI, DL, Amount, Flag);
383   // 2. SP = SP - RVV stack size
384   BuildMI(MBB, MBBI, DL, TII->get(Opc), SPReg)
385       .addReg(SPReg)
386       .addReg(FactorRegister, RegState::Kill)
387       .setMIFlag(Flag);
388 }
389 
390 void RISCVFrameLowering::emitPrologue(MachineFunction &MF,
391                                       MachineBasicBlock &MBB) const {
392   MachineFrameInfo &MFI = MF.getFrameInfo();
393   auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
394   const RISCVRegisterInfo *RI = STI.getRegisterInfo();
395   const RISCVInstrInfo *TII = STI.getInstrInfo();
396   MachineBasicBlock::iterator MBBI = MBB.begin();
397 
398   Register FPReg = getFPReg(STI);
399   Register SPReg = getSPReg(STI);
400   Register BPReg = RISCVABI::getBPReg();
401 
402   // Debug location must be unknown since the first debug location is used
403   // to determine the end of the prologue.
404   DebugLoc DL;
405 
406   // All calls are tail calls in GHC calling conv, and functions have no
407   // prologue/epilogue.
408   if (MF.getFunction().getCallingConv() == CallingConv::GHC)
409     return;
410 
411   // Emit prologue for shadow call stack.
412   emitSCSPrologue(MF, MBB, MBBI, DL);
413 
414   // Since spillCalleeSavedRegisters may have inserted a libcall, skip past
415   // any instructions marked as FrameSetup
416   while (MBBI != MBB.end() && MBBI->getFlag(MachineInstr::FrameSetup))
417     ++MBBI;
418 
419   // Determine the correct frame layout
420   determineFrameLayout(MF);
421 
422   // If libcalls are used to spill and restore callee-saved registers, the frame
423   // has two sections; the opaque section managed by the libcalls, and the
424   // section managed by MachineFrameInfo which can also hold callee saved
425   // registers in fixed stack slots, both of which have negative frame indices.
426   // This gets even more complicated when incoming arguments are passed via the
427   // stack, as these too have negative frame indices. An example is detailed
428   // below:
429   //
430   //  | incoming arg | <- FI[-3]
431   //  | libcallspill |
432   //  | calleespill  | <- FI[-2]
433   //  | calleespill  | <- FI[-1]
434   //  | this_frame   | <- FI[0]
435   //
436   // For negative frame indices, the offset from the frame pointer will differ
437   // depending on which of these groups the frame index applies to.
438   // The following calculates the correct offset knowing the number of callee
439   // saved registers spilt by the two methods.
440   if (int LibCallRegs = getLibCallID(MF, MFI.getCalleeSavedInfo()) + 1) {
441     // Calculate the size of the frame managed by the libcall. The libcalls are
442     // implemented such that the stack will always be 16 byte aligned.
443     unsigned LibCallFrameSize = alignTo((STI.getXLen() / 8) * LibCallRegs, 16);
444     RVFI->setLibCallStackSize(LibCallFrameSize);
445   }
446 
447   // FIXME (note copied from Lanai): This appears to be overallocating.  Needs
448   // investigation. Get the number of bytes to allocate from the FrameInfo.
449   uint64_t StackSize = getStackSizeWithRVVPadding(MF);
450   uint64_t RealStackSize = StackSize + RVFI->getLibCallStackSize();
451   uint64_t RVVStackSize = RVFI->getRVVStackSize();
452 
453   // Early exit if there is no need to allocate on the stack
454   if (RealStackSize == 0 && !MFI.adjustsStack() && RVVStackSize == 0)
455     return;
456 
457   // If the stack pointer has been marked as reserved, then produce an error if
458   // the frame requires stack allocation
459   if (STI.isRegisterReservedByUser(SPReg))
460     MF.getFunction().getContext().diagnose(DiagnosticInfoUnsupported{
461         MF.getFunction(), "Stack pointer required, but has been reserved."});
462 
463   uint64_t FirstSPAdjustAmount = getFirstSPAdjustAmount(MF);
464   // Split the SP adjustment to reduce the offsets of callee saved spill.
465   if (FirstSPAdjustAmount) {
466     StackSize = FirstSPAdjustAmount;
467     RealStackSize = FirstSPAdjustAmount;
468   }
469 
470   // Allocate space on the stack if necessary.
471   adjustReg(MBB, MBBI, DL, SPReg, SPReg, -StackSize, MachineInstr::FrameSetup);
472 
473   // Emit ".cfi_def_cfa_offset RealStackSize"
474   unsigned CFIIndex = MF.addFrameInst(
475       MCCFIInstruction::cfiDefCfaOffset(nullptr, RealStackSize));
476   BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
477       .addCFIIndex(CFIIndex)
478       .setMIFlag(MachineInstr::FrameSetup);
479 
480   const auto &CSI = MFI.getCalleeSavedInfo();
481 
482   // The frame pointer is callee-saved, and code has been generated for us to
483   // save it to the stack. We need to skip over the storing of callee-saved
484   // registers as the frame pointer must be modified after it has been saved
485   // to the stack, not before.
486   // FIXME: assumes exactly one instruction is used to save each callee-saved
487   // register.
488   std::advance(MBBI, getNonLibcallCSI(MF, CSI).size());
489 
490   // Iterate over list of callee-saved registers and emit .cfi_offset
491   // directives.
492   for (const auto &Entry : CSI) {
493     int FrameIdx = Entry.getFrameIdx();
494     int64_t Offset;
495     // Offsets for objects with fixed locations (IE: those saved by libcall) are
496     // simply calculated from the frame index.
497     if (FrameIdx < 0)
498       Offset = FrameIdx * (int64_t) STI.getXLen() / 8;
499     else
500       Offset = MFI.getObjectOffset(Entry.getFrameIdx()) -
501                RVFI->getLibCallStackSize();
502     Register Reg = Entry.getReg();
503     unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
504         nullptr, RI->getDwarfRegNum(Reg, true), Offset));
505     BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
506         .addCFIIndex(CFIIndex)
507         .setMIFlag(MachineInstr::FrameSetup);
508   }
509 
510   // Generate new FP.
511   if (hasFP(MF)) {
512     if (STI.isRegisterReservedByUser(FPReg))
513       MF.getFunction().getContext().diagnose(DiagnosticInfoUnsupported{
514           MF.getFunction(), "Frame pointer required, but has been reserved."});
515 
516     adjustReg(MBB, MBBI, DL, FPReg, SPReg,
517               RealStackSize - RVFI->getVarArgsSaveSize(),
518               MachineInstr::FrameSetup);
519 
520     // Emit ".cfi_def_cfa $fp, RVFI->getVarArgsSaveSize()"
521     unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::cfiDefCfa(
522         nullptr, RI->getDwarfRegNum(FPReg, true), RVFI->getVarArgsSaveSize()));
523     BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
524         .addCFIIndex(CFIIndex)
525         .setMIFlag(MachineInstr::FrameSetup);
526   }
527 
528   // Emit the second SP adjustment after saving callee saved registers.
529   if (FirstSPAdjustAmount) {
530     uint64_t SecondSPAdjustAmount =
531         getStackSizeWithRVVPadding(MF) - FirstSPAdjustAmount;
532     assert(SecondSPAdjustAmount > 0 &&
533            "SecondSPAdjustAmount should be greater than zero");
534     adjustReg(MBB, MBBI, DL, SPReg, SPReg, -SecondSPAdjustAmount,
535               MachineInstr::FrameSetup);
536 
537     // If we are using a frame-pointer, and thus emitted ".cfi_def_cfa fp, 0",
538     // don't emit an sp-based .cfi_def_cfa_offset
539     if (!hasFP(MF)) {
540       // Emit ".cfi_def_cfa_offset StackSize"
541       unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(
542           nullptr, getStackSizeWithRVVPadding(MF)));
543       BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
544           .addCFIIndex(CFIIndex)
545           .setMIFlag(MachineInstr::FrameSetup);
546     }
547   }
548 
549   if (RVVStackSize)
550     adjustStackForRVV(MF, MBB, MBBI, DL, -RVVStackSize,
551                       MachineInstr::FrameSetup);
552 
553   if (hasFP(MF)) {
554     // Realign Stack
555     const RISCVRegisterInfo *RI = STI.getRegisterInfo();
556     if (RI->hasStackRealignment(MF)) {
557       Align MaxAlignment = MFI.getMaxAlign();
558 
559       const RISCVInstrInfo *TII = STI.getInstrInfo();
560       if (isInt<12>(-(int)MaxAlignment.value())) {
561         BuildMI(MBB, MBBI, DL, TII->get(RISCV::ANDI), SPReg)
562             .addReg(SPReg)
563             .addImm(-(int)MaxAlignment.value())
564             .setMIFlag(MachineInstr::FrameSetup);
565       } else {
566         unsigned ShiftAmount = Log2(MaxAlignment);
567         Register VR =
568             MF.getRegInfo().createVirtualRegister(&RISCV::GPRRegClass);
569         BuildMI(MBB, MBBI, DL, TII->get(RISCV::SRLI), VR)
570             .addReg(SPReg)
571             .addImm(ShiftAmount)
572             .setMIFlag(MachineInstr::FrameSetup);
573         BuildMI(MBB, MBBI, DL, TII->get(RISCV::SLLI), SPReg)
574             .addReg(VR)
575             .addImm(ShiftAmount)
576             .setMIFlag(MachineInstr::FrameSetup);
577       }
578       // FP will be used to restore the frame in the epilogue, so we need
579       // another base register BP to record SP after re-alignment. SP will
580       // track the current stack after allocating variable sized objects.
581       if (hasBP(MF)) {
582         // move BP, SP
583         BuildMI(MBB, MBBI, DL, TII->get(RISCV::ADDI), BPReg)
584             .addReg(SPReg)
585             .addImm(0)
586             .setMIFlag(MachineInstr::FrameSetup);
587       }
588     }
589   }
590 }
591 
592 void RISCVFrameLowering::emitEpilogue(MachineFunction &MF,
593                                       MachineBasicBlock &MBB) const {
594   const RISCVRegisterInfo *RI = STI.getRegisterInfo();
595   MachineFrameInfo &MFI = MF.getFrameInfo();
596   auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
597   Register FPReg = getFPReg(STI);
598   Register SPReg = getSPReg(STI);
599 
600   // All calls are tail calls in GHC calling conv, and functions have no
601   // prologue/epilogue.
602   if (MF.getFunction().getCallingConv() == CallingConv::GHC)
603     return;
604 
605   // Get the insert location for the epilogue. If there were no terminators in
606   // the block, get the last instruction.
607   MachineBasicBlock::iterator MBBI = MBB.end();
608   DebugLoc DL;
609   if (!MBB.empty()) {
610     MBBI = MBB.getLastNonDebugInstr();
611     if (MBBI != MBB.end())
612       DL = MBBI->getDebugLoc();
613 
614     MBBI = MBB.getFirstTerminator();
615 
616     // If callee-saved registers are saved via libcall, place stack adjustment
617     // before this call.
618     while (MBBI != MBB.begin() &&
619            std::prev(MBBI)->getFlag(MachineInstr::FrameDestroy))
620       --MBBI;
621   }
622 
623   const auto &CSI = getNonLibcallCSI(MF, MFI.getCalleeSavedInfo());
624 
625   // Skip to before the restores of callee-saved registers
626   // FIXME: assumes exactly one instruction is used to restore each
627   // callee-saved register.
628   auto LastFrameDestroy = MBBI;
629   if (!CSI.empty())
630     LastFrameDestroy = std::prev(MBBI, CSI.size());
631 
632   uint64_t StackSize = getStackSizeWithRVVPadding(MF);
633   uint64_t RealStackSize = StackSize + RVFI->getLibCallStackSize();
634   uint64_t FPOffset = RealStackSize - RVFI->getVarArgsSaveSize();
635   uint64_t RVVStackSize = RVFI->getRVVStackSize();
636 
637   // Restore the stack pointer using the value of the frame pointer. Only
638   // necessary if the stack pointer was modified, meaning the stack size is
639   // unknown.
640   //
641   // In order to make sure the stack point is right through the EH region,
642   // we also need to restore stack pointer from the frame pointer if we
643   // don't preserve stack space within prologue/epilogue for outgoing variables,
644   // normally it's just checking the variable sized object is present or not
645   // is enough, but we also don't preserve that at prologue/epilogue when
646   // have vector objects in stack.
647   if (RI->hasStackRealignment(MF) || MFI.hasVarSizedObjects() ||
648       !hasReservedCallFrame(MF)) {
649     assert(hasFP(MF) && "frame pointer should not have been eliminated");
650     adjustReg(MBB, LastFrameDestroy, DL, SPReg, FPReg, -FPOffset,
651               MachineInstr::FrameDestroy);
652   } else {
653     if (RVVStackSize)
654       adjustStackForRVV(MF, MBB, LastFrameDestroy, DL, RVVStackSize,
655                         MachineInstr::FrameDestroy);
656   }
657 
658   uint64_t FirstSPAdjustAmount = getFirstSPAdjustAmount(MF);
659   if (FirstSPAdjustAmount) {
660     uint64_t SecondSPAdjustAmount =
661         getStackSizeWithRVVPadding(MF) - FirstSPAdjustAmount;
662     assert(SecondSPAdjustAmount > 0 &&
663            "SecondSPAdjustAmount should be greater than zero");
664 
665     adjustReg(MBB, LastFrameDestroy, DL, SPReg, SPReg, SecondSPAdjustAmount,
666               MachineInstr::FrameDestroy);
667   }
668 
669   if (FirstSPAdjustAmount)
670     StackSize = FirstSPAdjustAmount;
671 
672   // Deallocate stack
673   adjustReg(MBB, MBBI, DL, SPReg, SPReg, StackSize, MachineInstr::FrameDestroy);
674 
675   // Emit epilogue for shadow call stack.
676   emitSCSEpilogue(MF, MBB, MBBI, DL);
677 }
678 
679 StackOffset
680 RISCVFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
681                                            Register &FrameReg) const {
682   const MachineFrameInfo &MFI = MF.getFrameInfo();
683   const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
684   const auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
685 
686   // Callee-saved registers should be referenced relative to the stack
687   // pointer (positive offset), otherwise use the frame pointer (negative
688   // offset).
689   const auto &CSI = getNonLibcallCSI(MF, MFI.getCalleeSavedInfo());
690   int MinCSFI = 0;
691   int MaxCSFI = -1;
692   StackOffset Offset;
693   auto StackID = MFI.getStackID(FI);
694 
695   assert((StackID == TargetStackID::Default ||
696           StackID == TargetStackID::ScalableVector) &&
697          "Unexpected stack ID for the frame object.");
698   if (StackID == TargetStackID::Default) {
699     Offset =
700         StackOffset::getFixed(MFI.getObjectOffset(FI) - getOffsetOfLocalArea() +
701                               MFI.getOffsetAdjustment());
702   } else if (StackID == TargetStackID::ScalableVector) {
703     Offset = StackOffset::getScalable(MFI.getObjectOffset(FI));
704   }
705 
706   uint64_t FirstSPAdjustAmount = getFirstSPAdjustAmount(MF);
707 
708   if (CSI.size()) {
709     MinCSFI = CSI[0].getFrameIdx();
710     MaxCSFI = CSI[CSI.size() - 1].getFrameIdx();
711   }
712 
713   if (FI >= MinCSFI && FI <= MaxCSFI) {
714     FrameReg = RISCV::X2;
715 
716     if (FirstSPAdjustAmount)
717       Offset += StackOffset::getFixed(FirstSPAdjustAmount);
718     else
719       Offset += StackOffset::getFixed(getStackSizeWithRVVPadding(MF));
720     return Offset;
721   }
722 
723   if (RI->hasStackRealignment(MF) && !MFI.isFixedObjectIndex(FI)) {
724     // If the stack was realigned, the frame pointer is set in order to allow
725     // SP to be restored, so we need another base register to record the stack
726     // after realignment.
727     // |--------------------------| -- <-- FP
728     // | callee-allocated save    | | <----|
729     // | area for register varargs| |      |
730     // |--------------------------| |      |
731     // | callee-saved registers   | |      |
732     // |--------------------------| --     |
733     // | realignment (the size of | |      |
734     // | this area is not counted | |      |
735     // | in MFI.getStackSize())   | |      |
736     // |--------------------------| --     |-- MFI.getStackSize()
737     // | RVV alignment padding    | |      |
738     // | (not counted in          | |      |
739     // | MFI.getStackSize() but   | |      |
740     // | counted in               | |      |
741     // | RVFI.getRVVStackSize())  | |      |
742     // |--------------------------| --     |
743     // | RVV objects              | |      |
744     // | (not counted in          | |      |
745     // | MFI.getStackSize())      | |      |
746     // |--------------------------| --     |
747     // | padding before RVV       | |      |
748     // | (not counted in          | |      |
749     // | MFI.getStackSize() or in | |      |
750     // | RVFI.getRVVStackSize())  | |      |
751     // |--------------------------| --     |
752     // | scalar local variables   | | <----'
753     // |--------------------------| -- <-- BP (if var sized objects present)
754     // | VarSize objects          | |
755     // |--------------------------| -- <-- SP
756     if (hasBP(MF)) {
757       FrameReg = RISCVABI::getBPReg();
758     } else {
759       // VarSize objects must be empty in this case!
760       assert(!MFI.hasVarSizedObjects());
761       FrameReg = RISCV::X2;
762     }
763   } else {
764     FrameReg = RI->getFrameRegister(MF);
765   }
766 
767   if (FrameReg == getFPReg(STI)) {
768     Offset += StackOffset::getFixed(RVFI->getVarArgsSaveSize());
769     if (FI >= 0)
770       Offset -= StackOffset::getFixed(RVFI->getLibCallStackSize());
771     // When using FP to access scalable vector objects, we need to minus
772     // the frame size.
773     //
774     // |--------------------------| -- <-- FP
775     // | callee-allocated save    | |
776     // | area for register varargs| |
777     // |--------------------------| |
778     // | callee-saved registers   | |
779     // |--------------------------| | MFI.getStackSize()
780     // | scalar local variables   | |
781     // |--------------------------| -- (Offset of RVV objects is from here.)
782     // | RVV objects              |
783     // |--------------------------|
784     // | VarSize objects          |
785     // |--------------------------| <-- SP
786     if (MFI.getStackID(FI) == TargetStackID::ScalableVector) {
787       assert(!RI->hasStackRealignment(MF) &&
788              "Can't index across variable sized realign");
789       // We don't expect any extra RVV alignment padding, as the stack size
790       // and RVV object sections should be correct aligned in their own
791       // right.
792       assert(MFI.getStackSize() == getStackSizeWithRVVPadding(MF) &&
793              "Inconsistent stack layout");
794       Offset -= StackOffset::getFixed(MFI.getStackSize());
795     }
796     return Offset;
797   }
798 
799   // This case handles indexing off both SP and BP.
800   // If indexing off SP, there must not be any var sized objects
801   assert(FrameReg == RISCVABI::getBPReg() || !MFI.hasVarSizedObjects());
802 
803   // When using SP to access frame objects, we need to add RVV stack size.
804   //
805   // |--------------------------| -- <-- FP
806   // | callee-allocated save    | | <----|
807   // | area for register varargs| |      |
808   // |--------------------------| |      |
809   // | callee-saved registers   | |      |
810   // |--------------------------| --     |
811   // | RVV alignment padding    | |      |
812   // | (not counted in          | |      |
813   // | MFI.getStackSize() but   | |      |
814   // | counted in               | |      |
815   // | RVFI.getRVVStackSize())  | |      |
816   // |--------------------------| --     |
817   // | RVV objects              | |      |-- MFI.getStackSize()
818   // | (not counted in          | |      |
819   // | MFI.getStackSize())      | |      |
820   // |--------------------------| --     |
821   // | padding before RVV       | |      |
822   // | (not counted in          | |      |
823   // | MFI.getStackSize())      | |      |
824   // |--------------------------| --     |
825   // | scalar local variables   | | <----'
826   // |--------------------------| -- <-- BP (if var sized objects present)
827   // | VarSize objects          | |
828   // |--------------------------| -- <-- SP
829   //
830   // The total amount of padding surrounding RVV objects is described by
831   // RVV->getRVVPadding() and it can be zero. It allows us to align the RVV
832   // objects to the required alignment.
833   if (MFI.getStackID(FI) == TargetStackID::Default) {
834     if (MFI.isFixedObjectIndex(FI)) {
835       assert(!RI->hasStackRealignment(MF) &&
836              "Can't index across variable sized realign");
837       Offset += StackOffset::get(getStackSizeWithRVVPadding(MF) +
838                                  RVFI->getLibCallStackSize(),
839                                  RVFI->getRVVStackSize());
840     } else {
841       Offset += StackOffset::getFixed(MFI.getStackSize());
842     }
843   } else if (MFI.getStackID(FI) == TargetStackID::ScalableVector) {
844     // Ensure the base of the RVV stack is correctly aligned: add on the
845     // alignment padding.
846     int ScalarLocalVarSize =
847       MFI.getStackSize() - RVFI->getCalleeSavedStackSize() -
848       RVFI->getVarArgsSaveSize() + RVFI->getRVVPadding();
849     Offset += StackOffset::get(ScalarLocalVarSize, RVFI->getRVVStackSize());
850   }
851   return Offset;
852 }
853 
854 void RISCVFrameLowering::determineCalleeSaves(MachineFunction &MF,
855                                               BitVector &SavedRegs,
856                                               RegScavenger *RS) const {
857   TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
858   // Unconditionally spill RA and FP only if the function uses a frame
859   // pointer.
860   if (hasFP(MF)) {
861     SavedRegs.set(RISCV::X1);
862     SavedRegs.set(RISCV::X8);
863   }
864   // Mark BP as used if function has dedicated base pointer.
865   if (hasBP(MF))
866     SavedRegs.set(RISCVABI::getBPReg());
867 
868   // If interrupt is enabled and there are calls in the handler,
869   // unconditionally save all Caller-saved registers and
870   // all FP registers, regardless whether they are used.
871   MachineFrameInfo &MFI = MF.getFrameInfo();
872 
873   if (MF.getFunction().hasFnAttribute("interrupt") && MFI.hasCalls()) {
874 
875     static const MCPhysReg CSRegs[] = { RISCV::X1,      /* ra */
876       RISCV::X5, RISCV::X6, RISCV::X7,                  /* t0-t2 */
877       RISCV::X10, RISCV::X11,                           /* a0-a1, a2-a7 */
878       RISCV::X12, RISCV::X13, RISCV::X14, RISCV::X15, RISCV::X16, RISCV::X17,
879       RISCV::X28, RISCV::X29, RISCV::X30, RISCV::X31, 0 /* t3-t6 */
880     };
881 
882     for (unsigned i = 0; CSRegs[i]; ++i)
883       SavedRegs.set(CSRegs[i]);
884 
885     if (MF.getSubtarget<RISCVSubtarget>().hasStdExtF()) {
886 
887       // If interrupt is enabled, this list contains all FP registers.
888       const MCPhysReg * Regs = MF.getRegInfo().getCalleeSavedRegs();
889 
890       for (unsigned i = 0; Regs[i]; ++i)
891         if (RISCV::FPR16RegClass.contains(Regs[i]) ||
892             RISCV::FPR32RegClass.contains(Regs[i]) ||
893             RISCV::FPR64RegClass.contains(Regs[i]))
894           SavedRegs.set(Regs[i]);
895     }
896   }
897 }
898 
899 std::pair<int64_t, Align>
900 RISCVFrameLowering::assignRVVStackObjectOffsets(MachineFrameInfo &MFI) const {
901   // Create a buffer of RVV objects to allocate.
902   SmallVector<int, 8> ObjectsToAllocate;
903   for (int I = 0, E = MFI.getObjectIndexEnd(); I != E; ++I) {
904     unsigned StackID = MFI.getStackID(I);
905     if (StackID != TargetStackID::ScalableVector)
906       continue;
907     if (MFI.isDeadObjectIndex(I))
908       continue;
909 
910     ObjectsToAllocate.push_back(I);
911   }
912 
913   // Allocate all RVV locals and spills
914   int64_t Offset = 0;
915   // The minimum alignment is 16 bytes.
916   Align RVVStackAlign(16);
917   for (int FI : ObjectsToAllocate) {
918     // ObjectSize in bytes.
919     int64_t ObjectSize = MFI.getObjectSize(FI);
920     auto ObjectAlign = std::max(Align(8), MFI.getObjectAlign(FI));
921     // If the data type is the fractional vector type, reserve one vector
922     // register for it.
923     if (ObjectSize < 8)
924       ObjectSize = 8;
925     Offset = alignTo(Offset + ObjectSize, ObjectAlign);
926     MFI.setObjectOffset(FI, -Offset);
927     // Update the maximum alignment of the RVV stack section
928     RVVStackAlign = std::max(RVVStackAlign, ObjectAlign);
929   }
930 
931   // Ensure the alignment of the RVV stack. Since we want the most-aligned
932   // object right at the bottom (i.e., any padding at the top of the frame),
933   // readjust all RVV objects down by the alignment padding.
934   uint64_t StackSize = Offset;
935   if (auto AlignmentPadding = offsetToAlignment(StackSize, RVVStackAlign)) {
936     StackSize += AlignmentPadding;
937     for (int FI : ObjectsToAllocate)
938       MFI.setObjectOffset(FI, MFI.getObjectOffset(FI) - AlignmentPadding);
939   }
940 
941   return std::make_pair(StackSize, RVVStackAlign);
942 }
943 
944 static bool hasRVVSpillWithFIs(MachineFunction &MF) {
945   if (!MF.getSubtarget<RISCVSubtarget>().hasVInstructions())
946     return false;
947   for (const MachineBasicBlock &MBB : MF)
948     for (const MachineInstr &MI : MBB)
949       if (RISCV::isRVVSpill(MI, /*CheckFIs*/ true))
950         return true;
951   return false;
952 }
953 
954 void RISCVFrameLowering::processFunctionBeforeFrameFinalized(
955     MachineFunction &MF, RegScavenger *RS) const {
956   const RISCVRegisterInfo *RegInfo =
957       MF.getSubtarget<RISCVSubtarget>().getRegisterInfo();
958   MachineFrameInfo &MFI = MF.getFrameInfo();
959   const TargetRegisterClass *RC = &RISCV::GPRRegClass;
960   auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
961 
962   int64_t RVVStackSize;
963   Align RVVStackAlign;
964   std::tie(RVVStackSize, RVVStackAlign) = assignRVVStackObjectOffsets(MFI);
965 
966   RVFI->setRVVStackSize(RVVStackSize);
967   RVFI->setRVVStackAlign(RVVStackAlign);
968 
969   // Ensure the entire stack is aligned to at least the RVV requirement: some
970   // scalable-vector object alignments are not considered by the
971   // target-independent code.
972   MFI.ensureMaxAlignment(RVVStackAlign);
973 
974   // estimateStackSize has been observed to under-estimate the final stack
975   // size, so give ourselves wiggle-room by checking for stack size
976   // representable an 11-bit signed field rather than 12-bits.
977   // FIXME: It may be possible to craft a function with a small stack that
978   // still needs an emergency spill slot for branch relaxation. This case
979   // would currently be missed.
980   // RVV loads & stores have no capacity to hold the immediate address offsets
981   // so we must always reserve an emergency spill slot if the MachineFunction
982   // contains any RVV spills.
983   if (!isInt<11>(MFI.estimateStackSize(MF)) || hasRVVSpillWithFIs(MF)) {
984     int RegScavFI = MFI.CreateStackObject(RegInfo->getSpillSize(*RC),
985                                           RegInfo->getSpillAlign(*RC), false);
986     RS->addScavengingFrameIndex(RegScavFI);
987     // For RVV, scalable stack offsets require up to two scratch registers to
988     // compute the final offset. Reserve an additional emergency spill slot.
989     if (RVVStackSize != 0) {
990       int RVVRegScavFI = MFI.CreateStackObject(
991           RegInfo->getSpillSize(*RC), RegInfo->getSpillAlign(*RC), false);
992       RS->addScavengingFrameIndex(RVVRegScavFI);
993     }
994   }
995 
996   if (MFI.getCalleeSavedInfo().empty() || RVFI->useSaveRestoreLibCalls(MF)) {
997     RVFI->setCalleeSavedStackSize(0);
998     return;
999   }
1000 
1001   unsigned Size = 0;
1002   for (const auto &Info : MFI.getCalleeSavedInfo()) {
1003     int FrameIdx = Info.getFrameIdx();
1004     if (MFI.getStackID(FrameIdx) != TargetStackID::Default)
1005       continue;
1006 
1007     Size += MFI.getObjectSize(FrameIdx);
1008   }
1009   RVFI->setCalleeSavedStackSize(Size);
1010 }
1011 
1012 static bool hasRVVFrameObject(const MachineFunction &MF) {
1013   // Originally, the function will scan all the stack objects to check whether
1014   // if there is any scalable vector object on the stack or not. However, it
1015   // causes errors in the register allocator. In issue 53016, it returns false
1016   // before RA because there is no RVV stack objects. After RA, it returns true
1017   // because there are spilling slots for RVV values during RA. It will not
1018   // reserve BP during register allocation and generate BP access in the PEI
1019   // pass due to the inconsistent behavior of the function.
1020   //
1021   // The function is changed to use hasVInstructions() as the return value. It
1022   // is not precise, but it can make the register allocation correct.
1023   //
1024   // FIXME: Find a better way to make the decision or revisit the solution in
1025   // D103622.
1026   //
1027   // Refer to https://github.com/llvm/llvm-project/issues/53016.
1028   return MF.getSubtarget<RISCVSubtarget>().hasVInstructions();
1029 }
1030 
1031 // Not preserve stack space within prologue for outgoing variables when the
1032 // function contains variable size objects or there are vector objects accessed
1033 // by the frame pointer.
1034 // Let eliminateCallFramePseudoInstr preserve stack space for it.
1035 bool RISCVFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
1036   return !MF.getFrameInfo().hasVarSizedObjects() &&
1037          !(hasFP(MF) && hasRVVFrameObject(MF));
1038 }
1039 
1040 // Eliminate ADJCALLSTACKDOWN, ADJCALLSTACKUP pseudo instructions.
1041 MachineBasicBlock::iterator RISCVFrameLowering::eliminateCallFramePseudoInstr(
1042     MachineFunction &MF, MachineBasicBlock &MBB,
1043     MachineBasicBlock::iterator MI) const {
1044   Register SPReg = RISCV::X2;
1045   DebugLoc DL = MI->getDebugLoc();
1046 
1047   if (!hasReservedCallFrame(MF)) {
1048     // If space has not been reserved for a call frame, ADJCALLSTACKDOWN and
1049     // ADJCALLSTACKUP must be converted to instructions manipulating the stack
1050     // pointer. This is necessary when there is a variable length stack
1051     // allocation (e.g. alloca), which means it's not possible to allocate
1052     // space for outgoing arguments from within the function prologue.
1053     int64_t Amount = MI->getOperand(0).getImm();
1054 
1055     if (Amount != 0) {
1056       // Ensure the stack remains aligned after adjustment.
1057       Amount = alignSPAdjust(Amount);
1058 
1059       if (MI->getOpcode() == RISCV::ADJCALLSTACKDOWN)
1060         Amount = -Amount;
1061 
1062       adjustReg(MBB, MI, DL, SPReg, SPReg, Amount, MachineInstr::NoFlags);
1063     }
1064   }
1065 
1066   return MBB.erase(MI);
1067 }
1068 
1069 // We would like to split the SP adjustment to reduce prologue/epilogue
1070 // as following instructions. In this way, the offset of the callee saved
1071 // register could fit in a single store.
1072 //   add     sp,sp,-2032
1073 //   sw      ra,2028(sp)
1074 //   sw      s0,2024(sp)
1075 //   sw      s1,2020(sp)
1076 //   sw      s3,2012(sp)
1077 //   sw      s4,2008(sp)
1078 //   add     sp,sp,-64
1079 uint64_t
1080 RISCVFrameLowering::getFirstSPAdjustAmount(const MachineFunction &MF) const {
1081   const auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
1082   const MachineFrameInfo &MFI = MF.getFrameInfo();
1083   const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
1084   uint64_t StackSize = getStackSizeWithRVVPadding(MF);
1085 
1086   // Disable SplitSPAdjust if save-restore libcall is used. The callee-saved
1087   // registers will be pushed by the save-restore libcalls, so we don't have to
1088   // split the SP adjustment in this case.
1089   if (RVFI->getLibCallStackSize())
1090     return 0;
1091 
1092   // Return the FirstSPAdjustAmount if the StackSize can not fit in a signed
1093   // 12-bit and there exists a callee-saved register needing to be pushed.
1094   if (!isInt<12>(StackSize) && (CSI.size() > 0)) {
1095     // FirstSPAdjustAmount is chosen as (2048 - StackAlign) because 2048 will
1096     // cause sp = sp + 2048 in the epilogue to be split into multiple
1097     // instructions. Offsets smaller than 2048 can fit in a single load/store
1098     // instruction, and we have to stick with the stack alignment. 2048 has
1099     // 16-byte alignment. The stack alignment for RV32 and RV64 is 16 and for
1100     // RV32E it is 4. So (2048 - StackAlign) will satisfy the stack alignment.
1101     return 2048 - getStackAlign().value();
1102   }
1103   return 0;
1104 }
1105 
1106 bool RISCVFrameLowering::spillCalleeSavedRegisters(
1107     MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
1108     ArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
1109   if (CSI.empty())
1110     return true;
1111 
1112   MachineFunction *MF = MBB.getParent();
1113   const TargetInstrInfo &TII = *MF->getSubtarget().getInstrInfo();
1114   DebugLoc DL;
1115   if (MI != MBB.end() && !MI->isDebugInstr())
1116     DL = MI->getDebugLoc();
1117 
1118   const char *SpillLibCall = getSpillLibCallName(*MF, CSI);
1119   if (SpillLibCall) {
1120     // Add spill libcall via non-callee-saved register t0.
1121     BuildMI(MBB, MI, DL, TII.get(RISCV::PseudoCALLReg), RISCV::X5)
1122         .addExternalSymbol(SpillLibCall, RISCVII::MO_CALL)
1123         .setMIFlag(MachineInstr::FrameSetup);
1124 
1125     // Add registers spilled in libcall as liveins.
1126     for (auto &CS : CSI)
1127       MBB.addLiveIn(CS.getReg());
1128   }
1129 
1130   // Manually spill values not spilled by libcall.
1131   const auto &NonLibcallCSI = getNonLibcallCSI(*MF, CSI);
1132   for (auto &CS : NonLibcallCSI) {
1133     // Insert the spill to the stack frame.
1134     Register Reg = CS.getReg();
1135     const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
1136     TII.storeRegToStackSlot(MBB, MI, Reg, !MBB.isLiveIn(Reg), CS.getFrameIdx(),
1137                             RC, TRI);
1138   }
1139 
1140   return true;
1141 }
1142 
1143 bool RISCVFrameLowering::restoreCalleeSavedRegisters(
1144     MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
1145     MutableArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
1146   if (CSI.empty())
1147     return true;
1148 
1149   MachineFunction *MF = MBB.getParent();
1150   const TargetInstrInfo &TII = *MF->getSubtarget().getInstrInfo();
1151   DebugLoc DL;
1152   if (MI != MBB.end() && !MI->isDebugInstr())
1153     DL = MI->getDebugLoc();
1154 
1155   // Manually restore values not restored by libcall.
1156   // Keep the same order as in the prologue. There is no need to reverse the
1157   // order in the epilogue. In addition, the return address will be restored
1158   // first in the epilogue. It increases the opportunity to avoid the
1159   // load-to-use data hazard between loading RA and return by RA.
1160   // loadRegFromStackSlot can insert multiple instructions.
1161   const auto &NonLibcallCSI = getNonLibcallCSI(*MF, CSI);
1162   for (auto &CS : NonLibcallCSI) {
1163     Register Reg = CS.getReg();
1164     const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
1165     TII.loadRegFromStackSlot(MBB, MI, Reg, CS.getFrameIdx(), RC, TRI);
1166     assert(MI != MBB.begin() && "loadRegFromStackSlot didn't insert any code!");
1167   }
1168 
1169   const char *RestoreLibCall = getRestoreLibCallName(*MF, CSI);
1170   if (RestoreLibCall) {
1171     // Add restore libcall via tail call.
1172     MachineBasicBlock::iterator NewMI =
1173         BuildMI(MBB, MI, DL, TII.get(RISCV::PseudoTAIL))
1174             .addExternalSymbol(RestoreLibCall, RISCVII::MO_CALL)
1175             .setMIFlag(MachineInstr::FrameDestroy);
1176 
1177     // Remove trailing returns, since the terminator is now a tail call to the
1178     // restore function.
1179     if (MI != MBB.end() && MI->getOpcode() == RISCV::PseudoRET) {
1180       NewMI->copyImplicitOps(*MF, *MI);
1181       MI->eraseFromParent();
1182     }
1183   }
1184 
1185   return true;
1186 }
1187 
1188 bool RISCVFrameLowering::canUseAsPrologue(const MachineBasicBlock &MBB) const {
1189   MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB);
1190   const MachineFunction *MF = MBB.getParent();
1191   const auto *RVFI = MF->getInfo<RISCVMachineFunctionInfo>();
1192 
1193   if (!RVFI->useSaveRestoreLibCalls(*MF))
1194     return true;
1195 
1196   // Inserting a call to a __riscv_save libcall requires the use of the register
1197   // t0 (X5) to hold the return address. Therefore if this register is already
1198   // used we can't insert the call.
1199 
1200   RegScavenger RS;
1201   RS.enterBasicBlock(*TmpMBB);
1202   return !RS.isRegUsed(RISCV::X5);
1203 }
1204 
1205 bool RISCVFrameLowering::canUseAsEpilogue(const MachineBasicBlock &MBB) const {
1206   const MachineFunction *MF = MBB.getParent();
1207   MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB);
1208   const auto *RVFI = MF->getInfo<RISCVMachineFunctionInfo>();
1209 
1210   if (!RVFI->useSaveRestoreLibCalls(*MF))
1211     return true;
1212 
1213   // Using the __riscv_restore libcalls to restore CSRs requires a tail call.
1214   // This means if we still need to continue executing code within this function
1215   // the restore cannot take place in this basic block.
1216 
1217   if (MBB.succ_size() > 1)
1218     return false;
1219 
1220   MachineBasicBlock *SuccMBB =
1221       MBB.succ_empty() ? TmpMBB->getFallThrough() : *MBB.succ_begin();
1222 
1223   // Doing a tail call should be safe if there are no successors, because either
1224   // we have a returning block or the end of the block is unreachable, so the
1225   // restore will be eliminated regardless.
1226   if (!SuccMBB)
1227     return true;
1228 
1229   // The successor can only contain a return, since we would effectively be
1230   // replacing the successor with our own tail return at the end of our block.
1231   return SuccMBB->isReturnBlock() && SuccMBB->size() == 1;
1232 }
1233 
1234 bool RISCVFrameLowering::isSupportedStackID(TargetStackID::Value ID) const {
1235   switch (ID) {
1236   case TargetStackID::Default:
1237   case TargetStackID::ScalableVector:
1238     return true;
1239   case TargetStackID::NoAlloc:
1240   case TargetStackID::SGPRSpill:
1241   case TargetStackID::WasmLocal:
1242     return false;
1243   }
1244   llvm_unreachable("Invalid TargetStackID::Value");
1245 }
1246 
1247 TargetStackID::Value RISCVFrameLowering::getStackIDForScalableVectors() const {
1248   return TargetStackID::ScalableVector;
1249 }
1250