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);
51 unsigned ImmediateDominators::DFSPass(BasicBlock *V, InfoRec &VInfo,
56 Vertex.push_back(V); // Vertex[n] = V;
57 //Info[V].Ancestor = 0; // Ancestor[n] = 0
58 //Child[V] = 0; // Child[v] = 0
59 VInfo.Size = 1; // Size[v] = 1
61 for (succ_iterator SI = succ_begin(V), E = succ_end(V); SI != E; ++SI) {
62 InfoRec &SuccVInfo = Info[*SI];
63 if (SuccVInfo.Semi == 0) {
65 N = DFSPass(*SI, SuccVInfo, N);
71 void ImmediateDominators::Compress(BasicBlock *V, InfoRec &VInfo) {
72 BasicBlock *VAncestor = VInfo.Ancestor;
73 InfoRec &VAInfo = Info[VAncestor];
74 if (VAInfo.Ancestor == 0)
77 Compress(VAncestor, VAInfo);
79 BasicBlock *VAncestorLabel = VAInfo.Label;
80 BasicBlock *VLabel = VInfo.Label;
81 if (Info[VAncestorLabel].Semi < Info[VLabel].Semi)
82 VInfo.Label = VAncestorLabel;
84 VInfo.Ancestor = VAInfo.Ancestor;
87 BasicBlock *ImmediateDominators::Eval(BasicBlock *V) {
88 InfoRec &VInfo = Info[V];
89 #if !BALANCE_IDOM_TREE
90 // Higher-complexity but faster implementation
91 if (VInfo.Ancestor == 0)
96 // Lower-complexity but slower implementation
97 if (VInfo.Ancestor == 0)
100 BasicBlock *VLabel = VInfo.Label;
102 BasicBlock *VAncestorLabel = Info[VInfo.Ancestor].Label;
103 if (Info[VAncestorLabel].Semi >= Info[VLabel].Semi)
106 return VAncestorLabel;
110 void ImmediateDominators::Link(BasicBlock *V, BasicBlock *W, InfoRec &WInfo){
111 #if !BALANCE_IDOM_TREE
112 // Higher-complexity but faster implementation
115 // Lower-complexity but slower implementation
116 BasicBlock *WLabel = WInfo.Label;
117 unsigned WLabelSemi = Info[WLabel].Semi;
119 InfoRec *SInfo = &Info[S];
121 BasicBlock *SChild = SInfo->Child;
122 InfoRec *SChildInfo = &Info[SChild];
124 while (WLabelSemi < Info[SChildInfo->Label].Semi) {
125 BasicBlock *SChildChild = SChildInfo->Child;
126 if (SInfo->Size+Info[SChildChild].Size >= 2*SChildInfo->Size) {
127 SChildInfo->Ancestor = S;
128 SInfo->Child = SChild = SChildChild;
129 SChildInfo = &Info[SChild];
131 SChildInfo->Size = SInfo->Size;
132 S = SInfo->Ancestor = SChild;
134 SChild = SChildChild;
135 SChildInfo = &Info[SChild];
139 InfoRec &VInfo = Info[V];
140 SInfo->Label = WLabel;
142 assert(V != W && "The optimization here will not work in this case!");
143 unsigned WSize = WInfo.Size;
144 unsigned VSize = (VInfo.Size += WSize);
147 std::swap(S, VInfo.Child);
159 bool ImmediateDominators::runOnFunction(Function &F) {
160 IDoms.clear(); // Reset from the last time we were run...
161 BasicBlock *Root = &F.getEntryBlock();
163 Roots.push_back(Root);
167 // Step #1: Number blocks in depth-first order and initialize variables used
168 // in later stages of the algorithm.
170 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
171 N = DFSPass(Roots[i], Info[Roots[i]], 0);
173 for (unsigned i = N; i >= 2; --i) {
174 BasicBlock *W = Vertex[i];
175 InfoRec &WInfo = Info[W];
177 // Step #2: Calculate the semidominators of all vertices
178 for (pred_iterator PI = pred_begin(W), E = pred_end(W); PI != E; ++PI)
179 if (Info.count(*PI)) { // Only if this predecessor is reachable!
180 unsigned SemiU = Info[Eval(*PI)].Semi;
181 if (SemiU < WInfo.Semi)
185 Info[Vertex[WInfo.Semi]].Bucket.push_back(W);
187 BasicBlock *WParent = WInfo.Parent;
188 Link(WParent, W, WInfo);
190 // Step #3: Implicitly define the immediate dominator of vertices
191 std::vector<BasicBlock*> &WParentBucket = Info[WParent].Bucket;
192 while (!WParentBucket.empty()) {
193 BasicBlock *V = WParentBucket.back();
194 WParentBucket.pop_back();
195 BasicBlock *U = Eval(V);
196 IDoms[V] = Info[U].Semi < Info[V].Semi ? U : WParent;
200 // Step #4: Explicitly define the immediate dominator of each vertex
201 for (unsigned i = 2; i <= N; ++i) {
202 BasicBlock *W = Vertex[i];
203 BasicBlock *&WIDom = IDoms[W];
204 if (WIDom != Vertex[Info[W].Semi])
205 WIDom = IDoms[WIDom];
208 // Free temporary memory used to construct idom's
210 std::vector<BasicBlock*>().swap(Vertex);
215 /// dominates - Return true if A dominates B.
217 bool ImmediateDominatorsBase::dominates(BasicBlock *A, BasicBlock *B) const {
218 assert(A && B && "Null pointers?");
220 // Walk up the dominator tree from B to determine if A dom B.
226 void ImmediateDominatorsBase::print(std::ostream &o, const Module* ) const {
227 Function *F = getRoots()[0]->getParent();
228 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
229 o << " Immediate Dominator For Basic Block:";
230 WriteAsOperand(o, I, false);
232 if (BasicBlock *ID = get(I))
233 WriteAsOperand(o, ID, false);
235 o << " <<exit node>>";
242 static std::ostream &operator<<(std::ostream &o,
243 const std::set<BasicBlock*> &BBs) {
244 for (std::set<BasicBlock*>::const_iterator I = BBs.begin(), E = BBs.end();
247 WriteAsOperand(o, *I, false);
249 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);
353 class DFCalculateWorkObject {
355 DFCalculateWorkObject(BasicBlock *B, BasicBlock *P,
356 const DominatorTree::Node *N,
357 const DominatorTree::Node *PN)
358 : currentBB(B), parentBB(P), Node(N), parentNode(PN) {}
359 BasicBlock *currentBB;
360 BasicBlock *parentBB;
361 const DominatorTree::Node *Node;
362 const DominatorTree::Node *parentNode;
366 const DominanceFrontier::DomSetType &
367 DominanceFrontier::calculate(const DominatorTree &DT,
368 const DominatorTree::Node *Node) {
369 BasicBlock *BB = Node->getBlock();
370 DomSetType *Result = NULL;
372 std::vector<DFCalculateWorkObject> workList;
373 SmallPtrSet<BasicBlock *, 32> visited;
375 workList.push_back(DFCalculateWorkObject(BB, NULL, Node, NULL));
377 DFCalculateWorkObject *currentW = &workList.back();
378 assert (currentW && "Missing work object.");
380 BasicBlock *currentBB = currentW->currentBB;
381 BasicBlock *parentBB = currentW->parentBB;
382 const DominatorTree::Node *currentNode = currentW->Node;
383 const DominatorTree::Node *parentNode = currentW->parentNode;
384 assert (currentBB && "Invalid work object. Missing current Basic Block");
385 assert (currentNode && "Invalid work object. Missing current Node");
386 DomSetType &S = Frontiers[currentBB];
388 // Visit each block only once.
389 if (visited.count(currentBB) == 0) {
390 visited.insert(currentBB);
392 // Loop over CFG successors to calculate DFlocal[currentNode]
393 for (succ_iterator SI = succ_begin(currentBB), SE = succ_end(currentBB);
395 // Does Node immediately dominate this successor?
396 if (DT[*SI]->getIDom() != currentNode)
401 // At this point, S is DFlocal. Now we union in DFup's of our children...
402 // Loop through and visit the nodes that Node immediately dominates (Node's
403 // children in the IDomTree)
404 bool visitChild = false;
405 for (DominatorTree::Node::const_iterator NI = currentNode->begin(),
406 NE = currentNode->end(); NI != NE; ++NI) {
407 DominatorTree::Node *IDominee = *NI;
408 BasicBlock *childBB = IDominee->getBlock();
409 if (visited.count(childBB) == 0) {
410 workList.push_back(DFCalculateWorkObject(childBB, currentBB,
411 IDominee, currentNode));
416 // If all children are visited or there is any child then pop this block
417 // from the workList.
425 DomSetType::const_iterator CDFI = S.begin(), CDFE = S.end();
426 DomSetType &parentSet = Frontiers[parentBB];
427 for (; CDFI != CDFE; ++CDFI) {
428 if (!parentNode->properlyDominates(DT[*CDFI]))
429 parentSet.insert(*CDFI);
434 } while (!workList.empty());
439 void DominanceFrontierBase::print(std::ostream &o, const Module* ) const {
440 for (const_iterator I = begin(), E = end(); I != E; ++I) {
441 o << " DomFrontier for BB";
443 WriteAsOperand(o, I->first, false);
445 o << " <<exit node>>";
446 o << " is:\t" << I->second << "\n";
450 //===----------------------------------------------------------------------===//
451 // ETOccurrence Implementation
452 //===----------------------------------------------------------------------===//
454 void ETOccurrence::Splay() {
455 ETOccurrence *father;
456 ETOccurrence *grandfather;
464 fatherdepth = Parent->Depth;
465 grandfather = father->Parent;
467 // If we have no grandparent, a single zig or zag will do.
469 setDepthAdd(fatherdepth);
470 MinOccurrence = father->MinOccurrence;
473 // See what we have to rotate
474 if (father->Left == this) {
476 father->setLeft(Right);
479 father->Left->setDepthAdd(occdepth);
482 father->setRight(Left);
485 father->Right->setDepthAdd(occdepth);
487 father->setDepth(-occdepth);
490 father->recomputeMin();
494 // If we have a grandfather, we need to do some
495 // combination of zig and zag.
496 int grandfatherdepth = grandfather->Depth;
498 setDepthAdd(fatherdepth + grandfatherdepth);
499 MinOccurrence = grandfather->MinOccurrence;
500 Min = grandfather->Min;
502 ETOccurrence *greatgrandfather = grandfather->Parent;
504 if (grandfather->Left == father) {
505 if (father->Left == this) {
507 grandfather->setLeft(father->Right);
508 father->setLeft(Right);
510 father->setRight(grandfather);
512 father->setDepth(-occdepth);
515 father->Left->setDepthAdd(occdepth);
517 grandfather->setDepth(-fatherdepth);
518 if (grandfather->Left)
519 grandfather->Left->setDepthAdd(fatherdepth);
522 grandfather->setLeft(Right);
523 father->setRight(Left);
525 setRight(grandfather);
527 father->setDepth(-occdepth);
529 father->Right->setDepthAdd(occdepth);
530 grandfather->setDepth(-occdepth - fatherdepth);
531 if (grandfather->Left)
532 grandfather->Left->setDepthAdd(occdepth + fatherdepth);
535 if (father->Left == this) {
537 grandfather->setRight(Left);
538 father->setLeft(Right);
539 setLeft(grandfather);
542 father->setDepth(-occdepth);
544 father->Left->setDepthAdd(occdepth);
545 grandfather->setDepth(-occdepth - fatherdepth);
546 if (grandfather->Right)
547 grandfather->Right->setDepthAdd(occdepth + fatherdepth);
549 grandfather->setRight(father->Left);
550 father->setRight(Left);
552 father->setLeft(grandfather);
554 father->setDepth(-occdepth);
556 father->Right->setDepthAdd(occdepth);
557 grandfather->setDepth(-fatherdepth);
558 if (grandfather->Right)
559 grandfather->Right->setDepthAdd(fatherdepth);
563 // Might need one more rotate depending on greatgrandfather.
564 setParent(greatgrandfather);
565 if (greatgrandfather) {
566 if (greatgrandfather->Left == grandfather)
567 greatgrandfather->Left = this;
569 greatgrandfather->Right = this;
572 grandfather->recomputeMin();
573 father->recomputeMin();
577 //===----------------------------------------------------------------------===//
578 // ETNode implementation
579 //===----------------------------------------------------------------------===//
581 void ETNode::Split() {
582 ETOccurrence *right, *left;
583 ETOccurrence *rightmost = RightmostOcc;
584 ETOccurrence *parent;
586 // Update the occurrence tree first.
587 RightmostOcc->Splay();
589 // Find the leftmost occurrence in the rightmost subtree, then splay
591 for (right = rightmost->Right; right->Left; right = right->Left);
596 right->Left->Parent = NULL;
602 parent->Right->Parent = NULL;
604 right->setLeft(left);
606 right->recomputeMin();
609 rightmost->Depth = 0;
614 // Now update *our* tree
616 if (Father->Son == this)
619 if (Father->Son == this)
629 void ETNode::setFather(ETNode *NewFather) {
630 ETOccurrence *rightmost;
631 ETOccurrence *leftpart;
632 ETOccurrence *NewFatherOcc;
635 // First update the path in the splay tree
636 NewFatherOcc = new ETOccurrence(NewFather);
638 rightmost = NewFather->RightmostOcc;
641 leftpart = rightmost->Left;
646 NewFatherOcc->setLeft(leftpart);
647 NewFatherOcc->setRight(temp);
651 NewFatherOcc->recomputeMin();
653 rightmost->setLeft(NewFatherOcc);
655 if (NewFatherOcc->Min + rightmost->Depth < rightmost->Min) {
656 rightmost->Min = NewFatherOcc->Min + rightmost->Depth;
657 rightmost->MinOccurrence = NewFatherOcc->MinOccurrence;
661 ParentOcc = NewFatherOcc;
683 bool ETNode::Below(ETNode *other) {
684 ETOccurrence *up = other->RightmostOcc;
685 ETOccurrence *down = RightmostOcc;
692 ETOccurrence *left, *right;
702 right->Parent = NULL;
706 if (left == down || left->Parent != NULL) {
713 // If the two occurrences are in different trees, put things
714 // back the way they were.
715 if (right && right->Parent != NULL)
722 if (down->Depth <= 0)
725 return !down->Right || down->Right->Min + down->Depth >= 0;
728 ETNode *ETNode::NCA(ETNode *other) {
729 ETOccurrence *occ1 = RightmostOcc;
730 ETOccurrence *occ2 = other->RightmostOcc;
732 ETOccurrence *left, *right, *ret;
733 ETOccurrence *occmin;
747 right->Parent = NULL;
750 if (left == occ2 || (left && left->Parent != NULL)) {
755 right->Parent = occ1;
759 occ1->setRight(occ2);
764 if (occ2->Depth > 0) {
766 mindepth = occ1->Depth;
769 mindepth = occ2->Depth + occ1->Depth;
772 if (ret && ret->Min + occ1->Depth + occ2->Depth < mindepth)
773 return ret->MinOccurrence->OccFor;
775 return occmin->OccFor;
778 void ETNode::assignDFSNumber(int num) {
779 std::vector<ETNode *> workStack;
780 std::set<ETNode *> visitedNodes;
782 workStack.push_back(this);
783 visitedNodes.insert(this);
784 this->DFSNumIn = num++;
786 while (!workStack.empty()) {
787 ETNode *Node = workStack.back();
789 // If this is leaf node then set DFSNumOut and pop the stack
791 Node->DFSNumOut = num++;
792 workStack.pop_back();
796 ETNode *son = Node->Son;
798 // Visit Node->Son first
799 if (visitedNodes.count(son) == 0) {
800 son->DFSNumIn = num++;
801 workStack.push_back(son);
802 visitedNodes.insert(son);
806 bool visitChild = false;
807 // Visit remaining children
808 for (ETNode *s = son->Right; s != son && !visitChild; s = s->Right) {
809 if (visitedNodes.count(s) == 0) {
812 workStack.push_back(s);
813 visitedNodes.insert(s);
818 // If we reach here means all children are visited
819 Node->DFSNumOut = num++;
820 workStack.pop_back();
825 //===----------------------------------------------------------------------===//
826 // ETForest implementation
827 //===----------------------------------------------------------------------===//
829 static RegisterPass<ETForest>
830 D("etforest", "ET Forest Construction", true);
832 void ETForestBase::reset() {
833 for (ETMapType::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I)
838 void ETForestBase::updateDFSNumbers()
841 // Iterate over all nodes in depth first order.
842 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
843 for (df_iterator<BasicBlock*> I = df_begin(Roots[i]),
844 E = df_end(Roots[i]); I != E; ++I) {
846 ETNode *ETN = getNode(BB);
847 if (ETN && !ETN->hasFather())
848 ETN->assignDFSNumber(dfsnum);
854 // dominates - Return true if A dominates B. THis performs the
855 // special checks necessary if A and B are in the same basic block.
856 bool ETForestBase::dominates(Instruction *A, Instruction *B) {
857 BasicBlock *BBA = A->getParent(), *BBB = B->getParent();
858 if (BBA != BBB) return dominates(BBA, BBB);
860 // Loop through the basic block until we find A or B.
861 BasicBlock::iterator I = BBA->begin();
862 for (; &*I != A && &*I != B; ++I) /*empty*/;
864 // It is not possible to determine dominance between two PHI nodes
865 // based on their ordering.
866 if (isa<PHINode>(A) && isa<PHINode>(B))
869 if(!IsPostDominators) {
870 // A dominates B if it is found first in the basic block.
873 // A post-dominates B if B is found first in the basic block.
878 /// isReachableFromEntry - Return true if A is dominated by the entry
879 /// block of the function containing it.
880 const bool ETForestBase::isReachableFromEntry(BasicBlock* A) {
881 return dominates(&A->getParent()->getEntryBlock(), A);
884 ETNode *ETForest::getNodeForBlock(BasicBlock *BB) {
885 ETNode *&BBNode = Nodes[BB];
886 if (BBNode) return BBNode;
888 // Haven't calculated this node yet? Get or calculate the node for the
889 // immediate dominator.
890 BasicBlock *IDom = getAnalysis<ImmediateDominators>()[BB];
892 // If we are unreachable, we may not have an immediate dominator.
894 return BBNode = new ETNode(BB);
896 ETNode *IDomNode = getNodeForBlock(IDom);
898 // Add a new tree node for this BasicBlock, and link it as a child of
900 BBNode = new ETNode(BB);
901 BBNode->setFather(IDomNode);
906 void ETForest::calculate(const ImmediateDominators &ID) {
907 assert(Roots.size() == 1 && "ETForest should have 1 root block!");
908 BasicBlock *Root = Roots[0];
909 Nodes[Root] = new ETNode(Root); // Add a node for the root
911 Function *F = Root->getParent();
912 // Loop over all of the reachable blocks in the function...
913 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
914 if (BasicBlock *ImmDom = ID.get(I)) { // Reachable block.
915 ETNode *&BBNode = Nodes[I];
916 if (!BBNode) { // Haven't calculated this node yet?
917 // Get or calculate the node for the immediate dominator
918 ETNode *IDomNode = getNodeForBlock(ImmDom);
920 // Add a new ETNode for this BasicBlock, and set it's parent
921 // to it's immediate dominator.
922 BBNode = new ETNode(I);
923 BBNode->setFather(IDomNode);
927 // Make sure we've got nodes around for every block
928 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
929 ETNode *&BBNode = Nodes[I];
931 BBNode = new ETNode(I);
937 //===----------------------------------------------------------------------===//
938 // ETForestBase Implementation
939 //===----------------------------------------------------------------------===//
941 void ETForestBase::addNewBlock(BasicBlock *BB, BasicBlock *IDom) {
942 ETNode *&BBNode = Nodes[BB];
943 assert(!BBNode && "BasicBlock already in ET-Forest");
945 BBNode = new ETNode(BB);
946 BBNode->setFather(getNode(IDom));
947 DFSInfoValid = false;
950 void ETForestBase::setImmediateDominator(BasicBlock *BB, BasicBlock *newIDom) {
951 assert(getNode(BB) && "BasicBlock not in ET-Forest");
952 assert(getNode(newIDom) && "IDom not in ET-Forest");
954 ETNode *Node = getNode(BB);
955 if (Node->hasFather()) {
956 if (Node->getFather()->getData<BasicBlock>() == newIDom)
960 Node->setFather(getNode(newIDom));
964 void ETForestBase::print(std::ostream &o, const Module *) const {
965 o << "=============================--------------------------------\n";
972 o << " up to date\n";
974 Function *F = getRoots()[0]->getParent();
975 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
976 o << " DFS Numbers For Basic Block:";
977 WriteAsOperand(o, I, false);
979 if (ETNode *EN = getNode(I)) {
980 o << "In: " << EN->getDFSNumIn();
981 o << " Out: " << EN->getDFSNumOut() << "\n";
983 o << "No associated ETNode";