Common Subexpression Elimination (CSE)

Graduate Depth 71 in the knowledge graph I know this Set as goal
Unlocks 11 downstream topics
optimization cse expression-reuse

Core Idea

Common subexpression elimination detects and removes redundant computations. If the same expression is computed multiple times with unchanged operands, compute it once and reuse the result. CSE requires tracking when expressions are available (all operands have definitions reaching the current point) and when they are not killed (operands not reassigned).

Explainer

Consider this fragment of intermediate code: `t1 = a + b; t2 = a + b;`. If neither `a` nor `b` is modified between the two statements, then `t2` will always equal `t1`. Computing `a + b` a second time is pure waste — the compiler can replace the second computation with `t2 = t1`. This is the essence of common subexpression elimination (CSE): find expressions that have already been computed with unchanged operands, and reuse the earlier result instead of recomputing.

The challenge is determining *when* an expression is safe to reuse. From your study of dataflow analysis and reaching definitions, you have the machinery to answer this. An expression `a + b` is available at a program point if every path from the start of the program to that point computes `a + b`, and neither `a` nor `b` is redefined after the most recent computation. If any path redefines an operand without recomputing the expression, the earlier value may be stale and cannot be reused. The compiler solves this with available expressions analysis, a forward dataflow problem: the gen set at each statement includes expressions it computes, the kill set includes all expressions containing variables it redefines, and the meet operation at join points takes the intersection (an expression is only available if it is available on *all* incoming paths).

There are two scopes of CSE. Local CSE operates within a single basic block — a straight-line sequence of instructions with no branches. This is simple because there is only one path, so you just scan forward, maintaining a table of computed expressions and replacing duplicates. Global CSE operates across an entire function's control flow graph, handling branches and loops. Global CSE requires the full available expressions dataflow analysis, which propagates information across basic block boundaries. The result is more powerful: it can catch redundancies across branches, in loop bodies, and between distant parts of the function.

CSE interacts productively with other optimizations. Copy propagation can expose new common subexpressions by replacing variable copies with their originals, making previously different-looking expressions identical. Constant folding can simplify operands, again revealing matches. In loops, CSE often works hand-in-hand with loop-invariant code motion: an expression computed inside a loop whose operands never change across iterations is both a common subexpression and loop-invariant, and can be hoisted out of the loop entirely. The compiler's optimization pipeline typically runs these passes in sequence, with each pass creating opportunities for the next.

Practice Questions 5 questions

Prerequisite Chain

Counting to 10Counting to 20Understanding ZeroThe Number ZeroCounting to FiveOne-to-One CorrespondenceCombining Small Groups Within 5Addition Within 10Addition Within 20Two-Digit Addition Without RegroupingTwo-Digit Addition with RegroupingAddition Within 100Repeated Addition as MultiplicationMultiplication 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 100Two-Digit by One-Digit DivisionDivision with RemaindersRemainders and Quotients in DivisionDivision Word ProblemsIntroduction to Long DivisionFactors and MultiplesPrime and Composite NumbersEquivalent FractionsRelating Fractions and DecimalsDecimal Place ValueReading and Writing DecimalsComparing and Ordering DecimalsAdding and Subtracting DecimalsMultiplying DecimalsDividing DecimalsDividing FractionsMixed Number ArithmeticOrder of OperationsInteger Order of OperationsVariable ExpressionsCombining Like TermsOne-Step EquationsTwo-Step EquationsSolving Multi-Step EquationsEquations with Variables on Both SidesLiteral EquationsSlope-Intercept FormPoint-Slope FormWriting Linear EquationsParallel and Perpendicular Line SlopesGraphing Linear EquationsPiecewise FunctionsStep FunctionsComposition of FunctionsInverse FunctionsRadical Functions and GraphsRational ExponentsExponential Functions and GraphsLogarithms IntroductionTime and Space ComplexityAmortized AnalysisHash TablesSymbol Tables and Scope ResolutionSemantic Analysis PhaseIntermediate Code RepresentationControl Flow GraphsFixpoint Computation and IterationDataflow AnalysisReaching Definitions AnalysisCommon Subexpression Elimination (CSE)

Longest path: 72 steps · 377 total prerequisite topics

Prerequisites (2)

Leads To (2)