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"
27 //===----------------------------------------------------------------------===//
28 // ImmediateDominators Implementation
29 //===----------------------------------------------------------------------===//
31 // Immediate Dominators construction - This pass constructs immediate dominator
32 // information for a flow-graph based on the algorithm described in this
35 // A Fast Algorithm for Finding Dominators in a Flowgraph
36 // T. Lengauer & R. Tarjan, ACM TOPLAS July 1979, pgs 121-141.
38 // This implements both the O(n*ack(n)) and the O(n*log(n)) versions of EVAL and
39 // LINK, but it turns out that the theoretically slower O(n*log(n))
40 // implementation is actually faster than the "efficient" algorithm (even for
41 // large CFGs) because the constant overheads are substantially smaller. The
42 // lower-complexity version can be enabled with the following #define:
44 #define BALANCE_IDOM_TREE 0
46 //===----------------------------------------------------------------------===//
48 static RegisterPass<ImmediateDominators>
49 C("idom", "Immediate Dominators Construction", true);
52 class DFCalculateWorkObject {
54 DFCalculateWorkObject(BasicBlock *B, BasicBlock *P,
55 const DominatorTree::Node *N,
56 const DominatorTree::Node *PN)
57 : currentBB(B), parentBB(P), Node(N), parentNode(PN) {}
58 BasicBlock *currentBB;
60 const DominatorTree::Node *Node;
61 const DominatorTree::Node *parentNode;
64 unsigned ImmediateDominators::DFSPass(BasicBlock *V, InfoRec &VInfo,
69 Vertex.push_back(V); // Vertex[n] = V;
70 //Info[V].Ancestor = 0; // Ancestor[n] = 0
71 //Child[V] = 0; // Child[v] = 0
72 VInfo.Size = 1; // Size[v] = 1
74 for (succ_iterator SI = succ_begin(V), E = succ_end(V); SI != E; ++SI) {
75 InfoRec &SuccVInfo = Info[*SI];
76 if (SuccVInfo.Semi == 0) {
78 N = DFSPass(*SI, SuccVInfo, N);
84 void ImmediateDominators::Compress(BasicBlock *V, InfoRec &VInfo) {
85 BasicBlock *VAncestor = VInfo.Ancestor;
86 InfoRec &VAInfo = Info[VAncestor];
87 if (VAInfo.Ancestor == 0)
90 Compress(VAncestor, VAInfo);
92 BasicBlock *VAncestorLabel = VAInfo.Label;
93 BasicBlock *VLabel = VInfo.Label;
94 if (Info[VAncestorLabel].Semi < Info[VLabel].Semi)
95 VInfo.Label = VAncestorLabel;
97 VInfo.Ancestor = VAInfo.Ancestor;
100 BasicBlock *ImmediateDominators::Eval(BasicBlock *V) {
101 InfoRec &VInfo = Info[V];
102 #if !BALANCE_IDOM_TREE
103 // Higher-complexity but faster implementation
104 if (VInfo.Ancestor == 0)
109 // Lower-complexity but slower implementation
110 if (VInfo.Ancestor == 0)
113 BasicBlock *VLabel = VInfo.Label;
115 BasicBlock *VAncestorLabel = Info[VInfo.Ancestor].Label;
116 if (Info[VAncestorLabel].Semi >= Info[VLabel].Semi)
119 return VAncestorLabel;
123 void ImmediateDominators::Link(BasicBlock *V, BasicBlock *W, InfoRec &WInfo){
124 #if !BALANCE_IDOM_TREE
125 // Higher-complexity but faster implementation
128 // Lower-complexity but slower implementation
129 BasicBlock *WLabel = WInfo.Label;
130 unsigned WLabelSemi = Info[WLabel].Semi;
132 InfoRec *SInfo = &Info[S];
134 BasicBlock *SChild = SInfo->Child;
135 InfoRec *SChildInfo = &Info[SChild];
137 while (WLabelSemi < Info[SChildInfo->Label].Semi) {
138 BasicBlock *SChildChild = SChildInfo->Child;
139 if (SInfo->Size+Info[SChildChild].Size >= 2*SChildInfo->Size) {
140 SChildInfo->Ancestor = S;
141 SInfo->Child = SChild = SChildChild;
142 SChildInfo = &Info[SChild];
144 SChildInfo->Size = SInfo->Size;
145 S = SInfo->Ancestor = SChild;
147 SChild = SChildChild;
148 SChildInfo = &Info[SChild];
152 InfoRec &VInfo = Info[V];
153 SInfo->Label = WLabel;
155 assert(V != W && "The optimization here will not work in this case!");
156 unsigned WSize = WInfo.Size;
157 unsigned VSize = (VInfo.Size += WSize);
160 std::swap(S, VInfo.Child);
172 bool ImmediateDominators::runOnFunction(Function &F) {
173 IDoms.clear(); // Reset from the last time we were run...
174 BasicBlock *Root = &F.getEntryBlock();
176 Roots.push_back(Root);
180 // Step #1: Number blocks in depth-first order and initialize variables used
181 // in later stages of the algorithm.
183 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
184 N = DFSPass(Roots[i], Info[Roots[i]], 0);
186 for (unsigned i = N; i >= 2; --i) {
187 BasicBlock *W = Vertex[i];
188 InfoRec &WInfo = Info[W];
190 // Step #2: Calculate the semidominators of all vertices
191 for (pred_iterator PI = pred_begin(W), E = pred_end(W); PI != E; ++PI)
192 if (Info.count(*PI)) { // Only if this predecessor is reachable!
193 unsigned SemiU = Info[Eval(*PI)].Semi;
194 if (SemiU < WInfo.Semi)
198 Info[Vertex[WInfo.Semi]].Bucket.push_back(W);
200 BasicBlock *WParent = WInfo.Parent;
201 Link(WParent, W, WInfo);
203 // Step #3: Implicitly define the immediate dominator of vertices
204 std::vector<BasicBlock*> &WParentBucket = Info[WParent].Bucket;
205 while (!WParentBucket.empty()) {
206 BasicBlock *V = WParentBucket.back();
207 WParentBucket.pop_back();
208 BasicBlock *U = Eval(V);
209 IDoms[V] = Info[U].Semi < Info[V].Semi ? U : WParent;
213 // Step #4: Explicitly define the immediate dominator of each vertex
214 for (unsigned i = 2; i <= N; ++i) {
215 BasicBlock *W = Vertex[i];
216 BasicBlock *&WIDom = IDoms[W];
217 if (WIDom != Vertex[Info[W].Semi])
218 WIDom = IDoms[WIDom];
221 // Free temporary memory used to construct idom's
223 std::vector<BasicBlock*>().swap(Vertex);
228 /// dominates - Return true if A dominates B.
230 bool ImmediateDominatorsBase::dominates(BasicBlock *A, BasicBlock *B) const {
231 assert(A && B && "Null pointers?");
233 // Walk up the dominator tree from B to determine if A dom B.
239 void ImmediateDominatorsBase::print(std::ostream &o, const Module* ) const {
240 Function *F = getRoots()[0]->getParent();
241 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
242 o << " Immediate Dominator For Basic Block:";
243 WriteAsOperand(o, I, false);
245 if (BasicBlock *ID = get(I))
246 WriteAsOperand(o, ID, false);
248 o << " <<exit node>>";
255 static std::ostream &operator<<(std::ostream &o,
256 const std::set<BasicBlock*> &BBs) {
257 for (std::set<BasicBlock*>::const_iterator I = BBs.begin(), E = BBs.end();
260 WriteAsOperand(o, *I, false);
262 o << " <<exit node>>";
267 //===----------------------------------------------------------------------===//
268 // DominatorTree Implementation
269 //===----------------------------------------------------------------------===//
271 static RegisterPass<DominatorTree>
272 E("domtree", "Dominator Tree Construction", true);
274 // DominatorTreeBase::reset - Free all of the tree node memory.
276 void DominatorTreeBase::reset() {
277 for (NodeMapType::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I)
283 void DominatorTreeBase::Node::setIDom(Node *NewIDom) {
284 assert(IDom && "No immediate dominator?");
285 if (IDom != NewIDom) {
286 std::vector<Node*>::iterator I =
287 std::find(IDom->Children.begin(), IDom->Children.end(), this);
288 assert(I != IDom->Children.end() &&
289 "Not in immediate dominator children set!");
290 // I am no longer your child...
291 IDom->Children.erase(I);
293 // Switch to new dominator
295 IDom->Children.push_back(this);
299 DominatorTreeBase::Node *DominatorTree::getNodeForBlock(BasicBlock *BB) {
300 Node *&BBNode = Nodes[BB];
301 if (BBNode) return BBNode;
303 // Haven't calculated this node yet? Get or calculate the node for the
304 // immediate dominator.
305 BasicBlock *IDom = getAnalysis<ImmediateDominators>()[BB];
306 Node *IDomNode = getNodeForBlock(IDom);
308 // Add a new tree node for this BasicBlock, and link it as a child of
310 return BBNode = IDomNode->addChild(new Node(BB, IDomNode));
313 void DominatorTree::calculate(const ImmediateDominators &ID) {
314 assert(Roots.size() == 1 && "DominatorTree should have 1 root block!");
315 BasicBlock *Root = Roots[0];
316 Nodes[Root] = RootNode = new Node(Root, 0); // Add a node for the root...
318 Function *F = Root->getParent();
319 // Loop over all of the reachable blocks in the function...
320 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
321 if (BasicBlock *ImmDom = ID.get(I)) { // Reachable block.
322 Node *&BBNode = Nodes[I];
323 if (!BBNode) { // Haven't calculated this node yet?
324 // Get or calculate the node for the immediate dominator
325 Node *IDomNode = getNodeForBlock(ImmDom);
327 // Add a new tree node for this BasicBlock, and link it as a child of
329 BBNode = IDomNode->addChild(new Node(I, IDomNode));
334 static std::ostream &operator<<(std::ostream &o,
335 const DominatorTreeBase::Node *Node) {
336 if (Node->getBlock())
337 WriteAsOperand(o, Node->getBlock(), false);
339 o << " <<exit node>>";
343 static void PrintDomTree(const DominatorTreeBase::Node *N, std::ostream &o,
345 o << std::string(2*Lev, ' ') << "[" << Lev << "] " << N;
346 for (DominatorTreeBase::Node::const_iterator I = N->begin(), E = N->end();
348 PrintDomTree(*I, o, Lev+1);
351 void DominatorTreeBase::print(std::ostream &o, const Module* ) const {
352 o << "=============================--------------------------------\n"
353 << "Inorder Dominator Tree:\n";
354 PrintDomTree(getRootNode(), o, 1);
358 //===----------------------------------------------------------------------===//
359 // DominanceFrontier Implementation
360 //===----------------------------------------------------------------------===//
362 static RegisterPass<DominanceFrontier>
363 G("domfrontier", "Dominance Frontier Construction", true);
365 const DominanceFrontier::DomSetType &
366 DominanceFrontier::calculate(const DominatorTree &DT,
367 const DominatorTree::Node *Node) {
368 BasicBlock *BB = Node->getBlock();
369 DomSetType *Result = NULL;
371 std::vector<DFCalculateWorkObject> workList;
372 SmallPtrSet<BasicBlock *, 32> visited;
374 workList.push_back(DFCalculateWorkObject(BB, NULL, Node, NULL));
376 DFCalculateWorkObject *currentW = &workList.back();
377 assert (currentW && "Missing work object.");
379 BasicBlock *currentBB = currentW->currentBB;
380 BasicBlock *parentBB = currentW->parentBB;
381 const DominatorTree::Node *currentNode = currentW->Node;
382 const DominatorTree::Node *parentNode = currentW->parentNode;
383 assert (currentBB && "Invalid work object. Missing current Basic Block");
384 assert (currentNode && "Invalid work object. Missing current Node");
385 DomSetType &S = Frontiers[currentBB];
387 // Visit each block only once.
388 if (visited.count(currentBB) == 0) {
389 visited.insert(currentBB);
391 // Loop over CFG successors to calculate DFlocal[currentNode]
392 for (succ_iterator SI = succ_begin(currentBB), SE = succ_end(currentBB);
394 // Does Node immediately dominate this successor?
395 if (DT[*SI]->getIDom() != currentNode)
400 // At this point, S is DFlocal. Now we union in DFup's of our children...
401 // Loop through and visit the nodes that Node immediately dominates (Node's
402 // children in the IDomTree)
403 bool visitChild = false;
404 for (DominatorTree::Node::const_iterator NI = currentNode->begin(),
405 NE = currentNode->end(); NI != NE; ++NI) {
406 DominatorTree::Node *IDominee = *NI;
407 BasicBlock *childBB = IDominee->getBlock();
408 if (visited.count(childBB) == 0) {
409 workList.push_back(DFCalculateWorkObject(childBB, currentBB, IDominee, currentNode));
414 // If all children are visited or there is any child then pop this block
415 // from the workList.
423 DomSetType::const_iterator CDFI = S.begin(), CDFE = S.end();
424 DomSetType &parentSet = Frontiers[parentBB];
425 for (; CDFI != CDFE; ++CDFI) {
426 if (!parentNode->properlyDominates(DT[*CDFI]))
427 parentSet.insert(*CDFI);
432 } while (!workList.empty());
437 void DominanceFrontierBase::print(std::ostream &o, const Module* ) const {
438 for (const_iterator I = begin(), E = end(); I != E; ++I) {
439 o << " DomFrontier for BB";
441 WriteAsOperand(o, I->first, false);
443 o << " <<exit node>>";
444 o << " is:\t" << I->second << "\n";
448 //===----------------------------------------------------------------------===//
449 // ETOccurrence Implementation
450 //===----------------------------------------------------------------------===//
452 void ETOccurrence::Splay() {
453 ETOccurrence *father;
454 ETOccurrence *grandfather;
462 fatherdepth = Parent->Depth;
463 grandfather = father->Parent;
465 // If we have no grandparent, a single zig or zag will do.
467 setDepthAdd(fatherdepth);
468 MinOccurrence = father->MinOccurrence;
471 // See what we have to rotate
472 if (father->Left == this) {
474 father->setLeft(Right);
477 father->Left->setDepthAdd(occdepth);
480 father->setRight(Left);
483 father->Right->setDepthAdd(occdepth);
485 father->setDepth(-occdepth);
488 father->recomputeMin();
492 // If we have a grandfather, we need to do some
493 // combination of zig and zag.
494 int grandfatherdepth = grandfather->Depth;
496 setDepthAdd(fatherdepth + grandfatherdepth);
497 MinOccurrence = grandfather->MinOccurrence;
498 Min = grandfather->Min;
500 ETOccurrence *greatgrandfather = grandfather->Parent;
502 if (grandfather->Left == father) {
503 if (father->Left == this) {
505 grandfather->setLeft(father->Right);
506 father->setLeft(Right);
508 father->setRight(grandfather);
510 father->setDepth(-occdepth);
513 father->Left->setDepthAdd(occdepth);
515 grandfather->setDepth(-fatherdepth);
516 if (grandfather->Left)
517 grandfather->Left->setDepthAdd(fatherdepth);
520 grandfather->setLeft(Right);
521 father->setRight(Left);
523 setRight(grandfather);
525 father->setDepth(-occdepth);
527 father->Right->setDepthAdd(occdepth);
528 grandfather->setDepth(-occdepth - fatherdepth);
529 if (grandfather->Left)
530 grandfather->Left->setDepthAdd(occdepth + fatherdepth);
533 if (father->Left == this) {
535 grandfather->setRight(Left);
536 father->setLeft(Right);
537 setLeft(grandfather);
540 father->setDepth(-occdepth);
542 father->Left->setDepthAdd(occdepth);
543 grandfather->setDepth(-occdepth - fatherdepth);
544 if (grandfather->Right)
545 grandfather->Right->setDepthAdd(occdepth + fatherdepth);
547 grandfather->setRight(father->Left);
548 father->setRight(Left);
550 father->setLeft(grandfather);
552 father->setDepth(-occdepth);
554 father->Right->setDepthAdd(occdepth);
555 grandfather->setDepth(-fatherdepth);
556 if (grandfather->Right)
557 grandfather->Right->setDepthAdd(fatherdepth);
561 // Might need one more rotate depending on greatgrandfather.
562 setParent(greatgrandfather);
563 if (greatgrandfather) {
564 if (greatgrandfather->Left == grandfather)
565 greatgrandfather->Left = this;
567 greatgrandfather->Right = this;
570 grandfather->recomputeMin();
571 father->recomputeMin();
575 //===----------------------------------------------------------------------===//
576 // ETNode implementation
577 //===----------------------------------------------------------------------===//
579 void ETNode::Split() {
580 ETOccurrence *right, *left;
581 ETOccurrence *rightmost = RightmostOcc;
582 ETOccurrence *parent;
584 // Update the occurrence tree first.
585 RightmostOcc->Splay();
587 // Find the leftmost occurrence in the rightmost subtree, then splay
589 for (right = rightmost->Right; right->Left; right = right->Left);
594 right->Left->Parent = NULL;
600 parent->Right->Parent = NULL;
602 right->setLeft(left);
604 right->recomputeMin();
607 rightmost->Depth = 0;
612 // Now update *our* tree
614 if (Father->Son == this)
617 if (Father->Son == this)
627 void ETNode::setFather(ETNode *NewFather) {
628 ETOccurrence *rightmost;
629 ETOccurrence *leftpart;
630 ETOccurrence *NewFatherOcc;
633 // First update the path in the splay tree
634 NewFatherOcc = new ETOccurrence(NewFather);
636 rightmost = NewFather->RightmostOcc;
639 leftpart = rightmost->Left;
644 NewFatherOcc->setLeft(leftpart);
645 NewFatherOcc->setRight(temp);
649 NewFatherOcc->recomputeMin();
651 rightmost->setLeft(NewFatherOcc);
653 if (NewFatherOcc->Min + rightmost->Depth < rightmost->Min) {
654 rightmost->Min = NewFatherOcc->Min + rightmost->Depth;
655 rightmost->MinOccurrence = NewFatherOcc->MinOccurrence;
659 ParentOcc = NewFatherOcc;
681 bool ETNode::Below(ETNode *other) {
682 ETOccurrence *up = other->RightmostOcc;
683 ETOccurrence *down = RightmostOcc;
690 ETOccurrence *left, *right;
700 right->Parent = NULL;
704 if (left == down || left->Parent != NULL) {
711 // If the two occurrences are in different trees, put things
712 // back the way they were.
713 if (right && right->Parent != NULL)
720 if (down->Depth <= 0)
723 return !down->Right || down->Right->Min + down->Depth >= 0;
726 ETNode *ETNode::NCA(ETNode *other) {
727 ETOccurrence *occ1 = RightmostOcc;
728 ETOccurrence *occ2 = other->RightmostOcc;
730 ETOccurrence *left, *right, *ret;
731 ETOccurrence *occmin;
745 right->Parent = NULL;
748 if (left == occ2 || (left && left->Parent != NULL)) {
753 right->Parent = occ1;
757 occ1->setRight(occ2);
762 if (occ2->Depth > 0) {
764 mindepth = occ1->Depth;
767 mindepth = occ2->Depth + occ1->Depth;
770 if (ret && ret->Min + occ1->Depth + occ2->Depth < mindepth)
771 return ret->MinOccurrence->OccFor;
773 return occmin->OccFor;
776 void ETNode::assignDFSNumber(int num) {
777 std::vector<ETNode *> workStack;
778 std::set<ETNode *> visitedNodes;
780 workStack.push_back(this);
781 visitedNodes.insert(this);
782 this->DFSNumIn = num++;
784 while (!workStack.empty()) {
785 ETNode *Node = workStack.back();
787 // If this is leaf node then set DFSNumOut and pop the stack
789 Node->DFSNumOut = num++;
790 workStack.pop_back();
794 ETNode *son = Node->Son;
796 // Visit Node->Son first
797 if (visitedNodes.count(son) == 0) {
798 son->DFSNumIn = num++;
799 workStack.push_back(son);
800 visitedNodes.insert(son);
804 bool visitChild = false;
805 // Visit remaining children
806 for (ETNode *s = son->Right; s != son && !visitChild; s = s->Right) {
807 if (visitedNodes.count(s) == 0) {
810 workStack.push_back(s);
811 visitedNodes.insert(s);
816 // If we reach here means all children are visited
817 Node->DFSNumOut = num++;
818 workStack.pop_back();
823 //===----------------------------------------------------------------------===//
824 // ETForest implementation
825 //===----------------------------------------------------------------------===//
827 static RegisterPass<ETForest>
828 D("etforest", "ET Forest Construction", true);
830 void ETForestBase::reset() {
831 for (ETMapType::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I)
836 void ETForestBase::updateDFSNumbers()
839 // Iterate over all nodes in depth first order.
840 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
841 for (df_iterator<BasicBlock*> I = df_begin(Roots[i]),
842 E = df_end(Roots[i]); I != E; ++I) {
844 ETNode *ETN = getNode(BB);
845 if (ETN && !ETN->hasFather())
846 ETN->assignDFSNumber(dfsnum);
852 // dominates - Return true if A dominates B. THis performs the
853 // special checks necessary if A and B are in the same basic block.
854 bool ETForestBase::dominates(Instruction *A, Instruction *B) {
855 BasicBlock *BBA = A->getParent(), *BBB = B->getParent();
856 if (BBA != BBB) return dominates(BBA, BBB);
858 // Loop through the basic block until we find A or B.
859 BasicBlock::iterator I = BBA->begin();
860 for (; &*I != A && &*I != B; ++I) /*empty*/;
862 // It is not possible to determine dominance between two PHI nodes
863 // based on their ordering.
864 if (isa<PHINode>(A) && isa<PHINode>(B))
867 if(!IsPostDominators) {
868 // A dominates B if it is found first in the basic block.
871 // A post-dominates B if B is found first in the basic block.
876 /// isReachableFromEntry - Return true if A is dominated by the entry
877 /// block of the function containing it.
878 const bool ETForestBase::isReachableFromEntry(BasicBlock* A) {
879 return dominates(&A->getParent()->getEntryBlock(), A);
882 ETNode *ETForest::getNodeForBlock(BasicBlock *BB) {
883 ETNode *&BBNode = Nodes[BB];
884 if (BBNode) return BBNode;
886 // Haven't calculated this node yet? Get or calculate the node for the
887 // immediate dominator.
888 BasicBlock *IDom = getAnalysis<ImmediateDominators>()[BB];
890 // If we are unreachable, we may not have an immediate dominator.
892 return BBNode = new ETNode(BB);
894 ETNode *IDomNode = getNodeForBlock(IDom);
896 // Add a new tree node for this BasicBlock, and link it as a child of
898 BBNode = new ETNode(BB);
899 BBNode->setFather(IDomNode);
904 void ETForest::calculate(const ImmediateDominators &ID) {
905 assert(Roots.size() == 1 && "ETForest should have 1 root block!");
906 BasicBlock *Root = Roots[0];
907 Nodes[Root] = new ETNode(Root); // Add a node for the root
909 Function *F = Root->getParent();
910 // Loop over all of the reachable blocks in the function...
911 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
912 if (BasicBlock *ImmDom = ID.get(I)) { // Reachable block.
913 ETNode *&BBNode = Nodes[I];
914 if (!BBNode) { // Haven't calculated this node yet?
915 // Get or calculate the node for the immediate dominator
916 ETNode *IDomNode = getNodeForBlock(ImmDom);
918 // Add a new ETNode for this BasicBlock, and set it's parent
919 // to it's immediate dominator.
920 BBNode = new ETNode(I);
921 BBNode->setFather(IDomNode);
925 // Make sure we've got nodes around for every block
926 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
927 ETNode *&BBNode = Nodes[I];
929 BBNode = new ETNode(I);
935 //===----------------------------------------------------------------------===//
936 // ETForestBase Implementation
937 //===----------------------------------------------------------------------===//
939 void ETForestBase::addNewBlock(BasicBlock *BB, BasicBlock *IDom) {
940 ETNode *&BBNode = Nodes[BB];
941 assert(!BBNode && "BasicBlock already in ET-Forest");
943 BBNode = new ETNode(BB);
944 BBNode->setFather(getNode(IDom));
945 DFSInfoValid = false;
948 void ETForestBase::setImmediateDominator(BasicBlock *BB, BasicBlock *newIDom) {
949 assert(getNode(BB) && "BasicBlock not in ET-Forest");
950 assert(getNode(newIDom) && "IDom not in ET-Forest");
952 ETNode *Node = getNode(BB);
953 if (Node->hasFather()) {
954 if (Node->getFather()->getData<BasicBlock>() == newIDom)
958 Node->setFather(getNode(newIDom));
962 void ETForestBase::print(std::ostream &o, const Module *) const {
963 o << "=============================--------------------------------\n";
970 o << " up to date\n";
972 Function *F = getRoots()[0]->getParent();
973 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
974 o << " DFS Numbers For Basic Block:";
975 WriteAsOperand(o, I, false);
977 if (ETNode *EN = getNode(I)) {
978 o << "In: " << EN->getDFSNumIn();
979 o << " Out: " << EN->getDFSNumOut() << "\n";
981 o << "No associated ETNode";