xref: /llvm-project/llvm/lib/Transforms/Utils/EscapeEnumerator.cpp (revision 7b4cc0961b142877794645576d2393af43c48069)
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/ADT/Triple.h"
16 #include "llvm/Analysis/EHPersonalities.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/Transforms/Utils/Local.h"
19 
20 using namespace llvm;
21 
22 static FunctionCallee 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.
40     Instruction *TI = CurBB->getTerminator();
41     if (!isa<ReturnInst>(TI) && !isa<ResumeInst>(TI))
42       continue;
43 
44     // If the ret instruction is followed by a musttaill call,
45     // or a bitcast instruction and then a musttail call, we should return
46     // the musttail call as the insertion point to not break the musttail
47     // contract.
48     auto AdjustMustTailCall = [&](Instruction *I) -> Instruction * {
49       auto *RI = dyn_cast<ReturnInst>(I);
50       if (!RI || !RI->getPrevNode())
51         return I;
52       auto *CI = dyn_cast<CallInst>(RI->getPrevNode());
53       if (CI && CI->isMustTailCall())
54         return CI;
55       auto *BI = dyn_cast<BitCastInst>(RI->getPrevNode());
56       if (!BI || !BI->getPrevNode())
57         return I;
58       CI = dyn_cast<CallInst>(BI->getPrevNode());
59       if (CI && CI->isMustTailCall())
60         return CI;
61       return I;
62     };
63 
64     Builder.SetInsertPoint(AdjustMustTailCall(TI));
65     return &Builder;
66   }
67 
68   Done = true;
69 
70   if (!HandleExceptions)
71     return nullptr;
72 
73   if (F.doesNotThrow())
74     return nullptr;
75 
76   // Find all 'call' instructions that may throw.
77   // We cannot tranform calls with musttail tag.
78   SmallVector<Instruction *, 16> Calls;
79   for (BasicBlock &BB : F)
80     for (Instruction &II : BB)
81       if (CallInst *CI = dyn_cast<CallInst>(&II))
82         if (!CI->doesNotThrow() && !CI->isMustTailCall())
83           Calls.push_back(CI);
84 
85   if (Calls.empty())
86     return nullptr;
87 
88   // Create a cleanup block.
89   LLVMContext &C = F.getContext();
90   BasicBlock *CleanupBB = BasicBlock::Create(C, CleanupBBName, &F);
91   Type *ExnTy = StructType::get(Type::getInt8PtrTy(C), Type::getInt32Ty(C));
92   if (!F.hasPersonalityFn()) {
93     FunctionCallee PersFn = getDefaultPersonalityFn(F.getParent());
94     F.setPersonalityFn(cast<Constant>(PersFn.getCallee()));
95   }
96 
97   if (isScopedEHPersonality(classifyEHPersonality(F.getPersonalityFn()))) {
98     report_fatal_error("Scoped EH not supported");
99   }
100 
101   LandingPadInst *LPad =
102       LandingPadInst::Create(ExnTy, 1, "cleanup.lpad", CleanupBB);
103   LPad->setCleanup(true);
104   ResumeInst *RI = ResumeInst::Create(LPad, CleanupBB);
105 
106   // Transform the 'call' instructions into 'invoke's branching to the
107   // cleanup block. Go in reverse order to make prettier BB names.
108   SmallVector<Value *, 16> Args;
109   for (unsigned I = Calls.size(); I != 0;) {
110     CallInst *CI = cast<CallInst>(Calls[--I]);
111     changeToInvokeAndSplitBasicBlock(CI, CleanupBB);
112   }
113 
114   Builder.SetInsertPoint(RI);
115   return &Builder;
116 }
117