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 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 8 | // |
| 9 | // This file implements a simple interprocedural pass which walks the |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 10 | // call-graph, turning invoke instructions into calls, iff the callee cannot |
Duncan Sands | 9f76be6 | 2007-12-10 19:09:40 +0000 | [diff] [blame] | 11 | // throw an exception, and marking functions 'nounwind' if they cannot throw. |
| 12 | // It implements this as a bottom-up traversal of the call-graph. |
Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Duncan Sands | 57512a1 | 2008-09-29 14:59:04 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallPtrSet.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/CallGraph.h" |
Chandler Carruth | 839a98e | 2013-01-07 15:26:48 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/CallGraphSCCPass.h" |
David Majnemer | 70497c6 | 2015-12-02 23:06:39 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/EHPersonalities.h" |
Chandler Carruth | 1305dc3 | 2014-03-04 11:45:46 +0000 | [diff] [blame] | 21 | #include "llvm/IR/CFG.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Constants.h" |
| 23 | #include "llvm/IR/Function.h" |
David Majnemer | 5185c3c | 2015-06-27 07:52:53 +0000 | [diff] [blame] | 24 | #include "llvm/IR/InlineAsm.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Instructions.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 26 | #include "llvm/IR/LLVMContext.h" |
Reid Kleckner | 05da2fe | 2019-11-13 13:15:01 -0800 | [diff] [blame^] | 27 | #include "llvm/InitializePasses.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
| 29 | #include "llvm/Transforms/IPO.h" |
Reid Kleckner | 05da2fe | 2019-11-13 13:15:01 -0800 | [diff] [blame^] | 30 | #include "llvm/Transforms/Utils/Local.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 | |
Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 49 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 50 | } |
Sean Silva | 0fb7774 | 2016-07-02 19:12:56 +0000 | [diff] [blame] | 51 | static bool SimplifyFunction(Function *F, CallGraph &CG); |
| 52 | static void DeleteBasicBlock(BasicBlock *BB, CallGraph &CG); |
Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 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 | |
Sean Silva | 0fb7774 | 2016-07-02 19:12:56 +0000 | [diff] [blame] | 63 | static bool runImpl(CallGraphSCC &SCC, CallGraph &CG) { |
Duncan Sands | 57512a1 | 2008-09-29 14:59:04 +0000 | [diff] [blame] | 64 | SmallPtrSet<CallGraphNode *, 8> SCCNodes; |
Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 65 | bool MadeChange = false; |
Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 66 | |
Duncan Sands | 57512a1 | 2008-09-29 14:59:04 +0000 | [diff] [blame] | 67 | // Fill SCCNodes with the elements of the SCC. Used for quickly |
| 68 | // looking up whether a given CallGraphNode is in this SCC. |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 69 | for (CallGraphNode *I : SCC) |
| 70 | SCCNodes.insert(I); |
Duncan Sands | 57512a1 | 2008-09-29 14:59:04 +0000 | [diff] [blame] | 71 | |
Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 72 | // First pass, scan all of the functions in the SCC, simplifying them |
| 73 | // according to what we know. |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 74 | for (CallGraphNode *I : SCC) |
| 75 | if (Function *F = I->getFunction()) |
Sean Silva | 0fb7774 | 2016-07-02 19:12:56 +0000 | [diff] [blame] | 76 | MadeChange |= SimplifyFunction(F, CG); |
Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 77 | |
| 78 | // 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] | 79 | // functions in this SCC: if so, we cannot prune any functions in this SCC. |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 80 | // Definitions that are weak and not declared non-throwing might be |
Dale Johannesen | e7f5bc2 | 2008-05-16 21:31:48 +0000 | [diff] [blame] | 81 | // overridden at linktime with something that throws, so assume that. |
Chris Lattner | 04ecefe | 2003-09-08 19:44:26 +0000 | [diff] [blame] | 82 | // If this SCC includes the unwind instruction, we KNOW it throws, so |
Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 83 | // obviously the SCC might throw. |
| 84 | // |
Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 85 | bool SCCMightUnwind = false, SCCMightReturn = false; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 86 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); |
Chris Lattner | 4422d31 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 87 | (!SCCMightUnwind || !SCCMightReturn) && I != E; ++I) { |
| 88 | Function *F = (*I)->getFunction(); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 89 | if (!F) { |
Chris Lattner | 7f4f773 | 2005-04-26 23:53:25 +0000 | [diff] [blame] | 90 | SCCMightUnwind = true; |
Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 91 | SCCMightReturn = true; |
Sanjoy Das | 0359a19 | 2016-10-03 19:35:30 +0000 | [diff] [blame] | 92 | } else if (!F->hasExactDefinition()) { |
Duncan Sands | 3353ed0 | 2007-12-18 09:59:50 +0000 | [diff] [blame] | 93 | SCCMightUnwind |= !F->doesNotThrow(); |
| 94 | SCCMightReturn |= !F->doesNotReturn(); |
Chris Lattner | d041dcd | 2004-04-12 04:06:38 +0000 | [diff] [blame] | 95 | } else { |
Duncan Sands | 3353ed0 | 2007-12-18 09:59:50 +0000 | [diff] [blame] | 96 | bool CheckUnwind = !SCCMightUnwind && !F->doesNotThrow(); |
| 97 | bool CheckReturn = !SCCMightReturn && !F->doesNotReturn(); |
David Majnemer | 5185c3c | 2015-06-27 07:52:53 +0000 | [diff] [blame] | 98 | // Determine if we should scan for InlineAsm in a naked function as it |
| 99 | // is the only way to return without a ReturnInst. Only do this for |
| 100 | // no-inline functions as functions which may be inlined cannot |
| 101 | // meaningfully return via assembly. |
| 102 | bool CheckReturnViaAsm = CheckReturn && |
| 103 | F->hasFnAttribute(Attribute::Naked) && |
| 104 | F->hasFnAttribute(Attribute::NoInline); |
Duncan Sands | 9f76be6 | 2007-12-10 19:09:40 +0000 | [diff] [blame] | 105 | |
| 106 | if (!CheckUnwind && !CheckReturn) |
| 107 | continue; |
Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 108 | |
David Majnemer | 5185c3c | 2015-06-27 07:52:53 +0000 | [diff] [blame] | 109 | for (const BasicBlock &BB : *F) { |
Chandler Carruth | edb12a8 | 2018-10-15 10:04:59 +0000 | [diff] [blame] | 110 | const Instruction *TI = BB.getTerminator(); |
David Majnemer | 5185c3c | 2015-06-27 07:52:53 +0000 | [diff] [blame] | 111 | if (CheckUnwind && TI->mayThrow()) { |
Chris Lattner | 7f4f773 | 2005-04-26 23:53:25 +0000 | [diff] [blame] | 112 | SCCMightUnwind = true; |
David Majnemer | 5185c3c | 2015-06-27 07:52:53 +0000 | [diff] [blame] | 113 | } else if (CheckReturn && isa<ReturnInst>(TI)) { |
Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 114 | SCCMightReturn = true; |
Chris Lattner | d041dcd | 2004-04-12 04:06:38 +0000 | [diff] [blame] | 115 | } |
Chris Lattner | 56997dd | 2004-02-08 21:15:59 +0000 | [diff] [blame] | 116 | |
David Majnemer | 5185c3c | 2015-06-27 07:52:53 +0000 | [diff] [blame] | 117 | for (const Instruction &I : BB) { |
| 118 | if ((!CheckUnwind || SCCMightUnwind) && |
| 119 | (!CheckReturnViaAsm || SCCMightReturn)) |
| 120 | break; |
| 121 | |
| 122 | // Check to see if this function performs an unwind or calls an |
| 123 | // unwinding function. |
| 124 | if (CheckUnwind && !SCCMightUnwind && I.mayThrow()) { |
| 125 | bool InstMightUnwind = true; |
| 126 | if (const auto *CI = dyn_cast<CallInst>(&I)) { |
| 127 | if (Function *Callee = CI->getCalledFunction()) { |
Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 128 | CallGraphNode *CalleeNode = CG[Callee]; |
David Majnemer | 5185c3c | 2015-06-27 07:52:53 +0000 | [diff] [blame] | 129 | // If the callee is outside our current SCC then we may throw |
| 130 | // because it might. If it is inside, do nothing. |
| 131 | if (SCCNodes.count(CalleeNode) > 0) |
| 132 | InstMightUnwind = false; |
Chris Lattner | 56997dd | 2004-02-08 21:15:59 +0000 | [diff] [blame] | 133 | } |
| 134 | } |
David Majnemer | 5185c3c | 2015-06-27 07:52:53 +0000 | [diff] [blame] | 135 | SCCMightUnwind |= InstMightUnwind; |
| 136 | } |
| 137 | if (CheckReturnViaAsm && !SCCMightReturn) |
| 138 | if (auto ICS = ImmutableCallSite(&I)) |
| 139 | if (const auto *IA = dyn_cast<InlineAsm>(ICS.getCalledValue())) |
| 140 | if (IA->hasSideEffects()) |
| 141 | SCCMightReturn = true; |
| 142 | } |
| 143 | |
| 144 | if (SCCMightUnwind && SCCMightReturn) |
| 145 | break; |
Chris Lattner | 5a6fa29 | 2003-09-15 02:22:50 +0000 | [diff] [blame] | 146 | } |
Chris Lattner | d041dcd | 2004-04-12 04:06:38 +0000 | [diff] [blame] | 147 | } |
| 148 | } |
Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 149 | |
| 150 | // 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] | 151 | if (!SCCMightUnwind || !SCCMightReturn) |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 152 | for (CallGraphNode *I : SCC) { |
| 153 | Function *F = I->getFunction(); |
Benjamin Kramer | 9ddfaf2 | 2013-06-15 10:55:39 +0000 | [diff] [blame] | 154 | |
Artur Pilipenko | 7ae49ac | 2015-12-11 16:30:26 +0000 | [diff] [blame] | 155 | if (!SCCMightUnwind && !F->hasFnAttribute(Attribute::NoUnwind)) { |
| 156 | F->addFnAttr(Attribute::NoUnwind); |
Duncan Sands | 6dd02b5 | 2008-09-05 09:08:37 +0000 | [diff] [blame] | 157 | MadeChange = true; |
Artur Pilipenko | 7ae49ac | 2015-12-11 16:30:26 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | if (!SCCMightReturn && !F->hasFnAttribute(Attribute::NoReturn)) { |
| 161 | F->addFnAttr(Attribute::NoReturn); |
| 162 | MadeChange = true; |
Duncan Sands | 6dd02b5 | 2008-09-05 09:08:37 +0000 | [diff] [blame] | 163 | } |
Duncan Sands | 9f76be6 | 2007-12-10 19:09:40 +0000 | [diff] [blame] | 164 | } |
Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 165 | |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 166 | for (CallGraphNode *I : SCC) { |
Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 167 | // Convert any invoke instructions to non-throwing functions in this node |
| 168 | // into call instructions with a branch. This makes the exception blocks |
| 169 | // dead. |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 170 | if (Function *F = I->getFunction()) |
Sean Silva | 0fb7774 | 2016-07-02 19:12:56 +0000 | [diff] [blame] | 171 | MadeChange |= SimplifyFunction(F, CG); |
Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 174 | return MadeChange; |
Chris Lattner | 0a3f8d5 | 2003-08-31 02:47:32 +0000 | [diff] [blame] | 175 | } |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 176 | |
Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 177 | |
Sean Silva | 0fb7774 | 2016-07-02 19:12:56 +0000 | [diff] [blame] | 178 | bool PruneEH::runOnSCC(CallGraphSCC &SCC) { |
| 179 | if (skipSCC(SCC)) |
| 180 | return false; |
| 181 | CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph(); |
| 182 | return runImpl(SCC, CG); |
| 183 | } |
| 184 | |
| 185 | |
Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 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. |
Sean Silva | 0fb7774 | 2016-07-02 19:12:56 +0000 | [diff] [blame] | 189 | static bool SimplifyFunction(Function *F, CallGraph &CG) { |
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)) { |
Duncan Sands | 9f76be6 | 2007-12-10 19:09:40 +0000 | [diff] [blame] | 194 | BasicBlock *UnwindBlock = II->getUnwindDest(); |
David Majnemer | 146d781 | 2016-01-23 05:41:22 +0000 | [diff] [blame] | 195 | removeUnwindEdge(&*BB); |
Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 196 | |
Duncan Sands | 9f76be6 | 2007-12-10 19:09:40 +0000 | [diff] [blame] | 197 | // If the unwind block is now dead, nuke it. |
Ramkumar Ramachandra | 40c3e03 | 2015-01-13 03:46:47 +0000 | [diff] [blame] | 198 | if (pred_empty(UnwindBlock)) |
Sean Silva | 0fb7774 | 2016-07-02 19:12:56 +0000 | [diff] [blame] | 199 | DeleteBasicBlock(UnwindBlock, CG); // Delete the new BB. |
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 | ++NumRemoved; |
| 202 | MadeChange = true; |
Nick Lewycky | 929703b | 2008-03-09 17:11:18 +0000 | [diff] [blame] | 203 | } |
Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 204 | |
Chris Lattner | 36ffb1f | 2005-04-27 20:12:17 +0000 | [diff] [blame] | 205 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) |
Nick Lewycky | 4d43d3c | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 206 | if (CallInst *CI = dyn_cast<CallInst>(I++)) |
Joseph Tremoulet | b69afa8 | 2019-04-02 15:47:11 +0000 | [diff] [blame] | 207 | if (CI->doesNotReturn() && !CI->isMustTailCall() && |
| 208 | !isa<UnreachableInst>(I)) { |
Duncan Sands | 9f76be6 | 2007-12-10 19:09:40 +0000 | [diff] [blame] | 209 | // This call calls a function that cannot return. Insert an |
| 210 | // unreachable instruction after it and simplify the code. Do this |
| 211 | // by splitting the BB, adding the unreachable, then deleting the |
| 212 | // new BB. |
| 213 | BasicBlock *New = BB->splitBasicBlock(I); |
Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 214 | |
Duncan Sands | 9f76be6 | 2007-12-10 19:09:40 +0000 | [diff] [blame] | 215 | // Remove the uncond branch and add an unreachable. |
| 216 | BB->getInstList().pop_back(); |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 217 | new UnreachableInst(BB->getContext(), &*BB); |
Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 218 | |
Sean Silva | 0fb7774 | 2016-07-02 19:12:56 +0000 | [diff] [blame] | 219 | DeleteBasicBlock(New, CG); // Delete the new BB. |
Duncan Sands | 9f76be6 | 2007-12-10 19:09:40 +0000 | [diff] [blame] | 220 | MadeChange = true; |
| 221 | ++NumUnreach; |
| 222 | break; |
Nick Lewycky | 0ac65c3 | 2008-03-09 17:13:05 +0000 | [diff] [blame] | 223 | } |
Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 224 | } |
Nick Lewycky | 4d43d3c | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 225 | |
Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 226 | return MadeChange; |
| 227 | } |
| 228 | |
| 229 | /// DeleteBasicBlock - remove the specified basic block from the program, |
| 230 | /// updating the callgraph to reflect any now-obsolete edges due to calls that |
| 231 | /// exist in the BB. |
Sean Silva | 0fb7774 | 2016-07-02 19:12:56 +0000 | [diff] [blame] | 232 | static void DeleteBasicBlock(BasicBlock *BB, CallGraph &CG) { |
Ramkumar Ramachandra | 40c3e03 | 2015-01-13 03:46:47 +0000 | [diff] [blame] | 233 | assert(pred_empty(BB) && "BB is not dead!"); |
Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 234 | |
David Majnemer | 4bf0b6b | 2016-01-23 05:41:29 +0000 | [diff] [blame] | 235 | Instruction *TokenInst = nullptr; |
| 236 | |
Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 237 | CallGraphNode *CGN = CG[BB->getParent()]; |
| 238 | for (BasicBlock::iterator I = BB->end(), E = BB->begin(); I != E; ) { |
| 239 | --I; |
David Majnemer | 2d728ec | 2016-01-23 05:41:27 +0000 | [diff] [blame] | 240 | |
David Majnemer | 4bf0b6b | 2016-01-23 05:41:29 +0000 | [diff] [blame] | 241 | if (I->getType()->isTokenTy()) { |
| 242 | TokenInst = &*I; |
| 243 | break; |
| 244 | } |
| 245 | |
Chandler Carruth | ce3f75d | 2019-04-19 05:59:42 +0000 | [diff] [blame] | 246 | if (auto *Call = dyn_cast<CallBase>(&*I)) { |
| 247 | const Function *Callee = Call->getCalledFunction(); |
David Majnemer | 2d728ec | 2016-01-23 05:41:27 +0000 | [diff] [blame] | 248 | if (!Callee || !Intrinsic::isLeaf(Callee->getIntrinsicID())) |
Chandler Carruth | ce3f75d | 2019-04-19 05:59:42 +0000 | [diff] [blame] | 249 | CGN->removeCallEdgeFor(*Call); |
David Majnemer | 2d728ec | 2016-01-23 05:41:27 +0000 | [diff] [blame] | 250 | else if (!Callee->isIntrinsic()) |
Chandler Carruth | ce3f75d | 2019-04-19 05:59:42 +0000 | [diff] [blame] | 251 | CGN->removeCallEdgeFor(*Call); |
David Majnemer | 2d728ec | 2016-01-23 05:41:27 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 254 | if (!I->use_empty()) |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 255 | I->replaceAllUsesWith(UndefValue::get(I->getType())); |
Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 256 | } |
| 257 | |
David Majnemer | 4bf0b6b | 2016-01-23 05:41:29 +0000 | [diff] [blame] | 258 | if (TokenInst) { |
Chandler Carruth | 9ae926b | 2018-08-26 09:51:22 +0000 | [diff] [blame] | 259 | if (!TokenInst->isTerminator()) |
David Majnemer | e14e7bc | 2016-06-25 08:19:55 +0000 | [diff] [blame] | 260 | changeToUnreachable(TokenInst->getNextNode(), /*UseLLVMTrap=*/false); |
David Majnemer | 4bf0b6b | 2016-01-23 05:41:29 +0000 | [diff] [blame] | 261 | } else { |
| 262 | // Get the list of successors of this block. |
| 263 | std::vector<BasicBlock *> Succs(succ_begin(BB), succ_end(BB)); |
Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 264 | |
David Majnemer | 4bf0b6b | 2016-01-23 05:41:29 +0000 | [diff] [blame] | 265 | for (unsigned i = 0, e = Succs.size(); i != e; ++i) |
| 266 | Succs[i]->removePredecessor(BB); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 267 | |
David Majnemer | 4bf0b6b | 2016-01-23 05:41:29 +0000 | [diff] [blame] | 268 | BB->eraseFromParent(); |
| 269 | } |
Chris Lattner | 93f4e9d | 2005-04-27 04:52:23 +0000 | [diff] [blame] | 270 | } |