Add a GraphTraits partial specialization to make the inverse of an inverse be the...
[oota-llvm.git] / lib / Analysis / PostDominatorCalculation.h
1 //==- PostDominatorCalculation.h - Post-Dominator Calculation ----*- C++ -*-==//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Owen Anderson and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // PostDominatorTree calculation implementation.
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_ANALYSIS_POST_DOMINATOR_CALCULATION_H
14 #define LLVM_ANALYSIS_POST_DOMINATOR_CALCULATION_H
15
16 #include "llvm/Analysis/PostDominators.h"
17 #include "llvm/Analysis/DominatorInternals.h"
18
19 namespace llvm {
20
21 void PDTcalculate(PostDominatorTree& PDT, Function &F) {
22   // Step #1: Number blocks in depth-first order and initialize variables used
23   // in later stages of the algorithm.
24   unsigned N = 0;
25   for (unsigned i = 0, e = PDT.Roots.size(); i != e; ++i)
26     N = DFSPass<GraphTraits<Inverse<BasicBlock*> > >(PDT, PDT.Roots[i], N);
27   
28   for (unsigned i = N; i >= 2; --i) {
29     BasicBlock *W = PDT.Vertex[i];
30     PostDominatorTree::InfoRec &WInfo = PDT.Info[W];
31     
32     // Step #2: Calculate the semidominators of all vertices
33     for (succ_iterator SI = succ_begin(W), SE = succ_end(W); SI != SE; ++SI)
34       if (PDT.Info.count(*SI)) {  // Only if this predecessor is reachable!
35         unsigned SemiU =
36              PDT.Info[Eval<GraphTraits<Inverse<BasicBlock*> > >(PDT, *SI)].Semi;
37         if (SemiU < WInfo.Semi)
38           WInfo.Semi = SemiU;
39       }
40         
41     PDT.Info[PDT.Vertex[WInfo.Semi]].Bucket.push_back(W);
42     
43     BasicBlock *WParent = WInfo.Parent;
44     Link<GraphTraits<Inverse<BasicBlock*> > >(PDT, WParent, W, WInfo);
45     
46     // Step #3: Implicitly define the immediate dominator of vertices
47     std::vector<BasicBlock*> &WParentBucket = PDT.Info[WParent].Bucket;
48     while (!WParentBucket.empty()) {
49       BasicBlock *V = WParentBucket.back();
50       WParentBucket.pop_back();
51       BasicBlock *U = Eval<GraphTraits<Inverse<BasicBlock*> > >(PDT, V);
52       PDT.IDoms[V] = PDT.Info[U].Semi < PDT.Info[V].Semi ? U : WParent;
53     }
54   }
55   
56   // Step #4: Explicitly define the immediate dominator of each vertex
57   for (unsigned i = 2; i <= N; ++i) {
58     BasicBlock *W = PDT.Vertex[i];
59     BasicBlock *&WIDom = PDT.IDoms[W];
60     if (WIDom != PDT.Vertex[PDT.Info[W].Semi])
61       WIDom = PDT.IDoms[WIDom];
62   }
63   
64   if (PDT.Roots.empty()) return;
65
66   // Add a node for the root.  This node might be the actual root, if there is
67   // one exit block, or it may be the virtual exit (denoted by (BasicBlock *)0)
68   // which postdominates all real exits if there are multiple exit blocks.
69   BasicBlock *Root = PDT.Roots.size() == 1 ? PDT.Roots[0] : 0;
70   PDT.DomTreeNodes[Root] = PDT.RootNode = new DomTreeNode(Root, 0);
71   
72   // Loop over all of the reachable blocks in the function...
73   for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
74     if (BasicBlock *ImmPostDom = PDT.getIDom(I)) {  // Reachable block.
75       DomTreeNode *&BBNode = PDT.DomTreeNodes[I];
76       if (!BBNode) {  // Haven't calculated this node yet?
77                       // Get or calculate the node for the immediate dominator
78         DomTreeNode *IPDomNode = PDT.getNodeForBlock(ImmPostDom);
79         
80         // Add a new tree node for this BasicBlock, and link it as a child of
81         // IDomNode
82         DomTreeNode *C = new DomTreeNode(I, IPDomNode);
83         PDT.DomTreeNodes[I] = C;
84         BBNode = IPDomNode->addChild(C);
85       }
86     }
87
88   // Free temporary memory used to construct idom's
89   PDT.IDoms.clear();
90   PDT.Info.clear();
91   std::vector<BasicBlock*>().swap(PDT.Vertex);
92
93   // Start out with the DFS numbers being invalid.  Let them be computed if
94   // demanded.
95   PDT.DFSInfoValid = false;
96 }
97
98 }
99 #endif