From 7861154e26c764f1c4fd5cb0c862d6e0c87d1fca Mon Sep 17 00:00:00 2001 From: Andy Ayers Date: Thu, 1 Sep 2022 18:55:54 -0700 Subject: [PATCH] JIT: assign plausible loop number to new throw helper blocks These can get added during CSE and may appear in a span of blocks that comprise an existing loop. Annotate the new blocks as belonging to an appropriate loop, even though by rights they don't belong to any loop. Closes #74739. --- src/coreclr/jit/fgbasic.cpp | 52 +++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) 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