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 std::vector<unsigned> Buckets;
199 Buckets.resize(N + 1);
200 for (unsigned i = 1; i <= N; ++i)
203 for (unsigned i = N; i >= 2; --i) {
204 typename GraphT::NodeType* W = DT.Vertex[i];
205 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &WInfo =
208 // Step #2: Implicitly define the immediate dominator of vertices
209 for (unsigned j = i; Buckets[j] != i; j = Buckets[j]) {
210 typename GraphT::NodeType* V = DT.Vertex[Buckets[j]];
211 typename GraphT::NodeType* U = Eval<GraphT>(DT, V);
212 DT.IDoms[V] = DT.Info[U].Semi < i ? U : W;
215 // Step #3: Calculate the semidominators of all vertices
217 // initialize the semi dominator to point to the parent node
218 WInfo.Semi = WInfo.Parent;
219 typedef GraphTraits<Inverse<NodeT> > InvTraits;
220 for (typename InvTraits::ChildIteratorType CI =
221 InvTraits::child_begin(W),
222 E = InvTraits::child_end(W); CI != E; ++CI) {
223 typename InvTraits::NodeType *N = *CI;
224 if (DT.Info.count(N)) { // Only if this predecessor is reachable!
225 unsigned SemiU = DT.Info[Eval<GraphT>(DT, N)].Semi;
226 if (SemiU < WInfo.Semi)
231 typename GraphT::NodeType* WParent = DT.Vertex[WInfo.Parent];
233 // If V is a non-root vertex and sdom(V) = parent(V), then idom(V) is
234 // necessarily parent(V). In this case, set idom(V) here and avoid placing
236 if (WInfo.Semi == WInfo.Parent) {
237 DT.IDoms[W] = WParent;
239 Buckets[i] = Buckets[WInfo.Semi];
240 Buckets[WInfo.Semi] = i;
243 Link<GraphT>(DT, WInfo.Parent, W, WInfo);
247 typename GraphT::NodeType* Root = DT.Vertex[1];
248 for (unsigned j = 1; Buckets[j] != 1; j = Buckets[j]) {
249 typename GraphT::NodeType* V = DT.Vertex[Buckets[j]];
254 // Step #4: Explicitly define the immediate dominator of each vertex
255 for (unsigned i = 2; i <= N; ++i) {
256 typename GraphT::NodeType* W = DT.Vertex[i];
257 typename GraphT::NodeType*& WIDom = DT.IDoms[W];
258 if (WIDom != DT.Vertex[DT.Info[W].Semi])
259 WIDom = DT.IDoms[WIDom];
262 if (DT.Roots.empty()) return;
264 // Add a node for the root. This node might be the actual root, if there is
265 // one exit block, or it may be the virtual exit (denoted by (BasicBlock *)0)
266 // which postdominates all real exits if there are multiple exit blocks, or
268 typename GraphT::NodeType* Root = !MultipleRoots ? DT.Roots[0] : 0;
270 DT.DomTreeNodes[Root] = DT.RootNode =
271 new DomTreeNodeBase<typename GraphT::NodeType>(Root, 0);
273 // Loop over all of the reachable blocks in the function...
274 for (unsigned i = 2; i <= N; ++i) {
275 typename GraphT::NodeType* W = DT.Vertex[i];
277 DomTreeNodeBase<typename GraphT::NodeType> *BBNode = DT.DomTreeNodes[W];
278 if (BBNode) continue; // Haven't calculated this node yet?
280 typename GraphT::NodeType* ImmDom = DT.getIDom(W);
282 assert(ImmDom || DT.DomTreeNodes[NULL]);
284 // Get or calculate the node for the immediate dominator
285 DomTreeNodeBase<typename GraphT::NodeType> *IDomNode =
286 DT.getNodeForBlock(ImmDom);
288 // Add a new tree node for this BasicBlock, and link it as a child of
290 DomTreeNodeBase<typename GraphT::NodeType> *C =
291 new DomTreeNodeBase<typename GraphT::NodeType>(W, IDomNode);
292 DT.DomTreeNodes[W] = IDomNode->addChild(C);
295 // Free temporary memory used to construct idom's
298 std::vector<typename GraphT::NodeType*>().swap(DT.Vertex);
300 DT.updateDFSNumbers();