LLVM 20.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"
15#include "RISCVSubtarget.h"
24#include "llvm/MC/MCDwarf.h"
25#include "llvm/Support/LEB128.h"
26
27#include <algorithm>
28
29using namespace llvm;
30
31namespace {
32
33class CFISaveRegisterEmitter {
36
37public:
38 CFISaveRegisterEmitter(MachineFunction &MF)
39 : MF{MF}, MFI{MF.getFrameInfo()} {};
40
42 const RISCVRegisterInfo &RI, const RISCVInstrInfo &TII,
43 const DebugLoc &DL, const CalleeSavedInfo &CS) const {
44 int FrameIdx = CS.getFrameIdx();
45 int64_t Offset = MFI.getObjectOffset(FrameIdx);
46 Register Reg = CS.getReg();
47 unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
48 nullptr, RI.getDwarfRegNum(Reg, true), Offset));
49 BuildMI(MBB, MBBI, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
50 .addCFIIndex(CFIIndex)
52 }
53};
54
55class CFIRestoreRegisterEmitter {
57
58public:
59 CFIRestoreRegisterEmitter(MachineFunction &MF) : MF{MF} {};
60
62 const RISCVRegisterInfo &RI, const RISCVInstrInfo &TII,
63 const DebugLoc &DL, const CalleeSavedInfo &CS) const {
64 Register Reg = CS.getReg();
65 unsigned CFIIndex = MF.addFrameInst(
66 MCCFIInstruction::createRestore(nullptr, RI.getDwarfRegNum(Reg, true)));
67 BuildMI(MBB, MBBI, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
68 .addCFIIndex(CFIIndex)
70 }
71};
72
73} // namespace
74
75template <typename Emitter>
76void RISCVFrameLowering::emitCFIForCSI(
78 const SmallVector<CalleeSavedInfo, 8> &CSI) const {
83
84 Emitter E{*MF};
85 for (const auto &CS : CSI)
86 E.emit(MBB, MBBI, *RI, *TII, DL, CS);
87}
88
90 if (ABI == RISCVABI::ABI_ILP32E)
91 return Align(4);
92 if (ABI == RISCVABI::ABI_LP64E)
93 return Align(8);
94 return Align(16);
95}
96
99 StackGrowsDown, getABIStackAlignment(STI.getTargetABI()),
100 /*LocalAreaOffset=*/0,
101 /*TransientStackAlignment=*/getABIStackAlignment(STI.getTargetABI())),
102 STI(STI) {}
103
104// The register used to hold the frame pointer.
105static constexpr Register FPReg = RISCV::X8;
106
107// The register used to hold the stack pointer.
108static constexpr Register SPReg = RISCV::X2;
109
110// The register used to hold the return address.
111static constexpr Register RAReg = RISCV::X1;
112
113// Offsets which need to be scale by XLen representing locations of CSRs which
114// are given a fixed location by save/restore libcalls or Zcmp Push/Pop.
115static const std::pair<MCPhysReg, int8_t> FixedCSRFIMap[] = {
116 {/*ra*/ RAReg, -1}, {/*s0*/ FPReg, -2},
117 {/*s1*/ RISCV::X9, -3}, {/*s2*/ RISCV::X18, -4},
118 {/*s3*/ RISCV::X19, -5}, {/*s4*/ RISCV::X20, -6},
119 {/*s5*/ RISCV::X21, -7}, {/*s6*/ RISCV::X22, -8},
120 {/*s7*/ RISCV::X23, -9}, {/*s8*/ RISCV::X24, -10},
121 {/*s9*/ RISCV::X25, -11}, {/*s10*/ RISCV::X26, -12},
122 {/*s11*/ RISCV::X27, -13}};
123
124// For now we use x3, a.k.a gp, as pointer to shadow call stack.
125// User should not use x3 in their asm.
128 const DebugLoc &DL) {
129 const auto &STI = MF.getSubtarget<RISCVSubtarget>();
130 bool HasHWShadowStack = MF.getFunction().hasFnAttribute("hw-shadow-stack") &&
131 STI.hasStdExtZicfiss();
132 bool HasSWShadowStack =
133 MF.getFunction().hasFnAttribute(Attribute::ShadowCallStack);
134 if (!HasHWShadowStack && !HasSWShadowStack)
135 return;
136
137 const llvm::RISCVRegisterInfo *TRI = STI.getRegisterInfo();
138 Register RAReg = TRI->getRARegister();
139
140 // Do not save RA to the SCS if it's not saved to the regular stack,
141 // i.e. RA is not at risk of being overwritten.
142 std::vector<CalleeSavedInfo> &CSI = MF.getFrameInfo().getCalleeSavedInfo();
143 if (llvm::none_of(
144 CSI, [&](CalleeSavedInfo &CSR) { return CSR.getReg() == RAReg; }))
145 return;
146
147 const RISCVInstrInfo *TII = STI.getInstrInfo();
148 if (HasHWShadowStack) {
149 BuildMI(MBB, MI, DL, TII->get(RISCV::SSPUSH)).addReg(RAReg);
150 return;
151 }
152
153 Register SCSPReg = RISCVABI::getSCSPReg();
154
155 bool IsRV64 = STI.is64Bit();
156 int64_t SlotSize = STI.getXLen() / 8;
157 // Store return address to shadow call stack
158 // addi gp, gp, [4|8]
159 // s[w|d] ra, -[4|8](gp)
160 BuildMI(MBB, MI, DL, TII->get(RISCV::ADDI))
161 .addReg(SCSPReg, RegState::Define)
162 .addReg(SCSPReg)
163 .addImm(SlotSize)
165 BuildMI(MBB, MI, DL, TII->get(IsRV64 ? RISCV::SD : RISCV::SW))
166 .addReg(RAReg)
167 .addReg(SCSPReg)
168 .addImm(-SlotSize)
170
171 // Emit a CFI instruction that causes SlotSize to be subtracted from the value
172 // of the shadow stack pointer when unwinding past this frame.
173 char DwarfSCSReg = TRI->getDwarfRegNum(SCSPReg, /*IsEH*/ true);
174 assert(DwarfSCSReg < 32 && "SCS Register should be < 32 (X3).");
175
176 char Offset = static_cast<char>(-SlotSize) & 0x7f;
177 const char CFIInst[] = {
178 dwarf::DW_CFA_val_expression,
179 DwarfSCSReg, // register
180 2, // length
181 static_cast<char>(unsigned(dwarf::DW_OP_breg0 + DwarfSCSReg)),
182 Offset, // addend (sleb128)
183 };
184
185 unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createEscape(
186 nullptr, StringRef(CFIInst, sizeof(CFIInst))));
187 BuildMI(MBB, MI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
188 .addCFIIndex(CFIIndex)
190}
191
194 const DebugLoc &DL) {
195 const auto &STI = MF.getSubtarget<RISCVSubtarget>();
196 bool HasHWShadowStack = MF.getFunction().hasFnAttribute("hw-shadow-stack") &&
197 STI.hasStdExtZicfiss();
198 bool HasSWShadowStack =
199 MF.getFunction().hasFnAttribute(Attribute::ShadowCallStack);
200 if (!HasHWShadowStack && !HasSWShadowStack)
201 return;
202
203 Register RAReg = STI.getRegisterInfo()->getRARegister();
204
205 // See emitSCSPrologue() above.
206 std::vector<CalleeSavedInfo> &CSI = MF.getFrameInfo().getCalleeSavedInfo();
207 if (llvm::none_of(
208 CSI, [&](CalleeSavedInfo &CSR) { return CSR.getReg() == RAReg; }))
209 return;
210
211 const RISCVInstrInfo *TII = STI.getInstrInfo();
212 if (HasHWShadowStack) {
213 BuildMI(MBB, MI, DL, TII->get(RISCV::SSPOPCHK)).addReg(RAReg);
214 return;
215 }
216
217 Register SCSPReg = RISCVABI::getSCSPReg();
218
219 bool IsRV64 = STI.is64Bit();
220 int64_t SlotSize = STI.getXLen() / 8;
221 // Load return address from shadow call stack
222 // l[w|d] ra, -[4|8](gp)
223 // addi gp, gp, -[4|8]
224 BuildMI(MBB, MI, DL, TII->get(IsRV64 ? RISCV::LD : RISCV::LW))
226 .addReg(SCSPReg)
227 .addImm(-SlotSize)
229 BuildMI(MBB, MI, DL, TII->get(RISCV::ADDI))
230 .addReg(SCSPReg, RegState::Define)
231 .addReg(SCSPReg)
232 .addImm(-SlotSize)
234 // Restore the SCS pointer
235 unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createRestore(
236 nullptr, STI.getRegisterInfo()->getDwarfRegNum(SCSPReg, /*IsEH*/ true)));
237 BuildMI(MBB, MI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
238 .addCFIIndex(CFIIndex)
240}
241
242// Get the ID of the libcall used for spilling and restoring callee saved
243// registers. The ID is representative of the number of registers saved or
244// restored by the libcall, except it is zero-indexed - ID 0 corresponds to a
245// single register.
246static int getLibCallID(const MachineFunction &MF,
247 const std::vector<CalleeSavedInfo> &CSI) {
248 const auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
249
250 if (CSI.empty() || !RVFI->useSaveRestoreLibCalls(MF))
251 return -1;
252
253 Register MaxReg = RISCV::NoRegister;
254 for (auto &CS : CSI)
255 // assignCalleeSavedSpillSlots assigns negative frame indexes to
256 // registers which can be saved by libcall.
257 if (CS.getFrameIdx() < 0)
258 MaxReg = std::max(MaxReg.id(), CS.getReg().id());
259
260 if (MaxReg == RISCV::NoRegister)
261 return -1;
262
263 switch (MaxReg) {
264 default:
265 llvm_unreachable("Something has gone wrong!");
266 // clang-format off
267 case /*s11*/ RISCV::X27: return 12;
268 case /*s10*/ RISCV::X26: return 11;
269 case /*s9*/ RISCV::X25: return 10;
270 case /*s8*/ RISCV::X24: return 9;
271 case /*s7*/ RISCV::X23: return 8;
272 case /*s6*/ RISCV::X22: return 7;
273 case /*s5*/ RISCV::X21: return 6;
274 case /*s4*/ RISCV::X20: return 5;
275 case /*s3*/ RISCV::X19: return 4;
276 case /*s2*/ RISCV::X18: return 3;
277 case /*s1*/ RISCV::X9: return 2;
278 case /*s0*/ FPReg: return 1;
279 case /*ra*/ RAReg: return 0;
280 // clang-format on
281 }
282}
283
284// Get the name of the libcall used for spilling callee saved registers.
285// If this function will not use save/restore libcalls, then return a nullptr.
286static const char *
288 const std::vector<CalleeSavedInfo> &CSI) {
289 static const char *const SpillLibCalls[] = {
290 "__riscv_save_0",
291 "__riscv_save_1",
292 "__riscv_save_2",
293 "__riscv_save_3",
294 "__riscv_save_4",
295 "__riscv_save_5",
296 "__riscv_save_6",
297 "__riscv_save_7",
298 "__riscv_save_8",
299 "__riscv_save_9",
300 "__riscv_save_10",
301 "__riscv_save_11",
302 "__riscv_save_12"
303 };
304
305 int LibCallID = getLibCallID(MF, CSI);
306 if (LibCallID == -1)
307 return nullptr;
308 return SpillLibCalls[LibCallID];
309}
310
311// Get the name of the libcall used for restoring callee saved registers.
312// If this function will not use save/restore libcalls, then return a nullptr.
313static const char *
315 const std::vector<CalleeSavedInfo> &CSI) {
316 static const char *const RestoreLibCalls[] = {
317 "__riscv_restore_0",
318 "__riscv_restore_1",
319 "__riscv_restore_2",
320 "__riscv_restore_3",
321 "__riscv_restore_4",
322 "__riscv_restore_5",
323 "__riscv_restore_6",
324 "__riscv_restore_7",
325 "__riscv_restore_8",
326 "__riscv_restore_9",
327 "__riscv_restore_10",
328 "__riscv_restore_11",
329 "__riscv_restore_12"
330 };
331
332 int LibCallID = getLibCallID(MF, CSI);
333 if (LibCallID == -1)
334 return nullptr;
335 return RestoreLibCalls[LibCallID];
336}
337
338// Return encoded value and register count for PUSH/POP instruction,
339// representing registers to store/load.
340static std::pair<unsigned, unsigned>
342 switch (MaxReg) {
343 default:
344 llvm_unreachable("Unexpected Reg for Push/Pop Inst");
345 case RISCV::X27: /*s11*/
346 return std::make_pair(llvm::RISCVZC::RLISTENCODE::RA_S0_S11, 13);
347 case RISCV::X25: /*s9*/
348 return std::make_pair(llvm::RISCVZC::RLISTENCODE::RA_S0_S9, 11);
349 case RISCV::X24: /*s8*/
350 return std::make_pair(llvm::RISCVZC::RLISTENCODE::RA_S0_S8, 10);
351 case RISCV::X23: /*s7*/
352 return std::make_pair(llvm::RISCVZC::RLISTENCODE::RA_S0_S7, 9);
353 case RISCV::X22: /*s6*/
354 return std::make_pair(llvm::RISCVZC::RLISTENCODE::RA_S0_S6, 8);
355 case RISCV::X21: /*s5*/
356 return std::make_pair(llvm::RISCVZC::RLISTENCODE::RA_S0_S5, 7);
357 case RISCV::X20: /*s4*/
358 return std::make_pair(llvm::RISCVZC::RLISTENCODE::RA_S0_S4, 6);
359 case RISCV::X19: /*s3*/
360 return std::make_pair(llvm::RISCVZC::RLISTENCODE::RA_S0_S3, 5);
361 case RISCV::X18: /*s2*/
362 return std::make_pair(llvm::RISCVZC::RLISTENCODE::RA_S0_S2, 4);
363 case RISCV::X9: /*s1*/
364 return std::make_pair(llvm::RISCVZC::RLISTENCODE::RA_S0_S1, 3);
365 case FPReg: /*s0*/
366 return std::make_pair(llvm::RISCVZC::RLISTENCODE::RA_S0, 2);
367 case RAReg: /*ra*/
368 return std::make_pair(llvm::RISCVZC::RLISTENCODE::RA, 1);
369 }
370}
371
372// Get the max reg of Push/Pop for restoring callee saved registers.
374 const std::vector<CalleeSavedInfo> &CSI) {
375 Register MaxPushPopReg = RISCV::NoRegister;
376 for (auto &CS : CSI) {
377 if (llvm::find_if(FixedCSRFIMap, [&](auto P) {
378 return P.first == CS.getReg();
379 }) != std::end(FixedCSRFIMap))
380 MaxPushPopReg = std::max(MaxPushPopReg.id(), CS.getReg().id());
381 }
382 assert(MaxPushPopReg != RISCV::X26 && "x26 requires x27 to also be pushed");
383 return MaxPushPopReg;
384}
385
386// Return true if the specified function should have a dedicated frame
387// pointer register. This is true if frame pointer elimination is
388// disabled, if it needs dynamic stack realignment, if the function has
389// variable sized allocas, or if the frame address is taken.
391 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
392
393 const MachineFrameInfo &MFI = MF.getFrameInfo();
394 return MF.getTarget().Options.DisableFramePointerElim(MF) ||
395 RegInfo->hasStackRealignment(MF) || MFI.hasVarSizedObjects() ||
397}
398
400 const MachineFrameInfo &MFI = MF.getFrameInfo();
402
403 // If we do not reserve stack space for outgoing arguments in prologue,
404 // we will adjust the stack pointer before call instruction. After the
405 // adjustment, we can not use SP to access the stack objects for the
406 // arguments. Instead, use BP to access these stack objects.
407 return (MFI.hasVarSizedObjects() ||
409 MFI.getMaxCallFrameSize() != 0))) &&
410 TRI->hasStackRealignment(MF);
411}
412
413// Determines the size of the frame and maximum call frame size.
414void RISCVFrameLowering::determineFrameLayout(MachineFunction &MF) const {
415 MachineFrameInfo &MFI = MF.getFrameInfo();
416 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
417
418 // Get the number of bytes to allocate from the FrameInfo.
419 uint64_t FrameSize = MFI.getStackSize();
420
421 // Get the alignment.
422 Align StackAlign = getStackAlign();
423
424 // Make sure the frame is aligned.
425 FrameSize = alignTo(FrameSize, StackAlign);
426
427 // Update frame info.
428 MFI.setStackSize(FrameSize);
429
430 // When using SP or BP to access stack objects, we may require extra padding
431 // to ensure the bottom of the RVV stack is correctly aligned within the main
432 // stack. We calculate this as the amount required to align the scalar local
433 // variable section up to the RVV alignment.
435 if (RVFI->getRVVStackSize() && (!hasFP(MF) || TRI->hasStackRealignment(MF))) {
436 int ScalarLocalVarSize = FrameSize - RVFI->getCalleeSavedStackSize() -
437 RVFI->getVarArgsSaveSize();
438 if (auto RVVPadding =
439 offsetToAlignment(ScalarLocalVarSize, RVFI->getRVVStackAlign()))
440 RVFI->setRVVPadding(RVVPadding);
441 }
442}
443
444// Returns the stack size including RVV padding (when required), rounded back
445// up to the required stack alignment.
447 const MachineFunction &MF) const {
448 const MachineFrameInfo &MFI = MF.getFrameInfo();
449 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
450 return alignTo(MFI.getStackSize() + RVFI->getRVVPadding(), getStackAlign());
451}
452
455 const std::vector<CalleeSavedInfo> &CSI) {
456 const MachineFrameInfo &MFI = MF.getFrameInfo();
458
459 for (auto &CS : CSI) {
460 int FI = CS.getFrameIdx();
461 if (FI >= 0 && MFI.getStackID(FI) == TargetStackID::Default)
462 NonLibcallCSI.push_back(CS);
463 }
464
465 return NonLibcallCSI;
466}
467
470 const std::vector<CalleeSavedInfo> &CSI) {
471 const MachineFrameInfo &MFI = MF.getFrameInfo();
473
474 for (auto &CS : CSI) {
475 int FI = CS.getFrameIdx();
476 if (FI >= 0 && MFI.getStackID(FI) == TargetStackID::ScalableVector)
477 RVVCSI.push_back(CS);
478 }
479
480 return RVVCSI;
481}
482
485 const std::vector<CalleeSavedInfo> &CSI) {
486 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
487
488 SmallVector<CalleeSavedInfo, 8> PushOrLibCallsCSI;
489 if (!RVFI->useSaveRestoreLibCalls(MF) && !RVFI->isPushable(MF))
490 return PushOrLibCallsCSI;
491
492 for (const auto &CS : CSI) {
493 const auto *FII = llvm::find_if(
494 FixedCSRFIMap, [&](auto P) { return P.first == CS.getReg(); });
495 if (FII != std::end(FixedCSRFIMap))
496 PushOrLibCallsCSI.push_back(CS);
497 }
498
499 return PushOrLibCallsCSI;
500}
501
502void RISCVFrameLowering::allocateAndProbeStackForRVV(
504 MachineBasicBlock::iterator MBBI, const DebugLoc &DL, int64_t Amount,
505 MachineInstr::MIFlag Flag, bool EmitCFI) const {
506 assert(Amount != 0 && "Did not need to adjust stack pointer for RVV.");
507
508 // Emit a variable-length allocation probing loop.
509
510 // Get VLEN in TargetReg
512 Register TargetReg = RISCV::X6;
513 uint32_t NumOfVReg = Amount / (RISCV::RVVBitsPerBlock / 8);
514 BuildMI(MBB, MBBI, DL, TII->get(RISCV::PseudoReadVLENB), TargetReg)
515 .setMIFlag(Flag);
516 TII->mulImm(MF, MBB, MBBI, DL, TargetReg, NumOfVReg, Flag);
517
518 if (EmitCFI) {
519 // Set the CFA register to TargetReg.
520 unsigned Reg = STI.getRegisterInfo()->getDwarfRegNum(TargetReg, true);
521 unsigned CFIIndex =
522 MF.addFrameInst(MCCFIInstruction::cfiDefCfa(nullptr, Reg, -Amount));
523 BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
524 .addCFIIndex(CFIIndex)
526 }
527
528 // It will be expanded to a probe loop in `inlineStackProbe`.
529 BuildMI(MBB, MBBI, DL, TII->get(RISCV::PROBED_STACKALLOC_RVV))
530 .addReg(SPReg)
531 .addReg(TargetReg);
532
533 if (EmitCFI) {
534 // Set the CFA register back to SP.
535 unsigned Reg = STI.getRegisterInfo()->getDwarfRegNum(SPReg, true);
536 unsigned CFIIndex =
538 BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
539 .addCFIIndex(CFIIndex)
541 }
542
543 // SUB SP, SP, T1
544 BuildMI(MBB, MBBI, DL, TII->get(RISCV::SUB), SPReg)
545 .addReg(SPReg)
546 .addReg(TargetReg)
547 .setMIFlag(Flag);
548}
549
552 int FixedOffset, int ScalableOffset,
553 llvm::raw_string_ostream &Comment) {
554 unsigned DwarfVLenB = TRI.getDwarfRegNum(RISCV::VLENB, true);
555 uint8_t Buffer[16];
556 if (FixedOffset) {
557 Expr.push_back(dwarf::DW_OP_consts);
558 Expr.append(Buffer, Buffer + encodeSLEB128(FixedOffset, Buffer));
559 Expr.push_back((uint8_t)dwarf::DW_OP_plus);
560 Comment << (FixedOffset < 0 ? " - " : " + ") << std::abs(FixedOffset);
561 }
562
563 Expr.push_back((uint8_t)dwarf::DW_OP_consts);
564 Expr.append(Buffer, Buffer + encodeSLEB128(ScalableOffset, Buffer));
565
566 Expr.push_back((uint8_t)dwarf::DW_OP_bregx);
567 Expr.append(Buffer, Buffer + encodeULEB128(DwarfVLenB, Buffer));
568 Expr.push_back(0);
569
570 Expr.push_back((uint8_t)dwarf::DW_OP_mul);
571 Expr.push_back((uint8_t)dwarf::DW_OP_plus);
572
573 Comment << (ScalableOffset < 0 ? " - " : " + ") << std::abs(ScalableOffset)
574 << " * vlenb";
575}
576
578 Register Reg,
579 uint64_t FixedOffset,
580 uint64_t ScalableOffset) {
581 assert(ScalableOffset != 0 && "Did not need to adjust CFA for RVV");
582 SmallString<64> Expr;
583 std::string CommentBuffer;
584 llvm::raw_string_ostream Comment(CommentBuffer);
585 // Build up the expression (Reg + FixedOffset + ScalableOffset * VLENB).
586 unsigned DwarfReg = TRI.getDwarfRegNum(Reg, true);
587 Expr.push_back((uint8_t)(dwarf::DW_OP_breg0 + DwarfReg));
588 Expr.push_back(0);
589 if (Reg == SPReg)
590 Comment << "sp";
591 else
592 Comment << printReg(Reg, &TRI);
593
594 appendScalableVectorExpression(TRI, Expr, FixedOffset, ScalableOffset,
595 Comment);
596
597 SmallString<64> DefCfaExpr;
598 uint8_t Buffer[16];
599 DefCfaExpr.push_back(dwarf::DW_CFA_def_cfa_expression);
600 DefCfaExpr.append(Buffer, Buffer + encodeULEB128(Expr.size(), Buffer));
601 DefCfaExpr.append(Expr.str());
602
603 return MCCFIInstruction::createEscape(nullptr, DefCfaExpr.str(), SMLoc(),
604 Comment.str());
605}
606
608 Register Reg, uint64_t FixedOffset,
609 uint64_t ScalableOffset) {
610 assert(ScalableOffset != 0 && "Did not need to adjust CFA for RVV");
611 SmallString<64> Expr;
612 std::string CommentBuffer;
613 llvm::raw_string_ostream Comment(CommentBuffer);
614 Comment << printReg(Reg, &TRI) << " @ cfa";
615
616 // Build up the expression (FixedOffset + ScalableOffset * VLENB).
617 appendScalableVectorExpression(TRI, Expr, FixedOffset, ScalableOffset,
618 Comment);
619
620 SmallString<64> DefCfaExpr;
621 uint8_t Buffer[16];
622 unsigned DwarfReg = TRI.getDwarfRegNum(Reg, true);
623 DefCfaExpr.push_back(dwarf::DW_CFA_expression);
624 DefCfaExpr.append(Buffer, Buffer + encodeULEB128(DwarfReg, Buffer));
625 DefCfaExpr.append(Buffer, Buffer + encodeULEB128(Expr.size(), Buffer));
626 DefCfaExpr.append(Expr.str());
627
628 return MCCFIInstruction::createEscape(nullptr, DefCfaExpr.str(), SMLoc(),
629 Comment.str());
630}
631
632// Allocate stack space and probe it if necessary.
636 uint64_t RealStackSize, bool EmitCFI,
637 bool NeedProbe,
638 uint64_t ProbeSize) const {
639 DebugLoc DL;
642
643 // Simply allocate the stack if it's not big enough to require a probe.
644 if (!NeedProbe || Offset <= ProbeSize) {
647
648 if (EmitCFI) {
649 // Emit ".cfi_def_cfa_offset RealStackSize"
650 unsigned CFIIndex = MF.addFrameInst(
651 MCCFIInstruction::cfiDefCfaOffset(nullptr, RealStackSize));
652 BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
653 .addCFIIndex(CFIIndex)
655 }
656
657 return;
658 }
659
660 // Unroll the probe loop depending on the number of iterations.
661 if (Offset < ProbeSize * 5) {
662 uint64_t CurrentOffset = 0;
663 bool IsRV64 = STI.is64Bit();
664 while (CurrentOffset + ProbeSize <= Offset) {
665 RI->adjustReg(MBB, MBBI, DL, SPReg, SPReg,
667 getStackAlign());
668 // s[d|w] zero, 0(sp)
669 BuildMI(MBB, MBBI, DL, TII->get(IsRV64 ? RISCV::SD : RISCV::SW))
670 .addReg(RISCV::X0)
671 .addReg(SPReg)
672 .addImm(0)
674
675 CurrentOffset += ProbeSize;
676 if (EmitCFI) {
677 // Emit ".cfi_def_cfa_offset CurrentOffset"
678 unsigned CFIIndex = MF.addFrameInst(
679 MCCFIInstruction::cfiDefCfaOffset(nullptr, CurrentOffset));
680 BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
681 .addCFIIndex(CFIIndex)
683 }
684 }
685
686 uint64_t Residual = Offset - CurrentOffset;
687 if (Residual) {
688 RI->adjustReg(MBB, MBBI, DL, SPReg, SPReg,
690 getStackAlign());
691 if (EmitCFI) {
692 // Emit ".cfi_def_cfa_offset Offset"
693 unsigned CFIIndex =
695 BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
696 .addCFIIndex(CFIIndex)
698 }
699 }
700
701 return;
702 }
703
704 // Emit a variable-length allocation probing loop.
705 uint64_t RoundedSize = alignDown(Offset, ProbeSize);
706 uint64_t Residual = Offset - RoundedSize;
707
708 Register TargetReg = RISCV::X6;
709 // SUB TargetReg, SP, RoundedSize
710 RI->adjustReg(MBB, MBBI, DL, TargetReg, SPReg,
712 getStackAlign());
713
714 if (EmitCFI) {
715 // Set the CFA register to TargetReg.
716 unsigned Reg = STI.getRegisterInfo()->getDwarfRegNum(TargetReg, true);
717 unsigned CFIIndex =
718 MF.addFrameInst(MCCFIInstruction::cfiDefCfa(nullptr, Reg, RoundedSize));
719 BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
720 .addCFIIndex(CFIIndex)
722 }
723
724 // It will be expanded to a probe loop in `inlineStackProbe`.
725 BuildMI(MBB, MBBI, DL, TII->get(RISCV::PROBED_STACKALLOC))
726 .addReg(SPReg)
727 .addReg(TargetReg);
728
729 if (EmitCFI) {
730 // Set the CFA register back to SP.
731 unsigned Reg = STI.getRegisterInfo()->getDwarfRegNum(SPReg, true);
732 unsigned CFIIndex =
734 BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
735 .addCFIIndex(CFIIndex)
737 }
738
739 if (Residual)
742
743 if (EmitCFI) {
744 // Emit ".cfi_def_cfa_offset Offset"
745 unsigned CFIIndex =
747 BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
748 .addCFIIndex(CFIIndex)
750 }
751}
752
754 MachineBasicBlock &MBB) const {
755 MachineFrameInfo &MFI = MF.getFrameInfo();
756 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
760
762
763 // Debug location must be unknown since the first debug location is used
764 // to determine the end of the prologue.
765 DebugLoc DL;
766
767 // All calls are tail calls in GHC calling conv, and functions have no
768 // prologue/epilogue.
770 return;
771
772 // Emit prologue for shadow call stack.
773 emitSCSPrologue(MF, MBB, MBBI, DL);
774
775 auto FirstFrameSetup = MBBI;
776
777 // Since spillCalleeSavedRegisters may have inserted a libcall, skip past
778 // any instructions marked as FrameSetup
779 while (MBBI != MBB.end() && MBBI->getFlag(MachineInstr::FrameSetup))
780 ++MBBI;
781
782 // Determine the correct frame layout
783 determineFrameLayout(MF);
784
785 const auto &CSI = MFI.getCalleeSavedInfo();
786
787 // If libcalls are used to spill and restore callee-saved registers, the frame
788 // has two sections; the opaque section managed by the libcalls, and the
789 // section managed by MachineFrameInfo which can also hold callee saved
790 // registers in fixed stack slots, both of which have negative frame indices.
791 // This gets even more complicated when incoming arguments are passed via the
792 // stack, as these too have negative frame indices. An example is detailed
793 // below:
794 //
795 // | incoming arg | <- FI[-3]
796 // | libcallspill |
797 // | calleespill | <- FI[-2]
798 // | calleespill | <- FI[-1]
799 // | this_frame | <- FI[0]
800 //
801 // For negative frame indices, the offset from the frame pointer will differ
802 // depending on which of these groups the frame index applies to.
803 // The following calculates the correct offset knowing the number of callee
804 // saved registers spilt by the two methods.
805 if (int LibCallRegs = getLibCallID(MF, MFI.getCalleeSavedInfo()) + 1) {
806 // Calculate the size of the frame managed by the libcall. The stack
807 // alignment of these libcalls should be the same as how we set it in
808 // getABIStackAlignment.
809 unsigned LibCallFrameSize =
810 alignTo((STI.getXLen() / 8) * LibCallRegs, getStackAlign());
811 RVFI->setLibCallStackSize(LibCallFrameSize);
812
813 unsigned CFIIndex = MF.addFrameInst(
814 MCCFIInstruction::cfiDefCfaOffset(nullptr, LibCallFrameSize));
815 BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
816 .addCFIIndex(CFIIndex)
818
819 emitCFIForCSI<CFISaveRegisterEmitter>(MBB, MBBI,
821 }
822
823 // FIXME (note copied from Lanai): This appears to be overallocating. Needs
824 // investigation. Get the number of bytes to allocate from the FrameInfo.
825 uint64_t RealStackSize = getStackSizeWithRVVPadding(MF);
826 uint64_t StackSize = RealStackSize - RVFI->getReservedSpillsSize();
827 uint64_t RVVStackSize = RVFI->getRVVStackSize();
828
829 // Early exit if there is no need to allocate on the stack
830 if (RealStackSize == 0 && !MFI.adjustsStack() && RVVStackSize == 0)
831 return;
832
833 // If the stack pointer has been marked as reserved, then produce an error if
834 // the frame requires stack allocation
837 MF.getFunction(), "Stack pointer required, but has been reserved."});
838
839 uint64_t FirstSPAdjustAmount = getFirstSPAdjustAmount(MF);
840 // Split the SP adjustment to reduce the offsets of callee saved spill.
841 if (FirstSPAdjustAmount) {
842 StackSize = FirstSPAdjustAmount;
843 RealStackSize = FirstSPAdjustAmount;
844 }
845
846 if (RVFI->isPushable(MF) && FirstFrameSetup != MBB.end() &&
847 FirstFrameSetup->getOpcode() == RISCV::CM_PUSH) {
848 // Use available stack adjustment in push instruction to allocate additional
849 // stack space. Align the stack size down to a multiple of 16. This is
850 // needed for RVE.
851 // FIXME: Can we increase the stack size to a multiple of 16 instead?
852 uint64_t Spimm =
853 std::min(alignDown(StackSize, 16), static_cast<uint64_t>(48));
854 FirstFrameSetup->getOperand(1).setImm(Spimm);
855 StackSize -= Spimm;
856
857 unsigned CFIIndex = MF.addFrameInst(
858 MCCFIInstruction::cfiDefCfaOffset(nullptr, RealStackSize - StackSize));
859 BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
860 .addCFIIndex(CFIIndex)
862
863 emitCFIForCSI<CFISaveRegisterEmitter>(MBB, MBBI,
865 }
866
867 // Allocate space on the stack if necessary.
868 auto &Subtarget = MF.getSubtarget<RISCVSubtarget>();
869 const RISCVTargetLowering *TLI = Subtarget.getTargetLowering();
870 bool NeedProbe = TLI->hasInlineStackProbe(MF);
871 uint64_t ProbeSize = TLI->getStackProbeSize(MF, getStackAlign());
872 if (StackSize != 0)
873 allocateStack(MBB, MBBI, MF, StackSize, RealStackSize, /*EmitCFI=*/true,
874 NeedProbe, ProbeSize);
875
876 // The frame pointer is callee-saved, and code has been generated for us to
877 // save it to the stack. We need to skip over the storing of callee-saved
878 // registers as the frame pointer must be modified after it has been saved
879 // to the stack, not before.
880 // FIXME: assumes exactly one instruction is used to save each callee-saved
881 // register.
882 std::advance(MBBI, getUnmanagedCSI(MF, CSI).size());
883
884 // Iterate over list of callee-saved registers and emit .cfi_offset
885 // directives.
886 emitCFIForCSI<CFISaveRegisterEmitter>(MBB, MBBI, getUnmanagedCSI(MF, CSI));
887
888 // Generate new FP.
889 if (hasFP(MF)) {
892 MF.getFunction(), "Frame pointer required, but has been reserved."});
893 // The frame pointer does need to be reserved from register allocation.
894 assert(MF.getRegInfo().isReserved(FPReg) && "FP not reserved");
895
896 RI->adjustReg(MBB, MBBI, DL, FPReg, SPReg,
897 StackOffset::getFixed(RealStackSize - RVFI->getVarArgsSaveSize()),
899
900 // Emit ".cfi_def_cfa $fp, RVFI->getVarArgsSaveSize()"
901 unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::cfiDefCfa(
902 nullptr, RI->getDwarfRegNum(FPReg, true), RVFI->getVarArgsSaveSize()));
903 BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
904 .addCFIIndex(CFIIndex)
906 }
907
908 uint64_t SecondSPAdjustAmount = 0;
909 // Emit the second SP adjustment after saving callee saved registers.
910 if (FirstSPAdjustAmount) {
911 SecondSPAdjustAmount = getStackSizeWithRVVPadding(MF) - FirstSPAdjustAmount;
912 assert(SecondSPAdjustAmount > 0 &&
913 "SecondSPAdjustAmount should be greater than zero");
914
915 allocateStack(MBB, MBBI, MF, SecondSPAdjustAmount,
916 getStackSizeWithRVVPadding(MF), !hasFP(MF), NeedProbe,
917 ProbeSize);
918 }
919
920 if (RVVStackSize) {
921 if (NeedProbe) {
922 allocateAndProbeStackForRVV(MF, MBB, MBBI, DL, RVVStackSize,
924 } else {
925 // We must keep the stack pointer aligned through any intermediate
926 // updates.
927 RI->adjustReg(MBB, MBBI, DL, SPReg, SPReg,
928 StackOffset::getScalable(-RVVStackSize),
930 }
931
932 if (!hasFP(MF)) {
933 // Emit .cfi_def_cfa_expression "sp + StackSize + RVVStackSize * vlenb".
934 unsigned CFIIndex = MF.addFrameInst(createDefCFAExpression(
935 *RI, SPReg, getStackSizeWithRVVPadding(MF), RVVStackSize / 8));
936 BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
937 .addCFIIndex(CFIIndex)
939 }
940
941 std::advance(MBBI, getRVVCalleeSavedInfo(MF, CSI).size());
942 emitCalleeSavedRVVPrologCFI(MBB, MBBI, hasFP(MF));
943 }
944
945 if (hasFP(MF)) {
946 // Realign Stack
948 if (RI->hasStackRealignment(MF)) {
949 Align MaxAlignment = MFI.getMaxAlign();
950
952 if (isInt<12>(-(int)MaxAlignment.value())) {
953 BuildMI(MBB, MBBI, DL, TII->get(RISCV::ANDI), SPReg)
954 .addReg(SPReg)
955 .addImm(-(int)MaxAlignment.value())
957 } else {
958 unsigned ShiftAmount = Log2(MaxAlignment);
959 Register VR =
960 MF.getRegInfo().createVirtualRegister(&RISCV::GPRRegClass);
961 BuildMI(MBB, MBBI, DL, TII->get(RISCV::SRLI), VR)
962 .addReg(SPReg)
963 .addImm(ShiftAmount)
965 BuildMI(MBB, MBBI, DL, TII->get(RISCV::SLLI), SPReg)
966 .addReg(VR)
967 .addImm(ShiftAmount)
969 }
970 if (NeedProbe && RVVStackSize == 0) {
971 // Do a probe if the align + size allocated just passed the probe size
972 // and was not yet probed.
973 if (SecondSPAdjustAmount < ProbeSize &&
974 SecondSPAdjustAmount + MaxAlignment.value() >= ProbeSize) {
975 bool IsRV64 = STI.is64Bit();
976 BuildMI(MBB, MBBI, DL, TII->get(IsRV64 ? RISCV::SD : RISCV::SW))
977 .addReg(RISCV::X0)
978 .addReg(SPReg)
979 .addImm(0)
981 }
982 }
983 // FP will be used to restore the frame in the epilogue, so we need
984 // another base register BP to record SP after re-alignment. SP will
985 // track the current stack after allocating variable sized objects.
986 if (hasBP(MF)) {
987 // move BP, SP
988 BuildMI(MBB, MBBI, DL, TII->get(RISCV::ADDI), BPReg)
989 .addReg(SPReg)
990 .addImm(0)
992 }
993 }
994 }
995}
996
997void RISCVFrameLowering::deallocateStack(MachineFunction &MF,
1000 const DebugLoc &DL,
1001 uint64_t &StackSize,
1002 int64_t CFAOffset) const {
1005
1006 RI->adjustReg(MBB, MBBI, DL, SPReg, SPReg, StackOffset::getFixed(StackSize),
1008 StackSize = 0;
1009
1010 unsigned CFIIndex =
1011 MF.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(nullptr, CFAOffset));
1012 BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
1013 .addCFIIndex(CFIIndex)
1015}
1016
1018 MachineBasicBlock &MBB) const {
1020 MachineFrameInfo &MFI = MF.getFrameInfo();
1021 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
1023
1024 // All calls are tail calls in GHC calling conv, and functions have no
1025 // prologue/epilogue.
1027 return;
1028
1029 // Get the insert location for the epilogue. If there were no terminators in
1030 // the block, get the last instruction.
1032 DebugLoc DL;
1033 if (!MBB.empty()) {
1035 if (MBBI != MBB.end())
1036 DL = MBBI->getDebugLoc();
1037
1039
1040 // If callee-saved registers are saved via libcall, place stack adjustment
1041 // before this call.
1042 while (MBBI != MBB.begin() &&
1043 std::prev(MBBI)->getFlag(MachineInstr::FrameDestroy))
1044 --MBBI;
1045 }
1046
1047 const auto &CSI = MFI.getCalleeSavedInfo();
1048
1049 // Skip to before the restores of scalar callee-saved registers
1050 // FIXME: assumes exactly one instruction is used to restore each
1051 // callee-saved register.
1052 auto LastFrameDestroy = std::prev(MBBI, getUnmanagedCSI(MF, CSI).size());
1053
1054 uint64_t FirstSPAdjustAmount = getFirstSPAdjustAmount(MF);
1055 uint64_t RealStackSize = FirstSPAdjustAmount ? FirstSPAdjustAmount
1057 uint64_t StackSize = FirstSPAdjustAmount ? FirstSPAdjustAmount
1059 RVFI->getReservedSpillsSize();
1060 uint64_t FPOffset = RealStackSize - RVFI->getVarArgsSaveSize();
1061 uint64_t RVVStackSize = RVFI->getRVVStackSize();
1062
1063 bool RestoreSPFromFP = RI->hasStackRealignment(MF) ||
1065 if (RVVStackSize) {
1066 // If RestoreSPFromFP the stack pointer will be restored using the frame
1067 // pointer value.
1068 if (!RestoreSPFromFP)
1069 RI->adjustReg(MBB, LastFrameDestroy, DL, SPReg, SPReg,
1070 StackOffset::getScalable(RVVStackSize),
1072
1073 if (!hasFP(MF)) {
1074 unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::cfiDefCfa(
1075 nullptr, RI->getDwarfRegNum(SPReg, true), RealStackSize));
1076 BuildMI(MBB, LastFrameDestroy, DL,
1077 TII->get(TargetOpcode::CFI_INSTRUCTION))
1078 .addCFIIndex(CFIIndex)
1080 }
1081
1082 emitCalleeSavedRVVEpilogCFI(MBB, LastFrameDestroy);
1083 }
1084
1085 if (FirstSPAdjustAmount) {
1086 uint64_t SecondSPAdjustAmount =
1087 getStackSizeWithRVVPadding(MF) - FirstSPAdjustAmount;
1088 assert(SecondSPAdjustAmount > 0 &&
1089 "SecondSPAdjustAmount should be greater than zero");
1090
1091 // If RestoreSPFromFP the stack pointer will be restored using the frame
1092 // pointer value.
1093 if (!RestoreSPFromFP)
1094 RI->adjustReg(MBB, LastFrameDestroy, DL, SPReg, SPReg,
1095 StackOffset::getFixed(SecondSPAdjustAmount),
1097
1098 if (!hasFP(MF)) {
1099 unsigned CFIIndex = MF.addFrameInst(
1100 MCCFIInstruction::cfiDefCfaOffset(nullptr, FirstSPAdjustAmount));
1101 BuildMI(MBB, LastFrameDestroy, DL,
1102 TII->get(TargetOpcode::CFI_INSTRUCTION))
1103 .addCFIIndex(CFIIndex)
1105 }
1106 }
1107
1108 // Restore the stack pointer using the value of the frame pointer. Only
1109 // necessary if the stack pointer was modified, meaning the stack size is
1110 // unknown.
1111 //
1112 // In order to make sure the stack point is right through the EH region,
1113 // we also need to restore stack pointer from the frame pointer if we
1114 // don't preserve stack space within prologue/epilogue for outgoing variables,
1115 // normally it's just checking the variable sized object is present or not
1116 // is enough, but we also don't preserve that at prologue/epilogue when
1117 // have vector objects in stack.
1118 if (RestoreSPFromFP) {
1119 assert(hasFP(MF) && "frame pointer should not have been eliminated");
1120 RI->adjustReg(MBB, LastFrameDestroy, DL, SPReg, FPReg,
1122 getStackAlign());
1123 }
1124
1125 if (hasFP(MF)) {
1126 unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::cfiDefCfa(
1127 nullptr, RI->getDwarfRegNum(SPReg, true), RealStackSize));
1128 BuildMI(MBB, LastFrameDestroy, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
1129 .addCFIIndex(CFIIndex)
1131 }
1132
1133 if (getLibCallID(MF, CSI) != -1) {
1134 // tail __riscv_restore_[0-12] instruction is considered as a terminator,
1135 // therefor it is unnecessary to place any CFI instructions after it. Just
1136 // deallocate stack if needed and return.
1137 if (StackSize != 0)
1138 deallocateStack(MF, MBB, MBBI, DL, StackSize,
1139 RVFI->getLibCallStackSize());
1140
1141 // Emit epilogue for shadow call stack.
1142 emitSCSEpilogue(MF, MBB, MBBI, DL);
1143 return;
1144 }
1145
1146 // Recover callee-saved registers.
1147 emitCFIForCSI<CFIRestoreRegisterEmitter>(MBB, MBBI, getUnmanagedCSI(MF, CSI));
1148
1149 bool ApplyPop = RVFI->isPushable(MF) && MBBI != MBB.end() &&
1150 MBBI->getOpcode() == RISCV::CM_POP;
1151 if (ApplyPop) {
1152 // Use available stack adjustment in pop instruction to deallocate stack
1153 // space. Align the stack size down to a multiple of 16. This is needed for
1154 // RVE.
1155 // FIXME: Can we increase the stack size to a multiple of 16 instead?
1156 uint64_t Spimm =
1157 std::min(alignDown(StackSize, 16), static_cast<uint64_t>(48));
1158 MBBI->getOperand(1).setImm(Spimm);
1159 StackSize -= Spimm;
1160
1161 if (StackSize != 0)
1162 deallocateStack(MF, MBB, MBBI, DL, StackSize,
1163 /*stack_adj of cm.pop instr*/ RealStackSize - StackSize);
1164
1165 auto NextI = next_nodbg(MBBI, MBB.end());
1166 if (NextI == MBB.end() || NextI->getOpcode() != RISCV::PseudoRET) {
1167 ++MBBI;
1168
1169 emitCFIForCSI<CFIRestoreRegisterEmitter>(
1171
1172 // Update CFA offset. After CM_POP SP should be equal to CFA, so CFA
1173 // offset should be a zero.
1174 unsigned CFIIndex =
1176 BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
1177 .addCFIIndex(CFIIndex)
1179 }
1180 }
1181
1182 // Deallocate stack if StackSize isn't a zero yet
1183 if (StackSize != 0)
1184 deallocateStack(MF, MBB, MBBI, DL, StackSize, 0);
1185
1186 // Emit epilogue for shadow call stack.
1187 emitSCSEpilogue(MF, MBB, MBBI, DL);
1188}
1189
1192 Register &FrameReg) const {
1193 const MachineFrameInfo &MFI = MF.getFrameInfo();
1195 const auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
1196
1197 // Callee-saved registers should be referenced relative to the stack
1198 // pointer (positive offset), otherwise use the frame pointer (negative
1199 // offset).
1200 const auto &CSI = getUnmanagedCSI(MF, MFI.getCalleeSavedInfo());
1201 int MinCSFI = 0;
1202 int MaxCSFI = -1;
1204 auto StackID = MFI.getStackID(FI);
1205
1206 assert((StackID == TargetStackID::Default ||
1207 StackID == TargetStackID::ScalableVector) &&
1208 "Unexpected stack ID for the frame object.");
1209 if (StackID == TargetStackID::Default) {
1210 assert(getOffsetOfLocalArea() == 0 && "LocalAreaOffset is not 0!");
1212 MFI.getOffsetAdjustment());
1213 } else if (StackID == TargetStackID::ScalableVector) {
1215 }
1216
1217 uint64_t FirstSPAdjustAmount = getFirstSPAdjustAmount(MF);
1218
1219 if (CSI.size()) {
1220 MinCSFI = CSI[0].getFrameIdx();
1221 MaxCSFI = CSI[CSI.size() - 1].getFrameIdx();
1222 }
1223
1224 if (FI >= MinCSFI && FI <= MaxCSFI) {
1225 FrameReg = SPReg;
1226
1227 if (FirstSPAdjustAmount)
1228 Offset += StackOffset::getFixed(FirstSPAdjustAmount);
1229 else
1231 return Offset;
1232 }
1233
1234 if (RI->hasStackRealignment(MF) && !MFI.isFixedObjectIndex(FI)) {
1235 // If the stack was realigned, the frame pointer is set in order to allow
1236 // SP to be restored, so we need another base register to record the stack
1237 // after realignment.
1238 // |--------------------------| -- <-- FP
1239 // | callee-allocated save | | <----|
1240 // | area for register varargs| | |
1241 // |--------------------------| | |
1242 // | callee-saved registers | | |
1243 // |--------------------------| -- |
1244 // | realignment (the size of | | |
1245 // | this area is not counted | | |
1246 // | in MFI.getStackSize()) | | |
1247 // |--------------------------| -- |-- MFI.getStackSize()
1248 // | RVV alignment padding | | |
1249 // | (not counted in | | |
1250 // | MFI.getStackSize() but | | |
1251 // | counted in | | |
1252 // | RVFI.getRVVStackSize()) | | |
1253 // |--------------------------| -- |
1254 // | RVV objects | | |
1255 // | (not counted in | | |
1256 // | MFI.getStackSize()) | | |
1257 // |--------------------------| -- |
1258 // | padding before RVV | | |
1259 // | (not counted in | | |
1260 // | MFI.getStackSize() or in | | |
1261 // | RVFI.getRVVStackSize()) | | |
1262 // |--------------------------| -- |
1263 // | scalar local variables | | <----'
1264 // |--------------------------| -- <-- BP (if var sized objects present)
1265 // | VarSize objects | |
1266 // |--------------------------| -- <-- SP
1267 if (hasBP(MF)) {
1268 FrameReg = RISCVABI::getBPReg();
1269 } else {
1270 // VarSize objects must be empty in this case!
1271 assert(!MFI.hasVarSizedObjects());
1272 FrameReg = SPReg;
1273 }
1274 } else {
1275 FrameReg = RI->getFrameRegister(MF);
1276 }
1277
1278 if (FrameReg == FPReg) {
1279 Offset += StackOffset::getFixed(RVFI->getVarArgsSaveSize());
1280 // When using FP to access scalable vector objects, we need to minus
1281 // the frame size.
1282 //
1283 // |--------------------------| -- <-- FP
1284 // | callee-allocated save | |
1285 // | area for register varargs| |
1286 // |--------------------------| |
1287 // | callee-saved registers | |
1288 // |--------------------------| | MFI.getStackSize()
1289 // | scalar local variables | |
1290 // |--------------------------| -- (Offset of RVV objects is from here.)
1291 // | RVV objects |
1292 // |--------------------------|
1293 // | VarSize objects |
1294 // |--------------------------| <-- SP
1296 assert(!RI->hasStackRealignment(MF) &&
1297 "Can't index across variable sized realign");
1298 // We don't expect any extra RVV alignment padding, as the stack size
1299 // and RVV object sections should be correct aligned in their own
1300 // right.
1302 "Inconsistent stack layout");
1304 }
1305 return Offset;
1306 }
1307
1308 // This case handles indexing off both SP and BP.
1309 // If indexing off SP, there must not be any var sized objects
1310 assert(FrameReg == RISCVABI::getBPReg() || !MFI.hasVarSizedObjects());
1311
1312 // When using SP to access frame objects, we need to add RVV stack size.
1313 //
1314 // |--------------------------| -- <-- FP
1315 // | callee-allocated save | | <----|
1316 // | area for register varargs| | |
1317 // |--------------------------| | |
1318 // | callee-saved registers | | |
1319 // |--------------------------| -- |
1320 // | RVV alignment padding | | |
1321 // | (not counted in | | |
1322 // | MFI.getStackSize() but | | |
1323 // | counted in | | |
1324 // | RVFI.getRVVStackSize()) | | |
1325 // |--------------------------| -- |
1326 // | RVV objects | | |-- MFI.getStackSize()
1327 // | (not counted in | | |
1328 // | MFI.getStackSize()) | | |
1329 // |--------------------------| -- |
1330 // | padding before RVV | | |
1331 // | (not counted in | | |
1332 // | MFI.getStackSize()) | | |
1333 // |--------------------------| -- |
1334 // | scalar local variables | | <----'
1335 // |--------------------------| -- <-- BP (if var sized objects present)
1336 // | VarSize objects | |
1337 // |--------------------------| -- <-- SP
1338 //
1339 // The total amount of padding surrounding RVV objects is described by
1340 // RVV->getRVVPadding() and it can be zero. It allows us to align the RVV
1341 // objects to the required alignment.
1342 if (MFI.getStackID(FI) == TargetStackID::Default) {
1343 if (MFI.isFixedObjectIndex(FI)) {
1344 assert(!RI->hasStackRealignment(MF) &&
1345 "Can't index across variable sized realign");
1347 RVFI->getRVVStackSize());
1348 } else {
1350 }
1351 } else if (MFI.getStackID(FI) == TargetStackID::ScalableVector) {
1352 // Ensure the base of the RVV stack is correctly aligned: add on the
1353 // alignment padding.
1354 int ScalarLocalVarSize = MFI.getStackSize() -
1355 RVFI->getCalleeSavedStackSize() -
1356 RVFI->getVarArgsSaveSize() + RVFI->getRVVPadding();
1357 Offset += StackOffset::get(ScalarLocalVarSize, RVFI->getRVVStackSize());
1358 }
1359 return Offset;
1360}
1361
1363 BitVector &SavedRegs,
1364 RegScavenger *RS) const {
1366 // Unconditionally spill RA and FP only if the function uses a frame
1367 // pointer.
1368 if (hasFP(MF)) {
1369 SavedRegs.set(RAReg);
1370 SavedRegs.set(FPReg);
1371 }
1372 // Mark BP as used if function has dedicated base pointer.
1373 if (hasBP(MF))
1374 SavedRegs.set(RISCVABI::getBPReg());
1375
1376 // When using cm.push/pop we must save X27 if we save X26.
1377 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
1378 if (RVFI->isPushable(MF) && SavedRegs.test(RISCV::X26))
1379 SavedRegs.set(RISCV::X27);
1380}
1381
1382std::pair<int64_t, Align>
1383RISCVFrameLowering::assignRVVStackObjectOffsets(MachineFunction &MF) const {
1384 MachineFrameInfo &MFI = MF.getFrameInfo();
1385 // Create a buffer of RVV objects to allocate.
1386 SmallVector<int, 8> ObjectsToAllocate;
1387 auto pushRVVObjects = [&](int FIBegin, int FIEnd) {
1388 for (int I = FIBegin, E = FIEnd; I != E; ++I) {
1389 unsigned StackID = MFI.getStackID(I);
1390 if (StackID != TargetStackID::ScalableVector)
1391 continue;
1392 if (MFI.isDeadObjectIndex(I))
1393 continue;
1394
1395 ObjectsToAllocate.push_back(I);
1396 }
1397 };
1398 // First push RVV Callee Saved object, then push RVV stack object
1399 std::vector<CalleeSavedInfo> &CSI = MF.getFrameInfo().getCalleeSavedInfo();
1400 const auto &RVVCSI = getRVVCalleeSavedInfo(MF, CSI);
1401 if (!RVVCSI.empty())
1402 pushRVVObjects(RVVCSI[0].getFrameIdx(),
1403 RVVCSI[RVVCSI.size() - 1].getFrameIdx() + 1);
1404 pushRVVObjects(0, MFI.getObjectIndexEnd() - RVVCSI.size());
1405
1406 // The minimum alignment is 16 bytes.
1407 Align RVVStackAlign(16);
1408 const auto &ST = MF.getSubtarget<RISCVSubtarget>();
1409
1410 if (!ST.hasVInstructions()) {
1411 assert(ObjectsToAllocate.empty() &&
1412 "Can't allocate scalable-vector objects without V instructions");
1413 return std::make_pair(0, RVVStackAlign);
1414 }
1415
1416 // Allocate all RVV locals and spills
1417 int64_t Offset = 0;
1418 for (int FI : ObjectsToAllocate) {
1419 // ObjectSize in bytes.
1420 int64_t ObjectSize = MFI.getObjectSize(FI);
1421 auto ObjectAlign =
1422 std::max(Align(RISCV::RVVBitsPerBlock / 8), MFI.getObjectAlign(FI));
1423 // If the data type is the fractional vector type, reserve one vector
1424 // register for it.
1425 if (ObjectSize < (RISCV::RVVBitsPerBlock / 8))
1426 ObjectSize = (RISCV::RVVBitsPerBlock / 8);
1427 Offset = alignTo(Offset + ObjectSize, ObjectAlign);
1428 MFI.setObjectOffset(FI, -Offset);
1429 // Update the maximum alignment of the RVV stack section
1430 RVVStackAlign = std::max(RVVStackAlign, ObjectAlign);
1431 }
1432
1433 uint64_t StackSize = Offset;
1434
1435 // Ensure the alignment of the RVV stack. Since we want the most-aligned
1436 // object right at the bottom (i.e., any padding at the top of the frame),
1437 // readjust all RVV objects down by the alignment padding.
1438 // Stack size and offsets are multiples of vscale, stack alignment is in
1439 // bytes, we can divide stack alignment by minimum vscale to get a maximum
1440 // stack alignment multiple of vscale.
1441 auto VScale =
1442 std::max<uint64_t>(ST.getRealMinVLen() / RISCV::RVVBitsPerBlock, 1);
1443 if (auto RVVStackAlignVScale = RVVStackAlign.value() / VScale) {
1444 if (auto AlignmentPadding =
1445 offsetToAlignment(StackSize, Align(RVVStackAlignVScale))) {
1446 StackSize += AlignmentPadding;
1447 for (int FI : ObjectsToAllocate)
1448 MFI.setObjectOffset(FI, MFI.getObjectOffset(FI) - AlignmentPadding);
1449 }
1450 }
1451
1452 return std::make_pair(StackSize, RVVStackAlign);
1453}
1454
1456 // For RVV spill, scalable stack offsets computing requires up to two scratch
1457 // registers
1458 static constexpr unsigned ScavSlotsNumRVVSpillScalableObject = 2;
1459
1460 // For RVV spill, non-scalable stack offsets computing requires up to one
1461 // scratch register.
1462 static constexpr unsigned ScavSlotsNumRVVSpillNonScalableObject = 1;
1463
1464 // ADDI instruction's destination register can be used for computing
1465 // offsets. So Scalable stack offsets require up to one scratch register.
1466 static constexpr unsigned ScavSlotsADDIScalableObject = 1;
1467
1468 static constexpr unsigned MaxScavSlotsNumKnown =
1469 std::max({ScavSlotsADDIScalableObject, ScavSlotsNumRVVSpillScalableObject,
1470 ScavSlotsNumRVVSpillNonScalableObject});
1471
1472 unsigned MaxScavSlotsNum = 0;
1474 return false;
1475 for (const MachineBasicBlock &MBB : MF)
1476 for (const MachineInstr &MI : MBB) {
1477 bool IsRVVSpill = RISCV::isRVVSpill(MI);
1478 for (auto &MO : MI.operands()) {
1479 if (!MO.isFI())
1480 continue;
1481 bool IsScalableVectorID = MF.getFrameInfo().getStackID(MO.getIndex()) ==
1483 if (IsRVVSpill) {
1484 MaxScavSlotsNum = std::max(
1485 MaxScavSlotsNum, IsScalableVectorID
1486 ? ScavSlotsNumRVVSpillScalableObject
1487 : ScavSlotsNumRVVSpillNonScalableObject);
1488 } else if (MI.getOpcode() == RISCV::ADDI && IsScalableVectorID) {
1489 MaxScavSlotsNum =
1490 std::max(MaxScavSlotsNum, ScavSlotsADDIScalableObject);
1491 }
1492 }
1493 if (MaxScavSlotsNum == MaxScavSlotsNumKnown)
1494 return MaxScavSlotsNumKnown;
1495 }
1496 return MaxScavSlotsNum;
1497}
1498
1499static bool hasRVVFrameObject(const MachineFunction &MF) {
1500 // Originally, the function will scan all the stack objects to check whether
1501 // if there is any scalable vector object on the stack or not. However, it
1502 // causes errors in the register allocator. In issue 53016, it returns false
1503 // before RA because there is no RVV stack objects. After RA, it returns true
1504 // because there are spilling slots for RVV values during RA. It will not
1505 // reserve BP during register allocation and generate BP access in the PEI
1506 // pass due to the inconsistent behavior of the function.
1507 //
1508 // The function is changed to use hasVInstructions() as the return value. It
1509 // is not precise, but it can make the register allocation correct.
1510 //
1511 // FIXME: Find a better way to make the decision or revisit the solution in
1512 // D103622.
1513 //
1514 // Refer to https://github.com/llvm/llvm-project/issues/53016.
1515 return MF.getSubtarget<RISCVSubtarget>().hasVInstructions();
1516}
1517
1519 const RISCVInstrInfo &TII) {
1520 unsigned FnSize = 0;
1521 for (auto &MBB : MF) {
1522 for (auto &MI : MBB) {
1523 // Far branches over 20-bit offset will be relaxed in branch relaxation
1524 // pass. In the worst case, conditional branches will be relaxed into
1525 // the following instruction sequence. Unconditional branches are
1526 // relaxed in the same way, with the exception that there is no first
1527 // branch instruction.
1528 //
1529 // foo
1530 // bne t5, t6, .rev_cond # `TII->getInstSizeInBytes(MI)` bytes
1531 // sd s11, 0(sp) # 4 bytes, or 2 bytes in RVC
1532 // jump .restore, s11 # 8 bytes
1533 // .rev_cond
1534 // bar
1535 // j .dest_bb # 4 bytes, or 2 bytes in RVC
1536 // .restore:
1537 // ld s11, 0(sp) # 4 bytes, or 2 bytes in RVC
1538 // .dest:
1539 // baz
1540 if (MI.isConditionalBranch())
1541 FnSize += TII.getInstSizeInBytes(MI);
1542 if (MI.isConditionalBranch() || MI.isUnconditionalBranch()) {
1544 FnSize += 2 + 8 + 2 + 2;
1545 else
1546 FnSize += 4 + 8 + 4 + 4;
1547 continue;
1548 }
1549
1550 FnSize += TII.getInstSizeInBytes(MI);
1551 }
1552 }
1553 return FnSize;
1554}
1555
1557 MachineFunction &MF, RegScavenger *RS) const {
1558 const RISCVRegisterInfo *RegInfo =
1559 MF.getSubtarget<RISCVSubtarget>().getRegisterInfo();
1560 const RISCVInstrInfo *TII = MF.getSubtarget<RISCVSubtarget>().getInstrInfo();
1561 MachineFrameInfo &MFI = MF.getFrameInfo();
1562 const TargetRegisterClass *RC = &RISCV::GPRRegClass;
1563 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
1564
1565 int64_t RVVStackSize;
1566 Align RVVStackAlign;
1567 std::tie(RVVStackSize, RVVStackAlign) = assignRVVStackObjectOffsets(MF);
1568
1569 RVFI->setRVVStackSize(RVVStackSize);
1570 RVFI->setRVVStackAlign(RVVStackAlign);
1571
1572 if (hasRVVFrameObject(MF)) {
1573 // Ensure the entire stack is aligned to at least the RVV requirement: some
1574 // scalable-vector object alignments are not considered by the
1575 // target-independent code.
1576 MFI.ensureMaxAlignment(RVVStackAlign);
1577 }
1578
1579 unsigned ScavSlotsNum = 0;
1580
1581 // estimateStackSize has been observed to under-estimate the final stack
1582 // size, so give ourselves wiggle-room by checking for stack size
1583 // representable an 11-bit signed field rather than 12-bits.
1584 if (!isInt<11>(MFI.estimateStackSize(MF)))
1585 ScavSlotsNum = 1;
1586
1587 // Far branches over 20-bit offset require a spill slot for scratch register.
1588 bool IsLargeFunction = !isInt<20>(estimateFunctionSizeInBytes(MF, *TII));
1589 if (IsLargeFunction)
1590 ScavSlotsNum = std::max(ScavSlotsNum, 1u);
1591
1592 // RVV loads & stores have no capacity to hold the immediate address offsets
1593 // so we must always reserve an emergency spill slot if the MachineFunction
1594 // contains any RVV spills.
1595 ScavSlotsNum = std::max(ScavSlotsNum, getScavSlotsNumForRVV(MF));
1596
1597 for (unsigned I = 0; I < ScavSlotsNum; I++) {
1598 int FI = MFI.CreateSpillStackObject(RegInfo->getSpillSize(*RC),
1599 RegInfo->getSpillAlign(*RC));
1601
1602 if (IsLargeFunction && RVFI->getBranchRelaxationScratchFrameIndex() == -1)
1603 RVFI->setBranchRelaxationScratchFrameIndex(FI);
1604 }
1605
1606 unsigned Size = RVFI->getReservedSpillsSize();
1607 for (const auto &Info : MFI.getCalleeSavedInfo()) {
1608 int FrameIdx = Info.getFrameIdx();
1609 if (FrameIdx < 0 || MFI.getStackID(FrameIdx) != TargetStackID::Default)
1610 continue;
1611
1612 Size += MFI.getObjectSize(FrameIdx);
1613 }
1614 RVFI->setCalleeSavedStackSize(Size);
1615}
1616
1617// Not preserve stack space within prologue for outgoing variables when the
1618// function contains variable size objects or there are vector objects accessed
1619// by the frame pointer.
1620// Let eliminateCallFramePseudoInstr preserve stack space for it.
1622 return !MF.getFrameInfo().hasVarSizedObjects() &&
1623 !(hasFP(MF) && hasRVVFrameObject(MF));
1624}
1625
1626// Eliminate ADJCALLSTACKDOWN, ADJCALLSTACKUP pseudo instructions.
1630 DebugLoc DL = MI->getDebugLoc();
1631
1632 if (!hasReservedCallFrame(MF)) {
1633 // If space has not been reserved for a call frame, ADJCALLSTACKDOWN and
1634 // ADJCALLSTACKUP must be converted to instructions manipulating the stack
1635 // pointer. This is necessary when there is a variable length stack
1636 // allocation (e.g. alloca), which means it's not possible to allocate
1637 // space for outgoing arguments from within the function prologue.
1638 int64_t Amount = MI->getOperand(0).getImm();
1639
1640 if (Amount != 0) {
1641 // Ensure the stack remains aligned after adjustment.
1642 Amount = alignSPAdjust(Amount);
1643
1644 if (MI->getOpcode() == RISCV::ADJCALLSTACKDOWN)
1645 Amount = -Amount;
1646
1647 const RISCVRegisterInfo &RI = *STI.getRegisterInfo();
1650 }
1651 }
1652
1653 return MBB.erase(MI);
1654}
1655
1656// We would like to split the SP adjustment to reduce prologue/epilogue
1657// as following instructions. In this way, the offset of the callee saved
1658// register could fit in a single store. Supposed that the first sp adjust
1659// amount is 2032.
1660// add sp,sp,-2032
1661// sw ra,2028(sp)
1662// sw s0,2024(sp)
1663// sw s1,2020(sp)
1664// sw s3,2012(sp)
1665// sw s4,2008(sp)
1666// add sp,sp,-64
1669 const auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
1670 const MachineFrameInfo &MFI = MF.getFrameInfo();
1671 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
1672 uint64_t StackSize = getStackSizeWithRVVPadding(MF);
1673
1674 // Disable SplitSPAdjust if save-restore libcall is used. The callee-saved
1675 // registers will be pushed by the save-restore libcalls, so we don't have to
1676 // split the SP adjustment in this case.
1677 if (RVFI->getReservedSpillsSize())
1678 return 0;
1679
1680 // Return the FirstSPAdjustAmount if the StackSize can not fit in a signed
1681 // 12-bit and there exists a callee-saved register needing to be pushed.
1682 if (!isInt<12>(StackSize) && (CSI.size() > 0)) {
1683 // FirstSPAdjustAmount is chosen at most as (2048 - StackAlign) because
1684 // 2048 will cause sp = sp + 2048 in the epilogue to be split into multiple
1685 // instructions. Offsets smaller than 2048 can fit in a single load/store
1686 // instruction, and we have to stick with the stack alignment. 2048 has
1687 // 16-byte alignment. The stack alignment for RV32 and RV64 is 16 and for
1688 // RV32E it is 4. So (2048 - StackAlign) will satisfy the stack alignment.
1689 const uint64_t StackAlign = getStackAlign().value();
1690
1691 // Amount of (2048 - StackAlign) will prevent callee saved and restored
1692 // instructions be compressed, so try to adjust the amount to the largest
1693 // offset that stack compression instructions accept when target supports
1694 // compression instructions.
1695 if (STI.hasStdExtCOrZca()) {
1696 // The compression extensions may support the following instructions:
1697 // riscv32: c.lwsp rd, offset[7:2] => 2^(6 + 2)
1698 // c.swsp rs2, offset[7:2] => 2^(6 + 2)
1699 // c.flwsp rd, offset[7:2] => 2^(6 + 2)
1700 // c.fswsp rs2, offset[7:2] => 2^(6 + 2)
1701 // riscv64: c.ldsp rd, offset[8:3] => 2^(6 + 3)
1702 // c.sdsp rs2, offset[8:3] => 2^(6 + 3)
1703 // c.fldsp rd, offset[8:3] => 2^(6 + 3)
1704 // c.fsdsp rs2, offset[8:3] => 2^(6 + 3)
1705 const uint64_t RVCompressLen = STI.getXLen() * 8;
1706 // Compared with amount (2048 - StackAlign), StackSize needs to
1707 // satisfy the following conditions to avoid using more instructions
1708 // to adjust the sp after adjusting the amount, such as
1709 // StackSize meets the condition (StackSize <= 2048 + RVCompressLen),
1710 // case1: Amount is 2048 - StackAlign: use addi + addi to adjust sp.
1711 // case2: Amount is RVCompressLen: use addi + addi to adjust sp.
1712 auto CanCompress = [&](uint64_t CompressLen) -> bool {
1713 if (StackSize <= 2047 + CompressLen ||
1714 (StackSize > 2048 * 2 - StackAlign &&
1715 StackSize <= 2047 * 2 + CompressLen) ||
1716 StackSize > 2048 * 3 - StackAlign)
1717 return true;
1718
1719 return false;
1720 };
1721 // In the epilogue, addi sp, sp, 496 is used to recover the sp and it
1722 // can be compressed(C.ADDI16SP, offset can be [-512, 496]), but
1723 // addi sp, sp, 512 can not be compressed. So try to use 496 first.
1724 const uint64_t ADDI16SPCompressLen = 496;
1725 if (STI.is64Bit() && CanCompress(ADDI16SPCompressLen))
1726 return ADDI16SPCompressLen;
1727 if (CanCompress(RVCompressLen))
1728 return RVCompressLen;
1729 }
1730 return 2048 - StackAlign;
1731 }
1732 return 0;
1733}
1734
1737 std::vector<CalleeSavedInfo> &CSI, unsigned &MinCSFrameIndex,
1738 unsigned &MaxCSFrameIndex) const {
1739 // Early exit if no callee saved registers are modified!
1740 if (CSI.empty())
1741 return true;
1742
1743 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
1744
1745 if (RVFI->isPushable(MF)) {
1746 // Determine how many GPRs we need to push and save it to RVFI.
1747 Register MaxReg = getMaxPushPopReg(MF, CSI);
1748 if (MaxReg != RISCV::NoRegister) {
1749 auto [RegEnc, PushedRegNum] = getPushPopEncodingAndNum(MaxReg);
1750 RVFI->setRVPushRegs(PushedRegNum);
1751 RVFI->setRVPushStackSize(alignTo((STI.getXLen() / 8) * PushedRegNum, 16));
1752
1753 // Use encoded number to represent registers to spill.
1754 RVFI->setRVPushRlist(RegEnc);
1755 }
1756 }
1757
1758 MachineFrameInfo &MFI = MF.getFrameInfo();
1759 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
1760
1761 for (auto &CS : CSI) {
1762 unsigned Reg = CS.getReg();
1763 const TargetRegisterClass *RC = RegInfo->getMinimalPhysRegClass(Reg);
1764 unsigned Size = RegInfo->getSpillSize(*RC);
1765
1766 // This might need a fixed stack slot.
1767 if (RVFI->useSaveRestoreLibCalls(MF) || RVFI->isPushable(MF)) {
1768 const auto *FII = llvm::find_if(
1769 FixedCSRFIMap, [&](auto P) { return P.first == CS.getReg(); });
1770 if (FII != std::end(FixedCSRFIMap)) {
1771 int64_t Offset;
1772 if (RVFI->isPushable(MF))
1773 Offset = -((FII->second + RVFI->getRVPushRegs() + 1) * (int64_t)Size);
1774 else
1775 Offset = FII->second * (int64_t)Size;
1776
1777 int FrameIdx = MFI.CreateFixedSpillStackObject(Size, Offset);
1778 assert(FrameIdx < 0);
1779 CS.setFrameIdx(FrameIdx);
1780 continue;
1781 }
1782 }
1783
1784 // Not a fixed slot.
1785 Align Alignment = RegInfo->getSpillAlign(*RC);
1786 // We may not be able to satisfy the desired alignment specification of
1787 // the TargetRegisterClass if the stack alignment is smaller. Use the
1788 // min.
1789 Alignment = std::min(Alignment, getStackAlign());
1790 int FrameIdx = MFI.CreateStackObject(Size, Alignment, true);
1791 if ((unsigned)FrameIdx < MinCSFrameIndex)
1792 MinCSFrameIndex = FrameIdx;
1793 if ((unsigned)FrameIdx > MaxCSFrameIndex)
1794 MaxCSFrameIndex = FrameIdx;
1795 CS.setFrameIdx(FrameIdx);
1798 }
1799
1800 // Allocate a fixed object that covers the full push or libcall size.
1801 if (RVFI->isPushable(MF)) {
1802 if (int64_t PushSize = RVFI->getRVPushStackSize())
1803 MFI.CreateFixedSpillStackObject(PushSize, -PushSize);
1804 } else if (int LibCallRegs = getLibCallID(MF, CSI) + 1) {
1805 int64_t LibCallFrameSize =
1806 alignTo((STI.getXLen() / 8) * LibCallRegs, getStackAlign());
1807 MFI.CreateFixedSpillStackObject(LibCallFrameSize, -LibCallFrameSize);
1808 }
1809
1810 return true;
1811}
1812
1816 if (CSI.empty())
1817 return true;
1818
1820 const TargetInstrInfo &TII = *MF->getSubtarget().getInstrInfo();
1821 DebugLoc DL;
1822 if (MI != MBB.end() && !MI->isDebugInstr())
1823 DL = MI->getDebugLoc();
1824
1825 // Emit CM.PUSH with base SPimm & evaluate Push stack
1827 if (RVFI->isPushable(*MF)) {
1828 unsigned PushedRegNum = RVFI->getRVPushRegs();
1829 if (PushedRegNum > 0) {
1830 // Use encoded number to represent registers to spill.
1831 int RegEnc = RVFI->getRVPushRlist();
1832 MachineInstrBuilder PushBuilder =
1833 BuildMI(MBB, MI, DL, TII.get(RISCV::CM_PUSH))
1835 PushBuilder.addImm((int64_t)RegEnc);
1836 PushBuilder.addImm(0);
1837
1838 for (unsigned i = 0; i < PushedRegNum; i++)
1839 PushBuilder.addUse(FixedCSRFIMap[i].first, RegState::Implicit);
1840 }
1841 } else if (const char *SpillLibCall = getSpillLibCallName(*MF, CSI)) {
1842 // Add spill libcall via non-callee-saved register t0.
1843 BuildMI(MBB, MI, DL, TII.get(RISCV::PseudoCALLReg), RISCV::X5)
1844 .addExternalSymbol(SpillLibCall, RISCVII::MO_CALL)
1846
1847 // Add registers spilled in libcall as liveins.
1848 for (auto &CS : CSI)
1849 MBB.addLiveIn(CS.getReg());
1850 }
1851
1852 // Manually spill values not spilled by libcall & Push/Pop.
1853 const auto &UnmanagedCSI = getUnmanagedCSI(*MF, CSI);
1854 const auto &RVVCSI = getRVVCalleeSavedInfo(*MF, CSI);
1855
1856 auto storeRegsToStackSlots = [&](decltype(UnmanagedCSI) CSInfo) {
1857 for (auto &CS : CSInfo) {
1858 // Insert the spill to the stack frame.
1859 Register Reg = CS.getReg();
1860 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
1862 CS.getFrameIdx(), RC, TRI, Register());
1863 }
1864 };
1865 storeRegsToStackSlots(UnmanagedCSI);
1866 storeRegsToStackSlots(RVVCSI);
1867
1868 return true;
1869}
1870
1871static unsigned getCalleeSavedRVVNumRegs(const Register &BaseReg) {
1872 return RISCV::VRRegClass.contains(BaseReg) ? 1
1873 : RISCV::VRM2RegClass.contains(BaseReg) ? 2
1874 : RISCV::VRM4RegClass.contains(BaseReg) ? 4
1875 : 8;
1876}
1877
1879 const Register &Reg) {
1880 MCRegister BaseReg = TRI.getSubReg(Reg, RISCV::sub_vrm1_0);
1881 // If it's not a grouped vector register, it doesn't have subregister, so
1882 // the base register is just itself.
1883 if (BaseReg == RISCV::NoRegister)
1884 BaseReg = Reg;
1885 return BaseReg;
1886}
1887
1888void RISCVFrameLowering::emitCalleeSavedRVVPrologCFI(
1891 const MachineFrameInfo &MFI = MF->getFrameInfo();
1893 const TargetInstrInfo &TII = *STI.getInstrInfo();
1896
1897 const auto &RVVCSI = getRVVCalleeSavedInfo(*MF, MFI.getCalleeSavedInfo());
1898 if (RVVCSI.empty())
1899 return;
1900
1901 uint64_t FixedSize = getStackSizeWithRVVPadding(*MF);
1902 if (!HasFP) {
1903 uint64_t ScalarLocalVarSize =
1904 MFI.getStackSize() - RVFI->getCalleeSavedStackSize() -
1905 RVFI->getVarArgsSaveSize() + RVFI->getRVVPadding();
1906 FixedSize -= ScalarLocalVarSize;
1907 }
1908
1909 for (auto &CS : RVVCSI) {
1910 // Insert the spill to the stack frame.
1911 int FI = CS.getFrameIdx();
1912 MCRegister BaseReg = getRVVBaseRegister(TRI, CS.getReg());
1913 unsigned NumRegs = getCalleeSavedRVVNumRegs(CS.getReg());
1914 for (unsigned i = 0; i < NumRegs; ++i) {
1915 unsigned CFIIndex = MF->addFrameInst(createDefCFAOffset(
1916 TRI, BaseReg + i, -FixedSize, MFI.getObjectOffset(FI) / 8 + i));
1917 BuildMI(MBB, MI, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
1918 .addCFIIndex(CFIIndex)
1920 }
1921 }
1922}
1923
1924void RISCVFrameLowering::emitCalleeSavedRVVEpilogCFI(
1927 const MachineFrameInfo &MFI = MF->getFrameInfo();
1929 const TargetInstrInfo &TII = *STI.getInstrInfo();
1932
1933 const auto &RVVCSI = getRVVCalleeSavedInfo(*MF, MFI.getCalleeSavedInfo());
1934 for (auto &CS : RVVCSI) {
1935 MCRegister BaseReg = getRVVBaseRegister(TRI, CS.getReg());
1936 unsigned NumRegs = getCalleeSavedRVVNumRegs(CS.getReg());
1937 for (unsigned i = 0; i < NumRegs; ++i) {
1938 unsigned CFIIndex = MF->addFrameInst(MCCFIInstruction::createRestore(
1939 nullptr, RI->getDwarfRegNum(BaseReg + i, true)));
1940 BuildMI(MBB, MI, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
1941 .addCFIIndex(CFIIndex)
1943 }
1944 }
1945}
1946
1950 if (CSI.empty())
1951 return true;
1952
1954 const TargetInstrInfo &TII = *MF->getSubtarget().getInstrInfo();
1955 DebugLoc DL;
1956 if (MI != MBB.end() && !MI->isDebugInstr())
1957 DL = MI->getDebugLoc();
1958
1959 // Manually restore values not restored by libcall & Push/Pop.
1960 // Reverse the restore order in epilog. In addition, the return
1961 // address will be restored first in the epilogue. It increases
1962 // the opportunity to avoid the load-to-use data hazard between
1963 // loading RA and return by RA. loadRegFromStackSlot can insert
1964 // multiple instructions.
1965 const auto &UnmanagedCSI = getUnmanagedCSI(*MF, CSI);
1966 const auto &RVVCSI = getRVVCalleeSavedInfo(*MF, CSI);
1967
1968 auto loadRegFromStackSlot = [&](decltype(UnmanagedCSI) CSInfo) {
1969 for (auto &CS : CSInfo) {
1970 Register Reg = CS.getReg();
1971 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
1972 TII.loadRegFromStackSlot(MBB, MI, Reg, CS.getFrameIdx(), RC, TRI,
1973 Register());
1974 assert(MI != MBB.begin() &&
1975 "loadRegFromStackSlot didn't insert any code!");
1976 }
1977 };
1978 loadRegFromStackSlot(RVVCSI);
1979 loadRegFromStackSlot(UnmanagedCSI);
1980
1982 if (RVFI->isPushable(*MF)) {
1983 int RegEnc = RVFI->getRVPushRlist();
1985 MachineInstrBuilder PopBuilder =
1986 BuildMI(MBB, MI, DL, TII.get(RISCV::CM_POP))
1988 // Use encoded number to represent registers to restore.
1989 PopBuilder.addImm(RegEnc);
1990 PopBuilder.addImm(0);
1991
1992 for (unsigned i = 0; i < RVFI->getRVPushRegs(); i++)
1993 PopBuilder.addDef(FixedCSRFIMap[i].first, RegState::ImplicitDefine);
1994 }
1995 } else {
1996 const char *RestoreLibCall = getRestoreLibCallName(*MF, CSI);
1997 if (RestoreLibCall) {
1998 // Add restore libcall via tail call.
2000 BuildMI(MBB, MI, DL, TII.get(RISCV::PseudoTAIL))
2001 .addExternalSymbol(RestoreLibCall, RISCVII::MO_CALL)
2003
2004 // Remove trailing returns, since the terminator is now a tail call to the
2005 // restore function.
2006 if (MI != MBB.end() && MI->getOpcode() == RISCV::PseudoRET) {
2007 NewMI->copyImplicitOps(*MF, *MI);
2008 MI->eraseFromParent();
2009 }
2010 }
2011 }
2012 return true;
2013}
2014
2016 // Keep the conventional code flow when not optimizing.
2017 if (MF.getFunction().hasOptNone())
2018 return false;
2019
2020 return true;
2021}
2022
2024 MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB);
2025 const MachineFunction *MF = MBB.getParent();
2026 const auto *RVFI = MF->getInfo<RISCVMachineFunctionInfo>();
2027
2028 if (!RVFI->useSaveRestoreLibCalls(*MF))
2029 return true;
2030
2031 // Inserting a call to a __riscv_save libcall requires the use of the register
2032 // t0 (X5) to hold the return address. Therefore if this register is already
2033 // used we can't insert the call.
2034
2035 RegScavenger RS;
2036 RS.enterBasicBlock(*TmpMBB);
2037 return !RS.isRegUsed(RISCV::X5);
2038}
2039
2041 const MachineFunction *MF = MBB.getParent();
2042 MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB);
2043 const auto *RVFI = MF->getInfo<RISCVMachineFunctionInfo>();
2044
2045 if (!RVFI->useSaveRestoreLibCalls(*MF))
2046 return true;
2047
2048 // Using the __riscv_restore libcalls to restore CSRs requires a tail call.
2049 // This means if we still need to continue executing code within this function
2050 // the restore cannot take place in this basic block.
2051
2052 if (MBB.succ_size() > 1)
2053 return false;
2054
2055 MachineBasicBlock *SuccMBB =
2056 MBB.succ_empty() ? TmpMBB->getFallThrough() : *MBB.succ_begin();
2057
2058 // Doing a tail call should be safe if there are no successors, because either
2059 // we have a returning block or the end of the block is unreachable, so the
2060 // restore will be eliminated regardless.
2061 if (!SuccMBB)
2062 return true;
2063
2064 // The successor can only contain a return, since we would effectively be
2065 // replacing the successor with our own tail return at the end of our block.
2066 return SuccMBB->isReturnBlock() && SuccMBB->size() == 1;
2067}
2068
2070 switch (ID) {
2073 return true;
2077 return false;
2078 }
2079 llvm_unreachable("Invalid TargetStackID::Value");
2080}
2081
2084}
2085
2086// Synthesize the probe loop.
2089 Register TargetReg, bool IsRVV) {
2090 assert(TargetReg != RISCV::X2 && "New top of stack cannot already be in SP");
2091
2092 auto &Subtarget = MF.getSubtarget<RISCVSubtarget>();
2093 const RISCVInstrInfo *TII = Subtarget.getInstrInfo();
2094 bool IsRV64 = Subtarget.is64Bit();
2095 Align StackAlign = Subtarget.getFrameLowering()->getStackAlign();
2096 const RISCVTargetLowering *TLI = Subtarget.getTargetLowering();
2097 uint64_t ProbeSize = TLI->getStackProbeSize(MF, StackAlign);
2098
2099 MachineFunction::iterator MBBInsertPoint = std::next(MBB.getIterator());
2100 MachineBasicBlock *LoopTestMBB =
2102 MF.insert(MBBInsertPoint, LoopTestMBB);
2104 MF.insert(MBBInsertPoint, ExitMBB);
2106 Register ScratchReg = RISCV::X7;
2107
2108 // ScratchReg = ProbeSize
2109 TII->movImm(MBB, MBBI, DL, ScratchReg, ProbeSize, Flags);
2110
2111 // LoopTest:
2112 // SUB SP, SP, ProbeSize
2113 BuildMI(*LoopTestMBB, LoopTestMBB->end(), DL, TII->get(RISCV::SUB), SPReg)
2114 .addReg(SPReg)
2115 .addReg(ScratchReg)
2116 .setMIFlags(Flags);
2117
2118 // s[d|w] zero, 0(sp)
2119 BuildMI(*LoopTestMBB, LoopTestMBB->end(), DL,
2120 TII->get(IsRV64 ? RISCV::SD : RISCV::SW))
2121 .addReg(RISCV::X0)
2122 .addReg(SPReg)
2123 .addImm(0)
2124 .setMIFlags(Flags);
2125
2126 if (IsRVV) {
2127 // SUB TargetReg, TargetReg, ProbeSize
2128 BuildMI(*LoopTestMBB, LoopTestMBB->end(), DL, TII->get(RISCV::SUB),
2129 TargetReg)
2130 .addReg(TargetReg)
2131 .addReg(ScratchReg)
2132 .setMIFlags(Flags);
2133
2134 // BGE TargetReg, ProbeSize, LoopTest
2135 BuildMI(*LoopTestMBB, LoopTestMBB->end(), DL, TII->get(RISCV::BGE))
2136 .addReg(TargetReg)
2137 .addReg(ScratchReg)
2138 .addMBB(LoopTestMBB)
2139 .setMIFlags(Flags);
2140
2141 } else {
2142 // BNE SP, TargetReg, LoopTest
2143 BuildMI(*LoopTestMBB, LoopTestMBB->end(), DL, TII->get(RISCV::BNE))
2144 .addReg(SPReg)
2145 .addReg(TargetReg)
2146 .addMBB(LoopTestMBB)
2147 .setMIFlags(Flags);
2148 }
2149
2150 ExitMBB->splice(ExitMBB->end(), &MBB, std::next(MBBI), MBB.end());
2151
2152 LoopTestMBB->addSuccessor(ExitMBB);
2153 LoopTestMBB->addSuccessor(LoopTestMBB);
2154 MBB.addSuccessor(LoopTestMBB);
2155 // Update liveins.
2156 fullyRecomputeLiveIns({ExitMBB, LoopTestMBB});
2157}
2158
2159void RISCVFrameLowering::inlineStackProbe(MachineFunction &MF,
2160 MachineBasicBlock &MBB) const {
2161 // Get the instructions that need to be replaced. We emit at most two of
2162 // these. Remember them in order to avoid complications coming from the need
2163 // to traverse the block while potentially creating more blocks.
2165 for (MachineInstr &MI : MBB) {
2166 unsigned Opc = MI.getOpcode();
2167 if (Opc == RISCV::PROBED_STACKALLOC ||
2168 Opc == RISCV::PROBED_STACKALLOC_RVV) {
2169 ToReplace.push_back(&MI);
2170 }
2171 }
2172
2173 for (MachineInstr *MI : ToReplace) {
2174 if (MI->getOpcode() == RISCV::PROBED_STACKALLOC ||
2175 MI->getOpcode() == RISCV::PROBED_STACKALLOC_RVV) {
2176 MachineBasicBlock::iterator MBBI = MI->getIterator();
2178 Register TargetReg = MI->getOperand(1).getReg();
2179 emitStackProbeInline(MF, MBB, MBBI, DL, TargetReg,
2180 (MI->getOpcode() == RISCV::PROBED_STACKALLOC_RVV));
2182 }
2183 }
2184}
static MCCFIInstruction createDefCFAExpression(const TargetRegisterInfo &TRI, unsigned Reg, const StackOffset &Offset)
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
dxil DXContainer Global Emitter
This file contains constants used for implementing Dwarf debug support.
uint64_t Size
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
static uint64_t estimateFunctionSizeInBytes(const LoongArchInstrInfo *TII, const MachineFunction &MF)
#define I(x, y, z)
Definition: MD5.cpp:58
unsigned const TargetRegisterInfo * TRI
#define P(N)
static constexpr Register SPReg
static constexpr Register FPReg
static MCRegister getRVVBaseRegister(const RISCVRegisterInfo &TRI, const Register &Reg)
static void emitStackProbeInline(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, DebugLoc DL, Register TargetReg, bool IsRVV)
static const char * getRestoreLibCallName(const MachineFunction &MF, const std::vector< CalleeSavedInfo > &CSI)
static const char * getSpillLibCallName(const MachineFunction &MF, const std::vector< CalleeSavedInfo > &CSI)
static bool hasRVVFrameObject(const MachineFunction &MF)
static std::pair< unsigned, unsigned > getPushPopEncodingAndNum(const Register MaxReg)
static const std::pair< MCPhysReg, int8_t > FixedCSRFIMap[]
static SmallVector< CalleeSavedInfo, 8 > getRVVCalleeSavedInfo(const MachineFunction &MF, const std::vector< CalleeSavedInfo > &CSI)
static void appendScalableVectorExpression(const TargetRegisterInfo &TRI, SmallVectorImpl< char > &Expr, int FixedOffset, int ScalableOffset, llvm::raw_string_ostream &Comment)
static unsigned getCalleeSavedRVVNumRegs(const Register &BaseReg)
static Align getABIStackAlignment(RISCVABI::ABI ABI)
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 void emitSCSPrologue(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, const DebugLoc &DL)
static constexpr Register RAReg
static void emitSCSEpilogue(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, const DebugLoc &DL)
static SmallVector< CalleeSavedInfo, 8 > getUnmanagedCSI(const MachineFunction &MF, const std::vector< CalleeSavedInfo > &CSI)
static Register getMaxPushPopReg(const MachineFunction &MF, const std::vector< CalleeSavedInfo > &CSI)
static unsigned getScavSlotsNumForRVV(MachineFunction &MF)
static MCCFIInstruction createDefCFAOffset(const TargetRegisterInfo &TRI, Register Reg, uint64_t FixedOffset, uint64_t ScalableOffset)
This file declares the machine register scavenger class.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:163
bool test(unsigned Idx) const
Definition: BitVector.h:461
BitVector & set()
Definition: BitVector.h:351
The CalleeSavedInfo class tracks the information need to locate where a callee saved register is in t...
Register getReg() const
A debug info location.
Definition: DebugLoc.h:33
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:277
bool hasOptNone() const
Do not optimize this function (-O0).
Definition: Function.h:701
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition: Function.cpp:369
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.cpp:731
void storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, Register SrcReg, bool isKill, int FrameIndex, const TargetRegisterClass *RC, const TargetRegisterInfo *TRI, Register VReg) const override
Store the specified register of the given register class to the specified stack frame index.
void loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, Register DestReg, int FrameIndex, const TargetRegisterClass *RC, const TargetRegisterInfo *TRI, Register VReg) const override
Load the specified register of the given register class from the specified stack frame index.
void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
static MCCFIInstruction createDefCfaRegister(MCSymbol *L, unsigned Register, SMLoc Loc={})
.cfi_def_cfa_register modifies a rule for computing CFA.
Definition: MCDwarf.h:582
static MCCFIInstruction createRestore(MCSymbol *L, unsigned Register, SMLoc Loc={})
.cfi_restore says that the rule for Register is now the same as it was at the beginning of the functi...
Definition: MCDwarf.h:656
static MCCFIInstruction cfiDefCfa(MCSymbol *L, unsigned Register, int64_t Offset, SMLoc Loc={})
.cfi_def_cfa defines a rule for computing CFA as: take address from Register and add Offset to it.
Definition: MCDwarf.h:575
static MCCFIInstruction createOffset(MCSymbol *L, unsigned Register, int64_t Offset, SMLoc Loc={})
.cfi_offset Previous value of Register is saved at offset Offset from CFA.
Definition: MCDwarf.h:617
static MCCFIInstruction cfiDefCfaOffset(MCSymbol *L, int64_t Offset, SMLoc Loc={})
.cfi_def_cfa_offset modifies a rule for computing CFA.
Definition: MCDwarf.h:590
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:687
Wrapper class representing physical registers. Should be passed by value.
Definition: MCRegister.h:33
MachineBasicBlock * getFallThrough(bool JumpToFallThrough=true)
Return the fallthrough block if the block can implicitly transfer control to the block after it by fa...
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
unsigned succ_size() const
bool isReturnBlock() const
Convenience function that returns true if the block ends in a return instruction.
void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
DebugLoc findDebugLoc(instr_iterator MBBI)
Find the next valid DebugLoc starting at MBBI, skipping any debug instructions.
iterator getLastNonDebugInstr(bool SkipPseudoOp=true)
Returns an iterator to the last non-debug instruction in the basic block, or end().
void eraseFromParent()
This method unlinks 'this' from the containing function and deletes it.
void addLiveIn(MCRegister PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
bool isLiveIn(MCRegister Reg, LaneBitmask LaneMask=LaneBitmask::getAll()) const
Return true if the specified register is in the live in set.
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
bool hasVarSizedObjects() const
This method may be called any time after instruction selection is complete to determine if the stack ...
uint64_t getStackSize() const
Return the number of bytes that must be allocated to hold all of the fixed size frame objects.
bool adjustsStack() const
Return true if this function adjusts the stack – e.g., when calling another function.
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.
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.
int CreateSpillStackObject(uint64_t Size, Align Alignment)
Create a new statically sized stack object that represents a spill slot, returning a nonnegative iden...
uint64_t estimateStackSize(const MachineFunction &MF) const
Estimate and return the size of the stack frame.
void setStackID(int ObjectIdx, uint8_t ID)
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
int64_t getObjectSize(int ObjectIdx) const
Return the size of the specified object.
bool isMaxCallFrameSizeComputed() const
const std::vector< CalleeSavedInfo > & getCalleeSavedInfo() const
Returns a reference to call saved info vector for the current function.
int getObjectIndexEnd() const
Return one past the maximum frame object index.
int CreateFixedSpillStackObject(uint64_t Size, int64_t SPOffset, bool IsImmutable=false)
Create a spill slot at a fixed location on the stack.
uint8_t getStackID(int ObjectIdx) const
int64_t getObjectOffset(int ObjectIdx) const
Return the assigned stack offset of the specified object from the incoming stack pointer.
void setStackSize(uint64_t Size)
Set the size of the stack.
bool isFixedObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a fixed stack object.
bool isDeadObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a dead object.
unsigned addFrameInst(const MCCFIInstruction &Inst)
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *BB=nullptr, std::optional< UniqueBBID > BBID=std::nullopt)
CreateMachineBasicBlock - Allocate a new MachineBasicBlock.
void insert(iterator MBBI, MachineBasicBlock *MBB)
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
const MachineInstrBuilder & addExternalSymbol(const char *FnName, unsigned TargetFlags=0) const
const MachineInstrBuilder & addCFIIndex(unsigned CFIIndex) const
const MachineInstrBuilder & setMIFlag(MachineInstr::MIFlag Flag) const
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
const MachineInstrBuilder & addUse(Register RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register use operand.
const MachineInstrBuilder & setMIFlags(unsigned Flags) const
const MachineInstrBuilder & addDef(Register RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register definition operand.
Representation of each machine instruction.
Definition: MachineInstr.h:69
bool isReserved(MCRegister PhysReg) const
isReserved - Returns true when PhysReg is a reserved register.
Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:310
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
bool canUseAsEpilogue(const MachineBasicBlock &MBB) const override
Check whether or not the given MBB can be used as a epilogue for the target.
bool assignCalleeSavedSpillSlots(MachineFunction &MF, const TargetRegisterInfo *TRI, std::vector< CalleeSavedInfo > &CSI, unsigned &MinCSFrameIndex, unsigned &MaxCSFrameIndex) const override
assignCalleeSavedSpillSlots - Allows target to override spill slot assignment logic.
bool hasFPImpl(const MachineFunction &MF) const override
bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, MutableArrayRef< CalleeSavedInfo > CSI, const TargetRegisterInfo *TRI) const override
restoreCalleeSavedRegisters - Issues instruction(s) to restore all callee saved registers and returns...
bool hasReservedCallFrame(const MachineFunction &MF) const override
hasReservedCallFrame - Under normal circumstances, when a frame pointer is not required,...
void allocateStack(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, MachineFunction &MF, uint64_t Offset, uint64_t RealStackSize, bool EmitCFI, bool NeedProbe, uint64_t ProbeSize) const
const RISCVSubtarget & STI
StackOffset getFrameIndexReference(const MachineFunction &MF, int FI, Register &FrameReg) const override
getFrameIndexReference - This method should return the base register and offset used to reference a f...
bool isSupportedStackID(TargetStackID::Value ID) const override
void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS) const override
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override
TargetStackID::Value getStackIDForScalableVectors() const override
Returns the StackID that scalable vectors should be associated with.
void processFunctionBeforeFrameFinalized(MachineFunction &MF, RegScavenger *RS) const override
processFunctionBeforeFrameFinalized - This method is called immediately before the specified function...
MachineBasicBlock::iterator eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const override
This method is called during prolog/epilog code insertion to eliminate call frame setup and destroy p...
bool canUseAsPrologue(const MachineBasicBlock &MBB) const override
Check whether or not the given MBB can be used as a prologue for the target.
RISCVFrameLowering(const RISCVSubtarget &STI)
uint64_t getStackSizeWithRVVPadding(const MachineFunction &MF) const
RISCVMachineFunctionInfo - This class is derived from MachineFunctionInfo and contains private RISCV-...
bool isPushable(const MachineFunction &MF) const
bool useSaveRestoreLibCalls(const MachineFunction &MF) const
bool hasStdExtCOrZca() const
unsigned getXLen() const
bool hasVInstructions() const
bool isRegisterReservedByUser(Register i) const override
const RISCVRegisterInfo * getRegisterInfo() const override
const RISCVInstrInfo * getInstrInfo() const override
bool hasInlineStackProbe(const MachineFunction &MF) const override
True if stack clash protection is enabled for this functions.
unsigned getStackProbeSize(const MachineFunction &MF, Align StackAlign) const
bool isRegUsed(Register Reg, bool includeReserved=true) const
Return if a specific register is currently used.
void enterBasicBlock(MachineBasicBlock &MBB)
Start tracking liveness from the begin of basic block MBB.
void addScavengingFrameIndex(int FI)
Add a scavenging frame index.
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
constexpr unsigned id() const
Definition: Register.h:103
Represents a location in source code.
Definition: SMLoc.h:23
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
void append(StringRef RHS)
Append from a StringRef.
Definition: SmallString.h:68
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:254
bool empty() const
Definition: SmallVector.h:81
size_t size() const
Definition: SmallVector.h:78
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:573
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:683
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
StackOffset holds a fixed and a scalable offset in bytes.
Definition: TypeSize.h:33
int64_t getFixed() const
Returns the fixed component of the stack.
Definition: TypeSize.h:49
int64_t getScalable() const
Returns the scalable component of the stack.
Definition: TypeSize.h:52
static StackOffset get(int64_t Fixed, int64_t Scalable)
Definition: TypeSize.h:44
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
Information about stack frame layout on the target.
bool hasFP(const MachineFunction &MF) const
hasFP - Return true if the specified function should have a dedicated frame pointer register.
virtual void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS=nullptr) const
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
int getOffsetOfLocalArea() const
getOffsetOfLocalArea - This method returns the offset of the local area from the stack pointer on ent...
Align getStackAlign() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
int alignSPAdjust(int SPAdj) const
alignSPAdjust - This method aligns the stack adjustment to the correct alignment.
TargetInstrInfo - Interface to description of machine instruction set.
TargetOptions Options
bool DisableFramePointerElim(const MachineFunction &MF) const
DisableFramePointerElim - This returns true if frame pointer elimination optimization should be disab...
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
const TargetRegisterClass * getMinimalPhysRegClass(MCRegister Reg, MVT VT=MVT::Other) const
Returns the Register Class of a physical register of the given type, picking the most sub register cl...
Align getSpillAlign(const TargetRegisterClass &RC) const
Return the minimum required alignment in bytes for a spill slot for a register of this class.
bool hasStackRealignment(const MachineFunction &MF) const
True if stack realignment is required and still possible.
unsigned getSpillSize(const TargetRegisterClass &RC) const
Return the size in bytes of the stack slot allocated to hold a spilled copy of a register from class ...
virtual Register getFrameRegister(const MachineFunction &MF) const =0
Debug information queries.
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
virtual const TargetInstrInfo * getInstrInfo() const
self_iterator getIterator()
Definition: ilist_node.h:132
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:661
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ GHC
Used by the Glasgow Haskell Compiler (GHC).
Definition: CallingConv.h:50
MCRegister getBPReg()
MCRegister getSCSPReg()
static constexpr unsigned RVVBitsPerBlock
bool isRVVSpill(const MachineInstr &MI)
@ Implicit
Not emitted register (e.g. carry, or temporary result).
@ Define
Register definition.
Reg
All possible values of the reg field in the ModR/M byte.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
IterT next_nodbg(IterT It, IterT End, bool SkipPseudoOp=true)
Increment It, then continue incrementing it while it points to a debug instruction.
@ Offset
Definition: DWP.cpp:480
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:1697
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
constexpr T alignDown(U Value, V Align, W Skew=0)
Returns the largest unsigned integer less than or equal to Value and is Skew mod Align.
Definition: MathExtras.h:555
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:1753
uint64_t offsetToAlignment(uint64_t Value, Align Alignment)
Returns the offset to the next integer (mod 2**64) that is greater than or equal to Value and is a mu...
Definition: Alignment.h:197
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:155
unsigned encodeSLEB128(int64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a SLEB128 value to an output stream.
Definition: LEB128.h:23
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:1766
unsigned encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a ULEB128 value to an output stream.
Definition: LEB128.h:80
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition: Alignment.h:208
void fullyRecomputeLiveIns(ArrayRef< MachineBasicBlock * > MBBs)
Convenience function for recomputing live-in's for a set of MBBs until the computation converges.
Definition: LivePhysRegs.h:215
Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
uint64_t value() const
This is a hole in the type system and should not be abused.
Definition: Alignment.h:85
static bool isRVVRegClass(const TargetRegisterClass *RC)
void adjustReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator II, const DebugLoc &DL, Register DestReg, Register SrcReg, StackOffset Offset, MachineInstr::MIFlag Flag, MaybeAlign RequiredAlign) const