This article
http://www-128.ibm.com/developerworks/library/j-jtp12214/ has an entire section on dead-code elimination (DCE).
Does anyone know what all the criteria are by which the hotspot JIT compiler can determine that code is "dead"?
One obvious criteria is if the code will never get executed. I think that the canonical example is code like this:
static final boolean debug = false;
static void someMethod() {
if (debug) {
...
}
}
Here, the compiler (javac as well as hotspot's JIT) can determine that someMethod has nothing inside it that could ever get executed, so it can be turned into an empty method.
If you look at that developerworks link above, he gives an even more interesting example for how hotspot's JIT can make sophisticated DCE based on inlining.
What I want to know is what are all the criteria that hotspot can use to decide that code is dead. Code that is never executed is always correct. But can it make more liberal decisions as to what counts as dead code?
Consider this code:
int[] foo = new int[1000];
int bar = foo.length;
// don't use foo again, but do use bar
A smart compiler could recognize that the array declaration and allocation is actually useless, and simply do
int bar = 1000;
(Thank you Brian for this example.)