Repository navigation
[arm64] JIT: X % 2 == 0 -> X & 1 == 0 - #62399
Conversation
|
Tagging subscribers to this area: @JulieLeeMSFT Issue DetailsOn arm64 we transfrom The same transformation in LLVM looks way smaller 🙂 https://github.com/llvm/llvm-project/blob/main/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp#L1372-L1381 void Test(int x)
{
if (x % 2 == 0)
Console.WriteLine("x is even");
}; Method Program:Test(int):this
G_M52364_IG01:
stp fp, lr, [sp,#-16]!
mov fp, sp
;; bbWeight=1 PerfScore 1.50
G_M52364_IG02:
- lsr w0, w1, #31
- add w0, w0, w1
- asr w0, w0, #1
- lsl w0, w0, #1
- subs w0, w1, w0
- bne G_M52364_IG05
+ tbnz w1, #0, G_M52364_IG05
;; bbWeight=1 PerfScore 1.00
G_M52364_IG03:
movz x0, #0xd1ffab1e
movk x0, #0xd1ffab1e LSL #16
movk x0, #0xd1ffab1e LSL #32
ldr x0, [x0]
;; bbWeight=0.50 PerfScore 2.25
G_M52364_IG04:
ldp fp, lr, [sp],#16
b System.Console:WriteLine(System.String)
;; bbWeight=0.50 PerfScore 1.00
G_M52364_IG05:
ldp fp, lr, [sp],#16
ret lr
;; bbWeight=0.50 PerfScore 1.00
G_M52364_IG06:
bl CORINFO_HELP_OVERFLOW
;; bbWeight=0 PerfScore 0.00
G_M52364_IG07:
bl CORINFO_HELP_THROWDIVZERO
brk_windows #0
;; bbWeight=0 PerfScore 0.00
-; Total bytes of code: 76
+; Total bytes of code: 56
|
|
No diffs on x64. |
|
According to this link: https://stackoverflow.com/questions/3072665/bitwise-and-in-place-of-modulus-operator It seems like this can be expanded to other forms of modulus as well. |
the current algorithm handles any power-of-two number, the title is slightly misleading yes |
Move
X % 2 == 0 -> X & 1 == 0transformation to pre-order in morph - it helps ARM64 back-end to recognize it too before it gets rid of GT_MOD (thanks @SingleAccretion for the idea).; Method Program:Test(int):this G_M52364_IG01: stp fp, lr, [sp,#-16]! mov fp, sp ;; bbWeight=1 PerfScore 1.50 G_M52364_IG02: - lsr w0, w1, #31 - add w0, w0, w1 - asr w0, w0, #1 - lsl w0, w0, #1 - subs w0, w1, w0 - bne G_M52364_IG05 + tbnz w1, #0, G_M52364_IG05 ;; bbWeight=1 PerfScore 1.00 G_M52364_IG03: movz x0, #0xd1ffab1e movk x0, #0xd1ffab1e LSL #16 movk x0, #0xd1ffab1e LSL #32 ldr x0, [x0] ;; bbWeight=0.50 PerfScore 2.25 G_M52364_IG04: ldp fp, lr, [sp],#16 b System.Console:WriteLine(System.String) ;; bbWeight=0.50 PerfScore 1.00 G_M52364_IG05: ldp fp, lr, [sp],#16 ret lr ;; bbWeight=0.50 PerfScore 1.00 -G_M52364_IG06: - bl CORINFO_HELP_OVERFLOW - ;; bbWeight=0 PerfScore 0.00 - -G_M52364_IG07: - bl CORINFO_HELP_THROWDIVZERO - brk_windows #0 - ;; bbWeight=0 PerfScore 0.00 -; Total bytes of code: 76 +; Total bytes of code: 44benchmarks.run.windows.arm64.checked.mch:
Detail diffs
coreclr_tests.pmi.windows.arm64.checked.mch:
Detail diffs
libraries.crossgen2.windows.arm64.checked.mch:
Detail diffs
libraries.pmi.windows.arm64.checked.mch:
Detail diffs
libraries_tests.pmi.windows.arm64.checked.mch:
Detail diffs