Skip to content

Commit ea721e2

Browse files
preameslukel97
andauthored
[TII] Split isTrivialReMaterializable into two versions [nfc] (#160377)
This change builds on #160319 which tries to clarify which *callers* (not backends) assume that the result is actually trivial. This change itself should be NFC. Essentially, I'm just renaming the existing isTrivialRematerializable to the non-trivial version and then adding a new trivial version (with the same name as the prior function) and simplifying a few callers which want that semantic. This change does *not* enable non-trivial remat any more broadly than was already done for our targets which were lying through the old APIs; that will come separately. The goal here is simply to make the code easier to follow in terms of what assumptions are being made where. --------- Co-authored-by: Luke Lau <[email protected]>
1 parent 151a80b commit ea721e2

File tree

11 files changed

+47
-66
lines changed

11 files changed

+47
-66
lines changed

llvm/include/llvm/CodeGen/TargetInstrInfo.h

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,22 @@ class LLVM_ABI TargetInstrInfo : public MCInstrInfo {
168168
/// registers so that the instructions result is independent of the place
169169
/// in the function.
170170
bool isTriviallyReMaterializable(const MachineInstr &MI) const {
171+
if (!isReMaterializable(MI))
172+
return false;
173+
for (const MachineOperand &MO : MI.all_uses()) {
174+
if (MO.getReg().isVirtual())
175+
return false;
176+
}
177+
return true;
178+
}
179+
180+
/// Return true if the instruction would be materializable at a point
181+
/// in the containing function where all virtual register uses were
182+
/// known to be live and available in registers.
183+
bool isReMaterializable(const MachineInstr &MI) const {
171184
return (MI.getOpcode() == TargetOpcode::IMPLICIT_DEF &&
172185
MI.getNumOperands() == 1) ||
173-
(MI.getDesc().isRematerializable() &&
174-
isReMaterializableImpl(MI));
186+
(MI.getDesc().isRematerializable() && isReMaterializableImpl(MI));
175187
}
176188

177189
/// Given \p MO is a PhysReg use return if it can be ignored for the purpose
@@ -194,10 +206,9 @@ class LLVM_ABI TargetInstrInfo : public MCInstrInfo {
194206
protected:
195207
/// For instructions with opcodes for which the M_REMATERIALIZABLE flag is
196208
/// set, this hook lets the target specify whether the instruction is actually
197-
/// trivially rematerializable, taking into consideration its operands. This
209+
/// rematerializable, taking into consideration its operands. This
198210
/// predicate must return false if the instruction has any side effects other
199-
/// than producing a value, or if it requres any address registers that are
200-
/// not always available.
211+
/// than producing a value.
201212
virtual bool isReMaterializableImpl(const MachineInstr &MI) const;
202213

203214
/// This method commutes the operands of the given machine instruction MI.

llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2212,11 +2212,7 @@ findPrologueEndLoc(const MachineFunction *MF) {
22122212
-> std::optional<std::pair<const MachineInstr *, bool>> {
22132213
// Is this instruction trivial data shuffling or frame-setup?
22142214
bool isCopy = (TII.isCopyInstr(MI) ? true : false);
2215-
bool isTrivRemat =
2216-
TII.isTriviallyReMaterializable(MI) &&
2217-
llvm::all_of(MI.all_uses(), [](const MachineOperand &MO) {
2218-
return MO.getReg().isVirtual();
2219-
});
2215+
bool isTrivRemat = TII.isTriviallyReMaterializable(MI);
22202216
bool isFrameSetup = MI.getFlag(MachineInstr::FrameSetup);
22212217

22222218
if (!isFrameSetup && MI.getDebugLoc()) {

llvm/lib/CodeGen/CalcSpillWeights.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ bool VirtRegAuxInfo::isRematerializable(const LiveInterval &LI,
122122
assert(MI && "Dead valno in interval");
123123
}
124124

125-
if (!TII.isTriviallyReMaterializable(*MI))
125+
if (!TII.isReMaterializable(*MI))
126126
return false;
127127
}
128128
return true;

llvm/lib/CodeGen/LiveRangeEdit.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void LiveRangeEdit::scanRemattable() {
8080
MachineInstr *DefMI = LIS.getInstructionFromIndex(OrigVNI->def);
8181
if (!DefMI)
8282
continue;
83-
if (TII.isTriviallyReMaterializable(*DefMI))
83+
if (TII.isReMaterializable(*DefMI))
8484
Remattable.insert(OrigVNI);
8585
}
8686
ScannedRemattable = true;
@@ -387,7 +387,7 @@ void LiveRangeEdit::eliminateDeadDef(MachineInstr *MI, ToShrinkSet &ToShrink) {
387387
// register uses. That may provoke RA to split an interval at the KILL
388388
// and later result in an invalid live segment end.
389389
if (isOrigDef && DeadRemats && !HasLiveVRegUses &&
390-
TII.isTriviallyReMaterializable(*MI)) {
390+
TII.isReMaterializable(*MI)) {
391391
LiveInterval &NewLI = createEmptyIntervalFrom(Dest, false);
392392
VNInfo::Allocator &Alloc = LIS.getVNInfoAllocator();
393393
VNInfo *VNI = NewLI.getNextValue(Idx, Alloc);

llvm/lib/CodeGen/MachineLICM.cpp

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,6 @@ namespace {
244244

245245
bool IsGuaranteedToExecute(MachineBasicBlock *BB, MachineLoop *CurLoop);
246246

247-
bool isTriviallyReMaterializable(const MachineInstr &MI) const;
248-
249247
void EnterScope(MachineBasicBlock *MBB);
250248

251249
void ExitScope(MachineBasicBlock *MBB);
@@ -771,23 +769,6 @@ bool MachineLICMImpl::IsGuaranteedToExecute(MachineBasicBlock *BB,
771769
return true;
772770
}
773771

774-
/// Check if \p MI is trivially remateralizable and if it does not have any
775-
/// virtual register uses. Even though rematerializable RA might not actually
776-
/// rematerialize it in this scenario. In that case we do not want to hoist such
777-
/// instruction out of the loop in a belief RA will sink it back if needed.
778-
bool MachineLICMImpl::isTriviallyReMaterializable(
779-
const MachineInstr &MI) const {
780-
if (!TII->isTriviallyReMaterializable(MI))
781-
return false;
782-
783-
for (const MachineOperand &MO : MI.all_uses()) {
784-
if (MO.getReg().isVirtual())
785-
return false;
786-
}
787-
788-
return true;
789-
}
790-
791772
void MachineLICMImpl::EnterScope(MachineBasicBlock *MBB) {
792773
LLVM_DEBUG(dbgs() << "Entering " << printMBBReference(*MBB) << '\n');
793774

@@ -1300,9 +1281,9 @@ bool MachineLICMImpl::IsProfitableToHoist(MachineInstr &MI,
13001281
return false;
13011282
}
13021283

1303-
// Rematerializable instructions should always be hoisted providing the
1304-
// register allocator can just pull them down again when needed.
1305-
if (isTriviallyReMaterializable(MI))
1284+
// Trivially rematerializable instructions should always be hoisted
1285+
// providing the register allocator can just pull them down again when needed.
1286+
if (TII->isTriviallyReMaterializable(MI))
13061287
return true;
13071288

13081289
// FIXME: If there are long latency loop-invariant instructions inside the
@@ -1386,7 +1367,7 @@ bool MachineLICMImpl::IsProfitableToHoist(MachineInstr &MI,
13861367

13871368
// High register pressure situation, only hoist if the instruction is going
13881369
// to be remat'ed.
1389-
if (!isTriviallyReMaterializable(MI) &&
1370+
if (!TII->isTriviallyReMaterializable(MI) &&
13901371
!MI.isDereferenceableInvariantLoad()) {
13911372
LLVM_DEBUG(dbgs() << "Can't remat / high reg-pressure: " << MI);
13921373
return false;

llvm/lib/CodeGen/RegAllocScore.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ llvm::calculateRegAllocScore(const MachineFunction &MF,
7979
return MBFI.getBlockFreqRelativeToEntryBlock(&MBB);
8080
},
8181
[&](const MachineInstr &MI) {
82-
return MF.getSubtarget().getInstrInfo()->isTriviallyReMaterializable(
83-
MI);
82+
return MF.getSubtarget().getInstrInfo()->isReMaterializable(MI);
8483
});
8584
}
8685

llvm/lib/CodeGen/RegisterCoalescer.cpp

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -294,10 +294,10 @@ class RegisterCoalescer : private LiveRangeEdit::Delegate {
294294
/// We found a copy which can be moved to its less frequent predecessor.
295295
bool removePartialRedundancy(const CoalescerPair &CP, MachineInstr &CopyMI);
296296

297-
/// If the source of a copy is defined by a
298-
/// trivial computation, replace the copy by rematerialize the definition.
299-
bool reMaterializeTrivialDef(const CoalescerPair &CP, MachineInstr *CopyMI,
300-
bool &IsDefCopy);
297+
/// If the source of a copy is defined by a CheapAsAMove computation,
298+
/// replace the copy by rematerialize the definition.
299+
bool reMaterializeDef(const CoalescerPair &CP, MachineInstr *CopyMI,
300+
bool &IsDefCopy);
301301

302302
/// Return true if a copy involving a physreg should be joined.
303303
bool canJoinPhys(const CoalescerPair &CP);
@@ -1297,9 +1297,9 @@ static bool definesFullReg(const MachineInstr &MI, Register Reg) {
12971297
return false;
12981298
}
12991299

1300-
bool RegisterCoalescer::reMaterializeTrivialDef(const CoalescerPair &CP,
1301-
MachineInstr *CopyMI,
1302-
bool &IsDefCopy) {
1300+
bool RegisterCoalescer::reMaterializeDef(const CoalescerPair &CP,
1301+
MachineInstr *CopyMI,
1302+
bool &IsDefCopy) {
13031303
IsDefCopy = false;
13041304
Register SrcReg = CP.isFlipped() ? CP.getDstReg() : CP.getSrcReg();
13051305
unsigned SrcIdx = CP.isFlipped() ? CP.getDstIdx() : CP.getSrcIdx();
@@ -1325,7 +1325,7 @@ bool RegisterCoalescer::reMaterializeTrivialDef(const CoalescerPair &CP,
13251325
if (!TII->isAsCheapAsAMove(*DefMI))
13261326
return false;
13271327

1328-
if (!TII->isTriviallyReMaterializable(*DefMI))
1328+
if (!TII->isReMaterializable(*DefMI))
13291329
return false;
13301330

13311331
if (!definesFullReg(*DefMI, SrcReg))
@@ -2128,10 +2128,10 @@ bool RegisterCoalescer::joinCopy(
21282128
<< printReg(CP.getSrcReg(), TRI) << " with "
21292129
<< printReg(CP.getDstReg(), TRI, CP.getSrcIdx()) << '\n');
21302130
if (!canJoinPhys(CP)) {
2131-
// Before giving up coalescing, if definition of source is defined by
2132-
// trivial computation, try rematerializing it.
2131+
// Before giving up coalescing, try rematerializing the source of
2132+
// the copy instead if it is cheap.
21332133
bool IsDefCopy = false;
2134-
if (reMaterializeTrivialDef(CP, CopyMI, IsDefCopy))
2134+
if (reMaterializeDef(CP, CopyMI, IsDefCopy))
21352135
return true;
21362136
if (IsDefCopy)
21372137
Again = true; // May be possible to coalesce later.
@@ -2167,10 +2167,9 @@ bool RegisterCoalescer::joinCopy(
21672167
if (!joinIntervals(CP)) {
21682168
// Coalescing failed.
21692169

2170-
// If definition of source is defined by trivial computation, try
2171-
// rematerializing it.
2170+
// Try rematerializing the definition of the source if it is cheap.
21722171
bool IsDefCopy = false;
2173-
if (reMaterializeTrivialDef(CP, CopyMI, IsDefCopy))
2172+
if (reMaterializeDef(CP, CopyMI, IsDefCopy))
21742173
return true;
21752174

21762175
// If we can eliminate the copy without merging the live segments, do so

llvm/lib/CodeGen/TargetRegisterInfo.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ bool TargetRegisterInfo::shouldRegionSplitForVirtReg(
6767
const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
6868
const MachineRegisterInfo &MRI = MF.getRegInfo();
6969
MachineInstr *MI = MRI.getUniqueVRegDef(VirtReg.reg());
70-
if (MI && TII->isTriviallyReMaterializable(*MI) &&
71-
VirtReg.size() > HugeSizeForSplit)
70+
if (MI && TII->isReMaterializable(*MI) && VirtReg.size() > HugeSizeForSplit)
7271
return false;
7372
return true;
7473
}

llvm/lib/Target/AMDGPU/GCNSchedStrategy.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1777,9 +1777,9 @@ bool PreRARematStage::canIncreaseOccupancyOrReduceSpill() {
17771777
for (unsigned I = 0, E = DAG.Regions.size(); I != E; ++I) {
17781778
auto Region = DAG.Regions[I];
17791779
for (auto MI = Region.first; MI != Region.second; ++MI) {
1780-
// The instruction must be trivially rematerializable.
1780+
// The instruction must be rematerializable.
17811781
MachineInstr &DefMI = *MI;
1782-
if (!isTriviallyReMaterializable(DefMI))
1782+
if (!isReMaterializable(DefMI))
17831783
continue;
17841784

17851785
// We only support rematerializing virtual registers with one definition.
@@ -2002,8 +2002,8 @@ void PreRARematStage::rematerialize() {
20022002
}
20032003

20042004
// Copied from MachineLICM
2005-
bool PreRARematStage::isTriviallyReMaterializable(const MachineInstr &MI) {
2006-
if (!DAG.TII->isTriviallyReMaterializable(MI))
2005+
bool PreRARematStage::isReMaterializable(const MachineInstr &MI) {
2006+
if (!DAG.TII->isReMaterializable(MI))
20072007
return false;
20082008

20092009
for (const MachineOperand &MO : MI.all_uses()) {

llvm/lib/Target/AMDGPU/GCNSchedStrategy.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ class ClusteredLowOccStage : public GCNSchedStage {
433433

434434
/// Attempts to reduce function spilling or, if there is no spilling, to
435435
/// increase function occupancy by one with respect to ArchVGPR usage by sinking
436-
/// trivially rematerializable instructions to their use. When the stage
436+
/// rematerializable instructions to their use. When the stage
437437
/// estimates reducing spilling or increasing occupancy is possible, as few
438438
/// instructions as possible are rematerialized to reduce potential negative
439439
/// effects on function latency.
@@ -483,9 +483,8 @@ class PreRARematStage : public GCNSchedStage {
483483
/// PreRARematStage::TargetOccupancy.
484484
bool canIncreaseOccupancyOrReduceSpill();
485485

486-
/// Whether the MI is trivially rematerializable and does not have any virtual
487-
/// register use.
488-
bool isTriviallyReMaterializable(const MachineInstr &MI);
486+
/// Whether the MI is rematerializable
487+
bool isReMaterializable(const MachineInstr &MI);
489488

490489
/// Rematerializes all instructions in PreRARematStage::Rematerializations
491490
/// and stores the achieved occupancy after remat in

0 commit comments

Comments
 (0)