Fix a bug in the unreachable block elim pass. Dropping all references on a
[oota-llvm.git] / lib / CodeGen / UnreachableBlockElim.cpp
1 //===-- UnreachableBlockElim.cpp - Remove unreachable blocks for codegen --===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 // 
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.
14 //
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
19 // complexity.
20 //
21 //===----------------------------------------------------------------------===//
22
23 #include "llvm/CodeGen/Passes.h"
24 #include "llvm/iPHINode.h"
25 #include "llvm/Constant.h"
26 #include "llvm/Function.h"
27 #include "llvm/Pass.h"
28 #include "llvm/Support/CFG.h"
29 #include "Support/DepthFirstIterator.h"
30 using namespace llvm;
31
32 namespace {
33   class UnreachableBlockElim : public FunctionPass {
34     virtual bool runOnFunction(Function &F);
35   };
36   RegisterOpt<UnreachableBlockElim>
37   X("unreachableblockelim", "Remove unreachable blocks from the CFG");
38 }
39
40 FunctionPass *llvm::createUnreachableBlockEliminationPass() {
41   return new UnreachableBlockElim();
42 }
43
44 bool UnreachableBlockElim::runOnFunction(Function &F) {
45   std::set<BasicBlock*> Reachable;
46
47   // Mark all reachable blocks.
48   for (df_ext_iterator<Function*> I = df_ext_begin(&F, Reachable),
49          E = df_ext_end(&F, Reachable); I != E; ++I)
50     /* Mark all reachable blocks */;
51
52   // Loop over all dead blocks, remembering them and deleting all instructions
53   // in them.
54   std::vector<BasicBlock*> DeadBlocks;
55   for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
56     if (!Reachable.count(I)) {
57       BasicBlock *BB = I;
58       DeadBlocks.push_back(BB);
59       while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
60         PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
61         BB->getInstList().pop_front();
62       }
63       for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
64         (*SI)->removePredecessor(BB);
65       BB->dropAllReferences();
66     }
67
68   if (DeadBlocks.empty()) return false;
69
70   // Actually remove the blocks now.
71   for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i)
72     F.getBasicBlockList().erase(DeadBlocks[i]);
73
74   return true;
75 }