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

Constant Propagation and Folding

Graduate Depth 100 in the knowledge graph ☐ I know this ☆ Set as goal
536prerequisites beneath it
See this on the map →
Code Optimization FundamentalsReaching Definitions Analysis→
optimization constant-propagation algebraic-simplification

Core Idea

Constant propagation identifies variables assigned constant values and replaces their uses with the constant. Constant folding evaluates constant expressions at compile-time. For example, `x = 5; y = x + 3` becomes `y = 8`. This simple optimization enables further simplifications and can expose dead code.

Explainer

From reaching definitions analysis, you know how to determine, for each point in a program, which assignments could have produced the current value of a variable. Constant propagation builds directly on this: if every reaching definition of a variable assigns it the same constant value, then every use of that variable can be replaced with that constant. The compiler does not need to wait until runtime to look up the variable — it already knows the answer.

Consider a straightforward example. If the program says `x = 7` on line 3 and no other assignment to x reaches line 10, then at line 10 the compiler knows x is 7 and can substitute the literal value directly. Constant propagation performs this substitution throughout the program. Constant folding is the companion step: once propagation has replaced variables with constants, expressions like `7 + 3` can be evaluated at compile time to produce `10`. Together, these two transformations often chain — propagating a constant enables folding an expression, which produces a new constant that can be propagated further.

The analysis works on the control flow graph using a lattice of values for each variable. Each variable starts as "undefined" (no assignment has been seen), can become a specific constant (exactly one constant value reaches this point), or can become "not a constant" (multiple different values reach this point, or the value depends on runtime input). At merge points in the control flow — where two branches of an if-statement rejoin — the values from both paths are combined: if both paths agree on the same constant, the variable remains that constant; if they disagree, the variable becomes "not a constant." This is a forward dataflow analysis that iterates until the lattice values stabilize at a fixed point.

The power of constant propagation lies in what it enables downstream. Replacing a variable with a constant can make a branch condition statically evaluable — if `x` is known to be 7, then `if (x > 5)` is always true, and the compiler can eliminate the branch and its dead else-block entirely. This dead code elimination shrinks the program, which in turn may expose more constants by removing conflicting assignments. Many compiler optimizations work this way: each pass creates opportunities for the next, and constant propagation is often one of the first and most impactful passes in the sequence because its simplifications cascade broadly.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Boolean Algebra and Fundamental Laws → Logic Gates Fundamentals → Implementing Boolean Functions with Gates → Karnaugh Map Simplification → Combinational Circuit Design → Flip-Flops and Latches → Finite State Machines (FSMs) → Deterministic Finite Automata (DFA) → Nondeterministic Finite Automata (NFA) → Two-Way Finite Automata → NFA to DFA Conversion (Subset Construction) → DFA Properties and Minimization Algorithms → Regular Languages: Definition and Characterization → Context-Free Grammars (CFGs) → Context-Free Grammar Properties and Ambiguity → Parse Trees, Derivations, and Ambiguity in CFGs → Context-Free Grammars in Compiler Design → Abstract Syntax Trees (ASTs) → Symbol Tables and Scope Resolution → Semantic Analysis Phase → Intermediate Code Representation → Control Flow Graphs → Fixpoint Computation and Iteration → Dataflow Analysis → Reaching Definitions Analysis → Common Subexpression Elimination (CSE) → Dead Code Elimination → Code Optimization Fundamentals → Constant Propagation and Folding

Longest path: 101 steps · 536 total prerequisite topics

Prerequisites (2)

Leads To (0)

No topics depend on this one yet.