1

I was playing around with JMH, and under different load Math.max(int, int) from same JVM (OpenJDK 13) compiled to two different implementations (both were emitted by C2):

call handling code (stack and base pointers setting/restoration, etc.) omitted

  0x00007f8ce456cd8c:   cmp    %edx,%esi
  0x00007f8ce456cd8e:   mov    %esi,%eax
  0x00007f8ce456cd90:   cmovl  %edx,%eax                    ;*ireturn {reexecute=0 rethrow=0 return_oop=0}
                                                            ; - java.lang.Math::max@10 (line 1441)

some call handling code present due to nature of jumps

  0x00007fdda859270c:   cmp    %edx,%esi
  0x00007fdda859270e:   jl     0x00007fdda8592722           ;*if_icmplt {reexecute=0 rethrow=0 return_oop=0}
                                                            ; - java.lang.Math::max@2 (line 1441)
  0x00007fdda8592710:   mov    %esi,%eax                    ;*ireturn {reexecute=0 rethrow=0 return_oop=0}
                                                            ; - java.lang.Math::max@10 (line 1441)
  0x00007fdda8592712:   add    $0x10,%rsp
  0x00007fdda8592716:   pop    %rbp
  0x00007fdda8592717:   mov    0x108(%r15),%r10
  0x00007fdda859271e:   test   %eax,(%r10)                  ;   {poll_return}
  0x00007fdda8592721:   retq   
  0x00007fdda8592722:   mov    %edx,%eax
  0x00007fdda8592724:   jmp    0x00007fdda8592712

I used -XX:+UnlockDiagnosticVMOptions -XX:+PrintIntrinsics as well, and Math.max is printed out.

I was sure that intrinsic means static (i.e. it's always the same) C++ implementation for specific method from JRE, however, given the output above I'm confused. I know that intrinsics can be described as "optimization technique that provides custom, more performant implementation for standard methods", but what exactly is intrinsic (let's restrict domain by HotSpot JVM only)? Is it static code that is compiled with JVM and shouldn't differ on same architecture? Is it some kind of IR that replaces original one? Is it optimized bytecode, that again replaces whatever is source code compiled to? Is it any of the above depending on particular method?

Etki
  • 2,042
  • 2
  • 17
  • 40
  • I don't know enough about the topic to write a proper answer, but I've dug around and found [this method](https://github.com/openjdk/jdk13/blob/ecc58444df373d9e78eb7d6ade450f4559980139/src/hotspot/share/opto/library_call.cpp) which *looks to me* as if it's generating intermediate-level opcodes for an inlined `Math.max` (and `Math.min`) call based on the surrounding use of the value. As such it's clearly much more powerful than a simple "textual" drop-in implementation of a given function. – Joachim Sauer Dec 19 '19 at 12:49
  • Intrinsic only means well known and handled specifically, regardless of how. By I don’t see why you assume that intrinsic handling is involved. I see `java.lang.Math::max@2 (line 1441)` and `java.lang.Math::max@10 (line 1441)` clearly referring to bytecode positions 2 and 10, and even their mapping to the source code line 1441. Looks like ordinary bytecode to native code translation. – Holger Dec 19 '19 at 14:38
  • @Holger i guess that's because i'm pretty much dumb and made some assumptions without any grounds for them (when i've first seen v1 implementation, i thought that was manually created branching evasion). It's not clear why then it's compiled by C2 at all then. – Etki Dec 19 '19 at 15:26

0 Answers0