Skip to content

Commit 4d5f49b

Browse files
committed
Bring in and use MI.isDead()
Change-Id: Ia3e109d47eaa23113e6777079be952fa29eb3e34
1 parent 672150e commit 4d5f49b

File tree

4 files changed

+59
-95
lines changed

4 files changed

+59
-95
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,

llvm/lib/CodeGen/MachineSink.cpp

Lines changed: 12 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ class MachineSinking : public MachineFunctionPass {
164164
/// would re-order assignments.
165165
using SeenDbgUser = PointerIntPair<MachineInstr *, 1>;
166166

167+
using SinkItem = std::pair<MachineInstr *, MachineBasicBlock *>;
168+
167169
/// Record of DBG_VALUE uses of vregs in a block, so that we can identify
168170
/// debug instructions to sink.
169171
SmallDenseMap<unsigned, TinyPtrVector<SeenDbgUser>> SeenDbgUsers;
@@ -259,11 +261,9 @@ class MachineSinking : public MachineFunctionPass {
259261
void FindCycleSinkCandidates(MachineCycle *Cycle, MachineBasicBlock *BB,
260262
SmallVectorImpl<MachineInstr *> &Candidates);
261263

262-
bool isDead(const MachineInstr *MI) const;
263-
bool aggressivelySinkIntoCycle(
264-
MachineCycle *Cycle, MachineInstr &I,
265-
DenseMap<std::pair<MachineInstr *, MachineBasicBlock *>, MachineInstr *>
266-
&SunkInstrs);
264+
bool
265+
aggressivelySinkIntoCycle(MachineCycle *Cycle, MachineInstr &I,
266+
DenseMap<SinkItem, MachineInstr *> &SunkInstrs);
267267

268268
bool isProfitableToSinkTo(Register Reg, MachineInstr &MI,
269269
MachineBasicBlock *MBB,
@@ -692,7 +692,7 @@ void MachineSinking::FindCycleSinkCandidates(
692692
for (auto &MI : *BB) {
693693
LLVM_DEBUG(dbgs() << "CycleSink: Analysing candidate: " << MI);
694694
if (MI.isMetaInstruction()) {
695-
LLVM_DEBUG(dbgs() << "CycleSink: Dont sink meta instructions\n");
695+
LLVM_DEBUG(dbgs() << "CycleSink: not sinking meta instruction\n");
696696
continue;
697697
}
698698
if (!TII->shouldSink(MI)) {
@@ -789,8 +789,8 @@ bool MachineSinking::runOnMachineFunction(MachineFunction &MF) {
789789
SmallVector<MachineCycle *, 8> Cycles(CI->toplevel_cycles());
790790
SchedModel.init(STI);
791791
bool HasHighPressure;
792-
DenseMap<std::pair<MachineInstr *, MachineBasicBlock *>, MachineInstr *>
793-
SunkInstrs;
792+
793+
DenseMap<SinkItem, MachineInstr *> SunkInstrs;
794794

795795
enum CycleSinkStage { COPY, LOW_LATENCY, AGGRESSIVE, END };
796796
for (unsigned Stage = CycleSinkStage::COPY; Stage != CycleSinkStage::END;
@@ -1644,52 +1644,14 @@ bool MachineSinking::hasStoreBetween(MachineBasicBlock *From,
16441644
return HasAliasedStore;
16451645
}
16461646

1647-
bool MachineSinking::isDead(const MachineInstr *MI) const {
1648-
// Instructions without side-effects are dead iff they only define dead regs.
1649-
// This function is hot and this loop returns early in the common case,
1650-
// so only perform additional checks before this if absolutely necessary.
1651-
1652-
for (const MachineOperand &MO : MI->all_defs()) {
1653-
Register Reg = MO.getReg();
1654-
if (Reg.isPhysical())
1655-
return false;
1656-
1657-
if (MO.isDead()) {
1658-
#ifndef NDEBUG
1659-
// Basic check on the register. All of them should be 'undef'.
1660-
for (auto &U : MRI->use_nodbg_operands(Reg))
1661-
assert(U.isUndef() && "'Undef' use on a 'dead' register is found!");
1662-
#endif
1663-
continue;
1664-
}
1665-
1666-
if (!(MRI->hasAtMostUserInstrs(Reg, 0)))
1667-
return false;
1668-
}
1669-
1670-
// Technically speaking inline asm without side effects and no defs can still
1671-
// be deleted. But there is so much bad inline asm code out there, we should
1672-
// let them be.
1673-
if (MI->isInlineAsm())
1674-
return false;
1675-
1676-
// FIXME: See issue #105950 for why LIFETIME markers are considered dead here.
1677-
if (MI->isLifetimeMarker())
1678-
return true;
1679-
1680-
// If there are no defs with uses, the instruction might be dead.
1681-
return MI->wouldBeTriviallyDead();
1682-
}
1683-
16841647
/// Aggressively sink instructions into cycles. This will aggressively try to
16851648
/// sink all instructions in the top-most preheaders in an attempt to reduce RP.
16861649
/// In particular, it will sink into multiple successor blocks without limits
16871650
/// based on the amount of sinking, or the type of ops being sunk (so long as
16881651
/// they are safe to sink).
16891652
bool MachineSinking::aggressivelySinkIntoCycle(
16901653
MachineCycle *Cycle, MachineInstr &I,
1691-
DenseMap<std::pair<MachineInstr *, MachineBasicBlock *>, MachineInstr *>
1692-
&SunkInstrs) {
1654+
DenseMap<SinkItem, MachineInstr *> &SunkInstrs) {
16931655
// TODO: support instructions with multiple defs
16941656
if (I.getNumDefs() > 1)
16951657
return false;
@@ -1713,7 +1675,7 @@ bool MachineSinking::aggressivelySinkIntoCycle(
17131675
continue;
17141676
}
17151677
// We cannot sink before the prologue
1716-
if (TII->isBasicBlockPrologue(*MI) || MI->isPosition()) {
1678+
if (MI->isPosition() || TII->isBasicBlockPrologue(*MI)) {
17171679
LLVM_DEBUG(dbgs() << "AggressiveCycleSink: Use is BasicBlock prologue, "
17181680
"can't sink.\n");
17191681
continue;
@@ -1726,7 +1688,7 @@ bool MachineSinking::aggressivelySinkIntoCycle(
17261688

17271689
MachineBasicBlock *SinkBlock = MI->getParent();
17281690
MachineInstr *NewMI = nullptr;
1729-
std::pair<MachineInstr *, MachineBasicBlock *> MapEntry(&I, SinkBlock);
1691+
SinkItem MapEntry(&I, SinkBlock);
17301692

17311693
auto SI = SunkInstrs.find(MapEntry);
17321694

@@ -1772,7 +1734,7 @@ bool MachineSinking::aggressivelySinkIntoCycle(
17721734
UseReg.SubReg, *TRI);
17731735
}
17741736
// If we have replaced all uses, then delete the dead instruction
1775-
if (isDead(&I))
1737+
if (I.isDead(MRI))
17761738
I.eraseFromParent();
17771739
return true;
17781740
}

0 commit comments

Comments
 (0)