Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 1 | //===- SimplifyCFG.cpp - CFG Simplification Pass --------------------------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements dead code elimination and basic block merging. |
| 11 | // |
| 12 | // Specifically, this: |
| 13 | // * removes basic blocks with no predecessors |
| 14 | // * merges a basic block into its predecessor if there is only one and the |
| 15 | // predecessor only has one successor. |
| 16 | // * Eliminates PHI nodes for basic blocks with a single predecessor |
| 17 | // * Eliminates a basic block that only contains an unconditional branch |
| 18 | // |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | |
| 21 | #include "llvm/Transforms/Scalar.h" |
| 22 | #include "llvm/Transforms/Utils/Local.h" |
| 23 | #include "llvm/Module.h" |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 24 | #include "llvm/Support/CFG.h" |
| 25 | #include "llvm/Pass.h" |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 26 | #include "Support/Statistic.h" |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 27 | #include <set> |
Chris Lattner | d745602 | 2004-01-09 06:02:20 +0000 | [diff] [blame] | 28 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 29 | |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 30 | namespace { |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 31 | Statistic<> NumSimpl("cfgsimplify", "Number of blocks simplified"); |
| 32 | |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 33 | struct CFGSimplifyPass : public FunctionPass { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 34 | virtual bool runOnFunction(Function &F); |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 35 | }; |
Chris Lattner | a6275cc | 2002-07-26 21:12:46 +0000 | [diff] [blame] | 36 | RegisterOpt<CFGSimplifyPass> X("simplifycfg", "Simplify the CFG"); |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 39 | // Public interface to the CFGSimplification pass |
Chris Lattner | d745602 | 2004-01-09 06:02:20 +0000 | [diff] [blame] | 40 | FunctionPass *llvm::createCFGSimplificationPass() { |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 41 | return new CFGSimplifyPass(); |
| 42 | } |
| 43 | |
| 44 | static bool MarkAliveBlocks(BasicBlock *BB, std::set<BasicBlock*> &Reachable) { |
| 45 | if (Reachable.count(BB)) return false; |
| 46 | Reachable.insert(BB); |
| 47 | |
| 48 | bool Changed = ConstantFoldTerminator(BB); |
| 49 | for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI) |
| 50 | MarkAliveBlocks(*SI, Reachable); |
| 51 | |
| 52 | return Changed; |
| 53 | } |
| 54 | |
| 55 | |
| 56 | // It is possible that we may require multiple passes over the code to fully |
| 57 | // simplify the CFG. |
| 58 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 59 | bool CFGSimplifyPass::runOnFunction(Function &F) { |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 60 | std::set<BasicBlock*> Reachable; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 61 | bool Changed = MarkAliveBlocks(F.begin(), Reachable); |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 62 | |
| 63 | // If there are unreachable blocks in the CFG... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 64 | if (Reachable.size() != F.size()) { |
| 65 | assert(Reachable.size() < F.size()); |
| 66 | NumSimpl += F.size()-Reachable.size(); |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 67 | |
| 68 | // Loop over all of the basic blocks that are not reachable, dropping all of |
| 69 | // their internal references... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 70 | for (Function::iterator BB = ++F.begin(), E = F.end(); BB != E; ++BB) |
| 71 | if (!Reachable.count(BB)) { |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 72 | for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI!=SE; ++SI) |
| 73 | if (Reachable.count(*SI)) |
| 74 | (*SI)->removePredecessor(BB); |
| 75 | BB->dropAllReferences(); |
| 76 | } |
| 77 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 78 | for (Function::iterator I = ++F.begin(); I != F.end();) |
| 79 | if (!Reachable.count(I)) |
| 80 | I = F.getBasicBlockList().erase(I); |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 81 | else |
| 82 | ++I; |
| 83 | |
| 84 | Changed = true; |
| 85 | } |
| 86 | |
| 87 | bool LocalChange = true; |
| 88 | while (LocalChange) { |
| 89 | LocalChange = false; |
| 90 | |
| 91 | // Loop over all of the basic blocks (except the first one) and remove them |
| 92 | // if they are unneeded... |
| 93 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 94 | for (Function::iterator BBIt = ++F.begin(); BBIt != F.end(); ) { |
| 95 | if (SimplifyCFG(BBIt++)) { |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 96 | LocalChange = true; |
| 97 | ++NumSimpl; |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | Changed |= LocalChange; |
| 101 | } |
| 102 | |
| 103 | return Changed; |
| 104 | } |