Skip to content
Open
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
55 changes: 48 additions & 7 deletions src/coreclr/jit/fgopt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5212,6 +5212,23 @@ PhaseStatus Compiler::fgHeadTailMerge(bool early)

fgUnlinkStmt(predBlock, stmt);

bool canRemove =
predBlock->isEmpty() && !predBlock->HasFlag(BBF_DONT_REMOVE) && predBlock != fgFirstBB;
if (canRemove)
{
for (BasicBlock* const pred : predBlock->PredBlocksEditing())
{
fgReplaceJumpTarget(pred, predBlock, commSucc);
}

fgRemoveBlock(predBlock, true);

if (commSucc->hasProfileWeight())
{
commSucc->increaseBBProfileWeight(predBlock->bbWeight);
}
}

// Add one of the matching stmts to block, and
// update its flags.
//
Expand Down Expand Up @@ -5344,15 +5361,32 @@ PhaseStatus Compiler::fgHeadTailMerge(bool early)

// Fix up the flow.
//
if (commSucc != nullptr)
bool canRemove = predBlock->isEmpty() && !predBlock->HasFlag(BBF_DONT_REMOVE) && predBlock != fgFirstBB;
if (canRemove)
{
assert(predBlock->KindIs(BBJ_ALWAYS));
fgRedirectEdge(predBlock->TargetEdgeRef(), crossJumpTarget);
for (BasicBlock* const pred : predBlock->PredBlocksEditing())
{
fgReplaceJumpTarget(pred, predBlock, crossJumpTarget);
}
fgRemoveBlock(predBlock, true);

if (commSucc != nullptr && commSucc->hasProfileWeight())
{
commSucc->increaseBBProfileWeight(predBlock->bbWeight);
}
}
else
{
FlowEdge* const newEdge = fgAddRefPred(crossJumpTarget, predBlock);
predBlock->SetKindAndTargetEdge(BBJ_ALWAYS, newEdge);
if (commSucc != nullptr)
{
assert(predBlock->KindIs(BBJ_ALWAYS));
fgRedirectEdge(predBlock->TargetEdgeRef(), crossJumpTarget);
}
else
{
FlowEdge* const newEdge = fgAddRefPred(crossJumpTarget, predBlock);
predBlock->SetKindAndTargetEdge(BBJ_ALWAYS, newEdge);
}
}

// For tail merge we have a common successor of predBlock and
Expand Down Expand Up @@ -5402,6 +5436,13 @@ PhaseStatus Compiler::fgHeadTailMerge(bool early)
continue;
}

// If this block was already processed, skip it
//
if (predBlock->isEmpty())
{
continue;
}

Statement* lastStmt = predBlock->lastStmt();

// Block might be empty.
Expand Down Expand Up @@ -5498,9 +5539,9 @@ PhaseStatus Compiler::fgHeadTailMerge(bool early)
predInfo.Reset();
for (BasicBlock* const block : retOrThrowBlocks.BottomUpOrder())
{
// If this block was already merged, skip it
// If this block was already processed, skip it
//
if (!block->KindIs(BBJ_RETURN, BBJ_THROW))
if (!block->KindIs(BBJ_RETURN, BBJ_THROW) || block->isEmpty())
{
continue;
}
Expand Down
Loading