热点编译的汇编器
mov r10d,DWORD PTR [rsi+0x8]
shl r10,0x3
cmp rax,r10
jne 0x00007fb509045b60 ; {runtime_call}
data32 xchg ax,ax
nop WORD PTR [rax+rax*1+0x0]
nop WORD PTR [rax+rax*1+0x0]
[Verified Entry Point]
sub rsp,0x18
mov QWORD PTR [rsp+0x10],rbp ;*synchronization entry
;- Main$TestClass::myFunction@-1 (line 25)
mov eax,DWORD PTR [rsi+0x10]
imul eax,DWORD PTR [rsi+0xc]
add eax,DWORD PTR [rsi+0x14] ;*iadd
;- Main$TestClass::myFunction@13 (line 25)
add rsp,0x10
pop rbp
test DWORD PTR [rip+0x15f7a8bf],eax # 0x00007fb51f087000
;{poll_return}
ret
Java 代码
public class Main {
public static void main(String... args) {
int accm = 0;
TestClass t = new TestClass(542,452);
while(true){
t.myVariable = accm;
accm += t.myFunction();
if(false) break;
}
System.out.println(accm);
}
public static class TestClass{
final int foo;
final int bar;
public int myVariable = 2;
public TestClass(int foo, int bar){
this.foo = foo;
this.bar = bar;
}
int myFunction(){
return foo*bar + myVariable;
}
}
}
Java版
java version "1.8.0-ea"
Java(TM) SE Runtime Environment (build 1.8.0-ea-b124)
Java HotSpot(TM) 64-Bit Server VM (build 25.0-b66, mixed mode)
结论
不,它不能。
虽然我不明白那里发生的大部分事情,但似乎该函数已被内联到循环中[Verified Entry Point]
,但常量没有折叠,因为在 处仍然发生乘法imul eax,DWORD PTR [rsi+0xc]
。