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