Definition
An intermediate representation (IR) is the form in which a compiler or analysis tool holds a program between reading its source text and producing its output. An IR is a language with its own instructions, types and semantics (the exact meaning of each instruction), designed for programs to analyze and transform, not for people to write. A compiler translates the source into an IR, improves the IR in steps, then translates the IR into machine code. Each IR keeps some details of the source and the machine and drops others.
The idea
A compiler for C must produce code for several families of processors, such as Arm and x86, and so must compilers for other languages. Writing every optimization once for each pair of language and processor would multiply the work. With a shared IR, each language needs one front end that translates into the IR, each processor needs one back end that translates out of the IR, and each optimization is written once, on the IR. The Clang §12.17 C compiler and the Rust compiler, for example, both translate into the IR of LLVM §12.18, a widely used compiler infrastructure (LLVM began as Low Level Virtual Machine; the project now treats the letters as a plain name). An IR also helps analysis. One C expression can hide several operations, conversions between types and changes to memory; an IR spells out each step as a separate instruction with its types written down.
An IR is like a recipe rewritten as a numbered list of single actions with every quantity in grams: easier to check, scale and reorder than the cook’s prose. But the rewriting already made decisions, such as the size of a cup, and some intentions, such as “season to taste”, did not survive it. The analogy stops at precision: what a compiler IR keeps is fixed by the IR’s specification and can be looked up.
The consequence for analysis is direct: an analysis can only see what the IR it runs on keeps. A compiler usually passes through several IRs, from high-level ones close to the source, with types, objects and structured loops, to low-level ones close to the machine, with fixed-width registers, addresses and explicit jumps. Each step down, called lowering, adds machine decisions and removes source facts.
How it works
A typical optimizing compiler has four stages.
Front end. It reads the source, checks it against the language rules and emits IR. It makes implicit language rules explicit. In C, for example, integer promotion §4.18, the rule that converts small integer types to
intbefore arithmetic, becomes an explicit widening instruction.IR form. Many IRs are three-address code: each instruction applies one operation to at most two operands and names its result, as in . The instructions are grouped into basic blocks §3.14, straight-line runs entered only at the top, and the blocks form a control-flow graph §3.13 (CFG), whose edges are the possible jumps. Many IRs also use static single assignment §3.38 (SSA) form, in which every variable is assigned exactly once.
Optimizer. A sequence of passes rewrites IR into better IR of the same language.
Back end. It lowers the IR to the target processor. Instruction selection chooses machine instructions for each IR operation. Register allocation decides which of the processor’s few registers holds each value and which values must move to the stack. The ABI §12.47 (application binary interface), the target’s conventions for data layout and function calls, fixes how arguments are passed. The output is assembly or machine code.
LLVM IR shows what a mid-level IR keeps and what it leaves out.
Kept: integer widths, as types such as
i8,i32andi64; every memory load and store with its width, its alignment §4.22 and a mark if it is volatile, meaning that it must be performed as written; the ordering of atomic operations §9.8; and the control flow.Kept, but moved: an integer type has no sign. Signedness lives in the operations instead, which come in signed and unsigned versions, such as
sdivandudivfor division.Kept as a flag: the
addinstruction wraps around by default. The flagsnsw(no signed wrap) andnuw(no unsigned wrap) state that the addition does not overflow. If it does overflow, the result is a poison value, which spreads to every result computed from it and makes the behavior undefined if it decides a branch or serves as a memory address. This is how C’s rule that signed overflow §4.15 is undefined behavior §4.12 reaches the IR.Not yet decided: which machine register holds a value, since the IR has an unlimited number of virtual registers; the layout of each stack frame; and the exact machine instructions. A 64-bit multiplication on a 32-bit processor, for example, becomes several instructions or a call to a library routine only in the back end.
Lost: comments, macros, most source names, and much of the source type information, such as whether an integer type was declared signed or unsigned. An access to a field of a structure becomes an address computation, whose byte offset is fixed by the target’s data layout.
Worked example
Take the C statement y = (a + b) * 2; where a, b and y have type uint8_t, an unsigned 8-bit integer with values 0 to 255. Integer promotion converts a and b to the 32-bit int of a typical target before the arithmetic, so the IR spells the statement out in five instructions.
is
awidened from 8 to 32 bits by adding zero bits at the top (zero extension §4.19).is
b, widened the same way., in 32-bit arithmetic.
, in 32-bit arithmetic.
yreceives the low 8 bits of (truncation).
With a = 200 and b = 100: , , , , and y receives . At this stage the IR shows a fact that the source text hides: the intermediate sum 300 does not fit in 8 bits, and the arithmetic is done in 32 bits, so the sum is not lost.
An optimizer may later notice that only the low 8 bits of are used. The low 8 bits of a sum or a product depend only on the low 8 bits of the operands, so the optimizer may legally redo the whole computation in 8-bit arithmetic: , then . The stored value is the same, 88. An analysis run before this optimization finds an intermediate sum of 300; an analysis run after it finds 44. Each is correct about its own stage, and neither describes the other.
A second example concerns memory. A structure with a 4-byte field id followed by a 2-byte field len occupies 8 bytes on a typical 32-bit target: bytes of fields, padded to 8 so that the structure’s size is a multiple of its 4-byte alignment. The source expression p->len becomes, in a low-level IR, a 2-byte load from address with 2-byte alignment. The width, the offset and the alignment are kept. Lost is the fact that the 2 bytes belong to an 8-byte object that p must point to; an analysis of memory safety at this stage needs that fact from elsewhere.
Stated precisely
Let be the property an analysis establishes, and let the analysis run at IR stage of a compiler. The result carries over to the machine code only when two conditions hold.
Adequacy: the semantics of stage decides . Every fact that depends on, such as integer widths, signedness, overflow rules, object boundaries, alignment, memory effects and the order of volatile accesses, is either kept explicitly at stage or fixed by a written assumption.
Preservation: every later step, from stage down to the machine code, preserves . Every execution of the machine code must correspond to an execution at stage that agrees on everything depends on. This relation is a form of refinement §3.32.
A compiler’s correctness guarantee usually covers observable behavior only: input and output, volatile accesses and termination. A property of internal values, such as the width of an intermediate sum or the number of loads, is not observable, and the compiler is free to change it. Condition 2 therefore needs its own argument for such a property.
Limits and common mistakes
“IR” names many different languages. What one IR keeps says nothing about another, and the semantics of one IR can change between compiler versions; the rules for poison and undefined values in LLVM IR, for example, have been revised over the years.
An IR produced from C is usually already specific to one target. The front end has fixed the sizes of types, the layout of structures and parts of the calling convention for that target’s ABI, so the same C file can give different LLVM IR for a 32-bit and for a 64-bit processor.
An analysis at IR level does not cover the stages after it. The back end, the assembler, the linker §12.4, hand-written assembly, the runtime library and the loader each need their own argument.
Flags carry permissions. An analysis that treats an add marked nsw as an ordinary wrapping addition misunderstands what the optimizer is allowed to do with it. An analysis that treats the flag as proof that overflow cannot happen trusts a promise that the source program may break.
Lost facts do not come back. Analysis of machine code must reconstruct types and object boundaries from instructions, which is often imperfect.
A bytecode §2.4 such as eBPF §2.1 (extended Berkeley Packet Filter) bytecode or Java bytecode is sometimes called an IR. The difference is the contract: a bytecode is a distribution format with a published, stable specification, run by a virtual machine §2.3 or translated by a just-in-time compiler §2.14, while a compiler’s internal IR serves only the compiler and can change from one release to the next.
Questions answered
Which machine details are retained or lost in the IR?
Which machine details an intermediate representation (IR) keeps depends on the particular IR and the stage of compilation, so the answer must be read from that IR’s specification; the name IR alone settles none of it. A mid-level IR such as LLVM IR, the IR of the LLVM compiler infrastructure, keeps integer widths, the width, alignment and volatile mark of each memory access, the ordering of atomic operations and the control flow. It keeps the source language’s overflow rules as flags: an addition marked nsw, for no signed wrap, gives a poison value if it overflows. It has not yet decided which machine register holds each value, how stack frames are laid out, which exact machine instructions are used, or every detail of how arguments are passed under the ABI (application binary interface); the back end decides these. It has already lost comments, macros, most names and much source type information, such as whether an integer type was signed, and an optimizer may have removed or narrowed intermediate values.
For a given analysis, check each item the property depends on: widths, signedness, overflow rules, pointer and object facts, alignment, memory effects, exceptional outcomes such as traps, and the calling convention. Each item is kept explicitly, encoded as an assumption, or left to a later stage. Only the first two can support a claim made at that stage.
Related entries: Static single assignment (SSA) (§3.38), Compiler optimization (§3.39), Compiler correctness (§3.40), Control-flow graph (CFG) (§3.13), LLVM (§12.18), Integer promotion and conversion (§4.18). Further reading: [114].