Skip to content

Lesson 17.1 — Constant propagation: Kildall, sparse, and conditional

Techniques: Kildall's dense constant propagation (1973); sparse simple constant propagation on SSA (Reif–Lewis 1977, Wegman–Zadeck's SSC); sparse conditional constant propagation, SCCP (Wegman–Zadeck 1991), with dense conditional CP (Wegbreit 1975) as its ancestor and interprocedural SCCP as a preview of Ch 20 · Pebble implements: pebble-sccp (exercise E1); simple CP and SCCP side by side in the comparison lab (labs/ch17-scalar, Part A) · Prerequisites: lattices and fixed points (Lesson 14.1), constant propagation as a dataflow problem (Lesson 14.3), sparse propagation (Lesson 14.6), SSA (Ch 16) · Time: 6–8 hours

Lesson 14.6 introduced SCCP as one example of sparse analysis. This lesson treats constant propagation as a family: four algorithms that differ along two independent axes, dense vs sparse (facts per program point or per SSA value) and unconditional vs conditional (every edge assumed executable, or edges discovered as the analysis runs). The running example of the whole chapter is the function below, compiled by clang-23 -O0 and promoted to SSA by mem2reg. It always returns 1, but no single classic pass can prove it; this lesson proves x = 1, Lesson 17.5 proves i = j, and Lesson 17.8 shows why you need both.

int run(int n) {
  int x = 1, i = 0, j = 0;
  while (i < n) {
    if (x != 1)
      x = 2;
    i = i + 1;
    j = j + 1;
  }
  int t = i * 4, u = j * 4;
  return t - u + x;
}

In the chapter's compact SSA notation (the format of the drills and of tools/course/lib/scalaropt.py; value and block names are exactly LLVM's, lt is icmp slt):

fn run(n):
entry:
  br while.cond
while.cond:
  i.0 = phi [0, entry], [add, if.end]
  x.0 = phi [1, entry], [x.1, if.end]
  j.0 = phi [0, entry], [add2, if.end]
  cmp = lt i.0, n
  cbr cmp, while.body, while.end
while.body:
  cmp1 = ne x.0, 1
  cbr cmp1, if.then, if.end
if.then:
  br if.end
if.end:
  x.1 = phi [2, if.then], [x.0, while.body]
  add = add i.0, 1
  add2 = add j.0, 1
  br while.cond
while.end:
  mul = mul i.0, 4
  mul3 = mul j.0, 4
  sub = sub mul, mul3
  add4 = add sub, x.0
  ret add4
flowchart TD
  entry([entry]) --> wc[while.cond]
  wc -->|T| wb[while.body]
  wc -->|F| we[while.end]
  wb -->|T| it[if.then]
  wb -->|F| ie[if.end]
  it --> ie
  ie --> wc

1. Problem and motivation

A value is a constant if every execution that computes it computes the same number. Knowing that lets the compiler fold the instruction, delete branches whose condition it decides, and feed smaller programs to every later pass; LLVM runs sccp in its function pipeline and ipsccp once per module at -O2 [LLVM-Pipelines], GCC runs CCP several times [GCC-CCP]. The question is undecidable in general, so each algorithm computes a sound approximation, and the four algorithms of this lesson form a precision ladder.

Kildall's dense constant propagation

Kildall's 1973 paper introduced iterative dataflow analysis with constant propagation as its first example [Kil73]: keep, at every program point, a map from variables to "the constant \(c\), for some \(c \in \mathbb{Z}_{32}\), or not a constant", and iterate the transfer functions around the CFG until nothing changes. It works on any IR, needs no SSA, and is the specification against which later algorithms are measured. Its cost is density: every map is copied through every block, whether or not the block mentions the variable.

Sparse simple constant propagation

Reif and Lewis observed that constant facts only need to travel from a definition to its uses [RL77, RL86]. On SSA, def-use edges are the SSA edges, so the fact for a value is stored once and each instruction is re-evaluated only when one of its operands changes. Wegman and Zadeck call this the sparse simple constant algorithm (SSC) [WZ91, §3]. It finds exactly the constants Kildall finds on the same SSA program (Theorem 17.1.11), in time proportional to the number of SSA edges instead of blocks × variables.

Sparse conditional constant propagation

Both previous algorithms assume every CFG edge can execute. In the running example they conclude that x.0 is not a constant, because the dead block if.then merges the value 2 into x.1. Wegbreit showed in 1975 that a dense analysis can instead discover which branches are possible as it goes [Weg75]; Wegman and Zadeck combined that idea with sparseness into SCCP [WZ91]. SCCP proves x.0 = 1, deletes if.then, and is strictly more powerful than running constant propagation and unreachable-code elimination to a fixed point (Theorem 17.1.12). pebble-sccp (exercise E1) implements it on LLVM IR; the lab measures the gap between simple CP and SCCP on a corpus.

2. Definitions and algorithms

Notation follows the chapter's Notation section. Throughout the lesson, let \(F\) be the function being analysed and \(G = (N, E, r)\) its CFG (NOTATION §3): \(N\) its basic blocks, \(E \subseteq N \times N\) its edges and \(r \in N\) its entry block, with \(n = \lvert N \rvert\) and \(e = \lvert E \rvert\). Write \(\mathbb{Z}_{32}\) for the 32-bit two's-complement integers, and write \(\mathrm{add}\), \(\mathrm{sub}\), \(\mathrm{mul}\), \(\mathrm{and}\), \(\mathrm{lt}\), \(\mathrm{ne}\), … for the binary operators of the compact SSA notation, each a total function \(\mathbb{Z}_{32} \times \mathbb{Z}_{32} \to \mathbb{Z}_{32}\) (comparisons return 0 or 1). When \(F\) is in SSA form, write \(\mathit{Val}\) for its set of SSA values (parameters, phis and instruction results), \(\mathit{Par} \subseteq \mathit{Val}\) for its parameters, \(I\) for its number of instructions and \(U\) for its number of SSA edges (operand uses, Definition 17.1.5); a literal constant \(c \in \mathbb{Z}_{32}\) may appear wherever an operand is expected. Every numbered box restates the objects it uses.

Definition 17.1.1 (Constant lattice)

The constant lattice is the flat lattice \(L_c \triangleq \mathbb{Z}_{32} \cup \{\bot, \top\}\), ordered by \(\bot \sqsubseteq c \sqsubseteq \top\) for every \(c \in \mathbb{Z}_{32}\), with distinct integers incomparable and \(\sqsubseteq\) reflexive. Its join is, for all \(c, d \in \mathbb{Z}_{32}\) with \(c \neq d\) and every \(x \in L_c\): \(c \sqcup c = c\), \(c \sqcup d = \top\), \(\bot \sqcup x = x\), \(\top \sqcup x = \top\). The element \(\bot\) is read no evidence yet (the optimistic start), an element \(c \in \mathbb{Z}_{32}\) is read the constant \(c\), and \(\top\) is read overdefined (not a constant). The height of \(L_c\) is \(h(L_c) = 2\).

This is Ch 14's orientation (facts rise). LLVM's ValueLatticeElement calls the three kinds of element unknown, constant and overdefined; GCC's CCP calls them UNDEFINED, CONSTANT and VARYING.

Definition 17.1.2 (Constant value, concretization, abstract evaluation)

Let \(F\) be a function and \(v\) one of its SSA values or variables. \(v\) is the constant \(c\), for \(c \in \mathbb{Z}_{32}\), if every execution of \(F\), on every input, that evaluates the definition of \(v\) assigns the value \(c\) to it. The concretization is the function \(\gamma : L_c \to \mathcal{P}(\mathbb{Z}_{32})\) with \(\gamma(\bot) = \emptyset\), \(\gamma(c) = \{c\}\) for \(c \in \mathbb{Z}_{32}\), and \(\gamma(\top) = \mathbb{Z}_{32}\). For a binary operator \(\mathit{op} : \mathbb{Z}_{32} \times \mathbb{Z}_{32} \to \mathbb{Z}_{32}\) the abstract evaluation is the function \(\widehat{\mathit{op}} : L_c \times L_c \to L_c\) with, for all \(\ell_1, \ell_2 \in L_c\): \(\widehat{\mathit{op}}(\ell_1, \ell_2) = \top\) if \(\ell_1 = \top\) or \(\ell_2 = \top\); otherwise \(\bot\) if \(\ell_1 = \bot\) or \(\ell_2 = \bot\); otherwise \(\mathit{op}(\ell_1, \ell_2)\) computed in \(\mathbb{Z}_{32}\).

Two properties of \(\widehat{\mathit{op}}\) are used by every proof of §4 and follow directly from the three cases of the definition: it is monotone (if \(\ell_1 \sqsubseteq \ell_1'\) and \(\ell_2 \sqsubseteq \ell_2'\) then \(\widehat{\mathit{op}}(\ell_1, \ell_2) \sqsubseteq \widehat{\mathit{op}}(\ell_1', \ell_2')\): raising an argument from \(\bot\) to a constant moves the result from \(\bot\) up, and raising one to \(\top\) moves it to \(\top\)), and it is sound: \(\mathit{op}(a_1, a_2) \in \gamma(\widehat{\mathit{op}}(\ell_1, \ell_2))\) whenever \(a_1 \in \gamma(\ell_1)\) and \(a_2 \in \gamma(\ell_2)\) (if either \(\gamma(\ell_i)\) is empty there is nothing to show; if either is \(\mathbb{Z}_{32}\) so is the result's; otherwise both are singletons and the result is the singleton \(\{\mathit{op}(\ell_1, \ell_2)\}\)).

Abstract evaluation

\(\widehat{\mathrm{add}}(20, 1) = 21\); \(\widehat{\mathrm{mul}}(\top, 0) = \top\) (Definition 17.1.2 is deliberately simple; LLVM refines this case to 0, see §6); \(\widehat{\mathrm{lt}}(\bot, 5) = \bot\): an operand without evidence produces no evidence.

Definition 17.1.3 (Dense constant-propagation framework)

Let \(F\) be a function with CFG \(G = (N, E, r)\), a finite set \(\mathit{Vars}\) of variables, a set \(\mathit{Params} \subseteq \mathit{Vars}\) of parameters, and a sequence of statements in every block \(B \in N\), each statement of the form \(x \gets \mathit{e}\) with \(x \in \mathit{Vars}\) and \(\mathit{e}\) an expression over \(\mathit{Vars} \cup \mathbb{Z}_{32}\) built from the operators of Definition 17.1.2. An environment is a map \(\rho : \mathit{Vars} \to L_c\); environments are ordered pointwise, and \(\rho_\bot\) is the environment with \(\rho_\bot(x) = \bot\) for every \(x \in \mathit{Vars}\). The abstract value of an expression \(\mathit{e}\) in \(\rho\), written \(\hat{\mathit{e}}(\rho)\), is \(\rho(x)\) if \(\mathit{e}\) is a variable \(x\), the constant \(c\) if \(\mathit{e}\) is a literal \(c \in \mathbb{Z}_{32}\), and \(\widehat{\mathit{op}}(\hat{\mathit{e}}_1(\rho), \hat{\mathit{e}}_2(\rho))\) if \(\mathit{e} = \mathit{op}(\mathit{e}_1, \mathit{e}_2)\). The transfer function of the statement \(x \gets \mathit{e}\) is \(\rho \mapsto \rho[x \mapsto \hat{\mathit{e}}(\rho)]\); the transfer function \(f_B\) of a block \(B \in N\) is the composition of the transfer functions of its statements in order; branches do not change \(\rho\). The boundary environment \(\iota\) maps every parameter in \(\mathit{Params}\) to \(\top\) and every other variable to \(\bot\). The dense CP solution is the least solution \((\mathrm{IN}, \mathrm{OUT})\) of the system

\[ \mathrm{IN}[r] = \iota, \qquad \mathrm{IN}[B] = \bigsqcup_{P \in \mathrm{preds}(B)} \mathrm{OUT}[P] \quad (B \neq r), \qquad \mathrm{OUT}[B] = f_B(\mathrm{IN}[B]) \quad (B \in N), \]

an instance of the forward dataflow framework of Ch 14 (Definition 14.2.9).

Algorithm 17.1.4 (Kildall's dense constant propagation)

  • Input: a function \(F\) with CFG \(G = (N, E, r)\), variables \(\mathit{Vars}\), parameters \(\mathit{Params}\) and statements as in Definition 17.1.3.
  • Output: an environment \(\mathrm{OUT}[B] : \mathit{Vars} \to L_c\) for every block \(B \in N\).
  • Precondition: \(r\) has no predecessors, and every block of \(N\) is reachable from \(r\).
  • Postcondition: \(\mathrm{OUT}\) is the \(\mathrm{OUT}\) component of the dense CP solution of Definition 17.1.3 (Theorem 17.1.14).
  • State: the map \(\mathrm{OUT} : N \to (\mathit{Vars} \to L_c)\), initially \(\rho_\bot\) everywhere, and a flag changed.
  • Invariant: after every visit of a block \(B\), \(\mathrm{OUT}[B] \sqsubseteq\) the least solution's \(\mathrm{OUT}[B]\), and each \(\mathrm{OUT}[B]\) only rises over time.
function KildallCP(G, Params):
    for each block B ∈ N: OUT[B] ← ρ⊥
    order ← ReversePostorder(G)
    changed ← true
    while changed:                                  # one round-robin pass
        changed ← false
        for B in order:
            ρ ← (B = r) ? ι : join of OUT[P] over P in preds(B)    # pointwise ⊔
            for each statement x ← e in B, in order:
                ρ[x] ← AbstractEval(e, ρ)            # ê(ρ), Definition 17.1.3
            if ρ ≠ OUT[B]: OUT[B] ← ρ; changed ← true
    return OUT

Definition 17.1.5 (SSA value graph; sparse CP equations)

Let \(F\) be a function in strict SSA form (Ch 16) with SSA values \(\mathit{Val}\) and parameters \(\mathit{Par} \subseteq \mathit{Val}\); every value \(v \in \mathit{Val} \setminus \mathit{Par}\) is defined by exactly one instruction, either an operation \(v = \mathit{op}(a_1, a_2)\) with operands \(a_1, a_2 \in \mathit{Val} \cup \mathbb{Z}_{32}\), or a phi \(v = \phi(a_1 : P_1, \dots, a_m : P_m)\) in a block \(B\) with predecessors \(P_1, \dots, P_m\) and operands \(a_1, \dots, a_m \in \mathit{Val} \cup \mathbb{Z}_{32}\). A user of a value \(d\) is an instruction that has \(d\) as an operand. The SSA edges are the pairs \((d, u)\) with \(d \in \mathit{Val}\) and \(u\) a user of \(d\); write \(U\) for their number. A value state is a map \(V : \mathit{Val} \to L_c\); it is extended to operands by \(V(c) \triangleq c\) for a literal \(c \in \mathbb{Z}_{32}\). The sparse CP equations are: \(V(p) = \top\) for every \(p \in \mathit{Par}\), and for every \(v \in \mathit{Val} \setminus \mathit{Par}\)

\[ V(v) = \widehat{\mathit{op}}(V(a_1), V(a_2)) \ \text{ if } v = \mathit{op}(a_1, a_2), \qquad V(v) = \bigsqcup_{k=1}^{m} V(a_k) \ \text{ if } v = \phi(a_1 : P_1, \dots, a_m : P_m). \]

Value states are ordered pointwise; the sparse simple CP (SSC) solution is the least value state satisfying every equation.

Algorithm 17.1.6 (Sparse simple constant propagation, SSC)

  • Input: a function \(F\) in strict SSA form with values \(\mathit{Val}\) and parameters \(\mathit{Par}\) (Definition 17.1.5).
  • Output: a value state \(V : \mathit{Val} \to L_c\).
  • Precondition: strict SSA (every use is dominated by its definition).
  • Postcondition: \(V\) is the SSC solution of Definition 17.1.5.
  • State: the value state \(V\), initially \(\top\) on \(\mathit{Par}\) and \(\bot\) elsewhere, and the worklist \(W\), a FIFO queue of instructions. Adding an instruction to \(W\) appends it to the queue if it is not already in it; popping removes and returns the front instruction.
  • Invariant: \(V \sqsubseteq\) the SSC solution; every instruction whose equation is violated by the current \(V\) is in \(W\).
function SSC(F):
    V[p] ← ⊤ for every p ∈ Par;  V[v] ← ⊥ for every other value v
    W ← every instruction that defines a value, in program order
    while W ≠ []:
        I ← pop front of W
        new ← (I is a phi) ? ⊔ over all operands a of I: V(a)
                           : AbstractEval(I, V)           # ôp(V(a_1), V(a_2)), Definition 17.1.2
        if new ≠ V[I]:
            V[I] ← new
            add every user of I to W
    return V

The conditional algorithms add one more unknown per CFG edge: is it executable, i.e. can some execution traverse it? The next definition makes that unknown an object, the set \(X\).

Definition 17.1.7 (Executable edges; SCCP equations)

Let \(F\) be a function in strict SSA form with CFG \(G = (N, E, r)\), values \(\mathit{Val}\), parameters \(\mathit{Par}\) and value states \(V : \mathit{Val} \to L_c\) as in Definition 17.1.5, in which every block \(B \in N\) ends in a terminator: an unconditional branch to a block \(S \in N\), written br \(S\); a conditional branch on a condition \(c \in \mathit{Val}\) with a true target \(B_{\mathrm{t}} \in N\) and a false target \(B_{\mathrm{f}} \in N\), written cbr \(c, B_{\mathrm{t}}, B_{\mathrm{f}}\); or a return. Write \(\ast\) for a virtual node outside \(N\) with the single entry edge \((\ast, r)\). An executable-edge set is a set \(X \subseteq E \cup \{(\ast, r)\}\); its elements are called executable edges, and a block \(B \in N\) is executable (with respect to \(X\)) if some edge \((P, B) \in X\) enters it. The SCCP system over a pair \((V, X)\) consists of:

  1. \((\ast, r) \in X\).
  2. For every executable block \(B\): if \(B\) ends in br \(S\), then \((B, S) \in X\); if \(B\) ends in cbr \(c, B_{\mathrm{t}}, B_{\mathrm{f}}\), then \((B, B_{\mathrm{t}}) \in X\) whenever \(V(c) = \top\) or \(V(c)\) is a nonzero constant, and \((B, B_{\mathrm{f}}) \in X\) whenever \(V(c) = \top\) or \(V(c) = 0\); when \(V(c) = \bot\) neither edge is required.
  3. For every value \(v\) defined in an executable block \(B\): if \(v = \mathit{op}(a_1, a_2)\) then \(V(v) = \widehat{\mathit{op}}(V(a_1), V(a_2))\); if \(v = \phi(a_1 : P_1, \dots, a_m : P_m)\) then \(V(v) = \bigsqcup_{k : (P_k, B) \in X} V(a_k)\), the join over the indices \(k \in \{1, \dots, m\}\) whose edge is executable. For every value \(v\) defined in a non-executable block, \(V(v) = \bot\). For every \(p \in \mathit{Par}\), \(V(p) = \top\).

Pairs are ordered by \((V, X) \sqsubseteq (V', X')\) iff \(V \sqsubseteq V'\) pointwise and \(X \subseteq X'\). The SCCP solution is the least pair \((V, X)\) satisfying the system.

Dense conditional constant propagation, Wegbreit's algorithm [Weg75] (Wegman–Zadeck's CC), solves the same system with Kildall's environments in place of value states.

Algorithm 17.1.8 (Sparse conditional constant propagation, Wegman–Zadeck)

  • Input: a function \(F\) in strict SSA form with CFG \(G = (N, E, r)\), values \(\mathit{Val}\) and parameters \(\mathit{Par}\) (Definition 17.1.7).
  • Output: a value state \(V : \mathit{Val} \to L_c\) and an executable-edge set \(X \subseteq E \cup \{(\ast, r)\}\).
  • Precondition: strict SSA; every block ends in br, cbr or ret.
  • Postcondition: \((V, X)\) is the SCCP solution of Definition 17.1.7 (Theorem 17.1.9 (b)).
  • State: the value state \(V\), initially \(\top\) on \(\mathit{Par}\) and \(\bot\) elsewhere; the executable-edge set \(X\), initially \(\emptyset\); the set \(\mathit{Exec} \subseteq N\) of blocks already entered, initially \(\emptyset\); the flow worklist FlowWL, a FIFO queue of edges of \(E \cup \{(\ast, r)\}\); and the SSA worklist SSAWL, a FIFO queue of instructions.
  • Operations: for blocks \(P, B \in N\), marking an edge \((P, B)\) executable is the update \(X \gets X \cup \{(P, B)\}\); it asserts that rule 2 of Definition 17.1.7 requires \((P, B)\) for the current \((V, X)\). Pushing an edge appends it to FlowWL unless it is already in \(X\) or in FlowWL. Updating a value \(v\) to an element \(\ell \in L_c\) sets \(V[v] \gets \ell\) and, if \(\ell \neq\) the old \(V[v]\), appends every user of \(v\) not already in SSAWL to SSAWL. Visiting an instruction re-evaluates its equation (rule 2 or 3) under the current \((V, X)\) and applies the resulting pushes or update.
  • Invariant: (I1) \(V \sqsubseteq V^\ast\) and \(X \subseteq X^\ast\) for the SCCP solution \((V^\ast, X^\ast)\); (I2) every edge that rule 2 requires for the current \((V, X)\) but that is not in \(X\) is in FlowWL; (I3) every instruction in an executable block whose equation the current \((V, X)\) violates is in SSAWL.
function SCCP(F):
    V[p] ← ⊤ for every p ∈ Par;  V[v] ← ⊥ otherwise
    X ← ∅;  Exec ← ∅
    FlowWL ← [(∗, r)];  SSAWL ← []                     # FIFO queues
    while FlowWL ≠ [] or SSAWL ≠ []:
        if FlowWL ≠ []:                                  # edges first
            (P, B) ← pop FlowWL
            if (P, B) ∈ X: continue
            X ← X ∪ {(P, B)}                             # mark (P, B) executable
            for each phi φ of B: Visit(φ)                # a new operand may count now
            if B ∉ Exec:
                Exec ← Exec ∪ {B}
                for each non-phi instruction I of B, in order: Visit(I)
        else:
            I ← pop SSAWL
            if block(I) ∈ Exec: Visit(I)                 # otherwise wait for an edge

function Visit(I):
    if I is φ(a_1 : P_1, …, a_m : P_m) in block B:
                              Update(I, ⊔ { V(a_k) : (P_k, B) ∈ X })      # rule 3
    else if I = br S:         PushEdge(block(I), S)                          # rule 2
    else if I = cbr c, B_t, B_f:                                             # rule 2
        if V(c) = ⊤:            PushEdge(block(I), B_t); PushEdge(block(I), B_f)
        else if V(c) ≠ ⊥:       PushEdge(block(I), V(c) ≠ 0 ? B_t : B_f)
    else if I defines a value: Update(I, AbstractEval(I, V))                 # rule 3
function Update(v, new):
    if new ≠ V[v]: V[v] ← new; append each user of v not already in SSAWL
function PushEdge(P, S): if (P, S) ∉ X and (P, S) ∉ FlowWL: append (P, S) to FlowWL

scalaropt.sccp in tools/course/lib/ is this algorithm line by line (FIFO queues, edges drained first), and so is the drill sccp-trace; pebble-sccp implements it on LLVM IR with ConstantInt values (exercise E1). In LLVM's SCCPSolver the operation "mark executable" is markEdgeExecutable, "update" is markConstant/markOverdefined, and the two worklists are BBWorkList and InstWorkList (§7).

3. Worked example

Kildall's dense constant propagation

Kildall's algorithm needs no SSA, so run it on the running example before mem2reg: variables x i j, one assignment per statement. As a Ch 14 program (the if conditions do not matter to an unconditional analysis; t u r s are the temporaries of while.end):

A: x = 1; i = 0; j = 0 -> B          # entry
B: if i < n -> C, F                  # while.cond
C: if x < 1 -> D, E                  # while.body (the test x != 1)
D: x = 2 -> E                        # if.then
E: i = i + 1; j = j + 1 -> B         # if.end
F: t = i * 4; u = j * 4; r = t - u; s = r + x; ret s

Round-robin in RPO (A, B, F, C, D, E), OUT sets of x i j (variables not listed are \(\bot\); bold = changed in that pass). Computed with dataflow.cp_problem + dataflow.solve(..., "round-robin") from tools/course/lib:

block pass 1 pass 2 pass 3
A {i=0, j=0, x=1} {i=0, j=0, x=1} {i=0, j=0, x=1}
B {i=0, j=0, x=1} {i=⊤, j=⊤, x=⊤} {i=⊤, j=⊤, x=⊤}
F {…, r=0, s=1, t=0, u=0} {all ⊤} {all ⊤}
C {i=0, j=0, x=1} {i=⊤, j=⊤, x=⊤} {i=⊤, j=⊤, x=⊤}
D {i=0, j=0, x=2} {i=⊤, j=⊤, x=2} {i=⊤, j=⊤, x=2}
E {i=1, j=1, x=⊤} {i=⊤, j=⊤, x=⊤} {i=⊤, j=⊤, x=⊤}
  • Pass 1: B sees only A (E is still \(\rho_\bot\)), so everything looks constant; E joins C (\(x = 1\)) and D (\(x = 2\)) into \(x = \top\).
  • Pass 2: B joins A (\(i = 0\)) with E (\(i = 1\)): \(\top\), and the same for \(j\) and \(x\); everything downstream becomes \(\top\).
  • Pass 3 changes nothing (3 passes, 18 block evaluations). Result: no constants at all. The value 2 from D reaches E even though D never runs.

Sparse simple constant propagation

SSC (Algorithm 17.1.6) on the SSA form. The worklist starts with the twelve values in program order; bold = changed. Computed by scalaropt.sparse_cp (27 visits):

step visit new value changed? worklist after
1 i.0 0 yes x.0 j.0 cmp cmp1 x.1 add add2 mul mul3 sub add4
2 x.0 1 yes j.0 cmp cmp1 x.1 add add2 mul mul3 sub add4
3 j.0 0 yes cmp cmp1 x.1 add add2 mul mul3 sub add4
4 cmp ⊤ yes cmp1 x.1 add add2 mul mul3 sub add4
5 cmp1 0 yes x.1 add add2 mul mul3 sub add4
6 x.1 ⊤ yes add add2 mul mul3 sub add4 x.0
7 add 1 yes add2 mul mul3 sub add4 x.0 i.0
8 add2 1 yes mul mul3 sub add4 x.0 i.0 j.0
9 mul 0 yes mul3 sub add4 x.0 i.0 j.0
10 mul3 0 yes sub add4 x.0 i.0 j.0
11 sub 0 yes add4 x.0 i.0 j.0
12 add4 1 yes x.0 i.0 j.0
13 x.0 ⊤ yes i.0 j.0 cmp1 x.1 add4
14 i.0 ⊤ yes j.0 cmp1 x.1 add4 cmp add mul
15 j.0 ⊤ yes cmp1 x.1 add4 cmp add mul add2 mul3
16 cmp1 ⊤ yes x.1 add4 cmp add mul add2 mul3
17 x.1 ⊤ no add4 cmp add mul add2 mul3
18 add4 ⊤ yes cmp add mul add2 mul3
19 cmp ⊤ no add mul add2 mul3
20 add ⊤ yes mul add2 mul3 i.0
21 mul ⊤ yes add2 mul3 i.0 sub
22 add2 ⊤ yes mul3 i.0 sub j.0
23 mul3 ⊤ yes i.0 sub j.0
24 i.0 ⊤ no sub j.0
25 sub ⊤ yes j.0 add4
26 j.0 ⊤ no add4
27 add4 ⊤ no —

Step 1 is the optimism of sparse propagation: i.0 = phi [0, entry], [add, if.end] ignores its second operand while add is still \(\bot\). Step 6 is where SSC loses x: the phi x.1 joins the literal 2 from if.then although cmp1 is already known to be 0. The final solution is the same as Kildall's (every value \(\top\)), as Theorem 17.1.11 predicts.

Sparse conditional constant propagation

SCCP (Algorithm 17.1.8) on the same function, computed by scalaropt.sccp and printed by the drill's worked solution. "Visited" lists the instructions evaluated in the step (cbr/br/ret are terminators); B.term in the worklists is block B's terminator:

step pop visited lattice changes flow worklist SSA worklist
1 edge ∗→entry br none entry→while.cond —
2 edge entry→while.cond i.0, x.0, j.0, cmp, cbr i.0 ⊥→0, x.0 ⊥→1, j.0 ⊥→0, cmp ⊥→⊤ while.cond→while.body while.cond→while.end cmp add mul cmp1 x.1 add4 add2 mul3 while.cond.term
3 edge while.cond→while.body cmp1, cbr cmp1 ⊥→0 while.cond→while.end while.body→if.end cmp add mul cmp1 x.1 add4 add2 mul3 while.cond.term while.body.term
4 edge while.cond→while.end mul, mul3, sub, add4, ret mul ⊥→0, mul3 ⊥→0, sub ⊥→0, add4 ⊥→1 while.body→if.end … sub while.end.term
5 edge while.body→if.end x.1, add, add2, br x.1 ⊥→1, add ⊥→1, add2 ⊥→1 if.end→while.cond … x.0 i.0 j.0
6 edge if.end→while.cond i.0, x.0, j.0 i.0 0→⊤, j.0 0→⊤ — cmp add mul cmp1 x.1 add4 add2 mul3 while.cond.term while.body.term sub while.end.term x.0 i.0 j.0
7 use cmp cmp none — add mul cmp1 x.1 add4 add2 mul3 while.cond.term while.body.term sub while.end.term x.0 i.0 j.0
8 use add add add 1→⊤ — mul cmp1 x.1 add4 add2 mul3 while.cond.term while.body.term sub while.end.term x.0 i.0 j.0
9 use mul mul mul 0→⊤ — cmp1 x.1 add4 add2 mul3 while.cond.term while.body.term sub while.end.term x.0 i.0 j.0
10 use cmp1 cmp1 none — x.1 add4 add2 mul3 while.cond.term while.body.term sub while.end.term x.0 i.0 j.0
11 use x.1 x.1 none — add4 add2 mul3 while.cond.term while.body.term sub while.end.term x.0 i.0 j.0
12 use add4 add4 none — add2 mul3 while.cond.term while.body.term sub while.end.term x.0 i.0 j.0
13 use add2 add2 add2 1→⊤ — mul3 while.cond.term while.body.term sub while.end.term x.0 i.0 j.0
14 use mul3 mul3 mul3 0→⊤ — while.cond.term while.body.term sub while.end.term x.0 i.0 j.0
15 use while.cond.term cbr none — while.body.term sub while.end.term x.0 i.0 j.0
16 use while.body.term cbr none — sub while.end.term x.0 i.0 j.0
17 use sub sub sub 0→⊤ — while.end.term x.0 i.0 j.0 add4
18 use while.end.term ret none — x.0 i.0 j.0 add4
19 use x.0 x.0 none — i.0 j.0 add4
20 use i.0 i.0 none — j.0 add4
21 use j.0 j.0 none — add4
22 use add4 add4 add4 1→⊤ — while.end.term
23 use while.end.term ret none — —

(In steps 3–5 the SSA worklist is abbreviated with …; it only grows, and step 6 shows it in full.) Both queues are empty after step 23: 37 instruction visits. The decisive steps:

  • Step 3: cmp1 = ne x.0, 1 evaluates to 0 because x.0 is 1 so far, so the cbr pushes only while.body→if.end. The edge into if.then is never pushed.
  • Step 5: x.1 = phi [2, if.then], [x.0, while.body] joins only the operand on its executable edge: \(V(\text{x.1}) = V(\text{x.0}) = 1\). SSC joined the 2 here.
  • Step 6: the back edge makes i.0 and j.0 overdefined (0 ⊔ 1); x.0 stays \(1 \sqcup 1 = 1\).
  • Step 15–16: re-visiting the branches changes nothing: cmp was already \(\top\), cmp1 is still 0.

Result: \(V(\text{x.0}) = V(\text{x.1}) = 1\), \(V(\text{cmp1}) = 0\); everything else \(\top\); if.then is not executable. pebble-sccp therefore rewrites add4 = add sub, x.0 to add sub, 1 and deletes if.then (the lit test tests/ch17/lit/sccp-running.ll).

CP + DCE iterated does not get there. Run SSC, fold every branch whose condition is a constant, delete unreachable blocks, repeat (scalaropt.cp_dce_fixpoint): round 1 finds no constant condition (SSC says cmp1 is \(\top\)), so nothing is folded and the fixed point is reached after one round with no constants. This is the witness for the strictness part of Theorem 17.1.12.

Try it

./course drill sccp-trace --seed 4 --difficulty medium --solution traces SCCP on a random function in the same format; --difficulty hard also asks which constants simple CP misses.

4. Invariants and correctness

Theorem 17.1.9 (SCCP: termination, bound, least solution)

Let \(F\) be a function in strict SSA form with CFG \(G = (N, E, r)\), values \(\mathit{Val}\), \(I\) instructions, \(e = \lvert E \rvert\) CFG edges and \(U\) SSA edges (Definition 17.1.5), and let \(U_\phi\) be the total number of phi operands in \(F\). Run Algorithm 17.1.8 on \(F\), and let \((V, X)\) be its state at any moment. (a) During the run, \(V[v]\) changes at most twice for every \(v \in \mathit{Val}\), and every edge is added to \(X\) at most once. (b) The run terminates after at most \(2U + I + U_\phi\) instruction visits and at most \(e + 1\) edge pops, and the final \((V, X)\) is the SCCP solution of Definition 17.1.7.

Proof

(a) Update only changes \(V[v]\) to the value of \(v\)'s equation (rule 3 of Definition 17.1.7) under the current \((V, X)\). By induction on the number of steps, the values only rise: a right-hand side of rule 3 is monotone in \(V\) (by monotonicity of \(\widehat{\mathit{op}}\) and of \(\sqcup\), Definition 17.1.2) and in \(X\) (a larger \(X\) joins more operands), and both \(V\) and \(X\) only grow. A strictly rising chain in \(L_c\) has at most two steps (\(\bot \sqsubset c \sqsubset \top\), Definition 17.1.1). An edge enters \(X\) once, because a re-popped edge is skipped by the continue test. (b) Visits. A change of \(V[v]\) appends at most the users of \(v\) to SSAWL, so by (a) at most \(2U\) instructions are ever appended to it, and each pop of SSAWL visits at most one instruction. Each edge \((P, B)\) is popped as new at most once (by (a)) and then visits the phis of \(B\); summed over all edges this is at most \(U_\phi\) visits, because a phi of \(B\) has one operand per predecessor of \(B\). Each block's non-phi instructions are visited once, when the block first enters \(\mathit{Exec}\): at most \(I\) visits. Edges are pushed only when not in \(X\) or in FlowWL, so at most \(e + 1\) edges (the edges of \(E\) and the entry edge) are ever popped. Invariants. Initially (I1) holds (\(V\) is \(\bot\) except on parameters, where every solution has \(\top\); \(X = \emptyset\)), (I2) holds (only rule 1 applies, and \((\ast, r)\) is queued) and (I3) holds (no block is executable). An edge pop marks an edge that rule 2 requires (by (I2) it was pushed only when required, and rule 2's requirements only grow as \((V, X)\) rises), and re-evaluates exactly the equations that mention the new edge: the phis of its target and, on first entry, all instructions of the block. An SSA pop re-evaluates one equation. Each re-evaluation sets \(V[v]\) to a value \(\sqsubseteq\) the SCCP solution's (monotonicity and (I1)), and pushes every edge or instruction whose rule may have become violated, restoring (I2) and (I3). At termination both queues are empty, so by (I2) and (I3) every rule holds: \((V, X)\) satisfies the system, and by (I1) it is \(\sqsubseteq\) the least pair that does, so it is the SCCP solution.

Lemma 17.1.10 (No ⊥ in executable code)

Let \(F\) be a function in strict SSA form with CFG \(G = (N, E, r)\) and values \(\mathit{Val}\), and let \((V, X)\) be its SCCP solution (Definition 17.1.7). Then every value \(v \in \mathit{Val}\) defined in a block that is executable with respect to \(X\) has \(V(v) \neq \bot\).

Proof

By Theorem 17.1.9 (b) the run of Algorithm 17.1.8 on \(F\) ends in \((V, X)\). Order the instructions of the executable blocks by the time their block entered \(\mathit{Exec}\) in that run, then by position in the block, and use induction on this order; since values only rise (Theorem 17.1.9 (a)), it suffices to show that each value is \(\neq \bot\) at some moment of the run. The edges of \(X\) form paths from \(r\) (rule 2 adds an edge only out of an executable block), so every dominator of an executable block \(B\) lies on such a path and entered \(\mathit{Exec}\) no later than \(B\). If \(v\) in \(B\) is an operation, each operand is a literal, a parameter (\(\top\)) or a value defined earlier in \(B\) or in a strict dominator of \(B\) (strict SSA), hence earlier in the order and \(\neq \bot\) by the induction hypothesis; \(\widehat{\mathit{op}}\) of two non-\(\bot\) arguments is \(\neq \bot\) (Definition 17.1.2). If \(v\) is a phi, let \((P, B)\) be the edge whose pop first put \(B\) into \(\mathit{Exec}\). \(P\) was executable before \(B\), and the phi's operand for \(P\) is a literal, a parameter, or a value defined in a block \(D\) that dominates \(P\) (strict SSA: the use sits at the end of \(P\)). \(D \neq B\), since otherwise \(B\) would dominate \(P\) and \(P\) could not have entered \(\mathit{Exec}\) before \(B\); so \(D\) entered \(\mathit{Exec}\) no later than \(P\), before \(B\), and the operand is \(\neq \bot\) by the induction hypothesis. The join over the executable edges includes \((P, B)\), so it is \(\neq \bot\). (This order also covers irreducible CFGs, where reverse postorder would not.)

Theorem 17.1.11 (Dense and sparse simple CP agree on SSA)

Let \(F\) be a function in strict SSA form with CFG \(G = (N, E, r)\) and values \(\mathit{Val}\), let \(V_{\mathrm{SSC}} : \mathit{Val} \to L_c\) be its SSC solution (Definition 17.1.5), and let \(\mathrm{OUT}\) be the dense CP solution of Definition 17.1.3 with \(\mathit{Vars} = \mathit{Val}\) and \(\mathit{Params} = \mathit{Par}\) (every SSA instruction read as a statement). Then for every value \(v \in \mathit{Val}\) defined in a block \(D \in N\) and every block \(B \in N\) with \(D \mathrel{\mathrm{dom}} B\), \(\mathrm{OUT}[B](v) = V_{\mathrm{SSC}}(v)\). In particular the two solutions report the same constants.

Proof sketch (full proof: [WZ91, §3])

Wegman and Zadeck prove that SC and SSC find the same constants. On SSA the argument is short. A value \(v\) is assigned only in its defining block \(D\), so on every path from \(D\) to a block \(B\) that \(D\) dominates, the environment's entry for \(v\) is carried unchanged by the transfer functions of Definition 17.1.3; at a join inside \(D\)'s dominance region every predecessor is also dominated by \(D\) (or contributes \(\bot\) for \(v\), which is the unit of \(\sqcup\)), so the dense join never mixes two different facts for the same SSA value. Hence the dense system restricted to the entries \((B, v)\) with \(D \mathrel{\mathrm{dom}} B\) is equivalent to the sparse system with one unknown per value, and a phi's dense evaluation reads exactly the operands' values at the ends of the predecessors. Both least solutions are then equal by induction on the Kleene iterates of the equivalent systems (Ch 14, Theorem 14.1.12). The test ConstantPropagation.test_dense_equals_sparse checks the statement on 250 random functions.

Theorem 17.1.12 (SCCP ⊇ constant propagation and dead-code elimination, iterated)

Let \(F\) be a function in strict SSA form with CFG \(G = (N, E, r)\) and values \(\mathit{Val}\), in which every conditional branch tests a value of \(\mathit{Val}\). Define the CP+DCE iteration as the sequence of edge sets \(E_0 \supseteq E_1 \supseteq \dots\) with \(E_0 = E\), where \(E_{k+1}\) is obtained from \(E_k\) as follows: compute the SSC solution \(V_k\) of \(F\) restricted to the edges \(E_k\) (a phi joins only the operands whose edge is in \(E_k\), and a value in a block unreachable from \(r\) through \(E_k\) has \(V_k = \bot\)); for every conditional branch cbr \(c, B_{\mathrm{t}}, B_{\mathrm{f}}\) in a reachable block whose condition has \(V_k(c) \in \mathbb{Z}_{32}\), remove the edge to the successor not selected by \(V_k(c)\); then remove every edge out of a block that became unreachable. The iteration stops at the first \(k\) with \(E_{k+1} = E_k\); write \(E^\ast = E_k\) and \(V^\ast = V_k\) for that \(k\). Let \((V_S, X)\) be the SCCP solution of \(F\) (Definition 17.1.7). Then: (a) every block executable with respect to \(X\) is reachable from \(r\) through \(E^\ast\); (b) for every value \(v \in \mathit{Val}\) defined in a block executable with respect to \(X\) and every \(c \in \mathbb{Z}_{32}\), \(V^\ast(v) = c\) implies \(V_S(v) = c\); (c) the inclusion is strict: on the running example SCCP finds a constant that the CP+DCE iteration does not.

Proof

Let \(\Phi\) be the monotone operator on pairs \((V, Y)\) (a value state and an edge set) that applies rules 1–3 of Definition 17.1.7 once; its least fixed point is the SCCP solution \((V_S, X)\). For every round \(k\) let \(Y_k\) be the set of edges of \(E_k\) whose source is reachable from \(r\) through \(E_k\), together with \((\ast, r)\); for a block \(B\) ending in a conditional branch write \(c_B\) for its condition. Step 1: \(V_{k} \sqsubseteq V_{k-1}\). The SSC operator on \(E_k\) joins each phi over a subset of the operands that the operator on \(E_{k-1}\) joins, and gives \(\bot\) to values in blocks that became unreachable, so it is pointwise below the operator on \(E_{k-1}\). If \(H \sqsubseteq H'\) pointwise and both are monotone, then \(H(\mathrm{lfp}\,H') \sqsubseteq H'(\mathrm{lfp}\,H') = \mathrm{lfp}\,H'\), so \(\mathrm{lfp}\,H'\) is a pre-fixed point of \(H\) and \(\mathrm{lfp}\,H \sqsubseteq \mathrm{lfp}\,H'\), because the least fixed point lies below every pre-fixed point (Knaster–Tarski, Ch 14, Theorem 14.1.12). Step 2: \((V_k, Y_k)\) is a pre-fixed point of \(\Phi\). Edges: take an edge \((B, S)\) that rule 2 produces from \((V_k, Y_k)\). \(B\) is entered by an edge of \(Y_k\), so it is reachable in \(E_k\). If \((B, S) \notin E_k\), it was removed in some round \(j < k\) because \(V_j(c_B)\) was a constant selecting the other successor; by Step 1 \(V_k(c_B) \sqsubseteq V_j(c_B)\), so \(V_k(c_B)\) is \(\bot\) (then rule 2 produces nothing) or that same constant (then rule 2 produces only the other successor): a contradiction. So \((B, S) \in E_k\) with \(B\) reachable, i.e. \((B, S) \in Y_k\). Values: for \(v\) in a block entered by \(Y_k\), rule 3 is SSC's equation on \(E_k\), whose solution is \(V_k\), so it returns \(V_k(v)\); for every other \(v\) it returns \(\bot \sqsubseteq V_k(v)\). Hence \(\Phi(V_k, Y_k) \sqsubseteq (V_k, Y_k)\). Step 3. By Knaster–Tarski again the least fixed point of \(\Phi\) is below every pre-fixed point, so for the final round \(X \subseteq Y^\ast\) and \(V_S \sqsubseteq V^\ast\), where \(Y^\ast\) is the \(Y_k\) of the final round. (a) follows from \(X \subseteq Y^\ast\). For (b), if \(V^\ast(v) = c\) and \(v\)'s block is executable, then \(V_S(v) \sqsubseteq c\), so \(V_S(v) \in \{\bot, c\}\), and by Lemma 17.1.10 \(V_S(v) \neq \bot\). (c) On the running example the CP+DCE iteration stops after one round with \(V^\ast(\text{x.0}) = \top\) (§3), while \(V_S(\text{x.0}) = 1\). The unit test ConstantPropagation.test_sccp_contains_cp_dce_fixpoint checks (a) and (b) on 250 random functions and finds strict cases.

Theorem 17.1.13 (SCCP is sound)

Let \(F\) be a function in strict SSA form with CFG \(G = (N, E, r)\) and values \(\mathit{Val}\), whose operators are total functions on \(\mathbb{Z}_{32}\) (no operand is undef or poison), and let \((V_S, X)\) be its SCCP solution (Definition 17.1.7), with concretization \(\gamma\) as in Definition 17.1.2. Then in every execution of \(F\) on any input, every traversed CFG edge is in \(X\), and every value \(v \in \mathit{Val}\) that the execution computes has its concrete value in \(\gamma(V_S(v))\). In particular, for every \(c \in \mathbb{Z}_{32}\), a value with \(V_S(v) = c\) is the constant \(c\) in the sense of Definition 17.1.2, and a block that is not executable with respect to \(X\) never runs.

Proof

By induction on the number of executed instructions, with the induction hypothesis that every edge traversed so far is in \(X\) and every value computed so far lies in \(\gamma\) of its lattice element. Initially only the entry edge \((\ast, r) \in X\) (rule 1) has been traversed, and a parameter's value lies in \(\gamma(\top) = \mathbb{Z}_{32}\). Executing an operation \(v = \mathit{op}(a_1, a_2)\) in a block reached over edges of \(X\) (so executable) gives, by soundness of \(\widehat{\mathit{op}}\) (the remark after Definition 17.1.2) and the induction hypothesis on \(a_1, a_2\), a value in \(\gamma\) of the right-hand side of rule 3, which equals \(V_S(v)\) because \((V_S, X)\) satisfies the system. Arriving at a phi \(v = \phi(a_1 : P_1, \dots, a_m : P_m)\) in block \(B\) over an edge \((P_k, B)\): that edge was traversed, so it is in \(X\) by the induction hypothesis; the phi takes the value of \(a_k\), which lies in \(\gamma(V_S(a_k)) \subseteq \gamma(\bigsqcup_{j : (P_j, B) \in X} V_S(a_j)) = \gamma(V_S(v))\), since \(\gamma\) is monotone. A cbr \(c, B_{\mathrm{t}}, B_{\mathrm{f}}\) whose condition has concrete value \(z \in \gamma(V_S(c))\): \(V_S(c) \neq \bot\) because \(\gamma(\bot)\) is empty; if \(V_S(c) = \top\) both successor edges are in \(X\) (rule 2), and if \(V_S(c)\) is a constant it equals \(z\), and rule 2 put exactly the taken edge in \(X\). For br \(S\), rule 2 puts \((B, S)\) in \(X\) directly.

Theorem 17.1.14 (Kildall's CP terminates and computes the least solution)

Let \(F\) be a function with CFG \(G = (N, E, r)\), variables \(\mathit{Vars}\) and parameters \(\mathit{Params}\) as in Definition 17.1.3, satisfying the precondition of Algorithm 17.1.4, with \(n = \lvert N \rvert\), and with every operator a total function on \(\mathbb{Z}_{32}\). Then Algorithm 17.1.4 terminates after at most \(2 \lvert \mathit{Vars} \rvert \cdot n + 1\) passes and returns the \(\mathrm{OUT}\) component of the dense CP solution of Definition 17.1.3. Moreover, for every block \(B \in N\), variable \(x \in \mathit{Vars}\) and \(c \in \mathbb{Z}_{32}\), \(\mathrm{OUT}[B](x) = c\) implies that every execution of \(F\) reaching the end of \(B\) has \(x = c\) there.

Proof

Monotonicity. Each statement's transfer function is monotone because \(\widehat{\mathit{op}}\) is monotone (the remark after Definition 17.1.2) and environment update is monotone in both arguments; joins are monotone. The equation system of Definition 17.1.3 is therefore a monotone function on the finite-height lattice \((\mathit{Vars} \to L_c)^N\). Least fixed point. The algorithm is round-robin iteration from \(\bot\), so by Ch 14, Theorem 14.4.4 it computes the least fixed point, the least solution. Bound. Each pass that is not the last changes at least one entry \(\mathrm{OUT}[B](x)\), which only rises (by induction on passes, each new value is computed from larger inputs by monotone functions), and each such entry can rise at most \(h(L_c) = 2\) times; there are \(n \lvert \mathit{Vars} \rvert\) entries, so at most \(2 n \lvert \mathit{Vars} \rvert\) changing passes plus one confirming pass. Soundness. For an execution of \(F\) and a block \(B\), let \(\sigma_B : \mathit{Vars} \rightharpoonup \mathbb{Z}_{32}\) be the concrete values of the variables assigned so far at the end of \(B\). By induction on the execution length, \(\sigma_B(x) \in \gamma(\mathrm{OUT}[B](x))\) for every variable \(x\) the execution has assigned: the entry satisfies it by \(\iota\) (parameters are \(\top\)), a statement preserves it by soundness of \(\widehat{\mathit{op}}\), and control arriving at \(B\) from a predecessor \(P\) satisfies it because \(\mathrm{OUT}[P] \sqsubseteq \mathrm{IN}[B]\) and \(\gamma\) is monotone. Hence if \(\mathrm{OUT}[B](x) = c\) then every execution reaching the end of \(B\) has \(x = c\).

Which precondition breaks it. Without strict SSA the sparse equations are not the program's semantics (a use could see a definition from another path). With undefined behavior in the IR the soundness argument changes: LLVM's SCCP may exploit undef/poison (it may treat an undef operand as any constant it likes, a refinement), whereas this lesson's \(\widehat{\mathit{op}}\) treats them as \(\top\); pebble-sccp follows the lesson and never folds to poison (tests/ch17/lit/sccp-no-fold.ll).

5. Complexity

Variables: \(n\) blocks, \(e\) CFG edges, \(\lvert \mathit{Vars} \rvert\) variables, \(I\) instructions, \(U\) SSA edges (operand uses).

Algorithm Time (worst) Time (typical) Space Justification
Kildall dense CP (17.1.4) \(O(\lvert\mathit{Vars}\rvert^2 \cdot n \cdot e)\) map operations a few passes (\(d(G) + 3\) on reducible CFGs) \(O(n \lvert \mathit{Vars} \rvert)\) \(2 n \lvert\mathit{Vars}\rvert\) changing passes (Theorem 17.1.14), each pass joins \(e\) maps of size \(\lvert\mathit{Vars}\rvert\)
Sparse simple CP (17.1.6) \(O(U + I)\) evaluations linear \(O(I)\) each value rises twice and pushes its users (as Theorem 17.1.9 (a))
Dense conditional CP [Weg75] as Kildall, plus \(e\) edge flags a few passes \(O(n \lvert\mathit{Vars}\rvert + e)\) same argument, with one more lattice per edge
SCCP (17.1.8) \(O(U + e + I)\) visits linear, ~2–3 visits per instruction \(O(I + e)\) Theorem 17.1.9 (b)

Pathological family for dense CP. Let \(P_k\) be the loop A: x1 = 0; …; xk = 0 → B, B: if c < n → C, E, C: xk = x(k−1); …; x2 = x1; x1 = xk + 1 → B, E: ret x1 (the copies in \(C\) are listed against the dependence order). On the first pass \(x_1\) becomes 1 in \(C\); each later pass pushes the \(\top\) created at \(B\)'s join one copy further through \(C\), because within \(C\) the copy that would carry it runs before its source is updated. Round-robin therefore needs exactly \(k + 2\) passes over 4 blocks, each joining maps of \(k\) variables: \(\Theta(k^2)\) map-entry operations for a loop of \(k + 1\) statements (5, 7 and 10 passes for \(k = 3, 5, 8\), measured with dataflow.solve(cp_problem(P_k), "round-robin")). On the SSA form of the same loop, each SSA value rises at most twice and each rise re-queues only its one or two users, so SSC needs \(\Theta(k)\) visits. On real code the difference is small: the lab's ch17-compare --table --time analyzes the whole 12-function corpus with each of its four algorithms in about 0.03–0.12 ms in total on this container.

Real-world scale. SCCP is one of the cheapest scalar passes: LLVM runs it once in the function simplification pipeline and IPSCCP once per module at -O2 [LLVM-Pipelines], and GCC 14 runs CCP five times at -O2 (ccp1–ccp5 in gcc-14 -O2 -fdump-passes, [GCC-TreeSSAPasses]).

6. Variants and refinements

  • Constant folding through \(\top\) — \(\widehat{\mathrm{mul}}(\top, 0) = 0\), \(\widehat{\mathrm{and}}(\top, 0) = 0\): LLVM's SCCPInstVisitor folds these [LLVM-SCCP]. Trade-off: still monotone, but every operator needs its own rules; this chapter's oracles and pebble-sccp leave them out so that the lab's Python and C++ agree exactly.
  • Richer lattices — LLVM's ValueLatticeElement is not flat: it also holds constantrange (with a widening after MaxNumRangeExtensions steps, Ch 14 Lesson 14.7) and "not a constant" [LLVM-SCCP]; this is SCCP with the interval domain of Lesson 17.2. Trade-off: more constants (the range(...) attributes in the §7 box) for a taller lattice.
  • Undef as "any constant" — LLVM's SCCP lets undef merge with any constant (refinement: undef may be chosen equal to it) and resolves branches on undef at the end. Trade-off: more folding, subtle correctness rules (Ch 13's poison/undef lesson).
  • Predicate-aware SCCP — x == 3 on the true edge of if (x == 3) is a constant. LLVM's PredicateInfo inserts ssa.copy per edge so SCCP can use it (e-SSA, Ch 14 Lesson 14.6 §6); GVN does the same (Lesson 17.5 §6). Trade-off: more SSA values.
  • Combining with value numbering — Click and Cooper solve constants, reachability and congruences together and show it beats any order of separate passes [CC95, Cli95t]; Lesson 17.8.
  • Interprocedural SCCP (IPSCCP, preview of Ch 20) — run the same solver over all internal functions of a module at once: a call's arguments join into the callee's parameters, the callee's returned values join into the call's result, and a function's blocks become executable when a call to it does [LLVM-IPSCCP]. Trade-off: needs all callers visible (internal linkage), and the lattice is only as good as the call graph.
  • Dense conditional CP (Wegbreit) — the dense twin of SCCP, same result as SCCP (checked by test_conditional_dense_equals_sccp) and \(\Theta(n \lvert\mathit{Vars}\rvert)\) space; historically first [Weg75].

7. In real compilers

Kildall's dense constant propagation

Dense constant propagation survives where values are not in SSA: GCC's RTL pass cprop propagates constants and copies over available assignments, a dense bit-vector problem solved with the same compute_available routine as RTL CSE [GCC-CPROP]. On GIMPLE (SSA) GCC uses sparse CCP instead, so by the time RTL cprop runs there is usually nothing left.

GCC: dense global constant propagation on RTL, after sparse CCP

Reproduce (gcc 14.2.0 for the dump; the source is gcc 15.1.0's cprop.cc, fetched from the gcc-mirror repository on GitHub):

cat > kd.c <<'EOF'
int kildall(int p, int q) {
  int k = 3;
  int r;
  if (p)
    r = k + q;
  else
    r = k * q;
  return r + k;
}
EOF
gcc-14 -O2 -fdump-rtl-cprop1-details -S kd.c -o /dev/null
grep -h '^CPROP of' kd.c.*r.cprop1
curl -sSfL https://raw.githubusercontent.com/gcc-mirror/gcc/releases/gcc-15.1.0/gcc/cprop.cc |
  sed -n '/^compute_cprop_data/,/^}/p' | head -6

Output:

CPROP of kildall, 6 basic blocks, 224 bytes needed, 0 local const props, 0 local copy props, 0 global const props, 0 global copy props
compute_cprop_data (void)
{
  basic_block bb;

  compute_local_properties (cprop_kill, cprop_avloc, &set_hash_table);
  compute_available (cprop_avloc, cprop_kill, cprop_avout, cprop_avin);

What to notice: RTL cprop computes per-block available assignments (cprop_avin, cprop_avout) exactly as Kildall does per program point, and found 0 global const props: GIMPLE's sparse CCP already replaced k by 3 everywhere. Dense and sparse CP find the same constants (Theorem 17.1.11), so the second, dense run is only useful for constants that appear later, during RTL expansion.

Sparse simple constant propagation

No production compiler ships the unconditional sparse algorithm on its own; its role today is played by pessimistic sparse folding along def-use chains, which gives up SSC's optimism at loop phis. LLVM's instsimplify folds constants operand by operand, SCCP's solver is the optimistic version.

Pessimistic vs optimistic sparse folding: instsimplify vs sccp

Reproduce (opt 23.1.2):

cat > opt.ll <<'EOF'
define i32 @chain(i32 %n) {
entry:
  %a = add i32 20, 1
  %b = mul i32 %a, 2
  %c = sub i32 %b, %a
  br label %loop
loop:
  %k = phi i32 [ 3, %entry ], [ %k2, %loop ]
  %i = phi i32 [ 0, %entry ], [ %i2, %loop ]
  %odd = trunc i32 %i to i1
  %k2 = select i1 %odd, i32 %k, i32 3   ; 3 or k: optimistically 3
  %i2 = add i32 %i, 1
  %more = icmp slt i32 %i2, %n
  br i1 %more, label %loop, label %exit
exit:
  %r = add i32 %k2, %c
  ret i32 %r
}
EOF
opt -passes=instsimplify -S opt.ll | sed -n '/^exit:/,/^}/p'
opt -passes=sccp -S opt.ll | sed -n '/^exit:/,/^}/p'

Output:

exit:                                             ; preds = %loop
  %r = add i32 %k2, 21
  ret i32 %r
}
exit:                                             ; preds = %loop
  ret i32 24
}

What to notice: both fold the straight-line chain (%c = 21) by following def-use edges. Only the optimistic solver proves %k = 3: it assumes the back-edge operand %k2 is \(\bot\) on the first visit (step 1 of the SSC trace in §3 does the same), gets %k2 = select(⊤, 3, 3) = 3, and the loop phi stays 3. A pessimistic folder would need %k2 folded before it can fold %k, and vice versa.

Sparse conditional constant propagation

LLVM: llvm/lib/Transforms/Utils/SCCPSolver.cpp — SCCPInstVisitor::solve drains BBWorkList (the flow worklist) and the instruction worklists, markEdgeExecutable adds an edge and re-visits the target's phis, visitPHINode joins only feasible incoming values [LLVM-SCCP]; llvm/lib/Transforms/Scalar/SCCP.cpp — runSCCP rewrites the function [LLVM-SCCPPass]; llvm/lib/Transforms/IPO/SCCP.cpp — runIPSCCP [LLVM-IPSCCP]. GCC: gcc/tree-ssa-ccp.cc, a four-level lattice (ccp_lattice_t: UNINITIALIZED, UNDEFINED, CONSTANT, VARYING) over GCC's generic SSA propagation engine; likely_value is the operand rule of Definition 17.1.2 [GCC-CCP] (Lesson 14.6 prints its trace). The production versions differ from Algorithm 17.1.8 in three ways: ranges in the lattice, special folding with \(\top\) operands, and undef handling (§6).

LLVM SCCP on the running example

Reproduce (clang 23.1.2, opt 23.1.2):

cat > run.c <<'EOF'
int run(int n) {
  int x = 1, i = 0, j = 0;
  while (i < n) {
    if (x != 1)
      x = 2;
    i = i + 1;
    j = j + 1;
  }
  int t = i * 4, u = j * 4;
  return t - u + x;
}
EOF
clang-23 -O0 -Xclang -disable-O0-optnone -fno-discard-value-names -S -emit-llvm run.c -o run.O0.ll
opt -passes=mem2reg -S run.O0.ll -o run.ll
opt -passes=sccp -S run.ll | sed -n '/^define/,/^}/p'

Output:

define dso_local range(i32 -2147483647, -2147483648) i32 @run(i32 noundef %n) #0 {
entry:
  br label %while.cond

while.cond:                                       ; preds = %if.end, %entry
  %i.0 = phi i32 [ 0, %entry ], [ %add, %if.end ]
  %j.0 = phi i32 [ 0, %entry ], [ %add2, %if.end ]
  %cmp = icmp slt i32 %i.0, %n
  br i1 %cmp, label %while.body, label %while.end

while.body:                                       ; preds = %while.cond
  br label %if.end

if.end:                                           ; preds = %while.body
  %add = add nsw i32 %i.0, 1
  %add2 = add nsw i32 %j.0, 1
  br label %while.cond, !llvm.loop !5

while.end:                                        ; preds = %while.cond
  %mul = mul nsw i32 %i.0, 4
  %mul3 = mul nsw i32 %j.0, 4
  %sub = sub nsw i32 %mul, %mul3
  %add4 = add nsw i32 %sub, 1
  ret i32 %add4
}

What to notice: exactly the result of the §3 trace: %x.0 and %x.1 are gone (constant 1), %cmp1 folded, the branch in while.body became br label %if.end and if.then was deleted. %i.0 and %j.0 are overdefined, so %sub survives. The range(...) return attribute comes from the range part of LLVM's lattice (Lesson 17.2): %sub + 1 with nsw can be anything but INT_MIN.

IPSCCP (preview of Ch 20): constants across calls

Reproduce (clang 23.1.2, opt 23.1.2):

cat > ip.c <<'EOF'
static int scale(int x, int factor) {
  if (factor == 0)
    return -1;
  return x * factor;
}

int api(int a) { return scale(a, 4) + scale(a + 1, 4); }
EOF
clang-23 -O0 -Xclang -disable-O0-optnone -fno-discard-value-names -S -emit-llvm ip.c -o ip.O0.ll
opt -passes='function(mem2reg),ipsccp' -S ip.O0.ll -o - | sed -n '/^define internal/,/^}/p'

Output:

define internal i32 @scale(i32 noundef %x, i32 noundef %factor) #0 {
entry:
  br label %if.end

if.end:                                           ; preds = %entry
  %mul = mul nsw i32 %x, 4
  br label %return

return:                                           ; preds = %if.end
  ret i32 %mul
}

What to notice: scale is internal and every call passes 4, so the parameter %factor joins to the constant 4, the test factor == 0 is false, and the return -1 block is never executable — SCCP's executable-edge reasoning, applied across the call graph.

Find where LLVM does it. Open llvm/lib/Transforms/Utils/SCCPSolver.cpp and find the class that holds one lattice value per SSA value (the argument type of markOverdefined). What is it called, and which states can it hold besides "constant" and "overdefined"? (Quiz llvm-where-sccp-lattice.)

8. Comparison

Technique Power / precision Speed (asymptotic · practical) Output / error quality Implementation effort Typical use
Kildall dense CP Constants valid on all paths, every edge assumed executable; equals SSC on SSA \(O(\lvert\mathit{Vars}\rvert^2 n e)\) · slow on large functions A map per program point; works without SSA Low (one generic solver) Non-SSA IRs (GCC RTL cprop), teaching
Sparse simple CP (SSC) Same constants as Kildall (Theorem 17.1.11); optimistic at phis \(O(U + I)\) · linear One lattice value per SSA value Low Folding along def-use chains; the base of SCCP
SCCP Strictly more than CP + DCE iterated (Theorem 17.1.12); proves blocks unreachable \(O(U + e + I)\) · linear, ~2–3 visits per instruction Constants plus executable edges; drives branch folding Medium (two worklists, edge bookkeeping) LLVM sccp/ipsccp, GCC CCP, MLIR -sccp

Choose Kildall's dense CP when the IR is not in SSA and variables are few, or when constant propagation is one instance of an existing generic dense solver. Choose sparse simple CP when you only need folding along def-use chains and branches are handled elsewhere. Choose SCCP when you optimize SSA code at all: it is as cheap as SSC and strictly more precise; add ranges (Lesson 17.2) and predicates when you can afford the taller lattice.

The comparison lab measures the difference on the C corpus (ch17-compare --table labs/ch17-scalar/corpus/*.ll): simple CP finds 7 constants and SCCP 12, and SCCP proves 5 of the 48 blocks unreachable; on the running example alone, 0 vs 3 (x.0, cmp1, x.1).

9. Assessment

  • Quiz (./course quiz 17): kildall-passes, kildall-lattice-height (tag kildall-cp); sparse-cp-values, sparse-vs-dense (tag sparse-cp); sccp-values, sccp-vs-cpdce, llvm-where-sccp-lattice (tag sccp).
  • Drill: ./course drill sccp-trace (all difficulties; hard also asks for the constants simple CP misses). Kildall's tables: ./course drill dataflow-table from Ch 14 exercises the same round-robin mechanics.
  • Flashcards: tags kildall-cp, sparse-cp, sccp in flashcards.tsv.
  • Exercises: E1 pebble-sccp; lab Part A (labs/ch17-scalar/SPEC.md).

References

See the chapter references.