{
  "id": "intermediate-representation",
  "title": "Intermediate representation",
  "lang": "en",
  "source_lang": "en",
  "summary": "An intermediate representation is a structured form of a program used to support analysis, optimization, and translation between source languages and execution targets.",
  "category": "computer-science",
  "subcategory": "software",
  "type": "concept",
  "aliases": [
    "Intermediate code",
    "IR"
  ],
  "generated_at": "2026-11-05",
  "model": "pro/gpt-6.1-sol",
  "url": "https://aiwiki.page/en/intermediate-representation/",
  "translations": {
    "zh-Hans": {
      "url": "https://aiwiki.page/zh/intermediate-representation/",
      "json": "https://aiwiki.page/zh/intermediate-representation.json"
    }
  },
  "sections": [
    {
      "heading": "",
      "text": "An intermediate representation (IR) is a structured representation of a program used between, or across, stages of software translation and execution. In a compiler, it typically stands between source code and machine code, exposing program operations in a form suited to analysis and transformation. An IR can resemble a source-language syntax tree, a sequence of simple instructions, a graph of dependencies, or a collection of domain-specific operations. It need not be textual, executable, or independent of a particular processor.\n\nIntermediate representations make complex translation systems manageable by separating concerns. A language front end can express a program without deciding exactly which processor instructions will execute it; an optimizer can improve that representation; and a back end can map it to a target machine. Shared representations also allow multiple languages and multiple targets to reuse substantial parts of a compiler.\n\nThe design of an IR determines which facts are explicit, which transformations are easy, and which semantic obligations must be preserved. Modern compilation therefore usually employs several representations rather than one universal intermediate language. These range from high-level forms that preserve arrays, objects, or parallel operations to low-level forms that expose registers, memory accesses, and calling conventions."
    },
    {
      "heading": "Abstraction levels and the organization of compilation",
      "text": "A compiler front end typically performs parsing, name resolution, and type checking before constructing or refining an intermediate representation. A middle end analyzes and transforms the program, while a back end selects target instructions, allocates registers, and emits executable code or assembly language. These boundaries are architectural conventions rather than mandatory divisions. Some compilers optimize directly on syntax trees; others repeatedly translate between representations with different purposes.\n\nA high-level IR preserves concepts close to those of a programming language: structured loops, array indexing, closures, exceptions, or object operations. Retaining these concepts makes certain transformations easier. For example, an operation that explicitly multiplies two matrices can be replaced with an optimized library call without first reconstructing matrix multiplication from individual loads, additions, and multiplications. High-level information is also valuable for diagnostics and source-oriented tools.\n\nA low-level IR exposes details closer to an instruction set architecture, such as fixed-width arithmetic, explicit loads and stores, branches, and register-like temporary values. A machine-oriented representation may further distinguish physical registers, instruction encodings, condition codes, or stack locations. The labels “high,” “middle,” and “low” are relative: LLVM IR is low-level compared with a language syntax tree, but it remains abstract compared with a representation containing target instructions and assigned physical registers.\n\nA shared IR reduces duplication in a multi-language, multi-target compiler. With \\(m\\) source languages and \\(n\\) target architectures, separate direct translators could require as many as \\(mn\\) language–target combinations. A common representation can organize much of this work into roughly \\(m\\) front ends and \\(n\\) back ends. This is an architectural comparison, not a guarantee about implementation effort: language-specific runtime behavior, target-specific optimizations, and incompatible data models can still require specialized components.\n\nThe word representation also distinguishes the underlying model from its storage format. One IR may have an in-memory graph, a readable textual syntax, and a compact binary encoding. These are alternative encodings of substantially the same program model, not necessarily separate abstraction levels. Conversely, two representations with similar textual instructions may have very different semantics. Their treatment of overflow, pointer values, exceptions, or memory ordering can matter more than their surface appearance."
    },
    {
      "heading": "Trees, instruction sequences, and dependency graphs",
      "text": "An abstract syntax tree represents a program through nested language constructs. An assignment node may contain a destination and an expression subtree; a loop node may contain an initialization, condition, update, and body. Unlike a concrete parse tree, an abstract syntax tree generally omits punctuation and grammar details that are unnecessary after parsing. When used for analysis or transformation, such a tree serves as an IR, although some compiler descriptions reserve that term for representations introduced after the initial syntax tree.\n\nInstruction-oriented IRs replace complex expressions with smaller operations. A common model is three-address code, in which an instruction usually has one result and at most two principal input operands. The expression r = (a + b) * (c - d) can become:\n\nt1 = add a, b\nt2 = sub c, d\nr  = mul t1, t2\n\nThe temporary names expose intermediate results and separate evaluation steps. Actual IR instruction sets usually also contain operations with different arities, including calls, returns, memory accesses, and multiway branches.\n\nLinear instruction sequences provide a convenient order, but dependencies can also be represented explicitly as graphs. In a directed acyclic expression graph, two operations may refer to the same node for a shared subexpression. This can represent reuse without duplicating the computation. A graph for an entire procedure is more complicated: loops introduce cycles, and memory effects or control dependencies require additional structure. A computational graph is therefore a broad family of representations rather than a single compiler format.\n\nDifferent graph organizations emphasize different relationships. A control-flow-oriented representation groups instructions according to possible execution paths. A dataflow-oriented representation emphasizes which values feed which operations. A “sea of nodes” representation combines value, control, and sometimes memory dependencies in a graph, leaving some scheduling decisions implicit until later compilation. Such designs allow transformations to move or combine operations without preserving an unnecessarily rigid original instruction order.\n\nThe choice also affects implementation cost. Trees support straightforward recursive traversal but do not naturally express shared ownership of a computed value. Graphs make sharing explicit but require careful maintenance of use lists, dominance relationships, and cyclic structures. Linear forms can be compact and easy to inspect, yet their apparent order may obscure which reorderings are legal. Compilers commonly maintain auxiliary indexes and analyses alongside the IR, so the representation visible in a textual dump is only part of the optimizer’s working state."
    },
    {
      "heading": "Control-flow graphs and static single assignment",
      "text": "A control-flow graph divides a procedure into basic blocks and connects them with directed edges. A basic block is a straight-line sequence whose instructions execute in order after entry, with control leaving through its terminating operation. Conditional branches create alternative successors; returns have no normal successor within the procedure. Loops appear as cycles. The graph makes reachability, branch structure, and iteration explicit without requiring the original source-language loop syntax to survive.\n\nMany optimizers combine this structure with static single assignment form, or SSA. In SSA, each ordinary named value has exactly one definition, and uses refer to that definition. Repeated assignments to a source variable become distinct values. This does not mean that a program cannot change state or execute a loop. It means that the representation distinguishes individual definitions instead of relying on a mutable variable name to identify several different values.\n\nWhen control paths merge, SSA needs a way to identify the value arriving from each predecessor. A conventional representation uses a \\(\\phi\\)-function:\n\nentry:\n    branch condition, left, right\n\nleft:\n    x1 = add a, 1\n    jump join\n\nright:\n    x2 = sub a, 1\n    jump join\n\njoin:\n    x3 = phi [x1, left], [x2, right]\n    return x3\n\nHere x3 takes the value associated with the edge by which execution reaches join. The \\(\\phi\\)-function is not an instruction that evaluates both alternatives. Some IRs express the same idea through block arguments: incoming branches pass values to parameters declared by the destination block.\n\nSSA construction is closely connected to dominance. A block \\(A\\) dominates a block \\(B\\) if every path from the procedure’s entry to \\(B\\) passes through \\(A\\). An ordinary SSA definition must dominate its uses, with special edge-based rules for \\(\\phi\\)-operands. Dominance frontiers help determine where merging definitions may be required. Loops are handled by merging an initial value with a value produced on a back edge, making loop-carried dependencies explicit.\n\nSSA simplifies many def–use analyses, but it does not automatically describe all memory state. A load through a pointer may depend on an earlier store through another pointer. Compilers therefore combine scalar SSA with alias analysis, memory-dependence analysis, or memory-oriented SSA structures. Before final machine code, SSA merges are commonly converted into copies on incoming paths; register coalescing can remove many of those copies. Consequently, SSA is primarily an analysis and transformation discipline, not a requirement imposed on the eventual processor."
    },
    {
      "heading": "Types, effects, and semantic contracts",
      "text": "An IR is defined not merely by its operations but by their semantics. A typed addition instruction may specify operand widths, permitted conversions, and overflow behavior. A pointer operation may carry assumptions about alignment or the object being accessed. A call may be able to throw an exception, allocate memory, or modify externally visible state. These rules determine whether a transformation preserves the meaning of the program.\n\nThe IR’s type system need not reproduce the source language’s types. A source-level class might become a record layout plus explicit dispatch operations; a generic function might be specialized into several concrete versions; signed and unsigned source integers might share a bit-vector representation while using different comparison or division instructions. Retaining richer types can support stronger checks and transformations, but removing them can simplify translation across languages with different type systems.\n\nArithmetic semantics are particularly consequential. Addition of fixed-width bit vectors may be defined modulo \\(2^w\\), while another operation may require the mathematical result to fit within its width. In the C programming language, unsigned arithmetic is modular, whereas signed overflow has undefined behavior. An optimizer must reflect the relevant distinctions rather than treating every addition as interchangeable. Similarly, floating-point operations governed by IEEE 754 generally cannot be reassociated freely: \\((a+b)+c\\) and \\(a+(b+c)\\) may differ because of rounding, infinities, signed zero, or exceptional values.\n\nSome IRs distinguish undefined behavior from intermediate values that represent deferred invalidity. LLVM, for example, includes a poison concept and a freeze instruction. Poison can propagate through operations, while certain uses make execution undefined; freeze converts a poison or undefined input into an arbitrary but fixed value for that execution of the instruction. Such mechanisms permit aggressive optimization, but they also make apparently simple rewrites dependent on precise semantic rules.\n\nEffects require equally explicit treatment. A load from ordinary memory is not equivalent to a volatile access to a device register. An atomic operation participates in the memory model used to reason about concurrent computation. Exception-producing operations can have exceptional successors, while effect tokens or memory edges can record ordering constraints in graph-based IRs. An operation described as “pure” must have a precise meaning: absence of memory writes alone does not establish that it can be duplicated or moved if it can trap, fail to terminate, or interact with other observable behavior."
    },
    {
      "heading": "Program analysis and optimization",
      "text": "An IR provides the vocabulary on which compiler optimizations operate. Common transformations include constant folding, dead-code elimination, common-subexpression elimination, inlining, loop transformations, and vectorization. Their effectiveness depends on which properties the representation makes explicit. A structured loop representation exposes iteration bounds directly, while a low-level branch graph may require analysis to rediscover them. Explicit vector operations can preserve parallel intent that would otherwise need to be inferred from scalar code.\n\nMany optimizations depend on data-flow analysis, which propagates information through a program until it stabilizes. A constant-propagation analysis, for example, may classify a value as unknown, a specific constant, or nonconstant. At a control-flow merge, information from predecessor paths must be combined conservatively. In SSA, direct links between definitions and uses allow sparse analyses to focus on affected values rather than repeatedly processing every instruction in every block.\n\nMemory optimization illustrates why representation and analysis must work together. Consider a load from address p, a store through address q, and then another load from p. Reusing the first loaded value is valid only if the intervening store cannot modify the relevant memory and no other applicable rule forbids reuse. Alias analysis may prove that the addresses refer to distinct objects. Call summaries can similarly establish whether a function reads or writes particular memory regions. Without such information, an optimizer must retain potentially necessary operations.\n\nLoop optimization often involves several interacting analyses. Moving an expression outside a loop requires establishing that its operands are invariant and that earlier evaluation is legal. Even a loop-invariant division cannot always be hoisted: if the loop executes zero times, evaluating a division before entry might introduce a fault that the original program never produced. Vectorization additionally requires suitable dependence relationships, supported operations, and a treatment of exceptional or masked execution consistent with the program’s semantics.\n\nOptimization passes also have costs and interactions. Inlining can reveal constants and eliminate calls, but it increases code size and may increase pressure on instruction caches. Unrolling can expose parallel work while increasing register demand. A heuristic or cost model estimates whether a legal transformation is likely to help on the intended target. The phase-ordering problem arises because one transformation can expose or destroy opportunities for another. Production compilers therefore use carefully arranged pipelines, repeated cleanup passes, profiling information, and target-specific policies rather than assuming that every locally simplifying rewrite improves the final executable."
    },
    {
      "heading": "Lowering, instruction selection, and machine constraints",
      "text": "Lowering translates operations into forms that are more explicit or closer to the execution target. It may replace a high-level operation with several lower-level operations, convert structured control constructs into branches, or implement an unsupported primitive through a runtime call. Lowering need not produce machine instructions immediately. A compiler can move through several intermediate levels, each making a different set of decisions.\n\nAn array access, for example, can begin as an operation on a multidimensional array with shape and layout information. Later stages may express bounds checks, index arithmetic, and element access separately. Still later, the address becomes a base pointer plus byte offsets, and the access becomes a target load or store. If the representation discards array shape too early, transformations such as loop interchange or tiling become harder because the compiler must reconstruct relationships from pointer arithmetic.\n\nInstruction selection maps IR operations to instructions supported by the target. A target instruction may implement a whole pattern rather than a single operation: an addressing mode might combine a base, a scaled index, and a constant displacement. Conversely, one IR operation may require several instructions. Legalization handles unsupported types or operations, such as arithmetic on integers wider than the processor’s native registers. The selected sequence must respect both the IR semantics and the target’s actual behavior.\n\nRegister allocation then maps temporary values onto a finite set of physical registers, introducing spills to memory when necessary. Machine-oriented IRs must describe register classes, fixed-register requirements, instructions that overwrite an input, and values that remain live across calls. The application binary interface supplies additional rules: where arguments and results are placed, which registers a callee must preserve, how stack frames are aligned, and how certain data types are passed.\n\nThese constraints create feedback between stages. An operation that appears inexpensive in a target-independent IR may require a costly instruction sequence on one processor. Aggressive scheduling can increase the number of simultaneously live values and therefore cause spills. A fused multiply-add instruction also cannot always replace a separate multiplication and addition because its single rounding step can change the result. Target-aware optimization consequently begins before final instruction emission, while some decisions remain deliberately delayed until the machine’s constraints are explicit. Most mature compilers use a family of related representations to manage this gradual commitment."
    },
    {
      "heading": "Historical development and major compiler infrastructures",
      "text": "Intermediate program forms appeared in early compilation systems as a practical way to separate language processing from machine-specific translation. The terminology and structures were not uniform: compilers used trees, lists of operations, temporary variables, and machine-oriented pseudo-instructions. Their importance increased as language implementations grew more sophisticated and as the same language needed to support multiple machines.\n\nIn 1969, John McCarthy, James Painter, and other early researchers had already helped establish mathematical approaches to compiler correctness, while practical optimizing compilers were developing systematic program analysis. A particularly influential later foundation was Frances E. Allen’s work on control-flow analysis, including her 1970 paper “Control Flow Analysis.” Such research made program structure an explicit object of analysis rather than merely an incidental consequence of translation. In 1991, Ron Cytron, Jeanne Ferrante, Barry K. Rosen, Mark N. Wegman, and F. Kenneth Zadeck published an influential algorithmic treatment of SSA construction and control dependence. SSA subsequently became central to many optimizing compilers.\n\nThe GNU Compiler Collection, originating in Richard Stallman’s compiler released in 1987, illustrates a layered approach. GCC uses GENERIC and GIMPLE for relatively high- and middle-level processing and RTL, or register transfer language, for lower-level compilation. GIMPLE simplifies complex constructs and supports SSA-based optimization. RTL expresses operations closer to machine behavior and supports target-dependent processing. These representations are not simply alternative print formats: they serve different stages and preserve different kinds of information.\n\nLLVM began as a research project at the University of Illinois, with Chris Lattner and Vikram Adve describing its compilation framework in 2004. LLVM IR uses typed operations, explicit control flow, and SSA values. Its in-memory form, textual assembly form, and bitcode encoding support different workflows around a common representation. The wider LLVM project also includes distinct machine-level representations, so “LLVM IR” should not be treated as the only internal language used throughout code generation.\n\nMore recent infrastructure has emphasized extensibility and multiple abstraction levels. MLIR, initially developed at Google, organizes operations and types into dialects, allowing domain-specific and lower-level constructs to coexist within a common framework. Its design was presented in a 2020 paper by Chris Lattner and collaborators. This approach addresses the difficulty of forcing heterogeneous domains into one fixed instruction vocabulary. Across these systems, a recurring historical development is the shift from one private intermediate notation toward reusable infrastructures with explicit semantics, analysis interfaces, verification rules, and configurable transformation pipelines."
    },
    {
      "heading": "Domain-specific representations and portable execution formats",
      "text": "Domain-specific IRs preserve operations whose structure would be difficult to recover after generic lowering. In machine learning, a graph may represent convolutions, matrix products, reductions, tensor reshaping, and activation functions. Such operations carry shapes, element types, and sometimes layout constraints. An optimizer can fuse adjacent operators, eliminate redundant data movement, or choose specialized kernels before translating the result into loops and machine instructions.\n\nThis is especially important for graphics processing units and other parallel accelerators. A matrix operation does not by itself specify how work is divided among threads, which data is placed in shared memory, or how values are tiled into registers. Successive IR levels can separate the mathematical computation from execution mapping and physical layout. Automatic differentiation can also transform an IR to construct derivative computations, making the representation part of both model execution and training infrastructure.\n\nDatabase systems use representations with a different semantic emphasis. A query may first become a logical plan containing scans, filters, joins, and aggregations, and then a physical plan specifying execution algorithms and access paths. A query optimizer can reorder joins or push filters toward data sources, subject to the language’s treatment of null values, duplicates, and other observable behavior. The physical plan may be interpreted directly or compiled into lower-level code. Here, the useful intermediate abstraction is relational or data-processing structure rather than processor instructions.\n\nPortable execution formats overlap with IRs but serve additional purposes. Bytecode for a virtual machine may be interpreted or compiled at runtime, with verification and runtime services defined by the platform. Java bytecode uses an operand-stack model and retains information needed by the Java Virtual Machine. WebAssembly is a portable low-level instruction format designed for validation and execution across implementations. These formats are intermediate with respect to native execution, yet they are also externally specified deployment interfaces, unlike many private compiler IRs.\n\nGraphics systems provide another example. SPIR-V is a binary intermediate language standardized by the Khronos Group and used in graphics and compute ecosystems. Its capabilities and execution environments constrain which programs are valid. Such formats demonstrate why “intermediate” does not mean temporary or purely internal: a representation may be stored, distributed, validated, and consumed independently. However, a stable interchange format must support compatibility and security requirements that a compiler’s rapidly changing internal IR may not have. Most execution systems therefore translate portable input formats into their own optimization-oriented representations."
    },
    {
      "heading": "Correctness, inspection, and representation design",
      "text": "Every IR transformation carries a correctness obligation. Informally, the transformed program must preserve the observable behavior required by the source and intermediate semantics. This is often a refinement relation rather than literal equality of all possible internal steps. An optimizer can remove a computation whose result is unused, but it cannot remove an externally visible effect merely because no local instruction consumes a returned value.\n\nAn IR verifier checks structural and local semantic invariants: operands must have permitted types, branch destinations must exist, blocks must be properly terminated, and SSA uses must obey dominance rules. Such checks catch malformed transformations early. They do not establish that a well-formed transformed program computes the same result as its input. A pass can produce valid IR while introducing a subtle arithmetic error or moving a memory operation across an incompatible effect.\n\nFormal verification addresses stronger guarantees. The CompCert compiler, developed under Xavier Leroy’s leadership, uses multiple intermediate languages and machine-checked proofs of semantic preservation for its verified compilation passes. Another approach, translation validation, checks the relationship between particular input and output programs rather than proving the optimizer implementation correct once and for all. Tools such as Alive2 analyze LLVM IR transformations using formal semantics and satisfiability modulo theories solving. Their guarantees depend on the supported semantics and the scope of the particular check.\n\nInspection and testing remain essential in practical development. Textual IR dumps let compiler engineers identify the pass that introduced an incorrect operation or lost an optimization opportunity. Small IR-level regression tests can isolate transformations more directly than large source programs. Randomized testing and differential testing compare implementations or optimization settings, while reduction tools shrink failing cases. Debug information must also survive transformations sufficiently well to relate generated behavior to source locations and variables, even when inlining, elimination, and instruction movement have altered the original structure.\n\nRepresentation design therefore involves trade-offs among expressiveness, simplicity, stability, and implementation cost. A rich IR can expose valuable facts but impose more complicated rewrite rules. A minimal IR can make generic tooling easier while forcing later passes to reconstruct structure. Extensible systems need clear ownership of semantics: adding a new operation is insufficient unless its effects, verification rules, and interactions with transformations are defined. Persistent interchange formats additionally require versioning and compatibility policies, whereas internal IRs can evolve with the compiler. These choices determine not only optimization quality but also how reliably the compilation system can be tested, extended, and maintained."
    }
  ],
  "links": [
    "compiler",
    "source-code",
    "machine-code",
    "assembly-language",
    "programming-language",
    "abstract-syntax-tree",
    "computational-graph",
    "aliasing",
    "type-system",
    "c-programming-language",
    "ieee-seven-five-four",
    "memory-model",
    "concurrent-computing",
    "loop-invariant",
    "heuristic",
    "machine-learning",
    "graphics-processing-unit",
    "automatic-differentiation",
    "database",
    "query-optimizer",
    "virtual-machine",
    "formal-verification",
    "compcert",
    "satisfiability-modulo-theories"
  ],
  "sources": [],
  "license": "https://creativecommons.org/licenses/by/4.0/"
}