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/CallSite.h"
18#include "llvm/IR/Module.h"
19using namespace llvm;
20
21static FunctionCallee getDefaultPersonalityFn(Module *M) {
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
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.
39    Instruction *TI = CurBB->getTerminator();
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);
69  Type *ExnTy = StructType::get(Type::getInt8PtrTy(C), Type::getInt32Ty(C));
70  if (!F.hasPersonalityFn()) {
71    FunctionCallee PersFn = getDefaultPersonalityFn(F.getParent());
72    F.setPersonalityFn(cast<Constant>(PersFn.getCallee()));
73  }
74
75  if (isScopedEHPersonality(classifyEHPersonality(F.getPersonalityFn()))) {
76    report_fatal_error("Scoped EH not supported");
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}
95