31#define DEBUG_TYPE "rematerializer"
37void Rematerializer::Listener::anchor() {}
50 if ((SR.LaneMask & Mask).none())
52 if (!SR.liveAt(UseIdx))
70 if (
Reg.isPhysical()) {
86 FirstMI ? FirstMI : Regions[UseRegion].second;
113 NewDepRegIdx = RematIdx->second;
124 transferUserImpl(FromRegIdx, ToRegIdx, UserMI);
126 Regs[ToRegIdx].addUser(&UserMI, UserRegion);
127 extendToNewUsers(ToRegIdx, &UserMI);
129 Regs[FromRegIdx].eraseUser(&UserMI, UserRegion);
130 shrinkToUses(FromRegIdx);
135 unsigned UseRegion) {
136 Reg &FromReg = Regs[FromRegIdx];
137 auto UsesIt = FromReg.
Uses.find(UseRegion);
138 if (UsesIt == FromReg.
Uses.end())
144 transferUserImpl(FromRegIdx, ToRegIdx, *UserMI);
148 extendToNewUsers(ToRegIdx, NewUsers);
149 Regs[ToRegIdx].addUsers(RegionUsers, UseRegion);
151 FromReg.
Uses.erase(UseRegion);
152 shrinkToUses(FromRegIdx);
157 Reg &FromReg = Regs[FromRegIdx];
159 for (
const auto &[UseRegion, RegionUsers] : FromReg.
Uses) {
161 transferUserImpl(FromRegIdx, ToRegIdx, *UserMI);
164 Regs[ToRegIdx].addUsers(RegionUsers, UseRegion);
166 extendToNewUsers(ToRegIdx, NewUsers);
168 FromReg.
Uses.clear();
169 deleteReg(FromRegIdx);
172void Rematerializer::transferUserImpl(
RegisterIdx FromRegIdx,
175 assert(FromRegIdx != ToRegIdx &&
"identical registers");
177 "unrelated registers");
186 if (UserRegIdx ==
NoReg)
191 Reg &UserReg = Regs[UserRegIdx];
193 bool IsNewDep =
true;
194 if (UserReg.Defs.size() > 1) {
203 if (
DefMI == &UserMI)
206 if (MO.getReg() == FromReg) {
218 unsigned *FindFromReg =
find(UserDeps, FromRegIdx);
219 assert(FindFromReg != UserDeps.
end() &&
"broken dependency");
221 *FindFromReg = ToRegIdx;
223 UserReg.Dependencies.erase(FindFromReg);
229 LaneBitmask Mask = SubIdx ? TRI.getSubRegIndexLaneMask(SubIdx)
230 : MRI.getMaxLaneMaskForVReg(MO.
getReg());
233 LIS.getInstructionIndex(*MO.
getParent()).getRegSlot(
true),
Uses);
256 if (It == Rematerializations.end())
258 const RematsOf &Remats = It->getSecond();
263 const Reg &RematReg =
getReg(RematRegIdx);
268 if (RematRegSlot < Before &&
269 (BestRegIdx ==
NoReg || RematRegSlot > BestSlot)) {
270 BestSlot = RematRegSlot;
271 BestRegIdx = RematRegIdx;
277void Rematerializer::deleteReg(
RegisterIdx RootIdx) {
287 for (
RegisterIdx DepRegIdx : DeleteReg.Dependencies) {
289 Reg &DepReg = Regs[DepRegIdx];
290 for (MachineInstr *
DefMI : DeleteReg.Defs) {
291 if (DepReg.tryEraseUser(
DefMI, DeleteReg.DefRegion) &&
292 DepReg.Uses.empty()) {
303 }
while (!DepDAG.
empty());
307 Reg &DeleteReg = Regs[RegIdx];
308 Register DefReg = DeleteReg.getDefReg();
310 LIS.RemoveMachineInstrFromMaps(*
DefMI);
313 LIS.removeInterval(DefReg);
314 DeleteReg.Defs.clear();
317 SmallSet<RegisterIdx, 8> ShrinkRematRegs;
318 SmallSet<Register, 8> ShrinkUnrematRegs;
324 if (DepReg.isAlive() && ShrinkRematRegs.
insert(DepRegIdx).second) {
325 assert(!DepReg.Uses.empty() &&
"dep should have uses");
326 shrinkToUses(DepRegIdx);
330 if (ShrinkUnrematRegs.
insert(
Reg).second)
331 shrinkToUsesUnremat(
Reg);
336void Rematerializer::DeadDefDelegate::LRE_WillEraseInstruction(
341 Remater.noteMIWillBeDeleted(*
MI);
352 const LiveIntervals &LIS = Remater.LIS;
353 SlotIndex MISlot = LIS.getInstructionIndex(*
MI);
354 unsigned MIRegion = ~0
U;
355 for (
auto [RegionIdx, Bounds] :
enumerate(Remater.Regions)) {
356 auto &[RegionBegin, RegionEnd] = Bounds;
359 if (FirstMI == RegionEnd) {
364 if (LIS.getInstructionIndex(*FirstMI) <= MISlot) {
369 if (LIS.getInstructionIndex(*LastMI) < MISlot)
373 MIRegion = RegionIdx;
374 if (RegionBegin == It)
381 SmallDenseSet<Register, 2> UsedRegs;
382 for (
const MachineOperand &MO :
MI->all_uses()) {
386 auto RematRegUse = Remater.RegToIdx.find(
Reg);
387 if (RematRegUse == Remater.RegToIdx.end())
389 assert(MIRegion != ~0U &&
"remat user cannot be outside regions");
390 Remater.Regs[RematRegUse->second].eraseUser(
MI, MIRegion);
397 Reg &DeleteReg = Remater.Regs[RegIdx];
398 for (
RegisterIdx DepRegIdx : DeleteReg.Dependencies)
399 Remater.Regs[DepRegIdx].tryEraseUser(
MI, DeleteReg.DefRegion);
410 if (!DeleteReg.isAlive())
412 assert(DeleteReg.getLastDef() ==
MI &&
"last def should be deleted first");
413 assert(DeleteReg.Uses.empty() &&
"register should no longer have uses");
418 Remater.preDeletion(RegIdx);
419 DeleteReg.Defs.clear();
422void Rematerializer::preDeletion(
RegisterIdx DeleteRegIdx) {
423 Reg &DeleteReg = Regs[DeleteRegIdx];
424 assert(DeleteReg.isAlive() &&
"register must still be alive");
425 noteRegWillBeDeleted(DeleteRegIdx);
432 for (MachineInstr *
DefMI : DeleteReg.Defs) {
433 if (RegionBegin !=
DefMI)
443 RematsOf &OriginRemats = Rematerializations.at(OriginIdx);
444 assert(OriginRemats.contains(DeleteRegIdx) &&
"broken remat<->origin link");
445 OriginRemats.erase(DeleteRegIdx);
446 if (OriginRemats.empty())
447 Rematerializations.erase(OriginIdx);
453 : Regions(Regions), MRI(MF.getRegInfo()), LIS(LIS),
454 TII(*MF.getSubtarget().getInstrInfo()), TRI(TII.getRegisterInfo()) {
455#ifdef EXPENSIVE_CHECKS
458 for (
const auto &[RegionBegin, RegionEnd] : Regions) {
459 assert(RegionBegin != RegionEnd &&
"empty region");
460 for (
auto MI = RegionBegin;
MI != RegionEnd; ++
MI) {
461 bool IsNewMI = SeenMIs.
insert(&*
MI).second;
462 assert(IsNewMI &&
"overlapping regions");
463 assert(!
MI->isTerminator() &&
"terminator in region");
465 if (RegionEnd != RegionBegin->getParent()->end()) {
466 bool IsNewMI = SeenMIs.
insert(&*RegionEnd).second;
467 assert(IsNewMI &&
"overlapping regions (upper bound)");
475 UnrematableDeps.clear();
477 Rematerializations.clear();
488 RegionMBB.reserve(Regions.size());
489 for (
unsigned I = 0, E = Regions.size();
I < E; ++
I) {
497 RegionMBB.push_back(&
MBB);
502 assert(!MIRegion.
contains(RegionTerm) &&
"regions should not intersect");
503 MIRegion.
insert({RegionTerm,
I});
507 const unsigned NumVirtRegs = MRI.getNumVirtRegs();
509 for (
unsigned I = 0, E = NumVirtRegs;
I != E; ++
I) {
511 addRegIfRematerializable(
I, MIRegion, SeenRegs);
513 assert(Regs.size() == UnrematableDeps.size());
519 return !Regs.empty();
522void Rematerializer::addRegIfRematerializable(
525 assert(!SeenRegs[VirtRegIdx] &&
"register already seen");
527 SeenRegs.
set(VirtRegIdx);
540 if (!isMIRematerializable(
DefMI))
543 if (DefRegion == MIRegion.
end())
545 if (RematReg.Defs.empty())
546 RematReg.DefRegion = DefRegion->getSecond();
547 else if (RematReg.DefRegion != DefRegion->getSecond())
549 RematReg.Defs.push_back(&
DefMI);
551 if (RematReg.Defs.empty())
556 return LIS.getInstructionIndex(*LHS) < LIS.getInstructionIndex(*RHS);
561 if (DefMO.getReg() == DefReg && DefMO.isUndef())
566 SlotIndex LastDefSlot = LIS.getInstructionIndex(*RematReg.getLastDef());
569 const LiveInterval &DefLI = LIS.getInterval(DefReg);
570 SlotIndex AfterLastDef = LastDefSlot.
getRegSlot();
572 for (
const LiveInterval::SubRange &SR : DefLI.
subranges())
573 if (SR.liveAt(AfterLastDef))
574 RematReg.Mask |= SR.LaneMask;
576 RematReg.Mask = MRI.getMaxLaneMaskForVReg(DefReg);
581 const bool MoreThanOneDef = RematReg.Defs.size() > 1;
582 for (MachineInstr &
UseMI : MRI.use_nodbg_instructions(DefReg)) {
589 if (UseRegion == MIRegion.
end())
592 if (MoreThanOneDef && RematReg.DefRegion == UseRegion->second &&
593 LastDefSlot > LIS.getInstructionIndex(
UseMI))
596 RematReg.addUser(&
UseMI, UseRegion->second);
598 if (RematReg.Uses.empty())
604 SmallSetVector<RegisterIdx, 2> RematDeps;
605 SmallMapVector<Register, LaneBitmask, 2> UnrematDeps;
606 for (
const MachineInstr *
DefMI : RematReg.Defs) {
609 if (!DepReg || DepReg == DefReg)
612 if (!SeenRegs[DepRegIdx])
613 addRegIfRematerializable(DepRegIdx, MIRegion, SeenRegs);
614 if (
auto DepIt = RegToIdx.find(DepReg); DepIt != RegToIdx.end()) {
615 RematDeps.
insert(DepIt->second);
617 LaneBitmask &CurrentMask =
620 LaneBitmask
Mask = MO.getSubReg()
621 ? TRI.getSubRegIndexLaneMask(MO.getSubReg())
622 : MRI.getMaxLaneMaskForVReg(DepReg);
628 if (MoreThanOneDef) {
631 SlotIndex FirstDefSlot = LIS.getInstructionIndex(*RematReg.getFirstDef());
632 for (
const auto &[UnrematDepReg,
_] : UnrematDeps) {
633 for (MachineInstr &UnrematDefMI : MRI.def_instructions(UnrematDepReg)) {
634 SlotIndex UnrematDefSlot = LIS.getInstructionIndex(UnrematDefMI);
635 if (UnrematDefSlot > FirstDefSlot || UnrematDefSlot < LastDefSlot)
642 RematReg.Dependencies = RematDeps.
takeVector();
643 RegToIdx.
insert({DefReg, Regs.size()});
644 Regs.push_back(RematReg);
645 UnrematableDeps.push_back(UnrematDeps.takeVector());
648bool Rematerializer::isMIRematerializable(
const MachineInstr &
MI)
const {
649 if (!TII.isReMaterializable(
MI))
652 assert(
MI.getOperand(0).getReg().isVirtual() &&
"should be virtual");
654 for (
const MachineOperand &MO :
MI.all_uses()) {
657 if (MO.getReg().isPhysical()) {
658 if (MRI.isConstantPhysReg(MO.getReg()) ||
659 TII.isIgnorableUse(
MI,
MI.getOperandNo(&MO)))
669 if (!
MI.getNumOperands() || !
MI.getOperand(0).isReg() ||
670 !
MI.getOperand(0).isDef())
673 auto UserRegIt = RegToIdx.find(
Reg);
674 if (UserRegIt == RegToIdx.end())
676 return UserRegIt->second;
685 Reg &NewReg = Regs.emplace_back();
686 Reg &FromReg = Regs[RegIdx];
689 NewReg.
Defs.reserve(FromReg.
Defs.size());
696 Origins.push_back(OriginIdx);
697 Rematerializations[OriginIdx].insert(NewRegIdx);
703 TII.reMaterialize(*RegionMBB[UseRegion], InsertPos, NewDefReg, 0, *
DefMI);
704 NewReg.
Defs.push_back(&*std::prev(InsertPos));
706 RegToIdx.insert({NewDefReg, NewRegIdx});
707 postRematerialization(RegIdx, NewRegIdx);
709 noteRegCreated(NewRegIdx);
718 assert(RegToIdx.contains(DefReg) &&
"unknown defined register");
719 assert(RegToIdx.at(DefReg) == RegIdx &&
"incorrect defined register");
720 assert(!
getReg(RegIdx).isAlive() &&
"register is still alive");
721 Reg &OriginReg = Regs[RegIdx];
725 if (!RecreateOriginalReg)
726 Rematerializations[
getOriginOf(RegIdx)].insert(RegIdx);
732 if (RecreateOriginalReg) {
733 assert(Rematerializations.contains(RegIdx) &&
"expected remats");
734 ModelRegIdx = *Rematerializations.at(RegIdx).begin();
739 const Reg &ModelReg =
getReg(ModelRegIdx);
742 TII.reMaterialize(*RegionMBB[OriginReg.
DefRegion], InsertPos, DefReg, 0,
744 OriginReg.
Defs.push_back(&*std::prev(InsertPos));
746 postRematerialization(ModelRegIdx, RegIdx);
751void Rematerializer::postRematerialization(
RegisterIdx ModelRegIdx,
753 Reg &ModelReg = Regs[ModelRegIdx], &RematReg = Regs[RematRegIdx];
766 auto &[RegionBegin, RegionEnd] = Regions[RematReg.DefRegion];
767 if (RegionBegin == RegionEnd ||
774 auto ZipedDeps =
zip_equal(ModelReg.Dependencies, RematReg.Dependencies);
775 for (
const auto &[OldDepRegIdx, NewDepRegIdx] : ZipedDeps) {
777 <<
printID(NewDepRegIdx) <<
'\n');
783 bool NewDefHasReg =
false;
785 if (!MO.isReg() || MO.getReg() != OldReg)
789 if (OldDepRegIdx != NewDepRegIdx)
790 MO.substVirtReg(NewReg, 0,
TRI);
793 Regs[NewDepRegIdx].addUser(
DefMI, RematReg.DefRegion);
795 assert(!DefsUsingNewDep.
empty() &&
"no user of dependency");
796 extendToNewUsers(NewDepRegIdx, DefsUsingNewDep);
802 extendInterval(LIS.getInterval(
Reg), Mask, UseIdx);
805void Rematerializer::extendToNewUsers(
RegisterIdx RegIdx,
807 if (NewUsers.
empty())
810 assert(ExtendReg.isAlive() &&
"register must be alive");
812 Register DefReg = ExtendReg.getDefReg();
813 LiveInterval &LI = LIS.getInterval(DefReg);
814 const LaneBitmask FullLaneMask = MRI.getMaxLaneMaskForVReg(DefReg);
815 const bool ShouldTrackSubReg = MRI.shouldTrackSubRegLiveness(DefReg);
825 for (MachineInstr *UserMI : NewUsers) {
826 SlotIndex UseIdx = LIS.getInstructionIndex(*UserMI).
getRegSlot();
830 for (MachineOperand &MO : UserMI->
all_uses()) {
831 if (MO.getReg() == DefReg) {
832 unsigned SubIdx = MO.getSubReg();
834 RegMask = FullLaneMask;
837 RegMask |= TRI.getSubRegIndexLaneMask(SubIdx);
841 if (RegMask != FullLaneMask) {
844 LIS.getVNInfoAllocator(), RegMask, [](LiveInterval::SubRange &SR) {},
845 *LIS.getSlotIndexes(), TRI);
849 extendInterval(LI, RegMask, UseIdx);
857 if (ExtendReg.getLastDef()->getOperand(0).isDead())
858 dbgs() <<
"Clearing dead flag for "
860 ExtendReg.Defs.size() - 1)
863 ExtendReg.getLastDef()->getOperand(0).setIsDead(
false);
871 <<
printReg(LI.
reg(), &TRI, 0, &MRI) <<
" to " << UseIdx
873 LIS.extendToIndices(LI, UseIdx);
877 bool SubRangeExtended =
false;
878 for (LiveInterval::SubRange &SR : LI.
subranges()) {
879 if ((SR.LaneMask & Mask).any() && !SR.liveAt(UseIdx)) {
880 SubRangeExtended =
true;
881 LLVM_DEBUG(
dbgs() <<
"Extending subrange " << SR <<
" of register "
882 <<
printReg(LI.
reg(), &TRI, 0, &MRI) <<
" to " << UseIdx
884 LIS.extendToIndices(SR, UseIdx);
887 if (!SubRangeExtended)
893 LIS.constructMainRangeFromSubranges(LI);
896void Rematerializer::shrinkToUses(
RegisterIdx RegIdx) {
897 Reg &ShrinkReg = Regs[RegIdx];
898 assert(ShrinkReg.isAlive() &&
"register must be alive");
899 if (ShrinkReg.Uses.empty()) {
906 LiveInterval &LI = LIS.getInterval(ShrinkReg.getDefReg());
909 LIS.shrinkToUses(&LI);
912void Rematerializer::shrinkToUsesUnremat(
Register Reg) {
913 LiveInterval &LI = LIS.getInterval(
Reg);
914 LLVM_DEBUG(
dbgs() <<
"Shrinking interval of unrematerializable register "
917 SmallVector<MachineInstr *, 2> DeadDefs;
918 if (!LIS.shrinkToUses(&LI, &DeadDefs)) {
919 assert(DeadDefs.
empty() &&
"expected no dead def");
925 if (DeadDefs.
empty())
931 DeadDefDelegate DeadDefDeleg(*
this);
933 LiveRangeEdit(
nullptr, NewRegs, MF, LIS,
nullptr, &DeadDefDeleg)
934 .eliminateDeadDefs(DeadDefs);
937std::pair<MachineInstr *, MachineInstr *>
940 auto It =
Uses.find(UseRegion);
941 if (It ==
Uses.end())
942 return {
nullptr,
nullptr};
948 SlotIndex FirstIndex = LIS.getInstructionIndex(*FirstMI),
949 LastIndex = FirstIndex;
951 while (++
User != UserEnd) {
953 if (UserIndex < FirstIndex) {
954 FirstIndex = UserIndex;
956 }
else if (UserIndex > LastIndex) {
957 LastIndex = UserIndex;
962 return {FirstMI, LastMI};
969void Rematerializer::Reg::addUsers(
const RegionUsers &NewUsers,
976 assert(RUsers.contains(
MI) &&
"user not in region");
977 if (RUsers.size() == 1)
983bool Rematerializer::Reg::tryEraseUser(MachineInstr *
MI,
unsigned Region) {
984 auto RegionUsers =
Uses.find(Region);
985 if (RegionUsers ==
Uses.end() || !RegionUsers->getSecond().erase(
MI))
987 if (RegionUsers->getSecond().empty())
995 std::function<void(
RegisterIdx,
unsigned)> WalkTree =
1000 WalkTree(DepRegIdx,
Depth + 1);
1002 WalkTree(RootIdx, 0);
1007 sort(Regs, [](
const auto &LHS,
const auto &RHS) {
1008 return LHS.second > RHS.second;
1011 OS <<
printID(RootIdx) <<
" has " << Regs.size() - 1 <<
" dependencies\n";
1012 for (
const auto &[RegIdx,
Depth] : Regs) {
1023 OS <<
'(' << RegIdx <<
'/';
1028 OS <<
")[" << PrintReg.
DefRegion <<
"]";
1033 unsigned DefIdx)
const {
1039 if (!PrintReg.
Uses.empty()) {
1040 assert(PrintReg.
isAlive() &&
"dead register cannot have uses");
1045 for (
const auto &[
I, Bounds] :
enumerate(Regions)) {
1046 if (PrintReg.
Uses.contains(
I))
1052 if (LiveIn == Bounds.second) {
1060 std::prev(Bounds.second), Bounds.first);
1061 if (LI.
liveAt(LIS.getInstructionIndex(*LiveIn)) &&
1062 LI.
liveAt(LIS.getInstructionIndex(*LiveOut).getDeadSlot())) {
1063 OS << (
First ?
" - " :
",") <<
I;
1067 OS << (
First ?
" --> " :
" -> ");
1070 auto It = PrintReg.
Uses.begin();
1072 while (++It != PrintReg.
Uses.end())
1073 OS <<
"," << It->first;
1078 assert(DefIdx < PrintReg.
Defs.size() &&
"out-of-bound def");
1080 OS <<
"(def. " << DefIdx + 1 <<
" / " << PrintReg.
Defs.size() <<
") ";
1081 PrintDef.
print(OS,
true,
false,
1084 LIS.getInstructionIndex(PrintDef).print(OS);
1093 OS <<
" User " <<
printUser(
MI, UseRegion) <<
'\n';
1099 std::optional<unsigned> UseRegion)
const {
1102 if (RegIdx !=
NoReg) {
1113 MI->print(OS,
true,
false,
1116 LIS.getInstructionIndex(*MI).print(OS);
1125 Rematerializations[Remater.
getOriginOf(RegIdx)].insert(RegIdx);
1137 auto GetNextValidPosAfterDef =
1140 DefIdx + 1 < Reg.Defs.size() ? Reg.Defs[DefIdx + 1] :
nullptr;
1142 std::next(Reg.Defs[DefIdx]->getIterator());
1144 while (ValidPos != ParentMBB->
end()) {
1150 if (NextDef && &CandMI == NextDef)
1151 return LastValidPos;
1152 if (!isRollbackableMI(CandMI, Remater))
1156 ValidPos = std::next(ValidPos);
1159 LastValidPos = ValidPos;
1168 for (
unsigned I = Reg.Defs.size();
I > 0; --
I)
1169 invalidatePosition(Reg.Defs[
I - 1], GetNextValidPosAfterDef(
I - 1));
1175 DeadRegs.push_back(DeadReg(RegIdx, Remater));
1176 for (
unsigned I = Reg.Defs.size();
I > 0; --
I) {
1177 const InsertBeforePos InsertPos =
1178 makePos(GetNextValidPosAfterDef(
I - 1), ParentMBB);
1179 PosToIdx[InsertPos].insert(Positions.size());
1180 Positions.push_back(InsertPos);
1193 while (ValidPos != ParentMBB->
end() && isRollbackableMI(*ValidPos, Remater))
1194 ValidPos = std::next(ValidPos);
1195 invalidatePosition(&
MI, ValidPos);
1205 unsigned PositionIndex = Positions.
size();
1211 for (
const DeadReg &Reg :
reverse(DeadRegs)) {
1215 Rematerializations.erase(Reg.Idx);
1216 PositionIndex -= Reg.Defs.size();
1223 for (
unsigned I = 0, E = Reg.Defs.size();
I < E; ++
I) {
1224 InsertBeforePos Pos = Positions[--PositionIndex];
1234 Remater.
recreateReg(Reg.Idx, InsertPositions, Reg.DefReg);
1237 for (
const auto [OldDef, NewDef] :
zip_equal(Reg.Defs, RecreateReg.
Defs)) {
1238 assert(!Replacements.
contains(OldDef) &&
"duplicate deleted MI");
1239 Replacements[OldDef] = NewDef;
1244 for (
const auto &[RegIdx,
RematsOf] : Rematerializations) {
1258 Rematerializations.clear();
1259 RollingBack =
false;
1272 if (
RematsOf == Rematerializations.end())
1279 const InsertBeforePos MIPos = InsertBeforePos(
MI),
1280 NewPos = makePos(It,
MI->getParent());
1281 auto MIIndices = PosToIdx.find(MIPos);
1282 if (MIIndices == PosToIdx.end())
1285 assert(!InvalIndices.
empty() &&
"no index hold position");
1286 for (
unsigned I : InvalIndices)
1287 Positions[
I] = NewPos;
1288 PosToIdx.try_emplace(NewPos).first->getSecond().insert_range(InvalIndices);
1289 PosToIdx.erase(MIPos);
MachineInstrBuilder & UseMI
MachineInstrBuilder MachineInstrBuilder & DefMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
iv Induction Variable Users
A common definition of LaneBitmask for use in TableGen and CodeGen.
Register const TargetRegisterInfo * TRI
This file implements a map that provides insertion order iteration.
Promote Memory to Register
Rematerializer::RegisterIdx RegisterIdx
static Register getRegDependency(const MachineOperand &MO)
If MO is a virtual read register, returns it.
static bool isIdenticalAtUse(const VNInfo &OVNI, LaneBitmask Mask, SlotIndex UseIdx, const LiveInterval &LI)
Checks whether the value in LI at UseIdx is identical to OVNI (this implies it is also live there).
MIR-level target-independent rematerialization helpers.
Remove Loads Into Fake Uses
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallSet class.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
bool empty() const
Check if the array is empty.
BitVector & set()
Set all bits in the bitvector.
iterator find(const_arg_type_t< KeyT > Val)
std::pair< iterator, bool > emplace_or_assign(const KeyT &Key, Ts &&...Args)
bool contains(const_arg_type_t< KeyT > Val) const
Return true if the specified key is in the map, false otherwise.
ValueT lookup_or(const_arg_type_t< KeyT > Val, U &&Default) const
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Implements a dense probed hash-table based set.
A live range for subregisters.
LiveInterval - This class represents the liveness of a register, or stack slot.
LLVM_ABI void removeEmptySubRanges()
Removes all subranges without any segments (subranges without segments are not considered valid and s...
bool hasSubRanges() const
Returns true if subregister liveness information is available.
SubRange * createSubRangeFrom(BumpPtrAllocator &Allocator, LaneBitmask LaneMask, const LiveRange &CopyFrom)
Like createSubRange() but the new range is filled with a copy of the liveness information in CopyFrom...
iterator_range< subrange_iterator > subranges()
LLVM_ABI void refineSubRanges(BumpPtrAllocator &Allocator, LaneBitmask LaneMask, std::function< void(LiveInterval::SubRange &)> Apply, const SlotIndexes &Indexes, const TargetRegisterInfo &TRI, unsigned ComposeSubRegIdx=0)
Refines the subranges to support LaneMask.
SlotIndex InsertMachineInstrInMaps(MachineInstr &MI)
SlotIndex getInstructionIndex(const MachineInstr &Instr) const
Returns the base index of the given instruction.
LiveInterval & createAndComputeVirtRegInterval(Register Reg)
bool liveAt(SlotIndex index) const
VNInfo * getVNInfoAt(SlotIndex Idx) const
getVNInfoAt - Return the VNInfo that is live at Idx, or NULL.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
MachineInstrBundleIterator< MachineInstr > iterator
Representation of each machine instruction.
LLVM_ABI void substituteRegister(Register FromReg, Register ToReg, unsigned SubIdx, const TargetRegisterInfo &RegInfo)
Replace all occurrences of FromReg with ToReg:SubIdx, properly composing subreg indices where necessa...
LLVM_ABI void print(raw_ostream &OS, bool IsStandalone=true, bool SkipOpers=false, bool SkipDebugLoc=false, bool AddNewLine=true, const TargetInstrInfo *TII=nullptr) const
Print this MI to OS.
filtered_mop_range all_uses()
Returns an iterator range over all operands that are (explicit or implicit) register uses.
LLVM_ABI MachineInstrBundleIterator< MachineInstr > eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
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.
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
Register getReg() const
getReg - Returns the register number.
iterator_range< def_instr_iterator > def_instructions(Register Reg) const
std::pair< iterator, bool > try_emplace(const KeyT &Key, Ts &&...Args)
Simple wrapper around std::function<void(raw_ostream&)>.
RegionT * getParent() const
Get the parent of the Region.
Wrapper class representing virtual and physical registers.
static Register index2VirtReg(unsigned Index)
Convert a 0-based index to a virtual register number.
unsigned virtRegIndex() const
Convert a virtual register number to a 0-based index.
Rematerializer::RegisterIdx RegisterIdx
MIR-level target-independent rematerializer.
LLVM_ABI Printable printDependencyDAG(RegisterIdx RootIdx) const
RegisterIdx getOriginOrSelf(RegisterIdx RegIdx) const
If RegIdx is a rematerialization, returns its origin's index.
bool isOriginalRegister(RegisterIdx RegIdx) const
Whether register RegIdx is an original register.
static constexpr unsigned NoReg
Error value for register indices.
LLVM_ABI Printable printID(RegisterIdx RegIdx) const
LLVM_ABI RegisterIdx rematerializeToPos(RegisterIdx RootIdx, unsigned UseRegion, MachineBasicBlock::iterator InsertPos, DependencyReuseInfo &DRI)
Rematerializes register RootIdx before position InsertPos in UseRegion and returns the new register's...
unsigned getNumRegs() const
SmallDenseSet< RegisterIdx, 4 > RematsOf
RegisterIdx getOriginOf(RegisterIdx RematRegIdx) const
Returns the origin index of rematerializable register RegIdx.
const Reg & getReg(RegisterIdx RegIdx) const
LLVM_ABI RegisterIdx rematerializeToRegion(RegisterIdx RootIdx, unsigned UseRegion, DependencyReuseInfo &DRI)
Rematerializes register RootIdx just before its first user inside region UseRegion (or at the end of ...
std::pair< MachineBasicBlock::iterator, MachineBasicBlock::iterator > RegionBoundaries
A region's boundaries i.e.
LLVM_ABI RegisterIdx getDefRegIdx(const MachineInstr &MI) const
If MI's first operand defines a register and that register is a rematerializable register tracked by ...
bool isPermanentlyDead(RegisterIdx RegIdx) const
Determines whether register RegIdx fully disappeared from the MIR.
unsigned RegisterIdx
Index type for rematerializable registers.
LLVM_ABI void recreateReg(RegisterIdx RegIdx, ArrayRef< MachineBasicBlock::iterator > Positions, Register DefReg)
Re-creates each defining instruction of a previously deleted register RegIdx before each position in ...
LLVM_ABI bool isMOIdenticalAtUses(MachineOperand &MO, ArrayRef< SlotIndex > Uses) const
Determines whether (sub-)register operand MO has the same value at all Uses as at MO.
ArrayRef< std::pair< Register, LaneBitmask > > getUnrematableDeps(RegisterIdx RegIdx) const
Returns unreamaterializable read lanes of register operands for register RegIdx.
LLVM_ABI void transferRegionUsers(RegisterIdx FromRegIdx, RegisterIdx ToRegIdx, unsigned UseRegion)
Transfers all users of register FromRegIdx in region UseRegion to ToRegIdx, the latter of which must ...
LLVM_ABI Rematerializer(MachineFunction &MF, SmallVectorImpl< RegionBoundaries > &Regions, LiveIntervals &LIS)
Simply initializes some internal state, does not identify rematerialization candidates.
LLVM_ABI void transferUser(RegisterIdx FromRegIdx, RegisterIdx ToRegIdx, unsigned UserRegion, MachineInstr &UserMI)
Transfers user UserMI in region UserRegion from register FromRegIdx to ToRegIdx, the latter of which ...
LLVM_ABI void transferAllUsers(RegisterIdx FromRegIdx, RegisterIdx ToRegIdx)
Transfers all users of register FromRegIdx to register ToRegIdx, the latter of which must be a remate...
LLVM_ABI bool isRegIdenticalAtUses(Register Reg, LaneBitmask Mask, SlotIndex RefSlot, ArrayRef< SlotIndex > Uses) const
Determines whether lanes Mask of register Reg habe the same value at all Uses as at RefSlot.
bool isRematerializedRegister(RegisterIdx RegIdx) const
Whether register RegIdx is a rematerialization of some original register.
LLVM_ABI Printable printRegUsers(RegisterIdx RegIdx) const
LLVM_ABI Printable printUser(const MachineInstr *MI, std::optional< unsigned > UseRegion=std::nullopt) const
LLVM_ABI RegisterIdx rematerializeReg(RegisterIdx RegIdx, unsigned UseRegion, MachineBasicBlock::iterator InsertPos, SmallVectorImpl< RegisterIdx > &&Dependencies)
Rematerializes register RegIdx before InsertPos in UseRegion, adding the new rematerializable registe...
LLVM_ABI Printable printRematReg(RegisterIdx RegIdx, bool SkipRegions=false, unsigned DefIdx=0) const
LLVM_ABI RegisterIdx findRematInRegion(RegisterIdx RegIdx, unsigned Region, SlotIndex Before) const
Finds the closest rematerialization of register RegIdx in region Region that exists before slot Befor...
LLVM_ABI bool analyze()
Goes through the whole MF and identifies all rematerializable registers.
void rollback(Rematerializer &Remater)
Re-creates all deleted registers and rolls back all rematerializations that were recorded.
void rematerializerNoteRegWillBeDeleted(const Rematerializer &Remater, RegisterIdx RegIdx) override
Called just before register RegIdx is deleted from the MIR.
void rematerializerNoteMIWillBeDeleted(const Rematerializer &Remater, MachineInstr &MI) override
Called just before unrematerializable instruction MI is deleted from the MIR because it has become a ...
void rematerializerNoteRegCreated(const Rematerializer &Remater, RegisterIdx RegIdx) override
Called just after register NewRegIdx is created (following a rematerialization).
Vector takeVector()
Clear the SetVector and return the underlying vector.
bool insert(const value_type &X)
Insert a new element into the SetVector.
SlotIndex - An opaque wrapper around machine indexes.
SlotIndex getRegSlot(bool EC=false) const
Returns the register use/def slot in the current instruction for a normal or early-clobber def.
Implements a dense probed hash-table based set with some number of buckets stored inline.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool contains(ConstPtrType Ptr) const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
iterator insert(iterator I, T &&Elt)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
A Use represents the edge between a Value definition and its users.
VNInfo - Value Number Information.
std::pair< iterator, bool > insert(const ValueT &V)
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
self_iterator getIterator()
This class implements an extremely fast bulk output stream that can only output to a stream.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
detail::zippy< detail::zip_first, T, U, Args... > zip_equal(T &&t, U &&u, Args &&...args)
zip iterator that assumes that all iteratees have the same length.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
IterT skipDebugInstructionsForward(IterT It, IterT End, bool SkipPseudoOp=true)
Increment It until it points to a non-debug instruction or to End and return the resulting iterator.
auto reverse(ContainerTy &&C)
void sort(IteratorTy Start, IteratorTy End)
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...
IterT skipDebugInstructionsBackward(IterT It, IterT Begin, bool SkipPseudoOp=true)
Decrement It until it points to a non-debug instruction or to Begin and return the resulting iterator...
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
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 getNone()
When rematerializating a register (called the "root" register in this context) to a given position,...
SmallDenseMap< RegisterIdx, RegisterIdx, 4 > DependencyMap
Keys and values are rematerializable register indices.
A rematerializable register, potentially defined by multiple instructions.
LaneBitmask Mask
The rematerializable register's lane bitmask.
LLVM_ABI std::pair< MachineInstr *, MachineInstr * > getRegionUseBounds(unsigned UseRegion, const LiveIntervals &LIS) const
Returns the first and last user of the register in region UseRegion.
SmallVector< MachineInstr *, 1 > Defs
All instructions that define the register, in program order.
unsigned DefRegion
Defining region of the register.
SmallDenseMap< unsigned, RegionUsers, 2 > Uses
Uses of the register, mapped by region.
MachineInstr * getLastDef() const
Register getDefReg() const
Returns the rematerializable register from one of its defining instructions.
SmallVector< RegisterIdx, 2 > Dependencies
This register's rematerializable dependencies, one per unique rematerializable register operand over ...
SmallDenseSet< MachineInstr *, 4 > RegionUsers