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