Skip to content

JIT generates redundant mov for inline ++i (pre-increment) #129532

@rameel

Description

@rameel

https://godbolt.org/z/aYMq87E9G

// Simplified repro
public static int N(ReadOnlySpan<char> s, int i)
{
    while (true)
    {
        if ((uint)++i >= (uint)s.Length)
            break;

        if (s[i] == '?')
            return i;
    }

    return -1;
}

Generated ASM:

lea      eax, [rdx+0x01]
mov      edx, eax          ; redundant
cmp      edx, esi

With a separate increment:

++i;
if ((uint)i >= (uint)s.Length) 
    break;

Generated ASM:

inc      edx
cmp      edx, esi

.NET 8.0 behavior: both variants generate identical inc; cmp code.

Issue:

  • Inline ++i adds an extra mov, increasing register pressure
  • Causes spills in register-constrained methods
  • Larger binary size

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIuntriagedNew issue has not been triaged by the area owner

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions