r/Compilers • • 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 Upvotes

4 comments sorted by

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?

0

u/Signal-Scholar1299 Oct 19 '26 edited Oct 19 '26

Vous avez raison,
j'ai voulu mentionner l'accès hotspot jit.
l'optimisation C2 attend beaucoup avant de déclencher l'optimisation.
(je ne veux pas le changer pour l'ensemble de la jvm)
et certaines optimisations comme la mutualisation de variables ne fonctionneront pas parce que le jit n'est pas capable d'identifier qu'un appel de méthode est idempotent.
Je pense qu'il va le mettre en ligne et ensuite peut-être que tu obtiendras l'optimisation qui ajoute de la latence, mais dans notre cas, il s'agit d'un accès à la mémoire hors tas que nous savons constant pendant l'exécution de la méthode et le jit ne le sait pas.

2

u/egybtianberson Oct 19 '26

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?

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.

0

u/Signal-Scholar1299 Oct 21 '26

Bonjour je génère du code donc cela ne correspond pas