Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 1 | //===- PruneEH.cpp - Pass which deletes unused exception handlers ---------===// |
John Criswell | 482202a | 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 | 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" |
| 19 | #include "llvm/Function.h" |
| 20 | #include "llvm/Intrinsics.h" |
Misha Brukman | 63b38bd | 2004-07-29 17:30:56 +0000 | [diff] [blame] | 21 | #include "llvm/Instructions.h" |
Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/CallGraph.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 24 | #include <set> |
Chris Lattner | f52e03c | 2003-11-21 21:54:22 +0000 | [diff] [blame] | 25 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 26 | |
Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 27 | namespace { |
| 28 | Statistic<> NumRemoved("prune-eh", "Number of invokes removed"); |
| 29 | |
| 30 | struct PruneEH : public CallGraphSCCPass { |
| 31 | /// DoesNotThrow - This set contains all of the functions which we have |
| 32 | /// determined cannot throw exceptions. |
| 33 | std::set<CallGraphNode*> DoesNotThrow; |
| 34 | |
| 35 | // runOnSCC - Analyze the SCC, performing the transformation if possible. |
| 36 | bool runOnSCC(const std::vector<CallGraphNode *> &SCC); |
| 37 | }; |
| 38 | RegisterOpt<PruneEH> X("prune-eh", "Remove unused exception handling info"); |
| 39 | } |
| 40 | |
Chris Lattner | 4f2cf03 | 2004-09-20 04:48:05 +0000 | [diff] [blame^] | 41 | ModulePass *llvm::createPruneEHPass() { return new PruneEH(); } |
Chris Lattner | 75444c7 | 2003-08-31 16:30:07 +0000 | [diff] [blame] | 42 | |
Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 43 | |
| 44 | bool PruneEH::runOnSCC(const std::vector<CallGraphNode *> &SCC) { |
| 45 | CallGraph &CG = getAnalysis<CallGraph>(); |
| 46 | |
| 47 | // First, check to see if any callees might throw or if there are any external |
| 48 | // 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] | 49 | // If this SCC includes the unwind instruction, we KNOW it throws, so |
Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 50 | // obviously the SCC might throw. |
| 51 | // |
| 52 | bool SCCMightThrow = false; |
Chris Lattner | d041dcd | 2004-04-12 04:06:38 +0000 | [diff] [blame] | 53 | for (unsigned i = 0, e = SCC.size(); !SCCMightThrow && i != e; ++i) { |
| 54 | Function *F = SCC[i]->getFunction(); |
| 55 | if (F == 0 || (F->isExternal() && !F->getIntrinsicID())) { |
| 56 | SCCMightThrow = true; |
| 57 | } else { |
| 58 | // Check to see if this function performs an unwind or calls an |
| 59 | // unwinding function. |
| 60 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { |
| 61 | if (isa<UnwindInst>(BB->getTerminator())) { // Uses unwind! |
| 62 | SCCMightThrow = true; |
| 63 | break; |
| 64 | } |
Chris Lattner | 56997dd | 2004-02-08 21:15:59 +0000 | [diff] [blame] | 65 | |
Chris Lattner | d041dcd | 2004-04-12 04:06:38 +0000 | [diff] [blame] | 66 | // Invoke instructions don't allow unwinding to continue, so we are |
| 67 | // only interested in call instructions. |
| 68 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 69 | if (CallInst *CI = dyn_cast<CallInst>(I)) { |
| 70 | if (Function *Callee = CI->getCalledFunction()) { |
| 71 | CallGraphNode *CalleeNode = CG[Callee]; |
| 72 | // If the callee is outside our current SCC, or if it is not |
| 73 | // known to throw, then we might throw also. |
| 74 | if (std::find(SCC.begin(), SCC.end(), CalleeNode) == SCC.end()&& |
| 75 | !DoesNotThrow.count(CalleeNode)) { |
Chris Lattner | 56997dd | 2004-02-08 21:15:59 +0000 | [diff] [blame] | 76 | SCCMightThrow = true; |
| 77 | break; |
| 78 | } |
Chris Lattner | d041dcd | 2004-04-12 04:06:38 +0000 | [diff] [blame] | 79 | |
| 80 | } else { |
| 81 | // Indirect call, it might throw. |
| 82 | SCCMightThrow = true; |
| 83 | break; |
Chris Lattner | 56997dd | 2004-02-08 21:15:59 +0000 | [diff] [blame] | 84 | } |
Chris Lattner | d041dcd | 2004-04-12 04:06:38 +0000 | [diff] [blame] | 85 | } |
| 86 | if (SCCMightThrow) break; |
Chris Lattner | 5a6fa29 | 2003-09-15 02:22:50 +0000 | [diff] [blame] | 87 | } |
Chris Lattner | d041dcd | 2004-04-12 04:06:38 +0000 | [diff] [blame] | 88 | } |
| 89 | } |
Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 90 | bool MadeChange = false; |
| 91 | |
| 92 | for (unsigned i = 0, e = SCC.size(); i != e; ++i) { |
| 93 | // If the SCC can't throw, remember this for callers... |
| 94 | if (!SCCMightThrow) |
| 95 | DoesNotThrow.insert(SCC[i]); |
| 96 | |
| 97 | // Convert any invoke instructions to non-throwing functions in this node |
| 98 | // into call instructions with a branch. This makes the exception blocks |
| 99 | // dead. |
| 100 | if (Function *F = SCC[i]->getFunction()) |
| 101 | for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) |
| 102 | if (InvokeInst *II = dyn_cast<InvokeInst>(I->getTerminator())) |
| 103 | if (Function *F = II->getCalledFunction()) |
| 104 | if (DoesNotThrow.count(CG[F])) { |
| 105 | // Insert a call instruction before the invoke... |
| 106 | std::string Name = II->getName(); II->setName(""); |
| 107 | Value *Call = new CallInst(II->getCalledValue(), |
| 108 | std::vector<Value*>(II->op_begin()+3, |
| 109 | II->op_end()), |
| 110 | Name, II); |
| 111 | |
| 112 | // Anything that used the value produced by the invoke instruction |
| 113 | // now uses the value produced by the call instruction. |
| 114 | II->replaceAllUsesWith(Call); |
Chris Lattner | fae8ab3 | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 115 | II->getUnwindDest()->removePredecessor(II->getParent()); |
Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 116 | |
| 117 | // Insert a branch to the normal destination right before the |
| 118 | // invoke. |
| 119 | new BranchInst(II->getNormalDest(), II); |
| 120 | |
| 121 | // Finally, delete the invoke instruction! |
| 122 | I->getInstList().pop_back(); |
| 123 | |
| 124 | ++NumRemoved; |
| 125 | MadeChange = true; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | return MadeChange; |
| 130 | } |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 131 | |