Java Cup
Inside Java

News and views from members of the Java team at Oracle

Handling Irreducibility in a Just-in-Time Compiler

by Oskar Grönman (guest) on December 3, 2026

My name is Oskar, and during the spring of 2026 I finished my master's degree in Computer Science at KTH Royal Institute of Technology, within the Software Engineering track. For my master's thesis I collaborated with the JVM compiler team at Oracle's Stockholm office to investigate how to handle irreducibility in a JIT compiler.

Just-in-Time (JIT) compilers have many unique advantages over traditional Ahead-of-Time (AOT) compilers. A JIT compiler compiles code while the program is running, so it can use runtime information about how the program actually behaves. However, JIT compilation also comes with interesting challenges. One such challenge is the trade-off between compilation speed and compilation quality. A JIT compiler needs to compile code quickly so that the program can benefit from optimized machine code sooner, while the quality of those optimizations determines how much faster the program can ultimately run.

Modern compilers often rely on intermediate representations (IRs) based on control-flow graphs to analyze and optimize programs. However, not all control-flow graphs are equally easy to work with. Some structures make certain common compiler analyses and optimizations more difficult, or even prevent them entirely, potentially leading to missed optimizations and performance losses. One such structure is an irreducible control-flow graph, a graph with a cycle that has multiple entry points.

The HotSpot JVM has two compilers, C1 and C2. C1 is designed to allow for the JVM to quickly produce native code, while C2 focuses on producing more highly optimized code. In my thesis, I investigated how to handle irreducible control flow in the JVM's C2 compiler.

Problem

There are some established methods for handling irreducible control flow in AOT compilers. However, methods that work well in an AOT compiler may not be suitable for JIT compilation. A solution that significantly increases compilation time may not be worthwhile, even if it produces better optimized code. In my thesis, I investigated how two established approaches, node splitting and dispatching, could be adapted into the HotSpot C2 compiler. I compared them on runtime performance, code size, number of optimizations enabled, and compilation time.

Problem Background

One intermediate representation that is often used is a control-flow graph, which shows the possible paths that can be executed in a program. Control-flow graphs consist of nodes representing blocks of sequential instructions and edges representing possible transfers of control between those blocks. This representation makes it easier for a compiler to reason about how a program executes.

Consider a control-flow graph containing a cycle, where the same node can be visited multiple times. If the cycle can only be entered through a single node, then that node acts as the loop's header. Such loops are often called natural loops, and many compiler optimizations, such as loop unrolling, rely on being able to identify them.

Reducible control flow graph

Figure 1: Reducible control flow graph

In the graph above, the cycle [C → D → C] can only be entered through node C, making C its header. Now instead consider a similar graph in which execution can enter the same cycle through multiple nodes.

Irreducible control flow graph

Figure 2: Irreducible control flow graph

Here, the cycle has two entry points. There is no single node that dominates the entire cycle, making the graph irreducible.

Connections to the JVM

Irreducible control flow is often associated with unstructured programs, such as those that use arbitrary goto statements. Java does not support gotos, but this does not mean that irreducible control flow cannot occur on the JVM. Java bytecode has goto instructions, which allow execution to jump between different locations in a method. Furthermore, some JVM languages, like Kotlin, contain certain constructs that can translate to irreducible Java bytecode depending on how they are compiled.

At runtime the HotSpot JVM initially interprets code and compiles frequently executed code to improve performance. The JVM switches from interpreting code to compiled code at certain points, for example at later method invocations. However, sometimes it is beneficial to switch to compiled code while a method is already running, rather than waiting for the method to be invoked again. This is commonly used for very long-running methods, such as a main method with a big loop. This is called On-Stack Replacement (OSR). To make OSR possible, the compiled code must be able to start executing at the corresponding point in the method, rather than at its beginning. This introduces an additional entry point into the control-flow graph. If this entry point is inside an existing loop, it can introduce another way to enter the loop, potentially making an otherwise reducible graph irreducible.

Currently, if a program has irreducible control flow, the HotSpot JVM will not apply loop optimizations to those methods.

Existing Methods

Two established approaches to handling irreducible control flow are node splitting and dispatching. Both aim to transform an irreducible graph into a reducible one, but they do so in different ways.

Node Splitting

The main idea of node splitting is to duplicate certain nodes so that each copy is reachable from only one place, leaving the cycle with a single entry point. The standard algorithm repeatedly merges nodes to simplify the graph and duplicates nodes when it gets stuck, before expanding the graph while preserving the duplicates.

How node splitting resolves irreducibility

Figure 3: How node splitting resolves irreducibility

This algorithm will always produce a reducible graph, and the resulting graph preserves the original program's behavior. However, it may contain more nodes. In the absolute worst case the number of nodes will increase exponentially.

The main advantage of node splitting is that it does not inherently require additional runtime instructions. However, duplicating nodes can increase the size of the compiler's intermediate representation and potentially the generated code. This can make compilation more expensive or cause the compilation to fail.

Dispatching

Instead of duplicating nodes, dispatching creates a new common entry point, called the dispatcher, for the irreducible cycle. Each incoming edge into the cycle records where it came from, via a variable, and the dispatcher uses the value of this variable to branch to the correct target. This approach avoids the potentially large increase in graph size caused by node splitting. However, the dispatcher introduces additional state and instructions, which can add runtime overhead and affect the optimizations available to the compiler.

How dispatching resolves irreducibility

Figure 4: How dispatching resolves irreducibility

JIT Considerations

Ideally, any modifications to the JIT compiler should be relatively lightweight, so as to not increase compilation time significantly. The traditional node splitting algorithm can be expensive, as it may require substantial computation to analyze and transform the graph. For my thesis, I implemented a modified version that resolves irreducibility locally and incrementally. Rather than processing the entire graph in one go, the transformation can be performed as the compiler constructs the graph, only modifying the parts that need to be changed. This means that little to no extra cost is paid for reducible programs, while it is still able to handle irreducible graphs.

Another important consideration is when the transformation takes place. To allow subsequent optimization passes to benefit from the resulting graph, the changes should happen as early as possible in the compilation process. For node splitting, the transformation takes place entirely during bytecode parsing. For dispatching, the necessary blocks are introduced during bytecode parsing, while the dispatching logic is implemented in the compiler's sea-of-nodes intermediate representation.

Results

The two approaches were implemented in HotSpot C2 and evaluated using a synthetic benchmark suite of 23 programs containing irreducible control flow. The benchmark programs are a mix of manually written programs that test specific C2 loop optimizations, and randomly generated irreducible programs. An unmodified C2 compiler was used as a baseline to compare the implemented methods against.

The modified node splitting approach showed promising results across the tested benchmarks. It allowed C2 to apply additional optimizations and improved the runtime performance of the compiled code in every tested case. Out of the tested programs, about 60% showed at least a five times runtime performance increase, with the largest observed speed-up being approximately 7900 times. In this case the node splitting algorithm allowed for a loop optimization to remove the entire loop and directly replace it with its final result, causing the significant speed-up. Node splitting also compared favorably with the baseline in compilation time in the majority of the tested cases.

Dispatching produced less positive results than node splitting. While some programs benefited from the transformation, the improvements were generally smaller than with node splitting, in most cases only allowing for a single extra loop optimization. Otherwise, it performed similarly to the baseline on all metrics. One possible explanation is that dispatching obscures information about loop structure, preventing C2 from applying certain loop optimizations that it can apply after node splitting.

Conclusions

Irreducible graphs, where cycles have multiple entry points, can make certain optimizations more difficult or prevent them entirely. This is particularly relevant to the HotSpot JVM, where On-Stack Replacement can introduce irreducible control flow during compilation.

The two evaluated approaches for handling irreducibility were node splitting and dispatching. Dispatching produced code that is close to the baseline in most metrics, and showed slight improvements in the number of optimizations. Node splitting consistently produced code that is both faster and easier to optimize than the baseline, while keeping code size and compilation time manageable. These results suggest that node splitting can improve the quality of the compiled code in C2 without a significant cost to compilation time.

Lastly, I want to thank everyone I had the pleasure of working with at Oracle, particularly at the Stockholm office, for making my thesis work a great experience. I especially want to thank my industry supervisors, Emanuel Peter and Roberto Castañeda Lozano. I learned a lot during my time there, not only through working on my thesis, but also from the knowledge and experience that everyone was happy to share. It was also a lot of fun working with such friendly people who clearly enjoy what they do.