Skip to content
Merged
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
69 changes: 31 additions & 38 deletions src/coreclr/jit/fgopt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5285,10 +5285,9 @@ PhaseStatus Compiler::fgHeadTailMerge(bool early)

JITDUMPEXEC(gtDispStmt(matchedPredInfo.TopRef(0).m_stmt));

BasicBlock* crossJumpVictim = nullptr;
Statement* crossJumpStmt = nullptr;
bool haveNoSplitVictim = false;
bool haveFallThroughVictim = false;
BasicBlock* crossJumpVictim = nullptr;
Statement* crossJumpStmt = nullptr;
unsigned bestRank = UINT32_MAX;

for (PredInfo& info : matchedPredInfo.TopDownOrder())
{
Expand All @@ -5302,46 +5301,40 @@ PhaseStatus Compiler::fgHeadTailMerge(bool early)
continue;
}

bool const isNoSplit = stmt == predBlock->firstStmt();
bool const isFallThrough = (predBlock->KindIs(BBJ_ALWAYS) && predBlock->JumpsToNext());
auto getRank = [=]() -> unsigned {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure I understand why you wrapped it to a single-use lambda, not a big fan of this approach.

bool const isNoSplit = stmt == predBlock->firstStmt();
bool const isFallThrough = (predBlock->KindIs(BBJ_ALWAYS) && predBlock->JumpsToNext());

// Is this block possibly better than what we have?
//
bool useBlock = false;

if (crossJumpVictim == nullptr)
{
// Pick an initial candidate.
useBlock = true;
}
else if (isNoSplit && isFallThrough)
{
// This is the ideal choice.
// From most to least preferable.
//
useBlock = true;
}
else if (!haveNoSplitVictim && isNoSplit)
{
useBlock = true;
}
else if (!haveNoSplitVictim && !haveFallThroughVictim && isFallThrough)
{
useBlock = true;
}
if (isNoSplit && isFallThrough)
Comment thread
BoyBaykiller marked this conversation as resolved.
{
return 0;
}
if (isNoSplit)
{
return 1;
}
if (isFallThrough)
{
return 2;
}

return 3;
};

if (useBlock)
unsigned const rank = getRank();
bool pick = rank < bestRank;
if (rank == bestRank)
{
crossJumpVictim = predBlock;
crossJumpStmt = stmt;
haveNoSplitVictim = isNoSplit;
haveFallThroughVictim = isFallThrough;
pick = predBlock->bbID < crossJumpVictim->bbID;
}

// If we have the perfect victim, stop looking.
//
if (haveNoSplitVictim && haveFallThroughVictim)
if (pick)
{
break;
crossJumpVictim = predBlock;
crossJumpStmt = stmt;
bestRank = rank;
}
}

Expand All @@ -5350,7 +5343,7 @@ PhaseStatus Compiler::fgHeadTailMerge(bool early)
// If this block requires splitting, then split it.
// Note we know that stmt has a prev stmt.
//
if (haveNoSplitVictim)
if (crossJumpStmt == crossJumpTarget->firstStmt())
{
JITDUMP("Will cross-jump to " FMT_BB "\n", crossJumpTarget->bbNum);
}
Expand Down
Loading