Skip to content

Commit 0f4e84a

Browse files
[CodeGen] Refactor DeadMIElim isDead and GISel isTriviallyDead
1 parent 3e763db commit 0f4e84a

File tree

4 files changed

+47
-38
lines changed

4 files changed

+47
-38
lines changed

llvm/include/llvm/CodeGen/MachineInstr.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,6 +1323,11 @@ class MachineInstr
13231323
return getOpcode() == TargetOpcode::ANNOTATION_LABEL;
13241324
}
13251325

1326+
bool isLifetimeMarker() const {
1327+
return getOpcode() == TargetOpcode::LIFETIME_START ||
1328+
getOpcode() == TargetOpcode::LIFETIME_END;
1329+
}
1330+
13261331
/// Returns true if the MachineInstr represents a label.
13271332
bool isLabel() const {
13281333
return isEHLabel() || isGCLabel() || isAnnotationLabel();
@@ -1725,6 +1730,10 @@ class MachineInstr
17251730
/// the instruction's location and its intended destination.
17261731
bool isSafeToMove(bool &SawStore) const;
17271732

1733+
/// Return true if this instruction can be considered dead if all of its
1734+
/// defined registers are dead.
1735+
bool canBeDead() const;
1736+
17281737
/// Returns true if this instruction's memory access aliases the memory
17291738
/// access of Other.
17301739
//

llvm/lib/CodeGen/DeadMachineInstructionElim.cpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -80,22 +80,9 @@ INITIALIZE_PASS(DeadMachineInstructionElim, DEBUG_TYPE,
8080
"Remove dead machine instructions", false, false)
8181

8282
bool DeadMachineInstructionElimImpl::isDead(const MachineInstr *MI) const {
83-
// Technically speaking inline asm without side effects and no defs can still
84-
// be deleted. But there is so much bad inline asm code out there, we should
85-
// let them be.
86-
if (MI->isInlineAsm())
87-
return false;
88-
89-
// Don't delete frame allocation labels.
90-
if (MI->getOpcode() == TargetOpcode::LOCAL_ESCAPE)
91-
return false;
92-
93-
// Don't delete instructions with side effects.
94-
bool SawStore = false;
95-
if (!MI->isSafeToMove(SawStore) && !MI->isPHI())
96-
return false;
97-
98-
// Examine each operand.
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.
9986
for (const MachineOperand &MO : MI->all_defs()) {
10087
Register Reg = MO.getReg();
10188
if (Reg.isPhysical()) {
@@ -119,8 +106,18 @@ bool DeadMachineInstructionElimImpl::isDead(const MachineInstr *MI) const {
119106
}
120107
}
121108

122-
// If there are no defs with uses, the instruction is dead.
123-
return true;
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->canBeDead();
124121
}
125122

126123
bool DeadMachineInstructionElimImpl::runImpl(MachineFunction &MF) {

llvm/lib/CodeGen/GlobalISel/Utils.cpp

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -221,31 +221,15 @@ bool llvm::canReplaceReg(Register DstReg, Register SrcReg,
221221

222222
bool llvm::isTriviallyDead(const MachineInstr &MI,
223223
const MachineRegisterInfo &MRI) {
224-
// FIXME: This logical is mostly duplicated with
225-
// DeadMachineInstructionElim::isDead. Why is LOCAL_ESCAPE not considered in
226-
// MachineInstr::isLabel?
227-
228-
// Don't delete frame allocation labels.
229-
if (MI.getOpcode() == TargetOpcode::LOCAL_ESCAPE)
230-
return false;
231-
// LIFETIME markers should be preserved even if they seem dead.
232-
if (MI.getOpcode() == TargetOpcode::LIFETIME_START ||
233-
MI.getOpcode() == TargetOpcode::LIFETIME_END)
234-
return false;
235-
236-
// If we can move an instruction, we can remove it. Otherwise, it has
237-
// a side-effect of some sort.
238-
bool SawStore = false;
239-
if (!MI.isSafeToMove(SawStore) && !MI.isPHI())
240-
return false;
241-
242-
// Instructions without side-effects are dead iff they only define dead vregs.
224+
// Instructions without side-effects are dead iff they only define dead regs.
225+
// This function is hot and this loop returns early in the common case,
226+
// so only perform additional checks before this if absolutely necessary.
243227
for (const auto &MO : MI.all_defs()) {
244228
Register Reg = MO.getReg();
245229
if (Reg.isPhysical() || !MRI.use_nodbg_empty(Reg))
246230
return false;
247231
}
248-
return true;
232+
return MI.canBeDead();
249233
}
250234

251235
static void reportGISelDiagnostic(DiagnosticSeverity Severity,

llvm/lib/CodeGen/MachineInstr.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,6 +1323,25 @@ bool MachineInstr::isSafeToMove(bool &SawStore) const {
13231323
return true;
13241324
}
13251325

1326+
bool MachineInstr::canBeDead() const {
1327+
// Don't delete frame allocation labels.
1328+
// FIXME: Why is LOCAL_ESCAPE not considered in MachineInstr::isLabel?
1329+
if (getOpcode() == TargetOpcode::LOCAL_ESCAPE)
1330+
return false;
1331+
1332+
// LIFETIME markers should be preserved.
1333+
if (isLifetimeMarker())
1334+
return false;
1335+
1336+
// If we can move an instruction, we can remove it. Otherwise, it has
1337+
// a side-effect of some sort.
1338+
bool SawStore = false;
1339+
if (!isSafeToMove(SawStore) && !isPHI())
1340+
return false;
1341+
1342+
return true;
1343+
}
1344+
13261345
static bool MemOperandsHaveAlias(const MachineFrameInfo &MFI, AAResults *AA,
13271346
bool UseTBAA, const MachineMemOperand *MMOa,
13281347
const MachineMemOperand *MMOb) {

0 commit comments

Comments
 (0)