| 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 | // | 
| Chris Lattner | f3ebc3f | 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 | 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 | 
| Duncan Sands | 9f76be6 | 2007-12-10 19:09:40 +0000 | [diff] [blame] | 12 | // throw an exception, and marking functions 'nounwind' if they cannot throw. | 
|  | 13 | // It implements this as a bottom-up traversal of the call-graph. | 
| Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 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" | 
| Owen Anderson | 605a8c7 | 2009-07-06 01:34:54 +0000 | [diff] [blame] | 22 | #include "llvm/LLVMContext.h" | 
| Misha Brukman | 63b38bd | 2004-07-29 17:30:56 +0000 | [diff] [blame] | 23 | #include "llvm/Instructions.h" | 
| Dale Johannesen | 2050968 | 2009-03-19 18:03:56 +0000 | [diff] [blame] | 24 | #include "llvm/IntrinsicInst.h" | 
| Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/CallGraph.h" | 
| Duncan Sands | 57512a1 | 2008-09-29 14:59:04 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/SmallPtrSet.h" | 
| Chris Lattner | a06a8fd | 2007-02-13 02:10:56 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/SmallVector.h" | 
| Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/Statistic.h" | 
| Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 29 | #include "llvm/Support/CFG.h" | 
| Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 30 | #include <set> | 
| Chris Lattner | 88a8a32 | 2004-10-18 15:43:46 +0000 | [diff] [blame] | 31 | #include <algorithm> | 
| Chris Lattner | f52e03c | 2003-11-21 21:54:22 +0000 | [diff] [blame] | 32 | using namespace llvm; | 
| Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 33 |  | 
| Chris Lattner | 1631bcb | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 34 | STATISTIC(NumRemoved, "Number of invokes removed"); | 
|  | 35 | STATISTIC(NumUnreach, "Number of noreturn calls optimized"); | 
| Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 36 |  | 
| Chris Lattner | 1631bcb | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 37 | namespace { | 
| Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 38 | struct PruneEH : public CallGraphSCCPass { | 
| Nick Lewycky | e7da2d6 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 39 | static char ID; // Pass identification, replacement for typeid | 
| Dan Gohman | a79db30 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 40 | PruneEH() : CallGraphSCCPass(&ID) {} | 
| Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 41 |  | 
| Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 42 | // runOnSCC - Analyze the SCC, performing the transformation if possible. | 
| Chris Lattner | 4422d31 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 43 | bool runOnSCC(CallGraphSCC &SCC); | 
| Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 44 |  | 
|  | 45 | bool SimplifyFunction(Function *F); | 
|  | 46 | void DeleteBasicBlock(BasicBlock *BB); | 
| Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 47 | }; | 
| Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 48 | } | 
|  | 49 |  | 
| Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 50 | char PruneEH::ID = 0; | 
|  | 51 | static RegisterPass<PruneEH> | 
|  | 52 | X("prune-eh", "Remove unused exception handling info"); | 
|  | 53 |  | 
| Devang Patel | 13058a5 | 2007-01-26 00:47:38 +0000 | [diff] [blame] | 54 | Pass *llvm::createPruneEHPass() { return new PruneEH(); } | 
| Chris Lattner | 75444c7 | 2003-08-31 16:30:07 +0000 | [diff] [blame] | 55 |  | 
| Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 56 |  | 
| Chris Lattner | 4422d31 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 57 | bool PruneEH::runOnSCC(CallGraphSCC &SCC) { | 
| Duncan Sands | 57512a1 | 2008-09-29 14:59:04 +0000 | [diff] [blame] | 58 | SmallPtrSet<CallGraphNode *, 8> SCCNodes; | 
| Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 59 | CallGraph &CG = getAnalysis<CallGraph>(); | 
| Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 60 | bool MadeChange = false; | 
| Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 61 |  | 
| Duncan Sands | 57512a1 | 2008-09-29 14:59:04 +0000 | [diff] [blame] | 62 | // Fill SCCNodes with the elements of the SCC.  Used for quickly | 
|  | 63 | // looking up whether a given CallGraphNode is in this SCC. | 
| Chris Lattner | 4422d31 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 64 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) | 
|  | 65 | SCCNodes.insert(*I); | 
| Duncan Sands | 57512a1 | 2008-09-29 14:59:04 +0000 | [diff] [blame] | 66 |  | 
| Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 67 | // First pass, scan all of the functions in the SCC, simplifying them | 
|  | 68 | // according to what we know. | 
| Chris Lattner | 4422d31 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 69 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) | 
|  | 70 | if (Function *F = (*I)->getFunction()) | 
| Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 71 | MadeChange |= SimplifyFunction(F); | 
|  | 72 |  | 
|  | 73 | // 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] | 74 | // functions in this SCC: if so, we cannot prune any functions in this SCC. | 
| Dale Johannesen | e7f5bc2 | 2008-05-16 21:31:48 +0000 | [diff] [blame] | 75 | // Definitions that are weak and not declared non-throwing might be | 
|  | 76 | // overridden at linktime with something that throws, so assume that. | 
| Chris Lattner | 04ecefe | 2003-09-08 19:44:26 +0000 | [diff] [blame] | 77 | // If this SCC includes the unwind instruction, we KNOW it throws, so | 
| Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 78 | // obviously the SCC might throw. | 
|  | 79 | // | 
| Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 80 | bool SCCMightUnwind = false, SCCMightReturn = false; | 
| Chris Lattner | 4422d31 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 81 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); | 
|  | 82 | (!SCCMightUnwind || !SCCMightReturn) && I != E; ++I) { | 
|  | 83 | Function *F = (*I)->getFunction(); | 
| Dale Johannesen | 5610dab | 2008-05-16 23:18:52 +0000 | [diff] [blame] | 84 | if (F == 0) { | 
| 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 | SCCMightReturn = true; | 
| Duncan Sands | 08d9117 | 2008-09-29 11:25:42 +0000 | [diff] [blame] | 87 | } else if (F->isDeclaration() || F->mayBeOverridden()) { | 
| Duncan Sands | 3353ed0 | 2007-12-18 09:59:50 +0000 | [diff] [blame] | 88 | SCCMightUnwind |= !F->doesNotThrow(); | 
|  | 89 | SCCMightReturn |= !F->doesNotReturn(); | 
| Chris Lattner | d041dcd | 2004-04-12 04:06:38 +0000 | [diff] [blame] | 90 | } else { | 
| Duncan Sands | 3353ed0 | 2007-12-18 09:59:50 +0000 | [diff] [blame] | 91 | bool CheckUnwind = !SCCMightUnwind && !F->doesNotThrow(); | 
|  | 92 | bool CheckReturn = !SCCMightReturn && !F->doesNotReturn(); | 
| Duncan Sands | 9f76be6 | 2007-12-10 19:09:40 +0000 | [diff] [blame] | 93 |  | 
|  | 94 | if (!CheckUnwind && !CheckReturn) | 
|  | 95 | continue; | 
| Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 96 |  | 
| Chris Lattner | d041dcd | 2004-04-12 04:06:38 +0000 | [diff] [blame] | 97 | // Check to see if this function performs an unwind or calls an | 
|  | 98 | // unwinding function. | 
|  | 99 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { | 
| Duncan Sands | 9f76be6 | 2007-12-10 19:09:40 +0000 | [diff] [blame] | 100 | if (CheckUnwind && isa<UnwindInst>(BB->getTerminator())) { | 
|  | 101 | // Uses unwind! | 
| Chris Lattner | 7f4f773 | 2005-04-26 23:53:25 +0000 | [diff] [blame] | 102 | SCCMightUnwind = true; | 
| Duncan Sands | 9f76be6 | 2007-12-10 19:09:40 +0000 | [diff] [blame] | 103 | } else if (CheckReturn && isa<ReturnInst>(BB->getTerminator())) { | 
| Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 104 | SCCMightReturn = true; | 
| Chris Lattner | d041dcd | 2004-04-12 04:06:38 +0000 | [diff] [blame] | 105 | } | 
| Chris Lattner | 56997dd | 2004-02-08 21:15:59 +0000 | [diff] [blame] | 106 |  | 
| Chris Lattner | d041dcd | 2004-04-12 04:06:38 +0000 | [diff] [blame] | 107 | // Invoke instructions don't allow unwinding to continue, so we are | 
|  | 108 | // only interested in call instructions. | 
| Duncan Sands | 9f76be6 | 2007-12-10 19:09:40 +0000 | [diff] [blame] | 109 | if (CheckUnwind && !SCCMightUnwind) | 
| Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 110 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) | 
|  | 111 | if (CallInst *CI = dyn_cast<CallInst>(I)) { | 
| Duncan Sands | 3353ed0 | 2007-12-18 09:59:50 +0000 | [diff] [blame] | 112 | if (CI->doesNotThrow()) { | 
| Duncan Sands | 9f76be6 | 2007-12-10 19:09:40 +0000 | [diff] [blame] | 113 | // This call cannot throw. | 
|  | 114 | } else if (Function *Callee = CI->getCalledFunction()) { | 
| Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 115 | CallGraphNode *CalleeNode = CG[Callee]; | 
| Duncan Sands | 9f76be6 | 2007-12-10 19:09:40 +0000 | [diff] [blame] | 116 | // If the callee is outside our current SCC then we may | 
|  | 117 | // throw because it might. | 
| Duncan Sands | 57512a1 | 2008-09-29 14:59:04 +0000 | [diff] [blame] | 118 | if (!SCCNodes.count(CalleeNode)) { | 
| Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 119 | SCCMightUnwind = true; | 
|  | 120 | break; | 
|  | 121 | } | 
|  | 122 | } else { | 
|  | 123 | // Indirect call, it might throw. | 
| Chris Lattner | 7f4f773 | 2005-04-26 23:53:25 +0000 | [diff] [blame] | 124 | SCCMightUnwind = true; | 
| Chris Lattner | 56997dd | 2004-02-08 21:15:59 +0000 | [diff] [blame] | 125 | break; | 
|  | 126 | } | 
|  | 127 | } | 
| Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 128 | if (SCCMightUnwind && SCCMightReturn) break; | 
| Chris Lattner | 5a6fa29 | 2003-09-15 02:22:50 +0000 | [diff] [blame] | 129 | } | 
| Chris Lattner | d041dcd | 2004-04-12 04:06:38 +0000 | [diff] [blame] | 130 | } | 
|  | 131 | } | 
| Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 132 |  | 
|  | 133 | // If the SCC doesn't unwind or doesn't throw, note this fact. | 
| Duncan Sands | 9f76be6 | 2007-12-10 19:09:40 +0000 | [diff] [blame] | 134 | if (!SCCMightUnwind || !SCCMightReturn) | 
| Chris Lattner | 4422d31 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 135 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { | 
| Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 136 | Attributes NewAttributes = Attribute::None; | 
| Duncan Sands | 9f76be6 | 2007-12-10 19:09:40 +0000 | [diff] [blame] | 137 |  | 
|  | 138 | if (!SCCMightUnwind) | 
| Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 139 | NewAttributes |= Attribute::NoUnwind; | 
| Duncan Sands | 9f76be6 | 2007-12-10 19:09:40 +0000 | [diff] [blame] | 140 | if (!SCCMightReturn) | 
| Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 141 | NewAttributes |= Attribute::NoReturn; | 
| Duncan Sands | 9f76be6 | 2007-12-10 19:09:40 +0000 | [diff] [blame] | 142 |  | 
| Chris Lattner | 4422d31 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 143 | Function *F = (*I)->getFunction(); | 
|  | 144 | const AttrListPtr &PAL = F->getAttributes(); | 
| Devang Patel | a05633e | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 145 | const AttrListPtr &NPAL = PAL.addAttr(~0, NewAttributes); | 
| Duncan Sands | 6dd02b5 | 2008-09-05 09:08:37 +0000 | [diff] [blame] | 146 | if (PAL != NPAL) { | 
|  | 147 | MadeChange = true; | 
| Chris Lattner | 4422d31 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 148 | F->setAttributes(NPAL); | 
| Duncan Sands | 6dd02b5 | 2008-09-05 09:08:37 +0000 | [diff] [blame] | 149 | } | 
| Duncan Sands | 9f76be6 | 2007-12-10 19:09:40 +0000 | [diff] [blame] | 150 | } | 
| Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 151 |  | 
| Chris Lattner | 4422d31 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 152 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { | 
| Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 153 | // Convert any invoke instructions to non-throwing functions in this node | 
|  | 154 | // into call instructions with a branch.  This makes the exception blocks | 
|  | 155 | // dead. | 
| Chris Lattner | 4422d31 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 156 | if (Function *F = (*I)->getFunction()) | 
| Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 157 | MadeChange |= SimplifyFunction(F); | 
| Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 158 | } | 
|  | 159 |  | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 160 | return MadeChange; | 
| Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 161 | } | 
| Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 162 |  | 
| Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 163 |  | 
|  | 164 | // SimplifyFunction - Given information about callees, simplify the specified | 
|  | 165 | // function if we have invokes to non-unwinding functions or code after calls to | 
|  | 166 | // no-return functions. | 
|  | 167 | bool PruneEH::SimplifyFunction(Function *F) { | 
| Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 168 | bool MadeChange = false; | 
|  | 169 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { | 
|  | 170 | if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) | 
| Duncan Sands | 3353ed0 | 2007-12-18 09:59:50 +0000 | [diff] [blame] | 171 | if (II->doesNotThrow()) { | 
| Gabor Greif | a2fbc0a | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 172 | SmallVector<Value*, 8> Args(II->op_begin(), II->op_end() - 3); | 
| Duncan Sands | 9f76be6 | 2007-12-10 19:09:40 +0000 | [diff] [blame] | 173 | // Insert a call instruction before the invoke. | 
| Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 174 | CallInst *Call = CallInst::Create(II->getCalledValue(), | 
|  | 175 | Args.begin(), Args.end(), "", II); | 
| Duncan Sands | 9f76be6 | 2007-12-10 19:09:40 +0000 | [diff] [blame] | 176 | Call->takeName(II); | 
|  | 177 | Call->setCallingConv(II->getCallingConv()); | 
| Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 178 | Call->setAttributes(II->getAttributes()); | 
| Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 179 |  | 
| Duncan Sands | 9f76be6 | 2007-12-10 19:09:40 +0000 | [diff] [blame] | 180 | // Anything that used the value produced by the invoke instruction | 
| Chris Lattner | 063d065 | 2009-09-01 06:31:31 +0000 | [diff] [blame] | 181 | // now uses the value produced by the call instruction.  Note that we | 
|  | 182 | // do this even for void functions and calls with no uses so that the | 
|  | 183 | // callgraph edge is updated. | 
| Duncan Sands | 9f76be6 | 2007-12-10 19:09:40 +0000 | [diff] [blame] | 184 | II->replaceAllUsesWith(Call); | 
|  | 185 | BasicBlock *UnwindBlock = II->getUnwindDest(); | 
|  | 186 | UnwindBlock->removePredecessor(II->getParent()); | 
| Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 187 |  | 
| Duncan Sands | 9f76be6 | 2007-12-10 19:09:40 +0000 | [diff] [blame] | 188 | // Insert a branch to the normal destination right before the | 
|  | 189 | // invoke. | 
| Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 190 | BranchInst::Create(II->getNormalDest(), II); | 
| Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 191 |  | 
| Duncan Sands | 9f76be6 | 2007-12-10 19:09:40 +0000 | [diff] [blame] | 192 | // Finally, delete the invoke instruction! | 
|  | 193 | BB->getInstList().pop_back(); | 
| Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 194 |  | 
| Duncan Sands | 9f76be6 | 2007-12-10 19:09:40 +0000 | [diff] [blame] | 195 | // If the unwind block is now dead, nuke it. | 
|  | 196 | if (pred_begin(UnwindBlock) == pred_end(UnwindBlock)) | 
|  | 197 | DeleteBasicBlock(UnwindBlock);  // Delete the new BB. | 
| Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 198 |  | 
| Duncan Sands | 9f76be6 | 2007-12-10 19:09:40 +0000 | [diff] [blame] | 199 | ++NumRemoved; | 
|  | 200 | MadeChange = true; | 
| Nick Lewycky | 929703b | 2008-03-09 17:11:18 +0000 | [diff] [blame] | 201 | } | 
| Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 202 |  | 
| Chris Lattner | 36ffb1f | 2005-04-27 20:12:17 +0000 | [diff] [blame] | 203 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) | 
| Nick Lewycky | 4d43d3c | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 204 | if (CallInst *CI = dyn_cast<CallInst>(I++)) | 
| Duncan Sands | 3353ed0 | 2007-12-18 09:59:50 +0000 | [diff] [blame] | 205 | if (CI->doesNotReturn() && !isa<UnreachableInst>(I)) { | 
| Duncan Sands | 9f76be6 | 2007-12-10 19:09:40 +0000 | [diff] [blame] | 206 | // This call calls a function that cannot return.  Insert an | 
|  | 207 | // unreachable instruction after it and simplify the code.  Do this | 
|  | 208 | // by splitting the BB, adding the unreachable, then deleting the | 
|  | 209 | // new BB. | 
|  | 210 | BasicBlock *New = BB->splitBasicBlock(I); | 
| Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 211 |  | 
| Duncan Sands | 9f76be6 | 2007-12-10 19:09:40 +0000 | [diff] [blame] | 212 | // Remove the uncond branch and add an unreachable. | 
|  | 213 | BB->getInstList().pop_back(); | 
| Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 214 | new UnreachableInst(BB->getContext(), BB); | 
| Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 215 |  | 
| Duncan Sands | 9f76be6 | 2007-12-10 19:09:40 +0000 | [diff] [blame] | 216 | DeleteBasicBlock(New);  // Delete the new BB. | 
|  | 217 | MadeChange = true; | 
|  | 218 | ++NumUnreach; | 
|  | 219 | break; | 
| Nick Lewycky | 0ac65c3 | 2008-03-09 17:13:05 +0000 | [diff] [blame] | 220 | } | 
| Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 221 | } | 
| Nick Lewycky | 4d43d3c | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 222 |  | 
| Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 223 | return MadeChange; | 
|  | 224 | } | 
|  | 225 |  | 
|  | 226 | /// DeleteBasicBlock - remove the specified basic block from the program, | 
|  | 227 | /// updating the callgraph to reflect any now-obsolete edges due to calls that | 
|  | 228 | /// exist in the BB. | 
|  | 229 | void PruneEH::DeleteBasicBlock(BasicBlock *BB) { | 
|  | 230 | assert(pred_begin(BB) == pred_end(BB) && "BB is not dead!"); | 
|  | 231 | CallGraph &CG = getAnalysis<CallGraph>(); | 
|  | 232 |  | 
|  | 233 | CallGraphNode *CGN = CG[BB->getParent()]; | 
|  | 234 | for (BasicBlock::iterator I = BB->end(), E = BB->begin(); I != E; ) { | 
|  | 235 | --I; | 
| Dale Johannesen | 2050968 | 2009-03-19 18:03:56 +0000 | [diff] [blame] | 236 | if (CallInst *CI = dyn_cast<CallInst>(I)) { | 
|  | 237 | if (!isa<DbgInfoIntrinsic>(I)) | 
|  | 238 | CGN->removeCallEdgeFor(CI); | 
|  | 239 | } else if (InvokeInst *II = dyn_cast<InvokeInst>(I)) | 
| Duncan Sands | 46911f1 | 2008-09-08 11:05:51 +0000 | [diff] [blame] | 240 | CGN->removeCallEdgeFor(II); | 
| Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 241 | if (!I->use_empty()) | 
| Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 242 | I->replaceAllUsesWith(UndefValue::get(I->getType())); | 
| Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 243 | } | 
|  | 244 |  | 
|  | 245 | // Get the list of successors of this block. | 
|  | 246 | std::vector<BasicBlock*> Succs(succ_begin(BB), succ_end(BB)); | 
|  | 247 |  | 
|  | 248 | for (unsigned i = 0, e = Succs.size(); i != e; ++i) | 
|  | 249 | Succs[i]->removePredecessor(BB); | 
| Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 250 |  | 
| Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 251 | BB->eraseFromParent(); | 
|  | 252 | } |