Skip to content
Merged
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
3 changes: 1 addition & 2 deletions src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -7065,8 +7065,7 @@ class Compiler

bool optIsLoopTestEvalIntoTemp(Statement* testStmt, Statement** newTestStmt);
unsigned optIsLoopIncrTree(GenTree* incr);
bool optExtractTestIncr(
BasicBlock* bottom, BasicBlock* top, GenTree** ppTest, GenTree** ppIncr);
bool optExtractTestIncr(BasicBlock* cond, GenTree** ppTest, GenTree** ppIncr);

void optSetMappedBlockTargets(BasicBlock* blk,
BasicBlock* newBlk,
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/flowgraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5519,7 +5519,7 @@ bool FlowGraphNaturalLoop::AnalyzeIteration(NaturalLoopIterInfo* info)
}

GenTree* iterTree = nullptr;
if (!comp->optExtractTestIncr(cond, m_header, &test, &iterTree))
if (!comp->optExtractTestIncr(cond, &test, &iterTree))
{
JITDUMP(" Could not extract an IV\n");
continue;
Expand Down
15 changes: 13 additions & 2 deletions src/coreclr/jit/optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,6 @@ bool Compiler::optIsLoopTestEvalIntoTemp(Statement* testStmt, Statement** newTes
//
// Arguments:
// cond - A BBJ_COND block that exits the loop
// header - Loop header block
// ppTest - [out] The test stmt of the loop if found.
// ppIncr - [out] The incr stmt of the loop if found.
//
Expand All @@ -352,7 +351,7 @@ bool Compiler::optIsLoopTestEvalIntoTemp(Statement* testStmt, Statement** newTes
// This method just retrieves what it thinks is the "test" node,
// the callers are expected to verify that "iterVar" is used in the test.
//
bool Compiler::optExtractTestIncr(BasicBlock* cond, BasicBlock* header, GenTree** ppTest, GenTree** ppIncr)
bool Compiler::optExtractTestIncr(BasicBlock* cond, GenTree** ppTest, GenTree** ppIncr)
{
assert(ppTest != nullptr);
assert(ppIncr != nullptr);
Expand Down Expand Up @@ -1839,6 +1838,18 @@ bool Compiler::optTryInvertWhileLoop(FlowGraphNaturalLoop* loop)
return false;
}

// There may be multiple exits, and one of the other exits may also be a
// latch. That latch could be preferable to leave (for example because it
// is an IV test).
NaturalLoopIterInfo iterInfo;
if (loop->AnalyzeIteration(&iterInfo) &&
(iterInfo.TestBlock->TrueTargetIs(loop->GetHeader()) != iterInfo.TestBlock->FalseTargetIs(loop->GetHeader())))
{
// Test block is both a latch and exit, so the loop is already inverted in a preferable way.
JITDUMP("No loop-inversion for " FMT_LP " since it is already inverted (with an IV test)\n", loop->GetIndex());
return false;
}

JITDUMP("Condition in block " FMT_BB " of loop " FMT_LP " is a candidate for duplication to invert the loop\n",
condBlock->bbNum, loop->GetIndex());

Expand Down
Loading