Skip to content

Commit f2942b9

Browse files
authored
[CodeGen] NFC: Move isDead to MachineInstr (#123531)
Provide isDead interface for access to ad-hoc isDead queries. LivePhysRegs is optional: if not provided, pessimistically check deadness of a single MI without doing the LivePhysReg walk; if provided it is assumed to be at the position of MI.
1 parent 76ed4b1 commit f2942b9

File tree

3 files changed

+52
-45
lines changed

3 files changed

+52
-45
lines changed

llvm/include/llvm/CodeGen/MachineInstr.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class BatchAAResults;
4646
template <typename T> class ArrayRef;
4747
class DIExpression;
4848
class DILocalVariable;
49+
class LiveRegUnits;
4950
class MachineBasicBlock;
5051
class MachineFunction;
5152
class MachineRegisterInfo;
@@ -1744,6 +1745,18 @@ class MachineInstr
17441745
/// defined registers were dead.
17451746
bool wouldBeTriviallyDead() const;
17461747

1748+
/// Check whether an MI is dead. If \p LivePhysRegs is provided, it is assumed
1749+
/// to be at the position of MI and will be used to check the Liveness of
1750+
/// physical register defs. If \p LivePhysRegs is not provided, this will
1751+
/// pessimistically assume any PhysReg def is live.
1752+
/// For trivially dead instructions (i.e. those without hard to model effects
1753+
/// / wouldBeTriviallyDead), this checks deadness by analyzing defs of the
1754+
/// MachineInstr. If the instruction wouldBeTriviallyDead, and all the defs
1755+
/// either have dead flags or have no uses, then the instruction is said to be
1756+
/// dead.
1757+
bool isDead(const MachineRegisterInfo &MRI,
1758+
LiveRegUnits *LivePhysRegs = nullptr) const;
1759+
17471760
/// Returns true if this instruction's memory access aliases the memory
17481761
/// access of Other.
17491762
//

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"
@@ -1350,6 +1351,43 @@ bool MachineInstr::wouldBeTriviallyDead() const {
13501351
return isPHI() || isSafeToMove(SawStore);
13511352
}
13521353

1354+
bool MachineInstr::isDead(const MachineRegisterInfo &MRI,
1355+
LiveRegUnits *LivePhysRegs) const {
1356+
// Technically speaking inline asm without side effects and no defs can still
1357+
// be deleted. But there is so much bad inline asm code out there, we should
1358+
// let them be.
1359+
if (isInlineAsm())
1360+
return false;
1361+
1362+
// If we suspect this instruction may have some side-effects, then we say
1363+
// this instruction cannot be dead.
1364+
// FIXME: See issue #105950 for why LIFETIME markers are considered dead here.
1365+
if (!isLifetimeMarker() && !wouldBeTriviallyDead())
1366+
return false;
1367+
1368+
// Instructions without side-effects are dead iff they only define dead regs.
1369+
// This function is hot and this loop returns early in the common case,
1370+
// so only perform additional checks before this if absolutely necessary.
1371+
for (const MachineOperand &MO : all_defs()) {
1372+
Register Reg = MO.getReg();
1373+
if (Reg.isPhysical()) {
1374+
// Don't delete live physreg defs, or any reserved register defs.
1375+
if (!LivePhysRegs || !LivePhysRegs->available(Reg) || MRI.isReserved(Reg))
1376+
return false;
1377+
} else {
1378+
if (MO.isDead())
1379+
continue;
1380+
for (const MachineInstr &Use : MRI.use_nodbg_instructions(Reg)) {
1381+
if (&Use != this)
1382+
// This def has a non-debug use. Don't delete the instruction!
1383+
return false;
1384+
}
1385+
}
1386+
}
1387+
1388+
return true;
1389+
}
1390+
13531391
static bool MemOperandsHaveAlias(const MachineFrameInfo &MFI,
13541392
BatchAAResults *AA, bool UseTBAA,
13551393
const MachineMemOperand *MMOa,

0 commit comments

Comments
 (0)