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
15 changes: 8 additions & 7 deletions src/coreclr/jit/optimizebools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1698,18 +1698,19 @@ bool Compiler::fgFoldCondToReturnBlock(BasicBlock* block)
}

// Both edges must be BBJ_RETURN
BasicBlock* retFalseBb = block->GetFalseTarget();
BasicBlock* retTrueBb = block->GetTrueTarget();
BasicBlock* const retFalseBb = block->GetFalseTarget();
BasicBlock* const retTrueBb = block->GetTrueTarget();

// Although, we might want to fold fallthrough BBJ_ALWAYS blocks first
if (fgCanCompactBlock(retTrueBb))
// We might want to compact BBJ_ALWAYS blocks first,
// but don't compact the conditional block away in the process
if (fgCanCompactBlock(retTrueBb) && !retTrueBb->TargetIs(block))
{
fgCompactBlock(retTrueBb);
modified = true;
}
// By the time we get to the retFalseBb, it might be removed by fgCompactBlock()
// so we need to check if it is still valid.
if (!retFalseBb->HasFlag(BBF_REMOVED) && fgCanCompactBlock(retFalseBb))
if (!retFalseBb->HasFlag(BBF_REMOVED) && fgCanCompactBlock(retFalseBb) && !retFalseBb->TargetIs(block))
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.

presumably you also don't need !retFalseBb->HasFlag(BBF_REMOVED) check

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 think we still need this too in the case of the diamond shape: If the true target jumps to the false target and we compact them, retFalseBb will be removed from the flowgraph. This check currently defends against this case.

{
fgCompactBlock(retFalseBb);
modified = true;
Expand All @@ -1720,8 +1721,8 @@ bool Compiler::fgFoldCondToReturnBlock(BasicBlock* block)
return modified;
}

retTrueBb = block->GetTrueTarget();
retFalseBb = block->GetFalseTarget();
assert(block->TrueTargetIs(retTrueBb));
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.

there is Same here - bail out if the block is no longer BBJ_COND after compacting. comment with a condition that you can remove I presume

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 don't think we can remove this: Suppose the true target jumps to the false target, and we compact the false target into the true target. This will make the conditional block degenerate, since it now branches to the same target in either case. fgReplaceJumpTarget will detect this, and convert the block into a BBJ_ALWAYS, so I think we need to check for this case in the caller.

assert(block->FalseTargetIs(retFalseBb));
if (!retTrueBb->KindIs(BBJ_RETURN) || !retFalseBb->KindIs(BBJ_RETURN) ||
!BasicBlock::sameEHRegion(block, retTrueBb) || !BasicBlock::sameEHRegion(block, retFalseBb) ||
(retTrueBb == genReturnBB) || (retFalseBb == genReturnBB))
Expand Down
Loading