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