Does HotSpot do dead code elimination?

On one hand I would expect so, OTOH I would expect javac to do this…
Is HotSpot able to do dead code elimination on the fly?

AFAIK, server yes, client no.

Cas :slight_smile:

Both Client and Server do dead code elimination…

See:
http://java.sun.com/javaone/javaone2001/pdfs/2696.pdf
http://java.sun.com/products/hotspot/docs/whitepaper/Java_Hotspot_v1.4.1/Java_HSpot_WP_v1.4.1_1002_4.html

Thanks for that.

The PDF says at one point “Generated code can be problematic”. Unfortunately, this is exactly the thing we (or rather, Toby) intend to do with dynamic recompilation of an emulated CPU. Are there any specific do’s and (more importantly) don’ts here?

[quote]Thanks for that.

The PDF says at one point “Generated code can be problematic”. Unfortunately, this is exactly the thing we (or rather, Toby) intend to do with dynamic recompilation of an emulated CPU. Are there any specific do’s and (more importantly) don’ts here?
[/quote]
I’m not sure, but the PDF is quite old (2001), and this may not be much of a problem anymore. The only suggestions I can give, is to make sure your outputed code is as clean as possible :slight_smile:

I remember a while back that the client VM definitely didn’t do dynamic dead code elimination in 1.4 as shown by some very simple tests… you’re saying this is now sorted in 1.5?

As I recall, code like this


class Test {
final boolean flag;
Test(boolean flag) {
this.flag = flag;
}
void loop() {
if (flag) {
// blah
} else {
// bleh
}
}
}

under client kept the if() test; and under server it cleverly got removed.

Cas :slight_smile:

Is there a tool to get or inspect the code generated by hotspot?

[quote]Is there a tool to get or inspect the code generated by hotspot?
[/quote]
Exactly. How do you know what was or was not removed Cas? I’d love to be able to know the exact transformation HotSpot performs on my code at runtime.

God bless,
-Toby Reyelts

Client does dead code elimination, but only code that has a zero reference count and will not remove calls that return a null value.
Something like an instanceOf, or in your case you if() statement are not dead code. Client does not know that removing the if() might change the structure of the code. Server on the other hand does much more extensive evaluation of the code. In this case Server realizes that the if() does nothing and removes it.

Also if you want to know what the generated code from HotSpot looks like use:

-XX:+PrintOptoAssembly // This dumps the generated assembly
-XX:+PrintCompilation // This dumps the compiled methods

Both are unsupported, they might destroy your machine, crash your OS, standard disclaimer applies (saving myself here).

Oh and -XX:+PrintOptoAssembly only works in debug mode, so you’ll need to use the JDK and run java_g vs java.

First, thanks for the info.

Second, I’m running on x86 - where do I get debug mode java? I don’t have a java_g, or any kind of jvm_g.dll or anything that I can spot.

God bless,
-Toby Reyelts

You should be able to get java_g with the JDK, or you use to be able to. Maybe that’s changed…

I have both JDK 1.4.2_02 and and JDK 1.5.0-beta2, and neither have a java_g or anything similar (I should have been more specific - this is Windows x86 - not Solaris x86). I can point to several links off of Google that say that java_g was no longer distributed JDK1.2+.

Oh, and btw, I tried -XX:+PrintOptoAssembly and it wasn’t recognized, but -XX:+PrintCompilation was.

God bless,
-Toby Reyelts

You should be able to compile your own debug VM by getting the source through the Sun community license… I’ve never tried it myself, but I figured that would be how us mere mortals would get java_g.

There are a few flags that are marked as “debug only” in the docs for hotspot options that are on java.sun.com

[quote]You should be able to compile your own debug VM by getting the source through the Sun community license…
[/quote]
There’s got to be a better way than that. I don’t think building the VM is a 1,2,3 step thing, and I don’t have weeks to devote to it. On top of that, I would only get to see the output for the VM I built, as opposed to any VM I can run (i.e. JDK 1.5, etc…)

God bless,
-Toby Reyelts

I never said that option didn’t suck :slight_smile:

I get the impression that the debug stuff is otherwise internal to Sun.

Shame, 'cause it would be quite cool to see what sort of code was produced. But I suspect then that Sun would get bombarded with complaints that the code produced wasn’t the best possible code for the task, by people that don’t understand that it can take time for a complier to produce really really optimized code and when you are compiling on-the-fly you don’t have all the time (or memory, etc.) in the world to optimize.

[quote]I never said that option didn’t suck :slight_smile:
[/quote]
Lol.

[quote]I get the impression that the debug stuff is otherwise internal to Sun.
[/quote]
Well, they used to ship java_g in the early days. I’m more under the impression no one used it, so they just stopped shipping it.

[quote]But I suspect then that Sun would get bombarded with complaints that the code produced wasn’t the best possible code for the task
[/quote]
Perhaps, but that’s not the goal. The goal is to understand what the compiler is/is not optimizing, so you know if you need to hand optimize something yourself.

God bless,
-Toby Reyelts

If that’s your goal then you need a good profiler. Seeing what the compiler outputs is meaningless unless you understand assembly. Even then a good profiler (there are a bunch) will tell you much more than any stream of assembly.

[quote]If that’s your goal then you need a good profiler.
[/quote]
I don’t see how that is the case. The profiler isn’t going to tell me whether or not HotSpot performed a particular optimization for me - which is what I need to know.

God bless,
-Toby Reyelts

Maybe I misunderstood you, but I’m guessing the reason you want to see what optimizations are being applied is because you are trying to improve performance. Am I right? If you really want to see what optimizations are being applied, then you’ll need to start reading the HotSpot source code (I’m sure its on java.sun.com) and build a debug version and go from there. If you want to improve performance, than you need to profile your code and see which methods are taking up the majority of CPU cycles, etc and go from there.

Either way to see the assembly output, you’ll need the debug bits (and I guess we don’t give them out anymore). Building HotSpot isn’t difficult under any of the unixes (make fastdebug), but Windows requires a bit more work

[quote]Maybe I misunderstood you, but I’m guessing the reason you want to see what optimizations are being applied is because you are trying to improve performance.
[/quote]
You’re working under the assumption that I don’t know what will speed up the program. It’s well known in emulation circles that the dead code removal that dynamic recompilation brings is a huge win. The question is whether or not HotSpot will provide that dead code removal, or if I need to implement it myself.

If I can’t easily determine whether or not HotSpot performs that optimization, it will be a darn shame, because it means that I will have no choice but to implement the optimization myself. And as far as reading assembly goes, I think you’ll find that a fair share of the people on this board know how to do just that.

God bless,
-Toby Reyelts