@@ -843,6 +843,7 @@ void SingleExitLoopTransformer::ensureSingleExitBlock() {
843
843
// Update loop info if this belongs to a parent loop.
844
844
SILLoop *outsideBlockLoop = LI->getLoopFor (outsideBlock);
845
845
if (outsideBlockLoop == nullptr ) {
846
+ // outsideBlock is not part of any other loop. Simply add it to our loop.
846
847
loop->addBasicBlockToLoop (outsideBlock, LI->getBase ());
847
848
} else {
848
849
// We deal with the case where the nodes being moved in
@@ -854,13 +855,17 @@ void SingleExitLoopTransformer::ensureSingleExitBlock() {
854
855
// }
855
856
// }
856
857
if (outsideBlockLoop->contains (loop)) {
857
- // `loop` is nested within `outsideBlockLoop`. Move the node from
858
- // `outsideBlockLoop` into our `loop`.
858
+ // If our `loop` is nested within `outsideBlockLoop`. Move the node
859
+ // from `outsideBlockLoop` into our `loop`.
859
860
outsideBlockLoop->removeBlockFromLoop (outsideBlock);
860
861
LI->changeLoopFor (outsideBlock, nullptr );
861
862
loop->addBasicBlockToLoop (outsideBlock, LI->getBase ());
862
863
} else {
864
+ // We should only nest `outsideBlockLoop` into our `loop` when we
865
+ // process the very first node of the `outsideBlockLoop`. Check that we
866
+ // have not already nested the `outsideBlockLoop` into our `loop`.
863
867
if (!loop->contains (outsideBlockLoop)) {
868
+ // Not yet nested, adjust the LoopInfo w.r.t nesting.
864
869
if (outsideBlockLoop->getParentLoop () == nullptr ) {
865
870
// Remove from top-level loops as we are nesting it in `loop`.
866
871
LI->removeLoop (llvm::find (*LI, outsideBlockLoop));
@@ -877,7 +882,6 @@ void SingleExitLoopTransformer::ensureSingleExitBlock() {
877
882
// top-level loop is already correct.
878
883
}
879
884
}
880
-
881
885
if (cloner.hasCloned ()) {
882
886
// TODO(https://bugs.swift.org/browse/SR-8336): the transformations here are
883
887
// simple that we should be able to incrementally update the DI & PDI.
0 commit comments