1 //=== llvm/Analysis/DominatorInternals.h - Dominator Calculation -*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_ANALYSIS_DOMINATOR_INTERNALS_H
11 #define LLVM_ANALYSIS_DOMINATOR_INTERNALS_H
13 #include "llvm/Analysis/Dominators.h"
14 #include "llvm/ADT/SmallPtrSet.h"
16 //===----------------------------------------------------------------------===//
18 // DominatorTree construction - This pass constructs immediate dominator
19 // information for a flow-graph based on the algorithm described in this
22 // A Fast Algorithm for Finding Dominators in a Flowgraph
23 // T. Lengauer & R. Tarjan, ACM TOPLAS July 1979, pgs 121-141.
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:
31 #define BALANCE_IDOM_TREE 0
33 //===----------------------------------------------------------------------===//
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.
44 InfoRec &VInfo = DT.Info[DT.Roots[i]];
45 VInfo.DFSNum = VInfo.Semi = ++N;
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
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) {
57 N = DTDFSPass(DT, *SI, N);
61 bool IsChilOfArtificialExit = (N != 0);
63 std::vector<std::pair<typename GraphT::NodeType*,
64 typename GraphT::ChildIteratorType> > Worklist;
65 Worklist.push_back(std::make_pair(V, GraphT::child_begin(V)));
66 while (!Worklist.empty()) {
67 typename GraphT::NodeType* BB = Worklist.back().first;
68 typename GraphT::ChildIteratorType NextSucc = Worklist.back().second;
70 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &BBInfo =
73 // First time we visited this BB?
74 if (NextSucc == GraphT::child_begin(BB)) {
75 BBInfo.DFSNum = BBInfo.Semi = ++N;
78 DT.Vertex.push_back(BB); // Vertex[n] = V;
79 //BBInfo[V].Ancestor = 0; // Ancestor[n] = 0
80 //BBInfo[V].Child = 0; // Child[v] = 0
81 BBInfo.Size = 1; // Size[v] = 1
83 if (IsChilOfArtificialExit)
86 IsChilOfArtificialExit = false;
89 // store the DFS number of the current BB - the reference to BBInfo might
90 // get invalidated when processing the successors.
91 unsigned BBDFSNum = BBInfo.DFSNum;
93 // If we are done with this block, remove it from the worklist.
94 if (NextSucc == GraphT::child_end(BB)) {
99 // Increment the successor number for the next time we get to it.
100 ++Worklist.back().second;
102 // Visit the successor next, if it isn't already visited.
103 typename GraphT::NodeType* Succ = *NextSucc;
105 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &SuccVInfo =
107 if (SuccVInfo.Semi == 0) {
108 SuccVInfo.Parent = BBDFSNum;
109 Worklist.push_back(std::make_pair(Succ, GraphT::child_begin(Succ)));
116 template<class GraphT>
117 void Compress(DominatorTreeBase<typename GraphT::NodeType>& DT,
118 typename GraphT::NodeType *VIn) {
119 std::vector<typename GraphT::NodeType*> Work;
120 SmallPtrSet<typename GraphT::NodeType*, 32> Visited;
121 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &VInVAInfo =
122 DT.Info[DT.Vertex[DT.Info[VIn].Ancestor]];
124 if (VInVAInfo.Ancestor != 0)
127 while (!Work.empty()) {
128 typename GraphT::NodeType* V = Work.back();
129 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &VInfo =
131 typename GraphT::NodeType* VAncestor = DT.Vertex[VInfo.Ancestor];
132 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &VAInfo =
135 // Process Ancestor first
136 if (Visited.insert(VAncestor) &&
137 VAInfo.Ancestor != 0) {
138 Work.push_back(VAncestor);
143 // Update VInfo based on Ancestor info
144 if (VAInfo.Ancestor == 0)
146 typename GraphT::NodeType* VAncestorLabel = VAInfo.Label;
147 typename GraphT::NodeType* VLabel = VInfo.Label;
148 if (DT.Info[VAncestorLabel].Semi < DT.Info[VLabel].Semi)
149 VInfo.Label = VAncestorLabel;
150 VInfo.Ancestor = VAInfo.Ancestor;
154 template<class GraphT>
155 typename GraphT::NodeType* Eval(DominatorTreeBase<typename GraphT::NodeType>& DT,
156 typename GraphT::NodeType *V) {
157 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &VInfo =
159 #if !BALANCE_IDOM_TREE
160 // Higher-complexity but faster implementation
161 if (VInfo.Ancestor == 0)
163 Compress<GraphT>(DT, V);
166 // Lower-complexity but slower implementation
167 if (VInfo.Ancestor == 0)
169 Compress<GraphT>(DT, V);
170 GraphT::NodeType* VLabel = VInfo.Label;
172 GraphT::NodeType* VAncestorLabel = DT.Info[VInfo.Ancestor].Label;
173 if (DT.Info[VAncestorLabel].Semi >= DT.Info[VLabel].Semi)
176 return VAncestorLabel;
180 template<class GraphT>
181 void Link(DominatorTreeBase<typename GraphT::NodeType>& DT,
182 unsigned DFSNumV, typename GraphT::NodeType* W,
183 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &WInfo) {
184 #if !BALANCE_IDOM_TREE
185 // Higher-complexity but faster implementation
186 WInfo.Ancestor = DFSNumV;
188 // Lower-complexity but slower implementation
189 GraphT::NodeType* WLabel = WInfo.Label;
190 unsigned WLabelSemi = DT.Info[WLabel].Semi;
191 GraphT::NodeType* S = W;
192 InfoRec *SInfo = &DT.Info[S];
194 GraphT::NodeType* SChild = SInfo->Child;
195 InfoRec *SChildInfo = &DT.Info[SChild];
197 while (WLabelSemi < DT.Info[SChildInfo->Label].Semi) {
198 GraphT::NodeType* SChildChild = SChildInfo->Child;
199 if (SInfo->Size+DT.Info[SChildChild].Size >= 2*SChildInfo->Size) {
200 SChildInfo->Ancestor = S;
201 SInfo->Child = SChild = SChildChild;
202 SChildInfo = &DT.Info[SChild];
204 SChildInfo->Size = SInfo->Size;
205 S = SInfo->Ancestor = SChild;
207 SChild = SChildChild;
208 SChildInfo = &DT.Info[SChild];
212 DominatorTreeBase::InfoRec &VInfo = DT.Info[V];
213 SInfo->Label = WLabel;
215 assert(V != W && "The optimization here will not work in this case!");
216 unsigned WSize = WInfo.Size;
217 unsigned VSize = (VInfo.Size += WSize);
220 std::swap(S, VInfo.Child);
230 template<class FuncT, class NodeT>
231 void Calculate(DominatorTreeBase<typename GraphTraits<NodeT>::NodeType>& DT,
233 typedef GraphTraits<NodeT> GraphT;
236 bool MultipleRoots = (DT.Roots.size() > 1);
238 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &BBInfo =
240 BBInfo.DFSNum = BBInfo.Semi = ++N;
243 DT.Vertex.push_back(NULL); // Vertex[n] = V;
244 //BBInfo[V].Ancestor = 0; // Ancestor[n] = 0
245 //BBInfo[V].Child = 0; // Child[v] = 0
246 BBInfo.Size = 1; // Size[v] = 1
249 // Step #1: Number blocks in depth-first order and initialize variables used
250 // in later stages of the algorithm.
251 for (unsigned i = 0, e = static_cast<unsigned>(DT.Roots.size());
253 N = DFSPass<GraphT>(DT, DT.Roots[i], N);
255 // it might be that some blocks did not get a DFS number (e.g., blocks of
256 // infinite loops). In these cases an artificial exit node is required.
257 MultipleRoots |= (DT.isPostDominator() && N != F.size());
259 for (unsigned i = N; i >= 2; --i) {
260 typename GraphT::NodeType* W = DT.Vertex[i];
261 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &WInfo =
264 // Step #2: Calculate the semidominators of all vertices
265 bool HasChildOutsideDFS = false;
267 // initialize the semi dominator to point to the parent node
268 WInfo.Semi = WInfo.Parent;
269 for (typename GraphTraits<Inverse<NodeT> >::ChildIteratorType CI =
270 GraphTraits<Inverse<NodeT> >::child_begin(W),
271 E = GraphTraits<Inverse<NodeT> >::child_end(W); CI != E; ++CI) {
272 if (DT.Info.count(*CI)) { // Only if this predecessor is reachable!
273 unsigned SemiU = DT.Info[Eval<GraphT>(DT, *CI)].Semi;
274 if (SemiU < WInfo.Semi)
278 // if the child has no DFS number it is not post-dominated by any exit,
279 // and so is the current block.
280 HasChildOutsideDFS = true;
284 // if some child has no DFS number it is not post-dominated by any exit,
285 // and so is the current block.
286 if (DT.isPostDominator() && HasChildOutsideDFS)
289 DT.Info[DT.Vertex[WInfo.Semi]].Bucket.push_back(W);
291 typename GraphT::NodeType* WParent = DT.Vertex[WInfo.Parent];
292 Link<GraphT>(DT, WInfo.Parent, W, WInfo);
294 // Step #3: Implicitly define the immediate dominator of vertices
295 std::vector<typename GraphT::NodeType*> &WParentBucket =
296 DT.Info[WParent].Bucket;
297 while (!WParentBucket.empty()) {
298 typename GraphT::NodeType* V = WParentBucket.back();
299 WParentBucket.pop_back();
300 typename GraphT::NodeType* U = Eval<GraphT>(DT, V);
301 DT.IDoms[V] = DT.Info[U].Semi < DT.Info[V].Semi ? U : WParent;
305 // Step #4: Explicitly define the immediate dominator of each vertex
306 for (unsigned i = 2; i <= N; ++i) {
307 typename GraphT::NodeType* W = DT.Vertex[i];
308 typename GraphT::NodeType*& WIDom = DT.IDoms[W];
309 if (WIDom != DT.Vertex[DT.Info[W].Semi])
310 WIDom = DT.IDoms[WIDom];
313 if (DT.Roots.empty()) return;
315 // Add a node for the root. This node might be the actual root, if there is
316 // one exit block, or it may be the virtual exit (denoted by (BasicBlock *)0)
317 // which postdominates all real exits if there are multiple exit blocks, or
319 typename GraphT::NodeType* Root = !MultipleRoots ? DT.Roots[0] : 0;
321 DT.DomTreeNodes[Root] = DT.RootNode =
322 new DomTreeNodeBase<typename GraphT::NodeType>(Root, 0);
324 // Loop over all of the reachable blocks in the function...
325 for (unsigned i = 2; i <= N; ++i) {
326 typename GraphT::NodeType* W = DT.Vertex[i];
328 DomTreeNodeBase<typename GraphT::NodeType> *BBNode = DT.DomTreeNodes[W];
329 if (BBNode) continue; // Haven't calculated this node yet?
331 typename GraphT::NodeType* ImmDom = DT.getIDom(W);
333 assert(ImmDom || DT.DomTreeNodes[NULL]);
335 // Get or calculate the node for the immediate dominator
336 DomTreeNodeBase<typename GraphT::NodeType> *IDomNode =
337 DT.getNodeForBlock(ImmDom);
339 // Add a new tree node for this BasicBlock, and link it as a child of
341 DomTreeNodeBase<typename GraphT::NodeType> *C =
342 new DomTreeNodeBase<typename GraphT::NodeType>(W, IDomNode);
343 DT.DomTreeNodes[W] = IDomNode->addChild(C);
346 // Free temporary memory used to construct idom's
349 std::vector<typename GraphT::NodeType*>().swap(DT.Vertex);
351 // FIXME: This does not work on PostDomTrees. It seems likely that this is
352 // due to an error in the algorithm for post-dominators. This really should
353 // be investigated and fixed at some point.
354 // DT.updateDFSNumbers();
356 // Start out with the DFS numbers being invalid. Let them be computed if
358 DT.DFSInfoValid = false;