xref: /llvm-project/llvm/lib/Transforms/Utils/EscapeEnumerator.cpp (revision 05a11974ae9f8fc03b7cc8d679031ebba8e6a12b)
1 //===- EscapeEnumerator.cpp -----------------------------------------------===//
2 //
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
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"
16 #include "llvm/Transforms/Utils/Local.h"
17 #include "llvm/IR/Module.h"
18 using namespace llvm;
19 
20 static FunctionCallee getDefaultPersonalityFn(Module *M) {
21   LLVMContext &C = M->getContext();
22   Triple T(M->getTargetTriple());
23   EHPersonality Pers = getDefaultEHPersonality(T);
24   return M->getOrInsertFunction(getEHPersonalityName(Pers),
25                                 FunctionType::get(Type::getInt32Ty(C), true));
26 }
27 
28 IRBuilder<> *EscapeEnumerator::Next() {
29   if (Done)
30     return nullptr;
31 
32   // Find all 'return', 'resume', and 'unwind' instructions.
33   while (StateBB != StateE) {
34     BasicBlock *CurBB = &*StateBB++;
35 
36     // Branches and invokes do not escape, only unwind, resume, and return
37     // do.
38     Instruction *TI = CurBB->getTerminator();
39     if (!isa<ReturnInst>(TI) && !isa<ResumeInst>(TI))
40       continue;
41 
42     Builder.SetInsertPoint(TI);
43     return &Builder;
44   }
45 
46   Done = true;
47 
48   if (!HandleExceptions)
49     return nullptr;
50 
51   if (F.doesNotThrow())
52     return nullptr;
53 
54   // Find all 'call' instructions that may throw.
55   SmallVector<Instruction *, 16> Calls;
56   for (BasicBlock &BB : F)
57     for (Instruction &II : BB)
58       if (CallInst *CI = dyn_cast<CallInst>(&II))
59         if (!CI->doesNotThrow())
60           Calls.push_back(CI);
61 
62   if (Calls.empty())
63     return nullptr;
64 
65   // Create a cleanup block.
66   LLVMContext &C = F.getContext();
67   BasicBlock *CleanupBB = BasicBlock::Create(C, CleanupBBName, &F);
68   Type *ExnTy = StructType::get(Type::getInt8PtrTy(C), Type::getInt32Ty(C));
69   if (!F.hasPersonalityFn()) {
70     FunctionCallee PersFn = getDefaultPersonalityFn(F.getParent());
71     F.setPersonalityFn(cast<Constant>(PersFn.getCallee()));
72   }
73 
74   if (isScopedEHPersonality(classifyEHPersonality(F.getPersonalityFn()))) {
75     report_fatal_error("Scoped EH not supported");
76   }
77 
78   LandingPadInst *LPad =
79       LandingPadInst::Create(ExnTy, 1, "cleanup.lpad", CleanupBB);
80   LPad->setCleanup(true);
81   ResumeInst *RI = ResumeInst::Create(LPad, CleanupBB);
82 
83   // Transform the 'call' instructions into 'invoke's branching to the
84   // cleanup block. Go in reverse order to make prettier BB names.
85   SmallVector<Value *, 16> Args;
86   for (unsigned I = Calls.size(); I != 0;) {
87     CallInst *CI = cast<CallInst>(Calls[--I]);
88     changeToInvokeAndSplitBasicBlock(CI, CleanupBB);
89   }
90 
91   Builder.SetInsertPoint(RI);
92   return &Builder;
93 }
94