-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Signed division performed even when values are provably non-negative #94218
Comments
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch Issue DetailsRepro: static int Result(int i)
{
if (i < 4) return 0;
return i / 4;
} This results in: ; Method Program:<<Main>$>g__Result|0_0(int):int (FullOpts)
G_M000_IG01: ;; offset=0x0000
G_M000_IG02: ;; offset=0x0000
cmp ecx, 4
jge SHORT G_M000_IG05
G_M000_IG03: ;; offset=0x0005
xor eax, eax
G_M000_IG04: ;; offset=0x0007
ret
G_M000_IG05: ;; offset=0x0008
mov eax, ecx
sar eax, 31
and eax, 3
add eax, ecx
sar eax, 2
G_M000_IG06: ;; offset=0x0015
ret
; Total bytes of code: 22 but as the JIT can prove that the static int Result(int i)
{
if (i < 4) return 0;
return (int)((uint)i / 4);
} were instead used? And it results in: ; Method Program:<<Main>$>g__Result|0_0(int):int (FullOpts)
G_M000_IG01: ;; offset=0x0000
G_M000_IG02: ;; offset=0x0000
cmp ecx, 4
jge SHORT G_M000_IG05
G_M000_IG03: ;; offset=0x0005
xor eax, eax
G_M000_IG04: ;; offset=0x0007
ret
G_M000_IG05: ;; offset=0x0008
mov eax, ecx
shr eax, 2
G_M000_IG06: ;; offset=0x000D
ret
; Total bytes of code: 14
|
As just an observer here, it seems JIT can prove the sign of |
It's already supposed to: runtime/src/coreclr/jit/morph.cpp Lines 8510 to 8517 in f2e4ddf
|
|
While #94347 fixes your example, we might still have a few cases where we won't optimize, e.g. when JIT expands |
Repro:
This results in:
but as the JIT can prove that the
i
used ini / 4
isn't negative, so presumably it should be able to emit the code as if:were instead used? And it results in:
The text was updated successfully, but these errors were encountered: