Sum of integers optimization
Ionut
ionutb83 at yahoo.com
Tue Oct 31 21:59:07 UTC 2017
Hello All,
I am playing with below example (very trivial, just computing a sum of 1...N integers):
@Benchmarkpublic long sum() { long sum = 0; for (int i = 1; i <= N; i++) { sum += i; } return sum;}
Generated asm on my machine (snapshot from the main scalar loop): ...................................................... ↗ 0x00007f4779bff060: movsxd r10,r11d │ 0x00007f4779bff063: add rax,r10
7.67% 24.83% │ 0x00007f4779bff066: add rax,r10 6.11% 3.64% │ 0x00007f4779bff069: add rax,r10 4.54% 3.71% │ 0x00007f4779bff06c: add rax,r10 6.12% 5.85% │ 0x00007f4779bff06f: add rax,r10 5.75% 4.21% │ 0x00007f4779bff072: add rax,r10 5.96% 4.38% │ 0x00007f4779bff075: add rax,r10 4.23% 3.63% │ 0x00007f4779bff078: add rax,r10 6.70% 6.32% │ 0x00007f4779bff07b: add rax,r10 7.40% 4.56% │ 0x00007f4779bff07e: add rax,r10 4.61% 3.31% │ 0x00007f4779bff081: add rax,r10 5.45% 5.24% │ 0x00007f4779bff084: add rax,r10 5.99% 5.14% │ 0x00007f4779bff087: add rax,r10 7.70% 5.36% │ 0x00007f4779bff08a: add rax,r10 5.17% 4.16% │ 0x00007f4779bff08d: add rax,r10 3.97% 3.83% │ 0x00007f4779bff090: add rax,r10 4.80% 3.97% │ 0x00007f4779bff093: add rax,0x78 5.92% 5.97% │ 0x00007f4779bff097: add r11d,0x10 0.01% │ 0x00007f4779bff09b: cmp r11d,0x5f5e0f2
╰ 0x00007f4779bff0a2: jl 0x00007f4779bff060 ......................................................
Questions: - Would it be possible for JIT C2 to perform a better optimization in this context, as for example replacing the main loop (which might be costly) by a reduction formula as N*(N-1)/2 (in this specific case)? - Is there any context where JIT C2 can perform such optimization but I am missing? - If not, what prevents it for doing this?
ThanksIonut
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/hotspot-compiler-dev/attachments/20171031/6d6450be/attachment-0001.html>
More information about the hotspot-compiler-dev
mailing list