|
20 | 20 | /// out that the theoretically slower O(n*log(n)) implementation is actually |
21 | 21 | /// faster than the almost-linear O(n*alpha(n)) version, even for large CFGs. |
22 | 22 | /// |
| 23 | +/// The file uses the Depth Based Search algorithm to perform incremental |
| 24 | +/// upates (insertion and deletions). The implemented algorithm is based on this |
| 25 | +/// publication: |
| 26 | +/// |
| 27 | +/// An Experimental Study of Dynamic Dominators |
| 28 | +/// Loukas Georgiadis, et al., April 12 2016, pp. 5-7, 9-10: |
| 29 | +/// https://arxiv.org/pdf/1604.02711.pdf |
| 30 | +/// |
23 | 31 | //===----------------------------------------------------------------------===// |
24 | 32 |
|
25 | 33 | #ifndef LLVM_SUPPORT_GENERICDOMTREECONSTRUCTION_H |
26 | 34 | #define LLVM_SUPPORT_GENERICDOMTREECONSTRUCTION_H |
27 | 35 |
|
| 36 | +#include <queue> |
28 | 37 | #include "llvm/ADT/DepthFirstIterator.h" |
29 | 38 | #include "llvm/ADT/SmallPtrSet.h" |
30 | 39 | #include "llvm/Support/Debug.h" |
@@ -54,6 +63,7 @@ struct SemiNCAInfo { |
54 | 63 | using NodePtr = typename DomTreeT::NodePtr; |
55 | 64 | using NodeT = typename DomTreeT::NodeType; |
56 | 65 | using TreeNodePtr = DomTreeNodeBase<NodeT> *; |
| 66 | + static constexpr bool IsPostDom = DomTreeT::IsPostDominator; |
57 | 67 |
|
58 | 68 | // Information record used by Semi-NCA during tree construction. |
59 | 69 | struct InfoRec { |
@@ -198,6 +208,7 @@ struct SemiNCAInfo { |
198 | 208 | return VInInfo.Label; |
199 | 209 | } |
200 | 210 |
|
| 211 | + // This function requires DFS to be run before calling it. |
201 | 212 | void runSemiNCA(DomTreeT &DT, const unsigned MinLevel = 0) { |
202 | 213 | const unsigned NextDFSNum(NumToNode.size()); |
203 | 214 | // Initialize IDoms to spanning tree parents. |
@@ -315,6 +326,199 @@ struct SemiNCAInfo { |
315 | 326 | } |
316 | 327 | } |
317 | 328 |
|
| 329 | + // Helper struct used during edge insertions. |
| 330 | + struct InsertionInfo { |
| 331 | + using BucketElementTy = std::pair<unsigned, TreeNodePtr>; |
| 332 | + struct DecreasingLevel { |
| 333 | + bool operator()(const BucketElementTy &First, |
| 334 | + const BucketElementTy &Second) const { |
| 335 | + return First.first > Second.first; |
| 336 | + } |
| 337 | + }; |
| 338 | + |
| 339 | + std::priority_queue<BucketElementTy, SmallVector<BucketElementTy, 8>, |
| 340 | + DecreasingLevel> |
| 341 | + Bucket; // Queue of tree nodes sorted by level in descending order. |
| 342 | + SmallDenseSet<TreeNodePtr, 8> Affected; |
| 343 | + SmallDenseSet<TreeNodePtr, 8> Visited; |
| 344 | + SmallVector<TreeNodePtr, 8> AffectedQueue; |
| 345 | + SmallVector<TreeNodePtr, 8> VisitedNotAffectedQueue; |
| 346 | + }; |
| 347 | + |
| 348 | + static void InsertEdge(DomTreeT &DT, const NodePtr From, const NodePtr To) { |
| 349 | + assert(From && To && "Cannot connect nullptrs"); |
| 350 | + DEBUG(dbgs() << "Inserting edge " << BlockNamePrinter(From) << " -> " |
| 351 | + << BlockNamePrinter(To) << "\n"); |
| 352 | + const TreeNodePtr FromTN = DT.getNode(From); |
| 353 | + |
| 354 | + // Ignore edges from unreachable nodes. |
| 355 | + if (!FromTN) return; |
| 356 | + |
| 357 | + DT.DFSInfoValid = false; |
| 358 | + |
| 359 | + const TreeNodePtr ToTN = DT.getNode(To); |
| 360 | + if (!ToTN) |
| 361 | + InsertUnreachable(DT, FromTN, To); |
| 362 | + else |
| 363 | + InsertReachable(DT, FromTN, ToTN); |
| 364 | + } |
| 365 | + |
| 366 | + // Handles insertion to a node already in the dominator tree. |
| 367 | + static void InsertReachable(DomTreeT &DT, const TreeNodePtr From, |
| 368 | + const TreeNodePtr To) { |
| 369 | + DEBUG(dbgs() << "\tReachable " << BlockNamePrinter(From->getBlock()) |
| 370 | + << " -> " << BlockNamePrinter(To->getBlock()) << "\n"); |
| 371 | + const NodePtr NCDBlock = |
| 372 | + DT.findNearestCommonDominator(From->getBlock(), To->getBlock()); |
| 373 | + assert(NCDBlock || DT.isPostDominator()); |
| 374 | + const TreeNodePtr NCD = DT.getNode(NCDBlock); |
| 375 | + assert(NCD); |
| 376 | + |
| 377 | + DEBUG(dbgs() << "\t\tNCA == " << BlockNamePrinter(NCD) << "\n"); |
| 378 | + const TreeNodePtr ToIDom = To->getIDom(); |
| 379 | + |
| 380 | + // Nothing affected -- NCA property holds. |
| 381 | + // (Based on the lemma 2.5 from the second paper.) |
| 382 | + if (NCD == To || NCD == ToIDom) return; |
| 383 | + |
| 384 | + // Identify and collect affected nodes. |
| 385 | + InsertionInfo II; |
| 386 | + DEBUG(dbgs() << "Marking " << BlockNamePrinter(To) << " as affected\n"); |
| 387 | + II.Affected.insert(To); |
| 388 | + const unsigned ToLevel = To->getLevel(); |
| 389 | + DEBUG(dbgs() << "Putting " << BlockNamePrinter(To) << " into a Bucket\n"); |
| 390 | + II.Bucket.push({ToLevel, To}); |
| 391 | + |
| 392 | + while (!II.Bucket.empty()) { |
| 393 | + const TreeNodePtr CurrentNode = II.Bucket.top().second; |
| 394 | + II.Bucket.pop(); |
| 395 | + DEBUG(dbgs() << "\tAdding to Visited and AffectedQueue: " |
| 396 | + << BlockNamePrinter(CurrentNode) << "\n"); |
| 397 | + II.Visited.insert(CurrentNode); |
| 398 | + II.AffectedQueue.push_back(CurrentNode); |
| 399 | + |
| 400 | + // Discover and collect affected successors of the current node. |
| 401 | + VisitInsertion(DT, CurrentNode, ToLevel, NCD, II); |
| 402 | + } |
| 403 | + |
| 404 | + // Finish by updating immediate dominators and levels. |
| 405 | + UpdateInsertion(DT, NCD, II); |
| 406 | + } |
| 407 | + |
| 408 | + // Visits an affected node and collect its affected successors. |
| 409 | + static void VisitInsertion(DomTreeT &DT, const TreeNodePtr TN, |
| 410 | + const unsigned RootLevel, const TreeNodePtr NCD, |
| 411 | + InsertionInfo &II) { |
| 412 | + const unsigned NCDLevel = NCD->getLevel(); |
| 413 | + DEBUG(dbgs() << "Visiting " << BlockNamePrinter(TN) << "\n"); |
| 414 | + |
| 415 | + assert(TN->getBlock()); |
| 416 | + for (const NodePtr Succ : |
| 417 | + ChildrenGetter<NodePtr, IsPostDom>::Get(TN->getBlock())) { |
| 418 | + const TreeNodePtr SuccTN = DT.getNode(Succ); |
| 419 | + assert(SuccTN && "Unreachable successor found at reachable insertion"); |
| 420 | + const unsigned SuccLevel = SuccTN->getLevel(); |
| 421 | + |
| 422 | + DEBUG(dbgs() << "\tSuccessor " << BlockNamePrinter(Succ) |
| 423 | + << ", level = " << SuccLevel << "\n"); |
| 424 | + |
| 425 | + // Succ dominated by subtree From -- not affected. |
| 426 | + // (Based on the lemma 2.5 from the second paper.) |
| 427 | + if (SuccLevel > RootLevel) { |
| 428 | + DEBUG(dbgs() << "\t\tDominated by subtree From\n"); |
| 429 | + if (II.Visited.count(SuccTN) != 0) continue; |
| 430 | + |
| 431 | + DEBUG(dbgs() << "\t\tMarking visited not affected " |
| 432 | + << BlockNamePrinter(Succ) << "\n"); |
| 433 | + II.Visited.insert(SuccTN); |
| 434 | + II.VisitedNotAffectedQueue.push_back(SuccTN); |
| 435 | + VisitInsertion(DT, SuccTN, RootLevel, NCD, II); |
| 436 | + } else if ((SuccLevel > NCDLevel + 1) && II.Affected.count(SuccTN) == 0) { |
| 437 | + DEBUG(dbgs() << "\t\tMarking affected and adding " |
| 438 | + << BlockNamePrinter(Succ) << " to a Bucket\n"); |
| 439 | + II.Affected.insert(SuccTN); |
| 440 | + II.Bucket.push({SuccLevel, SuccTN}); |
| 441 | + } |
| 442 | + } |
| 443 | + } |
| 444 | + |
| 445 | + // Updates immediate dominators and levels after insertion. |
| 446 | + static void UpdateInsertion(DomTreeT &DT, const TreeNodePtr NCD, |
| 447 | + InsertionInfo &II) { |
| 448 | + DEBUG(dbgs() << "Updating NCD = " << BlockNamePrinter(NCD) << "\n"); |
| 449 | + |
| 450 | + for (const TreeNodePtr TN : II.AffectedQueue) { |
| 451 | + DEBUG(dbgs() << "\tIDom(" << BlockNamePrinter(TN) |
| 452 | + << ") = " << BlockNamePrinter(NCD) << "\n"); |
| 453 | + TN->setIDom(NCD); |
| 454 | + } |
| 455 | + |
| 456 | + UpdateLevelsAfterInsertion(II); |
| 457 | + } |
| 458 | + |
| 459 | + static void UpdateLevelsAfterInsertion(InsertionInfo &II) { |
| 460 | + DEBUG(dbgs() << "Updating levels for visited but not affected nodes\n"); |
| 461 | + |
| 462 | + for (const TreeNodePtr TN : II.VisitedNotAffectedQueue) { |
| 463 | + DEBUG(dbgs() << "\tlevel(" << BlockNamePrinter(TN) << ") = (" |
| 464 | + << BlockNamePrinter(TN->getIDom()) << ") " |
| 465 | + << TN->getIDom()->getLevel() << " + 1\n"); |
| 466 | + TN->UpdateLevel(); |
| 467 | + } |
| 468 | + } |
| 469 | + |
| 470 | + // Handles insertion to previousely unreachable nodes. |
| 471 | + static void InsertUnreachable(DomTreeT &DT, const TreeNodePtr From, |
| 472 | + const NodePtr To) { |
| 473 | + DEBUG(dbgs() << "Inserting " << BlockNamePrinter(From) |
| 474 | + << " -> (unreachable) " << BlockNamePrinter(To) << "\n"); |
| 475 | + |
| 476 | + // Collect discovered edges to already reachable nodes. |
| 477 | + SmallVector<std::pair<NodePtr, TreeNodePtr>, 8> DiscoveredEdgesToReachable; |
| 478 | + // Discover and connect nodes that became reachable with the insertion. |
| 479 | + ComputeUnreachableDominators(DT, To, From, DiscoveredEdgesToReachable); |
| 480 | + |
| 481 | + DEBUG(dbgs() << "Inserted " << BlockNamePrinter(From) |
| 482 | + << " -> (prev unreachable) " << BlockNamePrinter(To) << "\n"); |
| 483 | + |
| 484 | + DEBUG(DT.print(dbgs())); |
| 485 | + |
| 486 | + // Used the discovered edges and inset discovered connecting (incoming) |
| 487 | + // edges. |
| 488 | + for (const auto &Edge : DiscoveredEdgesToReachable) { |
| 489 | + DEBUG(dbgs() << "\tInserting discovered connecting edge " |
| 490 | + << BlockNamePrinter(Edge.first) << " -> " |
| 491 | + << BlockNamePrinter(Edge.second) << "\n"); |
| 492 | + InsertReachable(DT, DT.getNode(Edge.first), Edge.second); |
| 493 | + } |
| 494 | + } |
| 495 | + |
| 496 | + // Connects nodes that become reachable with an insertion. |
| 497 | + static void ComputeUnreachableDominators( |
| 498 | + DomTreeT &DT, const NodePtr Root, const TreeNodePtr Incoming, |
| 499 | + SmallVectorImpl<std::pair<NodePtr, TreeNodePtr>> |
| 500 | + &DiscoveredConnectingEdges) { |
| 501 | + assert(!DT.getNode(Root) && "Root must not be reachable"); |
| 502 | + |
| 503 | + // Visit only previously unreachable nodes. |
| 504 | + auto UnreachableDescender = [&DT, &DiscoveredConnectingEdges](NodePtr From, |
| 505 | + NodePtr To) { |
| 506 | + const TreeNodePtr ToTN = DT.getNode(To); |
| 507 | + if (!ToTN) return true; |
| 508 | + |
| 509 | + DiscoveredConnectingEdges.push_back({From, ToTN}); |
| 510 | + return false; |
| 511 | + }; |
| 512 | + |
| 513 | + SemiNCAInfo SNCA; |
| 514 | + SNCA.runDFS<IsPostDom>(Root, 0, UnreachableDescender, 0); |
| 515 | + SNCA.runSemiNCA(DT); |
| 516 | + SNCA.attachNewSubtree(DT, Incoming); |
| 517 | + |
| 518 | + DEBUG(dbgs() << "After adding unreachable nodes\n"); |
| 519 | + DEBUG(DT.print(dbgs())); |
| 520 | + } |
| 521 | + |
318 | 522 | // Checks if the tree contains all reachable nodes in the input graph. |
319 | 523 | bool verifyReachability(const DomTreeT &DT) { |
320 | 524 | clear(); |
@@ -527,6 +731,13 @@ void Calculate(DomTreeT &DT, FuncT &F) { |
527 | 731 | SNCA.calculateFromScratch(DT, GraphTraits<FuncT *>::size(&F)); |
528 | 732 | } |
529 | 733 |
|
| 734 | +template <class DomTreeT> |
| 735 | +void InsertEdge(DomTreeT &DT, typename DomTreeT::NodePtr From, |
| 736 | + typename DomTreeT::NodePtr To) { |
| 737 | + if (DT.isPostDominator()) std::swap(From, To); |
| 738 | + SemiNCAInfo<DomTreeT>::InsertEdge(DT, From, To); |
| 739 | +} |
| 740 | + |
530 | 741 | template <class DomTreeT> |
531 | 742 | bool Verify(const DomTreeT &DT) { |
532 | 743 | SemiNCAInfo<DomTreeT> SNCA; |
|
0 commit comments