From d4b3f19ba6617c7aef8f538fa657bb6de7fc31f6 Mon Sep 17 00:00:00 2001 From: Alina Sbirlea Date: Thu, 16 Aug 2018 21:54:33 +0000 Subject: [PATCH] [DomTree] Add constructor to create a new DT based on current DT/CFG and a set of Updates. Summary: Add the posibility of creating a new DT using a set of Updates. This will essentially create a DT based on a CFG snapshot/view. Additional refactoring for either this patch or follow-ups: - create an utility for building BUI. - replace BUI with a GraphDiff. Reviewers: kuhar Subscribers: sanjoy, jlebar, llvm-commits Differential Revision: https://reviews.llvm.org/D50671 llvm-svn: 339947 --- llvm/include/llvm/IR/Dominators.h | 6 ++++++ llvm/include/llvm/Support/GenericDomTree.h | 9 +++++++++ .../llvm/Support/GenericDomTreeConstruction.h | 19 +++++++++++++++++++ llvm/lib/IR/Dominators.cpp | 5 +++++ 4 files changed, 39 insertions(+) diff --git a/llvm/include/llvm/IR/Dominators.h b/llvm/include/llvm/IR/Dominators.h index 47032c004812..f7da47d07663 100644 --- a/llvm/include/llvm/IR/Dominators.h +++ b/llvm/include/llvm/IR/Dominators.h @@ -46,6 +46,9 @@ using BBPostDomTree = PostDomTreeBase; using BBUpdates = ArrayRef>; extern template void Calculate(BBDomTree &DT); +extern template void CalculateWithUpdates(BBDomTree &DT, + BBUpdates U); + extern template void Calculate(BBPostDomTree &DT); extern template void InsertEdge(BBDomTree &DT, BasicBlock *From, @@ -145,6 +148,9 @@ class DominatorTree : public DominatorTreeBase { DominatorTree() = default; explicit DominatorTree(Function &F) { recalculate(F); } + explicit DominatorTree(DominatorTree &DT, DomTreeBuilder::BBUpdates U) { + recalculate(*DT.Parent, U); + } /// Handle invalidation explicitly. bool invalidate(Function &F, const PreservedAnalyses &PA, diff --git a/llvm/include/llvm/Support/GenericDomTree.h b/llvm/include/llvm/Support/GenericDomTree.h index 3c3ec0921011..b3018bac310a 100644 --- a/llvm/include/llvm/Support/GenericDomTree.h +++ b/llvm/include/llvm/Support/GenericDomTree.h @@ -192,6 +192,10 @@ namespace DomTreeBuilder { template void Calculate(DomTreeT &DT); +template +void CalculateWithUpdates(DomTreeT &DT, + ArrayRef Updates); + template void InsertEdge(DomTreeT &DT, typename DomTreeT::NodePtr From, typename DomTreeT::NodePtr To); @@ -730,6 +734,11 @@ public: DomTreeBuilder::Calculate(*this); } + void recalculate(ParentType &Func, ArrayRef Updates) { + Parent = &Func; + DomTreeBuilder::CalculateWithUpdates(*this, Updates); + } + /// verify - checks if the tree is correct. There are 3 level of verification: /// - Full -- verifies if the tree is correct by making sure all the /// properties (including the parent and the sibling property) diff --git a/llvm/include/llvm/Support/GenericDomTreeConstruction.h b/llvm/include/llvm/Support/GenericDomTreeConstruction.h index 80d3b276c6db..5eb09430de04 100644 --- a/llvm/include/llvm/Support/GenericDomTreeConstruction.h +++ b/llvm/include/llvm/Support/GenericDomTreeConstruction.h @@ -1576,6 +1576,25 @@ void Calculate(DomTreeT &DT) { SemiNCAInfo::CalculateFromScratch(DT, nullptr); } +template +void CalculateWithUpdates(DomTreeT &DT, + ArrayRef Updates) { + // TODO: Move BUI creation in common method, reuse in ApplyUpdates. + typename SemiNCAInfo::BatchUpdateInfo BUI; + LLVM_DEBUG(dbgs() << "Legalizing " << BUI.Updates.size() << " updates\n"); + cfg::LegalizeUpdates(Updates, BUI.Updates, + DomTreeT::IsPostDominator); + const size_t NumLegalized = BUI.Updates.size(); + BUI.FutureSuccessors.reserve(NumLegalized); + BUI.FuturePredecessors.reserve(NumLegalized); + for (auto &U : BUI.Updates) { + BUI.FutureSuccessors[U.getFrom()].push_back({U.getTo(), U.getKind()}); + BUI.FuturePredecessors[U.getTo()].push_back({U.getFrom(), U.getKind()}); + } + + SemiNCAInfo::CalculateFromScratch(DT, &BUI); +} + template void InsertEdge(DomTreeT &DT, typename DomTreeT::NodePtr From, typename DomTreeT::NodePtr To) { diff --git a/llvm/lib/IR/Dominators.cpp b/llvm/lib/IR/Dominators.cpp index dc4fa9eee66a..c78f220439af 100644 --- a/llvm/lib/IR/Dominators.cpp +++ b/llvm/lib/IR/Dominators.cpp @@ -71,8 +71,13 @@ template class llvm::cfg::Update; template void llvm::DomTreeBuilder::Calculate( DomTreeBuilder::BBDomTree &DT); +template void +llvm::DomTreeBuilder::CalculateWithUpdates( + DomTreeBuilder::BBDomTree &DT, BBUpdates U); + template void llvm::DomTreeBuilder::Calculate( DomTreeBuilder::BBPostDomTree &DT); +// No CalculateWithUpdates instantiation, unless a usecase arises. template void llvm::DomTreeBuilder::InsertEdge( DomTreeBuilder::BBDomTree &DT, BasicBlock *From, BasicBlock *To);