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

Value Numbering and Redundancy Elimination

Graduate Depth 100 in the knowledge graph I know this Set as goal
536prerequisites beneath it
See this on the map →
Code Optimization FundamentalsCommon Subexpression Elimination (CSE)+1 more
optimization redundancy CSE

Core Idea

Value numbering assigns numbers to expressions based on their semantic value; identical expressions receive the same number. Redundant computations are then replaced with the first computation's result, achieving both common subexpression elimination and constant folding in a single, efficient pass.

Explainer

Consider a basic block containing `t1 = a + b` followed later by `t2 = a + b`, where `a` and `b` have not been reassigned. A human reader immediately sees that `t2` is redundant — it computes the same thing `t1` already holds. Value numbering is the compiler's systematic way of recognizing this. It assigns each computed value a unique number, and when it encounters an expression whose operands have the same value numbers as a previously computed expression with the same operator, it reuses the earlier result instead of recomputing.

The algorithm maintains a hash table mapping (operator, value-number-of-left-operand, value-number-of-right-operand) to value numbers. As it processes each instruction in order, it looks up the operands' value numbers, forms the hash key, and checks the table. If the key is already present, the expression is redundant — the compiler replaces it with a copy from the variable that already holds that value. If the key is absent, a new value number is assigned and recorded. Constants receive value numbers too, which means constant folding falls out naturally: `3 + 4` hashes to the same entry as any other expression producing 7.

Local value numbering (LVN) operates within a single basic block and is simple to implement — a single forward pass suffices. From your knowledge of code optimization and dataflow analysis, you can appreciate why extending this across basic blocks is harder. Global value numbering (GVN) must reason about values that flow through multiple paths in the control flow graph. If `a + b` is computed in two predecessor blocks but with different assignments to `a`, the value numbers may differ along different paths. GVN typically uses a dominator-based approach: a computation in a dominating block is available to all blocks it dominates, so redundancies within a dominator tree can be eliminated safely.

Value numbering is particularly effective because it subsumes several optimizations at once. It eliminates common subexpressions, folds constants, and can even detect algebraic identities (like `x + 0` or `x * 1`) if extended with simple rewrite rules. It is also efficient — local value numbering is linear in the number of instructions, making it one of the best cost-to-benefit optimizations a compiler can perform.

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 RepresentationControl Flow GraphsFixpoint Computation and IterationDataflow AnalysisReaching Definitions AnalysisCommon Subexpression Elimination (CSE)Dead Code EliminationCode Optimization FundamentalsValue Numbering and Redundancy Elimination

Longest path: 101 steps · 536 total prerequisite topics

Prerequisites (3)

Leads To (0)

No topics depend on this one yet.