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