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/ADT/SmallVector.h"
24 #include "llvm/Instructions.h"
25 #include "llvm/Support/Streams.h"
30 static std::ostream &operator<<(std::ostream &o,
31 const std::set<BasicBlock*> &BBs) {
32 for (std::set<BasicBlock*>::const_iterator I = BBs.begin(), E = BBs.end();
35 WriteAsOperand(o, *I, false);
37 o << " <<exit node>>";
42 //===----------------------------------------------------------------------===//
43 // DominatorTree Implementation
44 //===----------------------------------------------------------------------===//
46 // DominatorTree construction - This pass constructs immediate dominator
47 // information for a flow-graph based on the algorithm described in this
50 // A Fast Algorithm for Finding Dominators in a Flowgraph
51 // T. Lengauer & R. Tarjan, ACM TOPLAS July 1979, pgs 121-141.
53 // This implements both the O(n*ack(n)) and the O(n*log(n)) versions of EVAL and
54 // LINK, but it turns out that the theoretically slower O(n*log(n))
55 // implementation is actually faster than the "efficient" algorithm (even for
56 // large CFGs) because the constant overheads are substantially smaller. The
57 // lower-complexity version can be enabled with the following #define:
59 #define BALANCE_IDOM_TREE 0
61 //===----------------------------------------------------------------------===//
63 char DominatorTree::ID = 0;
64 static RegisterPass<DominatorTree>
65 E("domtree", "Dominator Tree Construction", true);
67 // NewBB is split and now it has one successor. Update dominator tree to
68 // reflect this change.
69 void DominatorTree::splitBlock(BasicBlock *NewBB) {
70 assert(NewBB->getTerminator()->getNumSuccessors() == 1
71 && "NewBB should have a single successor!");
72 BasicBlock *NewBBSucc = NewBB->getTerminator()->getSuccessor(0);
74 std::vector<BasicBlock*> PredBlocks;
75 for (pred_iterator PI = pred_begin(NewBB), PE = pred_end(NewBB);
77 PredBlocks.push_back(*PI);
79 assert(!PredBlocks.empty() && "No predblocks??");
81 // The newly inserted basic block will dominate existing basic blocks iff the
82 // PredBlocks dominate all of the non-pred blocks. If all predblocks dominate
83 // the non-pred blocks, then they all must be the same block!
85 bool NewBBDominatesNewBBSucc = true;
87 BasicBlock *OnePred = PredBlocks[0];
88 unsigned i = 1, e = PredBlocks.size();
89 for (i = 1; !isReachableFromEntry(OnePred); ++i) {
90 assert(i != e && "Didn't find reachable pred?");
91 OnePred = PredBlocks[i];
95 if (PredBlocks[i] != OnePred && isReachableFromEntry(OnePred)) {
96 NewBBDominatesNewBBSucc = false;
100 if (NewBBDominatesNewBBSucc)
101 for (pred_iterator PI = pred_begin(NewBBSucc), E = pred_end(NewBBSucc);
103 if (*PI != NewBB && !dominates(NewBBSucc, *PI)) {
104 NewBBDominatesNewBBSucc = false;
109 // The other scenario where the new block can dominate its successors are when
110 // all predecessors of NewBBSucc that are not NewBB are dominated by NewBBSucc
112 if (!NewBBDominatesNewBBSucc) {
113 NewBBDominatesNewBBSucc = true;
114 for (pred_iterator PI = pred_begin(NewBBSucc), E = pred_end(NewBBSucc);
116 if (*PI != NewBB && !dominates(NewBBSucc, *PI)) {
117 NewBBDominatesNewBBSucc = false;
122 // Find NewBB's immediate dominator and create new dominator tree node for
124 BasicBlock *NewBBIDom = 0;
126 for (i = 0; i < PredBlocks.size(); ++i)
127 if (isReachableFromEntry(PredBlocks[i])) {
128 NewBBIDom = PredBlocks[i];
131 assert(i != PredBlocks.size() && "No reachable preds?");
132 for (i = i + 1; i < PredBlocks.size(); ++i) {
133 if (isReachableFromEntry(PredBlocks[i]))
134 NewBBIDom = findNearestCommonDominator(NewBBIDom, PredBlocks[i]);
136 assert(NewBBIDom && "No immediate dominator found??");
138 // Create the new dominator tree node... and set the idom of NewBB.
139 DomTreeNode *NewBBNode = addNewBlock(NewBB, NewBBIDom);
141 // If NewBB strictly dominates other blocks, then it is now the immediate
142 // dominator of NewBBSucc. Update the dominator tree as appropriate.
143 if (NewBBDominatesNewBBSucc) {
144 DomTreeNode *NewBBSuccNode = getNode(NewBBSucc);
145 changeImmediateDominator(NewBBSuccNode, NewBBNode);
149 unsigned DominatorTree::DFSPass(BasicBlock *V, unsigned N) {
150 // This is more understandable as a recursive algorithm, but we can't use the
151 // recursive algorithm due to stack depth issues. Keep it here for
152 // documentation purposes.
154 InfoRec &VInfo = Info[Roots[i]];
158 Vertex.push_back(V); // Vertex[n] = V;
159 //Info[V].Ancestor = 0; // Ancestor[n] = 0
160 //Info[V].Child = 0; // Child[v] = 0
161 VInfo.Size = 1; // Size[v] = 1
163 for (succ_iterator SI = succ_begin(V), E = succ_end(V); SI != E; ++SI) {
164 InfoRec &SuccVInfo = Info[*SI];
165 if (SuccVInfo.Semi == 0) {
166 SuccVInfo.Parent = V;
171 std::vector<std::pair<BasicBlock*, unsigned> > Worklist;
172 Worklist.push_back(std::make_pair(V, 0U));
173 while (!Worklist.empty()) {
174 BasicBlock *BB = Worklist.back().first;
175 unsigned NextSucc = Worklist.back().second;
177 // First time we visited this BB?
179 InfoRec &BBInfo = Info[BB];
183 Vertex.push_back(BB); // Vertex[n] = V;
184 //BBInfo[V].Ancestor = 0; // Ancestor[n] = 0
185 //BBInfo[V].Child = 0; // Child[v] = 0
186 BBInfo.Size = 1; // Size[v] = 1
189 // If we are done with this block, remove it from the worklist.
190 if (NextSucc == BB->getTerminator()->getNumSuccessors()) {
195 // Otherwise, increment the successor number for the next time we get to it.
196 ++Worklist.back().second;
198 // Visit the successor next, if it isn't already visited.
199 BasicBlock *Succ = BB->getTerminator()->getSuccessor(NextSucc);
201 InfoRec &SuccVInfo = Info[Succ];
202 if (SuccVInfo.Semi == 0) {
203 SuccVInfo.Parent = BB;
204 Worklist.push_back(std::make_pair(Succ, 0U));
211 void DominatorTree::Compress(BasicBlock *VIn) {
213 std::vector<BasicBlock *> Work;
214 SmallPtrSet<BasicBlock *, 32> Visited;
215 BasicBlock *VInAncestor = Info[VIn].Ancestor;
216 InfoRec &VInVAInfo = Info[VInAncestor];
218 if (VInVAInfo.Ancestor != 0)
221 while (!Work.empty()) {
222 BasicBlock *V = Work.back();
223 InfoRec &VInfo = Info[V];
224 BasicBlock *VAncestor = VInfo.Ancestor;
225 InfoRec &VAInfo = Info[VAncestor];
227 // Process Ancestor first
228 if (Visited.insert(VAncestor) &&
229 VAInfo.Ancestor != 0) {
230 Work.push_back(VAncestor);
235 // Update VInfo based on Ancestor info
236 if (VAInfo.Ancestor == 0)
238 BasicBlock *VAncestorLabel = VAInfo.Label;
239 BasicBlock *VLabel = VInfo.Label;
240 if (Info[VAncestorLabel].Semi < Info[VLabel].Semi)
241 VInfo.Label = VAncestorLabel;
242 VInfo.Ancestor = VAInfo.Ancestor;
246 BasicBlock *DominatorTree::Eval(BasicBlock *V) {
247 InfoRec &VInfo = Info[V];
248 #if !BALANCE_IDOM_TREE
249 // Higher-complexity but faster implementation
250 if (VInfo.Ancestor == 0)
255 // Lower-complexity but slower implementation
256 if (VInfo.Ancestor == 0)
259 BasicBlock *VLabel = VInfo.Label;
261 BasicBlock *VAncestorLabel = Info[VInfo.Ancestor].Label;
262 if (Info[VAncestorLabel].Semi >= Info[VLabel].Semi)
265 return VAncestorLabel;
269 void DominatorTree::Link(BasicBlock *V, BasicBlock *W, InfoRec &WInfo){
270 #if !BALANCE_IDOM_TREE
271 // Higher-complexity but faster implementation
274 // Lower-complexity but slower implementation
275 BasicBlock *WLabel = WInfo.Label;
276 unsigned WLabelSemi = Info[WLabel].Semi;
278 InfoRec *SInfo = &Info[S];
280 BasicBlock *SChild = SInfo->Child;
281 InfoRec *SChildInfo = &Info[SChild];
283 while (WLabelSemi < Info[SChildInfo->Label].Semi) {
284 BasicBlock *SChildChild = SChildInfo->Child;
285 if (SInfo->Size+Info[SChildChild].Size >= 2*SChildInfo->Size) {
286 SChildInfo->Ancestor = S;
287 SInfo->Child = SChild = SChildChild;
288 SChildInfo = &Info[SChild];
290 SChildInfo->Size = SInfo->Size;
291 S = SInfo->Ancestor = SChild;
293 SChild = SChildChild;
294 SChildInfo = &Info[SChild];
298 InfoRec &VInfo = Info[V];
299 SInfo->Label = WLabel;
301 assert(V != W && "The optimization here will not work in this case!");
302 unsigned WSize = WInfo.Size;
303 unsigned VSize = (VInfo.Size += WSize);
306 std::swap(S, VInfo.Child);
316 void DominatorTree::calculate(Function &F) {
317 BasicBlock* Root = Roots[0];
319 // Add a node for the root...
320 DomTreeNodes[Root] = RootNode = new DomTreeNode(Root, 0);
324 // Step #1: Number blocks in depth-first order and initialize variables used
325 // in later stages of the algorithm.
326 unsigned N = DFSPass(Root, 0);
328 for (unsigned i = N; i >= 2; --i) {
329 BasicBlock *W = Vertex[i];
330 InfoRec &WInfo = Info[W];
332 // Step #2: Calculate the semidominators of all vertices
333 for (pred_iterator PI = pred_begin(W), E = pred_end(W); PI != E; ++PI)
334 if (Info.count(*PI)) { // Only if this predecessor is reachable!
335 unsigned SemiU = Info[Eval(*PI)].Semi;
336 if (SemiU < WInfo.Semi)
340 Info[Vertex[WInfo.Semi]].Bucket.push_back(W);
342 BasicBlock *WParent = WInfo.Parent;
343 Link(WParent, W, WInfo);
345 // Step #3: Implicitly define the immediate dominator of vertices
346 std::vector<BasicBlock*> &WParentBucket = Info[WParent].Bucket;
347 while (!WParentBucket.empty()) {
348 BasicBlock *V = WParentBucket.back();
349 WParentBucket.pop_back();
350 BasicBlock *U = Eval(V);
351 IDoms[V] = Info[U].Semi < Info[V].Semi ? U : WParent;
355 // Step #4: Explicitly define the immediate dominator of each vertex
356 for (unsigned i = 2; i <= N; ++i) {
357 BasicBlock *W = Vertex[i];
358 BasicBlock *&WIDom = IDoms[W];
359 if (WIDom != Vertex[Info[W].Semi])
360 WIDom = IDoms[WIDom];
363 // Loop over all of the reachable blocks in the function...
364 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
365 if (BasicBlock *ImmDom = getIDom(I)) { // Reachable block.
366 DomTreeNode *BBNode = DomTreeNodes[I];
367 if (BBNode) continue; // Haven't calculated this node yet?
369 // Get or calculate the node for the immediate dominator
370 DomTreeNode *IDomNode = getNodeForBlock(ImmDom);
372 // Add a new tree node for this BasicBlock, and link it as a child of
374 DomTreeNode *C = new DomTreeNode(I, IDomNode);
375 DomTreeNodes[I] = IDomNode->addChild(C);
378 // Free temporary memory used to construct idom's
381 std::vector<BasicBlock*>().swap(Vertex);
386 void DominatorTreeBase::updateDFSNumbers() {
389 SmallVector<DomTreeNode *, 32> WorkStack;
390 SmallPtrSet<DomTreeNode *, 32> Visited;
392 for (unsigned i = 0, e = Roots.size(); i != e; ++i) {
393 DomTreeNode *ThisRoot = getNode(Roots[i]);
394 WorkStack.push_back(ThisRoot);
395 Visited.insert(ThisRoot);
396 ThisRoot->DFSNumIn = DFSNum++;
398 while (!WorkStack.empty()) {
399 DomTreeNode *Node = WorkStack.back();
401 bool MustVisitAChild = false;
402 for (DomTreeNode::iterator DI = Node->begin(), E = Node->end();
404 DomTreeNode *Child = *DI;
405 if (!Visited.insert(Child))
408 MustVisitAChild = true;
409 Child->DFSNumIn = DFSNum++;
410 WorkStack.push_back(Child);
414 if (!MustVisitAChild) {
415 // If we reach here means all children are visited
416 Node->DFSNumOut = DFSNum++;
417 WorkStack.pop_back();
425 /// isReachableFromEntry - Return true if A is dominated by the entry
426 /// block of the function containing it.
427 const bool DominatorTreeBase::isReachableFromEntry(BasicBlock* A) {
428 assert (!isPostDominator()
429 && "This is not implemented for post dominators");
430 return dominates(&A->getParent()->getEntryBlock(), A);
433 // dominates - Return true if A dominates B. THis performs the
434 // special checks necessary if A and B are in the same basic block.
435 bool DominatorTreeBase::dominates(Instruction *A, Instruction *B) {
436 BasicBlock *BBA = A->getParent(), *BBB = B->getParent();
437 if (BBA != BBB) return dominates(BBA, BBB);
439 // It is not possible to determine dominance between two PHI nodes
440 // based on their ordering.
441 if (isa<PHINode>(A) && isa<PHINode>(B))
444 // Loop through the basic block until we find A or B.
445 BasicBlock::iterator I = BBA->begin();
446 for (; &*I != A && &*I != B; ++I) /*empty*/;
448 if(!IsPostDominators) {
449 // A dominates B if it is found first in the basic block.
452 // A post-dominates B if B is found first in the basic block.
457 // DominatorTreeBase::reset - Free all of the tree node memory.
459 void DominatorTreeBase::reset() {
460 for (DomTreeNodeMapType::iterator I = DomTreeNodes.begin(),
461 E = DomTreeNodes.end(); I != E; ++I)
463 DomTreeNodes.clear();
470 /// findNearestCommonDominator - Find nearest common dominator basic block
471 /// for basic block A and B. If there is no such block then return NULL.
472 BasicBlock *DominatorTreeBase::findNearestCommonDominator(BasicBlock *A,
475 assert (!isPostDominator()
476 && "This is not implemented for post dominators");
477 assert (A->getParent() == B->getParent()
478 && "Two blocks are not in same function");
480 // If either A or B is a entry block then it is nearest common dominator.
481 BasicBlock &Entry = A->getParent()->getEntryBlock();
482 if (A == &Entry || B == &Entry)
485 // If B dominates A then B is nearest common dominator.
489 // If A dominates B then A is nearest common dominator.
493 DomTreeNode *NodeA = getNode(A);
494 DomTreeNode *NodeB = getNode(B);
496 // Collect NodeA dominators set.
497 SmallPtrSet<DomTreeNode*, 16> NodeADoms;
498 NodeADoms.insert(NodeA);
499 DomTreeNode *IDomA = NodeA->getIDom();
501 NodeADoms.insert(IDomA);
502 IDomA = IDomA->getIDom();
505 // Walk NodeB immediate dominators chain and find common dominator node.
506 DomTreeNode *IDomB = NodeB->getIDom();
508 if (NodeADoms.count(IDomB) != 0)
509 return IDomB->getBlock();
511 IDomB = IDomB->getIDom();
517 void DomTreeNode::setIDom(DomTreeNode *NewIDom) {
518 assert(IDom && "No immediate dominator?");
519 if (IDom != NewIDom) {
520 std::vector<DomTreeNode*>::iterator I =
521 std::find(IDom->Children.begin(), IDom->Children.end(), this);
522 assert(I != IDom->Children.end() &&
523 "Not in immediate dominator children set!");
524 // I am no longer your child...
525 IDom->Children.erase(I);
527 // Switch to new dominator
529 IDom->Children.push_back(this);
533 DomTreeNode *DominatorTree::getNodeForBlock(BasicBlock *BB) {
534 if (DomTreeNode *BBNode = DomTreeNodes[BB])
537 // Haven't calculated this node yet? Get or calculate the node for the
538 // immediate dominator.
539 BasicBlock *IDom = getIDom(BB);
540 DomTreeNode *IDomNode = getNodeForBlock(IDom);
542 // Add a new tree node for this BasicBlock, and link it as a child of
544 DomTreeNode *C = new DomTreeNode(BB, IDomNode);
545 return DomTreeNodes[BB] = IDomNode->addChild(C);
548 static std::ostream &operator<<(std::ostream &o, const DomTreeNode *Node) {
549 if (Node->getBlock())
550 WriteAsOperand(o, Node->getBlock(), false);
552 o << " <<exit node>>";
554 o << " {" << Node->getDFSNumIn() << "," << Node->getDFSNumOut() << "}";
559 static void PrintDomTree(const DomTreeNode *N, std::ostream &o,
561 o << std::string(2*Lev, ' ') << "[" << Lev << "] " << N;
562 for (DomTreeNode::const_iterator I = N->begin(), E = N->end();
564 PrintDomTree(*I, o, Lev+1);
567 void DominatorTreeBase::print(std::ostream &o, const Module* ) const {
568 o << "=============================--------------------------------\n";
569 o << "Inorder Dominator Tree: ";
571 o << "DFSNumbers invalid: " << SlowQueries << " slow queries.";
574 PrintDomTree(getRootNode(), o, 1);
577 void DominatorTreeBase::dump() {
581 bool DominatorTree::runOnFunction(Function &F) {
582 reset(); // Reset from the last time we were run...
583 Roots.push_back(&F.getEntryBlock());
588 //===----------------------------------------------------------------------===//
589 // DominanceFrontier Implementation
590 //===----------------------------------------------------------------------===//
592 char DominanceFrontier::ID = 0;
593 static RegisterPass<DominanceFrontier>
594 G("domfrontier", "Dominance Frontier Construction", true);
596 // NewBB is split and now it has one successor. Update dominace frontier to
597 // reflect this change.
598 void DominanceFrontier::splitBlock(BasicBlock *NewBB) {
599 assert(NewBB->getTerminator()->getNumSuccessors() == 1
600 && "NewBB should have a single successor!");
601 BasicBlock *NewBBSucc = NewBB->getTerminator()->getSuccessor(0);
603 std::vector<BasicBlock*> PredBlocks;
604 for (pred_iterator PI = pred_begin(NewBB), PE = pred_end(NewBB);
606 PredBlocks.push_back(*PI);
608 if (PredBlocks.empty())
609 // If NewBB does not have any predecessors then it is a entry block.
610 // In this case, NewBB and its successor NewBBSucc dominates all
614 // NewBBSucc inherits original NewBB frontier.
615 DominanceFrontier::iterator NewBBI = find(NewBB);
616 if (NewBBI != end()) {
617 DominanceFrontier::DomSetType NewBBSet = NewBBI->second;
618 DominanceFrontier::DomSetType NewBBSuccSet;
619 NewBBSuccSet.insert(NewBBSet.begin(), NewBBSet.end());
620 addBasicBlock(NewBBSucc, NewBBSuccSet);
623 // If NewBB dominates NewBBSucc, then DF(NewBB) is now going to be the
624 // DF(PredBlocks[0]) without the stuff that the new block does not dominate
626 DominatorTree &DT = getAnalysis<DominatorTree>();
627 if (DT.dominates(NewBB, NewBBSucc)) {
628 DominanceFrontier::iterator DFI = find(PredBlocks[0]);
630 DominanceFrontier::DomSetType Set = DFI->second;
631 // Filter out stuff in Set that we do not dominate a predecessor of.
632 for (DominanceFrontier::DomSetType::iterator SetI = Set.begin(),
633 E = Set.end(); SetI != E;) {
634 bool DominatesPred = false;
635 for (pred_iterator PI = pred_begin(*SetI), E = pred_end(*SetI);
637 if (DT.dominates(NewBB, *PI))
638 DominatesPred = true;
645 if (NewBBI != end()) {
646 DominanceFrontier::DomSetType NewBBSet = NewBBI->second;
647 NewBBSet.insert(Set.begin(), Set.end());
649 addBasicBlock(NewBB, Set);
653 // DF(NewBB) is {NewBBSucc} because NewBB does not strictly dominate
654 // NewBBSucc, but it does dominate itself (and there is an edge (NewBB ->
655 // NewBBSucc)). NewBBSucc is the single successor of NewBB.
656 DominanceFrontier::DomSetType NewDFSet;
657 NewDFSet.insert(NewBBSucc);
658 addBasicBlock(NewBB, NewDFSet);
661 // Now we must loop over all of the dominance frontiers in the function,
662 // replacing occurrences of NewBBSucc with NewBB in some cases. All
663 // blocks that dominate a block in PredBlocks and contained NewBBSucc in
664 // their dominance frontier must be updated to contain NewBB instead.
666 for (Function::iterator FI = NewBB->getParent()->begin(),
667 FE = NewBB->getParent()->end(); FI != FE; ++FI) {
668 DominanceFrontier::iterator DFI = find(FI);
669 if (DFI == end()) continue; // unreachable block.
671 // Only consider nodes that have NewBBSucc in their dominator frontier.
672 if (!DFI->second.count(NewBBSucc)) continue;
674 // Verify whether this block dominates a block in predblocks. If not, do
676 bool BlockDominatesAny = false;
677 for (std::vector<BasicBlock*>::const_iterator BI = PredBlocks.begin(),
678 BE = PredBlocks.end(); BI != BE; ++BI) {
679 if (DT.dominates(FI, *BI)) {
680 BlockDominatesAny = true;
685 if (!BlockDominatesAny)
688 // If NewBBSucc should not stay in our dominator frontier, remove it.
689 // We remove it unless there is a predecessor of NewBBSucc that we
690 // dominate, but we don't strictly dominate NewBBSucc.
691 bool ShouldRemove = true;
692 if ((BasicBlock*)FI == NewBBSucc || !DT.dominates(FI, NewBBSucc)) {
693 // Okay, we know that PredDom does not strictly dominate NewBBSucc.
694 // Check to see if it dominates any predecessors of NewBBSucc.
695 for (pred_iterator PI = pred_begin(NewBBSucc),
696 E = pred_end(NewBBSucc); PI != E; ++PI)
697 if (DT.dominates(FI, *PI)) {
698 ShouldRemove = false;
704 removeFromFrontier(DFI, NewBBSucc);
705 addToFrontier(DFI, NewBB);
710 class DFCalculateWorkObject {
712 DFCalculateWorkObject(BasicBlock *B, BasicBlock *P,
713 const DomTreeNode *N,
714 const DomTreeNode *PN)
715 : currentBB(B), parentBB(P), Node(N), parentNode(PN) {}
716 BasicBlock *currentBB;
717 BasicBlock *parentBB;
718 const DomTreeNode *Node;
719 const DomTreeNode *parentNode;
723 const DominanceFrontier::DomSetType &
724 DominanceFrontier::calculate(const DominatorTree &DT,
725 const DomTreeNode *Node) {
726 BasicBlock *BB = Node->getBlock();
727 DomSetType *Result = NULL;
729 std::vector<DFCalculateWorkObject> workList;
730 SmallPtrSet<BasicBlock *, 32> visited;
732 workList.push_back(DFCalculateWorkObject(BB, NULL, Node, NULL));
734 DFCalculateWorkObject *currentW = &workList.back();
735 assert (currentW && "Missing work object.");
737 BasicBlock *currentBB = currentW->currentBB;
738 BasicBlock *parentBB = currentW->parentBB;
739 const DomTreeNode *currentNode = currentW->Node;
740 const DomTreeNode *parentNode = currentW->parentNode;
741 assert (currentBB && "Invalid work object. Missing current Basic Block");
742 assert (currentNode && "Invalid work object. Missing current Node");
743 DomSetType &S = Frontiers[currentBB];
745 // Visit each block only once.
746 if (visited.count(currentBB) == 0) {
747 visited.insert(currentBB);
749 // Loop over CFG successors to calculate DFlocal[currentNode]
750 for (succ_iterator SI = succ_begin(currentBB), SE = succ_end(currentBB);
752 // Does Node immediately dominate this successor?
753 if (DT[*SI]->getIDom() != currentNode)
758 // At this point, S is DFlocal. Now we union in DFup's of our children...
759 // Loop through and visit the nodes that Node immediately dominates (Node's
760 // children in the IDomTree)
761 bool visitChild = false;
762 for (DomTreeNode::const_iterator NI = currentNode->begin(),
763 NE = currentNode->end(); NI != NE; ++NI) {
764 DomTreeNode *IDominee = *NI;
765 BasicBlock *childBB = IDominee->getBlock();
766 if (visited.count(childBB) == 0) {
767 workList.push_back(DFCalculateWorkObject(childBB, currentBB,
768 IDominee, currentNode));
773 // If all children are visited or there is any child then pop this block
774 // from the workList.
782 DomSetType::const_iterator CDFI = S.begin(), CDFE = S.end();
783 DomSetType &parentSet = Frontiers[parentBB];
784 for (; CDFI != CDFE; ++CDFI) {
785 if (!DT.properlyDominates(parentNode, DT[*CDFI]))
786 parentSet.insert(*CDFI);
791 } while (!workList.empty());
796 void DominanceFrontierBase::print(std::ostream &o, const Module* ) const {
797 for (const_iterator I = begin(), E = end(); I != E; ++I) {
798 o << " DomFrontier for BB";
800 WriteAsOperand(o, I->first, false);
802 o << " <<exit node>>";
803 o << " is:\t" << I->second << "\n";
807 void DominanceFrontierBase::dump() {