A tracing JIT compiler goes through various phases at runtime. First,
profiling information for loops is collected. After a hot loop has been identified, a special
tracing phase is entered, which records all executed operations of that loop. This sequence of operations is called a trace. The trace is then optimized and compiled to machine code. When this loop is executed again, the compiled trace is called instead of the program counterpart. These steps are explained in detail in the following:
Profiling phase The goal of profiling is to identify hot loops. This is often done by counting the number of iterations for every loop. After the count of a loop exceeds a certain threshold, the loop is considered to be hot, and tracing phase is entered.
Tracing phase In the tracing phase the execution of the loop proceeds normally, but in addition every executed operation is recorded into a trace. The recorded operations are typically stored in a
trace tree, often in an
intermediate representation (IR). Tracing follows function calls, which leads to them being inlined into the trace. Tracing continues until the loop reaches its end and jumps back to the start. Since the trace is recorded by following one concrete execution path of the loop, later executions of that trace can diverge from that path. To identify the places where that can happen, special
guard instructions are inserted into the trace. One example for such a place are if statements. The guard is a quick check to determine whether the original condition is still true. If a guard fails, the execution of the trace is aborted. Since tracing is done during execution, the trace can be made to contain
runtime information (e.g.,
type information). This information can later be used in the optimization phase to increase code efficiency.
Optimization and code-generation phase Traces are easy to optimize, since they represent only one execution path, which means that no control flow exists and needs no handling. Typical optimizations include
common-subexpression elimination,
dead code elimination,
register allocation,
invariant-code motion,
constant folding, and
escape analysis. After the optimization, the trace is turned into machine code. Similarly to optimization, this is easy due to the linear nature of traces.
Execution After the trace has been compiled to machine code, it can be executed in subsequent iterations of the loop. Trace execution continues until a guard fails. ==History==