Java jvm jit公共子表达式消除

Java jvm jit公共子表达式消除,java,jvm,jit,Java,Jvm,Jit,使用测试代码测试以下代码段 如果我理解程序集正确,则不会对a.test(x)执行公共子表达式消除(CSE)。我猜直接原因是两个调用使用了不同的寄存器,从而阻止(阻碍)JIT执行CSE test仅仅是纯方法的一个例子,它没有什么有趣的作用。我的意图是,test(x)对于一些x来说足够昂贵,因此CSE将是有益的 我想知道在本例或其他类似场景中是否有任何方法可以显式启用CSE。或者CSE如何在JVM的JIT中工作 环境: openjdk版本“9-internal” OpenJDK运行时环境(build

使用测试代码测试以下代码段

如果我理解程序集正确,则不会对
a.test(x)
执行公共子表达式消除(CSE)。我猜直接原因是两个调用使用了不同的寄存器,从而阻止(阻碍)JIT执行CSE

test
仅仅是纯方法的一个例子,它没有什么有趣的作用。我的意图是,
test(x)
对于一些
x
来说足够昂贵,因此CSE将是有益的

我想知道在本例或其他类似场景中是否有任何方法可以显式启用CSE。或者CSE如何在JVM的JIT中工作

环境: openjdk版本“9-internal” OpenJDK运行时环境(build 9-internal+0-2016-04-14-195246.buildd.src)
OpenJDK 64位服务器虚拟机(build 9-internal+0-2016-04-14-195246.buildd.src,混合模式)

首先,您看到了错误的方法。这不是在热路径上执行的
MyBenchmark.testMethod
内联到基准循环中,大部分时间花在JMH生成的方法中,如

org.sample.generated.MyBenchmark_testMethod_jmhTest::testMethod_avgt_jmhStub
您可以通过使用
-prof perfasm
选项运行JMH来检查它


无论如何,您猜对了,在给定的示例中CSE不起作用。但这并不是因为不同的寄存器(在大多数独立于机器的优化之后,寄存器分配执行得比较晚),而是因为这里的控制流太复杂了。通常,热点中的CSE不适用于具有圈(即循环)的子图

我想知道在这种情况下是否有办法显式启用CSE,或者 其他类似情况

当然,通过手动操作,也就是说,通过将方法结果缓存在一个临时变量中。HotSpot在检测常见的子表达式方面不是很聪明:例如,
s*y+x
y*s+x
被视为不同的表达式,但是您可以通过像中那样重写代码来帮助JIT

ImmutableOopMap{}pc offsets: 762 772 800 Compiled method (c2)     397  551       4       org.sample.MyBenchmark::testMethod (32 bytes)
 total in heap  [0x00007f96dd74bd90,0x00007f96dd74c170] = 992
 relocation     [0x00007f96dd74bed0,0x00007f96dd74bee0] = 16
 main code      [0x00007f96dd74bee0,0x00007f96dd74bfa0] = 192
 stub code      [0x00007f96dd74bfa0,0x00007f96dd74bfb8] = 24
 oops           [0x00007f96dd74bfb8,0x00007f96dd74bfc0] = 8
 metadata       [0x00007f96dd74bfc0,0x00007f96dd74bfd0] = 16
 scopes data    [0x00007f96dd74bfd0,0x00007f96dd74c038] = 104
 scopes pcs     [0x00007f96dd74c038,0x00007f96dd74c168] = 304
 dependencies   [0x00007f96dd74c168,0x00007f96dd74c170] = 8
----------------------------------------------------------------------
org/sample/MyBenchmark.testMethod()I  [0x00007f96dd74bee0, 0x00007f96dd74bfb8]  216 bytes
Argument 0 is unknown.RIP: 0x7f96dd74bee0 Code size: 0x000000d8
[Entry Point]
[Constants]
  # {method} {0x00007f95ed0efe80} 'testMethod' '()I' in 'org/sample/MyBenchmark'
  #           [sp+0x20]  (sp of caller)
  0x00007f96dd74bee0: cmp     0x8(%rsi),%rax
  0x00007f96dd74bee4: jne     0x7f96d5c99c60    ;   {runtime_call ic_miss_stub}
  0x00007f96dd74beea: nop
  0x00007f96dd74beec: nopl    0x0(%rax)
[Verified Entry Point]
  0x00007f96dd74bef0: mov     %eax,0xfffffffffffec000(%rsp)
  0x00007f96dd74bef7: push    %rbp
  0x00007f96dd74bef8: sub     $0x10,%rsp        ;*synchronization entry
                                                ; - org.sample.MyBenchmark::testMethod@-1 (line 66)

  0x00007f96dd74befc: mov     0x10(%rsi),%r11d  ;*getfield x {reexecute=0 rethrow=0 return_oop=0}
                                                ; - org.sample.MyBenchmark::testMethod@8 (line 67)

  0x00007f96dd74bf00: mov     0x18(%rsi),%r10
  0x00007f96dd74bf04: test    %r10,%r10
  0x00007f96dd74bf07: je      0x7f96dd74bf73    ;*invokevirtual test {reexecute=0 rethrow=0 return_oop=0}
                                                ; - org.sample.MyBenchmark::testMethod@11 (line 67)

  0x00007f96dd74bf09: xor     %eax,%eax
  0x00007f96dd74bf0b: test    %r11d,%r11d
  0x00007f96dd74bf0e: jle     0x7f96dd74bf67    ;*if_icmpge {reexecute=0 rethrow=0 return_oop=0}
                                                ; - org.sample.A::test@8 (line 46)
                                                ; - org.sample.MyBenchmark::testMethod@11 (line 67)

  0x00007f96dd74bf10: xor     %r10d,%r10d
  0x00007f96dd74bf13: xor     %r9d,%r9d
  0x00007f96dd74bf16: xor     %r8d,%r8d
  0x00007f96dd74bf19: mov     $0x1,%edi         ;*ishl {reexecute=0 rethrow=0 return_oop=0}
                                                ; - org.sample.A::test@18 (line 47)
                                                ; - org.sample.MyBenchmark::testMethod@11 (line 67)

  0x00007f96dd74bf1e: movsxd  %edi,%rcx
  0x00007f96dd74bf21: add     %rcx,%r8          ;*ladd {reexecute=0 rethrow=0 return_oop=0}
                                                ; - org.sample.A::test@20 (line 47)
                                                ; - org.sample.MyBenchmark::testMethod@11 (line 67)

  0x00007f96dd74bf24: incl    %r9d              ;*iinc {reexecute=0 rethrow=0 return_oop=0}
                                                ; - org.sample.A::test@22 (line 46)
                                                ; - org.sample.MyBenchmark::testMethod@11 (line 67)

  0x00007f96dd74bf27: cmp     %r11d,%r9d
  0x00007f96dd74bf2a: jnl     0x7f96dd74bf3b    ;*if_icmpge {reexecute=0 rethrow=0 return_oop=0}
                                                ; - org.sample.A::test@8 (line 46)
                                                ; - org.sample.MyBenchmark::testMethod@11 (line 67)

  0x00007f96dd74bf2c: mov     %r9d,%ecx
  0x00007f96dd74bf2f: and     $0x1f,%ecx
  0x00007f96dd74bf32: mov     $0x1,%edi
  0x00007f96dd74bf37: shl     %cl,%edi          ;*ishl {reexecute=0 rethrow=0 return_oop=0}
                                                ; - org.sample.A::test@18 (line 47)
                                                ; - org.sample.MyBenchmark::testMethod@11 (line 67)

  0x00007f96dd74bf39: jmp     0x7f96dd74bf1e
  0x00007f96dd74bf3b: mov     $0x1,%r9d         ;*ishl {reexecute=0 rethrow=0 return_oop=0}
                                                ; - org.sample.A::test@18 (line 47)
                                                ; - org.sample.MyBenchmark::testMethod@25 (line 68)

  0x00007f96dd74bf41: movsxd  %r9d,%r9
  0x00007f96dd74bf44: add     %r9,%r10          ;*ladd {reexecute=0 rethrow=0 return_oop=0}
                                                ; - org.sample.A::test@20 (line 47)
                                                ; - org.sample.MyBenchmark::testMethod@25 (line 68)

  0x00007f96dd74bf47: incl    %eax              ;*iinc {reexecute=0 rethrow=0 return_oop=0}
                                                ; - org.sample.A::test@22 (line 46)
                                                ; - org.sample.MyBenchmark::testMethod@25 (line 68)

  0x00007f96dd74bf49: cmp     %r11d,%eax
  0x00007f96dd74bf4c: jnl     0x7f96dd74bf5e    ;*if_icmpge {reexecute=0 rethrow=0 return_oop=0}
                                                ; - org.sample.A::test@8 (line 46)
                                                ; - org.sample.MyBenchmark::testMethod@25 (line 68)

  0x00007f96dd74bf4e: mov     %eax,%ecx
  0x00007f96dd74bf50: and     $0x1f,%ecx
  0x00007f96dd74bf53: mov     $0x1,%r9d
  0x00007f96dd74bf59: shl     %cl,%r9d          ;*ishl {reexecute=0 rethrow=0 return_oop=0}
                                                ; - org.sample.A::test@18 (line 47)
                                                ; - org.sample.MyBenchmark::testMethod@25 (line 68)

  0x00007f96dd74bf5c: jmp     0x7f96dd74bf41
  0x00007f96dd74bf5e: mov     %r8d,%r11d
  0x00007f96dd74bf61: mov     %r10d,%eax
  0x00007f96dd74bf64: add     %r11d,%eax        ;*iadd {reexecute=0 rethrow=0 return_oop=0}
                                                ; - org.sample.MyBenchmark::testMethod@28 (line 68)

  0x00007f96dd74bf67: add     $0x10,%rsp
  0x00007f96dd74bf6b: pop     %rbp
  0x00007f96dd74bf6c: test    %eax,0x165a708e(%rip)  ;   {poll_return}
  0x00007f96dd74bf72: retq
  0x00007f96dd74bf73: mov     $0xfffffff6,%esi
  0x00007f96dd74bf78: mov     %r11d,%ebp
  0x00007f96dd74bf7b: callq   0x7f96d5c9b560    ; ImmutableOopMap{}
                                                ;*invokevirtual test {reexecute=0 rethrow=0 return_oop=0}
                                                ; - org.sample.MyBenchmark::testMethod@11 (line 67)
                                                ;   {runtime_call UncommonTrapBlob}
  0x00007f96dd74bf80: callq   0x7f96f2772aa0    ;*invokevirtual test {reexecute=0 rethrow=0 return_oop=0}
                                                ; - org.sample.MyBenchmark::testMethod@11 (line 67)
                                                ;   {runtime_call}
  0x00007f96dd74bf85: hlt
  0x00007f96dd74bf86: hlt
  0x00007f96dd74bf87: hlt
  0x00007f96dd74bf88: hlt
  0x00007f96dd74bf89: hlt
  0x00007f96dd74bf8a: hlt
  0x00007f96dd74bf8b: hlt
  0x00007f96dd74bf8c: hlt
  0x00007f96dd74bf8d: hlt
  0x00007f96dd74bf8e: hlt
  0x00007f96dd74bf8f: hlt
  0x00007f96dd74bf90: hlt
  0x00007f96dd74bf91: hlt
  0x00007f96dd74bf92: hlt
  0x00007f96dd74bf93: hlt
  0x00007f96dd74bf94: hlt
  0x00007f96dd74bf95: hlt
  0x00007f96dd74bf96: hlt
  0x00007f96dd74bf97: hlt
  0x00007f96dd74bf98: hlt
  0x00007f96dd74bf99: hlt
  0x00007f96dd74bf9a: hlt
  0x00007f96dd74bf9b: hlt
  0x00007f96dd74bf9c: hlt
  0x00007f96dd74bf9d: hlt
  0x00007f96dd74bf9e: hlt
  0x00007f96dd74bf9f: hlt
[Exception Handler]
[Stub Code]
org.sample.generated.MyBenchmark_testMethod_jmhTest::testMethod_avgt_jmhStub