r/Compilers • u/Signal-Scholar1299 • Oct 19 '26
Are there any libraries for optimizing generated Java code?
Hi,
I’m working on a compiler at my company that generates Java code represented as a custom AST, then uses ASM to produce JVM bytecode.
I’d like to apply optimizations to this AST, or convert it to a standard AST representation if needed. Are there any libraries that support optimizations such as constant folding, dead code elimination, and factoring out duplicated code?
As an alternative, I’m considering targeting LLVM IR, although this would introduce other challenges, particularly around memory sharing and interoperability with the JVM. Do you know of any libraries or tools that can translate Java code or JVM bytecode into LLVM IR?
2
u/SirYwell Oct 19 '26
Do you want to optimize Java code, JVM Bytecode, or your AST?
If you're producing JVM Bytecode, I assume you're also running the code on "the" JVM (i.e., HotSpot). This already brings basically all the relevant optimizations, so what problem are you trying to solve exactly?
2
u/egybtianberson Oct 19 '26
If you're willing to give up on things like reflection, then swapping your JVM with GraalVM's native-image will try to compile your program ahead-of-time (with optimizations) to an executable.