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 char DominatorTree::ID = 0;
62 static RegisterPass<DominatorTree>
63 E("domtree", "Dominator Tree Construction", true);
65 unsigned DominatorTree::DFSPass(BasicBlock *V, InfoRec &VInfo,
67 // This is more understandable as a recursive algorithm, but we can't use the
68 // recursive algorithm due to stack depth issues. Keep it here for
69 // documentation purposes.
74 Vertex.push_back(V); // Vertex[n] = V;
75 //Info[V].Ancestor = 0; // Ancestor[n] = 0
76 //Info[V].Child = 0; // Child[v] = 0
77 VInfo.Size = 1; // Size[v] = 1
79 for (succ_iterator SI = succ_begin(V), E = succ_end(V); SI != E; ++SI) {
80 InfoRec &SuccVInfo = Info[*SI];
81 if (SuccVInfo.Semi == 0) {
83 N = DFSPass(*SI, SuccVInfo, N);
87 std::vector<std::pair<BasicBlock*, unsigned> > Worklist;
88 Worklist.push_back(std::make_pair(V, 0U));
89 while (!Worklist.empty()) {
90 BasicBlock *BB = Worklist.back().first;
91 unsigned NextSucc = Worklist.back().second;
93 // First time we visited this BB?
95 InfoRec &BBInfo = Info[BB];
99 Vertex.push_back(BB); // Vertex[n] = V;
100 //BBInfo[V].Ancestor = 0; // Ancestor[n] = 0
101 //BBInfo[V].Child = 0; // Child[v] = 0
102 BBInfo.Size = 1; // Size[v] = 1
105 // If we are done with this block, remove it from the worklist.
106 if (NextSucc == BB->getTerminator()->getNumSuccessors()) {
111 // Otherwise, increment the successor number for the next time we get to it.
112 ++Worklist.back().second;
114 // Visit the successor next, if it isn't already visited.
115 BasicBlock *Succ = BB->getTerminator()->getSuccessor(NextSucc);
117 InfoRec &SuccVInfo = Info[Succ];
118 if (SuccVInfo.Semi == 0) {
119 SuccVInfo.Parent = BB;
120 Worklist.push_back(std::make_pair(Succ, 0U));
127 void DominatorTree::Compress(BasicBlock *VIn) {
129 std::vector<BasicBlock *> Work;
130 std::set<BasicBlock *> Visited;
131 InfoRec &VInInfo = Info[VIn];
132 BasicBlock *VInAncestor = VInInfo.Ancestor;
133 InfoRec &VInVAInfo = Info[VInAncestor];
135 if (VInVAInfo.Ancestor != 0)
138 while (!Work.empty()) {
139 BasicBlock *V = Work.back();
140 InfoRec &VInfo = Info[V];
141 BasicBlock *VAncestor = VInfo.Ancestor;
142 InfoRec &VAInfo = Info[VAncestor];
144 // Process Ancestor first
145 if (Visited.count(VAncestor) == 0 && VAInfo.Ancestor != 0) {
146 Work.push_back(VAncestor);
147 Visited.insert(VAncestor);
152 // Update VINfo based on Ancestor info
153 if (VAInfo.Ancestor == 0)
155 BasicBlock *VAncestorLabel = VAInfo.Label;
156 BasicBlock *VLabel = VInfo.Label;
157 if (Info[VAncestorLabel].Semi < Info[VLabel].Semi)
158 VInfo.Label = VAncestorLabel;
159 VInfo.Ancestor = VAInfo.Ancestor;
163 BasicBlock *DominatorTree::Eval(BasicBlock *V) {
164 InfoRec &VInfo = Info[V];
165 #if !BALANCE_IDOM_TREE
166 // Higher-complexity but faster implementation
167 if (VInfo.Ancestor == 0)
172 // Lower-complexity but slower implementation
173 if (VInfo.Ancestor == 0)
176 BasicBlock *VLabel = VInfo.Label;
178 BasicBlock *VAncestorLabel = Info[VInfo.Ancestor].Label;
179 if (Info[VAncestorLabel].Semi >= Info[VLabel].Semi)
182 return VAncestorLabel;
186 void DominatorTree::Link(BasicBlock *V, BasicBlock *W, InfoRec &WInfo){
187 #if !BALANCE_IDOM_TREE
188 // Higher-complexity but faster implementation
191 // Lower-complexity but slower implementation
192 BasicBlock *WLabel = WInfo.Label;
193 unsigned WLabelSemi = Info[WLabel].Semi;
195 InfoRec *SInfo = &Info[S];
197 BasicBlock *SChild = SInfo->Child;
198 InfoRec *SChildInfo = &Info[SChild];
200 while (WLabelSemi < Info[SChildInfo->Label].Semi) {
201 BasicBlock *SChildChild = SChildInfo->Child;
202 if (SInfo->Size+Info[SChildChild].Size >= 2*SChildInfo->Size) {
203 SChildInfo->Ancestor = S;
204 SInfo->Child = SChild = SChildChild;
205 SChildInfo = &Info[SChild];
207 SChildInfo->Size = SInfo->Size;
208 S = SInfo->Ancestor = SChild;
210 SChild = SChildChild;
211 SChildInfo = &Info[SChild];
215 InfoRec &VInfo = Info[V];
216 SInfo->Label = WLabel;
218 assert(V != W && "The optimization here will not work in this case!");
219 unsigned WSize = WInfo.Size;
220 unsigned VSize = (VInfo.Size += WSize);
223 std::swap(S, VInfo.Child);
233 void DominatorTree::calculate(Function& F) {
234 BasicBlock* Root = Roots[0];
236 Nodes[Root] = RootNode = new Node(Root, 0); // Add a node for the root...
240 // Step #1: Number blocks in depth-first order and initialize variables used
241 // in later stages of the algorithm.
243 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
244 N = DFSPass(Roots[i], Info[Roots[i]], 0);
246 for (unsigned i = N; i >= 2; --i) {
247 BasicBlock *W = Vertex[i];
248 InfoRec &WInfo = Info[W];
250 // Step #2: Calculate the semidominators of all vertices
251 for (pred_iterator PI = pred_begin(W), E = pred_end(W); PI != E; ++PI)
252 if (Info.count(*PI)) { // Only if this predecessor is reachable!
253 unsigned SemiU = Info[Eval(*PI)].Semi;
254 if (SemiU < WInfo.Semi)
258 Info[Vertex[WInfo.Semi]].Bucket.push_back(W);
260 BasicBlock *WParent = WInfo.Parent;
261 Link(WParent, W, WInfo);
263 // Step #3: Implicitly define the immediate dominator of vertices
264 std::vector<BasicBlock*> &WParentBucket = Info[WParent].Bucket;
265 while (!WParentBucket.empty()) {
266 BasicBlock *V = WParentBucket.back();
267 WParentBucket.pop_back();
268 BasicBlock *U = Eval(V);
269 IDoms[V] = Info[U].Semi < Info[V].Semi ? U : WParent;
273 // Step #4: Explicitly define the immediate dominator of each vertex
274 for (unsigned i = 2; i <= N; ++i) {
275 BasicBlock *W = Vertex[i];
276 BasicBlock *&WIDom = IDoms[W];
277 if (WIDom != Vertex[Info[W].Semi])
278 WIDom = IDoms[WIDom];
281 // Loop over all of the reachable blocks in the function...
282 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
283 if (BasicBlock *ImmDom = getIDom(I)) { // Reachable block.
284 Node *&BBNode = Nodes[I];
285 if (!BBNode) { // Haven't calculated this node yet?
286 // Get or calculate the node for the immediate dominator
287 Node *IDomNode = getNodeForBlock(ImmDom);
289 // Add a new tree node for this BasicBlock, and link it as a child of
291 BBNode = IDomNode->addChild(new Node(I, IDomNode));
295 // Free temporary memory used to construct idom's
298 std::vector<BasicBlock*>().swap(Vertex);
301 // DominatorTreeBase::reset - Free all of the tree node memory.
303 void DominatorTreeBase::reset() {
304 for (NodeMapType::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I)
313 void DominatorTreeBase::Node::setIDom(Node *NewIDom) {
314 assert(IDom && "No immediate dominator?");
315 if (IDom != NewIDom) {
316 std::vector<Node*>::iterator I =
317 std::find(IDom->Children.begin(), IDom->Children.end(), this);
318 assert(I != IDom->Children.end() &&
319 "Not in immediate dominator children set!");
320 // I am no longer your child...
321 IDom->Children.erase(I);
323 // Switch to new dominator
325 IDom->Children.push_back(this);
329 DominatorTreeBase::Node *DominatorTree::getNodeForBlock(BasicBlock *BB) {
330 Node *&BBNode = Nodes[BB];
331 if (BBNode) return BBNode;
333 // Haven't calculated this node yet? Get or calculate the node for the
334 // immediate dominator.
335 BasicBlock *IDom = getIDom(BB);
336 Node *IDomNode = getNodeForBlock(IDom);
338 // Add a new tree node for this BasicBlock, and link it as a child of
340 return BBNode = IDomNode->addChild(new Node(BB, IDomNode));
343 static std::ostream &operator<<(std::ostream &o,
344 const DominatorTreeBase::Node *Node) {
345 if (Node->getBlock())
346 WriteAsOperand(o, Node->getBlock(), false);
348 o << " <<exit node>>";
352 static void PrintDomTree(const DominatorTreeBase::Node *N, std::ostream &o,
354 o << std::string(2*Lev, ' ') << "[" << Lev << "] " << N;
355 for (DominatorTreeBase::Node::const_iterator I = N->begin(), E = N->end();
357 PrintDomTree(*I, o, Lev+1);
360 void DominatorTreeBase::print(std::ostream &o, const Module* ) const {
361 o << "=============================--------------------------------\n"
362 << "Inorder Dominator Tree:\n";
363 PrintDomTree(getRootNode(), o, 1);
366 bool DominatorTree::runOnFunction(Function &F) {
367 reset(); // Reset from the last time we were run...
368 Roots.push_back(&F.getEntryBlock());
373 //===----------------------------------------------------------------------===//
374 // DominanceFrontier Implementation
375 //===----------------------------------------------------------------------===//
377 char DominanceFrontier::ID = 0;
378 static RegisterPass<DominanceFrontier>
379 G("domfrontier", "Dominance Frontier Construction", true);
382 class DFCalculateWorkObject {
384 DFCalculateWorkObject(BasicBlock *B, BasicBlock *P,
385 const DominatorTree::Node *N,
386 const DominatorTree::Node *PN)
387 : currentBB(B), parentBB(P), Node(N), parentNode(PN) {}
388 BasicBlock *currentBB;
389 BasicBlock *parentBB;
390 const DominatorTree::Node *Node;
391 const DominatorTree::Node *parentNode;
395 const DominanceFrontier::DomSetType &
396 DominanceFrontier::calculate(const DominatorTree &DT,
397 const DominatorTree::Node *Node) {
398 BasicBlock *BB = Node->getBlock();
399 DomSetType *Result = NULL;
401 std::vector<DFCalculateWorkObject> workList;
402 SmallPtrSet<BasicBlock *, 32> visited;
404 workList.push_back(DFCalculateWorkObject(BB, NULL, Node, NULL));
406 DFCalculateWorkObject *currentW = &workList.back();
407 assert (currentW && "Missing work object.");
409 BasicBlock *currentBB = currentW->currentBB;
410 BasicBlock *parentBB = currentW->parentBB;
411 const DominatorTree::Node *currentNode = currentW->Node;
412 const DominatorTree::Node *parentNode = currentW->parentNode;
413 assert (currentBB && "Invalid work object. Missing current Basic Block");
414 assert (currentNode && "Invalid work object. Missing current Node");
415 DomSetType &S = Frontiers[currentBB];
417 // Visit each block only once.
418 if (visited.count(currentBB) == 0) {
419 visited.insert(currentBB);
421 // Loop over CFG successors to calculate DFlocal[currentNode]
422 for (succ_iterator SI = succ_begin(currentBB), SE = succ_end(currentBB);
424 // Does Node immediately dominate this successor?
425 if (DT[*SI]->getIDom() != currentNode)
430 // At this point, S is DFlocal. Now we union in DFup's of our children...
431 // Loop through and visit the nodes that Node immediately dominates (Node's
432 // children in the IDomTree)
433 bool visitChild = false;
434 for (DominatorTree::Node::const_iterator NI = currentNode->begin(),
435 NE = currentNode->end(); NI != NE; ++NI) {
436 DominatorTree::Node *IDominee = *NI;
437 BasicBlock *childBB = IDominee->getBlock();
438 if (visited.count(childBB) == 0) {
439 workList.push_back(DFCalculateWorkObject(childBB, currentBB,
440 IDominee, currentNode));
445 // If all children are visited or there is any child then pop this block
446 // from the workList.
454 DomSetType::const_iterator CDFI = S.begin(), CDFE = S.end();
455 DomSetType &parentSet = Frontiers[parentBB];
456 for (; CDFI != CDFE; ++CDFI) {
457 if (!parentNode->properlyDominates(DT[*CDFI]))
458 parentSet.insert(*CDFI);
463 } while (!workList.empty());
468 void DominanceFrontierBase::print(std::ostream &o, const Module* ) const {
469 for (const_iterator I = begin(), E = end(); I != E; ++I) {
470 o << " DomFrontier for BB";
472 WriteAsOperand(o, I->first, false);
474 o << " <<exit node>>";
475 o << " is:\t" << I->second << "\n";
479 //===----------------------------------------------------------------------===//
480 // ETOccurrence Implementation
481 //===----------------------------------------------------------------------===//
483 void ETOccurrence::Splay() {
484 ETOccurrence *father;
485 ETOccurrence *grandfather;
493 fatherdepth = Parent->Depth;
494 grandfather = father->Parent;
496 // If we have no grandparent, a single zig or zag will do.
498 setDepthAdd(fatherdepth);
499 MinOccurrence = father->MinOccurrence;
502 // See what we have to rotate
503 if (father->Left == this) {
505 father->setLeft(Right);
508 father->Left->setDepthAdd(occdepth);
511 father->setRight(Left);
514 father->Right->setDepthAdd(occdepth);
516 father->setDepth(-occdepth);
519 father->recomputeMin();
523 // If we have a grandfather, we need to do some
524 // combination of zig and zag.
525 int grandfatherdepth = grandfather->Depth;
527 setDepthAdd(fatherdepth + grandfatherdepth);
528 MinOccurrence = grandfather->MinOccurrence;
529 Min = grandfather->Min;
531 ETOccurrence *greatgrandfather = grandfather->Parent;
533 if (grandfather->Left == father) {
534 if (father->Left == this) {
536 grandfather->setLeft(father->Right);
537 father->setLeft(Right);
539 father->setRight(grandfather);
541 father->setDepth(-occdepth);
544 father->Left->setDepthAdd(occdepth);
546 grandfather->setDepth(-fatherdepth);
547 if (grandfather->Left)
548 grandfather->Left->setDepthAdd(fatherdepth);
551 grandfather->setLeft(Right);
552 father->setRight(Left);
554 setRight(grandfather);
556 father->setDepth(-occdepth);
558 father->Right->setDepthAdd(occdepth);
559 grandfather->setDepth(-occdepth - fatherdepth);
560 if (grandfather->Left)
561 grandfather->Left->setDepthAdd(occdepth + fatherdepth);
564 if (father->Left == this) {
566 grandfather->setRight(Left);
567 father->setLeft(Right);
568 setLeft(grandfather);
571 father->setDepth(-occdepth);
573 father->Left->setDepthAdd(occdepth);
574 grandfather->setDepth(-occdepth - fatherdepth);
575 if (grandfather->Right)
576 grandfather->Right->setDepthAdd(occdepth + fatherdepth);
578 grandfather->setRight(father->Left);
579 father->setRight(Left);
581 father->setLeft(grandfather);
583 father->setDepth(-occdepth);
585 father->Right->setDepthAdd(occdepth);
586 grandfather->setDepth(-fatherdepth);
587 if (grandfather->Right)
588 grandfather->Right->setDepthAdd(fatherdepth);
592 // Might need one more rotate depending on greatgrandfather.
593 setParent(greatgrandfather);
594 if (greatgrandfather) {
595 if (greatgrandfather->Left == grandfather)
596 greatgrandfather->Left = this;
598 greatgrandfather->Right = this;
601 grandfather->recomputeMin();
602 father->recomputeMin();
606 //===----------------------------------------------------------------------===//
607 // ETNode implementation
608 //===----------------------------------------------------------------------===//
610 void ETNode::Split() {
611 ETOccurrence *right, *left;
612 ETOccurrence *rightmost = RightmostOcc;
613 ETOccurrence *parent;
615 // Update the occurrence tree first.
616 RightmostOcc->Splay();
618 // Find the leftmost occurrence in the rightmost subtree, then splay
620 for (right = rightmost->Right; right->Left; right = right->Left);
625 right->Left->Parent = NULL;
631 parent->Right->Parent = NULL;
633 right->setLeft(left);
635 right->recomputeMin();
638 rightmost->Depth = 0;
643 // Now update *our* tree
645 if (Father->Son == this)
648 if (Father->Son == this)
658 void ETNode::setFather(ETNode *NewFather) {
659 ETOccurrence *rightmost;
660 ETOccurrence *leftpart;
661 ETOccurrence *NewFatherOcc;
664 // First update the path in the splay tree
665 NewFatherOcc = new ETOccurrence(NewFather);
667 rightmost = NewFather->RightmostOcc;
670 leftpart = rightmost->Left;
675 NewFatherOcc->setLeft(leftpart);
676 NewFatherOcc->setRight(temp);
680 NewFatherOcc->recomputeMin();
682 rightmost->setLeft(NewFatherOcc);
684 if (NewFatherOcc->Min + rightmost->Depth < rightmost->Min) {
685 rightmost->Min = NewFatherOcc->Min + rightmost->Depth;
686 rightmost->MinOccurrence = NewFatherOcc->MinOccurrence;
690 ParentOcc = NewFatherOcc;
712 bool ETNode::Below(ETNode *other) {
713 ETOccurrence *up = other->RightmostOcc;
714 ETOccurrence *down = RightmostOcc;
721 ETOccurrence *left, *right;
731 right->Parent = NULL;
735 if (left == down || left->Parent != NULL) {
742 // If the two occurrences are in different trees, put things
743 // back the way they were.
744 if (right && right->Parent != NULL)
751 if (down->Depth <= 0)
754 return !down->Right || down->Right->Min + down->Depth >= 0;
757 ETNode *ETNode::NCA(ETNode *other) {
758 ETOccurrence *occ1 = RightmostOcc;
759 ETOccurrence *occ2 = other->RightmostOcc;
761 ETOccurrence *left, *right, *ret;
762 ETOccurrence *occmin;
776 right->Parent = NULL;
779 if (left == occ2 || (left && left->Parent != NULL)) {
784 right->Parent = occ1;
788 occ1->setRight(occ2);
793 if (occ2->Depth > 0) {
795 mindepth = occ1->Depth;
798 mindepth = occ2->Depth + occ1->Depth;
801 if (ret && ret->Min + occ1->Depth + occ2->Depth < mindepth)
802 return ret->MinOccurrence->OccFor;
804 return occmin->OccFor;
807 void ETNode::assignDFSNumber(int num) {
808 std::vector<ETNode *> workStack;
809 std::set<ETNode *> visitedNodes;
811 workStack.push_back(this);
812 visitedNodes.insert(this);
813 this->DFSNumIn = num++;
815 while (!workStack.empty()) {
816 ETNode *Node = workStack.back();
818 // If this is leaf node then set DFSNumOut and pop the stack
820 Node->DFSNumOut = num++;
821 workStack.pop_back();
825 ETNode *son = Node->Son;
827 // Visit Node->Son first
828 if (visitedNodes.count(son) == 0) {
829 son->DFSNumIn = num++;
830 workStack.push_back(son);
831 visitedNodes.insert(son);
835 bool visitChild = false;
836 // Visit remaining children
837 for (ETNode *s = son->Right; s != son && !visitChild; s = s->Right) {
838 if (visitedNodes.count(s) == 0) {
841 workStack.push_back(s);
842 visitedNodes.insert(s);
847 // If we reach here means all children are visited
848 Node->DFSNumOut = num++;
849 workStack.pop_back();
854 //===----------------------------------------------------------------------===//
855 // ETForest implementation
856 //===----------------------------------------------------------------------===//
858 char ETForest::ID = 0;
859 static RegisterPass<ETForest>
860 D("etforest", "ET Forest Construction", true);
862 void ETForestBase::reset() {
863 for (ETMapType::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I)
868 void ETForestBase::updateDFSNumbers()
871 // Iterate over all nodes in depth first order.
872 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
873 for (df_iterator<BasicBlock*> I = df_begin(Roots[i]),
874 E = df_end(Roots[i]); I != E; ++I) {
876 ETNode *ETN = getNode(BB);
877 if (ETN && !ETN->hasFather())
878 ETN->assignDFSNumber(dfsnum);
884 // dominates - Return true if A dominates B. THis performs the
885 // special checks necessary if A and B are in the same basic block.
886 bool ETForestBase::dominates(Instruction *A, Instruction *B) {
887 BasicBlock *BBA = A->getParent(), *BBB = B->getParent();
888 if (BBA != BBB) return dominates(BBA, BBB);
890 // It is not possible to determine dominance between two PHI nodes
891 // based on their ordering.
892 if (isa<PHINode>(A) && isa<PHINode>(B))
895 // Loop through the basic block until we find A or B.
896 BasicBlock::iterator I = BBA->begin();
897 for (; &*I != A && &*I != B; ++I) /*empty*/;
899 if(!IsPostDominators) {
900 // A dominates B if it is found first in the basic block.
903 // A post-dominates B if B is found first in the basic block.
908 /// isReachableFromEntry - Return true if A is dominated by the entry
909 /// block of the function containing it.
910 const bool ETForestBase::isReachableFromEntry(BasicBlock* A) {
911 return dominates(&A->getParent()->getEntryBlock(), A);
914 ETNode *ETForest::getNodeForBlock(BasicBlock *BB) {
915 ETNode *&BBNode = Nodes[BB];
916 if (BBNode) return BBNode;
918 // Haven't calculated this node yet? Get or calculate the node for the
919 // immediate dominator.
920 DominatorTree::Node *node= getAnalysis<DominatorTree>().getNode(BB);
922 // If we are unreachable, we may not have an immediate dominator.
923 if (!node || !node->getIDom())
924 return BBNode = new ETNode(BB);
926 ETNode *IDomNode = getNodeForBlock(node->getIDom()->getBlock());
928 // Add a new tree node for this BasicBlock, and link it as a child of
930 BBNode = new ETNode(BB);
931 BBNode->setFather(IDomNode);
936 void ETForest::calculate(const DominatorTree &DT) {
937 assert(Roots.size() == 1 && "ETForest should have 1 root block!");
938 BasicBlock *Root = Roots[0];
939 Nodes[Root] = new ETNode(Root); // Add a node for the root
941 Function *F = Root->getParent();
942 // Loop over all of the reachable blocks in the function...
943 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
944 DominatorTree::Node* node = DT.getNode(I);
945 if (node && node->getIDom()) { // Reachable block.
946 BasicBlock* ImmDom = node->getIDom()->getBlock();
947 ETNode *&BBNode = Nodes[I];
948 if (!BBNode) { // Haven't calculated this node yet?
949 // Get or calculate the node for the immediate dominator
950 ETNode *IDomNode = getNodeForBlock(ImmDom);
952 // Add a new ETNode for this BasicBlock, and set it's parent
953 // to it's immediate dominator.
954 BBNode = new ETNode(I);
955 BBNode->setFather(IDomNode);
960 // Make sure we've got nodes around for every block
961 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
962 ETNode *&BBNode = Nodes[I];
964 BBNode = new ETNode(I);
970 //===----------------------------------------------------------------------===//
971 // ETForestBase Implementation
972 //===----------------------------------------------------------------------===//
974 void ETForestBase::addNewBlock(BasicBlock *BB, BasicBlock *IDom) {
975 ETNode *&BBNode = Nodes[BB];
976 assert(!BBNode && "BasicBlock already in ET-Forest");
978 BBNode = new ETNode(BB);
979 BBNode->setFather(getNode(IDom));
980 DFSInfoValid = false;
983 void ETForestBase::setImmediateDominator(BasicBlock *BB, BasicBlock *newIDom) {
984 assert(getNode(BB) && "BasicBlock not in ET-Forest");
985 assert(getNode(newIDom) && "IDom not in ET-Forest");
987 ETNode *Node = getNode(BB);
988 if (Node->hasFather()) {
989 if (Node->getFather()->getData<BasicBlock>() == newIDom)
993 Node->setFather(getNode(newIDom));
997 void ETForestBase::print(std::ostream &o, const Module *) const {
998 o << "=============================--------------------------------\n";
1005 o << " up to date\n";
1007 Function *F = getRoots()[0]->getParent();
1008 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
1009 o << " DFS Numbers For Basic Block:";
1010 WriteAsOperand(o, I, false);
1012 if (ETNode *EN = getNode(I)) {
1013 o << "In: " << EN->getDFSNumIn();
1014 o << " Out: " << EN->getDFSNumOut() << "\n";
1016 o << "No associated ETNode";