Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 1 | //===- PruneEH.cpp - Pass which deletes unused exception handlers ---------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 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 | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements a simple interprocedural pass which walks the |
| 11 | // call-graph, turning invoke instructions into calls, iff the callee cannot |
| 12 | // throw an exception. It implements this as a bottom-up traversal of the |
| 13 | // call-graph. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Chris Lattner | f52e03c | 2003-11-21 21:54:22 +0000 | [diff] [blame] | 17 | #include "llvm/Transforms/IPO.h" |
Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 18 | #include "llvm/CallGraphSCCPass.h" |
Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 19 | #include "llvm/Constants.h" |
Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 20 | #include "llvm/Function.h" |
| 21 | #include "llvm/Intrinsics.h" |
Misha Brukman | 63b38bd | 2004-07-29 17:30:56 +0000 | [diff] [blame] | 22 | #include "llvm/Instructions.h" |
Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/CallGraph.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CFG.h" |
Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 26 | #include <set> |
Chris Lattner | 88a8a32 | 2004-10-18 15:43:46 +0000 | [diff] [blame] | 27 | #include <algorithm> |
Chris Lattner | f52e03c | 2003-11-21 21:54:22 +0000 | [diff] [blame] | 28 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 29 | |
Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 30 | namespace { |
| 31 | Statistic<> NumRemoved("prune-eh", "Number of invokes removed"); |
Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 32 | Statistic<> NumUnreach("prune-eh", "Number of noreturn calls optimized"); |
Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 33 | |
| 34 | struct PruneEH : public CallGraphSCCPass { |
Chris Lattner | 7f4f773 | 2005-04-26 23:53:25 +0000 | [diff] [blame] | 35 | /// DoesNotUnwind - This set contains all of the functions which we have |
Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 36 | /// determined cannot unwind. |
Chris Lattner | 7f4f773 | 2005-04-26 23:53:25 +0000 | [diff] [blame] | 37 | std::set<CallGraphNode*> DoesNotUnwind; |
Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 38 | |
Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 39 | /// DoesNotReturn - This set contains all of the functions which we have |
| 40 | /// determined cannot return normally (but might unwind). |
| 41 | std::set<CallGraphNode*> DoesNotReturn; |
| 42 | |
Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 43 | // runOnSCC - Analyze the SCC, performing the transformation if possible. |
| 44 | bool runOnSCC(const std::vector<CallGraphNode *> &SCC); |
Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 45 | |
| 46 | bool SimplifyFunction(Function *F); |
| 47 | void DeleteBasicBlock(BasicBlock *BB); |
Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 48 | }; |
| 49 | RegisterOpt<PruneEH> X("prune-eh", "Remove unused exception handling info"); |
| 50 | } |
| 51 | |
Chris Lattner | 4f2cf03 | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 52 | ModulePass *llvm::createPruneEHPass() { return new PruneEH(); } |
Chris Lattner | 75444c7 | 2003-08-31 16:30:07 +0000 | [diff] [blame] | 53 | |
Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 54 | |
| 55 | bool PruneEH::runOnSCC(const std::vector<CallGraphNode *> &SCC) { |
| 56 | CallGraph &CG = getAnalysis<CallGraph>(); |
Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 57 | bool MadeChange = false; |
Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 58 | |
Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 59 | // First pass, scan all of the functions in the SCC, simplifying them |
| 60 | // according to what we know. |
| 61 | for (unsigned i = 0, e = SCC.size(); i != e; ++i) |
| 62 | if (Function *F = SCC[i]->getFunction()) |
| 63 | MadeChange |= SimplifyFunction(F); |
| 64 | |
| 65 | // Next, check to see if any callees might throw or if there are any external |
Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 66 | // functions in this SCC: if so, we cannot prune any functions in this SCC. |
Chris Lattner | 04ecefe | 2003-09-08 19:44:26 +0000 | [diff] [blame] | 67 | // If this SCC includes the unwind instruction, we KNOW it throws, so |
Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 68 | // obviously the SCC might throw. |
| 69 | // |
Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 70 | bool SCCMightUnwind = false, SCCMightReturn = false; |
| 71 | for (unsigned i = 0, e = SCC.size(); |
| 72 | (!SCCMightUnwind || !SCCMightReturn) && i != e; ++i) { |
Chris Lattner | d041dcd | 2004-04-12 04:06:38 +0000 | [diff] [blame] | 73 | Function *F = SCC[i]->getFunction(); |
| 74 | if (F == 0 || (F->isExternal() && !F->getIntrinsicID())) { |
Chris Lattner | 7f4f773 | 2005-04-26 23:53:25 +0000 | [diff] [blame] | 75 | SCCMightUnwind = true; |
Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 76 | SCCMightReturn = true; |
Chris Lattner | d041dcd | 2004-04-12 04:06:38 +0000 | [diff] [blame] | 77 | } else { |
Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 78 | if (F->isExternal()) |
| 79 | SCCMightReturn = true; |
| 80 | |
Chris Lattner | d041dcd | 2004-04-12 04:06:38 +0000 | [diff] [blame] | 81 | // Check to see if this function performs an unwind or calls an |
| 82 | // unwinding function. |
| 83 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { |
| 84 | if (isa<UnwindInst>(BB->getTerminator())) { // Uses unwind! |
Chris Lattner | 7f4f773 | 2005-04-26 23:53:25 +0000 | [diff] [blame] | 85 | SCCMightUnwind = true; |
Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 86 | } else if (isa<ReturnInst>(BB->getTerminator())) { |
| 87 | SCCMightReturn = true; |
Chris Lattner | d041dcd | 2004-04-12 04:06:38 +0000 | [diff] [blame] | 88 | } |
Chris Lattner | 56997dd | 2004-02-08 21:15:59 +0000 | [diff] [blame] | 89 | |
Chris Lattner | d041dcd | 2004-04-12 04:06:38 +0000 | [diff] [blame] | 90 | // Invoke instructions don't allow unwinding to continue, so we are |
| 91 | // only interested in call instructions. |
Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 92 | if (!SCCMightUnwind) |
| 93 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 94 | if (CallInst *CI = dyn_cast<CallInst>(I)) { |
| 95 | if (Function *Callee = CI->getCalledFunction()) { |
| 96 | CallGraphNode *CalleeNode = CG[Callee]; |
| 97 | // If the callee is outside our current SCC, or if it is not |
| 98 | // known to throw, then we might throw also. |
| 99 | if (std::find(SCC.begin(), SCC.end(), CalleeNode) == SCC.end()&& |
| 100 | !DoesNotUnwind.count(CalleeNode)) { |
| 101 | SCCMightUnwind = true; |
| 102 | break; |
| 103 | } |
| 104 | } else { |
| 105 | // Indirect call, it might throw. |
Chris Lattner | 7f4f773 | 2005-04-26 23:53:25 +0000 | [diff] [blame] | 106 | SCCMightUnwind = true; |
Chris Lattner | 56997dd | 2004-02-08 21:15:59 +0000 | [diff] [blame] | 107 | break; |
| 108 | } |
| 109 | } |
Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 110 | if (SCCMightUnwind && SCCMightReturn) break; |
Chris Lattner | 5a6fa29 | 2003-09-15 02:22:50 +0000 | [diff] [blame] | 111 | } |
Chris Lattner | d041dcd | 2004-04-12 04:06:38 +0000 | [diff] [blame] | 112 | } |
| 113 | } |
Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 114 | |
| 115 | // If the SCC doesn't unwind or doesn't throw, note this fact. |
| 116 | if (!SCCMightUnwind) |
| 117 | for (unsigned i = 0, e = SCC.size(); i != e; ++i) |
| 118 | DoesNotUnwind.insert(SCC[i]); |
| 119 | if (!SCCMightReturn) |
| 120 | for (unsigned i = 0, e = SCC.size(); i != e; ++i) |
| 121 | DoesNotReturn.insert(SCC[i]); |
Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 122 | |
| 123 | for (unsigned i = 0, e = SCC.size(); i != e; ++i) { |
Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 124 | // Convert any invoke instructions to non-throwing functions in this node |
| 125 | // into call instructions with a branch. This makes the exception blocks |
| 126 | // dead. |
| 127 | if (Function *F = SCC[i]->getFunction()) |
Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 128 | MadeChange |= SimplifyFunction(F); |
Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 131 | return MadeChange; |
Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 132 | } |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 133 | |
Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 134 | |
| 135 | // SimplifyFunction - Given information about callees, simplify the specified |
| 136 | // function if we have invokes to non-unwinding functions or code after calls to |
| 137 | // no-return functions. |
| 138 | bool PruneEH::SimplifyFunction(Function *F) { |
| 139 | CallGraph &CG = getAnalysis<CallGraph>(); |
| 140 | bool MadeChange = false; |
| 141 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { |
| 142 | if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) |
| 143 | if (Function *F = II->getCalledFunction()) |
| 144 | if (DoesNotUnwind.count(CG[F])) { |
| 145 | // Insert a call instruction before the invoke... |
| 146 | std::string Name = II->getName(); II->setName(""); |
| 147 | Value *Call = new CallInst(II->getCalledValue(), |
| 148 | std::vector<Value*>(II->op_begin()+3, |
| 149 | II->op_end()), |
| 150 | Name, II); |
| 151 | |
| 152 | // Anything that used the value produced by the invoke instruction |
| 153 | // now uses the value produced by the call instruction. |
| 154 | II->replaceAllUsesWith(Call); |
| 155 | BasicBlock *UnwindBlock = II->getUnwindDest(); |
| 156 | UnwindBlock->removePredecessor(II->getParent()); |
| 157 | |
| 158 | // Insert a branch to the normal destination right before the |
| 159 | // invoke. |
| 160 | new BranchInst(II->getNormalDest(), II); |
| 161 | |
| 162 | // Finally, delete the invoke instruction! |
| 163 | BB->getInstList().pop_back(); |
| 164 | |
| 165 | // If the unwind block is now dead, nuke it. |
| 166 | if (pred_begin(UnwindBlock) == pred_end(UnwindBlock)) |
| 167 | DeleteBasicBlock(UnwindBlock); // Delete the new BB. |
| 168 | |
| 169 | ++NumRemoved; |
| 170 | MadeChange = true; |
| 171 | } |
| 172 | |
Chris Lattner | 36ffb1f | 2005-04-27 20:12:17 +0000 | [diff] [blame^] | 173 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) |
Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 174 | if (CallInst *CI = dyn_cast<CallInst>(I++)) |
| 175 | if (Function *Callee = CI->getCalledFunction()) |
| 176 | if (DoesNotReturn.count(CG[Callee]) && !isa<UnreachableInst>(I)) { |
| 177 | // This call calls a function that cannot return. Insert an |
| 178 | // unreachable instruction after it and simplify the code. Do this |
| 179 | // by splitting the BB, adding the unreachable, then deleting the |
| 180 | // new BB. |
| 181 | BasicBlock *New = BB->splitBasicBlock(I); |
| 182 | |
| 183 | // Remove the uncond branch and add an unreachable. |
| 184 | BB->getInstList().pop_back(); |
| 185 | new UnreachableInst(BB); |
| 186 | |
| 187 | DeleteBasicBlock(New); // Delete the new BB. |
| 188 | MadeChange = true; |
| 189 | ++NumUnreach; |
| 190 | break; |
| 191 | } |
| 192 | |
| 193 | } |
| 194 | return MadeChange; |
| 195 | } |
| 196 | |
| 197 | /// DeleteBasicBlock - remove the specified basic block from the program, |
| 198 | /// updating the callgraph to reflect any now-obsolete edges due to calls that |
| 199 | /// exist in the BB. |
| 200 | void PruneEH::DeleteBasicBlock(BasicBlock *BB) { |
| 201 | assert(pred_begin(BB) == pred_end(BB) && "BB is not dead!"); |
| 202 | CallGraph &CG = getAnalysis<CallGraph>(); |
| 203 | |
| 204 | CallGraphNode *CGN = CG[BB->getParent()]; |
| 205 | for (BasicBlock::iterator I = BB->end(), E = BB->begin(); I != E; ) { |
| 206 | --I; |
| 207 | if (CallInst *CI = dyn_cast<CallInst>(I)) { |
| 208 | if (Function *Callee = CI->getCalledFunction()) |
| 209 | CGN->removeCallEdgeTo(CG[Callee]); |
| 210 | } else if (InvokeInst *II = dyn_cast<InvokeInst>(I)) { |
| 211 | if (Function *Callee = II->getCalledFunction()) |
| 212 | CGN->removeCallEdgeTo(CG[Callee]); |
| 213 | } |
| 214 | if (!I->use_empty()) |
| 215 | I->replaceAllUsesWith(UndefValue::get(I->getType())); |
| 216 | } |
| 217 | |
| 218 | // Get the list of successors of this block. |
| 219 | std::vector<BasicBlock*> Succs(succ_begin(BB), succ_end(BB)); |
| 220 | |
| 221 | for (unsigned i = 0, e = Succs.size(); i != e; ++i) |
| 222 | Succs[i]->removePredecessor(BB); |
| 223 | |
| 224 | BB->eraseFromParent(); |
| 225 | } |