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"
24 #include "llvm/Support/Streams.h"
29 static std::ostream &operator<<(std::ostream &o,
30 const std::set<BasicBlock*> &BBs) {
31 for (std::set<BasicBlock*>::const_iterator I = BBs.begin(), E = BBs.end();
34 WriteAsOperand(o, *I, false);
36 o << " <<exit node>>";
41 //===----------------------------------------------------------------------===//
42 // DominatorTree Implementation
43 //===----------------------------------------------------------------------===//
45 // DominatorTree construction - This pass constructs immediate dominator
46 // information for a flow-graph based on the algorithm described in this
49 // A Fast Algorithm for Finding Dominators in a Flowgraph
50 // T. Lengauer & R. Tarjan, ACM TOPLAS July 1979, pgs 121-141.
52 // This implements both the O(n*ack(n)) and the O(n*log(n)) versions of EVAL and
53 // LINK, but it turns out that the theoretically slower O(n*log(n))
54 // implementation is actually faster than the "efficient" algorithm (even for
55 // large CFGs) because the constant overheads are substantially smaller. The
56 // lower-complexity version can be enabled with the following #define:
58 #define BALANCE_IDOM_TREE 0
60 //===----------------------------------------------------------------------===//
62 char DominatorTree::ID = 0;
63 static RegisterPass<DominatorTree>
64 E("domtree", "Dominator Tree Construction", true);
66 // NewBB is split and now it has one successor. Update dominator tree to
67 // reflect this change.
68 void DominatorTree::splitBlock(BasicBlock *NewBB) {
69 assert(NewBB->getTerminator()->getNumSuccessors() == 1
70 && "NewBB should have a single successor!");
71 BasicBlock *NewBBSucc = NewBB->getTerminator()->getSuccessor(0);
73 std::vector<BasicBlock*> PredBlocks;
74 for (pred_iterator PI = pred_begin(NewBB), PE = pred_end(NewBB);
76 PredBlocks.push_back(*PI);
78 assert(!PredBlocks.empty() && "No predblocks??");
80 // The newly inserted basic block will dominate existing basic blocks iff the
81 // PredBlocks dominate all of the non-pred blocks. If all predblocks dominate
82 // the non-pred blocks, then they all must be the same block!
84 bool NewBBDominatesNewBBSucc = true;
86 BasicBlock *OnePred = PredBlocks[0];
87 unsigned i = 1, e = PredBlocks.size();
88 for (i = 1; !isReachableFromEntry(OnePred); ++i) {
89 assert(i != e && "Didn't find reachable pred?");
90 OnePred = PredBlocks[i];
94 if (PredBlocks[i] != OnePred && isReachableFromEntry(OnePred)) {
95 NewBBDominatesNewBBSucc = false;
99 if (NewBBDominatesNewBBSucc)
100 for (pred_iterator PI = pred_begin(NewBBSucc), E = pred_end(NewBBSucc);
102 if (*PI != NewBB && !dominates(NewBBSucc, *PI)) {
103 NewBBDominatesNewBBSucc = false;
108 // The other scenario where the new block can dominate its successors are when
109 // all predecessors of NewBBSucc that are not NewBB are dominated by NewBBSucc
111 if (!NewBBDominatesNewBBSucc) {
112 NewBBDominatesNewBBSucc = true;
113 for (pred_iterator PI = pred_begin(NewBBSucc), E = pred_end(NewBBSucc);
115 if (*PI != NewBB && !dominates(NewBBSucc, *PI)) {
116 NewBBDominatesNewBBSucc = false;
121 // Find NewBB's immediate dominator and create new dominator tree node for
123 BasicBlock *NewBBIDom = 0;
125 for (i = 0; i < PredBlocks.size(); ++i)
126 if (isReachableFromEntry(PredBlocks[i])) {
127 NewBBIDom = PredBlocks[i];
130 assert(i != PredBlocks.size() && "No reachable preds?");
131 for (i = i + 1; i < PredBlocks.size(); ++i) {
132 if (isReachableFromEntry(PredBlocks[i]))
133 NewBBIDom = findNearestCommonDominator(NewBBIDom, PredBlocks[i]);
135 assert(NewBBIDom && "No immediate dominator found??");
137 // Create the new dominator tree node... and set the idom of NewBB.
138 DomTreeNode *NewBBNode = addNewBlock(NewBB, NewBBIDom);
140 // If NewBB strictly dominates other blocks, then it is now the immediate
141 // dominator of NewBBSucc. Update the dominator tree as appropriate.
142 if (NewBBDominatesNewBBSucc) {
143 DomTreeNode *NewBBSuccNode = getNode(NewBBSucc);
144 changeImmediateDominator(NewBBSuccNode, NewBBNode);
148 unsigned DominatorTree::DFSPass(BasicBlock *V, unsigned N) {
149 // This is more understandable as a recursive algorithm, but we can't use the
150 // recursive algorithm due to stack depth issues. Keep it here for
151 // documentation purposes.
153 InfoRec &VInfo = Info[Roots[i]];
157 Vertex.push_back(V); // Vertex[n] = V;
158 //Info[V].Ancestor = 0; // Ancestor[n] = 0
159 //Info[V].Child = 0; // Child[v] = 0
160 VInfo.Size = 1; // Size[v] = 1
162 for (succ_iterator SI = succ_begin(V), E = succ_end(V); SI != E; ++SI) {
163 InfoRec &SuccVInfo = Info[*SI];
164 if (SuccVInfo.Semi == 0) {
165 SuccVInfo.Parent = V;
170 std::vector<std::pair<BasicBlock*, unsigned> > Worklist;
171 Worklist.push_back(std::make_pair(V, 0U));
172 while (!Worklist.empty()) {
173 BasicBlock *BB = Worklist.back().first;
174 unsigned NextSucc = Worklist.back().second;
176 // First time we visited this BB?
178 InfoRec &BBInfo = Info[BB];
182 Vertex.push_back(BB); // Vertex[n] = V;
183 //BBInfo[V].Ancestor = 0; // Ancestor[n] = 0
184 //BBInfo[V].Child = 0; // Child[v] = 0
185 BBInfo.Size = 1; // Size[v] = 1
188 // If we are done with this block, remove it from the worklist.
189 if (NextSucc == BB->getTerminator()->getNumSuccessors()) {
194 // Otherwise, increment the successor number for the next time we get to it.
195 ++Worklist.back().second;
197 // Visit the successor next, if it isn't already visited.
198 BasicBlock *Succ = BB->getTerminator()->getSuccessor(NextSucc);
200 InfoRec &SuccVInfo = Info[Succ];
201 if (SuccVInfo.Semi == 0) {
202 SuccVInfo.Parent = BB;
203 Worklist.push_back(std::make_pair(Succ, 0U));
210 void DominatorTree::Compress(BasicBlock *VIn) {
212 std::vector<BasicBlock *> Work;
213 SmallPtrSet<BasicBlock *, 32> Visited;
214 BasicBlock *VInAncestor = Info[VIn].Ancestor;
215 InfoRec &VInVAInfo = Info[VInAncestor];
217 if (VInVAInfo.Ancestor != 0)
220 while (!Work.empty()) {
221 BasicBlock *V = Work.back();
222 InfoRec &VInfo = Info[V];
223 BasicBlock *VAncestor = VInfo.Ancestor;
224 InfoRec &VAInfo = Info[VAncestor];
226 // Process Ancestor first
227 if (Visited.insert(VAncestor) &&
228 VAInfo.Ancestor != 0) {
229 Work.push_back(VAncestor);
234 // Update VInfo based on Ancestor info
235 if (VAInfo.Ancestor == 0)
237 BasicBlock *VAncestorLabel = VAInfo.Label;
238 BasicBlock *VLabel = VInfo.Label;
239 if (Info[VAncestorLabel].Semi < Info[VLabel].Semi)
240 VInfo.Label = VAncestorLabel;
241 VInfo.Ancestor = VAInfo.Ancestor;
245 BasicBlock *DominatorTree::Eval(BasicBlock *V) {
246 InfoRec &VInfo = Info[V];
247 #if !BALANCE_IDOM_TREE
248 // Higher-complexity but faster implementation
249 if (VInfo.Ancestor == 0)
254 // Lower-complexity but slower implementation
255 if (VInfo.Ancestor == 0)
258 BasicBlock *VLabel = VInfo.Label;
260 BasicBlock *VAncestorLabel = Info[VInfo.Ancestor].Label;
261 if (Info[VAncestorLabel].Semi >= Info[VLabel].Semi)
264 return VAncestorLabel;
268 void DominatorTree::Link(BasicBlock *V, BasicBlock *W, InfoRec &WInfo){
269 #if !BALANCE_IDOM_TREE
270 // Higher-complexity but faster implementation
273 // Lower-complexity but slower implementation
274 BasicBlock *WLabel = WInfo.Label;
275 unsigned WLabelSemi = Info[WLabel].Semi;
277 InfoRec *SInfo = &Info[S];
279 BasicBlock *SChild = SInfo->Child;
280 InfoRec *SChildInfo = &Info[SChild];
282 while (WLabelSemi < Info[SChildInfo->Label].Semi) {
283 BasicBlock *SChildChild = SChildInfo->Child;
284 if (SInfo->Size+Info[SChildChild].Size >= 2*SChildInfo->Size) {
285 SChildInfo->Ancestor = S;
286 SInfo->Child = SChild = SChildChild;
287 SChildInfo = &Info[SChild];
289 SChildInfo->Size = SInfo->Size;
290 S = SInfo->Ancestor = SChild;
292 SChild = SChildChild;
293 SChildInfo = &Info[SChild];
297 InfoRec &VInfo = Info[V];
298 SInfo->Label = WLabel;
300 assert(V != W && "The optimization here will not work in this case!");
301 unsigned WSize = WInfo.Size;
302 unsigned VSize = (VInfo.Size += WSize);
305 std::swap(S, VInfo.Child);
315 void DominatorTree::calculate(Function &F) {
316 BasicBlock* Root = Roots[0];
318 // Add a node for the root...
319 DomTreeNodes[Root] = RootNode = new DomTreeNode(Root, 0);
323 // Step #1: Number blocks in depth-first order and initialize variables used
324 // in later stages of the algorithm.
325 unsigned N = DFSPass(Root, 0);
327 for (unsigned i = N; i >= 2; --i) {
328 BasicBlock *W = Vertex[i];
329 InfoRec &WInfo = Info[W];
331 // Step #2: Calculate the semidominators of all vertices
332 for (pred_iterator PI = pred_begin(W), E = pred_end(W); PI != E; ++PI)
333 if (Info.count(*PI)) { // Only if this predecessor is reachable!
334 unsigned SemiU = Info[Eval(*PI)].Semi;
335 if (SemiU < WInfo.Semi)
339 Info[Vertex[WInfo.Semi]].Bucket.push_back(W);
341 BasicBlock *WParent = WInfo.Parent;
342 Link(WParent, W, WInfo);
344 // Step #3: Implicitly define the immediate dominator of vertices
345 std::vector<BasicBlock*> &WParentBucket = Info[WParent].Bucket;
346 while (!WParentBucket.empty()) {
347 BasicBlock *V = WParentBucket.back();
348 WParentBucket.pop_back();
349 BasicBlock *U = Eval(V);
350 IDoms[V] = Info[U].Semi < Info[V].Semi ? U : WParent;
354 // Step #4: Explicitly define the immediate dominator of each vertex
355 for (unsigned i = 2; i <= N; ++i) {
356 BasicBlock *W = Vertex[i];
357 BasicBlock *&WIDom = IDoms[W];
358 if (WIDom != Vertex[Info[W].Semi])
359 WIDom = IDoms[WIDom];
362 // Loop over all of the reachable blocks in the function...
363 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
364 if (BasicBlock *ImmDom = getIDom(I)) { // Reachable block.
365 DomTreeNode *BBNode = DomTreeNodes[I];
366 if (BBNode) continue; // Haven't calculated this node yet?
368 // Get or calculate the node for the immediate dominator
369 DomTreeNode *IDomNode = getNodeForBlock(ImmDom);
371 // Add a new tree node for this BasicBlock, and link it as a child of
373 DomTreeNode *C = new DomTreeNode(I, IDomNode);
374 DomTreeNodes[I] = IDomNode->addChild(C);
377 // Free temporary memory used to construct idom's
380 std::vector<BasicBlock*>().swap(Vertex);
385 void DominatorTreeBase::updateDFSNumbers() {
387 // Iterate over all nodes in depth first order.
388 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
389 for (df_iterator<BasicBlock*> I = df_begin(Roots[i]),
390 E = df_end(Roots[i]); I != E; ++I) {
391 if (DomTreeNode *BBNode = getNode(*I)) {
392 if (!BBNode->getIDom())
393 BBNode->assignDFSNumber(dfsnum);
400 /// isReachableFromEntry - Return true if A is dominated by the entry
401 /// block of the function containing it.
402 const bool DominatorTreeBase::isReachableFromEntry(BasicBlock* A) {
403 assert (!isPostDominator()
404 && "This is not implemented for post dominators");
405 return dominates(&A->getParent()->getEntryBlock(), A);
408 // dominates - Return true if A dominates B. THis performs the
409 // special checks necessary if A and B are in the same basic block.
410 bool DominatorTreeBase::dominates(Instruction *A, Instruction *B) {
411 BasicBlock *BBA = A->getParent(), *BBB = B->getParent();
412 if (BBA != BBB) return dominates(BBA, BBB);
414 // It is not possible to determine dominance between two PHI nodes
415 // based on their ordering.
416 if (isa<PHINode>(A) && isa<PHINode>(B))
419 // Loop through the basic block until we find A or B.
420 BasicBlock::iterator I = BBA->begin();
421 for (; &*I != A && &*I != B; ++I) /*empty*/;
423 if(!IsPostDominators) {
424 // A dominates B if it is found first in the basic block.
427 // A post-dominates B if B is found first in the basic block.
432 // DominatorTreeBase::reset - Free all of the tree node memory.
434 void DominatorTreeBase::reset() {
435 for (DomTreeNodeMapType::iterator I = DomTreeNodes.begin(),
436 E = DomTreeNodes.end(); I != E; ++I)
438 DomTreeNodes.clear();
445 /// findNearestCommonDominator - Find nearest common dominator basic block
446 /// for basic block A and B. If there is no such block then return NULL.
447 BasicBlock *DominatorTreeBase::findNearestCommonDominator(BasicBlock *A,
450 assert (!isPostDominator()
451 && "This is not implemented for post dominators");
452 assert (A->getParent() == B->getParent()
453 && "Two blocks are not in same function");
455 // If either A or B is a entry block then it is nearest common dominator.
456 BasicBlock &Entry = A->getParent()->getEntryBlock();
457 if (A == &Entry || B == &Entry)
460 // If B dominates A then B is nearest common dominator.
464 // If A dominates B then A is nearest common dominator.
468 DomTreeNode *NodeA = getNode(A);
469 DomTreeNode *NodeB = getNode(B);
471 // Collect NodeA dominators set.
472 SmallPtrSet<DomTreeNode*, 16> NodeADoms;
473 NodeADoms.insert(NodeA);
474 DomTreeNode *IDomA = NodeA->getIDom();
476 NodeADoms.insert(IDomA);
477 IDomA = IDomA->getIDom();
480 // Walk NodeB immediate dominators chain and find common dominator node.
481 DomTreeNode *IDomB = NodeB->getIDom();
483 if (NodeADoms.count(IDomB) != 0)
484 return IDomB->getBlock();
486 IDomB = IDomB->getIDom();
492 /// assignDFSNumber - Assign In and Out numbers while walking dominator tree
494 void DomTreeNode::assignDFSNumber(int num) {
495 std::vector<DomTreeNode *> workStack;
496 SmallPtrSet<DomTreeNode *, 32> Visited;
498 workStack.push_back(this);
499 Visited.insert(this);
500 this->DFSNumIn = num++;
502 while (!workStack.empty()) {
503 DomTreeNode *Node = workStack.back();
505 bool visitChild = false;
506 for (std::vector<DomTreeNode*>::iterator DI = Node->begin(),
507 E = Node->end(); DI != E && !visitChild; ++DI) {
508 DomTreeNode *Child = *DI;
509 if (!Visited.insert(Child))
513 Child->DFSNumIn = num++;
514 workStack.push_back(Child);
517 // If we reach here means all children are visited
518 Node->DFSNumOut = num++;
519 workStack.pop_back();
524 void DomTreeNode::setIDom(DomTreeNode *NewIDom) {
525 assert(IDom && "No immediate dominator?");
526 if (IDom != NewIDom) {
527 std::vector<DomTreeNode*>::iterator I =
528 std::find(IDom->Children.begin(), IDom->Children.end(), this);
529 assert(I != IDom->Children.end() &&
530 "Not in immediate dominator children set!");
531 // I am no longer your child...
532 IDom->Children.erase(I);
534 // Switch to new dominator
536 IDom->Children.push_back(this);
540 DomTreeNode *DominatorTree::getNodeForBlock(BasicBlock *BB) {
541 if (DomTreeNode *BBNode = DomTreeNodes[BB])
544 // Haven't calculated this node yet? Get or calculate the node for the
545 // immediate dominator.
546 BasicBlock *IDom = getIDom(BB);
547 DomTreeNode *IDomNode = getNodeForBlock(IDom);
549 // Add a new tree node for this BasicBlock, and link it as a child of
551 DomTreeNode *C = new DomTreeNode(BB, IDomNode);
552 return DomTreeNodes[BB] = IDomNode->addChild(C);
555 static std::ostream &operator<<(std::ostream &o, const DomTreeNode *Node) {
556 if (Node->getBlock())
557 WriteAsOperand(o, Node->getBlock(), false);
559 o << " <<exit node>>";
561 o << " {" << Node->getDFSNumIn() << "," << Node->getDFSNumOut() << "}";
566 static void PrintDomTree(const DomTreeNode *N, std::ostream &o,
568 o << std::string(2*Lev, ' ') << "[" << Lev << "] " << N;
569 for (DomTreeNode::const_iterator I = N->begin(), E = N->end();
571 PrintDomTree(*I, o, Lev+1);
574 void DominatorTreeBase::print(std::ostream &o, const Module* ) const {
575 o << "=============================--------------------------------\n";
576 o << "Inorder Dominator Tree: ";
578 o << "DFSNumbers invalid: " << SlowQueries << " slow queries.";
581 PrintDomTree(getRootNode(), o, 1);
584 void DominatorTreeBase::dump() {
588 bool DominatorTree::runOnFunction(Function &F) {
589 reset(); // Reset from the last time we were run...
590 Roots.push_back(&F.getEntryBlock());
595 //===----------------------------------------------------------------------===//
596 // DominanceFrontier Implementation
597 //===----------------------------------------------------------------------===//
599 char DominanceFrontier::ID = 0;
600 static RegisterPass<DominanceFrontier>
601 G("domfrontier", "Dominance Frontier Construction", true);
603 // NewBB is split and now it has one successor. Update dominace frontier to
604 // reflect this change.
605 void DominanceFrontier::splitBlock(BasicBlock *NewBB) {
606 assert(NewBB->getTerminator()->getNumSuccessors() == 1
607 && "NewBB should have a single successor!");
608 BasicBlock *NewBBSucc = NewBB->getTerminator()->getSuccessor(0);
610 std::vector<BasicBlock*> PredBlocks;
611 for (pred_iterator PI = pred_begin(NewBB), PE = pred_end(NewBB);
613 PredBlocks.push_back(*PI);
615 if (PredBlocks.empty())
616 // If NewBB does not have any predecessors then it is a entry block.
617 // In this case, NewBB and its successor NewBBSucc dominates all
621 // NewBBSucc inherits original NewBB frontier.
622 DominanceFrontier::iterator NewBBI = find(NewBB);
623 if (NewBBI != end()) {
624 DominanceFrontier::DomSetType NewBBSet = NewBBI->second;
625 DominanceFrontier::DomSetType NewBBSuccSet;
626 NewBBSuccSet.insert(NewBBSet.begin(), NewBBSet.end());
627 addBasicBlock(NewBBSucc, NewBBSuccSet);
630 // If NewBB dominates NewBBSucc, then DF(NewBB) is now going to be the
631 // DF(PredBlocks[0]) without the stuff that the new block does not dominate
633 DominatorTree &DT = getAnalysis<DominatorTree>();
634 if (DT.dominates(NewBB, NewBBSucc)) {
635 DominanceFrontier::iterator DFI = find(PredBlocks[0]);
637 DominanceFrontier::DomSetType Set = DFI->second;
638 // Filter out stuff in Set that we do not dominate a predecessor of.
639 for (DominanceFrontier::DomSetType::iterator SetI = Set.begin(),
640 E = Set.end(); SetI != E;) {
641 bool DominatesPred = false;
642 for (pred_iterator PI = pred_begin(*SetI), E = pred_end(*SetI);
644 if (DT.dominates(NewBB, *PI))
645 DominatesPred = true;
652 if (NewBBI != end()) {
653 DominanceFrontier::DomSetType NewBBSet = NewBBI->second;
654 NewBBSet.insert(Set.begin(), Set.end());
656 addBasicBlock(NewBB, Set);
660 // DF(NewBB) is {NewBBSucc} because NewBB does not strictly dominate
661 // NewBBSucc, but it does dominate itself (and there is an edge (NewBB ->
662 // NewBBSucc)). NewBBSucc is the single successor of NewBB.
663 DominanceFrontier::DomSetType NewDFSet;
664 NewDFSet.insert(NewBBSucc);
665 addBasicBlock(NewBB, NewDFSet);
668 // Now we must loop over all of the dominance frontiers in the function,
669 // replacing occurrences of NewBBSucc with NewBB in some cases. All
670 // blocks that dominate a block in PredBlocks and contained NewBBSucc in
671 // their dominance frontier must be updated to contain NewBB instead.
673 for (Function::iterator FI = NewBB->getParent()->begin(),
674 FE = NewBB->getParent()->end(); FI != FE; ++FI) {
675 DominanceFrontier::iterator DFI = find(FI);
676 if (DFI == end()) continue; // unreachable block.
678 // Only consider nodes that have NewBBSucc in their dominator frontier.
679 if (!DFI->second.count(NewBBSucc)) continue;
681 // Verify whether this block dominates a block in predblocks. If not, do
683 bool BlockDominatesAny = false;
684 for (std::vector<BasicBlock*>::const_iterator BI = PredBlocks.begin(),
685 BE = PredBlocks.end(); BI != BE; ++BI) {
686 if (DT.dominates(FI, *BI)) {
687 BlockDominatesAny = true;
692 if (!BlockDominatesAny)
695 // If NewBBSucc should not stay in our dominator frontier, remove it.
696 // We remove it unless there is a predecessor of NewBBSucc that we
697 // dominate, but we don't strictly dominate NewBBSucc.
698 bool ShouldRemove = true;
699 if ((BasicBlock*)FI == NewBBSucc || !DT.dominates(FI, NewBBSucc)) {
700 // Okay, we know that PredDom does not strictly dominate NewBBSucc.
701 // Check to see if it dominates any predecessors of NewBBSucc.
702 for (pred_iterator PI = pred_begin(NewBBSucc),
703 E = pred_end(NewBBSucc); PI != E; ++PI)
704 if (DT.dominates(FI, *PI)) {
705 ShouldRemove = false;
711 removeFromFrontier(DFI, NewBBSucc);
712 addToFrontier(DFI, NewBB);
717 class DFCalculateWorkObject {
719 DFCalculateWorkObject(BasicBlock *B, BasicBlock *P,
720 const DomTreeNode *N,
721 const DomTreeNode *PN)
722 : currentBB(B), parentBB(P), Node(N), parentNode(PN) {}
723 BasicBlock *currentBB;
724 BasicBlock *parentBB;
725 const DomTreeNode *Node;
726 const DomTreeNode *parentNode;
730 const DominanceFrontier::DomSetType &
731 DominanceFrontier::calculate(const DominatorTree &DT,
732 const DomTreeNode *Node) {
733 BasicBlock *BB = Node->getBlock();
734 DomSetType *Result = NULL;
736 std::vector<DFCalculateWorkObject> workList;
737 SmallPtrSet<BasicBlock *, 32> visited;
739 workList.push_back(DFCalculateWorkObject(BB, NULL, Node, NULL));
741 DFCalculateWorkObject *currentW = &workList.back();
742 assert (currentW && "Missing work object.");
744 BasicBlock *currentBB = currentW->currentBB;
745 BasicBlock *parentBB = currentW->parentBB;
746 const DomTreeNode *currentNode = currentW->Node;
747 const DomTreeNode *parentNode = currentW->parentNode;
748 assert (currentBB && "Invalid work object. Missing current Basic Block");
749 assert (currentNode && "Invalid work object. Missing current Node");
750 DomSetType &S = Frontiers[currentBB];
752 // Visit each block only once.
753 if (visited.count(currentBB) == 0) {
754 visited.insert(currentBB);
756 // Loop over CFG successors to calculate DFlocal[currentNode]
757 for (succ_iterator SI = succ_begin(currentBB), SE = succ_end(currentBB);
759 // Does Node immediately dominate this successor?
760 if (DT[*SI]->getIDom() != currentNode)
765 // At this point, S is DFlocal. Now we union in DFup's of our children...
766 // Loop through and visit the nodes that Node immediately dominates (Node's
767 // children in the IDomTree)
768 bool visitChild = false;
769 for (DomTreeNode::const_iterator NI = currentNode->begin(),
770 NE = currentNode->end(); NI != NE; ++NI) {
771 DomTreeNode *IDominee = *NI;
772 BasicBlock *childBB = IDominee->getBlock();
773 if (visited.count(childBB) == 0) {
774 workList.push_back(DFCalculateWorkObject(childBB, currentBB,
775 IDominee, currentNode));
780 // If all children are visited or there is any child then pop this block
781 // from the workList.
789 DomSetType::const_iterator CDFI = S.begin(), CDFE = S.end();
790 DomSetType &parentSet = Frontiers[parentBB];
791 for (; CDFI != CDFE; ++CDFI) {
792 if (!DT.properlyDominates(parentNode, DT[*CDFI]))
793 parentSet.insert(*CDFI);
798 } while (!workList.empty());
803 void DominanceFrontierBase::print(std::ostream &o, const Module* ) const {
804 for (const_iterator I = begin(), E = end(); I != E; ++I) {
805 o << " DomFrontier for BB";
807 WriteAsOperand(o, I->first, false);
809 o << " <<exit node>>";
810 o << " is:\t" << I->second << "\n";
814 void DominanceFrontierBase::dump() {