Skip to content

Commit 9ddbb61

Browse files
committed
[CodeGen] NFC: Move isDead to MachineInstr
1 parent 1eaa179 commit 9ddbb61

File tree

3 files changed

+47
-45
lines changed

3 files changed

+47
-45
lines changed

llvm/include/llvm/CodeGen/MachineInstr.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class AAResults;
4545
template <typename T> class ArrayRef;
4646
class DIExpression;
4747
class DILocalVariable;
48+
class LiveRegUnits;
4849
class MachineBasicBlock;
4950
class MachineFunction;
5051
class MachineRegisterInfo;
@@ -1786,6 +1787,13 @@ class MachineInstr
17861787
/// Return true if all the implicit defs of this instruction are dead.
17871788
bool allImplicitDefsAreDead() const;
17881789

1790+
/// Check whether an MI is dead. If \p LivePhysRegs is provided, it is assumed
1791+
/// to be at the position of MI and will be used to check the Liveness of
1792+
/// physical register defs. If \p LivePhysRegs is not provided, this will
1793+
/// pessimistically assume any PhysReg def is live.
1794+
bool isDead(const MachineRegisterInfo *MRI,
1795+
LiveRegUnits *LivePhysRegs = nullptr) const;
1796+
17891797
/// Return a valid size if the instruction is a spill instruction.
17901798
std::optional<LocationSize> getSpillSize(const TargetInstrInfo *TII) const;
17911799

llvm/lib/CodeGen/DeadMachineInstructionElim.cpp

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ class DeadMachineInstructionElimImpl {
3838
bool runImpl(MachineFunction &MF);
3939

4040
private:
41-
bool isDead(const MachineInstr *MI) const;
4241
bool eliminateDeadMI(MachineFunction &MF);
4342
};
4443

@@ -79,47 +78,6 @@ char &llvm::DeadMachineInstructionElimID = DeadMachineInstructionElim::ID;
7978
INITIALIZE_PASS(DeadMachineInstructionElim, DEBUG_TYPE,
8079
"Remove dead machine instructions", false, false)
8180

82-
bool DeadMachineInstructionElimImpl::isDead(const MachineInstr *MI) const {
83-
// Instructions without side-effects are dead iff they only define dead regs.
84-
// This function is hot and this loop returns early in the common case,
85-
// so only perform additional checks before this if absolutely necessary.
86-
for (const MachineOperand &MO : MI->all_defs()) {
87-
Register Reg = MO.getReg();
88-
if (Reg.isPhysical()) {
89-
// Don't delete live physreg defs, or any reserved register defs.
90-
if (!LivePhysRegs.available(Reg) || MRI->isReserved(Reg))
91-
return false;
92-
} else {
93-
if (MO.isDead()) {
94-
#ifndef NDEBUG
95-
// Basic check on the register. All of them should be 'undef'.
96-
for (auto &U : MRI->use_nodbg_operands(Reg))
97-
assert(U.isUndef() && "'Undef' use on a 'dead' register is found!");
98-
#endif
99-
continue;
100-
}
101-
for (const MachineInstr &Use : MRI->use_nodbg_instructions(Reg)) {
102-
if (&Use != MI)
103-
// This def has a non-debug use. Don't delete the instruction!
104-
return false;
105-
}
106-
}
107-
}
108-
109-
// Technically speaking inline asm without side effects and no defs can still
110-
// be deleted. But there is so much bad inline asm code out there, we should
111-
// let them be.
112-
if (MI->isInlineAsm())
113-
return false;
114-
115-
// FIXME: See issue #105950 for why LIFETIME markers are considered dead here.
116-
if (MI->isLifetimeMarker())
117-
return true;
118-
119-
// If there are no defs with uses, the instruction might be dead.
120-
return MI->wouldBeTriviallyDead();
121-
}
122-
12381
bool DeadMachineInstructionElimImpl::runImpl(MachineFunction &MF) {
12482
MRI = &MF.getRegInfo();
12583

@@ -146,7 +104,7 @@ bool DeadMachineInstructionElimImpl::eliminateDeadMI(MachineFunction &MF) {
146104
// liveness as we go.
147105
for (MachineInstr &MI : make_early_inc_range(reverse(*MBB))) {
148106
// If the instruction is dead, delete it!
149-
if (isDead(&MI)) {
107+
if (MI.isDead(MRI, &LivePhysRegs)) {
150108
LLVM_DEBUG(dbgs() << "DeadMachineInstructionElim: DELETING: " << MI);
151109
// It is possible that some DBG_VALUE instructions refer to this
152110
// instruction. They will be deleted in the live debug variable
@@ -156,11 +114,9 @@ bool DeadMachineInstructionElimImpl::eliminateDeadMI(MachineFunction &MF) {
156114
++NumDeletes;
157115
continue;
158116
}
159-
160117
LivePhysRegs.stepBackward(MI);
161118
}
162119
}
163-
164120
LivePhysRegs.clear();
165121
return AnyChanges;
166122
}

llvm/lib/CodeGen/MachineInstr.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "llvm/ADT/SmallVector.h"
1919
#include "llvm/Analysis/AliasAnalysis.h"
2020
#include "llvm/Analysis/MemoryLocation.h"
21+
#include "llvm/CodeGen/LiveRegUnits.h"
2122
#include "llvm/CodeGen/MachineBasicBlock.h"
2223
#include "llvm/CodeGen/MachineFrameInfo.h"
2324
#include "llvm/CodeGen/MachineFunction.h"
@@ -1592,6 +1593,43 @@ bool MachineInstr::allImplicitDefsAreDead() const {
15921593
return true;
15931594
}
15941595

1596+
bool MachineInstr::isDead(const MachineRegisterInfo *MRI,
1597+
LiveRegUnits *LivePhysRegs) const {
1598+
// Instructions without side-effects are dead iff they only define dead regs.
1599+
// This function is hot and this loop returns early in the common case,
1600+
// so only perform additional checks before this if absolutely necessary.
1601+
for (const MachineOperand &MO : all_defs()) {
1602+
Register Reg = MO.getReg();
1603+
if (Reg.isPhysical()) {
1604+
// Don't delete live physreg defs, or any reserved register defs.
1605+
if (!LivePhysRegs || !LivePhysRegs->available(Reg) ||
1606+
MRI->isReserved(Reg))
1607+
return false;
1608+
} else {
1609+
if (MO.isDead())
1610+
continue;
1611+
for (const MachineInstr &Use : MRI->use_nodbg_instructions(Reg)) {
1612+
if (&Use != this)
1613+
// This def has a non-debug use. Don't delete the instruction!
1614+
return false;
1615+
}
1616+
}
1617+
}
1618+
1619+
// Technically speaking inline asm without side effects and no defs can still
1620+
// be deleted. But there is so much bad inline asm code out there, we should
1621+
// let them be.
1622+
if (isInlineAsm())
1623+
return false;
1624+
1625+
// FIXME: See issue #105950 for why LIFETIME markers are considered dead here.
1626+
if (isLifetimeMarker())
1627+
return true;
1628+
1629+
// If there are no defs with uses, the instruction might be dead.
1630+
return wouldBeTriviallyDead();
1631+
}
1632+
15951633
/// copyImplicitOps - Copy implicit register operands from specified
15961634
/// instruction to this instruction.
15971635
void MachineInstr::copyImplicitOps(MachineFunction &MF,

0 commit comments

Comments
 (0)