34#define DEBUG_TYPE "riscv-frame"
67 RISCV::X18, RISCV::X19, RISCV::X20,
68 RISCV::X21, RISCV::X22, RISCV::X23,
69 RISCV::X24, RISCV::X25, RISCV::X26,
82 return STI.hasStdExtE() ? 24 : 32;
89 { RISCV::X10, -1}, { RISCV::X11, -2}, { RISCV::X12, -3},
90 { RISCV::X13, -4}, { RISCV::X14, -5}, { RISCV::X15, -6},
151 "Unexpected Shadow Stack Kind");
155 bool IsRV64 = STI.is64Bit();
156 int64_t SlotSize = STI.getXLen() / 8;
175 char DwarfSCSReg =
TRI->getDwarfRegNum(SCSPReg,
true);
176 assert(DwarfSCSReg < 32 &&
"SCS Register should be < 32 (X3).");
178 char Offset =
static_cast<char>(-SlotSize) & 0x7f;
179 const char CFIInst[] = {
180 dwarf::DW_CFA_val_expression,
183 static_cast<char>(
unsigned(dwarf::DW_OP_breg0 + DwarfSCSReg)),
208 if (
MI !=
MBB.end() &&
209 (
MI->getOpcode() == RISCV::CM_POP ||
MI->getOpcode() == RISCV::QC_CM_POP))
223 "Unexpected Shadow Stack Kind");
227 bool IsRV64 = STI.is64Bit();
228 int64_t SlotSize = STI.getXLen() / 8;
253 if (!RVFI->isSiFiveStackSwapInterrupt(MF))
259 assert(STI.hasVendorXSfmclic() &&
"Stack Swapping Requires XSfmclic");
262 .
addImm(RISCVSysReg::sf_mscratchcsw)
282 for (
int I = 0;
I < 2; ++
I) {
295 assert(ScratchCS != CSI.end() &&
"Missing interrupt scratch spill slot");
296 return ScratchCS->getFrameIdx();
305 if (!RVFI->isSiFivePreemptibleInterrupt(MF))
316 TII->storeRegToStackSlot(
MBB,
MBBI, RISCV::X5,
true, ScratchFI,
324 .
addImm(RISCVSysReg::mcause)
327 TII->storeRegToStackSlot(
MBB,
MBBI, RISCV::X5,
true,
328 RVFI->getInterruptCSRFrameIndex(0),
333 .
addImm(RISCVSysReg::mepc)
339 .
addImm(RISCVSysReg::mstatus)
342 TII->storeRegToStackSlot(
MBB,
MBBI, RISCV::X5,
true,
343 RVFI->getInterruptCSRFrameIndex(1),
355 if (!RVFI->isSiFivePreemptibleInterrupt(MF))
365 TII->loadRegFromStackSlot(
MBB,
MBBI, RISCV::X5,
366 RVFI->getInterruptCSRFrameIndex(1),
372 .
addImm(RISCVSysReg::mstatus)
379 .
addImm(RISCVSysReg::mepc)
383 TII->loadRegFromStackSlot(
MBB,
MBBI, RISCV::X5,
384 RVFI->getInterruptCSRFrameIndex(0),
388 .
addImm(RISCVSysReg::mcause)
394 TII->loadRegFromStackSlot(
MBB,
MBBI, RISCV::X5,
408 if (!RVFI->isACLICPreemptibleInterrupt(MF))
413 const unsigned StatusCSR =
414 IsSupervisor ? RISCVSysReg::sistatus : RISCVSysReg::mistatus;
415 const unsigned EpcCSR = IsSupervisor ? RISCVSysReg::sepc : RISCVSysReg::mepc;
420 TII->storeRegToStackSlot(
MBB,
MBBI, RISCV::X5,
true, ScratchFI,
431 TII->storeRegToStackSlot(
432 MBB,
MBBI, RISCV::X5,
true, RVFI->getInterruptCSRFrameIndex(0),
441 TII->storeRegToStackSlot(
442 MBB,
MBBI, RISCV::X5,
true, RVFI->getInterruptCSRFrameIndex(1),
453 if (!RVFI->isACLICPreemptibleInterrupt(MF))
458 const unsigned StatusCSR =
459 IsSupervisor ? RISCVSysReg::sistatus : RISCVSysReg::mistatus;
460 const unsigned EpcCSR = IsSupervisor ? RISCVSysReg::sepc : RISCVSysReg::mepc;
464 TII->loadRegFromStackSlot(
MBB,
MBBI, RISCV::X5,
465 RVFI->getInterruptCSRFrameIndex(1),
473 TII->loadRegFromStackSlot(
MBB,
MBBI, RISCV::X5,
474 RVFI->getInterruptCSRFrameIndex(0),
482 TII->loadRegFromStackSlot(
MBB,
MBBI, RISCV::X5,
495 const std::vector<CalleeSavedInfo> &CSI) {
498 if (CSI.empty() || !RVFI->useSaveRestoreLibCalls(MF))
505 if (CS.getFrameIdx() < 0)
506 MaxReg = std::max(MaxReg.
id(), CS.getReg().id());
511 switch (MaxReg.
id()) {
515 case RISCV::X27:
return 12;
516 case RISCV::X26:
return 11;
517 case RISCV::X25:
return 10;
518 case RISCV::X24:
return 9;
519 case RISCV::X23:
return 8;
520 case RISCV::X22:
return 7;
521 case RISCV::X21:
return 6;
522 case RISCV::X20:
return 5;
523 case RISCV::X19:
return 4;
524 case RISCV::X18:
return 3;
525 case RISCV::X9:
return 2;
526 case FPReg:
return 1;
527 case RAReg:
return 0;
536 const std::vector<CalleeSavedInfo> &CSI) {
537 static const char *
const SpillLibCalls[] = {
556 return SpillLibCalls[LibCallID];
563 const std::vector<CalleeSavedInfo> &CSI) {
564 static const char *
const RestoreLibCalls[] = {
575 "__riscv_restore_10",
576 "__riscv_restore_11",
583 return RestoreLibCalls[LibCallID];
588 unsigned NumPushPopRegs = 0;
589 for (
auto &CS : CSI) {
593 unsigned RegNum = std::distance(std::begin(
FixedCSRFIMap), FII);
594 NumPushPopRegs = std::max(NumPushPopRegs, RegNum + 1);
597 assert(NumPushPopRegs != 12 &&
"x26 requires x27 to also be pushed");
598 return NumPushPopRegs;
604 const auto *
TRI =
STI.getRegisterInfo();
609 if (RVFI->getShadowStackKind(MF) ==
619 if (RISCV::GPRPairRegClass.
contains(Reg)) {
620 MCRegister EvenReg =
TRI->getSubReg(Reg, RISCV::sub_gpr_even);
670 TRI->hasStackRealignment(MF);
674void RISCVFrameLowering::determineFrameLayout(
MachineFunction &MF)
const {
682 if (RVFI->useQCIInterrupt(MF))
689 FrameSize =
alignTo(FrameSize, StackAlign);
699 if (RVFI->getRVVStackSize() && (!
hasFP(MF) ||
TRI->hasStackRealignment(MF))) {
700 int ScalarLocalVarSize = FrameSize - RVFI->getCalleeSavedStackSize() -
701 RVFI->getVarArgsSaveSize();
702 if (
auto RVVPadding =
704 RVFI->setRVVPadding(RVVPadding);
719 const std::vector<CalleeSavedInfo> &CSI,
720 bool ReverseOrder =
false) {
724 for (
auto &CS : CSI) {
725 int FI = CS.getFrameIdx();
728 bool IsManagedFI = (FI < 0);
736 std::reverse(NonLibcallCSI.
begin(), NonLibcallCSI.
end());
738 return NonLibcallCSI;
745 const std::vector<CalleeSavedInfo> &CSI,
746 bool ReverseOrder =
false) {
749 if (RVFI->isSiFivePreemptibleInterrupt(MF) ||
750 RVFI->isACLICPreemptibleInterrupt(MF))
752 return CS.
getReg() == RISCV::X5;
759 const std::vector<CalleeSavedInfo> &CSI) {
763 for (
auto &CS : CSI) {
764 int FI = CS.getFrameIdx();
774 const std::vector<CalleeSavedInfo> &CSI) {
778 if (!RVFI->useSaveRestoreLibCalls(MF) && !RVFI->isPushable(MF))
779 return PushOrLibCallsCSI;
781 for (
const auto &CS : CSI) {
782 if (RVFI->useQCIInterrupt(MF)) {
793 PushOrLibCallsCSI.push_back(CS);
796 return PushOrLibCallsCSI;
801 const std::vector<CalleeSavedInfo> &CSI) {
805 if (!RVFI->useQCIInterrupt(MF))
806 return QCIInterruptCSI;
808 for (
const auto &CS : CSI) {
811 QCIInterruptCSI.push_back(CS);
814 return QCIInterruptCSI;
819 const std::vector<CalleeSavedInfo> &CSI) {
823 if (!RVFI->isACLICPushPopInterrupt(MF))
824 return ACLICInterruptCSI;
826 for (
const auto &CS : CSI) {
829 ACLICInterruptCSI.push_back(CS);
832 return ACLICInterruptCSI;
840 for (
unsigned i = 0; CSRegs[i]; ++i)
863 static const MCPhysReg CandidateRegs[] = {
864 RISCV::X5, RISCV::X6, RISCV::X7, RISCV::X28,
865 RISCV::X29, RISCV::X30, RISCV::X31,
868 for (
unsigned Reg : CandidateRegs) {
869 if (Reg != DontUseReg &&
LiveRegs.available(
MRI, Reg))
876void RISCVFrameLowering::allocateAndProbeStackForRVV(
880 assert(Amount != 0 &&
"Did not need to adjust stack pointer for RVV.");
887 "No available scratch register for stack probing");
892 TII->mulImm(MF,
MBB,
MBBI,
DL, TargetReg, NumOfVReg, Flag);
897 CFIBuilder.buildDefCFA(TargetReg, -Amount);
906 CFIBuilder.buildDefCFARegister(
SPReg);
920 int64_t FixedOffset =
Offset.getFixed();
921 int64_t ScalableOffset =
Offset.getScalable();
922 unsigned DwarfVLenB =
TRI.getDwarfRegNum(RISCV::VLENB,
true);
927 Comment << (FixedOffset < 0 ?
" - " :
" + ") << std::abs(FixedOffset);
940 Comment << (ScalableOffset < 0 ?
" - " :
" + ") << std::abs(ScalableOffset)
947 assert(
Offset.getScalable() != 0 &&
"Did not need to adjust CFA for RVV");
949 std::string CommentBuffer;
952 unsigned DwarfReg =
TRI.getDwarfRegNum(
Reg,
true);
963 DefCfaExpr.
push_back(dwarf::DW_CFA_def_cfa_expression);
973 assert(
Offset.getScalable() != 0 &&
"Did not need to adjust CFA for RVV");
975 std::string CommentBuffer;
983 unsigned DwarfReg =
TRI.getDwarfRegNum(
Reg,
true);
984 DefCfaExpr.
push_back(dwarf::DW_CFA_expression);
1001 uint64_t RealStackSize,
bool EmitCFI,
1002 bool NeedProbe, uint64_t ProbeSize,
1003 uint64_t FollowupSize,
bool SpillsToSP,
1008 bool IsRV64 =
STI.is64Bit();
1010 auto NeedsSPProbe = [&](uint64_t Unprobed) {
1011 return NeedProbe && !SpillsToSP && Unprobed != 0 &&
1016 if (!NeedProbe ||
Offset <= ProbeSize) {
1023 if (NeedsSPProbe(
Offset)) {
1036 uint64_t CFAAdjust = RealStackSize -
Offset;
1039 if (
Offset < ProbeSize * 5) {
1040 uint64_t CurrentOffset = 0;
1041 while (CurrentOffset + ProbeSize <=
Offset) {
1051 CurrentOffset += ProbeSize;
1056 uint64_t Residual =
Offset - CurrentOffset;
1063 if (NeedsSPProbe(Residual)) {
1078 uint64_t Residual =
Offset - RoundedSize;
1082 "No available scratch register for stack probing");
1089 CFIBuilder.
buildDefCFA(TargetReg, RoundedSize + CFAAdjust);
1107 if (NeedsSPProbe(Residual)) {
1119 case RISCV::CM_PUSH:
1120 case RISCV::QC_CM_PUSH:
1121 case RISCV::QC_CM_PUSHFP:
1133 case RISCV::QC_CM_POP:
1144 return RISCV::CM_PUSH;
1146 return UpdateFP ? RISCV::QC_CM_PUSHFP : RISCV::QC_CM_PUSH;
1157 return RISCV::CM_POP;
1159 return RISCV::QC_CM_POP;
1171 bool PreferAscendingLS =
STI.preferAscendingLoadStore();
1186 if (
needsDwarfCFI(MF) && RVFI->isACLICPushPopInterrupt(MF)) {
1204 auto PossiblePush =
MBBI;
1211 determineFrameLayout(MF);
1224 if (
unsigned LibCallFrameSize = RVFI->getLibCallStackSize()) {
1225 if (NeedsDwarfCFI) {
1236 uint64_t StackSize = RealStackSize - RVFI->getReservedSpillsSize();
1237 uint64_t RVVStackSize = RVFI->getRVVStackSize();
1240 if (RealStackSize == 0 && !MFI.
adjustsStack() && RVVStackSize == 0)
1245 if (
STI.isRegisterReservedByUser(
SPReg))
1247 MF.
getFunction(),
"Stack pointer required, but has been reserved."});
1251 if (FirstSPAdjustAmount) {
1252 StackSize = FirstSPAdjustAmount - RVFI->getACLICInterruptStackSize();
1253 RealStackSize = FirstSPAdjustAmount;
1256 if (RVFI->useQCIInterrupt(MF)) {
1261 if (NeedsDwarfCFI) {
1274 if (RVFI->isPushable(MF) && PossiblePush !=
MBB.end() &&
1275 isPush(PossiblePush->getOpcode())) {
1281 std::min(
alignDown(StackSize, 16),
static_cast<uint64_t
>(48));
1282 PossiblePush->getOperand(1).setImm(StackAdj);
1283 StackSize -= StackAdj;
1285 if (NeedsDwarfCFI) {
1296 bool NeedProbe =
TLI->hasInlineStackProbe(MF);
1305 uint64_t BodyAllocSize = 0;
1306 if (RVFI->hasDynamicAllocation())
1307 BodyAllocSize = FromProbedSP;
1315 bool RealignStack = RI->hasStackRealignment(MF);
1316 uint64_t RealignPadding =
1320 bool ProbeRealignLoop = NeedProbe && RealignPadding >= ProbeSize;
1321 bool ProbeRealignedSP = NeedProbe && RealignStack &&
1322 (ProbeRealignLoop || RealignPadding + BodyAllocSize >
1327 uint64_t FollowupSize =
1328 RealignPadding + (ProbeRealignedSP ? 0 : BodyAllocSize);
1329 if (RVVStackSize || ProbeRealignLoop)
1330 FollowupSize = FromProbedSP;
1331 if (StackSize != 0) {
1341 return FrameReg ==
SPReg && !
Offset.getScalable() &&
1346 NeedProbe, ProbeSize,
1347 FirstSPAdjustAmount ? FromProbedSP : FollowupSize, SpillsToSP,
1368 if (NeedsDwarfCFI) {
1375 if (RISCV::GPRPairRegClass.
contains(Reg)) {
1376 MCRegister EvenReg = RI->getSubReg(Reg, RISCV::sub_gpr_even);
1377 MCRegister OddReg = RI->getSubReg(Reg, RISCV::sub_gpr_odd);
1388 if (
STI.isRegisterReservedByUser(
FPReg))
1390 MF.
getFunction(),
"Frame pointer required, but has been reserved."});
1396 if (!RVFI->hasImplicitFPUpdates(MF)) {
1407 uint64_t SecondSPAdjustAmount = 0;
1409 if (FirstSPAdjustAmount) {
1411 assert(SecondSPAdjustAmount > 0 &&
1412 "SecondSPAdjustAmount should be greater than zero");
1417 NeedProbe, ProbeSize, FollowupSize,
false,
1423 allocateAndProbeStackForRVV(MF,
MBB,
MBBI,
DL, RVVStackSize,
1425 NeedsDwarfCFI && !
hasFP(MF));
1434 if (NeedsDwarfCFI && !
hasFP(MF)) {
1460 if (RI->hasStackRealignment(MF)) {
1464 if (ProbeRealignLoop) {
1470 "No available scratch register for stack probing");
1471 unsigned ShiftAmount =
STI.getXLen() -
Log2(MaxAlignment);
1488 }
else if (
isInt<12>(-(int64_t)MaxAlignment.value())) {
1491 .
addImm(-(int64_t)MaxAlignment.value())
1494 unsigned ShiftAmount =
Log2(MaxAlignment);
1506 if (ProbeRealignedSP) {
1507 bool IsRV64 =
STI.is64Bit();
1532 uint64_t &StackSize,
1533 int64_t CFAOffset)
const {
1550 bool PreferAscendingLS =
STI.preferAscendingLoadStore();
1562 MBBI =
MBB.getLastNonDebugInstr();
1564 DL =
MBBI->getDebugLoc();
1566 MBBI =
MBB.getFirstTerminator();
1579 auto FirstScalarCSRRestoreInsn =
1586 uint64_t RealStackSize = FirstSPAdjustAmount ? FirstSPAdjustAmount
1588 uint64_t StackSize =
1590 ? FirstSPAdjustAmount - RVFI->getACLICInterruptStackSize()
1592 uint64_t FPOffset = RealStackSize - RVFI->getVarArgsSaveSize();
1593 uint64_t RVVStackSize = RVFI->getRVVStackSize();
1599 bool CallFrameMovesSP =
1602 bool RestoreSPFromFP =
1609 if (!RestoreSPFromFP)
1614 if (NeedsDwarfCFI) {
1617 emitCalleeSavedRVVEpilogCFI(
MBB, FirstScalarCSRRestoreInsn);
1621 if (FirstSPAdjustAmount) {
1622 uint64_t SecondSPAdjustAmount =
1624 assert(SecondSPAdjustAmount > 0 &&
1625 "SecondSPAdjustAmount should be greater than zero");
1629 if (!RestoreSPFromFP)
1634 if (NeedsDwarfCFI && !
hasFP(MF))
1648 if (RestoreSPFromFP) {
1649 assert(
hasFP(MF) &&
"frame pointer should not have been eliminated");
1655 if (NeedsDwarfCFI &&
hasFP(MF))
1661 MBBI = std::next(FirstScalarCSRRestoreInsn,
1667 auto emitRestoreCFI = [&](
auto CSInfo) {
1668 for (
auto &CS : CSInfo) {
1671 if (RISCV::GPRPairRegClass.
contains(Reg)) {
1672 MCRegister EvenReg = RI->getSubReg(Reg, RISCV::sub_gpr_even);
1673 MCRegister OddReg = RI->getSubReg(Reg, RISCV::sub_gpr_odd);
1682 if (RVFI->useSaveRestoreLibCalls(MF)) {
1683 if (RVFI->hasShadowStack(MF)) {
1688 StackSize += RVFI->getLibCallStackSize();
1696 emitRestoreCFI(LibcallCSI);
1702 deallocateStack(MF,
MBB,
MBBI,
DL, StackSize,
1703 RVFI->getLibCallStackSize());
1715 if (RVFI->isPushable(MF) &&
MBBI !=
MBB.end() &&
isPop(
MBBI->getOpcode())) {
1721 std::min(
alignDown(StackSize, 16),
static_cast<uint64_t
>(48));
1722 MBBI->getOperand(1).setImm(StackAdj);
1723 StackSize -= StackAdj;
1726 deallocateStack(MF,
MBB,
MBBI,
DL, StackSize,
1727 RealStackSize - StackSize);
1729 if (NeedsDwarfCFI) {
1742 deallocateStack(MF,
MBB,
MBBI,
DL, StackSize,
1743 RVFI->getQCIInterruptStackSize() +
1744 RVFI->getACLICInterruptStackSize());
1758 std::array<TargetRegisterClass const *, 2> RegisterClasses = {
1759 &RISCV::GPRF16RegClass, &RISCV::GPRF32RegClass};
1760 std::array<unsigned, 2> SubIdx = {RISCV::sub_16, RISCV::sub_32};
1762 for (
auto [RegClass, SubReg] :
zip(RegisterClasses, SubIdx)) {
1763 if (RegClass->contains(
Reg)) {
1765 TRI.getMatchingSuperReg(
Reg, SubReg, &RISCV::GPRRegClass))
1771 "getPhysicalGPR called with unsupported register");
1777 if (!STI.hasStdExtF())
1781 assert(LargestFPRegClass);
1786 std::array<TargetRegisterClass const *, 3> RegisterClasses = {
1787 &RISCV::FPR16RegClass, &RISCV::FPR32RegClass, &RISCV::FPR64RegClass};
1788 std::array<unsigned, 3> SubIdx = {RISCV::sub_16, RISCV::sub_32,
1791 for (
auto [RegClass, SubReg] :
zip(RegisterClasses, SubIdx)) {
1792 if (RegClass->contains(
Reg)) {
1794 TRI.getMatchingSuperReg(
Reg, SubReg, LargestFPRegClass))
1804void RISCVFrameLowering::emitZeroCallUsedRegs(
BitVector RegsToZero,
1813 DL =
MBBI->getDebugLoc();
1816 const RISCVRegisterInfo &
TRI = *
STI.getRegisterInfo();
1817 const RISCVInstrInfo &
TII = *
STI.getInstrInfo();
1819 BitVector FinalRegsToZero(
TRI.getNumRegs());
1821 bool HasVRegister =
false;
1824 if (
TRI.isGeneralPurposeRegister(MF,
Reg)) {
1826 }
else if (RISCV::GPRPairRegClass.
contains(
Reg)) {
1827 FinalRegsToZero.set(
1829 FinalRegsToZero.set(
1831 }
else if (
TRI.isFPRegister(
Reg)) {
1833 FinalRegsToZero.set(MaybeReg.id());
1835 TRI.getMinimalPhysRegClass(
Reg))) {
1836 if (!
STI.hasVInstructions())
1838 HasVRegister =
true;
1840 for (MCRegister SubReg :
TRI.subregs_inclusive(
Reg)) {
1841 if (
TRI.subregs(SubReg).empty())
1842 FinalRegsToZero.set(SubReg.id());
1850 VLMUL, 32,
true,
true);
1852 MCRegister TemporaryReg = RISCV::NoRegister;
1853 for (MCRegister
Reg : FinalRegsToZero.set_bits()) {
1854 if (
TRI.isGeneralPurposeRegister(MF,
Reg)) {
1860 if (TemporaryReg == RISCV::NoRegister) {
1861 RS->enterBasicBlockEnd(
MBB);
1862 TemporaryReg =
RS->scavengeRegisterBackwards(RISCV::GPRRegClass,
MBBI,
1871 FinalRegsToZero.set(TemporaryReg.
id());
1880 for (MCRegister
Reg : FinalRegsToZero.set_bits())
1895 STI.preferAscendingLoadStore());
1903 "Unexpected stack ID for the frame object.");
1915 MinCSFI = std::min(CSI.front().getFrameIdx(), CSI.back().getFrameIdx());
1916 MaxCSFI = std::max(CSI.front().getFrameIdx(), CSI.back().getFrameIdx());
1920 return FI == CS.getFrameIdx();
1923 bool IsInterruptCSR = RVFI->isInterruptCSRFrameIndex(FI);
1926 if ((FI >= MinCSFI && FI <= MaxCSFI) || IsInterruptCSR ||
1927 IsInlineRestoredLibcallCSR) {
1930 if (FirstSPAdjustAmount)
1994 int64_t CLWSPMaxOffset = 252;
1995 int64_t CLDSPMaxOffset = 504;
1996 int64_t SPThreshold =
STI.is64Bit() ? CLDSPMaxOffset : CLWSPMaxOffset;
1997 if (SPOff >= 0 && SPOff <= SPThreshold)
2003 "Expected fixed object with stack realignment");
2004 assert(
hasFP(MF) &&
"Re-aligned stack must have frame pointer");
2008 if (FrameReg ==
FPReg) {
2027 "Can't index across variable sized realign");
2032 "Inconsistent stack layout");
2075 "Can't index across variable sized realign");
2077 RVFI->getRVVStackSize());
2084 int64_t ScalarLocalVarSize =
2086 RVFI->getVarArgsSaveSize() + RVFI->getRVVPadding();
2097 if (!BaseReg.isValid())
2117 for (
unsigned i = 0; CSRegs[i]; ++i) {
2118 unsigned CSReg = CSRegs[i];
2123 SavedRegs.
reset(CSReg);
2125 auto SubRegs =
TRI.subregs(CSReg);
2127 if (!
MRI.def_empty(CSReg) ||
MRI.getUsedPhysRegsMask().test(CSReg)) {
2128 SavedRegs.
set(CSReg);
2129 for (
unsigned Reg : SubRegs)
2147 if (RVFI->isSiFivePreemptibleInterrupt(MF) ||
2148 RVFI->isACLICPreemptibleInterrupt(MF))
2149 SavedRegs.
set(RISCV::X5);
2152 if (RVFI->isPushable(MF) && SavedRegs.
test(RISCV::X26))
2153 SavedRegs.
set(RISCV::X27);
2160 bool UseZilsd = !
STI.is64Bit() &&
STI.hasStdExtZilsd() &&
2162 !RVFI->isPushable(MF) && !RVFI->useSaveRestoreLibCalls(MF);
2166 for (
unsigned i = 0; CSRegs[i]; ++i) {
2168 CSRSet.
insert(CSRegs[i]);
2173 for (
MCPhysReg Pair : RISCV::GPRPairRegClass) {
2177 MCRegister EvenReg =
TRI.getSubReg(Pair, RISCV::sub_gpr_even);
2178 MCRegister OddReg =
TRI.getSubReg(Pair, RISCV::sub_gpr_odd);
2185 MRI.setCalleeSavedRegs(NewCSRs);
2186 CSRegs =
MRI.getCalleeSavedRegs();
2192 for (
unsigned i = 0; CSRegs[i]; ++i) {
2194 bool CombineToSuperReg;
2195 if (RISCV::GPRPairRegClass.
contains(CSReg)) {
2196 MCRegister EvenReg =
TRI.getSubReg(CSReg, RISCV::sub_gpr_even);
2197 MCRegister OddReg =
TRI.getSubReg(CSReg, RISCV::sub_gpr_odd);
2199 SavedRegs.
test(EvenReg.
id()) && SavedRegs.
test(OddReg.
id());
2202 if (
hasFP(MF) && CSReg == RISCV::X8_X9)
2203 CombineToSuperReg =
false;
2204 if (RVFI->useQCIInterrupt(MF) &&
2207 CombineToSuperReg =
false;
2209 if (RVFI->isACLICPushPopInterrupt(MF) &&
2212 CombineToSuperReg =
false;
2214 auto SubRegs =
TRI.subregs(CSReg);
2216 !SubRegs.empty() &&
llvm::all_of(SubRegs, [&](
unsigned Reg) {
2217 return SavedRegs.
test(Reg);
2221 if (CombineToSuperReg)
2222 SavedRegs.
set(CSReg);
2229std::pair<int64_t, Align>
2230RISCVFrameLowering::assignRVVStackObjectOffsets(
MachineFunction &MF)
const {
2234 auto pushRVVObjects = [&](
int FIBegin,
int FIEnd) {
2235 for (
int I = FIBegin, E = FIEnd;
I != E; ++
I) {
2248 if (!RVVCSI.empty())
2249 pushRVVObjects(RVVCSI[0].getFrameIdx(),
2250 RVVCSI[RVVCSI.size() - 1].getFrameIdx() + 1);
2254 Align RVVStackAlign(16);
2257 if (!
ST.hasVInstructions()) {
2259 "Can't allocate scalable-vector objects without V instructions");
2260 return std::make_pair(0, RVVStackAlign);
2265 for (
int FI : ObjectsToAllocate) {
2277 RVVStackAlign = std::max(RVVStackAlign, ObjectAlign);
2290 if (
auto RVVStackAlignVScale = RVVStackAlign.value() / VScale) {
2291 if (
auto AlignmentPadding =
2293 StackSize += AlignmentPadding;
2294 for (
int FI : ObjectsToAllocate)
2299 return std::make_pair(StackSize, RVVStackAlign);
2305 static constexpr unsigned ScavSlotsNumRVVSpillScalableObject = 2;
2309 static constexpr unsigned ScavSlotsNumRVVSpillNonScalableObject = 1;
2313 static constexpr unsigned ScavSlotsADDIScalableObject = 1;
2315 static constexpr unsigned MaxScavSlotsNumKnown =
2316 std::max({ScavSlotsADDIScalableObject, ScavSlotsNumRVVSpillScalableObject,
2317 ScavSlotsNumRVVSpillNonScalableObject});
2319 unsigned MaxScavSlotsNum = 0;
2325 for (
auto &MO :
MI.operands()) {
2331 MaxScavSlotsNum = std::max(
2332 MaxScavSlotsNum, IsScalableVectorID
2333 ? ScavSlotsNumRVVSpillScalableObject
2334 : ScavSlotsNumRVVSpillNonScalableObject);
2335 }
else if (
MI.getOpcode() == RISCV::ADDI && IsScalableVectorID) {
2337 std::max(MaxScavSlotsNum, ScavSlotsADDIScalableObject);
2340 if (MaxScavSlotsNum == MaxScavSlotsNumKnown)
2341 return MaxScavSlotsNumKnown;
2343 return MaxScavSlotsNum;
2367 unsigned FnSize = 0;
2368 for (
auto &
MBB : MF) {
2369 for (
auto &
MI :
MBB) {
2387 if (
MI.isConditionalBranch())
2388 FnSize +=
TII.getInstSizeInBytes(
MI);
2389 if (
MI.isConditionalBranch() ||
MI.isUnconditionalBranch()) {
2391 FnSize += 2 + 8 + 2 + 2;
2393 FnSize += 4 + 8 + 4 + 4;
2397 FnSize +=
TII.getInstSizeInBytes(
MI);
2412 int64_t RVVStackSize;
2413 Align RVVStackAlign;
2414 std::tie(RVVStackSize, RVVStackAlign) = assignRVVStackObjectOffsets(MF);
2416 RVFI->setRVVStackSize(RVVStackSize);
2417 RVFI->setRVVStackAlign(RVVStackAlign);
2426 unsigned ScavSlotsNum = 0;
2436 if (IsLargeFunction)
2437 ScavSlotsNum = std::max(ScavSlotsNum, 1u);
2444 for (
unsigned I = 0;
I < ScavSlotsNum;
I++) {
2446 RegInfo->getSpillAlign(*RC));
2447 RS->addScavengingFrameIndex(FI);
2449 if (IsLargeFunction && RVFI->getBranchRelaxationScratchFrameIndex() == -1)
2450 RVFI->setBranchRelaxationScratchFrameIndex(FI);
2453 unsigned Size = RVFI->getReservedSpillsSize();
2455 int FrameIdx = Info.getFrameIdx();
2461 RVFI->setCalleeSavedStackSize(
Size);
2485 int64_t Amount =
MI->getOperand(0).getImm();
2491 if (
MI->getOpcode() == RISCV::ADJCALLSTACKDOWN)
2497 if (
TLI->hasInlineStackProbe(MF) &&
2509 inlineStackProbe(MF,
MBB);
2518 return MBB.erase(
MI);
2536 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
2546 if (RVFI->getLibCallStackSize() || RVFI->getRVPushStackSize() ||
2547 RVFI->getQCIInterruptStackSize())
2566 if (
STI.hasStdExtZca()) {
2576 const uint64_t RVCompressLen =
STI.getXLen() * 8;
2583 auto CanCompress = [&](uint64_t CompressLen) ->
bool {
2584 if (StackSize <= 2047 + CompressLen ||
2585 (StackSize > 2048 * 2 - StackAlign &&
2586 StackSize <= 2047 * 2 + CompressLen) ||
2587 StackSize > 2048 * 3 - StackAlign)
2595 const uint64_t ADDI16SPCompressLen = 496;
2596 if (
STI.is64Bit() && CanCompress(ADDI16SPCompressLen))
2597 return ADDI16SPCompressLen;
2598 if (CanCompress(RVCompressLen))
2599 return RVCompressLen;
2601 return 2048 - StackAlign;
2608 std::vector<CalleeSavedInfo> &CSI)
const {
2613 if (RVFI->isACLICPushPopInterrupt(MF)) {
2615 RVFI->setACLICInterruptStackSize(PushAmount);
2616 MFI.CreateFixedSpillStackObject(PushAmount,
2617 -
static_cast<int64_t
>(PushAmount));
2622 if (RVFI->isSiFivePreemptibleInterrupt(MF) ||
2623 RVFI->isACLICPreemptibleInterrupt(MF)) {
2624 for (
int I = 0;
I < 2; ++
I)
2625 MFI.setIsCalleeSavedObjectIndex(RVFI->getInterruptCSRFrameIndex(
I),
true);
2632 if (RVFI->useQCIInterrupt(MF)) {
2636 if (RVFI->isPushable(MF)) {
2643 unsigned OnlyPushIfMoreThan = RVFI->useQCIInterrupt(MF) ? 2 : 0;
2644 if (PushedRegNum > OnlyPushIfMoreThan) {
2645 RVFI->setRVPushRegs(PushedRegNum);
2646 RVFI->setRVPushStackSize(
alignTo((
STI.getXLen() / 8) * PushedRegNum, 16));
2650 for (
auto &CS : CSI) {
2653 unsigned Size = RegInfo->getSpillSize(*RC);
2655 if (RVFI->useQCIInterrupt(MF)) {
2657 return P.first == CS.getReg();
2660 int64_t
Offset = FFI->second * (int64_t)
Size;
2662 int FrameIdx = MFI.CreateFixedSpillStackObject(
Size,
Offset);
2664 CS.setFrameIdx(FrameIdx);
2669 if (RVFI->isACLICPushPopInterrupt(MF)) {
2671 return P.first == CS.getReg();
2674 int64_t
Offset = FFI->second * (int64_t)
Size;
2676 int FrameIdx = MFI.CreateFixedSpillStackObject(
Size,
Offset);
2678 CS.setFrameIdx(FrameIdx);
2683 if (RVFI->useSaveRestoreLibCalls(MF) || RVFI->isPushable(MF)) {
2686 unsigned RegNum = std::distance(std::begin(
FixedCSRFIMap), FII);
2690 if (RVFI->getPushPopKind(MF) ==
2692 Offset = -int64_t(RVFI->getRVPushRegs() - RegNum) *
Size;
2696 if (RVFI->useQCIInterrupt(MF))
2699 int FrameIdx = MFI.CreateFixedSpillStackObject(
Size,
Offset);
2701 CS.setFrameIdx(FrameIdx);
2707 if (!
STI.is64Bit() &&
STI.hasStdExtZilsd() &&
2708 RISCV::GPRPairRegClass.contains(Reg)) {
2709 Align PairAlign =
STI.getZilsdAlign();
2710 int FrameIdx = MFI.CreateStackObject(8, PairAlign,
true);
2711 MFI.setIsCalleeSavedObjectIndex(FrameIdx,
true);
2712 CS.setFrameIdx(FrameIdx);
2717 Align Alignment = RegInfo->getSpillAlign(*RC);
2722 int FrameIdx = MFI.CreateStackObject(
Size, Alignment,
true);
2723 MFI.setIsCalleeSavedObjectIndex(FrameIdx,
true);
2724 CS.setFrameIdx(FrameIdx);
2729 if (RVFI->useQCIInterrupt(MF)) {
2732 MFI.CreateFixedSpillStackObject(
2736 if (RVFI->isPushable(MF)) {
2739 if (int64_t PushSize = RVFI->getRVPushStackSize())
2740 MFI.CreateFixedSpillStackObject(PushSize, -PushSize - QCIOffset);
2741 }
else if (
int LibCallRegs =
getLibCallID(MF, CSI) + 1) {
2760 unsigned LibCallFrameSize =
2762 RVFI->setLibCallStackSize(LibCallFrameSize);
2764 MFI.CreateFixedSpillStackObject(LibCallFrameSize,
2765 -
static_cast<int64_t
>(LibCallFrameSize));
2780 if (
MI !=
MBB.end() && !
MI->isDebugInstr())
2781 DL =
MI->getDebugLoc();
2790 ? RISCV::QC_C_MIENTER_NEST
2791 : RISCV::QC_C_MIENTER))
2801 if (PushedRegNum > 0) {
2809 PushBuilder.
addImm(RegEnc);
2812 for (
unsigned i = 0; i < PushedRegNum; i++)
2829 for (
auto &CS : CSI)
2834 const auto &UnmanagedCSI =
2839 for (
auto &CS : CSInfo) {
2843 TII.storeRegToStackSlot(
MBB,
MI, Reg, !
MBB.isLiveIn(Reg),
2848 storeRegsToStackSlots(UnmanagedCSI);
2849 storeRegsToStackSlots(RVVCSI);
2855 return RISCV::VRRegClass.contains(BaseReg) ? 1
2856 : RISCV::VRM2RegClass.contains(BaseReg) ? 2
2857 : RISCV::VRM4RegClass.contains(BaseReg) ? 4
2861void RISCVFrameLowering::emitCalleeSavedRVVPrologCFI(
2865 RISCVMachineFunctionInfo *RVFI = MF->
getInfo<RISCVMachineFunctionInfo>();
2866 const RISCVRegisterInfo &
TRI = *
STI.getRegisterInfo();
2877 FixedSize -= ScalarLocalVarSize;
2881 for (
auto &CS : RVVCSI) {
2883 int FI = CS.getFrameIdx();
2886 for (
unsigned i = 0; i < NumRegs; ++i) {
2894void RISCVFrameLowering::emitCalleeSavedRVVEpilogCFI(
2898 const RISCVRegisterInfo &
TRI = *
STI.getRegisterInfo();
2902 for (
auto &CS : RVVCSI) {
2905 for (
unsigned i = 0; i < NumRegs; ++i)
2906 CFIHelper.buildRestore(BaseReg + i);
2919 if (
MI !=
MBB.end() && !
MI->isDebugInstr())
2920 DL =
MI->getDebugLoc();
2928 const auto &UnmanagedCSI =
2933 for (
auto &CS : CSInfo) {
2937 RISCV::NoSubRegister,
2940 "loadRegFromStackSlot didn't insert any code!");
2943 loadRegFromStackSlot(RVVCSI);
2944 loadRegFromStackSlot(UnmanagedCSI);
2950 assert(
MI->getOpcode() == RISCV::QC_C_MILEAVERET &&
2951 "Unexpected QCI Interrupt Return Instruction");
2956 if (PushedRegNum > 0) {
2963 PopBuilder.
addImm(RegEnc);
2971 "Expected restore with libcall to use save restore libcalls");
2992 for (
auto &CS : CSI)
2997 if (
MI !=
MBB.end() &&
MI->getOpcode() == RISCV::PseudoRET) {
2999 MI->eraseFromParent();
3035 if (
STI.preferVsetvliOverReadVLENB() &&
3036 (
MBB.isLiveIn(RISCV::VTYPE) ||
MBB.isLiveIn(RISCV::VL)))
3048 RS.enterBasicBlock(*TmpMBB);
3049 return !RS.isRegUsed(RISCV::X5) && !RS.isRegUsed(RISCV::X6) &&
3050 !RS.isRegUsed(RISCV::X7);
3069 if (
MBB.succ_size() > 1)
3086 return !MI.isDebugInstr();
3102 assert(TargetReg != RISCV::X2 &&
"New top of stack cannot already be in SP");
3103 assert(ScratchReg != RISCV::X2 &&
"Scratch register cannot be SP");
3104 assert(TargetReg != ScratchReg &&
"Target and scratch must be different");
3112 Align StackAlign =
Subtarget.getFrameLowering()->getStackAlign();
3114 uint64_t ProbeSize =
TLI->getStackProbeSize(MF, StackAlign);
3119 MF.
insert(MBBInsertPoint, LoopTestMBB);
3121 MF.
insert(MBBInsertPoint, ExitMBB);
3125 TII->movImm(
MBB,
MBBI,
DL, ScratchReg, ProbeSize, Flags);
3136 TII->get(IsRV64 ? RISCV::SD : RISCV::SW))
3171 MBB.addSuccessor(LoopTestMBB);
3181 SmallVector<MachineInstr *, 4> ToReplace;
3182 for (MachineInstr &
MI :
MBB) {
3183 unsigned Opc =
MI.getOpcode();
3184 if (
Opc == RISCV::PROBED_STACKALLOC ||
3185 Opc == RISCV::PROBED_STACKALLOC_RVV) {
3190 for (MachineInstr *
MI : ToReplace) {
3191 if (
MI->getOpcode() == RISCV::PROBED_STACKALLOC ||
3192 MI->getOpcode() == RISCV::PROBED_STACKALLOC_RVV) {
3195 Register TargetReg =
MI->getOperand(0).getReg();
3201 "No available scratch register for stack probe loop");
3204 (
MI->getOpcode() == RISCV::PROBED_STACKALLOC_RVV));
static void getLiveRegsForEntryMBB(LivePhysRegs &LiveRegs, const MachineBasicBlock &MBB)
const AArch64Subtarget & Subtarget
const TargetLowering & TLI
static MCCFIInstruction createDefCFAExpression(const TargetRegisterInfo &TRI, unsigned Reg, const StackOffset &Offset)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
This file contains constants used for implementing Dwarf debug support.
const HexagonInstrInfo * TII
static void emitSCSPrologue(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, const DebugLoc &DL)
static void emitSCSEpilogue(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, const DebugLoc &DL)
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
static uint64_t estimateFunctionSizeInBytes(const LoongArchInstrInfo *TII, const MachineFunction &MF)
static void emitStackProbeInline(MachineBasicBlock::iterator MBBI, DebugLoc DL, Register TargetReg)
Register const TargetRegisterInfo * TRI
Promote Memory to Register
static constexpr uint64_t QCIInterruptPushAmount
static uint64_t getACLICInterruptPushAmount(const MachineFunction &MF)
static void emitSiFiveCLICStackSwap(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, const DebugLoc &DL, MachineInstr::MIFlag FrameFlag)
static unsigned getPushOpcode(RISCVMachineFunctionInfo::PushPopKind Kind, bool UpdateFP)
static void emitSiFiveCLICPreemptibleSaves(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, const DebugLoc &DL)
static MCRegister getRVVBaseRegister(const RISCVRegisterInfo &TRI, const Register &Reg)
static constexpr MCPhysReg FPReg
static const char * getRestoreLibCallName(const MachineFunction &MF, const std::vector< CalleeSavedInfo > &CSI)
static bool needsDwarfCFI(const MachineFunction &MF)
Returns true if DWARF CFI instructions ("frame moves") should be emitted.
static constexpr MCPhysReg SPReg
static const char * getSpillLibCallName(const MachineFunction &MF, const std::vector< CalleeSavedInfo > &CSI)
static bool hasRVVFrameObject(const MachineFunction &MF)
static constexpr unsigned StackProbeMaxUnprobedStack
static void emitACLICPreemptibleRestores(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, CFIInstBuilder &CFIBuilder, const DebugLoc &DL)
static SmallVector< CalleeSavedInfo, 8 > getUnmanagedInterruptCSI(const MachineFunction &MF, const std::vector< CalleeSavedInfo > &CSI, bool ReverseOrder=false)
static SmallVector< CalleeSavedInfo, 8 > getACLICSavedInfo(const MachineFunction &MF, const std::vector< CalleeSavedInfo > &CSI)
static void appendScalableVectorExpression(const TargetRegisterInfo &TRI, SmallVectorImpl< char > &Expr, StackOffset Offset, llvm::raw_string_ostream &Comment)
static void emitACLICPreemptibleSaves(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, const DebugLoc &DL)
static SmallVector< CalleeSavedInfo, 8 > getQCISavedInfo(const MachineFunction &MF, const std::vector< CalleeSavedInfo > &CSI)
static SmallVector< CalleeSavedInfo, 8 > getRVVCalleeSavedInfo(const MachineFunction &MF, const std::vector< CalleeSavedInfo > &CSI)
static const std::pair< MCPhysReg, int8_t > FixedCSRFIACLICInterruptMap[]
static void createPreemptibleInterruptFrameEntries(MachineFunction &MF, RISCVMachineFunctionInfo &RVFI)
static SmallVector< CalleeSavedInfo, 8 > getUnmanagedCSI(const MachineFunction &MF, const std::vector< CalleeSavedInfo > &CSI, bool ReverseOrder=false)
static void emitSiFiveCLICPreemptibleRestores(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, CFIInstBuilder &CFIBuilder, const DebugLoc &DL)
static bool isPop(unsigned Opcode)
static unsigned getCalleeSavedRVVNumRegs(const Register &BaseReg)
static int getPreemptibleInterruptScratchFrameIndex(const MachineFunction &MF)
static MCCFIInstruction createDefCFAOffset(const TargetRegisterInfo &TRI, Register Reg, StackOffset Offset)
static unsigned getPopOpcode(RISCVMachineFunctionInfo::PushPopKind Kind)
static SmallVector< CalleeSavedInfo, 8 > getPushOrLibCallsSavedInfo(const MachineFunction &MF, const std::vector< CalleeSavedInfo > &CSI)
static int getLibCallID(const MachineFunction &MF, const std::vector< CalleeSavedInfo > &CSI)
static const std::pair< MCPhysReg, int8_t > FixedCSRFIQCIInterruptMap[]
static bool isPush(unsigned Opcode)
static constexpr MCPhysReg RAReg
static MCRegister getLargestFPRegisterOrZero(const RISCVSubtarget &STI, const TargetRegisterInfo &TRI, MCRegister Reg)
static const MCPhysReg FixedCSRFIMap[]
static unsigned getNumPushPopRegs(const std::vector< CalleeSavedInfo > &CSI)
static MCRegister getPhysicalGPR(const TargetRegisterInfo &TRI, MCRegister Reg)
static unsigned getScavSlotsNumForRVV(MachineFunction &MF)
MachineRegisterInfo & MRI
This file declares the machine register scavenger class.
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Represent a constant reference to an array (0 or more elements consecutively in memory),...
bool empty() const
Check if the array is empty.
LLVM_ABI StringRef getValueAsString() const
Return the attribute's value as a string.
bool test(unsigned Idx) const
Returns true if bit Idx is set.
BitVector & reset()
Reset all bits in the bitvector.
BitVector & set()
Set all bits in the bitvector.
iterator_range< const_set_bits_iterator > set_bits() const
Helper class for creating CFI instructions and inserting them into MIR.
void buildEscape(StringRef Bytes, StringRef Comment="") const
void buildDefCFAOffset(int64_t Offset, MCSymbol *Label=nullptr) const
void buildRestore(MCRegister Reg) const
void buildDefCFARegister(MCRegister Reg) const
void buildSameValue(MCRegister Reg) const
void buildOffset(MCRegister Reg, int64_t Offset) const
void insertCFIInst(const MCCFIInstruction &CFIInst) const
void buildDefCFA(MCRegister Reg, int64_t Offset) const
void setInsertPoint(MachineBasicBlock::iterator IP)
The CalleeSavedInfo class tracks the information need to locate where a callee saved register is in t...
MCRegister getReg() const
Diagnostic information for unsupported feature in backend.
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
bool hasOptNone() const
Do not optimize this function (-O0).
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
LLVM_ABI void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
A set of physical registers with utility functions to track liveness when walking backward/forward th...
static MCCFIInstruction createEscape(MCSymbol *L, StringRef Vals, SMLoc Loc={}, StringRef Comment="")
.cfi_escape Allows the user to add arbitrary bytes to the unwind info.
bool contains(MCRegister Reg) const
contains - Return true if the specified register is included in this register class.
Wrapper class representing physical registers and target specific virtual registers.
constexpr unsigned id() const
LLVM_ABI void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *FromMBB)
Transfers all the successors, as in transferSuccessors, and update PHI operands in the successor bloc...
LLVM_ABI MachineBasicBlock * getFallThrough(bool JumpToFallThrough=true)
Return the fallthrough block if the block can implicitly transfer control to the block after it by fa...
LLVM_ABI iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
bool isReturnBlock() const
Convenience function that returns true if the block ends in a return instruction.
LLVM_ABI void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
LLVM_ABI DebugLoc findDebugLoc(instr_iterator MBBI)
Find the next valid DebugLoc starting at MBBI, skipping any debug instructions.
LLVM_ABI void eraseFromParent()
This method unlinks 'this' from the containing function and deletes it.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
MachineInstrBundleIterator< MachineInstr > iterator
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
bool hasVarSizedObjects() const
This method may be called any time after instruction selection is complete to determine if the stack ...
uint64_t getStackSize() const
Return the number of bytes that must be allocated to hold all of the fixed size frame objects.
bool adjustsStack() const
Return true if this function adjusts the stack – e.g., when calling another function.
LLVM_ABI int CreateStackObject(uint64_t Size, Align Alignment, bool isSpillSlot, const AllocaInst *Alloca=nullptr, uint8_t ID=0)
Create a new statically sized stack object, returning a nonnegative identifier to represent it.
LLVM_ABI void ensureMaxAlignment(Align Alignment)
Make sure the function's frame is at least Align bytes aligned.
bool isFrameAddressTaken() const
This method may be called any time after instruction selection is complete to determine if there is a...
Align getMaxAlign() const
Return alignment of this function's frame.
void setObjectOffset(int ObjectIdx, int64_t SPOffset)
Set the stack frame offset of the specified object.
uint64_t getMaxCallFrameSize() const
Return the maximum size of a call frame that must be allocated for an outgoing function call.
int64_t getOffsetAdjustment() const
Return the correction for frame offsets.
LLVM_ABI uint64_t estimateStackSize(const MachineFunction &MF) const
Estimate and return the size of the stack frame.
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
int64_t getObjectSize(int ObjectIdx) const
Return the size of the specified object.
bool isMaxCallFrameSizeComputed() const
LLVM_ABI int CreateSpillStackObject(uint64_t Size, Align Alignment, TargetStackID::Value StackID=TargetStackID::Default)
Create a new statically sized stack object that represents a spill slot, returning a nonnegative iden...
const std::vector< CalleeSavedInfo > & getCalleeSavedInfo() const
Returns a reference to call saved info vector for the current function.
int getObjectIndexEnd() const
Return one past the maximum frame object index.
uint8_t getStackID(int ObjectIdx) const
int64_t getObjectOffset(int ObjectIdx) const
Return the assigned stack offset of the specified object from the incoming stack pointer.
void setStackSize(uint64_t Size)
Set the size of the stack.
bool isFixedObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a fixed stack object.
bool isDeadObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a dead object.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
bool needsFrameMoves() const
True if this function needs frame moves for debug or exceptions.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
BasicBlockListType::iterator iterator
bool disableFramePointerElim() const
Returns true if frame pointer elimination should be disabled for this function.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
const MachineBasicBlock & front() const
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *BB=nullptr, std::optional< UniqueBBID > BBID=std::nullopt)
CreateMachineInstr - Allocate a new MachineInstr.
void insert(iterator MBBI, MachineBasicBlock *MBB)
const MachineInstrBuilder & addExternalSymbol(const char *FnName, unsigned TargetFlags=0) const
const MachineInstrBuilder & addUse(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register use operand.
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & setMIFlag(MachineInstr::MIFlag Flag) const
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
const MachineInstrBuilder & addDef(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register definition operand.
const MachineInstrBuilder & setMIFlags(unsigned Flags) const
MachineInstr * getInstr() const
If conversion operators fail, use this method to get the MachineInstr explicitly.
Representation of each machine instruction.
LLVM_ABI void copyImplicitOps(MachineFunction &MF, const MachineInstr &MI)
Copy implicit register operands from specified instruction to this instruction.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
bool isReserved(MCRegister PhysReg) const
isReserved - Returns true when PhysReg is a reserved register.
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
LLVM_ABI const MCPhysReg * getCalleeSavedRegs() const
Returns list of callee saved registers.
Represent a mutable reference to an array (0 or more elements consecutively in memory),...
bool assignCalleeSavedSpillSlots(MachineFunction &MF, const TargetRegisterInfo *TRI, std::vector< CalleeSavedInfo > &CSI) const override
assignCalleeSavedSpillSlots - Allows target to override spill slot assignment logic.
void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override
emitProlog/emitEpilog - These methods insert prolog and epilog code into the function.
void resetCFIToInitialState(MachineBasicBlock &MBB) const override
Emit CFI instructions that recreate the state of the unwind information upon function entry.
uint64_t getFirstSPAdjustAmount(const MachineFunction &MF) const
bool enableShrinkWrapping(const MachineFunction &MF) const override
Returns true if the target will correctly handle shrink wrapping.
uint64_t getStackThreshold() const override
getStackThreshold - Return the maximum stack size
bool spillCalleeSavedRegisters(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, ArrayRef< CalleeSavedInfo > CSI, const TargetRegisterInfo *TRI) const override
spillCalleeSavedRegisters - Issues instruction(s) to spill all callee saved registers and returns tru...
bool hasBP(const MachineFunction &MF) const
bool canUseAsEpilogue(const MachineBasicBlock &MBB) const override
Check whether or not the given MBB can be used as a epilogue for the target.
bool hasFPImpl(const MachineFunction &MF) const override
Register findScratchNonCalleeSaveRegister(MachineBasicBlock *MBB, Register PreferredReg, Register DontUseReg=Register()) const
bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, MutableArrayRef< CalleeSavedInfo > CSI, const TargetRegisterInfo *TRI) const override
restoreCalleeSavedRegisters - Issues instruction(s) to restore all callee saved registers and returns...
bool hasReservedCallFrame(const MachineFunction &MF) const override
hasReservedCallFrame - Under normal circumstances, when a frame pointer is not required,...
Register getInitialCFARegister(const MachineFunction &MF) const override
Return initial CFA register value i.e.
const RISCVSubtarget & STI
StackOffset getFrameIndexReference(const MachineFunction &MF, int FI, Register &FrameReg) const override
getFrameIndexReference - This method should return the base register and offset used to reference a f...
bool isSupportedStackID(TargetStackID::Value ID) const override
void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS) const override
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override
TargetStackID::Value getStackIDForScalableVectors() const override
Returns the StackID that scalable vectors should be associated with.
void allocateStack(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, MachineFunction &MF, uint64_t Offset, uint64_t RealStackSize, bool EmitCFI, bool NeedProbe, uint64_t ProbeSize, uint64_t FollowupSize, bool SpillsToSP, MachineInstr::MIFlag Flag) const
int getInitialCFAOffset(const MachineFunction &MF) const override
Return initial CFA offset value i.e.
void processFunctionBeforeFrameFinalized(MachineFunction &MF, RegScavenger *RS) const override
processFunctionBeforeFrameFinalized - This method is called immediately before the specified function...
MachineBasicBlock::iterator eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const override
This method is called during prolog/epilog code insertion to eliminate call frame setup and destroy p...
bool canUseAsPrologue(const MachineBasicBlock &MBB) const override
Check whether or not the given MBB can be used as a prologue for the target.
RISCVFrameLowering(const RISCVSubtarget &STI)
uint64_t getStackSizeWithRVVPadding(const MachineFunction &MF) const
RISCVMachineFunctionInfo - This class is derived from MachineFunctionInfo and contains private RISCV-...
bool isPushable(const MachineFunction &MF) const
bool isACLICPushPopInterrupt(const MachineFunction &MF) const
InterruptStackKind getInterruptStackKind(const MachineFunction &MF) const
bool useSiFiveInterrupt(const MachineFunction &MF) const
bool isSiFivePreemptibleInterrupt(const MachineFunction &MF) const
void pushInterruptCSRFrameIndex(int FI)
PushPopKind getPushPopKind(const MachineFunction &MF) const
bool hasShadowStack(const MachineFunction &MF) const
uint64_t getRVVPadding() const
bool isACLICPreemptibleInterrupt(const MachineFunction &MF) const
unsigned getRVPushRegs() const
bool useSaveRestoreLibCalls(const MachineFunction &MF) const
bool isACLICSupervisorInterrupt(const MachineFunction &MF) const
unsigned getVarArgsSaveSize() const
bool useQCIInterrupt(const MachineFunction &MF) const
unsigned getCalleeSavedStackSize() const
bool hasCFProtectionBranch() const
TargetRegisterClass const * getLargestFPRegClass() const
bool hasVInstructions() const
const RISCVRegisterInfo * getRegisterInfo() const override
const RISCVInstrInfo * getInstrInfo() const override
Wrapper class representing virtual and physical registers.
constexpr bool isValid() const
Represents a location in source code.
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
bool contains(const T &V) const
Check if the SmallSet contains the given element.
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
void append(StringRef RHS)
Append from a StringRef.
StringRef str() const
Explicit conversion to StringRef.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
LLVM_NO_SANITIZE_CFI_UNRELATED_CAST iterator begin()
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StackOffset holds a fixed and a scalable offset in bytes.
int64_t getFixed() const
Returns the fixed component of the stack.
int64_t getScalable() const
Returns the scalable component of the stack.
static StackOffset get(int64_t Fixed, int64_t Scalable)
static StackOffset getScalable(int64_t Scalable)
static StackOffset getFixed(int64_t Fixed)
Represent a constant reference to a string, i.e.
unsigned getStackAlignment() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
bool hasFP(const MachineFunction &MF) const
hasFP - Return true if the specified function should have a dedicated frame pointer register.
virtual void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS=nullptr) const
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
int getOffsetOfLocalArea() const
getOffsetOfLocalArea - This method returns the offset of the local area from the stack pointer on ent...
TargetFrameLowering(StackDirection D, Align StackAl, int LAO, Align TransAl=Align(1), bool StackReal=true)
Align getStackAlign() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
int alignSPAdjust(int SPAdj) const
alignSPAdjust - This method aligns the stack adjustment to the correct alignment.
TargetInstrInfo - Interface to description of machine instruction set.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
bool hasStackRealignment(const MachineFunction &MF) const
True if stack realignment is required and still possible.
virtual const TargetInstrInfo * getInstrInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
self_iterator getIterator()
A raw_ostream that writes to an std::string.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
@ GHC
Used by the Glasgow Haskell Compiler (GHC).
static VLMUL encodeLMUL(unsigned LMUL, bool Fractional)
LLVM_ABI unsigned encodeVTYPE(VLMUL VLMUL, unsigned SEW, bool TailAgnostic, bool MaskAgnostic, bool AltFmt=false)
static unsigned encodeRegListNumRegs(unsigned NumRegs)
static constexpr unsigned RVVBitsPerBlock
bool isRVVSpill(const MachineInstr &MI)
static constexpr unsigned RVVBytesPerBlock
BaseReg
Stack frame base register. Bit 0 of FREInfo.Info.
This is an optimization pass for GlobalISel generic memory operations.
detail::zippy< detail::zip_shortest, T, U, Args... > zip(T &&t, U &&u, Args &&...args)
zip iterator for two or more iteratable types.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
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.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
@ Implicit
Not emitted register (e.g. carry, or temporary result).
@ Kill
The last use of a register.
constexpr T alignDown(U Value, V Align, W Skew=0)
Returns the largest unsigned integer less than or equal to Value and is Skew mod Align.
LLVM_ABI void reportFatalInternalError(Error Err)
Report a fatal error that indicates a bug in LLVM.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
auto make_first_range(ContainerTy &&c)
Given a container of pairs, return a range over the first elements.
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
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...
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
auto count_if(R &&Range, UnaryPredicate P)
Wrapper function around std::count_if to count the number of times an element satisfying a given pred...
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
void appendLEB128(SmallVectorImpl< U > &Buffer, T Value)
unsigned Log2(Align A)
Returns the log2 of the alignment.
void fullyRecomputeLiveIns(ArrayRef< MachineBasicBlock * > MBBs)
Convenience function for recomputing live-in's for a set of MBBs until the computation converges.
LLVM_ABI Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
MCRegisterClass TargetRegisterClass
This struct is a compact representation of a valid (non-zero power of two) alignment.
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
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