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 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.
29 //===----------------------------------------------------------------------===//
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.
40 InfoRec &VInfo = DT.Info[DT.Roots[i]];
41 VInfo.DFSNum = VInfo.Semi = ++N;
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
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) {
53 N = DTDFSPass(DT, *SI, N);
57 bool IsChilOfArtificialExit = (N != 0);
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;
66 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &BBInfo =
69 // First time we visited this BB?
70 if (NextSucc == GraphT::child_begin(BB)) {
71 BBInfo.DFSNum = BBInfo.Semi = ++N;
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
79 if (IsChilOfArtificialExit)
82 IsChilOfArtificialExit = false;
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;
89 // If we are done with this block, remove it from the worklist.
90 if (NextSucc == GraphT::child_end(BB)) {
95 // Increment the successor number for the next time we get to it.
96 ++Worklist.back().second;
98 // Visit the successor next, if it isn't already visited.
99 typename GraphT::NodeType* Succ = *NextSucc;
101 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &SuccVInfo =
103 if (SuccVInfo.Semi == 0) {
104 SuccVInfo.Parent = BBDFSNum;
105 Worklist.push_back(std::make_pair(Succ, GraphT::child_begin(Succ)));
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]];
120 if (VInVAInfo.Ancestor != 0)
123 while (!Work.empty()) {
124 typename GraphT::NodeType* V = Work.back();
125 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &VInfo =
127 typename GraphT::NodeType* VAncestor = DT.Vertex[VInfo.Ancestor];
128 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &VAInfo =
131 // Process Ancestor first
132 if (Visited.insert(VAncestor) &&
133 VAInfo.Ancestor != 0) {
134 Work.push_back(VAncestor);
139 // Update VInfo based on Ancestor info
140 if (VAInfo.Ancestor == 0)
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;
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 =
156 if (VInfo.Ancestor == 0)
158 Compress<GraphT>(DT, V);
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;
169 template<class FuncT, class NodeT>
170 void Calculate(DominatorTreeBase<typename GraphTraits<NodeT>::NodeType>& DT,
172 typedef GraphTraits<NodeT> GraphT;
175 bool MultipleRoots = (DT.Roots.size() > 1);
177 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &BBInfo =
179 BBInfo.DFSNum = BBInfo.Semi = ++N;
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
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());
192 N = DFSPass<GraphT>(DT, DT.Roots[i], N);
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());
198 // When naively implemented, the Lengauer-Tarjan algorithm requires a separate
199 // bucket for each vertex. However, this is unnecessary, because each vertex
200 // is only placed into a single bucket (that of its semidominator), and each
201 // vertex's bucket is processed before it is added to any bucket itself.
203 // Instead of using a bucket per vertex, we use a single array Buckets that
204 // has two purposes. Before the vertex V with preorder number i is processed,
205 // Buckets[i] stores the index of the first element in V's bucket. After V's
206 // bucket is processed, Buckets[i] stores the index of the next element in the
207 // bucket containing V, if any.
208 std::vector<unsigned> Buckets;
209 Buckets.resize(N + 1);
210 for (unsigned i = 1; i <= N; ++i)
213 for (unsigned i = N; i >= 2; --i) {
214 typename GraphT::NodeType* W = DT.Vertex[i];
215 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &WInfo =
218 // Step #2: Implicitly define the immediate dominator of vertices
219 for (unsigned j = i; Buckets[j] != i; j = Buckets[j]) {
220 typename GraphT::NodeType* V = DT.Vertex[Buckets[j]];
221 typename GraphT::NodeType* U = Eval<GraphT>(DT, V);
222 DT.IDoms[V] = DT.Info[U].Semi < i ? U : W;
225 // Step #3: Calculate the semidominators of all vertices
227 // initialize the semi dominator to point to the parent node
228 WInfo.Semi = WInfo.Parent;
229 typedef GraphTraits<Inverse<NodeT> > InvTraits;
230 for (typename InvTraits::ChildIteratorType CI =
231 InvTraits::child_begin(W),
232 E = InvTraits::child_end(W); CI != E; ++CI) {
233 typename InvTraits::NodeType *N = *CI;
234 if (DT.Info.count(N)) { // Only if this predecessor is reachable!
235 unsigned SemiU = DT.Info[Eval<GraphT>(DT, N)].Semi;
236 if (SemiU < WInfo.Semi)
241 // If V is a non-root vertex and sdom(V) = parent(V), then idom(V) is
242 // necessarily parent(V). In this case, set idom(V) here and avoid placing
244 if (WInfo.Semi == WInfo.Parent) {
245 DT.IDoms[W] = DT.Vertex[WInfo.Parent];
247 Buckets[i] = Buckets[WInfo.Semi];
248 Buckets[WInfo.Semi] = i;
251 Link<GraphT>(DT, WInfo.Parent, W, WInfo);
255 typename GraphT::NodeType* Root = DT.Vertex[1];
256 for (unsigned j = 1; Buckets[j] != 1; j = Buckets[j]) {
257 typename GraphT::NodeType* V = DT.Vertex[Buckets[j]];
262 // Step #4: Explicitly define the immediate dominator of each vertex
263 for (unsigned i = 2; i <= N; ++i) {
264 typename GraphT::NodeType* W = DT.Vertex[i];
265 typename GraphT::NodeType*& WIDom = DT.IDoms[W];
266 if (WIDom != DT.Vertex[DT.Info[W].Semi])
267 WIDom = DT.IDoms[WIDom];
270 if (DT.Roots.empty()) return;
272 // Add a node for the root. This node might be the actual root, if there is
273 // one exit block, or it may be the virtual exit (denoted by (BasicBlock *)0)
274 // which postdominates all real exits if there are multiple exit blocks, or
276 typename GraphT::NodeType* Root = !MultipleRoots ? DT.Roots[0] : 0;
278 DT.DomTreeNodes[Root] = DT.RootNode =
279 new DomTreeNodeBase<typename GraphT::NodeType>(Root, 0);
281 // Loop over all of the reachable blocks in the function...
282 for (unsigned i = 2; i <= N; ++i) {
283 typename GraphT::NodeType* W = DT.Vertex[i];
285 DomTreeNodeBase<typename GraphT::NodeType> *BBNode = DT.DomTreeNodes[W];
286 if (BBNode) continue; // Haven't calculated this node yet?
288 typename GraphT::NodeType* ImmDom = DT.getIDom(W);
290 assert(ImmDom || DT.DomTreeNodes[NULL]);
292 // Get or calculate the node for the immediate dominator
293 DomTreeNodeBase<typename GraphT::NodeType> *IDomNode =
294 DT.getNodeForBlock(ImmDom);
296 // Add a new tree node for this BasicBlock, and link it as a child of
298 DomTreeNodeBase<typename GraphT::NodeType> *C =
299 new DomTreeNodeBase<typename GraphT::NodeType>(W, IDomNode);
300 DT.DomTreeNodes[W] = IDomNode->addChild(C);
303 // Free temporary memory used to construct idom's
306 std::vector<typename GraphT::NodeType*>().swap(DT.Vertex);
308 DT.updateDFSNumbers();