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/DepthFirstIterator.h"
28 #include "llvm/ADT/SmallPtrSet.h"
29 #include "llvm/Support/GenericDomTree.h"
33 // External storage for depth first iterator that reuses the info lookup map
34 // domtree already has. We don't have a set, but a map instead, so we are
35 // converting the one argument insert calls.
36 template <class NodeRef, class InfoType> struct df_iterator_dom_storage {
38 typedef DenseMap<NodeRef, InfoType> BaseSet;
39 df_iterator_dom_storage(BaseSet &Storage) : Storage(Storage) {}
41 typedef typename BaseSet::iterator iterator;
42 std::pair<iterator, bool> insert(NodeRef N) {
43 return Storage.insert({N, InfoType()});
45 void completed(NodeRef) {}
51 template <class GraphT>
52 unsigned ReverseDFSPass(DominatorTreeBaseByGraphTraits<GraphT> &DT,
53 typename GraphT::NodeRef V, unsigned N) {
54 df_iterator_dom_storage<
55 typename GraphT::NodeRef,
56 typename DominatorTreeBaseByGraphTraits<GraphT>::InfoRec>
58 bool IsChildOfArtificialExit = (N != 0);
59 for (auto I = idf_ext_begin(V, DFStorage), E = idf_ext_end(V, DFStorage);
61 typename GraphT::NodeRef BB = *I;
62 auto &BBInfo = DT.Info[BB];
63 BBInfo.DFSNum = BBInfo.Semi = ++N;
65 // Set the parent to the top of the visited stack. The stack includes us,
66 // and is 1 based, so we subtract to account for both of these.
67 if (I.getPathLength() > 1)
68 BBInfo.Parent = DT.Info[I.getPath(I.getPathLength() - 2)].DFSNum;
69 DT.Vertex.push_back(BB); // Vertex[n] = V;
71 if (IsChildOfArtificialExit)
74 IsChildOfArtificialExit = false;
78 template <class GraphT>
79 unsigned DFSPass(DominatorTreeBaseByGraphTraits<GraphT> &DT,
80 typename GraphT::NodeRef V, unsigned N) {
81 df_iterator_dom_storage<
82 typename GraphT::NodeRef,
83 typename DominatorTreeBaseByGraphTraits<GraphT>::InfoRec>
85 for (auto I = df_ext_begin(V, DFStorage), E = df_ext_end(V, DFStorage);
87 typename GraphT::NodeRef BB = *I;
88 auto &BBInfo = DT.Info[BB];
89 BBInfo.DFSNum = BBInfo.Semi = ++N;
91 // Set the parent to the top of the visited stack. The stack includes us,
92 // and is 1 based, so we subtract to account for both of these.
93 if (I.getPathLength() > 1)
94 BBInfo.Parent = DT.Info[I.getPath(I.getPathLength() - 2)].DFSNum;
95 DT.Vertex.push_back(BB); // Vertex[n] = V;
100 template <class GraphT>
101 typename GraphT::NodeRef Eval(DominatorTreeBaseByGraphTraits<GraphT> &DT,
102 typename GraphT::NodeRef VIn,
103 unsigned LastLinked) {
104 auto &VInInfo = DT.Info[VIn];
105 if (VInInfo.DFSNum < LastLinked)
108 SmallVector<typename GraphT::NodeRef, 32> Work;
109 SmallPtrSet<typename GraphT::NodeRef, 32> Visited;
111 if (VInInfo.Parent >= LastLinked)
114 while (!Work.empty()) {
115 typename GraphT::NodeRef V = Work.back();
116 auto &VInfo = DT.Info[V];
117 typename GraphT::NodeRef VAncestor = DT.Vertex[VInfo.Parent];
119 // Process Ancestor first
120 if (Visited.insert(VAncestor).second && VInfo.Parent >= LastLinked) {
121 Work.push_back(VAncestor);
126 // Update VInfo based on Ancestor info
127 if (VInfo.Parent < LastLinked)
130 auto &VAInfo = DT.Info[VAncestor];
131 typename GraphT::NodeRef VAncestorLabel = VAInfo.Label;
132 typename GraphT::NodeRef VLabel = VInfo.Label;
133 if (DT.Info[VAncestorLabel].Semi < DT.Info[VLabel].Semi)
134 VInfo.Label = VAncestorLabel;
135 VInfo.Parent = VAInfo.Parent;
138 return VInInfo.Label;
141 template <class FuncT, class NodeT>
142 void Calculate(DominatorTreeBaseByGraphTraits<GraphTraits<NodeT>> &DT,
144 typedef GraphTraits<NodeT> GraphT;
145 static_assert(std::is_pointer<typename GraphT::NodeRef>::value,
146 "NodeRef should be pointer type");
147 typedef typename std::remove_pointer<typename GraphT::NodeRef>::type NodeType;
150 bool MultipleRoots = (DT.Roots.size() > 1);
152 auto &BBInfo = DT.Info[nullptr];
153 BBInfo.DFSNum = BBInfo.Semi = ++N;
154 BBInfo.Label = nullptr;
156 DT.Vertex.push_back(nullptr); // Vertex[n] = V;
159 // Step #1: Number blocks in depth-first order and initialize variables used
160 // in later stages of the algorithm.
161 if (DT.isPostDominator()){
162 for (unsigned i = 0, e = static_cast<unsigned>(DT.Roots.size());
164 N = ReverseDFSPass<GraphT>(DT, DT.Roots[i], N);
166 N = DFSPass<GraphT>(DT, DT.Roots[0], N);
169 // it might be that some blocks did not get a DFS number (e.g., blocks of
170 // infinite loops). In these cases an artificial exit node is required.
171 MultipleRoots |= (DT.isPostDominator() && N != GraphTraits<FuncT*>::size(&F));
173 // When naively implemented, the Lengauer-Tarjan algorithm requires a separate
174 // bucket for each vertex. However, this is unnecessary, because each vertex
175 // is only placed into a single bucket (that of its semidominator), and each
176 // vertex's bucket is processed before it is added to any bucket itself.
178 // Instead of using a bucket per vertex, we use a single array Buckets that
179 // has two purposes. Before the vertex V with preorder number i is processed,
180 // Buckets[i] stores the index of the first element in V's bucket. After V's
181 // bucket is processed, Buckets[i] stores the index of the next element in the
182 // bucket containing V, if any.
183 SmallVector<unsigned, 32> Buckets;
184 Buckets.resize(N + 1);
185 for (unsigned i = 1; i <= N; ++i)
188 for (unsigned i = N; i >= 2; --i) {
189 typename GraphT::NodeRef W = DT.Vertex[i];
190 auto &WInfo = DT.Info[W];
192 // Step #2: Implicitly define the immediate dominator of vertices
193 for (unsigned j = i; Buckets[j] != i; j = Buckets[j]) {
194 typename GraphT::NodeRef V = DT.Vertex[Buckets[j]];
195 typename GraphT::NodeRef U = Eval<GraphT>(DT, V, i + 1);
196 DT.IDoms[V] = DT.Info[U].Semi < i ? U : W;
199 // Step #3: Calculate the semidominators of all vertices
201 // initialize the semi dominator to point to the parent node
202 WInfo.Semi = WInfo.Parent;
203 for (const auto &N : inverse_children<NodeT>(W))
204 if (DT.Info.count(N)) { // Only if this predecessor is reachable!
205 unsigned SemiU = DT.Info[Eval<GraphT>(DT, N, i + 1)].Semi;
206 if (SemiU < WInfo.Semi)
210 // If V is a non-root vertex and sdom(V) = parent(V), then idom(V) is
211 // necessarily parent(V). In this case, set idom(V) here and avoid placing
213 if (WInfo.Semi == WInfo.Parent) {
214 DT.IDoms[W] = DT.Vertex[WInfo.Parent];
216 Buckets[i] = Buckets[WInfo.Semi];
217 Buckets[WInfo.Semi] = i;
222 typename GraphT::NodeRef Root = DT.Vertex[1];
223 for (unsigned j = 1; Buckets[j] != 1; j = Buckets[j]) {
224 typename GraphT::NodeRef V = DT.Vertex[Buckets[j]];
229 // Step #4: Explicitly define the immediate dominator of each vertex
230 for (unsigned i = 2; i <= N; ++i) {
231 typename GraphT::NodeRef W = DT.Vertex[i];
232 typename GraphT::NodeRef &WIDom = DT.IDoms[W];
233 if (WIDom != DT.Vertex[DT.Info[W].Semi])
234 WIDom = DT.IDoms[WIDom];
237 if (DT.Roots.empty()) return;
239 // Add a node for the root. This node might be the actual root, if there is
240 // one exit block, or it may be the virtual exit (denoted by (BasicBlock *)0)
241 // which postdominates all real exits if there are multiple exit blocks, or
243 typename GraphT::NodeRef Root = !MultipleRoots ? DT.Roots[0] : nullptr;
246 (DT.DomTreeNodes[Root] =
247 llvm::make_unique<DomTreeNodeBase<NodeType>>(Root, nullptr))
250 // Loop over all of the reachable blocks in the function...
251 for (unsigned i = 2; i <= N; ++i) {
252 typename GraphT::NodeRef W = DT.Vertex[i];
254 // Don't replace this with 'count', the insertion side effect is important
255 if (DT.DomTreeNodes[W])
256 continue; // Haven't calculated this node yet?
258 typename GraphT::NodeRef ImmDom = DT.getIDom(W);
260 assert(ImmDom || DT.DomTreeNodes[nullptr]);
262 // Get or calculate the node for the immediate dominator
263 DomTreeNodeBase<NodeType> *IDomNode = DT.getNodeForBlock(ImmDom);
265 // Add a new tree node for this BasicBlock, and link it as a child of
267 DT.DomTreeNodes[W] = IDomNode->addChild(
268 llvm::make_unique<DomTreeNodeBase<NodeType>>(W, IDomNode));
271 // Free temporary memory used to construct idom's
275 DT.Vertex.shrink_to_fit();
277 DT.updateDFSNumbers();