LLVM 23.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) {}
49
50 bool runOnMachineFunction(MachineFunction &MF) override {
51 if (skipFunction(MF.getFunction()))
52 return false;
53 return DeadMachineInstructionElimImpl().runImpl(MF);
54 }
55
56 void getAnalysisUsage(AnalysisUsage &AU) const override {
57 AU.setPreservesCFG();
59 }
60};
61} // namespace
62
72
73char DeadMachineInstructionElim::ID = 0;
74char &llvm::DeadMachineInstructionElimID = DeadMachineInstructionElim::ID;
75
76INITIALIZE_PASS(DeadMachineInstructionElim, DEBUG_TYPE,
77 "Remove dead machine instructions", false, false)
78
79bool DeadMachineInstructionElimImpl::runImpl(MachineFunction &MF) {
80 MRI = &MF.getRegInfo();
81
82 const TargetSubtargetInfo &ST = MF.getSubtarget();
83 TII = ST.getInstrInfo();
84 LivePhysRegs.init(*ST.getRegisterInfo());
85
86 bool AnyChanges = eliminateDeadMI(MF);
87 while (AnyChanges && eliminateDeadMI(MF))
88 ;
89 return AnyChanges;
90}
91
92bool DeadMachineInstructionElimImpl::eliminateDeadMI(MachineFunction &MF) {
93 bool AnyChanges = false;
94
95 // Loop over all instructions in all blocks, from bottom to top, so that it's
96 // more likely that chains of dependent but ultimately dead instructions will
97 // be cleaned up.
98 for (MachineBasicBlock *MBB : post_order(&MF)) {
100
101 // Now scan the instructions and delete dead ones, tracking physreg
102 // liveness as we go.
104 // If the instruction is dead, delete it!
105 if (MI.isDead(*MRI, &LivePhysRegs)) {
106 LLVM_DEBUG(dbgs() << "DeadMachineInstructionElim: DELETING: " << MI);
107 // It is possible that some DBG_VALUE instructions refer to this
108 // instruction. They will be deleted in the live debug variable
109 // analysis.
110 MI.eraseFromParent();
111 AnyChanges = true;
112 ++NumDeletes;
113 continue;
114 }
116 }
117 }
119 return AnyChanges;
120}
unsigned const MachineRegisterInfo * MRI
MachineBasicBlock & MBB
static bool runImpl(Function &F, const TargetLowering &TLI, const LibcallLoweringInfo &Libcalls, AssumptionCache *AC)
#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: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:114
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:270
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
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.
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.
void addLiveOuts(const MachineBasicBlock &MBB)
Adds all live-out registers of basic block MBB.
A set of register units used to track register liveness.
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
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.
Definition Types.h:26
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:632
iterator_range< po_iterator< T > > post_order(const T &G)
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:406
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI char & DeadMachineInstructionElimID
DeadMachineInstructionElim - This pass removes dead machine instructions.