The main loop in:
static int[] array;
static long base;
static int test1() {
int res = 0;
for (int i = 0; i < 100; i++) {
long address = (((long) i) << 2) + base;
res += UNSAFE.getInt(array, address);
}
return res;
}
is compiled as:
0b0 B2: # B2 B3 <- B1 B2 Loop: B2-B2 inner Freq: 101.492
0b0 movslq R9, R10 # i2l
0b3 salq R9, #2
0b7 addq R9, [R11 + #112 (8-bit)] # long
0bb addl RAX, [R8 + R9] # int
0bf incl R10 # int
0c2 cmpl R10, #100
0c6 jl,s B2 # loop end P=0.990147 C=6732.000000
but could be compiled as:
0b2 B2: # B2 B3 <- B1 B2 Loop: B2-B2 inner Freq: 101.492
0b2 addl RAX, [R8 + pos R10 << #2] # int
0b6 incl R10 # int
0b9 cmpl R10, #100
0bd jl,s B2 # loop end P=0.990147 C=6732.000000
base and array are loop invariant so array + base can be computed before the loop.