-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JIT Optimization: Merge multiple inc
into one add
#65025
Comments
Tagging subscribers to this area: @JulieLeeMSFT Issue DetailsThe codegen of B is better (less cpu cycles). public int A(int a) {
a++; // inc edx
a++; // inc edx
return a;
}
public int B(int a) {
a += 2; // add edx, 2
return a;
} Expected result- inc edx
- inc edx
+ add edx, 2
mov eax, edx
ret
<table>
<tr>
<th align="left">Author:</th>
<td>deeprobin</td>
</tr>
<tr>
<th align="left">Assignees:</th>
<td>-</td>
</tr>
<tr>
<th align="left">Labels:</th>
<td>
`area-CodeGen-coreclr`, `untriaged`
</td>
</tr>
<tr>
<th align="left">Milestone:</th>
<td>-</td>
</tr>
</table>
</details> |
This does not seem to be what C++ compilers do: https://godbolt.org/z/r1f7Pxdfa |
@danmoseley |
@omariom thanks, I thought that was default. |
Same for
|
IMO this should be generalized to all operators. Interestingly, copying to temporary variables will result in optimal code, so maybe this is related to SSA/constant propagation: public int M(int x) {
x *= 7;
x *= 7;
return x;
}
public int M2(int x) {
int t1 = x * 7;
int t2 = t1 * 7;
return t2;
} C.M(Int32)
L0000: imul edx, 7
L0003: imul edx, 7
L0006: mov eax, edx
L0008: ret
C.M2(Int32)
L0000: imul eax, edx, 0x31
L0003: ret |
cc @dotnet/jit-contrib |
Note forward sub (#63720) does not handle cases where a local has multiple definitions and uses, even if each definition has just one use. It might not be too hard to extend it to cover these cases. Eg if we run it again once SSA is built and track the number of uses of each SSA def. |
https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA0AXEBLANgHwAEAmARgFgAoQgZgAIS6BhOgbyrs4fuwDsM6AQQAUfAQEMAlGw5c54gNQKA3LLmdFKtesIB2OuNWU5AX23badMXQBCo/gentj6jXQUBeOsSOvOeg18uM0oTIA
The codegen of B is better (less cpu cycles).
Expected result
The text was updated successfully, but these errors were encountered: