Skip to content

Loop inversion should consider top-tested loops #50204

@BruceForstall

Description

@BruceForstall

Compiler::optInvertWhileLoop() contains the check:

if (fgIsForwardBranch(block) == false)
{
    return;
}

This check is potentially expensive (it iterates over the block list because block numbers are not ordered when this is called).

It is also potentially unnecessary.

Without this check, the loop inversion code works just fine. Instead of the expected pattern, though, it turns top-tested, top-entry loops with an unconditional back-edge to top-tested, top-entry loops with a duplicated condition at the bottom and thus a conditional back-edge. So, it reduces the branches in a loop from 2 to 1, at the cost of the duplicated condition.

There are diffs when removing this check, so the code pattern does exist.

Investigate whether removing this check and enabling this transformation is worthwhile.

category:cq
theme:loop-opt
skill-level:intermediate
cost:small
impact:small

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIin-prThere is an active PR which will close this issue when it is merged

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions