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