Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 1 | //===- SimplifyCFG.cpp - CFG Simplification Pass --------------------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +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 | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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" |
Chris Lattner | fc5c1bb0 | 2004-10-18 03:00:50 +0000 | [diff] [blame] | 23 | #include "llvm/Constants.h" |
| 24 | #include "llvm/Instructions.h" |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 25 | #include "llvm/Module.h" |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CFG.h" |
| 27 | #include "llvm/Pass.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 29 | #include <set> |
Chris Lattner | d745602 | 2004-01-09 06:02:20 +0000 | [diff] [blame] | 30 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 31 | |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 32 | namespace { |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 33 | Statistic<> NumSimpl("cfgsimplify", "Number of blocks simplified"); |
| 34 | |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 35 | struct CFGSimplifyPass : public FunctionPass { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 36 | virtual bool runOnFunction(Function &F); |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 37 | }; |
Chris Lattner | a6275cc | 2002-07-26 21:12:46 +0000 | [diff] [blame] | 38 | RegisterOpt<CFGSimplifyPass> X("simplifycfg", "Simplify the CFG"); |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 41 | // Public interface to the CFGSimplification pass |
Chris Lattner | d745602 | 2004-01-09 06:02:20 +0000 | [diff] [blame] | 42 | FunctionPass *llvm::createCFGSimplificationPass() { |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 43 | return new CFGSimplifyPass(); |
| 44 | } |
| 45 | |
| 46 | static bool MarkAliveBlocks(BasicBlock *BB, std::set<BasicBlock*> &Reachable) { |
| 47 | if (Reachable.count(BB)) return false; |
| 48 | Reachable.insert(BB); |
| 49 | |
Chris Lattner | fc5c1bb0 | 2004-10-18 03:00:50 +0000 | [diff] [blame] | 50 | // Do a quick scan of the basic block, turning any obviously unreachable |
| 51 | // instructions into LLVM unreachable insts. The instruction combining pass |
| 52 | // canonnicalizes unreachable insts into stores to null or undef. |
| 53 | for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ++BBI) |
| 54 | if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) |
| 55 | if (isa<ConstantPointerNull>(SI->getOperand(1)) || |
| 56 | isa<UndefValue>(SI->getOperand(1))) { |
| 57 | // Loop over all of the successors, removing BB's entry from any PHI |
| 58 | // nodes. |
| 59 | for (succ_iterator I = succ_begin(BB), SE = succ_end(BB); I != SE; ++I) |
| 60 | (*I)->removePredecessor(BB); |
| 61 | |
| 62 | new UnreachableInst(SI); |
Chris Lattner | fc5c1bb0 | 2004-10-18 03:00:50 +0000 | [diff] [blame] | 63 | |
| 64 | // All instructions after this are dead. |
| 65 | for (; BBI != E; ) { |
| 66 | if (!BBI->use_empty()) |
| 67 | BBI->replaceAllUsesWith(UndefValue::get(BBI->getType())); |
| 68 | BB->getInstList().erase(BBI++); |
| 69 | } |
| 70 | break; |
| 71 | } |
| 72 | |
| 73 | |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 74 | bool Changed = ConstantFoldTerminator(BB); |
| 75 | for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI) |
| 76 | MarkAliveBlocks(*SI, Reachable); |
| 77 | |
| 78 | return Changed; |
| 79 | } |
| 80 | |
| 81 | |
| 82 | // It is possible that we may require multiple passes over the code to fully |
| 83 | // simplify the CFG. |
| 84 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 85 | bool CFGSimplifyPass::runOnFunction(Function &F) { |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 86 | std::set<BasicBlock*> Reachable; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 87 | bool Changed = MarkAliveBlocks(F.begin(), Reachable); |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 88 | |
| 89 | // If there are unreachable blocks in the CFG... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 90 | if (Reachable.size() != F.size()) { |
| 91 | assert(Reachable.size() < F.size()); |
| 92 | NumSimpl += F.size()-Reachable.size(); |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 93 | |
| 94 | // Loop over all of the basic blocks that are not reachable, dropping all of |
| 95 | // their internal references... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 96 | for (Function::iterator BB = ++F.begin(), E = F.end(); BB != E; ++BB) |
| 97 | if (!Reachable.count(BB)) { |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 98 | for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI!=SE; ++SI) |
| 99 | if (Reachable.count(*SI)) |
| 100 | (*SI)->removePredecessor(BB); |
| 101 | BB->dropAllReferences(); |
| 102 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 103 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 104 | for (Function::iterator I = ++F.begin(); I != F.end();) |
| 105 | if (!Reachable.count(I)) |
| 106 | I = F.getBasicBlockList().erase(I); |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 107 | else |
| 108 | ++I; |
| 109 | |
| 110 | Changed = true; |
| 111 | } |
| 112 | |
| 113 | bool LocalChange = true; |
| 114 | while (LocalChange) { |
| 115 | LocalChange = false; |
| 116 | |
| 117 | // Loop over all of the basic blocks (except the first one) and remove them |
| 118 | // if they are unneeded... |
| 119 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 120 | for (Function::iterator BBIt = ++F.begin(); BBIt != F.end(); ) { |
| 121 | if (SimplifyCFG(BBIt++)) { |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 122 | LocalChange = true; |
| 123 | ++NumSimpl; |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | Changed |= LocalChange; |
| 127 | } |
| 128 | |
| 129 | return Changed; |
| 130 | } |