Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
30 changes: 30 additions & 0 deletions src/coreclr/jit/fgbasic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6723,6 +6723,36 @@ 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)
{
EHblkDsc* ehDsc = ehGetDsc(tryIndex);
BasicBlock* const oldTryLast = ehDsc->ebdTryLast;
BasicBlock* const newBlock = fgNewBBafter(jumpKind, oldTryLast, /* extendRegion */ false);
newBlock->setTryIndex(tryIndex);
newBlock->clearHndIndex();

for (EHblkDsc* const HBtab : EHClauses(this))
{
if (HBtab->ebdTryLast == oldTryLast)
{
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