LLVM 20.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;
34 const TargetInstrInfo *TII = nullptr;
36
37public:
38 bool runImpl(MachineFunction &MF);
39
40private:
41 bool eliminateDeadMI(MachineFunction &MF);
42};
43
44class DeadMachineInstructionElim : public MachineFunctionPass {
45public:
46 static char ID; // Pass identification, replacement for typeid
47
48 DeadMachineInstructionElim() : MachineFunctionPass(ID) {
50 }
51
52 bool runOnMachineFunction(MachineFunction &MF) override {
53 if (skipFunction(MF.getFunction()))
54 return false;
55 return DeadMachineInstructionElimImpl().runImpl(MF);
56 }
57
58 void getAnalysisUsage(AnalysisUsage &AU) const override {
59 AU.setPreservesCFG();
61 }
62};
63} // namespace
64
68 if (!DeadMachineInstructionElimImpl().runImpl(MF))
72 return PA;
73}
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 AnyChanges = eliminateDeadMI(MF);
89 while (AnyChanges && eliminateDeadMI(MF))
90 ;
91 return AnyChanges;
92}
93
94bool DeadMachineInstructionElimImpl::eliminateDeadMI(MachineFunction &MF) {
95 bool AnyChanges = false;
96
97 // Loop over all instructions in all blocks, from bottom to top, so that it's
98 // more likely that chains of dependent but ultimately dead instructions will
99 // be cleaned up.
100 for (MachineBasicBlock *MBB : post_order(&MF)) {
102
103 // Now scan the instructions and delete dead ones, tracking physreg
104 // liveness as we go.
106 // If the instruction is dead, delete it!
107 if (MI.isDead(*MRI, &LivePhysRegs)) {
108 LLVM_DEBUG(dbgs() << "DeadMachineInstructionElim: DELETING: " << MI);
109 // It is possible that some DBG_VALUE instructions refer to this
110 // instruction. They will be deleted in the live debug variable
111 // analysis.
112 MI.eraseFromParent();
113 AnyChanges = true;
114 ++NumDeletes;
115 continue;
116 }
118 }
119 }
121 return AnyChanges;
122}
unsigned const MachineRegisterInfo * MRI
MachineBasicBlock & MBB
#define LLVM_DEBUG(...)
Definition: Debug.h:106
static bool runImpl(Function &F, const TargetLowering &TLI)
#define DEBUG_TYPE
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
A set of register units.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
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:166
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
Represent the analysis usage information of a pass.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:256
Represents analyses that only rely on functions' control flow.
Definition: Analysis.h:72
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
bool skipFunction(const Function &F) const
Optional passes call this function to check whether the pass should be skipped.
Definition: Pass.cpp:178
A set of physical registers with utility functions to track liveness when walking backward/forward th...
Definition: LivePhysRegs.h:52
void clear()
Clears the set.
Definition: LivePhysRegs.h:77
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.
Definition: LivePhysRegs.h:70
void addLiveOuts(const MachineBasicBlock &MBB)
Adds all live-out registers of basic block MBB.
A set of register units used to track register liveness.
Definition: LiveRegUnits.h:30
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.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
Function & getFunction()
Return the LLVM function that this machine code represents.
Representation of each machine instruction.
Definition: MachineInstr.h:71
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:117
void preserveSet()
Mark an analysis set as preserved.
Definition: Analysis.h:146
TargetInstrInfo - Interface to description of machine instruction set.
TargetSubtargetInfo - Generic base class for all target subtargets.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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:657
iterator_range< po_iterator< T > > post_order(const T &G)
void initializeDeadMachineInstructionElimPass(PassRegistry &)
PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
auto reverse(ContainerTy &&C)
Definition: STLExtras.h:420
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
char & DeadMachineInstructionElimID
DeadMachineInstructionElim - This pass removes dead machine instructions.