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()) || TII.isIgnorableUse(MO))
668 if (!
MI.getNumOperands() || !
MI.getOperand(0).isReg() ||
669 !
MI.getOperand(0).isDef())
672 auto UserRegIt = RegToIdx.find(
Reg);
673 if (UserRegIt == RegToIdx.end())
675 return UserRegIt->second;
684 Reg &NewReg = Regs.emplace_back();
685 Reg &FromReg = Regs[RegIdx];
688 NewReg.
Defs.reserve(FromReg.
Defs.size());
695 Origins.push_back(OriginIdx);
696 Rematerializations[OriginIdx].insert(NewRegIdx);
702 TII.reMaterialize(*RegionMBB[UseRegion], InsertPos, NewDefReg, 0, *
DefMI);
703 NewReg.
Defs.push_back(&*std::prev(InsertPos));
705 RegToIdx.insert({NewDefReg, NewRegIdx});
706 postRematerialization(RegIdx, NewRegIdx);
708 noteRegCreated(NewRegIdx);
717 assert(RegToIdx.contains(DefReg) &&
"unknown defined register");
718 assert(RegToIdx.at(DefReg) == RegIdx &&
"incorrect defined register");
719 assert(!
getReg(RegIdx).isAlive() &&
"register is still alive");
720 Reg &OriginReg = Regs[RegIdx];
724 if (!RecreateOriginalReg)
725 Rematerializations[
getOriginOf(RegIdx)].insert(RegIdx);
731 if (RecreateOriginalReg) {
732 assert(Rematerializations.contains(RegIdx) &&
"expected remats");
733 ModelRegIdx = *Rematerializations.at(RegIdx).begin();
738 const Reg &ModelReg =
getReg(ModelRegIdx);
741 TII.reMaterialize(*RegionMBB[OriginReg.
DefRegion], InsertPos, DefReg, 0,
743 OriginReg.
Defs.push_back(&*std::prev(InsertPos));
745 postRematerialization(ModelRegIdx, RegIdx);
750void Rematerializer::postRematerialization(
RegisterIdx ModelRegIdx,
752 Reg &ModelReg = Regs[ModelRegIdx], &RematReg = Regs[RematRegIdx];
765 auto &[RegionBegin, RegionEnd] = Regions[RematReg.DefRegion];
766 if (RegionBegin == RegionEnd ||
773 auto ZipedDeps =
zip_equal(ModelReg.Dependencies, RematReg.Dependencies);
774 for (
const auto &[OldDepRegIdx, NewDepRegIdx] : ZipedDeps) {
776 <<
printID(NewDepRegIdx) <<
'\n');
782 bool NewDefHasReg =
false;
784 if (!MO.isReg() || MO.getReg() != OldReg)
788 if (OldDepRegIdx != NewDepRegIdx)
789 MO.substVirtReg(NewReg, 0,
TRI);
792 Regs[NewDepRegIdx].addUser(
DefMI, RematReg.DefRegion);
794 assert(!DefsUsingNewDep.
empty() &&
"no user of dependency");
795 extendToNewUsers(NewDepRegIdx, DefsUsingNewDep);
801 extendInterval(LIS.getInterval(
Reg), Mask, UseIdx);
804void Rematerializer::extendToNewUsers(
RegisterIdx RegIdx,
806 if (NewUsers.
empty())
809 assert(ExtendReg.isAlive() &&
"register must be alive");
811 Register DefReg = ExtendReg.getDefReg();
812 LiveInterval &LI = LIS.getInterval(DefReg);
813 const LaneBitmask FullLaneMask = MRI.getMaxLaneMaskForVReg(DefReg);
814 const bool ShouldTrackSubReg = MRI.shouldTrackSubRegLiveness(DefReg);
824 for (MachineInstr *UserMI : NewUsers) {
825 SlotIndex UseIdx = LIS.getInstructionIndex(*UserMI).
getRegSlot();
829 for (MachineOperand &MO : UserMI->
all_uses()) {
830 if (MO.getReg() == DefReg) {
831 unsigned SubIdx = MO.getSubReg();
833 RegMask = FullLaneMask;
836 RegMask |= TRI.getSubRegIndexLaneMask(SubIdx);
840 if (RegMask != FullLaneMask) {
843 LIS.getVNInfoAllocator(), RegMask, [](LiveInterval::SubRange &SR) {},
844 *LIS.getSlotIndexes(), TRI);
848 extendInterval(LI, RegMask, UseIdx);
856 if (ExtendReg.getLastDef()->getOperand(0).isDead())
857 dbgs() <<
"Clearing dead flag for "
859 ExtendReg.Defs.size() - 1)
862 ExtendReg.getLastDef()->getOperand(0).setIsDead(
false);
870 <<
printReg(LI.
reg(), &TRI, 0, &MRI) <<
" to " << UseIdx
872 LIS.extendToIndices(LI, UseIdx);
876 bool SubRangeExtended =
false;
877 for (LiveInterval::SubRange &SR : LI.
subranges()) {
878 if ((SR.LaneMask & Mask).any() && !SR.liveAt(UseIdx)) {
879 SubRangeExtended =
true;
880 LLVM_DEBUG(
dbgs() <<
"Extending subrange " << SR <<
" of register "
881 <<
printReg(LI.
reg(), &TRI, 0, &MRI) <<
" to " << UseIdx
883 LIS.extendToIndices(SR, UseIdx);
886 if (!SubRangeExtended)
892 LIS.constructMainRangeFromSubranges(LI);
895void Rematerializer::shrinkToUses(
RegisterIdx RegIdx) {
896 Reg &ShrinkReg = Regs[RegIdx];
897 assert(ShrinkReg.isAlive() &&
"register must be alive");
898 if (ShrinkReg.Uses.empty()) {
905 LiveInterval &LI = LIS.getInterval(ShrinkReg.getDefReg());
908 LIS.shrinkToUses(&LI);
911void Rematerializer::shrinkToUsesUnremat(
Register Reg) {
912 LiveInterval &LI = LIS.getInterval(
Reg);
913 LLVM_DEBUG(
dbgs() <<
"Shrinking interval of unrematerializable register "
916 SmallVector<MachineInstr *, 2> DeadDefs;
917 if (!LIS.shrinkToUses(&LI, &DeadDefs)) {
918 assert(DeadDefs.
empty() &&
"expected no dead def");
924 if (DeadDefs.
empty())
930 DeadDefDelegate DeadDefDeleg(*
this);
932 LiveRangeEdit(
nullptr, NewRegs, MF, LIS,
nullptr, &DeadDefDeleg)
933 .eliminateDeadDefs(DeadDefs);
936std::pair<MachineInstr *, MachineInstr *>
939 auto It =
Uses.find(UseRegion);
940 if (It ==
Uses.end())
941 return {
nullptr,
nullptr};
947 SlotIndex FirstIndex = LIS.getInstructionIndex(*FirstMI),
948 LastIndex = FirstIndex;
950 while (++
User != UserEnd) {
952 if (UserIndex < FirstIndex) {
953 FirstIndex = UserIndex;
955 }
else if (UserIndex > LastIndex) {
956 LastIndex = UserIndex;
961 return {FirstMI, LastMI};
968void Rematerializer::Reg::addUsers(
const RegionUsers &NewUsers,
975 assert(RUsers.contains(
MI) &&
"user not in region");
976 if (RUsers.size() == 1)
982bool Rematerializer::Reg::tryEraseUser(MachineInstr *
MI,
unsigned Region) {
983 auto RegionUsers =
Uses.find(Region);
984 if (RegionUsers ==
Uses.end() || !RegionUsers->getSecond().erase(
MI))
986 if (RegionUsers->getSecond().empty())
994 std::function<void(
RegisterIdx,
unsigned)> WalkTree =
999 WalkTree(DepRegIdx,
Depth + 1);
1001 WalkTree(RootIdx, 0);
1006 sort(Regs, [](
const auto &LHS,
const auto &RHS) {
1007 return LHS.second > RHS.second;
1010 OS <<
printID(RootIdx) <<
" has " << Regs.size() - 1 <<
" dependencies\n";
1011 for (
const auto &[RegIdx,
Depth] : Regs) {
1022 OS <<
'(' << RegIdx <<
'/';
1027 OS <<
")[" << PrintReg.
DefRegion <<
"]";
1032 unsigned DefIdx)
const {
1038 if (!PrintReg.
Uses.empty()) {
1039 assert(PrintReg.
isAlive() &&
"dead register cannot have uses");
1044 for (
const auto &[
I, Bounds] :
enumerate(Regions)) {
1045 if (PrintReg.
Uses.contains(
I))
1051 if (LiveIn == Bounds.second) {
1059 std::prev(Bounds.second), Bounds.first);
1060 if (LI.
liveAt(LIS.getInstructionIndex(*LiveIn)) &&
1061 LI.
liveAt(LIS.getInstructionIndex(*LiveOut).getDeadSlot())) {
1062 OS << (
First ?
" - " :
",") <<
I;
1066 OS << (
First ?
" --> " :
" -> ");
1069 auto It = PrintReg.
Uses.begin();
1071 while (++It != PrintReg.
Uses.end())
1072 OS <<
"," << It->first;
1077 assert(DefIdx < PrintReg.
Defs.size() &&
"out-of-bound def");
1079 OS <<
"(def. " << DefIdx + 1 <<
" / " << PrintReg.
Defs.size() <<
") ";
1080 PrintDef.
print(OS,
true,
false,
1083 LIS.getInstructionIndex(PrintDef).print(OS);
1092 OS <<
" User " <<
printUser(
MI, UseRegion) <<
'\n';
1098 std::optional<unsigned> UseRegion)
const {
1101 if (RegIdx !=
NoReg) {
1112 MI->print(OS,
true,
false,
1115 LIS.getInstructionIndex(*MI).print(OS);
1124 Rematerializations[Remater.
getOriginOf(RegIdx)].insert(RegIdx);
1136 auto GetNextValidPosAfterDef =
1139 DefIdx + 1 < Reg.Defs.size() ? Reg.Defs[DefIdx + 1] :
nullptr;
1141 std::next(Reg.Defs[DefIdx]->getIterator());
1143 while (ValidPos != ParentMBB->
end()) {
1149 if (NextDef && &CandMI == NextDef)
1150 return LastValidPos;
1151 if (!isRollbackableMI(CandMI, Remater))
1155 ValidPos = std::next(ValidPos);
1158 LastValidPos = ValidPos;
1167 for (
unsigned I = Reg.Defs.size();
I > 0; --
I)
1168 invalidatePosition(Reg.Defs[
I - 1], GetNextValidPosAfterDef(
I - 1));
1174 DeadRegs.push_back(DeadReg(RegIdx, Remater));
1175 for (
unsigned I = Reg.Defs.size();
I > 0; --
I) {
1176 const InsertBeforePos InsertPos =
1177 makePos(GetNextValidPosAfterDef(
I - 1), ParentMBB);
1178 PosToIdx[InsertPos].insert(Positions.size());
1179 Positions.push_back(InsertPos);
1192 while (ValidPos != ParentMBB->
end() && isRollbackableMI(*ValidPos, Remater))
1193 ValidPos = std::next(ValidPos);
1194 invalidatePosition(&
MI, ValidPos);
1204 unsigned PositionIndex = Positions.
size();
1210 for (
const DeadReg &Reg :
reverse(DeadRegs)) {
1214 Rematerializations.erase(Reg.Idx);
1215 PositionIndex -= Reg.Defs.size();
1222 for (
unsigned I = 0, E = Reg.Defs.size();
I < E; ++
I) {
1223 InsertBeforePos Pos = Positions[--PositionIndex];
1233 Remater.
recreateReg(Reg.Idx, InsertPositions, Reg.DefReg);
1236 for (
const auto [OldDef, NewDef] :
zip_equal(Reg.Defs, RecreateReg.
Defs)) {
1237 assert(!Replacements.
contains(OldDef) &&
"duplicate deleted MI");
1238 Replacements[OldDef] = NewDef;
1243 for (
const auto &[RegIdx,
RematsOf] : Rematerializations) {
1257 Rematerializations.clear();
1258 RollingBack =
false;
1271 if (
RematsOf == Rematerializations.end())
1278 const InsertBeforePos MIPos = InsertBeforePos(
MI),
1279 NewPos = makePos(It,
MI->getParent());
1280 auto MIIndices = PosToIdx.find(MIPos);
1281 if (MIIndices == PosToIdx.end())
1284 assert(!InvalIndices.
empty() &&
"no index hold position");
1285 for (
unsigned I : InvalIndices)
1286 Positions[
I] = NewPos;
1287 PosToIdx.try_emplace(NewPos).first->getSecond().insert_range(InvalIndices);
1288 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