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"
26 //===----------------------------------------------------------------------===//
27 // ImmediateDominators Implementation
28 //===----------------------------------------------------------------------===//
30 // Immediate Dominators construction - This pass constructs immediate dominator
31 // information for a flow-graph based on the algorithm described in this
34 // A Fast Algorithm for Finding Dominators in a Flowgraph
35 // T. Lengauer & R. Tarjan, ACM TOPLAS July 1979, pgs 121-141.
37 // This implements both the O(n*ack(n)) and the O(n*log(n)) versions of EVAL and
38 // LINK, but it turns out that the theoretically slower O(n*log(n))
39 // implementation is actually faster than the "efficient" algorithm (even for
40 // large CFGs) because the constant overheads are substantially smaller. The
41 // lower-complexity version can be enabled with the following #define:
43 #define BALANCE_IDOM_TREE 0
45 //===----------------------------------------------------------------------===//
47 static RegisterPass<ImmediateDominators>
48 C("idom", "Immediate Dominators Construction", true);
50 unsigned ImmediateDominators::DFSPass(BasicBlock *V, InfoRec &VInfo,
55 Vertex.push_back(V); // Vertex[n] = V;
56 //Info[V].Ancestor = 0; // Ancestor[n] = 0
57 //Child[V] = 0; // Child[v] = 0
58 VInfo.Size = 1; // Size[v] = 1
60 for (succ_iterator SI = succ_begin(V), E = succ_end(V); SI != E; ++SI) {
61 InfoRec &SuccVInfo = Info[*SI];
62 if (SuccVInfo.Semi == 0) {
64 N = DFSPass(*SI, SuccVInfo, N);
70 void ImmediateDominators::Compress(BasicBlock *V, InfoRec &VInfo) {
71 BasicBlock *VAncestor = VInfo.Ancestor;
72 InfoRec &VAInfo = Info[VAncestor];
73 if (VAInfo.Ancestor == 0)
76 Compress(VAncestor, VAInfo);
78 BasicBlock *VAncestorLabel = VAInfo.Label;
79 BasicBlock *VLabel = VInfo.Label;
80 if (Info[VAncestorLabel].Semi < Info[VLabel].Semi)
81 VInfo.Label = VAncestorLabel;
83 VInfo.Ancestor = VAInfo.Ancestor;
86 BasicBlock *ImmediateDominators::Eval(BasicBlock *V) {
87 InfoRec &VInfo = Info[V];
88 #if !BALANCE_IDOM_TREE
89 // Higher-complexity but faster implementation
90 if (VInfo.Ancestor == 0)
95 // Lower-complexity but slower implementation
96 if (VInfo.Ancestor == 0)
99 BasicBlock *VLabel = VInfo.Label;
101 BasicBlock *VAncestorLabel = Info[VInfo.Ancestor].Label;
102 if (Info[VAncestorLabel].Semi >= Info[VLabel].Semi)
105 return VAncestorLabel;
109 void ImmediateDominators::Link(BasicBlock *V, BasicBlock *W, InfoRec &WInfo){
110 #if !BALANCE_IDOM_TREE
111 // Higher-complexity but faster implementation
114 // Lower-complexity but slower implementation
115 BasicBlock *WLabel = WInfo.Label;
116 unsigned WLabelSemi = Info[WLabel].Semi;
118 InfoRec *SInfo = &Info[S];
120 BasicBlock *SChild = SInfo->Child;
121 InfoRec *SChildInfo = &Info[SChild];
123 while (WLabelSemi < Info[SChildInfo->Label].Semi) {
124 BasicBlock *SChildChild = SChildInfo->Child;
125 if (SInfo->Size+Info[SChildChild].Size >= 2*SChildInfo->Size) {
126 SChildInfo->Ancestor = S;
127 SInfo->Child = SChild = SChildChild;
128 SChildInfo = &Info[SChild];
130 SChildInfo->Size = SInfo->Size;
131 S = SInfo->Ancestor = SChild;
133 SChild = SChildChild;
134 SChildInfo = &Info[SChild];
138 InfoRec &VInfo = Info[V];
139 SInfo->Label = WLabel;
141 assert(V != W && "The optimization here will not work in this case!");
142 unsigned WSize = WInfo.Size;
143 unsigned VSize = (VInfo.Size += WSize);
146 std::swap(S, VInfo.Child);
158 bool ImmediateDominators::runOnFunction(Function &F) {
159 IDoms.clear(); // Reset from the last time we were run...
160 BasicBlock *Root = &F.getEntryBlock();
162 Roots.push_back(Root);
166 // Step #1: Number blocks in depth-first order and initialize variables used
167 // in later stages of the algorithm.
169 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
170 N = DFSPass(Roots[i], Info[Roots[i]], 0);
172 for (unsigned i = N; i >= 2; --i) {
173 BasicBlock *W = Vertex[i];
174 InfoRec &WInfo = Info[W];
176 // Step #2: Calculate the semidominators of all vertices
177 for (pred_iterator PI = pred_begin(W), E = pred_end(W); PI != E; ++PI)
178 if (Info.count(*PI)) { // Only if this predecessor is reachable!
179 unsigned SemiU = Info[Eval(*PI)].Semi;
180 if (SemiU < WInfo.Semi)
184 Info[Vertex[WInfo.Semi]].Bucket.push_back(W);
186 BasicBlock *WParent = WInfo.Parent;
187 Link(WParent, W, WInfo);
189 // Step #3: Implicitly define the immediate dominator of vertices
190 std::vector<BasicBlock*> &WParentBucket = Info[WParent].Bucket;
191 while (!WParentBucket.empty()) {
192 BasicBlock *V = WParentBucket.back();
193 WParentBucket.pop_back();
194 BasicBlock *U = Eval(V);
195 IDoms[V] = Info[U].Semi < Info[V].Semi ? U : WParent;
199 // Step #4: Explicitly define the immediate dominator of each vertex
200 for (unsigned i = 2; i <= N; ++i) {
201 BasicBlock *W = Vertex[i];
202 BasicBlock *&WIDom = IDoms[W];
203 if (WIDom != Vertex[Info[W].Semi])
204 WIDom = IDoms[WIDom];
207 // Free temporary memory used to construct idom's
209 std::vector<BasicBlock*>().swap(Vertex);
214 /// dominates - Return true if A dominates B.
216 bool ImmediateDominatorsBase::dominates(BasicBlock *A, BasicBlock *B) const {
217 assert(A && B && "Null pointers?");
219 // Walk up the dominator tree from B to determine if A dom B.
225 void ImmediateDominatorsBase::print(std::ostream &o, const Module* ) const {
226 Function *F = getRoots()[0]->getParent();
227 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
228 o << " Immediate Dominator For Basic Block:";
229 WriteAsOperand(o, I, false);
231 if (BasicBlock *ID = get(I))
232 WriteAsOperand(o, ID, false);
234 o << " <<exit node>>";
242 //===----------------------------------------------------------------------===//
243 // DominatorSet Implementation
244 //===----------------------------------------------------------------------===//
246 static RegisterPass<DominatorSet>
247 B("domset", "Dominator Set Construction", true);
249 // dominates - Return true if A dominates B. This performs the special checks
250 // necessary if A and B are in the same basic block.
252 bool DominatorSetBase::dominates(Instruction *A, Instruction *B) const {
253 BasicBlock *BBA = A->getParent(), *BBB = B->getParent();
254 if (BBA != BBB) return dominates(BBA, BBB);
256 // Loop through the basic block until we find A or B.
257 BasicBlock::iterator I = BBA->begin();
258 for (; &*I != A && &*I != B; ++I) /*empty*/;
260 if(!IsPostDominators) {
261 // A dominates B if it is found first in the basic block.
264 // A post-dominates B if B is found first in the basic block.
270 // runOnFunction - This method calculates the forward dominator sets for the
271 // specified function.
273 bool DominatorSet::runOnFunction(Function &F) {
274 BasicBlock *Root = &F.getEntryBlock();
276 Roots.push_back(Root);
277 assert(pred_begin(Root) == pred_end(Root) &&
278 "Root node has predecessors in function!");
280 ImmediateDominators &ID = getAnalysis<ImmediateDominators>();
282 if (Roots.empty()) return false;
284 // Root nodes only dominate themselves.
285 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
286 Doms[Roots[i]].insert(Roots[i]);
288 // Loop over all of the blocks in the function, calculating dominator sets for
290 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
291 if (BasicBlock *IDom = ID[I]) { // Get idom if block is reachable
292 DomSetType &DS = Doms[I];
293 assert(DS.empty() && "Domset already filled in for this block?");
294 DS.insert(I); // Blocks always dominate themselves
296 // Insert all dominators into the set...
298 // If we have already computed the dominator sets for our immediate
299 // dominator, just use it instead of walking all the way up to the root.
300 DomSetType &IDS = Doms[IDom];
302 DS.insert(IDS.begin(), IDS.end());
310 // Ensure that every basic block has at least an empty set of nodes. This
311 // is important for the case when there is unreachable blocks.
319 static std::ostream &operator<<(std::ostream &o,
320 const std::set<BasicBlock*> &BBs) {
321 for (std::set<BasicBlock*>::const_iterator I = BBs.begin(), E = BBs.end();
324 WriteAsOperand(o, *I, false);
326 o << " <<exit node>>";
331 void DominatorSetBase::print(std::ostream &o, const Module* ) const {
332 for (const_iterator I = begin(), E = end(); I != E; ++I) {
333 o << " DomSet For BB: ";
335 WriteAsOperand(o, I->first, false);
337 o << " <<exit node>>";
338 o << " is:\t" << I->second << "\n";
342 //===----------------------------------------------------------------------===//
343 // DominatorTree Implementation
344 //===----------------------------------------------------------------------===//
346 static RegisterPass<DominatorTree>
347 E("domtree", "Dominator Tree Construction", true);
349 // DominatorTreeBase::reset - Free all of the tree node memory.
351 void DominatorTreeBase::reset() {
352 for (NodeMapType::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I)
358 void DominatorTreeBase::Node::setIDom(Node *NewIDom) {
359 assert(IDom && "No immediate dominator?");
360 if (IDom != NewIDom) {
361 std::vector<Node*>::iterator I =
362 std::find(IDom->Children.begin(), IDom->Children.end(), this);
363 assert(I != IDom->Children.end() &&
364 "Not in immediate dominator children set!");
365 // I am no longer your child...
366 IDom->Children.erase(I);
368 // Switch to new dominator
370 IDom->Children.push_back(this);
374 DominatorTreeBase::Node *DominatorTree::getNodeForBlock(BasicBlock *BB) {
375 Node *&BBNode = Nodes[BB];
376 if (BBNode) return BBNode;
378 // Haven't calculated this node yet? Get or calculate the node for the
379 // immediate dominator.
380 BasicBlock *IDom = getAnalysis<ImmediateDominators>()[BB];
381 Node *IDomNode = getNodeForBlock(IDom);
383 // Add a new tree node for this BasicBlock, and link it as a child of
385 return BBNode = IDomNode->addChild(new Node(BB, IDomNode));
388 void DominatorTree::calculate(const ImmediateDominators &ID) {
389 assert(Roots.size() == 1 && "DominatorTree should have 1 root block!");
390 BasicBlock *Root = Roots[0];
391 Nodes[Root] = RootNode = new Node(Root, 0); // Add a node for the root...
393 Function *F = Root->getParent();
394 // Loop over all of the reachable blocks in the function...
395 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
396 if (BasicBlock *ImmDom = ID.get(I)) { // Reachable block.
397 Node *&BBNode = Nodes[I];
398 if (!BBNode) { // Haven't calculated this node yet?
399 // Get or calculate the node for the immediate dominator
400 Node *IDomNode = getNodeForBlock(ImmDom);
402 // Add a new tree node for this BasicBlock, and link it as a child of
404 BBNode = IDomNode->addChild(new Node(I, IDomNode));
409 static std::ostream &operator<<(std::ostream &o,
410 const DominatorTreeBase::Node *Node) {
411 if (Node->getBlock())
412 WriteAsOperand(o, Node->getBlock(), false);
414 o << " <<exit node>>";
418 static void PrintDomTree(const DominatorTreeBase::Node *N, std::ostream &o,
420 o << std::string(2*Lev, ' ') << "[" << Lev << "] " << N;
421 for (DominatorTreeBase::Node::const_iterator I = N->begin(), E = N->end();
423 PrintDomTree(*I, o, Lev+1);
426 void DominatorTreeBase::print(std::ostream &o, const Module* ) const {
427 o << "=============================--------------------------------\n"
428 << "Inorder Dominator Tree:\n";
429 PrintDomTree(getRootNode(), o, 1);
433 //===----------------------------------------------------------------------===//
434 // DominanceFrontier Implementation
435 //===----------------------------------------------------------------------===//
437 static RegisterPass<DominanceFrontier>
438 G("domfrontier", "Dominance Frontier Construction", true);
440 const DominanceFrontier::DomSetType &
441 DominanceFrontier::calculate(const DominatorTree &DT,
442 const DominatorTree::Node *Node) {
443 // Loop over CFG successors to calculate DFlocal[Node]
444 BasicBlock *BB = Node->getBlock();
445 DomSetType &S = Frontiers[BB]; // The new set to fill in...
447 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB);
449 // Does Node immediately dominate this successor?
450 if (DT[*SI]->getIDom() != Node)
454 // At this point, S is DFlocal. Now we union in DFup's of our children...
455 // Loop through and visit the nodes that Node immediately dominates (Node's
456 // children in the IDomTree)
458 for (DominatorTree::Node::const_iterator NI = Node->begin(), NE = Node->end();
460 DominatorTree::Node *IDominee = *NI;
461 const DomSetType &ChildDF = calculate(DT, IDominee);
463 DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end();
464 for (; CDFI != CDFE; ++CDFI) {
465 if (!Node->properlyDominates(DT[*CDFI]))
473 void DominanceFrontierBase::print(std::ostream &o, const Module* ) const {
474 for (const_iterator I = begin(), E = end(); I != E; ++I) {
475 o << " DomFrontier for BB";
477 WriteAsOperand(o, I->first, false);
479 o << " <<exit node>>";
480 o << " is:\t" << I->second << "\n";
484 //===----------------------------------------------------------------------===//
485 // ETOccurrence Implementation
486 //===----------------------------------------------------------------------===//
488 void ETOccurrence::Splay() {
489 ETOccurrence *father;
490 ETOccurrence *grandfather;
498 fatherdepth = Parent->Depth;
499 grandfather = father->Parent;
501 // If we have no grandparent, a single zig or zag will do.
503 setDepthAdd(fatherdepth);
504 MinOccurrence = father->MinOccurrence;
507 // See what we have to rotate
508 if (father->Left == this) {
510 father->setLeft(Right);
513 father->Left->setDepthAdd(occdepth);
516 father->setRight(Left);
519 father->Right->setDepthAdd(occdepth);
521 father->setDepth(-occdepth);
524 father->recomputeMin();
528 // If we have a grandfather, we need to do some
529 // combination of zig and zag.
530 int grandfatherdepth = grandfather->Depth;
532 setDepthAdd(fatherdepth + grandfatherdepth);
533 MinOccurrence = grandfather->MinOccurrence;
534 Min = grandfather->Min;
536 ETOccurrence *greatgrandfather = grandfather->Parent;
538 if (grandfather->Left == father) {
539 if (father->Left == this) {
541 grandfather->setLeft(father->Right);
542 father->setLeft(Right);
544 father->setRight(grandfather);
546 father->setDepth(-occdepth);
549 father->Left->setDepthAdd(occdepth);
551 grandfather->setDepth(-fatherdepth);
552 if (grandfather->Left)
553 grandfather->Left->setDepthAdd(fatherdepth);
556 grandfather->setLeft(Right);
557 father->setRight(Left);
559 setRight(grandfather);
561 father->setDepth(-occdepth);
563 father->Right->setDepthAdd(occdepth);
564 grandfather->setDepth(-occdepth - fatherdepth);
565 if (grandfather->Left)
566 grandfather->Left->setDepthAdd(occdepth + fatherdepth);
569 if (father->Left == this) {
571 grandfather->setRight(Left);
572 father->setLeft(Right);
573 setLeft(grandfather);
576 father->setDepth(-occdepth);
578 father->Left->setDepthAdd(occdepth);
579 grandfather->setDepth(-occdepth - fatherdepth);
580 if (grandfather->Right)
581 grandfather->Right->setDepthAdd(occdepth + fatherdepth);
583 grandfather->setRight(father->Left);
584 father->setRight(Left);
586 father->setLeft(grandfather);
588 father->setDepth(-occdepth);
590 father->Right->setDepthAdd(occdepth);
591 grandfather->setDepth(-fatherdepth);
592 if (grandfather->Right)
593 grandfather->Right->setDepthAdd(fatherdepth);
597 // Might need one more rotate depending on greatgrandfather.
598 setParent(greatgrandfather);
599 if (greatgrandfather) {
600 if (greatgrandfather->Left == grandfather)
601 greatgrandfather->Left = this;
603 greatgrandfather->Right = this;
606 grandfather->recomputeMin();
607 father->recomputeMin();
611 //===----------------------------------------------------------------------===//
612 // ETNode implementation
613 //===----------------------------------------------------------------------===//
615 void ETNode::Split() {
616 ETOccurrence *right, *left;
617 ETOccurrence *rightmost = RightmostOcc;
618 ETOccurrence *parent;
620 // Update the occurrence tree first.
621 RightmostOcc->Splay();
623 // Find the leftmost occurrence in the rightmost subtree, then splay
625 for (right = rightmost->Right; right->Left; right = right->Left);
630 right->Left->Parent = NULL;
636 parent->Right->Parent = NULL;
638 right->setLeft(left);
640 right->recomputeMin();
643 rightmost->Depth = 0;
648 // Now update *our* tree
650 if (Father->Son == this)
653 if (Father->Son == this)
663 void ETNode::setFather(ETNode *NewFather) {
664 ETOccurrence *rightmost;
665 ETOccurrence *leftpart;
666 ETOccurrence *NewFatherOcc;
669 // First update the path in the splay tree
670 NewFatherOcc = new ETOccurrence(NewFather);
672 rightmost = NewFather->RightmostOcc;
675 leftpart = rightmost->Left;
680 NewFatherOcc->setLeft(leftpart);
681 NewFatherOcc->setRight(temp);
685 NewFatherOcc->recomputeMin();
687 rightmost->setLeft(NewFatherOcc);
689 if (NewFatherOcc->Min + rightmost->Depth < rightmost->Min) {
690 rightmost->Min = NewFatherOcc->Min + rightmost->Depth;
691 rightmost->MinOccurrence = NewFatherOcc->MinOccurrence;
695 ParentOcc = NewFatherOcc;
717 bool ETNode::Below(ETNode *other) {
718 ETOccurrence *up = other->RightmostOcc;
719 ETOccurrence *down = RightmostOcc;
726 ETOccurrence *left, *right;
736 right->Parent = NULL;
740 if (left == down || left->Parent != NULL) {
747 // If the two occurrences are in different trees, put things
748 // back the way they were.
749 if (right && right->Parent != NULL)
756 if (down->Depth <= 0)
759 return !down->Right || down->Right->Min + down->Depth >= 0;
762 ETNode *ETNode::NCA(ETNode *other) {
763 ETOccurrence *occ1 = RightmostOcc;
764 ETOccurrence *occ2 = other->RightmostOcc;
766 ETOccurrence *left, *right, *ret;
767 ETOccurrence *occmin;
781 right->Parent = NULL;
784 if (left == occ2 || (left && left->Parent != NULL)) {
789 right->Parent = occ1;
793 occ1->setRight(occ2);
798 if (occ2->Depth > 0) {
800 mindepth = occ1->Depth;
803 mindepth = occ2->Depth + occ1->Depth;
806 if (ret && ret->Min + occ1->Depth + occ2->Depth < mindepth)
807 return ret->MinOccurrence->OccFor;
809 return occmin->OccFor;
812 void ETNode::assignDFSNumber(int num) {
813 std::vector<ETNode *> workStack;
814 std::set<ETNode *> visitedNodes;
816 workStack.push_back(this);
817 visitedNodes.insert(this);
818 this->DFSNumIn = num++;
820 while (!workStack.empty()) {
821 ETNode *Node = workStack.back();
823 // If this is leaf node then set DFSNumOut and pop the stack
825 Node->DFSNumOut = num++;
826 workStack.pop_back();
830 ETNode *son = Node->Son;
832 // Visit Node->Son first
833 if (visitedNodes.count(son) == 0) {
834 son->DFSNumIn = num++;
835 workStack.push_back(son);
836 visitedNodes.insert(son);
840 bool visitChild = false;
841 // Visit remaining children
842 for (ETNode *s = son->Right; s != son && !visitChild; s = s->Right) {
843 if (visitedNodes.count(s) == 0) {
846 workStack.push_back(s);
847 visitedNodes.insert(s);
852 // If we reach here means all children are visited
853 Node->DFSNumOut = num++;
854 workStack.pop_back();
859 //===----------------------------------------------------------------------===//
860 // ETForest implementation
861 //===----------------------------------------------------------------------===//
863 static RegisterPass<ETForest>
864 D("etforest", "ET Forest Construction", true);
866 void ETForestBase::reset() {
867 for (ETMapType::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I)
872 void ETForestBase::updateDFSNumbers()
875 // Iterate over all nodes in depth first order.
876 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
877 for (df_iterator<BasicBlock*> I = df_begin(Roots[i]),
878 E = df_end(Roots[i]); I != E; ++I) {
880 if (!getNode(BB)->hasFather())
881 getNode(BB)->assignDFSNumber(dfsnum);
887 ETNode *ETForest::getNodeForBlock(BasicBlock *BB) {
888 ETNode *&BBNode = Nodes[BB];
889 if (BBNode) return BBNode;
891 // Haven't calculated this node yet? Get or calculate the node for the
892 // immediate dominator.
893 BasicBlock *IDom = getAnalysis<ImmediateDominators>()[BB];
895 // If we are unreachable, we may not have an immediate dominator.
897 return BBNode = new ETNode(BB);
899 ETNode *IDomNode = getNodeForBlock(IDom);
901 // Add a new tree node for this BasicBlock, and link it as a child of
903 BBNode = new ETNode(BB);
904 BBNode->setFather(IDomNode);
909 void ETForest::calculate(const ImmediateDominators &ID) {
910 assert(Roots.size() == 1 && "ETForest should have 1 root block!");
911 BasicBlock *Root = Roots[0];
912 Nodes[Root] = new ETNode(Root); // Add a node for the root
914 Function *F = Root->getParent();
915 // Loop over all of the reachable blocks in the function...
916 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
917 if (BasicBlock *ImmDom = ID.get(I)) { // Reachable block.
918 ETNode *&BBNode = Nodes[I];
919 if (!BBNode) { // Haven't calculated this node yet?
920 // Get or calculate the node for the immediate dominator
921 ETNode *IDomNode = getNodeForBlock(ImmDom);
923 // Add a new ETNode for this BasicBlock, and set it's parent
924 // to it's immediate dominator.
925 BBNode = new ETNode(I);
926 BBNode->setFather(IDomNode);
930 // Make sure we've got nodes around for every block
931 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
932 ETNode *&BBNode = Nodes[I];
934 BBNode = new ETNode(I);
940 //===----------------------------------------------------------------------===//
941 // ETForestBase Implementation
942 //===----------------------------------------------------------------------===//
944 void ETForestBase::addNewBlock(BasicBlock *BB, BasicBlock *IDom) {
945 ETNode *&BBNode = Nodes[BB];
946 assert(!BBNode && "BasicBlock already in ET-Forest");
948 BBNode = new ETNode(BB);
949 BBNode->setFather(getNode(IDom));
950 DFSInfoValid = false;
953 void ETForestBase::setImmediateDominator(BasicBlock *BB, BasicBlock *newIDom) {
954 assert(getNode(BB) && "BasicBlock not in ET-Forest");
955 assert(getNode(newIDom) && "IDom not in ET-Forest");
957 ETNode *Node = getNode(BB);
958 if (Node->hasFather()) {
959 if (Node->getFather()->getData<BasicBlock>() == newIDom)
963 Node->setFather(getNode(newIDom));
967 void ETForestBase::print(std::ostream &o, const Module *) const {
968 o << "=============================--------------------------------\n";
975 o << " up to date\n";
977 Function *F = getRoots()[0]->getParent();
978 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
979 o << " DFS Numbers For Basic Block:";
980 WriteAsOperand(o, I, false);
982 if (ETNode *EN = getNode(I)) {
983 o << "In: " << EN->getDFSNumIn();
984 o << " Out: " << EN->getDFSNumOut() << "\n";
986 o << "No associated ETNode";
993 DEFINING_FILE_FOR(DominatorSet)