33 #ifndef LLVM_SUPPORT_GENERICDOMTREECONSTRUCTION_H
34 #define LLVM_SUPPORT_GENERICDOMTREECONSTRUCTION_H
43 #define DEBUG_TYPE "dom-tree-builder"
46 namespace DomTreeBuilder {
48 template <
typename NodePtr,
bool Inverse>
50 static auto Get(NodePtr
N) -> decltype(
reverse(children<NodePtr>(
N))) {
51 return reverse(children<NodePtr>(
N));
55 template <
typename NodePtr>
57 static auto Get(NodePtr
N) -> decltype(inverse_children<NodePtr>(
N)) {
58 return inverse_children<NodePtr>(
N);
62 template <
typename DomTreeT>
64 using NodePtr =
typename DomTreeT::NodePtr;
67 static constexpr
bool IsPostDom = DomTreeT::IsPostDominator;
91 if (InfoIt ==
NodeToInfo.end())
return nullptr;
93 return InfoIt->second.IDom;
103 assert(IDom || DT.DomTreeNodes[
nullptr]);
108 return (DT.DomTreeNodes[BB] = IDomNode->
addChild(
125 BP.
N->printAsOperand(O,
false);
134 template <
bool Inverse,
typename DescendCondition>
136 unsigned AttachToNum) {
141 while (!WorkList.
empty()) {
146 if (BBInfo.DFSNum != 0)
continue;
147 BBInfo.DFSNum = BBInfo.Semi = ++LastNum;
155 if (SIT !=
NodeToInfo.end() && SIT->second.DFSNum != 0) {
156 if (Succ != BB) SIT->second.ReverseChildren.push_back(BB);
160 if (!Condition(BB, Succ))
continue;
166 SuccInfo.Parent = LastNum;
167 SuccInfo.ReverseChildren.push_back(BB);
176 if (VInInfo.DFSNum < LastLinked)
182 if (VInInfo.Parent >= LastLinked)
185 while (!Work.
empty()) {
191 if (Visited.
insert(VAncestor).second && VInfo.Parent >= LastLinked) {
198 if (VInfo.Parent < LastLinked)
202 NodePtr VAncestorLabel = VAInfo.Label;
205 VInfo.Label = VAncestorLabel;
206 VInfo.Parent = VAInfo.Parent;
209 return VInInfo.Label;
214 const unsigned NextDFSNum(
NumToNode.size());
216 for (
unsigned i = 1; i < NextDFSNum; ++i) {
223 for (
unsigned i = NextDFSNum - 1; i >= 2; --i) {
228 WInfo.Semi = WInfo.Parent;
229 for (
const auto &
N : WInfo.ReverseChildren) {
235 if (TN && TN->
getLevel() < MinLevel)
239 if (SemiU < WInfo.Semi) WInfo.Semi = SemiU;
247 for (
unsigned i = 2; i < NextDFSNum; ++i) {
251 NodePtr WIDomCandidate = WInfo.IDom;
252 while (
NodeToInfo[WIDomCandidate].DFSNum > SDomNum)
253 WIDomCandidate =
NodeToInfo[WIDomCandidate].IDom;
255 WInfo.IDom = WIDomCandidate;
259 template <
typename DescendCondition>
263 if (DT.Roots.size() > 1) {
265 BBInfo.DFSNum = BBInfo.Semi = ++Num;
266 BBInfo.Label =
nullptr;
271 if (DT.isPostDominator()) {
272 for (
auto *Root : DT.Roots) Num = runDFS<true>(Root, Num,
DC, 1);
274 assert(DT.Roots.size() == 1);
275 Num = runDFS<false>(DT.Roots[0], Num,
DC, Num);
288 if (DT.Roots.empty())
return;
296 const bool MultipleRoots = DT.Roots.size() > 1 || (DT.isPostDominator() &&
297 LastDFSNum != NumBlocks);
298 NodePtr Root = !MultipleRoots ? DT.Roots[0] :
nullptr;
300 DT.RootNode = (DT.DomTreeNodes[Root] =
301 llvm::make_unique<DomTreeNodeBase<NodeT>>(Root,
nullptr))
310 for (
size_t i = 1, e =
NumToNode.size(); i != e; ++i) {
312 DEBUG(
dbgs() <<
"\tdiscovered a new reachable node "
316 if (DT.DomTreeNodes[W])
continue;
325 DT.DomTreeNodes[W] = IDomNode->
addChild(
332 for (
size_t i = 1, e =
NumToNode.size(); i != e; ++i) {
347 return First.first > Second.first;
351 std::priority_queue<BucketElementTy, SmallVector<BucketElementTy, 8>,
361 assert(From && To &&
"Cannot connect nullptrs");
369 DT.DFSInfoValid =
false;
385 assert(NCDBlock || DT.isPostDominator());
394 if (NCD == To || NCD == ToIDom)
return;
400 const unsigned ToLevel = To->
getLevel();
402 II.
Bucket.push({ToLevel, To});
404 while (!II.
Bucket.empty()) {
407 DEBUG(
dbgs() <<
"\tAdding to Visited and AffectedQueue: "
409 II.
Visited.insert(CurrentNode);
424 const unsigned NCDLevel = NCD->
getLevel();
431 assert(SuccTN &&
"Unreachable successor found at reachable insertion");
432 const unsigned SuccLevel = SuccTN->
getLevel();
435 <<
", level = " << SuccLevel <<
"\n");
439 if (SuccLevel > RootLevel) {
440 DEBUG(
dbgs() <<
"\t\tDominated by subtree From\n");
441 if (II.
Visited.count(SuccTN) != 0)
continue;
443 DEBUG(
dbgs() <<
"\t\tMarking visited not affected "
448 }
else if ((SuccLevel > NCDLevel + 1) && II.
Affected.count(SuccTN) == 0) {
449 DEBUG(
dbgs() <<
"\t\tMarking affected and adding "
452 II.
Bucket.push({SuccLevel, SuccTN});
472 DEBUG(
dbgs() <<
"Updating levels for visited but not affected nodes\n");
500 for (
const auto &Edge : DiscoveredEdgesToReachable) {
501 DEBUG(
dbgs() <<
"\tInserting discovered connecting edge "
512 &DiscoveredConnectingEdges) {
513 assert(!DT.getNode(Root) &&
"Root must not be reachable");
516 auto UnreachableDescender = [&DT, &DiscoveredConnectingEdges](
NodePtr From,
519 if (!ToTN)
return true;
521 DiscoveredConnectingEdges.push_back({From, ToTN});
526 SNCA.runDFS<
IsPostDom>(Root, 0, UnreachableDescender, 0);
528 SNCA.attachNewSubtree(DT, Incoming);
530 DEBUG(
dbgs() <<
"After adding unreachable nodes\n");
539 for (
auto &NodeToTN : DT.DomTreeNodes) {
544 if (DT.isVirtualRoot(TN))
continue;
548 <<
" not found by DFS walk!\n";
556 if (
N && !DT.getNode(
N)) {
558 <<
" not found in the DomTree!\n";
569 assert(From && To &&
"Cannot disconnect nullptrs");
577 auto IsSuccessor = [](
const NodePtr SuccCandidate,
const NodePtr Of) {
579 return llvm::find(Successors, SuccCandidate) != Successors.end();
582 assert(!IsSuccessor(To, From) &&
"Deleted edge still exists in the CFG!");
590 assert(ToTN &&
"To already unreachable -- there is no edge to delete");
591 const NodePtr NCDBlock = DT.findNearestCommonDominator(From, To);
595 if (ToTN == NCD)
return;
614 DEBUG(
dbgs() <<
"\tRebuilding subtree\n");
620 assert(ToIDom || DT.isPostDominator());
626 if (!PrevIDomSubTree) {
627 DEBUG(
dbgs() <<
"The entire tree needs to be rebuilt\n");
628 DT.recalculate(*DT.Parent);
633 const unsigned Level = ToIDomTN->getLevel();
635 return DT.getNode(To)->getLevel() >
Level;
641 SNCA.runDFS<
IsPostDom>(ToIDom, 0, DescendBelow, 0);
643 SNCA.runSemiNCA(DT, Level);
644 SNCA.reattachExistingSubtree(DT, PrevIDomSubTree);
654 if (!DT.getNode(Pred))
continue;
657 DT.findNearestCommonDominator(TN->
getBlock(), Pred);
661 <<
" is reachable from support "
683 auto DescendAndCollect = [
Level, &AffectedQueue, &DT](
NodePtr, NodePtr To) {
687 if (
llvm::find(AffectedQueue, To) == AffectedQueue.end())
688 AffectedQueue.push_back(To);
694 unsigned LastDFSNum =
701 for (
const NodePtr
N : AffectedQueue) {
703 const NodePtr NCDBlock =
705 assert(NCDBlock || DT.isPostDominator());
712 if (NCD != TN && NCD->
getLevel() < MinNode->getLevel()) MinNode = NCD;
716 if (!MinNode->getIDom()) {
717 DEBUG(
dbgs() <<
"The entire tree needs to be rebuilt\n");
718 DT.recalculate(*DT.Parent);
724 for (
unsigned i = LastDFSNum; i > 0; --i) {
725 const NodePtr
N = SNCA.NumToNode[i];
733 if (MinNode == ToTN)
return;
735 DEBUG(
dbgs() <<
"DeleteUnreachable: running DFS with MinNode = "
737 const unsigned MinLevel = MinNode->getLevel();
743 auto DescendBelow = [MinLevel, &DT](
NodePtr, NodePtr To) {
745 return ToTN && ToTN->
getLevel() > MinLevel;
747 SNCA.runDFS<
IsPostDom>(MinNode->getBlock(), 0, DescendBelow, 0);
750 <<
"\nRunning Semi-NCA\n");
753 SNCA.runSemiNCA(DT, MinLevel);
754 SNCA.reattachExistingSubtree(DT, PrevIDom);
766 assert(ChIt != IDom->Children.end());
768 IDom->Children.pop_back();
770 DT.DomTreeNodes.erase(TN->
getBlock());
780 for (
auto &NodeToTN : DT.DomTreeNodes) {
788 <<
" has a nonzero level " << TN->
getLevel() <<
"!\n";
796 << TN->
getLevel() <<
" while its IDom "
815 auto &Info = BlockToInfo.second;
820 const NodePtr To = BlockToInfo.first;
824 const NodePtr NCD = DT.findNearestCommonDominator(From, To);
827 if (NCDTN != ToTN && NCDTN != ToIDom) {
828 errs() <<
"NearestCommonDominator verification failed:\n\tNCD(From:"
886 for (
auto &NodeToTN : DT.DomTreeNodes) {
893 return From != BB && To != BB;
897 if (
NodeToInfo.count(Child->getBlock()) != 0) {
916 for (
auto &NodeToTN : DT.DomTreeNodes) {
926 return From != BBN && To != BBN;
930 if (S ==
N)
continue;
949 template <
class DomTreeT,
class FuncT>
955 template <
class DomTreeT>
956 void InsertEdge(DomTreeT &DT,
typename DomTreeT::NodePtr From,
957 typename DomTreeT::NodePtr To) {
958 if (DT.isPostDominator())
std::swap(From, To);
962 template <
class DomTreeT>
963 void DeleteEdge(DomTreeT &DT,
typename DomTreeT::NodePtr From,
964 typename DomTreeT::NodePtr To) {
965 if (DT.isPostDominator())
std::swap(From, To);
969 template <
class DomTreeT>
972 return SNCA.verifyReachability(DT) && SNCA.VerifyLevels(DT) &&
973 SNCA.verifyNCD(DT) && SNCA.verifyParentProperty(DT) &&
974 SNCA.verifySiblingProperty(DT);
SmallDenseSet< TreeNodePtr, 8 > Affected
static void ComputeUnreachableDominators(DomTreeT &DT, const NodePtr Root, const TreeNodePtr Incoming, SmallVectorImpl< std::pair< NodePtr, TreeNodePtr >> &DiscoveredConnectingEdges)
void push_back(const T &Elt)
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
DenseMap< NodePtr, InfoRec > NodeToInfo
static void EraseNode(DomTreeT &DT, const TreeNodePtr TN)
void Calculate(DomTreeT &DT, FuncT &F)
NodePtr getIDom(NodePtr BB) const
bool verifyReachability(const DomTreeT &DT)
unsigned getLevel() const
bool verifyNCD(const DomTreeT &DT)
static auto Get(NodePtr N) -> decltype(inverse_children< NodePtr >(N))
std::pair< unsigned, TreeNodePtr > BucketElementTy
std::priority_queue< BucketElementTy, SmallVector< BucketElementTy, 8 >, DecreasingLevel > Bucket
static bool VerifyLevels(const DomTreeT &DT)
NodeType
ISD::NodeType enum - This enum defines the target-independent operators for a SelectionDAG.
static void VisitInsertion(DomTreeT &DT, const TreeNodePtr TN, const unsigned RootLevel, const TreeNodePtr NCD, InsertionInfo &II)
const std::vector< DomTreeNodeBase * > & getChildren() const
NodePtr eval(NodePtr VIn, unsigned LastLinked)
void runSemiNCA(DomTreeT &DT, const unsigned MinLevel=0)
void InsertEdge(DomTreeT &DT, typename DomTreeT::NodePtr From, typename DomTreeT::NodePtr To)
static ManagedStatic< DebugCounter > DC
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
SmallDenseSet< TreeNodePtr, 8 > Visited
static auto Get(NodePtr N) -> decltype(reverse(children< NodePtr >(N)))
bool operator()(const BucketElementTy &First, const BucketElementTy &Second) const
static void DeleteReachable(DomTreeT &DT, const TreeNodePtr FromTN, const TreeNodePtr ToTN)
static void InsertEdge(DomTreeT &DT, const NodePtr From, const NodePtr To)
LLVM_NODISCARD bool empty() const
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
static bool HasProperSupport(DomTreeT &DT, const TreeNodePtr TN)
DomTreeNodeBase * getIDom() const
Base class for the actual dominator tree node.
bool verifySiblingProperty(const DomTreeT &DT)
typename DomTreeT::NodePtr NodePtr
unsigned runDFS(NodePtr V, unsigned LastNum, DescendCondition Condition, unsigned AttachToNum)
bool Verify(const DomTreeT &DT)
std::enable_if<!std::is_array< T >::value, std::unique_ptr< T > >::type make_unique(Args &&...args)
Constructs a new T() with the given args and returns a unique_ptr<T> which owns the object...
void DeleteEdge(DomTreeT &DT, typename DomTreeT::NodePtr From, typename DomTreeT::NodePtr To)
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
static void UpdateLevelsAfterInsertion(InsertionInfo &II)
BlockNamePrinter(NodePtr Block)
SmallVector< NodePtr, 2 > ReverseChildren
SmallVector< TreeNodePtr, 8 > AffectedQueue
typename DomTreeT::NodeType NodeT
bool verifyParentProperty(const DomTreeT &DT)
static constexpr bool IsPostDom
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
BlockNamePrinter(TreeNodePtr TN)
auto find(R &&Range, const T &Val) -> decltype(std::begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
LLVM_NODISCARD T pop_back_val()
void calculateFromScratch(DomTreeT &DT, const unsigned NumBlocks)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Implements a dense probed hash-table based set with some number of buckets stored inline...
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
static bool AlwaysDescend(NodePtr, NodePtr)
friend raw_ostream & operator<<(raw_ostream &O, const BlockNamePrinter &BP)
static void InsertUnreachable(DomTreeT &DT, const TreeNodePtr From, const NodePtr To)
std::unique_ptr< DomTreeNodeBase > addChild(std::unique_ptr< DomTreeNodeBase > C)
unsigned doFullDFSWalk(const DomTreeT &DT, DescendCondition DC)
SmallVector< TreeNodePtr, 8 > VisitedNotAffectedQueue
void attachNewSubtree(DomTreeT &DT, const TreeNodePtr AttachTo)
void reattachExistingSubtree(DomTreeT &DT, const TreeNodePtr AttachTo)
static void UpdateInsertion(DomTreeT &DT, const TreeNodePtr NCD, InsertionInfo &II)
static void DeleteUnreachable(DomTreeT &DT, const TreeNodePtr ToTN)
static void InsertReachable(DomTreeT &DT, const TreeNodePtr From, const TreeNodePtr To)
std::vector< NodePtr > NumToNode
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static void DeleteEdge(DomTreeT &DT, const NodePtr From, const NodePtr To)
This class implements an extremely fast bulk output stream that can only output to a stream...
This file defines a set of templates that efficiently compute a dominator tree over a generic graph...
TreeNodePtr getNodeForBlock(NodePtr BB, DomTreeT &DT)
size_t getNumChildren() const
void setIDom(DomTreeNodeBase *NewIDom)