Skip to content

JIT: Simplify arithmetic operations on memory, as is already done for register #125300

Open

Description

@BoyBaykiller

I expected the reg and mem variant to have same codegen, https://godbolt.org/z/MGzTd3s3c
JitOptRepeat seems to help a little for SubMem only.

static int AddReg(int mem)
{
    int a = mem;
    int b = mem;
    int c = mem;
    int r = a + b + c;
    return r;
}

static int AddMem(ref int mem)
{
    int a = mem;
    int b = mem;
    int c = mem;
    int r = a + b + c;
    return r;
}

static int SubReg(int mem)
{
    int a = mem;
    int b = mem;
    int c = mem;
    int r = a - b - c;
    return r;
}

static int SubMem(ref int mem)
{
    int a = mem;
    int b = mem;
    int c = mem;
    int r = a - b - c;
    return r;
}
Program:AddReg(int):int (FullOpts):
       lea      eax, [rdi+2*rdi]
       ret      

Program:AddMem(byref):int (FullOpts):
       mov      eax, dword ptr [rdi]
       lea      ecx, [rax+rax]
       add      eax, ecx
       ret      

Program:SubReg(int):int (FullOpts):
       xor      eax, eax
       sub      eax, edi
       ret      

Program:SubMem(byref):int (FullOpts):
       mov      eax, dword ptr [rdi]
       mov      ecx, eax
       sub      ecx, eax
       sub      ecx, eax
       mov      eax, ecx
       ret      

Activity

  1. added
    untriaged
    on Mar 8, 2026
  2. added
    area-CodeGen-coreclr
    on Mar 8, 2026
  3. dotnet-policy-service commented on Mar 8, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
    See info in area-owners.md if you want to be subscribed.

  4. added
    help wanted
    and removed
    untriaged
    on Mar 13, 2026
  5. added this to the Future milestone on Mar 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclrhelp wantedoptimization

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions