1 //===- GenericDomTreeConstruction.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 //===----------------------------------------------------------------------===//
11 /// Generic dominator tree construction - This file provides routines to
12 /// construct immediate dominator information for a flow-graph based on the
13 /// algorithm described in this document:
15 /// A Fast Algorithm for Finding Dominators in a Flowgraph
16 /// T. Lengauer & R. Tarjan, ACM TOPLAS July 1979, pgs 121-141.
18 /// This implements the O(n*log(n)) versions of EVAL and LINK, because it turns
19 /// out that the theoretically slower O(n*log(n)) implementation is actually
20 /// faster than the almost-linear O(n*alpha(n)) version, even for large CFGs.
22 //===----------------------------------------------------------------------===//
24 #ifndef LLVM_SUPPORT_GENERICDOMTREECONSTRUCTION_H
25 #define LLVM_SUPPORT_GENERICDOMTREECONSTRUCTION_H
27 #include "llvm/ADT/SmallPtrSet.h"
28 #include "llvm/Support/GenericDomTree.h"
32 template<class GraphT>
33 unsigned DFSPass(DominatorTreeBase<typename GraphT::NodeType>& DT,
34 typename GraphT::NodeType* V, unsigned N) {
35 // This is more understandable as a recursive algorithm, but we can't use the
36 // recursive algorithm due to stack depth issues. Keep it here for
37 // documentation purposes.
39 InfoRec &VInfo = DT.Info[DT.Roots[i]];
40 VInfo.DFSNum = VInfo.Semi = ++N;
43 Vertex.push_back(V); // Vertex[n] = V;
45 for (succ_iterator SI = succ_begin(V), E = succ_end(V); SI != E; ++SI) {
46 InfoRec &SuccVInfo = DT.Info[*SI];
47 if (SuccVInfo.Semi == 0) {
49 N = DTDFSPass(DT, *SI, N);
53 bool IsChildOfArtificialExit = (N != 0);
55 SmallVector<std::pair<typename GraphT::NodeType*,
56 typename GraphT::ChildIteratorType>, 32> Worklist;
57 Worklist.push_back(std::make_pair(V, GraphT::child_begin(V)));
58 while (!Worklist.empty()) {
59 typename GraphT::NodeType* BB = Worklist.back().first;
60 typename GraphT::ChildIteratorType NextSucc = Worklist.back().second;
62 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &BBInfo =
65 // First time we visited this BB?
66 if (NextSucc == GraphT::child_begin(BB)) {
67 BBInfo.DFSNum = BBInfo.Semi = ++N;
70 DT.Vertex.push_back(BB); // Vertex[n] = V;
72 if (IsChildOfArtificialExit)
75 IsChildOfArtificialExit = false;
78 // store the DFS number of the current BB - the reference to BBInfo might
79 // get invalidated when processing the successors.
80 unsigned BBDFSNum = BBInfo.DFSNum;
82 // If we are done with this block, remove it from the worklist.
83 if (NextSucc == GraphT::child_end(BB)) {
88 // Increment the successor number for the next time we get to it.
89 ++Worklist.back().second;
91 // Visit the successor next, if it isn't already visited.
92 typename GraphT::NodeType* Succ = *NextSucc;
94 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &SuccVInfo =
96 if (SuccVInfo.Semi == 0) {
97 SuccVInfo.Parent = BBDFSNum;
98 Worklist.push_back(std::make_pair(Succ, GraphT::child_begin(Succ)));
105 template <class GraphT>
106 typename GraphT::NodeType *
107 Eval(DominatorTreeBase<typename GraphT::NodeType> &DT,
108 typename GraphT::NodeType *VIn, unsigned LastLinked) {
109 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &VInInfo =
111 if (VInInfo.DFSNum < LastLinked)
114 SmallVector<typename GraphT::NodeType*, 32> Work;
115 SmallPtrSet<typename GraphT::NodeType*, 32> Visited;
117 if (VInInfo.Parent >= LastLinked)
120 while (!Work.empty()) {
121 typename GraphT::NodeType* V = Work.back();
122 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &VInfo =
124 typename GraphT::NodeType* VAncestor = DT.Vertex[VInfo.Parent];
126 // Process Ancestor first
127 if (Visited.insert(VAncestor).second && VInfo.Parent >= LastLinked) {
128 Work.push_back(VAncestor);
133 // Update VInfo based on Ancestor info
134 if (VInfo.Parent < LastLinked)
137 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &VAInfo =
139 typename GraphT::NodeType* VAncestorLabel = VAInfo.Label;
140 typename GraphT::NodeType* VLabel = VInfo.Label;
141 if (DT.Info[VAncestorLabel].Semi < DT.Info[VLabel].Semi)
142 VInfo.Label = VAncestorLabel;
143 VInfo.Parent = VAInfo.Parent;
146 return VInInfo.Label;
149 template<class FuncT, class NodeT>
150 void Calculate(DominatorTreeBase<typename GraphTraits<NodeT>::NodeType>& DT,
152 typedef GraphTraits<NodeT> GraphT;
155 bool MultipleRoots = (DT.Roots.size() > 1);
157 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &BBInfo =
159 BBInfo.DFSNum = BBInfo.Semi = ++N;
160 BBInfo.Label = nullptr;
162 DT.Vertex.push_back(nullptr); // Vertex[n] = V;
165 // Step #1: Number blocks in depth-first order and initialize variables used
166 // in later stages of the algorithm.
167 for (unsigned i = 0, e = static_cast<unsigned>(DT.Roots.size());
169 N = DFSPass<GraphT>(DT, DT.Roots[i], N);
171 // it might be that some blocks did not get a DFS number (e.g., blocks of
172 // infinite loops). In these cases an artificial exit node is required.
173 MultipleRoots |= (DT.isPostDominator() && N != GraphTraits<FuncT*>::size(&F));
175 // When naively implemented, the Lengauer-Tarjan algorithm requires a separate
176 // bucket for each vertex. However, this is unnecessary, because each vertex
177 // is only placed into a single bucket (that of its semidominator), and each
178 // vertex's bucket is processed before it is added to any bucket itself.
180 // Instead of using a bucket per vertex, we use a single array Buckets that
181 // has two purposes. Before the vertex V with preorder number i is processed,
182 // Buckets[i] stores the index of the first element in V's bucket. After V's
183 // bucket is processed, Buckets[i] stores the index of the next element in the
184 // bucket containing V, if any.
185 SmallVector<unsigned, 32> Buckets;
186 Buckets.resize(N + 1);
187 for (unsigned i = 1; i <= N; ++i)
190 for (unsigned i = N; i >= 2; --i) {
191 typename GraphT::NodeType* W = DT.Vertex[i];
192 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &WInfo =
195 // Step #2: Implicitly define the immediate dominator of vertices
196 for (unsigned j = i; Buckets[j] != i; j = Buckets[j]) {
197 typename GraphT::NodeType* V = DT.Vertex[Buckets[j]];
198 typename GraphT::NodeType* U = Eval<GraphT>(DT, V, i + 1);
199 DT.IDoms[V] = DT.Info[U].Semi < i ? U : W;
202 // Step #3: Calculate the semidominators of all vertices
204 // initialize the semi dominator to point to the parent node
205 WInfo.Semi = WInfo.Parent;
206 typedef GraphTraits<Inverse<NodeT> > InvTraits;
207 for (typename InvTraits::ChildIteratorType CI =
208 InvTraits::child_begin(W),
209 E = InvTraits::child_end(W); CI != E; ++CI) {
210 typename InvTraits::NodeType *N = *CI;
211 if (DT.Info.count(N)) { // Only if this predecessor is reachable!
212 unsigned SemiU = DT.Info[Eval<GraphT>(DT, N, i + 1)].Semi;
213 if (SemiU < WInfo.Semi)
218 // If V is a non-root vertex and sdom(V) = parent(V), then idom(V) is
219 // necessarily parent(V). In this case, set idom(V) here and avoid placing
221 if (WInfo.Semi == WInfo.Parent) {
222 DT.IDoms[W] = DT.Vertex[WInfo.Parent];
224 Buckets[i] = Buckets[WInfo.Semi];
225 Buckets[WInfo.Semi] = i;
230 typename GraphT::NodeType* Root = DT.Vertex[1];
231 for (unsigned j = 1; Buckets[j] != 1; j = Buckets[j]) {
232 typename GraphT::NodeType* V = DT.Vertex[Buckets[j]];
237 // Step #4: Explicitly define the immediate dominator of each vertex
238 for (unsigned i = 2; i <= N; ++i) {
239 typename GraphT::NodeType* W = DT.Vertex[i];
240 typename GraphT::NodeType*& WIDom = DT.IDoms[W];
241 if (WIDom != DT.Vertex[DT.Info[W].Semi])
242 WIDom = DT.IDoms[WIDom];
245 if (DT.Roots.empty()) return;
247 // Add a node for the root. This node might be the actual root, if there is
248 // one exit block, or it may be the virtual exit (denoted by (BasicBlock *)0)
249 // which postdominates all real exits if there are multiple exit blocks, or
251 typename GraphT::NodeType* Root = !MultipleRoots ? DT.Roots[0] : nullptr;
254 (DT.DomTreeNodes[Root] =
255 llvm::make_unique<DomTreeNodeBase<typename GraphT::NodeType>>(
256 Root, nullptr)).get();
258 // Loop over all of the reachable blocks in the function...
259 for (unsigned i = 2; i <= N; ++i) {
260 typename GraphT::NodeType* W = DT.Vertex[i];
262 // Don't replace this with 'count', the insertion side effect is important
263 if (DT.DomTreeNodes[W])
264 continue; // Haven't calculated this node yet?
266 typename GraphT::NodeType* ImmDom = DT.getIDom(W);
268 assert(ImmDom || DT.DomTreeNodes[nullptr]);
270 // Get or calculate the node for the immediate dominator
271 DomTreeNodeBase<typename GraphT::NodeType> *IDomNode =
272 DT.getNodeForBlock(ImmDom);
274 // Add a new tree node for this BasicBlock, and link it as a child of
276 DT.DomTreeNodes[W] = IDomNode->addChild(
277 llvm::make_unique<DomTreeNodeBase<typename GraphT::NodeType>>(
281 // Free temporary memory used to construct idom's
285 DT.Vertex.shrink_to_fit();
287 DT.updateDFSNumbers();