LLVM  10.0.0svn
EscapeEnumerator.cpp
Go to the documentation of this file.
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 
17 #include "llvm/IR/CallSite.h"
18 #include "llvm/IR/Module.h"
19 using namespace llvm;
20 
22  LLVMContext &C = M->getContext();
23  Triple T(M->getTargetTriple());
27 }
28 
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.
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);
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.
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 }
uint64_t CallInst * C
const std::string & getTargetTriple() const
Get the target triple which is a string describing the target host.
Definition: Module.h:240
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:139
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
static ResumeInst * Create(Value *Exn, Instruction *InsertBefore=nullptr)
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
Definition: DerivedTypes.h:170
This class represents a function call, abstracting a target machine&#39;s calling convention.
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:743
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:144
static LandingPadInst * Create(Type *RetTy, unsigned NumReservedClauses, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedClauses is a hint for the number of incoming clauses that this landingpad w...
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:244
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:779
static StructType * get(LLVMContext &Context, ArrayRef< Type *> Elements, bool isPacked=false)
This static method is the primary way to create a literal StructType.
Definition: Type.cpp:346
bool isScopedEHPersonality(EHPersonality Pers)
Returns true if this personality uses scope-style EH IR instructions: catchswitch, catchpad/ret, and cleanuppad/ret.
void setCleanup(bool V)
Indicate that this landingpad instruction is a cleanup.
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block...
Definition: IRBuilder.h:132
The landingpad instruction holds all of the information necessary to generate correct exception handl...
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:64
Resume the propagation of an exception.
EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
static FunctionType * get(Type *Result, ArrayRef< Type *> Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
Definition: Type.cpp:301
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:99
EHPersonality getDefaultEHPersonality(const Triple &T)
size_t size() const
Definition: SmallVector.h:52
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:224
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:43
bool doesNotThrow() const
Determine if the function cannot unwind.
Definition: Function.h:533
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
Module.h This file contains the declarations for the Module class.
FunctionCallee getOrInsertFunction(StringRef Name, FunctionType *T, AttributeList AttributeList)
Look up the specified function in the module symbol table.
Definition: Module.cpp:143
static FunctionCallee getDefaultPersonalityFn(Module *M)
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:180
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:55
#define I(x, y, z)
Definition: MD5.cpp:58
StringRef getEHPersonalityName(EHPersonality Pers)
BasicBlock * changeToInvokeAndSplitBasicBlock(CallInst *CI, BasicBlock *UnwindEdge)
Convert the CallInst to InvokeInst with the specified unwind edge basic block.
Definition: Local.cpp:1974
constexpr char Args[]
Key for Kernel::Metadata::mArgs.