1 //===-- UnreachableBlockElim.cpp - Remove unreachable blocks for codegen --===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass is an extremely simple version of the SimplifyCFG pass. Its sole
11 // job is to delete LLVM basic blocks that are not reachable from the entry
12 // node. To do this, it performs a simple depth first traversal of the CFG,
13 // then deletes any unvisited nodes.
15 // Note that this pass is really a hack. In particular, the instruction
16 // selectors for various targets should just not generate code for unreachable
17 // blocks. Until LLVM has a more systematic way of defining instruction
18 // selectors, however, we cannot really expect them to handle additional
21 //===----------------------------------------------------------------------===//
23 #include "llvm/CodeGen/Passes.h"
24 #include "llvm/Constant.h"
25 #include "llvm/Instructions.h"
26 #include "llvm/Function.h"
27 #include "llvm/Pass.h"
28 #include "llvm/Type.h"
29 #include "llvm/Analysis/ProfileInfo.h"
30 #include "llvm/CodeGen/MachineDominators.h"
31 #include "llvm/CodeGen/MachineFunctionPass.h"
32 #include "llvm/CodeGen/MachineModuleInfo.h"
33 #include "llvm/CodeGen/MachineLoopInfo.h"
34 #include "llvm/CodeGen/MachineRegisterInfo.h"
35 #include "llvm/Support/CFG.h"
36 #include "llvm/Target/TargetInstrInfo.h"
37 #include "llvm/ADT/DepthFirstIterator.h"
38 #include "llvm/ADT/SmallPtrSet.h"
42 class UnreachableBlockElim
: public FunctionPass
{
43 virtual bool runOnFunction(Function
&F
);
45 static char ID
; // Pass identification, replacement for typeid
46 UnreachableBlockElim() : FunctionPass(ID
) {
47 initializeUnreachableBlockElimPass(*PassRegistry::getPassRegistry());
50 virtual void getAnalysisUsage(AnalysisUsage
&AU
) const {
51 AU
.addPreserved
<ProfileInfo
>();
55 char UnreachableBlockElim::ID
= 0;
56 INITIALIZE_PASS(UnreachableBlockElim
, "unreachableblockelim",
57 "Remove unreachable blocks from the CFG", false, false)
59 FunctionPass
*llvm::createUnreachableBlockEliminationPass() {
60 return new UnreachableBlockElim();
63 bool UnreachableBlockElim::runOnFunction(Function
&F
) {
64 SmallPtrSet
<BasicBlock
*, 8> Reachable
;
66 // Mark all reachable blocks.
67 for (df_ext_iterator
<Function
*, SmallPtrSet
<BasicBlock
*, 8> > I
=
68 df_ext_begin(&F
, Reachable
), E
= df_ext_end(&F
, Reachable
); I
!= E
; ++I
)
69 /* Mark all reachable blocks */;
71 // Loop over all dead blocks, remembering them and deleting all instructions
73 std::vector
<BasicBlock
*> DeadBlocks
;
74 for (Function::iterator I
= F
.begin(), E
= F
.end(); I
!= E
; ++I
)
75 if (!Reachable
.count(I
)) {
77 DeadBlocks
.push_back(BB
);
78 while (PHINode
*PN
= dyn_cast
<PHINode
>(BB
->begin())) {
79 PN
->replaceAllUsesWith(Constant::getNullValue(PN
->getType()));
80 BB
->getInstList().pop_front();
82 for (succ_iterator SI
= succ_begin(BB
), E
= succ_end(BB
); SI
!= E
; ++SI
)
83 (*SI
)->removePredecessor(BB
);
84 BB
->dropAllReferences();
87 // Actually remove the blocks now.
88 ProfileInfo
*PI
= getAnalysisIfAvailable
<ProfileInfo
>();
89 for (unsigned i
= 0, e
= DeadBlocks
.size(); i
!= e
; ++i
) {
90 if (PI
) PI
->removeBlock(DeadBlocks
[i
]);
91 DeadBlocks
[i
]->eraseFromParent();
94 return DeadBlocks
.size();
99 class UnreachableMachineBlockElim
: public MachineFunctionPass
{
100 virtual bool runOnMachineFunction(MachineFunction
&F
);
101 virtual void getAnalysisUsage(AnalysisUsage
&AU
) const;
102 MachineModuleInfo
*MMI
;
104 static char ID
; // Pass identification, replacement for typeid
105 UnreachableMachineBlockElim() : MachineFunctionPass(ID
) {}
108 char UnreachableMachineBlockElim::ID
= 0;
110 INITIALIZE_PASS(UnreachableMachineBlockElim
, "unreachable-mbb-elimination",
111 "Remove unreachable machine basic blocks", false, false)
113 char &llvm::UnreachableMachineBlockElimID
= UnreachableMachineBlockElim::ID
;
115 void UnreachableMachineBlockElim::getAnalysisUsage(AnalysisUsage
&AU
) const {
116 AU
.addPreserved
<MachineLoopInfo
>();
117 AU
.addPreserved
<MachineDominatorTree
>();
118 MachineFunctionPass::getAnalysisUsage(AU
);
121 bool UnreachableMachineBlockElim::runOnMachineFunction(MachineFunction
&F
) {
122 SmallPtrSet
<MachineBasicBlock
*, 8> Reachable
;
123 bool ModifiedPHI
= false;
125 MMI
= getAnalysisIfAvailable
<MachineModuleInfo
>();
126 MachineDominatorTree
*MDT
= getAnalysisIfAvailable
<MachineDominatorTree
>();
127 MachineLoopInfo
*MLI
= getAnalysisIfAvailable
<MachineLoopInfo
>();
129 // Mark all reachable blocks.
130 for (df_ext_iterator
<MachineFunction
*, SmallPtrSet
<MachineBasicBlock
*, 8> >
131 I
= df_ext_begin(&F
, Reachable
), E
= df_ext_end(&F
, Reachable
);
133 /* Mark all reachable blocks */;
135 // Loop over all dead blocks, remembering them and deleting all instructions
137 std::vector
<MachineBasicBlock
*> DeadBlocks
;
138 for (MachineFunction::iterator I
= F
.begin(), E
= F
.end(); I
!= E
; ++I
) {
139 MachineBasicBlock
*BB
= I
;
141 // Test for deadness.
142 if (!Reachable
.count(BB
)) {
143 DeadBlocks
.push_back(BB
);
145 // Update dominator and loop info.
146 if (MLI
) MLI
->removeBlock(BB
);
147 if (MDT
&& MDT
->getNode(BB
)) MDT
->eraseNode(BB
);
149 while (BB
->succ_begin() != BB
->succ_end()) {
150 MachineBasicBlock
* succ
= *BB
->succ_begin();
152 MachineBasicBlock::iterator start
= succ
->begin();
153 while (start
!= succ
->end() && start
->isPHI()) {
154 for (unsigned i
= start
->getNumOperands() - 1; i
>= 2; i
-=2)
155 if (start
->getOperand(i
).isMBB() &&
156 start
->getOperand(i
).getMBB() == BB
) {
157 start
->RemoveOperand(i
);
158 start
->RemoveOperand(i
-1);
164 BB
->removeSuccessor(BB
->succ_begin());
169 // Actually remove the blocks now.
170 for (unsigned i
= 0, e
= DeadBlocks
.size(); i
!= e
; ++i
)
171 DeadBlocks
[i
]->eraseFromParent();
173 // Cleanup PHI nodes.
174 for (MachineFunction::iterator I
= F
.begin(), E
= F
.end(); I
!= E
; ++I
) {
175 MachineBasicBlock
*BB
= I
;
176 // Prune unneeded PHI entries.
177 SmallPtrSet
<MachineBasicBlock
*, 8> preds(BB
->pred_begin(),
179 MachineBasicBlock::iterator phi
= BB
->begin();
180 while (phi
!= BB
->end() && phi
->isPHI()) {
181 for (unsigned i
= phi
->getNumOperands() - 1; i
>= 2; i
-=2)
182 if (!preds
.count(phi
->getOperand(i
).getMBB())) {
183 phi
->RemoveOperand(i
);
184 phi
->RemoveOperand(i
-1);
188 if (phi
->getNumOperands() == 3) {
189 unsigned Input
= phi
->getOperand(1).getReg();
190 unsigned Output
= phi
->getOperand(0).getReg();
192 MachineInstr
* temp
= phi
;
194 temp
->eraseFromParent();
198 F
.getRegInfo().replaceRegWith(Output
, Input
);
209 return (DeadBlocks
.size() || ModifiedPHI
);