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 static RegisterPass<DominatorTree>
62 E("domtree", "Dominator Tree Construction", true);
64 unsigned DominatorTree::DFSPass(BasicBlock *V, InfoRec &VInfo,
66 // This is more understandable as a recursive algorithm, but we can't use the
67 // recursive algorithm due to stack depth issues. Keep it here for
68 // documentation purposes.
73 Vertex.push_back(V); // Vertex[n] = V;
74 //Info[V].Ancestor = 0; // Ancestor[n] = 0
75 //Info[V].Child = 0; // Child[v] = 0
76 VInfo.Size = 1; // Size[v] = 1
78 for (succ_iterator SI = succ_begin(V), E = succ_end(V); SI != E; ++SI) {
79 InfoRec &SuccVInfo = Info[*SI];
80 if (SuccVInfo.Semi == 0) {
82 N = DFSPass(*SI, SuccVInfo, N);
86 std::vector<std::pair<BasicBlock*, unsigned> > Worklist;
87 Worklist.push_back(std::make_pair(V, 0U));
88 while (!Worklist.empty()) {
89 BasicBlock *BB = Worklist.back().first;
90 unsigned NextSucc = Worklist.back().second;
92 // First time we visited this BB?
94 InfoRec &BBInfo = Info[BB];
98 Vertex.push_back(BB); // Vertex[n] = V;
99 //BBInfo[V].Ancestor = 0; // Ancestor[n] = 0
100 //BBInfo[V].Child = 0; // Child[v] = 0
101 BBInfo.Size = 1; // Size[v] = 1
104 // If we are done with this block, remove it from the worklist.
105 if (NextSucc == BB->getTerminator()->getNumSuccessors()) {
110 // Otherwise, increment the successor number for the next time we get to it.
111 ++Worklist.back().second;
113 // Visit the successor next, if it isn't already visited.
114 BasicBlock *Succ = BB->getTerminator()->getSuccessor(NextSucc);
116 InfoRec &SuccVInfo = Info[Succ];
117 if (SuccVInfo.Semi == 0) {
118 SuccVInfo.Parent = BB;
119 Worklist.push_back(std::make_pair(Succ, 0U));
126 void DominatorTree::Compress(BasicBlock *V, InfoRec &VInfo) {
127 BasicBlock *VAncestor = VInfo.Ancestor;
128 InfoRec &VAInfo = Info[VAncestor];
129 if (VAInfo.Ancestor == 0)
132 Compress(VAncestor, VAInfo);
134 BasicBlock *VAncestorLabel = VAInfo.Label;
135 BasicBlock *VLabel = VInfo.Label;
136 if (Info[VAncestorLabel].Semi < Info[VLabel].Semi)
137 VInfo.Label = VAncestorLabel;
139 VInfo.Ancestor = VAInfo.Ancestor;
142 BasicBlock *DominatorTree::Eval(BasicBlock *V) {
143 InfoRec &VInfo = Info[V];
144 #if !BALANCE_IDOM_TREE
145 // Higher-complexity but faster implementation
146 if (VInfo.Ancestor == 0)
151 // Lower-complexity but slower implementation
152 if (VInfo.Ancestor == 0)
155 BasicBlock *VLabel = VInfo.Label;
157 BasicBlock *VAncestorLabel = Info[VInfo.Ancestor].Label;
158 if (Info[VAncestorLabel].Semi >= Info[VLabel].Semi)
161 return VAncestorLabel;
165 void DominatorTree::Link(BasicBlock *V, BasicBlock *W, InfoRec &WInfo){
166 #if !BALANCE_IDOM_TREE
167 // Higher-complexity but faster implementation
170 // Lower-complexity but slower implementation
171 BasicBlock *WLabel = WInfo.Label;
172 unsigned WLabelSemi = Info[WLabel].Semi;
174 InfoRec *SInfo = &Info[S];
176 BasicBlock *SChild = SInfo->Child;
177 InfoRec *SChildInfo = &Info[SChild];
179 while (WLabelSemi < Info[SChildInfo->Label].Semi) {
180 BasicBlock *SChildChild = SChildInfo->Child;
181 if (SInfo->Size+Info[SChildChild].Size >= 2*SChildInfo->Size) {
182 SChildInfo->Ancestor = S;
183 SInfo->Child = SChild = SChildChild;
184 SChildInfo = &Info[SChild];
186 SChildInfo->Size = SInfo->Size;
187 S = SInfo->Ancestor = SChild;
189 SChild = SChildChild;
190 SChildInfo = &Info[SChild];
194 InfoRec &VInfo = Info[V];
195 SInfo->Label = WLabel;
197 assert(V != W && "The optimization here will not work in this case!");
198 unsigned WSize = WInfo.Size;
199 unsigned VSize = (VInfo.Size += WSize);
202 std::swap(S, VInfo.Child);
212 void DominatorTree::calculate(Function& F) {
213 BasicBlock* Root = Roots[0];
215 Nodes[Root] = RootNode = new Node(Root, 0); // Add a node for the root...
219 // Step #1: Number blocks in depth-first order and initialize variables used
220 // in later stages of the algorithm.
222 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
223 N = DFSPass(Roots[i], Info[Roots[i]], 0);
225 for (unsigned i = N; i >= 2; --i) {
226 BasicBlock *W = Vertex[i];
227 InfoRec &WInfo = Info[W];
229 // Step #2: Calculate the semidominators of all vertices
230 for (pred_iterator PI = pred_begin(W), E = pred_end(W); PI != E; ++PI)
231 if (Info.count(*PI)) { // Only if this predecessor is reachable!
232 unsigned SemiU = Info[Eval(*PI)].Semi;
233 if (SemiU < WInfo.Semi)
237 Info[Vertex[WInfo.Semi]].Bucket.push_back(W);
239 BasicBlock *WParent = WInfo.Parent;
240 Link(WParent, W, WInfo);
242 // Step #3: Implicitly define the immediate dominator of vertices
243 std::vector<BasicBlock*> &WParentBucket = Info[WParent].Bucket;
244 while (!WParentBucket.empty()) {
245 BasicBlock *V = WParentBucket.back();
246 WParentBucket.pop_back();
247 BasicBlock *U = Eval(V);
248 IDoms[V] = Info[U].Semi < Info[V].Semi ? U : WParent;
252 // Step #4: Explicitly define the immediate dominator of each vertex
253 for (unsigned i = 2; i <= N; ++i) {
254 BasicBlock *W = Vertex[i];
255 BasicBlock *&WIDom = IDoms[W];
256 if (WIDom != Vertex[Info[W].Semi])
257 WIDom = IDoms[WIDom];
260 // Loop over all of the reachable blocks in the function...
261 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
262 if (BasicBlock *ImmDom = getIDom(I)) { // Reachable block.
263 Node *&BBNode = Nodes[I];
264 if (!BBNode) { // Haven't calculated this node yet?
265 // Get or calculate the node for the immediate dominator
266 Node *IDomNode = getNodeForBlock(ImmDom);
268 // Add a new tree node for this BasicBlock, and link it as a child of
270 BBNode = IDomNode->addChild(new Node(I, IDomNode));
274 // Free temporary memory used to construct idom's
277 std::vector<BasicBlock*>().swap(Vertex);
280 // DominatorTreeBase::reset - Free all of the tree node memory.
282 void DominatorTreeBase::reset() {
283 for (NodeMapType::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I)
291 void DominatorTreeBase::Node::setIDom(Node *NewIDom) {
292 assert(IDom && "No immediate dominator?");
293 if (IDom != NewIDom) {
294 std::vector<Node*>::iterator I =
295 std::find(IDom->Children.begin(), IDom->Children.end(), this);
296 assert(I != IDom->Children.end() &&
297 "Not in immediate dominator children set!");
298 // I am no longer your child...
299 IDom->Children.erase(I);
301 // Switch to new dominator
303 IDom->Children.push_back(this);
307 DominatorTreeBase::Node *DominatorTree::getNodeForBlock(BasicBlock *BB) {
308 Node *&BBNode = Nodes[BB];
309 if (BBNode) return BBNode;
311 // Haven't calculated this node yet? Get or calculate the node for the
312 // immediate dominator.
313 BasicBlock *IDom = getIDom(BB);
314 Node *IDomNode = getNodeForBlock(IDom);
316 // Add a new tree node for this BasicBlock, and link it as a child of
318 return BBNode = IDomNode->addChild(new Node(BB, 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);
344 bool DominatorTree::runOnFunction(Function &F) {
345 reset(); // Reset from the last time we were run...
346 Roots.push_back(&F.getEntryBlock());
351 //===----------------------------------------------------------------------===//
352 // DominanceFrontier Implementation
353 //===----------------------------------------------------------------------===//
355 static RegisterPass<DominanceFrontier>
356 G("domfrontier", "Dominance Frontier Construction", true);
359 class DFCalculateWorkObject {
361 DFCalculateWorkObject(BasicBlock *B, BasicBlock *P,
362 const DominatorTree::Node *N,
363 const DominatorTree::Node *PN)
364 : currentBB(B), parentBB(P), Node(N), parentNode(PN) {}
365 BasicBlock *currentBB;
366 BasicBlock *parentBB;
367 const DominatorTree::Node *Node;
368 const DominatorTree::Node *parentNode;
372 const DominanceFrontier::DomSetType &
373 DominanceFrontier::calculate(const DominatorTree &DT,
374 const DominatorTree::Node *Node) {
375 BasicBlock *BB = Node->getBlock();
376 DomSetType *Result = NULL;
378 std::vector<DFCalculateWorkObject> workList;
379 SmallPtrSet<BasicBlock *, 32> visited;
381 workList.push_back(DFCalculateWorkObject(BB, NULL, Node, NULL));
383 DFCalculateWorkObject *currentW = &workList.back();
384 assert (currentW && "Missing work object.");
386 BasicBlock *currentBB = currentW->currentBB;
387 BasicBlock *parentBB = currentW->parentBB;
388 const DominatorTree::Node *currentNode = currentW->Node;
389 const DominatorTree::Node *parentNode = currentW->parentNode;
390 assert (currentBB && "Invalid work object. Missing current Basic Block");
391 assert (currentNode && "Invalid work object. Missing current Node");
392 DomSetType &S = Frontiers[currentBB];
394 // Visit each block only once.
395 if (visited.count(currentBB) == 0) {
396 visited.insert(currentBB);
398 // Loop over CFG successors to calculate DFlocal[currentNode]
399 for (succ_iterator SI = succ_begin(currentBB), SE = succ_end(currentBB);
401 // Does Node immediately dominate this successor?
402 if (DT[*SI]->getIDom() != currentNode)
407 // At this point, S is DFlocal. Now we union in DFup's of our children...
408 // Loop through and visit the nodes that Node immediately dominates (Node's
409 // children in the IDomTree)
410 bool visitChild = false;
411 for (DominatorTree::Node::const_iterator NI = currentNode->begin(),
412 NE = currentNode->end(); NI != NE; ++NI) {
413 DominatorTree::Node *IDominee = *NI;
414 BasicBlock *childBB = IDominee->getBlock();
415 if (visited.count(childBB) == 0) {
416 workList.push_back(DFCalculateWorkObject(childBB, currentBB,
417 IDominee, currentNode));
422 // If all children are visited or there is any child then pop this block
423 // from the workList.
431 DomSetType::const_iterator CDFI = S.begin(), CDFE = S.end();
432 DomSetType &parentSet = Frontiers[parentBB];
433 for (; CDFI != CDFE; ++CDFI) {
434 if (!parentNode->properlyDominates(DT[*CDFI]))
435 parentSet.insert(*CDFI);
440 } while (!workList.empty());
445 void DominanceFrontierBase::print(std::ostream &o, const Module* ) const {
446 for (const_iterator I = begin(), E = end(); I != E; ++I) {
447 o << " DomFrontier for BB";
449 WriteAsOperand(o, I->first, false);
451 o << " <<exit node>>";
452 o << " is:\t" << I->second << "\n";
456 //===----------------------------------------------------------------------===//
457 // ETOccurrence Implementation
458 //===----------------------------------------------------------------------===//
460 void ETOccurrence::Splay() {
461 ETOccurrence *father;
462 ETOccurrence *grandfather;
470 fatherdepth = Parent->Depth;
471 grandfather = father->Parent;
473 // If we have no grandparent, a single zig or zag will do.
475 setDepthAdd(fatherdepth);
476 MinOccurrence = father->MinOccurrence;
479 // See what we have to rotate
480 if (father->Left == this) {
482 father->setLeft(Right);
485 father->Left->setDepthAdd(occdepth);
488 father->setRight(Left);
491 father->Right->setDepthAdd(occdepth);
493 father->setDepth(-occdepth);
496 father->recomputeMin();
500 // If we have a grandfather, we need to do some
501 // combination of zig and zag.
502 int grandfatherdepth = grandfather->Depth;
504 setDepthAdd(fatherdepth + grandfatherdepth);
505 MinOccurrence = grandfather->MinOccurrence;
506 Min = grandfather->Min;
508 ETOccurrence *greatgrandfather = grandfather->Parent;
510 if (grandfather->Left == father) {
511 if (father->Left == this) {
513 grandfather->setLeft(father->Right);
514 father->setLeft(Right);
516 father->setRight(grandfather);
518 father->setDepth(-occdepth);
521 father->Left->setDepthAdd(occdepth);
523 grandfather->setDepth(-fatherdepth);
524 if (grandfather->Left)
525 grandfather->Left->setDepthAdd(fatherdepth);
528 grandfather->setLeft(Right);
529 father->setRight(Left);
531 setRight(grandfather);
533 father->setDepth(-occdepth);
535 father->Right->setDepthAdd(occdepth);
536 grandfather->setDepth(-occdepth - fatherdepth);
537 if (grandfather->Left)
538 grandfather->Left->setDepthAdd(occdepth + fatherdepth);
541 if (father->Left == this) {
543 grandfather->setRight(Left);
544 father->setLeft(Right);
545 setLeft(grandfather);
548 father->setDepth(-occdepth);
550 father->Left->setDepthAdd(occdepth);
551 grandfather->setDepth(-occdepth - fatherdepth);
552 if (grandfather->Right)
553 grandfather->Right->setDepthAdd(occdepth + fatherdepth);
555 grandfather->setRight(father->Left);
556 father->setRight(Left);
558 father->setLeft(grandfather);
560 father->setDepth(-occdepth);
562 father->Right->setDepthAdd(occdepth);
563 grandfather->setDepth(-fatherdepth);
564 if (grandfather->Right)
565 grandfather->Right->setDepthAdd(fatherdepth);
569 // Might need one more rotate depending on greatgrandfather.
570 setParent(greatgrandfather);
571 if (greatgrandfather) {
572 if (greatgrandfather->Left == grandfather)
573 greatgrandfather->Left = this;
575 greatgrandfather->Right = this;
578 grandfather->recomputeMin();
579 father->recomputeMin();
583 //===----------------------------------------------------------------------===//
584 // ETNode implementation
585 //===----------------------------------------------------------------------===//
587 void ETNode::Split() {
588 ETOccurrence *right, *left;
589 ETOccurrence *rightmost = RightmostOcc;
590 ETOccurrence *parent;
592 // Update the occurrence tree first.
593 RightmostOcc->Splay();
595 // Find the leftmost occurrence in the rightmost subtree, then splay
597 for (right = rightmost->Right; right->Left; right = right->Left);
602 right->Left->Parent = NULL;
608 parent->Right->Parent = NULL;
610 right->setLeft(left);
612 right->recomputeMin();
615 rightmost->Depth = 0;
620 // Now update *our* tree
622 if (Father->Son == this)
625 if (Father->Son == this)
635 void ETNode::setFather(ETNode *NewFather) {
636 ETOccurrence *rightmost;
637 ETOccurrence *leftpart;
638 ETOccurrence *NewFatherOcc;
641 // First update the path in the splay tree
642 NewFatherOcc = new ETOccurrence(NewFather);
644 rightmost = NewFather->RightmostOcc;
647 leftpart = rightmost->Left;
652 NewFatherOcc->setLeft(leftpart);
653 NewFatherOcc->setRight(temp);
657 NewFatherOcc->recomputeMin();
659 rightmost->setLeft(NewFatherOcc);
661 if (NewFatherOcc->Min + rightmost->Depth < rightmost->Min) {
662 rightmost->Min = NewFatherOcc->Min + rightmost->Depth;
663 rightmost->MinOccurrence = NewFatherOcc->MinOccurrence;
667 ParentOcc = NewFatherOcc;
689 bool ETNode::Below(ETNode *other) {
690 ETOccurrence *up = other->RightmostOcc;
691 ETOccurrence *down = RightmostOcc;
698 ETOccurrence *left, *right;
708 right->Parent = NULL;
712 if (left == down || left->Parent != NULL) {
719 // If the two occurrences are in different trees, put things
720 // back the way they were.
721 if (right && right->Parent != NULL)
728 if (down->Depth <= 0)
731 return !down->Right || down->Right->Min + down->Depth >= 0;
734 ETNode *ETNode::NCA(ETNode *other) {
735 ETOccurrence *occ1 = RightmostOcc;
736 ETOccurrence *occ2 = other->RightmostOcc;
738 ETOccurrence *left, *right, *ret;
739 ETOccurrence *occmin;
753 right->Parent = NULL;
756 if (left == occ2 || (left && left->Parent != NULL)) {
761 right->Parent = occ1;
765 occ1->setRight(occ2);
770 if (occ2->Depth > 0) {
772 mindepth = occ1->Depth;
775 mindepth = occ2->Depth + occ1->Depth;
778 if (ret && ret->Min + occ1->Depth + occ2->Depth < mindepth)
779 return ret->MinOccurrence->OccFor;
781 return occmin->OccFor;
784 void ETNode::assignDFSNumber(int num) {
785 std::vector<ETNode *> workStack;
786 std::set<ETNode *> visitedNodes;
788 workStack.push_back(this);
789 visitedNodes.insert(this);
790 this->DFSNumIn = num++;
792 while (!workStack.empty()) {
793 ETNode *Node = workStack.back();
795 // If this is leaf node then set DFSNumOut and pop the stack
797 Node->DFSNumOut = num++;
798 workStack.pop_back();
802 ETNode *son = Node->Son;
804 // Visit Node->Son first
805 if (visitedNodes.count(son) == 0) {
806 son->DFSNumIn = num++;
807 workStack.push_back(son);
808 visitedNodes.insert(son);
812 bool visitChild = false;
813 // Visit remaining children
814 for (ETNode *s = son->Right; s != son && !visitChild; s = s->Right) {
815 if (visitedNodes.count(s) == 0) {
818 workStack.push_back(s);
819 visitedNodes.insert(s);
824 // If we reach here means all children are visited
825 Node->DFSNumOut = num++;
826 workStack.pop_back();
831 //===----------------------------------------------------------------------===//
832 // ETForest implementation
833 //===----------------------------------------------------------------------===//
835 static RegisterPass<ETForest>
836 D("etforest", "ET Forest Construction", true);
838 void ETForestBase::reset() {
839 for (ETMapType::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I)
844 void ETForestBase::updateDFSNumbers()
847 // Iterate over all nodes in depth first order.
848 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
849 for (df_iterator<BasicBlock*> I = df_begin(Roots[i]),
850 E = df_end(Roots[i]); I != E; ++I) {
852 ETNode *ETN = getNode(BB);
853 if (ETN && !ETN->hasFather())
854 ETN->assignDFSNumber(dfsnum);
860 // dominates - Return true if A dominates B. THis performs the
861 // special checks necessary if A and B are in the same basic block.
862 bool ETForestBase::dominates(Instruction *A, Instruction *B) {
863 BasicBlock *BBA = A->getParent(), *BBB = B->getParent();
864 if (BBA != BBB) return dominates(BBA, BBB);
866 // It is not possible to determine dominance between two PHI nodes
867 // based on their ordering.
868 if (isa<PHINode>(A) && isa<PHINode>(B))
871 // Loop through the basic block until we find A or B.
872 BasicBlock::iterator I = BBA->begin();
873 for (; &*I != A && &*I != B; ++I) /*empty*/;
875 if(!IsPostDominators) {
876 // A dominates B if it is found first in the basic block.
879 // A post-dominates B if B is found first in the basic block.
884 /// isReachableFromEntry - Return true if A is dominated by the entry
885 /// block of the function containing it.
886 const bool ETForestBase::isReachableFromEntry(BasicBlock* A) {
887 return dominates(&A->getParent()->getEntryBlock(), A);
890 ETNode *ETForest::getNodeForBlock(BasicBlock *BB) {
891 ETNode *&BBNode = Nodes[BB];
892 if (BBNode) return BBNode;
894 // Haven't calculated this node yet? Get or calculate the node for the
895 // immediate dominator.
896 DominatorTree::Node *node= getAnalysis<DominatorTree>().getNode(BB);
898 // If we are unreachable, we may not have an immediate dominator.
899 if (!node || !node->getIDom())
900 return BBNode = new ETNode(BB);
902 ETNode *IDomNode = getNodeForBlock(node->getIDom()->getBlock());
904 // Add a new tree node for this BasicBlock, and link it as a child of
906 BBNode = new ETNode(BB);
907 BBNode->setFather(IDomNode);
912 void ETForest::calculate(const DominatorTree &DT) {
913 assert(Roots.size() == 1 && "ETForest should have 1 root block!");
914 BasicBlock *Root = Roots[0];
915 Nodes[Root] = new ETNode(Root); // Add a node for the root
917 Function *F = Root->getParent();
918 // Loop over all of the reachable blocks in the function...
919 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
920 DominatorTree::Node* node = DT.getNode(I);
921 if (node && node->getIDom()) { // Reachable block.
922 BasicBlock* ImmDom = node->getIDom()->getBlock();
923 ETNode *&BBNode = Nodes[I];
924 if (!BBNode) { // Haven't calculated this node yet?
925 // Get or calculate the node for the immediate dominator
926 ETNode *IDomNode = getNodeForBlock(ImmDom);
928 // Add a new ETNode for this BasicBlock, and set it's parent
929 // to it's immediate dominator.
930 BBNode = new ETNode(I);
931 BBNode->setFather(IDomNode);
936 // Make sure we've got nodes around for every block
937 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
938 ETNode *&BBNode = Nodes[I];
940 BBNode = new ETNode(I);
946 //===----------------------------------------------------------------------===//
947 // ETForestBase Implementation
948 //===----------------------------------------------------------------------===//
950 void ETForestBase::addNewBlock(BasicBlock *BB, BasicBlock *IDom) {
951 ETNode *&BBNode = Nodes[BB];
952 assert(!BBNode && "BasicBlock already in ET-Forest");
954 BBNode = new ETNode(BB);
955 BBNode->setFather(getNode(IDom));
956 DFSInfoValid = false;
959 void ETForestBase::setImmediateDominator(BasicBlock *BB, BasicBlock *newIDom) {
960 assert(getNode(BB) && "BasicBlock not in ET-Forest");
961 assert(getNode(newIDom) && "IDom not in ET-Forest");
963 ETNode *Node = getNode(BB);
964 if (Node->hasFather()) {
965 if (Node->getFather()->getData<BasicBlock>() == newIDom)
969 Node->setFather(getNode(newIDom));
973 void ETForestBase::print(std::ostream &o, const Module *) const {
974 o << "=============================--------------------------------\n";
981 o << " up to date\n";
983 Function *F = getRoots()[0]->getParent();
984 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
985 o << " DFS Numbers For Basic Block:";
986 WriteAsOperand(o, I, false);
988 if (ETNode *EN = getNode(I)) {
989 o << "In: " << EN->getDFSNumIn();
990 o << " Out: " << EN->getDFSNumOut() << "\n";
992 o << "No associated ETNode";