Skip to content

Commit 6ae6583

Browse files
authored
[CodeGen] Finish untangling LRE::scanRemattable [nfc] (#161963)
This is an attempt to simplify the rematerialization logic in InlineSpiller and SplitKit. I'd earlier done the same for RegisterCoalescer in 57b673. The basic idea of this change is that we don't need to check whether an instruction is rematerializable early. Instead, we can defer the check to the point where we're actually trying to materialize something. We also don't need to indirect that query through a VNI key, and can instead just check the instruction directly at the use site.
1 parent 0797bb7 commit 6ae6583

File tree

4 files changed

+21
-58
lines changed

4 files changed

+21
-58
lines changed

llvm/include/llvm/CodeGen/LiveRangeEdit.h

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,24 +75,14 @@ class LiveRangeEdit : private MachineRegisterInfo::Delegate {
7575
/// FirstNew - Index of the first register added to NewRegs.
7676
const unsigned FirstNew;
7777

78-
/// ScannedRemattable - true when remattable values have been identified.
79-
bool ScannedRemattable = false;
80-
8178
/// DeadRemats - The saved instructions which have already been dead after
8279
/// rematerialization but not deleted yet -- to be done in postOptimization.
8380
SmallPtrSet<MachineInstr *, 32> *DeadRemats;
8481

85-
/// Remattable - Values defined by remattable instructions as identified by
86-
/// tii.isTriviallyReMaterializable().
87-
SmallPtrSet<const VNInfo *, 4> Remattable;
88-
8982
/// Rematted - Values that were actually rematted, and so need to have their
9083
/// live range trimmed or entirely removed.
9184
SmallPtrSet<const VNInfo *, 4> Rematted;
9285

93-
/// scanRemattable - Identify the Parent values that may rematerialize.
94-
void scanRemattable();
95-
9686
/// foldAsLoad - If LI has a single use and a single def that can be folded as
9787
/// a load, eliminate the register by folding the def into the use.
9888
bool foldAsLoad(LiveInterval *LI, SmallVectorImpl<MachineInstr *> &Dead);
@@ -175,11 +165,6 @@ class LiveRangeEdit : private MachineRegisterInfo::Delegate {
175165

176166
Register create() { return createFrom(getReg()); }
177167

178-
/// anyRematerializable - Return true if any parent values may be
179-
/// rematerializable. This function must be called before
180-
/// canRematerializeAt is called..
181-
bool anyRematerializable();
182-
183168
/// Remat - Information needed to rematerialize at a specific location.
184169
struct Remat {
185170
const VNInfo *const ParentVNI; // parent_'s value at the remat location.
@@ -189,9 +174,9 @@ class LiveRangeEdit : private MachineRegisterInfo::Delegate {
189174
explicit Remat(const VNInfo *ParentVNI) : ParentVNI(ParentVNI) {}
190175
};
191176

192-
/// canRematerializeAt - Determine if ParentVNI can be rematerialized at
177+
/// canRematerializeAt - Determine if RM.Orig can be rematerialized at
193178
/// UseIdx. It is assumed that parent_.getVNINfoAt(UseIdx) == ParentVNI.
194-
bool canRematerializeAt(Remat &RM, VNInfo *OrigVNI, SlotIndex UseIdx);
179+
bool canRematerializeAt(Remat &RM, SlotIndex UseIdx);
195180

196181
/// rematerializeAt - Rematerialize RM.ParentVNI into DestReg by inserting an
197182
/// instruction into MBB before MI. The new instruction is mapped, but

llvm/lib/CodeGen/InlineSpiller.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -671,10 +671,22 @@ bool InlineSpiller::reMaterializeFor(LiveInterval &VirtReg, MachineInstr &MI) {
671671

672672
LiveInterval &OrigLI = LIS.getInterval(Original);
673673
VNInfo *OrigVNI = OrigLI.getVNInfoAt(UseIdx);
674-
LiveRangeEdit::Remat RM(ParentVNI);
675-
RM.OrigMI = LIS.getInstructionFromIndex(OrigVNI->def);
674+
assert(OrigVNI && "corrupted sub-interval");
675+
MachineInstr *DefMI = LIS.getInstructionFromIndex(OrigVNI->def);
676+
// This can happen if for two reasons: 1) This could be a phi valno,
677+
// or 2) the remat def has already been removed from the original
678+
// live interval; this happens if we rematted to all uses, and
679+
// then further split one of those live ranges.
680+
if (!DefMI) {
681+
markValueUsed(&VirtReg, ParentVNI);
682+
LLVM_DEBUG(dbgs() << "\tcannot remat missing def for " << UseIdx << '\t'
683+
<< MI);
684+
return false;
685+
}
676686

677-
if (!Edit->canRematerializeAt(RM, OrigVNI, UseIdx)) {
687+
LiveRangeEdit::Remat RM(ParentVNI);
688+
RM.OrigMI = DefMI;
689+
if (!Edit->canRematerializeAt(RM, UseIdx)) {
678690
markValueUsed(&VirtReg, ParentVNI);
679691
LLVM_DEBUG(dbgs() << "\tcannot remat for " << UseIdx << '\t' << MI);
680692
return false;
@@ -739,9 +751,6 @@ bool InlineSpiller::reMaterializeFor(LiveInterval &VirtReg, MachineInstr &MI) {
739751
/// reMaterializeAll - Try to rematerialize as many uses as possible,
740752
/// and trim the live ranges after.
741753
void InlineSpiller::reMaterializeAll() {
742-
if (!Edit->anyRematerializable())
743-
return;
744-
745754
UsedValues.clear();
746755

747756
// Try to remat before all uses of snippets.

llvm/lib/CodeGen/LiveRangeEdit.cpp

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -68,41 +68,12 @@ Register LiveRangeEdit::createFrom(Register OldReg) {
6868
return VReg;
6969
}
7070

71-
void LiveRangeEdit::scanRemattable() {
72-
for (VNInfo *VNI : getParent().valnos) {
73-
if (VNI->isUnused())
74-
continue;
75-
Register Original = VRM->getOriginal(getReg());
76-
LiveInterval &OrigLI = LIS.getInterval(Original);
77-
VNInfo *OrigVNI = OrigLI.getVNInfoAt(VNI->def);
78-
if (!OrigVNI)
79-
continue;
80-
MachineInstr *DefMI = LIS.getInstructionFromIndex(OrigVNI->def);
81-
if (!DefMI)
82-
continue;
83-
if (TII.isReMaterializable(*DefMI))
84-
Remattable.insert(OrigVNI);
85-
}
86-
ScannedRemattable = true;
87-
}
88-
89-
bool LiveRangeEdit::anyRematerializable() {
90-
if (!ScannedRemattable)
91-
scanRemattable();
92-
return !Remattable.empty();
93-
}
94-
95-
bool LiveRangeEdit::canRematerializeAt(Remat &RM, VNInfo *OrigVNI,
96-
SlotIndex UseIdx) {
97-
assert(ScannedRemattable && "Call anyRematerializable first");
71+
bool LiveRangeEdit::canRematerializeAt(Remat &RM, SlotIndex UseIdx) {
72+
assert(RM.OrigMI && "No defining instruction for remattable value");
9873

99-
// Use scanRemattable info.
100-
if (!Remattable.count(OrigVNI))
74+
if (!TII.isReMaterializable(*RM.OrigMI))
10175
return false;
10276

103-
// No defining instruction provided.
104-
assert(RM.OrigMI && "No defining instruction for remattable value");
105-
10677
// Verify that all used registers are available with the same values.
10778
if (!VirtRegAuxInfo::allUsesAvailableAt(RM.OrigMI, UseIdx, LIS, MRI, TII))
10879
return false;

llvm/lib/CodeGen/SplitKit.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,6 @@ void SplitEditor::reset(LiveRangeEdit &LRE, ComplementSpillMode SM) {
376376
if (SpillMode)
377377
LICalc[1].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
378378
&LIS.getVNInfoAllocator());
379-
380-
Edit->anyRematerializable();
381379
}
382380

383381
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
@@ -638,7 +636,7 @@ VNInfo *SplitEditor::defFromParent(unsigned RegIdx, const VNInfo *ParentVNI,
638636
LiveRangeEdit::Remat RM(ParentVNI);
639637
RM.OrigMI = LIS.getInstructionFromIndex(OrigVNI->def);
640638
if (RM.OrigMI && TII.isAsCheapAsAMove(*RM.OrigMI) &&
641-
Edit->canRematerializeAt(RM, OrigVNI, UseIdx)) {
639+
Edit->canRematerializeAt(RM, UseIdx)) {
642640
if (!rematWillIncreaseRestriction(RM.OrigMI, MBB, UseIdx)) {
643641
SlotIndex Def = Edit->rematerializeAt(MBB, I, Reg, RM, TRI, Late);
644642
++NumRemats;

0 commit comments

Comments
 (0)