我想了解從Java循環生成的本機代碼。本地代碼應該由C2編譯器進行優化,但在我的簡單示例中,似乎缺少一些優化。什麼時候由C2 JIT編譯器觸發Java循環預測優化?
這是Java方法我寫基座上的https://wiki.openjdk.java.net/display/HotSpot/LoopPredication最小例如:
104 public static byte[] myLoop(int init, int limit, int stride, int scale, int offset, byte value, byte[] array) {
105 for (int i = init; i < limit; i += stride) {
106 array [ scale * i + offset] = value;
107 }
108 return array;
109 }
這些都是給定的Java 8熱點VM迫使C2編譯的參數:
-server
-XX:-TieredCompilation
-XX:CompileThreshold=5
-XX:+UnlockDiagnosticVMOptions
-XX:+PrintAssembly
-XX:-UseCompressedOops
-XX:+LogCompilation
-XX:+TraceClassLoading
-XX:+UseLoopPredicate
-XX:+RangeCheckElimination
這是由C2生成的amd64本地代碼('myLoop'被稱爲至少10000次):
# {method} {0x00007fcb5088ef38} 'myLoop' '(IIIIIB[B)[B' in 'MyClass'
# parm0: rsi = int
# parm1: rdx = int
# parm2: rcx = int
# parm3: r8 = int
# parm4: r9 = int
# parm5: rdi = byte
# parm6: [sp+0x40] = '[B' (sp of caller)
0x00007fcd44ee9fe0: mov %eax,0xfffffffffffec000(%rsp)
0x00007fcd44ee9fe7: push %rbp
0x00007fcd44ee9fe8: sub $0x30,%rsp ;*synchronization entry
; - MyClass::[email protected] (line 105)
0x00007fcd44ee9fec: cmp %edx,%esi
0x00007fcd44ee9fee: jnl 0x7fcd44eea04a ;*if_icmplt
; - MyClass::[email protected] (line 105)
0x00007fcd44ee9ff0: mov 0x40(%rsp),%rax
0x00007fcd44ee9ff5: mov 0x10(%rax),%r10d ;*bastore
; - MyClass::[email protected] (line 106)
; implicit exception: dispatches to 0x00007fcd44eea051
0x00007fcd44ee9ff9: nopl 0x0(%rax) ;*aload
; - MyClass::[email protected] (line 106)
0x00007fcd44eea000: mov %esi,%ebx
0x00007fcd44eea002: imull %r8d,%ebx
0x00007fcd44eea006: add %r9d,%ebx ;*iadd
; - MyClass::[email protected] (line 106)
0x00007fcd44eea009: cmp %r10d,%ebx
0x00007fcd44eea00c: jnb 0x7fcd44eea02e ;*bastore
; - MyClass::[email protected] (line 106)
0x00007fcd44eea00e: add %ecx,%esi ;*iadd
; - MyClass::[email protected] (line 105)
0x00007fcd44eea010: movsxd %ebx,%r11
0x00007fcd44eea013: mov %dil,0x18(%rax,%r11) ; OopMap{rax=Oop off=56}
;*if_icmplt
; - MyClass::[email protected] (line 105)
0x00007fcd44eea018: test %eax,0xa025fe2(%rip) ; {poll}
0x00007fcd44eea01e: cmp %edx,%esi
0x00007fcd44eea020: jl 0x7fcd44eea000 ;*synchronization entry
; - MyClass::[email protected] (line 105)
0x00007fcd44eea022: add $0x30,%rsp
0x00007fcd44eea026: pop %rbp
0x00007fcd44eea027: test %eax,0xa025fd3(%rip) ; {poll_return}
0x00007fcd44eea02d: retq
0x00007fcd44eea02e: movabs $0x7fcca3c810a8,%rsi ; {oop(a 'java/lang/ArrayIndexOutOfBoundsException')}
0x00007fcd44eea038: movq $0x0,0x18(%rsi) ;*bastore
; - MyClass::[email protected] (line 106)
0x00007fcd44eea040: add $0x30,%rsp
0x00007fcd44eea044: pop %rbp
0x00007fcd44eea045: jmpq 0x7fcd44e529a0 ; {runtime_call}
0x00007fcd44eea04a: mov 0x40(%rsp),%rax
0x00007fcd44eea04f: jmp 0x7fcd44eea022
0x00007fcd44eea051: mov %edx,%ebp
0x00007fcd44eea053: mov %ecx,0x40(%rsp)
0x00007fcd44eea057: mov %r8d,0x44(%rsp)
0x00007fcd44eea05c: mov %r9d,(%rsp)
0x00007fcd44eea060: mov %edi,0x4(%rsp)
0x00007fcd44eea064: mov %rax,0x8(%rsp)
0x00007fcd44eea069: mov %esi,0x10(%rsp)
0x00007fcd44eea06d: mov $0xffffff86,%esi
0x00007fcd44eea072: nop
0x00007fcd44eea073: callq 0x7fcd44dea1a0 ; OopMap{[8]=Oop off=152}
;*aload
; - MyClass::[email protected] (line 106)
; {runtime_call}
0x00007fcd44eea078: callq 0x7fcd4dc47c50 ;*aload
; - MyClass::[email protected] (line 106)
; {runtime_call}
0x00007fcd44eea07d: hlt
0x00007fcd44eea07e: hlt
0x00007fcd44eea07f: hlt
根據https://wiki.openjdk.java.net/display/HotSpot/LoopPredication,一個稱爲「數組範圍消除」的優化消除了循環內的數組範圍檢查,但在循環之前添加了循環謂詞。看來這個優化還沒有在C2的'myLoop'上完成。循環的向後跳轉位於0x7fcd44eea020並跳回到0x7fcd44eea000。在循環內還有一個範圍檢查0x7fcd44eea009-0x7fcd44eea00c。
- 爲什麼仍然有循環檢查?
- 爲什麼循環預測優化未運行?
- 如何強制所有優化?
此外,您不需要指定'UseLoopPredicate'和'RangeCheckElimination'標誌 - 它們在默認情況下處於啓用狀態。 – apangin