LLVM 23.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 BuildMI(MBB, MI, DL, TII->get(RISCV::SSPUSH))
134 .addReg(RAReg)
136 return;
137 }
138
139 Register SCSPReg = RISCVABI::getSCSPReg();
140
141 bool IsRV64 = STI.is64Bit();
142 int64_t SlotSize = STI.getXLen() / 8;
143 // Store return address to shadow call stack
144 // addi gp, gp, [4|8]
145 // s[w|d] ra, -[4|8](gp)
146 BuildMI(MBB, MI, DL, TII->get(RISCV::ADDI))
147 .addReg(SCSPReg, RegState::Define)
148 .addReg(SCSPReg)
149 .addImm(SlotSize)
151 BuildMI(MBB, MI, DL, TII->get(IsRV64 ? RISCV::SD : RISCV::SW))
152 .addReg(RAReg)
153 .addReg(SCSPReg)
154 .addImm(-SlotSize)
156
157 if (!needsDwarfCFI(MF))
158 return;
159
160 // Emit a CFI instruction that causes SlotSize to be subtracted from the value
161 // of the shadow stack pointer when unwinding past this frame.
162 char DwarfSCSReg = TRI->getDwarfRegNum(SCSPReg, /*IsEH*/ true);
163 assert(DwarfSCSReg < 32 && "SCS Register should be < 32 (X3).");
164
165 char Offset = static_cast<char>(-SlotSize) & 0x7f;
166 const char CFIInst[] = {
167 dwarf::DW_CFA_val_expression,
168 DwarfSCSReg, // register
169 2, // length
170 static_cast<char>(unsigned(dwarf::DW_OP_breg0 + DwarfSCSReg)),
171 Offset, // addend (sleb128)
172 };
173
175 .buildEscape(StringRef(CFIInst, sizeof(CFIInst)));
176}
177
180 const DebugLoc &DL) {
181 const auto &STI = MF.getSubtarget<RISCVSubtarget>();
182 bool HasHWShadowStack = MF.getFunction().hasFnAttribute("hw-shadow-stack") &&
183 STI.hasStdExtZimop();
184 bool HasSWShadowStack =
185 MF.getFunction().hasFnAttribute(Attribute::ShadowCallStack);
186 if (!HasHWShadowStack && !HasSWShadowStack)
187 return;
188
189 // See emitSCSPrologue() above.
190 std::vector<CalleeSavedInfo> &CSI = MF.getFrameInfo().getCalleeSavedInfo();
191 if (llvm::none_of(
192 CSI, [&](CalleeSavedInfo &CSR) { return CSR.getReg() == RAReg; }))
193 return;
194
195 const RISCVInstrInfo *TII = STI.getInstrInfo();
196 if (HasHWShadowStack) {
197 BuildMI(MBB, MI, DL, TII->get(RISCV::SSPOPCHK))
198 .addReg(RAReg)
200 return;
201 }
202
203 Register SCSPReg = RISCVABI::getSCSPReg();
204
205 bool IsRV64 = STI.is64Bit();
206 int64_t SlotSize = STI.getXLen() / 8;
207 // Load return address from shadow call stack
208 // l[w|d] ra, -[4|8](gp)
209 // addi gp, gp, -[4|8]
210 BuildMI(MBB, MI, DL, TII->get(IsRV64 ? RISCV::LD : RISCV::LW))
212 .addReg(SCSPReg)
213 .addImm(-SlotSize)
215 BuildMI(MBB, MI, DL, TII->get(RISCV::ADDI))
216 .addReg(SCSPReg, RegState::Define)
217 .addReg(SCSPReg)
218 .addImm(-SlotSize)
220 if (needsDwarfCFI(MF)) {
221 // Restore the SCS pointer
223 }
224}
225
226// Insert instruction to swap mscratchsw with sp
229 const DebugLoc &DL) {
230 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
231
232 if (!RVFI->isSiFiveStackSwapInterrupt(MF))
233 return;
234
235 const auto &STI = MF.getSubtarget<RISCVSubtarget>();
236 const RISCVInstrInfo *TII = STI.getInstrInfo();
237
238 assert(STI.hasVendorXSfmclic() && "Stack Swapping Requires XSfmclic");
239
240 BuildMI(MBB, MBBI, DL, TII->get(RISCV::CSRRW))
242 .addImm(RISCVSysReg::sf_mscratchcsw)
245
246 // FIXME: CFI Information for this swap.
247}
248
249static void
252 if (!RVFI.isSiFivePreemptibleInterrupt(MF))
253 return;
254
255 const TargetRegisterClass &RC = RISCV::GPRRegClass;
256 const TargetRegisterInfo &TRI =
257 *MF.getSubtarget<RISCVSubtarget>().getRegisterInfo();
258 MachineFrameInfo &MFI = MF.getFrameInfo();
259
260 // Create two frame objects for spilling X8 and X9, which will be done in
261 // `emitSiFiveCLICPreemptibleSaves`. This is in addition to any other stack
262 // objects we might have for X8 and X9, as they might be saved twice.
263 for (int I = 0; I < 2; ++I) {
264 int FI = MFI.CreateStackObject(TRI.getSpillSize(RC), TRI.getSpillAlign(RC),
265 true);
267 }
268}
269
273 const DebugLoc &DL) {
274 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
275
276 if (!RVFI->isSiFivePreemptibleInterrupt(MF))
277 return;
278
279 const auto &STI = MF.getSubtarget<RISCVSubtarget>();
280 const RISCVInstrInfo *TII = STI.getInstrInfo();
281
282 // FIXME: CFI Information here is nonexistent/wrong.
283
284 // X8 and X9 might be stored into the stack twice, initially into the
285 // `interruptCSRFrameIndex` here, and then maybe again into their CSI frame
286 // index.
287 //
288 // This is done instead of telling the register allocator that we need two
289 // VRegs to store the value of `mcause` and `mepc` through the instruction,
290 // which affects other passes.
291 TII->storeRegToStackSlot(MBB, MBBI, RISCV::X8, /* IsKill=*/true,
292 RVFI->getInterruptCSRFrameIndex(0),
293 &RISCV::GPRRegClass, Register(),
295 TII->storeRegToStackSlot(MBB, MBBI, RISCV::X9, /* IsKill=*/true,
296 RVFI->getInterruptCSRFrameIndex(1),
297 &RISCV::GPRRegClass, Register(),
299
300 // Put `mcause` into X8 (s0), and `mepc` into X9 (s1). If either of these are
301 // used in the function, then they will appear in `getUnmanagedCSI` and will
302 // be saved again.
303 BuildMI(MBB, MBBI, DL, TII->get(RISCV::CSRRS))
304 .addReg(RISCV::X8, RegState::Define)
305 .addImm(RISCVSysReg::mcause)
306 .addReg(RISCV::X0)
308 BuildMI(MBB, MBBI, DL, TII->get(RISCV::CSRRS))
309 .addReg(RISCV::X9, RegState::Define)
310 .addImm(RISCVSysReg::mepc)
311 .addReg(RISCV::X0)
313
314 // Enable interrupts.
315 BuildMI(MBB, MBBI, DL, TII->get(RISCV::CSRRSI))
316 .addReg(RISCV::X0, RegState::Define)
317 .addImm(RISCVSysReg::mstatus)
318 .addImm(8)
320}
321
325 const DebugLoc &DL) {
326 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
327
328 if (!RVFI->isSiFivePreemptibleInterrupt(MF))
329 return;
330
331 const auto &STI = MF.getSubtarget<RISCVSubtarget>();
332 const RISCVInstrInfo *TII = STI.getInstrInfo();
333
334 // FIXME: CFI Information here is nonexistent/wrong.
335
336 // Disable interrupts.
337 BuildMI(MBB, MBBI, DL, TII->get(RISCV::CSRRCI))
338 .addReg(RISCV::X0, RegState::Define)
339 .addImm(RISCVSysReg::mstatus)
340 .addImm(8)
342
343 // Restore `mepc` from x9 (s1), and `mcause` from x8 (s0). If either were used
344 // in the function, they have already been restored once, so now have the
345 // value stored in `emitSiFiveCLICPreemptibleSaves`.
346 BuildMI(MBB, MBBI, DL, TII->get(RISCV::CSRRW))
347 .addReg(RISCV::X0, RegState::Define)
348 .addImm(RISCVSysReg::mepc)
349 .addReg(RISCV::X9, RegState::Kill)
351 BuildMI(MBB, MBBI, DL, TII->get(RISCV::CSRRW))
352 .addReg(RISCV::X0, RegState::Define)
353 .addImm(RISCVSysReg::mcause)
354 .addReg(RISCV::X8, RegState::Kill)
356
357 // X8 and X9 need to be restored to their values on function entry, which we
358 // saved onto the stack in `emitSiFiveCLICPreemptibleSaves`.
359 TII->loadRegFromStackSlot(MBB, MBBI, RISCV::X9,
360 RVFI->getInterruptCSRFrameIndex(1),
361 &RISCV::GPRRegClass, Register(),
362 RISCV::NoSubRegister, MachineInstr::FrameSetup);
363 TII->loadRegFromStackSlot(MBB, MBBI, RISCV::X8,
364 RVFI->getInterruptCSRFrameIndex(0),
365 &RISCV::GPRRegClass, Register(),
366 RISCV::NoSubRegister, MachineInstr::FrameSetup);
367}
368
369// Get the ID of the libcall used for spilling and restoring callee saved
370// registers. The ID is representative of the number of registers saved or
371// restored by the libcall, except it is zero-indexed - ID 0 corresponds to a
372// single register.
373static int getLibCallID(const MachineFunction &MF,
374 const std::vector<CalleeSavedInfo> &CSI) {
375 const auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
376
377 if (CSI.empty() || !RVFI->useSaveRestoreLibCalls(MF))
378 return -1;
379
380 MCRegister MaxReg;
381 for (auto &CS : CSI)
382 // assignCalleeSavedSpillSlots assigns negative frame indexes to
383 // registers which can be saved by libcall.
384 if (CS.getFrameIdx() < 0)
385 MaxReg = std::max(MaxReg.id(), CS.getReg().id());
386
387 if (!MaxReg)
388 return -1;
389
390 switch (MaxReg.id()) {
391 default:
392 llvm_unreachable("Something has gone wrong!");
393 // clang-format off
394 case /*s11*/ RISCV::X27: return 12;
395 case /*s10*/ RISCV::X26: return 11;
396 case /*s9*/ RISCV::X25: return 10;
397 case /*s8*/ RISCV::X24: return 9;
398 case /*s7*/ RISCV::X23: return 8;
399 case /*s6*/ RISCV::X22: return 7;
400 case /*s5*/ RISCV::X21: return 6;
401 case /*s4*/ RISCV::X20: return 5;
402 case /*s3*/ RISCV::X19: return 4;
403 case /*s2*/ RISCV::X18: return 3;
404 case /*s1*/ RISCV::X9: return 2;
405 case /*s0*/ FPReg: return 1;
406 case /*ra*/ RAReg: return 0;
407 // clang-format on
408 }
409}
410
411// Get the name of the libcall used for spilling callee saved registers.
412// If this function will not use save/restore libcalls, then return a nullptr.
413static const char *
415 const std::vector<CalleeSavedInfo> &CSI) {
416 static const char *const SpillLibCalls[] = {
417 "__riscv_save_0",
418 "__riscv_save_1",
419 "__riscv_save_2",
420 "__riscv_save_3",
421 "__riscv_save_4",
422 "__riscv_save_5",
423 "__riscv_save_6",
424 "__riscv_save_7",
425 "__riscv_save_8",
426 "__riscv_save_9",
427 "__riscv_save_10",
428 "__riscv_save_11",
429 "__riscv_save_12"
430 };
431
432 int LibCallID = getLibCallID(MF, CSI);
433 if (LibCallID == -1)
434 return nullptr;
435 return SpillLibCalls[LibCallID];
436}
437
438// Get the name of the libcall used for restoring callee saved registers.
439// If this function will not use save/restore libcalls, then return a nullptr.
440static const char *
442 const std::vector<CalleeSavedInfo> &CSI) {
443 static const char *const RestoreLibCalls[] = {
444 "__riscv_restore_0",
445 "__riscv_restore_1",
446 "__riscv_restore_2",
447 "__riscv_restore_3",
448 "__riscv_restore_4",
449 "__riscv_restore_5",
450 "__riscv_restore_6",
451 "__riscv_restore_7",
452 "__riscv_restore_8",
453 "__riscv_restore_9",
454 "__riscv_restore_10",
455 "__riscv_restore_11",
456 "__riscv_restore_12"
457 };
458
459 int LibCallID = getLibCallID(MF, CSI);
460 if (LibCallID == -1)
461 return nullptr;
462 return RestoreLibCalls[LibCallID];
463}
464
465// Get the max reg of Push/Pop for restoring callee saved registers.
466static unsigned getNumPushPopRegs(const std::vector<CalleeSavedInfo> &CSI) {
467 unsigned NumPushPopRegs = 0;
468 for (auto &CS : CSI) {
469 auto *FII = llvm::find_if(FixedCSRFIMap,
470 [&](MCPhysReg P) { return P == CS.getReg(); });
471 if (FII != std::end(FixedCSRFIMap)) {
472 unsigned RegNum = std::distance(std::begin(FixedCSRFIMap), FII);
473 NumPushPopRegs = std::max(NumPushPopRegs, RegNum + 1);
474 }
475 }
476 assert(NumPushPopRegs != 12 && "x26 requires x27 to also be pushed");
477 return NumPushPopRegs;
478}
479
480// Return true if the specified function should have a dedicated frame
481// pointer register. This is true if frame pointer elimination is
482// disabled, if it needs dynamic stack realignment, if the function has
483// variable sized allocas, or if the frame address is taken.
485 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
486
487 const MachineFrameInfo &MFI = MF.getFrameInfo();
489 RegInfo->hasStackRealignment(MF) || MFI.hasVarSizedObjects() ||
491 return true;
492
493 // With large callframes around we may need to use FP to access the scavenging
494 // emergency spillslot.
495 //
496 // We calculate the MaxCallFrameSize at the end of isel so this value should
497 // be stable for the whole post-isel MIR pipeline.
498 //
499 // NOTE: The idea of forcing a frame pointer is copied from AArch64, but they
500 // conservatively return true when the call frame size hasd not been
501 // computed yet. On RISC-V that caused MachineOutliner tests to fail the
502 // MachineVerifier due to outlined functions not computing max call frame
503 // size thus the frame pointer would always be reserved.
504 if (MFI.isMaxCallFrameSizeComputed() && MFI.getMaxCallFrameSize() > 2047)
505 return true;
506
507 return false;
508}
509
511 const MachineFrameInfo &MFI = MF.getFrameInfo();
512 const TargetRegisterInfo *TRI = STI.getRegisterInfo();
513
514 // If we do not reserve stack space for outgoing arguments in prologue,
515 // we will adjust the stack pointer before call instruction. After the
516 // adjustment, we can not use SP to access the stack objects for the
517 // arguments. Instead, use BP to access these stack objects.
518 return (MFI.hasVarSizedObjects() ||
520 MFI.getMaxCallFrameSize() != 0))) &&
521 TRI->hasStackRealignment(MF);
522}
523
524// Determines the size of the frame and maximum call frame size.
525void RISCVFrameLowering::determineFrameLayout(MachineFunction &MF) const {
526 MachineFrameInfo &MFI = MF.getFrameInfo();
527 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
528
529 // Get the number of bytes to allocate from the FrameInfo.
530 uint64_t FrameSize = MFI.getStackSize();
531
532 // QCI Interrupts use at least 96 bytes of stack space
533 if (RVFI->useQCIInterrupt(MF))
534 FrameSize = std::max(FrameSize, QCIInterruptPushAmount);
535
536 // Get the alignment.
537 Align StackAlign = getStackAlign();
538
539 // Make sure the frame is aligned.
540 FrameSize = alignTo(FrameSize, StackAlign);
541
542 // Update frame info.
543 MFI.setStackSize(FrameSize);
544
545 // When using SP or BP to access stack objects, we may require extra padding
546 // to ensure the bottom of the RVV stack is correctly aligned within the main
547 // stack. We calculate this as the amount required to align the scalar local
548 // variable section up to the RVV alignment.
550 if (RVFI->getRVVStackSize() && (!hasFP(MF) || TRI->hasStackRealignment(MF))) {
551 int ScalarLocalVarSize = FrameSize - RVFI->getCalleeSavedStackSize() -
552 RVFI->getVarArgsSaveSize();
553 if (auto RVVPadding =
554 offsetToAlignment(ScalarLocalVarSize, RVFI->getRVVStackAlign()))
555 RVFI->setRVVPadding(RVVPadding);
556 }
557}
558
559// Returns the stack size including RVV padding (when required), rounded back
560// up to the required stack alignment.
562 const MachineFunction &MF) const {
563 const MachineFrameInfo &MFI = MF.getFrameInfo();
564 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
565 return alignTo(MFI.getStackSize() + RVFI->getRVVPadding(), getStackAlign());
566}
567
570 const std::vector<CalleeSavedInfo> &CSI,
571 bool ReverseOrder = false) {
572 const MachineFrameInfo &MFI = MF.getFrameInfo();
574
575 for (auto &CS : CSI) {
576 int FI = CS.getFrameIdx();
577 if (FI >= 0 && MFI.getStackID(FI) == TargetStackID::Default)
578 NonLibcallCSI.push_back(CS);
579 }
580
581 // Reverse the order so that load/store operations use ascending addresses,
582 // enabling better load/store clustering and fusion.
583 if (ReverseOrder)
584 std::reverse(NonLibcallCSI.begin(), NonLibcallCSI.end());
585
586 return NonLibcallCSI;
587}
588
591 const std::vector<CalleeSavedInfo> &CSI) {
592 const MachineFrameInfo &MFI = MF.getFrameInfo();
594
595 for (auto &CS : CSI) {
596 int FI = CS.getFrameIdx();
597 if (FI >= 0 && MFI.getStackID(FI) == TargetStackID::ScalableVector)
598 RVVCSI.push_back(CS);
599 }
600
601 return RVVCSI;
602}
603
606 const std::vector<CalleeSavedInfo> &CSI) {
607 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
608
609 SmallVector<CalleeSavedInfo, 8> PushOrLibCallsCSI;
610 if (!RVFI->useSaveRestoreLibCalls(MF) && !RVFI->isPushable(MF))
611 return PushOrLibCallsCSI;
612
613 for (const auto &CS : CSI) {
614 if (RVFI->useQCIInterrupt(MF)) {
615 // Some registers are saved by both `QC.C.MIENTER(.NEST)` and
616 // `QC.CM.PUSH(FP)`. In these cases, prioritise the CFI info that points
617 // to the versions saved by `QC.C.MIENTER(.NEST)` which is what FP
618 // unwinding would use.
620 CS.getReg()))
621 continue;
622 }
623
624 if (llvm::is_contained(FixedCSRFIMap, CS.getReg()))
625 PushOrLibCallsCSI.push_back(CS);
626 }
627
628 return PushOrLibCallsCSI;
629}
630
633 const std::vector<CalleeSavedInfo> &CSI) {
634 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
635
636 SmallVector<CalleeSavedInfo, 8> QCIInterruptCSI;
637 if (!RVFI->useQCIInterrupt(MF))
638 return QCIInterruptCSI;
639
640 for (const auto &CS : CSI) {
642 CS.getReg()))
643 QCIInterruptCSI.push_back(CS);
644 }
645
646 return QCIInterruptCSI;
647}
648
649void RISCVFrameLowering::allocateAndProbeStackForRVV(
651 MachineBasicBlock::iterator MBBI, const DebugLoc &DL, int64_t Amount,
652 MachineInstr::MIFlag Flag, bool EmitCFI, bool DynAllocation) const {
653 assert(Amount != 0 && "Did not need to adjust stack pointer for RVV.");
654
655 // Emit a variable-length allocation probing loop.
656
657 // Get VLEN in TargetReg
658 const RISCVInstrInfo *TII = STI.getInstrInfo();
659 Register TargetReg = RISCV::X6;
660 uint32_t NumOfVReg = Amount / RISCV::RVVBytesPerBlock;
661 BuildMI(MBB, MBBI, DL, TII->get(RISCV::PseudoReadVLENB), TargetReg)
662 .setMIFlag(Flag);
663 TII->mulImm(MF, MBB, MBBI, DL, TargetReg, NumOfVReg, Flag);
664
665 CFIInstBuilder CFIBuilder(MBB, MBBI, MachineInstr::FrameSetup);
666 if (EmitCFI) {
667 // Set the CFA register to TargetReg.
668 CFIBuilder.buildDefCFA(TargetReg, -Amount);
669 }
670
671 // It will be expanded to a probe loop in `inlineStackProbe`.
672 BuildMI(MBB, MBBI, DL, TII->get(RISCV::PROBED_STACKALLOC_RVV))
673 .addReg(TargetReg);
674
675 if (EmitCFI) {
676 // Set the CFA register back to SP.
677 CFIBuilder.buildDefCFARegister(SPReg);
678 }
679
680 // SUB SP, SP, T1
681 BuildMI(MBB, MBBI, DL, TII->get(RISCV::SUB), SPReg)
682 .addReg(SPReg)
683 .addReg(TargetReg)
684 .setMIFlag(Flag);
685
686 // If we have a dynamic allocation later we need to probe any residuals.
687 if (DynAllocation) {
688 BuildMI(MBB, MBBI, DL, TII->get(STI.is64Bit() ? RISCV::SD : RISCV::SW))
689 .addReg(RISCV::X0)
690 .addReg(SPReg)
691 .addImm(0)
693 }
694}
695
699 llvm::raw_string_ostream &Comment) {
700 int64_t FixedOffset = Offset.getFixed();
701 int64_t ScalableOffset = Offset.getScalable();
702 unsigned DwarfVLenB = TRI.getDwarfRegNum(RISCV::VLENB, true);
703 if (FixedOffset) {
704 Expr.push_back(dwarf::DW_OP_consts);
705 appendLEB128<LEB128Sign::Signed>(Expr, FixedOffset);
706 Expr.push_back((uint8_t)dwarf::DW_OP_plus);
707 Comment << (FixedOffset < 0 ? " - " : " + ") << std::abs(FixedOffset);
708 }
709
710 Expr.push_back((uint8_t)dwarf::DW_OP_consts);
711 appendLEB128<LEB128Sign::Signed>(Expr, ScalableOffset);
712
713 Expr.push_back((uint8_t)dwarf::DW_OP_bregx);
714 appendLEB128<LEB128Sign::Unsigned>(Expr, DwarfVLenB);
715 Expr.push_back(0);
716
717 Expr.push_back((uint8_t)dwarf::DW_OP_mul);
718 Expr.push_back((uint8_t)dwarf::DW_OP_plus);
719
720 Comment << (ScalableOffset < 0 ? " - " : " + ") << std::abs(ScalableOffset)
721 << " * vlenb";
722}
723
727 assert(Offset.getScalable() != 0 && "Did not need to adjust CFA for RVV");
728 SmallString<64> Expr;
729 std::string CommentBuffer;
730 llvm::raw_string_ostream Comment(CommentBuffer);
731 // Build up the expression (Reg + FixedOffset + ScalableOffset * VLENB).
732 unsigned DwarfReg = TRI.getDwarfRegNum(Reg, true);
733 Expr.push_back((uint8_t)(dwarf::DW_OP_breg0 + DwarfReg));
734 Expr.push_back(0);
735 if (Reg == SPReg)
736 Comment << "sp";
737 else
738 Comment << printReg(Reg, &TRI);
739
741
742 SmallString<64> DefCfaExpr;
743 DefCfaExpr.push_back(dwarf::DW_CFA_def_cfa_expression);
744 appendLEB128<LEB128Sign::Unsigned>(DefCfaExpr, Expr.size());
745 DefCfaExpr.append(Expr.str());
746
747 return MCCFIInstruction::createEscape(nullptr, DefCfaExpr.str(), SMLoc(),
748 Comment.str());
749}
750
753 assert(Offset.getScalable() != 0 && "Did not need to adjust CFA for RVV");
754 SmallString<64> Expr;
755 std::string CommentBuffer;
756 llvm::raw_string_ostream Comment(CommentBuffer);
757 Comment << printReg(Reg, &TRI) << " @ cfa";
758
759 // Build up the expression (FixedOffset + ScalableOffset * VLENB).
761
762 SmallString<64> DefCfaExpr;
763 unsigned DwarfReg = TRI.getDwarfRegNum(Reg, true);
764 DefCfaExpr.push_back(dwarf::DW_CFA_expression);
765 appendLEB128<LEB128Sign::Unsigned>(DefCfaExpr, DwarfReg);
766 appendLEB128<LEB128Sign::Unsigned>(DefCfaExpr, Expr.size());
767 DefCfaExpr.append(Expr.str());
768
769 return MCCFIInstruction::createEscape(nullptr, DefCfaExpr.str(), SMLoc(),
770 Comment.str());
771}
772
773// Allocate stack space and probe it if necessary.
777 uint64_t RealStackSize, bool EmitCFI,
778 bool NeedProbe, uint64_t ProbeSize,
779 bool DynAllocation,
780 MachineInstr::MIFlag Flag) const {
781 DebugLoc DL;
782 const RISCVRegisterInfo *RI = STI.getRegisterInfo();
783 const RISCVInstrInfo *TII = STI.getInstrInfo();
784 bool IsRV64 = STI.is64Bit();
786
787 // Simply allocate the stack if it's not big enough to require a probe.
788 if (!NeedProbe || Offset <= ProbeSize) {
790 Flag, getStackAlign());
791
792 if (EmitCFI)
793 CFIBuilder.buildDefCFAOffset(RealStackSize);
794
795 if (NeedProbe && DynAllocation) {
796 // s[d|w] zero, 0(sp)
797 BuildMI(MBB, MBBI, DL, TII->get(IsRV64 ? RISCV::SD : RISCV::SW))
798 .addReg(RISCV::X0)
799 .addReg(SPReg)
800 .addImm(0)
801 .setMIFlags(Flag);
802 }
803
804 return;
805 }
806
807 // Unroll the probe loop depending on the number of iterations.
808 if (Offset < ProbeSize * 5) {
809 uint64_t CFAAdjust = RealStackSize - Offset;
810
811 uint64_t CurrentOffset = 0;
812 while (CurrentOffset + ProbeSize <= Offset) {
813 RI->adjustReg(MBB, MBBI, DL, SPReg, SPReg,
814 StackOffset::getFixed(-ProbeSize), Flag, getStackAlign());
815 // s[d|w] zero, 0(sp)
816 BuildMI(MBB, MBBI, DL, TII->get(IsRV64 ? RISCV::SD : RISCV::SW))
817 .addReg(RISCV::X0)
818 .addReg(SPReg)
819 .addImm(0)
820 .setMIFlags(Flag);
821
822 CurrentOffset += ProbeSize;
823 if (EmitCFI)
824 CFIBuilder.buildDefCFAOffset(CurrentOffset + CFAAdjust);
825 }
826
827 uint64_t Residual = Offset - CurrentOffset;
828 if (Residual) {
829 RI->adjustReg(MBB, MBBI, DL, SPReg, SPReg,
830 StackOffset::getFixed(-Residual), Flag, getStackAlign());
831 if (EmitCFI)
832 CFIBuilder.buildDefCFAOffset(RealStackSize);
833
834 if (DynAllocation) {
835 // s[d|w] zero, 0(sp)
836 BuildMI(MBB, MBBI, DL, TII->get(IsRV64 ? RISCV::SD : RISCV::SW))
837 .addReg(RISCV::X0)
838 .addReg(SPReg)
839 .addImm(0)
840 .setMIFlags(Flag);
841 }
842 }
843
844 return;
845 }
846
847 // Emit a variable-length allocation probing loop.
848 uint64_t RoundedSize = alignDown(Offset, ProbeSize);
849 uint64_t Residual = Offset - RoundedSize;
850
851 Register TargetReg = RISCV::X6;
852 // SUB TargetReg, SP, RoundedSize
853 RI->adjustReg(MBB, MBBI, DL, TargetReg, SPReg,
854 StackOffset::getFixed(-RoundedSize), Flag, getStackAlign());
855
856 if (EmitCFI) {
857 // Set the CFA register to TargetReg.
858 CFIBuilder.buildDefCFA(TargetReg, RoundedSize);
859 }
860
861 // It will be expanded to a probe loop in `inlineStackProbe`.
862 BuildMI(MBB, MBBI, DL, TII->get(RISCV::PROBED_STACKALLOC)).addReg(TargetReg);
863
864 if (EmitCFI) {
865 // Set the CFA register back to SP.
866 CFIBuilder.buildDefCFARegister(SPReg);
867 }
868
869 if (Residual) {
871 Flag, getStackAlign());
872 if (DynAllocation) {
873 // s[d|w] zero, 0(sp)
874 BuildMI(MBB, MBBI, DL, TII->get(IsRV64 ? RISCV::SD : RISCV::SW))
875 .addReg(RISCV::X0)
876 .addReg(SPReg)
877 .addImm(0)
878 .setMIFlags(Flag);
879 }
880 }
881
882 if (EmitCFI)
883 CFIBuilder.buildDefCFAOffset(Offset);
884}
885
886static bool isPush(unsigned Opcode) {
887 switch (Opcode) {
888 case RISCV::CM_PUSH:
889 case RISCV::QC_CM_PUSH:
890 case RISCV::QC_CM_PUSHFP:
891 return true;
892 default:
893 return false;
894 }
895}
896
897static bool isPop(unsigned Opcode) {
898 // There are other pops but these are the only ones introduced during this
899 // pass.
900 switch (Opcode) {
901 case RISCV::CM_POP:
902 case RISCV::QC_CM_POP:
903 return true;
904 default:
905 return false;
906 }
907}
908
910 bool UpdateFP) {
911 switch (Kind) {
913 return RISCV::CM_PUSH;
915 return UpdateFP ? RISCV::QC_CM_PUSHFP : RISCV::QC_CM_PUSH;
916 default:
917 llvm_unreachable("Unhandled PushPopKind");
918 }
919}
920
922 // There are other pops but they are introduced later by the Push/Pop
923 // Optimizer.
924 switch (Kind) {
926 return RISCV::CM_POP;
928 return RISCV::QC_CM_POP;
929 default:
930 llvm_unreachable("Unhandled PushPopKind");
931 }
932}
933
935 MachineBasicBlock &MBB) const {
936 MachineFrameInfo &MFI = MF.getFrameInfo();
937 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
938 const RISCVRegisterInfo *RI = STI.getRegisterInfo();
940 bool PreferAscendingLS = STI.preferAscendingLoadStore();
941
943
944 // Debug location must be unknown since the first debug location is used
945 // to determine the end of the prologue.
946 DebugLoc DL;
947
948 // All calls are tail calls in GHC calling conv, and functions have no
949 // prologue/epilogue.
951 return;
952
953 // SiFive CLIC needs to swap `sp` into `sf.mscratchcsw`
955
956 // Emit prologue for shadow call stack.
957 emitSCSPrologue(MF, MBB, MBBI, DL);
958
959 // We keep track of the first instruction because it might be a
960 // `(QC.)CM.PUSH(FP)`, and we may need to adjust the immediate rather than
961 // inserting an `addi sp, sp, -N*16`
962 auto PossiblePush = MBBI;
963
964 // Skip past all callee-saved register spill instructions.
965 while (MBBI != MBB.end() && MBBI->getFlag(MachineInstr::FrameSetup))
966 ++MBBI;
967
968 // Determine the correct frame layout
969 determineFrameLayout(MF);
970
971 const auto &CSI = MFI.getCalleeSavedInfo();
972
973 // Skip to before the spills of scalar callee-saved registers
974 // FIXME: assumes exactly one instruction is used to restore each
975 // callee-saved register.
976 MBBI =
977 std::prev(MBBI, getRVVCalleeSavedInfo(MF, CSI).size() +
978 getUnmanagedCSI(MF, CSI, PreferAscendingLS).size());
980 bool NeedsDwarfCFI = needsDwarfCFI(MF);
981
982 // If libcalls are used to spill and restore callee-saved registers, the frame
983 // has two sections; the opaque section managed by the libcalls, and the
984 // section managed by MachineFrameInfo which can also hold callee saved
985 // registers in fixed stack slots, both of which have negative frame indices.
986 // This gets even more complicated when incoming arguments are passed via the
987 // stack, as these too have negative frame indices. An example is detailed
988 // below:
989 //
990 // | incoming arg | <- FI[-3]
991 // | libcallspill |
992 // | calleespill | <- FI[-2]
993 // | calleespill | <- FI[-1]
994 // | this_frame | <- FI[0]
995 //
996 // For negative frame indices, the offset from the frame pointer will differ
997 // depending on which of these groups the frame index applies to.
998 // The following calculates the correct offset knowing the number of callee
999 // saved registers spilt by the two methods.
1000 if (int LibCallRegs = getLibCallID(MF, MFI.getCalleeSavedInfo()) + 1) {
1001 // Calculate the size of the frame managed by the libcall. The stack
1002 // alignment of these libcalls should be the same as how we set it in
1003 // getABIStackAlignment.
1004 unsigned LibCallFrameSize =
1005 alignTo((STI.getXLen() / 8) * LibCallRegs, getStackAlign());
1006 RVFI->setLibCallStackSize(LibCallFrameSize);
1007
1008 if (NeedsDwarfCFI) {
1009 CFIBuilder.buildDefCFAOffset(LibCallFrameSize);
1010 for (const CalleeSavedInfo &CS : getPushOrLibCallsSavedInfo(MF, CSI))
1011 CFIBuilder.buildOffset(CS.getReg(),
1012 MFI.getObjectOffset(CS.getFrameIdx()));
1013 }
1014 }
1015
1016 // FIXME (note copied from Lanai): This appears to be overallocating. Needs
1017 // investigation. Get the number of bytes to allocate from the FrameInfo.
1018 uint64_t RealStackSize = getStackSizeWithRVVPadding(MF);
1019 uint64_t StackSize = RealStackSize - RVFI->getReservedSpillsSize();
1020 uint64_t RVVStackSize = RVFI->getRVVStackSize();
1021
1022 // Early exit if there is no need to allocate on the stack
1023 if (RealStackSize == 0 && !MFI.adjustsStack() && RVVStackSize == 0)
1024 return;
1025
1026 // If the stack pointer has been marked as reserved, then produce an error if
1027 // the frame requires stack allocation
1028 if (STI.isRegisterReservedByUser(SPReg))
1030 MF.getFunction(), "Stack pointer required, but has been reserved."});
1031
1032 uint64_t FirstSPAdjustAmount = getFirstSPAdjustAmount(MF);
1033 // Split the SP adjustment to reduce the offsets of callee saved spill.
1034 if (FirstSPAdjustAmount) {
1035 StackSize = FirstSPAdjustAmount;
1036 RealStackSize = FirstSPAdjustAmount;
1037 }
1038
1039 if (RVFI->useQCIInterrupt(MF)) {
1040 // The function starts with `QC.C.MIENTER(.NEST)`, so the `(QC.)CM.PUSH(FP)`
1041 // could only be the next instruction.
1042 ++PossiblePush;
1043
1044 if (NeedsDwarfCFI) {
1045 // Insert the CFI metadata before where we think the `(QC.)CM.PUSH(FP)`
1046 // could be. The PUSH will also get its own CFI metadata for its own
1047 // modifications, which should come after the PUSH.
1048 CFIInstBuilder PushCFIBuilder(MBB, PossiblePush,
1051 for (const CalleeSavedInfo &CS : getQCISavedInfo(MF, CSI))
1052 PushCFIBuilder.buildOffset(CS.getReg(),
1053 MFI.getObjectOffset(CS.getFrameIdx()));
1054 }
1055 }
1056
1057 if (RVFI->isPushable(MF) && PossiblePush != MBB.end() &&
1058 isPush(PossiblePush->getOpcode())) {
1059 // Use available stack adjustment in push instruction to allocate additional
1060 // stack space. Align the stack size down to a multiple of 16. This is
1061 // needed for RVE.
1062 // FIXME: Can we increase the stack size to a multiple of 16 instead?
1063 uint64_t StackAdj =
1064 std::min(alignDown(StackSize, 16), static_cast<uint64_t>(48));
1065 PossiblePush->getOperand(1).setImm(StackAdj);
1066 StackSize -= StackAdj;
1067
1068 if (NeedsDwarfCFI) {
1069 CFIBuilder.buildDefCFAOffset(RealStackSize - StackSize);
1070 for (const CalleeSavedInfo &CS : getPushOrLibCallsSavedInfo(MF, CSI))
1071 CFIBuilder.buildOffset(CS.getReg(),
1072 MFI.getObjectOffset(CS.getFrameIdx()));
1073 }
1074 }
1075
1076 // Allocate space on the stack if necessary.
1077 auto &Subtarget = MF.getSubtarget<RISCVSubtarget>();
1078 const RISCVTargetLowering *TLI = Subtarget.getTargetLowering();
1079 bool NeedProbe = TLI->hasInlineStackProbe(MF);
1080 uint64_t ProbeSize = TLI->getStackProbeSize(MF, getStackAlign());
1081 bool DynAllocation =
1082 MF.getInfo<RISCVMachineFunctionInfo>()->hasDynamicAllocation();
1083 if (StackSize != 0)
1084 allocateStack(MBB, MBBI, MF, StackSize, RealStackSize, NeedsDwarfCFI,
1085 NeedProbe, ProbeSize, DynAllocation,
1087
1088 // Save SiFive CLIC CSRs into Stack
1090
1091 // The frame pointer is callee-saved, and code has been generated for us to
1092 // save it to the stack. We need to skip over the storing of callee-saved
1093 // registers as the frame pointer must be modified after it has been saved
1094 // to the stack, not before.
1095 // FIXME: assumes exactly one instruction is used to save each callee-saved
1096 // register.
1097 std::advance(MBBI, getUnmanagedCSI(MF, CSI, PreferAscendingLS).size());
1098 CFIBuilder.setInsertPoint(MBBI);
1099
1100 // Iterate over list of callee-saved registers and emit .cfi_offset
1101 // directives.
1102 if (NeedsDwarfCFI) {
1103 for (const CalleeSavedInfo &CS :
1104 getUnmanagedCSI(MF, CSI, PreferAscendingLS)) {
1105 MCRegister Reg = CS.getReg();
1106 int64_t Offset = MFI.getObjectOffset(CS.getFrameIdx());
1107 // Emit CFI for both sub-registers. The even register is at the base
1108 // offset and odd at base+4.
1109 if (RISCV::GPRPairRegClass.contains(Reg)) {
1110 MCRegister EvenReg = RI->getSubReg(Reg, RISCV::sub_gpr_even);
1111 MCRegister OddReg = RI->getSubReg(Reg, RISCV::sub_gpr_odd);
1112 CFIBuilder.buildOffset(EvenReg, Offset);
1113 CFIBuilder.buildOffset(OddReg, Offset + 4);
1114 } else {
1115 CFIBuilder.buildOffset(Reg, Offset);
1116 }
1117 }
1118 }
1119
1120 // Generate new FP.
1121 if (hasFP(MF)) {
1122 if (STI.isRegisterReservedByUser(FPReg))
1124 MF.getFunction(), "Frame pointer required, but has been reserved."});
1125 // The frame pointer does need to be reserved from register allocation.
1126 assert(MF.getRegInfo().isReserved(FPReg) && "FP not reserved");
1127
1128 // Some stack management variants automatically keep FP updated, so we don't
1129 // need an instruction to do so.
1130 if (!RVFI->hasImplicitFPUpdates(MF)) {
1131 RI->adjustReg(
1132 MBB, MBBI, DL, FPReg, SPReg,
1133 StackOffset::getFixed(RealStackSize - RVFI->getVarArgsSaveSize()),
1135 }
1136
1137 if (NeedsDwarfCFI)
1138 CFIBuilder.buildDefCFA(FPReg, RVFI->getVarArgsSaveSize());
1139 }
1140
1141 uint64_t SecondSPAdjustAmount = 0;
1142 // Emit the second SP adjustment after saving callee saved registers.
1143 if (FirstSPAdjustAmount) {
1144 SecondSPAdjustAmount = getStackSizeWithRVVPadding(MF) - FirstSPAdjustAmount;
1145 assert(SecondSPAdjustAmount > 0 &&
1146 "SecondSPAdjustAmount should be greater than zero");
1147
1148 allocateStack(MBB, MBBI, MF, SecondSPAdjustAmount,
1149 getStackSizeWithRVVPadding(MF), NeedsDwarfCFI && !hasFP(MF),
1150 NeedProbe, ProbeSize, DynAllocation,
1152 }
1153
1154 if (RVVStackSize) {
1155 if (NeedProbe) {
1156 allocateAndProbeStackForRVV(MF, MBB, MBBI, DL, RVVStackSize,
1158 NeedsDwarfCFI && !hasFP(MF), DynAllocation);
1159 } else {
1160 // We must keep the stack pointer aligned through any intermediate
1161 // updates.
1162 RI->adjustReg(MBB, MBBI, DL, SPReg, SPReg,
1163 StackOffset::getScalable(-RVVStackSize),
1165 }
1166
1167 if (NeedsDwarfCFI && !hasFP(MF)) {
1168 // Emit .cfi_def_cfa_expression "sp + StackSize + RVVStackSize * vlenb".
1170 *RI, SPReg,
1171 StackOffset::get(getStackSizeWithRVVPadding(MF), RVVStackSize / 8)));
1172 }
1173
1174 std::advance(MBBI, getRVVCalleeSavedInfo(MF, CSI).size());
1175 if (NeedsDwarfCFI)
1176 emitCalleeSavedRVVPrologCFI(MBB, MBBI, hasFP(MF));
1177 }
1178
1179 if (hasFP(MF)) {
1180 // Realign Stack
1181 const RISCVRegisterInfo *RI = STI.getRegisterInfo();
1182 if (RI->hasStackRealignment(MF)) {
1183 Align MaxAlignment = MFI.getMaxAlign();
1184
1185 const RISCVInstrInfo *TII = STI.getInstrInfo();
1186 if (isInt<12>(-(int)MaxAlignment.value())) {
1187 BuildMI(MBB, MBBI, DL, TII->get(RISCV::ANDI), SPReg)
1188 .addReg(SPReg)
1189 .addImm(-(int)MaxAlignment.value())
1191 } else {
1192 unsigned ShiftAmount = Log2(MaxAlignment);
1193 Register VR =
1194 MF.getRegInfo().createVirtualRegister(&RISCV::GPRRegClass);
1195 BuildMI(MBB, MBBI, DL, TII->get(RISCV::SRLI), VR)
1196 .addReg(SPReg)
1197 .addImm(ShiftAmount)
1199 BuildMI(MBB, MBBI, DL, TII->get(RISCV::SLLI), SPReg)
1200 .addReg(VR)
1201 .addImm(ShiftAmount)
1203 }
1204 if (NeedProbe && RVVStackSize == 0) {
1205 // Do a probe if the align + size allocated just passed the probe size
1206 // and was not yet probed.
1207 if (SecondSPAdjustAmount < ProbeSize &&
1208 SecondSPAdjustAmount + MaxAlignment.value() >= ProbeSize) {
1209 bool IsRV64 = STI.is64Bit();
1210 BuildMI(MBB, MBBI, DL, TII->get(IsRV64 ? RISCV::SD : RISCV::SW))
1211 .addReg(RISCV::X0)
1212 .addReg(SPReg)
1213 .addImm(0)
1215 }
1216 }
1217 // FP will be used to restore the frame in the epilogue, so we need
1218 // another base register BP to record SP after re-alignment. SP will
1219 // track the current stack after allocating variable sized objects.
1220 if (hasBP(MF)) {
1221 // move BP, SP
1222 BuildMI(MBB, MBBI, DL, TII->get(RISCV::ADDI), BPReg)
1223 .addReg(SPReg)
1224 .addImm(0)
1226 }
1227 }
1228 }
1229}
1230
1231void RISCVFrameLowering::deallocateStack(MachineFunction &MF,
1234 const DebugLoc &DL,
1235 uint64_t &StackSize,
1236 int64_t CFAOffset) const {
1238
1239 RI->adjustReg(MBB, MBBI, DL, SPReg, SPReg, StackOffset::getFixed(StackSize),
1241 StackSize = 0;
1242
1243 if (needsDwarfCFI(MF))
1245 .buildDefCFAOffset(CFAOffset);
1246}
1247
1249 MachineBasicBlock &MBB) const {
1250 const RISCVRegisterInfo *RI = STI.getRegisterInfo();
1251 MachineFrameInfo &MFI = MF.getFrameInfo();
1252 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
1253 bool PreferAscendingLS = STI.preferAscendingLoadStore();
1254
1255 // All calls are tail calls in GHC calling conv, and functions have no
1256 // prologue/epilogue.
1258 return;
1259
1260 // Get the insert location for the epilogue. If there were no terminators in
1261 // the block, get the last instruction.
1263 DebugLoc DL;
1264 if (!MBB.empty()) {
1265 MBBI = MBB.getLastNonDebugInstr();
1266 if (MBBI != MBB.end())
1267 DL = MBBI->getDebugLoc();
1268
1269 MBBI = MBB.getFirstTerminator();
1270
1271 // Skip to before the restores of all callee-saved registers.
1272 while (MBBI != MBB.begin() &&
1273 std::prev(MBBI)->getFlag(MachineInstr::FrameDestroy))
1274 --MBBI;
1275 }
1276
1277 const auto &CSI = MFI.getCalleeSavedInfo();
1278
1279 // Skip to before the restores of scalar callee-saved registers
1280 // FIXME: assumes exactly one instruction is used to restore each
1281 // callee-saved register.
1282 auto FirstScalarCSRRestoreInsn =
1283 std::next(MBBI, getRVVCalleeSavedInfo(MF, CSI).size());
1284 CFIInstBuilder CFIBuilder(MBB, FirstScalarCSRRestoreInsn,
1286 bool NeedsDwarfCFI = needsDwarfCFI(MF);
1287
1288 uint64_t FirstSPAdjustAmount = getFirstSPAdjustAmount(MF);
1289 uint64_t RealStackSize = FirstSPAdjustAmount ? FirstSPAdjustAmount
1291 uint64_t StackSize = FirstSPAdjustAmount ? FirstSPAdjustAmount
1293 RVFI->getReservedSpillsSize();
1294 uint64_t FPOffset = RealStackSize - RVFI->getVarArgsSaveSize();
1295 uint64_t RVVStackSize = RVFI->getRVVStackSize();
1296
1297 bool RestoreSPFromFP = RI->hasStackRealignment(MF) ||
1299 if (RVVStackSize) {
1300 // If RestoreSPFromFP the stack pointer will be restored using the frame
1301 // pointer value.
1302 if (!RestoreSPFromFP)
1303 RI->adjustReg(MBB, FirstScalarCSRRestoreInsn, DL, SPReg, SPReg,
1304 StackOffset::getScalable(RVVStackSize),
1306
1307 if (NeedsDwarfCFI) {
1308 if (!hasFP(MF))
1309 CFIBuilder.buildDefCFA(SPReg, RealStackSize);
1310 emitCalleeSavedRVVEpilogCFI(MBB, FirstScalarCSRRestoreInsn);
1311 }
1312 }
1313
1314 if (FirstSPAdjustAmount) {
1315 uint64_t SecondSPAdjustAmount =
1316 getStackSizeWithRVVPadding(MF) - FirstSPAdjustAmount;
1317 assert(SecondSPAdjustAmount > 0 &&
1318 "SecondSPAdjustAmount should be greater than zero");
1319
1320 // If RestoreSPFromFP the stack pointer will be restored using the frame
1321 // pointer value.
1322 if (!RestoreSPFromFP)
1323 RI->adjustReg(MBB, FirstScalarCSRRestoreInsn, DL, SPReg, SPReg,
1324 StackOffset::getFixed(SecondSPAdjustAmount),
1326
1327 if (NeedsDwarfCFI && !hasFP(MF))
1328 CFIBuilder.buildDefCFAOffset(FirstSPAdjustAmount);
1329 }
1330
1331 // Restore the stack pointer using the value of the frame pointer. Only
1332 // necessary if the stack pointer was modified, meaning the stack size is
1333 // unknown.
1334 //
1335 // In order to make sure the stack point is right through the EH region,
1336 // we also need to restore stack pointer from the frame pointer if we
1337 // don't preserve stack space within prologue/epilogue for outgoing variables,
1338 // normally it's just checking the variable sized object is present or not
1339 // is enough, but we also don't preserve that at prologue/epilogue when
1340 // have vector objects in stack.
1341 if (RestoreSPFromFP) {
1342 assert(hasFP(MF) && "frame pointer should not have been eliminated");
1343 RI->adjustReg(MBB, FirstScalarCSRRestoreInsn, DL, SPReg, FPReg,
1345 getStackAlign());
1346 }
1347
1348 if (NeedsDwarfCFI && hasFP(MF))
1349 CFIBuilder.buildDefCFA(SPReg, RealStackSize);
1350
1351 // Skip to after the restores of scalar callee-saved registers
1352 // FIXME: assumes exactly one instruction is used to restore each
1353 // callee-saved register.
1354 MBBI = std::next(FirstScalarCSRRestoreInsn,
1355 getUnmanagedCSI(MF, CSI, PreferAscendingLS).size());
1356 CFIBuilder.setInsertPoint(MBBI);
1357
1358 if (getLibCallID(MF, CSI) != -1) {
1359 // tail __riscv_restore_[0-12] instruction is considered as a terminator,
1360 // therefore it is unnecessary to place any CFI instructions after it. Just
1361 // deallocate stack if needed and return.
1362 if (StackSize != 0)
1363 deallocateStack(MF, MBB, MBBI, DL, StackSize,
1364 RVFI->getLibCallStackSize());
1365
1366 // Emit epilogue for shadow call stack.
1367 emitSCSEpilogue(MF, MBB, MBBI, DL);
1368 return;
1369 }
1370
1371 // Recover callee-saved registers.
1372 if (NeedsDwarfCFI) {
1373 for (const CalleeSavedInfo &CS :
1374 getUnmanagedCSI(MF, CSI, PreferAscendingLS)) {
1375 MCRegister Reg = CS.getReg();
1376 // Emit CFI for both sub-registers.
1377 if (RISCV::GPRPairRegClass.contains(Reg)) {
1378 MCRegister EvenReg = RI->getSubReg(Reg, RISCV::sub_gpr_even);
1379 MCRegister OddReg = RI->getSubReg(Reg, RISCV::sub_gpr_odd);
1380 CFIBuilder.buildRestore(EvenReg);
1381 CFIBuilder.buildRestore(OddReg);
1382 } else {
1383 CFIBuilder.buildRestore(Reg);
1384 }
1385 }
1386 }
1387
1388 if (RVFI->isPushable(MF) && MBBI != MBB.end() && isPop(MBBI->getOpcode())) {
1389 // Use available stack adjustment in pop instruction to deallocate stack
1390 // space. Align the stack size down to a multiple of 16. This is needed for
1391 // RVE.
1392 // FIXME: Can we increase the stack size to a multiple of 16 instead?
1393 uint64_t StackAdj =
1394 std::min(alignDown(StackSize, 16), static_cast<uint64_t>(48));
1395 MBBI->getOperand(1).setImm(StackAdj);
1396 StackSize -= StackAdj;
1397
1398 if (StackSize != 0)
1399 deallocateStack(MF, MBB, MBBI, DL, StackSize,
1400 /*stack_adj of cm.pop instr*/ RealStackSize - StackSize);
1401
1402 auto NextI = next_nodbg(MBBI, MBB.end());
1403 if (NextI == MBB.end() || NextI->getOpcode() != RISCV::PseudoRET) {
1404 ++MBBI;
1405 if (NeedsDwarfCFI) {
1406 CFIBuilder.setInsertPoint(MBBI);
1407
1408 for (const CalleeSavedInfo &CS : getPushOrLibCallsSavedInfo(MF, CSI))
1409 CFIBuilder.buildRestore(CS.getReg());
1410
1411 // Update CFA Offset. If this is a QCI interrupt function, there will
1412 // be a leftover offset which is deallocated by `QC.C.MILEAVERET`,
1413 // otherwise getQCIInterruptStackSize() will be 0.
1414 CFIBuilder.buildDefCFAOffset(RVFI->getQCIInterruptStackSize());
1415 }
1416 }
1417 }
1418
1420
1421 // Deallocate stack if StackSize isn't a zero yet. If this is a QCI interrupt
1422 // function, there will be a leftover offset which is deallocated by
1423 // `QC.C.MILEAVERET`, otherwise getQCIInterruptStackSize() will be 0.
1424 if (StackSize != 0)
1425 deallocateStack(MF, MBB, MBBI, DL, StackSize,
1426 RVFI->getQCIInterruptStackSize());
1427
1428 // Emit epilogue for shadow call stack.
1429 emitSCSEpilogue(MF, MBB, MBBI, DL);
1430
1431 // SiFive CLIC needs to swap `sf.mscratchcsw` into `sp`
1433}
1434
1435void RISCVFrameLowering::emitZeroCallUsedRegs(BitVector RegsToZero,
1436 MachineBasicBlock &MBB) const {
1437 // Insertion point.
1438 MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
1439
1440 // Fake a debug loc.
1441 DebugLoc DL;
1442 if (MBBI != MBB.end())
1443 DL = MBBI->getDebugLoc();
1444
1445 const MachineFunction &MF = *MBB.getParent();
1448 const RISCVInstrInfo &TII = *STI.getInstrInfo();
1449
1450 for (MCRegister Reg : RegsToZero.set_bits()) {
1451 if (TRI.isGeneralPurposeRegister(MF, Reg))
1452 TII.buildClearRegister(Reg, MBB, MBBI, DL);
1453 }
1454}
1455
1458 Register &FrameReg) const {
1459 const MachineFrameInfo &MFI = MF.getFrameInfo();
1461 const auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
1462
1463 // Callee-saved registers should be referenced relative to the stack
1464 // pointer (positive offset), otherwise use the frame pointer (negative
1465 // offset).
1466 const auto &CSI = getUnmanagedCSI(MF, MFI.getCalleeSavedInfo(),
1467 STI.preferAscendingLoadStore());
1468 int MinCSFI = 0;
1469 int MaxCSFI = -1;
1471 auto StackID = MFI.getStackID(FI);
1472
1473 assert((StackID == TargetStackID::Default ||
1474 StackID == TargetStackID::ScalableVector) &&
1475 "Unexpected stack ID for the frame object.");
1476 if (StackID == TargetStackID::Default) {
1477 assert(getOffsetOfLocalArea() == 0 && "LocalAreaOffset is not 0!");
1479 MFI.getOffsetAdjustment());
1480 } else if (StackID == TargetStackID::ScalableVector) {
1482 }
1483
1484 uint64_t FirstSPAdjustAmount = getFirstSPAdjustAmount(MF);
1485
1486 if (CSI.size()) {
1487 MinCSFI = std::min(CSI.front().getFrameIdx(), CSI.back().getFrameIdx());
1488 MaxCSFI = std::max(CSI.front().getFrameIdx(), CSI.back().getFrameIdx());
1489 }
1490
1491 if (FI >= MinCSFI && FI <= MaxCSFI) {
1492 FrameReg = SPReg;
1493
1494 if (FirstSPAdjustAmount)
1495 Offset += StackOffset::getFixed(FirstSPAdjustAmount);
1496 else
1498 return Offset;
1499 }
1500
1501 if (RI->hasStackRealignment(MF) && !MFI.isFixedObjectIndex(FI)) {
1502 // If the stack was realigned, the frame pointer is set in order to allow
1503 // SP to be restored, so we need another base register to record the stack
1504 // after realignment.
1505 // |--------------------------| --
1506 // | callee-allocated save | | <----|
1507 // | area for register varargs| | |
1508 // |--------------------------| <-- FP |
1509 // | callee-saved registers | | |
1510 // |--------------------------| -- |
1511 // | realignment (the size of | | |
1512 // | this area is not counted | | |
1513 // | in MFI.getStackSize()) | | |
1514 // |--------------------------| -- |-- MFI.getStackSize()
1515 // | RVV alignment padding | | |
1516 // | (not counted in | | |
1517 // | MFI.getStackSize() but | | |
1518 // | counted in | | |
1519 // | RVFI.getRVVStackSize()) | | |
1520 // |--------------------------| -- |
1521 // | RVV objects | | |
1522 // | (not counted in | | |
1523 // | MFI.getStackSize()) | | |
1524 // |--------------------------| -- |
1525 // | padding before RVV | | |
1526 // | (not counted in | | |
1527 // | MFI.getStackSize() or in | | |
1528 // | RVFI.getRVVStackSize()) | | |
1529 // |--------------------------| -- |
1530 // | scalar local variables | | <----'
1531 // |--------------------------| -- <-- BP (if var sized objects present)
1532 // | VarSize objects | |
1533 // |--------------------------| -- <-- SP
1534 if (hasBP(MF)) {
1535 FrameReg = RISCVABI::getBPReg();
1536 } else {
1537 // VarSize objects must be empty in this case!
1538 assert(!MFI.hasVarSizedObjects());
1539 FrameReg = SPReg;
1540 }
1541 } else if (!RI->hasStackRealignment(MF)) {
1542 // Note: Keeping the following as multiple 'if' statements rather than
1543 // merging to a single expression for readability.
1544 if (!hasFP(MF)) {
1545 // No FP available, must use SP.
1546 FrameReg = SPReg;
1547 } else {
1548 FrameReg = FPReg;
1549 // SP-relative addressing is only valid when SP is stable throughout
1550 // the function body: no dynamic SP adjustments for outgoing call args,
1551 // no variable-sized objects, and no RVV scalable stack regions.
1552 // hasReservedCallFrame() conservatively encompasses all these checks.
1553 if (hasReservedCallFrame(MF)) {
1554 // Both FP and SP are candidates.
1555 // Prefer SP when the SP-relative offset fits in the compressed
1556 // instruction immediate range.
1557 int64_t SPOff = Offset.getFixed() + MFI.getStackSize();
1558 int64_t CLWSPMaxOffset = 252;
1559 int64_t CLDSPMaxOffset = 504;
1560 int64_t SPThreshold = STI.is64Bit() ? CLDSPMaxOffset : CLWSPMaxOffset;
1561 if (SPOff >= 0 && SPOff <= SPThreshold)
1562 FrameReg = SPReg;
1563 }
1564 }
1565 } else {
1566 assert(RI->hasStackRealignment(MF) && MFI.isFixedObjectIndex(FI) &&
1567 "Expected fixed object with stack realignment");
1568 assert(hasFP(MF) && "Re-aligned stack must have frame pointer");
1569 FrameReg = FPReg;
1570 }
1571
1572 if (FrameReg == FPReg) {
1573 Offset += StackOffset::getFixed(RVFI->getVarArgsSaveSize());
1574 // When using FP to access scalable vector objects, we need to minus
1575 // the frame size.
1576 //
1577 // |--------------------------| --
1578 // | callee-allocated save | |
1579 // | area for register varargs| |
1580 // |--------------------------| | -- <-- FP
1581 // | callee-saved registers | |
1582 // |--------------------------| | MFI.getStackSize()
1583 // | scalar local variables | |
1584 // |--------------------------| -- (Offset of RVV objects is from here.)
1585 // | RVV objects |
1586 // |--------------------------|
1587 // | VarSize objects |
1588 // |--------------------------| <-- SP
1589 if (StackID == TargetStackID::ScalableVector) {
1590 assert(!RI->hasStackRealignment(MF) &&
1591 "Can't index across variable sized realign");
1592 // We don't expect any extra RVV alignment padding, as the stack size
1593 // and RVV object sections should be correct aligned in their own
1594 // right.
1596 "Inconsistent stack layout");
1598 }
1599 return Offset;
1600 }
1601
1602 // This case handles indexing off both SP and BP.
1603 // If indexing off SP, there must not be any var sized objects
1604 assert(FrameReg == RISCVABI::getBPReg() || !MFI.hasVarSizedObjects());
1605
1606 // When using SP to access frame objects, we need to add RVV stack size.
1607 //
1608 // |--------------------------| --
1609 // | callee-allocated save | | <----|
1610 // | area for register varargs| | |
1611 // |--------------------------| | | <-- FP
1612 // | callee-saved registers | | |
1613 // |--------------------------| -- |
1614 // | RVV alignment padding | | |
1615 // | (not counted in | | |
1616 // | MFI.getStackSize() but | | |
1617 // | counted in | | |
1618 // | RVFI.getRVVStackSize()) | | |
1619 // |--------------------------| -- |
1620 // | RVV objects | | |-- MFI.getStackSize()
1621 // | (not counted in | | |
1622 // | MFI.getStackSize()) | | |
1623 // |--------------------------| -- |
1624 // | padding before RVV | | |
1625 // | (not counted in | | |
1626 // | MFI.getStackSize()) | | |
1627 // |--------------------------| -- |
1628 // | scalar local variables | | <----'
1629 // |--------------------------| -- <-- BP (if var sized objects present)
1630 // | VarSize objects | |
1631 // |--------------------------| -- <-- SP
1632 //
1633 // The total amount of padding surrounding RVV objects is described by
1634 // RVV->getRVVPadding() and it can be zero. It allows us to align the RVV
1635 // objects to the required alignment.
1636 if (MFI.getStackID(FI) == TargetStackID::Default) {
1637 if (MFI.isFixedObjectIndex(FI)) {
1638 assert(!RI->hasStackRealignment(MF) &&
1639 "Can't index across variable sized realign");
1641 RVFI->getRVVStackSize());
1642 } else {
1644 }
1645 } else if (MFI.getStackID(FI) == TargetStackID::ScalableVector) {
1646 // Ensure the base of the RVV stack is correctly aligned: add on the
1647 // alignment padding.
1648 int64_t ScalarLocalVarSize =
1649 MFI.getStackSize() - RVFI->getCalleeSavedStackSize() -
1650 RVFI->getVarArgsSaveSize() + RVFI->getRVVPadding();
1651 Offset += StackOffset::get(ScalarLocalVarSize, RVFI->getRVVStackSize());
1652 }
1653 return Offset;
1654}
1655
1657 const Register &Reg) {
1658 MCRegister BaseReg = TRI.getSubReg(Reg, RISCV::sub_vrm1_0);
1659 // If it's not a grouped vector register, it doesn't have subregister, so
1660 // the base register is just itself.
1661 if (!BaseReg.isValid())
1662 BaseReg = Reg;
1663 return BaseReg;
1664}
1665
1667 BitVector &SavedRegs,
1668 RegScavenger *RS) const {
1670
1671 // In TargetFrameLowering::determineCalleeSaves, any vector register is marked
1672 // as saved if any of its subregister is clobbered, this is not correct in
1673 // vector registers. We only want the vector register to be marked as saved
1674 // if all of its subregisters are clobbered.
1675 // For example:
1676 // Original behavior: If v24 is marked, v24m2, v24m4, v24m8 are also marked.
1677 // Correct behavior: v24m2 is marked only if v24 and v25 are marked.
1678 MachineRegisterInfo &MRI = MF.getRegInfo();
1679 const MCPhysReg *CSRegs = MRI.getCalleeSavedRegs();
1680 const RISCVRegisterInfo &TRI = *STI.getRegisterInfo();
1681 for (unsigned i = 0; CSRegs[i]; ++i) {
1682 unsigned CSReg = CSRegs[i];
1683 // Only vector registers need special care.
1684 if (!RISCV::VRRegClass.contains(getRVVBaseRegister(TRI, CSReg)))
1685 continue;
1686
1687 SavedRegs.reset(CSReg);
1688
1689 auto SubRegs = TRI.subregs(CSReg);
1690 // Set the register and all its subregisters.
1691 if (!MRI.def_empty(CSReg) || MRI.getUsedPhysRegsMask().test(CSReg)) {
1692 SavedRegs.set(CSReg);
1693 for (unsigned Reg : SubRegs)
1694 SavedRegs.set(Reg);
1695 }
1696
1697 }
1698
1699 // Unconditionally spill RA and FP only if the function uses a frame
1700 // pointer.
1701 if (hasFP(MF)) {
1702 SavedRegs.set(RAReg);
1703 SavedRegs.set(FPReg);
1704 }
1705 // Mark BP as used if function has dedicated base pointer.
1706 if (hasBP(MF))
1707 SavedRegs.set(RISCVABI::getBPReg());
1708
1709 // When using cm.push/pop we must save X27 if we save X26.
1710 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
1711 if (RVFI->isPushable(MF) && SavedRegs.test(RISCV::X26))
1712 SavedRegs.set(RISCV::X27);
1713
1714 // For Zilsd on RV32, append GPRPair registers to the CSR list. This prevents
1715 // the need to create register sets for each abi which is a lot more complex.
1716 // Don't use Zilsd for callee-saved coalescing if the required alignment
1717 // exceeds the stack alignment or when Zcmp/Xqccmp or save/restore libcalls
1718 // are enabled.
1719 bool UseZilsd = !STI.is64Bit() && STI.hasStdExtZilsd() &&
1720 STI.getZilsdAlign() <= getStackAlign() &&
1721 !RVFI->isPushable(MF) && !RVFI->useSaveRestoreLibCalls(MF);
1722 if (UseZilsd) {
1725 for (unsigned i = 0; CSRegs[i]; ++i) {
1726 NewCSRs.push_back(CSRegs[i]);
1727 CSRSet.insert(CSRegs[i]);
1728 }
1729
1730 // Append GPRPair registers for pairs where both sub-registers are in CSR
1731 // list. Iterate through all GPRPairs and check if both sub-regs are CSRs.
1732 for (MCPhysReg Pair : RISCV::GPRPairRegClass) {
1733 // Do not append a pair that's already in the CSR list.
1734 if (CSRSet.contains(Pair))
1735 continue;
1736 MCPhysReg EvenReg = TRI.getSubReg(Pair, RISCV::sub_gpr_even);
1737 MCPhysReg OddReg = TRI.getSubReg(Pair, RISCV::sub_gpr_odd);
1738 if (CSRSet.contains(EvenReg) && CSRSet.contains(OddReg)) {
1739 NewCSRs.push_back(Pair);
1740 CSRSet.insert(Pair);
1741 }
1742 }
1743
1744 MRI.setCalleeSavedRegs(NewCSRs);
1745 CSRegs = MRI.getCalleeSavedRegs();
1746 }
1747
1748 // Check if all subregisters are marked for saving. If so, set the super
1749 // register bit. For GPRPair, only check sub_gpr_even and sub_gpr_odd, not
1750 // aliases like X8_W or X8_H which are not set in SavedRegs.
1751 for (unsigned i = 0; CSRegs[i]; ++i) {
1752 unsigned CSReg = CSRegs[i];
1753 bool CombineToSuperReg;
1754 if (RISCV::GPRPairRegClass.contains(CSReg)) {
1755 MCPhysReg EvenReg = TRI.getSubReg(CSReg, RISCV::sub_gpr_even);
1756 MCPhysReg OddReg = TRI.getSubReg(CSReg, RISCV::sub_gpr_odd);
1757 CombineToSuperReg = SavedRegs.test(EvenReg) && SavedRegs.test(OddReg);
1758 // If s0(x8) is used as FP we can't generate load/store pair because it
1759 // breaks the frame chain.
1760 if (hasFP(MF) && CSReg == RISCV::X8_X9)
1761 CombineToSuperReg = false;
1762 } else {
1763 auto SubRegs = TRI.subregs(CSReg);
1764 CombineToSuperReg =
1765 !SubRegs.empty() && llvm::all_of(SubRegs, [&](unsigned Reg) {
1766 return SavedRegs.test(Reg);
1767 });
1768 }
1769
1770 if (CombineToSuperReg)
1771 SavedRegs.set(CSReg);
1772 }
1773
1774 // SiFive Preemptible Interrupt Handlers need additional frame entries
1776}
1777
1778std::pair<int64_t, Align>
1779RISCVFrameLowering::assignRVVStackObjectOffsets(MachineFunction &MF) const {
1780 MachineFrameInfo &MFI = MF.getFrameInfo();
1781 // Create a buffer of RVV objects to allocate.
1782 SmallVector<int, 8> ObjectsToAllocate;
1783 auto pushRVVObjects = [&](int FIBegin, int FIEnd) {
1784 for (int I = FIBegin, E = FIEnd; I != E; ++I) {
1785 unsigned StackID = MFI.getStackID(I);
1786 if (StackID != TargetStackID::ScalableVector)
1787 continue;
1788 if (MFI.isDeadObjectIndex(I))
1789 continue;
1790
1791 ObjectsToAllocate.push_back(I);
1792 }
1793 };
1794 // First push RVV Callee Saved object, then push RVV stack object
1795 std::vector<CalleeSavedInfo> &CSI = MF.getFrameInfo().getCalleeSavedInfo();
1796 const auto &RVVCSI = getRVVCalleeSavedInfo(MF, CSI);
1797 if (!RVVCSI.empty())
1798 pushRVVObjects(RVVCSI[0].getFrameIdx(),
1799 RVVCSI[RVVCSI.size() - 1].getFrameIdx() + 1);
1800 pushRVVObjects(0, MFI.getObjectIndexEnd() - RVVCSI.size());
1801
1802 // The minimum alignment is 16 bytes.
1803 Align RVVStackAlign(16);
1804 const auto &ST = MF.getSubtarget<RISCVSubtarget>();
1805
1806 if (!ST.hasVInstructions()) {
1807 assert(ObjectsToAllocate.empty() &&
1808 "Can't allocate scalable-vector objects without V instructions");
1809 return std::make_pair(0, RVVStackAlign);
1810 }
1811
1812 // Allocate all RVV locals and spills
1813 int64_t Offset = 0;
1814 for (int FI : ObjectsToAllocate) {
1815 // ObjectSize in bytes.
1816 int64_t ObjectSize = MFI.getObjectSize(FI);
1817 auto ObjectAlign =
1818 std::max(Align(RISCV::RVVBytesPerBlock), MFI.getObjectAlign(FI));
1819 // If the data type is the fractional vector type, reserve one vector
1820 // register for it.
1821 if (ObjectSize < RISCV::RVVBytesPerBlock)
1822 ObjectSize = RISCV::RVVBytesPerBlock;
1823 Offset = alignTo(Offset + ObjectSize, ObjectAlign);
1824 MFI.setObjectOffset(FI, -Offset);
1825 // Update the maximum alignment of the RVV stack section
1826 RVVStackAlign = std::max(RVVStackAlign, ObjectAlign);
1827 }
1828
1829 uint64_t StackSize = Offset;
1830
1831 // Ensure the alignment of the RVV stack. Since we want the most-aligned
1832 // object right at the bottom (i.e., any padding at the top of the frame),
1833 // readjust all RVV objects down by the alignment padding.
1834 // Stack size and offsets are multiples of vscale, stack alignment is in
1835 // bytes, we can divide stack alignment by minimum vscale to get a maximum
1836 // stack alignment multiple of vscale.
1837 auto VScale =
1838 std::max<uint64_t>(ST.getRealMinVLen() / RISCV::RVVBitsPerBlock, 1);
1839 if (auto RVVStackAlignVScale = RVVStackAlign.value() / VScale) {
1840 if (auto AlignmentPadding =
1841 offsetToAlignment(StackSize, Align(RVVStackAlignVScale))) {
1842 StackSize += AlignmentPadding;
1843 for (int FI : ObjectsToAllocate)
1844 MFI.setObjectOffset(FI, MFI.getObjectOffset(FI) - AlignmentPadding);
1845 }
1846 }
1847
1848 return std::make_pair(StackSize, RVVStackAlign);
1849}
1850
1852 // For RVV spill, scalable stack offsets computing requires up to two scratch
1853 // registers
1854 static constexpr unsigned ScavSlotsNumRVVSpillScalableObject = 2;
1855
1856 // For RVV spill, non-scalable stack offsets computing requires up to one
1857 // scratch register.
1858 static constexpr unsigned ScavSlotsNumRVVSpillNonScalableObject = 1;
1859
1860 // ADDI instruction's destination register can be used for computing
1861 // offsets. So Scalable stack offsets require up to one scratch register.
1862 static constexpr unsigned ScavSlotsADDIScalableObject = 1;
1863
1864 static constexpr unsigned MaxScavSlotsNumKnown =
1865 std::max({ScavSlotsADDIScalableObject, ScavSlotsNumRVVSpillScalableObject,
1866 ScavSlotsNumRVVSpillNonScalableObject});
1867
1868 unsigned MaxScavSlotsNum = 0;
1870 return false;
1871 for (const MachineBasicBlock &MBB : MF)
1872 for (const MachineInstr &MI : MBB) {
1873 bool IsRVVSpill = RISCV::isRVVSpill(MI);
1874 for (auto &MO : MI.operands()) {
1875 if (!MO.isFI())
1876 continue;
1877 bool IsScalableVectorID = MF.getFrameInfo().getStackID(MO.getIndex()) ==
1879 if (IsRVVSpill) {
1880 MaxScavSlotsNum = std::max(
1881 MaxScavSlotsNum, IsScalableVectorID
1882 ? ScavSlotsNumRVVSpillScalableObject
1883 : ScavSlotsNumRVVSpillNonScalableObject);
1884 } else if (MI.getOpcode() == RISCV::ADDI && IsScalableVectorID) {
1885 MaxScavSlotsNum =
1886 std::max(MaxScavSlotsNum, ScavSlotsADDIScalableObject);
1887 }
1888 }
1889 if (MaxScavSlotsNum == MaxScavSlotsNumKnown)
1890 return MaxScavSlotsNumKnown;
1891 }
1892 return MaxScavSlotsNum;
1893}
1894
1895static bool hasRVVFrameObject(const MachineFunction &MF) {
1896 // Originally, the function will scan all the stack objects to check whether
1897 // if there is any scalable vector object on the stack or not. However, it
1898 // causes errors in the register allocator. In issue 53016, it returns false
1899 // before RA because there is no RVV stack objects. After RA, it returns true
1900 // because there are spilling slots for RVV values during RA. It will not
1901 // reserve BP during register allocation and generate BP access in the PEI
1902 // pass due to the inconsistent behavior of the function.
1903 //
1904 // The function is changed to use hasVInstructions() as the return value. It
1905 // is not precise, but it can make the register allocation correct.
1906 //
1907 // FIXME: Find a better way to make the decision or revisit the solution in
1908 // D103622.
1909 //
1910 // Refer to https://github.com/llvm/llvm-project/issues/53016.
1911 return MF.getSubtarget<RISCVSubtarget>().hasVInstructions();
1912}
1913
1915 const RISCVInstrInfo &TII) {
1916 unsigned FnSize = 0;
1917 for (auto &MBB : MF) {
1918 for (auto &MI : MBB) {
1919 // Far branches over 20-bit offset will be relaxed in branch relaxation
1920 // pass. In the worst case, conditional branches will be relaxed into
1921 // the following instruction sequence. Unconditional branches are
1922 // relaxed in the same way, with the exception that there is no first
1923 // branch instruction.
1924 //
1925 // foo
1926 // bne t5, t6, .rev_cond # `TII->getInstSizeInBytes(MI)` bytes
1927 // sd s11, 0(sp) # 4 bytes, or 2 bytes with Zca
1928 // jump .restore, s11 # 8 bytes
1929 // .rev_cond
1930 // bar
1931 // j .dest_bb # 4 bytes, or 2 bytes with Zca
1932 // .restore:
1933 // ld s11, 0(sp) # 4 bytes, or 2 bytes with Zca
1934 // .dest:
1935 // baz
1936 if (MI.isConditionalBranch())
1937 FnSize += TII.getInstSizeInBytes(MI);
1938 if (MI.isConditionalBranch() || MI.isUnconditionalBranch()) {
1939 if (MF.getSubtarget<RISCVSubtarget>().hasStdExtZca())
1940 FnSize += 2 + 8 + 2 + 2;
1941 else
1942 FnSize += 4 + 8 + 4 + 4;
1943 continue;
1944 }
1945
1946 FnSize += TII.getInstSizeInBytes(MI);
1947 }
1948 }
1949 return FnSize;
1950}
1951
1953 MachineFunction &MF, RegScavenger *RS) const {
1954 const RISCVRegisterInfo *RegInfo =
1955 MF.getSubtarget<RISCVSubtarget>().getRegisterInfo();
1956 const RISCVInstrInfo *TII = MF.getSubtarget<RISCVSubtarget>().getInstrInfo();
1957 MachineFrameInfo &MFI = MF.getFrameInfo();
1958 const TargetRegisterClass *RC = &RISCV::GPRRegClass;
1959 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
1960
1961 int64_t RVVStackSize;
1962 Align RVVStackAlign;
1963 std::tie(RVVStackSize, RVVStackAlign) = assignRVVStackObjectOffsets(MF);
1964
1965 RVFI->setRVVStackSize(RVVStackSize);
1966 RVFI->setRVVStackAlign(RVVStackAlign);
1967
1968 if (hasRVVFrameObject(MF)) {
1969 // Ensure the entire stack is aligned to at least the RVV requirement: some
1970 // scalable-vector object alignments are not considered by the
1971 // target-independent code.
1972 MFI.ensureMaxAlignment(RVVStackAlign);
1973 }
1974
1975 unsigned ScavSlotsNum = 0;
1976
1977 // estimateStackSize has been observed to under-estimate the final stack
1978 // size, so give ourselves wiggle-room by checking for stack size
1979 // representable an 11-bit signed field rather than 12-bits.
1980 if (!isInt<11>(MFI.estimateStackSize(MF)))
1981 ScavSlotsNum = 1;
1982
1983 // Far branches over 20-bit offset require a spill slot for scratch register.
1984 bool IsLargeFunction = !isInt<20>(estimateFunctionSizeInBytes(MF, *TII));
1985 if (IsLargeFunction)
1986 ScavSlotsNum = std::max(ScavSlotsNum, 1u);
1987
1988 // RVV loads & stores have no capacity to hold the immediate address offsets
1989 // so we must always reserve an emergency spill slot if the MachineFunction
1990 // contains any RVV spills.
1991 ScavSlotsNum = std::max(ScavSlotsNum, getScavSlotsNumForRVV(MF));
1992
1993 for (unsigned I = 0; I < ScavSlotsNum; I++) {
1994 int FI = MFI.CreateSpillStackObject(RegInfo->getSpillSize(*RC),
1995 RegInfo->getSpillAlign(*RC));
1996 RS->addScavengingFrameIndex(FI);
1997
1998 if (IsLargeFunction && RVFI->getBranchRelaxationScratchFrameIndex() == -1)
1999 RVFI->setBranchRelaxationScratchFrameIndex(FI);
2000 }
2001
2002 unsigned Size = RVFI->getReservedSpillsSize();
2003 for (const auto &Info : MFI.getCalleeSavedInfo()) {
2004 int FrameIdx = Info.getFrameIdx();
2005 if (FrameIdx < 0 || MFI.getStackID(FrameIdx) != TargetStackID::Default)
2006 continue;
2007
2008 Size += MFI.getObjectSize(FrameIdx);
2009 }
2010 RVFI->setCalleeSavedStackSize(Size);
2011}
2012
2013// Not preserve stack space within prologue for outgoing variables when the
2014// function contains variable size objects or there are vector objects accessed
2015// by the frame pointer.
2016// Let eliminateCallFramePseudoInstr preserve stack space for it.
2018 return !MF.getFrameInfo().hasVarSizedObjects() &&
2019 !(hasFP(MF) && hasRVVFrameObject(MF));
2020}
2021
2022// Eliminate ADJCALLSTACKDOWN, ADJCALLSTACKUP pseudo instructions.
2026 DebugLoc DL = MI->getDebugLoc();
2027
2028 if (!hasReservedCallFrame(MF)) {
2029 // If space has not been reserved for a call frame, ADJCALLSTACKDOWN and
2030 // ADJCALLSTACKUP must be converted to instructions manipulating the stack
2031 // pointer. This is necessary when there is a variable length stack
2032 // allocation (e.g. alloca), which means it's not possible to allocate
2033 // space for outgoing arguments from within the function prologue.
2034 int64_t Amount = MI->getOperand(0).getImm();
2035
2036 if (Amount != 0) {
2037 // Ensure the stack remains aligned after adjustment.
2038 Amount = alignSPAdjust(Amount);
2039
2040 if (MI->getOpcode() == RISCV::ADJCALLSTACKDOWN)
2041 Amount = -Amount;
2042
2043 const RISCVTargetLowering *TLI =
2044 MF.getSubtarget<RISCVSubtarget>().getTargetLowering();
2045 int64_t ProbeSize = TLI->getStackProbeSize(MF, getStackAlign());
2046 if (TLI->hasInlineStackProbe(MF) && -Amount >= ProbeSize) {
2047 // When stack probing is enabled, the decrement of SP may need to be
2048 // probed. We can handle both the decrement and the probing in
2049 // allocateStack.
2050 bool DynAllocation =
2051 MF.getInfo<RISCVMachineFunctionInfo>()->hasDynamicAllocation();
2052 allocateStack(MBB, MI, MF, -Amount, -Amount,
2053 needsDwarfCFI(MF) && !hasFP(MF),
2054 /*NeedProbe=*/true, ProbeSize, DynAllocation,
2056 inlineStackProbe(MF, MBB);
2057 } else {
2058 const RISCVRegisterInfo &RI = *STI.getRegisterInfo();
2061 }
2062 }
2063 }
2064
2065 return MBB.erase(MI);
2066}
2067
2068// We would like to split the SP adjustment to reduce prologue/epilogue
2069// as following instructions. In this way, the offset of the callee saved
2070// register could fit in a single store. Supposed that the first sp adjust
2071// amount is 2032.
2072// add sp,sp,-2032
2073// sw ra,2028(sp)
2074// sw s0,2024(sp)
2075// sw s1,2020(sp)
2076// sw s3,2012(sp)
2077// sw s4,2008(sp)
2078// add sp,sp,-64
2081 const auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
2082 const MachineFrameInfo &MFI = MF.getFrameInfo();
2083 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
2084 uint64_t StackSize = getStackSizeWithRVVPadding(MF);
2085
2086 // Disable SplitSPAdjust if save-restore libcall, push/pop or QCI interrupts
2087 // are used. The callee-saved registers will be pushed by the save-restore
2088 // libcalls, so we don't have to split the SP adjustment in this case.
2089 if (RVFI->getReservedSpillsSize())
2090 return 0;
2091
2092 // Return the FirstSPAdjustAmount if the StackSize can not fit in a signed
2093 // 12-bit and there exists a callee-saved register needing to be pushed.
2094 if (!isInt<12>(StackSize) && (CSI.size() > 0)) {
2095 // FirstSPAdjustAmount is chosen at most as (2048 - StackAlign) because
2096 // 2048 will cause sp = sp + 2048 in the epilogue to be split into multiple
2097 // instructions. Offsets smaller than 2048 can fit in a single load/store
2098 // instruction, and we have to stick with the stack alignment. 2048 has
2099 // 16-byte alignment. The stack alignment for RV32 and RV64 is 16 and for
2100 // RV32E it is 4. So (2048 - StackAlign) will satisfy the stack alignment.
2101 const uint64_t StackAlign = getStackAlign().value();
2102
2103 // Amount of (2048 - StackAlign) will prevent callee saved and restored
2104 // instructions be compressed, so try to adjust the amount to the largest
2105 // offset that stack compression instructions accept when target supports
2106 // compression instructions.
2107 if (STI.hasStdExtZca()) {
2108 // The compression extensions may support the following instructions:
2109 // riscv32: c.lwsp rd, offset[7:2] => 2^(6 + 2)
2110 // c.swsp rs2, offset[7:2] => 2^(6 + 2)
2111 // c.flwsp rd, offset[7:2] => 2^(6 + 2)
2112 // c.fswsp rs2, offset[7:2] => 2^(6 + 2)
2113 // riscv64: c.ldsp rd, offset[8:3] => 2^(6 + 3)
2114 // c.sdsp rs2, offset[8:3] => 2^(6 + 3)
2115 // c.fldsp rd, offset[8:3] => 2^(6 + 3)
2116 // c.fsdsp rs2, offset[8:3] => 2^(6 + 3)
2117 const uint64_t RVCompressLen = STI.getXLen() * 8;
2118 // Compared with amount (2048 - StackAlign), StackSize needs to
2119 // satisfy the following conditions to avoid using more instructions
2120 // to adjust the sp after adjusting the amount, such as
2121 // StackSize meets the condition (StackSize <= 2048 + RVCompressLen),
2122 // case1: Amount is 2048 - StackAlign: use addi + addi to adjust sp.
2123 // case2: Amount is RVCompressLen: use addi + addi to adjust sp.
2124 auto CanCompress = [&](uint64_t CompressLen) -> bool {
2125 if (StackSize <= 2047 + CompressLen ||
2126 (StackSize > 2048 * 2 - StackAlign &&
2127 StackSize <= 2047 * 2 + CompressLen) ||
2128 StackSize > 2048 * 3 - StackAlign)
2129 return true;
2130
2131 return false;
2132 };
2133 // In the epilogue, addi sp, sp, 496 is used to recover the sp and it
2134 // can be compressed(C.ADDI16SP, offset can be [-512, 496]), but
2135 // addi sp, sp, 512 can not be compressed. So try to use 496 first.
2136 const uint64_t ADDI16SPCompressLen = 496;
2137 if (STI.is64Bit() && CanCompress(ADDI16SPCompressLen))
2138 return ADDI16SPCompressLen;
2139 if (CanCompress(RVCompressLen))
2140 return RVCompressLen;
2141 }
2142 return 2048 - StackAlign;
2143 }
2144 return 0;
2145}
2146
2149 std::vector<CalleeSavedInfo> &CSI) const {
2150 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
2151 MachineFrameInfo &MFI = MF.getFrameInfo();
2152 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
2153
2154 // Preemptible Interrupts have two additional Callee-save Frame Indexes,
2155 // not tracked by `CSI`.
2156 if (RVFI->isSiFivePreemptibleInterrupt(MF)) {
2157 for (int I = 0; I < 2; ++I) {
2158 int FI = RVFI->getInterruptCSRFrameIndex(I);
2159 MFI.setIsCalleeSavedObjectIndex(FI, true);
2160 }
2161 }
2162
2163 // Early exit if no callee saved registers are modified!
2164 if (CSI.empty())
2165 return true;
2166
2167 if (RVFI->useQCIInterrupt(MF)) {
2168 RVFI->setQCIInterruptStackSize(QCIInterruptPushAmount);
2169 }
2170
2171 if (RVFI->isPushable(MF)) {
2172 // Determine how many GPRs we need to push and save it to RVFI.
2173 unsigned PushedRegNum = getNumPushPopRegs(CSI);
2174
2175 // `QC.C.MIENTER(.NEST)` will save `ra` and `s0`, so we should only push if
2176 // we want to push more than 2 registers. Otherwise, we should push if we
2177 // want to push more than 0 registers.
2178 unsigned OnlyPushIfMoreThan = RVFI->useQCIInterrupt(MF) ? 2 : 0;
2179 if (PushedRegNum > OnlyPushIfMoreThan) {
2180 RVFI->setRVPushRegs(PushedRegNum);
2181 RVFI->setRVPushStackSize(alignTo((STI.getXLen() / 8) * PushedRegNum, 16));
2182 }
2183 }
2184
2185 for (auto &CS : CSI) {
2186 MCRegister Reg = CS.getReg();
2187 const TargetRegisterClass *RC = RegInfo->getMinimalPhysRegClass(Reg);
2188 unsigned Size = RegInfo->getSpillSize(*RC);
2189
2190 if (RVFI->useQCIInterrupt(MF)) {
2191 const auto *FFI = llvm::find_if(FixedCSRFIQCIInterruptMap, [&](auto P) {
2192 return P.first == CS.getReg();
2193 });
2194 if (FFI != std::end(FixedCSRFIQCIInterruptMap)) {
2195 int64_t Offset = FFI->second * (int64_t)Size;
2196
2197 int FrameIdx = MFI.CreateFixedSpillStackObject(Size, Offset);
2198 assert(FrameIdx < 0);
2199 CS.setFrameIdx(FrameIdx);
2200 continue;
2201 }
2202 }
2203
2204 if (RVFI->useSaveRestoreLibCalls(MF) || RVFI->isPushable(MF)) {
2205 const auto *FII = llvm::find_if(
2206 FixedCSRFIMap, [&](MCPhysReg P) { return P == CS.getReg(); });
2207 unsigned RegNum = std::distance(std::begin(FixedCSRFIMap), FII);
2208
2209 if (FII != std::end(FixedCSRFIMap)) {
2210 int64_t Offset;
2211 if (RVFI->getPushPopKind(MF) ==
2213 Offset = -int64_t(RVFI->getRVPushRegs() - RegNum) * Size;
2214 else
2215 Offset = -int64_t(RegNum + 1) * Size;
2216
2217 if (RVFI->useQCIInterrupt(MF))
2219
2220 int FrameIdx = MFI.CreateFixedSpillStackObject(Size, Offset);
2221 assert(FrameIdx < 0);
2222 CS.setFrameIdx(FrameIdx);
2223 continue;
2224 }
2225 }
2226
2227 // For GPRPair registers, use 8-byte slots with required alignment by zilsd.
2228 if (!STI.is64Bit() && STI.hasStdExtZilsd() &&
2229 RISCV::GPRPairRegClass.contains(Reg)) {
2230 Align PairAlign = STI.getZilsdAlign();
2231 int FrameIdx = MFI.CreateStackObject(8, PairAlign, true);
2232 MFI.setIsCalleeSavedObjectIndex(FrameIdx, true);
2233 CS.setFrameIdx(FrameIdx);
2234 continue;
2235 }
2236
2237 // Not a fixed slot.
2238 Align Alignment = RegInfo->getSpillAlign(*RC);
2239 // We may not be able to satisfy the desired alignment specification of
2240 // the TargetRegisterClass if the stack alignment is smaller. Use the
2241 // min.
2242 Alignment = std::min(Alignment, getStackAlign());
2243 int FrameIdx = MFI.CreateStackObject(Size, Alignment, true);
2244 MFI.setIsCalleeSavedObjectIndex(FrameIdx, true);
2245 CS.setFrameIdx(FrameIdx);
2247 MFI.setStackID(FrameIdx, TargetStackID::ScalableVector);
2248 }
2249
2250 if (RVFI->useQCIInterrupt(MF)) {
2251 // Allocate a fixed object that covers the entire QCI stack allocation,
2252 // because there are gaps which are reserved for future use.
2253 MFI.CreateFixedSpillStackObject(
2254 QCIInterruptPushAmount, -static_cast<int64_t>(QCIInterruptPushAmount));
2255 }
2256
2257 if (RVFI->isPushable(MF)) {
2258 int64_t QCIOffset = RVFI->useQCIInterrupt(MF) ? QCIInterruptPushAmount : 0;
2259 // Allocate a fixed object that covers the full push.
2260 if (int64_t PushSize = RVFI->getRVPushStackSize())
2261 MFI.CreateFixedSpillStackObject(PushSize, -PushSize - QCIOffset);
2262 } else if (int LibCallRegs = getLibCallID(MF, CSI) + 1) {
2263 int64_t LibCallFrameSize =
2264 alignTo((STI.getXLen() / 8) * LibCallRegs, getStackAlign());
2265 MFI.CreateFixedSpillStackObject(LibCallFrameSize, -LibCallFrameSize);
2266 }
2267
2268 return true;
2269}
2270
2274 if (CSI.empty())
2275 return true;
2276
2277 MachineFunction *MF = MBB.getParent();
2278 const TargetInstrInfo &TII = *MF->getSubtarget().getInstrInfo();
2279 DebugLoc DL;
2280 if (MI != MBB.end() && !MI->isDebugInstr())
2281 DL = MI->getDebugLoc();
2282
2284 if (RVFI->useQCIInterrupt(*MF)) {
2285 // Emit QC.C.MIENTER(.NEST)
2286 BuildMI(
2287 MBB, MI, DL,
2288 TII.get(RVFI->getInterruptStackKind(*MF) ==
2290 ? RISCV::QC_C_MIENTER_NEST
2291 : RISCV::QC_C_MIENTER))
2293
2294 for (auto [Reg, _Offset] : FixedCSRFIQCIInterruptMap)
2295 MBB.addLiveIn(Reg);
2296 }
2297
2298 if (RVFI->isPushable(*MF)) {
2299 // Emit CM.PUSH with base StackAdj & evaluate Push stack
2300 unsigned PushedRegNum = RVFI->getRVPushRegs();
2301 if (PushedRegNum > 0) {
2302 // Use encoded number to represent registers to spill.
2303 unsigned Opcode = getPushOpcode(
2304 RVFI->getPushPopKind(*MF), hasFP(*MF) && !RVFI->useQCIInterrupt(*MF));
2305 unsigned RegEnc = RISCVZC::encodeRegListNumRegs(PushedRegNum);
2306 MachineInstrBuilder PushBuilder =
2307 BuildMI(MBB, MI, DL, TII.get(Opcode))
2309 PushBuilder.addImm(RegEnc);
2310 PushBuilder.addImm(0);
2311
2312 for (unsigned i = 0; i < PushedRegNum; i++)
2313 PushBuilder.addUse(FixedCSRFIMap[i], RegState::Implicit);
2314 }
2315 } else if (const char *SpillLibCall = getSpillLibCallName(*MF, CSI)) {
2316 // Add spill libcall via non-callee-saved register t0.
2317 MachineInstrBuilder NewMI =
2318 BuildMI(MBB, MI, DL, TII.get(RISCV::PseudoCALLReg), RISCV::X5)
2319 .addExternalSymbol(SpillLibCall, RISCVII::MO_CALL)
2321 .addUse(RISCV::X2, RegState::Implicit)
2322 .addDef(RISCV::X2, RegState::ImplicitDefine);
2323
2324 // Add registers spilled as implicit used.
2325 for (auto &CS : CSI)
2326 NewMI.addUse(CS.getReg(), RegState::Implicit);
2327 }
2328
2329 // Manually spill values not spilled by libcall & Push/Pop.
2330 const auto &UnmanagedCSI =
2331 getUnmanagedCSI(*MF, CSI, STI.preferAscendingLoadStore());
2332 const auto &RVVCSI = getRVVCalleeSavedInfo(*MF, CSI);
2333
2334 auto storeRegsToStackSlots = [&](decltype(UnmanagedCSI) CSInfo) {
2335 for (auto &CS : CSInfo) {
2336 // Insert the spill to the stack frame.
2337 MCRegister Reg = CS.getReg();
2338 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
2339 TII.storeRegToStackSlot(MBB, MI, Reg, !MBB.isLiveIn(Reg),
2340 CS.getFrameIdx(), RC, Register(),
2342 }
2343 };
2344 storeRegsToStackSlots(UnmanagedCSI);
2345 storeRegsToStackSlots(RVVCSI);
2346
2347 return true;
2348}
2349
2350static unsigned getCalleeSavedRVVNumRegs(const Register &BaseReg) {
2351 return RISCV::VRRegClass.contains(BaseReg) ? 1
2352 : RISCV::VRM2RegClass.contains(BaseReg) ? 2
2353 : RISCV::VRM4RegClass.contains(BaseReg) ? 4
2354 : 8;
2355}
2356
2357void RISCVFrameLowering::emitCalleeSavedRVVPrologCFI(
2359 MachineFunction *MF = MBB.getParent();
2360 const MachineFrameInfo &MFI = MF->getFrameInfo();
2361 RISCVMachineFunctionInfo *RVFI = MF->getInfo<RISCVMachineFunctionInfo>();
2362 const RISCVRegisterInfo &TRI = *STI.getRegisterInfo();
2363
2364 const auto &RVVCSI = getRVVCalleeSavedInfo(*MF, MFI.getCalleeSavedInfo());
2365 if (RVVCSI.empty())
2366 return;
2367
2368 uint64_t FixedSize = getStackSizeWithRVVPadding(*MF);
2369 if (!HasFP) {
2370 uint64_t ScalarLocalVarSize =
2371 MFI.getStackSize() - RVFI->getCalleeSavedStackSize() -
2372 RVFI->getVarArgsSaveSize() + RVFI->getRVVPadding();
2373 FixedSize -= ScalarLocalVarSize;
2374 }
2375
2376 CFIInstBuilder CFIBuilder(MBB, MI, MachineInstr::FrameSetup);
2377 for (auto &CS : RVVCSI) {
2378 // Insert the spill to the stack frame.
2379 int FI = CS.getFrameIdx();
2380 MCRegister BaseReg = getRVVBaseRegister(TRI, CS.getReg());
2381 unsigned NumRegs = getCalleeSavedRVVNumRegs(CS.getReg());
2382 for (unsigned i = 0; i < NumRegs; ++i) {
2383 CFIBuilder.insertCFIInst(createDefCFAOffset(
2384 TRI, BaseReg + i,
2385 StackOffset::get(-FixedSize, MFI.getObjectOffset(FI) / 8 + i)));
2386 }
2387 }
2388}
2389
2390void RISCVFrameLowering::emitCalleeSavedRVVEpilogCFI(
2392 MachineFunction *MF = MBB.getParent();
2393 const MachineFrameInfo &MFI = MF->getFrameInfo();
2394 const RISCVRegisterInfo &TRI = *STI.getRegisterInfo();
2395
2396 CFIInstBuilder CFIHelper(MBB, MI, MachineInstr::FrameDestroy);
2397 const auto &RVVCSI = getRVVCalleeSavedInfo(*MF, MFI.getCalleeSavedInfo());
2398 for (auto &CS : RVVCSI) {
2399 MCRegister BaseReg = getRVVBaseRegister(TRI, CS.getReg());
2400 unsigned NumRegs = getCalleeSavedRVVNumRegs(CS.getReg());
2401 for (unsigned i = 0; i < NumRegs; ++i)
2402 CFIHelper.buildRestore(BaseReg + i);
2403 }
2404}
2405
2409 if (CSI.empty())
2410 return true;
2411
2412 MachineFunction *MF = MBB.getParent();
2413 const TargetInstrInfo &TII = *MF->getSubtarget().getInstrInfo();
2414 DebugLoc DL;
2415 if (MI != MBB.end() && !MI->isDebugInstr())
2416 DL = MI->getDebugLoc();
2417
2418 // Manually restore values not restored by libcall & Push/Pop.
2419 // Reverse the restore order in epilog. In addition, the return
2420 // address will be restored first in the epilogue. It increases
2421 // the opportunity to avoid the load-to-use data hazard between
2422 // loading RA and return by RA. loadRegFromStackSlot can insert
2423 // multiple instructions.
2424 const auto &UnmanagedCSI =
2425 getUnmanagedCSI(*MF, CSI, STI.preferAscendingLoadStore());
2426 const auto &RVVCSI = getRVVCalleeSavedInfo(*MF, CSI);
2427
2428 auto loadRegFromStackSlot = [&](decltype(UnmanagedCSI) CSInfo) {
2429 for (auto &CS : CSInfo) {
2430 MCRegister Reg = CS.getReg();
2431 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
2432 TII.loadRegFromStackSlot(MBB, MI, Reg, CS.getFrameIdx(), RC, Register(),
2433 RISCV::NoSubRegister,
2435 assert(MI != MBB.begin() &&
2436 "loadRegFromStackSlot didn't insert any code!");
2437 }
2438 };
2439 loadRegFromStackSlot(RVVCSI);
2440 loadRegFromStackSlot(UnmanagedCSI);
2441
2443 if (RVFI->useQCIInterrupt(*MF)) {
2444 // Don't emit anything here because restoration is handled by
2445 // QC.C.MILEAVERET which we already inserted to return.
2446 assert(MI->getOpcode() == RISCV::QC_C_MILEAVERET &&
2447 "Unexpected QCI Interrupt Return Instruction");
2448 }
2449
2450 if (RVFI->isPushable(*MF)) {
2451 unsigned PushedRegNum = RVFI->getRVPushRegs();
2452 if (PushedRegNum > 0) {
2453 unsigned Opcode = getPopOpcode(RVFI->getPushPopKind(*MF));
2454 unsigned RegEnc = RISCVZC::encodeRegListNumRegs(PushedRegNum);
2455 MachineInstrBuilder PopBuilder =
2456 BuildMI(MBB, MI, DL, TII.get(Opcode))
2458 // Use encoded number to represent registers to restore.
2459 PopBuilder.addImm(RegEnc);
2460 PopBuilder.addImm(0);
2461
2462 for (unsigned i = 0; i < RVFI->getRVPushRegs(); i++)
2464 }
2465 } else if (const char *RestoreLibCall = getRestoreLibCallName(*MF, CSI)) {
2466 // Add restore libcall via tail call.
2467 MachineInstrBuilder NewMI =
2468 BuildMI(MBB, MI, DL, TII.get(RISCV::PseudoTAIL))
2469 .addExternalSymbol(RestoreLibCall, RISCVII::MO_CALL)
2471 .addDef(RISCV::X2, RegState::ImplicitDefine);
2472
2473 // Add registers restored as implicit defined.
2474 for (auto &CS : CSI)
2475 NewMI.addDef(CS.getReg(), RegState::ImplicitDefine);
2476
2477 // Remove trailing returns, since the terminator is now a tail call to the
2478 // restore function.
2479 if (MI != MBB.end() && MI->getOpcode() == RISCV::PseudoRET) {
2480 NewMI.getInstr()->copyImplicitOps(*MF, *MI);
2481 MI->eraseFromParent();
2482 }
2483 }
2484 return true;
2485}
2486
2488 // Keep the conventional code flow when not optimizing.
2489 if (MF.getFunction().hasOptNone())
2490 return false;
2491
2492 return true;
2493}
2494
2496 MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB);
2497 const MachineFunction *MF = MBB.getParent();
2498 const auto *RVFI = MF->getInfo<RISCVMachineFunctionInfo>();
2499
2500 // Make sure VTYPE and VL are not live-in since we will use vsetvli in the
2501 // prologue to get the VLEN, and that will clobber these registers.
2502 //
2503 // We may do also check the stack contains objects with scalable vector type,
2504 // but this will require iterating over all the stack objects, but this may
2505 // not worth since the situation is rare, we could do further check in future
2506 // if we find it is necessary.
2507 if (STI.preferVsetvliOverReadVLENB() &&
2508 (MBB.isLiveIn(RISCV::VTYPE) || MBB.isLiveIn(RISCV::VL)))
2509 return false;
2510
2511 if (!RVFI->useSaveRestoreLibCalls(*MF))
2512 return true;
2513
2514 // Inserting a call to a __riscv_save libcall requires the use of the register
2515 // t0 (X5) to hold the return address. Therefore if this register is already
2516 // used we can't insert the call.
2517
2518 RegScavenger RS;
2519 RS.enterBasicBlock(*TmpMBB);
2520 return !RS.isRegUsed(RISCV::X5);
2521}
2522
2524 const MachineFunction *MF = MBB.getParent();
2525 MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB);
2526 const auto *RVFI = MF->getInfo<RISCVMachineFunctionInfo>();
2527
2528 // We do not want QC.C.MILEAVERET to be subject to shrink-wrapping - it must
2529 // come in the final block of its function as it both pops and returns.
2530 if (RVFI->useQCIInterrupt(*MF))
2531 return MBB.succ_empty();
2532
2533 if (!RVFI->useSaveRestoreLibCalls(*MF))
2534 return true;
2535
2536 // Using the __riscv_restore libcalls to restore CSRs requires a tail call.
2537 // This means if we still need to continue executing code within this function
2538 // the restore cannot take place in this basic block.
2539
2540 if (MBB.succ_size() > 1)
2541 return false;
2542
2543 MachineBasicBlock *SuccMBB =
2544 MBB.succ_empty() ? TmpMBB->getFallThrough() : *MBB.succ_begin();
2545
2546 // Doing a tail call should be safe if there are no successors, because either
2547 // we have a returning block or the end of the block is unreachable, so the
2548 // restore will be eliminated regardless.
2549 if (!SuccMBB)
2550 return true;
2551
2552 // The successor can only contain a return, since we would effectively be
2553 // replacing the successor with our own tail return at the end of our block.
2554 return SuccMBB->isReturnBlock() && SuccMBB->size() == 1;
2555}
2556
2558 switch (ID) {
2561 return true;
2566 return false;
2567 }
2568 llvm_unreachable("Invalid TargetStackID::Value");
2569}
2570
2574
2575// Synthesize the probe loop.
2577 Register TargetReg, bool IsRVV) {
2578 assert(TargetReg != RISCV::X2 && "New top of stack cannot already be in SP");
2579
2580 MachineBasicBlock &MBB = *MBBI->getParent();
2581 MachineFunction &MF = *MBB.getParent();
2582
2583 auto &Subtarget = MF.getSubtarget<RISCVSubtarget>();
2584 const RISCVInstrInfo *TII = Subtarget.getInstrInfo();
2585 bool IsRV64 = Subtarget.is64Bit();
2586 Align StackAlign = Subtarget.getFrameLowering()->getStackAlign();
2587 const RISCVTargetLowering *TLI = Subtarget.getTargetLowering();
2588 uint64_t ProbeSize = TLI->getStackProbeSize(MF, StackAlign);
2589
2590 MachineFunction::iterator MBBInsertPoint = std::next(MBB.getIterator());
2591 MachineBasicBlock *LoopTestMBB =
2592 MF.CreateMachineBasicBlock(MBB.getBasicBlock());
2593 MF.insert(MBBInsertPoint, LoopTestMBB);
2594 MachineBasicBlock *ExitMBB = MF.CreateMachineBasicBlock(MBB.getBasicBlock());
2595 MF.insert(MBBInsertPoint, ExitMBB);
2597 Register ScratchReg = RISCV::X7;
2598
2599 // ScratchReg = ProbeSize
2600 TII->movImm(MBB, MBBI, DL, ScratchReg, ProbeSize, Flags);
2601
2602 // LoopTest:
2603 // SUB SP, SP, ProbeSize
2604 BuildMI(*LoopTestMBB, LoopTestMBB->end(), DL, TII->get(RISCV::SUB), SPReg)
2605 .addReg(SPReg)
2606 .addReg(ScratchReg)
2607 .setMIFlags(Flags);
2608
2609 // s[d|w] zero, 0(sp)
2610 BuildMI(*LoopTestMBB, LoopTestMBB->end(), DL,
2611 TII->get(IsRV64 ? RISCV::SD : RISCV::SW))
2612 .addReg(RISCV::X0)
2613 .addReg(SPReg)
2614 .addImm(0)
2615 .setMIFlags(Flags);
2616
2617 if (IsRVV) {
2618 // SUB TargetReg, TargetReg, ProbeSize
2619 BuildMI(*LoopTestMBB, LoopTestMBB->end(), DL, TII->get(RISCV::SUB),
2620 TargetReg)
2621 .addReg(TargetReg)
2622 .addReg(ScratchReg)
2623 .setMIFlags(Flags);
2624
2625 // BGE TargetReg, ProbeSize, LoopTest
2626 BuildMI(*LoopTestMBB, LoopTestMBB->end(), DL, TII->get(RISCV::BGE))
2627 .addReg(TargetReg)
2628 .addReg(ScratchReg)
2629 .addMBB(LoopTestMBB)
2630 .setMIFlags(Flags);
2631
2632 } else {
2633 // BNE SP, TargetReg, LoopTest
2634 BuildMI(*LoopTestMBB, LoopTestMBB->end(), DL, TII->get(RISCV::BNE))
2635 .addReg(SPReg)
2636 .addReg(TargetReg)
2637 .addMBB(LoopTestMBB)
2638 .setMIFlags(Flags);
2639 }
2640
2641 ExitMBB->splice(ExitMBB->end(), &MBB, std::next(MBBI), MBB.end());
2643
2644 LoopTestMBB->addSuccessor(ExitMBB);
2645 LoopTestMBB->addSuccessor(LoopTestMBB);
2646 MBB.addSuccessor(LoopTestMBB);
2647 // Update liveins.
2648 fullyRecomputeLiveIns({ExitMBB, LoopTestMBB});
2649}
2650
2651void RISCVFrameLowering::inlineStackProbe(MachineFunction &MF,
2652 MachineBasicBlock &MBB) const {
2653 // Get the instructions that need to be replaced. We emit at most two of
2654 // these. Remember them in order to avoid complications coming from the need
2655 // to traverse the block while potentially creating more blocks.
2656 SmallVector<MachineInstr *, 4> ToReplace;
2657 for (MachineInstr &MI : MBB) {
2658 unsigned Opc = MI.getOpcode();
2659 if (Opc == RISCV::PROBED_STACKALLOC ||
2660 Opc == RISCV::PROBED_STACKALLOC_RVV) {
2661 ToReplace.push_back(&MI);
2662 }
2663 }
2664
2665 for (MachineInstr *MI : ToReplace) {
2666 if (MI->getOpcode() == RISCV::PROBED_STACKALLOC ||
2667 MI->getOpcode() == RISCV::PROBED_STACKALLOC_RVV) {
2668 MachineBasicBlock::iterator MBBI = MI->getIterator();
2670 Register TargetReg = MI->getOperand(0).getReg();
2671 emitStackProbeInline(MBBI, DL, TargetReg,
2672 (MI->getOpcode() == RISCV::PROBED_STACKALLOC_RVV));
2674 }
2675 }
2676}
2677
2679 return 0;
2680}
2681
2684 return RISCV::X2;
2685}
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)
static void emitStackProbeInline(MachineBasicBlock::iterator MBBI, DebugLoc DL, Register TargetReg)
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
#define P(N)
static constexpr uint64_t QCIInterruptPushAmount
static 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 SmallVector< CalleeSavedInfo, 8 > getUnmanagedCSI(const MachineFunction &MF, const std::vector< CalleeSavedInfo > &CSI, bool ReverseOrder=false)
static bool isPop(unsigned Opcode)
static unsigned getCalleeSavedRVVNumRegs(const Register &BaseReg)
static MCCFIInstruction createDefCFAOffset(const TargetRegisterInfo &TRI, Register Reg, StackOffset Offset)
static Align getABIStackAlignment(RISCVABI::ABI ABI)
static unsigned getPopOpcode(RISCVMachineFunctionInfo::PushPopKind Kind)
static SmallVector< CalleeSavedInfo, 8 > getPushOrLibCallsSavedInfo(const MachineFunction &MF, const std::vector< CalleeSavedInfo > &CSI)
static int getLibCallID(const MachineFunction &MF, const std::vector< CalleeSavedInfo > &CSI)
static const std::pair< MCPhysReg, int8_t > FixedCSRFIQCIInterruptMap[]
static bool isPush(unsigned Opcode)
static constexpr MCPhysReg RAReg
static 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 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:484
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
bool test(unsigned Idx) const
Returns true if bit Idx is set.
Definition BitVector.h:482
BitVector & reset()
Reset all bits in the bitvector.
Definition BitVector.h:409
BitVector & set()
Set all bits in the bitvector.
Definition BitVector.h:366
iterator_range< const_set_bits_iterator > set_bits() const
Definition BitVector.h:159
Helper class for creating CFI instructions and inserting them into MIR.
void buildEscape(StringRef Bytes, StringRef Comment="") const
void buildDefCFAOffset(int64_t Offset, MCSymbol *Label=nullptr) const
void buildRestore(MCRegister Reg) const
void buildDefCFARegister(MCRegister Reg) const
void buildOffset(MCRegister Reg, int64_t Offset) const
void insertCFIInst(const MCCFIInstruction &CFIInst) const
void buildDefCFA(MCRegister Reg, int64_t Offset) const
void setInsertPoint(MachineBasicBlock::iterator IP)
The CalleeSavedInfo class tracks the information need to locate where a callee saved register is in t...
MCRegister getReg() const
A debug info location.
Definition DebugLoc.h:124
Diagnostic information for unsupported feature in backend.
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition Function.h:272
bool hasOptNone() const
Do not optimize this function (-O0).
Definition Function.h:708
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition Function.cpp:354
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition Function.cpp:724
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:727
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 uint64_t estimateStackSize(const MachineFunction &MF) const
Estimate and return the size of the stack frame.
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
int64_t getObjectSize(int ObjectIdx) const
Return the size of the specified object.
bool isMaxCallFrameSizeComputed() const
LLVM_ABI int CreateSpillStackObject(uint64_t Size, Align Alignment, TargetStackID::Value StackID=TargetStackID::Default)
Create a new statically sized stack object that represents a spill slot, returning a nonnegative iden...
const std::vector< CalleeSavedInfo > & getCalleeSavedInfo() const
Returns a reference to call saved info vector for the current function.
int getObjectIndexEnd() const
Return one past the maximum frame object index.
uint8_t getStackID(int ObjectIdx) const
int64_t getObjectOffset(int ObjectIdx) const
Return the assigned stack offset of the specified object from the incoming stack pointer.
void setStackSize(uint64_t Size)
Set the size of the stack.
bool isFixedObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a fixed stack object.
bool isDeadObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a dead object.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
bool needsFrameMoves() const
True if this function needs frame moves for debug or exceptions.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
BasicBlockListType::iterator iterator
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 & addUse(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register use operand.
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & setMIFlag(MachineInstr::MIFlag Flag) const
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
const MachineInstrBuilder & addDef(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register definition operand.
const MachineInstrBuilder & setMIFlags(unsigned Flags) const
MachineInstr * getInstr() const
If conversion operators fail, use this method to get the MachineInstr explicitly.
Representation of each machine instruction.
LLVM_ABI void copyImplicitOps(MachineFunction &MF, const MachineInstr &MI)
Copy implicit register operands from specified instruction to this instruction.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
const BitVector & getUsedPhysRegsMask() const
bool isReserved(MCRegister PhysReg) const
isReserved - Returns true when PhysReg is a reserved register.
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
bool def_empty(Register RegNo) const
def_empty - Return true if there are no instructions defining the specified register (it may be live-...
LLVM_ABI const MCPhysReg * getCalleeSavedRegs() const
Returns list of callee saved registers.
LLVM_ABI void setCalleeSavedRegs(ArrayRef< MCPhysReg > CSRs)
Sets the updated Callee Saved Registers list.
Represent a mutable reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:294
bool assignCalleeSavedSpillSlots(MachineFunction &MF, const TargetRegisterInfo *TRI, std::vector< CalleeSavedInfo > &CSI) const override
assignCalleeSavedSpillSlots - Allows target to override spill slot assignment logic.
void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override
emitProlog/emitEpilog - These methods insert prolog and epilog code into the function.
uint64_t getFirstSPAdjustAmount(const MachineFunction &MF) const
bool enableShrinkWrapping(const MachineFunction &MF) const override
Returns true if the target will correctly handle shrink wrapping.
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
const RISCVInstrInfo * getInstrInfo() const override
bool hasInlineStackProbe(const MachineFunction &MF) const override
True if stack clash protection is enabled for this functions.
unsigned getStackProbeSize(const MachineFunction &MF, Align StackAlign) const
Wrapper class representing virtual and physical registers.
Definition Register.h:20
Represents a location in source code.
Definition SMLoc.h:22
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition SmallSet.h:134
bool contains(const T &V) const
Check if the SmallSet contains the given element.
Definition SmallSet.h:229
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition SmallSet.h:184
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
void append(StringRef RHS)
Append from a StringRef.
Definition SmallString.h:68
StringRef str() const
Explicit conversion to StringRef.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StackOffset holds a fixed and a scalable offset in bytes.
Definition TypeSize.h:30
int64_t getFixed() const
Returns the fixed component of the stack.
Definition TypeSize.h:46
int64_t getScalable() const
Returns the scalable component of the stack.
Definition TypeSize.h:49
static StackOffset get(int64_t Fixed, int64_t Scalable)
Definition TypeSize.h:41
static StackOffset getScalable(int64_t Scalable)
Definition TypeSize.h:40
static StackOffset getFixed(int64_t Fixed)
Definition TypeSize.h:39
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
bool hasFP(const MachineFunction &MF) const
hasFP - Return true if the specified function should have a dedicated frame pointer register.
virtual void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS=nullptr) const
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
int getOffsetOfLocalArea() const
getOffsetOfLocalArea - This method returns the offset of the local area from the stack pointer on ent...
TargetFrameLowering(StackDirection D, Align StackAl, int LAO, Align TransAl=Align(1), bool StackReal=true)
Align getStackAlign() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
int alignSPAdjust(int SPAdj) const
alignSPAdjust - This method aligns the stack adjustment to the correct alignment.
TargetInstrInfo - Interface to description of machine instruction set.
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 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
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:558
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:1738
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:1668
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
@ Implicit
Not emitted register (e.g. carry, or temporary result).
@ Kill
The last use of a register.
@ Define
Register definition.
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:1752
auto make_first_range(ContainerTy &&c)
Given a container of pairs, return a range over the first elements.
Definition STLExtras.h:1398
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
uint64_t offsetToAlignment(uint64_t Value, Align Alignment)
Returns the offset to the next integer (mod 2**64) that is greater than or equal to Value and is a mu...
Definition Alignment.h:186
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
auto 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:1771
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1946
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