Index: include/llvm/Analysis/PostDominators.h =================================================================== --- include/llvm/Analysis/PostDominators.h +++ include/llvm/Analysis/PostDominators.h @@ -32,6 +32,12 @@ /// Handle invalidation explicitly. bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &); + + /// \brief Verify the correctness of the domtree by re-computing it. + /// + /// This should only be used for debugging as it aborts the program if the + /// verification fails. + void verifyDomTree() const; }; /// \brief Analysis pass which computes a \c PostDominatorTree. @@ -75,6 +81,8 @@ bool runOnFunction(Function &F) override; + void verifyAnalysis() const override; + void getAnalysisUsage(AnalysisUsage &AU) const override { AU.setPreservesAll(); } Index: lib/Analysis/PostDominators.cpp =================================================================== --- lib/Analysis/PostDominators.cpp +++ lib/Analysis/PostDominators.cpp @@ -39,11 +39,42 @@ PAC.preservedSet()); } +void PostDominatorTree::verifyDomTree() const { + // Perform the expensive checks only when VerifyDomInfo is set. + if (VerifyDomInfo && !verify()) { + errs() << "\n~~~~~~~~~~~\n\t\tDomTree verification failed!\n~~~~~~~~~~~\n"; + print(errs()); + abort(); + } + + assert(getRoots().size() >= 1); + Function &F = *getRoots()[0]->getParent(); + + PostDominatorTree OtherDT; + OtherDT.recalculate(F); + if (compare(OtherDT)) { + errs() << "PostDominatorTree for function " << F.getName() + << " is not up to date!\nExisting:\n"; + print(errs()); + errs() << "\nRecalculated:\n"; + OtherDT.print(errs()); + errs() << "\nCFG:\n"; + F.print(errs()); + errs().flush(); + abort(); + } +} + bool PostDominatorTreeWrapperPass::runOnFunction(Function &F) { DT.recalculate(F); return false; } +void PostDominatorTreeWrapperPass::verifyAnalysis() const { + if (VerifyDomInfo) + DT.verifyDomTree(); +} + void PostDominatorTreeWrapperPass::print(raw_ostream &OS, const Module *) const { DT.print(OS); } Index: lib/IR/Dominators.cpp =================================================================== --- lib/IR/Dominators.cpp +++ lib/IR/Dominators.cpp @@ -381,8 +381,8 @@ } void DominatorTreeWrapperPass::verifyAnalysis() const { - if (VerifyDomInfo) - DT.verifyDomTree(); + if (VerifyDomInfo) + DT.verifyDomTree(); } void DominatorTreeWrapperPass::print(raw_ostream &OS, const Module *) const {