Author: Roman Lebedev Date: 2021-01-02T01:01:19+03:00 New Revision: e6b1a27fb9c71a9a81439917368a25ddc7d371a9
URL: https://github.com/llvm/llvm-project/commit/e6b1a27fb9c71a9a81439917368a25ddc7d371a9 DIFF: https://github.com/llvm/llvm-project/commit/e6b1a27fb9c71a9a81439917368a25ddc7d371a9.diff LOG: [NFC][CodeGen] Split DwarfEHPrepare pass into an actual transform and an legacy-PM wrapper This is consistent with the layout of other passes, and simplifies further refinements regarding DomTree handling. This is indended to be a NFC commit. Added: Modified: llvm/include/llvm/InitializePasses.h llvm/lib/CodeGen/CodeGen.cpp llvm/lib/CodeGen/DwarfEHPrepare.cpp llvm/tools/opt/opt.cpp Removed: ################################################################################ diff --git a/llvm/include/llvm/InitializePasses.h b/llvm/include/llvm/InitializePasses.h index f77de64e2c64..4f89179a03de 100644 --- a/llvm/include/llvm/InitializePasses.h +++ b/llvm/include/llvm/InitializePasses.h @@ -140,7 +140,7 @@ void initializeDomPrinterPass(PassRegistry&); void initializeDomViewerPass(PassRegistry&); void initializeDominanceFrontierWrapperPassPass(PassRegistry&); void initializeDominatorTreeWrapperPassPass(PassRegistry&); -void initializeDwarfEHPreparePass(PassRegistry&); +void initializeDwarfEHPrepareLegacyPassPass(PassRegistry &); void initializeEarlyCSELegacyPassPass(PassRegistry&); void initializeEarlyCSEMemSSALegacyPassPass(PassRegistry&); void initializeEarlyIfConverterPass(PassRegistry&); diff --git a/llvm/lib/CodeGen/CodeGen.cpp b/llvm/lib/CodeGen/CodeGen.cpp index 94925498b01c..d2400d0371e3 100644 --- a/llvm/lib/CodeGen/CodeGen.cpp +++ b/llvm/lib/CodeGen/CodeGen.cpp @@ -30,7 +30,7 @@ void llvm::initializeCodeGen(PassRegistry &Registry) { initializeDeadMachineInstructionElimPass(Registry); initializeDebugifyMachineModulePass(Registry); initializeDetectDeadLanesPass(Registry); - initializeDwarfEHPreparePass(Registry); + initializeDwarfEHPrepareLegacyPassPass(Registry); initializeEarlyIfConverterPass(Registry); initializeEarlyIfPredicatorPass(Registry); initializeEarlyMachineLICMPass(Registry); diff --git a/llvm/lib/CodeGen/DwarfEHPrepare.cpp b/llvm/lib/CodeGen/DwarfEHPrepare.cpp index c05364354155..5696b2e46d3b 100644 --- a/llvm/lib/CodeGen/DwarfEHPrepare.cpp +++ b/llvm/lib/CodeGen/DwarfEHPrepare.cpp @@ -44,66 +44,44 @@ STATISTIC(NumResumesLowered, "Number of resume calls lowered"); namespace { -class DwarfEHPrepare : public FunctionPass { +class DwarfEHPrepare { + CodeGenOpt::Level OptLevel; + // RewindFunction - _Unwind_Resume or the target equivalent. - FunctionCallee RewindFunction = nullptr; + FunctionCallee &RewindFunction; - CodeGenOpt::Level OptLevel; - DominatorTree *DT = nullptr; - const TargetLowering *TLI = nullptr; + Function &F; + const TargetLowering &TLI; + DominatorTree *DT; + const TargetTransformInfo *TTI; - bool InsertUnwindResumeCalls(Function &Fn); + /// Return the exception object from the value passed into + /// the 'resume' instruction (typically an aggregate). Clean up any dead + /// instructions, including the 'resume' instruction. Value *GetExceptionObject(ResumeInst *RI); + + /// Replace resumes that are not reachable from a cleanup landing pad with + /// unreachable and then simplify those blocks. size_t - pruneUnreachableResumes(Function &Fn, SmallVectorImpl<ResumeInst *> &Resumes, + pruneUnreachableResumes(SmallVectorImpl<ResumeInst *> &Resumes, SmallVectorImpl<LandingPadInst *> &CleanupLPads); -public: - static char ID; // Pass identification, replacement for typeid. - - DwarfEHPrepare(CodeGenOpt::Level OptLevel = CodeGenOpt::Default) - : FunctionPass(ID), OptLevel(OptLevel) {} - - bool runOnFunction(Function &Fn) override; - - bool doFinalization(Module &M) override { - RewindFunction = nullptr; - return false; - } + /// Convert the ResumeInsts that are still present + /// into calls to the appropriate _Unwind_Resume function. + bool InsertUnwindResumeCalls(); - void getAnalysisUsage(AnalysisUsage &AU) const override; +public: + DwarfEHPrepare(CodeGenOpt::Level OptLevel_, FunctionCallee &RewindFunction_, + Function &F_, const TargetLowering &TLI_, DominatorTree *DT_, + const TargetTransformInfo *TTI_) + : OptLevel(OptLevel_), RewindFunction(RewindFunction_), F(F_), TLI(TLI_), + DT(DT_), TTI(TTI_) {} - StringRef getPassName() const override { - return "Exception handling preparation"; - } + bool run(); }; -} // end anonymous namespace - -char DwarfEHPrepare::ID = 0; - -INITIALIZE_PASS_BEGIN(DwarfEHPrepare, DEBUG_TYPE, "Prepare DWARF exceptions", - false, false) -INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) -INITIALIZE_PASS_DEPENDENCY(TargetPassConfig) -INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) -INITIALIZE_PASS_END(DwarfEHPrepare, DEBUG_TYPE, "Prepare DWARF exceptions", - false, false) +} // namespace -FunctionPass *llvm::createDwarfEHPass(CodeGenOpt::Level OptLevel) { - return new DwarfEHPrepare(OptLevel); -} - -void DwarfEHPrepare::getAnalysisUsage(AnalysisUsage &AU) const { - AU.addRequired<TargetPassConfig>(); - AU.addRequired<TargetTransformInfoWrapperPass>(); - if (OptLevel != CodeGenOpt::None) - AU.addRequired<DominatorTreeWrapperPass>(); -} - -/// GetExceptionObject - Return the exception object from the value passed into -/// the 'resume' instruction (typically an aggregate). Clean up any dead -/// instructions, including the 'resume' instruction. Value *DwarfEHPrepare::GetExceptionObject(ResumeInst *RI) { Value *V = RI->getOperand(0); Value *ExnObj = nullptr; @@ -141,10 +119,8 @@ Value *DwarfEHPrepare::GetExceptionObject(ResumeInst *RI) { return ExnObj; } -/// Replace resumes that are not reachable from a cleanup landing pad with -/// unreachable and then simplify those blocks. size_t DwarfEHPrepare::pruneUnreachableResumes( - Function &Fn, SmallVectorImpl<ResumeInst *> &Resumes, + SmallVectorImpl<ResumeInst *> &Resumes, SmallVectorImpl<LandingPadInst *> &CleanupLPads) { BitVector ResumeReachable(Resumes.size()); size_t ResumeIndex = 0; @@ -162,9 +138,7 @@ size_t DwarfEHPrepare::pruneUnreachableResumes( if (ResumeReachable.all()) return Resumes.size(); - const TargetTransformInfo &TTI = - getAnalysis<TargetTransformInfoWrapperPass>().getTTI(Fn); - LLVMContext &Ctx = Fn.getContext(); + LLVMContext &Ctx = F.getContext(); // Otherwise, insert unreachable instructions and call simplifycfg. size_t ResumesLeft = 0; @@ -176,19 +150,17 @@ size_t DwarfEHPrepare::pruneUnreachableResumes( BasicBlock *BB = RI->getParent(); new UnreachableInst(Ctx, RI); RI->eraseFromParent(); - simplifyCFG(BB, TTI); + simplifyCFG(BB, *TTI); } } Resumes.resize(ResumesLeft); return ResumesLeft; } -/// InsertUnwindResumeCalls - Convert the ResumeInsts that are still present -/// into calls to the appropriate _Unwind_Resume function. -bool DwarfEHPrepare::InsertUnwindResumeCalls(Function &Fn) { +bool DwarfEHPrepare::InsertUnwindResumeCalls() { SmallVector<ResumeInst *, 16> Resumes; SmallVector<LandingPadInst *, 16> CleanupLPads; - for (BasicBlock &BB : Fn) { + for (BasicBlock &BB : F) { if (auto *RI = dyn_cast<ResumeInst>(BB.getTerminator())) Resumes.push_back(RI); if (auto *LP = BB.getLandingPadInst()) @@ -200,15 +172,15 @@ bool DwarfEHPrepare::InsertUnwindResumeCalls(Function &Fn) { return false; // Check the personality, don't do anything if it's scope-based. - EHPersonality Pers = classifyEHPersonality(Fn.getPersonalityFn()); + EHPersonality Pers = classifyEHPersonality(F.getPersonalityFn()); if (isScopedEHPersonality(Pers)) return false; - LLVMContext &Ctx = Fn.getContext(); + LLVMContext &Ctx = F.getContext(); size_t ResumesLeft = Resumes.size(); if (OptLevel != CodeGenOpt::None) - ResumesLeft = pruneUnreachableResumes(Fn, Resumes, CleanupLPads); + ResumesLeft = pruneUnreachableResumes(Resumes, CleanupLPads); if (ResumesLeft == 0) return true; // We pruned them all. @@ -217,8 +189,8 @@ bool DwarfEHPrepare::InsertUnwindResumeCalls(Function &Fn) { if (!RewindFunction) { FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx), Type::getInt8PtrTy(Ctx), false); - const char *RewindName = TLI->getLibcallName(RTLIB::UNWIND_RESUME); - RewindFunction = Fn.getParent()->getOrInsertFunction(RewindName, FTy); + const char *RewindName = TLI.getLibcallName(RTLIB::UNWIND_RESUME); + RewindFunction = F.getParent()->getOrInsertFunction(RewindName, FTy); } // Create the basic block where the _Unwind_Resume call will live. @@ -231,7 +203,7 @@ bool DwarfEHPrepare::InsertUnwindResumeCalls(Function &Fn) { // Call the _Unwind_Resume function. CallInst *CI = CallInst::Create(RewindFunction, ExnObj, "", UnwindBB); - CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME)); + CI->setCallingConv(TLI.getLibcallCallingConv(RTLIB::UNWIND_RESUME)); // We never expect _Unwind_Resume to return. CI->setDoesNotReturn(); @@ -239,7 +211,7 @@ bool DwarfEHPrepare::InsertUnwindResumeCalls(Function &Fn) { return true; } - BasicBlock *UnwindBB = BasicBlock::Create(Ctx, "unwind_resume", &Fn); + BasicBlock *UnwindBB = BasicBlock::Create(Ctx, "unwind_resume", &F); PHINode *PN = PHINode::Create(Type::getInt8PtrTy(Ctx), ResumesLeft, "exn.obj", UnwindBB); @@ -257,7 +229,7 @@ bool DwarfEHPrepare::InsertUnwindResumeCalls(Function &Fn) { // Call the function. CallInst *CI = CallInst::Create(RewindFunction, PN, "", UnwindBB); - CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME)); + CI->setCallingConv(TLI.getLibcallCallingConv(RTLIB::UNWIND_RESUME)); // We never expect _Unwind_Resume to return. CI->setDoesNotReturn(); @@ -265,15 +237,61 @@ bool DwarfEHPrepare::InsertUnwindResumeCalls(Function &Fn) { return true; } -bool DwarfEHPrepare::runOnFunction(Function &Fn) { - const TargetMachine &TM = - getAnalysis<TargetPassConfig>().getTM<TargetMachine>(); - DT = OptLevel != CodeGenOpt::None - ? &getAnalysis<DominatorTreeWrapperPass>().getDomTree() - : nullptr; - TLI = TM.getSubtargetImpl(Fn)->getTargetLowering(); - bool Changed = InsertUnwindResumeCalls(Fn); - DT = nullptr; - TLI = nullptr; - return Changed; +bool DwarfEHPrepare::run() { return InsertUnwindResumeCalls(); } + +namespace { + +class DwarfEHPrepareLegacyPass : public FunctionPass { + // RewindFunction - _Unwind_Resume or the target equivalent. + FunctionCallee RewindFunction = nullptr; + + CodeGenOpt::Level OptLevel; + +public: + static char ID; // Pass identification, replacement for typeid. + + DwarfEHPrepareLegacyPass(CodeGenOpt::Level OptLevel = CodeGenOpt::Default) + : FunctionPass(ID), OptLevel(OptLevel) {} + + bool runOnFunction(Function &F) override { + const TargetMachine &TM = + getAnalysis<TargetPassConfig>().getTM<TargetMachine>(); + const TargetLowering &TLI = *TM.getSubtargetImpl(F)->getTargetLowering(); + DominatorTree *DT = nullptr; + const TargetTransformInfo *TTI = nullptr; + if (OptLevel != CodeGenOpt::None) { + DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); + TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F); + } + return DwarfEHPrepare(OptLevel, RewindFunction, F, TLI, DT, TTI).run(); + } + + void getAnalysisUsage(AnalysisUsage &AU) const override { + AU.addRequired<TargetPassConfig>(); + AU.addRequired<TargetTransformInfoWrapperPass>(); + if (OptLevel != CodeGenOpt::None) { + AU.addRequired<DominatorTreeWrapperPass>(); + AU.addRequired<TargetTransformInfoWrapperPass>(); + } + } + + StringRef getPassName() const override { + return "Exception handling preparation"; + } +}; + +} // end anonymous namespace + +char DwarfEHPrepareLegacyPass::ID = 0; + +INITIALIZE_PASS_BEGIN(DwarfEHPrepareLegacyPass, DEBUG_TYPE, + "Prepare DWARF exceptions", false, false) +INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) +INITIALIZE_PASS_DEPENDENCY(TargetPassConfig) +INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) +INITIALIZE_PASS_END(DwarfEHPrepareLegacyPass, DEBUG_TYPE, + "Prepare DWARF exceptions", false, false) + +FunctionPass *llvm::createDwarfEHPass(CodeGenOpt::Level OptLevel) { + return new DwarfEHPrepareLegacyPass(OptLevel); } diff --git a/llvm/tools/opt/opt.cpp b/llvm/tools/opt/opt.cpp index 094d79d742dd..94f4eb03e636 100644 --- a/llvm/tools/opt/opt.cpp +++ b/llvm/tools/opt/opt.cpp @@ -554,7 +554,7 @@ int main(int argc, char **argv) { initializeAtomicExpandPass(Registry); initializeRewriteSymbolsLegacyPassPass(Registry); initializeWinEHPreparePass(Registry); - initializeDwarfEHPreparePass(Registry); + initializeDwarfEHPrepareLegacyPassPass(Registry); initializeSafeStackLegacyPassPass(Registry); initializeSjLjEHPreparePass(Registry); initializePreISelIntrinsicLoweringLegacyPassPass(Registry); _______________________________________________ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits