SSA FORM

Every variable assigned exactly once

L318 MINCOMPILERS
PREDICT

SSA form gives every assignment its own name (x1, x2, ...). x = 1 in B0. Then if c = 1, B1 sets x = 2, else B2 sets x = x + 2. Both jump to B3, which returns x + 8. Renaming runs with no merge node (phi) at B3, so B3 reads the only version on every path into it. What does the SSA version return for c = 1 and c = 0?

In SSA form every variable is assigned exactly once. Each new assignment gets a new name, such as x1, x2 and x3. That makes every use point at one definition, which is what constant propagation and dead-code elimination need.

Branches break this. After an if/else, x could be x2 or x3. SSA adds a phi, x4 = φ(B1: x2, B2: x3), which picks a value by the edge control came in on. Phis go on the dominance frontier of each definition: the first blocks where that definition no longer dominates.

▶ SIMULATION
CFG
B0 → B1, B2 · B1 → B3 · B2 → B3
idom: —
DF: —
IR · 7 instrs
B0: c = param; x = 1; k = 4; t = k * 2; br c ? B1 : B2
B1: x = 2; jmp B3
B2: x = x + 2; jmp B3
B3: r = x + t; ret r
RUNS
not run
phis placed: 0
last—
uses with the wrong definition—
✓ every use has exactly one reaching definition✓ SSA returns what the original returns on both paths
SCHEDULE
01[ANALYSIS]compute dominators and dominance frontiers from the CFG
02[SSA]rename along the dominator tree (one stack per variable)
03[CHECK]run original and SSA for c = 1 and c = 0
04[OPT]constant propagation + dead-code elimination
05[CHECK]run original and SSA for c = 1 and c = 0
Start of rename, no phi scenario. 5 steps.
B0: c1 = param; x1 = 1; k1 = 4; t1 = k1 * 2; br c1 ? B1 : B2
B1: x2 = 2;                        jmp B3
B2: x3 = x1 + 2;                   jmp B3
B3: r1 = x1 + t1;   // which x? x1 only reaches B3 if neither arm ran
    ret r1

Watch the IR box and the RUNS box. RENAME, NO PHI turns the reaching-definition check red at B3 and returns 9 on both paths. The optimiser then folds that wrong value into ret 9, and the reaching-definition check turns green again because the bad use is gone. The strip shows ✓ on a miscompiled program; only the RUNS check still catches it. FIX: PHI AT THE FRONTIER places x4 at B3, and constant propagation shrinks the IR from 8 to 3 instructions while both paths still return 10 and 11.

BREAK IT NOT YET

This construction is correct: both paths return what the original returns. Inject one fault so that the final run of the SSA program returns a wrong answer.

GOAL: a final run returns a different value from the original

FIX IT NOT YET

Write placePhis(cfg, defs, domFrontier). defs[v] lists the blocks that assign v; domFrontier[b] lists block b's dominance frontier. Return { v: [blocks that need a phi for v] }, minimal SSA: the iterated dominance frontier, no more, no less. The grader tries a diamond, a loop, a nested diamond and a triply nested one.

Mental model:

SSA turns “which assignment reaches here?” into a name. That only works if every merge where two versions can meet has a phi. You find those merges on the dominance frontier, iterated because each phi is a new definition. Get placement right and every later pass can trust a name; get it wrong and every later pass amplifies the mistake.

IN PRODUCTION

LLVM IR (mem2reg places phis on the iterated dominance frontier, pruned to blocks where the variable is live), GCC GIMPLE SSA, V8 TurboFan, HotSpot C2, Go's SSA backend, Cranelift. You read it in opt -S output and compiler-explorer dumps as phi instructions at block starts.

IN INTERVIEWS

“What is SSA and why do compilers use it?” and “Where do you put phi nodes?” Define single assignment, explain phis as edge-selected values, describe dominance frontiers and why the placement iterates, and name an optimisation SSA makes simple.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.
NEXT: JIT COMPILATION →