1 //===- GenericDomTree.h - Generic dominator trees for graphs ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// This file defines a set of templates that efficiently compute a dominator
12 /// tree over a generic graph. This is used typically in LLVM for fast
13 /// dominance queries on the CFG, but is fully generic w.r.t. the underlying
16 /// Unlike ADT/* graph algorithms, generic dominator tree has more requirements
17 /// on the graph's NodeRef. The NodeRef should be a pointer and, depending on
18 /// the implementation, e.g. NodeRef->getParent() return the parent node.
20 /// FIXME: Maybe GenericDomTree needs a TreeTraits, instead of GraphTraits.
22 //===----------------------------------------------------------------------===//
24 #ifndef LLVM_SUPPORT_GENERICDOMTREE_H
25 #define LLVM_SUPPORT_GENERICDOMTREE_H
27 #include "llvm/ADT/DenseMap.h"
28 #include "llvm/ADT/GraphTraits.h"
29 #include "llvm/ADT/SmallPtrSet.h"
30 #include "llvm/ADT/SmallVector.h"
31 #include "llvm/ADT/STLExtras.h"
32 #include "llvm/Support/raw_ostream.h"
38 #include <type_traits>
44 template <class NodeT> class DominatorTreeBase;
48 template <typename GT> struct DominatorTreeBaseTraits {
49 static_assert(std::is_pointer<typename GT::NodeRef>::value,
50 "Currently NodeRef must be a pointer type.");
51 using type = DominatorTreeBase<
52 typename std::remove_pointer<typename GT::NodeRef>::type>;
55 } // end namespace detail
57 template <typename GT>
58 using DominatorTreeBaseByGraphTraits =
59 typename detail::DominatorTreeBaseTraits<GT>::type;
61 /// \brief Base class that other, more interesting dominator analyses
63 template <class NodeT> class DominatorBase {
65 std::vector<NodeT *> Roots;
66 bool IsPostDominators;
68 explicit DominatorBase(bool isPostDom)
69 : Roots(), IsPostDominators(isPostDom) {}
71 DominatorBase(DominatorBase &&Arg)
72 : Roots(std::move(Arg.Roots)),
73 IsPostDominators(std::move(Arg.IsPostDominators)) {
77 DominatorBase &operator=(DominatorBase &&RHS) {
78 Roots = std::move(RHS.Roots);
79 IsPostDominators = std::move(RHS.IsPostDominators);
85 /// getRoots - Return the root blocks of the current CFG. This may include
86 /// multiple blocks if we are computing post dominators. For forward
87 /// dominators, this will always be a single block (the entry node).
89 const std::vector<NodeT *> &getRoots() const { return Roots; }
91 /// isPostDominator - Returns true if analysis based of postdoms
93 bool isPostDominator() const { return IsPostDominators; }
96 /// \brief Base class for the actual dominator tree node.
97 template <class NodeT> class DomTreeNodeBase {
98 friend struct PostDominatorTree;
99 template <class N> friend class DominatorTreeBase;
102 DomTreeNodeBase<NodeT> *IDom;
103 std::vector<DomTreeNodeBase<NodeT> *> Children;
104 mutable int DFSNumIn = -1;
105 mutable int DFSNumOut = -1;
108 DomTreeNodeBase(NodeT *BB, DomTreeNodeBase<NodeT> *iDom)
109 : TheBB(BB), IDom(iDom) {}
111 typedef typename std::vector<DomTreeNodeBase<NodeT> *>::iterator iterator;
112 typedef typename std::vector<DomTreeNodeBase<NodeT> *>::const_iterator
115 iterator begin() { return Children.begin(); }
116 iterator end() { return Children.end(); }
117 const_iterator begin() const { return Children.begin(); }
118 const_iterator end() const { return Children.end(); }
120 NodeT *getBlock() const { return TheBB; }
121 DomTreeNodeBase<NodeT> *getIDom() const { return IDom; }
123 const std::vector<DomTreeNodeBase<NodeT> *> &getChildren() const {
127 std::unique_ptr<DomTreeNodeBase<NodeT>>
128 addChild(std::unique_ptr<DomTreeNodeBase<NodeT>> C) {
129 Children.push_back(C.get());
133 size_t getNumChildren() const { return Children.size(); }
135 void clearAllChildren() { Children.clear(); }
137 bool compare(const DomTreeNodeBase<NodeT> *Other) const {
138 if (getNumChildren() != Other->getNumChildren())
141 SmallPtrSet<const NodeT *, 4> OtherChildren;
142 for (const DomTreeNodeBase *I : *Other) {
143 const NodeT *Nd = I->getBlock();
144 OtherChildren.insert(Nd);
147 for (const DomTreeNodeBase *I : *this) {
148 const NodeT *N = I->getBlock();
149 if (OtherChildren.count(N) == 0)
155 void setIDom(DomTreeNodeBase<NodeT> *NewIDom) {
156 assert(IDom && "No immediate dominator?");
157 if (IDom != NewIDom) {
158 typename std::vector<DomTreeNodeBase<NodeT> *>::iterator I =
159 find(IDom->Children, this);
160 assert(I != IDom->Children.end() &&
161 "Not in immediate dominator children set!");
162 // I am no longer your child...
163 IDom->Children.erase(I);
165 // Switch to new dominator
167 IDom->Children.push_back(this);
171 /// getDFSNumIn/getDFSNumOut - These return the DFS visitation order for nodes
172 /// in the dominator tree. They are only guaranteed valid if
173 /// updateDFSNumbers() has been called.
174 unsigned getDFSNumIn() const { return DFSNumIn; }
175 unsigned getDFSNumOut() const { return DFSNumOut; }
178 // Return true if this node is dominated by other. Use this only if DFS info
180 bool DominatedBy(const DomTreeNodeBase<NodeT> *other) const {
181 return this->DFSNumIn >= other->DFSNumIn &&
182 this->DFSNumOut <= other->DFSNumOut;
186 template <class NodeT>
187 raw_ostream &operator<<(raw_ostream &o, const DomTreeNodeBase<NodeT> *Node) {
188 if (Node->getBlock())
189 Node->getBlock()->printAsOperand(o, false);
191 o << " <<exit node>>";
193 o << " {" << Node->getDFSNumIn() << "," << Node->getDFSNumOut() << "}";
198 template <class NodeT>
199 void PrintDomTree(const DomTreeNodeBase<NodeT> *N, raw_ostream &o,
201 o.indent(2 * Lev) << "[" << Lev << "] " << N;
202 for (typename DomTreeNodeBase<NodeT>::const_iterator I = N->begin(),
205 PrintDomTree<NodeT>(*I, o, Lev + 1);
208 // The calculate routine is provided in a separate header but referenced here.
209 template <class FuncT, class N>
210 void Calculate(DominatorTreeBaseByGraphTraits<GraphTraits<N>> &DT, FuncT &F);
212 /// \brief Core dominator tree base class.
214 /// This class is a generic template over graph nodes. It is instantiated for
215 /// various graphs in the LLVM IR or in the code generator.
216 template <class NodeT> class DominatorTreeBase : public DominatorBase<NodeT> {
217 bool dominatedBySlowTreeWalk(const DomTreeNodeBase<NodeT> *A,
218 const DomTreeNodeBase<NodeT> *B) const {
220 assert(isReachableFromEntry(B));
221 assert(isReachableFromEntry(A));
223 const DomTreeNodeBase<NodeT> *IDom;
224 while ((IDom = B->getIDom()) != nullptr && IDom != A && IDom != B)
225 B = IDom; // Walk up the tree
226 return IDom != nullptr;
229 /// \brief Wipe this tree's state without releasing any resources.
231 /// This is essentially a post-move helper only. It leaves the object in an
232 /// assignable and destroyable state, but otherwise invalid.
234 DomTreeNodes.clear();
242 typedef DenseMap<NodeT *, std::unique_ptr<DomTreeNodeBase<NodeT>>>
244 DomTreeNodeMapType DomTreeNodes;
245 DomTreeNodeBase<NodeT> *RootNode;
247 mutable bool DFSInfoValid = false;
248 mutable unsigned int SlowQueries = 0;
249 // Information record used during immediate dominators computation.
254 NodeT *Label = nullptr;
259 DenseMap<NodeT *, NodeT *> IDoms;
261 // Vertex - Map the DFS number to the NodeT*
262 std::vector<NodeT *> Vertex;
264 // Info - Collection of information used during the computation of idoms.
265 DenseMap<NodeT *, InfoRec> Info;
268 DomTreeNodes.clear();
273 DFSInfoValid = false;
277 // NewBB is split and now it has one successor. Update dominator tree to
278 // reflect this change.
280 void Split(typename GraphTraits<N>::NodeRef NewBB) {
281 using GraphT = GraphTraits<N>;
282 using NodeRef = typename GraphT::NodeRef;
283 assert(std::distance(GraphT::child_begin(NewBB),
284 GraphT::child_end(NewBB)) == 1 &&
285 "NewBB should have a single successor!");
286 NodeRef NewBBSucc = *GraphT::child_begin(NewBB);
288 std::vector<NodeRef> PredBlocks;
289 for (const auto Pred : children<Inverse<N>>(NewBB))
290 PredBlocks.push_back(Pred);
292 assert(!PredBlocks.empty() && "No predblocks?");
294 bool NewBBDominatesNewBBSucc = true;
295 for (const auto Pred : children<Inverse<N>>(NewBBSucc)) {
296 if (Pred != NewBB && !dominates(NewBBSucc, Pred) &&
297 isReachableFromEntry(Pred)) {
298 NewBBDominatesNewBBSucc = false;
303 // Find NewBB's immediate dominator and create new dominator tree node for
305 NodeT *NewBBIDom = nullptr;
307 for (i = 0; i < PredBlocks.size(); ++i)
308 if (isReachableFromEntry(PredBlocks[i])) {
309 NewBBIDom = PredBlocks[i];
313 // It's possible that none of the predecessors of NewBB are reachable;
314 // in that case, NewBB itself is unreachable, so nothing needs to be
319 for (i = i + 1; i < PredBlocks.size(); ++i) {
320 if (isReachableFromEntry(PredBlocks[i]))
321 NewBBIDom = findNearestCommonDominator(NewBBIDom, PredBlocks[i]);
324 // Create the new dominator tree node... and set the idom of NewBB.
325 DomTreeNodeBase<NodeT> *NewBBNode = addNewBlock(NewBB, NewBBIDom);
327 // If NewBB strictly dominates other blocks, then it is now the immediate
328 // dominator of NewBBSucc. Update the dominator tree as appropriate.
329 if (NewBBDominatesNewBBSucc) {
330 DomTreeNodeBase<NodeT> *NewBBSuccNode = getNode(NewBBSucc);
331 changeImmediateDominator(NewBBSuccNode, NewBBNode);
336 explicit DominatorTreeBase(bool isPostDom)
337 : DominatorBase<NodeT>(isPostDom) {}
339 DominatorTreeBase(DominatorTreeBase &&Arg)
340 : DominatorBase<NodeT>(
341 std::move(static_cast<DominatorBase<NodeT> &>(Arg))),
342 DomTreeNodes(std::move(Arg.DomTreeNodes)),
343 RootNode(std::move(Arg.RootNode)),
344 DFSInfoValid(std::move(Arg.DFSInfoValid)),
345 SlowQueries(std::move(Arg.SlowQueries)), IDoms(std::move(Arg.IDoms)),
346 Vertex(std::move(Arg.Vertex)), Info(std::move(Arg.Info)) {
350 DominatorTreeBase &operator=(DominatorTreeBase &&RHS) {
351 DominatorBase<NodeT>::operator=(
352 std::move(static_cast<DominatorBase<NodeT> &>(RHS)));
353 DomTreeNodes = std::move(RHS.DomTreeNodes);
354 RootNode = std::move(RHS.RootNode);
355 DFSInfoValid = std::move(RHS.DFSInfoValid);
356 SlowQueries = std::move(RHS.SlowQueries);
357 IDoms = std::move(RHS.IDoms);
358 Vertex = std::move(RHS.Vertex);
359 Info = std::move(RHS.Info);
364 DominatorTreeBase(const DominatorTreeBase &) = delete;
365 DominatorTreeBase &operator=(const DominatorTreeBase &) = delete;
367 /// compare - Return false if the other dominator tree base matches this
368 /// dominator tree base. Otherwise return true.
369 bool compare(const DominatorTreeBase &Other) const {
371 const DomTreeNodeMapType &OtherDomTreeNodes = Other.DomTreeNodes;
372 if (DomTreeNodes.size() != OtherDomTreeNodes.size())
375 for (const auto &DomTreeNode : DomTreeNodes) {
376 NodeT *BB = DomTreeNode.first;
377 typename DomTreeNodeMapType::const_iterator OI =
378 OtherDomTreeNodes.find(BB);
379 if (OI == OtherDomTreeNodes.end())
382 DomTreeNodeBase<NodeT> &MyNd = *DomTreeNode.second;
383 DomTreeNodeBase<NodeT> &OtherNd = *OI->second;
385 if (MyNd.compare(&OtherNd))
392 void releaseMemory() { reset(); }
394 /// getNode - return the (Post)DominatorTree node for the specified basic
395 /// block. This is the same as using operator[] on this class. The result
396 /// may (but is not required to) be null for a forward (backwards)
397 /// statically unreachable block.
398 DomTreeNodeBase<NodeT> *getNode(NodeT *BB) const {
399 auto I = DomTreeNodes.find(BB);
400 if (I != DomTreeNodes.end())
401 return I->second.get();
406 DomTreeNodeBase<NodeT> *operator[](NodeT *BB) const { return getNode(BB); }
408 /// getRootNode - This returns the entry node for the CFG of the function. If
409 /// this tree represents the post-dominance relations for a function, however,
410 /// this root may be a node with the block == NULL. This is the case when
411 /// there are multiple exit nodes from a particular function. Consumers of
412 /// post-dominance information must be capable of dealing with this
415 DomTreeNodeBase<NodeT> *getRootNode() { return RootNode; }
416 const DomTreeNodeBase<NodeT> *getRootNode() const { return RootNode; }
418 /// Get all nodes dominated by R, including R itself.
419 void getDescendants(NodeT *R, SmallVectorImpl<NodeT *> &Result) const {
421 const DomTreeNodeBase<NodeT> *RN = getNode(R);
423 return; // If R is unreachable, it will not be present in the DOM tree.
424 SmallVector<const DomTreeNodeBase<NodeT> *, 8> WL;
427 while (!WL.empty()) {
428 const DomTreeNodeBase<NodeT> *N = WL.pop_back_val();
429 Result.push_back(N->getBlock());
430 WL.append(N->begin(), N->end());
434 /// properlyDominates - Returns true iff A dominates B and A != B.
435 /// Note that this is not a constant time operation!
437 bool properlyDominates(const DomTreeNodeBase<NodeT> *A,
438 const DomTreeNodeBase<NodeT> *B) const {
443 return dominates(A, B);
446 bool properlyDominates(const NodeT *A, const NodeT *B) const;
448 /// isReachableFromEntry - Return true if A is dominated by the entry
449 /// block of the function containing it.
450 bool isReachableFromEntry(const NodeT *A) const {
451 assert(!this->isPostDominator() &&
452 "This is not implemented for post dominators");
453 return isReachableFromEntry(getNode(const_cast<NodeT *>(A)));
456 bool isReachableFromEntry(const DomTreeNodeBase<NodeT> *A) const { return A; }
458 /// dominates - Returns true iff A dominates B. Note that this is not a
459 /// constant time operation!
461 bool dominates(const DomTreeNodeBase<NodeT> *A,
462 const DomTreeNodeBase<NodeT> *B) const {
463 // A node trivially dominates itself.
467 // An unreachable node is dominated by anything.
468 if (!isReachableFromEntry(B))
471 // And dominates nothing.
472 if (!isReachableFromEntry(A))
475 // Compare the result of the tree walk and the dfs numbers, if expensive
476 // checks are enabled.
477 #ifdef EXPENSIVE_CHECKS
478 assert((!DFSInfoValid ||
479 (dominatedBySlowTreeWalk(A, B) == B->DominatedBy(A))) &&
480 "Tree walk disagrees with dfs numbers!");
484 return B->DominatedBy(A);
486 // If we end up with too many slow queries, just update the
487 // DFS numbers on the theory that we are going to keep querying.
489 if (SlowQueries > 32) {
491 return B->DominatedBy(A);
494 return dominatedBySlowTreeWalk(A, B);
497 bool dominates(const NodeT *A, const NodeT *B) const;
499 NodeT *getRoot() const {
500 assert(this->Roots.size() == 1 && "Should always have entry node!");
501 return this->Roots[0];
504 /// findNearestCommonDominator - Find nearest common dominator basic block
505 /// for basic block A and B. If there is no such block then return NULL.
506 NodeT *findNearestCommonDominator(NodeT *A, NodeT *B) {
507 assert(A->getParent() == B->getParent() &&
508 "Two blocks are not in same function");
510 // If either A or B is a entry block then it is nearest common dominator
511 // (for forward-dominators).
512 if (!this->isPostDominator()) {
513 NodeT &Entry = A->getParent()->front();
514 if (A == &Entry || B == &Entry)
518 // If B dominates A then B is nearest common dominator.
522 // If A dominates B then A is nearest common dominator.
526 DomTreeNodeBase<NodeT> *NodeA = getNode(A);
527 DomTreeNodeBase<NodeT> *NodeB = getNode(B);
529 // If we have DFS info, then we can avoid all allocations by just querying
530 // it from each IDom. Note that because we call 'dominates' twice above, we
531 // expect to call through this code at most 16 times in a row without
532 // building valid DFS information. This is important as below is a *very*
535 DomTreeNodeBase<NodeT> *IDomA = NodeA->getIDom();
537 if (NodeB->DominatedBy(IDomA))
538 return IDomA->getBlock();
539 IDomA = IDomA->getIDom();
544 // Collect NodeA dominators set.
545 SmallPtrSet<DomTreeNodeBase<NodeT> *, 16> NodeADoms;
546 NodeADoms.insert(NodeA);
547 DomTreeNodeBase<NodeT> *IDomA = NodeA->getIDom();
549 NodeADoms.insert(IDomA);
550 IDomA = IDomA->getIDom();
553 // Walk NodeB immediate dominators chain and find common dominator node.
554 DomTreeNodeBase<NodeT> *IDomB = NodeB->getIDom();
556 if (NodeADoms.count(IDomB) != 0)
557 return IDomB->getBlock();
559 IDomB = IDomB->getIDom();
565 const NodeT *findNearestCommonDominator(const NodeT *A, const NodeT *B) {
566 // Cast away the const qualifiers here. This is ok since
567 // const is re-introduced on the return type.
568 return findNearestCommonDominator(const_cast<NodeT *>(A),
569 const_cast<NodeT *>(B));
572 //===--------------------------------------------------------------------===//
573 // API to update (Post)DominatorTree information based on modifications to
576 /// Add a new node to the dominator tree information.
578 /// This creates a new node as a child of DomBB dominator node, linking it
579 /// into the children list of the immediate dominator.
581 /// \param BB New node in CFG.
582 /// \param DomBB CFG node that is dominator for BB.
583 /// \returns New dominator tree node that represents new CFG node.
585 DomTreeNodeBase<NodeT> *addNewBlock(NodeT *BB, NodeT *DomBB) {
586 assert(getNode(BB) == nullptr && "Block already in dominator tree!");
587 DomTreeNodeBase<NodeT> *IDomNode = getNode(DomBB);
588 assert(IDomNode && "Not immediate dominator specified for block!");
589 DFSInfoValid = false;
590 return (DomTreeNodes[BB] = IDomNode->addChild(
591 llvm::make_unique<DomTreeNodeBase<NodeT>>(BB, IDomNode))).get();
594 /// Add a new node to the forward dominator tree and make it a new root.
596 /// \param BB New node in CFG.
597 /// \returns New dominator tree node that represents new CFG node.
599 DomTreeNodeBase<NodeT> *setNewRoot(NodeT *BB) {
600 assert(getNode(BB) == nullptr && "Block already in dominator tree!");
601 assert(!this->isPostDominator() &&
602 "Cannot change root of post-dominator tree");
603 DFSInfoValid = false;
604 auto &Roots = DominatorBase<NodeT>::Roots;
605 DomTreeNodeBase<NodeT> *NewNode = (DomTreeNodes[BB] =
606 llvm::make_unique<DomTreeNodeBase<NodeT>>(BB, nullptr)).get();
610 assert(Roots.size() == 1);
611 NodeT *OldRoot = Roots.front();
612 DomTreeNodes[OldRoot] =
613 NewNode->addChild(std::move(DomTreeNodes[OldRoot]));
616 return RootNode = NewNode;
619 /// changeImmediateDominator - This method is used to update the dominator
620 /// tree information when a node's immediate dominator changes.
622 void changeImmediateDominator(DomTreeNodeBase<NodeT> *N,
623 DomTreeNodeBase<NodeT> *NewIDom) {
624 assert(N && NewIDom && "Cannot change null node pointers!");
625 DFSInfoValid = false;
629 void changeImmediateDominator(NodeT *BB, NodeT *NewBB) {
630 changeImmediateDominator(getNode(BB), getNode(NewBB));
633 /// eraseNode - Removes a node from the dominator tree. Block must not
634 /// dominate any other blocks. Removes node from its immediate dominator's
635 /// children list. Deletes dominator node associated with basic block BB.
636 void eraseNode(NodeT *BB) {
637 DomTreeNodeBase<NodeT> *Node = getNode(BB);
638 assert(Node && "Removing node that isn't in dominator tree.");
639 assert(Node->getChildren().empty() && "Node is not a leaf node.");
641 // Remove node from immediate dominator's children list.
642 DomTreeNodeBase<NodeT> *IDom = Node->getIDom();
644 typename std::vector<DomTreeNodeBase<NodeT> *>::iterator I =
645 find(IDom->Children, Node);
646 assert(I != IDom->Children.end() &&
647 "Not in immediate dominator children set!");
648 // I am no longer your child...
649 IDom->Children.erase(I);
652 DomTreeNodes.erase(BB);
655 /// splitBlock - BB is split and now it has one successor. Update dominator
656 /// tree to reflect this change.
657 void splitBlock(NodeT *NewBB) {
658 if (this->IsPostDominators)
659 Split<Inverse<NodeT *>>(NewBB);
661 Split<NodeT *>(NewBB);
664 /// print - Convert to human readable form
666 void print(raw_ostream &o) const {
667 o << "=============================--------------------------------\n";
668 if (this->isPostDominator())
669 o << "Inorder PostDominator Tree: ";
671 o << "Inorder Dominator Tree: ";
673 o << "DFSNumbers invalid: " << SlowQueries << " slow queries.";
676 // The postdom tree can have a null root if there are no returns.
678 PrintDomTree<NodeT>(getRootNode(), o, 1);
682 template <class GraphT>
683 friend typename GraphT::NodeRef
684 Eval(DominatorTreeBaseByGraphTraits<GraphT> &DT, typename GraphT::NodeRef V,
685 unsigned LastLinked);
687 template <class GraphT>
688 friend unsigned ReverseDFSPass(DominatorTreeBaseByGraphTraits<GraphT> &DT,
689 typename GraphT::NodeRef V, unsigned N);
691 template <class GraphT>
692 friend unsigned DFSPass(DominatorTreeBaseByGraphTraits<GraphT> &DT,
693 typename GraphT::NodeRef V, unsigned N);
695 template <class FuncT, class N>
696 friend void Calculate(DominatorTreeBaseByGraphTraits<GraphTraits<N>> &DT,
699 DomTreeNodeBase<NodeT> *getNodeForBlock(NodeT *BB) {
700 if (DomTreeNodeBase<NodeT> *Node = getNode(BB))
703 // Haven't calculated this node yet? Get or calculate the node for the
704 // immediate dominator.
705 NodeT *IDom = getIDom(BB);
707 assert(IDom || DomTreeNodes[nullptr]);
708 DomTreeNodeBase<NodeT> *IDomNode = getNodeForBlock(IDom);
710 // Add a new tree node for this NodeT, and link it as a child of
712 return (DomTreeNodes[BB] = IDomNode->addChild(
713 llvm::make_unique<DomTreeNodeBase<NodeT>>(BB, IDomNode))).get();
716 NodeT *getIDom(NodeT *BB) const { return IDoms.lookup(BB); }
718 void addRoot(NodeT *BB) { this->Roots.push_back(BB); }
721 /// updateDFSNumbers - Assign In and Out numbers to the nodes while walking
722 /// dominator tree in dfs order.
723 void updateDFSNumbers() const {
731 SmallVector<std::pair<const DomTreeNodeBase<NodeT> *,
732 typename DomTreeNodeBase<NodeT>::const_iterator>,
735 const DomTreeNodeBase<NodeT> *ThisRoot = getRootNode();
740 // Even in the case of multiple exits that form the post dominator root
741 // nodes, do not iterate over all exits, but start from the virtual root
742 // node. Otherwise bbs, that are not post dominated by any exit but by the
743 // virtual root node, will never be assigned a DFS number.
744 WorkStack.push_back(std::make_pair(ThisRoot, ThisRoot->begin()));
745 ThisRoot->DFSNumIn = DFSNum++;
747 while (!WorkStack.empty()) {
748 const DomTreeNodeBase<NodeT> *Node = WorkStack.back().first;
749 typename DomTreeNodeBase<NodeT>::const_iterator ChildIt =
750 WorkStack.back().second;
752 // If we visited all of the children of this node, "recurse" back up the
753 // stack setting the DFOutNum.
754 if (ChildIt == Node->end()) {
755 Node->DFSNumOut = DFSNum++;
756 WorkStack.pop_back();
758 // Otherwise, recursively visit this child.
759 const DomTreeNodeBase<NodeT> *Child = *ChildIt;
760 ++WorkStack.back().second;
762 WorkStack.push_back(std::make_pair(Child, Child->begin()));
763 Child->DFSNumIn = DFSNum++;
771 /// recalculate - compute a dominator tree for the given function
772 template <class FT> void recalculate(FT &F) {
773 typedef GraphTraits<FT *> TraitsTy;
775 Vertex.push_back(nullptr);
777 if (!this->IsPostDominators) {
779 NodeT *entry = TraitsTy::getEntryNode(&F);
782 Calculate<FT, NodeT *>(*this, F);
784 // Initialize the roots list
785 for (auto *Node : nodes(&F))
786 if (TraitsTy::child_begin(Node) == TraitsTy::child_end(Node))
789 Calculate<FT, Inverse<NodeT *>>(*this, F);
794 // These two functions are declared out of line as a workaround for building
795 // with old (< r147295) versions of clang because of pr11642.
796 template <class NodeT>
797 bool DominatorTreeBase<NodeT>::dominates(const NodeT *A, const NodeT *B) const {
801 // Cast away the const qualifiers here. This is ok since
802 // this function doesn't actually return the values returned
804 return dominates(getNode(const_cast<NodeT *>(A)),
805 getNode(const_cast<NodeT *>(B)));
807 template <class NodeT>
808 bool DominatorTreeBase<NodeT>::properlyDominates(const NodeT *A,
809 const NodeT *B) const {
813 // Cast away the const qualifiers here. This is ok since
814 // this function doesn't actually return the values returned
816 return dominates(getNode(const_cast<NodeT *>(A)),
817 getNode(const_cast<NodeT *>(B)));
820 } // end namespace llvm
822 #endif // LLVM_SUPPORT_GENERICDOMTREE_H