LLVM  8.0.0svn
Go to the documentation of this file.
1 //===- EscapeEnumerator.cpp -----------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Defines a helper class that enumerates all possible exits from a function,
11 // including exception handling.
12 //
13 //===----------------------------------------------------------------------===//
18 #include "llvm/IR/CallSite.h"
19 #include "llvm/IR/Module.h"
20 using namespace llvm;
23  LLVMContext &C = M->getContext();
24  Triple T(M->getTargetTriple());
28 }
31  if (Done)
32  return nullptr;
34  // Find all 'return', 'resume', and 'unwind' instructions.
35  while (StateBB != StateE) {
36  BasicBlock *CurBB = &*StateBB++;
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;
44  Builder.SetInsertPoint(TI);
45  return &Builder;
46  }
48  Done = true;
50  if (!HandleExceptions)
51  return nullptr;
53  if (F.doesNotThrow())
54  return nullptr;
56  // Find all 'call' instructions that may throw.
58  for (BasicBlock &BB : F)
59  for (Instruction &II : BB)
60  if (CallInst *CI = dyn_cast<CallInst>(&II))
61  if (!CI->doesNotThrow())
62  Calls.push_back(CI);
64  if (Calls.empty())
65  return nullptr;
67  // Create a cleanup block.
68  LLVMContext &C = F.getContext();
69  BasicBlock *CleanupBB = BasicBlock::Create(C, CleanupBBName, &F);
71  if (!F.hasPersonalityFn()) {
72  Constant *PersFn = getDefaultPersonalityFn(F.getParent());
73  F.setPersonalityFn(PersFn);
74  }
76  if (isScopedEHPersonality(classifyEHPersonality(F.getPersonalityFn()))) {
77  report_fatal_error("Scoped EH not supported");
78  }
80  LandingPadInst *LPad =
81  LandingPadInst::Create(ExnTy, 1, "cleanup.lpad", CleanupBB);
82  LPad->setCleanup(true);
83  ResumeInst *RI = ResumeInst::Create(LPad, CleanupBB);
85  // Transform the 'call' instructions into 'invoke's branching to the
86  // cleanup block. Go in reverse order to make prettier BB names.
88  for (unsigned I = Calls.size(); I != 0;) {
89  CallInst *CI = cast<CallInst>(Calls[--I]);
90  changeToInvokeAndSplitBasicBlock(CI, CleanupBB);
91  }
93  Builder.SetInsertPoint(RI);
94  return &Builder;
95 }
uint64_t CallInst * C
const std::string & getTargetTriple() const
Get the target triple which is a string describing the target host.
Definition: Module.h:239
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
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
Constant * getOrInsertFunction(StringRef Name, FunctionType *T, AttributeList AttributeList)
Look up the specified function in the module symbol table.
Definition: Module.cpp:143
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:64
static ResumeInst * Create(Value *Exn, Instruction *InsertBefore=nullptr)
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:714
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:138
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:243
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:743
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:336
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.
#define T
static Constant * getDefaultPersonalityFn(Module *M)
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block...
Definition: IRBuilder.h:127
The landingpad instruction holds all of the information necessary to generate correct exception handl...
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
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:69
This is an important base class in LLVM.
Definition: Constant.h:42
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:297
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:100
EHPersonality getDefaultEHPersonality(const Triple &T)
size_t size() const
Definition: SmallVector.h:53
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:220
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
bool doesNotThrow() const
Determine if the function cannot unwind.
Definition: Function.h:520
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
Module.h This file contains the declarations for the Module class.
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:176
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:56
#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:1979
constexpr char Args[]
Key for Kernel::Metadata::mArgs.