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/DenseMap.h"
15 #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]];
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 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;
68 // First time we visited this BB?
69 if (NextSucc == GraphT::child_begin(BB)) {
70 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &BBInfo =
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
81 // If we are done with this block, remove it from the worklist.
82 if (NextSucc == GraphT::child_end(BB)) {
87 // Increment the successor number for the next time we get to it.
88 ++Worklist.back().second;
90 // Visit the successor next, if it isn't already visited.
91 typename GraphT::NodeType* Succ = *NextSucc;
93 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &SuccVInfo =
95 if (SuccVInfo.Semi == 0) {
96 SuccVInfo.Parent = BB;
97 Worklist.push_back(std::make_pair(Succ, GraphT::child_begin(Succ)));
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];
113 if (VInVAInfo.Ancestor != 0)
116 while (!Work.empty()) {
117 typename GraphT::NodeType* V = Work.back();
118 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &VInfo =
120 typename GraphT::NodeType* VAncestor = VInfo.Ancestor;
121 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &VAInfo =
124 // Process Ancestor first
125 if (Visited.insert(VAncestor) &&
126 VAInfo.Ancestor != 0) {
127 Work.push_back(VAncestor);
132 // Update VInfo based on Ancestor info
133 if (VAInfo.Ancestor == 0)
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;
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 =
148 #if !BALANCE_IDOM_TREE
149 // Higher-complexity but faster implementation
150 if (VInfo.Ancestor == 0)
152 Compress<GraphT>(DT, V);
155 // Lower-complexity but slower implementation
156 if (VInfo.Ancestor == 0)
158 Compress<GraphT>(DT, V);
159 GraphT::NodeType* VLabel = VInfo.Label;
161 GraphT::NodeType* VAncestorLabel = DT.Info[VInfo.Ancestor].Label;
162 if (DT.Info[VAncestorLabel].Semi >= DT.Info[VLabel].Semi)
165 return VAncestorLabel;
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
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];
183 GraphT::NodeType* SChild = SInfo->Child;
184 InfoRec *SChildInfo = &DT.Info[SChild];
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];
193 SChildInfo->Size = SInfo->Size;
194 S = SInfo->Ancestor = SChild;
196 SChild = SChildChild;
197 SChildInfo = &DT.Info[SChild];
201 DominatorTreeBase::InfoRec &VInfo = DT.Info[V];
202 SInfo->Label = WLabel;
204 assert(V != W && "The optimization here will not work in this case!");
205 unsigned WSize = WInfo.Size;
206 unsigned VSize = (VInfo.Size += WSize);
209 std::swap(S, VInfo.Child);
219 template<class FuncT, class NodeT>
220 void Calculate(DominatorTreeBase<typename GraphTraits<NodeT>::NodeType>& DT,
222 typedef GraphTraits<NodeT> GraphT;
224 // Step #1: Number blocks in depth-first order and initialize variables used
225 // in later stages of the algorithm.
227 for (unsigned i = 0, e = DT.Roots.size(); i != e; ++i)
228 N = DFSPass<GraphT>(DT, DT.Roots[i], N);
230 for (unsigned i = N; i >= 2; --i) {
231 typename GraphT::NodeType* W = DT.Vertex[i];
232 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &WInfo =
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)
245 DT.Info[DT.Vertex[WInfo.Semi]].Bucket.push_back(W);
247 typename GraphT::NodeType* WParent = WInfo.Parent;
248 Link<GraphT>(DT, WParent, W, WInfo);
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;
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];
269 if (DT.Roots.empty()) return;
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]
276 DT.DomTreeNodes[Root] = DT.RootNode =
277 new DomTreeNodeBase<typename GraphT::NodeType>(Root, 0);
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)) {
283 DomTreeNodeBase<typename GraphT::NodeType> *BBNode = DT.DomTreeNodes[I];
284 if (BBNode) continue; // Haven't calculated this node yet?
286 // Get or calculate the node for the immediate dominator
287 DomTreeNodeBase<typename GraphT::NodeType> *IDomNode =
288 DT.getNodeForBlock(ImmDom);
290 // Add a new tree node for this BasicBlock, and link it as a child of
292 DomTreeNodeBase<typename GraphT::NodeType> *C =
293 new DomTreeNodeBase<typename GraphT::NodeType>(I, IDomNode);
294 DT.DomTreeNodes[I] = IDomNode->addChild(C);
297 // Free temporary memory used to construct idom's
300 std::vector<typename GraphT::NodeType*>().swap(DT.Vertex);
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();
307 // Start out with the DFS numbers being invalid. Let them be computed if
309 DT.DFSInfoValid = false;