Skip to content

Commit b523ec5

Browse files
JIT: Simplify block insertion logic during loop canonicalization (dotnet#107371)
1 parent c4792a2 commit b523ec5

File tree

3 files changed

+40
-21
lines changed

3 files changed

+40
-21
lines changed

src/coreclr/jit/compiler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5316,6 +5316,8 @@ class Compiler
53165316
unsigned xcptnIndex,
53175317
bool putInTryRegion);
53185318

5319+
BasicBlock* fgNewBBatTryRegionEnd(BBKinds jumpKind, unsigned tryIndex);
5320+
53195321
void fgInsertBBbefore(BasicBlock* insertBeforeBlk, BasicBlock* newBlk);
53205322
void fgInsertBBafter(BasicBlock* insertAfterBlk, BasicBlock* newBlk);
53215323
void fgUnlinkBlock(BasicBlock* block);

src/coreclr/jit/fgbasic.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6723,6 +6723,39 @@ BasicBlock* Compiler::fgNewBBinRegionWorker(BBKinds jumpKind,
67236723
return newBlk;
67246724
}
67256725

6726+
//-----------------------------------------------------------------------------
6727+
// fgNewBBatTryRegionEnd: Creates and inserts a new block at the end of the specified
6728+
// try region, updating the try end pointers in the EH table as necessary.
6729+
//
6730+
// Arguments:
6731+
// jumpKind - The jump kind of the new block
6732+
// tryIndex - The index of the try region to insert the new block in
6733+
//
6734+
// Returns:
6735+
// The new block
6736+
//
6737+
BasicBlock* Compiler::fgNewBBatTryRegionEnd(BBKinds jumpKind, unsigned tryIndex)
6738+
{
6739+
BasicBlock* const oldTryLast = ehGetDsc(tryIndex)->ebdTryLast;
6740+
BasicBlock* const newBlock = fgNewBBafter(jumpKind, oldTryLast, /* extendRegion */ false);
6741+
newBlock->setTryIndex(tryIndex);
6742+
newBlock->clearHndIndex();
6743+
6744+
// Update this try region's (and all parent try regions') last block pointer
6745+
//
6746+
for (unsigned XTnum = tryIndex; XTnum < compHndBBtabCount; XTnum++)
6747+
{
6748+
EHblkDsc* const HBtab = ehGetDsc(XTnum);
6749+
if (HBtab->ebdTryLast == oldTryLast)
6750+
{
6751+
assert((XTnum == tryIndex) || (ehGetDsc(tryIndex)->ebdEnclosingTryIndex != EHblkDsc::NO_ENCLOSING_INDEX));
6752+
fgSetTryEnd(HBtab, newBlock);
6753+
}
6754+
}
6755+
6756+
return newBlock;
6757+
}
6758+
67266759
//------------------------------------------------------------------------
67276760
// fgUseThrowHelperBlocks: Determinate does compiler use throw helper blocks.
67286761
//

src/coreclr/jit/optimizer.cpp

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2891,16 +2891,10 @@ bool Compiler::optCreatePreheader(FlowGraphNaturalLoop* loop)
28912891
}
28922892
}
28932893

2894-
BasicBlock* insertBefore = loop->GetLexicallyTopMostBlock();
2895-
if (!BasicBlock::sameEHRegion(insertBefore, header))
2896-
{
2897-
insertBefore = header;
2898-
}
2899-
2900-
BasicBlock* preheader = fgNewBBbefore(BBJ_ALWAYS, insertBefore, false);
2894+
BasicBlock* preheader = fgNewBBbefore(BBJ_ALWAYS, header, false);
29012895
preheader->SetFlags(BBF_INTERNAL);
29022896
fgSetEHRegionForNewPreheaderOrExit(preheader);
2903-
preheader->bbCodeOffs = insertBefore->bbCodeOffs;
2897+
preheader->bbCodeOffs = header->bbCodeOffs;
29042898

29052899
JITDUMP("Created new preheader " FMT_BB " for " FMT_LP "\n", preheader->bbNum, loop->GetIndex());
29062900

@@ -3003,21 +2997,11 @@ bool Compiler::optCanonicalizeExit(FlowGraphNaturalLoop* loop, BasicBlock* exit)
30032997
{
30042998
// Branches to a BBJ_CALLFINALLY _must_ come from inside its associated
30052999
// try region, and when we have callfinally thunks the BBJ_CALLFINALLY
3006-
// is outside it. First try to see if the lexically bottom most block
3007-
// is part of the try; if so, inserting after that is a good choice.
3000+
// is outside it. Thus, insert newExit at the end of the finally's
3001+
// try region.
30083002
BasicBlock* finallyBlock = exit->GetTarget();
30093003
assert(finallyBlock->hasHndIndex());
3010-
BasicBlock* bottom = loop->GetLexicallyBottomMostBlock();
3011-
if (bottom->hasTryIndex() && (bottom->getTryIndex() == finallyBlock->getHndIndex()) && !bottom->hasHndIndex())
3012-
{
3013-
newExit = fgNewBBafter(BBJ_ALWAYS, bottom, true);
3014-
}
3015-
else
3016-
{
3017-
// Otherwise just do the heavy-handed thing and insert it anywhere in the right region.
3018-
newExit = fgNewBBinRegion(BBJ_ALWAYS, finallyBlock->bbHndIndex, 0, nullptr, /* putInFilter */ false,
3019-
/* runRarely */ false, /* insertAtEnd */ true);
3020-
}
3004+
newExit = fgNewBBatTryRegionEnd(BBJ_ALWAYS, finallyBlock->getHndIndex());
30213005
}
30223006
else
30233007
{

0 commit comments

Comments
 (0)