Skip to content

Instantly share code, notes, and snippets.

@rednaxelafx
Created May 15, 2011 06:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rednaxelafx/972929 to your computer and use it in GitHub Desktop.
Save rednaxelafx/972929 to your computer and use it in GitHub Desktop.
Example of code reordering from with HotSpot 20. Run on JDK6u25.
dontinline TestC2BranchPrediction conditionalBranch
dontinline TestC2BranchPrediction marker
+UnlockDiagnosticVMOptions
+PrintAssembly
Decoding compiled method 0x00007fdf4c7f29d0:
Code:
[Disassembling for mach='i386:x86-64']
[Entry Point]
[Verified Entry Point]
[Constants]
# {method} 'conditionalBranch' '(ZZZ)V' in 'TestC2BranchPrediction'
# parm0: rsi = boolean
# parm1: rdx = boolean
# parm2: rcx = boolean
# [sp+0x20] (sp of caller)
0x00007fdf4c7f2b40: mov %eax,-0x6000(%rsp)
0x00007fdf4c7f2b47: push %rbp
0x00007fdf4c7f2b48: sub $0x10,%rsp ;*synchronization entry
; - TestC2BranchPrediction::conditionalBranch@-1 (line 23)
0x00007fdf4c7f2b4c: test %esi,%esi
0x00007fdf4c7f2b4e: jne 0x00007fdf4c7f2b70 ;*ifeq
; - TestC2BranchPrediction::conditionalBranch@1 (line 23)
0x00007fdf4c7f2b50: test %edx,%edx
0x00007fdf4c7f2b52: je 0x00007fdf4c7f2b82 ;*ifeq
; - TestC2BranchPrediction::conditionalBranch@13 (line 25)
0x00007fdf4c7f2b54: mov $0x780080cd8,%rsi ; {oop("DO SOMETHING B")}
0x00007fdf4c7f2b5e: nop
0x00007fdf4c7f2b5f: callq 0x00007fdf4c7ccc20 ; OopMap{off=36}
;*invokestatic marker
; - TestC2BranchPrediction::conditionalBranch@18 (line 26)
; {static_call}
0x00007fdf4c7f2b64: add $0x10,%rsp
0x00007fdf4c7f2b68: pop %rbp
0x00007fdf4c7f2b69: test %eax,0x5687491(%rip) # 0x00007fdf51e7a000
; {poll_return}
0x00007fdf4c7f2b6f: retq
0x00007fdf4c7f2b70: mov $0x780080d28,%rsi ; {oop("DO SOMETHING A")}
0x00007fdf4c7f2b7a: nop
0x00007fdf4c7f2b7b: callq 0x00007fdf4c7ccc20 ; OopMap{off=64}
;*invokestatic marker
; - TestC2BranchPrediction::conditionalBranch@6 (line 24)
; {static_call}
0x00007fdf4c7f2b80: jmp 0x00007fdf4c7f2b64
0x00007fdf4c7f2b82: test %ecx,%ecx
0x00007fdf4c7f2b84: je 0x00007fdf4c7f2b64 ;*ifeq
; - TestC2BranchPrediction::conditionalBranch@25 (line 27)
0x00007fdf4c7f2b86: mov $0x780080d78,%rsi ; {oop("DO SOMETHING C")}
0x00007fdf4c7f2b90: xchg %ax,%ax
0x00007fdf4c7f2b93: callq 0x00007fdf4c7ccc20 ; OopMap{off=88}
;*invokestatic marker
; - TestC2BranchPrediction::conditionalBranch@30 (line 28)
; {static_call}
0x00007fdf4c7f2b98: jmp 0x00007fdf4c7f2b64 ;*invokestatic marker
; - TestC2BranchPrediction::conditionalBranch@30 (line 28)
0x00007fdf4c7f2b9a: mov %rax,%rsi
0x00007fdf4c7f2b9d: jmp 0x00007fdf4c7f2ba7 ;*invokestatic marker
; - TestC2BranchPrediction::conditionalBranch@6 (line 24)
0x00007fdf4c7f2b9f: mov %rax,%rsi
0x00007fdf4c7f2ba2: jmp 0x00007fdf4c7f2ba7
0x00007fdf4c7f2ba4: mov %rax,%rsi ;*invokestatic marker
; - TestC2BranchPrediction::conditionalBranch@18 (line 26)
0x00007fdf4c7f2ba7: add $0x10,%rsp
0x00007fdf4c7f2bab: pop %rbp
0x00007fdf4c7f2bac: jmpq 0x00007fdf4c7f1e20 ; {runtime_call}
0x00007fdf4c7f2bb1: hlt
0x00007fdf4c7f2bb2: hlt
0x00007fdf4c7f2bb3: hlt
0x00007fdf4c7f2bb4: hlt
0x00007fdf4c7f2bb5: hlt
0x00007fdf4c7f2bb6: hlt
0x00007fdf4c7f2bb7: hlt
0x00007fdf4c7f2bb8: hlt
0x00007fdf4c7f2bb9: hlt
0x00007fdf4c7f2bba: hlt
0x00007fdf4c7f2bbb: hlt
0x00007fdf4c7f2bbc: hlt
0x00007fdf4c7f2bbd: hlt
0x00007fdf4c7f2bbe: hlt
0x00007fdf4c7f2bbf: hlt
[Stub Code]
0x00007fdf4c7f2bc0: mov $0x0,%rbx ; {no_reloc}
0x00007fdf4c7f2bca: jmpq 0x00007fdf4c7f2bca ; {runtime_call}
0x00007fdf4c7f2bcf: mov $0x0,%rbx ; {static_stub}
0x00007fdf4c7f2bd9: jmpq 0x00007fdf4c7f2bd9 ; {runtime_call}
0x00007fdf4c7f2bde: mov $0x0,%rbx ; {static_stub}
0x00007fdf4c7f2be8: jmpq 0x00007fdf4c7f2be8 ; {runtime_call}
[Exception Handler]
0x00007fdf4c7f2bed: jmpq 0x00007fdf4c7f2620 ; {runtime_call}
[Deopt Handler Code]
0x00007fdf4c7f2bf2: callq 0x00007fdf4c7f2bf7
0x00007fdf4c7f2bf7: subq $0x5,(%rsp)
0x00007fdf4c7f2bfc: jmpq 0x00007fdf4c7cd7c0 ; {runtime_call}
0x00007fdf4c7f2c01: std
0x00007fdf4c7f2c02: incl (%rax)
0x00007fdf4c7f2c04: add %al,(%rax)
0x00007fdf4c7f2c06: add %al,(%rax)
import java.util.Random;
public class TestC2BranchPrediction {
public static void main(String[] args) {
Random rand = new Random();
for (int i = 0; i < 10000000L; i++) {
int state = rand.nextInt(10);
switch (state) {
case 0: case 1:
conditionalBranch(true, true, false);
break;
case 2:
conditionalBranch(false, false, true);
break;
default:
conditionalBranch(false, true, false);
break;
}
}
}
public static void conditionalBranch(boolean flagA, boolean flagB, boolean flagC) {
if (flagA) {
marker("DO SOMETHING A");
} else if (flagB) {
marker("DO SOMETHING B");
} else if (flagC) {
marker("DO SOMETHING C");
}
}
public static void marker(String message) {
try { message.intern(); } catch (Exception e) { }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment