diff options
| author | Jeffrey Byrnes <jeffrey.byrnes@amd.com> | 2025-01-23 12:54:29 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-01-23 12:54:29 -0800 |
| commit | f2942b90778670d9ad974d025c779fc96afa737c (patch) | |
| tree | 85e7f1d05852f4c6ac9b7dca3362d1b2b5f030e6 /llvm/lib | |
| parent | 76ed4b1cec57ad26433e4808697d6c4d042d4b22 (diff) | |
| download | llvm-f2942b90778670d9ad974d025c779fc96afa737c.zip llvm-f2942b90778670d9ad974d025c779fc96afa737c.tar.gz llvm-f2942b90778670d9ad974d025c779fc96afa737c.tar.bz2 | |
[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.
Diffstat (limited to 'llvm/lib')
| -rw-r--r-- | llvm/lib/CodeGen/DeadMachineInstructionElim.cpp | 46 | ||||
| -rw-r--r-- | llvm/lib/CodeGen/MachineInstr.cpp | 38 |
2 files changed, 39 insertions, 45 deletions
diff --git a/llvm/lib/CodeGen/DeadMachineInstructionElim.cpp b/llvm/lib/CodeGen/DeadMachineInstructionElim.cpp index 2f17c04..836a912 100644 --- a/llvm/lib/CodeGen/DeadMachineInstructionElim.cpp +++ b/llvm/lib/CodeGen/DeadMachineInstructionElim.cpp @@ -38,7 +38,6 @@ public: bool runImpl(MachineFunction &MF); private: - bool isDead(const MachineInstr *MI) const; bool eliminateDeadMI(MachineFunction &MF); }; @@ -79,47 +78,6 @@ char &llvm::DeadMachineInstructionElimID = DeadMachineInstructionElim::ID; INITIALIZE_PASS(DeadMachineInstructionElim, DEBUG_TYPE, "Remove dead machine instructions", false, false) -bool DeadMachineInstructionElimImpl::isDead(const MachineInstr *MI) const { - // Instructions without side-effects are dead iff they only define dead regs. - // This function is hot and this loop returns early in the common case, - // so only perform additional checks before this if absolutely necessary. - for (const MachineOperand &MO : MI->all_defs()) { - Register Reg = MO.getReg(); - if (Reg.isPhysical()) { - // Don't delete live physreg defs, or any reserved register defs. - if (!LivePhysRegs.available(Reg) || MRI->isReserved(Reg)) - return false; - } else { - if (MO.isDead()) { -#ifndef NDEBUG - // Basic check on the register. All of them should be 'undef'. - for (auto &U : MRI->use_nodbg_operands(Reg)) - assert(U.isUndef() && "'Undef' use on a 'dead' register is found!"); -#endif - continue; - } - for (const MachineInstr &Use : MRI->use_nodbg_instructions(Reg)) { - if (&Use != MI) - // This def has a non-debug use. Don't delete the instruction! - return false; - } - } - } - - // Technically speaking inline asm without side effects and no defs can still - // be deleted. But there is so much bad inline asm code out there, we should - // let them be. - if (MI->isInlineAsm()) - return false; - - // FIXME: See issue #105950 for why LIFETIME markers are considered dead here. - if (MI->isLifetimeMarker()) - return true; - - // If there are no defs with uses, the instruction might be dead. - return MI->wouldBeTriviallyDead(); -} - bool DeadMachineInstructionElimImpl::runImpl(MachineFunction &MF) { MRI = &MF.getRegInfo(); @@ -146,7 +104,7 @@ bool DeadMachineInstructionElimImpl::eliminateDeadMI(MachineFunction &MF) { // liveness as we go. for (MachineInstr &MI : make_early_inc_range(reverse(*MBB))) { // If the instruction is dead, delete it! - if (isDead(&MI)) { + if (MI.isDead(*MRI, &LivePhysRegs)) { LLVM_DEBUG(dbgs() << "DeadMachineInstructionElim: DELETING: " << MI); // It is possible that some DBG_VALUE instructions refer to this // instruction. They will be deleted in the live debug variable @@ -156,11 +114,9 @@ bool DeadMachineInstructionElimImpl::eliminateDeadMI(MachineFunction &MF) { ++NumDeletes; continue; } - LivePhysRegs.stepBackward(MI); } } - LivePhysRegs.clear(); return AnyChanges; } diff --git a/llvm/lib/CodeGen/MachineInstr.cpp b/llvm/lib/CodeGen/MachineInstr.cpp index 8c2fab1..0f7f525 100644 --- a/llvm/lib/CodeGen/MachineInstr.cpp +++ b/llvm/lib/CodeGen/MachineInstr.cpp @@ -18,6 +18,7 @@ #include "llvm/ADT/SmallVector.h" #include "llvm/Analysis/AliasAnalysis.h" #include "llvm/Analysis/MemoryLocation.h" +#include "llvm/CodeGen/LiveRegUnits.h" #include "llvm/CodeGen/MachineBasicBlock.h" #include "llvm/CodeGen/MachineFrameInfo.h" #include "llvm/CodeGen/MachineFunction.h" @@ -1350,6 +1351,43 @@ bool MachineInstr::wouldBeTriviallyDead() const { return isPHI() || isSafeToMove(SawStore); } +bool MachineInstr::isDead(const MachineRegisterInfo &MRI, + LiveRegUnits *LivePhysRegs) const { + // Technically speaking inline asm without side effects and no defs can still + // be deleted. But there is so much bad inline asm code out there, we should + // let them be. + if (isInlineAsm()) + return false; + + // If we suspect this instruction may have some side-effects, then we say + // this instruction cannot be dead. + // FIXME: See issue #105950 for why LIFETIME markers are considered dead here. + if (!isLifetimeMarker() && !wouldBeTriviallyDead()) + return false; + + // Instructions without side-effects are dead iff they only define dead regs. + // This function is hot and this loop returns early in the common case, + // so only perform additional checks before this if absolutely necessary. + for (const MachineOperand &MO : all_defs()) { + Register Reg = MO.getReg(); + if (Reg.isPhysical()) { + // Don't delete live physreg defs, or any reserved register defs. + if (!LivePhysRegs || !LivePhysRegs->available(Reg) || MRI.isReserved(Reg)) + return false; + } else { + if (MO.isDead()) + continue; + for (const MachineInstr &Use : MRI.use_nodbg_instructions(Reg)) { + if (&Use != this) + // This def has a non-debug use. Don't delete the instruction! + return false; + } + } + } + + return true; +} + static bool MemOperandsHaveAlias(const MachineFrameInfo &MFI, BatchAAResults *AA, bool UseTBAA, const MachineMemOperand *MMOa, |
