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/CodeGen/MachineFunctionPass.h"
30 #include "llvm/Support/CFG.h"
31 #include "llvm/Support/Compiler.h"
32 #include "llvm/Target/TargetInstrInfo.h"
33 #include "llvm/ADT/DepthFirstIterator.h"
37 class VISIBILITY_HIDDEN UnreachableBlockElim : public FunctionPass {
38 virtual bool runOnFunction(Function &F);
40 static char ID; // Pass identification, replacement for typeid
41 UnreachableBlockElim() : FunctionPass((intptr_t)&ID) {}
44 char UnreachableBlockElim::ID = 0;
45 static RegisterPass<UnreachableBlockElim>
46 X("unreachableblockelim", "Remove unreachable blocks from the CFG");
48 FunctionPass *llvm::createUnreachableBlockEliminationPass() {
49 return new UnreachableBlockElim();
52 bool UnreachableBlockElim::runOnFunction(Function &F) {
53 std::set<BasicBlock*> Reachable;
55 // Mark all reachable blocks.
56 for (df_ext_iterator<Function*> I = df_ext_begin(&F, Reachable),
57 E = df_ext_end(&F, Reachable); I != E; ++I)
58 /* Mark all reachable blocks */;
60 // Loop over all dead blocks, remembering them and deleting all instructions
62 std::vector<BasicBlock*> DeadBlocks;
63 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
64 if (!Reachable.count(I)) {
66 DeadBlocks.push_back(BB);
67 while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
68 PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
69 BB->getInstList().pop_front();
71 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
72 (*SI)->removePredecessor(BB);
73 BB->dropAllReferences();
76 // Actually remove the blocks now.
77 for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i)
78 DeadBlocks[i]->eraseFromParent();
80 return DeadBlocks.size();
85 class VISIBILITY_HIDDEN UnreachableMachineBlockElim :
86 public MachineFunctionPass {
87 virtual bool runOnMachineFunction(MachineFunction &F);
88 bool iterateOnFunction(MachineFunction& F);
91 static char ID; // Pass identification, replacement for typeid
92 UnreachableMachineBlockElim() : MachineFunctionPass((intptr_t)&ID) {}
95 char UnreachableMachineBlockElim::ID = 0;
97 static RegisterPass<UnreachableMachineBlockElim>
98 Y("unreachable-mbb-elimination",
99 "Remove unreachable machine basic blocks");
101 const PassInfo *const llvm::UnreachableMachineBlockElimID = &Y;
103 bool UnreachableMachineBlockElim::runOnMachineFunction(MachineFunction &F) {
108 changed = iterateOnFunction(F);
118 bool UnreachableMachineBlockElim::iterateOnFunction(MachineFunction &F) {
119 std::set<MachineBasicBlock*> Reachable;
121 // Mark all reachable blocks.
122 for (df_ext_iterator<MachineFunction*> I = df_ext_begin(&F, Reachable),
123 E = df_ext_end(&F, Reachable); I != E; ++I)
124 /* Mark all reachable blocks */;
126 // Loop over all dead blocks, remembering them and deleting all instructions
128 std::vector<MachineBasicBlock*> DeadBlocks;
129 for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I)
130 if (!Reachable.count(I)) {
131 MachineBasicBlock *BB = I;
132 DeadBlocks.push_back(BB);
134 while (BB->succ_begin() != BB->succ_end()) {
135 MachineBasicBlock* succ = *BB->succ_begin();
137 MachineBasicBlock::iterator start = succ->begin();
138 while (start != succ->end() &&
139 start->getOpcode() == TargetInstrInfo::PHI) {
140 for (unsigned i = start->getNumOperands() - 1; i >= 2; i-=2)
141 if (start->getOperand(i).isMBB() &&
142 start->getOperand(i).getMBB() == BB) {
143 start->RemoveOperand(i);
144 start->RemoveOperand(i-1);
147 if (start->getNumOperands() == 1) {
148 MachineInstr* phi = start;
150 phi->eraseFromParent();
155 BB->removeSuccessor(BB->succ_begin());
159 // Actually remove the blocks now.
160 for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i)
161 DeadBlocks[i]->eraseFromParent();
163 return DeadBlocks.size();