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>>";
254 //===----------------------------------------------------------------------===//
255 // DominatorTree Implementation
256 //===----------------------------------------------------------------------===//
258 static RegisterPass<DominatorTree>
259 E("domtree", "Dominator Tree Construction", true);
261 // DominatorTreeBase::reset - Free all of the tree node memory.
263 void DominatorTreeBase::reset() {
264 for (NodeMapType::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I)
270 void DominatorTreeBase::Node::setIDom(Node *NewIDom) {
271 assert(IDom && "No immediate dominator?");
272 if (IDom != NewIDom) {
273 std::vector<Node*>::iterator I =
274 std::find(IDom->Children.begin(), IDom->Children.end(), this);
275 assert(I != IDom->Children.end() &&
276 "Not in immediate dominator children set!");
277 // I am no longer your child...
278 IDom->Children.erase(I);
280 // Switch to new dominator
282 IDom->Children.push_back(this);
286 DominatorTreeBase::Node *DominatorTree::getNodeForBlock(BasicBlock *BB) {
287 Node *&BBNode = Nodes[BB];
288 if (BBNode) return BBNode;
290 // Haven't calculated this node yet? Get or calculate the node for the
291 // immediate dominator.
292 BasicBlock *IDom = getAnalysis<ImmediateDominators>()[BB];
293 Node *IDomNode = getNodeForBlock(IDom);
295 // Add a new tree node for this BasicBlock, and link it as a child of
297 return BBNode = IDomNode->addChild(new Node(BB, IDomNode));
300 void DominatorTree::calculate(const ImmediateDominators &ID) {
301 assert(Roots.size() == 1 && "DominatorTree should have 1 root block!");
302 BasicBlock *Root = Roots[0];
303 Nodes[Root] = RootNode = new Node(Root, 0); // Add a node for the root...
305 Function *F = Root->getParent();
306 // Loop over all of the reachable blocks in the function...
307 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
308 if (BasicBlock *ImmDom = ID.get(I)) { // Reachable block.
309 Node *&BBNode = Nodes[I];
310 if (!BBNode) { // Haven't calculated this node yet?
311 // Get or calculate the node for the immediate dominator
312 Node *IDomNode = getNodeForBlock(ImmDom);
314 // Add a new tree node for this BasicBlock, and link it as a child of
316 BBNode = IDomNode->addChild(new Node(I, IDomNode));
321 static std::ostream &operator<<(std::ostream &o,
322 const DominatorTreeBase::Node *Node) {
323 if (Node->getBlock())
324 WriteAsOperand(o, Node->getBlock(), false);
326 o << " <<exit node>>";
330 static void PrintDomTree(const DominatorTreeBase::Node *N, std::ostream &o,
332 o << std::string(2*Lev, ' ') << "[" << Lev << "] " << N;
333 for (DominatorTreeBase::Node::const_iterator I = N->begin(), E = N->end();
335 PrintDomTree(*I, o, Lev+1);
338 void DominatorTreeBase::print(std::ostream &o, const Module* ) const {
339 o << "=============================--------------------------------\n"
340 << "Inorder Dominator Tree:\n";
341 PrintDomTree(getRootNode(), o, 1);
345 //===----------------------------------------------------------------------===//
346 // DominanceFrontier Implementation
347 //===----------------------------------------------------------------------===//
349 static RegisterPass<DominanceFrontier>
350 G("domfrontier", "Dominance Frontier Construction", true);
352 const DominanceFrontier::DomSetType &
353 DominanceFrontier::calculate(const DominatorTree &DT,
354 const DominatorTree::Node *Node) {
355 BasicBlock *BB = Node->getBlock();
356 DomSetType *Result = NULL;
358 std::vector<DFCalculateWorkObject> workList;
359 SmallPtrSet<BasicBlock *, 32> visited;
361 workList.push_back(DFCalculateWorkObject(BB, NULL, Node, NULL));
363 DFCalculateWorkObject *currentW = &workList.back();
364 assert (currentW && "Missing work object.");
366 BasicBlock *currentBB = currentW->currentBB;
367 BasicBlock *parentBB = currentW->parentBB;
368 const DominatorTree::Node *currentNode = currentW->Node;
369 const DominatorTree::Node *parentNode = currentW->parentNode;
370 assert (currentBB && "Invalid work object. Missing current Basic Block");
371 assert (currentNode && "Invalid work object. Missing current Node");
372 DomSetType &S = Frontiers[currentBB];
374 // Visit each block only once.
375 if (visited.count(currentBB) == 0) {
376 visited.insert(currentBB);
378 // Loop over CFG successors to calculate DFlocal[currentNode]
379 for (succ_iterator SI = succ_begin(currentBB), SE = succ_end(currentBB);
381 // Does Node immediately dominate this successor?
382 if (DT[*SI]->getIDom() != currentNode)
387 // At this point, S is DFlocal. Now we union in DFup's of our children...
388 // Loop through and visit the nodes that Node immediately dominates (Node's
389 // children in the IDomTree)
390 bool visitChild = false;
391 for (DominatorTree::Node::const_iterator NI = currentNode->begin(),
392 NE = currentNode->end(); NI != NE; ++NI) {
393 DominatorTree::Node *IDominee = *NI;
394 BasicBlock *childBB = IDominee->getBlock();
395 if (visited.count(childBB) == 0) {
396 workList.push_back(DFCalculateWorkObject(childBB, currentBB, IDominee, currentNode));
401 // If all children are visited or there is any child then pop this block
402 // from the workList.
410 DomSetType::const_iterator CDFI = S.begin(), CDFE = S.end();
411 DomSetType &parentSet = Frontiers[parentBB];
412 for (; CDFI != CDFE; ++CDFI) {
413 if (!parentNode->properlyDominates(DT[*CDFI]))
414 parentSet.insert(*CDFI);
419 } while (!workList.empty());
425 static std::ostream &operator<<(std::ostream &o,
426 const std::set<BasicBlock*> &BBs) {
427 for (std::set<BasicBlock*>::const_iterator I = BBs.begin(), E = BBs.end();
430 WriteAsOperand(o, *I, false);
432 o << " <<exit node>>";
438 void DominanceFrontierBase::print(std::ostream &o, const Module* ) const {
439 for (const_iterator I = begin(), E = end(); I != E; ++I) {
440 o << " DomFrontier for BB";
442 WriteAsOperand(o, I->first, false);
444 o << " <<exit node>>";
445 o << " is:\t" << I->second << "\n";
449 //===----------------------------------------------------------------------===//
450 // ETOccurrence Implementation
451 //===----------------------------------------------------------------------===//
453 void ETOccurrence::Splay() {
454 ETOccurrence *father;
455 ETOccurrence *grandfather;
463 fatherdepth = Parent->Depth;
464 grandfather = father->Parent;
466 // If we have no grandparent, a single zig or zag will do.
468 setDepthAdd(fatherdepth);
469 MinOccurrence = father->MinOccurrence;
472 // See what we have to rotate
473 if (father->Left == this) {
475 father->setLeft(Right);
478 father->Left->setDepthAdd(occdepth);
481 father->setRight(Left);
484 father->Right->setDepthAdd(occdepth);
486 father->setDepth(-occdepth);
489 father->recomputeMin();
493 // If we have a grandfather, we need to do some
494 // combination of zig and zag.
495 int grandfatherdepth = grandfather->Depth;
497 setDepthAdd(fatherdepth + grandfatherdepth);
498 MinOccurrence = grandfather->MinOccurrence;
499 Min = grandfather->Min;
501 ETOccurrence *greatgrandfather = grandfather->Parent;
503 if (grandfather->Left == father) {
504 if (father->Left == this) {
506 grandfather->setLeft(father->Right);
507 father->setLeft(Right);
509 father->setRight(grandfather);
511 father->setDepth(-occdepth);
514 father->Left->setDepthAdd(occdepth);
516 grandfather->setDepth(-fatherdepth);
517 if (grandfather->Left)
518 grandfather->Left->setDepthAdd(fatherdepth);
521 grandfather->setLeft(Right);
522 father->setRight(Left);
524 setRight(grandfather);
526 father->setDepth(-occdepth);
528 father->Right->setDepthAdd(occdepth);
529 grandfather->setDepth(-occdepth - fatherdepth);
530 if (grandfather->Left)
531 grandfather->Left->setDepthAdd(occdepth + fatherdepth);
534 if (father->Left == this) {
536 grandfather->setRight(Left);
537 father->setLeft(Right);
538 setLeft(grandfather);
541 father->setDepth(-occdepth);
543 father->Left->setDepthAdd(occdepth);
544 grandfather->setDepth(-occdepth - fatherdepth);
545 if (grandfather->Right)
546 grandfather->Right->setDepthAdd(occdepth + fatherdepth);
548 grandfather->setRight(father->Left);
549 father->setRight(Left);
551 father->setLeft(grandfather);
553 father->setDepth(-occdepth);
555 father->Right->setDepthAdd(occdepth);
556 grandfather->setDepth(-fatherdepth);
557 if (grandfather->Right)
558 grandfather->Right->setDepthAdd(fatherdepth);
562 // Might need one more rotate depending on greatgrandfather.
563 setParent(greatgrandfather);
564 if (greatgrandfather) {
565 if (greatgrandfather->Left == grandfather)
566 greatgrandfather->Left = this;
568 greatgrandfather->Right = this;
571 grandfather->recomputeMin();
572 father->recomputeMin();
576 //===----------------------------------------------------------------------===//
577 // ETNode implementation
578 //===----------------------------------------------------------------------===//
580 void ETNode::Split() {
581 ETOccurrence *right, *left;
582 ETOccurrence *rightmost = RightmostOcc;
583 ETOccurrence *parent;
585 // Update the occurrence tree first.
586 RightmostOcc->Splay();
588 // Find the leftmost occurrence in the rightmost subtree, then splay
590 for (right = rightmost->Right; right->Left; right = right->Left);
595 right->Left->Parent = NULL;
601 parent->Right->Parent = NULL;
603 right->setLeft(left);
605 right->recomputeMin();
608 rightmost->Depth = 0;
613 // Now update *our* tree
615 if (Father->Son == this)
618 if (Father->Son == this)
628 void ETNode::setFather(ETNode *NewFather) {
629 ETOccurrence *rightmost;
630 ETOccurrence *leftpart;
631 ETOccurrence *NewFatherOcc;
634 // First update the path in the splay tree
635 NewFatherOcc = new ETOccurrence(NewFather);
637 rightmost = NewFather->RightmostOcc;
640 leftpart = rightmost->Left;
645 NewFatherOcc->setLeft(leftpart);
646 NewFatherOcc->setRight(temp);
650 NewFatherOcc->recomputeMin();
652 rightmost->setLeft(NewFatherOcc);
654 if (NewFatherOcc->Min + rightmost->Depth < rightmost->Min) {
655 rightmost->Min = NewFatherOcc->Min + rightmost->Depth;
656 rightmost->MinOccurrence = NewFatherOcc->MinOccurrence;
660 ParentOcc = NewFatherOcc;
682 bool ETNode::Below(ETNode *other) {
683 ETOccurrence *up = other->RightmostOcc;
684 ETOccurrence *down = RightmostOcc;
691 ETOccurrence *left, *right;
701 right->Parent = NULL;
705 if (left == down || left->Parent != NULL) {
712 // If the two occurrences are in different trees, put things
713 // back the way they were.
714 if (right && right->Parent != NULL)
721 if (down->Depth <= 0)
724 return !down->Right || down->Right->Min + down->Depth >= 0;
727 ETNode *ETNode::NCA(ETNode *other) {
728 ETOccurrence *occ1 = RightmostOcc;
729 ETOccurrence *occ2 = other->RightmostOcc;
731 ETOccurrence *left, *right, *ret;
732 ETOccurrence *occmin;
746 right->Parent = NULL;
749 if (left == occ2 || (left && left->Parent != NULL)) {
754 right->Parent = occ1;
758 occ1->setRight(occ2);
763 if (occ2->Depth > 0) {
765 mindepth = occ1->Depth;
768 mindepth = occ2->Depth + occ1->Depth;
771 if (ret && ret->Min + occ1->Depth + occ2->Depth < mindepth)
772 return ret->MinOccurrence->OccFor;
774 return occmin->OccFor;
777 void ETNode::assignDFSNumber(int num) {
778 std::vector<ETNode *> workStack;
779 std::set<ETNode *> visitedNodes;
781 workStack.push_back(this);
782 visitedNodes.insert(this);
783 this->DFSNumIn = num++;
785 while (!workStack.empty()) {
786 ETNode *Node = workStack.back();
788 // If this is leaf node then set DFSNumOut and pop the stack
790 Node->DFSNumOut = num++;
791 workStack.pop_back();
795 ETNode *son = Node->Son;
797 // Visit Node->Son first
798 if (visitedNodes.count(son) == 0) {
799 son->DFSNumIn = num++;
800 workStack.push_back(son);
801 visitedNodes.insert(son);
805 bool visitChild = false;
806 // Visit remaining children
807 for (ETNode *s = son->Right; s != son && !visitChild; s = s->Right) {
808 if (visitedNodes.count(s) == 0) {
811 workStack.push_back(s);
812 visitedNodes.insert(s);
817 // If we reach here means all children are visited
818 Node->DFSNumOut = num++;
819 workStack.pop_back();
824 //===----------------------------------------------------------------------===//
825 // ETForest implementation
826 //===----------------------------------------------------------------------===//
828 static RegisterPass<ETForest>
829 D("etforest", "ET Forest Construction", true);
831 void ETForestBase::reset() {
832 for (ETMapType::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I)
837 void ETForestBase::updateDFSNumbers()
840 // Iterate over all nodes in depth first order.
841 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
842 for (df_iterator<BasicBlock*> I = df_begin(Roots[i]),
843 E = df_end(Roots[i]); I != E; ++I) {
845 if (!getNode(BB)->hasFather())
846 getNode(BB)->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 ETNode *ETForest::getNodeForBlock(BasicBlock *BB) {
877 ETNode *&BBNode = Nodes[BB];
878 if (BBNode) return BBNode;
880 // Haven't calculated this node yet? Get or calculate the node for the
881 // immediate dominator.
882 BasicBlock *IDom = getAnalysis<ImmediateDominators>()[BB];
884 // If we are unreachable, we may not have an immediate dominator.
886 return BBNode = new ETNode(BB);
888 ETNode *IDomNode = getNodeForBlock(IDom);
890 // Add a new tree node for this BasicBlock, and link it as a child of
892 BBNode = new ETNode(BB);
893 BBNode->setFather(IDomNode);
898 void ETForest::calculate(const ImmediateDominators &ID) {
899 assert(Roots.size() == 1 && "ETForest should have 1 root block!");
900 BasicBlock *Root = Roots[0];
901 Nodes[Root] = new ETNode(Root); // Add a node for the root
903 Function *F = Root->getParent();
904 // Loop over all of the reachable blocks in the function...
905 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
906 if (BasicBlock *ImmDom = ID.get(I)) { // Reachable block.
907 ETNode *&BBNode = Nodes[I];
908 if (!BBNode) { // Haven't calculated this node yet?
909 // Get or calculate the node for the immediate dominator
910 ETNode *IDomNode = getNodeForBlock(ImmDom);
912 // Add a new ETNode for this BasicBlock, and set it's parent
913 // to it's immediate dominator.
914 BBNode = new ETNode(I);
915 BBNode->setFather(IDomNode);
919 // Make sure we've got nodes around for every block
920 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
921 ETNode *&BBNode = Nodes[I];
923 BBNode = new ETNode(I);
929 //===----------------------------------------------------------------------===//
930 // ETForestBase Implementation
931 //===----------------------------------------------------------------------===//
933 void ETForestBase::addNewBlock(BasicBlock *BB, BasicBlock *IDom) {
934 ETNode *&BBNode = Nodes[BB];
935 assert(!BBNode && "BasicBlock already in ET-Forest");
937 BBNode = new ETNode(BB);
938 BBNode->setFather(getNode(IDom));
939 DFSInfoValid = false;
942 void ETForestBase::setImmediateDominator(BasicBlock *BB, BasicBlock *newIDom) {
943 assert(getNode(BB) && "BasicBlock not in ET-Forest");
944 assert(getNode(newIDom) && "IDom not in ET-Forest");
946 ETNode *Node = getNode(BB);
947 if (Node->hasFather()) {
948 if (Node->getFather()->getData<BasicBlock>() == newIDom)
952 Node->setFather(getNode(newIDom));
956 void ETForestBase::print(std::ostream &o, const Module *) const {
957 o << "=============================--------------------------------\n";
964 o << " up to date\n";
966 Function *F = getRoots()[0]->getParent();
967 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
968 o << " DFS Numbers For Basic Block:";
969 WriteAsOperand(o, I, false);
971 if (ETNode *EN = getNode(I)) {
972 o << "In: " << EN->getDFSNumIn();
973 o << " Out: " << EN->getDFSNumOut() << "\n";
975 o << "No associated ETNode";