46#include "llvm/Config/llvm-config.h"
61#define DEBUG_TYPE "regalloc"
63STATISTIC(NumSpilledRanges,
"Number of spilled live ranges");
64STATISTIC(NumSnippets,
"Number of spilled snippets");
66STATISTIC(NumSpillsRemoved,
"Number of spills removed");
68STATISTIC(NumReloadsRemoved,
"Number of reloads removed");
69STATISTIC(NumFolded,
"Number of folded stack accesses");
71STATISTIC(NumRemats,
"Number of rematerialized defs for spilling");
76 cl::desc(
"Restrict remat for statepoint operands"));
102 using MergeableSpillsMap =
104 MergeableSpillsMap MergeableSpills;
114 void rmRedundantSpills(
134 : MF(mf), LIS(Analyses.LIS), LSS(Analyses.LSS), MDT(Analyses.MDT),
135 VRM(vrm), MRI(mf.getRegInfo()),
TII(*mf.getSubtarget().getInstrInfo()),
136 TRI(*mf.getSubtarget().getRegisterInfo()), MBFI(Analyses.MBFI),
137 Matrix(matrix), IPA(LIS, mf.getNumBlockIDs()) {}
139 void addToMergeableSpills(MachineInstr &Spill,
int StackSlot,
141 bool rmFromMergeableSpills(MachineInstr &Spill,
int StackSlot);
142 void hoistAllSpills();
143 bool LRE_CanEraseVirtReg(
Register)
override;
147class InlineSpiller :
public Spiller {
152 MachineRegisterInfo &MRI;
153 const TargetInstrInfo &TII;
154 const TargetRegisterInfo &TRI;
155 LiveRegMatrix *Matrix =
nullptr;
158 LiveRangeEdit *Edit =
nullptr;
159 LiveInterval *StackInt =
nullptr;
162 AllocationOrder *Order =
nullptr;
174 SmallPtrSet<MachineInstr*, 8> SnippetCopies;
177 SmallPtrSet<VNInfo*, 8> UsedValues;
180 SmallVector<MachineInstr*, 8> DeadDefs;
183 HoistSpillHelper HSpiller;
186 VirtRegAuxInfo &VRAI;
188 ~InlineSpiller()
override =
default;
191 InlineSpiller(
const Spiller::RequiredAnalyses &Analyses, MachineFunction &MF,
192 VirtRegMap &VRM, VirtRegAuxInfo &VRAI, LiveRegMatrix *Matrix)
193 : MF(MF), LIS(Analyses.LIS), LSS(Analyses.LSS), VRM(VRM),
194 MRI(MF.getRegInfo()), TII(*MF.getSubtarget().getInstrInfo()),
195 TRI(*MF.getSubtarget().getRegisterInfo()), Matrix(Matrix),
196 HSpiller(Analyses, MF, VRM, Matrix), VRAI(VRAI) {}
198 void spill(LiveRangeEdit &, AllocationOrder *Order =
nullptr)
override;
201 void postOptimization()
override;
204 bool isSnippet(
const LiveInterval &SnipLI);
205 void collectRegsToSpill();
210 bool hoistSpillInsideBB(LiveInterval &SpillLI, MachineInstr &CopyMI);
211 void eliminateRedundantSpills(LiveInterval &LI, VNInfo *VNI);
213 void markValueUsed(LiveInterval*, VNInfo*);
214 bool canGuaranteeAssignmentAfterRemat(
Register VReg, MachineInstr &
MI);
215 bool hasPhysRegAvailable(
const MachineInstr &
MI);
216 bool reMaterializeFor(LiveInterval &, MachineInstr &
MI);
217 void reMaterializeAll();
220 bool foldMemoryOperand(
ArrayRef<std::pair<MachineInstr *, unsigned>>,
221 MachineInstr *LoadMI =
nullptr);
233void Spiller::anchor() {}
239 return new InlineSpiller(Analyses, MF, VRM, VRAI,
Matrix);
258 if (!
TII.isCopyInstr(
MI))
281 "expected to see first instruction in bundle");
285 while (
I->isBundledWithSucc()) {
287 auto CopyInst =
TII.isCopyInstr(
MI);
313 if (MO.getReg().isVirtual())
320bool InlineSpiller::isSnippet(
const LiveInterval &SnipLI) {
334 if (!LIS.intervalIsInOneMBB(SnipLI))
340 for (
auto *VNI : SnipLI.
vnis()) {
342 if (
MI->getOpcode() == TargetOpcode::STATEPOINT)
352 RI = MRI.reg_bundle_nodbg_begin(SnipLI.
reg()),
353 E = MRI.reg_bundle_nodbg_end();
383void InlineSpiller::collectRegsToSpill() {
387 RegsToSpill.assign(1,
Reg);
388 SnippetCopies.clear();
389 RegsReplaced.clear();
398 if (!isSibling(SnipReg))
401 if (!isSnippet(SnipLI))
403 SnippetCopies.insert(&
MI);
404 if (isRegToSpill(SnipReg))
406 RegsToSpill.push_back(SnipReg);
435bool InlineSpiller::hoistSpillInsideBB(
LiveInterval &SpillLI,
437 SlotIndex Idx = LIS.getInstructionIndex(CopyMI);
454 assert(StackInt &&
"No stack slot assigned yet.");
457 StackInt->MergeValueInAsValue(OrigLI, OrigVNI, StackInt->getValNumInfo(0));
459 << *StackInt <<
'\n');
463 eliminateRedundantSpills(SrcLI, SrcVNI);
471 assert(
DefMI &&
"Defining instruction disappeared");
478 MRI.getRegClass(SrcReg),
Register());
479 LIS.InsertMachineInstrRangeInMaps(MIS.begin(), MII);
488 if (MIS.begin() == MII)
489 HSpiller.addToMergeableSpills(*MII, StackSlot, Original);
497 assert(VNI &&
"Missing value");
499 WorkList.
push_back(std::make_pair(&SLI, VNI));
500 assert(StackInt &&
"No stack slot assigned yet.");
507 << VNI->
def <<
" in " << *LI <<
'\n');
510 if (isRegToSpill(
Reg))
514 StackInt->MergeValueInAsValue(*LI, VNI, StackInt->getValNumInfo(0));
515 LLVM_DEBUG(
dbgs() <<
"Merged to stack int: " << *StackInt <<
'\n');
520 if (!
MI.mayStore() && !
TII.isCopyInstr(
MI))
528 if (isSibling(DstReg)) {
531 assert(DstVNI &&
"Missing defined value");
534 WorkList.
push_back(std::make_pair(&DstLI, DstVNI));
544 MI.setDesc(
TII.get(TargetOpcode::KILL));
545 DeadDefs.push_back(&
MI);
547 if (HSpiller.rmFromMergeableSpills(
MI, StackSlot))
551 }
while (!WorkList.
empty());
562 WorkList.
push_back(std::make_pair(LI, VNI));
565 if (!UsedValues.insert(VNI).second)
573 WorkList.
push_back(std::make_pair(LI, PVNI));
580 if (!SnippetCopies.count(
MI))
582 LiveInterval &SnipLI = LIS.getInterval(
MI->getOperand(1).getReg());
583 assert(isRegToSpill(SnipLI.
reg()) &&
"Unexpected register in copy");
585 assert(SnipVNI &&
"Snippet undefined before copy");
586 WorkList.
push_back(std::make_pair(&SnipLI, SnipVNI));
587 }
while (!WorkList.
empty());
590bool InlineSpiller::canGuaranteeAssignmentAfterRemat(
Register VReg,
609 if (
MI.getOpcode() != TargetOpcode::STATEPOINT)
615 EndIdx =
MI.getNumOperands();
616 Idx < EndIdx; ++Idx) {
634 if (!
Matrix->checkInterference(PrevIdx, UseIdx, PhysReg))
668 if (SnippetCopies.count(&
MI)) {
669 LLVM_DEBUG(
dbgs() <<
"\tskipping remat snippet copy for " << UseIdx <<
'\t'
676 assert(OrigVNI &&
"corrupted sub-interval");
683 markValueUsed(&VirtReg, ParentVNI);
684 LLVM_DEBUG(
dbgs() <<
"\tcannot remat missing def for " << UseIdx <<
'\t'
691 if (!Edit->canRematerializeAt(RM, UseIdx)) {
692 markValueUsed(&VirtReg, ParentVNI);
700 markValueUsed(&VirtReg, ParentVNI);
707 if (
RM.OrigMI->canFoldAsLoad() &&
708 (
RM.OrigMI->mayLoad() || !hasPhysRegAvailable(
MI)) &&
709 foldMemoryOperand(
Ops,
RM.OrigMI)) {
710 Edit->markRematerialized(
RM.ParentVNI);
717 if (!canGuaranteeAssignmentAfterRemat(VirtReg.
reg(),
MI)) {
718 markValueUsed(&VirtReg, ParentVNI);
724 Register NewVReg = Edit->createFrom(Original);
727 MRI.constrainRegClass(NewVReg, MRI.getRegClass(VirtReg.
reg()));
734 if (SR.liveAt(UseIdx))
735 UsedLanes |= SR.LaneMask;
739 SlotIndex DefIdx = Edit->rematerializeAt(*
MI.getParent(),
MI, NewVReg, RM,
740 TRI,
false, 0,
nullptr, UsedLanes);
744 auto *NewMI = LIS.getInstructionFromIndex(DefIdx);
745 NewMI->setDebugLoc(
MI.getDebugLoc());
749 << *LIS.getInstructionFromIndex(DefIdx));
752 for (
const auto &OpPair :
Ops) {
767void InlineSpiller::reMaterializeAll() {
771 bool anyRemat =
false;
776 if (
MI.isDebugValue())
779 assert(!
MI.isDebugInstr() &&
"Did not expect to find a use in debug "
780 "instruction that isn't a DBG_VALUE");
782 anyRemat |= reMaterializeFor(LI,
MI);
796 if (!
MI->allDefsAreDead())
799 DeadDefs.push_back(
MI);
803 if (
MI->isBundledWithSucc() && !
MI->isBundledWithPred()) {
805 EndIt =
MI->getParent()->instr_end();
808 bool OnlyDeadCopies =
true;
810 It != EndIt && It->isBundledWithPred(); ++It) {
812 auto DestSrc =
TII.isCopyInstr(*It);
813 bool IsCopyToDeadReg =
814 DestSrc && DestSrc->Destination->getReg() ==
Reg;
815 if (!IsCopyToDeadReg) {
816 OnlyDeadCopies =
false;
820 if (OnlyDeadCopies) {
822 It != EndIt && It->isBundledWithPred(); ++It) {
823 It->addRegisterDead(
Reg, &
TRI);
825 DeadDefs.push_back(&*It);
834 if (DeadDefs.empty())
836 LLVM_DEBUG(
dbgs() <<
"Remat created " << DeadDefs.size() <<
" dead defs.\n");
837 Edit->eliminateDeadDefs(DeadDefs, RegsToSpill);
845 unsigned ResultPos = 0;
847 if (MRI.reg_nodbg_empty(
Reg)) {
848 Edit->eraseVirtReg(
Reg);
849 RegsReplaced.push_back(
Reg);
854 (!LIS.getInterval(
Reg).empty() || !MRI.reg_nodbg_empty(
Reg)) &&
855 "Empty and not used live-range?!");
857 RegsToSpill[ResultPos++] =
Reg;
859 RegsToSpill.erase(RegsToSpill.begin() + ResultPos, RegsToSpill.end());
861 <<
" registers to spill after remat.\n");
872 bool IsLoad = InstrReg.
isValid();
877 if (InstrReg !=
Reg || FI != StackSlot)
881 HSpiller.rmFromMergeableSpills(*
MI, StackSlot);
884 LIS.RemoveMachineInstrFromMaps(*
MI);
885 MI->eraseFromParent();
898#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
904 const char *
const header,
906 char NextLine =
'\n';
907 char SlotIndent =
'\t';
909 if (std::next(
B) ==
E) {
914 dbgs() <<
'\t' << header <<
": " << NextLine;
928 dbgs() << SlotIndent << Idx <<
'\t' << *
I;
940foldMemoryOperand(
ArrayRef<std::pair<MachineInstr *, unsigned>>
Ops,
946 if (
Ops.back().first !=
MI ||
MI->isBundled())
949 bool WasCopy =
TII.isCopyInstr(*MI).has_value();
958 bool UntieRegs =
MI->getOpcode() == TargetOpcode::STATEPOINT;
962 bool SpillSubRegs =
TII.isSubregFoldable() ||
963 MI->getOpcode() == TargetOpcode::STATEPOINT ||
964 MI->getOpcode() == TargetOpcode::PATCHPOINT ||
965 MI->getOpcode() == TargetOpcode::STACKMAP;
970 for (
const auto &OpPair :
Ops) {
971 unsigned Idx = OpPair.second;
972 assert(
MI == OpPair.first &&
"Instruction conflict during operand folding");
989 if (LoadMI && MO.
isDef())
992 if (UntieRegs || !
MI->isRegTiedToDefOperand(Idx))
1005 for (
unsigned Idx : FoldOps) {
1009 unsigned Tied =
MI->findTiedOperandIdx(Idx);
1016 MI->untieRegOperand(Idx);
1020 LoadMI ?
TII.foldMemoryOperand(*
MI, FoldOps, *LoadMI, &LIS)
1021 :
TII.foldMemoryOperand(*
MI, FoldOps, StackSlot, &LIS, &VRM);
1024 for (
auto Tied : TiedOps)
1025 MI->tieOperands(Tied.first, Tied.second);
1051 HSpiller.rmFromMergeableSpills(*
MI, FI))
1055 if (
MI->isCandidateForAdditionalCallInfo())
1056 MI->getMF()->moveAdditionalCallInfo(
MI, FoldMI);
1063 if (
MI->peekDebugInstrNum() &&
Ops[0].second == 0) {
1065 auto MakeSubstitution = [
this,FoldMI,
MI,&
Ops]() {
1067 unsigned OldOperandNum =
Ops[0].second;
1069 unsigned OldNum =
MI->getDebugInstrNum();
1070 MF.makeDebugValueSubstitution({OldNum, OldOperandNum},
1075 if (
Ops.size() == 1 && Op0.
isDef()) {
1077 }
else if (
Ops.size() == 2 && Op0.
isDef() &&
MI->getOperand(1).isTied() &&
1078 Op0.
getReg() ==
MI->getOperand(1).getReg()) {
1081 }
else if (
MI->peekDebugInstrNum()) {
1087 MF.substituteDebugValuesForInst(*
MI, *FoldMI,
Ops[0].second);
1090 MI->eraseFromParent();
1093 assert(!MIS.empty() &&
"Unexpected empty span of instructions!");
1105 if (MO.
getReg() == ImpReg)
1114 else if (
Ops.front().second == 0) {
1119 if (std::distance(MIS.begin(), MIS.end()) <= 1)
1120 HSpiller.addToMergeableSpills(*FoldMI, StackSlot, Original);
1126void InlineSpiller::insertReload(
Register NewVReg,
1133 MRI.getRegClass(NewVReg),
Register());
1146 if (!Def.isImplicitDef())
1152 return Def.getOperand(0).getSubReg();
1156void InlineSpiller::insertSpill(
Register NewVReg,
bool isKill,
1160 assert(!
MI->isTerminator() &&
"Inserting a spill after a terminator");
1169 MRI.getRegClass(NewVReg),
Register());
1175 BuildMI(
MBB, SpillBefore,
MI->getDebugLoc(),
TII.get(TargetOpcode::KILL))
1189 if (IsRealSpill && std::distance(Spill, MIS.end()) <= 1)
1190 HSpiller.addToMergeableSpills(*Spill, StackSlot, Original);
1194void InlineSpiller::spillAroundUses(
Register Reg) {
1201 if (
MI.isDebugValue()) {
1210 assert(!
MI.isDebugInstr() &&
"Did not expect to find a use in debug "
1211 "instruction that isn't a DBG_VALUE");
1214 if (SnippetCopies.count(&
MI))
1218 if (coalesceStackAccess(&
MI,
Reg))
1234 if (SibReg && isSibling(SibReg)) {
1236 if (isRegToSpill(SibReg)) {
1238 SnippetCopies.insert(&
MI);
1242 if (hoistSpillInsideBB(OldLI,
MI)) {
1244 MI.getOperand(0).setIsDead();
1245 DeadDefs.push_back(&
MI);
1251 eliminateRedundantSpills(SibLI, SibLI.
getVNInfoAt(Idx));
1257 if (foldMemoryOperand(
Ops))
1265 insertReload(NewVReg, Idx, &
MI);
1268 bool hasLiveDef =
false;
1269 for (
const auto &OpPair :
Ops) {
1273 if (!OpPair.first->isRegTiedToDefOperand(OpPair.second))
1285 insertSpill(NewVReg,
true, &
MI);
1290void InlineSpiller::spillAll() {
1293 StackSlot = VRM.assignVirt2StackSlot(Original);
1294 StackInt = &LSS.getOrCreateInterval(StackSlot, MRI.getRegClass(Original));
1295 StackInt->getNextValue(
SlotIndex(), LSS.getVNInfoAllocator());
1297 StackInt = &LSS.getInterval(StackSlot);
1299 if (Original != Edit->getReg())
1300 VRM.assignVirt2StackSlot(Edit->getReg(), StackSlot);
1302 assert(StackInt->getNumValNums() == 1 &&
"Bad stack interval values");
1306 LLVM_DEBUG(
dbgs() <<
"Merged spilled regs: " << *StackInt <<
'\n');
1310 spillAroundUses(
Reg);
1314 VRM.assignVirt2StackSlot(
Reg, StackSlot);
1318 if (!DeadDefs.empty()) {
1319 LLVM_DEBUG(
dbgs() <<
"Eliminating " << DeadDefs.size() <<
" dead defs\n");
1320 Edit->eliminateDeadDefs(DeadDefs, RegsToSpill);
1327 assert(SnippetCopies.count(&
MI) &&
"Remaining use wasn't a snippet copy");
1330 MI.eraseFromBundle();
1336 Edit->eraseVirtReg(
Reg);
1345 Original = VRM.getOriginal(edit.
getReg());
1346 StackSlot = VRM.getStackSlot(Original);
1350 <<
TRI.getRegClassName(MRI.getRegClass(edit.
getReg()))
1351 <<
':' << edit.
getParent() <<
"\nFrom original "
1354 "Attempting to spill already spilled value.");
1355 assert(DeadDefs.empty() &&
"Previous spill didn't remove dead defs");
1357 collectRegsToSpill();
1361 if (!RegsToSpill.empty())
1364 Edit->calculateRegClassAndHint(MF, VRAI);
1368void InlineSpiller::postOptimization() { HSpiller.hoistAllSpills(); }
1371void HoistSpillHelper::addToMergeableSpills(
MachineInstr &Spill,
int StackSlot,
1378 auto [Place,
Inserted] = StackSlotToOrigLI.try_emplace(StackSlot);
1380 auto LI = std::make_unique<LiveInterval>(OrigLI.
reg(), OrigLI.
weight());
1382 Place->second = std::move(LI);
1387 std::pair<int, VNInfo *> MIdx = std::make_pair(StackSlot, OrigVNI);
1388 MergeableSpills[MIdx].insert(&Spill);
1393bool HoistSpillHelper::rmFromMergeableSpills(
MachineInstr &Spill,
1395 auto It = StackSlotToOrigLI.find(StackSlot);
1396 if (It == StackSlotToOrigLI.end())
1400 std::pair<int, VNInfo *> MIdx = std::make_pair(StackSlot, OrigVNI);
1401 return MergeableSpills[MIdx].erase(&Spill);
1408 SlotIndex Idx = IPA.getLastInsertPoint(OrigLI, BB);
1411 if (Idx < OrigVNI.
def) {
1414 LLVM_DEBUG(
dbgs() <<
"can't spill in root block - def after LIP\n");
1421 for (
const Register &SibReg : Siblings) {
1429 return SR.getVNInfoAt(Idx) != nullptr;
1440void HoistSpillHelper::rmRedundantSpills(
1447 for (
auto *
const CurrentSpill : Spills) {
1454 MachineInstr *SpillToRm = (CIdx > PIdx) ? CurrentSpill : PrevSpill;
1455 MachineInstr *SpillToKeep = (CIdx > PIdx) ? PrevSpill : CurrentSpill;
1457 SpillBBToSpill[MDT.getNode(
Block)] = SpillToKeep;
1459 SpillBBToSpill[MDT.getNode(
Block)] = CurrentSpill;
1462 for (
auto *
const SpillToRm : SpillsToRm)
1463 Spills.erase(SpillToRm);
1472void HoistSpillHelper::getVisitOrders(
1496 for (
auto *
const Spill : Spills) {
1500 while (Node != RootIDomNode) {
1503 if (Node != MDT[
Block] && SpillBBToSpill[Node]) {
1504 SpillToRm = SpillBBToSpill[MDT[
Block]];
1509 }
else if (WorkSet.
count(Node)) {
1512 NodesOnPath.
insert(Node);
1527 NodesOnPath.
clear();
1537 if (WorkSet.
count(Child))
1540 }
while (idx != Orders.
size());
1542 "Orders have different size with WorkSet");
1547 for (; RIt != Orders.
rend(); RIt++)
1548 LLVM_DEBUG(
dbgs() <<
"BB" << (*RIt)->getBlock()->getNumber() <<
",");
1556void HoistSpillHelper::runHoistSpills(
1572 rmRedundantSpills(Spills, SpillsToRm, SpillBBToSpill);
1575 getVisitOrders(Root, Spills, Orders, SpillsToRm, SpillsToKeep,
1582 using NodesCostPair =
1583 std::pair<SmallPtrSet<MachineDomTreeNode *, 16>,
BlockFrequency>;
1591 for (; RIt != Orders.
rend(); RIt++) {
1595 if (
auto It = SpillsToKeep.
find(*RIt);
1596 It != SpillsToKeep.
end() && !It->second) {
1597 auto &SIt = SpillsInSubTreeMap[*RIt];
1600 SIt.second = MBFI.getBlockFreq(
Block);
1607 if (!SpillsInSubTreeMap.
contains(Child))
1615 auto &[SpillsInSubTree, SubTreeCost] = SpillsInSubTreeMap[*RIt];
1616 auto ChildIt = SpillsInSubTreeMap.
find(Child);
1617 SubTreeCost += ChildIt->second.second;
1618 auto BI = ChildIt->second.first.begin();
1619 auto EI = ChildIt->second.first.end();
1620 SpillsInSubTree.insert(BI, EI);
1621 SpillsInSubTreeMap.
erase(ChildIt);
1624 auto &[SpillsInSubTree, SubTreeCost] = SpillsInSubTreeMap[*RIt];
1626 if (SpillsInSubTree.empty())
1631 if (!isSpillCandBB(OrigLI, OrigVNI, *
Block, LiveReg))
1639 if (SubTreeCost > MBFI.getBlockFreq(
Block) * MarginProb) {
1641 for (auto *const SpillBB : SpillsInSubTree) {
1644 if (auto It = SpillsToKeep.find(SpillBB);
1645 It != SpillsToKeep.end() && !It->second) {
1646 MachineInstr *SpillToRm = SpillBBToSpill[SpillBB];
1647 SpillsToRm.push_back(SpillToRm);
1650 SpillsToKeep.erase(SpillBB);
1654 SpillsToKeep[*RIt] = LiveReg;
1656 dbgs() <<
"spills in BB: ";
1657 for (
const auto Rspill : SpillsInSubTree)
1658 dbgs() << Rspill->getBlock()->getNumber() <<
" ";
1659 dbgs() <<
"were promoted to BB" << (*RIt)->getBlock()->getNumber()
1662 SpillsInSubTree.clear();
1663 SpillsInSubTree.insert(*RIt);
1664 SubTreeCost = MBFI.getBlockFreq(
Block);
1669 for (
const auto &Ent : SpillsToKeep) {
1671 SpillsToIns[Ent.first->getBlock()] = Ent.second;
1691void HoistSpillHelper::hoistAllSpills() {
1695 for (
unsigned i = 0, e = MRI.getNumVirtRegs(); i != e; ++i) {
1698 if (!MRI.def_empty(
Reg) && Original.
isValid())
1699 Virt2SiblingsMap[Original].insert(
Reg);
1703 for (
auto &Ent : MergeableSpills) {
1704 int Slot = Ent.first.first;
1706 VNInfo *OrigVNI = Ent.first.second;
1708 if (Ent.second.empty())
1712 dbgs() <<
"\nFor Slot" <<
Slot <<
" and VN" << OrigVNI->
id <<
":\n"
1713 <<
"Equal spills in BB: ";
1714 for (
const auto spill : EqValSpills)
1715 dbgs() << spill->getParent()->getNumber() <<
" ";
1724 runHoistSpills(OrigLI, *OrigVNI, EqValSpills, SpillsToRm, SpillsToIns);
1727 dbgs() <<
"Finally inserted spills in BB: ";
1728 for (
const auto &Ispill : SpillsToIns)
1729 dbgs() << Ispill.first->getNumber() <<
" ";
1730 dbgs() <<
"\nFinally removed spills in BB: ";
1731 for (
const auto Rspill : SpillsToRm)
1732 dbgs() << Rspill->getParent()->getNumber() <<
" ";
1738 if (!SpillsToIns.empty() || !SpillsToRm.empty())
1740 StackIntvl.getValNumInfo(0));
1743 for (
auto const &Insert : SpillsToIns) {
1749 MRI.getRegClass(LiveReg),
Register());
1757 NumSpills -= SpillsToRm.size();
1758 for (
auto *
const RMEnt : SpillsToRm) {
1759 RMEnt->setDesc(
TII.get(TargetOpcode::KILL));
1760 for (
unsigned i = RMEnt->getNumOperands(); i; --i) {
1763 RMEnt->removeOperand(i - 1);
1766 Edit.eliminateDeadDefs(SpillsToRm, {});
1773bool HoistSpillHelper::LRE_CanEraseVirtReg(
Register VirtReg) {
1774 if (
Matrix && VRM.hasPhys(VirtReg)) {
1776 Matrix->unassign(LI,
true);
1784 if (VRM.hasPhys(Old))
1785 VRM.assignVirt2Phys(New, VRM.getPhys(Old));
1787 VRM.assignVirt2StackSlot(New, VRM.getStackSlot(Old));
1790 if (VRM.hasShape(Old))
1791 VRM.assignVirt2Shape(New, VRM.getShape(Old));
MachineInstrBuilder & UseMI
MachineInstrBuilder MachineInstrBuilder & DefMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
This file defines the DenseMap class.
const HexagonInstrInfo * TII
static LLVM_DUMP_METHOD void dumpMachineInstrRangeWithSlotIndex(MachineBasicBlock::iterator B, MachineBasicBlock::iterator E, LiveIntervals const &LIS, const char *const header, Register VReg=Register())
static Register isCopyOfBundle(const MachineInstr &FirstMI, Register Reg, const TargetInstrInfo &TII)
Check for a copy bundle as formed by SplitKit.
static bool isRealSpill(const MachineInstr &Def)
Check if Def fully defines a VReg with an undefined value.
static cl::opt< bool > RestrictStatepointRemat("restrict-statepoint-remat", cl::init(false), cl::Hidden, cl::desc("Restrict remat for statepoint operands"))
static Register isCopyOf(const MachineInstr &MI, Register Reg, const TargetInstrInfo &TII)
isFullCopyOf - If MI is a COPY to or from Reg, return the other register, otherwise return 0.
static void getVDefInterval(const MachineInstr &MI, LiveIntervals &LIS)
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
Register const TargetRegisterInfo * TRI
This file implements a map that provides insertion order iteration.
Promote Memory to Register
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
iterator find(const_arg_type_t< KeyT > Val)
bool erase(const KeyT &Val)
bool contains(const_arg_type_t< KeyT > Val) const
Return true if the specified key is in the map, false otherwise.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
void storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, Register SrcReg, bool isKill, int FrameIndex, const TargetRegisterClass *RC, Register VReg, MachineInstr::MIFlag Flags=MachineInstr::NoFlags) const override
Store the specified register of the given register class to the specified stack frame index.
Register isLoadFromStackSlot(const MachineInstr &MI, int &FrameIndex) const override
TargetInstrInfo overrides.
void loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, Register DestReg, int FrameIndex, const TargetRegisterClass *RC, Register VReg, unsigned SubReg=0, MachineInstr::MIFlag Flags=MachineInstr::NoFlags) const override
Load the specified register of the given register class from the specified stack frame index.
Register isStoreToStackSlot(const MachineInstr &MI, int &FrameIndex) const override
If the specified machine instruction is a direct store to a stack slot, return the virtual or physica...
Determines the latest safe point in a block in which we can insert a split, spill or other instructio...
A live range for subregisters.
LiveInterval - This class represents the liveness of a register, or stack slot.
bool isSpillable() const
isSpillable - Can this interval be spilled?
bool hasSubRanges() const
Returns true if subregister liveness information is available.
iterator_range< subrange_iterator > subranges()
SlotIndex InsertMachineInstrInMaps(MachineInstr &MI)
SlotIndexes * getSlotIndexes() const
SlotIndex getInstructionIndex(const MachineInstr &Instr) const
Returns the base index of the given instruction.
VNInfo::Allocator & getVNInfoAllocator()
LiveInterval & getInterval(Register Reg)
void InsertMachineInstrRangeInMaps(MachineBasicBlock::iterator B, MachineBasicBlock::iterator E)
LLVM_ABI void removePhysRegDefAt(MCRegister Reg, SlotIndex Pos)
Remove value numbers and related live segments starting at position Pos that are part of any liverang...
MachineBasicBlock * getMBBFromIndex(SlotIndex index) const
SlotIndex ReplaceMachineInstrInMaps(MachineInstr &MI, MachineInstr &NewMI)
Result of a LiveRange query.
bool isKill() const
Return true if the live-in value is killed by this instruction.
Callback methods for LiveRangeEdit owners.
const LiveInterval & getParent() const
VNInfo * getValNumInfo(unsigned ValNo)
getValNumInfo - Returns pointer to the specified val#.
LLVM_ABI void MergeValueInAsValue(const LiveRange &RHS, const VNInfo *RHSValNo, VNInfo *LHSValNo)
MergeValueInAsValue - Merge all of the segments of a specific val# in RHS into this live range as the...
iterator_range< vni_iterator > vnis()
LiveQueryResult Query(SlotIndex Idx) const
Query Liveness at Idx.
VNInfo * getVNInfoBefore(SlotIndex Idx) const
getVNInfoBefore - Return the VNInfo that is live up to but not necessarily including Idx,...
unsigned getNumValNums() const
void assign(const LiveRange &Other, BumpPtrAllocator &Allocator)
Copies values numbers and live segments from Other into this range.
VNInfo * getVNInfoAt(SlotIndex Idx) const
getVNInfoAt - Return the VNInfo that is live at Idx, or NULL.
MIBundleOperands - Iterate over all operands in a bundle of machine instructions.
LLVM_ABI iterator SkipPHIsLabelsAndDebug(iterator I, Register Reg=Register(), bool SkipPseudoOp=true)
Return the first instruction in MBB after I that is not a PHI, label or debug.
Instructions::iterator instr_iterator
Instructions::const_iterator const_instr_iterator
LLVM_ABI instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
iterator_range< pred_iterator > predecessors()
MachineInstrBundleIterator< MachineInstr > iterator
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
static const unsigned int DebugOperandMemNumber
A reserved operand number representing the instructions memory operand, for instructions that have a ...
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
MachineInstrSpan provides an interface to get an iteration range containing the instruction it was in...
Representation of each machine instruction.
const MachineBasicBlock * getParent() const
unsigned getNumOperands() const
Retuns the total number of operands.
bool isBundledWithPred() const
Return true if this instruction is part of a bundle, and it is not the first instruction in the bundl...
LLVM_ABI void removeOperand(unsigned OpNo)
Erase an operand from an instruction, leaving it with one fewer operand than it started with.
bool isBundledWithSucc() const
Return true if this instruction is part of a bundle, and it is not the last instruction in the bundle...
LLVM_ABI unsigned getDebugInstrNum()
Fetch the instruction number of this MachineInstr.
const MachineOperand & getOperand(unsigned i) const
bool isBundled() const
Return true if this instruction part of a bundle.
MachineOperand class - Representation of each machine instruction operand.
unsigned getSubReg() const
bool readsReg() const
readsReg - Returns true if this operand reads the previous value of its register.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
LLVM_ABI void setReg(Register Reg)
Change the register this operand corresponds to.
void setIsKill(bool Val=true)
void setIsUndef(bool Val=true)
bool isEarlyClobber() const
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
defusechain_instr_iterator< true, true, true, false > reg_bundle_nodbg_iterator
reg_bundle_nodbg_iterator/reg_bundle_nodbg_begin/reg_bundle_nodbg_end - Walk all defs and uses of the...
This class implements a map that also provides access to all stored values in a deterministic order.
Wrapper class representing virtual and physical registers.
constexpr bool isStack() const
Return true if this is a stack slot.
static Register index2VirtReg(unsigned Index)
Convert a 0-based index to a virtual register number.
MCRegister asMCReg() const
Utility to check-convert this value to a MCRegister.
constexpr bool isValid() const
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
SlotIndex - An opaque wrapper around machine indexes.
static bool isSameInstr(SlotIndex A, SlotIndex B)
isSameInstr - Return true if A and B refer to the same instruction.
SlotIndex getBaseIndex() const
Returns the base index for associated with this index.
SlotIndex getPrevSlot() const
Returns the previous slot in the index list.
SlotIndex getRegSlot(bool EC=false) const
Returns the register use/def slot in the current instruction for a normal or early-clobber def.
LLVM_ABI void removeSingleMachineInstrFromMaps(MachineInstr &MI)
Removes a single machine instruction MI from the mapping.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
void insert_range(Range &&R)
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
A SetVector that performs no allocations if smaller than a certain size.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void push_back(const T &Elt)
reverse_iterator rbegin()
std::reverse_iterator< iterator > reverse_iterator
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
MI-level Statepoint operands.
LLVM_ABI bool isFoldableReg(Register Reg) const
Return true if Reg is used only in operands which can be folded to stack usage.
TargetInstrInfo - Interface to description of machine instruction set.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
VNInfo - Value Number Information.
bool isUnused() const
Returns true if this value is unused.
unsigned id
The ID number of this value.
SlotIndex def
The index of the defining instruction.
bool isPHIDef() const
Returns true if this value is defined by a PHI instruction (or was, PHI instructions may have been el...
Calculate auxiliary information for a virtual register such as its spill weight and allocation hint.
static constexpr int NO_STACK_SLOT
self_iterator getIterator()
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
initializer< Ty > init(const Ty &Val)
NodeAddr< NodeBase * > Node
This is an optimization pass for GlobalISel generic memory operations.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
constexpr RegState getKillRegState(bool B)
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
LLVM_ABI PhysRegInfo AnalyzePhysRegInBundle(const MachineInstr &MI, Register Reg, const TargetRegisterInfo *TRI)
AnalyzePhysRegInBundle - Analyze how the current instruction or bundle uses a physical register.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
LLVM_ABI VirtRegInfo AnalyzeVirtRegInBundle(MachineInstr &MI, Register Reg, SmallVectorImpl< std::pair< MachineInstr *, unsigned > > *Ops=nullptr)
AnalyzeVirtRegInBundle - Analyze how the current instruction or bundle uses a virtual register.
DomTreeNodeBase< MachineBasicBlock > MachineDomTreeNode
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Spiller * createInlineSpiller(const Spiller::RequiredAnalyses &Analyses, MachineFunction &MF, VirtRegMap &VRM, VirtRegAuxInfo &VRAI, LiveRegMatrix *Matrix=nullptr)
Create and return a spiller that will insert spill code directly instead of deferring though VirtRegM...
ArrayRef(const T &OneElt) -> ArrayRef< T >
LLVM_ABI MachineInstr * buildDbgValueForSpill(MachineBasicBlock &BB, MachineBasicBlock::iterator I, const MachineInstr &Orig, int FrameIndex, Register SpillReg)
Clone a DBG_VALUE whose value has been spilled to FrameIndex.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
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.
static constexpr LaneBitmask getAll()
static constexpr LaneBitmask getNone()
Remat - Information needed to rematerialize at a specific location.
Information about how a physical register Reg is used by a set of operands.
bool FullyDefined
Reg or a super-register is defined.
VirtRegInfo - Information about a virtual register used by a set of operands.
bool Reads
Reads - One of the operands read the virtual register.
bool Tied
Tied - Uses and defs must use the same register.
bool Writes
Writes - One of the operands writes the virtual register.