SSA FORM
Every variable assigned exactly once
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.
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// phi for v goes on the ITERATED dominance frontier of v's def blocks
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: x4 = φ(B1: x2, B2: x3)
r1 = x4 + t1; ret r1
// after constant propagation + DCE:
B0: c1 = param; br c1 ? B1 : B2
B3: x4 = φ(B1: 2, B2: 3); r1 = x4 + 8; 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.
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
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.
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.
“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.