Don't attribute in file headers anymore. See llvmdev for the
[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/DenseMap.h"
15 #include "llvm/ADT/SmallPtrSet.h"
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 both the O(n*ack(n)) and the O(n*log(n)) versions of EVAL and
26 // LINK, but it turns out that the theoretically slower O(n*log(n))
27 // implementation is actually faster than the "efficient" algorithm (even for
28 // large CFGs) because the constant overheads are substantially smaller.  The
29 // lower-complexity version can be enabled with the following #define:
30 //
31 #define BALANCE_IDOM_TREE 0
32 //
33 //===----------------------------------------------------------------------===//
34
35 namespace llvm {
36
37 template<class GraphT>
38 unsigned DFSPass(DominatorTreeBase<typename GraphT::NodeType>& DT,
39                  typename GraphT::NodeType* V, unsigned N) {
40   // This is more understandable as a recursive algorithm, but we can't use the
41   // recursive algorithm due to stack depth issues.  Keep it here for
42   // documentation purposes.
43 #if 0
44   InfoRec &VInfo = DT.Info[DT.Roots[i]];
45   VInfo.Semi = ++N;
46   VInfo.Label = V;
47
48   Vertex.push_back(V);        // Vertex[n] = V;
49   //Info[V].Ancestor = 0;     // Ancestor[n] = 0
50   //Info[V].Child = 0;        // Child[v] = 0
51   VInfo.Size = 1;             // Size[v] = 1
52
53   for (succ_iterator SI = succ_begin(V), E = succ_end(V); SI != E; ++SI) {
54     InfoRec &SuccVInfo = DT.Info[*SI];
55     if (SuccVInfo.Semi == 0) {
56       SuccVInfo.Parent = V;
57       N = DTDFSPass(DT, *SI, N);
58     }
59   }
60 #else
61   std::vector<std::pair<typename GraphT::NodeType*,
62                         typename GraphT::ChildIteratorType> > Worklist;
63   Worklist.push_back(std::make_pair(V, GraphT::child_begin(V)));
64   while (!Worklist.empty()) {
65     typename GraphT::NodeType* BB = Worklist.back().first;
66     typename GraphT::ChildIteratorType NextSucc = Worklist.back().second;
67
68     // First time we visited this BB?
69     if (NextSucc == GraphT::child_begin(BB)) {
70       typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &BBInfo =
71                                                                     DT.Info[BB];
72       BBInfo.Semi = ++N;
73       BBInfo.Label = BB;
74
75       DT.Vertex.push_back(BB);       // Vertex[n] = V;
76       //BBInfo[V].Ancestor = 0;   // Ancestor[n] = 0
77       //BBInfo[V].Child = 0;      // Child[v] = 0
78       BBInfo.Size = 1;            // Size[v] = 1
79     }
80     
81     // If we are done with this block, remove it from the worklist.
82     if (NextSucc == GraphT::child_end(BB)) {
83       Worklist.pop_back();
84       continue;
85     }
86
87     // Increment the successor number for the next time we get to it.
88     ++Worklist.back().second;
89     
90     // Visit the successor next, if it isn't already visited.
91     typename GraphT::NodeType* Succ = *NextSucc;
92
93     typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &SuccVInfo =
94                                                                   DT.Info[Succ];
95     if (SuccVInfo.Semi == 0) {
96       SuccVInfo.Parent = BB;
97       Worklist.push_back(std::make_pair(Succ, GraphT::child_begin(Succ)));
98     }
99   }
100 #endif
101     return N;
102 }
103
104 template<class GraphT>
105 void Compress(DominatorTreeBase<typename GraphT::NodeType>& DT,
106               typename GraphT::NodeType *VIn) {
107   std::vector<typename GraphT::NodeType*> Work;
108   SmallPtrSet<typename GraphT::NodeType*, 32> Visited;
109   typename GraphT::NodeType* VInAncestor = DT.Info[VIn].Ancestor;
110   typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &VInVAInfo =
111                                                            DT.Info[VInAncestor];
112
113   if (VInVAInfo.Ancestor != 0)
114     Work.push_back(VIn);
115   
116   while (!Work.empty()) {
117     typename GraphT::NodeType* V = Work.back();
118     typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &VInfo =
119                                                                      DT.Info[V];
120     typename GraphT::NodeType* VAncestor = VInfo.Ancestor;
121     typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &VAInfo =
122                                                              DT.Info[VAncestor];
123
124     // Process Ancestor first
125     if (Visited.insert(VAncestor) &&
126         VAInfo.Ancestor != 0) {
127       Work.push_back(VAncestor);
128       continue;
129     } 
130     Work.pop_back(); 
131
132     // Update VInfo based on Ancestor info
133     if (VAInfo.Ancestor == 0)
134       continue;
135     typename GraphT::NodeType* VAncestorLabel = VAInfo.Label;
136     typename GraphT::NodeType* VLabel = VInfo.Label;
137     if (DT.Info[VAncestorLabel].Semi < DT.Info[VLabel].Semi)
138       VInfo.Label = VAncestorLabel;
139     VInfo.Ancestor = VAInfo.Ancestor;
140   }
141 }
142
143 template<class GraphT>
144 typename GraphT::NodeType* Eval(DominatorTreeBase<typename GraphT::NodeType>& DT,
145                                 typename GraphT::NodeType *V) {
146   typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &VInfo =
147                                                                      DT.Info[V];
148 #if !BALANCE_IDOM_TREE
149   // Higher-complexity but faster implementation
150   if (VInfo.Ancestor == 0)
151     return V;
152   Compress<GraphT>(DT, V);
153   return VInfo.Label;
154 #else
155   // Lower-complexity but slower implementation
156   if (VInfo.Ancestor == 0)
157     return VInfo.Label;
158   Compress<GraphT>(DT, V);
159   GraphT::NodeType* VLabel = VInfo.Label;
160
161   GraphT::NodeType* VAncestorLabel = DT.Info[VInfo.Ancestor].Label;
162   if (DT.Info[VAncestorLabel].Semi >= DT.Info[VLabel].Semi)
163     return VLabel;
164   else
165     return VAncestorLabel;
166 #endif
167 }
168
169 template<class GraphT>
170 void Link(DominatorTreeBase<typename GraphT::NodeType>& DT,
171           typename GraphT::NodeType* V, typename GraphT::NodeType* W,
172         typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &WInfo) {
173 #if !BALANCE_IDOM_TREE
174   // Higher-complexity but faster implementation
175   WInfo.Ancestor = V;
176 #else
177   // Lower-complexity but slower implementation
178   GraphT::NodeType* WLabel = WInfo.Label;
179   unsigned WLabelSemi = DT.Info[WLabel].Semi;
180   GraphT::NodeType* S = W;
181   InfoRec *SInfo = &DT.Info[S];
182
183   GraphT::NodeType* SChild = SInfo->Child;
184   InfoRec *SChildInfo = &DT.Info[SChild];
185
186   while (WLabelSemi < DT.Info[SChildInfo->Label].Semi) {
187     GraphT::NodeType* SChildChild = SChildInfo->Child;
188     if (SInfo->Size+DT.Info[SChildChild].Size >= 2*SChildInfo->Size) {
189       SChildInfo->Ancestor = S;
190       SInfo->Child = SChild = SChildChild;
191       SChildInfo = &DT.Info[SChild];
192     } else {
193       SChildInfo->Size = SInfo->Size;
194       S = SInfo->Ancestor = SChild;
195       SInfo = SChildInfo;
196       SChild = SChildChild;
197       SChildInfo = &DT.Info[SChild];
198     }
199   }
200
201   DominatorTreeBase::InfoRec &VInfo = DT.Info[V];
202   SInfo->Label = WLabel;
203
204   assert(V != W && "The optimization here will not work in this case!");
205   unsigned WSize = WInfo.Size;
206   unsigned VSize = (VInfo.Size += WSize);
207
208   if (VSize < 2*WSize)
209     std::swap(S, VInfo.Child);
210
211   while (S) {
212     SInfo = &DT.Info[S];
213     SInfo->Ancestor = V;
214     S = SInfo->Child;
215   }
216 #endif
217 }
218
219 template<class FuncT, class NodeT>
220 void Calculate(DominatorTreeBase<typename GraphTraits<NodeT>::NodeType>& DT,
221                FuncT& F) {
222   typedef GraphTraits<NodeT> GraphT;
223   
224   // Step #1: Number blocks in depth-first order and initialize variables used
225   // in later stages of the algorithm.
226   unsigned N = 0;
227   for (unsigned i = 0, e = DT.Roots.size(); i != e; ++i)
228     N = DFSPass<GraphT>(DT, DT.Roots[i], N);
229
230   for (unsigned i = N; i >= 2; --i) {
231     typename GraphT::NodeType* W = DT.Vertex[i];
232     typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &WInfo =
233                                                                      DT.Info[W];
234
235     // Step #2: Calculate the semidominators of all vertices
236     for (typename GraphTraits<Inverse<NodeT> >::ChildIteratorType CI =
237          GraphTraits<Inverse<NodeT> >::child_begin(W),
238          E = GraphTraits<Inverse<NodeT> >::child_end(W); CI != E; ++CI)
239       if (DT.Info.count(*CI)) {  // Only if this predecessor is reachable!
240         unsigned SemiU = DT.Info[Eval<GraphT>(DT, *CI)].Semi;
241         if (SemiU < WInfo.Semi)
242           WInfo.Semi = SemiU;
243       }
244
245     DT.Info[DT.Vertex[WInfo.Semi]].Bucket.push_back(W);
246
247     typename GraphT::NodeType* WParent = WInfo.Parent;
248     Link<GraphT>(DT, WParent, W, WInfo);
249
250     // Step #3: Implicitly define the immediate dominator of vertices
251     std::vector<typename GraphT::NodeType*> &WParentBucket =
252                                                         DT.Info[WParent].Bucket;
253     while (!WParentBucket.empty()) {
254       typename GraphT::NodeType* V = WParentBucket.back();
255       WParentBucket.pop_back();
256       typename GraphT::NodeType* U = Eval<GraphT>(DT, V);
257       DT.IDoms[V] = DT.Info[U].Semi < DT.Info[V].Semi ? U : WParent;
258     }
259   }
260
261   // Step #4: Explicitly define the immediate dominator of each vertex
262   for (unsigned i = 2; i <= N; ++i) {
263     typename GraphT::NodeType* W = DT.Vertex[i];
264     typename GraphT::NodeType*& WIDom = DT.IDoms[W];
265     if (WIDom != DT.Vertex[DT.Info[W].Semi])
266       WIDom = DT.IDoms[WIDom];
267   }
268   
269   if (DT.Roots.empty()) return;
270   
271   // Add a node for the root.  This node might be the actual root, if there is
272   // one exit block, or it may be the virtual exit (denoted by (BasicBlock *)0)
273   // which postdominates all real exits if there are multiple exit blocks.
274   typename GraphT::NodeType* Root = DT.Roots.size() == 1 ? DT.Roots[0]
275                                                          : 0;
276   DT.DomTreeNodes[Root] = DT.RootNode =
277                         new DomTreeNodeBase<typename GraphT::NodeType>(Root, 0);
278   
279   // Loop over all of the reachable blocks in the function...
280   for (typename FuncT::iterator I = F.begin(), E = F.end(); I != E; ++I)
281     if (typename GraphT::NodeType* ImmDom = DT.getIDom(I)) {
282       // Reachable block.
283       DomTreeNodeBase<typename GraphT::NodeType> *BBNode = DT.DomTreeNodes[I];
284       if (BBNode) continue;  // Haven't calculated this node yet?
285
286       // Get or calculate the node for the immediate dominator
287       DomTreeNodeBase<typename GraphT::NodeType> *IDomNode =
288                                                      DT.getNodeForBlock(ImmDom);
289
290       // Add a new tree node for this BasicBlock, and link it as a child of
291       // IDomNode
292       DomTreeNodeBase<typename GraphT::NodeType> *C =
293                     new DomTreeNodeBase<typename GraphT::NodeType>(I, IDomNode);
294       DT.DomTreeNodes[I] = IDomNode->addChild(C);
295     }
296   
297   // Free temporary memory used to construct idom's
298   DT.IDoms.clear();
299   DT.Info.clear();
300   std::vector<typename GraphT::NodeType*>().swap(DT.Vertex);
301   
302   // FIXME: This does not work on PostDomTrees.  It seems likely that this is
303   // due to an error in the algorithm for post-dominators.  This really should
304   // be investigated and fixed at some point.
305   // DT.updateDFSNumbers();
306
307   // Start out with the DFS numbers being invalid.  Let them be computed if
308   // demanded.
309   DT.DFSInfoValid = false;
310 }
311
312 }
313
314 #endif