LLVM 24.0.0git
DeadMachineInstructionElim.cpp
Go to the documentation of this file.
1//===- DeadMachineInstructionElim.cpp - Remove dead machine instructions --===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This is an extremely simple MachineInstr-level dead-code-elimination pass.
10//
11//===----------------------------------------------------------------------===//
12
15#include "llvm/ADT/Statistic.h"
21#include "llvm/Pass.h"
22#include "llvm/Support/Debug.h"
24
25using namespace llvm;
26
27#define DEBUG_TYPE "dead-mi-elimination"
28
29STATISTIC(NumDeletes, "Number of dead instructions deleted");
30
31namespace {
32class DeadMachineInstructionElimImpl {
33 const MachineRegisterInfo *MRI = nullptr;
35
36public:
37 bool runImpl(MachineFunction &MF);
38
39private:
40 bool eliminateDeadMI(MachineFunction &MF, bool &NeedAnotherIteration);
41};
42
43class DeadMachineInstructionElim : public MachineFunctionPass {
44public:
45 static char ID; // Pass identification, replacement for typeid
46
47 DeadMachineInstructionElim() : MachineFunctionPass(ID) {}
48
49 bool runOnMachineFunction(MachineFunction &MF) override {
50 if (skipFunction(MF.getFunction()))
51 return false;
52 return DeadMachineInstructionElimImpl().runImpl(MF);
53 }
54
55 void getAnalysisUsage(AnalysisUsage &AU) const override {
56 AU.setPreservesCFG();
58 }
59
60 MachineFunctionProperties getRequiredProperties() const override {
61 return MachineFunctionProperties().setIsSSA();
62 }
63};
64} // namespace
65
69 MFPropsModifier _(*this, MF);
70
71 if (!DeadMachineInstructionElimImpl().runImpl(MF))
75 return PA;
76}
77
78char DeadMachineInstructionElim::ID = 0;
79char &llvm::DeadMachineInstructionElimID = DeadMachineInstructionElim::ID;
80
81INITIALIZE_PASS(DeadMachineInstructionElim, DEBUG_TYPE,
82 "Remove dead machine instructions", false, false)
83
84bool DeadMachineInstructionElimImpl::runImpl(MachineFunction &MF) {
85 MRI = &MF.getRegInfo();
86
87 const TargetSubtargetInfo &ST = MF.getSubtarget();
88 LivePhysRegs.init(*ST.getRegisterInfo());
89
90 bool NeedAnotherIteration;
91 bool AnyChanges = eliminateDeadMI(MF, NeedAnotherIteration);
92 while (NeedAnotherIteration)
93 eliminateDeadMI(MF, NeedAnotherIteration);
94 return AnyChanges;
95}
96
97bool DeadMachineInstructionElimImpl::eliminateDeadMI(
98 MachineFunction &MF, bool &NeedAnotherIteration) {
99 bool AnyChanges = false;
101
102 // Loop over all instructions in all blocks, from bottom to top, so that it's
103 // more likely that chains of dependent but ultimately dead instructions will
104 // be cleaned up.
105 for (MachineBasicBlock *MBB : post_order(&MF)) {
107 NeedsProcessing.erase(MBB);
108
109 // Now scan the instructions and delete dead ones, tracking physreg
110 // liveness as we go.
112 if (MI.isDebugInstr())
113 continue;
114 // If the instruction is dead, delete it!
115 if (MI.isDead(*MRI, &LivePhysRegs)) {
116 if (MI.isPHI()) {
118 NeedsProcessing.insert(P);
119 }
120 LLVM_DEBUG(dbgs() << "DeadMachineInstructionElim: DELETING: " << MI);
121 // It is possible that some DBG_VALUE instructions refer to this
122 // instruction. They will be deleted in the live debug variable
123 // analysis.
124 MI.eraseFromParent();
125 AnyChanges = true;
126 ++NumDeletes;
127 continue;
128 }
129
130 for (MachineOperand &MO : MI.all_defs()) {
131 Register Reg = MO.getReg();
132 if (Reg.isPhysical() && !MO.isDead() && !MRI->isReserved(Reg) &&
134 MO.setIsDead();
135 }
136
138 }
139 }
141 NeedAnotherIteration = !NeedsProcessing.empty();
142 return AnyChanges;
143}
MachineBasicBlock & MBB
static bool runImpl(MachineFunction &MF)
Definition CFIFixup.cpp:304
#define DEBUG_TYPE
#define _
IRTranslator LLVM IR MI
A set of register units.
Register Reg
#define P(N)
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
MachineRegisterInfo & MRI
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:119
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:280
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
LLVM_ABI PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
A set of physical registers with utility functions to track liveness when walking backward/forward th...
void clear()
Clears the set.
LLVM_ABI void stepBackward(const MachineInstr &MI)
Simulates liveness when stepping backwards over an instruction(bundle).
void init(const TargetRegisterInfo &TRI)
(re-)initializes and clears the set.
LLVM_ABI bool available(const MachineRegisterInfo &MRI, MCRegister Reg) const
Returns true if register Reg and no aliasing register is in the set.
LLVM_ABI void addLiveOuts(const MachineBasicBlock &MBB)
Adds all live-out registers of basic block MBB.
A set of register units used to track register liveness.
An RAII based helper class to modify MachineFunctionProperties when running pass.
iterator_range< pred_iterator > predecessors()
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Function & getFunction()
Return the LLVM function that this machine code represents.
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
bool isReserved(MCRegister PhysReg) const
isReserved - Returns true when PhysReg is a reserved register.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:152
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition Register.h:83
bool erase(PtrType Ptr)
Remove pointer from the set.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
TargetSubtargetInfo - Generic base class for all target subtargets.
This is an optimization pass for GlobalISel generic memory operations.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:649
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
auto reverse(ContainerTy &&C)
Definition STLExtras.h:408
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
auto post_order(const T &G)
Post-order traversal of a graph.
LLVM_ABI char & DeadMachineInstructionElimID
DeadMachineInstructionElim - This pass removes dead machine instructions.