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