Today the JIT can replace loop tests by downwards counted ones that do not require an additional compare after a decrement. We can more generally have linear function test replacement that tries to rephrase the loop test in terms of other existing IVs.
One example that would benefit is the one in https://github.com/dotnet/runtime/issues/102068; after implementing #105241 we will end up in a situation where we have an IV with step = 2 that the loop test can be phrased in terms of.
That is, the codegen for
public static void Foo()
{
string puzzle = "003020600900305001001806400008102900700000008006708200002609500800203009005010300";
int[] board = new int[81];
for (int i = 0; i < puzzle.Length; i++)
{
board[i] = puzzle[i] - '0';
}
}
xor ecx, ecx
LoopStart:
mov r8, 0x160D4E52C30 ; '003020600900305001001806400008102900700000008006708200002609500'
movzx r8, word ptr [r8+rdx+0x0C]
add r8d, -48
mov dword ptr [rax+2*rdx+0x10], r8d
add edx, 2
cmp edx, 162 ; Linear function test replacement rephrased the test in terms of the IV that counts by 2 every iteration
jl LoopStart
Today the JIT can replace loop tests by downwards counted ones that do not require an additional compare after a decrement. We can more generally have linear function test replacement that tries to rephrase the loop test in terms of other existing IVs. One example that would benefit is the one in https://github.com/dotnet/runtime/issues/102068; after implementing #105241 we will end up in a situation where we have an IV with step = 2 that the loop test can be phrased in terms of.
That is, the codegen for
today looks like
but it can become something like