LLVM 22.0.0git
RISCVFrameLowering.cpp
Go to the documentation of this file.
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"
16#include "RISCVSubtarget.h"
26#include "llvm/MC/MCDwarf.h"
27#include "llvm/Support/LEB128.h"
28
29#include <algorithm>
30
31#define DEBUG_TYPE "riscv-frame"
32
33using namespace llvm;
34
36 if (ABI == RISCVABI::ABI_ILP32E)
37 return Align(4);
38 if (ABI == RISCVABI::ABI_LP64E)
39 return Align(8);
40 return Align(16);
41}
42
45 StackGrowsDown, getABIStackAlignment(STI.getTargetABI()),
46 /*LocalAreaOffset=*/0,
47 /*TransientStackAlignment=*/getABIStackAlignment(STI.getTargetABI())),
48 STI(STI) {}
49
50// The register used to hold the frame pointer.
51static constexpr MCPhysReg FPReg = RISCV::X8;
52
53// The register used to hold the stack pointer.
54static constexpr MCPhysReg SPReg = RISCV::X2;
55
56// The register used to hold the return address.
57static constexpr MCPhysReg RAReg = RISCV::X1;
58
59// LIst of CSRs that are given a fixed location by save/restore libcalls or
60// Zcmp/Xqccmp Push/Pop. The order in this table indicates the order the
61// registers are saved on the stack. Zcmp uses the reverse order of save/restore
62// and Xqccmp on the stack, but this is handled when offsets are calculated.
63static const MCPhysReg FixedCSRFIMap[] = {
64 /*ra*/ RAReg, /*s0*/ FPReg, /*s1*/ RISCV::X9,
65 /*s2*/ RISCV::X18, /*s3*/ RISCV::X19, /*s4*/ RISCV::X20,
66 /*s5*/ RISCV::X21, /*s6*/ RISCV::X22, /*s7*/ RISCV::X23,
67 /*s8*/ RISCV::X24, /*s9*/ RISCV::X25, /*s10*/ RISCV::X26,
68 /*s11*/ RISCV::X27};
69
70// The number of stack bytes allocated by `QC.C.MIENTER(.NEST)` and popped by
71// `QC.C.MILEAVERET`.
72static constexpr uint64_t QCIInterruptPushAmount = 96;
73
74static const std::pair<MCPhysReg, int8_t> FixedCSRFIQCIInterruptMap[] = {
75 /* -1 is a gap for mepc/mnepc */
76 {/*fp*/ FPReg, -2},
77 /* -3 is a gap for qc.mcause */
78 {/*ra*/ RAReg, -4},
79 /* -5 is reserved */
80 {/*t0*/ RISCV::X5, -6},
81 {/*t1*/ RISCV::X6, -7},
82 {/*t2*/ RISCV::X7, -8},
83 {/*a0*/ RISCV::X10, -9},
84 {/*a1*/ RISCV::X11, -10},
85 {/*a2*/ RISCV::X12, -11},
86 {/*a3*/ RISCV::X13, -12},
87 {/*a4*/ RISCV::X14, -13},
88 {/*a5*/ RISCV::X15, -14},
89 {/*a6*/ RISCV::X16, -15},
90 {/*a7*/ RISCV::X17, -16},
91 {/*t3*/ RISCV::X28, -17},
92 {/*t4*/ RISCV::X29, -18},
93 {/*t5*/ RISCV::X30, -19},
94 {/*t6*/ RISCV::X31, -20},
95 /* -21, -22, -23, -24 are reserved */
96};
97
98/// Returns true if DWARF CFI instructions ("frame moves") should be emitted.
99static bool needsDwarfCFI(const MachineFunction &MF) {
100 return MF.needsFrameMoves();
101}
102
103// For now we use x3, a.k.a gp, as pointer to shadow call stack.
104// User should not use x3 in their asm.
107 const DebugLoc &DL) {
108 const auto &STI = MF.getSubtarget<RISCVSubtarget>();
109 bool HasHWShadowStack = MF.getFunction().hasFnAttribute("hw-shadow-stack") &&
110 STI.hasStdExtZicfiss();
111 bool HasSWShadowStack =
112 MF.getFunction().hasFnAttribute(Attribute::ShadowCallStack);
113 if (!HasHWShadowStack && !HasSWShadowStack)
114 return;
115
116 const llvm::RISCVRegisterInfo *TRI = STI.getRegisterInfo();
117
118 // Do not save RA to the SCS if it's not saved to the regular stack,
119 // i.e. RA is not at risk of being overwritten.
120 std::vector<CalleeSavedInfo> &CSI = MF.getFrameInfo().getCalleeSavedInfo();
121 if (llvm::none_of(
122 CSI, [&](CalleeSavedInfo &CSR) { return CSR.getReg() == RAReg; }))
123 return;
124
125 const RISCVInstrInfo *TII = STI.getInstrInfo();
126 if (HasHWShadowStack) {
127 BuildMI(MBB, MI, DL, TII->get(RISCV::SSPUSH)).addReg(RAReg);
128 return;
129 }
130
131 Register SCSPReg = RISCVABI::getSCSPReg();
132
133 bool IsRV64 = STI.is64Bit();
134 int64_t SlotSize = STI.getXLen() / 8;
135 // Store return address to shadow call stack
136 // addi gp, gp, [4|8]
137 // s[w|d] ra, -[4|8](gp)
138 BuildMI(MBB, MI, DL, TII->get(RISCV::ADDI))
139 .addReg(SCSPReg, RegState::Define)
140 .addReg(SCSPReg)
141 .addImm(SlotSize)
143 BuildMI(MBB, MI, DL, TII->get(IsRV64 ? RISCV::SD : RISCV::SW))
144 .addReg(RAReg)
145 .addReg(SCSPReg)
146 .addImm(-SlotSize)
148
149 if (!needsDwarfCFI(MF))
150 return;
151
152 // Emit a CFI instruction that causes SlotSize to be subtracted from the value
153 // of the shadow stack pointer when unwinding past this frame.
154 char DwarfSCSReg = TRI->getDwarfRegNum(SCSPReg, /*IsEH*/ true);
155 assert(DwarfSCSReg < 32 && "SCS Register should be < 32 (X3).");
156
157 char Offset = static_cast<char>(-SlotSize) & 0x7f;
158 const char CFIInst[] = {
159 dwarf::DW_CFA_val_expression,
160 DwarfSCSReg, // register
161 2, // length
162 static_cast<char>(unsigned(dwarf::DW_OP_breg0 + DwarfSCSReg)),
163 Offset, // addend (sleb128)
164 };
165
167 .buildEscape(StringRef(CFIInst, sizeof(CFIInst)));
168}
169
172 const DebugLoc &DL) {
173 const auto &STI = MF.getSubtarget<RISCVSubtarget>();
174 bool HasHWShadowStack = MF.getFunction().hasFnAttribute("hw-shadow-stack") &&
175 STI.hasStdExtZicfiss();
176 bool HasSWShadowStack =
177 MF.getFunction().hasFnAttribute(Attribute::ShadowCallStack);
178 if (!HasHWShadowStack && !HasSWShadowStack)
179 return;
180
181 // See emitSCSPrologue() above.
182 std::vector<CalleeSavedInfo> &CSI = MF.getFrameInfo().getCalleeSavedInfo();
183 if (llvm::none_of(
184 CSI, [&](CalleeSavedInfo &CSR) { return CSR.getReg() == RAReg; }))
185 return;
186
187 const RISCVInstrInfo *TII = STI.getInstrInfo();
188 if (HasHWShadowStack) {
189 BuildMI(MBB, MI, DL, TII->get(RISCV::SSPOPCHK)).addReg(RAReg);
190 return;
191 }
192
193 Register SCSPReg = RISCVABI::getSCSPReg();
194
195 bool IsRV64 = STI.is64Bit();
196 int64_t SlotSize = STI.getXLen() / 8;
197 // Load return address from shadow call stack
198 // l[w|d] ra, -[4|8](gp)
199 // addi gp, gp, -[4|8]
200 BuildMI(MBB, MI, DL, TII->get(IsRV64 ? RISCV::LD : RISCV::LW))
202 .addReg(SCSPReg)
203 .addImm(-SlotSize)
205 BuildMI(MBB, MI, DL, TII->get(RISCV::ADDI))
206 .addReg(SCSPReg, RegState::Define)
207 .addReg(SCSPReg)
208 .addImm(-SlotSize)
210 if (needsDwarfCFI(MF)) {
211 // Restore the SCS pointer
213 }
214}
215
216// Insert instruction to swap mscratchsw with sp
219 const DebugLoc &DL) {
220 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
221
222 if (!RVFI->isSiFiveStackSwapInterrupt(MF))
223 return;
224
225 const auto &STI = MF.getSubtarget<RISCVSubtarget>();
226 const RISCVInstrInfo *TII = STI.getInstrInfo();
227
228 assert(STI.hasVendorXSfmclic() && "Stack Swapping Requires XSfmclic");
229
230 BuildMI(MBB, MBBI, DL, TII->get(RISCV::CSRRW))
232 .addImm(RISCVSysReg::sf_mscratchcsw)
235
236 // FIXME: CFI Information for this swap.
237}
238
239static void
242 if (!RVFI.isSiFivePreemptibleInterrupt(MF))
243 return;
244
245 const TargetRegisterClass &RC = RISCV::GPRRegClass;
246 const TargetRegisterInfo &TRI =
247 *MF.getSubtarget<RISCVSubtarget>().getRegisterInfo();
248 MachineFrameInfo &MFI = MF.getFrameInfo();
249
250 // Create two frame objects for spilling X8 and X9, which will be done in
251 // `emitSiFiveCLICPreemptibleSaves`. This is in addition to any other stack
252 // objects we might have for X8 and X9, as they might be saved twice.
253 for (int I = 0; I < 2; ++I) {
254 int FI = MFI.CreateStackObject(TRI.getSpillSize(RC), TRI.getSpillAlign(RC),
255 true);
257 }
258}
259
263 const DebugLoc &DL) {
264 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
265
266 if (!RVFI->isSiFivePreemptibleInterrupt(MF))
267 return;
268
269 const auto &STI = MF.getSubtarget<RISCVSubtarget>();
270 const RISCVInstrInfo *TII = STI.getInstrInfo();
271
272 // FIXME: CFI Information here is nonexistent/wrong.
273
274 // X8 and X9 might be stored into the stack twice, initially into the
275 // `interruptCSRFrameIndex` here, and then maybe again into their CSI frame
276 // index.
277 //
278 // This is done instead of telling the register allocator that we need two
279 // VRegs to store the value of `mcause` and `mepc` through the instruction,
280 // which affects other passes.
281 TII->storeRegToStackSlot(MBB, MBBI, RISCV::X8, /* IsKill=*/true,
282 RVFI->getInterruptCSRFrameIndex(0),
283 &RISCV::GPRRegClass, STI.getRegisterInfo(),
285 TII->storeRegToStackSlot(MBB, MBBI, RISCV::X9, /* IsKill=*/true,
286 RVFI->getInterruptCSRFrameIndex(1),
287 &RISCV::GPRRegClass, STI.getRegisterInfo(),
289
290 // Put `mcause` into X8 (s0), and `mepc` into X9 (s1). If either of these are
291 // used in the function, then they will appear in `getUnmanagedCSI` and will
292 // be saved again.
293 BuildMI(MBB, MBBI, DL, TII->get(RISCV::CSRRS))
294 .addReg(RISCV::X8, RegState::Define)
295 .addImm(RISCVSysReg::mcause)
296 .addReg(RISCV::X0)
298 BuildMI(MBB, MBBI, DL, TII->get(RISCV::CSRRS))
299 .addReg(RISCV::X9, RegState::Define)
300 .addImm(RISCVSysReg::mepc)
301 .addReg(RISCV::X0)
303
304 // Enable interrupts.
305 BuildMI(MBB, MBBI, DL, TII->get(RISCV::CSRRSI))
306 .addReg(RISCV::X0, RegState::Define)
307 .addImm(RISCVSysReg::mstatus)
308 .addImm(8)
310}
311
315 const DebugLoc &DL) {
316 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
317
318 if (!RVFI->isSiFivePreemptibleInterrupt(MF))
319 return;
320
321 const auto &STI = MF.getSubtarget<RISCVSubtarget>();
322 const RISCVInstrInfo *TII = STI.getInstrInfo();
323
324 // FIXME: CFI Information here is nonexistent/wrong.
325
326 // Disable interrupts.
327 BuildMI(MBB, MBBI, DL, TII->get(RISCV::CSRRCI))
328 .addReg(RISCV::X0, RegState::Define)
329 .addImm(RISCVSysReg::mstatus)
330 .addImm(8)
332
333 // Restore `mepc` from x9 (s1), and `mcause` from x8 (s0). If either were used
334 // in the function, they have already been restored once, so now have the
335 // value stored in `emitSiFiveCLICPreemptibleSaves`.
336 BuildMI(MBB, MBBI, DL, TII->get(RISCV::CSRRW))
337 .addReg(RISCV::X0, RegState::Define)
338 .addImm(RISCVSysReg::mepc)
339 .addReg(RISCV::X9, RegState::Kill)
341 BuildMI(MBB, MBBI, DL, TII->get(RISCV::CSRRW))
342 .addReg(RISCV::X0, RegState::Define)
343 .addImm(RISCVSysReg::mcause)
344 .addReg(RISCV::X8, RegState::Kill)
346
347 // X8 and X9 need to be restored to their values on function entry, which we
348 // saved onto the stack in `emitSiFiveCLICPreemptibleSaves`.
349 TII->loadRegFromStackSlot(MBB, MBBI, RISCV::X9,
350 RVFI->getInterruptCSRFrameIndex(1),
351 &RISCV::GPRRegClass, STI.getRegisterInfo(),
353 TII->loadRegFromStackSlot(MBB, MBBI, RISCV::X8,
354 RVFI->getInterruptCSRFrameIndex(0),
355 &RISCV::GPRRegClass, STI.getRegisterInfo(),
357}
358
359// Get the ID of the libcall used for spilling and restoring callee saved
360// registers. The ID is representative of the number of registers saved or
361// restored by the libcall, except it is zero-indexed - ID 0 corresponds to a
362// single register.
363static int getLibCallID(const MachineFunction &MF,
364 const std::vector<CalleeSavedInfo> &CSI) {
365 const auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
366
367 if (CSI.empty() || !RVFI->useSaveRestoreLibCalls(MF))
368 return -1;
369
370 MCRegister MaxReg;
371 for (auto &CS : CSI)
372 // assignCalleeSavedSpillSlots assigns negative frame indexes to
373 // registers which can be saved by libcall.
374 if (CS.getFrameIdx() < 0)
375 MaxReg = std::max(MaxReg.id(), CS.getReg().id());
376
377 if (!MaxReg)
378 return -1;
379
380 switch (MaxReg.id()) {
381 default:
382 llvm_unreachable("Something has gone wrong!");
383 // clang-format off
384 case /*s11*/ RISCV::X27: return 12;
385 case /*s10*/ RISCV::X26: return 11;
386 case /*s9*/ RISCV::X25: return 10;
387 case /*s8*/ RISCV::X24: return 9;
388 case /*s7*/ RISCV::X23: return 8;
389 case /*s6*/ RISCV::X22: return 7;
390 case /*s5*/ RISCV::X21: return 6;
391 case /*s4*/ RISCV::X20: return 5;
392 case /*s3*/ RISCV::X19: return 4;
393 case /*s2*/ RISCV::X18: return 3;
394 case /*s1*/ RISCV::X9: return 2;
395 case /*s0*/ FPReg: return 1;
396 case /*ra*/ RAReg: return 0;
397 // clang-format on
398 }
399}
400
401// Get the name of the libcall used for spilling callee saved registers.
402// If this function will not use save/restore libcalls, then return a nullptr.
403static const char *
405 const std::vector<CalleeSavedInfo> &CSI) {
406 static const char *const SpillLibCalls[] = {
407 "__riscv_save_0",
408 "__riscv_save_1",
409 "__riscv_save_2",
410 "__riscv_save_3",
411 "__riscv_save_4",
412 "__riscv_save_5",
413 "__riscv_save_6",
414 "__riscv_save_7",
415 "__riscv_save_8",
416 "__riscv_save_9",
417 "__riscv_save_10",
418 "__riscv_save_11",
419 "__riscv_save_12"
420 };
421
422 int LibCallID = getLibCallID(MF, CSI);
423 if (LibCallID == -1)
424 return nullptr;
425 return SpillLibCalls[LibCallID];
426}
427
428// Get the name of the libcall used for restoring callee saved registers.
429// If this function will not use save/restore libcalls, then return a nullptr.
430static const char *
432 const std::vector<CalleeSavedInfo> &CSI) {
433 static const char *const RestoreLibCalls[] = {
434 "__riscv_restore_0",
435 "__riscv_restore_1",
436 "__riscv_restore_2",
437 "__riscv_restore_3",
438 "__riscv_restore_4",
439 "__riscv_restore_5",
440 "__riscv_restore_6",
441 "__riscv_restore_7",
442 "__riscv_restore_8",
443 "__riscv_restore_9",
444 "__riscv_restore_10",
445 "__riscv_restore_11",
446 "__riscv_restore_12"
447 };
448
449 int LibCallID = getLibCallID(MF, CSI);
450 if (LibCallID == -1)
451 return nullptr;
452 return RestoreLibCalls[LibCallID];
453}
454
455// Get the max reg of Push/Pop for restoring callee saved registers.
456static unsigned getNumPushPopRegs(const std::vector<CalleeSavedInfo> &CSI) {
457 unsigned NumPushPopRegs = 0;
458 for (auto &CS : CSI) {
459 auto *FII = llvm::find_if(FixedCSRFIMap,
460 [&](MCPhysReg P) { return P == CS.getReg(); });
461 if (FII != std::end(FixedCSRFIMap)) {
462 unsigned RegNum = std::distance(std::begin(FixedCSRFIMap), FII);
463 NumPushPopRegs = std::max(NumPushPopRegs, RegNum + 1);
464 }
465 }
466 assert(NumPushPopRegs != 12 && "x26 requires x27 to also be pushed");
467 return NumPushPopRegs;
468}
469
470// Return true if the specified function should have a dedicated frame
471// pointer register. This is true if frame pointer elimination is
472// disabled, if it needs dynamic stack realignment, if the function has
473// variable sized allocas, or if the frame address is taken.
475 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
476
477 const MachineFrameInfo &MFI = MF.getFrameInfo();
478 return MF.getTarget().Options.DisableFramePointerElim(MF) ||
479 RegInfo->hasStackRealignment(MF) || MFI.hasVarSizedObjects() ||
481}
482
484 const MachineFrameInfo &MFI = MF.getFrameInfo();
486
487 // If we do not reserve stack space for outgoing arguments in prologue,
488 // we will adjust the stack pointer before call instruction. After the
489 // adjustment, we can not use SP to access the stack objects for the
490 // arguments. Instead, use BP to access these stack objects.
491 return (MFI.hasVarSizedObjects() ||
493 MFI.getMaxCallFrameSize() != 0))) &&
494 TRI->hasStackRealignment(MF);
495}
496
497// Determines the size of the frame and maximum call frame size.
498void RISCVFrameLowering::determineFrameLayout(MachineFunction &MF) const {
499 MachineFrameInfo &MFI = MF.getFrameInfo();
500 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
501
502 // Get the number of bytes to allocate from the FrameInfo.
503 uint64_t FrameSize = MFI.getStackSize();
504
505 // QCI Interrupts use at least 96 bytes of stack space
506 if (RVFI->useQCIInterrupt(MF))
507 FrameSize = std::max(FrameSize, QCIInterruptPushAmount);
508
509 // Get the alignment.
510 Align StackAlign = getStackAlign();
511
512 // Make sure the frame is aligned.
513 FrameSize = alignTo(FrameSize, StackAlign);
514
515 // Update frame info.
516 MFI.setStackSize(FrameSize);
517
518 // When using SP or BP to access stack objects, we may require extra padding
519 // to ensure the bottom of the RVV stack is correctly aligned within the main
520 // stack. We calculate this as the amount required to align the scalar local
521 // variable section up to the RVV alignment.
523 if (RVFI->getRVVStackSize() && (!hasFP(MF) || TRI->hasStackRealignment(MF))) {
524 int ScalarLocalVarSize = FrameSize - RVFI->getCalleeSavedStackSize() -
525 RVFI->getVarArgsSaveSize();
526 if (auto RVVPadding =
527 offsetToAlignment(ScalarLocalVarSize, RVFI->getRVVStackAlign()))
528 RVFI->setRVVPadding(RVVPadding);
529 }
530}
531
532// Returns the stack size including RVV padding (when required), rounded back
533// up to the required stack alignment.
535 const MachineFunction &MF) const {
536 const MachineFrameInfo &MFI = MF.getFrameInfo();
537 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
538 return alignTo(MFI.getStackSize() + RVFI->getRVVPadding(), getStackAlign());
539}
540
543 const std::vector<CalleeSavedInfo> &CSI) {
544 const MachineFrameInfo &MFI = MF.getFrameInfo();
546
547 for (auto &CS : CSI) {
548 int FI = CS.getFrameIdx();
549 if (FI >= 0 && MFI.getStackID(FI) == TargetStackID::Default)
550 NonLibcallCSI.push_back(CS);
551 }
552
553 return NonLibcallCSI;
554}
555
558 const std::vector<CalleeSavedInfo> &CSI) {
559 const MachineFrameInfo &MFI = MF.getFrameInfo();
561
562 for (auto &CS : CSI) {
563 int FI = CS.getFrameIdx();
564 if (FI >= 0 && MFI.getStackID(FI) == TargetStackID::ScalableVector)
565 RVVCSI.push_back(CS);
566 }
567
568 return RVVCSI;
569}
570
573 const std::vector<CalleeSavedInfo> &CSI) {
574 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
575
576 SmallVector<CalleeSavedInfo, 8> PushOrLibCallsCSI;
577 if (!RVFI->useSaveRestoreLibCalls(MF) && !RVFI->isPushable(MF))
578 return PushOrLibCallsCSI;
579
580 for (const auto &CS : CSI) {
581 if (RVFI->useQCIInterrupt(MF)) {
582 // Some registers are saved by both `QC.C.MIENTER(.NEST)` and
583 // `QC.CM.PUSH(FP)`. In these cases, prioritise the CFI info that points
584 // to the versions saved by `QC.C.MIENTER(.NEST)` which is what FP
585 // unwinding would use.
587 CS.getReg()))
588 continue;
589 }
590
591 if (llvm::is_contained(FixedCSRFIMap, CS.getReg()))
592 PushOrLibCallsCSI.push_back(CS);
593 }
594
595 return PushOrLibCallsCSI;
596}
597
600 const std::vector<CalleeSavedInfo> &CSI) {
601 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
602
603 SmallVector<CalleeSavedInfo, 8> QCIInterruptCSI;
604 if (!RVFI->useQCIInterrupt(MF))
605 return QCIInterruptCSI;
606
607 for (const auto &CS : CSI) {
609 CS.getReg()))
610 QCIInterruptCSI.push_back(CS);
611 }
612
613 return QCIInterruptCSI;
614}
615
616void RISCVFrameLowering::allocateAndProbeStackForRVV(
618 MachineBasicBlock::iterator MBBI, const DebugLoc &DL, int64_t Amount,
619 MachineInstr::MIFlag Flag, bool EmitCFI, bool DynAllocation) const {
620 assert(Amount != 0 && "Did not need to adjust stack pointer for RVV.");
621
622 // Emit a variable-length allocation probing loop.
623
624 // Get VLEN in TargetReg
626 Register TargetReg = RISCV::X6;
627 uint32_t NumOfVReg = Amount / RISCV::RVVBytesPerBlock;
628 BuildMI(MBB, MBBI, DL, TII->get(RISCV::PseudoReadVLENB), TargetReg)
629 .setMIFlag(Flag);
630 TII->mulImm(MF, MBB, MBBI, DL, TargetReg, NumOfVReg, Flag);
631
633 if (EmitCFI) {
634 // Set the CFA register to TargetReg.
635 CFIBuilder.buildDefCFA(TargetReg, -Amount);
636 }
637
638 // It will be expanded to a probe loop in `inlineStackProbe`.
639 BuildMI(MBB, MBBI, DL, TII->get(RISCV::PROBED_STACKALLOC_RVV))
640 .addReg(TargetReg);
641
642 if (EmitCFI) {
643 // Set the CFA register back to SP.
644 CFIBuilder.buildDefCFARegister(SPReg);
645 }
646
647 // SUB SP, SP, T1
648 BuildMI(MBB, MBBI, DL, TII->get(RISCV::SUB), SPReg)
649 .addReg(SPReg)
650 .addReg(TargetReg)
651 .setMIFlag(Flag);
652
653 // If we have a dynamic allocation later we need to probe any residuals.
654 if (DynAllocation) {
655 BuildMI(MBB, MBBI, DL, TII->get(STI.is64Bit() ? RISCV::SD : RISCV::SW))
656 .addReg(RISCV::X0)
657 .addReg(SPReg)
658 .addImm(0)
660 }
661}
662
665 int FixedOffset, int ScalableOffset,
666 llvm::raw_string_ostream &Comment) {
667 unsigned DwarfVLenB = TRI.getDwarfRegNum(RISCV::VLENB, true);
668 uint8_t Buffer[16];
669 if (FixedOffset) {
670 Expr.push_back(dwarf::DW_OP_consts);
671 Expr.append(Buffer, Buffer + encodeSLEB128(FixedOffset, Buffer));
672 Expr.push_back((uint8_t)dwarf::DW_OP_plus);
673 Comment << (FixedOffset < 0 ? " - " : " + ") << std::abs(FixedOffset);
674 }
675
676 Expr.push_back((uint8_t)dwarf::DW_OP_consts);
677 Expr.append(Buffer, Buffer + encodeSLEB128(ScalableOffset, Buffer));
678
679 Expr.push_back((uint8_t)dwarf::DW_OP_bregx);
680 Expr.append(Buffer, Buffer + encodeULEB128(DwarfVLenB, Buffer));
681 Expr.push_back(0);
682
683 Expr.push_back((uint8_t)dwarf::DW_OP_mul);
684 Expr.push_back((uint8_t)dwarf::DW_OP_plus);
685
686 Comment << (ScalableOffset < 0 ? " - " : " + ") << std::abs(ScalableOffset)
687 << " * vlenb";
688}
689
691 Register Reg,
692 uint64_t FixedOffset,
693 uint64_t ScalableOffset) {
694 assert(ScalableOffset != 0 && "Did not need to adjust CFA for RVV");
695 SmallString<64> Expr;
696 std::string CommentBuffer;
697 llvm::raw_string_ostream Comment(CommentBuffer);
698 // Build up the expression (Reg + FixedOffset + ScalableOffset * VLENB).
699 unsigned DwarfReg = TRI.getDwarfRegNum(Reg, true);
700 Expr.push_back((uint8_t)(dwarf::DW_OP_breg0 + DwarfReg));
701 Expr.push_back(0);
702 if (Reg == SPReg)
703 Comment << "sp";
704 else
705 Comment << printReg(Reg, &TRI);
706
707 appendScalableVectorExpression(TRI, Expr, FixedOffset, ScalableOffset,
708 Comment);
709
710 SmallString<64> DefCfaExpr;
711 uint8_t Buffer[16];
712 DefCfaExpr.push_back(dwarf::DW_CFA_def_cfa_expression);
713 DefCfaExpr.append(Buffer, Buffer + encodeULEB128(Expr.size(), Buffer));
714 DefCfaExpr.append(Expr.str());
715
716 return MCCFIInstruction::createEscape(nullptr, DefCfaExpr.str(), SMLoc(),
717 Comment.str());
718}
719
721 Register Reg, uint64_t FixedOffset,
722 uint64_t ScalableOffset) {
723 assert(ScalableOffset != 0 && "Did not need to adjust CFA for RVV");
724 SmallString<64> Expr;
725 std::string CommentBuffer;
726 llvm::raw_string_ostream Comment(CommentBuffer);
727 Comment << printReg(Reg, &TRI) << " @ cfa";
728
729 // Build up the expression (FixedOffset + ScalableOffset * VLENB).
730 appendScalableVectorExpression(TRI, Expr, FixedOffset, ScalableOffset,
731 Comment);
732
733 SmallString<64> DefCfaExpr;
734 uint8_t Buffer[16];
735 unsigned DwarfReg = TRI.getDwarfRegNum(Reg, true);
736 DefCfaExpr.push_back(dwarf::DW_CFA_expression);
737 DefCfaExpr.append(Buffer, Buffer + encodeULEB128(DwarfReg, Buffer));
738 DefCfaExpr.append(Buffer, Buffer + encodeULEB128(Expr.size(), Buffer));
739 DefCfaExpr.append(Expr.str());
740
741 return MCCFIInstruction::createEscape(nullptr, DefCfaExpr.str(), SMLoc(),
742 Comment.str());
743}
744
745// Allocate stack space and probe it if necessary.
749 uint64_t RealStackSize, bool EmitCFI,
750 bool NeedProbe, uint64_t ProbeSize,
751 bool DynAllocation,
752 MachineInstr::MIFlag Flag) const {
753 DebugLoc DL;
756 bool IsRV64 = STI.is64Bit();
758
759 // Simply allocate the stack if it's not big enough to require a probe.
760 if (!NeedProbe || Offset <= ProbeSize) {
762 Flag, getStackAlign());
763
764 if (EmitCFI)
765 CFIBuilder.buildDefCFAOffset(RealStackSize);
766
767 if (NeedProbe && DynAllocation) {
768 // s[d|w] zero, 0(sp)
769 BuildMI(MBB, MBBI, DL, TII->get(IsRV64 ? RISCV::SD : RISCV::SW))
770 .addReg(RISCV::X0)
771 .addReg(SPReg)
772 .addImm(0)
773 .setMIFlags(Flag);
774 }
775
776 return;
777 }
778
779 // Unroll the probe loop depending on the number of iterations.
780 if (Offset < ProbeSize * 5) {
781 uint64_t CurrentOffset = 0;
782 while (CurrentOffset + ProbeSize <= Offset) {
783 RI->adjustReg(MBB, MBBI, DL, SPReg, SPReg,
784 StackOffset::getFixed(-ProbeSize), Flag, getStackAlign());
785 // s[d|w] zero, 0(sp)
786 BuildMI(MBB, MBBI, DL, TII->get(IsRV64 ? RISCV::SD : RISCV::SW))
787 .addReg(RISCV::X0)
788 .addReg(SPReg)
789 .addImm(0)
790 .setMIFlags(Flag);
791
792 CurrentOffset += ProbeSize;
793 if (EmitCFI)
794 CFIBuilder.buildDefCFAOffset(CurrentOffset);
795 }
796
797 uint64_t Residual = Offset - CurrentOffset;
798 if (Residual) {
799 RI->adjustReg(MBB, MBBI, DL, SPReg, SPReg,
800 StackOffset::getFixed(-Residual), Flag, getStackAlign());
801 if (EmitCFI)
802 CFIBuilder.buildDefCFAOffset(Offset);
803
804 if (DynAllocation) {
805 // s[d|w] zero, 0(sp)
806 BuildMI(MBB, MBBI, DL, TII->get(IsRV64 ? RISCV::SD : RISCV::SW))
807 .addReg(RISCV::X0)
808 .addReg(SPReg)
809 .addImm(0)
810 .setMIFlags(Flag);
811 }
812 }
813
814 return;
815 }
816
817 // Emit a variable-length allocation probing loop.
818 uint64_t RoundedSize = alignDown(Offset, ProbeSize);
819 uint64_t Residual = Offset - RoundedSize;
820
821 Register TargetReg = RISCV::X6;
822 // SUB TargetReg, SP, RoundedSize
823 RI->adjustReg(MBB, MBBI, DL, TargetReg, SPReg,
824 StackOffset::getFixed(-RoundedSize), Flag, getStackAlign());
825
826 if (EmitCFI) {
827 // Set the CFA register to TargetReg.
828 CFIBuilder.buildDefCFA(TargetReg, RoundedSize);
829 }
830
831 // It will be expanded to a probe loop in `inlineStackProbe`.
832 BuildMI(MBB, MBBI, DL, TII->get(RISCV::PROBED_STACKALLOC)).addReg(TargetReg);
833
834 if (EmitCFI) {
835 // Set the CFA register back to SP.
836 CFIBuilder.buildDefCFARegister(SPReg);
837 }
838
839 if (Residual) {
841 Flag, getStackAlign());
842 if (DynAllocation) {
843 // s[d|w] zero, 0(sp)
844 BuildMI(MBB, MBBI, DL, TII->get(IsRV64 ? RISCV::SD : RISCV::SW))
845 .addReg(RISCV::X0)
846 .addReg(SPReg)
847 .addImm(0)
848 .setMIFlags(Flag);
849 }
850 }
851
852 if (EmitCFI)
853 CFIBuilder.buildDefCFAOffset(Offset);
854}
855
856static bool isPush(unsigned Opcode) {
857 switch (Opcode) {
858 case RISCV::CM_PUSH:
859 case RISCV::QC_CM_PUSH:
860 case RISCV::QC_CM_PUSHFP:
861 return true;
862 default:
863 return false;
864 }
865}
866
867static bool isPop(unsigned Opcode) {
868 // There are other pops but these are the only ones introduced during this
869 // pass.
870 switch (Opcode) {
871 case RISCV::CM_POP:
872 case RISCV::QC_CM_POP:
873 return true;
874 default:
875 return false;
876 }
877}
878
880 bool UpdateFP) {
881 switch (Kind) {
883 return RISCV::CM_PUSH;
885 return UpdateFP ? RISCV::QC_CM_PUSHFP : RISCV::QC_CM_PUSH;
886 default:
887 llvm_unreachable("Unhandled PushPopKind");
888 }
889}
890
892 // There are other pops but they are introduced later by the Push/Pop
893 // Optimizer.
894 switch (Kind) {
896 return RISCV::CM_POP;
898 return RISCV::QC_CM_POP;
899 default:
900 llvm_unreachable("Unhandled PushPopKind");
901 }
902}
903
905 MachineBasicBlock &MBB) const {
906 MachineFrameInfo &MFI = MF.getFrameInfo();
907 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
910
912
913 // Debug location must be unknown since the first debug location is used
914 // to determine the end of the prologue.
915 DebugLoc DL;
916
917 // All calls are tail calls in GHC calling conv, and functions have no
918 // prologue/epilogue.
920 return;
921
922 // SiFive CLIC needs to swap `sp` into `sf.mscratchcsw`
924
925 // Emit prologue for shadow call stack.
926 emitSCSPrologue(MF, MBB, MBBI, DL);
927
928 // We keep track of the first instruction because it might be a
929 // `(QC.)CM.PUSH(FP)`, and we may need to adjust the immediate rather than
930 // inserting an `addi sp, sp, -N*16`
931 auto PossiblePush = MBBI;
932
933 // Skip past all callee-saved register spill instructions.
934 while (MBBI != MBB.end() && MBBI->getFlag(MachineInstr::FrameSetup))
935 ++MBBI;
936
937 // Determine the correct frame layout
938 determineFrameLayout(MF);
939
940 const auto &CSI = MFI.getCalleeSavedInfo();
941
942 // Skip to before the spills of scalar callee-saved registers
943 // FIXME: assumes exactly one instruction is used to restore each
944 // callee-saved register.
945 MBBI = std::prev(MBBI, getRVVCalleeSavedInfo(MF, CSI).size() +
946 getUnmanagedCSI(MF, CSI).size());
948 bool NeedsDwarfCFI = needsDwarfCFI(MF);
949
950 // If libcalls are used to spill and restore callee-saved registers, the frame
951 // has two sections; the opaque section managed by the libcalls, and the
952 // section managed by MachineFrameInfo which can also hold callee saved
953 // registers in fixed stack slots, both of which have negative frame indices.
954 // This gets even more complicated when incoming arguments are passed via the
955 // stack, as these too have negative frame indices. An example is detailed
956 // below:
957 //
958 // | incoming arg | <- FI[-3]
959 // | libcallspill |
960 // | calleespill | <- FI[-2]
961 // | calleespill | <- FI[-1]
962 // | this_frame | <- FI[0]
963 //
964 // For negative frame indices, the offset from the frame pointer will differ
965 // depending on which of these groups the frame index applies to.
966 // The following calculates the correct offset knowing the number of callee
967 // saved registers spilt by the two methods.
968 if (int LibCallRegs = getLibCallID(MF, MFI.getCalleeSavedInfo()) + 1) {
969 // Calculate the size of the frame managed by the libcall. The stack
970 // alignment of these libcalls should be the same as how we set it in
971 // getABIStackAlignment.
972 unsigned LibCallFrameSize =
973 alignTo((STI.getXLen() / 8) * LibCallRegs, getStackAlign());
974 RVFI->setLibCallStackSize(LibCallFrameSize);
975
976 if (NeedsDwarfCFI) {
977 CFIBuilder.buildDefCFAOffset(LibCallFrameSize);
978 for (const CalleeSavedInfo &CS : getPushOrLibCallsSavedInfo(MF, CSI))
979 CFIBuilder.buildOffset(CS.getReg(),
980 MFI.getObjectOffset(CS.getFrameIdx()));
981 }
982 }
983
984 // FIXME (note copied from Lanai): This appears to be overallocating. Needs
985 // investigation. Get the number of bytes to allocate from the FrameInfo.
986 uint64_t RealStackSize = getStackSizeWithRVVPadding(MF);
987 uint64_t StackSize = RealStackSize - RVFI->getReservedSpillsSize();
988 uint64_t RVVStackSize = RVFI->getRVVStackSize();
989
990 // Early exit if there is no need to allocate on the stack
991 if (RealStackSize == 0 && !MFI.adjustsStack() && RVVStackSize == 0)
992 return;
993
994 // If the stack pointer has been marked as reserved, then produce an error if
995 // the frame requires stack allocation
998 MF.getFunction(), "Stack pointer required, but has been reserved."});
999
1000 uint64_t FirstSPAdjustAmount = getFirstSPAdjustAmount(MF);
1001 // Split the SP adjustment to reduce the offsets of callee saved spill.
1002 if (FirstSPAdjustAmount) {
1003 StackSize = FirstSPAdjustAmount;
1004 RealStackSize = FirstSPAdjustAmount;
1005 }
1006
1007 if (RVFI->useQCIInterrupt(MF)) {
1008 // The function starts with `QC.C.MIENTER(.NEST)`, so the `(QC.)CM.PUSH(FP)`
1009 // could only be the next instruction.
1010 ++PossiblePush;
1011
1012 if (NeedsDwarfCFI) {
1013 // Insert the CFI metadata before where we think the `(QC.)CM.PUSH(FP)`
1014 // could be. The PUSH will also get its own CFI metadata for its own
1015 // modifications, which should come after the PUSH.
1016 CFIInstBuilder PushCFIBuilder(MBB, PossiblePush,
1019 for (const CalleeSavedInfo &CS : getQCISavedInfo(MF, CSI))
1020 PushCFIBuilder.buildOffset(CS.getReg(),
1021 MFI.getObjectOffset(CS.getFrameIdx()));
1022 }
1023 }
1024
1025 if (RVFI->isPushable(MF) && PossiblePush != MBB.end() &&
1026 isPush(PossiblePush->getOpcode())) {
1027 // Use available stack adjustment in push instruction to allocate additional
1028 // stack space. Align the stack size down to a multiple of 16. This is
1029 // needed for RVE.
1030 // FIXME: Can we increase the stack size to a multiple of 16 instead?
1031 uint64_t StackAdj =
1032 std::min(alignDown(StackSize, 16), static_cast<uint64_t>(48));
1033 PossiblePush->getOperand(1).setImm(StackAdj);
1034 StackSize -= StackAdj;
1035
1036 if (NeedsDwarfCFI) {
1037 CFIBuilder.buildDefCFAOffset(RealStackSize - StackSize);
1038 for (const CalleeSavedInfo &CS : getPushOrLibCallsSavedInfo(MF, CSI))
1039 CFIBuilder.buildOffset(CS.getReg(),
1040 MFI.getObjectOffset(CS.getFrameIdx()));
1041 }
1042 }
1043
1044 // Allocate space on the stack if necessary.
1045 auto &Subtarget = MF.getSubtarget<RISCVSubtarget>();
1046 const RISCVTargetLowering *TLI = Subtarget.getTargetLowering();
1047 bool NeedProbe = TLI->hasInlineStackProbe(MF);
1048 uint64_t ProbeSize = TLI->getStackProbeSize(MF, getStackAlign());
1049 bool DynAllocation =
1050 MF.getInfo<RISCVMachineFunctionInfo>()->hasDynamicAllocation();
1051 if (StackSize != 0)
1052 allocateStack(MBB, MBBI, MF, StackSize, RealStackSize, NeedsDwarfCFI,
1053 NeedProbe, ProbeSize, DynAllocation,
1055
1056 // Save SiFive CLIC CSRs into Stack
1058
1059 // The frame pointer is callee-saved, and code has been generated for us to
1060 // save it to the stack. We need to skip over the storing of callee-saved
1061 // registers as the frame pointer must be modified after it has been saved
1062 // to the stack, not before.
1063 // FIXME: assumes exactly one instruction is used to save each callee-saved
1064 // register.
1065 std::advance(MBBI, getUnmanagedCSI(MF, CSI).size());
1066 CFIBuilder.setInsertPoint(MBBI);
1067
1068 // Iterate over list of callee-saved registers and emit .cfi_offset
1069 // directives.
1070 if (NeedsDwarfCFI)
1071 for (const CalleeSavedInfo &CS : getUnmanagedCSI(MF, CSI))
1072 CFIBuilder.buildOffset(CS.getReg(),
1073 MFI.getObjectOffset(CS.getFrameIdx()));
1074
1075 // Generate new FP.
1076 if (hasFP(MF)) {
1079 MF.getFunction(), "Frame pointer required, but has been reserved."});
1080 // The frame pointer does need to be reserved from register allocation.
1081 assert(MF.getRegInfo().isReserved(FPReg) && "FP not reserved");
1082
1083 // Some stack management variants automatically keep FP updated, so we don't
1084 // need an instruction to do so.
1085 if (!RVFI->hasImplicitFPUpdates(MF)) {
1086 RI->adjustReg(
1087 MBB, MBBI, DL, FPReg, SPReg,
1088 StackOffset::getFixed(RealStackSize - RVFI->getVarArgsSaveSize()),
1090 }
1091
1092 if (NeedsDwarfCFI)
1093 CFIBuilder.buildDefCFA(FPReg, RVFI->getVarArgsSaveSize());
1094 }
1095
1096 uint64_t SecondSPAdjustAmount = 0;
1097 // Emit the second SP adjustment after saving callee saved registers.
1098 if (FirstSPAdjustAmount) {
1099 SecondSPAdjustAmount = getStackSizeWithRVVPadding(MF) - FirstSPAdjustAmount;
1100 assert(SecondSPAdjustAmount > 0 &&
1101 "SecondSPAdjustAmount should be greater than zero");
1102
1103 allocateStack(MBB, MBBI, MF, SecondSPAdjustAmount,
1104 getStackSizeWithRVVPadding(MF), NeedsDwarfCFI && !hasFP(MF),
1105 NeedProbe, ProbeSize, DynAllocation,
1107 }
1108
1109 if (RVVStackSize) {
1110 if (NeedProbe) {
1111 allocateAndProbeStackForRVV(MF, MBB, MBBI, DL, RVVStackSize,
1113 NeedsDwarfCFI && !hasFP(MF), DynAllocation);
1114 } else {
1115 // We must keep the stack pointer aligned through any intermediate
1116 // updates.
1117 RI->adjustReg(MBB, MBBI, DL, SPReg, SPReg,
1118 StackOffset::getScalable(-RVVStackSize),
1120 }
1121
1122 if (NeedsDwarfCFI && !hasFP(MF)) {
1123 // Emit .cfi_def_cfa_expression "sp + StackSize + RVVStackSize * vlenb".
1125 *RI, SPReg, getStackSizeWithRVVPadding(MF), RVVStackSize / 8));
1126 }
1127
1128 std::advance(MBBI, getRVVCalleeSavedInfo(MF, CSI).size());
1129 if (NeedsDwarfCFI)
1130 emitCalleeSavedRVVPrologCFI(MBB, MBBI, hasFP(MF));
1131 }
1132
1133 if (hasFP(MF)) {
1134 // Realign Stack
1136 if (RI->hasStackRealignment(MF)) {
1137 Align MaxAlignment = MFI.getMaxAlign();
1138
1140 if (isInt<12>(-(int)MaxAlignment.value())) {
1141 BuildMI(MBB, MBBI, DL, TII->get(RISCV::ANDI), SPReg)
1142 .addReg(SPReg)
1143 .addImm(-(int)MaxAlignment.value())
1145 } else {
1146 unsigned ShiftAmount = Log2(MaxAlignment);
1147 Register VR =
1148 MF.getRegInfo().createVirtualRegister(&RISCV::GPRRegClass);
1149 BuildMI(MBB, MBBI, DL, TII->get(RISCV::SRLI), VR)
1150 .addReg(SPReg)
1151 .addImm(ShiftAmount)
1153 BuildMI(MBB, MBBI, DL, TII->get(RISCV::SLLI), SPReg)
1154 .addReg(VR)
1155 .addImm(ShiftAmount)
1157 }
1158 if (NeedProbe && RVVStackSize == 0) {
1159 // Do a probe if the align + size allocated just passed the probe size
1160 // and was not yet probed.
1161 if (SecondSPAdjustAmount < ProbeSize &&
1162 SecondSPAdjustAmount + MaxAlignment.value() >= ProbeSize) {
1163 bool IsRV64 = STI.is64Bit();
1164 BuildMI(MBB, MBBI, DL, TII->get(IsRV64 ? RISCV::SD : RISCV::SW))
1165 .addReg(RISCV::X0)
1166 .addReg(SPReg)
1167 .addImm(0)
1169 }
1170 }
1171 // FP will be used to restore the frame in the epilogue, so we need
1172 // another base register BP to record SP after re-alignment. SP will
1173 // track the current stack after allocating variable sized objects.
1174 if (hasBP(MF)) {
1175 // move BP, SP
1176 BuildMI(MBB, MBBI, DL, TII->get(RISCV::ADDI), BPReg)
1177 .addReg(SPReg)
1178 .addImm(0)
1180 }
1181 }
1182 }
1183}
1184
1185void RISCVFrameLowering::deallocateStack(MachineFunction &MF,
1188 const DebugLoc &DL,
1189 uint64_t &StackSize,
1190 int64_t CFAOffset) const {
1192
1193 RI->adjustReg(MBB, MBBI, DL, SPReg, SPReg, StackOffset::getFixed(StackSize),
1195 StackSize = 0;
1196
1197 if (needsDwarfCFI(MF))
1199 .buildDefCFAOffset(CFAOffset);
1200}
1201
1203 MachineBasicBlock &MBB) const {
1205 MachineFrameInfo &MFI = MF.getFrameInfo();
1206 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
1207
1208 // All calls are tail calls in GHC calling conv, and functions have no
1209 // prologue/epilogue.
1211 return;
1212
1213 // Get the insert location for the epilogue. If there were no terminators in
1214 // the block, get the last instruction.
1216 DebugLoc DL;
1217 if (!MBB.empty()) {
1219 if (MBBI != MBB.end())
1220 DL = MBBI->getDebugLoc();
1221
1223
1224 // Skip to before the restores of all callee-saved registers.
1225 while (MBBI != MBB.begin() &&
1226 std::prev(MBBI)->getFlag(MachineInstr::FrameDestroy))
1227 --MBBI;
1228 }
1229
1230 const auto &CSI = MFI.getCalleeSavedInfo();
1231
1232 // Skip to before the restores of scalar callee-saved registers
1233 // FIXME: assumes exactly one instruction is used to restore each
1234 // callee-saved register.
1235 auto FirstScalarCSRRestoreInsn =
1236 std::next(MBBI, getRVVCalleeSavedInfo(MF, CSI).size());
1237 CFIInstBuilder CFIBuilder(MBB, FirstScalarCSRRestoreInsn,
1239 bool NeedsDwarfCFI = needsDwarfCFI(MF);
1240
1241 uint64_t FirstSPAdjustAmount = getFirstSPAdjustAmount(MF);
1242 uint64_t RealStackSize = FirstSPAdjustAmount ? FirstSPAdjustAmount
1244 uint64_t StackSize = FirstSPAdjustAmount ? FirstSPAdjustAmount
1246 RVFI->getReservedSpillsSize();
1247 uint64_t FPOffset = RealStackSize - RVFI->getVarArgsSaveSize();
1248 uint64_t RVVStackSize = RVFI->getRVVStackSize();
1249
1250 bool RestoreSPFromFP = RI->hasStackRealignment(MF) ||
1252 if (RVVStackSize) {
1253 // If RestoreSPFromFP the stack pointer will be restored using the frame
1254 // pointer value.
1255 if (!RestoreSPFromFP)
1256 RI->adjustReg(MBB, FirstScalarCSRRestoreInsn, DL, SPReg, SPReg,
1257 StackOffset::getScalable(RVVStackSize),
1259
1260 if (NeedsDwarfCFI) {
1261 if (!hasFP(MF))
1262 CFIBuilder.buildDefCFA(SPReg, RealStackSize);
1263 emitCalleeSavedRVVEpilogCFI(MBB, FirstScalarCSRRestoreInsn);
1264 }
1265 }
1266
1267 if (FirstSPAdjustAmount) {
1268 uint64_t SecondSPAdjustAmount =
1269 getStackSizeWithRVVPadding(MF) - FirstSPAdjustAmount;
1270 assert(SecondSPAdjustAmount > 0 &&
1271 "SecondSPAdjustAmount should be greater than zero");
1272
1273 // If RestoreSPFromFP the stack pointer will be restored using the frame
1274 // pointer value.
1275 if (!RestoreSPFromFP)
1276 RI->adjustReg(MBB, FirstScalarCSRRestoreInsn, DL, SPReg, SPReg,
1277 StackOffset::getFixed(SecondSPAdjustAmount),
1279
1280 if (NeedsDwarfCFI && !hasFP(MF))
1281 CFIBuilder.buildDefCFAOffset(FirstSPAdjustAmount);
1282 }
1283
1284 // Restore the stack pointer using the value of the frame pointer. Only
1285 // necessary if the stack pointer was modified, meaning the stack size is
1286 // unknown.
1287 //
1288 // In order to make sure the stack point is right through the EH region,
1289 // we also need to restore stack pointer from the frame pointer if we
1290 // don't preserve stack space within prologue/epilogue for outgoing variables,
1291 // normally it's just checking the variable sized object is present or not
1292 // is enough, but we also don't preserve that at prologue/epilogue when
1293 // have vector objects in stack.
1294 if (RestoreSPFromFP) {
1295 assert(hasFP(MF) && "frame pointer should not have been eliminated");
1296 RI->adjustReg(MBB, FirstScalarCSRRestoreInsn, DL, SPReg, FPReg,
1298 getStackAlign());
1299 }
1300
1301 if (NeedsDwarfCFI && hasFP(MF))
1302 CFIBuilder.buildDefCFA(SPReg, RealStackSize);
1303
1304 // Skip to after the restores of scalar callee-saved registers
1305 // FIXME: assumes exactly one instruction is used to restore each
1306 // callee-saved register.
1307 MBBI = std::next(FirstScalarCSRRestoreInsn, getUnmanagedCSI(MF, CSI).size());
1308 CFIBuilder.setInsertPoint(MBBI);
1309
1310 if (getLibCallID(MF, CSI) != -1) {
1311 // tail __riscv_restore_[0-12] instruction is considered as a terminator,
1312 // therefore it is unnecessary to place any CFI instructions after it. Just
1313 // deallocate stack if needed and return.
1314 if (StackSize != 0)
1315 deallocateStack(MF, MBB, MBBI, DL, StackSize,
1316 RVFI->getLibCallStackSize());
1317
1318 // Emit epilogue for shadow call stack.
1319 emitSCSEpilogue(MF, MBB, MBBI, DL);
1320 return;
1321 }
1322
1323 // Recover callee-saved registers.
1324 if (NeedsDwarfCFI)
1325 for (const CalleeSavedInfo &CS : getUnmanagedCSI(MF, CSI))
1326 CFIBuilder.buildRestore(CS.getReg());
1327
1328 if (RVFI->isPushable(MF) && MBBI != MBB.end() && isPop(MBBI->getOpcode())) {
1329 // Use available stack adjustment in pop instruction to deallocate stack
1330 // space. Align the stack size down to a multiple of 16. This is needed for
1331 // RVE.
1332 // FIXME: Can we increase the stack size to a multiple of 16 instead?
1333 uint64_t StackAdj =
1334 std::min(alignDown(StackSize, 16), static_cast<uint64_t>(48));
1335 MBBI->getOperand(1).setImm(StackAdj);
1336 StackSize -= StackAdj;
1337
1338 if (StackSize != 0)
1339 deallocateStack(MF, MBB, MBBI, DL, StackSize,
1340 /*stack_adj of cm.pop instr*/ RealStackSize - StackSize);
1341
1342 auto NextI = next_nodbg(MBBI, MBB.end());
1343 if (NextI == MBB.end() || NextI->getOpcode() != RISCV::PseudoRET) {
1344 ++MBBI;
1345 if (NeedsDwarfCFI) {
1346 CFIBuilder.setInsertPoint(MBBI);
1347
1348 for (const CalleeSavedInfo &CS : getPushOrLibCallsSavedInfo(MF, CSI))
1349 CFIBuilder.buildRestore(CS.getReg());
1350
1351 // Update CFA Offset. If this is a QCI interrupt function, there will
1352 // be a leftover offset which is deallocated by `QC.C.MILEAVERET`,
1353 // otherwise getQCIInterruptStackSize() will be 0.
1354 CFIBuilder.buildDefCFAOffset(RVFI->getQCIInterruptStackSize());
1355 }
1356 }
1357 }
1358
1360
1361 // Deallocate stack if StackSize isn't a zero yet. If this is a QCI interrupt
1362 // function, there will be a leftover offset which is deallocated by
1363 // `QC.C.MILEAVERET`, otherwise getQCIInterruptStackSize() will be 0.
1364 if (StackSize != 0)
1365 deallocateStack(MF, MBB, MBBI, DL, StackSize,
1366 RVFI->getQCIInterruptStackSize());
1367
1368 // Emit epilogue for shadow call stack.
1369 emitSCSEpilogue(MF, MBB, MBBI, DL);
1370
1371 // SiFive CLIC needs to swap `sf.mscratchcsw` into `sp`
1373}
1374
1377 Register &FrameReg) const {
1378 const MachineFrameInfo &MFI = MF.getFrameInfo();
1380 const auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
1381
1382 // Callee-saved registers should be referenced relative to the stack
1383 // pointer (positive offset), otherwise use the frame pointer (negative
1384 // offset).
1385 const auto &CSI = getUnmanagedCSI(MF, MFI.getCalleeSavedInfo());
1386 int MinCSFI = 0;
1387 int MaxCSFI = -1;
1389 auto StackID = MFI.getStackID(FI);
1390
1391 assert((StackID == TargetStackID::Default ||
1392 StackID == TargetStackID::ScalableVector) &&
1393 "Unexpected stack ID for the frame object.");
1394 if (StackID == TargetStackID::Default) {
1395 assert(getOffsetOfLocalArea() == 0 && "LocalAreaOffset is not 0!");
1397 MFI.getOffsetAdjustment());
1398 } else if (StackID == TargetStackID::ScalableVector) {
1400 }
1401
1402 uint64_t FirstSPAdjustAmount = getFirstSPAdjustAmount(MF);
1403
1404 if (CSI.size()) {
1405 MinCSFI = CSI[0].getFrameIdx();
1406 MaxCSFI = CSI[CSI.size() - 1].getFrameIdx();
1407 }
1408
1409 if (FI >= MinCSFI && FI <= MaxCSFI) {
1410 FrameReg = SPReg;
1411
1412 if (FirstSPAdjustAmount)
1413 Offset += StackOffset::getFixed(FirstSPAdjustAmount);
1414 else
1416 return Offset;
1417 }
1418
1419 if (RI->hasStackRealignment(MF) && !MFI.isFixedObjectIndex(FI)) {
1420 // If the stack was realigned, the frame pointer is set in order to allow
1421 // SP to be restored, so we need another base register to record the stack
1422 // after realignment.
1423 // |--------------------------| -- <-- FP
1424 // | callee-allocated save | | <----|
1425 // | area for register varargs| | |
1426 // |--------------------------| | |
1427 // | callee-saved registers | | |
1428 // |--------------------------| -- |
1429 // | realignment (the size of | | |
1430 // | this area is not counted | | |
1431 // | in MFI.getStackSize()) | | |
1432 // |--------------------------| -- |-- MFI.getStackSize()
1433 // | RVV alignment padding | | |
1434 // | (not counted in | | |
1435 // | MFI.getStackSize() but | | |
1436 // | counted in | | |
1437 // | RVFI.getRVVStackSize()) | | |
1438 // |--------------------------| -- |
1439 // | RVV objects | | |
1440 // | (not counted in | | |
1441 // | MFI.getStackSize()) | | |
1442 // |--------------------------| -- |
1443 // | padding before RVV | | |
1444 // | (not counted in | | |
1445 // | MFI.getStackSize() or in | | |
1446 // | RVFI.getRVVStackSize()) | | |
1447 // |--------------------------| -- |
1448 // | scalar local variables | | <----'
1449 // |--------------------------| -- <-- BP (if var sized objects present)
1450 // | VarSize objects | |
1451 // |--------------------------| -- <-- SP
1452 if (hasBP(MF)) {
1453 FrameReg = RISCVABI::getBPReg();
1454 } else {
1455 // VarSize objects must be empty in this case!
1456 assert(!MFI.hasVarSizedObjects());
1457 FrameReg = SPReg;
1458 }
1459 } else {
1460 FrameReg = RI->getFrameRegister(MF);
1461 }
1462
1463 if (FrameReg == FPReg) {
1464 Offset += StackOffset::getFixed(RVFI->getVarArgsSaveSize());
1465 // When using FP to access scalable vector objects, we need to minus
1466 // the frame size.
1467 //
1468 // |--------------------------| -- <-- FP
1469 // | callee-allocated save | |
1470 // | area for register varargs| |
1471 // |--------------------------| |
1472 // | callee-saved registers | |
1473 // |--------------------------| | MFI.getStackSize()
1474 // | scalar local variables | |
1475 // |--------------------------| -- (Offset of RVV objects is from here.)
1476 // | RVV objects |
1477 // |--------------------------|
1478 // | VarSize objects |
1479 // |--------------------------| <-- SP
1480 if (StackID == TargetStackID::ScalableVector) {
1481 assert(!RI->hasStackRealignment(MF) &&
1482 "Can't index across variable sized realign");
1483 // We don't expect any extra RVV alignment padding, as the stack size
1484 // and RVV object sections should be correct aligned in their own
1485 // right.
1487 "Inconsistent stack layout");
1489 }
1490 return Offset;
1491 }
1492
1493 // This case handles indexing off both SP and BP.
1494 // If indexing off SP, there must not be any var sized objects
1495 assert(FrameReg == RISCVABI::getBPReg() || !MFI.hasVarSizedObjects());
1496
1497 // When using SP to access frame objects, we need to add RVV stack size.
1498 //
1499 // |--------------------------| -- <-- FP
1500 // | callee-allocated save | | <----|
1501 // | area for register varargs| | |
1502 // |--------------------------| | |
1503 // | callee-saved registers | | |
1504 // |--------------------------| -- |
1505 // | RVV alignment padding | | |
1506 // | (not counted in | | |
1507 // | MFI.getStackSize() but | | |
1508 // | counted in | | |
1509 // | RVFI.getRVVStackSize()) | | |
1510 // |--------------------------| -- |
1511 // | RVV objects | | |-- MFI.getStackSize()
1512 // | (not counted in | | |
1513 // | MFI.getStackSize()) | | |
1514 // |--------------------------| -- |
1515 // | padding before RVV | | |
1516 // | (not counted in | | |
1517 // | MFI.getStackSize()) | | |
1518 // |--------------------------| -- |
1519 // | scalar local variables | | <----'
1520 // |--------------------------| -- <-- BP (if var sized objects present)
1521 // | VarSize objects | |
1522 // |--------------------------| -- <-- SP
1523 //
1524 // The total amount of padding surrounding RVV objects is described by
1525 // RVV->getRVVPadding() and it can be zero. It allows us to align the RVV
1526 // objects to the required alignment.
1527 if (MFI.getStackID(FI) == TargetStackID::Default) {
1528 if (MFI.isFixedObjectIndex(FI)) {
1529 assert(!RI->hasStackRealignment(MF) &&
1530 "Can't index across variable sized realign");
1532 RVFI->getRVVStackSize());
1533 } else {
1535 }
1536 } else if (MFI.getStackID(FI) == TargetStackID::ScalableVector) {
1537 // Ensure the base of the RVV stack is correctly aligned: add on the
1538 // alignment padding.
1539 int ScalarLocalVarSize = MFI.getStackSize() -
1540 RVFI->getCalleeSavedStackSize() -
1541 RVFI->getVarArgsSaveSize() + RVFI->getRVVPadding();
1542 Offset += StackOffset::get(ScalarLocalVarSize, RVFI->getRVVStackSize());
1543 }
1544 return Offset;
1545}
1546
1548 const Register &Reg) {
1549 MCRegister BaseReg = TRI.getSubReg(Reg, RISCV::sub_vrm1_0);
1550 // If it's not a grouped vector register, it doesn't have subregister, so
1551 // the base register is just itself.
1552 if (BaseReg == RISCV::NoRegister)
1553 BaseReg = Reg;
1554 return BaseReg;
1555}
1556
1558 BitVector &SavedRegs,
1559 RegScavenger *RS) const {
1561
1562 // In TargetFrameLowering::determineCalleeSaves, any vector register is marked
1563 // as saved if any of its subregister is clobbered, this is not correct in
1564 // vector registers. We only want the vector register to be marked as saved
1565 // if all of its subregisters are clobbered.
1566 // For example:
1567 // Original behavior: If v24 is marked, v24m2, v24m4, v24m8 are also marked.
1568 // Correct behavior: v24m2 is marked only if v24 and v25 are marked.
1569 const MachineRegisterInfo &MRI = MF.getRegInfo();
1570 const MCPhysReg *CSRegs = MRI.getCalleeSavedRegs();
1572 for (unsigned i = 0; CSRegs[i]; ++i) {
1573 unsigned CSReg = CSRegs[i];
1574 // Only vector registers need special care.
1575 if (!RISCV::VRRegClass.contains(getRVVBaseRegister(TRI, CSReg)))
1576 continue;
1577
1578 SavedRegs.reset(CSReg);
1579
1580 auto SubRegs = TRI.subregs(CSReg);
1581 // Set the register and all its subregisters.
1582 if (!MRI.def_empty(CSReg) || MRI.getUsedPhysRegsMask().test(CSReg)) {
1583 SavedRegs.set(CSReg);
1584 for (unsigned Reg : SubRegs)
1585 SavedRegs.set(Reg);
1586 }
1587
1588 // Combine to super register if all of its subregisters are marked.
1589 if (!SubRegs.empty() && llvm::all_of(SubRegs, [&](unsigned Reg) {
1590 return SavedRegs.test(Reg);
1591 }))
1592 SavedRegs.set(CSReg);
1593 }
1594
1595 // Unconditionally spill RA and FP only if the function uses a frame
1596 // pointer.
1597 if (hasFP(MF)) {
1598 SavedRegs.set(RAReg);
1599 SavedRegs.set(FPReg);
1600 }
1601 // Mark BP as used if function has dedicated base pointer.
1602 if (hasBP(MF))
1603 SavedRegs.set(RISCVABI::getBPReg());
1604
1605 // When using cm.push/pop we must save X27 if we save X26.
1606 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
1607 if (RVFI->isPushable(MF) && SavedRegs.test(RISCV::X26))
1608 SavedRegs.set(RISCV::X27);
1609
1610 // SiFive Preemptible Interrupt Handlers need additional frame entries
1612}
1613
1614std::pair<int64_t, Align>
1615RISCVFrameLowering::assignRVVStackObjectOffsets(MachineFunction &MF) const {
1616 MachineFrameInfo &MFI = MF.getFrameInfo();
1617 // Create a buffer of RVV objects to allocate.
1618 SmallVector<int, 8> ObjectsToAllocate;
1619 auto pushRVVObjects = [&](int FIBegin, int FIEnd) {
1620 for (int I = FIBegin, E = FIEnd; I != E; ++I) {
1621 unsigned StackID = MFI.getStackID(I);
1622 if (StackID != TargetStackID::ScalableVector)
1623 continue;
1624 if (MFI.isDeadObjectIndex(I))
1625 continue;
1626
1627 ObjectsToAllocate.push_back(I);
1628 }
1629 };
1630 // First push RVV Callee Saved object, then push RVV stack object
1631 std::vector<CalleeSavedInfo> &CSI = MF.getFrameInfo().getCalleeSavedInfo();
1632 const auto &RVVCSI = getRVVCalleeSavedInfo(MF, CSI);
1633 if (!RVVCSI.empty())
1634 pushRVVObjects(RVVCSI[0].getFrameIdx(),
1635 RVVCSI[RVVCSI.size() - 1].getFrameIdx() + 1);
1636 pushRVVObjects(0, MFI.getObjectIndexEnd() - RVVCSI.size());
1637
1638 // The minimum alignment is 16 bytes.
1639 Align RVVStackAlign(16);
1640 const auto &ST = MF.getSubtarget<RISCVSubtarget>();
1641
1642 if (!ST.hasVInstructions()) {
1643 assert(ObjectsToAllocate.empty() &&
1644 "Can't allocate scalable-vector objects without V instructions");
1645 return std::make_pair(0, RVVStackAlign);
1646 }
1647
1648 // Allocate all RVV locals and spills
1649 int64_t Offset = 0;
1650 for (int FI : ObjectsToAllocate) {
1651 // ObjectSize in bytes.
1652 int64_t ObjectSize = MFI.getObjectSize(FI);
1653 auto ObjectAlign =
1654 std::max(Align(RISCV::RVVBytesPerBlock), MFI.getObjectAlign(FI));
1655 // If the data type is the fractional vector type, reserve one vector
1656 // register for it.
1657 if (ObjectSize < RISCV::RVVBytesPerBlock)
1658 ObjectSize = RISCV::RVVBytesPerBlock;
1659 Offset = alignTo(Offset + ObjectSize, ObjectAlign);
1660 MFI.setObjectOffset(FI, -Offset);
1661 // Update the maximum alignment of the RVV stack section
1662 RVVStackAlign = std::max(RVVStackAlign, ObjectAlign);
1663 }
1664
1665 uint64_t StackSize = Offset;
1666
1667 // Ensure the alignment of the RVV stack. Since we want the most-aligned
1668 // object right at the bottom (i.e., any padding at the top of the frame),
1669 // readjust all RVV objects down by the alignment padding.
1670 // Stack size and offsets are multiples of vscale, stack alignment is in
1671 // bytes, we can divide stack alignment by minimum vscale to get a maximum
1672 // stack alignment multiple of vscale.
1673 auto VScale =
1674 std::max<uint64_t>(ST.getRealMinVLen() / RISCV::RVVBitsPerBlock, 1);
1675 if (auto RVVStackAlignVScale = RVVStackAlign.value() / VScale) {
1676 if (auto AlignmentPadding =
1677 offsetToAlignment(StackSize, Align(RVVStackAlignVScale))) {
1678 StackSize += AlignmentPadding;
1679 for (int FI : ObjectsToAllocate)
1680 MFI.setObjectOffset(FI, MFI.getObjectOffset(FI) - AlignmentPadding);
1681 }
1682 }
1683
1684 return std::make_pair(StackSize, RVVStackAlign);
1685}
1686
1688 // For RVV spill, scalable stack offsets computing requires up to two scratch
1689 // registers
1690 static constexpr unsigned ScavSlotsNumRVVSpillScalableObject = 2;
1691
1692 // For RVV spill, non-scalable stack offsets computing requires up to one
1693 // scratch register.
1694 static constexpr unsigned ScavSlotsNumRVVSpillNonScalableObject = 1;
1695
1696 // ADDI instruction's destination register can be used for computing
1697 // offsets. So Scalable stack offsets require up to one scratch register.
1698 static constexpr unsigned ScavSlotsADDIScalableObject = 1;
1699
1700 static constexpr unsigned MaxScavSlotsNumKnown =
1701 std::max({ScavSlotsADDIScalableObject, ScavSlotsNumRVVSpillScalableObject,
1702 ScavSlotsNumRVVSpillNonScalableObject});
1703
1704 unsigned MaxScavSlotsNum = 0;
1706 return false;
1707 for (const MachineBasicBlock &MBB : MF)
1708 for (const MachineInstr &MI : MBB) {
1709 bool IsRVVSpill = RISCV::isRVVSpill(MI);
1710 for (auto &MO : MI.operands()) {
1711 if (!MO.isFI())
1712 continue;
1713 bool IsScalableVectorID = MF.getFrameInfo().getStackID(MO.getIndex()) ==
1715 if (IsRVVSpill) {
1716 MaxScavSlotsNum = std::max(
1717 MaxScavSlotsNum, IsScalableVectorID
1718 ? ScavSlotsNumRVVSpillScalableObject
1719 : ScavSlotsNumRVVSpillNonScalableObject);
1720 } else if (MI.getOpcode() == RISCV::ADDI && IsScalableVectorID) {
1721 MaxScavSlotsNum =
1722 std::max(MaxScavSlotsNum, ScavSlotsADDIScalableObject);
1723 }
1724 }
1725 if (MaxScavSlotsNum == MaxScavSlotsNumKnown)
1726 return MaxScavSlotsNumKnown;
1727 }
1728 return MaxScavSlotsNum;
1729}
1730
1731static bool hasRVVFrameObject(const MachineFunction &MF) {
1732 // Originally, the function will scan all the stack objects to check whether
1733 // if there is any scalable vector object on the stack or not. However, it
1734 // causes errors in the register allocator. In issue 53016, it returns false
1735 // before RA because there is no RVV stack objects. After RA, it returns true
1736 // because there are spilling slots for RVV values during RA. It will not
1737 // reserve BP during register allocation and generate BP access in the PEI
1738 // pass due to the inconsistent behavior of the function.
1739 //
1740 // The function is changed to use hasVInstructions() as the return value. It
1741 // is not precise, but it can make the register allocation correct.
1742 //
1743 // FIXME: Find a better way to make the decision or revisit the solution in
1744 // D103622.
1745 //
1746 // Refer to https://github.com/llvm/llvm-project/issues/53016.
1747 return MF.getSubtarget<RISCVSubtarget>().hasVInstructions();
1748}
1749
1751 const RISCVInstrInfo &TII) {
1752 unsigned FnSize = 0;
1753 for (auto &MBB : MF) {
1754 for (auto &MI : MBB) {
1755 // Far branches over 20-bit offset will be relaxed in branch relaxation
1756 // pass. In the worst case, conditional branches will be relaxed into
1757 // the following instruction sequence. Unconditional branches are
1758 // relaxed in the same way, with the exception that there is no first
1759 // branch instruction.
1760 //
1761 // foo
1762 // bne t5, t6, .rev_cond # `TII->getInstSizeInBytes(MI)` bytes
1763 // sd s11, 0(sp) # 4 bytes, or 2 bytes with Zca
1764 // jump .restore, s11 # 8 bytes
1765 // .rev_cond
1766 // bar
1767 // j .dest_bb # 4 bytes, or 2 bytes with Zca
1768 // .restore:
1769 // ld s11, 0(sp) # 4 bytes, or 2 bytes with Zca
1770 // .dest:
1771 // baz
1772 if (MI.isConditionalBranch())
1773 FnSize += TII.getInstSizeInBytes(MI);
1774 if (MI.isConditionalBranch() || MI.isUnconditionalBranch()) {
1775 if (MF.getSubtarget<RISCVSubtarget>().hasStdExtZca())
1776 FnSize += 2 + 8 + 2 + 2;
1777 else
1778 FnSize += 4 + 8 + 4 + 4;
1779 continue;
1780 }
1781
1782 FnSize += TII.getInstSizeInBytes(MI);
1783 }
1784 }
1785 return FnSize;
1786}
1787
1789 MachineFunction &MF, RegScavenger *RS) const {
1790 const RISCVRegisterInfo *RegInfo =
1791 MF.getSubtarget<RISCVSubtarget>().getRegisterInfo();
1792 const RISCVInstrInfo *TII = MF.getSubtarget<RISCVSubtarget>().getInstrInfo();
1793 MachineFrameInfo &MFI = MF.getFrameInfo();
1794 const TargetRegisterClass *RC = &RISCV::GPRRegClass;
1795 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
1796
1797 int64_t RVVStackSize;
1798 Align RVVStackAlign;
1799 std::tie(RVVStackSize, RVVStackAlign) = assignRVVStackObjectOffsets(MF);
1800
1801 RVFI->setRVVStackSize(RVVStackSize);
1802 RVFI->setRVVStackAlign(RVVStackAlign);
1803
1804 if (hasRVVFrameObject(MF)) {
1805 // Ensure the entire stack is aligned to at least the RVV requirement: some
1806 // scalable-vector object alignments are not considered by the
1807 // target-independent code.
1808 MFI.ensureMaxAlignment(RVVStackAlign);
1809 }
1810
1811 unsigned ScavSlotsNum = 0;
1812
1813 // estimateStackSize has been observed to under-estimate the final stack
1814 // size, so give ourselves wiggle-room by checking for stack size
1815 // representable an 11-bit signed field rather than 12-bits.
1816 if (!isInt<11>(MFI.estimateStackSize(MF)))
1817 ScavSlotsNum = 1;
1818
1819 // Far branches over 20-bit offset require a spill slot for scratch register.
1820 bool IsLargeFunction = !isInt<20>(estimateFunctionSizeInBytes(MF, *TII));
1821 if (IsLargeFunction)
1822 ScavSlotsNum = std::max(ScavSlotsNum, 1u);
1823
1824 // RVV loads & stores have no capacity to hold the immediate address offsets
1825 // so we must always reserve an emergency spill slot if the MachineFunction
1826 // contains any RVV spills.
1827 ScavSlotsNum = std::max(ScavSlotsNum, getScavSlotsNumForRVV(MF));
1828
1829 for (unsigned I = 0; I < ScavSlotsNum; I++) {
1830 int FI = MFI.CreateSpillStackObject(RegInfo->getSpillSize(*RC),
1831 RegInfo->getSpillAlign(*RC));
1833
1834 if (IsLargeFunction && RVFI->getBranchRelaxationScratchFrameIndex() == -1)
1835 RVFI->setBranchRelaxationScratchFrameIndex(FI);
1836 }
1837
1838 unsigned Size = RVFI->getReservedSpillsSize();
1839 for (const auto &Info : MFI.getCalleeSavedInfo()) {
1840 int FrameIdx = Info.getFrameIdx();
1841 if (FrameIdx < 0 || MFI.getStackID(FrameIdx) != TargetStackID::Default)
1842 continue;
1843
1844 Size += MFI.getObjectSize(FrameIdx);
1845 }
1846 RVFI->setCalleeSavedStackSize(Size);
1847}
1848
1849// Not preserve stack space within prologue for outgoing variables when the
1850// function contains variable size objects or there are vector objects accessed
1851// by the frame pointer.
1852// Let eliminateCallFramePseudoInstr preserve stack space for it.
1854 return !MF.getFrameInfo().hasVarSizedObjects() &&
1855 !(hasFP(MF) && hasRVVFrameObject(MF));
1856}
1857
1858// Eliminate ADJCALLSTACKDOWN, ADJCALLSTACKUP pseudo instructions.
1862 DebugLoc DL = MI->getDebugLoc();
1863
1864 if (!hasReservedCallFrame(MF)) {
1865 // If space has not been reserved for a call frame, ADJCALLSTACKDOWN and
1866 // ADJCALLSTACKUP must be converted to instructions manipulating the stack
1867 // pointer. This is necessary when there is a variable length stack
1868 // allocation (e.g. alloca), which means it's not possible to allocate
1869 // space for outgoing arguments from within the function prologue.
1870 int64_t Amount = MI->getOperand(0).getImm();
1871
1872 if (Amount != 0) {
1873 // Ensure the stack remains aligned after adjustment.
1874 Amount = alignSPAdjust(Amount);
1875
1876 if (MI->getOpcode() == RISCV::ADJCALLSTACKDOWN)
1877 Amount = -Amount;
1878
1879 const RISCVTargetLowering *TLI =
1880 MF.getSubtarget<RISCVSubtarget>().getTargetLowering();
1881 int64_t ProbeSize = TLI->getStackProbeSize(MF, getStackAlign());
1882 if (TLI->hasInlineStackProbe(MF) && -Amount >= ProbeSize) {
1883 // When stack probing is enabled, the decrement of SP may need to be
1884 // probed. We can handle both the decrement and the probing in
1885 // allocateStack.
1886 bool DynAllocation =
1887 MF.getInfo<RISCVMachineFunctionInfo>()->hasDynamicAllocation();
1888 allocateStack(MBB, MI, MF, -Amount, -Amount,
1889 needsDwarfCFI(MF) && !hasFP(MF),
1890 /*NeedProbe=*/true, ProbeSize, DynAllocation,
1892 } else {
1893 const RISCVRegisterInfo &RI = *STI.getRegisterInfo();
1896 }
1897 }
1898 }
1899
1900 return MBB.erase(MI);
1901}
1902
1903// We would like to split the SP adjustment to reduce prologue/epilogue
1904// as following instructions. In this way, the offset of the callee saved
1905// register could fit in a single store. Supposed that the first sp adjust
1906// amount is 2032.
1907// add sp,sp,-2032
1908// sw ra,2028(sp)
1909// sw s0,2024(sp)
1910// sw s1,2020(sp)
1911// sw s3,2012(sp)
1912// sw s4,2008(sp)
1913// add sp,sp,-64
1916 const auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
1917 const MachineFrameInfo &MFI = MF.getFrameInfo();
1918 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
1919 uint64_t StackSize = getStackSizeWithRVVPadding(MF);
1920
1921 // Disable SplitSPAdjust if save-restore libcall, push/pop or QCI interrupts
1922 // are used. The callee-saved registers will be pushed by the save-restore
1923 // libcalls, so we don't have to split the SP adjustment in this case.
1924 if (RVFI->getReservedSpillsSize())
1925 return 0;
1926
1927 // Return the FirstSPAdjustAmount if the StackSize can not fit in a signed
1928 // 12-bit and there exists a callee-saved register needing to be pushed.
1929 if (!isInt<12>(StackSize) && (CSI.size() > 0)) {
1930 // FirstSPAdjustAmount is chosen at most as (2048 - StackAlign) because
1931 // 2048 will cause sp = sp + 2048 in the epilogue to be split into multiple
1932 // instructions. Offsets smaller than 2048 can fit in a single load/store
1933 // instruction, and we have to stick with the stack alignment. 2048 has
1934 // 16-byte alignment. The stack alignment for RV32 and RV64 is 16 and for
1935 // RV32E it is 4. So (2048 - StackAlign) will satisfy the stack alignment.
1936 const uint64_t StackAlign = getStackAlign().value();
1937
1938 // Amount of (2048 - StackAlign) will prevent callee saved and restored
1939 // instructions be compressed, so try to adjust the amount to the largest
1940 // offset that stack compression instructions accept when target supports
1941 // compression instructions.
1942 if (STI.hasStdExtZca()) {
1943 // The compression extensions may support the following instructions:
1944 // riscv32: c.lwsp rd, offset[7:2] => 2^(6 + 2)
1945 // c.swsp rs2, offset[7:2] => 2^(6 + 2)
1946 // c.flwsp rd, offset[7:2] => 2^(6 + 2)
1947 // c.fswsp rs2, offset[7:2] => 2^(6 + 2)
1948 // riscv64: c.ldsp rd, offset[8:3] => 2^(6 + 3)
1949 // c.sdsp rs2, offset[8:3] => 2^(6 + 3)
1950 // c.fldsp rd, offset[8:3] => 2^(6 + 3)
1951 // c.fsdsp rs2, offset[8:3] => 2^(6 + 3)
1952 const uint64_t RVCompressLen = STI.getXLen() * 8;
1953 // Compared with amount (2048 - StackAlign), StackSize needs to
1954 // satisfy the following conditions to avoid using more instructions
1955 // to adjust the sp after adjusting the amount, such as
1956 // StackSize meets the condition (StackSize <= 2048 + RVCompressLen),
1957 // case1: Amount is 2048 - StackAlign: use addi + addi to adjust sp.
1958 // case2: Amount is RVCompressLen: use addi + addi to adjust sp.
1959 auto CanCompress = [&](uint64_t CompressLen) -> bool {
1960 if (StackSize <= 2047 + CompressLen ||
1961 (StackSize > 2048 * 2 - StackAlign &&
1962 StackSize <= 2047 * 2 + CompressLen) ||
1963 StackSize > 2048 * 3 - StackAlign)
1964 return true;
1965
1966 return false;
1967 };
1968 // In the epilogue, addi sp, sp, 496 is used to recover the sp and it
1969 // can be compressed(C.ADDI16SP, offset can be [-512, 496]), but
1970 // addi sp, sp, 512 can not be compressed. So try to use 496 first.
1971 const uint64_t ADDI16SPCompressLen = 496;
1972 if (STI.is64Bit() && CanCompress(ADDI16SPCompressLen))
1973 return ADDI16SPCompressLen;
1974 if (CanCompress(RVCompressLen))
1975 return RVCompressLen;
1976 }
1977 return 2048 - StackAlign;
1978 }
1979 return 0;
1980}
1981
1984 std::vector<CalleeSavedInfo> &CSI, unsigned &MinCSFrameIndex,
1985 unsigned &MaxCSFrameIndex) const {
1986 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
1987
1988 // Preemptible Interrupts have two additional Callee-save Frame Indexes,
1989 // not tracked by `CSI`.
1990 if (RVFI->isSiFivePreemptibleInterrupt(MF)) {
1991 for (int I = 0; I < 2; ++I) {
1992 int FI = RVFI->getInterruptCSRFrameIndex(I);
1993 MinCSFrameIndex = std::min<unsigned>(MinCSFrameIndex, FI);
1994 MaxCSFrameIndex = std::max<unsigned>(MaxCSFrameIndex, FI);
1995 }
1996 }
1997
1998 // Early exit if no callee saved registers are modified!
1999 if (CSI.empty())
2000 return true;
2001
2002 if (RVFI->useQCIInterrupt(MF)) {
2003 RVFI->setQCIInterruptStackSize(QCIInterruptPushAmount);
2004 }
2005
2006 if (RVFI->isPushable(MF)) {
2007 // Determine how many GPRs we need to push and save it to RVFI.
2008 unsigned PushedRegNum = getNumPushPopRegs(CSI);
2009
2010 // `QC.C.MIENTER(.NEST)` will save `ra` and `s0`, so we should only push if
2011 // we want to push more than 2 registers. Otherwise, we should push if we
2012 // want to push more than 0 registers.
2013 unsigned OnlyPushIfMoreThan = RVFI->useQCIInterrupt(MF) ? 2 : 0;
2014 if (PushedRegNum > OnlyPushIfMoreThan) {
2015 RVFI->setRVPushRegs(PushedRegNum);
2016 RVFI->setRVPushStackSize(alignTo((STI.getXLen() / 8) * PushedRegNum, 16));
2017 }
2018 }
2019
2020 MachineFrameInfo &MFI = MF.getFrameInfo();
2021 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
2022
2023 for (auto &CS : CSI) {
2024 MCRegister Reg = CS.getReg();
2025 const TargetRegisterClass *RC = RegInfo->getMinimalPhysRegClass(Reg);
2026 unsigned Size = RegInfo->getSpillSize(*RC);
2027
2028 if (RVFI->useQCIInterrupt(MF)) {
2029 const auto *FFI = llvm::find_if(FixedCSRFIQCIInterruptMap, [&](auto P) {
2030 return P.first == CS.getReg();
2031 });
2032 if (FFI != std::end(FixedCSRFIQCIInterruptMap)) {
2033 int64_t Offset = FFI->second * (int64_t)Size;
2034
2035 int FrameIdx = MFI.CreateFixedSpillStackObject(Size, Offset);
2036 assert(FrameIdx < 0);
2037 CS.setFrameIdx(FrameIdx);
2038 continue;
2039 }
2040 }
2041
2042 if (RVFI->useSaveRestoreLibCalls(MF) || RVFI->isPushable(MF)) {
2043 const auto *FII = llvm::find_if(
2044 FixedCSRFIMap, [&](MCPhysReg P) { return P == CS.getReg(); });
2045 unsigned RegNum = std::distance(std::begin(FixedCSRFIMap), FII);
2046
2047 if (FII != std::end(FixedCSRFIMap)) {
2048 int64_t Offset;
2049 if (RVFI->getPushPopKind(MF) ==
2051 Offset = -int64_t(RVFI->getRVPushRegs() - RegNum) * Size;
2052 else
2053 Offset = -int64_t(RegNum + 1) * Size;
2054
2055 if (RVFI->useQCIInterrupt(MF))
2057
2058 int FrameIdx = MFI.CreateFixedSpillStackObject(Size, Offset);
2059 assert(FrameIdx < 0);
2060 CS.setFrameIdx(FrameIdx);
2061 continue;
2062 }
2063 }
2064
2065 // Not a fixed slot.
2066 Align Alignment = RegInfo->getSpillAlign(*RC);
2067 // We may not be able to satisfy the desired alignment specification of
2068 // the TargetRegisterClass if the stack alignment is smaller. Use the
2069 // min.
2070 Alignment = std::min(Alignment, getStackAlign());
2071 int FrameIdx = MFI.CreateStackObject(Size, Alignment, true);
2072 if ((unsigned)FrameIdx < MinCSFrameIndex)
2073 MinCSFrameIndex = FrameIdx;
2074 if ((unsigned)FrameIdx > MaxCSFrameIndex)
2075 MaxCSFrameIndex = FrameIdx;
2076 CS.setFrameIdx(FrameIdx);
2079 }
2080
2081 if (RVFI->useQCIInterrupt(MF)) {
2082 // Allocate a fixed object that covers the entire QCI stack allocation,
2083 // because there are gaps which are reserved for future use.
2085 QCIInterruptPushAmount, -static_cast<int64_t>(QCIInterruptPushAmount));
2086 }
2087
2088 if (RVFI->isPushable(MF)) {
2089 int64_t QCIOffset = RVFI->useQCIInterrupt(MF) ? QCIInterruptPushAmount : 0;
2090 // Allocate a fixed object that covers the full push.
2091 if (int64_t PushSize = RVFI->getRVPushStackSize())
2092 MFI.CreateFixedSpillStackObject(PushSize, -PushSize - QCIOffset);
2093 } else if (int LibCallRegs = getLibCallID(MF, CSI) + 1) {
2094 int64_t LibCallFrameSize =
2095 alignTo((STI.getXLen() / 8) * LibCallRegs, getStackAlign());
2096 MFI.CreateFixedSpillStackObject(LibCallFrameSize, -LibCallFrameSize);
2097 }
2098
2099 return true;
2100}
2101
2105 if (CSI.empty())
2106 return true;
2107
2109 const TargetInstrInfo &TII = *MF->getSubtarget().getInstrInfo();
2110 DebugLoc DL;
2111 if (MI != MBB.end() && !MI->isDebugInstr())
2112 DL = MI->getDebugLoc();
2113
2115 if (RVFI->useQCIInterrupt(*MF)) {
2116 // Emit QC.C.MIENTER(.NEST)
2117 BuildMI(
2118 MBB, MI, DL,
2119 TII.get(RVFI->getInterruptStackKind(*MF) ==
2121 ? RISCV::QC_C_MIENTER_NEST
2122 : RISCV::QC_C_MIENTER))
2124
2125 for (auto [Reg, _Offset] : FixedCSRFIQCIInterruptMap)
2126 MBB.addLiveIn(Reg);
2127 }
2128
2129 if (RVFI->isPushable(*MF)) {
2130 // Emit CM.PUSH with base StackAdj & evaluate Push stack
2131 unsigned PushedRegNum = RVFI->getRVPushRegs();
2132 if (PushedRegNum > 0) {
2133 // Use encoded number to represent registers to spill.
2134 unsigned Opcode = getPushOpcode(
2135 RVFI->getPushPopKind(*MF), hasFP(*MF) && !RVFI->useQCIInterrupt(*MF));
2136 unsigned RegEnc = RISCVZC::encodeRegListNumRegs(PushedRegNum);
2137 MachineInstrBuilder PushBuilder =
2138 BuildMI(MBB, MI, DL, TII.get(Opcode))
2140 PushBuilder.addImm(RegEnc);
2141 PushBuilder.addImm(0);
2142
2143 for (unsigned i = 0; i < PushedRegNum; i++)
2144 PushBuilder.addUse(FixedCSRFIMap[i], RegState::Implicit);
2145 }
2146 } else if (const char *SpillLibCall = getSpillLibCallName(*MF, CSI)) {
2147 // Add spill libcall via non-callee-saved register t0.
2148 BuildMI(MBB, MI, DL, TII.get(RISCV::PseudoCALLReg), RISCV::X5)
2149 .addExternalSymbol(SpillLibCall, RISCVII::MO_CALL)
2151
2152 // Add registers spilled in libcall as liveins.
2153 for (auto &CS : CSI)
2154 MBB.addLiveIn(CS.getReg());
2155 }
2156
2157 // Manually spill values not spilled by libcall & Push/Pop.
2158 const auto &UnmanagedCSI = getUnmanagedCSI(*MF, CSI);
2159 const auto &RVVCSI = getRVVCalleeSavedInfo(*MF, CSI);
2160
2161 auto storeRegsToStackSlots = [&](decltype(UnmanagedCSI) CSInfo) {
2162 for (auto &CS : CSInfo) {
2163 // Insert the spill to the stack frame.
2164 MCRegister Reg = CS.getReg();
2165 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
2167 CS.getFrameIdx(), RC, TRI, Register(),
2169 }
2170 };
2171 storeRegsToStackSlots(UnmanagedCSI);
2172 storeRegsToStackSlots(RVVCSI);
2173
2174 return true;
2175}
2176
2177static unsigned getCalleeSavedRVVNumRegs(const Register &BaseReg) {
2178 return RISCV::VRRegClass.contains(BaseReg) ? 1
2179 : RISCV::VRM2RegClass.contains(BaseReg) ? 2
2180 : RISCV::VRM4RegClass.contains(BaseReg) ? 4
2181 : 8;
2182}
2183
2184void RISCVFrameLowering::emitCalleeSavedRVVPrologCFI(
2187 const MachineFrameInfo &MFI = MF->getFrameInfo();
2190
2191 const auto &RVVCSI = getRVVCalleeSavedInfo(*MF, MFI.getCalleeSavedInfo());
2192 if (RVVCSI.empty())
2193 return;
2194
2195 uint64_t FixedSize = getStackSizeWithRVVPadding(*MF);
2196 if (!HasFP) {
2197 uint64_t ScalarLocalVarSize =
2198 MFI.getStackSize() - RVFI->getCalleeSavedStackSize() -
2199 RVFI->getVarArgsSaveSize() + RVFI->getRVVPadding();
2200 FixedSize -= ScalarLocalVarSize;
2201 }
2202
2204 for (auto &CS : RVVCSI) {
2205 // Insert the spill to the stack frame.
2206 int FI = CS.getFrameIdx();
2207 MCRegister BaseReg = getRVVBaseRegister(TRI, CS.getReg());
2208 unsigned NumRegs = getCalleeSavedRVVNumRegs(CS.getReg());
2209 for (unsigned i = 0; i < NumRegs; ++i) {
2210 CFIBuilder.insertCFIInst(createDefCFAOffset(
2211 TRI, BaseReg + i, -FixedSize, MFI.getObjectOffset(FI) / 8 + i));
2212 }
2213 }
2214}
2215
2216void RISCVFrameLowering::emitCalleeSavedRVVEpilogCFI(
2219 const MachineFrameInfo &MFI = MF->getFrameInfo();
2221
2223 const auto &RVVCSI = getRVVCalleeSavedInfo(*MF, MFI.getCalleeSavedInfo());
2224 for (auto &CS : RVVCSI) {
2225 MCRegister BaseReg = getRVVBaseRegister(TRI, CS.getReg());
2226 unsigned NumRegs = getCalleeSavedRVVNumRegs(CS.getReg());
2227 for (unsigned i = 0; i < NumRegs; ++i)
2228 CFIHelper.buildRestore(BaseReg + i);
2229 }
2230}
2231
2235 if (CSI.empty())
2236 return true;
2237
2239 const TargetInstrInfo &TII = *MF->getSubtarget().getInstrInfo();
2240 DebugLoc DL;
2241 if (MI != MBB.end() && !MI->isDebugInstr())
2242 DL = MI->getDebugLoc();
2243
2244 // Manually restore values not restored by libcall & Push/Pop.
2245 // Reverse the restore order in epilog. In addition, the return
2246 // address will be restored first in the epilogue. It increases
2247 // the opportunity to avoid the load-to-use data hazard between
2248 // loading RA and return by RA. loadRegFromStackSlot can insert
2249 // multiple instructions.
2250 const auto &UnmanagedCSI = getUnmanagedCSI(*MF, CSI);
2251 const auto &RVVCSI = getRVVCalleeSavedInfo(*MF, CSI);
2252
2253 auto loadRegFromStackSlot = [&](decltype(UnmanagedCSI) CSInfo) {
2254 for (auto &CS : CSInfo) {
2255 MCRegister Reg = CS.getReg();
2256 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
2257 TII.loadRegFromStackSlot(MBB, MI, Reg, CS.getFrameIdx(), RC, TRI,
2259 assert(MI != MBB.begin() &&
2260 "loadRegFromStackSlot didn't insert any code!");
2261 }
2262 };
2263 loadRegFromStackSlot(RVVCSI);
2264 loadRegFromStackSlot(UnmanagedCSI);
2265
2267 if (RVFI->useQCIInterrupt(*MF)) {
2268 // Don't emit anything here because restoration is handled by
2269 // QC.C.MILEAVERET which we already inserted to return.
2270 assert(MI->getOpcode() == RISCV::QC_C_MILEAVERET &&
2271 "Unexpected QCI Interrupt Return Instruction");
2272 }
2273
2274 if (RVFI->isPushable(*MF)) {
2275 unsigned PushedRegNum = RVFI->getRVPushRegs();
2276 if (PushedRegNum > 0) {
2277 unsigned Opcode = getPopOpcode(RVFI->getPushPopKind(*MF));
2278 unsigned RegEnc = RISCVZC::encodeRegListNumRegs(PushedRegNum);
2279 MachineInstrBuilder PopBuilder =
2280 BuildMI(MBB, MI, DL, TII.get(Opcode))
2282 // Use encoded number to represent registers to restore.
2283 PopBuilder.addImm(RegEnc);
2284 PopBuilder.addImm(0);
2285
2286 for (unsigned i = 0; i < RVFI->getRVPushRegs(); i++)
2288 }
2289 } else {
2290 const char *RestoreLibCall = getRestoreLibCallName(*MF, CSI);
2291 if (RestoreLibCall) {
2292 // Add restore libcall via tail call.
2294 BuildMI(MBB, MI, DL, TII.get(RISCV::PseudoTAIL))
2295 .addExternalSymbol(RestoreLibCall, RISCVII::MO_CALL)
2297
2298 // Remove trailing returns, since the terminator is now a tail call to the
2299 // restore function.
2300 if (MI != MBB.end() && MI->getOpcode() == RISCV::PseudoRET) {
2301 NewMI->copyImplicitOps(*MF, *MI);
2302 MI->eraseFromParent();
2303 }
2304 }
2305 }
2306 return true;
2307}
2308
2310 // Keep the conventional code flow when not optimizing.
2311 if (MF.getFunction().hasOptNone())
2312 return false;
2313
2314 return true;
2315}
2316
2318 MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB);
2319 const MachineFunction *MF = MBB.getParent();
2320 const auto *RVFI = MF->getInfo<RISCVMachineFunctionInfo>();
2321
2322 // Make sure VTYPE and VL are not live-in since we will use vsetvli in the
2323 // prologue to get the VLEN, and that will clobber these registers.
2324 //
2325 // We may do also check the stack contains objects with scalable vector type,
2326 // but this will require iterating over all the stack objects, but this may
2327 // not worth since the situation is rare, we could do further check in future
2328 // if we find it is necessary.
2329 if (STI.preferVsetvliOverReadVLENB() &&
2330 (MBB.isLiveIn(RISCV::VTYPE) || MBB.isLiveIn(RISCV::VL)))
2331 return false;
2332
2333 if (!RVFI->useSaveRestoreLibCalls(*MF))
2334 return true;
2335
2336 // Inserting a call to a __riscv_save libcall requires the use of the register
2337 // t0 (X5) to hold the return address. Therefore if this register is already
2338 // used we can't insert the call.
2339
2340 RegScavenger RS;
2341 RS.enterBasicBlock(*TmpMBB);
2342 return !RS.isRegUsed(RISCV::X5);
2343}
2344
2346 const MachineFunction *MF = MBB.getParent();
2347 MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB);
2348 const auto *RVFI = MF->getInfo<RISCVMachineFunctionInfo>();
2349
2350 // We do not want QC.C.MILEAVERET to be subject to shrink-wrapping - it must
2351 // come in the final block of its function as it both pops and returns.
2352 if (RVFI->useQCIInterrupt(*MF))
2353 return MBB.succ_empty();
2354
2355 if (!RVFI->useSaveRestoreLibCalls(*MF))
2356 return true;
2357
2358 // Using the __riscv_restore libcalls to restore CSRs requires a tail call.
2359 // This means if we still need to continue executing code within this function
2360 // the restore cannot take place in this basic block.
2361
2362 if (MBB.succ_size() > 1)
2363 return false;
2364
2365 MachineBasicBlock *SuccMBB =
2366 MBB.succ_empty() ? TmpMBB->getFallThrough() : *MBB.succ_begin();
2367
2368 // Doing a tail call should be safe if there are no successors, because either
2369 // we have a returning block or the end of the block is unreachable, so the
2370 // restore will be eliminated regardless.
2371 if (!SuccMBB)
2372 return true;
2373
2374 // The successor can only contain a return, since we would effectively be
2375 // replacing the successor with our own tail return at the end of our block.
2376 return SuccMBB->isReturnBlock() && SuccMBB->size() == 1;
2377}
2378
2380 switch (ID) {
2383 return true;
2387 return false;
2388 }
2389 llvm_unreachable("Invalid TargetStackID::Value");
2390}
2391
2394}
2395
2396// Synthesize the probe loop.
2398 Register TargetReg, bool IsRVV) {
2399 assert(TargetReg != RISCV::X2 && "New top of stack cannot already be in SP");
2400
2402 MachineFunction &MF = *MBB.getParent();
2403
2404 auto &Subtarget = MF.getSubtarget<RISCVSubtarget>();
2405 const RISCVInstrInfo *TII = Subtarget.getInstrInfo();
2406 bool IsRV64 = Subtarget.is64Bit();
2407 Align StackAlign = Subtarget.getFrameLowering()->getStackAlign();
2408 const RISCVTargetLowering *TLI = Subtarget.getTargetLowering();
2409 uint64_t ProbeSize = TLI->getStackProbeSize(MF, StackAlign);
2410
2411 MachineFunction::iterator MBBInsertPoint = std::next(MBB.getIterator());
2412 MachineBasicBlock *LoopTestMBB =
2414 MF.insert(MBBInsertPoint, LoopTestMBB);
2416 MF.insert(MBBInsertPoint, ExitMBB);
2418 Register ScratchReg = RISCV::X7;
2419
2420 // ScratchReg = ProbeSize
2421 TII->movImm(MBB, MBBI, DL, ScratchReg, ProbeSize, Flags);
2422
2423 // LoopTest:
2424 // SUB SP, SP, ProbeSize
2425 BuildMI(*LoopTestMBB, LoopTestMBB->end(), DL, TII->get(RISCV::SUB), SPReg)
2426 .addReg(SPReg)
2427 .addReg(ScratchReg)
2428 .setMIFlags(Flags);
2429
2430 // s[d|w] zero, 0(sp)
2431 BuildMI(*LoopTestMBB, LoopTestMBB->end(), DL,
2432 TII->get(IsRV64 ? RISCV::SD : RISCV::SW))
2433 .addReg(RISCV::X0)
2434 .addReg(SPReg)
2435 .addImm(0)
2436 .setMIFlags(Flags);
2437
2438 if (IsRVV) {
2439 // SUB TargetReg, TargetReg, ProbeSize
2440 BuildMI(*LoopTestMBB, LoopTestMBB->end(), DL, TII->get(RISCV::SUB),
2441 TargetReg)
2442 .addReg(TargetReg)
2443 .addReg(ScratchReg)
2444 .setMIFlags(Flags);
2445
2446 // BGE TargetReg, ProbeSize, LoopTest
2447 BuildMI(*LoopTestMBB, LoopTestMBB->end(), DL, TII->get(RISCV::BGE))
2448 .addReg(TargetReg)
2449 .addReg(ScratchReg)
2450 .addMBB(LoopTestMBB)
2451 .setMIFlags(Flags);
2452
2453 } else {
2454 // BNE SP, TargetReg, LoopTest
2455 BuildMI(*LoopTestMBB, LoopTestMBB->end(), DL, TII->get(RISCV::BNE))
2456 .addReg(SPReg)
2457 .addReg(TargetReg)
2458 .addMBB(LoopTestMBB)
2459 .setMIFlags(Flags);
2460 }
2461
2462 ExitMBB->splice(ExitMBB->end(), &MBB, std::next(MBBI), MBB.end());
2464
2465 LoopTestMBB->addSuccessor(ExitMBB);
2466 LoopTestMBB->addSuccessor(LoopTestMBB);
2467 MBB.addSuccessor(LoopTestMBB);
2468 // Update liveins.
2469 fullyRecomputeLiveIns({ExitMBB, LoopTestMBB});
2470}
2471
2472void RISCVFrameLowering::inlineStackProbe(MachineFunction &MF,
2473 MachineBasicBlock &MBB) const {
2474 // Get the instructions that need to be replaced. We emit at most two of
2475 // these. Remember them in order to avoid complications coming from the need
2476 // to traverse the block while potentially creating more blocks.
2478 for (MachineInstr &MI : MBB) {
2479 unsigned Opc = MI.getOpcode();
2480 if (Opc == RISCV::PROBED_STACKALLOC ||
2481 Opc == RISCV::PROBED_STACKALLOC_RVV) {
2482 ToReplace.push_back(&MI);
2483 }
2484 }
2485
2486 for (MachineInstr *MI : ToReplace) {
2487 if (MI->getOpcode() == RISCV::PROBED_STACKALLOC ||
2488 MI->getOpcode() == RISCV::PROBED_STACKALLOC_RVV) {
2489 MachineBasicBlock::iterator MBBI = MI->getIterator();
2491 Register TargetReg = MI->getOperand(0).getReg();
2492 emitStackProbeInline(MBBI, DL, TargetReg,
2493 (MI->getOpcode() == RISCV::PROBED_STACKALLOC_RVV));
2495 }
2496 }
2497}
unsigned const MachineRegisterInfo * MRI
static MCCFIInstruction createDefCFAExpression(const TargetRegisterInfo &TRI, unsigned Reg, const StackOffset &Offset)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
This file contains constants used for implementing Dwarf debug support.
uint64_t Size
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
static uint64_t estimateFunctionSizeInBytes(const LoongArchInstrInfo *TII, const MachineFunction &MF)
#define I(x, y, z)
Definition: MD5.cpp:58
Register const TargetRegisterInfo * TRI
#define P(N)
static constexpr uint64_t QCIInterruptPushAmount
static unsigned getPushOpcode(RISCVMachineFunctionInfo::PushPopKind Kind, bool UpdateFP)
static void emitSiFiveCLICPreemptibleSaves(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, const DebugLoc &DL)
static MCRegister getRVVBaseRegister(const RISCVRegisterInfo &TRI, const Register &Reg)
static void createSiFivePreemptibleInterruptFrameEntries(MachineFunction &MF, RISCVMachineFunctionInfo &RVFI)
static constexpr MCPhysReg FPReg
static const char * getRestoreLibCallName(const MachineFunction &MF, const std::vector< CalleeSavedInfo > &CSI)
static bool needsDwarfCFI(const MachineFunction &MF)
Returns true if DWARF CFI instructions ("frame moves") should be emitted.
static constexpr MCPhysReg SPReg
static const char * getSpillLibCallName(const MachineFunction &MF, const std::vector< CalleeSavedInfo > &CSI)
static bool hasRVVFrameObject(const MachineFunction &MF)
static SmallVector< CalleeSavedInfo, 8 > getQCISavedInfo(const MachineFunction &MF, const std::vector< CalleeSavedInfo > &CSI)
static void emitSiFiveCLICPreemptibleRestores(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, const DebugLoc &DL)
static SmallVector< CalleeSavedInfo, 8 > getRVVCalleeSavedInfo(const MachineFunction &MF, const std::vector< CalleeSavedInfo > &CSI)
static void appendScalableVectorExpression(const TargetRegisterInfo &TRI, SmallVectorImpl< char > &Expr, int FixedOffset, int ScalableOffset, llvm::raw_string_ostream &Comment)
static bool isPop(unsigned Opcode)
static unsigned getCalleeSavedRVVNumRegs(const Register &BaseReg)
static void emitStackProbeInline(MachineBasicBlock::iterator MBBI, DebugLoc DL, Register TargetReg, bool IsRVV)
static Align getABIStackAlignment(RISCVABI::ABI ABI)
static unsigned getPopOpcode(RISCVMachineFunctionInfo::PushPopKind Kind)
static SmallVector< CalleeSavedInfo, 8 > getPushOrLibCallsSavedInfo(const MachineFunction &MF, const std::vector< CalleeSavedInfo > &CSI)
static int getLibCallID(const MachineFunction &MF, const std::vector< CalleeSavedInfo > &CSI)
static const std::pair< MCPhysReg, int8_t > FixedCSRFIQCIInterruptMap[]
static bool isPush(unsigned Opcode)
static constexpr MCPhysReg RAReg
static void emitSCSPrologue(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, const DebugLoc &DL)
static const MCPhysReg FixedCSRFIMap[]
static void emitSCSEpilogue(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, const DebugLoc &DL)
static SmallVector< CalleeSavedInfo, 8 > getUnmanagedCSI(const MachineFunction &MF, const std::vector< CalleeSavedInfo > &CSI)
static void emitSiFiveCLICStackSwap(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, const DebugLoc &DL)
static unsigned getNumPushPopRegs(const std::vector< CalleeSavedInfo > &CSI)
static unsigned getScavSlotsNumForRVV(MachineFunction &MF)
static MCCFIInstruction createDefCFAOffset(const TargetRegisterInfo &TRI, Register Reg, uint64_t FixedOffset, uint64_t ScalableOffset)
This file declares the machine register scavenger class.
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition: Value.cpp:480
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:142
bool test(unsigned Idx) const
Definition: BitVector.h:461
BitVector & reset()
Definition: BitVector.h:392
BitVector & set()
Definition: BitVector.h:351
Helper class for creating CFI instructions and inserting them into MIR.
void buildEscape(StringRef Bytes, StringRef Comment="") const
void buildDefCFAOffset(int64_t Offset, MCSymbol *Label=nullptr) const
void buildRestore(MCRegister Reg) const
void buildDefCFARegister(MCRegister Reg) const
void buildOffset(MCRegister Reg, int64_t Offset) const
void insertCFIInst(const MCCFIInstruction &CFIInst) const
void buildDefCFA(MCRegister Reg, int64_t Offset) const
void setInsertPoint(MachineBasicBlock::iterator IP)
The CalleeSavedInfo class tracks the information need to locate where a callee saved register is in t...
MCRegister getReg() const
A debug info location.
Definition: DebugLoc.h:124
Diagnostic information for unsupported feature in backend.
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition: Function.h:270
bool hasOptNone() const
Do not optimize this function (-O0).
Definition: Function.h:700
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition: Function.cpp:359
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.cpp:727
void storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, Register SrcReg, bool isKill, int FrameIndex, const TargetRegisterClass *RC, const TargetRegisterInfo *TRI, Register VReg, MachineInstr::MIFlag Flags=MachineInstr::NoFlags) const override
Store the specified register of the given register class to the specified stack frame index.
void loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, Register DestReg, int FrameIndex, const TargetRegisterClass *RC, const TargetRegisterInfo *TRI, Register VReg, MachineInstr::MIFlag Flags=MachineInstr::NoFlags) const override
Load the specified register of the given register class from the specified stack frame index.
LLVM_ABI void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
static MCCFIInstruction createEscape(MCSymbol *L, StringRef Vals, SMLoc Loc={}, StringRef Comment="")
.cfi_escape Allows the user to add arbitrary bytes to the unwind info.
Definition: MCDwarf.h:697
Wrapper class representing physical registers. Should be passed by value.
Definition: MCRegister.h:33
constexpr unsigned id() const
Definition: MCRegister.h:74
LLVM_ABI void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *FromMBB)
Transfers all the successors, as in transferSuccessors, and update PHI operands in the successor bloc...
LLVM_ABI MachineBasicBlock * getFallThrough(bool JumpToFallThrough=true)
Return the fallthrough block if the block can implicitly transfer control to the block after it by fa...
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
LLVM_ABI iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
unsigned succ_size() const
bool isReturnBlock() const
Convenience function that returns true if the block ends in a return instruction.
LLVM_ABI void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
LLVM_ABI DebugLoc findDebugLoc(instr_iterator MBBI)
Find the next valid DebugLoc starting at MBBI, skipping any debug instructions.
LLVM_ABI iterator getLastNonDebugInstr(bool SkipPseudoOp=true)
Returns an iterator to the last non-debug instruction in the basic block, or end().
LLVM_ABI void eraseFromParent()
This method unlinks 'this' from the containing function and deletes it.
void addLiveIn(MCRegister PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
LLVM_ABI instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
LLVM_ABI bool isLiveIn(MCRegister Reg, LaneBitmask LaneMask=LaneBitmask::getAll()) const
Return true if the specified register is in the live in set.
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
bool hasVarSizedObjects() const
This method may be called any time after instruction selection is complete to determine if the stack ...
uint64_t getStackSize() const
Return the number of bytes that must be allocated to hold all of the fixed size frame objects.
bool adjustsStack() const
Return true if this function adjusts the stack – e.g., when calling another function.
LLVM_ABI 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.
LLVM_ABI void ensureMaxAlignment(Align Alignment)
Make sure the function is at least Align bytes aligned.
bool isFrameAddressTaken() const
This method may be called any time after instruction selection is complete to determine if there is a...
Align getMaxAlign() const
Return the alignment in bytes that this function must be aligned to, which is greater than the defaul...
void setObjectOffset(int ObjectIdx, int64_t SPOffset)
Set the stack frame offset of the specified object.
uint64_t getMaxCallFrameSize() const
Return the maximum size of a call frame that must be allocated for an outgoing function call.
int64_t getOffsetAdjustment() const
Return the correction for frame offsets.
LLVM_ABI int CreateSpillStackObject(uint64_t Size, Align Alignment)
Create a new statically sized stack object that represents a spill slot, returning a nonnegative iden...
LLVM_ABI uint64_t estimateStackSize(const MachineFunction &MF) const
Estimate and return the size of the stack frame.
void setStackID(int ObjectIdx, uint8_t ID)
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
int64_t getObjectSize(int ObjectIdx) const
Return the size of the specified object.
bool isMaxCallFrameSizeComputed() const
const std::vector< CalleeSavedInfo > & getCalleeSavedInfo() const
Returns a reference to call saved info vector for the current function.
int getObjectIndexEnd() const
Return one past the maximum frame object index.
LLVM_ABI int CreateFixedSpillStackObject(uint64_t Size, int64_t SPOffset, bool IsImmutable=false)
Create a spill slot at a fixed location on the stack.
uint8_t getStackID(int ObjectIdx) const
int64_t getObjectOffset(int ObjectIdx) const
Return the assigned stack offset of the specified object from the incoming stack pointer.
void setStackSize(uint64_t Size)
Set the size of the stack.
bool isFixedObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a fixed stack object.
bool isDeadObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a dead object.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
bool needsFrameMoves() const
True if this function needs frame moves for debug or exceptions.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *BB=nullptr, std::optional< UniqueBBID > BBID=std::nullopt)
CreateMachineBasicBlock - Allocate a new MachineBasicBlock.
void insert(iterator MBBI, MachineBasicBlock *MBB)
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
const MachineInstrBuilder & addExternalSymbol(const char *FnName, unsigned TargetFlags=0) const
const MachineInstrBuilder & setMIFlag(MachineInstr::MIFlag Flag) const
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
const MachineInstrBuilder & addUse(Register RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register use operand.
const MachineInstrBuilder & setMIFlags(unsigned Flags) const
const MachineInstrBuilder & addDef(Register RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register definition operand.
Representation of each machine instruction.
Definition: MachineInstr.h:72
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
bool isReserved(MCRegister PhysReg) const
isReserved - Returns true when PhysReg is a reserved register.
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:303
void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override
emitProlog/emitEpilog - These methods insert prolog and epilog code into the function.
uint64_t getFirstSPAdjustAmount(const MachineFunction &MF) const
bool enableShrinkWrapping(const MachineFunction &MF) const override
Returns true if the target will correctly handle shrink wrapping.
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...
bool hasBP(const MachineFunction &MF) const
void allocateStack(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, MachineFunction &MF, uint64_t Offset, uint64_t RealStackSize, bool EmitCFI, bool NeedProbe, uint64_t ProbeSize, bool DynAllocation, MachineInstr::MIFlag Flag) const
bool canUseAsEpilogue(const MachineBasicBlock &MBB) const override
Check whether or not the given MBB can be used as a epilogue for the target.
bool assignCalleeSavedSpillSlots(MachineFunction &MF, const TargetRegisterInfo *TRI, std::vector< CalleeSavedInfo > &CSI, unsigned &MinCSFrameIndex, unsigned &MaxCSFrameIndex) const override
assignCalleeSavedSpillSlots - Allows target to override spill slot assignment logic.
bool hasFPImpl(const MachineFunction &MF) const override
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...
bool hasReservedCallFrame(const MachineFunction &MF) const override
hasReservedCallFrame - Under normal circumstances, when a frame pointer is not required,...
const RISCVSubtarget & STI
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...
bool isSupportedStackID(TargetStackID::Value ID) const override
void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS) const override
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override
TargetStackID::Value getStackIDForScalableVectors() const override
Returns the StackID that scalable vectors should be associated with.
void processFunctionBeforeFrameFinalized(MachineFunction &MF, RegScavenger *RS) const override
processFunctionBeforeFrameFinalized - This method is called immediately before the specified function...
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...
bool canUseAsPrologue(const MachineBasicBlock &MBB) const override
Check whether or not the given MBB can be used as a prologue for the target.
RISCVFrameLowering(const RISCVSubtarget &STI)
uint64_t getStackSizeWithRVVPadding(const MachineFunction &MF) const
RISCVMachineFunctionInfo - This class is derived from MachineFunctionInfo and contains private RISCV-...
bool isPushable(const MachineFunction &MF) const
InterruptStackKind getInterruptStackKind(const MachineFunction &MF) const
bool isSiFivePreemptibleInterrupt(const MachineFunction &MF) const
PushPopKind getPushPopKind(const MachineFunction &MF) const
bool useSaveRestoreLibCalls(const MachineFunction &MF) const
bool useQCIInterrupt(const MachineFunction &MF) const
int getInterruptCSRFrameIndex(size_t Idx) const
unsigned getXLen() const
bool hasVInstructions() const
bool isRegisterReservedByUser(Register i) const override
const RISCVRegisterInfo * getRegisterInfo() const override
const RISCVInstrInfo * getInstrInfo() const override
bool hasInlineStackProbe(const MachineFunction &MF) const override
True if stack clash protection is enabled for this functions.
unsigned getStackProbeSize(const MachineFunction &MF, Align StackAlign) const
bool isRegUsed(Register Reg, bool includeReserved=true) const
Return if a specific register is currently used.
void enterBasicBlock(MachineBasicBlock &MBB)
Start tracking liveness from the begin of basic block MBB.
void addScavengingFrameIndex(int FI)
Add a scavenging frame index.
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
Represents a location in source code.
Definition: SMLoc.h:23
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
void append(StringRef RHS)
Append from a StringRef.
Definition: SmallString.h:68
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:254
bool empty() const
Definition: SmallVector.h:82
size_t size() const
Definition: SmallVector.h:79
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:574
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:684
void push_back(const T &Elt)
Definition: SmallVector.h:414
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
StackOffset holds a fixed and a scalable offset in bytes.
Definition: TypeSize.h:34
int64_t getFixed() const
Returns the fixed component of the stack.
Definition: TypeSize.h:50
int64_t getScalable() const
Returns the scalable component of the stack.
Definition: TypeSize.h:53
static StackOffset get(int64_t Fixed, int64_t Scalable)
Definition: TypeSize.h:45
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
Information about stack frame layout on the target.
bool hasFP(const MachineFunction &MF) const
hasFP - Return true if the specified function should have a dedicated frame pointer register.
virtual void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS=nullptr) const
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
int getOffsetOfLocalArea() const
getOffsetOfLocalArea - This method returns the offset of the local area from the stack pointer on ent...
Align getStackAlign() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
int alignSPAdjust(int SPAdj) const
alignSPAdjust - This method aligns the stack adjustment to the correct alignment.
TargetInstrInfo - Interface to description of machine instruction set.
TargetOptions Options
LLVM_ABI bool DisableFramePointerElim(const MachineFunction &MF) const
DisableFramePointerElim - This returns true if frame pointer elimination optimization should be disab...
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
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...
Align getSpillAlign(const TargetRegisterClass &RC) const
Return the minimum required alignment in bytes for a spill slot for a register of this class.
bool hasStackRealignment(const MachineFunction &MF) const
True if stack realignment is required and still possible.
unsigned getSpillSize(const TargetRegisterClass &RC) const
Return the size in bytes of the stack slot allocated to hold a spilled copy of a register from class ...
virtual Register getFrameRegister(const MachineFunction &MF) const =0
Debug information queries.
virtual const TargetInstrInfo * getInstrInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
self_iterator getIterator()
Definition: ilist_node.h:134
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:662
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ GHC
Used by the Glasgow Haskell Compiler (GHC).
Definition: CallingConv.h:50
MCRegister getBPReg()
MCRegister getSCSPReg()
static unsigned encodeRegListNumRegs(unsigned NumRegs)
static constexpr unsigned RVVBitsPerBlock
bool isRVVSpill(const MachineInstr &MI)
static constexpr unsigned RVVBytesPerBlock
@ Implicit
Not emitted register (e.g. carry, or temporary result).
@ Define
Register definition.
@ Kill
The last use of a register.
BaseReg
Stack frame base register. Bit 0 of FREInfo.Info.
Definition: SFrame.h:77
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
IterT next_nodbg(IterT It, IterT End, bool SkipPseudoOp=true)
Increment It, then continue incrementing it while it points to a debug instruction.
@ Offset
Definition: DWP.cpp:477
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1744
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:1702
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
constexpr T alignDown(U Value, V Align, W Skew=0)
Returns the largest unsigned integer less than or equal to Value and is Skew mod Align.
Definition: MathExtras.h:551
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:1758
auto make_first_range(ContainerTy &&c)
Given a container of pairs, return a range over the first elements.
Definition: STLExtras.h:1444
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:197
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:155
unsigned encodeSLEB128(int64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a SLEB128 value to an output stream.
Definition: LEB128.h:24
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1777
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1916
unsigned encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a ULEB128 value to an output stream.
Definition: LEB128.h:81
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition: Alignment.h:208
void fullyRecomputeLiveIns(ArrayRef< MachineBasicBlock * > MBBs)
Convenience function for recomputing live-in's for a set of MBBs until the computation converges.
Definition: LivePhysRegs.h:225
LLVM_ABI Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
uint64_t value() const
This is a hole in the type system and should not be abused.
Definition: Alignment.h:85
static bool isRVVRegClass(const TargetRegisterClass *RC)
void adjustReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator II, const DebugLoc &DL, Register DestReg, Register SrcReg, StackOffset Offset, MachineInstr::MIFlag Flag, MaybeAlign RequiredAlign) const