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/Instructions.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);
51 class DFCalculateWorkObject {
53 DFCalculateWorkObject(BasicBlock *B, BasicBlock *P,
54 const DominatorTree::Node *N,
55 const DominatorTree::Node *PN)
56 : currentBB(B), parentBB(P), Node(N), parentNode(PN) {}
57 BasicBlock *currentBB;
59 const DominatorTree::Node *Node;
60 const DominatorTree::Node *parentNode;
63 unsigned ImmediateDominators::DFSPass(BasicBlock *V, InfoRec &VInfo,
68 Vertex.push_back(V); // Vertex[n] = V;
69 //Info[V].Ancestor = 0; // Ancestor[n] = 0
70 //Child[V] = 0; // Child[v] = 0
71 VInfo.Size = 1; // Size[v] = 1
73 for (succ_iterator SI = succ_begin(V), E = succ_end(V); SI != E; ++SI) {
74 InfoRec &SuccVInfo = Info[*SI];
75 if (SuccVInfo.Semi == 0) {
77 N = DFSPass(*SI, SuccVInfo, N);
83 void ImmediateDominators::Compress(BasicBlock *V, InfoRec &VInfo) {
84 BasicBlock *VAncestor = VInfo.Ancestor;
85 InfoRec &VAInfo = Info[VAncestor];
86 if (VAInfo.Ancestor == 0)
89 Compress(VAncestor, VAInfo);
91 BasicBlock *VAncestorLabel = VAInfo.Label;
92 BasicBlock *VLabel = VInfo.Label;
93 if (Info[VAncestorLabel].Semi < Info[VLabel].Semi)
94 VInfo.Label = VAncestorLabel;
96 VInfo.Ancestor = VAInfo.Ancestor;
99 BasicBlock *ImmediateDominators::Eval(BasicBlock *V) {
100 InfoRec &VInfo = Info[V];
101 #if !BALANCE_IDOM_TREE
102 // Higher-complexity but faster implementation
103 if (VInfo.Ancestor == 0)
108 // Lower-complexity but slower implementation
109 if (VInfo.Ancestor == 0)
112 BasicBlock *VLabel = VInfo.Label;
114 BasicBlock *VAncestorLabel = Info[VInfo.Ancestor].Label;
115 if (Info[VAncestorLabel].Semi >= Info[VLabel].Semi)
118 return VAncestorLabel;
122 void ImmediateDominators::Link(BasicBlock *V, BasicBlock *W, InfoRec &WInfo){
123 #if !BALANCE_IDOM_TREE
124 // Higher-complexity but faster implementation
127 // Lower-complexity but slower implementation
128 BasicBlock *WLabel = WInfo.Label;
129 unsigned WLabelSemi = Info[WLabel].Semi;
131 InfoRec *SInfo = &Info[S];
133 BasicBlock *SChild = SInfo->Child;
134 InfoRec *SChildInfo = &Info[SChild];
136 while (WLabelSemi < Info[SChildInfo->Label].Semi) {
137 BasicBlock *SChildChild = SChildInfo->Child;
138 if (SInfo->Size+Info[SChildChild].Size >= 2*SChildInfo->Size) {
139 SChildInfo->Ancestor = S;
140 SInfo->Child = SChild = SChildChild;
141 SChildInfo = &Info[SChild];
143 SChildInfo->Size = SInfo->Size;
144 S = SInfo->Ancestor = SChild;
146 SChild = SChildChild;
147 SChildInfo = &Info[SChild];
151 InfoRec &VInfo = Info[V];
152 SInfo->Label = WLabel;
154 assert(V != W && "The optimization here will not work in this case!");
155 unsigned WSize = WInfo.Size;
156 unsigned VSize = (VInfo.Size += WSize);
159 std::swap(S, VInfo.Child);
171 bool ImmediateDominators::runOnFunction(Function &F) {
172 IDoms.clear(); // Reset from the last time we were run...
173 BasicBlock *Root = &F.getEntryBlock();
175 Roots.push_back(Root);
179 // Step #1: Number blocks in depth-first order and initialize variables used
180 // in later stages of the algorithm.
182 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
183 N = DFSPass(Roots[i], Info[Roots[i]], 0);
185 for (unsigned i = N; i >= 2; --i) {
186 BasicBlock *W = Vertex[i];
187 InfoRec &WInfo = Info[W];
189 // Step #2: Calculate the semidominators of all vertices
190 for (pred_iterator PI = pred_begin(W), E = pred_end(W); PI != E; ++PI)
191 if (Info.count(*PI)) { // Only if this predecessor is reachable!
192 unsigned SemiU = Info[Eval(*PI)].Semi;
193 if (SemiU < WInfo.Semi)
197 Info[Vertex[WInfo.Semi]].Bucket.push_back(W);
199 BasicBlock *WParent = WInfo.Parent;
200 Link(WParent, W, WInfo);
202 // Step #3: Implicitly define the immediate dominator of vertices
203 std::vector<BasicBlock*> &WParentBucket = Info[WParent].Bucket;
204 while (!WParentBucket.empty()) {
205 BasicBlock *V = WParentBucket.back();
206 WParentBucket.pop_back();
207 BasicBlock *U = Eval(V);
208 IDoms[V] = Info[U].Semi < Info[V].Semi ? U : WParent;
212 // Step #4: Explicitly define the immediate dominator of each vertex
213 for (unsigned i = 2; i <= N; ++i) {
214 BasicBlock *W = Vertex[i];
215 BasicBlock *&WIDom = IDoms[W];
216 if (WIDom != Vertex[Info[W].Semi])
217 WIDom = IDoms[WIDom];
220 // Free temporary memory used to construct idom's
222 std::vector<BasicBlock*>().swap(Vertex);
227 /// dominates - Return true if A dominates B.
229 bool ImmediateDominatorsBase::dominates(BasicBlock *A, BasicBlock *B) const {
230 assert(A && B && "Null pointers?");
232 // Walk up the dominator tree from B to determine if A dom B.
238 void ImmediateDominatorsBase::print(std::ostream &o, const Module* ) const {
239 Function *F = getRoots()[0]->getParent();
240 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
241 o << " Immediate Dominator For Basic Block:";
242 WriteAsOperand(o, I, false);
244 if (BasicBlock *ID = get(I))
245 WriteAsOperand(o, ID, false);
247 o << " <<exit node>>";
255 //===----------------------------------------------------------------------===//
256 // DominatorSet Implementation
257 //===----------------------------------------------------------------------===//
259 static RegisterPass<DominatorSet>
260 B("domset", "Dominator Set Construction", true);
262 // dominates - Return true if A dominates B. This performs the special checks
263 // necessary if A and B are in the same basic block.
265 bool DominatorSetBase::dominates(Instruction *A, Instruction *B) const {
266 BasicBlock *BBA = A->getParent(), *BBB = B->getParent();
267 if (BBA != BBB) return dominates(BBA, BBB);
269 // It is not possible to determie dominance between two PHI nodes
270 // based on their ordering.
271 if (isa<PHINode>(A) && isa<PHINode>(B))
274 // Loop through the basic block until we find A or B.
275 BasicBlock::iterator I = BBA->begin();
276 for (; &*I != A && &*I != B; ++I) /*empty*/;
278 if(!IsPostDominators) {
279 // A dominates B if it is found first in the basic block.
282 // A post-dominates B if B is found first in the basic block.
288 // runOnFunction - This method calculates the forward dominator sets for the
289 // specified function.
291 bool DominatorSet::runOnFunction(Function &F) {
292 BasicBlock *Root = &F.getEntryBlock();
294 Roots.push_back(Root);
295 assert(pred_begin(Root) == pred_end(Root) &&
296 "Root node has predecessors in function!");
298 ImmediateDominators &ID = getAnalysis<ImmediateDominators>();
300 if (Roots.empty()) return false;
302 // Root nodes only dominate themselves.
303 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
304 Doms[Roots[i]].insert(Roots[i]);
306 // Loop over all of the blocks in the function, calculating dominator sets for
308 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
309 if (BasicBlock *IDom = ID[I]) { // Get idom if block is reachable
310 DomSetType &DS = Doms[I];
311 assert(DS.empty() && "Domset already filled in for this block?");
312 DS.insert(I); // Blocks always dominate themselves
314 // Insert all dominators into the set...
316 // If we have already computed the dominator sets for our immediate
317 // dominator, just use it instead of walking all the way up to the root.
318 DomSetType &IDS = Doms[IDom];
320 DS.insert(IDS.begin(), IDS.end());
328 // Ensure that every basic block has at least an empty set of nodes. This
329 // is important for the case when there is unreachable blocks.
337 static std::ostream &operator<<(std::ostream &o,
338 const std::set<BasicBlock*> &BBs) {
339 for (std::set<BasicBlock*>::const_iterator I = BBs.begin(), E = BBs.end();
342 WriteAsOperand(o, *I, false);
344 o << " <<exit node>>";
349 void DominatorSetBase::print(std::ostream &o, const Module* ) const {
350 for (const_iterator I = begin(), E = end(); I != E; ++I) {
351 o << " DomSet For BB: ";
353 WriteAsOperand(o, I->first, false);
355 o << " <<exit node>>";
356 o << " is:\t" << I->second << "\n";
360 //===----------------------------------------------------------------------===//
361 // DominatorTree Implementation
362 //===----------------------------------------------------------------------===//
364 static RegisterPass<DominatorTree>
365 E("domtree", "Dominator Tree Construction", true);
367 // DominatorTreeBase::reset - Free all of the tree node memory.
369 void DominatorTreeBase::reset() {
370 for (NodeMapType::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I)
376 void DominatorTreeBase::Node::setIDom(Node *NewIDom) {
377 assert(IDom && "No immediate dominator?");
378 if (IDom != NewIDom) {
379 std::vector<Node*>::iterator I =
380 std::find(IDom->Children.begin(), IDom->Children.end(), this);
381 assert(I != IDom->Children.end() &&
382 "Not in immediate dominator children set!");
383 // I am no longer your child...
384 IDom->Children.erase(I);
386 // Switch to new dominator
388 IDom->Children.push_back(this);
392 DominatorTreeBase::Node *DominatorTree::getNodeForBlock(BasicBlock *BB) {
393 Node *&BBNode = Nodes[BB];
394 if (BBNode) return BBNode;
396 // Haven't calculated this node yet? Get or calculate the node for the
397 // immediate dominator.
398 BasicBlock *IDom = getAnalysis<ImmediateDominators>()[BB];
399 Node *IDomNode = getNodeForBlock(IDom);
401 // Add a new tree node for this BasicBlock, and link it as a child of
403 return BBNode = IDomNode->addChild(new Node(BB, IDomNode));
406 void DominatorTree::calculate(const ImmediateDominators &ID) {
407 assert(Roots.size() == 1 && "DominatorTree should have 1 root block!");
408 BasicBlock *Root = Roots[0];
409 Nodes[Root] = RootNode = new Node(Root, 0); // Add a node for the root...
411 Function *F = Root->getParent();
412 // Loop over all of the reachable blocks in the function...
413 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
414 if (BasicBlock *ImmDom = ID.get(I)) { // Reachable block.
415 Node *&BBNode = Nodes[I];
416 if (!BBNode) { // Haven't calculated this node yet?
417 // Get or calculate the node for the immediate dominator
418 Node *IDomNode = getNodeForBlock(ImmDom);
420 // Add a new tree node for this BasicBlock, and link it as a child of
422 BBNode = IDomNode->addChild(new Node(I, IDomNode));
427 static std::ostream &operator<<(std::ostream &o,
428 const DominatorTreeBase::Node *Node) {
429 if (Node->getBlock())
430 WriteAsOperand(o, Node->getBlock(), false);
432 o << " <<exit node>>";
436 static void PrintDomTree(const DominatorTreeBase::Node *N, std::ostream &o,
438 o << std::string(2*Lev, ' ') << "[" << Lev << "] " << N;
439 for (DominatorTreeBase::Node::const_iterator I = N->begin(), E = N->end();
441 PrintDomTree(*I, o, Lev+1);
444 void DominatorTreeBase::print(std::ostream &o, const Module* ) const {
445 o << "=============================--------------------------------\n"
446 << "Inorder Dominator Tree:\n";
447 PrintDomTree(getRootNode(), o, 1);
451 //===----------------------------------------------------------------------===//
452 // DominanceFrontier Implementation
453 //===----------------------------------------------------------------------===//
455 static RegisterPass<DominanceFrontier>
456 G("domfrontier", "Dominance Frontier Construction", true);
458 const DominanceFrontier::DomSetType &
459 DominanceFrontier::calculate(const DominatorTree &DT,
460 const DominatorTree::Node *Node) {
461 BasicBlock *BB = Node->getBlock();
462 DomSetType *Result = NULL;
464 std::vector<DFCalculateWorkObject> workList;
465 std::set<BasicBlock *> visited;
467 workList.push_back(DFCalculateWorkObject(BB, NULL, Node, NULL));
469 DFCalculateWorkObject *currentW = &workList.back();
470 assert (currentW && "Missing work object.");
472 BasicBlock *currentBB = currentW->currentBB;
473 BasicBlock *parentBB = currentW->parentBB;
474 const DominatorTree::Node *currentNode = currentW->Node;
475 const DominatorTree::Node *parentNode = currentW->parentNode;
476 assert (currentBB && "Invalid work object. Missing current Basic Block");
477 assert (currentNode && "Invalid work object. Missing current Node");
478 DomSetType &S = Frontiers[currentBB];
480 // Visit each block only once.
481 if (visited.count(currentBB) == 0) {
482 visited.insert(currentBB);
484 // Loop over CFG successors to calculate DFlocal[currentNode]
485 for (succ_iterator SI = succ_begin(currentBB), SE = succ_end(currentBB);
487 // Does Node immediately dominate this successor?
488 if (DT[*SI]->getIDom() != currentNode)
493 // At this point, S is DFlocal. Now we union in DFup's of our children...
494 // Loop through and visit the nodes that Node immediately dominates (Node's
495 // children in the IDomTree)
496 bool visitChild = false;
497 for (DominatorTree::Node::const_iterator NI = currentNode->begin(),
498 NE = currentNode->end(); NI != NE; ++NI) {
499 DominatorTree::Node *IDominee = *NI;
500 BasicBlock *childBB = IDominee->getBlock();
501 if (visited.count(childBB) == 0) {
502 workList.push_back(DFCalculateWorkObject(childBB, currentBB, IDominee, currentNode));
507 // If all children are visited or there is any child then pop this block
508 // from the workList.
516 DomSetType::const_iterator CDFI = S.begin(), CDFE = S.end();
517 DomSetType &parentSet = Frontiers[parentBB];
518 for (; CDFI != CDFE; ++CDFI) {
519 if (!parentNode->properlyDominates(DT[*CDFI]))
520 parentSet.insert(*CDFI);
525 } while (!workList.empty());
530 void DominanceFrontierBase::print(std::ostream &o, const Module* ) const {
531 for (const_iterator I = begin(), E = end(); I != E; ++I) {
532 o << " DomFrontier for BB";
534 WriteAsOperand(o, I->first, false);
536 o << " <<exit node>>";
537 o << " is:\t" << I->second << "\n";
541 //===----------------------------------------------------------------------===//
542 // ETOccurrence Implementation
543 //===----------------------------------------------------------------------===//
545 void ETOccurrence::Splay() {
546 ETOccurrence *father;
547 ETOccurrence *grandfather;
555 fatherdepth = Parent->Depth;
556 grandfather = father->Parent;
558 // If we have no grandparent, a single zig or zag will do.
560 setDepthAdd(fatherdepth);
561 MinOccurrence = father->MinOccurrence;
564 // See what we have to rotate
565 if (father->Left == this) {
567 father->setLeft(Right);
570 father->Left->setDepthAdd(occdepth);
573 father->setRight(Left);
576 father->Right->setDepthAdd(occdepth);
578 father->setDepth(-occdepth);
581 father->recomputeMin();
585 // If we have a grandfather, we need to do some
586 // combination of zig and zag.
587 int grandfatherdepth = grandfather->Depth;
589 setDepthAdd(fatherdepth + grandfatherdepth);
590 MinOccurrence = grandfather->MinOccurrence;
591 Min = grandfather->Min;
593 ETOccurrence *greatgrandfather = grandfather->Parent;
595 if (grandfather->Left == father) {
596 if (father->Left == this) {
598 grandfather->setLeft(father->Right);
599 father->setLeft(Right);
601 father->setRight(grandfather);
603 father->setDepth(-occdepth);
606 father->Left->setDepthAdd(occdepth);
608 grandfather->setDepth(-fatherdepth);
609 if (grandfather->Left)
610 grandfather->Left->setDepthAdd(fatherdepth);
613 grandfather->setLeft(Right);
614 father->setRight(Left);
616 setRight(grandfather);
618 father->setDepth(-occdepth);
620 father->Right->setDepthAdd(occdepth);
621 grandfather->setDepth(-occdepth - fatherdepth);
622 if (grandfather->Left)
623 grandfather->Left->setDepthAdd(occdepth + fatherdepth);
626 if (father->Left == this) {
628 grandfather->setRight(Left);
629 father->setLeft(Right);
630 setLeft(grandfather);
633 father->setDepth(-occdepth);
635 father->Left->setDepthAdd(occdepth);
636 grandfather->setDepth(-occdepth - fatherdepth);
637 if (grandfather->Right)
638 grandfather->Right->setDepthAdd(occdepth + fatherdepth);
640 grandfather->setRight(father->Left);
641 father->setRight(Left);
643 father->setLeft(grandfather);
645 father->setDepth(-occdepth);
647 father->Right->setDepthAdd(occdepth);
648 grandfather->setDepth(-fatherdepth);
649 if (grandfather->Right)
650 grandfather->Right->setDepthAdd(fatherdepth);
654 // Might need one more rotate depending on greatgrandfather.
655 setParent(greatgrandfather);
656 if (greatgrandfather) {
657 if (greatgrandfather->Left == grandfather)
658 greatgrandfather->Left = this;
660 greatgrandfather->Right = this;
663 grandfather->recomputeMin();
664 father->recomputeMin();
668 //===----------------------------------------------------------------------===//
669 // ETNode implementation
670 //===----------------------------------------------------------------------===//
672 void ETNode::Split() {
673 ETOccurrence *right, *left;
674 ETOccurrence *rightmost = RightmostOcc;
675 ETOccurrence *parent;
677 // Update the occurrence tree first.
678 RightmostOcc->Splay();
680 // Find the leftmost occurrence in the rightmost subtree, then splay
682 for (right = rightmost->Right; right->Left; right = right->Left);
687 right->Left->Parent = NULL;
693 parent->Right->Parent = NULL;
695 right->setLeft(left);
697 right->recomputeMin();
700 rightmost->Depth = 0;
705 // Now update *our* tree
707 if (Father->Son == this)
710 if (Father->Son == this)
720 void ETNode::setFather(ETNode *NewFather) {
721 ETOccurrence *rightmost;
722 ETOccurrence *leftpart;
723 ETOccurrence *NewFatherOcc;
726 // First update the path in the splay tree
727 NewFatherOcc = new ETOccurrence(NewFather);
729 rightmost = NewFather->RightmostOcc;
732 leftpart = rightmost->Left;
737 NewFatherOcc->setLeft(leftpart);
738 NewFatherOcc->setRight(temp);
742 NewFatherOcc->recomputeMin();
744 rightmost->setLeft(NewFatherOcc);
746 if (NewFatherOcc->Min + rightmost->Depth < rightmost->Min) {
747 rightmost->Min = NewFatherOcc->Min + rightmost->Depth;
748 rightmost->MinOccurrence = NewFatherOcc->MinOccurrence;
752 ParentOcc = NewFatherOcc;
774 bool ETNode::Below(ETNode *other) {
775 ETOccurrence *up = other->RightmostOcc;
776 ETOccurrence *down = RightmostOcc;
783 ETOccurrence *left, *right;
793 right->Parent = NULL;
797 if (left == down || left->Parent != NULL) {
804 // If the two occurrences are in different trees, put things
805 // back the way they were.
806 if (right && right->Parent != NULL)
813 if (down->Depth <= 0)
816 return !down->Right || down->Right->Min + down->Depth >= 0;
819 ETNode *ETNode::NCA(ETNode *other) {
820 ETOccurrence *occ1 = RightmostOcc;
821 ETOccurrence *occ2 = other->RightmostOcc;
823 ETOccurrence *left, *right, *ret;
824 ETOccurrence *occmin;
838 right->Parent = NULL;
841 if (left == occ2 || (left && left->Parent != NULL)) {
846 right->Parent = occ1;
850 occ1->setRight(occ2);
855 if (occ2->Depth > 0) {
857 mindepth = occ1->Depth;
860 mindepth = occ2->Depth + occ1->Depth;
863 if (ret && ret->Min + occ1->Depth + occ2->Depth < mindepth)
864 return ret->MinOccurrence->OccFor;
866 return occmin->OccFor;
869 void ETNode::assignDFSNumber(int num) {
870 std::vector<ETNode *> workStack;
871 std::set<ETNode *> visitedNodes;
873 workStack.push_back(this);
874 visitedNodes.insert(this);
875 this->DFSNumIn = num++;
877 while (!workStack.empty()) {
878 ETNode *Node = workStack.back();
880 // If this is leaf node then set DFSNumOut and pop the stack
882 Node->DFSNumOut = num++;
883 workStack.pop_back();
887 ETNode *son = Node->Son;
889 // Visit Node->Son first
890 if (visitedNodes.count(son) == 0) {
891 son->DFSNumIn = num++;
892 workStack.push_back(son);
893 visitedNodes.insert(son);
897 bool visitChild = false;
898 // Visit remaining children
899 for (ETNode *s = son->Right; s != son && !visitChild; s = s->Right) {
900 if (visitedNodes.count(s) == 0) {
903 workStack.push_back(s);
904 visitedNodes.insert(s);
909 // If we reach here means all children are visited
910 Node->DFSNumOut = num++;
911 workStack.pop_back();
916 //===----------------------------------------------------------------------===//
917 // ETForest implementation
918 //===----------------------------------------------------------------------===//
920 static RegisterPass<ETForest>
921 D("etforest", "ET Forest Construction", true);
923 void ETForestBase::reset() {
924 for (ETMapType::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I)
929 void ETForestBase::updateDFSNumbers()
932 // Iterate over all nodes in depth first order.
933 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
934 for (df_iterator<BasicBlock*> I = df_begin(Roots[i]),
935 E = df_end(Roots[i]); I != E; ++I) {
937 if (!getNode(BB)->hasFather())
938 getNode(BB)->assignDFSNumber(dfsnum);
944 // dominates - Return true if A dominates B. THis performs the
945 // special checks necessary if A and B are in the same basic block.
946 bool ETForestBase::dominates(Instruction *A, Instruction *B) {
947 BasicBlock *BBA = A->getParent(), *BBB = B->getParent();
948 if (BBA != BBB) return dominates(BBA, BBB);
950 // Loop through the basic block until we find A or B.
951 BasicBlock::iterator I = BBA->begin();
952 for (; &*I != A && &*I != B; ++I) /*empty*/;
954 if(!IsPostDominators) {
955 // A dominates B if it is found first in the basic block.
958 // A post-dominates B if B is found first in the basic block.
963 ETNode *ETForest::getNodeForBlock(BasicBlock *BB) {
964 ETNode *&BBNode = Nodes[BB];
965 if (BBNode) return BBNode;
967 // Haven't calculated this node yet? Get or calculate the node for the
968 // immediate dominator.
969 BasicBlock *IDom = getAnalysis<ImmediateDominators>()[BB];
971 // If we are unreachable, we may not have an immediate dominator.
973 return BBNode = new ETNode(BB);
975 ETNode *IDomNode = getNodeForBlock(IDom);
977 // Add a new tree node for this BasicBlock, and link it as a child of
979 BBNode = new ETNode(BB);
980 BBNode->setFather(IDomNode);
985 void ETForest::calculate(const ImmediateDominators &ID) {
986 assert(Roots.size() == 1 && "ETForest should have 1 root block!");
987 BasicBlock *Root = Roots[0];
988 Nodes[Root] = new ETNode(Root); // Add a node for the root
990 Function *F = Root->getParent();
991 // Loop over all of the reachable blocks in the function...
992 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
993 if (BasicBlock *ImmDom = ID.get(I)) { // Reachable block.
994 ETNode *&BBNode = Nodes[I];
995 if (!BBNode) { // Haven't calculated this node yet?
996 // Get or calculate the node for the immediate dominator
997 ETNode *IDomNode = getNodeForBlock(ImmDom);
999 // Add a new ETNode for this BasicBlock, and set it's parent
1000 // to it's immediate dominator.
1001 BBNode = new ETNode(I);
1002 BBNode->setFather(IDomNode);
1006 // Make sure we've got nodes around for every block
1007 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
1008 ETNode *&BBNode = Nodes[I];
1010 BBNode = new ETNode(I);
1013 updateDFSNumbers ();
1016 //===----------------------------------------------------------------------===//
1017 // ETForestBase Implementation
1018 //===----------------------------------------------------------------------===//
1020 void ETForestBase::addNewBlock(BasicBlock *BB, BasicBlock *IDom) {
1021 ETNode *&BBNode = Nodes[BB];
1022 assert(!BBNode && "BasicBlock already in ET-Forest");
1024 BBNode = new ETNode(BB);
1025 BBNode->setFather(getNode(IDom));
1026 DFSInfoValid = false;
1029 void ETForestBase::setImmediateDominator(BasicBlock *BB, BasicBlock *newIDom) {
1030 assert(getNode(BB) && "BasicBlock not in ET-Forest");
1031 assert(getNode(newIDom) && "IDom not in ET-Forest");
1033 ETNode *Node = getNode(BB);
1034 if (Node->hasFather()) {
1035 if (Node->getFather()->getData<BasicBlock>() == newIDom)
1039 Node->setFather(getNode(newIDom));
1040 DFSInfoValid= false;
1043 void ETForestBase::print(std::ostream &o, const Module *) const {
1044 o << "=============================--------------------------------\n";
1045 o << "ET Forest:\n";
1051 o << " up to date\n";
1053 Function *F = getRoots()[0]->getParent();
1054 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
1055 o << " DFS Numbers For Basic Block:";
1056 WriteAsOperand(o, I, false);
1058 if (ETNode *EN = getNode(I)) {
1059 o << "In: " << EN->getDFSNumIn();
1060 o << " Out: " << EN->getDFSNumOut() << "\n";
1062 o << "No associated ETNode";
1069 DEFINING_FILE_FOR(DominatorSet)