Chris Lattner | 4186ad5 | 2008-05-14 20:38:44 +0000 | [diff] [blame] | 1 | //===- SimplifyCFGPass.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 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // 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 | // |
Chris Lattner | 4cf4b69 | 2007-12-03 19:43:18 +0000 | [diff] [blame] | 10 | // This file implements dead code elimination and basic block merging, along |
| 11 | // with a collection of other peephole control flow optimizations. For example: |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 12 | // |
Gordon Henriksen | c86b677 | 2007-11-04 16:15:04 +0000 | [diff] [blame] | 13 | // * Removes basic blocks with no predecessors. |
| 14 | // * Merges a basic block into its predecessor if there is only one and the |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 15 | // predecessor only has one successor. |
Gordon Henriksen | c86b677 | 2007-11-04 16:15:04 +0000 | [diff] [blame] | 16 | // * Eliminates PHI nodes for basic blocks with a single predecessor. |
| 17 | // * Eliminates a basic block that only contains an unconditional branch. |
Chris Lattner | 4cf4b69 | 2007-12-03 19:43:18 +0000 | [diff] [blame] | 18 | // * Changes invoke instructions to nounwind functions to be calls. |
| 19 | // * Change things like "if (x) if (y)" into "if (x&y)". |
| 20 | // * etc.. |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 21 | // |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 24 | #define DEBUG_TYPE "simplifycfg" |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 25 | #include "llvm/Transforms/Scalar.h" |
| 26 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | fc5c1bb0 | 2004-10-18 03:00:50 +0000 | [diff] [blame] | 27 | #include "llvm/Constants.h" |
| 28 | #include "llvm/Instructions.h" |
Owen Anderson | fa5cbd6 | 2009-07-03 19:42:02 +0000 | [diff] [blame] | 29 | #include "llvm/LLVMContext.h" |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 30 | #include "llvm/Module.h" |
Devang Patel | eaf42ab | 2008-09-23 23:03:40 +0000 | [diff] [blame] | 31 | #include "llvm/Attributes.h" |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 32 | #include "llvm/Support/CFG.h" |
| 33 | #include "llvm/Pass.h" |
Chris Lattner | b42c8f7 | 2007-11-13 07:32:38 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 5509178 | 2007-03-04 04:20:48 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/SmallPtrSet.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | d745602 | 2004-01-09 06:02:20 +0000 | [diff] [blame] | 37 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 38 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 39 | STATISTIC(NumSimpl, "Number of blocks simplified"); |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 41 | namespace { |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 42 | struct CFGSimplifyPass : public FunctionPass { |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 43 | static char ID; // Pass identification, replacement for typeid |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 44 | CFGSimplifyPass() : FunctionPass(&ID) {} |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 45 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 46 | virtual bool runOnFunction(Function &F); |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 47 | }; |
| 48 | } |
| 49 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 50 | char CFGSimplifyPass::ID = 0; |
| 51 | static RegisterPass<CFGSimplifyPass> X("simplifycfg", "Simplify the CFG"); |
| 52 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 53 | // Public interface to the CFGSimplification pass |
Chris Lattner | d745602 | 2004-01-09 06:02:20 +0000 | [diff] [blame] | 54 | FunctionPass *llvm::createCFGSimplificationPass() { |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 55 | return new CFGSimplifyPass(); |
| 56 | } |
| 57 | |
Chris Lattner | 1b12d88 | 2007-11-14 06:19:25 +0000 | [diff] [blame] | 58 | /// ChangeToUnreachable - Insert an unreachable instruction before the specified |
| 59 | /// instruction, making it and the rest of the code in the block dead. |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 60 | static void ChangeToUnreachable(Instruction *I, LLVMContext &Context) { |
Chris Lattner | 1b12d88 | 2007-11-14 06:19:25 +0000 | [diff] [blame] | 61 | BasicBlock *BB = I->getParent(); |
| 62 | // Loop over all of the successors, removing BB's entry from any PHI |
| 63 | // nodes. |
| 64 | for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI) |
| 65 | (*SI)->removePredecessor(BB); |
| 66 | |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 67 | new UnreachableInst(I->getContext(), I); |
Chris Lattner | 1b12d88 | 2007-11-14 06:19:25 +0000 | [diff] [blame] | 68 | |
| 69 | // All instructions after this are dead. |
| 70 | BasicBlock::iterator BBI = I, BBE = BB->end(); |
| 71 | while (BBI != BBE) { |
| 72 | if (!BBI->use_empty()) |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 73 | BBI->replaceAllUsesWith(UndefValue::get(BBI->getType())); |
Chris Lattner | 1b12d88 | 2007-11-14 06:19:25 +0000 | [diff] [blame] | 74 | BB->getInstList().erase(BBI++); |
| 75 | } |
| 76 | } |
| 77 | |
Duncan Sands | 73d4bb6 | 2007-11-22 22:24:59 +0000 | [diff] [blame] | 78 | /// ChangeToCall - Convert the specified invoke into a normal call. |
| 79 | static void ChangeToCall(InvokeInst *II) { |
| 80 | BasicBlock *BB = II->getParent(); |
Gabor Greif | 190390b | 2009-09-03 00:18:58 +0000 | [diff] [blame] | 81 | SmallVector<Value*, 8> Args(II->op_begin(), II->op_end() - 3); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 82 | CallInst *NewCall = CallInst::Create(II->getCalledValue(), Args.begin(), |
| 83 | Args.end(), "", II); |
Duncan Sands | 73d4bb6 | 2007-11-22 22:24:59 +0000 | [diff] [blame] | 84 | NewCall->takeName(II); |
| 85 | NewCall->setCallingConv(II->getCallingConv()); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 86 | NewCall->setAttributes(II->getAttributes()); |
Duncan Sands | 73d4bb6 | 2007-11-22 22:24:59 +0000 | [diff] [blame] | 87 | II->replaceAllUsesWith(NewCall); |
| 88 | |
| 89 | // Follow the call by a branch to the normal destination. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 90 | BranchInst::Create(II->getNormalDest(), II); |
Duncan Sands | 73d4bb6 | 2007-11-22 22:24:59 +0000 | [diff] [blame] | 91 | |
| 92 | // Update PHI nodes in the unwind destination |
| 93 | II->getUnwindDest()->removePredecessor(BB); |
| 94 | BB->getInstList().erase(II); |
Duncan Sands | 73d4bb6 | 2007-11-22 22:24:59 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Chris Lattner | 5509178 | 2007-03-04 04:20:48 +0000 | [diff] [blame] | 97 | static bool MarkAliveBlocks(BasicBlock *BB, |
Owen Anderson | fa5cbd6 | 2009-07-03 19:42:02 +0000 | [diff] [blame] | 98 | SmallPtrSet<BasicBlock*, 128> &Reachable, |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 99 | LLVMContext &Context) { |
Chris Lattner | 1984a328 | 2007-04-05 01:27:02 +0000 | [diff] [blame] | 100 | |
Chris Lattner | b42c8f7 | 2007-11-13 07:32:38 +0000 | [diff] [blame] | 101 | SmallVector<BasicBlock*, 128> Worklist; |
Chris Lattner | 1984a328 | 2007-04-05 01:27:02 +0000 | [diff] [blame] | 102 | Worklist.push_back(BB); |
| 103 | bool Changed = false; |
| 104 | while (!Worklist.empty()) { |
| 105 | BB = Worklist.back(); |
| 106 | Worklist.pop_back(); |
| 107 | |
| 108 | if (!Reachable.insert(BB)) |
| 109 | continue; |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 110 | |
Chris Lattner | 1984a328 | 2007-04-05 01:27:02 +0000 | [diff] [blame] | 111 | // Do a quick scan of the basic block, turning any obviously unreachable |
| 112 | // instructions into LLVM unreachable insts. The instruction combining pass |
Duncan Sands | 73d4bb6 | 2007-11-22 22:24:59 +0000 | [diff] [blame] | 113 | // canonicalizes unreachable insts into stores to null or undef. |
Chris Lattner | 1b12d88 | 2007-11-14 06:19:25 +0000 | [diff] [blame] | 114 | for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E;++BBI){ |
| 115 | if (CallInst *CI = dyn_cast<CallInst>(BBI)) { |
Duncan Sands | 2b0e899 | 2007-12-18 09:59:50 +0000 | [diff] [blame] | 116 | if (CI->doesNotReturn()) { |
Chris Lattner | 1b12d88 | 2007-11-14 06:19:25 +0000 | [diff] [blame] | 117 | // If we found a call to a no-return function, insert an unreachable |
| 118 | // instruction after it. Make sure there isn't *already* one there |
| 119 | // though. |
| 120 | ++BBI; |
| 121 | if (!isa<UnreachableInst>(BBI)) { |
Owen Anderson | fa5cbd6 | 2009-07-03 19:42:02 +0000 | [diff] [blame] | 122 | ChangeToUnreachable(BBI, Context); |
Chris Lattner | 1b12d88 | 2007-11-14 06:19:25 +0000 | [diff] [blame] | 123 | Changed = true; |
Chris Lattner | 1984a328 | 2007-04-05 01:27:02 +0000 | [diff] [blame] | 124 | } |
| 125 | break; |
Chris Lattner | fc5c1bb0 | 2004-10-18 03:00:50 +0000 | [diff] [blame] | 126 | } |
Chris Lattner | 1b12d88 | 2007-11-14 06:19:25 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Chris Lattner | 69074a5 | 2009-06-12 21:01:07 +0000 | [diff] [blame] | 129 | if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) { |
| 130 | Value *Ptr = SI->getOperand(1); |
Dale Johannesen | fa196e3 | 2009-07-14 17:48:25 +0000 | [diff] [blame] | 131 | |
| 132 | if (isa<UndefValue>(Ptr) || |
| 133 | (isa<ConstantPointerNull>(Ptr) && |
Chris Lattner | 8a67ac5 | 2009-08-30 20:06:40 +0000 | [diff] [blame] | 134 | SI->getPointerAddressSpace() == 0)) { |
Dale Johannesen | fa196e3 | 2009-07-14 17:48:25 +0000 | [diff] [blame] | 135 | ChangeToUnreachable(SI, Context); |
| 136 | Changed = true; |
| 137 | break; |
| 138 | } |
Chris Lattner | 69074a5 | 2009-06-12 21:01:07 +0000 | [diff] [blame] | 139 | } |
Chris Lattner | 1b12d88 | 2007-11-14 06:19:25 +0000 | [diff] [blame] | 140 | } |
Chris Lattner | fc5c1bb0 | 2004-10-18 03:00:50 +0000 | [diff] [blame] | 141 | |
Duncan Sands | 73d4bb6 | 2007-11-22 22:24:59 +0000 | [diff] [blame] | 142 | // Turn invokes that call 'nounwind' functions into ordinary calls. |
| 143 | if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) |
Duncan Sands | 2b0e899 | 2007-12-18 09:59:50 +0000 | [diff] [blame] | 144 | if (II->doesNotThrow()) { |
Duncan Sands | 73d4bb6 | 2007-11-22 22:24:59 +0000 | [diff] [blame] | 145 | ChangeToCall(II); |
| 146 | Changed = true; |
| 147 | } |
| 148 | |
Chris Lattner | 1984a328 | 2007-04-05 01:27:02 +0000 | [diff] [blame] | 149 | Changed |= ConstantFoldTerminator(BB); |
| 150 | for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI) |
| 151 | Worklist.push_back(*SI); |
| 152 | } |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 153 | return Changed; |
| 154 | } |
| 155 | |
Devang Patel | 2379089 | 2008-09-08 22:14:17 +0000 | [diff] [blame] | 156 | /// RemoveUnreachableBlocksFromFn - Remove blocks that are not reachable, even |
| 157 | /// if they are in a dead cycle. Return true if a change was made, false |
| 158 | /// otherwise. |
| 159 | static bool RemoveUnreachableBlocksFromFn(Function &F) { |
Chris Lattner | b42c8f7 | 2007-11-13 07:32:38 +0000 | [diff] [blame] | 160 | SmallPtrSet<BasicBlock*, 128> Reachable; |
Owen Anderson | fa5cbd6 | 2009-07-03 19:42:02 +0000 | [diff] [blame] | 161 | bool Changed = MarkAliveBlocks(F.begin(), Reachable, F.getContext()); |
Chris Lattner | b42c8f7 | 2007-11-13 07:32:38 +0000 | [diff] [blame] | 162 | |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 163 | // If there are unreachable blocks in the CFG... |
Bill Wendling | 381802f | 2008-01-26 06:51:24 +0000 | [diff] [blame] | 164 | if (Reachable.size() == F.size()) |
Chris Lattner | b42c8f7 | 2007-11-13 07:32:38 +0000 | [diff] [blame] | 165 | return Changed; |
| 166 | |
| 167 | assert(Reachable.size() < F.size()); |
| 168 | NumSimpl += F.size()-Reachable.size(); |
| 169 | |
| 170 | // Loop over all of the basic blocks that are not reachable, dropping all of |
| 171 | // their internal references... |
Chris Lattner | 13daadb | 2008-02-14 07:39:01 +0000 | [diff] [blame] | 172 | for (Function::iterator BB = ++F.begin(), E = F.end(); BB != E; ++BB) { |
| 173 | if (Reachable.count(BB)) |
| 174 | continue; |
| 175 | |
| 176 | for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI) |
| 177 | if (Reachable.count(*SI)) |
| 178 | (*SI)->removePredecessor(BB); |
| 179 | BB->dropAllReferences(); |
| 180 | } |
Chris Lattner | b42c8f7 | 2007-11-13 07:32:38 +0000 | [diff] [blame] | 181 | |
| 182 | for (Function::iterator I = ++F.begin(); I != F.end();) |
| 183 | if (!Reachable.count(I)) |
| 184 | I = F.getBasicBlockList().erase(I); |
| 185 | else |
| 186 | ++I; |
| 187 | |
| 188 | return true; |
| 189 | } |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 190 | |
Chris Lattner | b42c8f7 | 2007-11-13 07:32:38 +0000 | [diff] [blame] | 191 | /// IterativeSimplifyCFG - Call SimplifyCFG on all the blocks in the function, |
| 192 | /// iterating until no more changes are made. |
| 193 | static bool IterativeSimplifyCFG(Function &F) { |
| 194 | bool Changed = false; |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 195 | bool LocalChange = true; |
| 196 | while (LocalChange) { |
| 197 | LocalChange = false; |
Chris Lattner | b42c8f7 | 2007-11-13 07:32:38 +0000 | [diff] [blame] | 198 | |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 199 | // Loop over all of the basic blocks (except the first one) and remove them |
| 200 | // if they are unneeded... |
| 201 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 202 | for (Function::iterator BBIt = ++F.begin(); BBIt != F.end(); ) { |
| 203 | if (SimplifyCFG(BBIt++)) { |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 204 | LocalChange = true; |
| 205 | ++NumSimpl; |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | Changed |= LocalChange; |
| 209 | } |
Chris Lattner | 573527b | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 210 | return Changed; |
| 211 | } |
Chris Lattner | b42c8f7 | 2007-11-13 07:32:38 +0000 | [diff] [blame] | 212 | |
| 213 | // It is possible that we may require multiple passes over the code to fully |
| 214 | // simplify the CFG. |
| 215 | // |
| 216 | bool CFGSimplifyPass::runOnFunction(Function &F) { |
Devang Patel | 2379089 | 2008-09-08 22:14:17 +0000 | [diff] [blame] | 217 | bool EverChanged = RemoveUnreachableBlocksFromFn(F); |
Chris Lattner | b42c8f7 | 2007-11-13 07:32:38 +0000 | [diff] [blame] | 218 | EverChanged |= IterativeSimplifyCFG(F); |
| 219 | |
| 220 | // If neither pass changed anything, we're done. |
| 221 | if (!EverChanged) return false; |
| 222 | |
| 223 | // IterativeSimplifyCFG can (rarely) make some loops dead. If this happens, |
Devang Patel | 2379089 | 2008-09-08 22:14:17 +0000 | [diff] [blame] | 224 | // RemoveUnreachableBlocksFromFn is needed to nuke them, which means we should |
Chris Lattner | b42c8f7 | 2007-11-13 07:32:38 +0000 | [diff] [blame] | 225 | // iterate between the two optimizations. We structure the code like this to |
| 226 | // avoid reruning IterativeSimplifyCFG if the second pass of |
Devang Patel | 2379089 | 2008-09-08 22:14:17 +0000 | [diff] [blame] | 227 | // RemoveUnreachableBlocksFromFn doesn't do anything. |
| 228 | if (!RemoveUnreachableBlocksFromFn(F)) |
Chris Lattner | b42c8f7 | 2007-11-13 07:32:38 +0000 | [diff] [blame] | 229 | return true; |
| 230 | |
| 231 | do { |
| 232 | EverChanged = IterativeSimplifyCFG(F); |
Devang Patel | 2379089 | 2008-09-08 22:14:17 +0000 | [diff] [blame] | 233 | EverChanged |= RemoveUnreachableBlocksFromFn(F); |
Chris Lattner | b42c8f7 | 2007-11-13 07:32:38 +0000 | [diff] [blame] | 234 | } while (EverChanged); |
| 235 | |
| 236 | return true; |
| 237 | } |