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
62 changes: 31 additions & 31 deletions src/coreclr/jit/fgopt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5457,48 +5457,48 @@ PhaseStatus Compiler::fgHeadTailMerge(bool early)
}
};

ArrayStack<BasicBlock*> retOrThrowBlocks(getAllocator(CMK_ArrayStack));

// Visit each block
// Tail merge predecessors
//
for (BasicBlock* const block : Blocks())
{
iterateTailMerge(block);
if (block->isEmpty())
{
continue;
}
}

if (block->KindIs(BBJ_THROW))
{
retOrThrowBlocks.Push(block);
}
else if (block->KindIs(BBJ_RETURN) && (block != genReturnBB))
// Deduplicate RETURN blocks
//
do
{
predInfo.Reset();
for (BasicBlock* const block : Blocks())
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.

The set of eligible return and throw blocks never changes, so do we need to repeatedly walk the entire block list here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I would also think we don't need to, however when I tried to hoist that it caused asserts.
I didn't look further into it because the same approach is already done in iterateTailMerge() and there are also multiple comments arround this code about improving algorithm efficiency.
So I'd prefer properly understanding the entire code and improving efficiency in a separate PR, in the future.

{
// Avoid splitting a return away from a possible tail call
//
if (!block->hasSingleStmt())
if (block->isEmpty())
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This check was here before, but I dont think we actually need it. Because we only accept RETURN or THROW blocks and these should never be empty?

{
continue;
}

if (block->KindIs(BBJ_THROW))
{
predInfo.Push(PredInfo(block, block->lastStmt()));
}
else if (block->KindIs(BBJ_RETURN) && (block != genReturnBB))
{
Statement* const lastStmt = block->lastStmt();
Statement* const prevStmt = lastStmt->GetPrevStmt();
GenTree* const prevTree = prevStmt->GetRootNode();
if (prevTree->IsCall() && prevTree->AsCall()->CanTailCall())
// Avoid splitting a return away from a possible tail call
//
if (!block->hasSingleStmt())
{
continue;
Statement* const lastStmt = block->lastStmt();
Statement* const prevStmt = lastStmt->GetPrevStmt();
GenTree* const prevTree = prevStmt->GetRootNode();
if (prevTree->IsCall() && prevTree->AsCall()->CanTailCall())
{
continue;
}
}
}

retOrThrowBlocks.Push(block);
predInfo.Push(PredInfo(block, block->lastStmt()));
}
}
}

predInfo.Reset();
for (BasicBlock* const block : retOrThrowBlocks.BottomUpOrder())
{
predInfo.Push(PredInfo(block, block->lastStmt()));
}

tailMergePreds(nullptr);
} while (tailMergePreds(nullptr));

// Work through any retries
//
Expand Down
Loading