Skip to content

Commit 9190ad1

Browse files
committed
PHIElimination: add target hook to control reuse.
Add PHI reuse hook so that a target can decide if temporary register during PHI node elimination can be reused. By default, two phis are allowed to use the same temporary register if their right-hand-sides are the same. However, the left-hand sides of phis need to be checked as well for some targets, such as a GPU target. If the register banks of phis's left-hand sides are diffrerent, reuse is not allowed. Thus, this change adds a target hook for this kind of checking as it is target-dependent. This change has no functional change. This is to resolve the issue: #163500
1 parent b9ea93c commit 9190ad1

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

llvm/include/llvm/CodeGen/TargetInstrInfo.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2169,7 +2169,7 @@ class LLVM_ABI TargetInstrInfo : public MCInstrInfo {
21692169
return TargetOpcode::COPY;
21702170
}
21712171

2172-
/// During PHI eleimination lets target to make necessary checks and
2172+
/// During PHI elimination lets target to make necessary checks and
21732173
/// insert the copy to the PHI destination register in a target specific
21742174
/// manner.
21752175
virtual MachineInstr *createPHIDestinationCopy(
@@ -2179,7 +2179,7 @@ class LLVM_ABI TargetInstrInfo : public MCInstrInfo {
21792179
.addReg(Src);
21802180
}
21812181

2182-
/// During PHI eleimination lets target to make necessary checks and
2182+
/// During PHI elimination lets target to make necessary checks and
21832183
/// insert the copy to the PHI destination register in a target specific
21842184
/// manner.
21852185
virtual MachineInstr *createPHISourceCopy(MachineBasicBlock &MBB,
@@ -2191,6 +2191,17 @@ class LLVM_ABI TargetInstrInfo : public MCInstrInfo {
21912191
.addReg(Src, 0, SrcSubReg);
21922192
}
21932193

2194+
/// During PHI elimination lets target to decide if two phis can use the
2195+
/// same register \p Reg when they have the same rhs. Register \p Reg has
2196+
/// been used for the first phi and \p PHIReg is the DestReg of the second
2197+
/// Phi. This function is to check if the second phi can reuse \p Reg as
2198+
/// its temporary register.
2199+
/// The default is to allow reuse.
2200+
virtual bool allowPHIReuse(Register Reg, Register PHIReg,
2201+
const MachineFunction &MF) const {
2202+
return true;
2203+
}
2204+
21942205
/// Returns a \p outliner::OutlinedFunction struct containing target-specific
21952206
/// information for a set of outlining candidates. Returns std::nullopt if the
21962207
/// candidates are not suitable for outlining. \p MinRepeats is the minimum

llvm/lib/CodeGen/PHIElimination.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ void PHIEliminationImpl::LowerPHINode(MachineBasicBlock &MBB,
380380
Register *Entry = nullptr;
381381
if (AllEdgesCritical)
382382
Entry = &LoweredPHIs[MPhi];
383-
if (Entry && *Entry) {
383+
if (Entry && *Entry && TII->allowPHIReuse(*Entry, DestReg, MF)) {
384384
// An identical PHI node was already lowered. Reuse the incoming register.
385385
IncomingReg = *Entry;
386386
reusedIncoming = true;

0 commit comments

Comments
 (0)