dotnet / dotnet/runtime

JIT: Suboptimal codegen for simple `CONST - VAR`

Open
#126,442 6 comments 0 reactions 1 assignee Claimed by @BoyBaykiller View on GitHub
area-CodeGen-coreclr
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Description

https://godbolt.org/z/x3e349nEW

```cs
uint ConstSubVar(uint x)
{
return 32 - x;
}
```
```assembly
Program:ConstSubVar(uint):uint:this (FullOpts):
mov eax, esi
neg eax
add eax, 32
ret
```
Should be:
```assembly
Program:ConstSubVar(uint):uint:this (FullOpts):
mov eax, 32
sub eax, esi
ret
```
We canonicalize into `(cns1 + (-op2))` in morph:
https://github.com/dotnet/runtime/blob/209c66f5f1fcc497a07d1a40f82056ff5995272c/src/coreclr/jit/morph.cpp#L7890-L7920

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.