Fix a typo, which should also fix the failure on llvm-x86_64-linux-checks.
[oota-llvm.git] / include / llvm / Analysis / DominatorInternals.h
1 //=== llvm/Analysis/DominatorInternals.h - Dominator Calculation -*- C++ -*-==//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_ANALYSIS_DOMINATOR_INTERNALS_H
11 #define LLVM_ANALYSIS_DOMINATOR_INTERNALS_H
12
13 #include "llvm/Analysis/Dominators.h"
14 #include "llvm/ADT/SmallPtrSet.h"
15
16 //===----------------------------------------------------------------------===//
17 //
18 // DominatorTree construction - This pass constructs immediate dominator
19 // information for a flow-graph based on the algorithm described in this
20 // document:
21 //
22 //   A Fast Algorithm for Finding Dominators in a Flowgraph
23 //   T. Lengauer & R. Tarjan, ACM TOPLAS July 1979, pgs 121-141.
24 //
25 // This implements the O(n*log(n)) versions of EVAL and LINK, because it turns
26 // out that the theoretically slower O(n*log(n)) implementation is actually
27 // faster than the almost-linear O(n*alpha(n)) version, even for large CFGs.
28 //
29 //===----------------------------------------------------------------------===//
30
31 namespace llvm {
32
33 template<class GraphT>
34 unsigned DFSPass(DominatorTreeBase<typename GraphT::NodeType>& DT,
35                  typename GraphT::NodeType* V, unsigned N) {
36   // This is more understandable as a recursive algorithm, but we can't use the
37   // recursive algorithm due to stack depth issues.  Keep it here for
38   // documentation purposes.
39 #if 0
40   InfoRec &VInfo = DT.Info[DT.Roots[i]];
41   VInfo.DFSNum = VInfo.Semi = ++N;
42   VInfo.Label = V;
43
44   Vertex.push_back(V);        // Vertex[n] = V;
45   //Info[V].Ancestor = 0;     // Ancestor[n] = 0
46   //Info[V].Child = 0;        // Child[v] = 0
47   VInfo.Size = 1;             // Size[v] = 1
48
49   for (succ_iterator SI = succ_begin(V), E = succ_end(V); SI != E; ++SI) {
50     InfoRec &SuccVInfo = DT.Info[*SI];
51     if (SuccVInfo.Semi == 0) {
52       SuccVInfo.Parent = V;
53       N = DTDFSPass(DT, *SI, N);
54     }
55   }
56 #else
57   bool IsChilOfArtificialExit = (N != 0);
58
59   std::vector<std::pair<typename GraphT::NodeType*,
60                         typename GraphT::ChildIteratorType> > Worklist;
61   Worklist.push_back(std::make_pair(V, GraphT::child_begin(V)));
62   while (!Worklist.empty()) {
63     typename GraphT::NodeType* BB = Worklist.back().first;
64     typename GraphT::ChildIteratorType NextSucc = Worklist.back().second;
65
66     typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &BBInfo =
67                                                                     DT.Info[BB];
68
69     // First time we visited this BB?
70     if (NextSucc == GraphT::child_begin(BB)) {
71       BBInfo.DFSNum = BBInfo.Semi = ++N;
72       BBInfo.Label = BB;
73
74       DT.Vertex.push_back(BB);       // Vertex[n] = V;
75       //BBInfo[V].Ancestor = 0;   // Ancestor[n] = 0
76       //BBInfo[V].Child = 0;      // Child[v] = 0
77       BBInfo.Size = 1;            // Size[v] = 1
78
79       if (IsChilOfArtificialExit)
80         BBInfo.Parent = 1;
81
82       IsChilOfArtificialExit = false;
83     }
84
85     // store the DFS number of the current BB - the reference to BBInfo might
86     // get invalidated when processing the successors.
87     unsigned BBDFSNum = BBInfo.DFSNum;
88
89     // If we are done with this block, remove it from the worklist.
90     if (NextSucc == GraphT::child_end(BB)) {
91       Worklist.pop_back();
92       continue;
93     }
94
95     // Increment the successor number for the next time we get to it.
96     ++Worklist.back().second;
97     
98     // Visit the successor next, if it isn't already visited.
99     typename GraphT::NodeType* Succ = *NextSucc;
100
101     typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &SuccVInfo =
102                                                                   DT.Info[Succ];
103     if (SuccVInfo.Semi == 0) {
104       SuccVInfo.Parent = BBDFSNum;
105       Worklist.push_back(std::make_pair(Succ, GraphT::child_begin(Succ)));
106     }
107   }
108 #endif
109     return N;
110 }
111
112 template<class GraphT>
113 void Compress(DominatorTreeBase<typename GraphT::NodeType>& DT,
114               typename GraphT::NodeType *VIn) {
115   SmallVector<typename GraphT::NodeType*, 32> Work;
116   SmallPtrSet<typename GraphT::NodeType*, 32> Visited;
117   typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &VInVAInfo =
118                                       DT.Info[DT.Vertex[DT.Info[VIn].Ancestor]];
119
120   if (VInVAInfo.Ancestor != 0)
121     Work.push_back(VIn);
122   
123   while (!Work.empty()) {
124     typename GraphT::NodeType* V = Work.back();
125     typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &VInfo =
126                                                                      DT.Info[V];
127     typename GraphT::NodeType* VAncestor = DT.Vertex[VInfo.Ancestor];
128     typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &VAInfo =
129                                                              DT.Info[VAncestor];
130
131     // Process Ancestor first
132     if (Visited.insert(VAncestor) &&
133         VAInfo.Ancestor != 0) {
134       Work.push_back(VAncestor);
135       continue;
136     } 
137     Work.pop_back(); 
138
139     // Update VInfo based on Ancestor info
140     if (VAInfo.Ancestor == 0)
141       continue;
142     typename GraphT::NodeType* VAncestorLabel = VAInfo.Label;
143     typename GraphT::NodeType* VLabel = VInfo.Label;
144     if (DT.Info[VAncestorLabel].Semi < DT.Info[VLabel].Semi)
145       VInfo.Label = VAncestorLabel;
146     VInfo.Ancestor = VAInfo.Ancestor;
147   }
148 }
149
150 template<class GraphT>
151 typename GraphT::NodeType* 
152 Eval(DominatorTreeBase<typename GraphT::NodeType>& DT,
153      typename GraphT::NodeType *V) {
154   typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &VInfo =
155                                                                      DT.Info[V];
156   if (VInfo.Ancestor == 0)
157     return V;
158   Compress<GraphT>(DT, V);
159   return VInfo.Label;
160 }
161
162 template<class GraphT>
163 void Link(DominatorTreeBase<typename GraphT::NodeType>& DT,
164           unsigned DFSNumV, typename GraphT::NodeType* W,
165         typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &WInfo) {
166   WInfo.Ancestor = DFSNumV;
167 }
168
169 template<class FuncT, class NodeT>
170 void Calculate(DominatorTreeBase<typename GraphTraits<NodeT>::NodeType>& DT,
171                FuncT& F) {
172   typedef GraphTraits<NodeT> GraphT;
173
174   unsigned N = 0;
175   bool MultipleRoots = (DT.Roots.size() > 1);
176   if (MultipleRoots) {
177     typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &BBInfo =
178         DT.Info[NULL];
179     BBInfo.DFSNum = BBInfo.Semi = ++N;
180     BBInfo.Label = NULL;
181
182     DT.Vertex.push_back(NULL);       // 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
186   }
187
188   // Step #1: Number blocks in depth-first order and initialize variables used
189   // in later stages of the algorithm.
190   for (unsigned i = 0, e = static_cast<unsigned>(DT.Roots.size());
191        i != e; ++i)
192     N = DFSPass<GraphT>(DT, DT.Roots[i], N);
193
194   // it might be that some blocks did not get a DFS number (e.g., blocks of 
195   // infinite loops). In these cases an artificial exit node is required.
196   MultipleRoots |= (DT.isPostDominator() && N != F.size());
197
198   std::vector<unsigned> Buckets;
199   Buckets.resize(N + 1);
200   for (unsigned i = 1; i <= N; ++i)
201     Buckets[i] = i;
202
203   for (unsigned i = N; i >= 2; --i) {
204     typename GraphT::NodeType* W = DT.Vertex[i];
205     typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &WInfo =
206                                                                      DT.Info[W];
207
208     // Step #2: Implicitly define the immediate dominator of vertices
209     for (unsigned j = i; Buckets[j] != i; j = Buckets[j]) {
210        typename GraphT::NodeType* V = DT.Vertex[Buckets[j]];
211        typename GraphT::NodeType* U = Eval<GraphT>(DT, V);
212        DT.IDoms[V] = DT.Info[U].Semi < i ? U : W;
213     }
214
215     // Step #3: Calculate the semidominators of all vertices
216
217     // initialize the semi dominator to point to the parent node
218     WInfo.Semi = WInfo.Parent;
219     typedef GraphTraits<Inverse<NodeT> > InvTraits;
220     for (typename InvTraits::ChildIteratorType CI =
221          InvTraits::child_begin(W),
222          E = InvTraits::child_end(W); CI != E; ++CI) {
223       typename InvTraits::NodeType *N = *CI;
224       if (DT.Info.count(N)) {  // Only if this predecessor is reachable!
225         unsigned SemiU = DT.Info[Eval<GraphT>(DT, N)].Semi;
226         if (SemiU < WInfo.Semi)
227           WInfo.Semi = SemiU;
228       }
229     }
230
231     typename GraphT::NodeType* WParent = DT.Vertex[WInfo.Parent];
232
233     // If V is a non-root vertex and sdom(V) = parent(V), then idom(V) is
234     // necessarily parent(V). In this case, set idom(V) here and avoid placing
235     // V into a bucket.
236     if (WInfo.Semi == WInfo.Parent) {
237       DT.IDoms[W] = WParent;
238     } else {
239       Buckets[i] = Buckets[WInfo.Semi];
240       Buckets[WInfo.Semi] = i;
241     }
242
243     Link<GraphT>(DT, WInfo.Parent, W, WInfo);
244   }
245
246   if (N >= 1) {
247     typename GraphT::NodeType* Root = DT.Vertex[1];
248     for (unsigned j = 1; Buckets[j] != 1; j = Buckets[j]) {
249        typename GraphT::NodeType* V = DT.Vertex[Buckets[j]];
250        DT.IDoms[V] = Root;
251     }
252   }
253
254   // Step #4: Explicitly define the immediate dominator of each vertex
255   for (unsigned i = 2; i <= N; ++i) {
256     typename GraphT::NodeType* W = DT.Vertex[i];
257     typename GraphT::NodeType*& WIDom = DT.IDoms[W];
258     if (WIDom != DT.Vertex[DT.Info[W].Semi])
259       WIDom = DT.IDoms[WIDom];
260   }
261
262   if (DT.Roots.empty()) return;
263
264   // Add a node for the root.  This node might be the actual root, if there is
265   // one exit block, or it may be the virtual exit (denoted by (BasicBlock *)0)
266   // which postdominates all real exits if there are multiple exit blocks, or
267   // an infinite loop.
268   typename GraphT::NodeType* Root = !MultipleRoots ? DT.Roots[0] : 0;
269
270   DT.DomTreeNodes[Root] = DT.RootNode =
271                         new DomTreeNodeBase<typename GraphT::NodeType>(Root, 0);
272
273   // Loop over all of the reachable blocks in the function...
274   for (unsigned i = 2; i <= N; ++i) {
275     typename GraphT::NodeType* W = DT.Vertex[i];
276
277     DomTreeNodeBase<typename GraphT::NodeType> *BBNode = DT.DomTreeNodes[W];
278     if (BBNode) continue;  // Haven't calculated this node yet?
279
280     typename GraphT::NodeType* ImmDom = DT.getIDom(W);
281
282     assert(ImmDom || DT.DomTreeNodes[NULL]);
283
284     // Get or calculate the node for the immediate dominator
285     DomTreeNodeBase<typename GraphT::NodeType> *IDomNode =
286                                                      DT.getNodeForBlock(ImmDom);
287
288     // Add a new tree node for this BasicBlock, and link it as a child of
289     // IDomNode
290     DomTreeNodeBase<typename GraphT::NodeType> *C =
291                     new DomTreeNodeBase<typename GraphT::NodeType>(W, IDomNode);
292     DT.DomTreeNodes[W] = IDomNode->addChild(C);
293   }
294
295   // Free temporary memory used to construct idom's
296   DT.IDoms.clear();
297   DT.Info.clear();
298   std::vector<typename GraphT::NodeType*>().swap(DT.Vertex);
299
300   DT.updateDFSNumbers();
301 }
302
303 }
304
305 #endif