Skip to content

Commit c90a833

Browse files
committed
[TII] Split isTrivialReMaterializable into two versions [nfc]
This change builds on llvm#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.
1 parent 8b7a76a commit c90a833

File tree

11 files changed

+31
-48
lines changed

11 files changed

+31
-48
lines changed

llvm/include/llvm/CodeGen/TargetInstrInfo.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,19 @@ 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) ||
173186
(MI.getDesc().isRematerializable() &&
@@ -194,10 +207,9 @@ class LLVM_ABI TargetInstrInfo : public MCInstrInfo {
194207
protected:
195208
/// For instructions with opcodes for which the M_REMATERIALIZABLE flag is
196209
/// set, this hook lets the target specify whether the instruction is actually
197-
/// trivially rematerializable, taking into consideration its operands. This
210+
/// rematerializable, taking into consideration its operands. This
198211
/// 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.
212+
/// than producing a value.
201213
virtual bool isReMaterializableImpl(const MachineInstr &MI) const;
202214

203215
/// 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 & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,7 @@ llvm::calculateRegAllocScore(const MachineFunction &MF,
8080
},
8181
[&](const MachineInstr &MI) {
8282
auto *TTI = MF.getSubtarget().getInstrInfo();
83-
return TTI->isTriviallyReMaterializable(MI) &&
84-
llvm::all_of(MI.all_uses(), [](const MachineOperand &MO) {
85-
return MO.getReg().isVirtual();
86-
});
83+
return TTI->isTriviallyReMaterializable(MI);
8784
});
8885
}
8986

llvm/lib/CodeGen/RegisterCoalescer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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))

llvm/lib/CodeGen/TargetRegisterInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +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) &&
70+
if (MI && TII->isReMaterializable(*MI) &&
7171
VirtReg.size() > HugeSizeForSplit)
7272
return false;
7373
return true;

llvm/lib/Target/AMDGPU/GCNSchedStrategy.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,7 +1779,7 @@ bool PreRARematStage::canIncreaseOccupancyOrReduceSpill() {
17791779
for (auto MI = Region.first; MI != Region.second; ++MI) {
17801780
// The instruction must be trivially 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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ class PreRARematStage : public GCNSchedStage {
485485

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

490490
/// Rematerializes all instructions in PreRARematStage::Rematerializations
491491
/// and stores the achieved occupancy after remat in

0 commit comments

Comments
 (0)