| Chris Lattner | fc3c82a | 2004-07-02 05:46:10 +0000 | [diff] [blame] | 1 | //===-- UnreachableBlockElim.cpp - Remove unreachable blocks for codegen --===// | 
| Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2 | // | 
| Chris Lattner | fc3c82a | 2004-07-02 05:46:10 +0000 | [diff] [blame] | 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. | 
| Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 7 | // | 
| Chris Lattner | fc3c82a | 2004-07-02 05:46:10 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// | 
| Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 9 | // | 
| Chris Lattner | fc3c82a | 2004-07-02 05:46:10 +0000 | [diff] [blame] | 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" | 
| Chris Lattner | 7f0566c | 2004-07-06 06:36:11 +0000 | [diff] [blame] | 24 | #include "llvm/Constant.h" | 
| Chris Lattner | 879313a | 2004-07-29 17:23:00 +0000 | [diff] [blame] | 25 | #include "llvm/Instructions.h" | 
| Chris Lattner | fc3c82a | 2004-07-02 05:46:10 +0000 | [diff] [blame] | 26 | #include "llvm/Function.h" | 
 | 27 | #include "llvm/Pass.h" | 
| Chris Lattner | 5b3a455 | 2005-03-17 15:38:16 +0000 | [diff] [blame] | 28 | #include "llvm/Type.h" | 
| Chris Lattner | fc3c82a | 2004-07-02 05:46:10 +0000 | [diff] [blame] | 29 | #include "llvm/Support/CFG.h" | 
| Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Compiler.h" | 
| Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/DepthFirstIterator.h" | 
| Chris Lattner | fc3c82a | 2004-07-02 05:46:10 +0000 | [diff] [blame] | 32 | using namespace llvm; | 
 | 33 |  | 
 | 34 | namespace { | 
| Chris Lattner | 9525528 | 2006-06-28 23:17:24 +0000 | [diff] [blame] | 35 |   class VISIBILITY_HIDDEN UnreachableBlockElim : public FunctionPass { | 
| Chris Lattner | fc3c82a | 2004-07-02 05:46:10 +0000 | [diff] [blame] | 36 |     virtual bool runOnFunction(Function &F); | 
| Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 37 |   public: | 
| Devang Patel | 3e15bf3 | 2007-05-02 21:39:20 +0000 | [diff] [blame^] | 38 |     static const char ID; // Pass identifcation, replacement for typeid | 
| Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 39 |     UnreachableBlockElim() : FunctionPass((intptr_t)&ID) {} | 
| Chris Lattner | fc3c82a | 2004-07-02 05:46:10 +0000 | [diff] [blame] | 40 |   }; | 
| Devang Patel | 3e15bf3 | 2007-05-02 21:39:20 +0000 | [diff] [blame^] | 41 |   const char UnreachableBlockElim::ID = 0; | 
| Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 42 |   RegisterPass<UnreachableBlockElim> | 
| Chris Lattner | fc3c82a | 2004-07-02 05:46:10 +0000 | [diff] [blame] | 43 |   X("unreachableblockelim", "Remove unreachable blocks from the CFG"); | 
 | 44 | } | 
 | 45 |  | 
 | 46 | FunctionPass *llvm::createUnreachableBlockEliminationPass() { | 
 | 47 |   return new UnreachableBlockElim(); | 
 | 48 | } | 
 | 49 |  | 
 | 50 | bool UnreachableBlockElim::runOnFunction(Function &F) { | 
 | 51 |   std::set<BasicBlock*> Reachable; | 
 | 52 |  | 
 | 53 |   // Mark all reachable blocks. | 
 | 54 |   for (df_ext_iterator<Function*> I = df_ext_begin(&F, Reachable), | 
 | 55 |          E = df_ext_end(&F, Reachable); I != E; ++I) | 
 | 56 |     /* Mark all reachable blocks */; | 
 | 57 |  | 
 | 58 |   // Loop over all dead blocks, remembering them and deleting all instructions | 
 | 59 |   // in them. | 
 | 60 |   std::vector<BasicBlock*> DeadBlocks; | 
 | 61 |   for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) | 
 | 62 |     if (!Reachable.count(I)) { | 
| Chris Lattner | 7f0566c | 2004-07-06 06:36:11 +0000 | [diff] [blame] | 63 |       BasicBlock *BB = I; | 
 | 64 |       DeadBlocks.push_back(BB); | 
 | 65 |       while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) { | 
 | 66 |         PN->replaceAllUsesWith(Constant::getNullValue(PN->getType())); | 
 | 67 |         BB->getInstList().pop_front(); | 
 | 68 |       } | 
 | 69 |       for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) | 
 | 70 |         (*SI)->removePredecessor(BB); | 
 | 71 |       BB->dropAllReferences(); | 
| Chris Lattner | fc3c82a | 2004-07-02 05:46:10 +0000 | [diff] [blame] | 72 |     } | 
 | 73 |  | 
 | 74 |   if (DeadBlocks.empty()) return false; | 
 | 75 |  | 
 | 76 |   // Actually remove the blocks now. | 
 | 77 |   for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i) | 
 | 78 |     F.getBasicBlockList().erase(DeadBlocks[i]); | 
 | 79 |  | 
 | 80 |   return true; | 
 | 81 | } |