1 //===- Dominators.cpp - Dominator Calculation -----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements simple dominator construction algorithms for finding
11 // forward dominators. Postdominators are available in libanalysis, but are not
12 // included in libvmcore, because it's not needed. Forward dominators are
13 // needed to support the Verifier pass.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Analysis/Dominators.h"
18 #include "llvm/Support/CFG.h"
19 #include "llvm/Assembly/Writer.h"
20 #include "llvm/ADT/DepthFirstIterator.h"
21 #include "llvm/ADT/SetOperations.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/Instructions.h"
28 static std::ostream &operator<<(std::ostream &o,
29 const std::set<BasicBlock*> &BBs) {
30 for (std::set<BasicBlock*>::const_iterator I = BBs.begin(), E = BBs.end();
33 WriteAsOperand(o, *I, false);
35 o << " <<exit node>>";
40 //===----------------------------------------------------------------------===//
41 // DominatorTree Implementation
42 //===----------------------------------------------------------------------===//
44 // DominatorTree construction - This pass constructs immediate dominator
45 // information for a flow-graph based on the algorithm described in this
48 // A Fast Algorithm for Finding Dominators in a Flowgraph
49 // T. Lengauer & R. Tarjan, ACM TOPLAS July 1979, pgs 121-141.
51 // This implements both the O(n*ack(n)) and the O(n*log(n)) versions of EVAL and
52 // LINK, but it turns out that the theoretically slower O(n*log(n))
53 // implementation is actually faster than the "efficient" algorithm (even for
54 // large CFGs) because the constant overheads are substantially smaller. The
55 // lower-complexity version can be enabled with the following #define:
57 #define BALANCE_IDOM_TREE 0
59 //===----------------------------------------------------------------------===//
61 const int DominatorTree::ID = 0;
62 static RegisterPass<DominatorTree>
63 E("domtree", "Dominator Tree Construction", true);
65 unsigned DominatorTree::DFSPass(BasicBlock *V, InfoRec &VInfo,
67 // This is more understandable as a recursive algorithm, but we can't use the
68 // recursive algorithm due to stack depth issues. Keep it here for
69 // documentation purposes.
74 Vertex.push_back(V); // Vertex[n] = V;
75 //Info[V].Ancestor = 0; // Ancestor[n] = 0
76 //Info[V].Child = 0; // Child[v] = 0
77 VInfo.Size = 1; // Size[v] = 1
79 for (succ_iterator SI = succ_begin(V), E = succ_end(V); SI != E; ++SI) {
80 InfoRec &SuccVInfo = Info[*SI];
81 if (SuccVInfo.Semi == 0) {
83 N = DFSPass(*SI, SuccVInfo, N);
87 std::vector<std::pair<BasicBlock*, unsigned> > Worklist;
88 Worklist.push_back(std::make_pair(V, 0U));
89 while (!Worklist.empty()) {
90 BasicBlock *BB = Worklist.back().first;
91 unsigned NextSucc = Worklist.back().second;
93 // First time we visited this BB?
95 InfoRec &BBInfo = Info[BB];
99 Vertex.push_back(BB); // Vertex[n] = V;
100 //BBInfo[V].Ancestor = 0; // Ancestor[n] = 0
101 //BBInfo[V].Child = 0; // Child[v] = 0
102 BBInfo.Size = 1; // Size[v] = 1
105 // If we are done with this block, remove it from the worklist.
106 if (NextSucc == BB->getTerminator()->getNumSuccessors()) {
111 // Otherwise, increment the successor number for the next time we get to it.
112 ++Worklist.back().second;
114 // Visit the successor next, if it isn't already visited.
115 BasicBlock *Succ = BB->getTerminator()->getSuccessor(NextSucc);
117 InfoRec &SuccVInfo = Info[Succ];
118 if (SuccVInfo.Semi == 0) {
119 SuccVInfo.Parent = BB;
120 Worklist.push_back(std::make_pair(Succ, 0U));
127 void DominatorTree::Compress(BasicBlock *V, InfoRec &VInfo) {
128 BasicBlock *VAncestor = VInfo.Ancestor;
129 InfoRec &VAInfo = Info[VAncestor];
130 if (VAInfo.Ancestor == 0)
133 Compress(VAncestor, VAInfo);
135 BasicBlock *VAncestorLabel = VAInfo.Label;
136 BasicBlock *VLabel = VInfo.Label;
137 if (Info[VAncestorLabel].Semi < Info[VLabel].Semi)
138 VInfo.Label = VAncestorLabel;
140 VInfo.Ancestor = VAInfo.Ancestor;
143 BasicBlock *DominatorTree::Eval(BasicBlock *V) {
144 InfoRec &VInfo = Info[V];
145 #if !BALANCE_IDOM_TREE
146 // Higher-complexity but faster implementation
147 if (VInfo.Ancestor == 0)
152 // Lower-complexity but slower implementation
153 if (VInfo.Ancestor == 0)
156 BasicBlock *VLabel = VInfo.Label;
158 BasicBlock *VAncestorLabel = Info[VInfo.Ancestor].Label;
159 if (Info[VAncestorLabel].Semi >= Info[VLabel].Semi)
162 return VAncestorLabel;
166 void DominatorTree::Link(BasicBlock *V, BasicBlock *W, InfoRec &WInfo){
167 #if !BALANCE_IDOM_TREE
168 // Higher-complexity but faster implementation
171 // Lower-complexity but slower implementation
172 BasicBlock *WLabel = WInfo.Label;
173 unsigned WLabelSemi = Info[WLabel].Semi;
175 InfoRec *SInfo = &Info[S];
177 BasicBlock *SChild = SInfo->Child;
178 InfoRec *SChildInfo = &Info[SChild];
180 while (WLabelSemi < Info[SChildInfo->Label].Semi) {
181 BasicBlock *SChildChild = SChildInfo->Child;
182 if (SInfo->Size+Info[SChildChild].Size >= 2*SChildInfo->Size) {
183 SChildInfo->Ancestor = S;
184 SInfo->Child = SChild = SChildChild;
185 SChildInfo = &Info[SChild];
187 SChildInfo->Size = SInfo->Size;
188 S = SInfo->Ancestor = SChild;
190 SChild = SChildChild;
191 SChildInfo = &Info[SChild];
195 InfoRec &VInfo = Info[V];
196 SInfo->Label = WLabel;
198 assert(V != W && "The optimization here will not work in this case!");
199 unsigned WSize = WInfo.Size;
200 unsigned VSize = (VInfo.Size += WSize);
203 std::swap(S, VInfo.Child);
213 void DominatorTree::calculate(Function& F) {
214 BasicBlock* Root = Roots[0];
216 Nodes[Root] = RootNode = new Node(Root, 0); // Add a node for the root...
220 // Step #1: Number blocks in depth-first order and initialize variables used
221 // in later stages of the algorithm.
223 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
224 N = DFSPass(Roots[i], Info[Roots[i]], 0);
226 for (unsigned i = N; i >= 2; --i) {
227 BasicBlock *W = Vertex[i];
228 InfoRec &WInfo = Info[W];
230 // Step #2: Calculate the semidominators of all vertices
231 for (pred_iterator PI = pred_begin(W), E = pred_end(W); PI != E; ++PI)
232 if (Info.count(*PI)) { // Only if this predecessor is reachable!
233 unsigned SemiU = Info[Eval(*PI)].Semi;
234 if (SemiU < WInfo.Semi)
238 Info[Vertex[WInfo.Semi]].Bucket.push_back(W);
240 BasicBlock *WParent = WInfo.Parent;
241 Link(WParent, W, WInfo);
243 // Step #3: Implicitly define the immediate dominator of vertices
244 std::vector<BasicBlock*> &WParentBucket = Info[WParent].Bucket;
245 while (!WParentBucket.empty()) {
246 BasicBlock *V = WParentBucket.back();
247 WParentBucket.pop_back();
248 BasicBlock *U = Eval(V);
249 IDoms[V] = Info[U].Semi < Info[V].Semi ? U : WParent;
253 // Step #4: Explicitly define the immediate dominator of each vertex
254 for (unsigned i = 2; i <= N; ++i) {
255 BasicBlock *W = Vertex[i];
256 BasicBlock *&WIDom = IDoms[W];
257 if (WIDom != Vertex[Info[W].Semi])
258 WIDom = IDoms[WIDom];
261 // Loop over all of the reachable blocks in the function...
262 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
263 if (BasicBlock *ImmDom = getIDom(I)) { // Reachable block.
264 Node *&BBNode = Nodes[I];
265 if (!BBNode) { // Haven't calculated this node yet?
266 // Get or calculate the node for the immediate dominator
267 Node *IDomNode = getNodeForBlock(ImmDom);
269 // Add a new tree node for this BasicBlock, and link it as a child of
271 BBNode = IDomNode->addChild(new Node(I, IDomNode));
275 // Free temporary memory used to construct idom's
278 std::vector<BasicBlock*>().swap(Vertex);
281 // DominatorTreeBase::reset - Free all of the tree node memory.
283 void DominatorTreeBase::reset() {
284 for (NodeMapType::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I)
293 void DominatorTreeBase::Node::setIDom(Node *NewIDom) {
294 assert(IDom && "No immediate dominator?");
295 if (IDom != NewIDom) {
296 std::vector<Node*>::iterator I =
297 std::find(IDom->Children.begin(), IDom->Children.end(), this);
298 assert(I != IDom->Children.end() &&
299 "Not in immediate dominator children set!");
300 // I am no longer your child...
301 IDom->Children.erase(I);
303 // Switch to new dominator
305 IDom->Children.push_back(this);
309 DominatorTreeBase::Node *DominatorTree::getNodeForBlock(BasicBlock *BB) {
310 Node *&BBNode = Nodes[BB];
311 if (BBNode) return BBNode;
313 // Haven't calculated this node yet? Get or calculate the node for the
314 // immediate dominator.
315 BasicBlock *IDom = getIDom(BB);
316 Node *IDomNode = getNodeForBlock(IDom);
318 // Add a new tree node for this BasicBlock, and link it as a child of
320 return BBNode = IDomNode->addChild(new Node(BB, IDomNode));
323 static std::ostream &operator<<(std::ostream &o,
324 const DominatorTreeBase::Node *Node) {
325 if (Node->getBlock())
326 WriteAsOperand(o, Node->getBlock(), false);
328 o << " <<exit node>>";
332 static void PrintDomTree(const DominatorTreeBase::Node *N, std::ostream &o,
334 o << std::string(2*Lev, ' ') << "[" << Lev << "] " << N;
335 for (DominatorTreeBase::Node::const_iterator I = N->begin(), E = N->end();
337 PrintDomTree(*I, o, Lev+1);
340 void DominatorTreeBase::print(std::ostream &o, const Module* ) const {
341 o << "=============================--------------------------------\n"
342 << "Inorder Dominator Tree:\n";
343 PrintDomTree(getRootNode(), o, 1);
346 bool DominatorTree::runOnFunction(Function &F) {
347 reset(); // Reset from the last time we were run...
348 Roots.push_back(&F.getEntryBlock());
353 //===----------------------------------------------------------------------===//
354 // DominanceFrontier Implementation
355 //===----------------------------------------------------------------------===//
357 const int DominanceFrontier::ID = 0;
358 static RegisterPass<DominanceFrontier>
359 G("domfrontier", "Dominance Frontier Construction", true);
362 class DFCalculateWorkObject {
364 DFCalculateWorkObject(BasicBlock *B, BasicBlock *P,
365 const DominatorTree::Node *N,
366 const DominatorTree::Node *PN)
367 : currentBB(B), parentBB(P), Node(N), parentNode(PN) {}
368 BasicBlock *currentBB;
369 BasicBlock *parentBB;
370 const DominatorTree::Node *Node;
371 const DominatorTree::Node *parentNode;
375 const DominanceFrontier::DomSetType &
376 DominanceFrontier::calculate(const DominatorTree &DT,
377 const DominatorTree::Node *Node) {
378 BasicBlock *BB = Node->getBlock();
379 DomSetType *Result = NULL;
381 std::vector<DFCalculateWorkObject> workList;
382 SmallPtrSet<BasicBlock *, 32> visited;
384 workList.push_back(DFCalculateWorkObject(BB, NULL, Node, NULL));
386 DFCalculateWorkObject *currentW = &workList.back();
387 assert (currentW && "Missing work object.");
389 BasicBlock *currentBB = currentW->currentBB;
390 BasicBlock *parentBB = currentW->parentBB;
391 const DominatorTree::Node *currentNode = currentW->Node;
392 const DominatorTree::Node *parentNode = currentW->parentNode;
393 assert (currentBB && "Invalid work object. Missing current Basic Block");
394 assert (currentNode && "Invalid work object. Missing current Node");
395 DomSetType &S = Frontiers[currentBB];
397 // Visit each block only once.
398 if (visited.count(currentBB) == 0) {
399 visited.insert(currentBB);
401 // Loop over CFG successors to calculate DFlocal[currentNode]
402 for (succ_iterator SI = succ_begin(currentBB), SE = succ_end(currentBB);
404 // Does Node immediately dominate this successor?
405 if (DT[*SI]->getIDom() != currentNode)
410 // At this point, S is DFlocal. Now we union in DFup's of our children...
411 // Loop through and visit the nodes that Node immediately dominates (Node's
412 // children in the IDomTree)
413 bool visitChild = false;
414 for (DominatorTree::Node::const_iterator NI = currentNode->begin(),
415 NE = currentNode->end(); NI != NE; ++NI) {
416 DominatorTree::Node *IDominee = *NI;
417 BasicBlock *childBB = IDominee->getBlock();
418 if (visited.count(childBB) == 0) {
419 workList.push_back(DFCalculateWorkObject(childBB, currentBB,
420 IDominee, currentNode));
425 // If all children are visited or there is any child then pop this block
426 // from the workList.
434 DomSetType::const_iterator CDFI = S.begin(), CDFE = S.end();
435 DomSetType &parentSet = Frontiers[parentBB];
436 for (; CDFI != CDFE; ++CDFI) {
437 if (!parentNode->properlyDominates(DT[*CDFI]))
438 parentSet.insert(*CDFI);
443 } while (!workList.empty());
448 void DominanceFrontierBase::print(std::ostream &o, const Module* ) const {
449 for (const_iterator I = begin(), E = end(); I != E; ++I) {
450 o << " DomFrontier for BB";
452 WriteAsOperand(o, I->first, false);
454 o << " <<exit node>>";
455 o << " is:\t" << I->second << "\n";
459 //===----------------------------------------------------------------------===//
460 // ETOccurrence Implementation
461 //===----------------------------------------------------------------------===//
463 void ETOccurrence::Splay() {
464 ETOccurrence *father;
465 ETOccurrence *grandfather;
473 fatherdepth = Parent->Depth;
474 grandfather = father->Parent;
476 // If we have no grandparent, a single zig or zag will do.
478 setDepthAdd(fatherdepth);
479 MinOccurrence = father->MinOccurrence;
482 // See what we have to rotate
483 if (father->Left == this) {
485 father->setLeft(Right);
488 father->Left->setDepthAdd(occdepth);
491 father->setRight(Left);
494 father->Right->setDepthAdd(occdepth);
496 father->setDepth(-occdepth);
499 father->recomputeMin();
503 // If we have a grandfather, we need to do some
504 // combination of zig and zag.
505 int grandfatherdepth = grandfather->Depth;
507 setDepthAdd(fatherdepth + grandfatherdepth);
508 MinOccurrence = grandfather->MinOccurrence;
509 Min = grandfather->Min;
511 ETOccurrence *greatgrandfather = grandfather->Parent;
513 if (grandfather->Left == father) {
514 if (father->Left == this) {
516 grandfather->setLeft(father->Right);
517 father->setLeft(Right);
519 father->setRight(grandfather);
521 father->setDepth(-occdepth);
524 father->Left->setDepthAdd(occdepth);
526 grandfather->setDepth(-fatherdepth);
527 if (grandfather->Left)
528 grandfather->Left->setDepthAdd(fatherdepth);
531 grandfather->setLeft(Right);
532 father->setRight(Left);
534 setRight(grandfather);
536 father->setDepth(-occdepth);
538 father->Right->setDepthAdd(occdepth);
539 grandfather->setDepth(-occdepth - fatherdepth);
540 if (grandfather->Left)
541 grandfather->Left->setDepthAdd(occdepth + fatherdepth);
544 if (father->Left == this) {
546 grandfather->setRight(Left);
547 father->setLeft(Right);
548 setLeft(grandfather);
551 father->setDepth(-occdepth);
553 father->Left->setDepthAdd(occdepth);
554 grandfather->setDepth(-occdepth - fatherdepth);
555 if (grandfather->Right)
556 grandfather->Right->setDepthAdd(occdepth + fatherdepth);
558 grandfather->setRight(father->Left);
559 father->setRight(Left);
561 father->setLeft(grandfather);
563 father->setDepth(-occdepth);
565 father->Right->setDepthAdd(occdepth);
566 grandfather->setDepth(-fatherdepth);
567 if (grandfather->Right)
568 grandfather->Right->setDepthAdd(fatherdepth);
572 // Might need one more rotate depending on greatgrandfather.
573 setParent(greatgrandfather);
574 if (greatgrandfather) {
575 if (greatgrandfather->Left == grandfather)
576 greatgrandfather->Left = this;
578 greatgrandfather->Right = this;
581 grandfather->recomputeMin();
582 father->recomputeMin();
586 //===----------------------------------------------------------------------===//
587 // ETNode implementation
588 //===----------------------------------------------------------------------===//
590 void ETNode::Split() {
591 ETOccurrence *right, *left;
592 ETOccurrence *rightmost = RightmostOcc;
593 ETOccurrence *parent;
595 // Update the occurrence tree first.
596 RightmostOcc->Splay();
598 // Find the leftmost occurrence in the rightmost subtree, then splay
600 for (right = rightmost->Right; right->Left; right = right->Left);
605 right->Left->Parent = NULL;
611 parent->Right->Parent = NULL;
613 right->setLeft(left);
615 right->recomputeMin();
618 rightmost->Depth = 0;
623 // Now update *our* tree
625 if (Father->Son == this)
628 if (Father->Son == this)
638 void ETNode::setFather(ETNode *NewFather) {
639 ETOccurrence *rightmost;
640 ETOccurrence *leftpart;
641 ETOccurrence *NewFatherOcc;
644 // First update the path in the splay tree
645 NewFatherOcc = new ETOccurrence(NewFather);
647 rightmost = NewFather->RightmostOcc;
650 leftpart = rightmost->Left;
655 NewFatherOcc->setLeft(leftpart);
656 NewFatherOcc->setRight(temp);
660 NewFatherOcc->recomputeMin();
662 rightmost->setLeft(NewFatherOcc);
664 if (NewFatherOcc->Min + rightmost->Depth < rightmost->Min) {
665 rightmost->Min = NewFatherOcc->Min + rightmost->Depth;
666 rightmost->MinOccurrence = NewFatherOcc->MinOccurrence;
670 ParentOcc = NewFatherOcc;
692 bool ETNode::Below(ETNode *other) {
693 ETOccurrence *up = other->RightmostOcc;
694 ETOccurrence *down = RightmostOcc;
701 ETOccurrence *left, *right;
711 right->Parent = NULL;
715 if (left == down || left->Parent != NULL) {
722 // If the two occurrences are in different trees, put things
723 // back the way they were.
724 if (right && right->Parent != NULL)
731 if (down->Depth <= 0)
734 return !down->Right || down->Right->Min + down->Depth >= 0;
737 ETNode *ETNode::NCA(ETNode *other) {
738 ETOccurrence *occ1 = RightmostOcc;
739 ETOccurrence *occ2 = other->RightmostOcc;
741 ETOccurrence *left, *right, *ret;
742 ETOccurrence *occmin;
756 right->Parent = NULL;
759 if (left == occ2 || (left && left->Parent != NULL)) {
764 right->Parent = occ1;
768 occ1->setRight(occ2);
773 if (occ2->Depth > 0) {
775 mindepth = occ1->Depth;
778 mindepth = occ2->Depth + occ1->Depth;
781 if (ret && ret->Min + occ1->Depth + occ2->Depth < mindepth)
782 return ret->MinOccurrence->OccFor;
784 return occmin->OccFor;
787 void ETNode::assignDFSNumber(int num) {
788 std::vector<ETNode *> workStack;
789 std::set<ETNode *> visitedNodes;
791 workStack.push_back(this);
792 visitedNodes.insert(this);
793 this->DFSNumIn = num++;
795 while (!workStack.empty()) {
796 ETNode *Node = workStack.back();
798 // If this is leaf node then set DFSNumOut and pop the stack
800 Node->DFSNumOut = num++;
801 workStack.pop_back();
805 ETNode *son = Node->Son;
807 // Visit Node->Son first
808 if (visitedNodes.count(son) == 0) {
809 son->DFSNumIn = num++;
810 workStack.push_back(son);
811 visitedNodes.insert(son);
815 bool visitChild = false;
816 // Visit remaining children
817 for (ETNode *s = son->Right; s != son && !visitChild; s = s->Right) {
818 if (visitedNodes.count(s) == 0) {
821 workStack.push_back(s);
822 visitedNodes.insert(s);
827 // If we reach here means all children are visited
828 Node->DFSNumOut = num++;
829 workStack.pop_back();
834 //===----------------------------------------------------------------------===//
835 // ETForest implementation
836 //===----------------------------------------------------------------------===//
838 const int ETForest::ID = 0;
839 static RegisterPass<ETForest>
840 D("etforest", "ET Forest Construction", true);
842 void ETForestBase::reset() {
843 for (ETMapType::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I)
848 void ETForestBase::updateDFSNumbers()
851 // Iterate over all nodes in depth first order.
852 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
853 for (df_iterator<BasicBlock*> I = df_begin(Roots[i]),
854 E = df_end(Roots[i]); I != E; ++I) {
856 ETNode *ETN = getNode(BB);
857 if (ETN && !ETN->hasFather())
858 ETN->assignDFSNumber(dfsnum);
864 // dominates - Return true if A dominates B. THis performs the
865 // special checks necessary if A and B are in the same basic block.
866 bool ETForestBase::dominates(Instruction *A, Instruction *B) {
867 BasicBlock *BBA = A->getParent(), *BBB = B->getParent();
868 if (BBA != BBB) return dominates(BBA, BBB);
870 // It is not possible to determine dominance between two PHI nodes
871 // based on their ordering.
872 if (isa<PHINode>(A) && isa<PHINode>(B))
875 // Loop through the basic block until we find A or B.
876 BasicBlock::iterator I = BBA->begin();
877 for (; &*I != A && &*I != B; ++I) /*empty*/;
879 if(!IsPostDominators) {
880 // A dominates B if it is found first in the basic block.
883 // A post-dominates B if B is found first in the basic block.
888 /// isReachableFromEntry - Return true if A is dominated by the entry
889 /// block of the function containing it.
890 const bool ETForestBase::isReachableFromEntry(BasicBlock* A) {
891 return dominates(&A->getParent()->getEntryBlock(), A);
894 ETNode *ETForest::getNodeForBlock(BasicBlock *BB) {
895 ETNode *&BBNode = Nodes[BB];
896 if (BBNode) return BBNode;
898 // Haven't calculated this node yet? Get or calculate the node for the
899 // immediate dominator.
900 DominatorTree::Node *node= getAnalysis<DominatorTree>().getNode(BB);
902 // If we are unreachable, we may not have an immediate dominator.
903 if (!node || !node->getIDom())
904 return BBNode = new ETNode(BB);
906 ETNode *IDomNode = getNodeForBlock(node->getIDom()->getBlock());
908 // Add a new tree node for this BasicBlock, and link it as a child of
910 BBNode = new ETNode(BB);
911 BBNode->setFather(IDomNode);
916 void ETForest::calculate(const DominatorTree &DT) {
917 assert(Roots.size() == 1 && "ETForest should have 1 root block!");
918 BasicBlock *Root = Roots[0];
919 Nodes[Root] = new ETNode(Root); // Add a node for the root
921 Function *F = Root->getParent();
922 // Loop over all of the reachable blocks in the function...
923 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
924 DominatorTree::Node* node = DT.getNode(I);
925 if (node && node->getIDom()) { // Reachable block.
926 BasicBlock* ImmDom = node->getIDom()->getBlock();
927 ETNode *&BBNode = Nodes[I];
928 if (!BBNode) { // Haven't calculated this node yet?
929 // Get or calculate the node for the immediate dominator
930 ETNode *IDomNode = getNodeForBlock(ImmDom);
932 // Add a new ETNode for this BasicBlock, and set it's parent
933 // to it's immediate dominator.
934 BBNode = new ETNode(I);
935 BBNode->setFather(IDomNode);
940 // Make sure we've got nodes around for every block
941 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
942 ETNode *&BBNode = Nodes[I];
944 BBNode = new ETNode(I);
950 //===----------------------------------------------------------------------===//
951 // ETForestBase Implementation
952 //===----------------------------------------------------------------------===//
954 void ETForestBase::addNewBlock(BasicBlock *BB, BasicBlock *IDom) {
955 ETNode *&BBNode = Nodes[BB];
956 assert(!BBNode && "BasicBlock already in ET-Forest");
958 BBNode = new ETNode(BB);
959 BBNode->setFather(getNode(IDom));
960 DFSInfoValid = false;
963 void ETForestBase::setImmediateDominator(BasicBlock *BB, BasicBlock *newIDom) {
964 assert(getNode(BB) && "BasicBlock not in ET-Forest");
965 assert(getNode(newIDom) && "IDom not in ET-Forest");
967 ETNode *Node = getNode(BB);
968 if (Node->hasFather()) {
969 if (Node->getFather()->getData<BasicBlock>() == newIDom)
973 Node->setFather(getNode(newIDom));
977 void ETForestBase::print(std::ostream &o, const Module *) const {
978 o << "=============================--------------------------------\n";
985 o << " up to date\n";
987 Function *F = getRoots()[0]->getParent();
988 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
989 o << " DFS Numbers For Basic Block:";
990 WriteAsOperand(o, I, false);
992 if (ETNode *EN = getNode(I)) {
993 o << "In: " << EN->getDFSNumIn();
994 o << " Out: " << EN->getDFSNumOut() << "\n";
996 o << "No associated ETNode";