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"
22#include "llvm/Pass.h"
23#include "llvm/Support/Debug.h"
25
26using namespace llvm;
27
28#define DEBUG_TYPE "dead-mi-elimination"
29
30STATISTIC(NumDeletes, "Number of dead instructions deleted");
31
32namespace {
33class DeadMachineInstructionElimImpl {
34 const MachineRegisterInfo *MRI = nullptr;
35 const TargetInstrInfo *TII = nullptr;
37
38public:
39 bool runImpl(MachineFunction &MF);
40
41private:
42 bool eliminateDeadMI(MachineFunction &MF, bool &NeedAnotherIteration);
43};
44
45class DeadMachineInstructionElim : public MachineFunctionPass {
46public:
47 static char ID; // Pass identification, replacement for typeid
48
49 DeadMachineInstructionElim() : MachineFunctionPass(ID) {}
50
51 bool runOnMachineFunction(MachineFunction &MF) override {
52 if (skipFunction(MF.getFunction()))
53 return false;
54 return DeadMachineInstructionElimImpl().runImpl(MF);
55 }
56
57 void getAnalysisUsage(AnalysisUsage &AU) const override {
58 AU.setPreservesCFG();
59 AU.addPreserved<MachineRegisterClassInfoWrapperPass>();
61 }
62};
63} // namespace
64
74
75char DeadMachineInstructionElim::ID = 0;
76char &llvm::DeadMachineInstructionElimID = DeadMachineInstructionElim::ID;
77
78INITIALIZE_PASS(DeadMachineInstructionElim, DEBUG_TYPE,
79 "Remove dead machine instructions", false, false)
80
81bool DeadMachineInstructionElimImpl::runImpl(MachineFunction &MF) {
82 MRI = &MF.getRegInfo();
83
84 const TargetSubtargetInfo &ST = MF.getSubtarget();
85 TII = ST.getInstrInfo();
86 LivePhysRegs.init(*ST.getRegisterInfo());
87
88 bool NeedAnotherIteration;
89 bool AnyChanges = eliminateDeadMI(MF, NeedAnotherIteration);
90 while (NeedAnotherIteration)
91 eliminateDeadMI(MF, NeedAnotherIteration);
92 return AnyChanges;
93}
94
95bool DeadMachineInstructionElimImpl::eliminateDeadMI(
96 MachineFunction &MF, bool &NeedAnotherIteration) {
97 bool AnyChanges = false;
99
100 // Loop over all instructions in all blocks, from bottom to top, so that it's
101 // more likely that chains of dependent but ultimately dead instructions will
102 // be cleaned up.
103 for (MachineBasicBlock *MBB : post_order(&MF)) {
105 NeedsProcessing.erase(MBB);
106
107 // Now scan the instructions and delete dead ones, tracking physreg
108 // liveness as we go.
110 if (MI.isDebugInstr())
111 continue;
112 // If the instruction is dead, delete it!
113 if (MI.isDead(*MRI, &LivePhysRegs)) {
114 if (MI.isPHI()) {
116 NeedsProcessing.insert(P);
117 }
118 LLVM_DEBUG(dbgs() << "DeadMachineInstructionElim: DELETING: " << MI);
119 // It is possible that some DBG_VALUE instructions refer to this
120 // instruction. They will be deleted in the live debug variable
121 // analysis.
122 MI.eraseFromParent();
123 AnyChanges = true;
124 ++NumDeletes;
125 continue;
126 }
128 }
129 }
131 NeedAnotherIteration = !NeedsProcessing.empty();
132 return AnyChanges;
133}
MachineBasicBlock & MBB
static bool runImpl(MachineFunction &MF)
Definition CFIFixup.cpp:304
#define DEBUG_TYPE
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
A set of register units.
#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.
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
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:275
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 void addLiveOuts(const MachineBasicBlock &MBB)
Adds all live-out registers of basic block MBB.
A set of register units used to track register liveness.
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.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
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:151
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.
TargetInstrInfo - Interface to description of machine instruction set.
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:633
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:407
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.