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