LLVM  14.0.0git
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 
15 #include "llvm/ADT/Triple.h"
17 #include "llvm/IR/Module.h"
19 
20 using namespace llvm;
21 
23  LLVMContext &C = M->getContext();
24  Triple T(M->getTargetTriple());
26  return M->getOrInsertFunction(getEHPersonalityName(Pers),
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  Instruction *TI = CurBB->getTerminator();
41  if (!isa<ReturnInst>(TI) && !isa<ResumeInst>(TI))
42  continue;
43 
44  if (CallInst *CI = CurBB->getTerminatingMustTailCall())
45  TI = CI;
46  Builder.SetInsertPoint(TI);
47  return &Builder;
48  }
49 
50  Done = true;
51 
52  if (!HandleExceptions)
53  return nullptr;
54 
55  if (F.doesNotThrow())
56  return nullptr;
57 
58  // Find all 'call' instructions that may throw.
59  // We cannot tranform calls with musttail tag.
61  for (BasicBlock &BB : F)
62  for (Instruction &II : BB)
63  if (CallInst *CI = dyn_cast<CallInst>(&II))
64  if (!CI->doesNotThrow() && !CI->isMustTailCall())
65  Calls.push_back(CI);
66 
67  if (Calls.empty())
68  return nullptr;
69 
70  // Create a cleanup block.
71  LLVMContext &C = F.getContext();
72  BasicBlock *CleanupBB = BasicBlock::Create(C, CleanupBBName, &F);
74  if (!F.hasPersonalityFn()) {
75  FunctionCallee PersFn = getDefaultPersonalityFn(F.getParent());
76  F.setPersonalityFn(cast<Constant>(PersFn.getCallee()));
77  }
78 
79  if (isScopedEHPersonality(classifyEHPersonality(F.getPersonalityFn()))) {
80  report_fatal_error("Scoped EH not supported");
81  }
82 
83  LandingPadInst *LPad =
84  LandingPadInst::Create(ExnTy, 1, "cleanup.lpad", CleanupBB);
85  LPad->setCleanup(true);
86  ResumeInst *RI = ResumeInst::Create(LPad, CleanupBB);
87 
88  // Transform the 'call' instructions into 'invoke's branching to the
89  // cleanup block. Go in reverse order to make prettier BB names.
91  for (unsigned I = Calls.size(); I != 0;) {
92  CallInst *CI = cast<CallInst>(Calls[--I]);
93  changeToInvokeAndSplitBasicBlock(CI, CleanupBB, DTU);
94  }
95 
96  Builder.SetInsertPoint(RI);
97  return &Builder;
98 }
llvm::LandingPadInst::Create
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...
Definition: Instructions.cpp:214
llvm::IRBuilderBase::SetInsertPoint
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Definition: IRBuilder.h:184
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::StructType::get
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:408
llvm::Type::getInt8PtrTy
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:293
T
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1168
llvm::LandingPadInst
The landingpad instruction holds all of the information necessary to generate correct exception handl...
Definition: Instructions.h:2918
llvm::IRBuilder<>
llvm::FunctionType::get
static FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
Definition: Type.cpp:363
Local.h
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:45
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
Module.h
EHPersonalities.h
llvm::Type::getInt32Ty
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:241
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::BasicBlock
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
getDefaultPersonalityFn
static FunctionCallee getDefaultPersonalityFn(Module *M)
Definition: EscapeEnumerator.cpp:22
llvm::classifyEHPersonality
EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
Definition: EHPersonalities.cpp:21
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::Instruction
Definition: Instruction.h:45
llvm::report_fatal_error
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:143
llvm::ResumeInst::Create
static ResumeInst * Create(Value *Exn, Instruction *InsertBefore=nullptr)
Definition: Instructions.h:4239
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::isScopedEHPersonality
bool isScopedEHPersonality(EHPersonality Pers)
Returns true if this personality uses scope-style EH IR instructions: catchswitch,...
Definition: EHPersonalities.h:80
llvm::getEHPersonalityName
StringRef getEHPersonalityName(EHPersonality Pers)
Definition: EHPersonalities.cpp:46
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm::changeToInvokeAndSplitBasicBlock
BasicBlock * changeToInvokeAndSplitBasicBlock(CallInst *CI, BasicBlock *UnwindEdge, DomTreeUpdater *DTU=nullptr)
Convert the CallInst to InvokeInst with the specified unwind edge basic block.
Definition: Local.cpp:2200
Triple.h
llvm::BasicBlock::Create
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:100
llvm::getDefaultEHPersonality
EHPersonality getDefaultEHPersonality(const Triple &T)
Definition: EHPersonalities.cpp:69
llvm::EHPersonality
EHPersonality
Definition: EHPersonalities.h:22
llvm::BasicBlock::getTerminator
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:152
llvm::FunctionCallee::getCallee
Value * getCallee()
Definition: DerivedTypes.h:184
llvm::EscapeEnumerator::Next
IRBuilder * Next()
Definition: EscapeEnumerator.cpp:30
llvm::Function::doesNotThrow
bool doesNotThrow() const
Determine if the function cannot unwind.
Definition: Function.h:556
llvm::ResumeInst
Resume the propagation of an exception.
Definition: Instructions.h:4226
EscapeEnumerator.h
llvm::FunctionCallee
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
Definition: DerivedTypes.h:165
llvm::CallInst
This class represents a function call, abstracting a target machine's calling convention.
Definition: Instructions.h:1487
BB
Common register allocation spilling lr str ldr sxth r3 ldr mla r4 can lr mov lr str ldr sxth r3 mla r4 and then merge mul and lr str ldr sxth r3 mla r4 It also increase the likelihood the store may become dead bb27 Successors according to LLVM BB
Definition: README.txt:39
llvm::AMDGPU::HSAMD::Kernel::Key::Args
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
Definition: AMDGPUMetadata.h:389
llvm::BasicBlock::getTerminatingMustTailCall
const CallInst * getTerminatingMustTailCall() const
Returns the call instruction marked 'musttail' prior to the terminating return instruction of this ba...
Definition: BasicBlock.cpp:158
llvm::LandingPadInst::setCleanup
void setCleanup(bool V)
Indicate that this landingpad instruction is a cleanup.
Definition: Instructions.h:2968