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