LLVM 24.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"
17#include "RISCVSubtarget.h"
28#include "llvm/MC/MCDwarf.h"
29#include "llvm/Support/LEB128.h"
30
31#include <algorithm>
32#include <cstdint>
33
34#define DEBUG_TYPE "riscv-frame"
35
36using namespace llvm;
37
39 if (ABI == RISCVABI::ABI_ILP32E)
40 return Align(4);
41 if (ABI == RISCVABI::ABI_LP64E)
42 return Align(8);
43 return Align(16);
44}
45
49 /*LocalAreaOffset=*/0,
50 /*TransientStackAlignment=*/getABIStackAlignment(STI.getTargetABI())),
51 STI(STI) {}
52
53// The register used to hold the frame pointer.
54static constexpr MCPhysReg FPReg = RISCV::X8;
55
56// The register used to hold the stack pointer.
57static constexpr MCPhysReg SPReg = RISCV::X2;
58
59// The register used to hold the return address.
60static constexpr MCPhysReg RAReg = RISCV::X1;
61
62// LIst of CSRs that are given a fixed location by save/restore libcalls or
63// Zcmp/Xqccmp Push/Pop. The order in this table indicates the order the
64// registers are saved on the stack. Zcmp uses the reverse order of save/restore
65// and Xqccmp on the stack, but this is handled when offsets are calculated.
66static const MCPhysReg FixedCSRFIMap[] = {
67 /*ra*/ RAReg, /*s0*/ FPReg, /*s1*/ RISCV::X9,
68 /*s2*/ RISCV::X18, /*s3*/ RISCV::X19, /*s4*/ RISCV::X20,
69 /*s5*/ RISCV::X21, /*s6*/ RISCV::X22, /*s7*/ RISCV::X23,
70 /*s8*/ RISCV::X24, /*s9*/ RISCV::X25, /*s10*/ RISCV::X26,
71 /*s11*/ RISCV::X27};
72
73// The number of stack bytes allocated by `QC.C.MIENTER(.NEST)` and popped by
74// `QC.C.MILEAVERET`.
75static constexpr uint64_t QCIInterruptPushAmount = 96;
76
77static const std::pair<MCPhysReg, int8_t> FixedCSRFIQCIInterruptMap[] = {
78 /* -1 is a gap for mepc/mnepc */
79 {/*fp*/ FPReg, -2},
80 /* -3 is a gap for qc.mcause */
81 {/*ra*/ RAReg, -4},
82 /* -5 is reserved */
83 {/*t0*/ RISCV::X5, -6},
84 {/*t1*/ RISCV::X6, -7},
85 {/*t2*/ RISCV::X7, -8},
86 {/*a0*/ RISCV::X10, -9},
87 {/*a1*/ RISCV::X11, -10},
88 {/*a2*/ RISCV::X12, -11},
89 {/*a3*/ RISCV::X13, -12},
90 {/*a4*/ RISCV::X14, -13},
91 {/*a5*/ RISCV::X15, -14},
92 {/*a6*/ RISCV::X16, -15},
93 {/*a7*/ RISCV::X17, -16},
94 {/*t3*/ RISCV::X28, -17},
95 {/*t4*/ RISCV::X29, -18},
96 {/*t5*/ RISCV::X30, -19},
97 {/*t6*/ RISCV::X31, -20},
98 /* -21, -22, -23, -24 are reserved */
99};
100
101/// Returns true if DWARF CFI instructions ("frame moves") should be emitted.
102static bool needsDwarfCFI(const MachineFunction &MF) {
103 return MF.needsFrameMoves();
104}
105
106// For now we use x3, a.k.a gp, as pointer to shadow call stack.
107// User should not use x3 in their asm.
110 const DebugLoc &DL) {
111 const auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
112 RISCVMachineFunctionInfo::ShadowStackKind SSK = RVFI->getShadowStackKind(MF);
114 return;
115
116 const auto &STI = MF.getSubtarget<RISCVSubtarget>();
117 const llvm::RISCVRegisterInfo *TRI = STI.getRegisterInfo();
118
119 // Do not save RA to the SCS if it's not saved to the regular stack,
120 // i.e. RA is not at risk of being overwritten.
121 std::vector<CalleeSavedInfo> &CSI = MF.getFrameInfo().getCalleeSavedInfo();
122 if (llvm::none_of(
123 CSI, [&](CalleeSavedInfo &CSR) { return CSR.getReg() == RAReg; }))
124 return;
125
126 const RISCVInstrInfo *TII = STI.getInstrInfo();
128 BuildMI(MBB, MI, DL, TII->get(RISCV::SSPUSH))
129 .addReg(RAReg)
131 return;
132 }
133
135 "Unexpected Shadow Stack Kind");
136
137 Register SCSPReg = RISCVABI::getSCSPReg();
138
139 bool IsRV64 = STI.is64Bit();
140 int64_t SlotSize = STI.getXLen() / 8;
141 // Store return address to shadow call stack
142 // addi gp, gp, [4|8]
143 // s[w|d] ra, -[4|8](gp)
144 BuildMI(MBB, MI, DL, TII->get(RISCV::ADDI), SCSPReg)
145 .addReg(SCSPReg)
146 .addImm(SlotSize)
148 BuildMI(MBB, MI, DL, TII->get(IsRV64 ? RISCV::SD : RISCV::SW))
149 .addReg(RAReg)
150 .addReg(SCSPReg)
151 .addImm(-SlotSize)
153
154 if (!needsDwarfCFI(MF))
155 return;
156
157 // Emit a CFI instruction that causes SlotSize to be subtracted from the value
158 // of the shadow stack pointer when unwinding past this frame.
159 char DwarfSCSReg = TRI->getDwarfRegNum(SCSPReg, /*IsEH*/ true);
160 assert(DwarfSCSReg < 32 && "SCS Register should be < 32 (X3).");
161
162 char Offset = static_cast<char>(-SlotSize) & 0x7f;
163 const char CFIInst[] = {
164 dwarf::DW_CFA_val_expression,
165 DwarfSCSReg, // register
166 2, // length
167 static_cast<char>(unsigned(dwarf::DW_OP_breg0 + DwarfSCSReg)),
168 Offset, // addend (sleb128)
169 };
170
172 .buildEscape(StringRef(CFIInst, sizeof(CFIInst)));
173}
174
177 const DebugLoc &DL) {
178 const auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
179 RISCVMachineFunctionInfo::ShadowStackKind SSK = RVFI->getShadowStackKind(MF);
181 return;
182
183 const auto &STI = MF.getSubtarget<RISCVSubtarget>();
184
185 // See emitSCSPrologue() above.
186 std::vector<CalleeSavedInfo> &CSI = MF.getFrameInfo().getCalleeSavedInfo();
187 if (llvm::none_of(
188 CSI, [&](CalleeSavedInfo &CSR) { return CSR.getReg() == RAReg; }))
189 return;
190
191 // The shadow call stack popchk needs to happen after cm.pop that loads ra.
192 if (MI != MBB.end() &&
193 (MI->getOpcode() == RISCV::CM_POP || MI->getOpcode() == RISCV::QC_CM_POP))
194 ++MI;
195 const RISCVInstrInfo *TII = STI.getInstrInfo();
197 // `sspopchk x5` is the only compressible form, but this would require using
198 // `x5` as the return address everywhere, which would also mean reserving
199 // it. We prefer to just use `x1` to avoid that complexity.
200 BuildMI(MBB, MI, DL, TII->get(RISCV::SSPOPCHK))
201 .addReg(RAReg)
203 return;
204 }
205
207 "Unexpected Shadow Stack Kind");
208
209 Register SCSPReg = RISCVABI::getSCSPReg();
210
211 bool IsRV64 = STI.is64Bit();
212 int64_t SlotSize = STI.getXLen() / 8;
213 // Load return address from shadow call stack
214 // l[w|d] ra, -[4|8](gp)
215 // addi gp, gp, -[4|8]
216 BuildMI(MBB, MI, DL, TII->get(IsRV64 ? RISCV::LD : RISCV::LW), RAReg)
217 .addReg(SCSPReg)
218 .addImm(-SlotSize)
220 BuildMI(MBB, MI, DL, TII->get(RISCV::ADDI), SCSPReg)
221 .addReg(SCSPReg)
222 .addImm(-SlotSize)
224 if (needsDwarfCFI(MF)) {
225 // Restore the SCS pointer
227 }
228}
229
230// Insert instruction to swap mscratchsw with sp
233 const DebugLoc &DL,
234 MachineInstr::MIFlag FrameFlag) {
235 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
236
237 if (!RVFI->isSiFiveStackSwapInterrupt(MF))
238 return;
239
240 const auto &STI = MF.getSubtarget<RISCVSubtarget>();
241 const RISCVInstrInfo *TII = STI.getInstrInfo();
242
243 assert(STI.hasVendorXSfmclic() && "Stack Swapping Requires XSfmclic");
244
245 BuildMI(MBB, MBBI, DL, TII->get(RISCV::CSRRW), SPReg)
246 .addImm(RISCVSysReg::sf_mscratchcsw)
248 .setMIFlag(FrameFlag);
249
250 // FIXME: CFI Information for this swap.
251}
252
253static void
256 if (!RVFI.isSiFivePreemptibleInterrupt(MF))
257 return;
258
259 const TargetRegisterClass &RC = RISCV::GPRRegClass;
260 const TargetRegisterInfo &TRI =
261 *MF.getSubtarget<RISCVSubtarget>().getRegisterInfo();
262 MachineFrameInfo &MFI = MF.getFrameInfo();
263
264 // Create two frame objects for saving `mcause` and `mepc`.
265 for (int I = 0; I < 2; ++I) {
266 int FI = MFI.CreateStackObject(TRI.getSpillSize(RC), TRI.getSpillAlign(RC),
267 true);
269 }
270}
271
272// The scratch register retains an ordinary CSI slot, but its save and restore
273// are emitted explicitly as part of the SiFive CLIC interrupt sequence.
275 const auto &CSI = MF.getFrameInfo().getCalleeSavedInfo();
276 auto ScratchCS = llvm::find_if(
277 CSI, [](const CalleeSavedInfo &CS) { return CS.getReg() == RISCV::X5; });
278 assert(ScratchCS != CSI.end() && "Missing SiFive CLIC scratch spill slot");
279 return ScratchCS->getFrameIdx();
280}
281
285 const DebugLoc &DL) {
286 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
287
288 if (!RVFI->isSiFivePreemptibleInterrupt(MF))
289 return;
290
291 const auto &STI = MF.getSubtarget<RISCVSubtarget>();
292 const RISCVInstrInfo *TII = STI.getInstrInfo();
293
294 // FIXME: CFI information for `mcause` and `mepc` is missing.
295
296 // Preserve X5 before using it to save the interrupt CSRs. Other GPRs
297 // are saved by the ordinary spill sequence after preemption is enabled.
298 int ScratchFI = getSiFiveCLICScratchFrameIndex(MF);
299 TII->storeRegToStackSlot(MBB, MBBI, RISCV::X5, /*IsKill=*/true, ScratchFI,
300 &RISCV::GPRRegClass, Register(),
302 if (needsDwarfCFI(MF))
304 .buildOffset(RISCV::X5, MF.getFrameInfo().getObjectOffset(ScratchFI));
305
306 BuildMI(MBB, MBBI, DL, TII->get(RISCV::CSRRS), RISCV::X5)
307 .addImm(RISCVSysReg::mcause)
308 .addReg(RISCV::X0)
310 TII->storeRegToStackSlot(MBB, MBBI, RISCV::X5, /* IsKill=*/true,
311 RVFI->getInterruptCSRFrameIndex(0),
312 &RISCV::GPRRegClass, Register(),
314
315 BuildMI(MBB, MBBI, DL, TII->get(RISCV::CSRRS), RISCV::X5)
316 .addImm(RISCVSysReg::mepc)
317 .addReg(RISCV::X0)
319
320 // Enable interrupts.
321 BuildMI(MBB, MBBI, DL, TII->get(RISCV::CSRRSI), RISCV::X0)
322 .addImm(RISCVSysReg::mstatus)
323 .addImm(8)
325 TII->storeRegToStackSlot(MBB, MBBI, RISCV::X5, /* IsKill=*/true,
326 RVFI->getInterruptCSRFrameIndex(1),
327 &RISCV::GPRRegClass, Register(),
329}
330
334 CFIInstBuilder &CFIBuilder,
335 const DebugLoc &DL) {
336 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
337
338 if (!RVFI->isSiFivePreemptibleInterrupt(MF))
339 return;
340
341 const auto &STI = MF.getSubtarget<RISCVSubtarget>();
342 const RISCVInstrInfo *TII = STI.getInstrInfo();
343
344 // FIXME: CFI information for `mcause` and `mepc` is missing.
345
346 // Load mepc while preemption is still enabled. A nested handler preserves
347 // X5. Interrupts only need to be disabled before writing the CSRs back.
348 TII->loadRegFromStackSlot(MBB, MBBI, RISCV::X5,
349 RVFI->getInterruptCSRFrameIndex(1),
350 &RISCV::GPRRegClass, Register(),
351 RISCV::NoSubRegister, MachineInstr::FrameDestroy);
352
353 // Disable interrupts.
354 BuildMI(MBB, MBBI, DL, TII->get(RISCV::CSRRCI), RISCV::X0)
355 .addImm(RISCVSysReg::mstatus)
356 .addImm(8)
358
359 // Restore `mepc` and `mcause` through X5, then restore the value X5 held
360 // on entry to the handler.
361 BuildMI(MBB, MBBI, DL, TII->get(RISCV::CSRRW), RISCV::X0)
362 .addImm(RISCVSysReg::mepc)
363 .addReg(RISCV::X5, RegState::Kill)
365
366 TII->loadRegFromStackSlot(MBB, MBBI, RISCV::X5,
367 RVFI->getInterruptCSRFrameIndex(0),
368 &RISCV::GPRRegClass, Register(),
369 RISCV::NoSubRegister, MachineInstr::FrameDestroy);
370 BuildMI(MBB, MBBI, DL, TII->get(RISCV::CSRRW), RISCV::X0)
371 .addImm(RISCVSysReg::mcause)
372 .addReg(RISCV::X5, RegState::Kill)
374
375 // The ordinary reloads have finished. Recover the interrupted value of X5
376 // only after it has restored both CSRs.
377 TII->loadRegFromStackSlot(MBB, MBBI, RISCV::X5,
379 &RISCV::GPRRegClass, Register(),
380 RISCV::NoSubRegister, MachineInstr::FrameDestroy);
381 if (needsDwarfCFI(MF))
382 CFIBuilder.buildRestore(RISCV::X5);
383}
384
385// Get the ID of the libcall used for spilling and restoring callee saved
386// registers. The ID is representative of the number of registers saved or
387// restored by the libcall, except it is zero-indexed - ID 0 corresponds to a
388// single register.
389static int getLibCallID(const MachineFunction &MF,
390 const std::vector<CalleeSavedInfo> &CSI) {
391 const auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
392
393 if (CSI.empty() || !RVFI->useSaveRestoreLibCalls(MF))
394 return -1;
395
396 MCRegister MaxReg;
397 for (auto &CS : CSI)
398 // assignCalleeSavedSpillSlots assigns negative frame indexes to
399 // registers which can be saved by libcall.
400 if (CS.getFrameIdx() < 0)
401 MaxReg = std::max(MaxReg.id(), CS.getReg().id());
402
403 if (!MaxReg)
404 return -1;
405
406 switch (MaxReg.id()) {
407 default:
408 llvm_unreachable("Something has gone wrong!");
409 // clang-format off
410 case /*s11*/ RISCV::X27: return 12;
411 case /*s10*/ RISCV::X26: return 11;
412 case /*s9*/ RISCV::X25: return 10;
413 case /*s8*/ RISCV::X24: return 9;
414 case /*s7*/ RISCV::X23: return 8;
415 case /*s6*/ RISCV::X22: return 7;
416 case /*s5*/ RISCV::X21: return 6;
417 case /*s4*/ RISCV::X20: return 5;
418 case /*s3*/ RISCV::X19: return 4;
419 case /*s2*/ RISCV::X18: return 3;
420 case /*s1*/ RISCV::X9: return 2;
421 case /*s0*/ FPReg: return 1;
422 case /*ra*/ RAReg: return 0;
423 // clang-format on
424 }
425}
426
427// Get the name of the libcall used for spilling callee saved registers.
428// If this function will not use save/restore libcalls, then return a nullptr.
429static const char *
431 const std::vector<CalleeSavedInfo> &CSI) {
432 static const char *const SpillLibCalls[] = {
433 "__riscv_save_0",
434 "__riscv_save_1",
435 "__riscv_save_2",
436 "__riscv_save_3",
437 "__riscv_save_4",
438 "__riscv_save_5",
439 "__riscv_save_6",
440 "__riscv_save_7",
441 "__riscv_save_8",
442 "__riscv_save_9",
443 "__riscv_save_10",
444 "__riscv_save_11",
445 "__riscv_save_12"
446 };
447
448 int LibCallID = getLibCallID(MF, CSI);
449 if (LibCallID == -1)
450 return nullptr;
451 return SpillLibCalls[LibCallID];
452}
453
454// Get the name of the libcall used for restoring callee saved registers.
455// If this function will not use save/restore libcalls, then return a nullptr.
456static const char *
458 const std::vector<CalleeSavedInfo> &CSI) {
459 static const char *const RestoreLibCalls[] = {
460 "__riscv_restore_0",
461 "__riscv_restore_1",
462 "__riscv_restore_2",
463 "__riscv_restore_3",
464 "__riscv_restore_4",
465 "__riscv_restore_5",
466 "__riscv_restore_6",
467 "__riscv_restore_7",
468 "__riscv_restore_8",
469 "__riscv_restore_9",
470 "__riscv_restore_10",
471 "__riscv_restore_11",
472 "__riscv_restore_12"
473 };
474
475 int LibCallID = getLibCallID(MF, CSI);
476 if (LibCallID == -1)
477 return nullptr;
478 return RestoreLibCalls[LibCallID];
479}
480
481// Get the max reg of Push/Pop for restoring callee saved registers.
482static unsigned getNumPushPopRegs(const std::vector<CalleeSavedInfo> &CSI) {
483 unsigned NumPushPopRegs = 0;
484 for (auto &CS : CSI) {
485 auto *FII = llvm::find_if(FixedCSRFIMap,
486 [&](MCPhysReg P) { return P == CS.getReg(); });
487 if (FII != std::end(FixedCSRFIMap)) {
488 unsigned RegNum = std::distance(std::begin(FixedCSRFIMap), FII);
489 NumPushPopRegs = std::max(NumPushPopRegs, RegNum + 1);
490 }
491 }
492 assert(NumPushPopRegs != 12 && "x26 requires x27 to also be pushed");
493 return NumPushPopRegs;
494}
495
496// Return true if the specified function should have a dedicated frame
497// pointer register. This is true if frame pointer elimination is
498// disabled, if it needs dynamic stack realignment, if the function has
499// variable sized allocas, or if the frame address is taken.
501 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
502
503 const MachineFrameInfo &MFI = MF.getFrameInfo();
504 if (MF.disableFramePointerElim() || RegInfo->hasStackRealignment(MF) ||
506 return true;
507
508 // With large callframes around we may need to use FP to access the scavenging
509 // emergency spillslot.
510 //
511 // We calculate the MaxCallFrameSize at the end of isel so this value should
512 // be stable for the whole post-isel MIR pipeline.
513 //
514 // NOTE: The idea of forcing a frame pointer is copied from AArch64, but they
515 // conservatively return true when the call frame size hasd not been
516 // computed yet. On RISC-V that caused MachineOutliner tests to fail the
517 // MachineVerifier due to outlined functions not computing max call frame
518 // size thus the frame pointer would always be reserved.
519 if (MFI.isMaxCallFrameSizeComputed() && MFI.getMaxCallFrameSize() > 2047)
520 return true;
521
522 return false;
523}
524
526 const MachineFrameInfo &MFI = MF.getFrameInfo();
527 const TargetRegisterInfo *TRI = STI.getRegisterInfo();
528
529 // If we do not reserve stack space for outgoing arguments in prologue,
530 // we will adjust the stack pointer before call instruction. After the
531 // adjustment, we can not use SP to access the stack objects for the
532 // arguments. Instead, use BP to access these stack objects.
533 return (MFI.hasVarSizedObjects() ||
535 MFI.getMaxCallFrameSize() != 0))) &&
536 TRI->hasStackRealignment(MF);
537}
538
539// Determines the size of the frame and maximum call frame size.
540void RISCVFrameLowering::determineFrameLayout(MachineFunction &MF) const {
541 MachineFrameInfo &MFI = MF.getFrameInfo();
542 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
543
544 // Get the number of bytes to allocate from the FrameInfo.
545 uint64_t FrameSize = MFI.getStackSize();
546
547 // QCI Interrupts use at least 96 bytes of stack space
548 if (RVFI->useQCIInterrupt(MF))
549 FrameSize = std::max(FrameSize, QCIInterruptPushAmount);
550
551 // Get the alignment.
552 Align StackAlign = getStackAlign();
553
554 // Make sure the frame is aligned.
555 FrameSize = alignTo(FrameSize, StackAlign);
556
557 // Update frame info.
558 MFI.setStackSize(FrameSize);
559
560 // When using SP or BP to access stack objects, we may require extra padding
561 // to ensure the bottom of the RVV stack is correctly aligned within the main
562 // stack. We calculate this as the amount required to align the scalar local
563 // variable section up to the RVV alignment.
565 if (RVFI->getRVVStackSize() && (!hasFP(MF) || TRI->hasStackRealignment(MF))) {
566 int ScalarLocalVarSize = FrameSize - RVFI->getCalleeSavedStackSize() -
567 RVFI->getVarArgsSaveSize();
568 if (auto RVVPadding =
569 offsetToAlignment(ScalarLocalVarSize, RVFI->getRVVStackAlign()))
570 RVFI->setRVVPadding(RVVPadding);
571 }
572}
573
574// Returns the stack size including RVV padding (when required), rounded back
575// up to the required stack alignment.
577 const MachineFunction &MF) const {
578 const MachineFrameInfo &MFI = MF.getFrameInfo();
579 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
580 return alignTo(MFI.getStackSize() + RVFI->getRVVPadding(), getStackAlign());
581}
582
585 const std::vector<CalleeSavedInfo> &CSI,
586 bool ReverseOrder = false) {
587 const MachineFrameInfo &MFI = MF.getFrameInfo();
589
590 for (auto &CS : CSI) {
591 int FI = CS.getFrameIdx();
592 if (FI >= 0 && MFI.getStackID(FI) == TargetStackID::Default)
593 NonLibcallCSI.push_back(CS);
594 }
595
596 // Reverse the order so that load/store operations use ascending addresses,
597 // enabling better load/store clustering and fusion.
598 if (ReverseOrder)
599 std::reverse(NonLibcallCSI.begin(), NonLibcallCSI.end());
600
601 return NonLibcallCSI;
602}
603
604// Exclude X5 from ordinary spills and restores for SiFive CLIC preemptible
605// handlers, which save and restore it explicitly.
608 const std::vector<CalleeSavedInfo> &CSI,
609 bool ReverseOrder = false) {
610 auto InterruptCSI = getUnmanagedCSI(MF, CSI, ReverseOrder);
612 llvm::erase_if(InterruptCSI, [](const CalleeSavedInfo &CS) {
613 return CS.getReg() == RISCV::X5;
614 });
615 return InterruptCSI;
616}
617
620 const std::vector<CalleeSavedInfo> &CSI) {
621 const MachineFrameInfo &MFI = MF.getFrameInfo();
623
624 for (auto &CS : CSI) {
625 int FI = CS.getFrameIdx();
626 if (FI >= 0 && MFI.getStackID(FI) == TargetStackID::ScalableVector)
627 RVVCSI.push_back(CS);
628 }
629
630 return RVVCSI;
631}
632
635 const std::vector<CalleeSavedInfo> &CSI) {
636 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
637
638 SmallVector<CalleeSavedInfo, 8> PushOrLibCallsCSI;
639 if (!RVFI->useSaveRestoreLibCalls(MF) && !RVFI->isPushable(MF))
640 return PushOrLibCallsCSI;
641
642 for (const auto &CS : CSI) {
643 if (RVFI->useQCIInterrupt(MF)) {
644 // Some registers are saved by both `QC.C.MIENTER(.NEST)` and
645 // `QC.CM.PUSH(FP)`. In these cases, prioritise the CFI info that points
646 // to the versions saved by `QC.C.MIENTER(.NEST)` which is what FP
647 // unwinding would use.
649 CS.getReg()))
650 continue;
651 }
652
653 if (llvm::is_contained(FixedCSRFIMap, CS.getReg()))
654 PushOrLibCallsCSI.push_back(CS);
655 }
656
657 return PushOrLibCallsCSI;
658}
659
662 const std::vector<CalleeSavedInfo> &CSI) {
663 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
664
665 SmallVector<CalleeSavedInfo, 8> QCIInterruptCSI;
666 if (!RVFI->useQCIInterrupt(MF))
667 return QCIInterruptCSI;
668
669 for (const auto &CS : CSI) {
671 CS.getReg()))
672 QCIInterruptCSI.push_back(CS);
673 }
674
675 return QCIInterruptCSI;
676}
677
679 const MachineBasicBlock &MBB) {
680 const MachineFunction *MF = MBB.getParent();
681 LiveRegs.addLiveIns(MBB);
682 const MCPhysReg *CSRegs = MF->getRegInfo().getCalleeSavedRegs();
683 for (unsigned i = 0; CSRegs[i]; ++i)
684 LiveRegs.addReg(CSRegs[i]);
685}
686
688 MachineBasicBlock *MBB, Register PreferredReg, Register DontUseReg) const {
689 MachineFunction *MF = MBB->getParent();
690
691 // Stack protection code is being inserted at beginning of function, use
692 // register which has been historically used
693 if (&MF->front() == MBB)
694 return PreferredReg;
695
696 const RISCVSubtarget &Subtarget = MF->getSubtarget<RISCVSubtarget>();
697 const TargetRegisterInfo &TRI = *Subtarget.getRegisterInfo();
700
701 const MachineRegisterInfo &MRI = MF->getRegInfo();
702 // Prefer the register which has been historically used for stack protector
703 if (LiveRegs.available(MRI, PreferredReg))
704 return PreferredReg;
705
706 static const MCPhysReg CandidateRegs[] = {
707 RISCV::X5, RISCV::X6, RISCV::X7, RISCV::X28,
708 RISCV::X29, RISCV::X30, RISCV::X31,
709 };
710
711 for (unsigned Reg : CandidateRegs) {
712 if (Reg != DontUseReg && LiveRegs.available(MRI, Reg))
713 return Reg;
714 }
715
716 return Register();
717}
718
719void RISCVFrameLowering::allocateAndProbeStackForRVV(
721 MachineBasicBlock::iterator MBBI, const DebugLoc &DL, int64_t Amount,
722 MachineInstr::MIFlag Flag, bool EmitCFI, bool DynAllocation) const {
723 assert(Amount != 0 && "Did not need to adjust stack pointer for RVV.");
724
725 // Emit a variable-length allocation probing loop.
726
727 // Get VLEN in TargetReg
728 Register TargetReg = findScratchNonCalleeSaveRegister(&MBB, RISCV::X6);
729 assert(TargetReg.isValid() &&
730 "No available scratch register for stack probing");
732 uint32_t NumOfVReg = Amount / RISCV::RVVBytesPerBlock;
733 BuildMI(MBB, MBBI, DL, TII->get(RISCV::PseudoReadVLENB), TargetReg)
734 .setMIFlag(Flag);
735 TII->mulImm(MF, MBB, MBBI, DL, TargetReg, NumOfVReg, Flag);
736
738 if (EmitCFI) {
739 // Set the CFA register to TargetReg.
740 CFIBuilder.buildDefCFA(TargetReg, -Amount);
741 }
742
743 // It will be expanded to a probe loop in `inlineStackProbe`.
744 BuildMI(MBB, MBBI, DL, TII->get(RISCV::PROBED_STACKALLOC_RVV))
745 .addReg(TargetReg);
746
747 if (EmitCFI) {
748 // Set the CFA register back to SP.
749 CFIBuilder.buildDefCFARegister(SPReg);
750 }
751
752 // SUB SP, SP, T1
753 BuildMI(MBB, MBBI, DL, TII->get(RISCV::SUB), SPReg)
754 .addReg(SPReg)
755 .addReg(TargetReg)
756 .setMIFlag(Flag);
757
758 // If we have a dynamic allocation later we need to probe any residuals.
759 if (DynAllocation) {
760 BuildMI(MBB, MBBI, DL, TII->get(STI.is64Bit() ? RISCV::SD : RISCV::SW))
761 .addReg(RISCV::X0)
762 .addReg(SPReg)
763 .addImm(0)
765 }
766}
767
771 llvm::raw_string_ostream &Comment) {
772 int64_t FixedOffset = Offset.getFixed();
773 int64_t ScalableOffset = Offset.getScalable();
774 unsigned DwarfVLenB = TRI.getDwarfRegNum(RISCV::VLENB, true);
775 if (FixedOffset) {
776 Expr.push_back(dwarf::DW_OP_consts);
777 appendLEB128<LEB128Sign::Signed>(Expr, FixedOffset);
778 Expr.push_back((uint8_t)dwarf::DW_OP_plus);
779 Comment << (FixedOffset < 0 ? " - " : " + ") << std::abs(FixedOffset);
780 }
781
782 Expr.push_back((uint8_t)dwarf::DW_OP_consts);
783 appendLEB128<LEB128Sign::Signed>(Expr, ScalableOffset);
784
785 Expr.push_back((uint8_t)dwarf::DW_OP_bregx);
786 appendLEB128<LEB128Sign::Unsigned>(Expr, DwarfVLenB);
787 Expr.push_back(0);
788
789 Expr.push_back((uint8_t)dwarf::DW_OP_mul);
790 Expr.push_back((uint8_t)dwarf::DW_OP_plus);
791
792 Comment << (ScalableOffset < 0 ? " - " : " + ") << std::abs(ScalableOffset)
793 << " * vlenb";
794}
795
799 assert(Offset.getScalable() != 0 && "Did not need to adjust CFA for RVV");
800 SmallString<64> Expr;
801 std::string CommentBuffer;
802 llvm::raw_string_ostream Comment(CommentBuffer);
803 // Build up the expression (Reg + FixedOffset + ScalableOffset * VLENB).
804 unsigned DwarfReg = TRI.getDwarfRegNum(Reg, true);
805 Expr.push_back((uint8_t)(dwarf::DW_OP_breg0 + DwarfReg));
806 Expr.push_back(0);
807 if (Reg == SPReg)
808 Comment << "sp";
809 else
810 Comment << printReg(Reg, &TRI);
811
813
814 SmallString<64> DefCfaExpr;
815 DefCfaExpr.push_back(dwarf::DW_CFA_def_cfa_expression);
816 appendLEB128<LEB128Sign::Unsigned>(DefCfaExpr, Expr.size());
817 DefCfaExpr.append(Expr.str());
818
819 return MCCFIInstruction::createEscape(nullptr, DefCfaExpr.str(), SMLoc(),
820 Comment.str());
821}
822
825 assert(Offset.getScalable() != 0 && "Did not need to adjust CFA for RVV");
826 SmallString<64> Expr;
827 std::string CommentBuffer;
828 llvm::raw_string_ostream Comment(CommentBuffer);
829 Comment << printReg(Reg, &TRI) << " @ cfa";
830
831 // Build up the expression (FixedOffset + ScalableOffset * VLENB).
833
834 SmallString<64> DefCfaExpr;
835 unsigned DwarfReg = TRI.getDwarfRegNum(Reg, true);
836 DefCfaExpr.push_back(dwarf::DW_CFA_expression);
837 appendLEB128<LEB128Sign::Unsigned>(DefCfaExpr, DwarfReg);
838 appendLEB128<LEB128Sign::Unsigned>(DefCfaExpr, Expr.size());
839 DefCfaExpr.append(Expr.str());
840
841 return MCCFIInstruction::createEscape(nullptr, DefCfaExpr.str(), SMLoc(),
842 Comment.str());
843}
844
845// Allocate stack space and probe it if necessary.
848 MachineFunction &MF, uint64_t Offset,
849 uint64_t RealStackSize, bool EmitCFI,
850 bool NeedProbe, uint64_t ProbeSize,
851 bool DynAllocation,
852 MachineInstr::MIFlag Flag) const {
853 DebugLoc DL;
854 const RISCVRegisterInfo *RI = STI.getRegisterInfo();
855 const RISCVInstrInfo *TII = STI.getInstrInfo();
856 bool IsRV64 = STI.is64Bit();
858
859 // Simply allocate the stack if it's not big enough to require a probe.
860 if (!NeedProbe || Offset <= ProbeSize) {
862 Flag, getStackAlign());
863
864 if (EmitCFI)
865 CFIBuilder.buildDefCFAOffset(RealStackSize);
866
867 if (NeedProbe && DynAllocation) {
868 // s[d|w] zero, 0(sp)
869 BuildMI(MBB, MBBI, DL, TII->get(IsRV64 ? RISCV::SD : RISCV::SW))
870 .addReg(RISCV::X0)
871 .addReg(SPReg)
872 .addImm(0)
873 .setMIFlags(Flag);
874 }
875
876 return;
877 }
878
879 // The amount of stack that was already allocated before this allocation.
880 uint64_t CFAAdjust = RealStackSize - Offset;
881
882 // Unroll the probe loop depending on the number of iterations.
883 if (Offset < ProbeSize * 5) {
884 uint64_t CurrentOffset = 0;
885 while (CurrentOffset + ProbeSize <= Offset) {
886 RI->adjustReg(MBB, MBBI, DL, SPReg, SPReg,
887 StackOffset::getFixed(-ProbeSize), Flag, getStackAlign());
888 // s[d|w] zero, 0(sp)
889 BuildMI(MBB, MBBI, DL, TII->get(IsRV64 ? RISCV::SD : RISCV::SW))
890 .addReg(RISCV::X0)
891 .addReg(SPReg)
892 .addImm(0)
893 .setMIFlags(Flag);
894
895 CurrentOffset += ProbeSize;
896 if (EmitCFI)
897 CFIBuilder.buildDefCFAOffset(CurrentOffset + CFAAdjust);
898 }
899
900 uint64_t Residual = Offset - CurrentOffset;
901 if (Residual) {
902 RI->adjustReg(MBB, MBBI, DL, SPReg, SPReg,
903 StackOffset::getFixed(-Residual), Flag, getStackAlign());
904 if (EmitCFI)
905 CFIBuilder.buildDefCFAOffset(RealStackSize);
906
907 if (DynAllocation) {
908 // s[d|w] zero, 0(sp)
909 BuildMI(MBB, MBBI, DL, TII->get(IsRV64 ? RISCV::SD : RISCV::SW))
910 .addReg(RISCV::X0)
911 .addReg(SPReg)
912 .addImm(0)
913 .setMIFlags(Flag);
914 }
915 }
916
917 return;
918 }
919
920 // Emit a variable-length allocation probing loop.
921 uint64_t RoundedSize = alignDown(Offset, ProbeSize);
922 uint64_t Residual = Offset - RoundedSize;
923
924 Register TargetReg = findScratchNonCalleeSaveRegister(&MBB, RISCV::X6);
925 assert(TargetReg.isValid() &&
926 "No available scratch register for stack probing");
927 // SUB TargetReg, SP, RoundedSize
928 RI->adjustReg(MBB, MBBI, DL, TargetReg, SPReg,
929 StackOffset::getFixed(-RoundedSize), Flag, getStackAlign());
930
931 if (EmitCFI) {
932 // Set the CFA register to TargetReg.
933 CFIBuilder.buildDefCFA(TargetReg, RoundedSize + CFAAdjust);
934 }
935
936 // It will be expanded to a probe loop in `inlineStackProbe`.
937 BuildMI(MBB, MBBI, DL, TII->get(RISCV::PROBED_STACKALLOC)).addReg(TargetReg);
938
939 if (EmitCFI) {
940 // Set the CFA register back to SP.
941 CFIBuilder.buildDefCFARegister(SPReg);
942 }
943
944 if (Residual) {
946 Flag, getStackAlign());
947 if (DynAllocation) {
948 // s[d|w] zero, 0(sp)
949 BuildMI(MBB, MBBI, DL, TII->get(IsRV64 ? RISCV::SD : RISCV::SW))
950 .addReg(RISCV::X0)
951 .addReg(SPReg)
952 .addImm(0)
953 .setMIFlags(Flag);
954 }
955 }
956
957 if (EmitCFI)
958 CFIBuilder.buildDefCFAOffset(RealStackSize);
959}
960
961static bool isPush(unsigned Opcode) {
962 switch (Opcode) {
963 case RISCV::CM_PUSH:
964 case RISCV::QC_CM_PUSH:
965 case RISCV::QC_CM_PUSHFP:
966 return true;
967 default:
968 return false;
969 }
970}
971
972static bool isPop(unsigned Opcode) {
973 // There are other pops but these are the only ones introduced during this
974 // pass.
975 switch (Opcode) {
976 case RISCV::CM_POP:
977 case RISCV::QC_CM_POP:
978 return true;
979 default:
980 return false;
981 }
982}
983
985 bool UpdateFP) {
986 switch (Kind) {
988 return RISCV::CM_PUSH;
990 return UpdateFP ? RISCV::QC_CM_PUSHFP : RISCV::QC_CM_PUSH;
991 default:
992 llvm_unreachable("Unhandled PushPopKind");
993 }
994}
995
997 // There are other pops but they are introduced later by the Push/Pop
998 // Optimizer.
999 switch (Kind) {
1001 return RISCV::CM_POP;
1003 return RISCV::QC_CM_POP;
1004 default:
1005 llvm_unreachable("Unhandled PushPopKind");
1006 }
1007}
1008
1010 MachineBasicBlock &MBB) const {
1011 MachineFrameInfo &MFI = MF.getFrameInfo();
1012 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
1013 const RISCVRegisterInfo *RI = STI.getRegisterInfo();
1015 bool PreferAscendingLS = STI.preferAscendingLoadStore();
1016
1017 Register BPReg = RISCVABI::getBPReg();
1018
1019 // Debug location must be unknown since the first debug location is used
1020 // to determine the end of the prologue.
1021 DebugLoc DL;
1022
1023 // All calls are tail calls in GHC calling conv, and functions have no
1024 // prologue/epilogue.
1026 return;
1027
1028 // SiFive CLIC needs to swap `sp` into `sf.mscratchcsw`
1030
1031 // Emit prologue for shadow call stack.
1032 emitSCSPrologue(MF, MBB, MBBI, DL);
1033
1034 // We keep track of the first instruction because it might be a
1035 // `(QC.)CM.PUSH(FP)`, and we may need to adjust the immediate rather than
1036 // inserting an `addi sp, sp, -N*16`
1037 auto PossiblePush = MBBI;
1038
1039 // Skip past all callee-saved register spill instructions.
1040 while (MBBI != MBB.end() && MBBI->getFlag(MachineInstr::FrameSetup))
1041 ++MBBI;
1042
1043 // Determine the correct frame layout
1044 determineFrameLayout(MF);
1045
1046 const auto &CSI = MFI.getCalleeSavedInfo();
1047
1048 // Skip to before the spills of scalar callee-saved registers
1049 // FIXME: assumes exactly one instruction is used to restore each
1050 // callee-saved register.
1051 MBBI = std::prev(
1052 MBBI, getRVVCalleeSavedInfo(MF, CSI).size() +
1053 getUnmanagedInterruptCSI(MF, CSI, PreferAscendingLS).size());
1055 bool NeedsDwarfCFI = needsDwarfCFI(MF);
1056
1057 // If libcalls are used to spill and restore callee-saved registers, the frame
1058 // has two sections; the opaque section managed by the libcalls, and the
1059 // section managed by MachineFrameInfo which can also hold callee saved
1060 // registers in fixed stack slots, both of which have negative frame indices.
1061 // This gets even more complicated when incoming arguments are passed via the
1062 // stack, as these too have negative frame indices. An example is detailed
1063 // below:
1064 //
1065 // | incoming arg | <- FI[-3]
1066 // | libcallspill |
1067 // | calleespill | <- FI[-2]
1068 // | calleespill | <- FI[-1]
1069 // | this_frame | <- FI[0]
1070 //
1071 // For negative frame indices, the offset from the frame pointer will differ
1072 // depending on which of these groups the frame index applies to.
1073 // The following calculates the correct offset knowing the number of callee
1074 // saved registers spilt by the two methods.
1075 if (int LibCallRegs = getLibCallID(MF, MFI.getCalleeSavedInfo()) + 1) {
1076 // Calculate the size of the frame managed by the libcall. The stack
1077 // alignment of these libcalls should be the same as how we set it in
1078 // getABIStackAlignment.
1079 unsigned LibCallFrameSize =
1080 alignTo((STI.getXLen() / 8) * LibCallRegs, getStackAlign());
1081 RVFI->setLibCallStackSize(LibCallFrameSize);
1082
1083 if (NeedsDwarfCFI) {
1084 CFIBuilder.buildDefCFAOffset(LibCallFrameSize);
1085 for (const CalleeSavedInfo &CS : getPushOrLibCallsSavedInfo(MF, CSI))
1086 CFIBuilder.buildOffset(CS.getReg(),
1087 MFI.getObjectOffset(CS.getFrameIdx()));
1088 }
1089 }
1090
1091 // FIXME (note copied from Lanai): This appears to be overallocating. Needs
1092 // investigation. Get the number of bytes to allocate from the FrameInfo.
1093 uint64_t RealStackSize = getStackSizeWithRVVPadding(MF);
1094 uint64_t StackSize = RealStackSize - RVFI->getReservedSpillsSize();
1095 uint64_t RVVStackSize = RVFI->getRVVStackSize();
1096
1097 // Early exit if there is no need to allocate on the stack
1098 if (RealStackSize == 0 && !MFI.adjustsStack() && RVVStackSize == 0)
1099 return;
1100
1101 // If the stack pointer has been marked as reserved, then produce an error if
1102 // the frame requires stack allocation
1103 if (STI.isRegisterReservedByUser(SPReg))
1105 MF.getFunction(), "Stack pointer required, but has been reserved."});
1106
1107 uint64_t FirstSPAdjustAmount = getFirstSPAdjustAmount(MF);
1108 // Split the SP adjustment to reduce the offsets of callee saved spill.
1109 if (FirstSPAdjustAmount) {
1110 StackSize = FirstSPAdjustAmount;
1111 RealStackSize = FirstSPAdjustAmount;
1112 }
1113
1114 if (RVFI->useQCIInterrupt(MF)) {
1115 // The function starts with `QC.C.MIENTER(.NEST)`, so the `(QC.)CM.PUSH(FP)`
1116 // could only be the next instruction.
1117 ++PossiblePush;
1118
1119 if (NeedsDwarfCFI) {
1120 // Insert the CFI metadata before where we think the `(QC.)CM.PUSH(FP)`
1121 // could be. The PUSH will also get its own CFI metadata for its own
1122 // modifications, which should come after the PUSH.
1123 CFIInstBuilder PushCFIBuilder(MBB, PossiblePush,
1126 for (const CalleeSavedInfo &CS : getQCISavedInfo(MF, CSI))
1127 PushCFIBuilder.buildOffset(CS.getReg(),
1128 MFI.getObjectOffset(CS.getFrameIdx()));
1129 }
1130 }
1131
1132 if (RVFI->isPushable(MF) && PossiblePush != MBB.end() &&
1133 isPush(PossiblePush->getOpcode())) {
1134 // Use available stack adjustment in push instruction to allocate additional
1135 // stack space. Align the stack size down to a multiple of 16. This is
1136 // needed for RVE.
1137 // FIXME: Can we increase the stack size to a multiple of 16 instead?
1138 uint64_t StackAdj =
1139 std::min(alignDown(StackSize, 16), static_cast<uint64_t>(48));
1140 PossiblePush->getOperand(1).setImm(StackAdj);
1141 StackSize -= StackAdj;
1142
1143 if (NeedsDwarfCFI) {
1144 CFIBuilder.buildDefCFAOffset(RealStackSize - StackSize);
1145 for (const CalleeSavedInfo &CS : getPushOrLibCallsSavedInfo(MF, CSI))
1146 CFIBuilder.buildOffset(CS.getReg(),
1147 MFI.getObjectOffset(CS.getFrameIdx()));
1148 }
1149 }
1150
1151 // Allocate space on the stack if necessary.
1152 auto &Subtarget = MF.getSubtarget<RISCVSubtarget>();
1153 const RISCVTargetLowering *TLI = Subtarget.getTargetLowering();
1154 bool NeedProbe = TLI->hasInlineStackProbe(MF);
1155 uint64_t ProbeSize = TLI->getStackProbeSize(MF, getStackAlign());
1156 bool DynAllocation =
1157 MF.getInfo<RISCVMachineFunctionInfo>()->hasDynamicAllocation();
1158 if (StackSize != 0)
1159 allocateStack(MBB, MBBI, MF, StackSize, RealStackSize, NeedsDwarfCFI,
1160 NeedProbe, ProbeSize, DynAllocation,
1162
1163 // Save SiFive CLIC CSRs into Stack
1165
1166 // The frame pointer is callee-saved, and code has been generated for us to
1167 // save it to the stack. We need to skip over the storing of callee-saved
1168 // registers as the frame pointer must be modified after it has been saved
1169 // to the stack, not before.
1170 // FIXME: assumes exactly one instruction is used to save each callee-saved
1171 // register.
1172 std::advance(MBBI,
1173 getUnmanagedInterruptCSI(MF, CSI, PreferAscendingLS).size());
1174 CFIBuilder.setInsertPoint(MBBI);
1175
1176 // Iterate over list of callee-saved registers and emit .cfi_offset
1177 // directives.
1178 if (NeedsDwarfCFI) {
1179 for (const CalleeSavedInfo &CS :
1180 getUnmanagedInterruptCSI(MF, CSI, PreferAscendingLS)) {
1181 MCRegister Reg = CS.getReg();
1182 int64_t Offset = MFI.getObjectOffset(CS.getFrameIdx());
1183 // Emit CFI for both sub-registers. The even register is at the base
1184 // offset and odd at base+4.
1185 if (RISCV::GPRPairRegClass.contains(Reg)) {
1186 MCRegister EvenReg = RI->getSubReg(Reg, RISCV::sub_gpr_even);
1187 MCRegister OddReg = RI->getSubReg(Reg, RISCV::sub_gpr_odd);
1188 CFIBuilder.buildOffset(EvenReg, Offset);
1189 CFIBuilder.buildOffset(OddReg, Offset + 4);
1190 } else {
1191 CFIBuilder.buildOffset(Reg, Offset);
1192 }
1193 }
1194 }
1195
1196 // Generate new FP.
1197 if (hasFP(MF)) {
1198 if (STI.isRegisterReservedByUser(FPReg))
1200 MF.getFunction(), "Frame pointer required, but has been reserved."});
1201 // The frame pointer does need to be reserved from register allocation.
1202 assert(MF.getRegInfo().isReserved(FPReg) && "FP not reserved");
1203
1204 // Some stack management variants automatically keep FP updated, so we don't
1205 // need an instruction to do so.
1206 if (!RVFI->hasImplicitFPUpdates(MF)) {
1207 RI->adjustReg(
1208 MBB, MBBI, DL, FPReg, SPReg,
1209 StackOffset::getFixed(RealStackSize - RVFI->getVarArgsSaveSize()),
1211 }
1212
1213 if (NeedsDwarfCFI)
1214 CFIBuilder.buildDefCFA(FPReg, RVFI->getVarArgsSaveSize());
1215 }
1216
1217 uint64_t SecondSPAdjustAmount = 0;
1218 // Emit the second SP adjustment after saving callee saved registers.
1219 if (FirstSPAdjustAmount) {
1220 SecondSPAdjustAmount = getStackSizeWithRVVPadding(MF) - FirstSPAdjustAmount;
1221 assert(SecondSPAdjustAmount > 0 &&
1222 "SecondSPAdjustAmount should be greater than zero");
1223
1224 allocateStack(MBB, MBBI, MF, SecondSPAdjustAmount,
1225 getStackSizeWithRVVPadding(MF), NeedsDwarfCFI && !hasFP(MF),
1226 NeedProbe, ProbeSize, DynAllocation,
1228 }
1229
1230 if (RVVStackSize) {
1231 if (NeedProbe) {
1232 allocateAndProbeStackForRVV(MF, MBB, MBBI, DL, RVVStackSize,
1234 NeedsDwarfCFI && !hasFP(MF), DynAllocation);
1235 } else {
1236 // We must keep the stack pointer aligned through any intermediate
1237 // updates.
1238 RI->adjustReg(MBB, MBBI, DL, SPReg, SPReg,
1239 StackOffset::getScalable(-RVVStackSize),
1241 }
1242
1243 if (NeedsDwarfCFI && !hasFP(MF)) {
1244 // Emit .cfi_def_cfa_expression "sp + StackSize + RVVStackSize * vlenb".
1246 *RI, SPReg,
1247 StackOffset::get(getStackSizeWithRVVPadding(MF), RVVStackSize / 8)));
1248 }
1249
1250 std::advance(MBBI, getRVVCalleeSavedInfo(MF, CSI).size());
1251 if (NeedsDwarfCFI)
1252 emitCalleeSavedRVVPrologCFI(MBB, MBBI, hasFP(MF));
1253 }
1254
1255 if (hasFP(MF)) {
1256 // Realign Stack
1257 const RISCVRegisterInfo *RI = STI.getRegisterInfo();
1258 if (RI->hasStackRealignment(MF)) {
1259 Align MaxAlignment = MFI.getMaxAlign();
1260
1261 const RISCVInstrInfo *TII = STI.getInstrInfo();
1262 if (isInt<12>(-(int64_t)MaxAlignment.value())) {
1263 BuildMI(MBB, MBBI, DL, TII->get(RISCV::ANDI), SPReg)
1264 .addReg(SPReg)
1265 .addImm(-(int64_t)MaxAlignment.value())
1267 } else {
1268 unsigned ShiftAmount = Log2(MaxAlignment);
1269 Register VR =
1270 MF.getRegInfo().createVirtualRegister(&RISCV::GPRRegClass);
1271 BuildMI(MBB, MBBI, DL, TII->get(RISCV::SRLI), VR)
1272 .addReg(SPReg)
1273 .addImm(ShiftAmount)
1275 BuildMI(MBB, MBBI, DL, TII->get(RISCV::SLLI), SPReg)
1276 .addReg(VR)
1277 .addImm(ShiftAmount)
1279 }
1280 if (NeedProbe && RVVStackSize == 0) {
1281 // Do a probe if the align + size allocated just passed the probe size
1282 // and was not yet probed.
1283 if (SecondSPAdjustAmount < ProbeSize &&
1284 SecondSPAdjustAmount + MaxAlignment.value() >= ProbeSize) {
1285 bool IsRV64 = STI.is64Bit();
1286 BuildMI(MBB, MBBI, DL, TII->get(IsRV64 ? RISCV::SD : RISCV::SW))
1287 .addReg(RISCV::X0)
1288 .addReg(SPReg)
1289 .addImm(0)
1291 }
1292 }
1293 // FP will be used to restore the frame in the epilogue, so we need
1294 // another base register BP to record SP after re-alignment. SP will
1295 // track the current stack after allocating variable sized objects.
1296 if (hasBP(MF)) {
1297 // move BP, SP
1298 BuildMI(MBB, MBBI, DL, TII->get(RISCV::ADDI), BPReg)
1299 .addReg(SPReg)
1300 .addImm(0)
1302 }
1303 }
1304 }
1305}
1306
1307void RISCVFrameLowering::deallocateStack(MachineFunction &MF,
1310 const DebugLoc &DL,
1311 uint64_t &StackSize,
1312 int64_t CFAOffset) const {
1314
1315 RI->adjustReg(MBB, MBBI, DL, SPReg, SPReg, StackOffset::getFixed(StackSize),
1317 StackSize = 0;
1318
1319 if (needsDwarfCFI(MF))
1321 .buildDefCFAOffset(CFAOffset);
1322}
1323
1325 MachineBasicBlock &MBB) const {
1326 const RISCVRegisterInfo *RI = STI.getRegisterInfo();
1327 MachineFrameInfo &MFI = MF.getFrameInfo();
1328 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
1329 bool PreferAscendingLS = STI.preferAscendingLoadStore();
1330
1331 // All calls are tail calls in GHC calling conv, and functions have no
1332 // prologue/epilogue.
1334 return;
1335
1336 // Get the insert location for the epilogue. If there were no terminators in
1337 // the block, get the last instruction.
1339 DebugLoc DL;
1340 if (!MBB.empty()) {
1341 MBBI = MBB.getLastNonDebugInstr();
1342 if (MBBI != MBB.end())
1343 DL = MBBI->getDebugLoc();
1344
1345 MBBI = MBB.getFirstTerminator();
1346
1347 // Skip to before the restores of all callee-saved registers.
1348 while (MBBI != MBB.begin() &&
1349 std::prev(MBBI)->getFlag(MachineInstr::FrameDestroy))
1350 --MBBI;
1351 }
1352
1353 const auto &CSI = MFI.getCalleeSavedInfo();
1354
1355 // Skip to before the restores of scalar callee-saved registers
1356 // FIXME: assumes exactly one instruction is used to restore each
1357 // callee-saved register.
1358 auto FirstScalarCSRRestoreInsn =
1359 std::next(MBBI, getRVVCalleeSavedInfo(MF, CSI).size());
1360 CFIInstBuilder CFIBuilder(MBB, FirstScalarCSRRestoreInsn,
1362 bool NeedsDwarfCFI = needsDwarfCFI(MF);
1363
1364 uint64_t FirstSPAdjustAmount = getFirstSPAdjustAmount(MF);
1365 uint64_t RealStackSize = FirstSPAdjustAmount ? FirstSPAdjustAmount
1367 uint64_t StackSize = FirstSPAdjustAmount ? FirstSPAdjustAmount
1369 RVFI->getReservedSpillsSize();
1370 uint64_t FPOffset = RealStackSize - RVFI->getVarArgsSaveSize();
1371 uint64_t RVVStackSize = RVFI->getRVVStackSize();
1372
1373 bool RestoreSPFromFP = RI->hasStackRealignment(MF) ||
1375 if (RVVStackSize) {
1376 // If RestoreSPFromFP the stack pointer will be restored using the frame
1377 // pointer value.
1378 if (!RestoreSPFromFP)
1379 RI->adjustReg(MBB, FirstScalarCSRRestoreInsn, DL, SPReg, SPReg,
1380 StackOffset::getScalable(RVVStackSize),
1382
1383 if (NeedsDwarfCFI) {
1384 if (!hasFP(MF))
1385 CFIBuilder.buildDefCFA(SPReg, RealStackSize);
1386 emitCalleeSavedRVVEpilogCFI(MBB, FirstScalarCSRRestoreInsn);
1387 }
1388 }
1389
1390 if (FirstSPAdjustAmount) {
1391 uint64_t SecondSPAdjustAmount =
1392 getStackSizeWithRVVPadding(MF) - FirstSPAdjustAmount;
1393 assert(SecondSPAdjustAmount > 0 &&
1394 "SecondSPAdjustAmount should be greater than zero");
1395
1396 // If RestoreSPFromFP the stack pointer will be restored using the frame
1397 // pointer value.
1398 if (!RestoreSPFromFP)
1399 RI->adjustReg(MBB, FirstScalarCSRRestoreInsn, DL, SPReg, SPReg,
1400 StackOffset::getFixed(SecondSPAdjustAmount),
1402
1403 if (NeedsDwarfCFI && !hasFP(MF))
1404 CFIBuilder.buildDefCFAOffset(FirstSPAdjustAmount);
1405 }
1406
1407 // Restore the stack pointer using the value of the frame pointer. Only
1408 // necessary if the stack pointer was modified, meaning the stack size is
1409 // unknown.
1410 //
1411 // In order to make sure the stack point is right through the EH region,
1412 // we also need to restore stack pointer from the frame pointer if we
1413 // don't preserve stack space within prologue/epilogue for outgoing variables,
1414 // normally it's just checking the variable sized object is present or not
1415 // is enough, but we also don't preserve that at prologue/epilogue when
1416 // have vector objects in stack.
1417 if (RestoreSPFromFP) {
1418 assert(hasFP(MF) && "frame pointer should not have been eliminated");
1419 RI->adjustReg(MBB, FirstScalarCSRRestoreInsn, DL, SPReg, FPReg,
1421 getStackAlign());
1422 }
1423
1424 if (NeedsDwarfCFI && hasFP(MF))
1425 CFIBuilder.buildDefCFA(SPReg, RealStackSize);
1426
1427 // Skip to after the restores of scalar callee-saved registers
1428 // FIXME: assumes exactly one instruction is used to restore each
1429 // callee-saved register.
1430 MBBI = std::next(FirstScalarCSRRestoreInsn,
1431 getUnmanagedInterruptCSI(MF, CSI, PreferAscendingLS).size());
1432 CFIBuilder.setInsertPoint(MBBI);
1433 emitSiFiveCLICPreemptibleRestores(MF, MBB, MBBI, CFIBuilder, DL);
1434
1435 auto emitRestoreCFI = [&](auto CSInfo) {
1436 for (auto &CS : CSInfo) {
1437 MCRegister Reg = CS.getReg();
1438 // Emit CFI for both sub-registers.
1439 if (RISCV::GPRPairRegClass.contains(Reg)) {
1440 MCRegister EvenReg = RI->getSubReg(Reg, RISCV::sub_gpr_even);
1441 MCRegister OddReg = RI->getSubReg(Reg, RISCV::sub_gpr_odd);
1442 CFIBuilder.buildRestore(EvenReg);
1443 CFIBuilder.buildRestore(OddReg);
1444 } else {
1445 CFIBuilder.buildRestore(Reg);
1446 }
1447 }
1448 };
1449
1450 if (RVFI->useSaveRestoreLibCalls(MF)) {
1451 if (RVFI->hasShadowStack(MF)) {
1452 // We don't use `__riscv_restore_<N>` to restore in this case, so we need
1453 // to make sure we're correctly accounting for the stack deallocation we
1454 // need to do, which now must include the allocation made by
1455 // `__riscv_save_<N>`.
1456 StackSize += RVFI->getLibCallStackSize();
1457
1460 MBBI = std::next(MBBI, LibcallCSI.size());
1461 CFIBuilder.setInsertPoint(MBBI);
1462
1463 if (NeedsDwarfCFI)
1464 emitRestoreCFI(LibcallCSI);
1465 } else {
1466 // tail __riscv_restore_[0-12] instruction is considered as a terminator,
1467 // therefore it is unnecessary to place any CFI instructions after it.
1468 // Just deallocate stack if needed and return.
1469 if (StackSize != 0)
1470 deallocateStack(MF, MBB, MBBI, DL, StackSize,
1471 RVFI->getLibCallStackSize());
1472
1473 // Emit epilogue for shadow call stack.
1474 emitSCSEpilogue(MF, MBB, MBBI, DL);
1475 return;
1476 }
1477 }
1478
1479 // Recover callee-saved registers.
1480 if (NeedsDwarfCFI)
1481 emitRestoreCFI(getUnmanagedInterruptCSI(MF, CSI, PreferAscendingLS));
1482
1483 if (RVFI->isPushable(MF) && MBBI != MBB.end() && isPop(MBBI->getOpcode())) {
1484 // Use available stack adjustment in pop instruction to deallocate stack
1485 // space. Align the stack size down to a multiple of 16. This is needed for
1486 // RVE.
1487 // FIXME: Can we increase the stack size to a multiple of 16 instead?
1488 uint64_t StackAdj =
1489 std::min(alignDown(StackSize, 16), static_cast<uint64_t>(48));
1490 MBBI->getOperand(1).setImm(StackAdj);
1491 StackSize -= StackAdj;
1492
1493 if (StackSize != 0)
1494 deallocateStack(MF, MBB, MBBI, DL, StackSize,
1495 /*stack_adj of cm.pop instr*/ RealStackSize - StackSize);
1496
1497 auto NextI = next_nodbg(MBBI, MBB.end());
1498 if (NextI == MBB.end() || NextI->getOpcode() != RISCV::PseudoRET) {
1499 ++MBBI;
1500 if (NeedsDwarfCFI) {
1501 CFIBuilder.setInsertPoint(MBBI);
1502
1503 for (const CalleeSavedInfo &CS : getPushOrLibCallsSavedInfo(MF, CSI))
1504 CFIBuilder.buildRestore(CS.getReg());
1505
1506 // Update CFA Offset. If this is a QCI interrupt function, there will
1507 // be a leftover offset which is deallocated by `QC.C.MILEAVERET`,
1508 // otherwise getQCIInterruptStackSize() will be 0.
1509 CFIBuilder.buildDefCFAOffset(RVFI->getQCIInterruptStackSize());
1510 }
1511 }
1512 }
1513
1514 // Deallocate stack if StackSize isn't a zero yet. If this is a QCI interrupt
1515 // function, there will be a leftover offset which is deallocated by
1516 // `QC.C.MILEAVERET`, otherwise getQCIInterruptStackSize() will be 0.
1517 if (StackSize != 0)
1518 deallocateStack(MF, MBB, MBBI, DL, StackSize,
1519 RVFI->getQCIInterruptStackSize());
1520
1521 // Emit epilogue for shadow call stack.
1522 emitSCSEpilogue(MF, MBB, MBBI, DL);
1523
1524 // SiFive CLIC needs to swap `sf.mscratchcsw` into `sp`
1526}
1527
1529 MCRegister Reg) {
1530 if (RISCV::GPRRegClass.contains(Reg))
1531 return Reg;
1532
1533 std::array<TargetRegisterClass const *, 2> RegisterClasses = {
1534 &RISCV::GPRF16RegClass, &RISCV::GPRF32RegClass};
1535 std::array<unsigned, 2> SubIdx = {RISCV::sub_16, RISCV::sub_32};
1536
1537 for (auto [RegClass, SubReg] : zip(RegisterClasses, SubIdx)) {
1538 if (RegClass->contains(Reg)) {
1539 if (MCRegister Super =
1540 TRI.getMatchingSuperReg(Reg, SubReg, &RISCV::GPRRegClass))
1541 return Super;
1542 }
1543 }
1544
1546 "getPhysicalGPR called with unsupported register");
1547}
1548
1550 const TargetRegisterInfo &TRI,
1551 MCRegister Reg) {
1552 if (!STI.hasStdExtF())
1553 return MCRegister();
1554
1555 TargetRegisterClass const *LargestFPRegClass = STI.getLargestFPRegClass();
1556 assert(LargestFPRegClass);
1557
1558 if (LargestFPRegClass->contains(Reg))
1559 return Reg;
1560
1561 std::array<TargetRegisterClass const *, 3> RegisterClasses = {
1562 &RISCV::FPR16RegClass, &RISCV::FPR32RegClass, &RISCV::FPR64RegClass};
1563 std::array<unsigned, 3> SubIdx = {RISCV::sub_16, RISCV::sub_32,
1564 RISCV::sub_64};
1565
1566 for (auto [RegClass, SubReg] : zip(RegisterClasses, SubIdx)) {
1567 if (RegClass->contains(Reg)) {
1568 if (MCRegister Super =
1569 TRI.getMatchingSuperReg(Reg, SubReg, LargestFPRegClass))
1570 return Super;
1571 }
1572 }
1573
1574 // Reg is bigger than what's currently available for the target, we can ignore
1575 // it.
1576 return MCRegister();
1577}
1578
1579void RISCVFrameLowering::emitZeroCallUsedRegs(BitVector RegsToZero,
1581 RegScavenger *RS) const {
1582 // Insertion point.
1584
1585 // Fake a debug loc.
1586 DebugLoc DL;
1587 if (MBBI != MBB.end())
1588 DL = MBBI->getDebugLoc();
1589
1590 const MachineFunction &MF = *MBB.getParent();
1591 const RISCVRegisterInfo &TRI = *STI.getRegisterInfo();
1592 const RISCVInstrInfo &TII = *STI.getInstrInfo();
1593
1594 BitVector FinalRegsToZero(TRI.getNumRegs());
1595
1596 bool HasVRegister = false;
1597
1598 for (MCRegister Reg : RegsToZero.set_bits()) {
1599 if (TRI.isGeneralPurposeRegister(MF, Reg)) {
1600 FinalRegsToZero.set(getPhysicalGPR(TRI, Reg).id());
1601 } else if (RISCV::GPRPairRegClass.contains(Reg)) {
1602 FinalRegsToZero.set(
1603 getPhysicalGPR(TRI, TRI.getSubReg(Reg, RISCV::sub_gpr_even)).id());
1604 FinalRegsToZero.set(
1605 getPhysicalGPR(TRI, TRI.getSubReg(Reg, RISCV::sub_gpr_odd)).id());
1606 } else if (TRI.isFPRegister(Reg)) {
1607 if (MCRegister MaybeReg = getLargestFPRegisterOrZero(STI, TRI, Reg))
1608 FinalRegsToZero.set(MaybeReg.id());
1610 TRI.getMinimalPhysRegClass(Reg))) {
1611 if (!STI.hasVInstructions())
1612 continue;
1613 HasVRegister = true;
1614
1615 for (MCRegister SubReg : TRI.subregs_inclusive(Reg)) {
1616 if (TRI.subregs(SubReg).empty())
1617 FinalRegsToZero.set(SubReg.id());
1618 }
1619 }
1620 }
1621
1622 if (HasVRegister) {
1623 RISCVVType::VLMUL VLMUL = RISCVVType::encodeLMUL(1, /*Fractional=*/false);
1624 unsigned VTypeImm = RISCVVType::encodeVTYPE(
1625 VLMUL, /*SEW=*/32, /*TailAgnostic=*/true, /*MaskAgnostic=*/true);
1626
1627 MCRegister TemporaryReg = RISCV::NoRegister;
1628 for (MCRegister Reg : FinalRegsToZero.set_bits()) {
1629 if (TRI.isGeneralPurposeRegister(MF, Reg)) {
1630 TemporaryReg = Reg;
1631 break;
1632 }
1633 }
1634
1635 if (TemporaryReg == RISCV::NoRegister) {
1636 RS->enterBasicBlockEnd(MBB);
1637 TemporaryReg = RS->scavengeRegisterBackwards(RISCV::GPRRegClass, MBBI,
1638 /*RestoreAfter=*/false,
1639 /*SPAdj=*/0);
1640 }
1641
1642 if (MBB.getParent()
1643 ->getFunction()
1644 .getFnAttribute("zero-call-used-regs")
1645 .getValueAsString() == "used")
1646 FinalRegsToZero.set(TemporaryReg.id());
1647
1648 BuildMI(MBB, MBBI, DL, TII.get(RISCV::VSETVLI), TemporaryReg)
1649 .addReg(RISCV::X0)
1650 .addImm(VTypeImm)
1651 .addReg(RISCV::VL, RegState::ImplicitDefine)
1652 .addReg(RISCV::VTYPE, RegState::ImplicitDefine);
1653 }
1654
1655 for (MCRegister Reg : FinalRegsToZero.set_bits())
1656 TII.buildClearRegister(Reg, MBB, MBBI, DL);
1657}
1658
1661 Register &FrameReg) const {
1662 const MachineFrameInfo &MFI = MF.getFrameInfo();
1664 const auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
1665
1666 // Callee-saved registers should be referenced relative to the stack
1667 // pointer (positive offset), otherwise use the frame pointer (negative
1668 // offset).
1669 const auto &CSI = getUnmanagedCSI(MF, MFI.getCalleeSavedInfo(),
1670 STI.preferAscendingLoadStore());
1671 int MinCSFI = 0;
1672 int MaxCSFI = -1;
1674 auto StackID = MFI.getStackID(FI);
1675
1676 assert((StackID == TargetStackID::Default ||
1677 StackID == TargetStackID::ScalableVector) &&
1678 "Unexpected stack ID for the frame object.");
1679 if (StackID == TargetStackID::Default) {
1680 assert(getOffsetOfLocalArea() == 0 && "LocalAreaOffset is not 0!");
1682 MFI.getOffsetAdjustment());
1683 } else if (StackID == TargetStackID::ScalableVector) {
1685 }
1686
1687 uint64_t FirstSPAdjustAmount = getFirstSPAdjustAmount(MF);
1688
1689 if (CSI.size()) {
1690 MinCSFI = std::min(CSI.front().getFrameIdx(), CSI.back().getFrameIdx());
1691 MaxCSFI = std::max(CSI.front().getFrameIdx(), CSI.back().getFrameIdx());
1692 }
1693
1694 bool IsInterruptCSR = RVFI->isSiFivePreemptibleInterrupt(MF) &&
1695 (FI == RVFI->getInterruptCSRFrameIndex(0) ||
1696 FI == RVFI->getInterruptCSRFrameIndex(1));
1697 if ((FI >= MinCSFI && FI <= MaxCSFI) || IsInterruptCSR) {
1698 FrameReg = SPReg;
1699
1700 if (FirstSPAdjustAmount)
1701 Offset += StackOffset::getFixed(FirstSPAdjustAmount);
1702 else
1704 return Offset;
1705 }
1706
1707 if (RI->hasStackRealignment(MF) && !MFI.isFixedObjectIndex(FI)) {
1708 // If the stack was realigned, the frame pointer is set in order to allow
1709 // SP to be restored, so we need another base register to record the stack
1710 // after realignment.
1711 // |--------------------------| --
1712 // | callee-allocated save | | <----|
1713 // | area for register varargs| | |
1714 // |--------------------------| <-- FP |
1715 // | callee-saved registers | | |
1716 // |--------------------------| -- |
1717 // | realignment (the size of | | |
1718 // | this area is not counted | | |
1719 // | in MFI.getStackSize()) | | |
1720 // |--------------------------| -- |-- MFI.getStackSize()
1721 // | RVV alignment padding | | |
1722 // | (not counted in | | |
1723 // | MFI.getStackSize() but | | |
1724 // | counted in | | |
1725 // | RVFI.getRVVStackSize()) | | |
1726 // |--------------------------| -- |
1727 // | RVV objects | | |
1728 // | (not counted in | | |
1729 // | MFI.getStackSize()) | | |
1730 // |--------------------------| -- |
1731 // | padding before RVV | | |
1732 // | (not counted in | | |
1733 // | MFI.getStackSize() or in | | |
1734 // | RVFI.getRVVStackSize()) | | |
1735 // |--------------------------| -- |
1736 // | scalar local variables | | <----'
1737 // |--------------------------| -- <-- BP (if var sized objects present)
1738 // | VarSize objects | |
1739 // |--------------------------| -- <-- SP
1740 if (hasBP(MF)) {
1741 FrameReg = RISCVABI::getBPReg();
1742 } else {
1743 // VarSize objects must be empty in this case!
1744 assert(!MFI.hasVarSizedObjects());
1745 FrameReg = SPReg;
1746 }
1747 } else if (!RI->hasStackRealignment(MF)) {
1748 // Note: Keeping the following as multiple 'if' statements rather than
1749 // merging to a single expression for readability.
1750 if (!hasFP(MF)) {
1751 // No FP available, must use SP.
1752 FrameReg = SPReg;
1753 } else {
1754 FrameReg = FPReg;
1755 // SP-relative addressing is only valid when SP is stable throughout
1756 // the function body: no dynamic SP adjustments for outgoing call args,
1757 // no variable-sized objects, and no RVV scalable stack regions.
1758 // hasReservedCallFrame() conservatively encompasses all these checks.
1759 if (hasReservedCallFrame(MF)) {
1760 // Both FP and SP are candidates.
1761 // Prefer SP when the SP-relative offset fits in the compressed
1762 // instruction immediate range.
1763 int64_t SPOff = Offset.getFixed() + MFI.getStackSize();
1764 int64_t CLWSPMaxOffset = 252;
1765 int64_t CLDSPMaxOffset = 504;
1766 int64_t SPThreshold = STI.is64Bit() ? CLDSPMaxOffset : CLWSPMaxOffset;
1767 if (SPOff >= 0 && SPOff <= SPThreshold)
1768 FrameReg = SPReg;
1769 }
1770 }
1771 } else {
1772 assert(RI->hasStackRealignment(MF) && MFI.isFixedObjectIndex(FI) &&
1773 "Expected fixed object with stack realignment");
1774 assert(hasFP(MF) && "Re-aligned stack must have frame pointer");
1775 FrameReg = FPReg;
1776 }
1777
1778 if (FrameReg == FPReg) {
1779 Offset += StackOffset::getFixed(RVFI->getVarArgsSaveSize());
1780 // When using FP to access scalable vector objects, we need to minus
1781 // the frame size.
1782 //
1783 // |--------------------------| --
1784 // | callee-allocated save | |
1785 // | area for register varargs| |
1786 // |--------------------------| | -- <-- FP
1787 // | callee-saved registers | |
1788 // |--------------------------| | MFI.getStackSize()
1789 // | scalar local variables | |
1790 // |--------------------------| -- (Offset of RVV objects is from here.)
1791 // | RVV objects |
1792 // |--------------------------|
1793 // | VarSize objects |
1794 // |--------------------------| <-- SP
1795 if (StackID == TargetStackID::ScalableVector) {
1796 assert(!RI->hasStackRealignment(MF) &&
1797 "Can't index across variable sized realign");
1798 // We don't expect any extra RVV alignment padding, as the stack size
1799 // and RVV object sections should be correct aligned in their own
1800 // right.
1802 "Inconsistent stack layout");
1804 }
1805 return Offset;
1806 }
1807
1808 // This case handles indexing off both SP and BP.
1809 // If indexing off SP, there must not be any var sized objects
1810 assert(FrameReg == RISCVABI::getBPReg() || !MFI.hasVarSizedObjects());
1811
1812 // When using SP to access frame objects, we need to add RVV stack size.
1813 //
1814 // |--------------------------| --
1815 // | callee-allocated save | | <----|
1816 // | area for register varargs| | |
1817 // |--------------------------| | | <-- FP
1818 // | callee-saved registers | | |
1819 // |--------------------------| -- |
1820 // | RVV alignment padding | | |
1821 // | (not counted in | | |
1822 // | MFI.getStackSize() but | | |
1823 // | counted in | | |
1824 // | RVFI.getRVVStackSize()) | | |
1825 // |--------------------------| -- |
1826 // | RVV objects | | |-- MFI.getStackSize()
1827 // | (not counted in | | |
1828 // | MFI.getStackSize()) | | |
1829 // |--------------------------| -- |
1830 // | padding before RVV | | |
1831 // | (not counted in | | |
1832 // | MFI.getStackSize()) | | |
1833 // |--------------------------| -- |
1834 // | scalar local variables | | <----'
1835 // |--------------------------| -- <-- BP (if var sized objects present)
1836 // | VarSize objects | |
1837 // |--------------------------| -- <-- SP
1838 //
1839 // The total amount of padding surrounding RVV objects is described by
1840 // RVV->getRVVPadding() and it can be zero. It allows us to align the RVV
1841 // objects to the required alignment.
1842 if (MFI.getStackID(FI) == TargetStackID::Default) {
1843 if (MFI.isFixedObjectIndex(FI)) {
1844 assert(!RI->hasStackRealignment(MF) &&
1845 "Can't index across variable sized realign");
1847 RVFI->getRVVStackSize());
1848 } else {
1850 }
1851 } else if (MFI.getStackID(FI) == TargetStackID::ScalableVector) {
1852 // Ensure the base of the RVV stack is correctly aligned: add on the
1853 // alignment padding.
1854 int64_t ScalarLocalVarSize =
1855 MFI.getStackSize() - RVFI->getCalleeSavedStackSize() -
1856 RVFI->getVarArgsSaveSize() + RVFI->getRVVPadding();
1857 Offset += StackOffset::get(ScalarLocalVarSize, RVFI->getRVVStackSize());
1858 }
1859 return Offset;
1860}
1861
1863 const Register &Reg) {
1864 MCRegister BaseReg = TRI.getSubReg(Reg, RISCV::sub_vrm1_0);
1865 // If it's not a grouped vector register, it doesn't have subregister, so
1866 // the base register is just itself.
1867 if (!BaseReg.isValid())
1868 BaseReg = Reg;
1869 return BaseReg;
1870}
1871
1873 BitVector &SavedRegs,
1874 RegScavenger *RS) const {
1876
1877 // In TargetFrameLowering::determineCalleeSaves, any vector register is marked
1878 // as saved if any of its subregister is clobbered, this is not correct in
1879 // vector registers. We only want the vector register to be marked as saved
1880 // if all of its subregisters are clobbered.
1881 // For example:
1882 // Original behavior: If v24 is marked, v24m2, v24m4, v24m8 are also marked.
1883 // Correct behavior: v24m2 is marked only if v24 and v25 are marked.
1884 MachineRegisterInfo &MRI = MF.getRegInfo();
1885 const MCPhysReg *CSRegs = MRI.getCalleeSavedRegs();
1886 const RISCVRegisterInfo &TRI = *STI.getRegisterInfo();
1887 for (unsigned i = 0; CSRegs[i]; ++i) {
1888 unsigned CSReg = CSRegs[i];
1889 // Only vector registers need special care.
1890 if (!RISCV::VRRegClass.contains(getRVVBaseRegister(TRI, CSReg)))
1891 continue;
1892
1893 SavedRegs.reset(CSReg);
1894
1895 auto SubRegs = TRI.subregs(CSReg);
1896 // Set the register and all its subregisters.
1897 if (!MRI.def_empty(CSReg) || MRI.getUsedPhysRegsMask().test(CSReg)) {
1898 SavedRegs.set(CSReg);
1899 for (unsigned Reg : SubRegs)
1900 SavedRegs.set(Reg);
1901 }
1902
1903 }
1904
1905 // Unconditionally spill RA and FP only if the function uses a frame
1906 // pointer.
1907 if (hasFP(MF)) {
1908 SavedRegs.set(RAReg);
1909 SavedRegs.set(FPReg);
1910 }
1911 // Mark BP as used if function has dedicated base pointer.
1912 if (hasBP(MF))
1913 SavedRegs.set(RISCVABI::getBPReg());
1914
1915 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
1916 // X5 is used as a temporary for saving and restoring `mcause` and `mepc`.
1917 if (RVFI->isSiFivePreemptibleInterrupt(MF))
1918 SavedRegs.set(RISCV::X5);
1919
1920 // When using cm.push/pop we must save X27 if we save X26.
1921 if (RVFI->isPushable(MF) && SavedRegs.test(RISCV::X26))
1922 SavedRegs.set(RISCV::X27);
1923
1924 // For Zilsd on RV32, append GPRPair registers to the CSR list. This prevents
1925 // the need to create register sets for each abi which is a lot more complex.
1926 // Don't use Zilsd for callee-saved coalescing if the required alignment
1927 // exceeds the stack alignment or when Zcmp/Xqccmp or save/restore libcalls
1928 // are enabled.
1929 bool UseZilsd = !STI.is64Bit() && STI.hasStdExtZilsd() &&
1930 STI.getZilsdAlign() <= getStackAlign() &&
1931 !RVFI->isPushable(MF) && !RVFI->useSaveRestoreLibCalls(MF);
1932 if (UseZilsd) {
1935 for (unsigned i = 0; CSRegs[i]; ++i) {
1936 NewCSRs.push_back(CSRegs[i]);
1937 CSRSet.insert(CSRegs[i]);
1938 }
1939
1940 // Append GPRPair registers for pairs where both sub-registers are in CSR
1941 // list. Iterate through all GPRPairs and check if both sub-regs are CSRs.
1942 for (MCPhysReg Pair : RISCV::GPRPairRegClass) {
1943 // Do not append a pair that's already in the CSR list.
1944 if (CSRSet.contains(Pair))
1945 continue;
1946 MCRegister EvenReg = TRI.getSubReg(Pair, RISCV::sub_gpr_even);
1947 MCRegister OddReg = TRI.getSubReg(Pair, RISCV::sub_gpr_odd);
1948 if (CSRSet.contains(EvenReg.id()) && CSRSet.contains(OddReg.id())) {
1949 NewCSRs.push_back(Pair);
1950 CSRSet.insert(Pair);
1951 }
1952 }
1953
1954 MRI.setCalleeSavedRegs(NewCSRs);
1955 CSRegs = MRI.getCalleeSavedRegs();
1956 }
1957
1958 // Check if all subregisters are marked for saving. If so, set the super
1959 // register bit. For GPRPair, only check sub_gpr_even and sub_gpr_odd, not
1960 // aliases like X8_W or X8_H which are not set in SavedRegs.
1961 for (unsigned i = 0; CSRegs[i]; ++i) {
1962 MCRegister CSReg = CSRegs[i];
1963 bool CombineToSuperReg;
1964 if (RISCV::GPRPairRegClass.contains(CSReg)) {
1965 MCRegister EvenReg = TRI.getSubReg(CSReg, RISCV::sub_gpr_even);
1966 MCRegister OddReg = TRI.getSubReg(CSReg, RISCV::sub_gpr_odd);
1967 CombineToSuperReg =
1968 SavedRegs.test(EvenReg.id()) && SavedRegs.test(OddReg.id());
1969 // If s0(x8) is used as FP we can't generate load/store pair because it
1970 // breaks the frame chain.
1971 if (hasFP(MF) && CSReg == RISCV::X8_X9)
1972 CombineToSuperReg = false;
1973 } else {
1974 auto SubRegs = TRI.subregs(CSReg);
1975 CombineToSuperReg =
1976 !SubRegs.empty() && llvm::all_of(SubRegs, [&](unsigned Reg) {
1977 return SavedRegs.test(Reg);
1978 });
1979 }
1980
1981 if (CombineToSuperReg)
1982 SavedRegs.set(CSReg);
1983 }
1984
1985 // SiFive Preemptible Interrupt Handlers need additional frame entries
1987}
1988
1989std::pair<int64_t, Align>
1990RISCVFrameLowering::assignRVVStackObjectOffsets(MachineFunction &MF) const {
1991 MachineFrameInfo &MFI = MF.getFrameInfo();
1992 // Create a buffer of RVV objects to allocate.
1993 SmallVector<int, 8> ObjectsToAllocate;
1994 auto pushRVVObjects = [&](int FIBegin, int FIEnd) {
1995 for (int I = FIBegin, E = FIEnd; I != E; ++I) {
1996 unsigned StackID = MFI.getStackID(I);
1997 if (StackID != TargetStackID::ScalableVector)
1998 continue;
1999 if (MFI.isDeadObjectIndex(I))
2000 continue;
2001
2002 ObjectsToAllocate.push_back(I);
2003 }
2004 };
2005 // First push RVV Callee Saved object, then push RVV stack object
2006 std::vector<CalleeSavedInfo> &CSI = MF.getFrameInfo().getCalleeSavedInfo();
2007 const auto &RVVCSI = getRVVCalleeSavedInfo(MF, CSI);
2008 if (!RVVCSI.empty())
2009 pushRVVObjects(RVVCSI[0].getFrameIdx(),
2010 RVVCSI[RVVCSI.size() - 1].getFrameIdx() + 1);
2011 pushRVVObjects(0, MFI.getObjectIndexEnd() - RVVCSI.size());
2012
2013 // The minimum alignment is 16 bytes.
2014 Align RVVStackAlign(16);
2015 const auto &ST = MF.getSubtarget<RISCVSubtarget>();
2016
2017 if (!ST.hasVInstructions()) {
2018 assert(ObjectsToAllocate.empty() &&
2019 "Can't allocate scalable-vector objects without V instructions");
2020 return std::make_pair(0, RVVStackAlign);
2021 }
2022
2023 // Allocate all RVV locals and spills
2024 int64_t Offset = 0;
2025 for (int FI : ObjectsToAllocate) {
2026 // ObjectSize in bytes.
2027 int64_t ObjectSize = MFI.getObjectSize(FI);
2028 auto ObjectAlign =
2029 std::max(Align(RISCV::RVVBytesPerBlock), MFI.getObjectAlign(FI));
2030 // If the data type is the fractional vector type, reserve one vector
2031 // register for it.
2032 if (ObjectSize < RISCV::RVVBytesPerBlock)
2033 ObjectSize = RISCV::RVVBytesPerBlock;
2034 Offset = alignTo(Offset + ObjectSize, ObjectAlign);
2035 MFI.setObjectOffset(FI, -Offset);
2036 // Update the maximum alignment of the RVV stack section
2037 RVVStackAlign = std::max(RVVStackAlign, ObjectAlign);
2038 }
2039
2040 uint64_t StackSize = Offset;
2041
2042 // Ensure the alignment of the RVV stack. Since we want the most-aligned
2043 // object right at the bottom (i.e., any padding at the top of the frame),
2044 // readjust all RVV objects down by the alignment padding.
2045 // Stack size and offsets are multiples of vscale, stack alignment is in
2046 // bytes, we can divide stack alignment by minimum vscale to get a maximum
2047 // stack alignment multiple of vscale.
2048 auto VScale =
2049 std::max<uint64_t>(ST.getRealMinVLen() / RISCV::RVVBitsPerBlock, 1);
2050 if (auto RVVStackAlignVScale = RVVStackAlign.value() / VScale) {
2051 if (auto AlignmentPadding =
2052 offsetToAlignment(StackSize, Align(RVVStackAlignVScale))) {
2053 StackSize += AlignmentPadding;
2054 for (int FI : ObjectsToAllocate)
2055 MFI.setObjectOffset(FI, MFI.getObjectOffset(FI) - AlignmentPadding);
2056 }
2057 }
2058
2059 return std::make_pair(StackSize, RVVStackAlign);
2060}
2061
2063 // For RVV spill, scalable stack offsets computing requires up to two scratch
2064 // registers
2065 static constexpr unsigned ScavSlotsNumRVVSpillScalableObject = 2;
2066
2067 // For RVV spill, non-scalable stack offsets computing requires up to one
2068 // scratch register.
2069 static constexpr unsigned ScavSlotsNumRVVSpillNonScalableObject = 1;
2070
2071 // ADDI instruction's destination register can be used for computing
2072 // offsets. So Scalable stack offsets require up to one scratch register.
2073 static constexpr unsigned ScavSlotsADDIScalableObject = 1;
2074
2075 static constexpr unsigned MaxScavSlotsNumKnown =
2076 std::max({ScavSlotsADDIScalableObject, ScavSlotsNumRVVSpillScalableObject,
2077 ScavSlotsNumRVVSpillNonScalableObject});
2078
2079 unsigned MaxScavSlotsNum = 0;
2081 return false;
2082 for (const MachineBasicBlock &MBB : MF)
2083 for (const MachineInstr &MI : MBB) {
2084 bool IsRVVSpill = RISCV::isRVVSpill(MI);
2085 for (auto &MO : MI.operands()) {
2086 if (!MO.isFI())
2087 continue;
2088 bool IsScalableVectorID = MF.getFrameInfo().getStackID(MO.getIndex()) ==
2090 if (IsRVVSpill) {
2091 MaxScavSlotsNum = std::max(
2092 MaxScavSlotsNum, IsScalableVectorID
2093 ? ScavSlotsNumRVVSpillScalableObject
2094 : ScavSlotsNumRVVSpillNonScalableObject);
2095 } else if (MI.getOpcode() == RISCV::ADDI && IsScalableVectorID) {
2096 MaxScavSlotsNum =
2097 std::max(MaxScavSlotsNum, ScavSlotsADDIScalableObject);
2098 }
2099 }
2100 if (MaxScavSlotsNum == MaxScavSlotsNumKnown)
2101 return MaxScavSlotsNumKnown;
2102 }
2103 return MaxScavSlotsNum;
2104}
2105
2106static bool hasRVVFrameObject(const MachineFunction &MF) {
2107 // Originally, the function will scan all the stack objects to check whether
2108 // if there is any scalable vector object on the stack or not. However, it
2109 // causes errors in the register allocator. In issue 53016, it returns false
2110 // before RA because there is no RVV stack objects. After RA, it returns true
2111 // because there are spilling slots for RVV values during RA. It will not
2112 // reserve BP during register allocation and generate BP access in the PEI
2113 // pass due to the inconsistent behavior of the function.
2114 //
2115 // The function is changed to use hasVInstructions() as the return value. It
2116 // is not precise, but it can make the register allocation correct.
2117 //
2118 // FIXME: Find a better way to make the decision or revisit the solution in
2119 // D103622.
2120 //
2121 // Refer to https://github.com/llvm/llvm-project/issues/53016.
2122 return MF.getSubtarget<RISCVSubtarget>().hasVInstructions();
2123}
2124
2126 const RISCVInstrInfo &TII) {
2127 unsigned FnSize = 0;
2128 for (auto &MBB : MF) {
2129 for (auto &MI : MBB) {
2130 // Far branches over 20-bit offset will be relaxed in branch relaxation
2131 // pass. In the worst case, conditional branches will be relaxed into
2132 // the following instruction sequence. Unconditional branches are
2133 // relaxed in the same way, with the exception that there is no first
2134 // branch instruction.
2135 //
2136 // foo
2137 // bne t5, t6, .rev_cond # `TII->getInstSizeInBytes(MI)` bytes
2138 // sd s11, 0(sp) # 4 bytes, or 2 bytes with Zca
2139 // jump .restore, s11 # 8 bytes
2140 // .rev_cond
2141 // bar
2142 // j .dest_bb # 4 bytes, or 2 bytes with Zca
2143 // .restore:
2144 // ld s11, 0(sp) # 4 bytes, or 2 bytes with Zca
2145 // .dest:
2146 // baz
2147 if (MI.isConditionalBranch())
2148 FnSize += TII.getInstSizeInBytes(MI);
2149 if (MI.isConditionalBranch() || MI.isUnconditionalBranch()) {
2150 if (MF.getSubtarget<RISCVSubtarget>().hasStdExtZca())
2151 FnSize += 2 + 8 + 2 + 2;
2152 else
2153 FnSize += 4 + 8 + 4 + 4;
2154 continue;
2155 }
2156
2157 FnSize += TII.getInstSizeInBytes(MI);
2158 }
2159 }
2160 return FnSize;
2161}
2162
2164 MachineFunction &MF, RegScavenger *RS) const {
2165 const RISCVRegisterInfo *RegInfo =
2166 MF.getSubtarget<RISCVSubtarget>().getRegisterInfo();
2167 const RISCVInstrInfo *TII = MF.getSubtarget<RISCVSubtarget>().getInstrInfo();
2168 MachineFrameInfo &MFI = MF.getFrameInfo();
2169 const TargetRegisterClass *RC = &RISCV::GPRRegClass;
2170 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
2171
2172 int64_t RVVStackSize;
2173 Align RVVStackAlign;
2174 std::tie(RVVStackSize, RVVStackAlign) = assignRVVStackObjectOffsets(MF);
2175
2176 RVFI->setRVVStackSize(RVVStackSize);
2177 RVFI->setRVVStackAlign(RVVStackAlign);
2178
2179 if (hasRVVFrameObject(MF)) {
2180 // Ensure the entire stack is aligned to at least the RVV requirement: some
2181 // scalable-vector object alignments are not considered by the
2182 // target-independent code.
2183 MFI.ensureMaxAlignment(RVVStackAlign);
2184 }
2185
2186 unsigned ScavSlotsNum = 0;
2187
2188 // estimateStackSize has been observed to under-estimate the final stack
2189 // size, so give ourselves wiggle-room by checking for stack size
2190 // representable an 11-bit signed field rather than 12-bits.
2191 if (!isInt<11>(MFI.estimateStackSize(MF)))
2192 ScavSlotsNum = 1;
2193
2194 // Far branches over 20-bit offset require a spill slot for scratch register.
2195 bool IsLargeFunction = !isInt<20>(estimateFunctionSizeInBytes(MF, *TII));
2196 if (IsLargeFunction)
2197 ScavSlotsNum = std::max(ScavSlotsNum, 1u);
2198
2199 // RVV loads & stores have no capacity to hold the immediate address offsets
2200 // so we must always reserve an emergency spill slot if the MachineFunction
2201 // contains any RVV spills.
2202 ScavSlotsNum = std::max(ScavSlotsNum, getScavSlotsNumForRVV(MF));
2203
2204 for (unsigned I = 0; I < ScavSlotsNum; I++) {
2205 int FI = MFI.CreateSpillStackObject(RegInfo->getSpillSize(*RC),
2206 RegInfo->getSpillAlign(*RC));
2207 RS->addScavengingFrameIndex(FI);
2208
2209 if (IsLargeFunction && RVFI->getBranchRelaxationScratchFrameIndex() == -1)
2210 RVFI->setBranchRelaxationScratchFrameIndex(FI);
2211 }
2212
2213 unsigned Size = RVFI->getReservedSpillsSize();
2214 for (const auto &Info : MFI.getCalleeSavedInfo()) {
2215 int FrameIdx = Info.getFrameIdx();
2216 if (FrameIdx < 0 || MFI.getStackID(FrameIdx) != TargetStackID::Default)
2217 continue;
2218
2219 Size += MFI.getObjectSize(FrameIdx);
2220 }
2221 RVFI->setCalleeSavedStackSize(Size);
2222}
2223
2224// Not preserve stack space within prologue for outgoing variables when the
2225// function contains variable size objects or there are vector objects accessed
2226// by the frame pointer.
2227// Let eliminateCallFramePseudoInstr preserve stack space for it.
2229 return !MF.getFrameInfo().hasVarSizedObjects() &&
2230 !(hasFP(MF) && hasRVVFrameObject(MF));
2231}
2232
2233// Eliminate ADJCALLSTACKDOWN, ADJCALLSTACKUP pseudo instructions.
2237 DebugLoc DL = MI->getDebugLoc();
2238
2239 if (!hasReservedCallFrame(MF)) {
2240 // If space has not been reserved for a call frame, ADJCALLSTACKDOWN and
2241 // ADJCALLSTACKUP must be converted to instructions manipulating the stack
2242 // pointer. This is necessary when there is a variable length stack
2243 // allocation (e.g. alloca), which means it's not possible to allocate
2244 // space for outgoing arguments from within the function prologue.
2245 int64_t Amount = MI->getOperand(0).getImm();
2246
2247 if (Amount != 0) {
2248 // Ensure the stack remains aligned after adjustment.
2249 Amount = alignSPAdjust(Amount);
2250
2251 if (MI->getOpcode() == RISCV::ADJCALLSTACKDOWN)
2252 Amount = -Amount;
2253
2254 const RISCVTargetLowering *TLI =
2255 MF.getSubtarget<RISCVSubtarget>().getTargetLowering();
2256 int64_t ProbeSize = TLI->getStackProbeSize(MF, getStackAlign());
2257 if (TLI->hasInlineStackProbe(MF) && -Amount >= ProbeSize) {
2258 // When stack probing is enabled, the decrement of SP may need to be
2259 // probed. We can handle both the decrement and the probing in
2260 // allocateStack.
2261 bool DynAllocation =
2262 MF.getInfo<RISCVMachineFunctionInfo>()->hasDynamicAllocation();
2263 allocateStack(MBB, MI, MF, -Amount, -Amount,
2264 needsDwarfCFI(MF) && !hasFP(MF),
2265 /*NeedProbe=*/true, ProbeSize, DynAllocation,
2267 inlineStackProbe(MF, MBB);
2268 } else {
2269 const RISCVRegisterInfo &RI = *STI.getRegisterInfo();
2272 }
2273 }
2274 }
2275
2276 return MBB.erase(MI);
2277}
2278
2279// We would like to split the SP adjustment to reduce prologue/epilogue
2280// as following instructions. In this way, the offset of the callee saved
2281// register could fit in a single store. Supposed that the first sp adjust
2282// amount is 2032.
2283// add sp,sp,-2032
2284// sw ra,2028(sp)
2285// sw s0,2024(sp)
2286// sw s1,2020(sp)
2287// sw s3,2012(sp)
2288// sw s4,2008(sp)
2289// add sp,sp,-64
2290uint64_t
2292 const auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
2293 const MachineFrameInfo &MFI = MF.getFrameInfo();
2294 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
2295 uint64_t StackSize = getStackSizeWithRVVPadding(MF);
2296
2297 // Disable SplitSPAdjust if save-restore libcall, push/pop or QCI interrupts
2298 // are used. The callee-saved registers will be pushed by the save-restore
2299 // libcalls, so we don't have to split the SP adjustment in this case.
2300 if (RVFI->getReservedSpillsSize())
2301 return 0;
2302
2303 // Return the FirstSPAdjustAmount if the StackSize can not fit in a signed
2304 // 12-bit and there exists a callee-saved register needing to be pushed.
2305 if (!isInt<12>(StackSize) && (CSI.size() > 0)) {
2306 // FirstSPAdjustAmount is chosen at most as (2048 - StackAlign) because
2307 // 2048 will cause sp = sp + 2048 in the epilogue to be split into multiple
2308 // instructions. Offsets smaller than 2048 can fit in a single load/store
2309 // instruction, and we have to stick with the stack alignment. 2048 has
2310 // 16-byte alignment. The stack alignment for RV32 and RV64 is 16 and for
2311 // RV32E it is 4. So (2048 - StackAlign) will satisfy the stack alignment.
2312 const uint64_t StackAlign = getStackAlign().value();
2313
2314 // Amount of (2048 - StackAlign) will prevent callee saved and restored
2315 // instructions be compressed, so try to adjust the amount to the largest
2316 // offset that stack compression instructions accept when target supports
2317 // compression instructions.
2318 if (STI.hasStdExtZca()) {
2319 // The compression extensions may support the following instructions:
2320 // riscv32: c.lwsp rd, offset[7:2] => 2^(6 + 2)
2321 // c.swsp rs2, offset[7:2] => 2^(6 + 2)
2322 // c.flwsp rd, offset[7:2] => 2^(6 + 2)
2323 // c.fswsp rs2, offset[7:2] => 2^(6 + 2)
2324 // riscv64: c.ldsp rd, offset[8:3] => 2^(6 + 3)
2325 // c.sdsp rs2, offset[8:3] => 2^(6 + 3)
2326 // c.fldsp rd, offset[8:3] => 2^(6 + 3)
2327 // c.fsdsp rs2, offset[8:3] => 2^(6 + 3)
2328 const uint64_t RVCompressLen = STI.getXLen() * 8;
2329 // Compared with amount (2048 - StackAlign), StackSize needs to
2330 // satisfy the following conditions to avoid using more instructions
2331 // to adjust the sp after adjusting the amount, such as
2332 // StackSize meets the condition (StackSize <= 2048 + RVCompressLen),
2333 // case1: Amount is 2048 - StackAlign: use addi + addi to adjust sp.
2334 // case2: Amount is RVCompressLen: use addi + addi to adjust sp.
2335 auto CanCompress = [&](uint64_t CompressLen) -> bool {
2336 if (StackSize <= 2047 + CompressLen ||
2337 (StackSize > 2048 * 2 - StackAlign &&
2338 StackSize <= 2047 * 2 + CompressLen) ||
2339 StackSize > 2048 * 3 - StackAlign)
2340 return true;
2341
2342 return false;
2343 };
2344 // In the epilogue, addi sp, sp, 496 is used to recover the sp and it
2345 // can be compressed(C.ADDI16SP, offset can be [-512, 496]), but
2346 // addi sp, sp, 512 can not be compressed. So try to use 496 first.
2347 const uint64_t ADDI16SPCompressLen = 496;
2348 if (STI.is64Bit() && CanCompress(ADDI16SPCompressLen))
2349 return ADDI16SPCompressLen;
2350 if (CanCompress(RVCompressLen))
2351 return RVCompressLen;
2352 }
2353 return 2048 - StackAlign;
2354 }
2355 return 0;
2356}
2357
2360 std::vector<CalleeSavedInfo> &CSI) const {
2361 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
2362 MachineFrameInfo &MFI = MF.getFrameInfo();
2363 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
2364
2365 // Preemptible Interrupts have two additional Callee-save Frame Indexes,
2366 // not tracked by `CSI`.
2367 if (RVFI->isSiFivePreemptibleInterrupt(MF)) {
2368 for (int I = 0; I < 2; ++I) {
2369 int FI = RVFI->getInterruptCSRFrameIndex(I);
2370 MFI.setIsCalleeSavedObjectIndex(FI, true);
2371 }
2372 }
2373
2374 // Early exit if no callee saved registers are modified!
2375 if (CSI.empty())
2376 return true;
2377
2378 if (RVFI->useQCIInterrupt(MF)) {
2379 RVFI->setQCIInterruptStackSize(QCIInterruptPushAmount);
2380 }
2381
2382 if (RVFI->isPushable(MF)) {
2383 // Determine how many GPRs we need to push and save it to RVFI.
2384 unsigned PushedRegNum = getNumPushPopRegs(CSI);
2385
2386 // `QC.C.MIENTER(.NEST)` will save `ra` and `s0`, so we should only push if
2387 // we want to push more than 2 registers. Otherwise, we should push if we
2388 // want to push more than 0 registers.
2389 unsigned OnlyPushIfMoreThan = RVFI->useQCIInterrupt(MF) ? 2 : 0;
2390 if (PushedRegNum > OnlyPushIfMoreThan) {
2391 RVFI->setRVPushRegs(PushedRegNum);
2392 RVFI->setRVPushStackSize(alignTo((STI.getXLen() / 8) * PushedRegNum, 16));
2393 }
2394 }
2395
2396 for (auto &CS : CSI) {
2397 MCRegister Reg = CS.getReg();
2398 const TargetRegisterClass *RC = RegInfo->getMinimalPhysRegClass(Reg);
2399 unsigned Size = RegInfo->getSpillSize(*RC);
2400
2401 if (RVFI->useQCIInterrupt(MF)) {
2402 const auto *FFI = llvm::find_if(FixedCSRFIQCIInterruptMap, [&](auto P) {
2403 return P.first == CS.getReg();
2404 });
2405 if (FFI != std::end(FixedCSRFIQCIInterruptMap)) {
2406 int64_t Offset = FFI->second * (int64_t)Size;
2407
2408 int FrameIdx = MFI.CreateFixedSpillStackObject(Size, Offset);
2409 assert(FrameIdx < 0);
2410 CS.setFrameIdx(FrameIdx);
2411 continue;
2412 }
2413 }
2414
2415 if (RVFI->useSaveRestoreLibCalls(MF) || RVFI->isPushable(MF)) {
2416 const auto *FII = llvm::find_if(
2417 FixedCSRFIMap, [&](MCPhysReg P) { return P == CS.getReg(); });
2418 unsigned RegNum = std::distance(std::begin(FixedCSRFIMap), FII);
2419
2420 if (FII != std::end(FixedCSRFIMap)) {
2421 int64_t Offset;
2422 if (RVFI->getPushPopKind(MF) ==
2424 Offset = -int64_t(RVFI->getRVPushRegs() - RegNum) * Size;
2425 else
2426 Offset = -int64_t(RegNum + 1) * Size;
2427
2428 if (RVFI->useQCIInterrupt(MF))
2430
2431 int FrameIdx = MFI.CreateFixedSpillStackObject(Size, Offset);
2432 assert(FrameIdx < 0);
2433 CS.setFrameIdx(FrameIdx);
2434 continue;
2435 }
2436 }
2437
2438 // For GPRPair registers, use 8-byte slots with required alignment by zilsd.
2439 if (!STI.is64Bit() && STI.hasStdExtZilsd() &&
2440 RISCV::GPRPairRegClass.contains(Reg)) {
2441 Align PairAlign = STI.getZilsdAlign();
2442 int FrameIdx = MFI.CreateStackObject(8, PairAlign, true);
2443 MFI.setIsCalleeSavedObjectIndex(FrameIdx, true);
2444 CS.setFrameIdx(FrameIdx);
2445 continue;
2446 }
2447
2448 // Not a fixed slot.
2449 Align Alignment = RegInfo->getSpillAlign(*RC);
2450 // We may not be able to satisfy the desired alignment specification of
2451 // the TargetRegisterClass if the stack alignment is smaller. Use the
2452 // min.
2453 Alignment = std::min(Alignment, getStackAlign());
2454 int FrameIdx = MFI.CreateStackObject(Size, Alignment, true);
2455 MFI.setIsCalleeSavedObjectIndex(FrameIdx, true);
2456 CS.setFrameIdx(FrameIdx);
2458 MFI.setStackID(FrameIdx, TargetStackID::ScalableVector);
2459 }
2460
2461 if (RVFI->useQCIInterrupt(MF)) {
2462 // Allocate a fixed object that covers the entire QCI stack allocation,
2463 // because there are gaps which are reserved for future use.
2464 MFI.CreateFixedSpillStackObject(
2465 QCIInterruptPushAmount, -static_cast<int64_t>(QCIInterruptPushAmount));
2466 }
2467
2468 if (RVFI->isPushable(MF)) {
2469 int64_t QCIOffset = RVFI->useQCIInterrupt(MF) ? QCIInterruptPushAmount : 0;
2470 // Allocate a fixed object that covers the full push.
2471 if (int64_t PushSize = RVFI->getRVPushStackSize())
2472 MFI.CreateFixedSpillStackObject(PushSize, -PushSize - QCIOffset);
2473 } else if (int LibCallRegs = getLibCallID(MF, CSI) + 1) {
2474 int64_t LibCallFrameSize =
2475 alignTo((STI.getXLen() / 8) * LibCallRegs, getStackAlign());
2476 MFI.CreateFixedSpillStackObject(LibCallFrameSize, -LibCallFrameSize);
2477 }
2478
2479 return true;
2480}
2481
2485 if (CSI.empty())
2486 return true;
2487
2488 MachineFunction *MF = MBB.getParent();
2489 const TargetInstrInfo &TII = *MF->getSubtarget().getInstrInfo();
2490 DebugLoc DL;
2491 if (MI != MBB.end() && !MI->isDebugInstr())
2492 DL = MI->getDebugLoc();
2493
2495 if (RVFI->useQCIInterrupt(*MF)) {
2496 // Emit QC.C.MIENTER(.NEST)
2497 BuildMI(
2498 MBB, MI, DL,
2499 TII.get(RVFI->getInterruptStackKind(*MF) ==
2501 ? RISCV::QC_C_MIENTER_NEST
2502 : RISCV::QC_C_MIENTER))
2504
2505 for (auto [Reg, _Offset] : FixedCSRFIQCIInterruptMap)
2506 MBB.addLiveIn(Reg);
2507 }
2508
2509 if (RVFI->isPushable(*MF)) {
2510 // Emit CM.PUSH with base StackAdj & evaluate Push stack
2511 unsigned PushedRegNum = RVFI->getRVPushRegs();
2512 if (PushedRegNum > 0) {
2513 // Use encoded number to represent registers to spill.
2514 unsigned Opcode = getPushOpcode(
2515 RVFI->getPushPopKind(*MF), hasFP(*MF) && !RVFI->useQCIInterrupt(*MF));
2516 unsigned RegEnc = RISCVZC::encodeRegListNumRegs(PushedRegNum);
2517 MachineInstrBuilder PushBuilder =
2518 BuildMI(MBB, MI, DL, TII.get(Opcode))
2520 PushBuilder.addImm(RegEnc);
2521 PushBuilder.addImm(0);
2522
2523 for (unsigned i = 0; i < PushedRegNum; i++)
2524 PushBuilder.addUse(FixedCSRFIMap[i], RegState::Implicit);
2525 }
2526 } else if (const char *SpillLibCall = getSpillLibCallName(*MF, CSI)) {
2527 // Add spill libcall via non-callee-saved register t0.
2528 MachineInstrBuilder NewMI =
2529 BuildMI(MBB, MI, DL, TII.get(RISCV::PseudoCALLReg), RISCV::X5)
2530 .addExternalSymbol(SpillLibCall, RISCVII::MO_CALL)
2532 .addUse(RISCV::X2, RegState::Implicit)
2533 .addDef(RISCV::X2, RegState::ImplicitDefine);
2534
2535 // Add registers spilled as implicit used.
2536 for (auto &CS : CSI)
2537 NewMI.addUse(CS.getReg(), RegState::Implicit);
2538 }
2539
2540 // Manually spill values not spilled by libcall & Push/Pop.
2541 const auto &UnmanagedCSI =
2542 getUnmanagedInterruptCSI(*MF, CSI, STI.preferAscendingLoadStore());
2543 const auto &RVVCSI = getRVVCalleeSavedInfo(*MF, CSI);
2544
2545 auto storeRegsToStackSlots = [&](ArrayRef<CalleeSavedInfo> CSInfo) {
2546 for (auto &CS : CSInfo) {
2547 // Insert the spill to the stack frame.
2548 MCRegister Reg = CS.getReg();
2549 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
2550 TII.storeRegToStackSlot(MBB, MI, Reg, !MBB.isLiveIn(Reg),
2551 CS.getFrameIdx(), RC, Register(),
2553 }
2554 };
2555 storeRegsToStackSlots(UnmanagedCSI);
2556 storeRegsToStackSlots(RVVCSI);
2557
2558 return true;
2559}
2560
2561static unsigned getCalleeSavedRVVNumRegs(const Register &BaseReg) {
2562 return RISCV::VRRegClass.contains(BaseReg) ? 1
2563 : RISCV::VRM2RegClass.contains(BaseReg) ? 2
2564 : RISCV::VRM4RegClass.contains(BaseReg) ? 4
2565 : 8;
2566}
2567
2568void RISCVFrameLowering::emitCalleeSavedRVVPrologCFI(
2571 const MachineFrameInfo &MFI = MF->getFrameInfo();
2572 RISCVMachineFunctionInfo *RVFI = MF->getInfo<RISCVMachineFunctionInfo>();
2573 const RISCVRegisterInfo &TRI = *STI.getRegisterInfo();
2574
2575 const auto &RVVCSI = getRVVCalleeSavedInfo(*MF, MFI.getCalleeSavedInfo());
2576 if (RVVCSI.empty())
2577 return;
2578
2579 uint64_t FixedSize = getStackSizeWithRVVPadding(*MF);
2580 if (!HasFP) {
2581 uint64_t ScalarLocalVarSize =
2582 MFI.getStackSize() - RVFI->getCalleeSavedStackSize() -
2583 RVFI->getVarArgsSaveSize() + RVFI->getRVVPadding();
2584 FixedSize -= ScalarLocalVarSize;
2585 }
2586
2587 CFIInstBuilder CFIBuilder(MBB, MI, MachineInstr::FrameSetup);
2588 for (auto &CS : RVVCSI) {
2589 // Insert the spill to the stack frame.
2590 int FI = CS.getFrameIdx();
2591 MCRegister BaseReg = getRVVBaseRegister(TRI, CS.getReg());
2592 unsigned NumRegs = getCalleeSavedRVVNumRegs(CS.getReg());
2593 for (unsigned i = 0; i < NumRegs; ++i) {
2594 CFIBuilder.insertCFIInst(createDefCFAOffset(
2595 TRI, BaseReg + i,
2596 StackOffset::get(-FixedSize, MFI.getObjectOffset(FI) / 8 + i)));
2597 }
2598 }
2599}
2600
2601void RISCVFrameLowering::emitCalleeSavedRVVEpilogCFI(
2604 const MachineFrameInfo &MFI = MF->getFrameInfo();
2605 const RISCVRegisterInfo &TRI = *STI.getRegisterInfo();
2606
2607 CFIInstBuilder CFIHelper(MBB, MI, MachineInstr::FrameDestroy);
2608 const auto &RVVCSI = getRVVCalleeSavedInfo(*MF, MFI.getCalleeSavedInfo());
2609 for (auto &CS : RVVCSI) {
2610 MCRegister BaseReg = getRVVBaseRegister(TRI, CS.getReg());
2611 unsigned NumRegs = getCalleeSavedRVVNumRegs(CS.getReg());
2612 for (unsigned i = 0; i < NumRegs; ++i)
2613 CFIHelper.buildRestore(BaseReg + i);
2614 }
2615}
2616
2620 if (CSI.empty())
2621 return true;
2622
2623 MachineFunction *MF = MBB.getParent();
2624 const TargetInstrInfo &TII = *MF->getSubtarget().getInstrInfo();
2625 DebugLoc DL;
2626 if (MI != MBB.end() && !MI->isDebugInstr())
2627 DL = MI->getDebugLoc();
2628
2629 // Manually restore values not restored by libcall & Push/Pop.
2630 // Reverse the restore order in epilog. In addition, the return
2631 // address will be restored first in the epilogue. It increases
2632 // the opportunity to avoid the load-to-use data hazard between
2633 // loading RA and return by RA. loadRegFromStackSlot can insert
2634 // multiple instructions.
2635 const auto &UnmanagedCSI =
2636 getUnmanagedInterruptCSI(*MF, CSI, STI.preferAscendingLoadStore());
2637 const auto &RVVCSI = getRVVCalleeSavedInfo(*MF, CSI);
2638
2639 auto loadRegFromStackSlot = [&](ArrayRef<CalleeSavedInfo> CSInfo) {
2640 for (auto &CS : CSInfo) {
2641 MCRegister Reg = CS.getReg();
2642 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
2643 TII.loadRegFromStackSlot(MBB, MI, Reg, CS.getFrameIdx(), RC, Register(),
2644 RISCV::NoSubRegister,
2646 assert(MI != MBB.begin() &&
2647 "loadRegFromStackSlot didn't insert any code!");
2648 }
2649 };
2650 loadRegFromStackSlot(RVVCSI);
2651 loadRegFromStackSlot(UnmanagedCSI);
2652
2654 if (RVFI->useQCIInterrupt(*MF)) {
2655 // Don't emit anything here because restoration is handled by
2656 // QC.C.MILEAVERET which we already inserted to return.
2657 assert(MI->getOpcode() == RISCV::QC_C_MILEAVERET &&
2658 "Unexpected QCI Interrupt Return Instruction");
2659 }
2660
2661 if (RVFI->isPushable(*MF)) {
2662 unsigned PushedRegNum = RVFI->getRVPushRegs();
2663 if (PushedRegNum > 0) {
2664 unsigned Opcode = getPopOpcode(RVFI->getPushPopKind(*MF));
2665 unsigned RegEnc = RISCVZC::encodeRegListNumRegs(PushedRegNum);
2666 MachineInstrBuilder PopBuilder =
2667 BuildMI(MBB, MI, DL, TII.get(Opcode))
2669 // Use encoded number to represent registers to restore.
2670 PopBuilder.addImm(RegEnc);
2671 PopBuilder.addImm(0);
2672
2673 for (unsigned i = 0; i < RVFI->getRVPushRegs(); i++)
2675 }
2676 } else if (const char *RestoreLibCall = getRestoreLibCallName(*MF, CSI)) {
2677 // Restore is not compatible with shadow stacks, so do the restore manually.
2678 // This ensures we still get the code size saving of `__riscv_save_<N>`.
2679 if (RVFI->hasShadowStack(*MF)) {
2680 loadRegFromStackSlot(CSI);
2681 return true;
2682 }
2683
2684 // Add restore libcall via tail call.
2685 MachineInstrBuilder NewMI =
2686 BuildMI(MBB, MI, DL, TII.get(RISCV::PseudoTAIL))
2687 .addExternalSymbol(RestoreLibCall, RISCVII::MO_CALL)
2689 .addDef(RISCV::X2, RegState::ImplicitDefine);
2690
2691 // Add registers restored as implicit defined.
2692 for (auto &CS : CSI)
2693 NewMI.addDef(CS.getReg(), RegState::ImplicitDefine);
2694
2695 // Remove trailing returns, since the terminator is now a tail call to the
2696 // restore function.
2697 if (MI != MBB.end() && MI->getOpcode() == RISCV::PseudoRET) {
2698 NewMI.getInstr()->copyImplicitOps(*MF, *MI);
2699 MI->eraseFromParent();
2700 }
2701 }
2702 return true;
2703}
2704
2706 // Keep the conventional code flow when not optimizing.
2707 if (MF.getFunction().hasOptNone())
2708 return false;
2709
2710 // QCI and SiFive CLIC interrupt entry sequences must precede all handler
2711 // code.
2712 const auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
2713 if (RVFI->useQCIInterrupt(MF) || RVFI->useSiFiveInterrupt(MF))
2714 return false;
2715
2716 return true;
2717}
2718
2720 MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB);
2721 const MachineFunction *MF = MBB.getParent();
2722 const auto *RVFI = MF->getInfo<RISCVMachineFunctionInfo>();
2723
2724 // Make sure VTYPE and VL are not live-in since we will use vsetvli in the
2725 // prologue to get the VLEN, and that will clobber these registers.
2726 //
2727 // We may do also check the stack contains objects with scalable vector type,
2728 // but this will require iterating over all the stack objects, but this may
2729 // not worth since the situation is rare, we could do further check in future
2730 // if we find it is necessary.
2731 if (STI.preferVsetvliOverReadVLENB() &&
2732 (MBB.isLiveIn(RISCV::VTYPE) || MBB.isLiveIn(RISCV::VL)))
2733 return false;
2734
2735 if (!RVFI->useSaveRestoreLibCalls(*MF))
2736 return true;
2737
2738 // Inserting a call to a __riscv_save libcall requires the use of the register
2739 // t0 (X5) to hold the return address. Therefore if this register is already
2740 // used we can't insert the call.
2741
2742 RegScavenger RS;
2743 RS.enterBasicBlock(*TmpMBB);
2744 return !RS.isRegUsed(RISCV::X5);
2745}
2746
2748 const MachineFunction *MF = MBB.getParent();
2749 MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB);
2750 const auto *RVFI = MF->getInfo<RISCVMachineFunctionInfo>();
2751
2752 // If we have a shadow stack, we don't use `__riscv_restore` libcalls.
2753 if (RVFI->hasShadowStack(*MF))
2754 return true;
2755
2756 if (!RVFI->useSaveRestoreLibCalls(*MF))
2757 return true;
2758
2759 // Using the __riscv_restore libcalls to restore CSRs requires a tail call.
2760 // This means if we still need to continue executing code within this function
2761 // the restore cannot take place in this basic block.
2762
2763 if (MBB.succ_size() > 1)
2764 return false;
2765
2766 MachineBasicBlock *SuccMBB =
2767 MBB.succ_empty() ? TmpMBB->getFallThrough() : *MBB.succ_begin();
2768
2769 // Doing a tail call should be safe if there are no successors, because either
2770 // we have a returning block or the end of the block is unreachable, so the
2771 // restore will be eliminated regardless.
2772 if (!SuccMBB)
2773 return true;
2774
2775 // The successor can only contain a return and debug instructions, since we
2776 // would effectively replace it with our own tail return at the end of this
2777 // block. The debug instructions would not execute on the tail-return path.
2778 return SuccMBB->isReturnBlock() &&
2779 llvm::count_if(SuccMBB->instrs(), [](const MachineInstr &MI) {
2780 return !MI.isDebugInstr();
2781 }) == 1;
2782}
2783
2787
2791
2792// Synthesize the probe loop.
2794 Register TargetReg, Register ScratchReg,
2795 bool IsRVV) {
2796 assert(TargetReg != RISCV::X2 && "New top of stack cannot already be in SP");
2797 assert(ScratchReg != RISCV::X2 && "Scratch register cannot be SP");
2798 assert(TargetReg != ScratchReg && "Target and scratch must be different");
2799
2800 MachineBasicBlock &MBB = *MBBI->getParent();
2801 MachineFunction &MF = *MBB.getParent();
2802
2803 auto &Subtarget = MF.getSubtarget<RISCVSubtarget>();
2804 const RISCVInstrInfo *TII = Subtarget.getInstrInfo();
2805 bool IsRV64 = Subtarget.is64Bit();
2806 Align StackAlign = Subtarget.getFrameLowering()->getStackAlign();
2807 const RISCVTargetLowering *TLI = Subtarget.getTargetLowering();
2808 uint64_t ProbeSize = TLI->getStackProbeSize(MF, StackAlign);
2809
2810 MachineFunction::iterator MBBInsertPoint = std::next(MBB.getIterator());
2811 MachineBasicBlock *LoopTestMBB =
2812 MF.CreateMachineBasicBlock(MBB.getBasicBlock());
2813 MF.insert(MBBInsertPoint, LoopTestMBB);
2814 MachineBasicBlock *ExitMBB = MF.CreateMachineBasicBlock(MBB.getBasicBlock());
2815 MF.insert(MBBInsertPoint, ExitMBB);
2817
2818 // ScratchReg = ProbeSize
2819 TII->movImm(MBB, MBBI, DL, ScratchReg, ProbeSize, Flags);
2820
2821 // LoopTest:
2822 // SUB SP, SP, ProbeSize
2823 BuildMI(*LoopTestMBB, LoopTestMBB->end(), DL, TII->get(RISCV::SUB), SPReg)
2824 .addReg(SPReg)
2825 .addReg(ScratchReg)
2826 .setMIFlags(Flags);
2827
2828 // s[d|w] zero, 0(sp)
2829 BuildMI(*LoopTestMBB, LoopTestMBB->end(), DL,
2830 TII->get(IsRV64 ? RISCV::SD : RISCV::SW))
2831 .addReg(RISCV::X0)
2832 .addReg(SPReg)
2833 .addImm(0)
2834 .setMIFlags(Flags);
2835
2836 if (IsRVV) {
2837 // SUB TargetReg, TargetReg, ProbeSize
2838 BuildMI(*LoopTestMBB, LoopTestMBB->end(), DL, TII->get(RISCV::SUB),
2839 TargetReg)
2840 .addReg(TargetReg)
2841 .addReg(ScratchReg)
2842 .setMIFlags(Flags);
2843
2844 // BGE TargetReg, ProbeSize, LoopTest
2845 BuildMI(*LoopTestMBB, LoopTestMBB->end(), DL, TII->get(RISCV::BGE))
2846 .addReg(TargetReg)
2847 .addReg(ScratchReg)
2848 .addMBB(LoopTestMBB)
2849 .setMIFlags(Flags);
2850
2851 } else {
2852 // BNE SP, TargetReg, LoopTest
2853 BuildMI(*LoopTestMBB, LoopTestMBB->end(), DL, TII->get(RISCV::BNE))
2854 .addReg(SPReg)
2855 .addReg(TargetReg)
2856 .addMBB(LoopTestMBB)
2857 .setMIFlags(Flags);
2858 }
2859
2860 ExitMBB->splice(ExitMBB->end(), &MBB, std::next(MBBI), MBB.end());
2862
2863 LoopTestMBB->addSuccessor(ExitMBB);
2864 LoopTestMBB->addSuccessor(LoopTestMBB);
2865 MBB.addSuccessor(LoopTestMBB);
2866 // Update liveins.
2867 fullyRecomputeLiveIns({ExitMBB, LoopTestMBB});
2868}
2869
2870void RISCVFrameLowering::inlineStackProbe(MachineFunction &MF,
2871 MachineBasicBlock &MBB) const {
2872 // Get the instructions that need to be replaced. We emit at most two of
2873 // these. Remember them in order to avoid complications coming from the need
2874 // to traverse the block while potentially creating more blocks.
2875 SmallVector<MachineInstr *, 4> ToReplace;
2876 for (MachineInstr &MI : MBB) {
2877 unsigned Opc = MI.getOpcode();
2878 if (Opc == RISCV::PROBED_STACKALLOC ||
2879 Opc == RISCV::PROBED_STACKALLOC_RVV) {
2880 ToReplace.push_back(&MI);
2881 }
2882 }
2883
2884 for (MachineInstr *MI : ToReplace) {
2885 if (MI->getOpcode() == RISCV::PROBED_STACKALLOC ||
2886 MI->getOpcode() == RISCV::PROBED_STACKALLOC_RVV) {
2889 Register TargetReg = MI->getOperand(0).getReg();
2890
2891 Register ScratchReg =
2892 findScratchNonCalleeSaveRegister(&MBB, RISCV::X7, TargetReg);
2893
2894 assert(ScratchReg.isValid() &&
2895 "No available scratch register for stack probe loop");
2896
2897 emitStackProbeInline(MBBI, DL, TargetReg, ScratchReg,
2898 (MI->getOpcode() == RISCV::PROBED_STACKALLOC_RVV));
2900 }
2901 }
2902}
2903
2905 return 0;
2906}
2907
2910 return RISCV::X2;
2911}
2912
2913// On 64-bit systems the fixed stack can hold INT64_MAX bytes, since
2914// stack-offset calculation is done in 2s-complement.
2915// NOTE: In theory a register can hold any 64-bit number, so this constraint
2916// might be relaxed to UINT64_MAX in the future, if anyone actually needs
2917// that.
2919 return STI.is64Bit() ? INT64_MAX : UINT32_MAX;
2920}
static void getLiveRegsForEntryMBB(LivePhysRegs &LiveRegs, const MachineBasicBlock &MBB)
static MCCFIInstruction createDefCFAExpression(const TargetRegisterInfo &TRI, unsigned Reg, const StackOffset &Offset)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned uint64_t
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
This file contains constants used for implementing Dwarf debug support.
const HexagonInstrInfo * TII
static void emitSCSPrologue(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, const DebugLoc &DL)
static void emitSCSEpilogue(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, const DebugLoc &DL)
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)
static void emitStackProbeInline(MachineBasicBlock::iterator MBBI, DebugLoc DL, Register TargetReg)
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
#define P(N)
static constexpr uint64_t QCIInterruptPushAmount
static void emitSiFiveCLICStackSwap(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, const DebugLoc &DL, MachineInstr::MIFlag FrameFlag)
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 > getUnmanagedInterruptCSI(const MachineFunction &MF, const std::vector< CalleeSavedInfo > &CSI, bool ReverseOrder=false)
static void appendScalableVectorExpression(const TargetRegisterInfo &TRI, SmallVectorImpl< char > &Expr, StackOffset Offset, llvm::raw_string_ostream &Comment)
static SmallVector< CalleeSavedInfo, 8 > getQCISavedInfo(const MachineFunction &MF, const std::vector< CalleeSavedInfo > &CSI)
static SmallVector< CalleeSavedInfo, 8 > getRVVCalleeSavedInfo(const MachineFunction &MF, const std::vector< CalleeSavedInfo > &CSI)
static SmallVector< CalleeSavedInfo, 8 > getUnmanagedCSI(const MachineFunction &MF, const std::vector< CalleeSavedInfo > &CSI, bool ReverseOrder=false)
static void emitSiFiveCLICPreemptibleRestores(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, CFIInstBuilder &CFIBuilder, const DebugLoc &DL)
static bool isPop(unsigned Opcode)
static unsigned getCalleeSavedRVVNumRegs(const Register &BaseReg)
static MCCFIInstruction createDefCFAOffset(const TargetRegisterInfo &TRI, Register Reg, StackOffset Offset)
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 MCRegister getLargestFPRegisterOrZero(const RISCVSubtarget &STI, const TargetRegisterInfo &TRI, MCRegister Reg)
static const MCPhysReg FixedCSRFIMap[]
static int getSiFiveCLICScratchFrameIndex(const MachineFunction &MF)
static unsigned getNumPushPopRegs(const std::vector< CalleeSavedInfo > &CSI)
static MCRegister getPhysicalGPR(const TargetRegisterInfo &TRI, MCRegister Reg)
static unsigned getScavSlotsNumForRVV(MachineFunction &MF)
This file declares the machine register scavenger class.
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:484
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
LLVM_ABI StringRef getValueAsString() const
Return the attribute's value as a string.
bool test(unsigned Idx) const
Returns true if bit Idx is set.
Definition BitVector.h:482
BitVector & reset()
Reset all bits in the bitvector.
Definition BitVector.h:409
BitVector & set()
Set all bits in the bitvector.
Definition BitVector.h:366
iterator_range< const_set_bits_iterator > set_bits() const
Definition BitVector.h:159
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:126
Diagnostic information for unsupported feature in backend.
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
Definition Function.cpp:769
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition Function.h:273
bool hasOptNone() const
Do not optimize this function (-O0).
Definition Function.h:686
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition Function.cpp:356
LLVM_ABI void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
A set of physical registers with utility functions to track liveness when walking backward/forward th...
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:756
bool contains(MCRegister Reg) const
contains - Return true if the specified register is included in this register class.
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
constexpr unsigned id() const
Definition MCRegister.h:82
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...
LLVM_ABI iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
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 void eraseFromParent()
This method unlinks 'this' from the containing function and deletes it.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
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 '...
MachineInstrBundleIterator< MachineInstr > iterator
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's frame 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 alignment of this function's frame.
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 uint64_t estimateStackSize(const MachineFunction &MF) const
Estimate and return the size of the stack frame.
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
LLVM_ABI int CreateSpillStackObject(uint64_t Size, Align Alignment, TargetStackID::Value StackID=TargetStackID::Default)
Create a new statically sized stack object that represents a spill slot, returning a nonnegative iden...
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.
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.
BasicBlockListType::iterator iterator
bool disableFramePointerElim() const
Returns true if frame pointer elimination should be disabled for this function.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
const MachineBasicBlock & front() const
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *BB=nullptr, std::optional< UniqueBBID > BBID=std::nullopt)
CreateMachineInstr - Allocate a new MachineInstr.
void insert(iterator MBBI, MachineBasicBlock *MBB)
const MachineInstrBuilder & addExternalSymbol(const char *FnName, unsigned TargetFlags=0) const
const MachineInstrBuilder & addUse(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register use operand.
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & setMIFlag(MachineInstr::MIFlag Flag) const
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
const MachineInstrBuilder & addDef(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register definition operand.
const MachineInstrBuilder & setMIFlags(unsigned Flags) const
MachineInstr * getInstr() const
If conversion operators fail, use this method to get the MachineInstr explicitly.
Representation of each machine instruction.
LLVM_ABI void copyImplicitOps(MachineFunction &MF, const MachineInstr &MI)
Copy implicit register operands from specified instruction to this instruction.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
const BitVector & getUsedPhysRegsMask() const
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...
bool def_empty(Register RegNo) const
def_empty - Return true if there are no instructions defining the specified register (it may be live-...
LLVM_ABI const MCPhysReg * getCalleeSavedRegs() const
Returns list of callee saved registers.
LLVM_ABI void setCalleeSavedRegs(ArrayRef< MCPhysReg > CSRs)
Sets the updated Callee Saved Registers list.
Represent a mutable reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:294
bool assignCalleeSavedSpillSlots(MachineFunction &MF, const TargetRegisterInfo *TRI, std::vector< CalleeSavedInfo > &CSI) const override
assignCalleeSavedSpillSlots - Allows target to override spill slot assignment logic.
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.
uint64_t getStackThreshold() const override
getStackThreshold - Return the maximum stack size
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 hasFPImpl(const MachineFunction &MF) const override
Register findScratchNonCalleeSaveRegister(MachineBasicBlock *MBB, Register PreferredReg, Register DontUseReg=Register()) const
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,...
Register getInitialCFARegister(const MachineFunction &MF) const override
Return initial CFA register value i.e.
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.
int getInitialCFAOffset(const MachineFunction &MF) const override
Return initial CFA offset value i.e.
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 useSiFiveInterrupt(const MachineFunction &MF) const
bool isSiFivePreemptibleInterrupt(const MachineFunction &MF) const
PushPopKind getPushPopKind(const MachineFunction &MF) const
bool hasShadowStack(const MachineFunction &MF) const
bool useSaveRestoreLibCalls(const MachineFunction &MF) const
bool useQCIInterrupt(const MachineFunction &MF) const
TargetRegisterClass const * getLargestFPRegClass() const
bool hasVInstructions() const
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
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isValid() const
Definition Register.h:112
Represents a location in source code.
Definition SMLoc.h:22
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition SmallSet.h:134
bool contains(const T &V) const
Check if the SmallSet contains the given element.
Definition SmallSet.h:229
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition SmallSet.h:184
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.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StackOffset holds a fixed and a scalable offset in bytes.
Definition TypeSize.h:30
int64_t getFixed() const
Returns the fixed component of the stack.
Definition TypeSize.h:46
int64_t getScalable() const
Returns the scalable component of the stack.
Definition TypeSize.h:49
static StackOffset get(int64_t Fixed, int64_t Scalable)
Definition TypeSize.h:41
static StackOffset getScalable(int64_t Scalable)
Definition TypeSize.h:40
static StackOffset getFixed(int64_t Fixed)
Definition TypeSize.h:39
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
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...
TargetFrameLowering(StackDirection D, Align StackAl, int LAO, Align TransAl=Align(1), bool StackReal=true)
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.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
bool hasStackRealignment(const MachineFunction &MF) const
True if stack realignment is required and still possible.
virtual const TargetInstrInfo * getInstrInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
self_iterator getIterator()
Definition ilist_node.h:123
A raw_ostream that writes to an std::string.
#define INT64_MAX
Definition DataTypes.h:71
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
@ GHC
Used by the Glasgow Haskell Compiler (GHC).
Definition CallingConv.h:50
MCRegister getBPReg()
MCRegister getSCSPReg()
static VLMUL encodeLMUL(unsigned LMUL, bool Fractional)
LLVM_ABI unsigned encodeVTYPE(VLMUL VLMUL, unsigned SEW, bool TailAgnostic, bool MaskAgnostic, bool AltFmt=false)
static unsigned encodeRegListNumRegs(unsigned NumRegs)
static constexpr unsigned RVVBitsPerBlock
bool isRVVSpill(const MachineInstr &MI)
static constexpr unsigned RVVBytesPerBlock
BaseReg
Stack frame base register. Bit 0 of FREInfo.Info.
Definition SFrame.h:77
This is an optimization pass for GlobalISel generic memory operations.
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:577
detail::zippy< detail::zip_shortest, T, U, Args... > zip(T &&t, U &&u, Args &&...args)
zip iterator for two or more iteratable types.
Definition STLExtras.h:846
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:1755
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:1685
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:166
@ Implicit
Not emitted register (e.g. carry, or temporary result).
@ Kill
The last use of a register.
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:541
LLVM_ABI void reportFatalInternalError(Error Err)
Report a fatal error that indicates a bug in LLVM.
Definition Error.cpp:173
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:1769
auto make_first_range(ContainerTy &&c)
Given a container of pairs, return a range over the first elements.
Definition STLExtras.h:1415
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
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:186
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
auto count_if(R &&Range, UnaryPredicate P)
Wrapper function around std::count_if to count the number of times an element satisfying a given pred...
Definition STLExtras.h:2035
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:1788
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
Definition STLExtras.h:2208
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1963
void appendLEB128(SmallVectorImpl< U > &Buffer, T Value)
Definition LEB128.h:280
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition Alignment.h:197
void fullyRecomputeLiveIns(ArrayRef< MachineBasicBlock * > MBBs)
Convenience function for recomputing live-in's for a set of MBBs until the computation converges.
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.
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:77
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