@@ -640,46 +640,29 @@ bool swift::splitAllCondBrCriticalEdgesWithNonTrivialArgs(SILFunction &Fn,
640
640
return true ;
641
641
}
642
642
643
- namespace {
644
- class RemoveUnreachable {
645
- SILFunction &Fn;
646
- llvm::SmallSet<SILBasicBlock *, 8 > Visited;
647
- public:
648
- RemoveUnreachable (SILFunction &Fn) : Fn(Fn) { }
649
- void visit (SILBasicBlock *BB);
650
- bool run ();
651
- };
652
- } // end anonymous namespace
653
-
654
- void RemoveUnreachable::visit (SILBasicBlock *BB) {
655
- if (!Visited.insert (BB).second )
656
- return ;
643
+ bool swift::removeUnreachableBlocks (SILFunction &Fn) {
644
+ // All reachable blocks, but does not include the entry block.
645
+ llvm::SmallPtrSet<SILBasicBlock *, 8 > Visited;
657
646
658
- for (auto &Succ : BB->getSuccessors ())
659
- visit (Succ);
660
- }
647
+ // Walk over the CFG, starting at the entry block, until all reachable blocks are visited.
648
+ llvm::SmallVector<SILBasicBlock *, 8 > Worklist (1 , Fn.getEntryBlock ());
649
+ while (!Worklist.empty ()) {
650
+ SILBasicBlock *BB = Worklist.pop_back_val ();
651
+ for (auto &Succ : BB->getSuccessors ()) {
652
+ if (Visited.insert (Succ).second )
653
+ Worklist.push_back (Succ);
654
+ }
655
+ }
661
656
662
- bool RemoveUnreachable::run () {
657
+ // Remove the blocks we never reached. Exclude the entry block from the iteration because it's
658
+ // not included in the Visited set.
663
659
bool Changed = false ;
664
-
665
- // Clear each time we run so that we can run multiple times.
666
- Visited.clear ();
667
-
668
- // Visit all blocks reachable from the entry block of the function.
669
- visit (&*Fn.begin ());
670
-
671
- // Remove the blocks we never reached.
672
- for (auto It = Fn.begin (), End = Fn.end (); It != End; ) {
660
+ for (auto It = std::next (Fn.begin ()), End = Fn.end (); It != End; ) {
673
661
auto *BB = &*It++;
674
662
if (!Visited.count (BB)) {
675
663
removeDeadBlock (BB);
676
664
Changed = true ;
677
665
}
678
666
}
679
-
680
667
return Changed;
681
668
}
682
-
683
- bool swift::removeUnreachableBlocks (SILFunction &Fn) {
684
- return RemoveUnreachable (Fn).run ();
685
- }
0 commit comments