I have follow test class for checking JIT-compiler logic:
public static final int COUNT = 2_000_000_000;
public static final MyLogger LOG = new MyLogger(false);
//Here IS_DEBUG is false
public static final boolean IS_DEBUG = LOG.isDebug();
private void run() throws Exception
{
System.out.println(getSum(COUNT));
//Compilation without OSR
System.out.println(getSum(COUNT + 2));
//Change value IS_DEBUG -> true over reflection
setFinalStatic(TestDeadCode.class.getField("IS_DEBUG"), true);
//Show true
System.out.println(IS_DEBUG);
COUNT = COUNT / 2;
System.out.println(getSum(COUNT + 3));
}
private int getSum(int count)
{
int result = 0;
for (int j = 0; j < count; j++)
{
result = result + 1;
if (IS_DEBUG)
{
//Dead code here
System.out.println("debug: " + result);
}
}
return result;
}
If i call method run()
then "dead code"
System.out.println("debug: " + result);
is executed never. Is it JVM bug?
Java version:
Java(TM) SE Runtime Environment (build 1.7.0_17-b02)
Java HotSpot(TM) 64-Bit Server VM (build 23.7-b01, mixed mode)
UPDATE: PrintCompilation output:
79 1 % com.nau.sample.deadcode.TestDeadCode::getSum @ 7 (34 bytes) 85 1 com.nau.sample.deadcode.TestDeadCode::getSum (34 bytes)