Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JIT: Simplify block insertion logic during loop canonicalization #107371

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -5316,6 +5316,8 @@ class Compiler
unsigned xcptnIndex,
bool putInTryRegion);

BasicBlock* fgNewBBatTryRegionEnd(BBKinds jumpKind, unsigned tryIndex);

void fgInsertBBbefore(BasicBlock* insertBeforeBlk, BasicBlock* newBlk);
void fgInsertBBafter(BasicBlock* insertAfterBlk, BasicBlock* newBlk);
void fgUnlinkBlock(BasicBlock* block);
Expand Down
33 changes: 33 additions & 0 deletions src/coreclr/jit/fgbasic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6723,6 +6723,39 @@ BasicBlock* Compiler::fgNewBBinRegionWorker(BBKinds jumpKind,
return newBlk;
}

//-----------------------------------------------------------------------------
// fgNewBBatTryRegionEnd: Creates and inserts a new block at the end of the specified
// try region, updating the try end pointers in the EH table as necessary.
//
// Arguments:
// jumpKind - The jump kind of the new block
// tryIndex - The index of the try region to insert the new block in
//
// Returns:
// The new block
//
BasicBlock* Compiler::fgNewBBatTryRegionEnd(BBKinds jumpKind, unsigned tryIndex)
{
BasicBlock* const oldTryLast = ehGetDsc(tryIndex)->ebdTryLast;
BasicBlock* const newBlock = fgNewBBafter(jumpKind, oldTryLast, /* extendRegion */ false);
newBlock->setTryIndex(tryIndex);
newBlock->clearHndIndex();

// Update this try region's (and all parent try regions') last block pointer
//
for (unsigned XTnum = tryIndex; XTnum < compHndBBtabCount; XTnum++)
{
EHblkDsc* const HBtab = ehGetDsc(XTnum);
if (HBtab->ebdTryLast == oldTryLast)
{
assert((XTnum == tryIndex) || (ehGetDsc(tryIndex)->ebdEnclosingTryIndex != EHblkDsc::NO_ENCLOSING_INDEX));
fgSetTryEnd(HBtab, newBlock);
}
}

return newBlock;
}

//------------------------------------------------------------------------
// fgUseThrowHelperBlocks: Determinate does compiler use throw helper blocks.
//
Expand Down
26 changes: 5 additions & 21 deletions src/coreclr/jit/optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3024,16 +3024,10 @@ bool Compiler::optCreatePreheader(FlowGraphNaturalLoop* loop)
}
}

BasicBlock* insertBefore = loop->GetLexicallyTopMostBlock();
if (!BasicBlock::sameEHRegion(insertBefore, header))
{
insertBefore = header;
}

BasicBlock* preheader = fgNewBBbefore(BBJ_ALWAYS, insertBefore, false);
BasicBlock* preheader = fgNewBBbefore(BBJ_ALWAYS, header, false);
preheader->SetFlags(BBF_INTERNAL);
fgSetEHRegionForNewPreheaderOrExit(preheader);
preheader->bbCodeOffs = insertBefore->bbCodeOffs;
preheader->bbCodeOffs = header->bbCodeOffs;

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

Expand Down Expand Up @@ -3136,21 +3130,11 @@ bool Compiler::optCanonicalizeExit(FlowGraphNaturalLoop* loop, BasicBlock* exit)
{
// Branches to a BBJ_CALLFINALLY _must_ come from inside its associated
// try region, and when we have callfinally thunks the BBJ_CALLFINALLY
// is outside it. First try to see if the lexically bottom most block
// is part of the try; if so, inserting after that is a good choice.
// is outside it. Thus, insert newExit at the end of the finally's
// try region.
BasicBlock* finallyBlock = exit->GetTarget();
assert(finallyBlock->hasHndIndex());
BasicBlock* bottom = loop->GetLexicallyBottomMostBlock();
if (bottom->hasTryIndex() && (bottom->getTryIndex() == finallyBlock->getHndIndex()) && !bottom->hasHndIndex())
{
newExit = fgNewBBafter(BBJ_ALWAYS, bottom, true);
}
else
{
// Otherwise just do the heavy-handed thing and insert it anywhere in the right region.
newExit = fgNewBBinRegion(BBJ_ALWAYS, finallyBlock->bbHndIndex, 0, nullptr, /* putInFilter */ false,
/* runRarely */ false, /* insertAtEnd */ true);
}
newExit = fgNewBBatTryRegionEnd(BBJ_ALWAYS, finallyBlock->getHndIndex());
}
else
{
Expand Down
Loading