diff --git a/src/coreclr/jit/fgbasic.cpp b/src/coreclr/jit/fgbasic.cpp index f4e078cbaa1bc2..20c880ddc66646 100644 --- a/src/coreclr/jit/fgbasic.cpp +++ b/src/coreclr/jit/fgbasic.cpp @@ -6495,6 +6495,58 @@ BasicBlock* Compiler::fgNewBBinRegionWorker(BBjumpKinds jumpKind, /* If afterBlk falls through, we insert a jump around newBlk */ fgConnectFallThrough(afterBlk, newBlk->bbNext); + // If the loop table is valid, add this block to the appropriate loop. + // Note we don't verify (via flow) that this block actually belongs + // to the loop, just that it is lexically within the span of blocks + // in the loop. + // + if (optLoopTableValid) + { + BasicBlock* const bbPrev = newBlk->bbPrev; + BasicBlock* const bbNext = newBlk->bbNext; + + if ((bbPrev != nullptr) && (bbNext != nullptr)) + { + BasicBlock::loopNumber const prevLoopNum = bbPrev->bbNatLoopNum; + BasicBlock::loopNumber const nextLoopNum = bbNext->bbNatLoopNum; + + if ((prevLoopNum != BasicBlock::NOT_IN_LOOP) && (nextLoopNum != BasicBlock::NOT_IN_LOOP)) + { + if (prevLoopNum == nextLoopNum) + { + newBlk->bbNatLoopNum = prevLoopNum; + } + else + { + BasicBlock::loopNumber const prevParentLoopNum = optLoopTable[prevLoopNum].lpParent; + BasicBlock::loopNumber const nextParentLoopNum = optLoopTable[nextLoopNum].lpParent; + + if (nextParentLoopNum == prevLoopNum) + { + // next is in child loop + newBlk->bbNatLoopNum = prevLoopNum; + } + else if (prevParentLoopNum == nextLoopNum) + { + // prev is in child loop + newBlk->bbNatLoopNum = nextLoopNum; + } + else + { + // next and prev are siblings + assert(prevParentLoopNum == nextParentLoopNum); + newBlk->bbNatLoopNum = prevParentLoopNum; + } + } + } + } + + if (newBlk->bbNatLoopNum != BasicBlock::NOT_IN_LOOP) + { + JITDUMP("Marked " FMT_BB " as lying within " FMT_LP "\n", newBlk->bbNum, newBlk->bbNatLoopNum); + } + } + #ifdef DEBUG fgVerifyHandlerTab(); #endif