Skip to content

Commit d9e7f2a

Browse files
jakobbotschmichaelgsharp
authored andcommitted
JIT: Avoid relying on bbNum to find lexical loop boundaries (dotnet#101714)
Switch `FlowGraphNaturalLoop::GetLexicallyTopMostBlock` and `FlowGraphNaturalLoop::GetLexicallyBottomMostBlock` to more robust implementations that scan the basic block list forwards (and backwards) to find the boundary blocks. Fix dotnet#101695
1 parent bba287a commit d9e7f2a

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

src/coreclr/jit/flowgraph.cpp

+16-14
Original file line numberDiff line numberDiff line change
@@ -5555,20 +5555,21 @@ bool FlowGraphNaturalLoop::InitBlockEntersLoopOnTrue(BasicBlock* initBlock)
55555555
// the loop.
55565556
//
55575557
// Returns:
5558-
// Block with highest bbNum.
5558+
// First block in block order contained in the loop.
55595559
//
55605560
// Remarks:
55615561
// Mostly exists as a quirk while transitioning from the old loop
55625562
// representation to the new one.
55635563
//
55645564
BasicBlock* FlowGraphNaturalLoop::GetLexicallyTopMostBlock()
55655565
{
5566-
BasicBlock* top = m_header;
5567-
VisitLoopBlocks([&top](BasicBlock* loopBlock) {
5568-
if (loopBlock->bbNum < top->bbNum)
5569-
top = loopBlock;
5570-
return BasicBlockVisit::Continue;
5571-
});
5566+
BasicBlock* top = m_dfsTree->GetCompiler()->fgFirstBB;
5567+
5568+
while (!ContainsBlock(top))
5569+
{
5570+
top = top->Next();
5571+
assert(top != nullptr);
5572+
}
55725573

55735574
return top;
55745575
}
@@ -5578,20 +5579,21 @@ BasicBlock* FlowGraphNaturalLoop::GetLexicallyTopMostBlock()
55785579
// within the loop.
55795580
//
55805581
// Returns:
5581-
// Block with highest bbNum.
5582+
// Last block in block order contained in the loop.
55825583
//
55835584
// Remarks:
55845585
// Mostly exists as a quirk while transitioning from the old loop
55855586
// representation to the new one.
55865587
//
55875588
BasicBlock* FlowGraphNaturalLoop::GetLexicallyBottomMostBlock()
55885589
{
5589-
BasicBlock* bottom = m_header;
5590-
VisitLoopBlocks([&bottom](BasicBlock* loopBlock) {
5591-
if (loopBlock->bbNum > bottom->bbNum)
5592-
bottom = loopBlock;
5593-
return BasicBlockVisit::Continue;
5594-
});
5590+
BasicBlock* bottom = m_dfsTree->GetCompiler()->fgLastBB;
5591+
5592+
while (!ContainsBlock(bottom))
5593+
{
5594+
bottom = bottom->Prev();
5595+
assert(bottom != nullptr);
5596+
}
55955597

55965598
return bottom;
55975599
}

0 commit comments

Comments
 (0)