Skip to content

JIT: Move loop inversion to after loop recognition #115850

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions src/coreclr/jit/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4642,10 +4642,6 @@ void Compiler::compCompile(void** methodCodePtr, uint32_t* methodCodeSize, JitFl
//
DoPhase(this, PHASE_EMPTY_TRY_CATCH_FAULT_2, &Compiler::fgRemoveEmptyTryCatchOrTryFault);

// Invert loops
//
DoPhase(this, PHASE_INVERT_LOOPS, &Compiler::optInvertLoops);

// Run some flow graph optimizations (but don't reorder)
//
DoPhase(this, PHASE_OPTIMIZE_FLOW, &Compiler::optOptimizeFlow);
Expand All @@ -4668,6 +4664,10 @@ void Compiler::compCompile(void** methodCodePtr, uint32_t* methodCodeSize, JitFl
//
DoPhase(this, PHASE_REPAIR_PROFILE_POST_MORPH, &Compiler::fgRepairProfile);

// Invert loops
//
DoPhase(this, PHASE_INVERT_LOOPS, &Compiler::optInvertLoops);

// Scale block weights and mark run rarely blocks.
//
DoPhase(this, PHASE_SET_BLOCK_WEIGHTS, &Compiler::optSetBlockWeights);
Expand Down
22 changes: 22 additions & 0 deletions src/coreclr/jit/optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2322,6 +2322,20 @@ bool Compiler::optInvertWhileLoop(BasicBlock* block)
}
#endif // DEBUG

// Removing flow into 'bTest' may have made it possible to compact it.
// Compact 'bTest' now so we don't pessimize pattern-based IV analysis.
BasicBlock* const uniquePred = bTest->GetUniquePred(this);
if ((uniquePred != nullptr) && fgCanCompactBlock(uniquePred))
{
JITDUMP(FMT_BB " can now be compacted into its remaining predecessor.\n", bTest->bbNum);
fgCompactBlock(uniquePred);
}
// If we redirected all flow into 'bTest', remove it.
else if (bTest->bbPreds == nullptr)
{
fgRemoveBlock(bTest, /* unreachable */ true);
}

Metrics.LoopsInverted++;
return true;
}
Expand Down Expand Up @@ -2370,6 +2384,14 @@ PhaseStatus Compiler::optInvertLoops()
}
}

if (Metrics.LoopsInverted > 0)
{
assert(madeChanges);
fgInvalidateDfsTree();
m_dfsTree = fgComputeDfs();
optFindLoops();
}

return madeChanges ? PhaseStatus::MODIFIED_EVERYTHING : PhaseStatus::MODIFIED_NOTHING;
}

Expand Down
Loading