1 //===- llvm/Analysis/Dominators.h - Dominator Info Calculation --*- 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 //===----------------------------------------------------------------------===//
10 // This file defines the DominatorTree class, which provides fast and efficient
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_ANALYSIS_DOMINATORS_H
16 #define LLVM_ANALYSIS_DOMINATORS_H
18 #include "llvm/Pass.h"
19 #include "llvm/Function.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/DepthFirstIterator.h"
22 #include "llvm/ADT/GraphTraits.h"
23 #include "llvm/ADT/SmallPtrSet.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/Support/CFG.h"
26 #include "llvm/Support/Compiler.h"
27 #include "llvm/Support/raw_ostream.h"
32 //===----------------------------------------------------------------------===//
33 /// DominatorBase - Base class that other, more interesting dominator analyses
36 template <class NodeT>
39 std::vector<NodeT*> Roots;
40 const bool IsPostDominators;
41 inline explicit DominatorBase(bool isPostDom) :
42 Roots(), IsPostDominators(isPostDom) {}
45 /// getRoots - Return the root blocks of the current CFG. This may include
46 /// multiple blocks if we are computing post dominators. For forward
47 /// dominators, this will always be a single block (the entry node).
49 inline const std::vector<NodeT*> &getRoots() const { return Roots; }
51 /// isPostDominator - Returns true if analysis based of postdoms
53 bool isPostDominator() const { return IsPostDominators; }
57 //===----------------------------------------------------------------------===//
58 // DomTreeNode - Dominator Tree Node
59 template<class NodeT> class DominatorTreeBase;
60 struct PostDominatorTree;
61 class MachineBasicBlock;
63 template <class NodeT>
64 class DomTreeNodeBase {
66 DomTreeNodeBase<NodeT> *IDom;
67 std::vector<DomTreeNodeBase<NodeT> *> Children;
68 int DFSNumIn, DFSNumOut;
70 template<class N> friend class DominatorTreeBase;
71 friend struct PostDominatorTree;
73 typedef typename std::vector<DomTreeNodeBase<NodeT> *>::iterator iterator;
74 typedef typename std::vector<DomTreeNodeBase<NodeT> *>::const_iterator
77 iterator begin() { return Children.begin(); }
78 iterator end() { return Children.end(); }
79 const_iterator begin() const { return Children.begin(); }
80 const_iterator end() const { return Children.end(); }
82 NodeT *getBlock() const { return TheBB; }
83 DomTreeNodeBase<NodeT> *getIDom() const { return IDom; }
84 const std::vector<DomTreeNodeBase<NodeT>*> &getChildren() const {
88 DomTreeNodeBase(NodeT *BB, DomTreeNodeBase<NodeT> *iDom)
89 : TheBB(BB), IDom(iDom), DFSNumIn(-1), DFSNumOut(-1) { }
91 DomTreeNodeBase<NodeT> *addChild(DomTreeNodeBase<NodeT> *C) {
92 Children.push_back(C);
96 size_t getNumChildren() const {
97 return Children.size();
100 void clearAllChildren() {
104 bool compare(DomTreeNodeBase<NodeT> *Other) {
105 if (getNumChildren() != Other->getNumChildren())
108 SmallPtrSet<NodeT *, 4> OtherChildren;
109 for (iterator I = Other->begin(), E = Other->end(); I != E; ++I) {
110 NodeT *Nd = (*I)->getBlock();
111 OtherChildren.insert(Nd);
114 for (iterator I = begin(), E = end(); I != E; ++I) {
115 NodeT *N = (*I)->getBlock();
116 if (OtherChildren.count(N) == 0)
122 void setIDom(DomTreeNodeBase<NodeT> *NewIDom) {
123 assert(IDom && "No immediate dominator?");
124 if (IDom != NewIDom) {
125 typename std::vector<DomTreeNodeBase<NodeT>*>::iterator I =
126 std::find(IDom->Children.begin(), IDom->Children.end(), this);
127 assert(I != IDom->Children.end() &&
128 "Not in immediate dominator children set!");
129 // I am no longer your child...
130 IDom->Children.erase(I);
132 // Switch to new dominator
134 IDom->Children.push_back(this);
138 /// getDFSNumIn/getDFSNumOut - These are an internal implementation detail, do
140 unsigned getDFSNumIn() const { return DFSNumIn; }
141 unsigned getDFSNumOut() const { return DFSNumOut; }
143 // Return true if this node is dominated by other. Use this only if DFS info
145 bool DominatedBy(const DomTreeNodeBase<NodeT> *other) const {
146 return this->DFSNumIn >= other->DFSNumIn &&
147 this->DFSNumOut <= other->DFSNumOut;
151 EXTERN_TEMPLATE_INSTANTIATION(class DomTreeNodeBase<BasicBlock>);
152 EXTERN_TEMPLATE_INSTANTIATION(class DomTreeNodeBase<MachineBasicBlock>);
154 template<class NodeT>
155 static raw_ostream &operator<<(raw_ostream &o,
156 const DomTreeNodeBase<NodeT> *Node) {
157 if (Node->getBlock())
158 WriteAsOperand(o, Node->getBlock(), false);
160 o << " <<exit node>>";
162 o << " {" << Node->getDFSNumIn() << "," << Node->getDFSNumOut() << "}";
167 template<class NodeT>
168 static void PrintDomTree(const DomTreeNodeBase<NodeT> *N, raw_ostream &o,
170 o.indent(2*Lev) << "[" << Lev << "] " << N;
171 for (typename DomTreeNodeBase<NodeT>::const_iterator I = N->begin(),
172 E = N->end(); I != E; ++I)
173 PrintDomTree<NodeT>(*I, o, Lev+1);
176 typedef DomTreeNodeBase<BasicBlock> DomTreeNode;
178 //===----------------------------------------------------------------------===//
179 /// DominatorTree - Calculate the immediate dominator tree for a function.
182 template<class FuncT, class N>
183 void Calculate(DominatorTreeBase<typename GraphTraits<N>::NodeType>& DT,
186 template<class NodeT>
187 class DominatorTreeBase : public DominatorBase<NodeT> {
189 typedef DenseMap<NodeT*, DomTreeNodeBase<NodeT>*> DomTreeNodeMapType;
190 DomTreeNodeMapType DomTreeNodes;
191 DomTreeNodeBase<NodeT> *RootNode;
194 unsigned int SlowQueries;
195 // Information record used during immediate dominators computation.
202 InfoRec() : DFSNum(0), Parent(0), Semi(0), Label(0) {}
205 DenseMap<NodeT*, NodeT*> IDoms;
207 // Vertex - Map the DFS number to the BasicBlock*
208 std::vector<NodeT*> Vertex;
210 // Info - Collection of information used during the computation of idoms.
211 DenseMap<NodeT*, InfoRec> Info;
214 for (typename DomTreeNodeMapType::iterator I = this->DomTreeNodes.begin(),
215 E = DomTreeNodes.end(); I != E; ++I)
217 DomTreeNodes.clear();
224 // NewBB is split and now it has one successor. Update dominator tree to
225 // reflect this change.
226 template<class N, class GraphT>
227 void Split(DominatorTreeBase<typename GraphT::NodeType>& DT,
228 typename GraphT::NodeType* NewBB) {
229 assert(std::distance(GraphT::child_begin(NewBB),
230 GraphT::child_end(NewBB)) == 1 &&
231 "NewBB should have a single successor!");
232 typename GraphT::NodeType* NewBBSucc = *GraphT::child_begin(NewBB);
234 std::vector<typename GraphT::NodeType*> PredBlocks;
235 typedef GraphTraits<Inverse<N> > InvTraits;
236 for (typename InvTraits::ChildIteratorType PI =
237 InvTraits::child_begin(NewBB),
238 PE = InvTraits::child_end(NewBB); PI != PE; ++PI)
239 PredBlocks.push_back(*PI);
241 assert(!PredBlocks.empty() && "No predblocks?");
243 bool NewBBDominatesNewBBSucc = true;
244 for (typename InvTraits::ChildIteratorType PI =
245 InvTraits::child_begin(NewBBSucc),
246 E = InvTraits::child_end(NewBBSucc); PI != E; ++PI) {
247 typename InvTraits::NodeType *ND = *PI;
248 if (ND != NewBB && !DT.dominates(NewBBSucc, ND) &&
249 DT.isReachableFromEntry(ND)) {
250 NewBBDominatesNewBBSucc = false;
255 // Find NewBB's immediate dominator and create new dominator tree node for
257 NodeT *NewBBIDom = 0;
259 for (i = 0; i < PredBlocks.size(); ++i)
260 if (DT.isReachableFromEntry(PredBlocks[i])) {
261 NewBBIDom = PredBlocks[i];
265 // It's possible that none of the predecessors of NewBB are reachable;
266 // in that case, NewBB itself is unreachable, so nothing needs to be
271 for (i = i + 1; i < PredBlocks.size(); ++i) {
272 if (DT.isReachableFromEntry(PredBlocks[i]))
273 NewBBIDom = DT.findNearestCommonDominator(NewBBIDom, PredBlocks[i]);
276 // Create the new dominator tree node... and set the idom of NewBB.
277 DomTreeNodeBase<NodeT> *NewBBNode = DT.addNewBlock(NewBB, NewBBIDom);
279 // If NewBB strictly dominates other blocks, then it is now the immediate
280 // dominator of NewBBSucc. Update the dominator tree as appropriate.
281 if (NewBBDominatesNewBBSucc) {
282 DomTreeNodeBase<NodeT> *NewBBSuccNode = DT.getNode(NewBBSucc);
283 DT.changeImmediateDominator(NewBBSuccNode, NewBBNode);
288 explicit DominatorTreeBase(bool isPostDom)
289 : DominatorBase<NodeT>(isPostDom), DFSInfoValid(false), SlowQueries(0) {}
290 virtual ~DominatorTreeBase() { reset(); }
292 /// compare - Return false if the other dominator tree base matches this
293 /// dominator tree base. Otherwise return true.
294 bool compare(DominatorTreeBase &Other) const {
296 const DomTreeNodeMapType &OtherDomTreeNodes = Other.DomTreeNodes;
297 if (DomTreeNodes.size() != OtherDomTreeNodes.size())
300 for (typename DomTreeNodeMapType::const_iterator
301 I = this->DomTreeNodes.begin(),
302 E = this->DomTreeNodes.end(); I != E; ++I) {
303 NodeT *BB = I->first;
304 typename DomTreeNodeMapType::const_iterator OI = OtherDomTreeNodes.find(BB);
305 if (OI == OtherDomTreeNodes.end())
308 DomTreeNodeBase<NodeT>* MyNd = I->second;
309 DomTreeNodeBase<NodeT>* OtherNd = OI->second;
311 if (MyNd->compare(OtherNd))
318 virtual void releaseMemory() { reset(); }
320 /// getNode - return the (Post)DominatorTree node for the specified basic
321 /// block. This is the same as using operator[] on this class.
323 inline DomTreeNodeBase<NodeT> *getNode(NodeT *BB) const {
324 return DomTreeNodes.lookup(BB);
327 /// getRootNode - This returns the entry node for the CFG of the function. If
328 /// this tree represents the post-dominance relations for a function, however,
329 /// this root may be a node with the block == NULL. This is the case when
330 /// there are multiple exit nodes from a particular function. Consumers of
331 /// post-dominance information must be capable of dealing with this
334 DomTreeNodeBase<NodeT> *getRootNode() { return RootNode; }
335 const DomTreeNodeBase<NodeT> *getRootNode() const { return RootNode; }
337 /// properlyDominates - Returns true iff this dominates N and this != N.
338 /// Note that this is not a constant time operation!
340 bool properlyDominates(const DomTreeNodeBase<NodeT> *A,
341 const DomTreeNodeBase<NodeT> *B) {
342 if (A == 0 || B == 0)
346 return dominates(A, B);
349 inline bool properlyDominates(const NodeT *A, const NodeT *B) {
353 // Cast away the const qualifiers here. This is ok since
354 // this function doesn't actually return the values returned
356 return dominates(getNode(const_cast<NodeT *>(A)),
357 getNode(const_cast<NodeT *>(B)));
360 bool dominatedBySlowTreeWalk(const DomTreeNodeBase<NodeT> *A,
361 const DomTreeNodeBase<NodeT> *B) const {
362 // A node trivially dominates itself.
366 // An unreachable node is dominated by anything.
367 if (!isReachableFromEntry(B))
370 // And dominates nothing.
371 if (!isReachableFromEntry(A))
374 const DomTreeNodeBase<NodeT> *IDom;
375 while ((IDom = B->getIDom()) != 0 && IDom != A && IDom != B)
376 B = IDom; // Walk up the tree
381 /// isReachableFromEntry - Return true if A is dominated by the entry
382 /// block of the function containing it.
383 bool isReachableFromEntry(const NodeT* A) const {
384 assert(!this->isPostDominator() &&
385 "This is not implemented for post dominators");
386 return isReachableFromEntry(getNode(const_cast<NodeT *>(A)));
389 inline bool isReachableFromEntry(const DomTreeNodeBase<NodeT> *A) const {
393 /// dominates - Returns true iff A dominates B. Note that this is not a
394 /// constant time operation!
396 inline bool dominates(const DomTreeNodeBase<NodeT> *A,
397 const DomTreeNodeBase<NodeT> *B) {
398 // A node trivially dominates itself.
402 // An unreachable node is dominated by anything.
403 if (!isReachableFromEntry(B))
406 // And dominates nothing.
407 if (!isReachableFromEntry(A))
410 // Compare the result of the tree walk and the dfs numbers, if expensive
411 // checks are enabled.
413 assert((!DFSInfoValid ||
414 (dominatedBySlowTreeWalk(A, B) == B->DominatedBy(A))) &&
415 "Tree walk disagrees with dfs numbers!");
419 return B->DominatedBy(A);
421 // If we end up with too many slow queries, just update the
422 // DFS numbers on the theory that we are going to keep querying.
424 if (SlowQueries > 32) {
426 return B->DominatedBy(A);
429 return dominatedBySlowTreeWalk(A, B);
432 inline bool dominates(const NodeT *A, const NodeT *B) {
436 // Cast away the const qualifiers here. This is ok since
437 // this function doesn't actually return the values returned
439 return dominates(getNode(const_cast<NodeT *>(A)),
440 getNode(const_cast<NodeT *>(B)));
443 NodeT *getRoot() const {
444 assert(this->Roots.size() == 1 && "Should always have entry node!");
445 return this->Roots[0];
448 /// findNearestCommonDominator - Find nearest common dominator basic block
449 /// for basic block A and B. If there is no such block then return NULL.
450 NodeT *findNearestCommonDominator(NodeT *A, NodeT *B) {
451 assert(A->getParent() == B->getParent() &&
452 "Two blocks are not in same function");
454 // If either A or B is a entry block then it is nearest common dominator
455 // (for forward-dominators).
456 if (!this->isPostDominator()) {
457 NodeT &Entry = A->getParent()->front();
458 if (A == &Entry || B == &Entry)
462 // If B dominates A then B is nearest common dominator.
466 // If A dominates B then A is nearest common dominator.
470 DomTreeNodeBase<NodeT> *NodeA = getNode(A);
471 DomTreeNodeBase<NodeT> *NodeB = getNode(B);
473 // Collect NodeA dominators set.
474 SmallPtrSet<DomTreeNodeBase<NodeT>*, 16> NodeADoms;
475 NodeADoms.insert(NodeA);
476 DomTreeNodeBase<NodeT> *IDomA = NodeA->getIDom();
478 NodeADoms.insert(IDomA);
479 IDomA = IDomA->getIDom();
482 // Walk NodeB immediate dominators chain and find common dominator node.
483 DomTreeNodeBase<NodeT> *IDomB = NodeB->getIDom();
485 if (NodeADoms.count(IDomB) != 0)
486 return IDomB->getBlock();
488 IDomB = IDomB->getIDom();
494 const NodeT *findNearestCommonDominator(const NodeT *A, const NodeT *B) {
495 // Cast away the const qualifiers here. This is ok since
496 // const is re-introduced on the return type.
497 return findNearestCommonDominator(const_cast<NodeT *>(A),
498 const_cast<NodeT *>(B));
501 //===--------------------------------------------------------------------===//
502 // API to update (Post)DominatorTree information based on modifications to
505 /// addNewBlock - Add a new node to the dominator tree information. This
506 /// creates a new node as a child of DomBB dominator node,linking it into
507 /// the children list of the immediate dominator.
508 DomTreeNodeBase<NodeT> *addNewBlock(NodeT *BB, NodeT *DomBB) {
509 assert(getNode(BB) == 0 && "Block already in dominator tree!");
510 DomTreeNodeBase<NodeT> *IDomNode = getNode(DomBB);
511 assert(IDomNode && "Not immediate dominator specified for block!");
512 DFSInfoValid = false;
513 return DomTreeNodes[BB] =
514 IDomNode->addChild(new DomTreeNodeBase<NodeT>(BB, IDomNode));
517 /// changeImmediateDominator - This method is used to update the dominator
518 /// tree information when a node's immediate dominator changes.
520 void changeImmediateDominator(DomTreeNodeBase<NodeT> *N,
521 DomTreeNodeBase<NodeT> *NewIDom) {
522 assert(N && NewIDom && "Cannot change null node pointers!");
523 DFSInfoValid = false;
527 void changeImmediateDominator(NodeT *BB, NodeT *NewBB) {
528 changeImmediateDominator(getNode(BB), getNode(NewBB));
531 /// eraseNode - Removes a node from the dominator tree. Block must not
532 /// dominate any other blocks. Removes node from its immediate dominator's
533 /// children list. Deletes dominator node associated with basic block BB.
534 void eraseNode(NodeT *BB) {
535 DomTreeNodeBase<NodeT> *Node = getNode(BB);
536 assert(Node && "Removing node that isn't in dominator tree.");
537 assert(Node->getChildren().empty() && "Node is not a leaf node.");
539 // Remove node from immediate dominator's children list.
540 DomTreeNodeBase<NodeT> *IDom = Node->getIDom();
542 typename std::vector<DomTreeNodeBase<NodeT>*>::iterator I =
543 std::find(IDom->Children.begin(), IDom->Children.end(), Node);
544 assert(I != IDom->Children.end() &&
545 "Not in immediate dominator children set!");
546 // I am no longer your child...
547 IDom->Children.erase(I);
550 DomTreeNodes.erase(BB);
554 /// removeNode - Removes a node from the dominator tree. Block must not
555 /// dominate any other blocks. Invalidates any node pointing to removed
557 void removeNode(NodeT *BB) {
558 assert(getNode(BB) && "Removing node that isn't in dominator tree.");
559 DomTreeNodes.erase(BB);
562 /// splitBlock - BB is split and now it has one successor. Update dominator
563 /// tree to reflect this change.
564 void splitBlock(NodeT* NewBB) {
565 if (this->IsPostDominators)
566 this->Split<Inverse<NodeT*>, GraphTraits<Inverse<NodeT*> > >(*this, NewBB);
568 this->Split<NodeT*, GraphTraits<NodeT*> >(*this, NewBB);
571 /// print - Convert to human readable form
573 void print(raw_ostream &o) const {
574 o << "=============================--------------------------------\n";
575 if (this->isPostDominator())
576 o << "Inorder PostDominator Tree: ";
578 o << "Inorder Dominator Tree: ";
579 if (!this->DFSInfoValid)
580 o << "DFSNumbers invalid: " << SlowQueries << " slow queries.";
583 // The postdom tree can have a null root if there are no returns.
585 PrintDomTree<NodeT>(getRootNode(), o, 1);
589 template<class GraphT>
590 friend typename GraphT::NodeType* Eval(
591 DominatorTreeBase<typename GraphT::NodeType>& DT,
592 typename GraphT::NodeType* V,
593 unsigned LastLinked);
595 template<class GraphT>
596 friend unsigned DFSPass(DominatorTreeBase<typename GraphT::NodeType>& DT,
597 typename GraphT::NodeType* V,
600 template<class FuncT, class N>
601 friend void Calculate(DominatorTreeBase<typename GraphTraits<N>::NodeType>& DT,
604 /// updateDFSNumbers - Assign In and Out numbers to the nodes while walking
605 /// dominator tree in dfs order.
606 void updateDFSNumbers() {
609 SmallVector<std::pair<DomTreeNodeBase<NodeT>*,
610 typename DomTreeNodeBase<NodeT>::iterator>, 32> WorkStack;
612 DomTreeNodeBase<NodeT> *ThisRoot = getRootNode();
617 // Even in the case of multiple exits that form the post dominator root
618 // nodes, do not iterate over all exits, but start from the virtual root
619 // node. Otherwise bbs, that are not post dominated by any exit but by the
620 // virtual root node, will never be assigned a DFS number.
621 WorkStack.push_back(std::make_pair(ThisRoot, ThisRoot->begin()));
622 ThisRoot->DFSNumIn = DFSNum++;
624 while (!WorkStack.empty()) {
625 DomTreeNodeBase<NodeT> *Node = WorkStack.back().first;
626 typename DomTreeNodeBase<NodeT>::iterator ChildIt =
627 WorkStack.back().second;
629 // If we visited all of the children of this node, "recurse" back up the
630 // stack setting the DFOutNum.
631 if (ChildIt == Node->end()) {
632 Node->DFSNumOut = DFSNum++;
633 WorkStack.pop_back();
635 // Otherwise, recursively visit this child.
636 DomTreeNodeBase<NodeT> *Child = *ChildIt;
637 ++WorkStack.back().second;
639 WorkStack.push_back(std::make_pair(Child, Child->begin()));
640 Child->DFSNumIn = DFSNum++;
648 DomTreeNodeBase<NodeT> *getNodeForBlock(NodeT *BB) {
649 if (DomTreeNodeBase<NodeT> *Node = getNode(BB))
652 // Haven't calculated this node yet? Get or calculate the node for the
653 // immediate dominator.
654 NodeT *IDom = getIDom(BB);
656 assert(IDom || this->DomTreeNodes[NULL]);
657 DomTreeNodeBase<NodeT> *IDomNode = getNodeForBlock(IDom);
659 // Add a new tree node for this BasicBlock, and link it as a child of
661 DomTreeNodeBase<NodeT> *C = new DomTreeNodeBase<NodeT>(BB, IDomNode);
662 return this->DomTreeNodes[BB] = IDomNode->addChild(C);
665 inline NodeT *getIDom(NodeT *BB) const {
666 return IDoms.lookup(BB);
669 inline void addRoot(NodeT* BB) {
670 this->Roots.push_back(BB);
674 /// recalculate - compute a dominator tree for the given function
676 void recalculate(FT& F) {
677 typedef GraphTraits<FT*> TraitsTy;
679 this->Vertex.push_back(0);
681 if (!this->IsPostDominators) {
683 NodeT *entry = TraitsTy::getEntryNode(&F);
684 this->Roots.push_back(entry);
685 this->IDoms[entry] = 0;
686 this->DomTreeNodes[entry] = 0;
688 Calculate<FT, NodeT*>(*this, F);
690 // Initialize the roots list
691 for (typename TraitsTy::nodes_iterator I = TraitsTy::nodes_begin(&F),
692 E = TraitsTy::nodes_end(&F); I != E; ++I) {
693 if (std::distance(TraitsTy::child_begin(I),
694 TraitsTy::child_end(I)) == 0)
697 // Prepopulate maps so that we don't get iterator invalidation issues later.
699 this->DomTreeNodes[I] = 0;
702 Calculate<FT, Inverse<NodeT*> >(*this, F);
707 EXTERN_TEMPLATE_INSTANTIATION(class DominatorTreeBase<BasicBlock>);
709 //===-------------------------------------
710 /// DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
711 /// compute a normal dominator tree.
713 class DominatorTree : public FunctionPass {
715 static char ID; // Pass ID, replacement for typeid
716 DominatorTreeBase<BasicBlock>* DT;
718 DominatorTree() : FunctionPass(ID) {
719 initializeDominatorTreePass(*PassRegistry::getPassRegistry());
720 DT = new DominatorTreeBase<BasicBlock>(false);
727 DominatorTreeBase<BasicBlock>& getBase() { return *DT; }
729 /// getRoots - Return the root blocks of the current CFG. This may include
730 /// multiple blocks if we are computing post dominators. For forward
731 /// dominators, this will always be a single block (the entry node).
733 inline const std::vector<BasicBlock*> &getRoots() const {
734 return DT->getRoots();
737 inline BasicBlock *getRoot() const {
738 return DT->getRoot();
741 inline DomTreeNode *getRootNode() const {
742 return DT->getRootNode();
745 /// compare - Return false if the other dominator tree matches this
746 /// dominator tree. Otherwise return true.
747 inline bool compare(DominatorTree &Other) const {
748 DomTreeNode *R = getRootNode();
749 DomTreeNode *OtherR = Other.getRootNode();
751 if (!R || !OtherR || R->getBlock() != OtherR->getBlock())
754 if (DT->compare(Other.getBase()))
760 virtual bool runOnFunction(Function &F);
762 virtual void verifyAnalysis() const;
764 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
765 AU.setPreservesAll();
768 inline bool dominates(const DomTreeNode* A, const DomTreeNode* B) const {
769 return DT->dominates(A, B);
772 inline bool dominates(const BasicBlock* A, const BasicBlock* B) const {
773 return DT->dominates(A, B);
776 // dominates - Return true if Def dominates a use in User. This performs
777 // the special checks necessary if Def and User are in the same basic block.
778 // Note that Def doesn't dominate a use in Def itself!
779 bool dominates(const Instruction *Def, const Instruction *User) const;
780 bool dominates(const Instruction *Def, const BasicBlock *BB) const;
782 bool properlyDominates(const DomTreeNode *A, const DomTreeNode *B) const {
783 return DT->properlyDominates(A, B);
786 bool properlyDominates(const BasicBlock *A, const BasicBlock *B) const {
787 return DT->properlyDominates(A, B);
790 /// findNearestCommonDominator - Find nearest common dominator basic block
791 /// for basic block A and B. If there is no such block then return NULL.
792 inline BasicBlock *findNearestCommonDominator(BasicBlock *A, BasicBlock *B) {
793 return DT->findNearestCommonDominator(A, B);
796 inline const BasicBlock *findNearestCommonDominator(const BasicBlock *A,
797 const BasicBlock *B) {
798 return DT->findNearestCommonDominator(A, B);
801 inline DomTreeNode *operator[](BasicBlock *BB) const {
802 return DT->getNode(BB);
805 /// getNode - return the (Post)DominatorTree node for the specified basic
806 /// block. This is the same as using operator[] on this class.
808 inline DomTreeNode *getNode(BasicBlock *BB) const {
809 return DT->getNode(BB);
812 /// addNewBlock - Add a new node to the dominator tree information. This
813 /// creates a new node as a child of DomBB dominator node,linking it into
814 /// the children list of the immediate dominator.
815 inline DomTreeNode *addNewBlock(BasicBlock *BB, BasicBlock *DomBB) {
816 return DT->addNewBlock(BB, DomBB);
819 /// changeImmediateDominator - This method is used to update the dominator
820 /// tree information when a node's immediate dominator changes.
822 inline void changeImmediateDominator(BasicBlock *N, BasicBlock* NewIDom) {
823 DT->changeImmediateDominator(N, NewIDom);
826 inline void changeImmediateDominator(DomTreeNode *N, DomTreeNode* NewIDom) {
827 DT->changeImmediateDominator(N, NewIDom);
830 /// eraseNode - Removes a node from the dominator tree. Block must not
831 /// dominate any other blocks. Removes node from its immediate dominator's
832 /// children list. Deletes dominator node associated with basic block BB.
833 inline void eraseNode(BasicBlock *BB) {
837 /// splitBlock - BB is split and now it has one successor. Update dominator
838 /// tree to reflect this change.
839 inline void splitBlock(BasicBlock* NewBB) {
840 DT->splitBlock(NewBB);
843 bool isReachableFromEntry(const BasicBlock* A) const {
844 return DT->isReachableFromEntry(A);
848 virtual void releaseMemory() {
852 virtual void print(raw_ostream &OS, const Module* M= 0) const;
855 //===-------------------------------------
856 /// DominatorTree GraphTraits specialization so the DominatorTree can be
857 /// iterable by generic graph iterators.
859 template <> struct GraphTraits<DomTreeNode*> {
860 typedef DomTreeNode NodeType;
861 typedef NodeType::iterator ChildIteratorType;
863 static NodeType *getEntryNode(NodeType *N) {
866 static inline ChildIteratorType child_begin(NodeType *N) {
869 static inline ChildIteratorType child_end(NodeType *N) {
873 typedef df_iterator<DomTreeNode*> nodes_iterator;
875 static nodes_iterator nodes_begin(DomTreeNode *N) {
876 return df_begin(getEntryNode(N));
879 static nodes_iterator nodes_end(DomTreeNode *N) {
880 return df_end(getEntryNode(N));
884 template <> struct GraphTraits<DominatorTree*>
885 : public GraphTraits<DomTreeNode*> {
886 static NodeType *getEntryNode(DominatorTree *DT) {
887 return DT->getRootNode();
890 static nodes_iterator nodes_begin(DominatorTree *N) {
891 return df_begin(getEntryNode(N));
894 static nodes_iterator nodes_end(DominatorTree *N) {
895 return df_end(getEntryNode(N));
900 } // End llvm namespace