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