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 SmallVector<typename GraphT::NodeType*, 32> 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*
156 Eval(DominatorTreeBase<typename GraphT::NodeType>& DT,
157 typename GraphT::NodeType *V) {
158 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &VInfo =
160 #if !BALANCE_IDOM_TREE
161 // Higher-complexity but faster implementation
162 if (VInfo.Ancestor == 0)
164 Compress<GraphT>(DT, V);
167 // Lower-complexity but slower implementation
168 if (VInfo.Ancestor == 0)
170 Compress<GraphT>(DT, V);
171 GraphT::NodeType* VLabel = VInfo.Label;
173 GraphT::NodeType* VAncestorLabel = DT.Info[VInfo.Ancestor].Label;
174 if (DT.Info[VAncestorLabel].Semi >= DT.Info[VLabel].Semi)
177 return VAncestorLabel;
181 template<class GraphT>
182 void Link(DominatorTreeBase<typename GraphT::NodeType>& DT,
183 unsigned DFSNumV, typename GraphT::NodeType* W,
184 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &WInfo) {
185 #if !BALANCE_IDOM_TREE
186 // Higher-complexity but faster implementation
187 WInfo.Ancestor = DFSNumV;
189 // Lower-complexity but slower implementation
190 GraphT::NodeType* WLabel = WInfo.Label;
191 unsigned WLabelSemi = DT.Info[WLabel].Semi;
192 GraphT::NodeType* S = W;
193 InfoRec *SInfo = &DT.Info[S];
195 GraphT::NodeType* SChild = SInfo->Child;
196 InfoRec *SChildInfo = &DT.Info[SChild];
198 while (WLabelSemi < DT.Info[SChildInfo->Label].Semi) {
199 GraphT::NodeType* SChildChild = SChildInfo->Child;
200 if (SInfo->Size+DT.Info[SChildChild].Size >= 2*SChildInfo->Size) {
201 SChildInfo->Ancestor = S;
202 SInfo->Child = SChild = SChildChild;
203 SChildInfo = &DT.Info[SChild];
205 SChildInfo->Size = SInfo->Size;
206 S = SInfo->Ancestor = SChild;
208 SChild = SChildChild;
209 SChildInfo = &DT.Info[SChild];
213 DominatorTreeBase::InfoRec &VInfo = DT.Info[V];
214 SInfo->Label = WLabel;
216 assert(V != W && "The optimization here will not work in this case!");
217 unsigned WSize = WInfo.Size;
218 unsigned VSize = (VInfo.Size += WSize);
221 std::swap(S, VInfo.Child);
231 template<class FuncT, class NodeT>
232 void Calculate(DominatorTreeBase<typename GraphTraits<NodeT>::NodeType>& DT,
234 typedef GraphTraits<NodeT> GraphT;
237 bool MultipleRoots = (DT.Roots.size() > 1);
239 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &BBInfo =
241 BBInfo.DFSNum = BBInfo.Semi = ++N;
244 DT.Vertex.push_back(NULL); // Vertex[n] = V;
245 //BBInfo[V].Ancestor = 0; // Ancestor[n] = 0
246 //BBInfo[V].Child = 0; // Child[v] = 0
247 BBInfo.Size = 1; // Size[v] = 1
250 // Step #1: Number blocks in depth-first order and initialize variables used
251 // in later stages of the algorithm.
252 for (unsigned i = 0, e = static_cast<unsigned>(DT.Roots.size());
254 N = DFSPass<GraphT>(DT, DT.Roots[i], N);
256 // it might be that some blocks did not get a DFS number (e.g., blocks of
257 // infinite loops). In these cases an artificial exit node is required.
258 MultipleRoots |= (DT.isPostDominator() && N != F.size());
260 for (unsigned i = N; i >= 2; --i) {
261 typename GraphT::NodeType* W = DT.Vertex[i];
262 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &WInfo =
265 // Step #2: Calculate the semidominators of all vertices
267 // initialize the semi dominator to point to the parent node
268 WInfo.Semi = WInfo.Parent;
269 typedef GraphTraits<Inverse<NodeT> > InvTraits;
270 for (typename InvTraits::ChildIteratorType CI =
271 InvTraits::child_begin(W),
272 E = InvTraits::child_end(W); CI != E; ++CI) {
273 typename InvTraits::NodeType *N = *CI;
274 if (DT.Info.count(N)) { // Only if this predecessor is reachable!
275 unsigned SemiU = DT.Info[Eval<GraphT>(DT, N)].Semi;
276 if (SemiU < WInfo.Semi)
281 typename GraphT::NodeType* WParent = DT.Vertex[WInfo.Parent];
283 // If V is a non-root vertex and sdom(V) = parent(V), then idom(V) is
284 // necessarily parent(V). In this case, set idom(V) here and avoid placing
286 if (WInfo.Semi == WInfo.Parent)
287 DT.IDoms[W] = WParent;
289 DT.Info[DT.Vertex[WInfo.Semi]].Bucket.push_back(W);
291 Link<GraphT>(DT, WInfo.Parent, W, WInfo);
293 // Step #3: Implicitly define the immediate dominator of vertices
294 std::vector<typename GraphT::NodeType*> &WParentBucket =
295 DT.Info[WParent].Bucket;
296 while (!WParentBucket.empty()) {
297 typename GraphT::NodeType* V = WParentBucket.back();
298 WParentBucket.pop_back();
299 typename GraphT::NodeType* U = Eval<GraphT>(DT, V);
300 DT.IDoms[V] = DT.Info[U].Semi < DT.Info[V].Semi ? U : WParent;
304 // Step #4: Explicitly define the immediate dominator of each vertex
305 for (unsigned i = 2; i <= N; ++i) {
306 typename GraphT::NodeType* W = DT.Vertex[i];
307 typename GraphT::NodeType*& WIDom = DT.IDoms[W];
308 if (WIDom != DT.Vertex[DT.Info[W].Semi])
309 WIDom = DT.IDoms[WIDom];
312 if (DT.Roots.empty()) return;
314 // Add a node for the root. This node might be the actual root, if there is
315 // one exit block, or it may be the virtual exit (denoted by (BasicBlock *)0)
316 // which postdominates all real exits if there are multiple exit blocks, or
318 typename GraphT::NodeType* Root = !MultipleRoots ? DT.Roots[0] : 0;
320 DT.DomTreeNodes[Root] = DT.RootNode =
321 new DomTreeNodeBase<typename GraphT::NodeType>(Root, 0);
323 // Loop over all of the reachable blocks in the function...
324 for (unsigned i = 2; i <= N; ++i) {
325 typename GraphT::NodeType* W = DT.Vertex[i];
327 DomTreeNodeBase<typename GraphT::NodeType> *BBNode = DT.DomTreeNodes[W];
328 if (BBNode) continue; // Haven't calculated this node yet?
330 typename GraphT::NodeType* ImmDom = DT.getIDom(W);
332 assert(ImmDom || DT.DomTreeNodes[NULL]);
334 // Get or calculate the node for the immediate dominator
335 DomTreeNodeBase<typename GraphT::NodeType> *IDomNode =
336 DT.getNodeForBlock(ImmDom);
338 // Add a new tree node for this BasicBlock, and link it as a child of
340 DomTreeNodeBase<typename GraphT::NodeType> *C =
341 new DomTreeNodeBase<typename GraphT::NodeType>(W, IDomNode);
342 DT.DomTreeNodes[W] = IDomNode->addChild(C);
345 // Free temporary memory used to construct idom's
348 std::vector<typename GraphT::NodeType*>().swap(DT.Vertex);
350 DT.updateDFSNumbers();