Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

dead-code elimination--what are all the rules?

843829Feb 21 2008 — edited Feb 26 2008
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.)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 25 2008
Added on Feb 21 2008
1 comment
430 views