LLVM  6.0.0svn
EscapeEnumerator.cpp
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 //===----------------------------------------------------------------------===//
14 
17 #include "llvm/IR/CallSite.h"
18 #include "llvm/IR/Module.h"
20 using namespace llvm;
21 
23  LLVMContext &C = M->getContext();
24  Triple T(M->getTargetTriple());
28 }
29 
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  TerminatorInst *TI = CurBB->getTerminator();
41  if (!isa<ReturnInst>(TI) && !isa<ResumeInst>(TI))
42  continue;
43 
44  Builder.SetInsertPoint(TI);
45  return &Builder;
46  }
47 
48  Done = true;
49 
50  if (!HandleExceptions)
51  return nullptr;
52 
53  if (F.doesNotThrow())
54  return nullptr;
55 
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);
63 
64  if (Calls.empty())
65  return nullptr;
66 
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  }
75 
76  if (isFuncletEHPersonality(classifyEHPersonality(F.getPersonalityFn()))) {
77  report_fatal_error("Funclet EH not supported");
78  }
79 
80  LandingPadInst *LPad =
81  LandingPadInst::Create(ExnTy, 1, "cleanup.lpad", CleanupBB);
82  LPad->setCleanup(true);
83  ResumeInst *RI = ResumeInst::Create(LPad, CleanupBB);
84 
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  }
92 
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:233
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:115
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:142
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
static ResumeInst * Create(Value *Exn, Instruction *InsertBefore=nullptr)
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:136
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:728
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:237
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:668
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
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:128
The landingpad instruction holds all of the information necessary to generate correct exception handl...
Subclasses of this class are all able to terminate a basic block.
Definition: InstrTypes.h:54
LLVM Basic Block Representation.
Definition: BasicBlock.h:59
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:101
EHPersonality getDefaultEHPersonality(const Triple &T)
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:452
bool isFuncletEHPersonality(EHPersonality Pers)
Returns true if this is a personality function that invokes handler funclets (which must return to it...
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:864
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:61
#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:1503
const TerminatorInst * 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:120
constexpr char Args[]
Key for Kernel::Metadata::mArgs.