dotnet / dotnet/runtime

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

Open
#125,300 1 comment 0 reactions 0 assignees View on GitHub
area-CodeGen-coreclr help wanted optimization
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Description

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.
```cs
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;
}
```
```assembly
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
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.