Kuba Brecka | ddfdba3 | 2016-11-14 21:41:13 +0000 | [diff] [blame] | 1 | //===- EscapeEnumerator.cpp -----------------------------------------------===// |
| 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 |
Kuba Brecka | ddfdba3 | 2016-11-14 21:41:13 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // Defines a helper class that enumerates all possible exits from a function, |
| 10 | // including exception handling. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Transforms/Utils/EscapeEnumerator.h" |
| 15 | #include "llvm/Analysis/EHPersonalities.h" |
David Blaikie | 31b98d2 | 2018-06-04 21:23:21 +0000 | [diff] [blame] | 16 | #include "llvm/Transforms/Utils/Local.h" |
Kuba Brecka | ddfdba3 | 2016-11-14 21:41:13 +0000 | [diff] [blame] | 17 | #include "llvm/IR/CallSite.h" |
| 18 | #include "llvm/IR/Module.h" |
Kuba Brecka | ddfdba3 | 2016-11-14 21:41:13 +0000 | [diff] [blame] | 19 | using namespace llvm; |
| 20 | |
James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 21 | static FunctionCallee getDefaultPersonalityFn(Module *M) { |
Kuba Brecka | ddfdba3 | 2016-11-14 21:41:13 +0000 | [diff] [blame] | 22 | LLVMContext &C = M->getContext(); |
| 23 | Triple T(M->getTargetTriple()); |
| 24 | EHPersonality Pers = getDefaultEHPersonality(T); |
| 25 | return M->getOrInsertFunction(getEHPersonalityName(Pers), |
| 26 | FunctionType::get(Type::getInt32Ty(C), true)); |
| 27 | } |
| 28 | |
| 29 | IRBuilder<> *EscapeEnumerator::Next() { |
| 30 | if (Done) |
| 31 | return nullptr; |
| 32 | |
| 33 | // Find all 'return', 'resume', and 'unwind' instructions. |
| 34 | while (StateBB != StateE) { |
| 35 | BasicBlock *CurBB = &*StateBB++; |
| 36 | |
| 37 | // Branches and invokes do not escape, only unwind, resume, and return |
| 38 | // do. |
Chandler Carruth | edb12a8 | 2018-10-15 10:04:59 +0000 | [diff] [blame] | 39 | Instruction *TI = CurBB->getTerminator(); |
Kuba Brecka | ddfdba3 | 2016-11-14 21:41:13 +0000 | [diff] [blame] | 40 | if (!isa<ReturnInst>(TI) && !isa<ResumeInst>(TI)) |
| 41 | continue; |
| 42 | |
| 43 | Builder.SetInsertPoint(TI); |
| 44 | return &Builder; |
| 45 | } |
| 46 | |
| 47 | Done = true; |
| 48 | |
| 49 | if (!HandleExceptions) |
| 50 | return nullptr; |
| 51 | |
| 52 | if (F.doesNotThrow()) |
| 53 | return nullptr; |
| 54 | |
| 55 | // Find all 'call' instructions that may throw. |
| 56 | SmallVector<Instruction *, 16> Calls; |
| 57 | for (BasicBlock &BB : F) |
| 58 | for (Instruction &II : BB) |
| 59 | if (CallInst *CI = dyn_cast<CallInst>(&II)) |
| 60 | if (!CI->doesNotThrow()) |
| 61 | Calls.push_back(CI); |
| 62 | |
| 63 | if (Calls.empty()) |
| 64 | return nullptr; |
| 65 | |
| 66 | // Create a cleanup block. |
| 67 | LLVMContext &C = F.getContext(); |
| 68 | BasicBlock *CleanupBB = BasicBlock::Create(C, CleanupBBName, &F); |
Serge Guelton | e38003f | 2017-05-09 19:31:13 +0000 | [diff] [blame] | 69 | Type *ExnTy = StructType::get(Type::getInt8PtrTy(C), Type::getInt32Ty(C)); |
Kuba Brecka | ddfdba3 | 2016-11-14 21:41:13 +0000 | [diff] [blame] | 70 | if (!F.hasPersonalityFn()) { |
James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 71 | FunctionCallee PersFn = getDefaultPersonalityFn(F.getParent()); |
| 72 | F.setPersonalityFn(cast<Constant>(PersFn.getCallee())); |
Kuba Brecka | ddfdba3 | 2016-11-14 21:41:13 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Heejin Ahn | b4be38f | 2018-05-17 20:52:03 +0000 | [diff] [blame] | 75 | if (isScopedEHPersonality(classifyEHPersonality(F.getPersonalityFn()))) { |
| 76 | report_fatal_error("Scoped EH not supported"); |
Kuba Brecka | ddfdba3 | 2016-11-14 21:41:13 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | LandingPadInst *LPad = |
| 80 | LandingPadInst::Create(ExnTy, 1, "cleanup.lpad", CleanupBB); |
| 81 | LPad->setCleanup(true); |
| 82 | ResumeInst *RI = ResumeInst::Create(LPad, CleanupBB); |
| 83 | |
| 84 | // Transform the 'call' instructions into 'invoke's branching to the |
| 85 | // cleanup block. Go in reverse order to make prettier BB names. |
| 86 | SmallVector<Value *, 16> Args; |
| 87 | for (unsigned I = Calls.size(); I != 0;) { |
| 88 | CallInst *CI = cast<CallInst>(Calls[--I]); |
| 89 | changeToInvokeAndSplitBasicBlock(CI, CleanupBB); |
| 90 | } |
| 91 | |
| 92 | Builder.SetInsertPoint(RI); |
| 93 | return &Builder; |
| 94 | } |