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