1 //===- Dominators.cpp - Dominator Calculation -----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements simple dominator construction algorithms for finding
11 // forward dominators. Postdominators are available in libanalysis, but are not
12 // included in libvmcore, because it's not needed. Forward dominators are
13 // needed to support the Verifier pass.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Analysis/Dominators.h"
18 #include "llvm/Support/CFG.h"
19 #include "llvm/Assembly/Writer.h"
20 #include "llvm/ADT/DepthFirstIterator.h"
21 #include "llvm/ADT/SetOperations.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/Instructions.h"
27 //===----------------------------------------------------------------------===//
28 // ImmediateDominators Implementation
29 //===----------------------------------------------------------------------===//
31 // Immediate Dominators construction - This pass constructs immediate dominator
32 // information for a flow-graph based on the algorithm described in this
35 // A Fast Algorithm for Finding Dominators in a Flowgraph
36 // T. Lengauer & R. Tarjan, ACM TOPLAS July 1979, pgs 121-141.
38 // This implements both the O(n*ack(n)) and the O(n*log(n)) versions of EVAL and
39 // LINK, but it turns out that the theoretically slower O(n*log(n))
40 // implementation is actually faster than the "efficient" algorithm (even for
41 // large CFGs) because the constant overheads are substantially smaller. The
42 // lower-complexity version can be enabled with the following #define:
44 #define BALANCE_IDOM_TREE 0
46 //===----------------------------------------------------------------------===//
48 static RegisterPass<ImmediateDominators>
49 C("idom", "Immediate Dominators Construction", true);
52 class DFCalculateWorkObject {
54 DFCalculateWorkObject(BasicBlock *B, BasicBlock *P,
55 const DominatorTree::Node *N,
56 const DominatorTree::Node *PN)
57 : currentBB(B), parentBB(P), Node(N), parentNode(PN) {}
58 BasicBlock *currentBB;
60 const DominatorTree::Node *Node;
61 const DominatorTree::Node *parentNode;
64 unsigned ImmediateDominators::DFSPass(BasicBlock *V, InfoRec &VInfo,
69 Vertex.push_back(V); // Vertex[n] = V;
70 //Info[V].Ancestor = 0; // Ancestor[n] = 0
71 //Child[V] = 0; // Child[v] = 0
72 VInfo.Size = 1; // Size[v] = 1
74 for (succ_iterator SI = succ_begin(V), E = succ_end(V); SI != E; ++SI) {
75 InfoRec &SuccVInfo = Info[*SI];
76 if (SuccVInfo.Semi == 0) {
78 N = DFSPass(*SI, SuccVInfo, N);
84 void ImmediateDominators::Compress(BasicBlock *V, InfoRec &VInfo) {
85 BasicBlock *VAncestor = VInfo.Ancestor;
86 InfoRec &VAInfo = Info[VAncestor];
87 if (VAInfo.Ancestor == 0)
90 Compress(VAncestor, VAInfo);
92 BasicBlock *VAncestorLabel = VAInfo.Label;
93 BasicBlock *VLabel = VInfo.Label;
94 if (Info[VAncestorLabel].Semi < Info[VLabel].Semi)
95 VInfo.Label = VAncestorLabel;
97 VInfo.Ancestor = VAInfo.Ancestor;
100 BasicBlock *ImmediateDominators::Eval(BasicBlock *V) {
101 InfoRec &VInfo = Info[V];
102 #if !BALANCE_IDOM_TREE
103 // Higher-complexity but faster implementation
104 if (VInfo.Ancestor == 0)
109 // Lower-complexity but slower implementation
110 if (VInfo.Ancestor == 0)
113 BasicBlock *VLabel = VInfo.Label;
115 BasicBlock *VAncestorLabel = Info[VInfo.Ancestor].Label;
116 if (Info[VAncestorLabel].Semi >= Info[VLabel].Semi)
119 return VAncestorLabel;
123 void ImmediateDominators::Link(BasicBlock *V, BasicBlock *W, InfoRec &WInfo){
124 #if !BALANCE_IDOM_TREE
125 // Higher-complexity but faster implementation
128 // Lower-complexity but slower implementation
129 BasicBlock *WLabel = WInfo.Label;
130 unsigned WLabelSemi = Info[WLabel].Semi;
132 InfoRec *SInfo = &Info[S];
134 BasicBlock *SChild = SInfo->Child;
135 InfoRec *SChildInfo = &Info[SChild];
137 while (WLabelSemi < Info[SChildInfo->Label].Semi) {
138 BasicBlock *SChildChild = SChildInfo->Child;
139 if (SInfo->Size+Info[SChildChild].Size >= 2*SChildInfo->Size) {
140 SChildInfo->Ancestor = S;
141 SInfo->Child = SChild = SChildChild;
142 SChildInfo = &Info[SChild];
144 SChildInfo->Size = SInfo->Size;
145 S = SInfo->Ancestor = SChild;
147 SChild = SChildChild;
148 SChildInfo = &Info[SChild];
152 InfoRec &VInfo = Info[V];
153 SInfo->Label = WLabel;
155 assert(V != W && "The optimization here will not work in this case!");
156 unsigned WSize = WInfo.Size;
157 unsigned VSize = (VInfo.Size += WSize);
160 std::swap(S, VInfo.Child);
172 bool ImmediateDominators::runOnFunction(Function &F) {
173 IDoms.clear(); // Reset from the last time we were run...
174 BasicBlock *Root = &F.getEntryBlock();
176 Roots.push_back(Root);
180 // Step #1: Number blocks in depth-first order and initialize variables used
181 // in later stages of the algorithm.
183 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
184 N = DFSPass(Roots[i], Info[Roots[i]], 0);
186 for (unsigned i = N; i >= 2; --i) {
187 BasicBlock *W = Vertex[i];
188 InfoRec &WInfo = Info[W];
190 // Step #2: Calculate the semidominators of all vertices
191 for (pred_iterator PI = pred_begin(W), E = pred_end(W); PI != E; ++PI)
192 if (Info.count(*PI)) { // Only if this predecessor is reachable!
193 unsigned SemiU = Info[Eval(*PI)].Semi;
194 if (SemiU < WInfo.Semi)
198 Info[Vertex[WInfo.Semi]].Bucket.push_back(W);
200 BasicBlock *WParent = WInfo.Parent;
201 Link(WParent, W, WInfo);
203 // Step #3: Implicitly define the immediate dominator of vertices
204 std::vector<BasicBlock*> &WParentBucket = Info[WParent].Bucket;
205 while (!WParentBucket.empty()) {
206 BasicBlock *V = WParentBucket.back();
207 WParentBucket.pop_back();
208 BasicBlock *U = Eval(V);
209 IDoms[V] = Info[U].Semi < Info[V].Semi ? U : WParent;
213 // Step #4: Explicitly define the immediate dominator of each vertex
214 for (unsigned i = 2; i <= N; ++i) {
215 BasicBlock *W = Vertex[i];
216 BasicBlock *&WIDom = IDoms[W];
217 if (WIDom != Vertex[Info[W].Semi])
218 WIDom = IDoms[WIDom];
221 // Free temporary memory used to construct idom's
223 std::vector<BasicBlock*>().swap(Vertex);
228 /// dominates - Return true if A dominates B.
230 bool ImmediateDominatorsBase::dominates(BasicBlock *A, BasicBlock *B) const {
231 assert(A && B && "Null pointers?");
233 // Walk up the dominator tree from B to determine if A dom B.
239 void ImmediateDominatorsBase::print(std::ostream &o, const Module* ) const {
240 Function *F = getRoots()[0]->getParent();
241 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
242 o << " Immediate Dominator For Basic Block:";
243 WriteAsOperand(o, I, false);
245 if (BasicBlock *ID = get(I))
246 WriteAsOperand(o, ID, false);
248 o << " <<exit node>>";
256 //===----------------------------------------------------------------------===//
257 // DominatorSet Implementation
258 //===----------------------------------------------------------------------===//
260 static RegisterPass<DominatorSet>
261 B("domset", "Dominator Set Construction", true);
263 // dominates - Return true if A dominates B. This performs the special checks
264 // necessary if A and B are in the same basic block.
266 bool DominatorSetBase::dominates(Instruction *A, Instruction *B) const {
267 BasicBlock *BBA = A->getParent(), *BBB = B->getParent();
268 if (BBA != BBB) return dominates(BBA, BBB);
270 // It is not possible to determine dominance between two PHI nodes
271 // based on their ordering.
272 if (isa<PHINode>(A) && isa<PHINode>(B))
275 // Loop through the basic block until we find A or B.
276 BasicBlock::iterator I = BBA->begin();
277 for (; &*I != A && &*I != B; ++I) /*empty*/;
279 if(!IsPostDominators) {
280 // A dominates B if it is found first in the basic block.
283 // A post-dominates B if B is found first in the basic block.
289 // runOnFunction - This method calculates the forward dominator sets for the
290 // specified function.
292 bool DominatorSet::runOnFunction(Function &F) {
293 BasicBlock *Root = &F.getEntryBlock();
295 Roots.push_back(Root);
296 assert(pred_begin(Root) == pred_end(Root) &&
297 "Root node has predecessors in function!");
299 ImmediateDominators &ID = getAnalysis<ImmediateDominators>();
301 if (Roots.empty()) return false;
303 // Root nodes only dominate themselves.
304 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
305 Doms[Roots[i]].insert(Roots[i]);
307 // Loop over all of the blocks in the function, calculating dominator sets for
309 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
310 if (BasicBlock *IDom = ID[I]) { // Get idom if block is reachable
311 DomSetType &DS = Doms[I];
312 assert(DS.empty() && "Domset already filled in for this block?");
313 DS.insert(I); // Blocks always dominate themselves
315 // Insert all dominators into the set...
317 // If we have already computed the dominator sets for our immediate
318 // dominator, just use it instead of walking all the way up to the root.
319 DomSetType &IDS = Doms[IDom];
321 DS.insert(IDS.begin(), IDS.end());
329 // Ensure that every basic block has at least an empty set of nodes. This
330 // is important for the case when there is unreachable blocks.
338 static std::ostream &operator<<(std::ostream &o,
339 const std::set<BasicBlock*> &BBs) {
340 for (std::set<BasicBlock*>::const_iterator I = BBs.begin(), E = BBs.end();
343 WriteAsOperand(o, *I, false);
345 o << " <<exit node>>";
350 void DominatorSetBase::print(std::ostream &o, const Module* ) const {
351 for (const_iterator I = begin(), E = end(); I != E; ++I) {
352 o << " DomSet For BB: ";
354 WriteAsOperand(o, I->first, false);
356 o << " <<exit node>>";
357 o << " is:\t" << I->second << "\n";
361 //===----------------------------------------------------------------------===//
362 // DominatorTree Implementation
363 //===----------------------------------------------------------------------===//
365 static RegisterPass<DominatorTree>
366 E("domtree", "Dominator Tree Construction", true);
368 // DominatorTreeBase::reset - Free all of the tree node memory.
370 void DominatorTreeBase::reset() {
371 for (NodeMapType::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I)
377 void DominatorTreeBase::Node::setIDom(Node *NewIDom) {
378 assert(IDom && "No immediate dominator?");
379 if (IDom != NewIDom) {
380 std::vector<Node*>::iterator I =
381 std::find(IDom->Children.begin(), IDom->Children.end(), this);
382 assert(I != IDom->Children.end() &&
383 "Not in immediate dominator children set!");
384 // I am no longer your child...
385 IDom->Children.erase(I);
387 // Switch to new dominator
389 IDom->Children.push_back(this);
393 DominatorTreeBase::Node *DominatorTree::getNodeForBlock(BasicBlock *BB) {
394 Node *&BBNode = Nodes[BB];
395 if (BBNode) return BBNode;
397 // Haven't calculated this node yet? Get or calculate the node for the
398 // immediate dominator.
399 BasicBlock *IDom = getAnalysis<ImmediateDominators>()[BB];
400 Node *IDomNode = getNodeForBlock(IDom);
402 // Add a new tree node for this BasicBlock, and link it as a child of
404 return BBNode = IDomNode->addChild(new Node(BB, IDomNode));
407 void DominatorTree::calculate(const ImmediateDominators &ID) {
408 assert(Roots.size() == 1 && "DominatorTree should have 1 root block!");
409 BasicBlock *Root = Roots[0];
410 Nodes[Root] = RootNode = new Node(Root, 0); // Add a node for the root...
412 Function *F = Root->getParent();
413 // Loop over all of the reachable blocks in the function...
414 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
415 if (BasicBlock *ImmDom = ID.get(I)) { // Reachable block.
416 Node *&BBNode = Nodes[I];
417 if (!BBNode) { // Haven't calculated this node yet?
418 // Get or calculate the node for the immediate dominator
419 Node *IDomNode = getNodeForBlock(ImmDom);
421 // Add a new tree node for this BasicBlock, and link it as a child of
423 BBNode = IDomNode->addChild(new Node(I, IDomNode));
428 static std::ostream &operator<<(std::ostream &o,
429 const DominatorTreeBase::Node *Node) {
430 if (Node->getBlock())
431 WriteAsOperand(o, Node->getBlock(), false);
433 o << " <<exit node>>";
437 static void PrintDomTree(const DominatorTreeBase::Node *N, std::ostream &o,
439 o << std::string(2*Lev, ' ') << "[" << Lev << "] " << N;
440 for (DominatorTreeBase::Node::const_iterator I = N->begin(), E = N->end();
442 PrintDomTree(*I, o, Lev+1);
445 void DominatorTreeBase::print(std::ostream &o, const Module* ) const {
446 o << "=============================--------------------------------\n"
447 << "Inorder Dominator Tree:\n";
448 PrintDomTree(getRootNode(), o, 1);
452 //===----------------------------------------------------------------------===//
453 // DominanceFrontier Implementation
454 //===----------------------------------------------------------------------===//
456 static RegisterPass<DominanceFrontier>
457 G("domfrontier", "Dominance Frontier Construction", true);
459 const DominanceFrontier::DomSetType &
460 DominanceFrontier::calculate(const DominatorTree &DT,
461 const DominatorTree::Node *Node) {
462 BasicBlock *BB = Node->getBlock();
463 DomSetType *Result = NULL;
465 std::vector<DFCalculateWorkObject> workList;
466 SmallPtrSet<BasicBlock *, 32> visited;
468 workList.push_back(DFCalculateWorkObject(BB, NULL, Node, NULL));
470 DFCalculateWorkObject *currentW = &workList.back();
471 assert (currentW && "Missing work object.");
473 BasicBlock *currentBB = currentW->currentBB;
474 BasicBlock *parentBB = currentW->parentBB;
475 const DominatorTree::Node *currentNode = currentW->Node;
476 const DominatorTree::Node *parentNode = currentW->parentNode;
477 assert (currentBB && "Invalid work object. Missing current Basic Block");
478 assert (currentNode && "Invalid work object. Missing current Node");
479 DomSetType &S = Frontiers[currentBB];
481 // Visit each block only once.
482 if (visited.count(currentBB) == 0) {
483 visited.insert(currentBB);
485 // Loop over CFG successors to calculate DFlocal[currentNode]
486 for (succ_iterator SI = succ_begin(currentBB), SE = succ_end(currentBB);
488 // Does Node immediately dominate this successor?
489 if (DT[*SI]->getIDom() != currentNode)
494 // At this point, S is DFlocal. Now we union in DFup's of our children...
495 // Loop through and visit the nodes that Node immediately dominates (Node's
496 // children in the IDomTree)
497 bool visitChild = false;
498 for (DominatorTree::Node::const_iterator NI = currentNode->begin(),
499 NE = currentNode->end(); NI != NE; ++NI) {
500 DominatorTree::Node *IDominee = *NI;
501 BasicBlock *childBB = IDominee->getBlock();
502 if (visited.count(childBB) == 0) {
503 workList.push_back(DFCalculateWorkObject(childBB, currentBB, IDominee, currentNode));
508 // If all children are visited or there is any child then pop this block
509 // from the workList.
517 DomSetType::const_iterator CDFI = S.begin(), CDFE = S.end();
518 DomSetType &parentSet = Frontiers[parentBB];
519 for (; CDFI != CDFE; ++CDFI) {
520 if (!parentNode->properlyDominates(DT[*CDFI]))
521 parentSet.insert(*CDFI);
526 } while (!workList.empty());
531 void DominanceFrontierBase::print(std::ostream &o, const Module* ) const {
532 for (const_iterator I = begin(), E = end(); I != E; ++I) {
533 o << " DomFrontier for BB";
535 WriteAsOperand(o, I->first, false);
537 o << " <<exit node>>";
538 o << " is:\t" << I->second << "\n";
542 //===----------------------------------------------------------------------===//
543 // ETOccurrence Implementation
544 //===----------------------------------------------------------------------===//
546 void ETOccurrence::Splay() {
547 ETOccurrence *father;
548 ETOccurrence *grandfather;
556 fatherdepth = Parent->Depth;
557 grandfather = father->Parent;
559 // If we have no grandparent, a single zig or zag will do.
561 setDepthAdd(fatherdepth);
562 MinOccurrence = father->MinOccurrence;
565 // See what we have to rotate
566 if (father->Left == this) {
568 father->setLeft(Right);
571 father->Left->setDepthAdd(occdepth);
574 father->setRight(Left);
577 father->Right->setDepthAdd(occdepth);
579 father->setDepth(-occdepth);
582 father->recomputeMin();
586 // If we have a grandfather, we need to do some
587 // combination of zig and zag.
588 int grandfatherdepth = grandfather->Depth;
590 setDepthAdd(fatherdepth + grandfatherdepth);
591 MinOccurrence = grandfather->MinOccurrence;
592 Min = grandfather->Min;
594 ETOccurrence *greatgrandfather = grandfather->Parent;
596 if (grandfather->Left == father) {
597 if (father->Left == this) {
599 grandfather->setLeft(father->Right);
600 father->setLeft(Right);
602 father->setRight(grandfather);
604 father->setDepth(-occdepth);
607 father->Left->setDepthAdd(occdepth);
609 grandfather->setDepth(-fatherdepth);
610 if (grandfather->Left)
611 grandfather->Left->setDepthAdd(fatherdepth);
614 grandfather->setLeft(Right);
615 father->setRight(Left);
617 setRight(grandfather);
619 father->setDepth(-occdepth);
621 father->Right->setDepthAdd(occdepth);
622 grandfather->setDepth(-occdepth - fatherdepth);
623 if (grandfather->Left)
624 grandfather->Left->setDepthAdd(occdepth + fatherdepth);
627 if (father->Left == this) {
629 grandfather->setRight(Left);
630 father->setLeft(Right);
631 setLeft(grandfather);
634 father->setDepth(-occdepth);
636 father->Left->setDepthAdd(occdepth);
637 grandfather->setDepth(-occdepth - fatherdepth);
638 if (grandfather->Right)
639 grandfather->Right->setDepthAdd(occdepth + fatherdepth);
641 grandfather->setRight(father->Left);
642 father->setRight(Left);
644 father->setLeft(grandfather);
646 father->setDepth(-occdepth);
648 father->Right->setDepthAdd(occdepth);
649 grandfather->setDepth(-fatherdepth);
650 if (grandfather->Right)
651 grandfather->Right->setDepthAdd(fatherdepth);
655 // Might need one more rotate depending on greatgrandfather.
656 setParent(greatgrandfather);
657 if (greatgrandfather) {
658 if (greatgrandfather->Left == grandfather)
659 greatgrandfather->Left = this;
661 greatgrandfather->Right = this;
664 grandfather->recomputeMin();
665 father->recomputeMin();
669 //===----------------------------------------------------------------------===//
670 // ETNode implementation
671 //===----------------------------------------------------------------------===//
673 void ETNode::Split() {
674 ETOccurrence *right, *left;
675 ETOccurrence *rightmost = RightmostOcc;
676 ETOccurrence *parent;
678 // Update the occurrence tree first.
679 RightmostOcc->Splay();
681 // Find the leftmost occurrence in the rightmost subtree, then splay
683 for (right = rightmost->Right; right->Left; right = right->Left);
688 right->Left->Parent = NULL;
694 parent->Right->Parent = NULL;
696 right->setLeft(left);
698 right->recomputeMin();
701 rightmost->Depth = 0;
706 // Now update *our* tree
708 if (Father->Son == this)
711 if (Father->Son == this)
721 void ETNode::setFather(ETNode *NewFather) {
722 ETOccurrence *rightmost;
723 ETOccurrence *leftpart;
724 ETOccurrence *NewFatherOcc;
727 // First update the path in the splay tree
728 NewFatherOcc = new ETOccurrence(NewFather);
730 rightmost = NewFather->RightmostOcc;
733 leftpart = rightmost->Left;
738 NewFatherOcc->setLeft(leftpart);
739 NewFatherOcc->setRight(temp);
743 NewFatherOcc->recomputeMin();
745 rightmost->setLeft(NewFatherOcc);
747 if (NewFatherOcc->Min + rightmost->Depth < rightmost->Min) {
748 rightmost->Min = NewFatherOcc->Min + rightmost->Depth;
749 rightmost->MinOccurrence = NewFatherOcc->MinOccurrence;
753 ParentOcc = NewFatherOcc;
775 bool ETNode::Below(ETNode *other) {
776 ETOccurrence *up = other->RightmostOcc;
777 ETOccurrence *down = RightmostOcc;
784 ETOccurrence *left, *right;
794 right->Parent = NULL;
798 if (left == down || left->Parent != NULL) {
805 // If the two occurrences are in different trees, put things
806 // back the way they were.
807 if (right && right->Parent != NULL)
814 if (down->Depth <= 0)
817 return !down->Right || down->Right->Min + down->Depth >= 0;
820 ETNode *ETNode::NCA(ETNode *other) {
821 ETOccurrence *occ1 = RightmostOcc;
822 ETOccurrence *occ2 = other->RightmostOcc;
824 ETOccurrence *left, *right, *ret;
825 ETOccurrence *occmin;
839 right->Parent = NULL;
842 if (left == occ2 || (left && left->Parent != NULL)) {
847 right->Parent = occ1;
851 occ1->setRight(occ2);
856 if (occ2->Depth > 0) {
858 mindepth = occ1->Depth;
861 mindepth = occ2->Depth + occ1->Depth;
864 if (ret && ret->Min + occ1->Depth + occ2->Depth < mindepth)
865 return ret->MinOccurrence->OccFor;
867 return occmin->OccFor;
870 void ETNode::assignDFSNumber(int num) {
871 std::vector<ETNode *> workStack;
872 std::set<ETNode *> visitedNodes;
874 workStack.push_back(this);
875 visitedNodes.insert(this);
876 this->DFSNumIn = num++;
878 while (!workStack.empty()) {
879 ETNode *Node = workStack.back();
881 // If this is leaf node then set DFSNumOut and pop the stack
883 Node->DFSNumOut = num++;
884 workStack.pop_back();
888 ETNode *son = Node->Son;
890 // Visit Node->Son first
891 if (visitedNodes.count(son) == 0) {
892 son->DFSNumIn = num++;
893 workStack.push_back(son);
894 visitedNodes.insert(son);
898 bool visitChild = false;
899 // Visit remaining children
900 for (ETNode *s = son->Right; s != son && !visitChild; s = s->Right) {
901 if (visitedNodes.count(s) == 0) {
904 workStack.push_back(s);
905 visitedNodes.insert(s);
910 // If we reach here means all children are visited
911 Node->DFSNumOut = num++;
912 workStack.pop_back();
917 //===----------------------------------------------------------------------===//
918 // ETForest implementation
919 //===----------------------------------------------------------------------===//
921 static RegisterPass<ETForest>
922 D("etforest", "ET Forest Construction", true);
924 void ETForestBase::reset() {
925 for (ETMapType::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I)
930 void ETForestBase::updateDFSNumbers()
933 // Iterate over all nodes in depth first order.
934 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
935 for (df_iterator<BasicBlock*> I = df_begin(Roots[i]),
936 E = df_end(Roots[i]); I != E; ++I) {
938 ETNode *ETN = getNode(BB);
939 if (ETN && !ETN->hasFather())
940 ETN->assignDFSNumber(dfsnum);
946 // dominates - Return true if A dominates B. THis performs the
947 // special checks necessary if A and B are in the same basic block.
948 bool ETForestBase::dominates(Instruction *A, Instruction *B) {
949 BasicBlock *BBA = A->getParent(), *BBB = B->getParent();
950 if (BBA != BBB) return dominates(BBA, BBB);
952 // Loop through the basic block until we find A or B.
953 BasicBlock::iterator I = BBA->begin();
954 for (; &*I != A && &*I != B; ++I) /*empty*/;
956 // It is not possible to determine dominance between two PHI nodes
957 // based on their ordering.
958 if (isa<PHINode>(A) && isa<PHINode>(B))
961 if(!IsPostDominators) {
962 // A dominates B if it is found first in the basic block.
965 // A post-dominates B if B is found first in the basic block.
970 ETNode *ETForest::getNodeForBlock(BasicBlock *BB) {
971 ETNode *&BBNode = Nodes[BB];
972 if (BBNode) return BBNode;
974 // Haven't calculated this node yet? Get or calculate the node for the
975 // immediate dominator.
976 BasicBlock *IDom = getAnalysis<ImmediateDominators>()[BB];
978 // If we are unreachable, we may not have an immediate dominator.
980 return BBNode = new ETNode(BB);
982 ETNode *IDomNode = getNodeForBlock(IDom);
984 // Add a new tree node for this BasicBlock, and link it as a child of
986 BBNode = new ETNode(BB);
987 BBNode->setFather(IDomNode);
992 void ETForest::calculate(const ImmediateDominators &ID) {
993 assert(Roots.size() == 1 && "ETForest should have 1 root block!");
994 BasicBlock *Root = Roots[0];
995 Nodes[Root] = new ETNode(Root); // Add a node for the root
997 Function *F = Root->getParent();
998 // Loop over all of the reachable blocks in the function...
999 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
1000 if (BasicBlock *ImmDom = ID.get(I)) { // Reachable block.
1001 ETNode *&BBNode = Nodes[I];
1002 if (!BBNode) { // Haven't calculated this node yet?
1003 // Get or calculate the node for the immediate dominator
1004 ETNode *IDomNode = getNodeForBlock(ImmDom);
1006 // Add a new ETNode for this BasicBlock, and set it's parent
1007 // to it's immediate dominator.
1008 BBNode = new ETNode(I);
1009 BBNode->setFather(IDomNode);
1013 // Make sure we've got nodes around for every block
1014 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
1015 ETNode *&BBNode = Nodes[I];
1017 BBNode = new ETNode(I);
1020 updateDFSNumbers ();
1023 //===----------------------------------------------------------------------===//
1024 // ETForestBase Implementation
1025 //===----------------------------------------------------------------------===//
1027 void ETForestBase::addNewBlock(BasicBlock *BB, BasicBlock *IDom) {
1028 ETNode *&BBNode = Nodes[BB];
1029 assert(!BBNode && "BasicBlock already in ET-Forest");
1031 BBNode = new ETNode(BB);
1032 BBNode->setFather(getNode(IDom));
1033 DFSInfoValid = false;
1036 void ETForestBase::setImmediateDominator(BasicBlock *BB, BasicBlock *newIDom) {
1037 assert(getNode(BB) && "BasicBlock not in ET-Forest");
1038 assert(getNode(newIDom) && "IDom not in ET-Forest");
1040 ETNode *Node = getNode(BB);
1041 if (Node->hasFather()) {
1042 if (Node->getFather()->getData<BasicBlock>() == newIDom)
1046 Node->setFather(getNode(newIDom));
1047 DFSInfoValid= false;
1050 void ETForestBase::print(std::ostream &o, const Module *) const {
1051 o << "=============================--------------------------------\n";
1052 o << "ET Forest:\n";
1058 o << " up to date\n";
1060 Function *F = getRoots()[0]->getParent();
1061 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
1062 o << " DFS Numbers For Basic Block:";
1063 WriteAsOperand(o, I, false);
1065 if (ETNode *EN = getNode(I)) {
1066 o << "In: " << EN->getDFSNumIn();
1067 o << " Out: " << EN->getDFSNumOut() << "\n";
1069 o << "No associated ETNode";
1076 DEFINING_FILE_FOR(DominatorSet)