blob: 914babeb6829d88cf010332b80685b567922249d [file] [log] [blame]
Kuba Breckaddfdba32016-11-14 21:41:13 +00001//===- EscapeEnumerator.cpp -----------------------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Breckaddfdba32016-11-14 21:41:13 +00006//
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 Blaikie31b98d22018-06-04 21:23:21 +000016#include "llvm/Transforms/Utils/Local.h"
Kuba Breckaddfdba32016-11-14 21:41:13 +000017#include "llvm/IR/CallSite.h"
18#include "llvm/IR/Module.h"
Kuba Breckaddfdba32016-11-14 21:41:13 +000019using namespace llvm;
20
James Y Knight13680222019-02-01 02:28:03 +000021static FunctionCallee getDefaultPersonalityFn(Module *M) {
Kuba Breckaddfdba32016-11-14 21:41:13 +000022 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
29IRBuilder<> *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 Carruthedb12a82018-10-15 10:04:59 +000039 Instruction *TI = CurBB->getTerminator();
Kuba Breckaddfdba32016-11-14 21:41:13 +000040 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 Gueltone38003f2017-05-09 19:31:13 +000069 Type *ExnTy = StructType::get(Type::getInt8PtrTy(C), Type::getInt32Ty(C));
Kuba Breckaddfdba32016-11-14 21:41:13 +000070 if (!F.hasPersonalityFn()) {
James Y Knight13680222019-02-01 02:28:03 +000071 FunctionCallee PersFn = getDefaultPersonalityFn(F.getParent());
72 F.setPersonalityFn(cast<Constant>(PersFn.getCallee()));
Kuba Breckaddfdba32016-11-14 21:41:13 +000073 }
74
Heejin Ahnb4be38f2018-05-17 20:52:03 +000075 if (isScopedEHPersonality(classifyEHPersonality(F.getPersonalityFn()))) {
76 report_fatal_error("Scoped EH not supported");
Kuba Breckaddfdba32016-11-14 21:41:13 +000077 }
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}