llvm.org GIT mirror llvm / 0128b41
[DomTree] Use a non-recursive DFS instead of a recursive one; NFC Summary: The recursive DFS can stack overflow in pathological cases. Reviewers: kuhar Subscribers: mcrosier, llvm-commits Differential Revision: https://reviews.llvm.org/D36442 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@310383 91177308-0d34-0410-b5e6-96231b3b80d8 Sanjoy Das 2 years ago
1 changed file(s) with 36 addition(s) and 28 deletion(s). Raw diff Collapse all Expand all
432432 const unsigned NCDLevel = NCD->getLevel();
433433 DEBUG(dbgs() << "Visiting " << BlockNamePrinter(TN) << "\n");
434434
435 assert(TN->getBlock());
436 for (const NodePtr Succ :
437 ChildrenGetter::Get(TN->getBlock())) {
438 const TreeNodePtr SuccTN = DT.getNode(Succ);
439 assert(SuccTN && "Unreachable successor found at reachable insertion");
440 const unsigned SuccLevel = SuccTN->getLevel();
441
442 DEBUG(dbgs() << "\tSuccessor " << BlockNamePrinter(Succ)
443 << ", level = " << SuccLevel << "\n");
444
445 // Succ dominated by subtree From -- not affected.
446 // (Based on the lemma 2.5 from the second paper.)
447 if (SuccLevel > RootLevel) {
448 DEBUG(dbgs() << "\t\tDominated by subtree From\n");
449 if (II.Visited.count(SuccTN) != 0) continue;
450
451 DEBUG(dbgs() << "\t\tMarking visited not affected "
452 << BlockNamePrinter(Succ) << "\n");
453 II.Visited.insert(SuccTN);
454 II.VisitedNotAffectedQueue.push_back(SuccTN);
455 VisitInsertion(DT, SuccTN, RootLevel, NCD, II);
456 } else if ((SuccLevel > NCDLevel + 1) && II.Affected.count(SuccTN) == 0) {
457 DEBUG(dbgs() << "\t\tMarking affected and adding "
458 << BlockNamePrinter(Succ) << " to a Bucket\n");
459 II.Affected.insert(SuccTN);
460 II.Bucket.push({SuccLevel, SuccTN});
461 }
462 }
435 SmallVector Stack = {TN};
436 assert(TN->getBlock() && II.Visited.count(TN) && "Preconditions!");
437
438 do {
439 TreeNodePtr Next = Stack.pop_back_val();
440
441 for (const NodePtr Succ :
442 ChildrenGetter::Get(Next->getBlock())) {
443 const TreeNodePtr SuccTN = DT.getNode(Succ);
444 assert(SuccTN && "Unreachable successor found at reachable insertion");
445 const unsigned SuccLevel = SuccTN->getLevel();
446
447 DEBUG(dbgs() << "\tSuccessor " << BlockNamePrinter(Succ)
448 << ", level = " << SuccLevel << "\n");
449
450 // Succ dominated by subtree From -- not affected.
451 // (Based on the lemma 2.5 from the second paper.)
452 if (SuccLevel > RootLevel) {
453 DEBUG(dbgs() << "\t\tDominated by subtree From\n");
454 if (II.Visited.count(SuccTN) != 0)
455 continue;
456
457 DEBUG(dbgs() << "\t\tMarking visited not affected "
458 << BlockNamePrinter(Succ) << "\n");
459 II.Visited.insert(SuccTN);
460 II.VisitedNotAffectedQueue.push_back(SuccTN);
461 Stack.push_back(SuccTN);
462 } else if ((SuccLevel > NCDLevel + 1) &&
463 II.Affected.count(SuccTN) == 0) {
464 DEBUG(dbgs() << "\t\tMarking affected and adding "
465 << BlockNamePrinter(Succ) << " to a Bucket\n");
466 II.Affected.insert(SuccTN);
467 II.Bucket.push({SuccLevel, SuccTN});
468 }
469 }
470 } while (!Stack.empty());
463471 }
464472
465473 // Updates immediate dominators and levels after insertion.