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/ADT/SmallPtrSet.h"
14 #include "llvm/Analysis/Dominators.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;
46 for (succ_iterator SI = succ_begin(V), E = succ_end(V); SI != E; ++SI) {
47 InfoRec &SuccVInfo = DT.Info[*SI];
48 if (SuccVInfo.Semi == 0) {
50 N = DTDFSPass(DT, *SI, N);
54 bool IsChildOfArtificialExit = (N != 0);
56 SmallVector<std::pair<typename GraphT::NodeType*,
57 typename GraphT::ChildIteratorType>, 32> Worklist;
58 Worklist.push_back(std::make_pair(V, GraphT::child_begin(V)));
59 while (!Worklist.empty()) {
60 typename GraphT::NodeType* BB = Worklist.back().first;
61 typename GraphT::ChildIteratorType NextSucc = Worklist.back().second;
63 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &BBInfo =
66 // First time we visited this BB?
67 if (NextSucc == GraphT::child_begin(BB)) {
68 BBInfo.DFSNum = BBInfo.Semi = ++N;
71 DT.Vertex.push_back(BB); // Vertex[n] = V;
73 if (IsChildOfArtificialExit)
76 IsChildOfArtificialExit = false;
79 // store the DFS number of the current BB - the reference to BBInfo might
80 // get invalidated when processing the successors.
81 unsigned BBDFSNum = BBInfo.DFSNum;
83 // If we are done with this block, remove it from the worklist.
84 if (NextSucc == GraphT::child_end(BB)) {
89 // Increment the successor number for the next time we get to it.
90 ++Worklist.back().second;
92 // Visit the successor next, if it isn't already visited.
93 typename GraphT::NodeType* Succ = *NextSucc;
95 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &SuccVInfo =
97 if (SuccVInfo.Semi == 0) {
98 SuccVInfo.Parent = BBDFSNum;
99 Worklist.push_back(std::make_pair(Succ, GraphT::child_begin(Succ)));
106 template<class GraphT>
107 typename GraphT::NodeType*
108 Eval(DominatorTreeBase<typename GraphT::NodeType>& DT,
109 typename GraphT::NodeType *VIn, unsigned LastLinked) {
110 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &VInInfo =
112 if (VInInfo.DFSNum < LastLinked)
115 SmallVector<typename GraphT::NodeType*, 32> Work;
116 SmallPtrSet<typename GraphT::NodeType*, 32> Visited;
118 if (VInInfo.Parent >= LastLinked)
121 while (!Work.empty()) {
122 typename GraphT::NodeType* V = Work.back();
123 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &VInfo =
125 typename GraphT::NodeType* VAncestor = DT.Vertex[VInfo.Parent];
127 // Process Ancestor first
128 if (Visited.insert(VAncestor) && VInfo.Parent >= LastLinked) {
129 Work.push_back(VAncestor);
134 // Update VInfo based on Ancestor info
135 if (VInfo.Parent < LastLinked)
138 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &VAInfo =
140 typename GraphT::NodeType* VAncestorLabel = VAInfo.Label;
141 typename GraphT::NodeType* VLabel = VInfo.Label;
142 if (DT.Info[VAncestorLabel].Semi < DT.Info[VLabel].Semi)
143 VInfo.Label = VAncestorLabel;
144 VInfo.Parent = VAInfo.Parent;
147 return VInInfo.Label;
150 template<class FuncT, class NodeT>
151 void Calculate(DominatorTreeBase<typename GraphTraits<NodeT>::NodeType>& DT,
153 typedef GraphTraits<NodeT> GraphT;
156 bool MultipleRoots = (DT.Roots.size() > 1);
158 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &BBInfo =
160 BBInfo.DFSNum = BBInfo.Semi = ++N;
163 DT.Vertex.push_back(NULL); // Vertex[n] = V;
166 // Step #1: Number blocks in depth-first order and initialize variables used
167 // in later stages of the algorithm.
168 for (unsigned i = 0, e = static_cast<unsigned>(DT.Roots.size());
170 N = DFSPass<GraphT>(DT, DT.Roots[i], N);
172 // it might be that some blocks did not get a DFS number (e.g., blocks of
173 // infinite loops). In these cases an artificial exit node is required.
174 MultipleRoots |= (DT.isPostDominator() && N != GraphTraits<FuncT*>::size(&F));
176 // When naively implemented, the Lengauer-Tarjan algorithm requires a separate
177 // bucket for each vertex. However, this is unnecessary, because each vertex
178 // is only placed into a single bucket (that of its semidominator), and each
179 // vertex's bucket is processed before it is added to any bucket itself.
181 // Instead of using a bucket per vertex, we use a single array Buckets that
182 // has two purposes. Before the vertex V with preorder number i is processed,
183 // Buckets[i] stores the index of the first element in V's bucket. After V's
184 // bucket is processed, Buckets[i] stores the index of the next element in the
185 // bucket containing V, if any.
186 SmallVector<unsigned, 32> Buckets;
187 Buckets.resize(N + 1);
188 for (unsigned i = 1; i <= N; ++i)
191 for (unsigned i = N; i >= 2; --i) {
192 typename GraphT::NodeType* W = DT.Vertex[i];
193 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &WInfo =
196 // Step #2: Implicitly define the immediate dominator of vertices
197 for (unsigned j = i; Buckets[j] != i; j = Buckets[j]) {
198 typename GraphT::NodeType* V = DT.Vertex[Buckets[j]];
199 typename GraphT::NodeType* U = Eval<GraphT>(DT, V, i + 1);
200 DT.IDoms[V] = DT.Info[U].Semi < i ? U : W;
203 // Step #3: Calculate the semidominators of all vertices
205 // initialize the semi dominator to point to the parent node
206 WInfo.Semi = WInfo.Parent;
207 typedef GraphTraits<Inverse<NodeT> > InvTraits;
208 for (typename InvTraits::ChildIteratorType CI =
209 InvTraits::child_begin(W),
210 E = InvTraits::child_end(W); CI != E; ++CI) {
211 typename InvTraits::NodeType *N = *CI;
212 if (DT.Info.count(N)) { // Only if this predecessor is reachable!
213 unsigned SemiU = DT.Info[Eval<GraphT>(DT, N, i + 1)].Semi;
214 if (SemiU < WInfo.Semi)
219 // If V is a non-root vertex and sdom(V) = parent(V), then idom(V) is
220 // necessarily parent(V). In this case, set idom(V) here and avoid placing
222 if (WInfo.Semi == WInfo.Parent) {
223 DT.IDoms[W] = DT.Vertex[WInfo.Parent];
225 Buckets[i] = Buckets[WInfo.Semi];
226 Buckets[WInfo.Semi] = i;
231 typename GraphT::NodeType* Root = DT.Vertex[1];
232 for (unsigned j = 1; Buckets[j] != 1; j = Buckets[j]) {
233 typename GraphT::NodeType* V = DT.Vertex[Buckets[j]];
238 // Step #4: Explicitly define the immediate dominator of each vertex
239 for (unsigned i = 2; i <= N; ++i) {
240 typename GraphT::NodeType* W = DT.Vertex[i];
241 typename GraphT::NodeType*& WIDom = DT.IDoms[W];
242 if (WIDom != DT.Vertex[DT.Info[W].Semi])
243 WIDom = DT.IDoms[WIDom];
246 if (DT.Roots.empty()) return;
248 // Add a node for the root. This node might be the actual root, if there is
249 // one exit block, or it may be the virtual exit (denoted by (BasicBlock *)0)
250 // which postdominates all real exits if there are multiple exit blocks, or
252 typename GraphT::NodeType* Root = !MultipleRoots ? DT.Roots[0] : 0;
254 DT.DomTreeNodes[Root] = DT.RootNode =
255 new DomTreeNodeBase<typename GraphT::NodeType>(Root, 0);
257 // Loop over all of the reachable blocks in the function...
258 for (unsigned i = 2; i <= N; ++i) {
259 typename GraphT::NodeType* W = DT.Vertex[i];
261 DomTreeNodeBase<typename GraphT::NodeType> *BBNode = DT.DomTreeNodes[W];
262 if (BBNode) continue; // Haven't calculated this node yet?
264 typename GraphT::NodeType* ImmDom = DT.getIDom(W);
266 assert(ImmDom || DT.DomTreeNodes[NULL]);
268 // Get or calculate the node for the immediate dominator
269 DomTreeNodeBase<typename GraphT::NodeType> *IDomNode =
270 DT.getNodeForBlock(ImmDom);
272 // Add a new tree node for this BasicBlock, and link it as a child of
274 DomTreeNodeBase<typename GraphT::NodeType> *C =
275 new DomTreeNodeBase<typename GraphT::NodeType>(W, IDomNode);
276 DT.DomTreeNodes[W] = IDomNode->addChild(C);
279 // Free temporary memory used to construct idom's
282 std::vector<typename GraphT::NodeType*>().swap(DT.Vertex);
284 DT.updateDFSNumbers();