llvm.org GIT mirror llvm / 741f2b2
[Dominators] Move number to node mapping out of DominatorTreeBase Summary: Number to node mapping in DominatorTreeBase is used only during calculation, so there is no point keeping is as a member variable. This patch moves this mapping to Calculate function and passes it to helper functions. It also makes the name more descriptive. Reviewers: sanjoy, dberlin, davide, chandlerc Reviewed By: dberlin Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D34295 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@306562 91177308-0d34-0410-b5e6-96231b3b80d8 Jakub Kuderski 3 years ago
2 changed file(s) with 38 addition(s) and 36 deletion(s). Raw diff Collapse all Expand all
228228 void wipe() {
229229 DomTreeNodes.clear();
230230 IDoms.clear();
231 Vertex.clear();
232231 Info.clear();
233232 RootNode = nullptr;
234233 }
253252
254253 DenseMap IDoms;
255254
256 // Vertex - Map the DFS number to the NodeT*
257 std::vector Vertex;
258
259255 // Info - Collection of information used during the computation of idoms.
260256 DenseMap Info;
261257
263259 DomTreeNodes.clear();
264260 IDoms.clear();
265261 this->Roots.clear();
266 Vertex.clear();
267262 RootNode = nullptr;
268263 DFSInfoValid = false;
269264 SlowQueries = 0;
337332 DomTreeNodes(std::move(Arg.DomTreeNodes)),
338333 RootNode(std::move(Arg.RootNode)),
339334 DFSInfoValid(std::move(Arg.DFSInfoValid)),
340 SlowQueries(std::move(Arg.SlowQueries)), IDoms(std::move(Arg.IDoms)),
341 Vertex(std::move(Arg.Vertex)), Info(std::move(Arg.Info)) {
335 SlowQueries(std::move(Arg.SlowQueries)),
336 IDoms(std::move(Arg.IDoms)),
337 Info(std::move(Arg.Info)) {
342338 Arg.wipe();
343339 }
344340
350346 DFSInfoValid = std::move(RHS.DFSInfoValid);
351347 SlowQueries = std::move(RHS.SlowQueries);
352348 IDoms = std::move(RHS.IDoms);
353 Vertex = std::move(RHS.Vertex);
354349 Info = std::move(RHS.Info);
355350 RHS.wipe();
356351 return *this;
674669
675670 protected:
676671 template
677 friend typename GraphT::NodeRef
678 Eval(DominatorTreeBaseByGraphTraits &DT, typename GraphT::NodeRef V,
679 unsigned LastLinked);
672 friend typename GraphT::NodeRef Eval(
673 DominatorTreeBaseByGraphTraits &DT, typename GraphT::NodeRef V,
674 const std::vector &NumToNode,
675 unsigned LastLinked);
680676
681677 template
682 friend unsigned ReverseDFSPass(DominatorTreeBaseByGraphTraits &DT,
683 typename GraphT::NodeRef V, unsigned N);
678 friend unsigned ReverseDFSPass(
679 DominatorTreeBaseByGraphTraits &DT, typename GraphT::NodeRef V,
680 std::vector &NumToNode, unsigned N);
684681
685682 template
686683 friend unsigned DFSPass(DominatorTreeBaseByGraphTraits &DT,
687 typename GraphT::NodeRef V, unsigned N);
684 typename GraphT::NodeRef V,
685 std::vector &NumToNode,
686 unsigned N);
688687
689688 template
690689 friend void Calculate(DominatorTreeBaseByGraphTraits> &DT,
691 FuncT &F);
690 FuncT &F);
692691
693692 DomTreeNodeBase *getNodeForBlock(NodeT *BB) {
694693 if (DomTreeNodeBase *Node = getNode(BB))
766765 template void recalculate(FT &F) {
767766 using TraitsTy = GraphTraits;
768767 reset();
769 Vertex.push_back(nullptr);
770768
771769 if (!this->IsPostDominators) {
772770 // Initialize root
5050
5151 template
5252 unsigned ReverseDFSPass(DominatorTreeBaseByGraphTraits &DT,
53 typename GraphT::NodeRef V, unsigned N) {
53 typename GraphT::NodeRef V,
54 std::vector &NumToNode,
55 unsigned N) {
5456 df_iterator_dom_storage<
5557 typename GraphT::NodeRef,
5658 typename DominatorTreeBaseByGraphTraits::InfoRec>
6668 // and is 1 based, so we subtract to account for both of these.
6769 if (I.getPathLength() > 1)
6870 BBInfo.Parent = DT.Info[I.getPath(I.getPathLength() - 2)].DFSNum;
69 DT.Vertex.push_back(BB); // Vertex[n] = V;
71 NumToNode.push_back(BB); // NumToNode[n] = V;
7072
7173 if (IsChildOfArtificialExit)
7274 BBInfo.Parent = 1;
7779 }
7880 template
7981 unsigned DFSPass(DominatorTreeBaseByGraphTraits &DT,
80 typename GraphT::NodeRef V, unsigned N) {
82 typename GraphT::NodeRef V,
83 std::vector &NumToNode, unsigned N) {
8184 df_iterator_dom_storage<
8285 typename GraphT::NodeRef,
8386 typename DominatorTreeBaseByGraphTraits::InfoRec>
9295 // and is 1 based, so we subtract to account for both of these.
9396 if (I.getPathLength() > 1)
9497 BBInfo.Parent = DT.Info[I.getPath(I.getPathLength() - 2)].DFSNum;
95 DT.Vertex.push_back(BB); // Vertex[n] = V;
98 NumToNode.push_back(BB); // NumToNode[n] = V;
9699 }
97100 return N;
98101 }
99102
100103 template
101 typename GraphT::NodeRef Eval(DominatorTreeBaseByGraphTraits &DT,
102 typename GraphT::NodeRef VIn,
103 unsigned LastLinked) {
104 typename GraphT::NodeRef Eval(
105 DominatorTreeBaseByGraphTraits &DT, typename GraphT::NodeRef VIn,
106 const std::vector &NumToNode,
107 unsigned LastLinked) {
104108 using NodePtr = typename GraphT::NodeRef;
105109
106110 auto &VInInfo = DT.Info[VIn];
116120 while (!Work.empty()) {
117121 NodePtr V = Work.back();
118122 auto &VInfo = DT.Info[V];
119 NodePtr VAncestor = DT.Vertex[VInfo.Parent];
123 NodePtr VAncestor = NumToNode[VInfo.Parent];
120124
121125 // Process Ancestor first
122126 if (Visited.insert(VAncestor).second && VInfo.Parent >= LastLinked) {
150154 using NodeType = typename std::remove_pointer::type;
151155
152156 unsigned N = 0;
157 std::vector NumToNode = {nullptr};
158
153159 bool MultipleRoots = (DT.Roots.size() > 1);
154160 if (MultipleRoots) {
155161 auto &BBInfo = DT.Info[nullptr];
156162 BBInfo.DFSNum = BBInfo.Semi = ++N;
157163 BBInfo.Label = nullptr;
158164
159 DT.Vertex.push_back(nullptr); // Vertex[n] = V;
165 NumToNode.push_back(nullptr); // NumToNode[n] = V;
160166 }
161167
162168 // Step #1: Number blocks in depth-first order and initialize variables used
164170 if (DT.isPostDominator()){
165171 for (unsigned i = 0, e = static_cast(DT.Roots.size());
166172 i != e; ++i)
167 N = ReverseDFSPass(DT, DT.Roots[i], N);
173 N = ReverseDFSPass(DT, DT.Roots[i], NumToNode, N);
168174 } else {
169 N = DFSPass(DT, DT.Roots[0], N);
175 N = DFSPass(DT, DT.Roots[0], NumToNode, N);
170176 }
171177
172178 // It might be that some blocks did not get a DFS number (e.g., blocks of
175181
176182 // Initialize IDoms to spanning tree parents.
177183 for (unsigned i = 1; i <= N; ++i) {
178 const NodePtr V = DT.Vertex[i];
179 DT.IDoms[V] = DT.Vertex[DT.Info[V].Parent];
184 const NodePtr V = NumToNode[i];
185 DT.IDoms[V] = NumToNode[DT.Info[V].Parent];
180186 }
181187
182188 // Step #2: Calculate the semidominators of all vertices.
183189 for (unsigned i = N; i >= 2; --i) {
184 NodePtr W = DT.Vertex[i];
190 NodePtr W = NumToNode[i];
185191 auto &WInfo = DT.Info[W];
186192
187193 // Initialize the semi dominator to point to the parent node.
188194 WInfo.Semi = WInfo.Parent;
189195 for (const auto &N : inverse_children(W))
190196 if (DT.Info.count(N)) { // Only if this predecessor is reachable!
191 unsigned SemiU = DT.Info[Eval(DT, N, i + 1)].Semi;
197 unsigned SemiU = DT.Info[Eval(DT, N, NumToNode, i + 1)].Semi;
192198 if (SemiU < WInfo.Semi)
193199 WInfo.Semi = SemiU;
194200 }
200206 // Note that the parents were stored in IDoms and later got invalidated during
201207 // path compression in Eval.
202208 for (unsigned i = 2; i <= N; ++i) {
203 const NodePtr W = DT.Vertex[i];
209 const NodePtr W = NumToNode[i];
204210 const auto &WInfo = DT.Info[W];
205 const unsigned SDomNum = DT.Info[DT.Vertex[WInfo.Semi]].DFSNum;
211 const unsigned SDomNum = DT.Info[NumToNode[WInfo.Semi]].DFSNum;
206212 NodePtr WIDomCandidate = DT.IDoms[W];
207213 while (DT.Info[WIDomCandidate].DFSNum > SDomNum)
208214 WIDomCandidate = DT.IDoms[WIDomCandidate];
225231
226232 // Loop over all of the reachable blocks in the function...
227233 for (unsigned i = 2; i <= N; ++i) {
228 NodePtr W = DT.Vertex[i];
234 NodePtr W = NumToNode[i];
229235
230236 // Don't replace this with 'count', the insertion side effect is important
231237 if (DT.DomTreeNodes[W])
247253 // Free temporary memory used to construct idom's
248254 DT.IDoms.clear();
249255 DT.Info.clear();
250 DT.Vertex.clear();
251 DT.Vertex.shrink_to_fit();
252256
253257 DT.updateDFSNumbers();
254258 }