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 reuiqrement
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/DepthFirstIterator.h"
29 #include "llvm/ADT/GraphTraits.h"
30 #include "llvm/ADT/STLExtras.h"
31 #include "llvm/ADT/SmallPtrSet.h"
32 #include "llvm/ADT/SmallVector.h"
33 #include "llvm/Support/Compiler.h"
34 #include "llvm/Support/raw_ostream.h"
39 template <class NodeT> class DominatorTreeBase;
43 template <typename GT> struct DominatorTreeBaseTraits {
44 static_assert(std::is_pointer<typename GT::NodeRef>::value,
45 "Currently NodeRef must be a pointer type.");
46 using type = DominatorTreeBase<
47 typename std::remove_pointer<typename GT::NodeRef>::type>;
50 } // End namespace detail
52 template <typename GT>
53 using DominatorTreeBaseByGraphTraits =
54 typename detail::DominatorTreeBaseTraits<GT>::type;
56 /// \brief Base class that other, more interesting dominator analyses
58 template <class NodeT> class DominatorBase {
60 std::vector<NodeT *> Roots;
61 bool IsPostDominators;
62 explicit DominatorBase(bool isPostDom)
63 : Roots(), IsPostDominators(isPostDom) {}
64 DominatorBase(DominatorBase &&Arg)
65 : Roots(std::move(Arg.Roots)),
66 IsPostDominators(std::move(Arg.IsPostDominators)) {
69 DominatorBase &operator=(DominatorBase &&RHS) {
70 Roots = std::move(RHS.Roots);
71 IsPostDominators = std::move(RHS.IsPostDominators);
77 /// getRoots - Return the root blocks of the current CFG. This may include
78 /// multiple blocks if we are computing post dominators. For forward
79 /// dominators, this will always be a single block (the entry node).
81 const std::vector<NodeT *> &getRoots() const { return Roots; }
83 /// isPostDominator - Returns true if analysis based of postdoms
85 bool isPostDominator() const { return IsPostDominators; }
88 struct PostDominatorTree;
90 /// \brief Base class for the actual dominator tree node.
91 template <class NodeT> class DomTreeNodeBase {
93 DomTreeNodeBase<NodeT> *IDom;
94 std::vector<DomTreeNodeBase<NodeT> *> Children;
95 mutable int DFSNumIn, DFSNumOut;
97 template <class N> friend class DominatorTreeBase;
98 friend struct PostDominatorTree;
101 typedef typename std::vector<DomTreeNodeBase<NodeT> *>::iterator iterator;
102 typedef typename std::vector<DomTreeNodeBase<NodeT> *>::const_iterator
105 iterator begin() { return Children.begin(); }
106 iterator end() { return Children.end(); }
107 const_iterator begin() const { return Children.begin(); }
108 const_iterator end() const { return Children.end(); }
110 NodeT *getBlock() const { return TheBB; }
111 DomTreeNodeBase<NodeT> *getIDom() const { return IDom; }
112 const std::vector<DomTreeNodeBase<NodeT> *> &getChildren() const {
116 DomTreeNodeBase(NodeT *BB, DomTreeNodeBase<NodeT> *iDom)
117 : TheBB(BB), IDom(iDom), DFSNumIn(-1), DFSNumOut(-1) {}
119 std::unique_ptr<DomTreeNodeBase<NodeT>>
120 addChild(std::unique_ptr<DomTreeNodeBase<NodeT>> C) {
121 Children.push_back(C.get());
125 size_t getNumChildren() const { return Children.size(); }
127 void clearAllChildren() { Children.clear(); }
129 bool compare(const DomTreeNodeBase<NodeT> *Other) const {
130 if (getNumChildren() != Other->getNumChildren())
133 SmallPtrSet<const NodeT *, 4> OtherChildren;
134 for (const DomTreeNodeBase *I : *Other) {
135 const NodeT *Nd = I->getBlock();
136 OtherChildren.insert(Nd);
139 for (const DomTreeNodeBase *I : *this) {
140 const NodeT *N = I->getBlock();
141 if (OtherChildren.count(N) == 0)
147 void setIDom(DomTreeNodeBase<NodeT> *NewIDom) {
148 assert(IDom && "No immediate dominator?");
149 if (IDom != NewIDom) {
150 typename std::vector<DomTreeNodeBase<NodeT> *>::iterator I =
151 find(IDom->Children, this);
152 assert(I != IDom->Children.end() &&
153 "Not in immediate dominator children set!");
154 // I am no longer your child...
155 IDom->Children.erase(I);
157 // Switch to new dominator
159 IDom->Children.push_back(this);
163 /// getDFSNumIn/getDFSNumOut - These return the DFS visitation order for nodes
164 /// in the dominator tree. They are only guaranteed valid if
165 /// updateDFSNumbers() has been called.
166 unsigned getDFSNumIn() const { return DFSNumIn; }
167 unsigned getDFSNumOut() const { return DFSNumOut; }
170 // Return true if this node is dominated by other. Use this only if DFS info
172 bool DominatedBy(const DomTreeNodeBase<NodeT> *other) const {
173 return this->DFSNumIn >= other->DFSNumIn &&
174 this->DFSNumOut <= other->DFSNumOut;
178 template <class NodeT>
179 raw_ostream &operator<<(raw_ostream &o, const DomTreeNodeBase<NodeT> *Node) {
180 if (Node->getBlock())
181 Node->getBlock()->printAsOperand(o, false);
183 o << " <<exit node>>";
185 o << " {" << Node->getDFSNumIn() << "," << Node->getDFSNumOut() << "}";
190 template <class NodeT>
191 void PrintDomTree(const DomTreeNodeBase<NodeT> *N, raw_ostream &o,
193 o.indent(2 * Lev) << "[" << Lev << "] " << N;
194 for (typename DomTreeNodeBase<NodeT>::const_iterator I = N->begin(),
197 PrintDomTree<NodeT>(*I, o, Lev + 1);
200 // The calculate routine is provided in a separate header but referenced here.
201 template <class FuncT, class N>
202 void Calculate(DominatorTreeBaseByGraphTraits<GraphTraits<N>> &DT, FuncT &F);
204 /// \brief Core dominator tree base class.
206 /// This class is a generic template over graph nodes. It is instantiated for
207 /// various graphs in the LLVM IR or in the code generator.
208 template <class NodeT> class DominatorTreeBase : public DominatorBase<NodeT> {
209 DominatorTreeBase(const DominatorTreeBase &) = delete;
210 DominatorTreeBase &operator=(const DominatorTreeBase &) = delete;
212 bool dominatedBySlowTreeWalk(const DomTreeNodeBase<NodeT> *A,
213 const DomTreeNodeBase<NodeT> *B) const {
215 assert(isReachableFromEntry(B));
216 assert(isReachableFromEntry(A));
218 const DomTreeNodeBase<NodeT> *IDom;
219 while ((IDom = B->getIDom()) != nullptr && IDom != A && IDom != B)
220 B = IDom; // Walk up the tree
221 return IDom != nullptr;
224 /// \brief Wipe this tree's state without releasing any resources.
226 /// This is essentially a post-move helper only. It leaves the object in an
227 /// assignable and destroyable state, but otherwise invalid.
229 DomTreeNodes.clear();
237 typedef DenseMap<NodeT *, std::unique_ptr<DomTreeNodeBase<NodeT>>>
239 DomTreeNodeMapType DomTreeNodes;
240 DomTreeNodeBase<NodeT> *RootNode;
242 mutable bool DFSInfoValid;
243 mutable unsigned int SlowQueries;
244 // Information record used during immediate dominators computation.
251 InfoRec() : DFSNum(0), Parent(0), Semi(0), Label(nullptr) {}
254 DenseMap<NodeT *, NodeT *> IDoms;
256 // Vertex - Map the DFS number to the NodeT*
257 std::vector<NodeT *> Vertex;
259 // Info - Collection of information used during the computation of idoms.
260 DenseMap<NodeT *, InfoRec> Info;
263 DomTreeNodes.clear();
268 DFSInfoValid = false;
272 // NewBB is split and now it has one successor. Update dominator tree to
273 // reflect this change.
274 template <class N, class GraphT>
275 void Split(DominatorTreeBaseByGraphTraits<GraphT> &DT,
276 typename GraphT::NodeRef NewBB) {
277 assert(std::distance(GraphT::child_begin(NewBB),
278 GraphT::child_end(NewBB)) == 1 &&
279 "NewBB should have a single successor!");
280 typename GraphT::NodeRef NewBBSucc = *GraphT::child_begin(NewBB);
282 std::vector<typename GraphT::NodeRef> PredBlocks;
283 typedef GraphTraits<Inverse<N>> InvTraits;
284 for (typename InvTraits::ChildIteratorType
285 PI = InvTraits::child_begin(NewBB),
286 PE = InvTraits::child_end(NewBB);
288 PredBlocks.push_back(*PI);
290 assert(!PredBlocks.empty() && "No predblocks?");
292 bool NewBBDominatesNewBBSucc = true;
293 for (typename InvTraits::ChildIteratorType
294 PI = InvTraits::child_begin(NewBBSucc),
295 E = InvTraits::child_end(NewBBSucc);
297 typename InvTraits::NodeRef ND = *PI;
298 if (ND != NewBB && !DT.dominates(NewBBSucc, ND) &&
299 DT.isReachableFromEntry(ND)) {
300 NewBBDominatesNewBBSucc = false;
305 // Find NewBB's immediate dominator and create new dominator tree node for
307 NodeT *NewBBIDom = nullptr;
309 for (i = 0; i < PredBlocks.size(); ++i)
310 if (DT.isReachableFromEntry(PredBlocks[i])) {
311 NewBBIDom = PredBlocks[i];
315 // It's possible that none of the predecessors of NewBB are reachable;
316 // in that case, NewBB itself is unreachable, so nothing needs to be
321 for (i = i + 1; i < PredBlocks.size(); ++i) {
322 if (DT.isReachableFromEntry(PredBlocks[i]))
323 NewBBIDom = DT.findNearestCommonDominator(NewBBIDom, PredBlocks[i]);
326 // Create the new dominator tree node... and set the idom of NewBB.
327 DomTreeNodeBase<NodeT> *NewBBNode = DT.addNewBlock(NewBB, NewBBIDom);
329 // If NewBB strictly dominates other blocks, then it is now the immediate
330 // dominator of NewBBSucc. Update the dominator tree as appropriate.
331 if (NewBBDominatesNewBBSucc) {
332 DomTreeNodeBase<NodeT> *NewBBSuccNode = DT.getNode(NewBBSucc);
333 DT.changeImmediateDominator(NewBBSuccNode, NewBBNode);
338 explicit DominatorTreeBase(bool isPostDom)
339 : DominatorBase<NodeT>(isPostDom), DFSInfoValid(false), SlowQueries(0) {}
341 DominatorTreeBase(DominatorTreeBase &&Arg)
342 : DominatorBase<NodeT>(
343 std::move(static_cast<DominatorBase<NodeT> &>(Arg))),
344 DomTreeNodes(std::move(Arg.DomTreeNodes)),
345 RootNode(std::move(Arg.RootNode)),
346 DFSInfoValid(std::move(Arg.DFSInfoValid)),
347 SlowQueries(std::move(Arg.SlowQueries)), IDoms(std::move(Arg.IDoms)),
348 Vertex(std::move(Arg.Vertex)), Info(std::move(Arg.Info)) {
351 DominatorTreeBase &operator=(DominatorTreeBase &&RHS) {
352 DominatorBase<NodeT>::operator=(
353 std::move(static_cast<DominatorBase<NodeT> &>(RHS)));
354 DomTreeNodes = std::move(RHS.DomTreeNodes);
355 RootNode = std::move(RHS.RootNode);
356 DFSInfoValid = std::move(RHS.DFSInfoValid);
357 SlowQueries = std::move(RHS.SlowQueries);
358 IDoms = std::move(RHS.IDoms);
359 Vertex = std::move(RHS.Vertex);
360 Info = std::move(RHS.Info);
365 /// compare - Return false if the other dominator tree base matches this
366 /// dominator tree base. Otherwise return true.
367 bool compare(const DominatorTreeBase &Other) const {
369 const DomTreeNodeMapType &OtherDomTreeNodes = Other.DomTreeNodes;
370 if (DomTreeNodes.size() != OtherDomTreeNodes.size())
373 for (const auto &DomTreeNode : this->DomTreeNodes) {
374 NodeT *BB = DomTreeNode.first;
375 typename DomTreeNodeMapType::const_iterator OI =
376 OtherDomTreeNodes.find(BB);
377 if (OI == OtherDomTreeNodes.end())
380 DomTreeNodeBase<NodeT> &MyNd = *DomTreeNode.second;
381 DomTreeNodeBase<NodeT> &OtherNd = *OI->second;
383 if (MyNd.compare(&OtherNd))
390 void releaseMemory() { reset(); }
392 /// getNode - return the (Post)DominatorTree node for the specified basic
393 /// block. This is the same as using operator[] on this class. The result
394 /// may (but is not required to) be null for a forward (backwards)
395 /// statically unreachable block.
396 DomTreeNodeBase<NodeT> *getNode(NodeT *BB) const {
397 auto I = DomTreeNodes.find(BB);
398 if (I != DomTreeNodes.end())
399 return I->second.get();
404 DomTreeNodeBase<NodeT> *operator[](NodeT *BB) const { return getNode(BB); }
406 /// getRootNode - This returns the entry node for the CFG of the function. If
407 /// this tree represents the post-dominance relations for a function, however,
408 /// this root may be a node with the block == NULL. This is the case when
409 /// there are multiple exit nodes from a particular function. Consumers of
410 /// post-dominance information must be capable of dealing with this
413 DomTreeNodeBase<NodeT> *getRootNode() { return RootNode; }
414 const DomTreeNodeBase<NodeT> *getRootNode() const { return RootNode; }
416 /// Get all nodes dominated by R, including R itself.
417 void getDescendants(NodeT *R, SmallVectorImpl<NodeT *> &Result) const {
419 const DomTreeNodeBase<NodeT> *RN = getNode(R);
421 return; // If R is unreachable, it will not be present in the DOM tree.
422 SmallVector<const DomTreeNodeBase<NodeT> *, 8> WL;
425 while (!WL.empty()) {
426 const DomTreeNodeBase<NodeT> *N = WL.pop_back_val();
427 Result.push_back(N->getBlock());
428 WL.append(N->begin(), N->end());
432 /// properlyDominates - Returns true iff A dominates B and A != B.
433 /// Note that this is not a constant time operation!
435 bool properlyDominates(const DomTreeNodeBase<NodeT> *A,
436 const DomTreeNodeBase<NodeT> *B) const {
441 return dominates(A, B);
444 bool properlyDominates(const NodeT *A, const NodeT *B) const;
446 /// isReachableFromEntry - Return true if A is dominated by the entry
447 /// block of the function containing it.
448 bool isReachableFromEntry(const NodeT *A) const {
449 assert(!this->isPostDominator() &&
450 "This is not implemented for post dominators");
451 return isReachableFromEntry(getNode(const_cast<NodeT *>(A)));
454 bool isReachableFromEntry(const DomTreeNodeBase<NodeT> *A) const { return A; }
456 /// dominates - Returns true iff A dominates B. Note that this is not a
457 /// constant time operation!
459 bool dominates(const DomTreeNodeBase<NodeT> *A,
460 const DomTreeNodeBase<NodeT> *B) const {
461 // A node trivially dominates itself.
465 // An unreachable node is dominated by anything.
466 if (!isReachableFromEntry(B))
469 // And dominates nothing.
470 if (!isReachableFromEntry(A))
473 // Compare the result of the tree walk and the dfs numbers, if expensive
474 // checks are enabled.
475 #ifdef EXPENSIVE_CHECKS
476 assert((!DFSInfoValid ||
477 (dominatedBySlowTreeWalk(A, B) == B->DominatedBy(A))) &&
478 "Tree walk disagrees with dfs numbers!");
482 return B->DominatedBy(A);
484 // If we end up with too many slow queries, just update the
485 // DFS numbers on the theory that we are going to keep querying.
487 if (SlowQueries > 32) {
489 return B->DominatedBy(A);
492 return dominatedBySlowTreeWalk(A, B);
495 bool dominates(const NodeT *A, const NodeT *B) const;
497 NodeT *getRoot() const {
498 assert(this->Roots.size() == 1 && "Should always have entry node!");
499 return this->Roots[0];
502 /// findNearestCommonDominator - Find nearest common dominator basic block
503 /// for basic block A and B. If there is no such block then return NULL.
504 NodeT *findNearestCommonDominator(NodeT *A, NodeT *B) {
505 assert(A->getParent() == B->getParent() &&
506 "Two blocks are not in same function");
508 // If either A or B is a entry block then it is nearest common dominator
509 // (for forward-dominators).
510 if (!this->isPostDominator()) {
511 NodeT &Entry = A->getParent()->front();
512 if (A == &Entry || B == &Entry)
516 // If B dominates A then B is nearest common dominator.
520 // If A dominates B then A is nearest common dominator.
524 DomTreeNodeBase<NodeT> *NodeA = getNode(A);
525 DomTreeNodeBase<NodeT> *NodeB = getNode(B);
527 // If we have DFS info, then we can avoid all allocations by just querying
528 // it from each IDom. Note that because we call 'dominates' twice above, we
529 // expect to call through this code at most 16 times in a row without
530 // building valid DFS information. This is important as below is a *very*
533 DomTreeNodeBase<NodeT> *IDomA = NodeA->getIDom();
535 if (NodeB->DominatedBy(IDomA))
536 return IDomA->getBlock();
537 IDomA = IDomA->getIDom();
542 // Collect NodeA dominators set.
543 SmallPtrSet<DomTreeNodeBase<NodeT> *, 16> NodeADoms;
544 NodeADoms.insert(NodeA);
545 DomTreeNodeBase<NodeT> *IDomA = NodeA->getIDom();
547 NodeADoms.insert(IDomA);
548 IDomA = IDomA->getIDom();
551 // Walk NodeB immediate dominators chain and find common dominator node.
552 DomTreeNodeBase<NodeT> *IDomB = NodeB->getIDom();
554 if (NodeADoms.count(IDomB) != 0)
555 return IDomB->getBlock();
557 IDomB = IDomB->getIDom();
563 const NodeT *findNearestCommonDominator(const NodeT *A, const NodeT *B) {
564 // Cast away the const qualifiers here. This is ok since
565 // const is re-introduced on the return type.
566 return findNearestCommonDominator(const_cast<NodeT *>(A),
567 const_cast<NodeT *>(B));
570 //===--------------------------------------------------------------------===//
571 // API to update (Post)DominatorTree information based on modifications to
574 /// Add a new node to the dominator tree information.
576 /// This creates a new node as a child of DomBB dominator node, linking it
577 /// into the children list of the immediate dominator.
579 /// \param BB New node in CFG.
580 /// \param DomBB CFG node that is dominator for BB.
581 /// \returns New dominator tree node that represents new CFG node.
583 DomTreeNodeBase<NodeT> *addNewBlock(NodeT *BB, NodeT *DomBB) {
584 assert(getNode(BB) == nullptr && "Block already in dominator tree!");
585 DomTreeNodeBase<NodeT> *IDomNode = getNode(DomBB);
586 assert(IDomNode && "Not immediate dominator specified for block!");
587 DFSInfoValid = false;
588 return (DomTreeNodes[BB] = IDomNode->addChild(
589 llvm::make_unique<DomTreeNodeBase<NodeT>>(BB, IDomNode))).get();
592 /// Add a new node to the forward dominator tree and make it a new root.
594 /// \param BB New node in CFG.
595 /// \returns New dominator tree node that represents new CFG node.
597 DomTreeNodeBase<NodeT> *setNewRoot(NodeT *BB) {
598 assert(getNode(BB) == nullptr && "Block already in dominator tree!");
599 assert(!this->isPostDominator() &&
600 "Cannot change root of post-dominator tree");
601 DFSInfoValid = false;
602 auto &Roots = DominatorBase<NodeT>::Roots;
603 DomTreeNodeBase<NodeT> *NewNode = (DomTreeNodes[BB] =
604 llvm::make_unique<DomTreeNodeBase<NodeT>>(BB, nullptr)).get();
608 assert(Roots.size() == 1);
609 NodeT *OldRoot = Roots.front();
610 DomTreeNodes[OldRoot] =
611 NewNode->addChild(std::move(DomTreeNodes[OldRoot]));
614 return RootNode = NewNode;
617 /// changeImmediateDominator - This method is used to update the dominator
618 /// tree information when a node's immediate dominator changes.
620 void changeImmediateDominator(DomTreeNodeBase<NodeT> *N,
621 DomTreeNodeBase<NodeT> *NewIDom) {
622 assert(N && NewIDom && "Cannot change null node pointers!");
623 DFSInfoValid = false;
627 void changeImmediateDominator(NodeT *BB, NodeT *NewBB) {
628 changeImmediateDominator(getNode(BB), getNode(NewBB));
631 /// eraseNode - Removes a node from the dominator tree. Block must not
632 /// dominate any other blocks. Removes node from its immediate dominator's
633 /// children list. Deletes dominator node associated with basic block BB.
634 void eraseNode(NodeT *BB) {
635 DomTreeNodeBase<NodeT> *Node = getNode(BB);
636 assert(Node && "Removing node that isn't in dominator tree.");
637 assert(Node->getChildren().empty() && "Node is not a leaf node.");
639 // Remove node from immediate dominator's children list.
640 DomTreeNodeBase<NodeT> *IDom = Node->getIDom();
642 typename std::vector<DomTreeNodeBase<NodeT> *>::iterator I =
643 find(IDom->Children, Node);
644 assert(I != IDom->Children.end() &&
645 "Not in immediate dominator children set!");
646 // I am no longer your child...
647 IDom->Children.erase(I);
650 DomTreeNodes.erase(BB);
653 /// splitBlock - BB is split and now it has one successor. Update dominator
654 /// tree to reflect this change.
655 void splitBlock(NodeT *NewBB) {
656 if (this->IsPostDominators)
657 this->Split<Inverse<NodeT *>, GraphTraits<Inverse<NodeT *>>>(*this,
660 this->Split<NodeT *, GraphTraits<NodeT *>>(*this, NewBB);
663 /// print - Convert to human readable form
665 void print(raw_ostream &o) const {
666 o << "=============================--------------------------------\n";
667 if (this->isPostDominator())
668 o << "Inorder PostDominator Tree: ";
670 o << "Inorder Dominator Tree: ";
671 if (!this->DFSInfoValid)
672 o << "DFSNumbers invalid: " << SlowQueries << " slow queries.";
675 // The postdom tree can have a null root if there are no returns.
677 PrintDomTree<NodeT>(getRootNode(), o, 1);
681 template <class GraphT>
682 friend typename GraphT::NodeRef
683 Eval(DominatorTreeBaseByGraphTraits<GraphT> &DT, typename GraphT::NodeRef V,
684 unsigned LastLinked);
686 template <class GraphT>
687 friend unsigned DFSPass(DominatorTreeBaseByGraphTraits<GraphT> &DT,
688 typename GraphT::NodeRef V, unsigned N);
690 template <class FuncT, class N>
691 friend void Calculate(DominatorTreeBaseByGraphTraits<GraphTraits<N>> &DT,
694 DomTreeNodeBase<NodeT> *getNodeForBlock(NodeT *BB) {
695 if (DomTreeNodeBase<NodeT> *Node = getNode(BB))
698 // Haven't calculated this node yet? Get or calculate the node for the
699 // immediate dominator.
700 NodeT *IDom = getIDom(BB);
702 assert(IDom || this->DomTreeNodes[nullptr]);
703 DomTreeNodeBase<NodeT> *IDomNode = getNodeForBlock(IDom);
705 // Add a new tree node for this NodeT, and link it as a child of
707 return (this->DomTreeNodes[BB] = IDomNode->addChild(
708 llvm::make_unique<DomTreeNodeBase<NodeT>>(BB, IDomNode))).get();
711 NodeT *getIDom(NodeT *BB) const { return IDoms.lookup(BB); }
713 void addRoot(NodeT *BB) { this->Roots.push_back(BB); }
716 /// updateDFSNumbers - Assign In and Out numbers to the nodes while walking
717 /// dominator tree in dfs order.
718 void updateDFSNumbers() const {
727 SmallVector<std::pair<const DomTreeNodeBase<NodeT> *,
728 typename DomTreeNodeBase<NodeT>::const_iterator>,
731 const DomTreeNodeBase<NodeT> *ThisRoot = getRootNode();
736 // Even in the case of multiple exits that form the post dominator root
737 // nodes, do not iterate over all exits, but start from the virtual root
738 // node. Otherwise bbs, that are not post dominated by any exit but by the
739 // virtual root node, will never be assigned a DFS number.
740 WorkStack.push_back(std::make_pair(ThisRoot, ThisRoot->begin()));
741 ThisRoot->DFSNumIn = DFSNum++;
743 while (!WorkStack.empty()) {
744 const DomTreeNodeBase<NodeT> *Node = WorkStack.back().first;
745 typename DomTreeNodeBase<NodeT>::const_iterator ChildIt =
746 WorkStack.back().second;
748 // If we visited all of the children of this node, "recurse" back up the
749 // stack setting the DFOutNum.
750 if (ChildIt == Node->end()) {
751 Node->DFSNumOut = DFSNum++;
752 WorkStack.pop_back();
754 // Otherwise, recursively visit this child.
755 const DomTreeNodeBase<NodeT> *Child = *ChildIt;
756 ++WorkStack.back().second;
758 WorkStack.push_back(std::make_pair(Child, Child->begin()));
759 Child->DFSNumIn = DFSNum++;
767 /// recalculate - compute a dominator tree for the given function
768 template <class FT> void recalculate(FT &F) {
769 typedef GraphTraits<FT *> TraitsTy;
771 this->Vertex.push_back(nullptr);
773 if (!this->IsPostDominators) {
775 NodeT *entry = TraitsTy::getEntryNode(&F);
778 Calculate<FT, NodeT *>(*this, F);
780 // Initialize the roots list
781 for (typename TraitsTy::nodes_iterator I = TraitsTy::nodes_begin(&F),
782 E = TraitsTy::nodes_end(&F);
784 if (TraitsTy::child_begin(*I) == TraitsTy::child_end(*I))
787 Calculate<FT, Inverse<NodeT *>>(*this, F);
792 // These two functions are declared out of line as a workaround for building
793 // with old (< r147295) versions of clang because of pr11642.
794 template <class NodeT>
795 bool DominatorTreeBase<NodeT>::dominates(const NodeT *A, const NodeT *B) const {
799 // Cast away the const qualifiers here. This is ok since
800 // this function doesn't actually return the values returned
802 return dominates(getNode(const_cast<NodeT *>(A)),
803 getNode(const_cast<NodeT *>(B)));
805 template <class NodeT>
806 bool DominatorTreeBase<NodeT>::properlyDominates(const NodeT *A,
807 const NodeT *B) const {
811 // Cast away the const qualifiers here. This is ok since
812 // this function doesn't actually return the values returned
814 return dominates(getNode(const_cast<NodeT *>(A)),
815 getNode(const_cast<NodeT *>(B)));