arrays - When is Java loop predication optimization triggered by the C2 JIT compiler? -


i trying understand native code generated java loop. native code should optimized c2 compiler, on simple example seems optimizations missing.

this java method wrote base on minimal example of 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     (int = init; < limit; += stride) { 106         array [ scale * + offset] = value; 107     } 108     return array; 109    } 

these arguments given java 8 hotspot vm force c2 compilation:

-server -xx:-tieredcompilation -xx:compilethreshold=5 -xx:+unlockdiagnosticvmoptions  -xx:+printassembly -xx:-usecompressedoops -xx:+logcompilation -xx:+traceclassloading -xx:+uselooppredicate -xx:+rangecheckelimination 

this amd64 native code generated c2 ('myloop' called @ least 10000 times):

  # {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::myloop@-1 (line 105)    0x00007fcd44ee9fec: cmp     %edx,%esi   0x00007fcd44ee9fee: jnl     0x7fcd44eea04a    ;*if_icmplt                                                 ; - myclass::myloop@27 (line 105)    0x00007fcd44ee9ff0: mov     0x40(%rsp),%rax   0x00007fcd44ee9ff5: mov     0x10(%rax),%r10d  ;*bastore                                                 ; - myclass::myloop@17 (line 106)                                                 ; implicit exception: dispatches 0x00007fcd44eea051   0x00007fcd44ee9ff9: nopl    0x0(%rax)         ;*aload                                                 ; - myclass::myloop@6 (line 106)    0x00007fcd44eea000: mov     %esi,%ebx   0x00007fcd44eea002: imull   %r8d,%ebx   0x00007fcd44eea006: add     %r9d,%ebx         ;*iadd                                                 ; - myclass::myloop@14 (line 106)    0x00007fcd44eea009: cmp     %r10d,%ebx   0x00007fcd44eea00c: jnb     0x7fcd44eea02e    ;*bastore                                                 ; - myclass::myloop@17 (line 106)    0x00007fcd44eea00e: add     %ecx,%esi         ;*iadd                                                 ; - myclass::myloop@21 (line 105)    0x00007fcd44eea010: movsxd  %ebx,%r11   0x00007fcd44eea013: mov     %dil,0x18(%rax,%r11)  ; oopmap{rax=oop off=56}                                                 ;*if_icmplt                                                 ; - myclass::myloop@27 (line 105)    0x00007fcd44eea018: test    %eax,0xa025fe2(%rip)  ;   {poll}   0x00007fcd44eea01e: cmp     %edx,%esi   0x00007fcd44eea020: jl      0x7fcd44eea000    ;*synchronization entry                                                 ; - myclass::myloop@-1 (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::myloop@17 (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::myloop@6 (line 106)                                                 ;   {runtime_call}   0x00007fcd44eea078: callq   0x7fcd4dc47c50    ;*aload                                                 ; - myclass::myloop@6 (line 106)                                                 ;   {runtime_call}   0x00007fcd44eea07d: hlt   0x00007fcd44eea07e: hlt   0x00007fcd44eea07f: hlt 

according https://wiki.openjdk.java.net/display/hotspot/looppredication, 1 optimization, called "array range elimination", eliminates array range checks within loop adds loop predicate before loop. seems optimization has not been done on 'myloop' c2. loop's backward jump @ 0x7fcd44eea020 , jumps 0x7fcd44eea000. within loop there still range check @ 0x7fcd44eea009-0x7fcd44eea00c.

  1. why there still check in loop?
  2. why has loop predication optimization not been run?
  3. how can force optimizations?

the explanation right there on same page:

from above example, requirements perform loop predication array range check elimination init, limit, offset , array loop invariants, , stride , scale compile time constants.

in example scale , stride not compile time constants, optimization fails.

however, if call method constant arguments, hotspot able eliminate range checks due inling , constant propagation optimizations.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -