Index: include/llvm/CodeGen/Passes.h =================================================================== --- include/llvm/CodeGen/Passes.h +++ include/llvm/CodeGen/Passes.h @@ -340,7 +340,7 @@ /// createDwarfEHPass - This pass mulches exception handling code into a form /// adapted to code generation. Required if using dwarf exception handling. - FunctionPass *createDwarfEHPass(); + FunctionPass *createDwarfEHPass(bool PruneUnreachableResumes); /// createWinEHPass - Prepares personality functions used by MSVC on Windows, /// in addition to the Itanium LSDA based personalities. Index: lib/CodeGen/DwarfEHPrepare.cpp =================================================================== --- lib/CodeGen/DwarfEHPrepare.cpp +++ lib/CodeGen/DwarfEHPrepare.cpp @@ -48,6 +48,7 @@ // RewindFunction - _Unwind_Resume or the target equivalent. FunctionCallee RewindFunction = nullptr; + bool PruneUnreachableResumes; DominatorTree *DT = nullptr; const TargetLowering *TLI = nullptr; @@ -61,7 +62,8 @@ public: static char ID; // Pass identification, replacement for typeid. - DwarfEHPrepare() : FunctionPass(ID) {} + DwarfEHPrepare(bool PruneUnreachableResumes = true) + : FunctionPass(ID), PruneUnreachableResumes(PruneUnreachableResumes) {} bool runOnFunction(Function &Fn) override; @@ -89,12 +91,15 @@ INITIALIZE_PASS_END(DwarfEHPrepare, DEBUG_TYPE, "Prepare DWARF exceptions", false, false) -FunctionPass *llvm::createDwarfEHPass() { return new DwarfEHPrepare(); } +FunctionPass *llvm::createDwarfEHPass(bool PruneUnreachableResumes) { + return new DwarfEHPrepare(PruneUnreachableResumes); +} void DwarfEHPrepare::getAnalysisUsage(AnalysisUsage &AU) const { AU.addRequired(); AU.addRequired(); - AU.addRequired(); + if (PruneUnreachableResumes) + AU.addRequired(); } /// GetExceptionObject - Return the exception object from the value passed into @@ -142,6 +147,9 @@ size_t DwarfEHPrepare::pruneUnreachableResumes( Function &Fn, SmallVectorImpl &Resumes, SmallVectorImpl &CleanupLPads) { + if (!PruneUnreachableResumes) + return Resumes.size(); + BitVector ResumeReachable(Resumes.size()); size_t ResumeIndex = 0; for (auto *RI : Resumes) { @@ -259,7 +267,8 @@ bool DwarfEHPrepare::runOnFunction(Function &Fn) { const TargetMachine &TM = getAnalysis().getTM(); - DT = &getAnalysis().getDomTree(); + auto *DTWP = getAnalysisIfAvailable(); + DT = DTWP ? &DTWP->getDomTree() : nullptr; TLI = TM.getSubtargetImpl(Fn)->getTargetLowering(); bool Changed = InsertUnwindResumeCalls(Fn); DT = nullptr; Index: lib/CodeGen/TargetPassConfig.cpp =================================================================== --- lib/CodeGen/TargetPassConfig.cpp +++ lib/CodeGen/TargetPassConfig.cpp @@ -729,14 +729,16 @@ LLVM_FALLTHROUGH; case ExceptionHandling::DwarfCFI: case ExceptionHandling::ARM: - addPass(createDwarfEHPass()); + addPass(createDwarfEHPass( + /* PruneUnreachableResumes */ getOptLevel() != CodeGenOpt::None)); break; case ExceptionHandling::WinEH: // We support using both GCC-style and MSVC-style exceptions on Windows, so // add both preparation passes. Each pass will only actually run if it // recognizes the personality function. addPass(createWinEHPass()); - addPass(createDwarfEHPass()); + addPass(createDwarfEHPass( + /* PruneUnreachableResumes */ getOptLevel() != CodeGenOpt::None)); break; case ExceptionHandling::Wasm: // Wasm EH uses Windows EH instructions, but it does not need to demote PHIs Index: test/CodeGen/AArch64/O0-pipeline.ll =================================================================== --- test/CodeGen/AArch64/O0-pipeline.ll +++ test/CodeGen/AArch64/O0-pipeline.ll @@ -27,7 +27,6 @@ ; CHECK-NEXT: AArch64 Stack Tagging ; CHECK-NEXT: Rewrite Symbols ; CHECK-NEXT: FunctionPass Manager -; CHECK-NEXT: Dominator Tree Construction ; CHECK-NEXT: Exception handling preparation ; CHECK-NEXT: Safe Stack instrumentation pass ; CHECK-NEXT: Insert stack protectors Index: test/CodeGen/X86/O0-pipeline.ll =================================================================== --- test/CodeGen/X86/O0-pipeline.ll +++ test/CodeGen/X86/O0-pipeline.ll @@ -29,7 +29,6 @@ ; CHECK-NEXT: Expand indirectbr instructions ; CHECK-NEXT: Rewrite Symbols ; CHECK-NEXT: FunctionPass Manager -; CHECK-NEXT: Dominator Tree Construction ; CHECK-NEXT: Exception handling preparation ; CHECK-NEXT: Safe Stack instrumentation pass ; CHECK-NEXT: Insert stack protectors