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" 17*349cc55cSDimitry Andric #include "llvm/ADT/Triple.h" 180b57cec5SDimitry Andric #include "llvm/Analysis/CFG.h" 19e8d8bef9SDimitry Andric #include "llvm/Analysis/DomTreeUpdater.h" 200b57cec5SDimitry Andric #include "llvm/Analysis/EHPersonalities.h" 210b57cec5SDimitry Andric #include "llvm/Analysis/TargetTransformInfo.h" 220b57cec5SDimitry Andric #include "llvm/CodeGen/RuntimeLibcalls.h" 230b57cec5SDimitry Andric #include "llvm/CodeGen/TargetLowering.h" 240b57cec5SDimitry Andric #include "llvm/CodeGen/TargetPassConfig.h" 250b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h" 260b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h" 270b57cec5SDimitry Andric #include "llvm/IR/Constants.h" 280b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h" 290b57cec5SDimitry Andric #include "llvm/IR/Dominators.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" 38480093f4SDimitry Andric #include "llvm/Transforms/Utils/Local.h" 390b57cec5SDimitry Andric #include <cstddef> 400b57cec5SDimitry Andric 410b57cec5SDimitry Andric using namespace llvm; 420b57cec5SDimitry Andric 430b57cec5SDimitry Andric #define DEBUG_TYPE "dwarfehprepare" 440b57cec5SDimitry Andric 450b57cec5SDimitry Andric STATISTIC(NumResumesLowered, "Number of resume calls lowered"); 46fe6060f1SDimitry Andric STATISTIC(NumCleanupLandingPadsUnreachable, 47fe6060f1SDimitry Andric "Number of cleanup landing pads found unreachable"); 48fe6060f1SDimitry Andric STATISTIC(NumCleanupLandingPadsRemaining, 49fe6060f1SDimitry Andric "Number of cleanup landing pads remaining"); 50fe6060f1SDimitry Andric STATISTIC(NumNoUnwind, "Number of functions with nounwind"); 51fe6060f1SDimitry Andric STATISTIC(NumUnwind, "Number of functions with unwind"); 520b57cec5SDimitry Andric 530b57cec5SDimitry Andric namespace { 540b57cec5SDimitry Andric 55e8d8bef9SDimitry Andric class DwarfEHPrepare { 565ffd83dbSDimitry Andric CodeGenOpt::Level OptLevel; 570b57cec5SDimitry Andric 58e8d8bef9SDimitry Andric Function &F; 59e8d8bef9SDimitry Andric const TargetLowering &TLI; 60e8d8bef9SDimitry Andric DomTreeUpdater *DTU; 61e8d8bef9SDimitry Andric const TargetTransformInfo *TTI; 62*349cc55cSDimitry Andric const Triple &TargetTriple; 630b57cec5SDimitry Andric 64e8d8bef9SDimitry Andric /// Return the exception object from the value passed into 650b57cec5SDimitry Andric /// the 'resume' instruction (typically an aggregate). Clean up any dead 660b57cec5SDimitry Andric /// instructions, including the 'resume' instruction. 67e8d8bef9SDimitry Andric Value *GetExceptionObject(ResumeInst *RI); 68e8d8bef9SDimitry Andric 69e8d8bef9SDimitry Andric /// Replace resumes that are not reachable from a cleanup landing pad with 70e8d8bef9SDimitry Andric /// unreachable and then simplify those blocks. 71e8d8bef9SDimitry Andric size_t 72e8d8bef9SDimitry Andric pruneUnreachableResumes(SmallVectorImpl<ResumeInst *> &Resumes, 73e8d8bef9SDimitry Andric SmallVectorImpl<LandingPadInst *> &CleanupLPads); 74e8d8bef9SDimitry Andric 75e8d8bef9SDimitry Andric /// Convert the ResumeInsts that are still present 76e8d8bef9SDimitry Andric /// into calls to the appropriate _Unwind_Resume function. 77e8d8bef9SDimitry Andric bool InsertUnwindResumeCalls(); 78e8d8bef9SDimitry Andric 79e8d8bef9SDimitry Andric public: 80*349cc55cSDimitry Andric DwarfEHPrepare(CodeGenOpt::Level OptLevel_, Function &F_, 81*349cc55cSDimitry Andric const TargetLowering &TLI_, DomTreeUpdater *DTU_, 82*349cc55cSDimitry Andric const TargetTransformInfo *TTI_, const Triple &TargetTriple_) 83*349cc55cSDimitry Andric : OptLevel(OptLevel_), F(F_), TLI(TLI_), DTU(DTU_), TTI(TTI_), 84*349cc55cSDimitry Andric TargetTriple(TargetTriple_) {} 85e8d8bef9SDimitry Andric 86e8d8bef9SDimitry Andric bool run(); 87e8d8bef9SDimitry Andric }; 88e8d8bef9SDimitry Andric 89e8d8bef9SDimitry Andric } // namespace 90e8d8bef9SDimitry Andric 910b57cec5SDimitry Andric Value *DwarfEHPrepare::GetExceptionObject(ResumeInst *RI) { 920b57cec5SDimitry Andric Value *V = RI->getOperand(0); 930b57cec5SDimitry Andric Value *ExnObj = nullptr; 940b57cec5SDimitry Andric InsertValueInst *SelIVI = dyn_cast<InsertValueInst>(V); 950b57cec5SDimitry Andric LoadInst *SelLoad = nullptr; 960b57cec5SDimitry Andric InsertValueInst *ExcIVI = nullptr; 970b57cec5SDimitry Andric bool EraseIVIs = false; 980b57cec5SDimitry Andric 990b57cec5SDimitry Andric if (SelIVI) { 1000b57cec5SDimitry Andric if (SelIVI->getNumIndices() == 1 && *SelIVI->idx_begin() == 1) { 1010b57cec5SDimitry Andric ExcIVI = dyn_cast<InsertValueInst>(SelIVI->getOperand(0)); 1020b57cec5SDimitry Andric if (ExcIVI && isa<UndefValue>(ExcIVI->getOperand(0)) && 1030b57cec5SDimitry Andric ExcIVI->getNumIndices() == 1 && *ExcIVI->idx_begin() == 0) { 1040b57cec5SDimitry Andric ExnObj = ExcIVI->getOperand(1); 1050b57cec5SDimitry Andric SelLoad = dyn_cast<LoadInst>(SelIVI->getOperand(1)); 1060b57cec5SDimitry Andric EraseIVIs = true; 1070b57cec5SDimitry Andric } 1080b57cec5SDimitry Andric } 1090b57cec5SDimitry Andric } 1100b57cec5SDimitry Andric 1110b57cec5SDimitry Andric if (!ExnObj) 1120b57cec5SDimitry Andric ExnObj = ExtractValueInst::Create(RI->getOperand(0), 0, "exn.obj", RI); 1130b57cec5SDimitry Andric 1140b57cec5SDimitry Andric RI->eraseFromParent(); 1150b57cec5SDimitry Andric 1160b57cec5SDimitry Andric if (EraseIVIs) { 1170b57cec5SDimitry Andric if (SelIVI->use_empty()) 1180b57cec5SDimitry Andric SelIVI->eraseFromParent(); 1190b57cec5SDimitry Andric if (ExcIVI->use_empty()) 1200b57cec5SDimitry Andric ExcIVI->eraseFromParent(); 1210b57cec5SDimitry Andric if (SelLoad && SelLoad->use_empty()) 1220b57cec5SDimitry Andric SelLoad->eraseFromParent(); 1230b57cec5SDimitry Andric } 1240b57cec5SDimitry Andric 1250b57cec5SDimitry Andric return ExnObj; 1260b57cec5SDimitry Andric } 1270b57cec5SDimitry Andric 1280b57cec5SDimitry Andric size_t DwarfEHPrepare::pruneUnreachableResumes( 129e8d8bef9SDimitry Andric SmallVectorImpl<ResumeInst *> &Resumes, 1300b57cec5SDimitry Andric SmallVectorImpl<LandingPadInst *> &CleanupLPads) { 131e8d8bef9SDimitry Andric assert(DTU && "Should have DomTreeUpdater here."); 132e8d8bef9SDimitry Andric 1330b57cec5SDimitry Andric BitVector ResumeReachable(Resumes.size()); 1340b57cec5SDimitry Andric size_t ResumeIndex = 0; 1350b57cec5SDimitry Andric for (auto *RI : Resumes) { 1360b57cec5SDimitry Andric for (auto *LP : CleanupLPads) { 137e8d8bef9SDimitry Andric if (isPotentiallyReachable(LP, RI, nullptr, &DTU->getDomTree())) { 1380b57cec5SDimitry Andric ResumeReachable.set(ResumeIndex); 1390b57cec5SDimitry Andric break; 1400b57cec5SDimitry Andric } 1410b57cec5SDimitry Andric } 1420b57cec5SDimitry Andric ++ResumeIndex; 1430b57cec5SDimitry Andric } 1440b57cec5SDimitry Andric 1450b57cec5SDimitry Andric // If everything is reachable, there is no change. 1460b57cec5SDimitry Andric if (ResumeReachable.all()) 1470b57cec5SDimitry Andric return Resumes.size(); 1480b57cec5SDimitry Andric 149e8d8bef9SDimitry Andric LLVMContext &Ctx = F.getContext(); 1500b57cec5SDimitry Andric 1510b57cec5SDimitry Andric // Otherwise, insert unreachable instructions and call simplifycfg. 1520b57cec5SDimitry Andric size_t ResumesLeft = 0; 1530b57cec5SDimitry Andric for (size_t I = 0, E = Resumes.size(); I < E; ++I) { 1540b57cec5SDimitry Andric ResumeInst *RI = Resumes[I]; 1550b57cec5SDimitry Andric if (ResumeReachable[I]) { 1560b57cec5SDimitry Andric Resumes[ResumesLeft++] = RI; 1570b57cec5SDimitry Andric } else { 1580b57cec5SDimitry Andric BasicBlock *BB = RI->getParent(); 1590b57cec5SDimitry Andric new UnreachableInst(Ctx, RI); 1600b57cec5SDimitry Andric RI->eraseFromParent(); 161fe6060f1SDimitry Andric simplifyCFG(BB, *TTI, DTU); 1620b57cec5SDimitry Andric } 1630b57cec5SDimitry Andric } 1640b57cec5SDimitry Andric Resumes.resize(ResumesLeft); 1650b57cec5SDimitry Andric return ResumesLeft; 1660b57cec5SDimitry Andric } 1670b57cec5SDimitry Andric 168e8d8bef9SDimitry Andric bool DwarfEHPrepare::InsertUnwindResumeCalls() { 1690b57cec5SDimitry Andric SmallVector<ResumeInst *, 16> Resumes; 1700b57cec5SDimitry Andric SmallVector<LandingPadInst *, 16> CleanupLPads; 171fe6060f1SDimitry Andric if (F.doesNotThrow()) 172fe6060f1SDimitry Andric NumNoUnwind++; 173fe6060f1SDimitry Andric else 174fe6060f1SDimitry Andric NumUnwind++; 175e8d8bef9SDimitry Andric for (BasicBlock &BB : F) { 1760b57cec5SDimitry Andric if (auto *RI = dyn_cast<ResumeInst>(BB.getTerminator())) 1770b57cec5SDimitry Andric Resumes.push_back(RI); 1780b57cec5SDimitry Andric if (auto *LP = BB.getLandingPadInst()) 1790b57cec5SDimitry Andric if (LP->isCleanup()) 1800b57cec5SDimitry Andric CleanupLPads.push_back(LP); 1810b57cec5SDimitry Andric } 1820b57cec5SDimitry Andric 183fe6060f1SDimitry Andric NumCleanupLandingPadsRemaining += CleanupLPads.size(); 184fe6060f1SDimitry Andric 1850b57cec5SDimitry Andric if (Resumes.empty()) 1860b57cec5SDimitry Andric return false; 1870b57cec5SDimitry Andric 1880b57cec5SDimitry Andric // Check the personality, don't do anything if it's scope-based. 189e8d8bef9SDimitry Andric EHPersonality Pers = classifyEHPersonality(F.getPersonalityFn()); 1900b57cec5SDimitry Andric if (isScopedEHPersonality(Pers)) 1910b57cec5SDimitry Andric return false; 1920b57cec5SDimitry Andric 193e8d8bef9SDimitry Andric LLVMContext &Ctx = F.getContext(); 1940b57cec5SDimitry Andric 1955ffd83dbSDimitry Andric size_t ResumesLeft = Resumes.size(); 196fe6060f1SDimitry Andric if (OptLevel != CodeGenOpt::None) { 197e8d8bef9SDimitry Andric ResumesLeft = pruneUnreachableResumes(Resumes, CleanupLPads); 198fe6060f1SDimitry Andric #if LLVM_ENABLE_STATS 199fe6060f1SDimitry Andric unsigned NumRemainingLPs = 0; 200fe6060f1SDimitry Andric for (BasicBlock &BB : F) { 201fe6060f1SDimitry Andric if (auto *LP = BB.getLandingPadInst()) 202fe6060f1SDimitry Andric if (LP->isCleanup()) 203fe6060f1SDimitry Andric NumRemainingLPs++; 204fe6060f1SDimitry Andric } 205fe6060f1SDimitry Andric NumCleanupLandingPadsUnreachable += CleanupLPads.size() - NumRemainingLPs; 206fe6060f1SDimitry Andric NumCleanupLandingPadsRemaining -= CleanupLPads.size() - NumRemainingLPs; 207fe6060f1SDimitry Andric #endif 208fe6060f1SDimitry Andric } 2095ffd83dbSDimitry Andric 2100b57cec5SDimitry Andric if (ResumesLeft == 0) 2110b57cec5SDimitry Andric return true; // We pruned them all. 2120b57cec5SDimitry Andric 213*349cc55cSDimitry Andric // RewindFunction - _Unwind_Resume or the target equivalent. 214*349cc55cSDimitry Andric FunctionCallee RewindFunction; 215*349cc55cSDimitry Andric CallingConv::ID RewindFunctionCallingConv; 216*349cc55cSDimitry Andric FunctionType *FTy; 217*349cc55cSDimitry Andric const char *RewindName; 218*349cc55cSDimitry Andric bool DoesRewindFunctionNeedExceptionObject; 219*349cc55cSDimitry Andric 220*349cc55cSDimitry Andric if ((Pers == EHPersonality::GNU_CXX || Pers == EHPersonality::GNU_CXX_SjLj) && 221*349cc55cSDimitry Andric TargetTriple.isTargetEHABICompatible()) { 222*349cc55cSDimitry Andric RewindName = TLI.getLibcallName(RTLIB::CXA_END_CLEANUP); 223*349cc55cSDimitry Andric FTy = FunctionType::get(Type::getVoidTy(Ctx), false); 224*349cc55cSDimitry Andric RewindFunctionCallingConv = 225*349cc55cSDimitry Andric TLI.getLibcallCallingConv(RTLIB::CXA_END_CLEANUP); 226*349cc55cSDimitry Andric DoesRewindFunctionNeedExceptionObject = false; 227*349cc55cSDimitry Andric } else { 228*349cc55cSDimitry Andric RewindName = TLI.getLibcallName(RTLIB::UNWIND_RESUME); 229*349cc55cSDimitry Andric FTy = 230e8d8bef9SDimitry Andric FunctionType::get(Type::getVoidTy(Ctx), Type::getInt8PtrTy(Ctx), false); 231*349cc55cSDimitry Andric RewindFunctionCallingConv = TLI.getLibcallCallingConv(RTLIB::UNWIND_RESUME); 232*349cc55cSDimitry Andric DoesRewindFunctionNeedExceptionObject = true; 2330b57cec5SDimitry Andric } 234*349cc55cSDimitry Andric RewindFunction = F.getParent()->getOrInsertFunction(RewindName, FTy); 2350b57cec5SDimitry Andric 2360b57cec5SDimitry Andric // Create the basic block where the _Unwind_Resume call will live. 2370b57cec5SDimitry Andric if (ResumesLeft == 1) { 2380b57cec5SDimitry Andric // Instead of creating a new BB and PHI node, just append the call to 2390b57cec5SDimitry Andric // _Unwind_Resume to the end of the single resume block. 2400b57cec5SDimitry Andric ResumeInst *RI = Resumes.front(); 2410b57cec5SDimitry Andric BasicBlock *UnwindBB = RI->getParent(); 2420b57cec5SDimitry Andric Value *ExnObj = GetExceptionObject(RI); 243*349cc55cSDimitry Andric llvm::SmallVector<Value *, 1> RewindFunctionArgs; 244*349cc55cSDimitry Andric if (DoesRewindFunctionNeedExceptionObject) 245*349cc55cSDimitry Andric RewindFunctionArgs.push_back(ExnObj); 2460b57cec5SDimitry Andric 247*349cc55cSDimitry Andric // Call the rewind function. 248*349cc55cSDimitry Andric CallInst *CI = 249*349cc55cSDimitry Andric CallInst::Create(RewindFunction, RewindFunctionArgs, "", UnwindBB); 250*349cc55cSDimitry Andric CI->setCallingConv(RewindFunctionCallingConv); 2510b57cec5SDimitry Andric 2520b57cec5SDimitry Andric // We never expect _Unwind_Resume to return. 253e8d8bef9SDimitry Andric CI->setDoesNotReturn(); 2540b57cec5SDimitry Andric new UnreachableInst(Ctx, UnwindBB); 2550b57cec5SDimitry Andric return true; 2560b57cec5SDimitry Andric } 2570b57cec5SDimitry Andric 258e8d8bef9SDimitry Andric std::vector<DominatorTree::UpdateType> Updates; 259e8d8bef9SDimitry Andric Updates.reserve(Resumes.size()); 260e8d8bef9SDimitry Andric 261*349cc55cSDimitry Andric llvm::SmallVector<Value *, 1> RewindFunctionArgs; 262*349cc55cSDimitry Andric 263e8d8bef9SDimitry Andric BasicBlock *UnwindBB = BasicBlock::Create(Ctx, "unwind_resume", &F); 264e8d8bef9SDimitry Andric PHINode *PN = PHINode::Create(Type::getInt8PtrTy(Ctx), ResumesLeft, "exn.obj", 265e8d8bef9SDimitry Andric UnwindBB); 2660b57cec5SDimitry Andric 2670b57cec5SDimitry Andric // Extract the exception object from the ResumeInst and add it to the PHI node 2680b57cec5SDimitry Andric // that feeds the _Unwind_Resume call. 2690b57cec5SDimitry Andric for (ResumeInst *RI : Resumes) { 2700b57cec5SDimitry Andric BasicBlock *Parent = RI->getParent(); 2710b57cec5SDimitry Andric BranchInst::Create(UnwindBB, Parent); 272e8d8bef9SDimitry Andric Updates.push_back({DominatorTree::Insert, Parent, UnwindBB}); 2730b57cec5SDimitry Andric 2740b57cec5SDimitry Andric Value *ExnObj = GetExceptionObject(RI); 2750b57cec5SDimitry Andric PN->addIncoming(ExnObj, Parent); 2760b57cec5SDimitry Andric 2770b57cec5SDimitry Andric ++NumResumesLowered; 2780b57cec5SDimitry Andric } 2790b57cec5SDimitry Andric 280*349cc55cSDimitry Andric if (DoesRewindFunctionNeedExceptionObject) 281*349cc55cSDimitry Andric RewindFunctionArgs.push_back(PN); 282*349cc55cSDimitry Andric 2830b57cec5SDimitry Andric // Call the function. 284*349cc55cSDimitry Andric CallInst *CI = 285*349cc55cSDimitry Andric CallInst::Create(RewindFunction, RewindFunctionArgs, "", UnwindBB); 286*349cc55cSDimitry Andric CI->setCallingConv(RewindFunctionCallingConv); 2870b57cec5SDimitry Andric 2880b57cec5SDimitry Andric // We never expect _Unwind_Resume to return. 289e8d8bef9SDimitry Andric CI->setDoesNotReturn(); 2900b57cec5SDimitry Andric new UnreachableInst(Ctx, UnwindBB); 291e8d8bef9SDimitry Andric 292fe6060f1SDimitry Andric if (DTU) 293e8d8bef9SDimitry Andric DTU->applyUpdates(Updates); 294e8d8bef9SDimitry Andric 2950b57cec5SDimitry Andric return true; 2960b57cec5SDimitry Andric } 2970b57cec5SDimitry Andric 298e8d8bef9SDimitry Andric bool DwarfEHPrepare::run() { 299e8d8bef9SDimitry Andric bool Changed = InsertUnwindResumeCalls(); 300e8d8bef9SDimitry Andric 301e8d8bef9SDimitry Andric return Changed; 302e8d8bef9SDimitry Andric } 303e8d8bef9SDimitry Andric 304*349cc55cSDimitry Andric static bool prepareDwarfEH(CodeGenOpt::Level OptLevel, Function &F, 305e8d8bef9SDimitry Andric const TargetLowering &TLI, DominatorTree *DT, 306*349cc55cSDimitry Andric const TargetTransformInfo *TTI, 307*349cc55cSDimitry Andric const Triple &TargetTriple) { 308fe6060f1SDimitry Andric DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy); 309e8d8bef9SDimitry Andric 310*349cc55cSDimitry Andric return DwarfEHPrepare(OptLevel, F, TLI, DT ? &DTU : nullptr, TTI, 311*349cc55cSDimitry Andric TargetTriple) 312e8d8bef9SDimitry Andric .run(); 313e8d8bef9SDimitry Andric } 314e8d8bef9SDimitry Andric 315e8d8bef9SDimitry Andric namespace { 316e8d8bef9SDimitry Andric 317e8d8bef9SDimitry Andric class DwarfEHPrepareLegacyPass : public FunctionPass { 318e8d8bef9SDimitry Andric 319e8d8bef9SDimitry Andric CodeGenOpt::Level OptLevel; 320e8d8bef9SDimitry Andric 321e8d8bef9SDimitry Andric public: 322e8d8bef9SDimitry Andric static char ID; // Pass identification, replacement for typeid. 323e8d8bef9SDimitry Andric 324e8d8bef9SDimitry Andric DwarfEHPrepareLegacyPass(CodeGenOpt::Level OptLevel = CodeGenOpt::Default) 325e8d8bef9SDimitry Andric : FunctionPass(ID), OptLevel(OptLevel) {} 326e8d8bef9SDimitry Andric 327e8d8bef9SDimitry Andric bool runOnFunction(Function &F) override { 3280b57cec5SDimitry Andric const TargetMachine &TM = 3290b57cec5SDimitry Andric getAnalysis<TargetPassConfig>().getTM<TargetMachine>(); 330e8d8bef9SDimitry Andric const TargetLowering &TLI = *TM.getSubtargetImpl(F)->getTargetLowering(); 331e8d8bef9SDimitry Andric DominatorTree *DT = nullptr; 332e8d8bef9SDimitry Andric const TargetTransformInfo *TTI = nullptr; 333fe6060f1SDimitry Andric if (auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>()) 334fe6060f1SDimitry Andric DT = &DTWP->getDomTree(); 335e8d8bef9SDimitry Andric if (OptLevel != CodeGenOpt::None) { 336fe6060f1SDimitry Andric if (!DT) 337e8d8bef9SDimitry Andric DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 338e8d8bef9SDimitry Andric TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F); 339e8d8bef9SDimitry Andric } 340*349cc55cSDimitry Andric return prepareDwarfEH(OptLevel, F, TLI, DT, TTI, TM.getTargetTriple()); 341e8d8bef9SDimitry Andric } 342e8d8bef9SDimitry Andric 343e8d8bef9SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override { 344e8d8bef9SDimitry Andric AU.addRequired<TargetPassConfig>(); 345e8d8bef9SDimitry Andric AU.addRequired<TargetTransformInfoWrapperPass>(); 346e8d8bef9SDimitry Andric if (OptLevel != CodeGenOpt::None) { 347e8d8bef9SDimitry Andric AU.addRequired<DominatorTreeWrapperPass>(); 348e8d8bef9SDimitry Andric AU.addRequired<TargetTransformInfoWrapperPass>(); 349e8d8bef9SDimitry Andric } 350fe6060f1SDimitry Andric AU.addPreserved<DominatorTreeWrapperPass>(); 351e8d8bef9SDimitry Andric } 352e8d8bef9SDimitry Andric 353e8d8bef9SDimitry Andric StringRef getPassName() const override { 354e8d8bef9SDimitry Andric return "Exception handling preparation"; 355e8d8bef9SDimitry Andric } 356e8d8bef9SDimitry Andric }; 357e8d8bef9SDimitry Andric 358e8d8bef9SDimitry Andric } // end anonymous namespace 359e8d8bef9SDimitry Andric 360e8d8bef9SDimitry Andric char DwarfEHPrepareLegacyPass::ID = 0; 361e8d8bef9SDimitry Andric 362e8d8bef9SDimitry Andric INITIALIZE_PASS_BEGIN(DwarfEHPrepareLegacyPass, DEBUG_TYPE, 363e8d8bef9SDimitry Andric "Prepare DWARF exceptions", false, false) 364e8d8bef9SDimitry Andric INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 365e8d8bef9SDimitry Andric INITIALIZE_PASS_DEPENDENCY(TargetPassConfig) 366e8d8bef9SDimitry Andric INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) 367e8d8bef9SDimitry Andric INITIALIZE_PASS_END(DwarfEHPrepareLegacyPass, DEBUG_TYPE, 368e8d8bef9SDimitry Andric "Prepare DWARF exceptions", false, false) 369e8d8bef9SDimitry Andric 370e8d8bef9SDimitry Andric FunctionPass *llvm::createDwarfEHPass(CodeGenOpt::Level OptLevel) { 371e8d8bef9SDimitry Andric return new DwarfEHPrepareLegacyPass(OptLevel); 3720b57cec5SDimitry Andric } 373