LLVM  9.0.0svn
GenericDomTreeConstruction.h
Go to the documentation of this file.
1 //===- GenericDomTreeConstruction.h - Dominator Calculation ------*- C++ -*-==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 /// \file
9 ///
10 /// Generic dominator tree construction - This file provides routines to
11 /// construct immediate dominator information for a flow-graph based on the
12 /// Semi-NCA algorithm described in this dissertation:
13 ///
14 /// Linear-Time Algorithms for Dominators and Related Problems
15 /// Loukas Georgiadis, Princeton University, November 2005, pp. 21-23:
16 /// ftp://ftp.cs.princeton.edu/reports/2005/737.pdf
17 ///
18 /// Semi-NCA algorithm runs in O(n^2) worst-case time but usually slightly
19 /// faster than Simple Lengauer-Tarjan in practice.
20 ///
21 /// O(n^2) worst cases happen when the computation of nearest common ancestors
22 /// requires O(n) average time, which is very unlikely in real world. If this
23 /// ever turns out to be an issue, consider implementing a hybrid algorithm.
24 ///
25 /// The file uses the Depth Based Search algorithm to perform incremental
26 /// updates (insertion and deletions). The implemented algorithm is based on
27 /// this publication:
28 ///
29 /// An Experimental Study of Dynamic Dominators
30 /// Loukas Georgiadis, et al., April 12 2016, pp. 5-7, 9-10:
31 /// https://arxiv.org/pdf/1604.02711.pdf
32 ///
33 //===----------------------------------------------------------------------===//
34 
35 #ifndef LLVM_SUPPORT_GENERICDOMTREECONSTRUCTION_H
36 #define LLVM_SUPPORT_GENERICDOMTREECONSTRUCTION_H
37 
38 #include <queue>
39 #include "llvm/ADT/ArrayRef.h"
40 #include "llvm/ADT/DenseSet.h"
43 #include "llvm/ADT/SmallPtrSet.h"
44 #include "llvm/Support/Debug.h"
46 
47 #define DEBUG_TYPE "dom-tree-builder"
48 
49 namespace llvm {
50 namespace DomTreeBuilder {
51 
52 template <typename DomTreeT>
53 struct SemiNCAInfo {
54  using NodePtr = typename DomTreeT::NodePtr;
55  using NodeT = typename DomTreeT::NodeType;
57  using RootsT = decltype(DomTreeT::Roots);
58  static constexpr bool IsPostDom = DomTreeT::IsPostDominator;
59 
60  // Information record used by Semi-NCA during tree construction.
61  struct InfoRec {
62  unsigned DFSNum = 0;
63  unsigned Parent = 0;
64  unsigned Semi = 0;
65  NodePtr Label = nullptr;
66  NodePtr IDom = nullptr;
68  };
69 
70  // Number to node mapping is 1-based. Initialize the mapping to start with
71  // a dummy element.
72  std::vector<NodePtr> NumToNode = {nullptr};
74 
75  using UpdateT = typename DomTreeT::UpdateType;
76  using UpdateKind = typename DomTreeT::UpdateKind;
77  struct BatchUpdateInfo {
80 
81  // In order to be able to walk a CFG that is out of sync with the CFG
82  // DominatorTree last knew about, use the list of updates to reconstruct
83  // previous CFG versions of the current CFG. For each node, we store a set
84  // of its virtually added/deleted future successors and predecessors.
85  // Note that these children are from the future relative to what the
86  // DominatorTree knows about -- using them to gets us some snapshot of the
87  // CFG from the past (relative to the state of the CFG).
90  // Remembers if the whole tree was recalculated at some point during the
91  // current batch update.
92  bool IsRecalculated = false;
93  };
94 
97 
98  // If BUI is a nullptr, then there's no batch update in progress.
99  SemiNCAInfo(BatchUpdatePtr BUI) : BatchUpdates(BUI) {}
100 
101  void clear() {
102  NumToNode = {nullptr}; // Restore to initial state with a dummy start node.
103  NodeToInfo.clear();
104  // Don't reset the pointer to BatchUpdateInfo here -- if there's an update
105  // in progress, we need this information to continue it.
106  }
107 
108  template <bool Inverse>
109  struct ChildrenGetter {
111 
112  static ResultTy Get(NodePtr N, std::integral_constant<bool, false>) {
113  auto RChildren = reverse(children<NodePtr>(N));
114  return ResultTy(RChildren.begin(), RChildren.end());
115  }
116 
117  static ResultTy Get(NodePtr N, std::integral_constant<bool, true>) {
118  auto IChildren = inverse_children<NodePtr>(N);
119  return ResultTy(IChildren.begin(), IChildren.end());
120  }
121 
122  using Tag = std::integral_constant<bool, Inverse>;
123 
124  // The function below is the core part of the batch updater. It allows the
125  // Depth Based Search algorithm to perform incremental updates in lockstep
126  // with updates to the CFG. We emulated lockstep CFG updates by getting its
127  // next snapshots by reverse-applying future updates.
129  ResultTy Res = Get(N, Tag());
130  // If there's no batch update in progress, simply return node's children.
131  if (!BUI) return Res;
132 
133  // CFG children are actually its *most current* children, and we have to
134  // reverse-apply the future updates to get the node's children at the
135  // point in time the update was performed.
136  auto &FutureChildren = (Inverse != IsPostDom) ? BUI->FuturePredecessors
137  : BUI->FutureSuccessors;
138  auto FCIt = FutureChildren.find(N);
139  if (FCIt == FutureChildren.end()) return Res;
140 
141  for (auto ChildAndKind : FCIt->second) {
142  const NodePtr Child = ChildAndKind.getPointer();
143  const UpdateKind UK = ChildAndKind.getInt();
144 
145  // Reverse-apply the future update.
146  if (UK == UpdateKind::Insert) {
147  // If there's an insertion in the future, it means that the edge must
148  // exist in the current CFG, but was not present in it before.
149  assert(llvm::find(Res, Child) != Res.end()
150  && "Expected child not found in the CFG");
151  Res.erase(std::remove(Res.begin(), Res.end(), Child), Res.end());
152  LLVM_DEBUG(dbgs() << "\tHiding edge " << BlockNamePrinter(N) << " -> "
153  << BlockNamePrinter(Child) << "\n");
154  } else {
155  // If there's an deletion in the future, it means that the edge cannot
156  // exist in the current CFG, but existed in it before.
157  assert(llvm::find(Res, Child) == Res.end() &&
158  "Unexpected child found in the CFG");
159  LLVM_DEBUG(dbgs() << "\tShowing virtual edge " << BlockNamePrinter(N)
160  << " -> " << BlockNamePrinter(Child) << "\n");
161  Res.push_back(Child);
162  }
163  }
164 
165  return Res;
166  }
167  };
168 
169  NodePtr getIDom(NodePtr BB) const {
170  auto InfoIt = NodeToInfo.find(BB);
171  if (InfoIt == NodeToInfo.end()) return nullptr;
172 
173  return InfoIt->second.IDom;
174  }
175 
176  TreeNodePtr getNodeForBlock(NodePtr BB, DomTreeT &DT) {
177  if (TreeNodePtr Node = DT.getNode(BB)) return Node;
178 
179  // Haven't calculated this node yet? Get or calculate the node for the
180  // immediate dominator.
181  NodePtr IDom = getIDom(BB);
182 
183  assert(IDom || DT.DomTreeNodes[nullptr]);
184  TreeNodePtr IDomNode = getNodeForBlock(IDom, DT);
185 
186  // Add a new tree node for this NodeT, and link it as a child of
187  // IDomNode
188  return (DT.DomTreeNodes[BB] = IDomNode->addChild(
190  .get();
191  }
192 
193  static bool AlwaysDescend(NodePtr, NodePtr) { return true; }
194 
197 
198  BlockNamePrinter(NodePtr Block) : N(Block) {}
199  BlockNamePrinter(TreeNodePtr TN) : N(TN ? TN->getBlock() : nullptr) {}
200 
202  if (!BP.N)
203  O << "nullptr";
204  else
205  BP.N->printAsOperand(O, false);
206 
207  return O;
208  }
209  };
210 
211  // Custom DFS implementation which can skip nodes based on a provided
212  // predicate. It also collects ReverseChildren so that we don't have to spend
213  // time getting predecessors in SemiNCA.
214  //
215  // If IsReverse is set to true, the DFS walk will be performed backwards
216  // relative to IsPostDom -- using reverse edges for dominators and forward
217  // edges for postdominators.
218  template <bool IsReverse = false, typename DescendCondition>
219  unsigned runDFS(NodePtr V, unsigned LastNum, DescendCondition Condition,
220  unsigned AttachToNum) {
221  assert(V);
222  SmallVector<NodePtr, 64> WorkList = {V};
223  if (NodeToInfo.count(V) != 0) NodeToInfo[V].Parent = AttachToNum;
224 
225  while (!WorkList.empty()) {
226  const NodePtr BB = WorkList.pop_back_val();
227  auto &BBInfo = NodeToInfo[BB];
228 
229  // Visited nodes always have positive DFS numbers.
230  if (BBInfo.DFSNum != 0) continue;
231  BBInfo.DFSNum = BBInfo.Semi = ++LastNum;
232  BBInfo.Label = BB;
233  NumToNode.push_back(BB);
234 
235  constexpr bool Direction = IsReverse != IsPostDom; // XOR.
236  for (const NodePtr Succ :
237  ChildrenGetter<Direction>::Get(BB, BatchUpdates)) {
238  const auto SIT = NodeToInfo.find(Succ);
239  // Don't visit nodes more than once but remember to collect
240  // ReverseChildren.
241  if (SIT != NodeToInfo.end() && SIT->second.DFSNum != 0) {
242  if (Succ != BB) SIT->second.ReverseChildren.push_back(BB);
243  continue;
244  }
245 
246  if (!Condition(BB, Succ)) continue;
247 
248  // It's fine to add Succ to the map, because we know that it will be
249  // visited later.
250  auto &SuccInfo = NodeToInfo[Succ];
251  WorkList.push_back(Succ);
252  SuccInfo.Parent = LastNum;
253  SuccInfo.ReverseChildren.push_back(BB);
254  }
255  }
256 
257  return LastNum;
258  }
259 
260  // V is a predecessor of W. eval() returns V if V < W, otherwise the minimum
261  // of sdom(U), where U > W and there is a virtual forest path from U to V. The
262  // virtual forest consists of linked edges of processed vertices.
263  //
264  // We can follow Parent pointers (virtual forest edges) to determine the
265  // ancestor U with minimum sdom(U). But it is slow and thus we employ the path
266  // compression technique to speed up to O(m*log(n)). Theoretically the virtual
267  // forest can be organized as balanced trees to achieve almost linear
268  // O(m*alpha(m,n)) running time. But it requires two auxiliary arrays (Size
269  // and Child) and is unlikely to be faster than the simple implementation.
270  //
271  // For each vertex V, its Label points to the vertex with the minimal sdom(U)
272  // (Semi) in its path from V (included) to NodeToInfo[V].Parent (excluded).
273  NodePtr eval(NodePtr V, unsigned LastLinked,
275  InfoRec *VInfo = &NodeToInfo[V];
276  if (VInfo->Parent < LastLinked)
277  return VInfo->Label;
278 
279  // Store ancestors except the last (root of a virtual tree) into a stack.
280  assert(Stack.empty());
281  do {
282  Stack.push_back(VInfo);
283  VInfo = &NodeToInfo[NumToNode[VInfo->Parent]];
284  } while (VInfo->Parent >= LastLinked);
285 
286  // Path compression. Point each vertex's Parent to the root and update its
287  // Label if any of its ancestors (PInfo->Label) has a smaller Semi.
288  const InfoRec *PInfo = VInfo;
289  const InfoRec *PLabelInfo = &NodeToInfo[PInfo->Label];
290  do {
291  VInfo = Stack.pop_back_val();
292  VInfo->Parent = PInfo->Parent;
293  const InfoRec *VLabelInfo = &NodeToInfo[VInfo->Label];
294  if (PLabelInfo->Semi < VLabelInfo->Semi)
295  VInfo->Label = PInfo->Label;
296  else
297  PLabelInfo = VLabelInfo;
298  PInfo = VInfo;
299  } while (!Stack.empty());
300  return VInfo->Label;
301  }
302 
303  // This function requires DFS to be run before calling it.
304  void runSemiNCA(DomTreeT &DT, const unsigned MinLevel = 0) {
305  const unsigned NextDFSNum(NumToNode.size());
306  // Initialize IDoms to spanning tree parents.
307  for (unsigned i = 1; i < NextDFSNum; ++i) {
308  const NodePtr V = NumToNode[i];
309  auto &VInfo = NodeToInfo[V];
310  VInfo.IDom = NumToNode[VInfo.Parent];
311  }
312 
313  // Step #1: Calculate the semidominators of all vertices.
314  SmallVector<InfoRec *, 32> EvalStack;
315  for (unsigned i = NextDFSNum - 1; i >= 2; --i) {
316  NodePtr W = NumToNode[i];
317  auto &WInfo = NodeToInfo[W];
318 
319  // Initialize the semi dominator to point to the parent node.
320  WInfo.Semi = WInfo.Parent;
321  for (const auto &N : WInfo.ReverseChildren) {
322  if (NodeToInfo.count(N) == 0) // Skip unreachable predecessors.
323  continue;
324 
325  const TreeNodePtr TN = DT.getNode(N);
326  // Skip predecessors whose level is above the subtree we are processing.
327  if (TN && TN->getLevel() < MinLevel)
328  continue;
329 
330  unsigned SemiU = NodeToInfo[eval(N, i + 1, EvalStack)].Semi;
331  if (SemiU < WInfo.Semi) WInfo.Semi = SemiU;
332  }
333  }
334 
335  // Step #2: Explicitly define the immediate dominator of each vertex.
336  // IDom[i] = NCA(SDom[i], SpanningTreeParent(i)).
337  // Note that the parents were stored in IDoms and later got invalidated
338  // during path compression in Eval.
339  for (unsigned i = 2; i < NextDFSNum; ++i) {
340  const NodePtr W = NumToNode[i];
341  auto &WInfo = NodeToInfo[W];
342  const unsigned SDomNum = NodeToInfo[NumToNode[WInfo.Semi]].DFSNum;
343  NodePtr WIDomCandidate = WInfo.IDom;
344  while (NodeToInfo[WIDomCandidate].DFSNum > SDomNum)
345  WIDomCandidate = NodeToInfo[WIDomCandidate].IDom;
346 
347  WInfo.IDom = WIDomCandidate;
348  }
349  }
350 
351  // PostDominatorTree always has a virtual root that represents a virtual CFG
352  // node that serves as a single exit from the function. All the other exits
353  // (CFG nodes with terminators and nodes in infinite loops are logically
354  // connected to this virtual CFG exit node).
355  // This functions maps a nullptr CFG node to the virtual root tree node.
356  void addVirtualRoot() {
357  assert(IsPostDom && "Only postdominators have a virtual root");
358  assert(NumToNode.size() == 1 && "SNCAInfo must be freshly constructed");
359 
360  auto &BBInfo = NodeToInfo[nullptr];
361  BBInfo.DFSNum = BBInfo.Semi = 1;
362  BBInfo.Label = nullptr;
363 
364  NumToNode.push_back(nullptr); // NumToNode[1] = nullptr;
365  }
366 
367  // For postdominators, nodes with no forward successors are trivial roots that
368  // are always selected as tree roots. Roots with forward successors correspond
369  // to CFG nodes within infinite loops.
370  static bool HasForwardSuccessors(const NodePtr N, BatchUpdatePtr BUI) {
371  assert(N && "N must be a valid node");
372  return !ChildrenGetter<false>::Get(N, BUI).empty();
373  }
374 
375  static NodePtr GetEntryNode(const DomTreeT &DT) {
376  assert(DT.Parent && "Parent not set");
378  }
379 
380  // Finds all roots without relaying on the set of roots already stored in the
381  // tree.
382  // We define roots to be some non-redundant set of the CFG nodes
383  static RootsT FindRoots(const DomTreeT &DT, BatchUpdatePtr BUI) {
384  assert(DT.Parent && "Parent pointer is not set");
385  RootsT Roots;
386 
387  // For dominators, function entry CFG node is always a tree root node.
388  if (!IsPostDom) {
389  Roots.push_back(GetEntryNode(DT));
390  return Roots;
391  }
392 
393  SemiNCAInfo SNCA(BUI);
394 
395  // PostDominatorTree always has a virtual root.
396  SNCA.addVirtualRoot();
397  unsigned Num = 1;
398 
399  LLVM_DEBUG(dbgs() << "\t\tLooking for trivial roots\n");
400 
401  // Step #1: Find all the trivial roots that are going to will definitely
402  // remain tree roots.
403  unsigned Total = 0;
404  // It may happen that there are some new nodes in the CFG that are result of
405  // the ongoing batch update, but we cannot really pretend that they don't
406  // exist -- we won't see any outgoing or incoming edges to them, so it's
407  // fine to discover them here, as they would end up appearing in the CFG at
408  // some point anyway.
409  for (const NodePtr N : nodes(DT.Parent)) {
410  ++Total;
411  // If it has no *successors*, it is definitely a root.
412  if (!HasForwardSuccessors(N, BUI)) {
413  Roots.push_back(N);
414  // Run DFS not to walk this part of CFG later.
415  Num = SNCA.runDFS(N, Num, AlwaysDescend, 1);
416  LLVM_DEBUG(dbgs() << "Found a new trivial root: " << BlockNamePrinter(N)
417  << "\n");
418  LLVM_DEBUG(dbgs() << "Last visited node: "
419  << BlockNamePrinter(SNCA.NumToNode[Num]) << "\n");
420  }
421  }
422 
423  LLVM_DEBUG(dbgs() << "\t\tLooking for non-trivial roots\n");
424 
425  // Step #2: Find all non-trivial root candidates. Those are CFG nodes that
426  // are reverse-unreachable were not visited by previous DFS walks (i.e. CFG
427  // nodes in infinite loops).
428  bool HasNonTrivialRoots = false;
429  // Accounting for the virtual exit, see if we had any reverse-unreachable
430  // nodes.
431  if (Total + 1 != Num) {
432  HasNonTrivialRoots = true;
433  // Make another DFS pass over all other nodes to find the
434  // reverse-unreachable blocks, and find the furthest paths we'll be able
435  // to make.
436  // Note that this looks N^2, but it's really 2N worst case, if every node
437  // is unreachable. This is because we are still going to only visit each
438  // unreachable node once, we may just visit it in two directions,
439  // depending on how lucky we get.
440  SmallPtrSet<NodePtr, 4> ConnectToExitBlock;
441  for (const NodePtr I : nodes(DT.Parent)) {
442  if (SNCA.NodeToInfo.count(I) == 0) {
443  LLVM_DEBUG(dbgs()
444  << "\t\t\tVisiting node " << BlockNamePrinter(I) << "\n");
445  // Find the furthest away we can get by following successors, then
446  // follow them in reverse. This gives us some reasonable answer about
447  // the post-dom tree inside any infinite loop. In particular, it
448  // guarantees we get to the farthest away point along *some*
449  // path. This also matches the GCC's behavior.
450  // If we really wanted a totally complete picture of dominance inside
451  // this infinite loop, we could do it with SCC-like algorithms to find
452  // the lowest and highest points in the infinite loop. In theory, it
453  // would be nice to give the canonical backedge for the loop, but it's
454  // expensive and does not always lead to a minimal set of roots.
455  LLVM_DEBUG(dbgs() << "\t\t\tRunning forward DFS\n");
456 
457  const unsigned NewNum = SNCA.runDFS<true>(I, Num, AlwaysDescend, Num);
458  const NodePtr FurthestAway = SNCA.NumToNode[NewNum];
459  LLVM_DEBUG(dbgs() << "\t\t\tFound a new furthest away node "
460  << "(non-trivial root): "
461  << BlockNamePrinter(FurthestAway) << "\n");
462  ConnectToExitBlock.insert(FurthestAway);
463  Roots.push_back(FurthestAway);
464  LLVM_DEBUG(dbgs() << "\t\t\tPrev DFSNum: " << Num << ", new DFSNum: "
465  << NewNum << "\n\t\t\tRemoving DFS info\n");
466  for (unsigned i = NewNum; i > Num; --i) {
467  const NodePtr N = SNCA.NumToNode[i];
468  LLVM_DEBUG(dbgs() << "\t\t\t\tRemoving DFS info for "
469  << BlockNamePrinter(N) << "\n");
470  SNCA.NodeToInfo.erase(N);
471  SNCA.NumToNode.pop_back();
472  }
473  const unsigned PrevNum = Num;
474  LLVM_DEBUG(dbgs() << "\t\t\tRunning reverse DFS\n");
475  Num = SNCA.runDFS(FurthestAway, Num, AlwaysDescend, 1);
476  for (unsigned i = PrevNum + 1; i <= Num; ++i)
477  LLVM_DEBUG(dbgs() << "\t\t\t\tfound node "
478  << BlockNamePrinter(SNCA.NumToNode[i]) << "\n");
479  }
480  }
481  }
482 
483  LLVM_DEBUG(dbgs() << "Total: " << Total << ", Num: " << Num << "\n");
484  LLVM_DEBUG(dbgs() << "Discovered CFG nodes:\n");
485  LLVM_DEBUG(for (size_t i = 0; i <= Num; ++i) dbgs()
486  << i << ": " << BlockNamePrinter(SNCA.NumToNode[i]) << "\n");
487 
488  assert((Total + 1 == Num) && "Everything should have been visited");
489 
490  // Step #3: If we found some non-trivial roots, make them non-redundant.
491  if (HasNonTrivialRoots) RemoveRedundantRoots(DT, BUI, Roots);
492 
493  LLVM_DEBUG(dbgs() << "Found roots: ");
494  LLVM_DEBUG(for (auto *Root
495  : Roots) dbgs()
496  << BlockNamePrinter(Root) << " ");
497  LLVM_DEBUG(dbgs() << "\n");
498 
499  return Roots;
500  }
501 
502  // This function only makes sense for postdominators.
503  // We define roots to be some set of CFG nodes where (reverse) DFS walks have
504  // to start in order to visit all the CFG nodes (including the
505  // reverse-unreachable ones).
506  // When the search for non-trivial roots is done it may happen that some of
507  // the non-trivial roots are reverse-reachable from other non-trivial roots,
508  // which makes them redundant. This function removes them from the set of
509  // input roots.
510  static void RemoveRedundantRoots(const DomTreeT &DT, BatchUpdatePtr BUI,
511  RootsT &Roots) {
512  assert(IsPostDom && "This function is for postdominators only");
513  LLVM_DEBUG(dbgs() << "Removing redundant roots\n");
514 
515  SemiNCAInfo SNCA(BUI);
516 
517  for (unsigned i = 0; i < Roots.size(); ++i) {
518  auto &Root = Roots[i];
519  // Trivial roots are always non-redundant.
520  if (!HasForwardSuccessors(Root, BUI)) continue;
521  LLVM_DEBUG(dbgs() << "\tChecking if " << BlockNamePrinter(Root)
522  << " remains a root\n");
523  SNCA.clear();
524  // Do a forward walk looking for the other roots.
525  const unsigned Num = SNCA.runDFS<true>(Root, 0, AlwaysDescend, 0);
526  // Skip the start node and begin from the second one (note that DFS uses
527  // 1-based indexing).
528  for (unsigned x = 2; x <= Num; ++x) {
529  const NodePtr N = SNCA.NumToNode[x];
530  // If we wound another root in a (forward) DFS walk, remove the current
531  // root from the set of roots, as it is reverse-reachable from the other
532  // one.
533  if (llvm::find(Roots, N) != Roots.end()) {
534  LLVM_DEBUG(dbgs() << "\tForward DFS walk found another root "
535  << BlockNamePrinter(N) << "\n\tRemoving root "
536  << BlockNamePrinter(Root) << "\n");
537  std::swap(Root, Roots.back());
538  Roots.pop_back();
539 
540  // Root at the back takes the current root's place.
541  // Start the next loop iteration with the same index.
542  --i;
543  break;
544  }
545  }
546  }
547  }
548 
549  template <typename DescendCondition>
550  void doFullDFSWalk(const DomTreeT &DT, DescendCondition DC) {
551  if (!IsPostDom) {
552  assert(DT.Roots.size() == 1 && "Dominators should have a singe root");
553  runDFS(DT.Roots[0], 0, DC, 0);
554  return;
555  }
556 
557  addVirtualRoot();
558  unsigned Num = 1;
559  for (const NodePtr Root : DT.Roots) Num = runDFS(Root, Num, DC, 0);
560  }
561 
562  static void CalculateFromScratch(DomTreeT &DT, BatchUpdatePtr BUI) {
563  auto *Parent = DT.Parent;
564  DT.reset();
565  DT.Parent = Parent;
566  SemiNCAInfo SNCA(nullptr); // Since we are rebuilding the whole tree,
567  // there's no point doing it incrementally.
568 
569  // Step #0: Number blocks in depth-first order and initialize variables used
570  // in later stages of the algorithm.
571  DT.Roots = FindRoots(DT, nullptr);
572  SNCA.doFullDFSWalk(DT, AlwaysDescend);
573 
574  SNCA.runSemiNCA(DT);
575  if (BUI) {
576  BUI->IsRecalculated = true;
577  LLVM_DEBUG(
578  dbgs() << "DomTree recalculated, skipping future batch updates\n");
579  }
580 
581  if (DT.Roots.empty()) return;
582 
583  // Add a node for the root. If the tree is a PostDominatorTree it will be
584  // the virtual exit (denoted by (BasicBlock *) nullptr) which postdominates
585  // all real exits (including multiple exit blocks, infinite loops).
586  NodePtr Root = IsPostDom ? nullptr : DT.Roots[0];
587 
588  DT.RootNode = (DT.DomTreeNodes[Root] =
589  llvm::make_unique<DomTreeNodeBase<NodeT>>(Root, nullptr))
590  .get();
591  SNCA.attachNewSubtree(DT, DT.RootNode);
592  }
593 
594  void attachNewSubtree(DomTreeT& DT, const TreeNodePtr AttachTo) {
595  // Attach the first unreachable block to AttachTo.
596  NodeToInfo[NumToNode[1]].IDom = AttachTo->getBlock();
597  // Loop over all of the discovered blocks in the function...
598  for (size_t i = 1, e = NumToNode.size(); i != e; ++i) {
599  NodePtr W = NumToNode[i];
600  LLVM_DEBUG(dbgs() << "\tdiscovered a new reachable node "
601  << BlockNamePrinter(W) << "\n");
602 
603  // Don't replace this with 'count', the insertion side effect is important
604  if (DT.DomTreeNodes[W]) continue; // Haven't calculated this node yet?
605 
606  NodePtr ImmDom = getIDom(W);
607 
608  // Get or calculate the node for the immediate dominator.
609  TreeNodePtr IDomNode = getNodeForBlock(ImmDom, DT);
610 
611  // Add a new tree node for this BasicBlock, and link it as a child of
612  // IDomNode.
613  DT.DomTreeNodes[W] = IDomNode->addChild(
615  }
616  }
617 
618  void reattachExistingSubtree(DomTreeT &DT, const TreeNodePtr AttachTo) {
619  NodeToInfo[NumToNode[1]].IDom = AttachTo->getBlock();
620  for (size_t i = 1, e = NumToNode.size(); i != e; ++i) {
621  const NodePtr N = NumToNode[i];
622  const TreeNodePtr TN = DT.getNode(N);
623  assert(TN);
624  const TreeNodePtr NewIDom = DT.getNode(NodeToInfo[N].IDom);
625  TN->setIDom(NewIDom);
626  }
627  }
628 
629  // Helper struct used during edge insertions.
630  struct InsertionInfo {
631  struct Compare {
632  bool operator()(TreeNodePtr LHS, TreeNodePtr RHS) const {
633  return LHS->getLevel() < RHS->getLevel();
634  }
635  };
636 
637  // Bucket queue of tree nodes ordered by descending level. For simplicity,
638  // we use a priority_queue here.
639  std::priority_queue<TreeNodePtr, SmallVector<TreeNodePtr, 8>,
640  Compare>
644 #ifndef NDEBUG
646 #endif
647  };
648 
649  static void InsertEdge(DomTreeT &DT, const BatchUpdatePtr BUI,
650  const NodePtr From, const NodePtr To) {
651  assert((From || IsPostDom) &&
652  "From has to be a valid CFG node or a virtual root");
653  assert(To && "Cannot be a nullptr");
654  LLVM_DEBUG(dbgs() << "Inserting edge " << BlockNamePrinter(From) << " -> "
655  << BlockNamePrinter(To) << "\n");
656  TreeNodePtr FromTN = DT.getNode(From);
657 
658  if (!FromTN) {
659  // Ignore edges from unreachable nodes for (forward) dominators.
660  if (!IsPostDom) return;
661 
662  // The unreachable node becomes a new root -- a tree node for it.
663  TreeNodePtr VirtualRoot = DT.getNode(nullptr);
664  FromTN =
665  (DT.DomTreeNodes[From] = VirtualRoot->addChild(
666  llvm::make_unique<DomTreeNodeBase<NodeT>>(From, VirtualRoot)))
667  .get();
668  DT.Roots.push_back(From);
669  }
670 
671  DT.DFSInfoValid = false;
672 
673  const TreeNodePtr ToTN = DT.getNode(To);
674  if (!ToTN)
675  InsertUnreachable(DT, BUI, FromTN, To);
676  else
677  InsertReachable(DT, BUI, FromTN, ToTN);
678  }
679 
680  // Determines if some existing root becomes reverse-reachable after the
681  // insertion. Rebuilds the whole tree if that situation happens.
682  static bool UpdateRootsBeforeInsertion(DomTreeT &DT, const BatchUpdatePtr BUI,
683  const TreeNodePtr From,
684  const TreeNodePtr To) {
685  assert(IsPostDom && "This function is only for postdominators");
686  // Destination node is not attached to the virtual root, so it cannot be a
687  // root.
688  if (!DT.isVirtualRoot(To->getIDom())) return false;
689 
690  auto RIt = llvm::find(DT.Roots, To->getBlock());
691  if (RIt == DT.Roots.end())
692  return false; // To is not a root, nothing to update.
693 
694  LLVM_DEBUG(dbgs() << "\t\tAfter the insertion, " << BlockNamePrinter(To)
695  << " is no longer a root\n\t\tRebuilding the tree!!!\n");
696 
697  CalculateFromScratch(DT, BUI);
698  return true;
699  }
700 
701  // Updates the set of roots after insertion or deletion. This ensures that
702  // roots are the same when after a series of updates and when the tree would
703  // be built from scratch.
704  static void UpdateRootsAfterUpdate(DomTreeT &DT, const BatchUpdatePtr BUI) {
705  assert(IsPostDom && "This function is only for postdominators");
706 
707  // The tree has only trivial roots -- nothing to update.
708  if (std::none_of(DT.Roots.begin(), DT.Roots.end(), [BUI](const NodePtr N) {
709  return HasForwardSuccessors(N, BUI);
710  }))
711  return;
712 
713  // Recalculate the set of roots.
714  auto Roots = FindRoots(DT, BUI);
715  if (DT.Roots.size() != Roots.size() ||
716  !std::is_permutation(DT.Roots.begin(), DT.Roots.end(), Roots.begin())) {
717  // The roots chosen in the CFG have changed. This is because the
718  // incremental algorithm does not really know or use the set of roots and
719  // can make a different (implicit) decision about which node within an
720  // infinite loop becomes a root.
721 
722  LLVM_DEBUG(dbgs() << "Roots are different in updated trees\n"
723  << "The entire tree needs to be rebuilt\n");
724  // It may be possible to update the tree without recalculating it, but
725  // we do not know yet how to do it, and it happens rarely in practise.
726  CalculateFromScratch(DT, BUI);
727  return;
728  }
729  }
730 
731  // Handles insertion to a node already in the dominator tree.
732  static void InsertReachable(DomTreeT &DT, const BatchUpdatePtr BUI,
733  const TreeNodePtr From, const TreeNodePtr To) {
734  LLVM_DEBUG(dbgs() << "\tReachable " << BlockNamePrinter(From->getBlock())
735  << " -> " << BlockNamePrinter(To->getBlock()) << "\n");
736  if (IsPostDom && UpdateRootsBeforeInsertion(DT, BUI, From, To)) return;
737  // DT.findNCD expects both pointers to be valid. When From is a virtual
738  // root, then its CFG block pointer is a nullptr, so we have to 'compute'
739  // the NCD manually.
740  const NodePtr NCDBlock =
741  (From->getBlock() && To->getBlock())
742  ? DT.findNearestCommonDominator(From->getBlock(), To->getBlock())
743  : nullptr;
744  assert(NCDBlock || DT.isPostDominator());
745  const TreeNodePtr NCD = DT.getNode(NCDBlock);
746  assert(NCD);
747 
748  LLVM_DEBUG(dbgs() << "\t\tNCA == " << BlockNamePrinter(NCD) << "\n");
749  const unsigned NCDLevel = NCD->getLevel();
750 
751  // Based on Lemma 2.5 from the second paper, after insertion of (From,To), v
752  // is affected iff depth(NCD)+1 < depth(v) && a path P from To to v exists
753  // where every w on P s.t. depth(v) <= depth(w)
754  //
755  // This reduces to a widest path problem (maximizing the depth of the
756  // minimum vertex in the path) which can be solved by a modified version of
757  // Dijkstra with a bucket queue (named depth-based search in the paper).
758 
759  // To is in the path, so depth(NCD)+1 < depth(v) <= depth(To). Nothing
760  // affected if this does not hold.
761  if (NCDLevel + 1 >= To->getLevel())
762  return;
763 
764  InsertionInfo II;
765  SmallVector<TreeNodePtr, 8> UnaffectedOnCurrentLevel;
766  II.Bucket.push(To);
767  II.Visited.insert(To);
768 
769  while (!II.Bucket.empty()) {
770  TreeNodePtr TN = II.Bucket.top();
771  II.Bucket.pop();
772  II.Affected.push_back(TN);
773 
774  const unsigned CurrentLevel = TN->getLevel();
775  LLVM_DEBUG(dbgs() << "Mark " << BlockNamePrinter(TN) <<
776  "as affected, CurrentLevel " << CurrentLevel << "\n");
777 
778  assert(TN->getBlock() && II.Visited.count(TN) && "Preconditions!");
779 
780  while (true) {
781  // Unlike regular Dijkstra, we have an inner loop to expand more
782  // vertices. The first iteration is for the (affected) vertex popped
783  // from II.Bucket and the rest are for vertices in
784  // UnaffectedOnCurrentLevel, which may eventually expand to affected
785  // vertices.
786  //
787  // Invariant: there is an optimal path from `To` to TN with the minimum
788  // depth being CurrentLevel.
789  for (const NodePtr Succ :
791  const TreeNodePtr SuccTN = DT.getNode(Succ);
792  assert(SuccTN &&
793  "Unreachable successor found at reachable insertion");
794  const unsigned SuccLevel = SuccTN->getLevel();
795 
796  LLVM_DEBUG(dbgs() << "\tSuccessor " << BlockNamePrinter(Succ)
797  << ", level = " << SuccLevel << "\n");
798 
799  // There is an optimal path from `To` to Succ with the minimum depth
800  // being min(CurrentLevel, SuccLevel).
801  //
802  // If depth(NCD)+1 < depth(Succ) is not satisfied, Succ is unaffected
803  // and no affected vertex may be reached by a path passing through it.
804  // Stop here. Also, Succ may be visited by other predecessors but the
805  // first visit has the optimal path. Stop if Succ has been visited.
806  if (SuccLevel <= NCDLevel + 1 || !II.Visited.insert(SuccTN).second)
807  continue;
808 
809  if (SuccLevel > CurrentLevel) {
810  // Succ is unaffected but it may (transitively) expand to affected
811  // vertices. Store it in UnaffectedOnCurrentLevel.
812  LLVM_DEBUG(dbgs() << "\t\tMarking visited not affected "
813  << BlockNamePrinter(Succ) << "\n");
814  UnaffectedOnCurrentLevel.push_back(SuccTN);
815 #ifndef NDEBUG
816  II.VisitedUnaffected.push_back(SuccTN);
817 #endif
818  } else {
819  // The condition is satisfied (Succ is affected). Add Succ to the
820  // bucket queue.
821  LLVM_DEBUG(dbgs() << "\t\tAdd " << BlockNamePrinter(Succ)
822  << " to a Bucket\n");
823  II.Bucket.push(SuccTN);
824  }
825  }
826 
827  if (UnaffectedOnCurrentLevel.empty())
828  break;
829  TN = UnaffectedOnCurrentLevel.pop_back_val();
830  LLVM_DEBUG(dbgs() << " Next: " << BlockNamePrinter(TN) << "\n");
831  }
832  }
833 
834  // Finish by updating immediate dominators and levels.
835  UpdateInsertion(DT, BUI, NCD, II);
836  }
837 
838  // Updates immediate dominators and levels after insertion.
839  static void UpdateInsertion(DomTreeT &DT, const BatchUpdatePtr BUI,
840  const TreeNodePtr NCD, InsertionInfo &II) {
841  LLVM_DEBUG(dbgs() << "Updating NCD = " << BlockNamePrinter(NCD) << "\n");
842 
843  for (const TreeNodePtr TN : II.Affected) {
844  LLVM_DEBUG(dbgs() << "\tIDom(" << BlockNamePrinter(TN)
845  << ") = " << BlockNamePrinter(NCD) << "\n");
846  TN->setIDom(NCD);
847  }
848 
849 #ifndef NDEBUG
850  for (const TreeNodePtr TN : II.VisitedUnaffected)
851  assert(TN->getLevel() == TN->getIDom()->getLevel() + 1 &&
852  "TN should have been updated by an affected ancestor");
853 #endif
854 
855  if (IsPostDom) UpdateRootsAfterUpdate(DT, BUI);
856  }
857 
858  // Handles insertion to previously unreachable nodes.
859  static void InsertUnreachable(DomTreeT &DT, const BatchUpdatePtr BUI,
860  const TreeNodePtr From, const NodePtr To) {
861  LLVM_DEBUG(dbgs() << "Inserting " << BlockNamePrinter(From)
862  << " -> (unreachable) " << BlockNamePrinter(To) << "\n");
863 
864  // Collect discovered edges to already reachable nodes.
865  SmallVector<std::pair<NodePtr, TreeNodePtr>, 8> DiscoveredEdgesToReachable;
866  // Discover and connect nodes that became reachable with the insertion.
867  ComputeUnreachableDominators(DT, BUI, To, From, DiscoveredEdgesToReachable);
868 
869  LLVM_DEBUG(dbgs() << "Inserted " << BlockNamePrinter(From)
870  << " -> (prev unreachable) " << BlockNamePrinter(To)
871  << "\n");
872 
873  // Used the discovered edges and inset discovered connecting (incoming)
874  // edges.
875  for (const auto &Edge : DiscoveredEdgesToReachable) {
876  LLVM_DEBUG(dbgs() << "\tInserting discovered connecting edge "
877  << BlockNamePrinter(Edge.first) << " -> "
878  << BlockNamePrinter(Edge.second) << "\n");
879  InsertReachable(DT, BUI, DT.getNode(Edge.first), Edge.second);
880  }
881  }
882 
883  // Connects nodes that become reachable with an insertion.
885  DomTreeT &DT, const BatchUpdatePtr BUI, const NodePtr Root,
886  const TreeNodePtr Incoming,
887  SmallVectorImpl<std::pair<NodePtr, TreeNodePtr>>
888  &DiscoveredConnectingEdges) {
889  assert(!DT.getNode(Root) && "Root must not be reachable");
890 
891  // Visit only previously unreachable nodes.
892  auto UnreachableDescender = [&DT, &DiscoveredConnectingEdges](NodePtr From,
893  NodePtr To) {
894  const TreeNodePtr ToTN = DT.getNode(To);
895  if (!ToTN) return true;
896 
897  DiscoveredConnectingEdges.push_back({From, ToTN});
898  return false;
899  };
900 
901  SemiNCAInfo SNCA(BUI);
902  SNCA.runDFS(Root, 0, UnreachableDescender, 0);
903  SNCA.runSemiNCA(DT);
904  SNCA.attachNewSubtree(DT, Incoming);
905 
906  LLVM_DEBUG(dbgs() << "After adding unreachable nodes\n");
907  }
908 
909  static void DeleteEdge(DomTreeT &DT, const BatchUpdatePtr BUI,
910  const NodePtr From, const NodePtr To) {
911  assert(From && To && "Cannot disconnect nullptrs");
912  LLVM_DEBUG(dbgs() << "Deleting edge " << BlockNamePrinter(From) << " -> "
913  << BlockNamePrinter(To) << "\n");
914 
915 #ifndef NDEBUG
916  // Ensure that the edge was in fact deleted from the CFG before informing
917  // the DomTree about it.
918  // The check is O(N), so run it only in debug configuration.
919  auto IsSuccessor = [BUI](const NodePtr SuccCandidate, const NodePtr Of) {
920  auto Successors = ChildrenGetter<IsPostDom>::Get(Of, BUI);
921  return llvm::find(Successors, SuccCandidate) != Successors.end();
922  };
923  (void)IsSuccessor;
924  assert(!IsSuccessor(To, From) && "Deleted edge still exists in the CFG!");
925 #endif
926 
927  const TreeNodePtr FromTN = DT.getNode(From);
928  // Deletion in an unreachable subtree -- nothing to do.
929  if (!FromTN) return;
930 
931  const TreeNodePtr ToTN = DT.getNode(To);
932  if (!ToTN) {
933  LLVM_DEBUG(
934  dbgs() << "\tTo (" << BlockNamePrinter(To)
935  << ") already unreachable -- there is no edge to delete\n");
936  return;
937  }
938 
939  const NodePtr NCDBlock = DT.findNearestCommonDominator(From, To);
940  const TreeNodePtr NCD = DT.getNode(NCDBlock);
941 
942  // If To dominates From -- nothing to do.
943  if (ToTN != NCD) {
944  DT.DFSInfoValid = false;
945 
946  const TreeNodePtr ToIDom = ToTN->getIDom();
947  LLVM_DEBUG(dbgs() << "\tNCD " << BlockNamePrinter(NCD) << ", ToIDom "
948  << BlockNamePrinter(ToIDom) << "\n");
949 
950  // To remains reachable after deletion.
951  // (Based on the caption under Figure 4. from the second paper.)
952  if (FromTN != ToIDom || HasProperSupport(DT, BUI, ToTN))
953  DeleteReachable(DT, BUI, FromTN, ToTN);
954  else
955  DeleteUnreachable(DT, BUI, ToTN);
956  }
957 
958  if (IsPostDom) UpdateRootsAfterUpdate(DT, BUI);
959  }
960 
961  // Handles deletions that leave destination nodes reachable.
962  static void DeleteReachable(DomTreeT &DT, const BatchUpdatePtr BUI,
963  const TreeNodePtr FromTN,
964  const TreeNodePtr ToTN) {
965  LLVM_DEBUG(dbgs() << "Deleting reachable " << BlockNamePrinter(FromTN)
966  << " -> " << BlockNamePrinter(ToTN) << "\n");
967  LLVM_DEBUG(dbgs() << "\tRebuilding subtree\n");
968 
969  // Find the top of the subtree that needs to be rebuilt.
970  // (Based on the lemma 2.6 from the second paper.)
971  const NodePtr ToIDom =
972  DT.findNearestCommonDominator(FromTN->getBlock(), ToTN->getBlock());
973  assert(ToIDom || DT.isPostDominator());
974  const TreeNodePtr ToIDomTN = DT.getNode(ToIDom);
975  assert(ToIDomTN);
976  const TreeNodePtr PrevIDomSubTree = ToIDomTN->getIDom();
977  // Top of the subtree to rebuild is the root node. Rebuild the tree from
978  // scratch.
979  if (!PrevIDomSubTree) {
980  LLVM_DEBUG(dbgs() << "The entire tree needs to be rebuilt\n");
981  CalculateFromScratch(DT, BUI);
982  return;
983  }
984 
985  // Only visit nodes in the subtree starting at To.
986  const unsigned Level = ToIDomTN->getLevel();
987  auto DescendBelow = [Level, &DT](NodePtr, NodePtr To) {
988  return DT.getNode(To)->getLevel() > Level;
989  };
990 
991  LLVM_DEBUG(dbgs() << "\tTop of subtree: " << BlockNamePrinter(ToIDomTN)
992  << "\n");
993 
994  SemiNCAInfo SNCA(BUI);
995  SNCA.runDFS(ToIDom, 0, DescendBelow, 0);
996  LLVM_DEBUG(dbgs() << "\tRunning Semi-NCA\n");
997  SNCA.runSemiNCA(DT, Level);
998  SNCA.reattachExistingSubtree(DT, PrevIDomSubTree);
999  }
1000 
1001  // Checks if a node has proper support, as defined on the page 3 and later
1002  // explained on the page 7 of the second paper.
1003  static bool HasProperSupport(DomTreeT &DT, const BatchUpdatePtr BUI,
1004  const TreeNodePtr TN) {
1005  LLVM_DEBUG(dbgs() << "IsReachableFromIDom " << BlockNamePrinter(TN)
1006  << "\n");
1007  for (const NodePtr Pred :
1009  LLVM_DEBUG(dbgs() << "\tPred " << BlockNamePrinter(Pred) << "\n");
1010  if (!DT.getNode(Pred)) continue;
1011 
1012  const NodePtr Support =
1013  DT.findNearestCommonDominator(TN->getBlock(), Pred);
1014  LLVM_DEBUG(dbgs() << "\tSupport " << BlockNamePrinter(Support) << "\n");
1015  if (Support != TN->getBlock()) {
1016  LLVM_DEBUG(dbgs() << "\t" << BlockNamePrinter(TN)
1017  << " is reachable from support "
1018  << BlockNamePrinter(Support) << "\n");
1019  return true;
1020  }
1021  }
1022 
1023  return false;
1024  }
1025 
1026  // Handle deletions that make destination node unreachable.
1027  // (Based on the lemma 2.7 from the second paper.)
1028  static void DeleteUnreachable(DomTreeT &DT, const BatchUpdatePtr BUI,
1029  const TreeNodePtr ToTN) {
1030  LLVM_DEBUG(dbgs() << "Deleting unreachable subtree "
1031  << BlockNamePrinter(ToTN) << "\n");
1032  assert(ToTN);
1033  assert(ToTN->getBlock());
1034 
1035  if (IsPostDom) {
1036  // Deletion makes a region reverse-unreachable and creates a new root.
1037  // Simulate that by inserting an edge from the virtual root to ToTN and
1038  // adding it as a new root.
1039  LLVM_DEBUG(dbgs() << "\tDeletion made a region reverse-unreachable\n");
1040  LLVM_DEBUG(dbgs() << "\tAdding new root " << BlockNamePrinter(ToTN)
1041  << "\n");
1042  DT.Roots.push_back(ToTN->getBlock());
1043  InsertReachable(DT, BUI, DT.getNode(nullptr), ToTN);
1044  return;
1045  }
1046 
1047  SmallVector<NodePtr, 16> AffectedQueue;
1048  const unsigned Level = ToTN->getLevel();
1049 
1050  // Traverse destination node's descendants with greater level in the tree
1051  // and collect visited nodes.
1052  auto DescendAndCollect = [Level, &AffectedQueue, &DT](NodePtr, NodePtr To) {
1053  const TreeNodePtr TN = DT.getNode(To);
1054  assert(TN);
1055  if (TN->getLevel() > Level) return true;
1056  if (llvm::find(AffectedQueue, To) == AffectedQueue.end())
1057  AffectedQueue.push_back(To);
1058 
1059  return false;
1060  };
1061 
1062  SemiNCAInfo SNCA(BUI);
1063  unsigned LastDFSNum =
1064  SNCA.runDFS(ToTN->getBlock(), 0, DescendAndCollect, 0);
1065 
1066  TreeNodePtr MinNode = ToTN;
1067 
1068  // Identify the top of the subtree to rebuild by finding the NCD of all
1069  // the affected nodes.
1070  for (const NodePtr N : AffectedQueue) {
1071  const TreeNodePtr TN = DT.getNode(N);
1072  const NodePtr NCDBlock =
1073  DT.findNearestCommonDominator(TN->getBlock(), ToTN->getBlock());
1074  assert(NCDBlock || DT.isPostDominator());
1075  const TreeNodePtr NCD = DT.getNode(NCDBlock);
1076  assert(NCD);
1077 
1078  LLVM_DEBUG(dbgs() << "Processing affected node " << BlockNamePrinter(TN)
1079  << " with NCD = " << BlockNamePrinter(NCD)
1080  << ", MinNode =" << BlockNamePrinter(MinNode) << "\n");
1081  if (NCD != TN && NCD->getLevel() < MinNode->getLevel()) MinNode = NCD;
1082  }
1083 
1084  // Root reached, rebuild the whole tree from scratch.
1085  if (!MinNode->getIDom()) {
1086  LLVM_DEBUG(dbgs() << "The entire tree needs to be rebuilt\n");
1087  CalculateFromScratch(DT, BUI);
1088  return;
1089  }
1090 
1091  // Erase the unreachable subtree in reverse preorder to process all children
1092  // before deleting their parent.
1093  for (unsigned i = LastDFSNum; i > 0; --i) {
1094  const NodePtr N = SNCA.NumToNode[i];
1095  const TreeNodePtr TN = DT.getNode(N);
1096  LLVM_DEBUG(dbgs() << "Erasing node " << BlockNamePrinter(TN) << "\n");
1097 
1098  EraseNode(DT, TN);
1099  }
1100 
1101  // The affected subtree start at the To node -- there's no extra work to do.
1102  if (MinNode == ToTN) return;
1103 
1104  LLVM_DEBUG(dbgs() << "DeleteUnreachable: running DFS with MinNode = "
1105  << BlockNamePrinter(MinNode) << "\n");
1106  const unsigned MinLevel = MinNode->getLevel();
1107  const TreeNodePtr PrevIDom = MinNode->getIDom();
1108  assert(PrevIDom);
1109  SNCA.clear();
1110 
1111  // Identify nodes that remain in the affected subtree.
1112  auto DescendBelow = [MinLevel, &DT](NodePtr, NodePtr To) {
1113  const TreeNodePtr ToTN = DT.getNode(To);
1114  return ToTN && ToTN->getLevel() > MinLevel;
1115  };
1116  SNCA.runDFS(MinNode->getBlock(), 0, DescendBelow, 0);
1117 
1118  LLVM_DEBUG(dbgs() << "Previous IDom(MinNode) = "
1119  << BlockNamePrinter(PrevIDom) << "\nRunning Semi-NCA\n");
1120 
1121  // Rebuild the remaining part of affected subtree.
1122  SNCA.runSemiNCA(DT, MinLevel);
1123  SNCA.reattachExistingSubtree(DT, PrevIDom);
1124  }
1125 
1126  // Removes leaf tree nodes from the dominator tree.
1127  static void EraseNode(DomTreeT &DT, const TreeNodePtr TN) {
1128  assert(TN);
1129  assert(TN->getNumChildren() == 0 && "Not a tree leaf");
1130 
1131  const TreeNodePtr IDom = TN->getIDom();
1132  assert(IDom);
1133 
1134  auto ChIt = llvm::find(IDom->Children, TN);
1135  assert(ChIt != IDom->Children.end());
1136  std::swap(*ChIt, IDom->Children.back());
1137  IDom->Children.pop_back();
1138 
1139  DT.DomTreeNodes.erase(TN->getBlock());
1140  }
1141 
1142  //~~
1143  //===--------------------- DomTree Batch Updater --------------------------===
1144  //~~
1145 
1146  static void ApplyUpdates(DomTreeT &DT, ArrayRef<UpdateT> Updates) {
1147  const size_t NumUpdates = Updates.size();
1148  if (NumUpdates == 0)
1149  return;
1150 
1151  // Take the fast path for a single update and avoid running the batch update
1152  // machinery.
1153  if (NumUpdates == 1) {
1154  const auto &Update = Updates.front();
1155  if (Update.getKind() == UpdateKind::Insert)
1156  DT.insertEdge(Update.getFrom(), Update.getTo());
1157  else
1158  DT.deleteEdge(Update.getFrom(), Update.getTo());
1159 
1160  return;
1161  }
1162 
1163  BatchUpdateInfo BUI;
1164  LLVM_DEBUG(dbgs() << "Legalizing " << BUI.Updates.size() << " updates\n");
1165  cfg::LegalizeUpdates<NodePtr>(Updates, BUI.Updates, IsPostDom);
1166 
1167  const size_t NumLegalized = BUI.Updates.size();
1168  BUI.FutureSuccessors.reserve(NumLegalized);
1169  BUI.FuturePredecessors.reserve(NumLegalized);
1170 
1171  // Use the legalized future updates to initialize future successors and
1172  // predecessors. Note that these sets will only decrease size over time, as
1173  // the next CFG snapshots slowly approach the actual (current) CFG.
1174  for (UpdateT &U : BUI.Updates) {
1175  BUI.FutureSuccessors[U.getFrom()].push_back({U.getTo(), U.getKind()});
1176  BUI.FuturePredecessors[U.getTo()].push_back({U.getFrom(), U.getKind()});
1177  }
1178 
1179  LLVM_DEBUG(dbgs() << "About to apply " << NumLegalized << " updates\n");
1180  LLVM_DEBUG(if (NumLegalized < 32) for (const auto &U
1181  : reverse(BUI.Updates)) {
1182  dbgs() << "\t";
1183  U.dump();
1184  dbgs() << "\n";
1185  });
1186  LLVM_DEBUG(dbgs() << "\n");
1187 
1188  // Recalculate the DominatorTree when the number of updates
1189  // exceeds a threshold, which usually makes direct updating slower than
1190  // recalculation. We select this threshold proportional to the
1191  // size of the DominatorTree. The constant is selected
1192  // by choosing the one with an acceptable performance on some real-world
1193  // inputs.
1194 
1195  // Make unittests of the incremental algorithm work
1196  if (DT.DomTreeNodes.size() <= 100) {
1197  if (NumLegalized > DT.DomTreeNodes.size())
1198  CalculateFromScratch(DT, &BUI);
1199  } else if (NumLegalized > DT.DomTreeNodes.size() / 40)
1200  CalculateFromScratch(DT, &BUI);
1201 
1202  // If the DominatorTree was recalculated at some point, stop the batch
1203  // updates. Full recalculations ignore batch updates and look at the actual
1204  // CFG.
1205  for (size_t i = 0; i < NumLegalized && !BUI.IsRecalculated; ++i)
1206  ApplyNextUpdate(DT, BUI);
1207  }
1208 
1209  static void ApplyNextUpdate(DomTreeT &DT, BatchUpdateInfo &BUI) {
1210  assert(!BUI.Updates.empty() && "No updates to apply!");
1211  UpdateT CurrentUpdate = BUI.Updates.pop_back_val();
1212  LLVM_DEBUG(dbgs() << "Applying update: ");
1213  LLVM_DEBUG(CurrentUpdate.dump(); dbgs() << "\n");
1214 
1215  // Move to the next snapshot of the CFG by removing the reverse-applied
1216  // current update. Since updates are performed in the same order they are
1217  // legalized it's sufficient to pop the last item here.
1218  auto &FS = BUI.FutureSuccessors[CurrentUpdate.getFrom()];
1219  assert(FS.back().getPointer() == CurrentUpdate.getTo() &&
1220  FS.back().getInt() == CurrentUpdate.getKind());
1221  FS.pop_back();
1222  if (FS.empty()) BUI.FutureSuccessors.erase(CurrentUpdate.getFrom());
1223 
1224  auto &FP = BUI.FuturePredecessors[CurrentUpdate.getTo()];
1225  assert(FP.back().getPointer() == CurrentUpdate.getFrom() &&
1226  FP.back().getInt() == CurrentUpdate.getKind());
1227  FP.pop_back();
1228  if (FP.empty()) BUI.FuturePredecessors.erase(CurrentUpdate.getTo());
1229 
1230  if (CurrentUpdate.getKind() == UpdateKind::Insert)
1231  InsertEdge(DT, &BUI, CurrentUpdate.getFrom(), CurrentUpdate.getTo());
1232  else
1233  DeleteEdge(DT, &BUI, CurrentUpdate.getFrom(), CurrentUpdate.getTo());
1234  }
1235 
1236  //~~
1237  //===--------------- DomTree correctness verification ---------------------===
1238  //~~
1239 
1240  // Check if the tree has correct roots. A DominatorTree always has a single
1241  // root which is the function's entry node. A PostDominatorTree can have
1242  // multiple roots - one for each node with no successors and for infinite
1243  // loops.
1244  // Running time: O(N).
1245  bool verifyRoots(const DomTreeT &DT) {
1246  if (!DT.Parent && !DT.Roots.empty()) {
1247  errs() << "Tree has no parent but has roots!\n";
1248  errs().flush();
1249  return false;
1250  }
1251 
1252  if (!IsPostDom) {
1253  if (DT.Roots.empty()) {
1254  errs() << "Tree doesn't have a root!\n";
1255  errs().flush();
1256  return false;
1257  }
1258 
1259  if (DT.getRoot() != GetEntryNode(DT)) {
1260  errs() << "Tree's root is not its parent's entry node!\n";
1261  errs().flush();
1262  return false;
1263  }
1264  }
1265 
1266  RootsT ComputedRoots = FindRoots(DT, nullptr);
1267  if (DT.Roots.size() != ComputedRoots.size() ||
1268  !std::is_permutation(DT.Roots.begin(), DT.Roots.end(),
1269  ComputedRoots.begin())) {
1270  errs() << "Tree has different roots than freshly computed ones!\n";
1271  errs() << "\tPDT roots: ";
1272  for (const NodePtr N : DT.Roots) errs() << BlockNamePrinter(N) << ", ";
1273  errs() << "\n\tComputed roots: ";
1274  for (const NodePtr N : ComputedRoots)
1275  errs() << BlockNamePrinter(N) << ", ";
1276  errs() << "\n";
1277  errs().flush();
1278  return false;
1279  }
1280 
1281  return true;
1282  }
1283 
1284  // Checks if the tree contains all reachable nodes in the input graph.
1285  // Running time: O(N).
1286  bool verifyReachability(const DomTreeT &DT) {
1287  clear();
1289 
1290  for (auto &NodeToTN : DT.DomTreeNodes) {
1291  const TreeNodePtr TN = NodeToTN.second.get();
1292  const NodePtr BB = TN->getBlock();
1293 
1294  // Virtual root has a corresponding virtual CFG node.
1295  if (DT.isVirtualRoot(TN)) continue;
1296 
1297  if (NodeToInfo.count(BB) == 0) {
1298  errs() << "DomTree node " << BlockNamePrinter(BB)
1299  << " not found by DFS walk!\n";
1300  errs().flush();
1301 
1302  return false;
1303  }
1304  }
1305 
1306  for (const NodePtr N : NumToNode) {
1307  if (N && !DT.getNode(N)) {
1308  errs() << "CFG node " << BlockNamePrinter(N)
1309  << " not found in the DomTree!\n";
1310  errs().flush();
1311 
1312  return false;
1313  }
1314  }
1315 
1316  return true;
1317  }
1318 
1319  // Check if for every parent with a level L in the tree all of its children
1320  // have level L + 1.
1321  // Running time: O(N).
1322  static bool VerifyLevels(const DomTreeT &DT) {
1323  for (auto &NodeToTN : DT.DomTreeNodes) {
1324  const TreeNodePtr TN = NodeToTN.second.get();
1325  const NodePtr BB = TN->getBlock();
1326  if (!BB) continue;
1327 
1328  const TreeNodePtr IDom = TN->getIDom();
1329  if (!IDom && TN->getLevel() != 0) {
1330  errs() << "Node without an IDom " << BlockNamePrinter(BB)
1331  << " has a nonzero level " << TN->getLevel() << "!\n";
1332  errs().flush();
1333 
1334  return false;
1335  }
1336 
1337  if (IDom && TN->getLevel() != IDom->getLevel() + 1) {
1338  errs() << "Node " << BlockNamePrinter(BB) << " has level "
1339  << TN->getLevel() << " while its IDom "
1340  << BlockNamePrinter(IDom->getBlock()) << " has level "
1341  << IDom->getLevel() << "!\n";
1342  errs().flush();
1343 
1344  return false;
1345  }
1346  }
1347 
1348  return true;
1349  }
1350 
1351  // Check if the computed DFS numbers are correct. Note that DFS info may not
1352  // be valid, and when that is the case, we don't verify the numbers.
1353  // Running time: O(N log(N)).
1354  static bool VerifyDFSNumbers(const DomTreeT &DT) {
1355  if (!DT.DFSInfoValid || !DT.Parent)
1356  return true;
1357 
1358  const NodePtr RootBB = IsPostDom ? nullptr : DT.getRoots()[0];
1359  const TreeNodePtr Root = DT.getNode(RootBB);
1360 
1361  auto PrintNodeAndDFSNums = [](const TreeNodePtr TN) {
1362  errs() << BlockNamePrinter(TN) << " {" << TN->getDFSNumIn() << ", "
1363  << TN->getDFSNumOut() << '}';
1364  };
1365 
1366  // Verify the root's DFS In number. Although DFS numbering would also work
1367  // if we started from some other value, we assume 0-based numbering.
1368  if (Root->getDFSNumIn() != 0) {
1369  errs() << "DFSIn number for the tree root is not:\n\t";
1370  PrintNodeAndDFSNums(Root);
1371  errs() << '\n';
1372  errs().flush();
1373  return false;
1374  }
1375 
1376  // For each tree node verify if children's DFS numbers cover their parent's
1377  // DFS numbers with no gaps.
1378  for (const auto &NodeToTN : DT.DomTreeNodes) {
1379  const TreeNodePtr Node = NodeToTN.second.get();
1380 
1381  // Handle tree leaves.
1382  if (Node->getChildren().empty()) {
1383  if (Node->getDFSNumIn() + 1 != Node->getDFSNumOut()) {
1384  errs() << "Tree leaf should have DFSOut = DFSIn + 1:\n\t";
1385  PrintNodeAndDFSNums(Node);
1386  errs() << '\n';
1387  errs().flush();
1388  return false;
1389  }
1390 
1391  continue;
1392  }
1393 
1394  // Make a copy and sort it such that it is possible to check if there are
1395  // no gaps between DFS numbers of adjacent children.
1396  SmallVector<TreeNodePtr, 8> Children(Node->begin(), Node->end());
1397  llvm::sort(Children, [](const TreeNodePtr Ch1, const TreeNodePtr Ch2) {
1398  return Ch1->getDFSNumIn() < Ch2->getDFSNumIn();
1399  });
1400 
1401  auto PrintChildrenError = [Node, &Children, PrintNodeAndDFSNums](
1402  const TreeNodePtr FirstCh, const TreeNodePtr SecondCh) {
1403  assert(FirstCh);
1404 
1405  errs() << "Incorrect DFS numbers for:\n\tParent ";
1406  PrintNodeAndDFSNums(Node);
1407 
1408  errs() << "\n\tChild ";
1409  PrintNodeAndDFSNums(FirstCh);
1410 
1411  if (SecondCh) {
1412  errs() << "\n\tSecond child ";
1413  PrintNodeAndDFSNums(SecondCh);
1414  }
1415 
1416  errs() << "\nAll children: ";
1417  for (const TreeNodePtr Ch : Children) {
1418  PrintNodeAndDFSNums(Ch);
1419  errs() << ", ";
1420  }
1421 
1422  errs() << '\n';
1423  errs().flush();
1424  };
1425 
1426  if (Children.front()->getDFSNumIn() != Node->getDFSNumIn() + 1) {
1427  PrintChildrenError(Children.front(), nullptr);
1428  return false;
1429  }
1430 
1431  if (Children.back()->getDFSNumOut() + 1 != Node->getDFSNumOut()) {
1432  PrintChildrenError(Children.back(), nullptr);
1433  return false;
1434  }
1435 
1436  for (size_t i = 0, e = Children.size() - 1; i != e; ++i) {
1437  if (Children[i]->getDFSNumOut() + 1 != Children[i + 1]->getDFSNumIn()) {
1438  PrintChildrenError(Children[i], Children[i + 1]);
1439  return false;
1440  }
1441  }
1442  }
1443 
1444  return true;
1445  }
1446 
1447  // The below routines verify the correctness of the dominator tree relative to
1448  // the CFG it's coming from. A tree is a dominator tree iff it has two
1449  // properties, called the parent property and the sibling property. Tarjan
1450  // and Lengauer prove (but don't explicitly name) the properties as part of
1451  // the proofs in their 1972 paper, but the proofs are mostly part of proving
1452  // things about semidominators and idoms, and some of them are simply asserted
1453  // based on even earlier papers (see, e.g., lemma 2). Some papers refer to
1454  // these properties as "valid" and "co-valid". See, e.g., "Dominators,
1455  // directed bipolar orders, and independent spanning trees" by Loukas
1456  // Georgiadis and Robert E. Tarjan, as well as "Dominator Tree Verification
1457  // and Vertex-Disjoint Paths " by the same authors.
1458 
1459  // A very simple and direct explanation of these properties can be found in
1460  // "An Experimental Study of Dynamic Dominators", found at
1461  // https://arxiv.org/abs/1604.02711
1462 
1463  // The easiest way to think of the parent property is that it's a requirement
1464  // of being a dominator. Let's just take immediate dominators. For PARENT to
1465  // be an immediate dominator of CHILD, all paths in the CFG must go through
1466  // PARENT before they hit CHILD. This implies that if you were to cut PARENT
1467  // out of the CFG, there should be no paths to CHILD that are reachable. If
1468  // there are, then you now have a path from PARENT to CHILD that goes around
1469  // PARENT and still reaches CHILD, which by definition, means PARENT can't be
1470  // a dominator of CHILD (let alone an immediate one).
1471 
1472  // The sibling property is similar. It says that for each pair of sibling
1473  // nodes in the dominator tree (LEFT and RIGHT) , they must not dominate each
1474  // other. If sibling LEFT dominated sibling RIGHT, it means there are no
1475  // paths in the CFG from sibling LEFT to sibling RIGHT that do not go through
1476  // LEFT, and thus, LEFT is really an ancestor (in the dominator tree) of
1477  // RIGHT, not a sibling.
1478 
1479  // It is possible to verify the parent and sibling properties in
1480  // linear time, but the algorithms are complex. Instead, we do it in a
1481  // straightforward N^2 and N^3 way below, using direct path reachability.
1482 
1483  // Checks if the tree has the parent property: if for all edges from V to W in
1484  // the input graph, such that V is reachable, the parent of W in the tree is
1485  // an ancestor of V in the tree.
1486  // Running time: O(N^2).
1487  //
1488  // This means that if a node gets disconnected from the graph, then all of
1489  // the nodes it dominated previously will now become unreachable.
1490  bool verifyParentProperty(const DomTreeT &DT) {
1491  for (auto &NodeToTN : DT.DomTreeNodes) {
1492  const TreeNodePtr TN = NodeToTN.second.get();
1493  const NodePtr BB = TN->getBlock();
1494  if (!BB || TN->getChildren().empty()) continue;
1495 
1496  LLVM_DEBUG(dbgs() << "Verifying parent property of node "
1497  << BlockNamePrinter(TN) << "\n");
1498  clear();
1499  doFullDFSWalk(DT, [BB](NodePtr From, NodePtr To) {
1500  return From != BB && To != BB;
1501  });
1502 
1503  for (TreeNodePtr Child : TN->getChildren())
1504  if (NodeToInfo.count(Child->getBlock()) != 0) {
1505  errs() << "Child " << BlockNamePrinter(Child)
1506  << " reachable after its parent " << BlockNamePrinter(BB)
1507  << " is removed!\n";
1508  errs().flush();
1509 
1510  return false;
1511  }
1512  }
1513 
1514  return true;
1515  }
1516 
1517  // Check if the tree has sibling property: if a node V does not dominate a
1518  // node W for all siblings V and W in the tree.
1519  // Running time: O(N^3).
1520  //
1521  // This means that if a node gets disconnected from the graph, then all of its
1522  // siblings will now still be reachable.
1523  bool verifySiblingProperty(const DomTreeT &DT) {
1524  for (auto &NodeToTN : DT.DomTreeNodes) {
1525  const TreeNodePtr TN = NodeToTN.second.get();
1526  const NodePtr BB = TN->getBlock();
1527  if (!BB || TN->getChildren().empty()) continue;
1528 
1529  const auto &Siblings = TN->getChildren();
1530  for (const TreeNodePtr N : Siblings) {
1531  clear();
1532  NodePtr BBN = N->getBlock();
1533  doFullDFSWalk(DT, [BBN](NodePtr From, NodePtr To) {
1534  return From != BBN && To != BBN;
1535  });
1536 
1537  for (const TreeNodePtr S : Siblings) {
1538  if (S == N) continue;
1539 
1540  if (NodeToInfo.count(S->getBlock()) == 0) {
1541  errs() << "Node " << BlockNamePrinter(S)
1542  << " not reachable when its sibling " << BlockNamePrinter(N)
1543  << " is removed!\n";
1544  errs().flush();
1545 
1546  return false;
1547  }
1548  }
1549  }
1550  }
1551 
1552  return true;
1553  }
1554 
1555  // Check if the given tree is the same as a freshly computed one for the same
1556  // Parent.
1557  // Running time: O(N^2), but faster in practise (same as tree construction).
1558  //
1559  // Note that this does not check if that the tree construction algorithm is
1560  // correct and should be only used for fast (but possibly unsound)
1561  // verification.
1562  static bool IsSameAsFreshTree(const DomTreeT &DT) {
1563  DomTreeT FreshTree;
1564  FreshTree.recalculate(*DT.Parent);
1565  const bool Different = DT.compare(FreshTree);
1566 
1567  if (Different) {
1568  errs() << (DT.isPostDominator() ? "Post" : "")
1569  << "DominatorTree is different than a freshly computed one!\n"
1570  << "\tCurrent:\n";
1571  DT.print(errs());
1572  errs() << "\n\tFreshly computed tree:\n";
1573  FreshTree.print(errs());
1574  errs().flush();
1575  }
1576 
1577  return !Different;
1578  }
1579 };
1580 
1581 template <class DomTreeT>
1582 void Calculate(DomTreeT &DT) {
1584 }
1585 
1586 template <typename DomTreeT>
1587 void CalculateWithUpdates(DomTreeT &DT,
1589  // TODO: Move BUI creation in common method, reuse in ApplyUpdates.
1591  LLVM_DEBUG(dbgs() << "Legalizing " << BUI.Updates.size() << " updates\n");
1592  cfg::LegalizeUpdates<typename DomTreeT::NodePtr>(Updates, BUI.Updates,
1593  DomTreeT::IsPostDominator);
1594  const size_t NumLegalized = BUI.Updates.size();
1595  BUI.FutureSuccessors.reserve(NumLegalized);
1596  BUI.FuturePredecessors.reserve(NumLegalized);
1597  for (auto &U : BUI.Updates) {
1598  BUI.FutureSuccessors[U.getFrom()].push_back({U.getTo(), U.getKind()});
1599  BUI.FuturePredecessors[U.getTo()].push_back({U.getFrom(), U.getKind()});
1600  }
1601 
1603 }
1604 
1605 template <class DomTreeT>
1606 void InsertEdge(DomTreeT &DT, typename DomTreeT::NodePtr From,
1607  typename DomTreeT::NodePtr To) {
1608  if (DT.isPostDominator()) std::swap(From, To);
1609  SemiNCAInfo<DomTreeT>::InsertEdge(DT, nullptr, From, To);
1610 }
1611 
1612 template <class DomTreeT>
1613 void DeleteEdge(DomTreeT &DT, typename DomTreeT::NodePtr From,
1614  typename DomTreeT::NodePtr To) {
1615  if (DT.isPostDominator()) std::swap(From, To);
1616  SemiNCAInfo<DomTreeT>::DeleteEdge(DT, nullptr, From, To);
1617 }
1618 
1619 template <class DomTreeT>
1620 void ApplyUpdates(DomTreeT &DT,
1623 }
1624 
1625 template <class DomTreeT>
1626 bool Verify(const DomTreeT &DT, typename DomTreeT::VerificationLevel VL) {
1627  SemiNCAInfo<DomTreeT> SNCA(nullptr);
1628 
1629  // Simplist check is to compare against a new tree. This will also
1630  // usefully print the old and new trees, if they are different.
1631  if (!SNCA.IsSameAsFreshTree(DT))
1632  return false;
1633 
1634  // Common checks to verify the properties of the tree. O(N log N) at worst
1635  if (!SNCA.verifyRoots(DT) || !SNCA.verifyReachability(DT) ||
1636  !SNCA.VerifyLevels(DT) || !SNCA.VerifyDFSNumbers(DT))
1637  return false;
1638 
1639  // Extra checks depending on VerificationLevel. Up to O(N^3)
1642  if (!SNCA.verifyParentProperty(DT))
1643  return false;
1645  if (!SNCA.verifySiblingProperty(DT))
1646  return false;
1647 
1648  return true;
1649 }
1650 
1651 } // namespace DomTreeBuilder
1652 } // namespace llvm
1653 
1654 #undef DEBUG_TYPE
1655 
1656 #endif
const T & front() const
front - Get the first element.
Definition: ArrayRef.h:151
static void UpdateInsertion(DomTreeT &DT, const BatchUpdatePtr BUI, const TreeNodePtr NCD, InsertionInfo &II)
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
DenseMap< NodePtr, InfoRec > NodeToInfo
static void EraseNode(DomTreeT &DT, const TreeNodePtr TN)
This class represents lattice values for constants.
Definition: AllocatorList.h:23
std::error_code remove(const Twine &path, bool IgnoreNonExisting=true)
Remove path.
static void UpdateRootsAfterUpdate(DomTreeT &DT, const BatchUpdatePtr BUI)
void push_back(const T &Elt)
Definition: SmallVector.h:211
void doFullDFSWalk(const DomTreeT &DT, DescendCondition DC)
std::priority_queue< TreeNodePtr, SmallVector< TreeNodePtr, 8 >, Compare > Bucket
std::enable_if<!std::is_array< T >::value, std::unique_ptr< T > >::type make_unique(Args &&... args)
Constructs a new T() with the given args and returns a unique_ptr<T> which owns the object...
Definition: STLExtras.h:1348
static bool VerifyLevels(const DomTreeT &DT)
NodeType
ISD::NodeType enum - This enum defines the target-independent operators for a SelectionDAG.
Definition: ISDOpcodes.h:38
NodePtr eval(NodePtr V, unsigned LastLinked, SmallVectorImpl< InfoRec *> &Stack)
static void ApplyNextUpdate(DomTreeT &DT, BatchUpdateInfo &BUI)
void runSemiNCA(DomTreeT &DT, const unsigned MinLevel=0)
static ManagedStatic< DebugCounter > DC
static void RemoveRedundantRoots(const DomTreeT &DT, BatchUpdatePtr BUI, RootsT &Roots)
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:41
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1199
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
Definition: STLExtras.h:266
static bool IsSameAsFreshTree(const DomTreeT &DT)
static void DeleteEdge(DomTreeT &DT, const BatchUpdatePtr BUI, const NodePtr From, const NodePtr To)
Base class for the actual dominator tree node.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
const std::vector< DomTreeNodeBase * > & getChildren() const
static bool HasForwardSuccessors(const NodePtr N, BatchUpdatePtr BUI)
unsigned runDFS(NodePtr V, unsigned LastNum, DescendCondition Condition, unsigned AttachToNum)
typename DomTreeT::UpdateKind UpdateKind
unsigned getDFSNumIn() const
getDFSNumIn/getDFSNumOut - These return the DFS visitation order for nodes in the dominator tree...
NodeT * getBlock() const
static ResultTy Get(NodePtr N, std::integral_constant< bool, true >)
typename DomTreeT::UpdateType UpdateT
PointerIntPair - This class implements a pair of a pointer and small integer.
std::shared_ptr< Node > NodePtr
Short-hand for a Node pointer.
Definition: MsgPackTypes.h:32
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:148
DomTreeNodeBase * getIDom() const
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:370
static ResultTy Get(NodePtr N, BatchUpdatePtr BUI)
static RootsT FindRoots(const DomTreeT &DT, BatchUpdatePtr BUI)
unsigned getDFSNumOut() const
iterator erase(const_iterator CI)
Definition: SmallVector.h:437
size_t size() const
Definition: SmallVector.h:52
static void DeleteReachable(DomTreeT &DT, const BatchUpdatePtr BUI, const TreeNodePtr FromTN, const TreeNodePtr ToTN)
auto find(R &&Range, const T &Val) -> decltype(adl_begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1206
static bool UpdateRootsBeforeInsertion(DomTreeT &DT, const BatchUpdatePtr BUI, const TreeNodePtr From, const TreeNodePtr To)
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1115
static void ApplyUpdates(DomTreeT &DT, ArrayRef< UpdateT > Updates)
void CalculateWithUpdates(DomTreeT &DT, ArrayRef< typename DomTreeT::UpdateType > Updates)
static void CalculateFromScratch(DomTreeT &DT, BatchUpdatePtr BUI)
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:417
BlockVerifier::State From
size_t getNumChildren() const
static bool HasProperSupport(DomTreeT &DT, const BatchUpdatePtr BUI, const TreeNodePtr TN)
LLVM_NODISCARD T pop_back_val()
Definition: SmallVector.h:373
static bool VerifyDFSNumbers(const DomTreeT &DT)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
Implements a dense probed hash-table based set with some number of buckets stored inline...
Definition: DenseSet.h:267
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:940
static void DeleteUnreachable(DomTreeT &DT, const BatchUpdatePtr BUI, const TreeNodePtr ToTN)
bool Verify(const DomTreeT &DT, typename DomTreeT::VerificationLevel VL)
static bool AlwaysDescend(NodePtr, NodePtr)
friend raw_ostream & operator<<(raw_ostream &O, const BlockNamePrinter &BP)
std::unique_ptr< DomTreeNodeBase > addChild(std::unique_ptr< DomTreeNodeBase > C)
iterator_range< typename GraphTraits< GraphType >::nodes_iterator > nodes(const GraphType &G)
Definition: GraphTraits.h:108
void attachNewSubtree(DomTreeT &DT, const TreeNodePtr AttachTo)
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:55
void reattachExistingSubtree(DomTreeT &DT, const TreeNodePtr AttachTo)
#define I(x, y, z)
Definition: MD5.cpp:58
#define N
static NodePtr GetEntryNode(const DomTreeT &DT)
static void InsertEdge(DomTreeT &DT, const BatchUpdatePtr BUI, const NodePtr From, const NodePtr To)
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition: DenseMap.h:171
static void InsertReachable(DomTreeT &DT, const BatchUpdatePtr BUI, const TreeNodePtr From, const TreeNodePtr To)
static void ComputeUnreachableDominators(DomTreeT &DT, const BatchUpdatePtr BUI, const NodePtr Root, const TreeNodePtr Incoming, SmallVectorImpl< std::pair< NodePtr, TreeNodePtr >> &DiscoveredConnectingEdges)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
unsigned getLevel() const
This file defines a set of templates that efficiently compute a dominator tree over a generic graph...
static void InsertUnreachable(DomTreeT &DT, const BatchUpdatePtr BUI, const TreeNodePtr From, const NodePtr To)
#define LLVM_DEBUG(X)
Definition: Debug.h:122
TreeNodePtr getNodeForBlock(NodePtr BB, DomTreeT &DT)
DenseMap< NodePtr, SmallVector< NodePtrAndKind, 4 > > FutureSuccessors
DenseMap< NodePtr, SmallVector< NodePtrAndKind, 4 > > FuturePredecessors
static ResultTy Get(NodePtr N, std::integral_constant< bool, false >)
void setIDom(DomTreeNodeBase *NewIDom)