10b57cec5SDimitry Andric //===- DwarfEHPrepare - Prepare exception handling for code generation ----===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This pass mulches exception handling code into a form adapted to code 100b57cec5SDimitry Andric // generation. Required if using dwarf exception handling. 110b57cec5SDimitry Andric // 120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 130b57cec5SDimitry Andric 140b57cec5SDimitry Andric #include "llvm/ADT/BitVector.h" 150b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 160b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h" 170b57cec5SDimitry Andric #include "llvm/Analysis/CFG.h" 18e8d8bef9SDimitry Andric #include "llvm/Analysis/DomTreeUpdater.h" 190b57cec5SDimitry Andric #include "llvm/Analysis/TargetTransformInfo.h" 200b57cec5SDimitry Andric #include "llvm/CodeGen/RuntimeLibcalls.h" 210b57cec5SDimitry Andric #include "llvm/CodeGen/TargetLowering.h" 220b57cec5SDimitry Andric #include "llvm/CodeGen/TargetPassConfig.h" 230b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h" 240b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h" 250b57cec5SDimitry Andric #include "llvm/IR/Constants.h" 266246ae0bSDimitry Andric #include "llvm/IR/DebugInfoMetadata.h" 270b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h" 280b57cec5SDimitry Andric #include "llvm/IR/Dominators.h" 29*06c3fb27SDimitry Andric #include "llvm/IR/EHPersonalities.h" 300b57cec5SDimitry Andric #include "llvm/IR/Function.h" 310b57cec5SDimitry Andric #include "llvm/IR/Instructions.h" 320b57cec5SDimitry Andric #include "llvm/IR/Module.h" 330b57cec5SDimitry Andric #include "llvm/IR/Type.h" 34480093f4SDimitry Andric #include "llvm/InitializePasses.h" 350b57cec5SDimitry Andric #include "llvm/Pass.h" 360b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 370b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h" 38*06c3fb27SDimitry Andric #include "llvm/TargetParser/Triple.h" 39480093f4SDimitry Andric #include "llvm/Transforms/Utils/Local.h" 400b57cec5SDimitry Andric #include <cstddef> 410b57cec5SDimitry Andric 420b57cec5SDimitry Andric using namespace llvm; 430b57cec5SDimitry Andric 440b57cec5SDimitry Andric #define DEBUG_TYPE "dwarfehprepare" 450b57cec5SDimitry Andric 460b57cec5SDimitry Andric STATISTIC(NumResumesLowered, "Number of resume calls lowered"); 47fe6060f1SDimitry Andric STATISTIC(NumCleanupLandingPadsUnreachable, 48fe6060f1SDimitry Andric "Number of cleanup landing pads found unreachable"); 49fe6060f1SDimitry Andric STATISTIC(NumCleanupLandingPadsRemaining, 50fe6060f1SDimitry Andric "Number of cleanup landing pads remaining"); 51fe6060f1SDimitry Andric STATISTIC(NumNoUnwind, "Number of functions with nounwind"); 52fe6060f1SDimitry Andric STATISTIC(NumUnwind, "Number of functions with unwind"); 530b57cec5SDimitry Andric 540b57cec5SDimitry Andric namespace { 550b57cec5SDimitry Andric 56e8d8bef9SDimitry Andric class DwarfEHPrepare { 575ffd83dbSDimitry Andric CodeGenOpt::Level OptLevel; 580b57cec5SDimitry Andric 59e8d8bef9SDimitry Andric Function &F; 60e8d8bef9SDimitry Andric const TargetLowering &TLI; 61e8d8bef9SDimitry Andric DomTreeUpdater *DTU; 62e8d8bef9SDimitry Andric const TargetTransformInfo *TTI; 63349cc55cSDimitry Andric const Triple &TargetTriple; 640b57cec5SDimitry Andric 65e8d8bef9SDimitry Andric /// Return the exception object from the value passed into 660b57cec5SDimitry Andric /// the 'resume' instruction (typically an aggregate). Clean up any dead 670b57cec5SDimitry Andric /// instructions, including the 'resume' instruction. 68e8d8bef9SDimitry Andric Value *GetExceptionObject(ResumeInst *RI); 69e8d8bef9SDimitry Andric 70e8d8bef9SDimitry Andric /// Replace resumes that are not reachable from a cleanup landing pad with 71e8d8bef9SDimitry Andric /// unreachable and then simplify those blocks. 72e8d8bef9SDimitry Andric size_t 73e8d8bef9SDimitry Andric pruneUnreachableResumes(SmallVectorImpl<ResumeInst *> &Resumes, 74e8d8bef9SDimitry Andric SmallVectorImpl<LandingPadInst *> &CleanupLPads); 75e8d8bef9SDimitry Andric 76e8d8bef9SDimitry Andric /// Convert the ResumeInsts that are still present 77e8d8bef9SDimitry Andric /// into calls to the appropriate _Unwind_Resume function. 78e8d8bef9SDimitry Andric bool InsertUnwindResumeCalls(); 79e8d8bef9SDimitry Andric 80e8d8bef9SDimitry Andric public: 81349cc55cSDimitry Andric DwarfEHPrepare(CodeGenOpt::Level OptLevel_, Function &F_, 82349cc55cSDimitry Andric const TargetLowering &TLI_, DomTreeUpdater *DTU_, 83349cc55cSDimitry Andric const TargetTransformInfo *TTI_, const Triple &TargetTriple_) 84349cc55cSDimitry Andric : OptLevel(OptLevel_), F(F_), TLI(TLI_), DTU(DTU_), TTI(TTI_), 85349cc55cSDimitry Andric TargetTriple(TargetTriple_) {} 86e8d8bef9SDimitry Andric 87e8d8bef9SDimitry Andric bool run(); 88e8d8bef9SDimitry Andric }; 89e8d8bef9SDimitry Andric 90e8d8bef9SDimitry Andric } // namespace 91e8d8bef9SDimitry Andric 920b57cec5SDimitry Andric Value *DwarfEHPrepare::GetExceptionObject(ResumeInst *RI) { 930b57cec5SDimitry Andric Value *V = RI->getOperand(0); 940b57cec5SDimitry Andric Value *ExnObj = nullptr; 950b57cec5SDimitry Andric InsertValueInst *SelIVI = dyn_cast<InsertValueInst>(V); 960b57cec5SDimitry Andric LoadInst *SelLoad = nullptr; 970b57cec5SDimitry Andric InsertValueInst *ExcIVI = nullptr; 980b57cec5SDimitry Andric bool EraseIVIs = false; 990b57cec5SDimitry Andric 1000b57cec5SDimitry Andric if (SelIVI) { 1010b57cec5SDimitry Andric if (SelIVI->getNumIndices() == 1 && *SelIVI->idx_begin() == 1) { 1020b57cec5SDimitry Andric ExcIVI = dyn_cast<InsertValueInst>(SelIVI->getOperand(0)); 1030b57cec5SDimitry Andric if (ExcIVI && isa<UndefValue>(ExcIVI->getOperand(0)) && 1040b57cec5SDimitry Andric ExcIVI->getNumIndices() == 1 && *ExcIVI->idx_begin() == 0) { 1050b57cec5SDimitry Andric ExnObj = ExcIVI->getOperand(1); 1060b57cec5SDimitry Andric SelLoad = dyn_cast<LoadInst>(SelIVI->getOperand(1)); 1070b57cec5SDimitry Andric EraseIVIs = true; 1080b57cec5SDimitry Andric } 1090b57cec5SDimitry Andric } 1100b57cec5SDimitry Andric } 1110b57cec5SDimitry Andric 1120b57cec5SDimitry Andric if (!ExnObj) 1130b57cec5SDimitry Andric ExnObj = ExtractValueInst::Create(RI->getOperand(0), 0, "exn.obj", RI); 1140b57cec5SDimitry Andric 1150b57cec5SDimitry Andric RI->eraseFromParent(); 1160b57cec5SDimitry Andric 1170b57cec5SDimitry Andric if (EraseIVIs) { 1180b57cec5SDimitry Andric if (SelIVI->use_empty()) 1190b57cec5SDimitry Andric SelIVI->eraseFromParent(); 1200b57cec5SDimitry Andric if (ExcIVI->use_empty()) 1210b57cec5SDimitry Andric ExcIVI->eraseFromParent(); 1220b57cec5SDimitry Andric if (SelLoad && SelLoad->use_empty()) 1230b57cec5SDimitry Andric SelLoad->eraseFromParent(); 1240b57cec5SDimitry Andric } 1250b57cec5SDimitry Andric 1260b57cec5SDimitry Andric return ExnObj; 1270b57cec5SDimitry Andric } 1280b57cec5SDimitry Andric 1290b57cec5SDimitry Andric size_t DwarfEHPrepare::pruneUnreachableResumes( 130e8d8bef9SDimitry Andric SmallVectorImpl<ResumeInst *> &Resumes, 1310b57cec5SDimitry Andric SmallVectorImpl<LandingPadInst *> &CleanupLPads) { 132e8d8bef9SDimitry Andric assert(DTU && "Should have DomTreeUpdater here."); 133e8d8bef9SDimitry Andric 1340b57cec5SDimitry Andric BitVector ResumeReachable(Resumes.size()); 1350b57cec5SDimitry Andric size_t ResumeIndex = 0; 1360b57cec5SDimitry Andric for (auto *RI : Resumes) { 1370b57cec5SDimitry Andric for (auto *LP : CleanupLPads) { 138e8d8bef9SDimitry Andric if (isPotentiallyReachable(LP, RI, nullptr, &DTU->getDomTree())) { 1390b57cec5SDimitry Andric ResumeReachable.set(ResumeIndex); 1400b57cec5SDimitry Andric break; 1410b57cec5SDimitry Andric } 1420b57cec5SDimitry Andric } 1430b57cec5SDimitry Andric ++ResumeIndex; 1440b57cec5SDimitry Andric } 1450b57cec5SDimitry Andric 1460b57cec5SDimitry Andric // If everything is reachable, there is no change. 1470b57cec5SDimitry Andric if (ResumeReachable.all()) 1480b57cec5SDimitry Andric return Resumes.size(); 1490b57cec5SDimitry Andric 150e8d8bef9SDimitry Andric LLVMContext &Ctx = F.getContext(); 1510b57cec5SDimitry Andric 1520b57cec5SDimitry Andric // Otherwise, insert unreachable instructions and call simplifycfg. 1530b57cec5SDimitry Andric size_t ResumesLeft = 0; 1540b57cec5SDimitry Andric for (size_t I = 0, E = Resumes.size(); I < E; ++I) { 1550b57cec5SDimitry Andric ResumeInst *RI = Resumes[I]; 1560b57cec5SDimitry Andric if (ResumeReachable[I]) { 1570b57cec5SDimitry Andric Resumes[ResumesLeft++] = RI; 1580b57cec5SDimitry Andric } else { 1590b57cec5SDimitry Andric BasicBlock *BB = RI->getParent(); 1600b57cec5SDimitry Andric new UnreachableInst(Ctx, RI); 1610b57cec5SDimitry Andric RI->eraseFromParent(); 162fe6060f1SDimitry Andric simplifyCFG(BB, *TTI, DTU); 1630b57cec5SDimitry Andric } 1640b57cec5SDimitry Andric } 1650b57cec5SDimitry Andric Resumes.resize(ResumesLeft); 1660b57cec5SDimitry Andric return ResumesLeft; 1670b57cec5SDimitry Andric } 1680b57cec5SDimitry Andric 169e8d8bef9SDimitry Andric bool DwarfEHPrepare::InsertUnwindResumeCalls() { 1700b57cec5SDimitry Andric SmallVector<ResumeInst *, 16> Resumes; 1710b57cec5SDimitry Andric SmallVector<LandingPadInst *, 16> CleanupLPads; 172fe6060f1SDimitry Andric if (F.doesNotThrow()) 173fe6060f1SDimitry Andric NumNoUnwind++; 174fe6060f1SDimitry Andric else 175fe6060f1SDimitry Andric NumUnwind++; 176e8d8bef9SDimitry Andric for (BasicBlock &BB : F) { 1770b57cec5SDimitry Andric if (auto *RI = dyn_cast<ResumeInst>(BB.getTerminator())) 1780b57cec5SDimitry Andric Resumes.push_back(RI); 1790b57cec5SDimitry Andric if (auto *LP = BB.getLandingPadInst()) 1800b57cec5SDimitry Andric if (LP->isCleanup()) 1810b57cec5SDimitry Andric CleanupLPads.push_back(LP); 1820b57cec5SDimitry Andric } 1830b57cec5SDimitry Andric 184fe6060f1SDimitry Andric NumCleanupLandingPadsRemaining += CleanupLPads.size(); 185fe6060f1SDimitry Andric 1860b57cec5SDimitry Andric if (Resumes.empty()) 1870b57cec5SDimitry Andric return false; 1880b57cec5SDimitry Andric 1890b57cec5SDimitry Andric // Check the personality, don't do anything if it's scope-based. 190e8d8bef9SDimitry Andric EHPersonality Pers = classifyEHPersonality(F.getPersonalityFn()); 1910b57cec5SDimitry Andric if (isScopedEHPersonality(Pers)) 1920b57cec5SDimitry Andric return false; 1930b57cec5SDimitry Andric 194e8d8bef9SDimitry Andric LLVMContext &Ctx = F.getContext(); 1950b57cec5SDimitry Andric 1965ffd83dbSDimitry Andric size_t ResumesLeft = Resumes.size(); 197fe6060f1SDimitry Andric if (OptLevel != CodeGenOpt::None) { 198e8d8bef9SDimitry Andric ResumesLeft = pruneUnreachableResumes(Resumes, CleanupLPads); 199fe6060f1SDimitry Andric #if LLVM_ENABLE_STATS 200fe6060f1SDimitry Andric unsigned NumRemainingLPs = 0; 201fe6060f1SDimitry Andric for (BasicBlock &BB : F) { 202fe6060f1SDimitry Andric if (auto *LP = BB.getLandingPadInst()) 203fe6060f1SDimitry Andric if (LP->isCleanup()) 204fe6060f1SDimitry Andric NumRemainingLPs++; 205fe6060f1SDimitry Andric } 206fe6060f1SDimitry Andric NumCleanupLandingPadsUnreachable += CleanupLPads.size() - NumRemainingLPs; 207fe6060f1SDimitry Andric NumCleanupLandingPadsRemaining -= CleanupLPads.size() - NumRemainingLPs; 208fe6060f1SDimitry Andric #endif 209fe6060f1SDimitry Andric } 2105ffd83dbSDimitry Andric 2110b57cec5SDimitry Andric if (ResumesLeft == 0) 2120b57cec5SDimitry Andric return true; // We pruned them all. 2130b57cec5SDimitry Andric 214349cc55cSDimitry Andric // RewindFunction - _Unwind_Resume or the target equivalent. 215349cc55cSDimitry Andric FunctionCallee RewindFunction; 216349cc55cSDimitry Andric CallingConv::ID RewindFunctionCallingConv; 217349cc55cSDimitry Andric FunctionType *FTy; 218349cc55cSDimitry Andric const char *RewindName; 219349cc55cSDimitry Andric bool DoesRewindFunctionNeedExceptionObject; 220349cc55cSDimitry Andric 221349cc55cSDimitry Andric if ((Pers == EHPersonality::GNU_CXX || Pers == EHPersonality::GNU_CXX_SjLj) && 222349cc55cSDimitry Andric TargetTriple.isTargetEHABICompatible()) { 223349cc55cSDimitry Andric RewindName = TLI.getLibcallName(RTLIB::CXA_END_CLEANUP); 224349cc55cSDimitry Andric FTy = FunctionType::get(Type::getVoidTy(Ctx), false); 225349cc55cSDimitry Andric RewindFunctionCallingConv = 226349cc55cSDimitry Andric TLI.getLibcallCallingConv(RTLIB::CXA_END_CLEANUP); 227349cc55cSDimitry Andric DoesRewindFunctionNeedExceptionObject = false; 228349cc55cSDimitry Andric } else { 229349cc55cSDimitry Andric RewindName = TLI.getLibcallName(RTLIB::UNWIND_RESUME); 230349cc55cSDimitry Andric FTy = 231e8d8bef9SDimitry Andric FunctionType::get(Type::getVoidTy(Ctx), Type::getInt8PtrTy(Ctx), false); 232349cc55cSDimitry Andric RewindFunctionCallingConv = TLI.getLibcallCallingConv(RTLIB::UNWIND_RESUME); 233349cc55cSDimitry Andric DoesRewindFunctionNeedExceptionObject = true; 2340b57cec5SDimitry Andric } 235349cc55cSDimitry Andric RewindFunction = F.getParent()->getOrInsertFunction(RewindName, FTy); 2360b57cec5SDimitry Andric 2370b57cec5SDimitry Andric // Create the basic block where the _Unwind_Resume call will live. 2380b57cec5SDimitry Andric if (ResumesLeft == 1) { 2390b57cec5SDimitry Andric // Instead of creating a new BB and PHI node, just append the call to 2400b57cec5SDimitry Andric // _Unwind_Resume to the end of the single resume block. 2410b57cec5SDimitry Andric ResumeInst *RI = Resumes.front(); 2420b57cec5SDimitry Andric BasicBlock *UnwindBB = RI->getParent(); 2430b57cec5SDimitry Andric Value *ExnObj = GetExceptionObject(RI); 244349cc55cSDimitry Andric llvm::SmallVector<Value *, 1> RewindFunctionArgs; 245349cc55cSDimitry Andric if (DoesRewindFunctionNeedExceptionObject) 246349cc55cSDimitry Andric RewindFunctionArgs.push_back(ExnObj); 2470b57cec5SDimitry Andric 248349cc55cSDimitry Andric // Call the rewind function. 249349cc55cSDimitry Andric CallInst *CI = 250349cc55cSDimitry Andric CallInst::Create(RewindFunction, RewindFunctionArgs, "", UnwindBB); 2516246ae0bSDimitry Andric // The verifier requires that all calls of debug-info-bearing functions 2526246ae0bSDimitry Andric // from debug-info-bearing functions have a debug location (for inlining 2536246ae0bSDimitry Andric // purposes). Assign a dummy location to satisfy the constraint. 2546246ae0bSDimitry Andric Function *RewindFn = dyn_cast<Function>(RewindFunction.getCallee()); 2556246ae0bSDimitry Andric if (RewindFn && RewindFn->getSubprogram()) 2566246ae0bSDimitry Andric if (DISubprogram *SP = F.getSubprogram()) 2576246ae0bSDimitry Andric CI->setDebugLoc(DILocation::get(SP->getContext(), 0, 0, SP)); 258349cc55cSDimitry Andric CI->setCallingConv(RewindFunctionCallingConv); 2590b57cec5SDimitry Andric 2600b57cec5SDimitry Andric // We never expect _Unwind_Resume to return. 261e8d8bef9SDimitry Andric CI->setDoesNotReturn(); 2620b57cec5SDimitry Andric new UnreachableInst(Ctx, UnwindBB); 2630b57cec5SDimitry Andric return true; 2640b57cec5SDimitry Andric } 2650b57cec5SDimitry Andric 266e8d8bef9SDimitry Andric std::vector<DominatorTree::UpdateType> Updates; 267e8d8bef9SDimitry Andric Updates.reserve(Resumes.size()); 268e8d8bef9SDimitry Andric 269349cc55cSDimitry Andric llvm::SmallVector<Value *, 1> RewindFunctionArgs; 270349cc55cSDimitry Andric 271e8d8bef9SDimitry Andric BasicBlock *UnwindBB = BasicBlock::Create(Ctx, "unwind_resume", &F); 272e8d8bef9SDimitry Andric PHINode *PN = PHINode::Create(Type::getInt8PtrTy(Ctx), ResumesLeft, "exn.obj", 273e8d8bef9SDimitry Andric UnwindBB); 2740b57cec5SDimitry Andric 2750b57cec5SDimitry Andric // Extract the exception object from the ResumeInst and add it to the PHI node 2760b57cec5SDimitry Andric // that feeds the _Unwind_Resume call. 2770b57cec5SDimitry Andric for (ResumeInst *RI : Resumes) { 2780b57cec5SDimitry Andric BasicBlock *Parent = RI->getParent(); 2790b57cec5SDimitry Andric BranchInst::Create(UnwindBB, Parent); 280e8d8bef9SDimitry Andric Updates.push_back({DominatorTree::Insert, Parent, UnwindBB}); 2810b57cec5SDimitry Andric 2820b57cec5SDimitry Andric Value *ExnObj = GetExceptionObject(RI); 2830b57cec5SDimitry Andric PN->addIncoming(ExnObj, Parent); 2840b57cec5SDimitry Andric 2850b57cec5SDimitry Andric ++NumResumesLowered; 2860b57cec5SDimitry Andric } 2870b57cec5SDimitry Andric 288349cc55cSDimitry Andric if (DoesRewindFunctionNeedExceptionObject) 289349cc55cSDimitry Andric RewindFunctionArgs.push_back(PN); 290349cc55cSDimitry Andric 2910b57cec5SDimitry Andric // Call the function. 292349cc55cSDimitry Andric CallInst *CI = 293349cc55cSDimitry Andric CallInst::Create(RewindFunction, RewindFunctionArgs, "", UnwindBB); 294349cc55cSDimitry Andric CI->setCallingConv(RewindFunctionCallingConv); 2950b57cec5SDimitry Andric 2960b57cec5SDimitry Andric // We never expect _Unwind_Resume to return. 297e8d8bef9SDimitry Andric CI->setDoesNotReturn(); 2980b57cec5SDimitry Andric new UnreachableInst(Ctx, UnwindBB); 299e8d8bef9SDimitry Andric 300fe6060f1SDimitry Andric if (DTU) 301e8d8bef9SDimitry Andric DTU->applyUpdates(Updates); 302e8d8bef9SDimitry Andric 3030b57cec5SDimitry Andric return true; 3040b57cec5SDimitry Andric } 3050b57cec5SDimitry Andric 306e8d8bef9SDimitry Andric bool DwarfEHPrepare::run() { 307e8d8bef9SDimitry Andric bool Changed = InsertUnwindResumeCalls(); 308e8d8bef9SDimitry Andric 309e8d8bef9SDimitry Andric return Changed; 310e8d8bef9SDimitry Andric } 311e8d8bef9SDimitry Andric 312349cc55cSDimitry Andric static bool prepareDwarfEH(CodeGenOpt::Level OptLevel, Function &F, 313e8d8bef9SDimitry Andric const TargetLowering &TLI, DominatorTree *DT, 314349cc55cSDimitry Andric const TargetTransformInfo *TTI, 315349cc55cSDimitry Andric const Triple &TargetTriple) { 316fe6060f1SDimitry Andric DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy); 317e8d8bef9SDimitry Andric 318349cc55cSDimitry Andric return DwarfEHPrepare(OptLevel, F, TLI, DT ? &DTU : nullptr, TTI, 319349cc55cSDimitry Andric TargetTriple) 320e8d8bef9SDimitry Andric .run(); 321e8d8bef9SDimitry Andric } 322e8d8bef9SDimitry Andric 323e8d8bef9SDimitry Andric namespace { 324e8d8bef9SDimitry Andric 325e8d8bef9SDimitry Andric class DwarfEHPrepareLegacyPass : public FunctionPass { 326e8d8bef9SDimitry Andric 327e8d8bef9SDimitry Andric CodeGenOpt::Level OptLevel; 328e8d8bef9SDimitry Andric 329e8d8bef9SDimitry Andric public: 330e8d8bef9SDimitry Andric static char ID; // Pass identification, replacement for typeid. 331e8d8bef9SDimitry Andric 332e8d8bef9SDimitry Andric DwarfEHPrepareLegacyPass(CodeGenOpt::Level OptLevel = CodeGenOpt::Default) 333e8d8bef9SDimitry Andric : FunctionPass(ID), OptLevel(OptLevel) {} 334e8d8bef9SDimitry Andric 335e8d8bef9SDimitry Andric bool runOnFunction(Function &F) override { 3360b57cec5SDimitry Andric const TargetMachine &TM = 3370b57cec5SDimitry Andric getAnalysis<TargetPassConfig>().getTM<TargetMachine>(); 338e8d8bef9SDimitry Andric const TargetLowering &TLI = *TM.getSubtargetImpl(F)->getTargetLowering(); 339e8d8bef9SDimitry Andric DominatorTree *DT = nullptr; 340e8d8bef9SDimitry Andric const TargetTransformInfo *TTI = nullptr; 341fe6060f1SDimitry Andric if (auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>()) 342fe6060f1SDimitry Andric DT = &DTWP->getDomTree(); 343e8d8bef9SDimitry Andric if (OptLevel != CodeGenOpt::None) { 344fe6060f1SDimitry Andric if (!DT) 345e8d8bef9SDimitry Andric DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 346e8d8bef9SDimitry Andric TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F); 347e8d8bef9SDimitry Andric } 348349cc55cSDimitry Andric return prepareDwarfEH(OptLevel, F, TLI, DT, TTI, TM.getTargetTriple()); 349e8d8bef9SDimitry Andric } 350e8d8bef9SDimitry Andric 351e8d8bef9SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override { 352e8d8bef9SDimitry Andric AU.addRequired<TargetPassConfig>(); 353e8d8bef9SDimitry Andric AU.addRequired<TargetTransformInfoWrapperPass>(); 354e8d8bef9SDimitry Andric if (OptLevel != CodeGenOpt::None) { 355e8d8bef9SDimitry Andric AU.addRequired<DominatorTreeWrapperPass>(); 356e8d8bef9SDimitry Andric AU.addRequired<TargetTransformInfoWrapperPass>(); 357e8d8bef9SDimitry Andric } 358fe6060f1SDimitry Andric AU.addPreserved<DominatorTreeWrapperPass>(); 359e8d8bef9SDimitry Andric } 360e8d8bef9SDimitry Andric 361e8d8bef9SDimitry Andric StringRef getPassName() const override { 362e8d8bef9SDimitry Andric return "Exception handling preparation"; 363e8d8bef9SDimitry Andric } 364e8d8bef9SDimitry Andric }; 365e8d8bef9SDimitry Andric 366e8d8bef9SDimitry Andric } // end anonymous namespace 367e8d8bef9SDimitry Andric 368e8d8bef9SDimitry Andric char DwarfEHPrepareLegacyPass::ID = 0; 369e8d8bef9SDimitry Andric 370e8d8bef9SDimitry Andric INITIALIZE_PASS_BEGIN(DwarfEHPrepareLegacyPass, DEBUG_TYPE, 371e8d8bef9SDimitry Andric "Prepare DWARF exceptions", false, false) 372e8d8bef9SDimitry Andric INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 373e8d8bef9SDimitry Andric INITIALIZE_PASS_DEPENDENCY(TargetPassConfig) 374e8d8bef9SDimitry Andric INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) 375e8d8bef9SDimitry Andric INITIALIZE_PASS_END(DwarfEHPrepareLegacyPass, DEBUG_TYPE, 376e8d8bef9SDimitry Andric "Prepare DWARF exceptions", false, false) 377e8d8bef9SDimitry Andric 378e8d8bef9SDimitry Andric FunctionPass *llvm::createDwarfEHPass(CodeGenOpt::Level OptLevel) { 379e8d8bef9SDimitry Andric return new DwarfEHPrepareLegacyPass(OptLevel); 3800b57cec5SDimitry Andric } 381