Skip to content

Commit c145ffe

Browse files
committed
Early exit + review comments
Change-Id: I90e7829a0afc535760c08417cc50db71f46d9910
1 parent 9ddbb61 commit c145ffe

File tree

3 files changed

+50
-45
lines changed

3 files changed

+50
-45
lines changed

llvm/include/llvm/CodeGen/MachineInstr.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1738,6 +1738,18 @@ class MachineInstr
17381738
/// defined registers were dead.
17391739
bool wouldBeTriviallyDead() const;
17401740

1741+
/// Check whether an MI is dead. If \p LivePhysRegs is provided, it is assumed
1742+
/// to be at the position of MI and will be used to check the Liveness of
1743+
/// physical register defs. If \p LivePhysRegs is not provided, this will
1744+
/// pessimistically assume any PhysReg def is live.
1745+
/// For trivially dead instructions (i.e. those without hard to model effects
1746+
/// / wouldBeTriviallyDead), this checks deadness by analyzing defs of the
1747+
/// MachineInstr. If the instruction wouldBeTriviallyDead, and all the defs
1748+
/// either have dead flags or have no uses, then the instruction is said to be
1749+
/// dead.
1750+
bool isDead(const MachineRegisterInfo &MRI,
1751+
LiveRegUnits *LivePhysRegs = nullptr) const;
1752+
17411753
/// Returns true if this instruction's memory access aliases the memory
17421754
/// access of Other.
17431755
//
@@ -1787,13 +1799,6 @@ class MachineInstr
17871799
/// Return true if all the implicit defs of this instruction are dead.
17881800
bool allImplicitDefsAreDead() const;
17891801

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-
17971802
/// Return a valid size if the instruction is a spill instruction.
17981803
std::optional<LocationSize> getSpillSize(const TargetInstrInfo *TII) const;
17991804

llvm/lib/CodeGen/DeadMachineInstructionElim.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ bool DeadMachineInstructionElimImpl::eliminateDeadMI(MachineFunction &MF) {
104104
// liveness as we go.
105105
for (MachineInstr &MI : make_early_inc_range(reverse(*MBB))) {
106106
// If the instruction is dead, delete it!
107-
if (MI.isDead(MRI, &LivePhysRegs)) {
107+
if (MI.isDead(*MRI, &LivePhysRegs)) {
108108
LLVM_DEBUG(dbgs() << "DeadMachineInstructionElim: DELETING: " << MI);
109109
// It is possible that some DBG_VALUE instructions refer to this
110110
// instruction. They will be deleted in the live debug variable

llvm/lib/CodeGen/MachineInstr.cpp

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,6 +1351,43 @@ bool MachineInstr::wouldBeTriviallyDead() const {
13511351
return isPHI() || isSafeToMove(SawStore);
13521352
}
13531353

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+
13541391
static bool MemOperandsHaveAlias(const MachineFrameInfo &MFI, AAResults *AA,
13551392
bool UseTBAA, const MachineMemOperand *MMOa,
13561393
const MachineMemOperand *MMOb) {
@@ -1593,43 +1630,6 @@ bool MachineInstr::allImplicitDefsAreDead() const {
15931630
return true;
15941631
}
15951632

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-
16331633
/// copyImplicitOps - Copy implicit register operands from specified
16341634
/// instruction to this instruction.
16351635
void MachineInstr::copyImplicitOps(MachineFunction &MF,

0 commit comments

Comments
 (0)