A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Intermediate Code Representation

Graduate Depth 92 in the knowledge graph I know this Set as goal
34topics build on this
502prerequisites beneath it
See this on the map →
Abstract Syntax Trees (ASTs)Semantic Analysis PhaseBasic Block AnalysisBytecode Intermediate Representation and Virtual Machines+5 more
intermediate-representation ir compilation-phases

Core Idea

Intermediate representation (IR) is an abstraction between source and target languages. Common forms include three-address code (TAC), register-transfer language (RTL), and bytecode. IR simplifies optimization and retargeting: optimize once on IR, then generate code for multiple targets. IR abstracts away source-language details and target-machine specifics, enabling machine-independent transformations.

Explainer

After semantic analysis, you have an AST annotated with types and scope information — a tree that faithfully represents the structure of the source program. But an AST is a poor target for optimization and code generation: its structure mirrors the source language's syntax, not the machine's execution model, and tree transformations are awkward for the linear, instruction-by-instruction reasoning that optimization requires. Intermediate representation is the bridge: a language-neutral, machine-neutral format that is low-level enough to reason about execution but high-level enough to support powerful transformations before committing to any specific target architecture.

The most common IR form is three-address code (TAC), where every instruction has at most one operator and up to three operands: `t1 = a + b`, `t2 = t1 * c`, `if t2 > 0 goto L1`. Complex source expressions are decomposed into sequences of simple operations using temporary variables. The expression `a + b * c - d` becomes something like `t1 = b * c; t2 = a + t1; t3 = t2 - d`. This flat, explicit form makes data flow visible — you can see exactly which temporaries feed into which operations — and is easy to analyze for optimization. Control flow constructs like loops and conditionals become explicit labels and goto instructions, making the control flow graph straightforward to extract.

The strategic value of IR is the m × n problem. Without IR, supporting m source languages and n target machines requires m × n separate translators. With a common IR, you need only m frontends (source → IR) and n backends (IR → machine code), for m + n total components. This is why LLVM's IR is so influential: any language frontend that emits LLVM IR gets access to LLVM's entire suite of optimizations and all its target backends, from x86 to ARM to WebAssembly. The same principle applies at a smaller scale within a single compiler — machine-independent optimizations like constant folding, dead code elimination, and common subexpression elimination are written once on the IR and apply regardless of what source language produced it or what target will consume it.

Different compilers use IRs at different abstraction levels, and some use multiple IR levels. A high-level IR might preserve loop structure and array indexing; a low-level IR might expose individual memory loads, stores, and register-like temporaries. Static Single Assignment (SSA) form — where each variable is assigned exactly once — is a particularly powerful IR variant that simplifies many optimization analyses by making data flow explicit. Bytecode formats like the JVM's or Python's are also IRs, interpreted by a virtual machine rather than compiled to hardware. The choice of IR shapes what optimizations are easy to express: design the right intermediate language, and the optimizations almost write themselves.

Practice Questions 5 questions

Prerequisite Chain

Understanding ZeroThe Number ZeroCounting to FiveCounting to 10Counting to 20Counting a Set of Objects Up to 20Cardinality: The Last Number CountedMatching Numerals to QuantitiesSubitizing Small QuantitiesAddition Within 10Number Bonds to 10Addition Within 20Doubles and Near DoublesDoubles Facts Within 10Near Doubles Facts Within 20Mental Math Strategies for AdditionMental Math: Adding and Subtracting TensAddition Within 100Repeated Addition as MultiplicationMultiplication as Equal GroupsMultiplication: ArraysBasic Multiplication Facts (0s, 1s, 2s, 5s, 10s)Multiplication Facts Within 100Division as Equal SharingDivision as Grouping (Measurement Division)Division: Grouping (Repeated Subtraction) ModelDivision: Fair Sharing ModelDivision as Equal SharingDivision as GroupingBasic Division FactsDivision Facts Within 100Multiplication and Division Fact FamiliesRelationship Between Multiplication and DivisionDivision Facts as Inverse of MultiplicationRemainders and Quotients in DivisionDivision Word ProblemsMulti-Step Word ProblemsSolving Multi-Step Word ProblemsMultiplication Word ProblemsDivision Word ProblemsIntroduction to Long DivisionFactors and MultiplesPrime and Composite NumbersEquivalent FractionsRelating Fractions and DecimalsDecimal Place ValueIntegers and the Number LineComparing and Ordering IntegersAbsolute ValueAdding IntegersSubtracting IntegersMultiplying IntegersIntroduction to ExponentsOrder of OperationsInteger Order of OperationsVariable ExpressionsThe Distributive PropertyVariables and Expressions ReviewIntroduction to PolynomialsAdding and Subtracting PolynomialsMultiplying PolynomialsFactorialPermutationsCombinationsCounting Principles: Addition and Multiplication RulesIntroduction to Graph TheoryPropositional Logic FoundationsLogical EquivalencesBoolean AlgebraBoolean Type and Truth ValuesComparison Operators and Boolean TestsLogical Operators and Boolean AlgebraBoolean Algebra and Fundamental LawsLogic Gates FundamentalsImplementing Boolean Functions with GatesKarnaugh Map SimplificationCombinational Circuit DesignFlip-Flops and LatchesFinite State Machines (FSMs)Deterministic Finite Automata (DFA)Nondeterministic Finite Automata (NFA)Two-Way Finite AutomataNFA to DFA Conversion (Subset Construction)DFA Properties and Minimization AlgorithmsRegular Languages: Definition and CharacterizationContext-Free Grammars (CFGs)Context-Free Grammar Properties and AmbiguityParse Trees, Derivations, and Ambiguity in CFGsContext-Free Grammars in Compiler DesignAbstract Syntax Trees (ASTs)Symbol Tables and Scope ResolutionSemantic Analysis PhaseIntermediate Code Representation

Longest path: 93 steps · 502 total prerequisite topics

Prerequisites (2)

Leads To (7)