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