45#define DEBUG_TYPE "dwarf-eh-prepare"
47STATISTIC(NumResumesLowered,
"Number of resume calls lowered");
49 "Number of cleanup landing pads found unreachable");
51 "Number of cleanup landing pads remaining");
52STATISTIC(NumNoUnwind,
"Number of functions with nounwind");
53STATISTIC(NumUnwind,
"Number of functions with unwind");
64 const Triple &TargetTriple;
79 bool InsertUnwindResumeCalls();
85 : OptLevel(OptLevel_),
F(F_), TLI(TLI_), DTU(DTU_),
TTI(TTI_),
86 TargetTriple(TargetTriple_) {}
95 Value *ExnObj =
nullptr;
99 bool EraseIVIs =
false;
103 ExcIVI = dyn_cast<InsertValueInst>(SelIVI->
getOperand(0));
104 if (ExcIVI && isa<UndefValue>(ExcIVI->
getOperand(0)) &&
107 SelLoad = dyn_cast<LoadInst>(SelIVI->
getOperand(1));
131size_t DwarfEHPrepare::pruneUnreachableResumes(
134 assert(DTU &&
"Should have DomTreeUpdater here.");
137 size_t ResumeIndex = 0;
138 for (
auto *RI : Resumes) {
139 for (
auto *LP : CleanupLPads) {
141 ResumeReachable.set(ResumeIndex);
149 if (ResumeReachable.all())
150 return Resumes.size();
155 size_t ResumesLeft = 0;
156 for (
size_t I = 0, E = Resumes.size();
I < E; ++
I) {
158 if (ResumeReachable[
I]) {
159 Resumes[ResumesLeft++] = RI;
167 Resumes.resize(ResumesLeft);
171bool DwarfEHPrepare::InsertUnwindResumeCalls() {
174 if (
F.doesNotThrow())
186 NumCleanupLandingPadsRemaining += CleanupLPads.
size();
198 size_t ResumesLeft = Resumes.
size();
199 if (OptLevel != CodeGenOptLevel::None) {
200 ResumesLeft = pruneUnreachableResumes(Resumes, CleanupLPads);
202 unsigned NumRemainingLPs = 0;
208 NumCleanupLandingPadsUnreachable += CleanupLPads.
size() - NumRemainingLPs;
209 NumCleanupLandingPadsRemaining -= CleanupLPads.
size() - NumRemainingLPs;
213 if (ResumesLeft == 0)
220 const char *RewindName;
221 bool DoesRewindFunctionNeedExceptionObject;
223 if ((Pers == EHPersonality::GNU_CXX || Pers == EHPersonality::GNU_CXX_SjLj) &&
224 TargetTriple.isTargetEHABICompatible()) {
225 RewindName = TLI.getLibcallName(RTLIB::CXA_END_CLEANUP);
227 RewindFunctionCallingConv =
228 TLI.getLibcallCallingConv(RTLIB::CXA_END_CLEANUP);
229 DoesRewindFunctionNeedExceptionObject =
false;
231 RewindName = TLI.getLibcallName(RTLIB::UNWIND_RESUME);
232 FTy = FunctionType::get(
Type::getVoidTy(Ctx), PointerType::getUnqual(Ctx),
234 RewindFunctionCallingConv = TLI.getLibcallCallingConv(RTLIB::UNWIND_RESUME);
235 DoesRewindFunctionNeedExceptionObject =
true;
237 RewindFunction =
F.getParent()->getOrInsertFunction(RewindName, FTy);
240 if (ResumesLeft == 1) {
245 Value *ExnObj = GetExceptionObject(RI);
247 if (DoesRewindFunctionNeedExceptionObject)
259 CI->
setDebugLoc(DILocation::get(SP->getContext(), 0, 0, SP));
268 std::vector<DominatorTree::UpdateType> Updates;
269 Updates.reserve(Resumes.
size());
275 "exn.obj", UnwindBB);
282 Updates.push_back({DominatorTree::Insert, Parent, UnwindBB});
284 Value *ExnObj = GetExceptionObject(RI);
290 if (DoesRewindFunctionNeedExceptionObject)
303 DTU->applyUpdates(Updates);
308bool DwarfEHPrepare::run() {
309 bool Changed = InsertUnwindResumeCalls();
317 const Triple &TargetTriple) {
320 return DwarfEHPrepare(OptLevel,
F, TLI, DT ? &DTU :
nullptr,
TTI,
334 DwarfEHPrepareLegacyPass(
CodeGenOptLevel OptLevel = CodeGenOptLevel::Default)
343 if (
auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>())
344 DT = &DTWP->getDomTree();
345 if (OptLevel != CodeGenOptLevel::None) {
347 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
348 TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(
F);
356 if (OptLevel != CodeGenOptLevel::None) {
364 return "Exception handling preparation";
372 const auto &TLI = *TM->getSubtargetImpl(
F)->getTargetLowering();
375 auto OptLevel = TM->getOptLevel();
391char DwarfEHPrepareLegacyPass::ID = 0;
394 "Prepare DWARF exceptions",
false,
false)
402 return new DwarfEHPrepareLegacyPass(OptLevel);
This file implements the BitVector class.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static bool prepareDwarfEH(CodeGenOptLevel OptLevel, Function &F, const TargetLowering &TLI, DominatorTree *DT, const TargetTransformInfo *TTI, const Triple &TargetTriple)
Module.h This file contains the declarations for the Module class.
FunctionAnalysisManager FAM
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
This file describes how to lower LLVM code to machine code.
Target-Independent Code Generator Pass Configuration Options pass.
A container for analyses that lazily runs them and caches their results.
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM Basic Block Representation.
const LandingPadInst * getLandingPadInst() const
Return the landingpad instruction associated with the landing pad.
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
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...
static BranchInst * Create(BasicBlock *IfTrue, InsertPosition InsertBefore=nullptr)
void setCallingConv(CallingConv::ID CC)
This class represents a function call, abstracting a target machine's calling convention.
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Analysis pass which computes a DominatorTree.
Legacy analysis pass which computes a DominatorTree.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
FunctionPass class - This class is used to implement most global optimizations.
virtual bool runOnFunction(Function &F)=0
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
DISubprogram * getSubprogram() const
Get the attached subprogram.
This instruction inserts a struct field of array element value into an aggregate value.
unsigned getNumIndices() const
idx_iterator idx_begin() const
InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
This is an important class for using LLVM in a threaded context.
An instruction for reading from memory.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
void preserve()
Mark an analysis as preserved.
Resume the propagation of an exception.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Analysis pass providing the TargetTransformInfo.
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
Primary interface to the complete machine description for the target machine.
Target-Independent Code Generator Pass Configuration Options.
Triple - Helper class for working with autoconf configuration names.
static Type * getVoidTy(LLVMContext &C)
This function has undefined behavior.
Value * getOperand(unsigned i) const
LLVM Value Representation.
const ParentTy * getParent() const
self_iterator getIterator()
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
This is an optimization pass for GlobalISel generic memory operations.
bool isScopedEHPersonality(EHPersonality Pers)
Returns true if this personality uses scope-style EH IR instructions: catchswitch,...
EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
FunctionPass * createDwarfEHPass(CodeGenOptLevel OptLevel)
createDwarfEHPass - This pass mulches exception handling code into a form adapted to code generation.
CodeGenOptLevel
Code generation optimization level.
bool simplifyCFG(BasicBlock *BB, const TargetTransformInfo &TTI, DomTreeUpdater *DTU=nullptr, const SimplifyCFGOptions &Options={}, ArrayRef< WeakVH > LoopHeaders={})
bool isPotentiallyReachable(const Instruction *From, const Instruction *To, const SmallPtrSetImpl< BasicBlock * > *ExclusionSet=nullptr, const DominatorTree *DT=nullptr, const LoopInfo *LI=nullptr)
Determine whether instruction 'To' is reachable from 'From', without passing through any blocks in Ex...