Definition
Static single assignment (SSA) form is a way of writing a program, used in compiler intermediate representations §3.37, in which every variable is assigned exactly once in the program text. Each assignment to an original variable creates a new numbered version, such as or . Where control-flow paths that assigned different versions meet, a phi function, written , selects the version from the path along which execution arrived. Static means once in the text: a loop may execute that one assignment many times.
The idea
Analyses and optimizations keep asking one question: which assignment produced the value used here? In the code if (c) x = 3; else x = 7; y = x + 1; the use of x can receive either assignment, depending on c. Answering the question for every variable at every point is the job of reaching definitions §3.16 analysis, and it is costly to repeat. SSA form answers the question once, by construction: every variable has exactly one assignment, so every use names its own definition.
Renaming alone fails where paths meet. The left path assigns , the right path assigns , and the statement after the if needs a single name. SSA form adds a new definition there, , which means: is if execution came from the left path and if it came from the right path. The phi function is bookkeeping for the question “which way did execution come?”
A railway junction where two lines join is a fair picture: the cargo after the junction is the cargo of whichever train arrived, never a mixture or an average of the two. The picture stops at timing. All the phi functions at the start of a block select at the same instant, each from the value its operand had when execution left the previous block.
With one definition per name, many optimizations become simple. If , every use of is 3. A version that no instruction uses is dead, and dead-code elimination §12.11 deletes its definition.
How it works
Building SSA form starts from the control-flow graph §3.13 (CFG) of the program. Its nodes are basic blocks §3.14, straight-line runs of instructions entered only at the top, and its edges are the possible jumps between blocks. A block’s predecessors are the blocks with an edge into it. Construction has two steps.
Place phi functions. A block needs a phi function for variable when different definitions of can reach it along different edges. The standard placement uses dominance. Block dominates block when every path from the program’s entry to passes through ; strictly dominates when, in addition, . The dominance frontier of is the set of blocks such that dominates a predecessor of but does not strictly dominate . A phi function for goes into every block in the dominance frontier of every block that assigns . Each new phi function is itself an assignment of , so the rule is applied again to the new phi functions until nothing changes; the set of blocks reached this way is called the iterated dominance frontier. Cytron, Ferrante, Rosen, Wegman and Zadeck published this method in 1991 [269].
Rename. Walk the blocks in the order of the dominator tree, in which the parent of each block is the closest block that strictly dominates it, starting at the entry. Give each assignment of , including each phi function, a fresh version number, and rewrite each use of to the version current at that point. Each phi function receives one operand for each predecessor: the version current at the end of that predecessor.
The LLVM §12.18 compiler infrastructure (LLVM began as Low Level Virtual Machine; the project now treats the letters as a plain name) shows how this looks in practice. In its intermediate representation (IR), a phi function is an instruction named phi that lists pairs of a value and a predecessor block, for example %x3 = phi i32 [ %x1, %left ], [ %x2, %right ]. Its C front end, Clang §12.17, does not build SSA form directly: it places each local variable in a stack slot accessed by explicit loads and stores, and a later pass, mem2reg, promotes those slots to SSA values and inserts the phi functions.
No processor has a phi instruction, so the back end translates SSA form back into ordinary code before or during register allocation. Each phi function becomes a set of copies, one at the end of each predecessor: for , the left block ends with the copy and the right block with . Register allocation then often gives , and the same register, which removes the copies. Two complications need care.
Critical edges. An edge is critical when it leaves a block with several successors and enters a block with several predecessors. A copy placed at the end of the source block would also run when execution takes the source block’s other edges. The compiler therefore splits such an edge: it inserts a new, empty block on the edge to hold the copy.
Parallel copies. The phi functions of one block select simultaneously, so the copies for one edge must behave as if done all at once. Done one after another, a copy can overwrite a value that a later copy still needs; the worked example shows a case.
Worked example
A merge. Block tests a condition and jumps to block or block . Block sets , block sets , and both jump to block , which computes and then . Block dominates a predecessor of , namely itself, but does not dominate , so is in the dominance frontier of , and likewise of ; that is why the phi function stands in . If is true, execution runs : and . If is false, execution runs : and . The average would give , a value that no execution produces.
An analysis that forgets which path was taken summarizes either as the set or, in interval analysis §3.9, as the range . The range also contains 4, 5 and 6, which no execution produces: a sound overapproximation §19.2.5. Both are summaries made by the analysis; the phi function itself never holds two values at once.
A loop. The C code i = 0; while (i < 3) i = i + 1; return i; becomes four blocks.
: ; jump to .
: ; if , jump to , otherwise jump to .
: ; jump to .
: return .
Execution enters from , so ; since , computes . Entering from gives , then ; then and ; then , and since is false, returns 3. The phi function ran 4 times: once from , and 3 times along the back edge from , the jump that returns to the start of the loop. The version took the values 0, 1, 2 and 3, yet it has one definition in the text.
Parallel copies. A loop that exchanges two variables on every pass can, after SSA construction and simplification, have a header block that begins with and . Suppose and when execution leaves . Selecting simultaneously gives the new and the new , the intended exchange. Copies done one after another at the end of , first and then , give and then : the value 1 is lost. A correct translation saves one value first: , , , which gives and .
Stated precisely
Let the program be a CFG whose blocks each begin with zero or more phi functions. The program is in SSA form when the following hold.
Single definition: each variable is defined by exactly one instruction or phi function in the program text.
Phi selection: let block have predecessors and begin with the phi functions for . When execution enters along the edge from , every receives the value that held when execution left . All selections happen at once, so no phi function sees the new value of another.
Dominance, required by the usual strict form: the definition of each variable dominates each of its uses. A phi operand counts as used at the end of , so the definition of must dominate .
Under these conditions exactly one definition reaches each use, so the def-use chain §3.17 of a variable can be read directly from its name. Placement on the iterated dominance frontier gives what is called minimal SSA; the variant called pruned SSA also leaves out phi functions whose results are never used.
Limits and common mistakes
SSA form covers variables, not memory. In *p = 1; *q = 2; y = *p; the value of y is 1 or 2 depending on whether p and q point to the same location. SSA names for p and q do not answer that question; alias analysis §3.18 does. Extensions that number the states of memory, such as memory SSA, still need alias analysis to be precise.
Single assignment is a property of the text, not of execution. A version inside a loop takes a new value on every pass, and the machine register that eventually holds the version is written many times.
A phi function selects; it does not merge. Merging is what an analysis does when it forgets which path was taken, and that loss of path sensitivity §3.11 belongs to the analysis, not to SSA form.
Phi operands belong to predecessor blocks, not to positions. A transformation that changes the CFG, for example by splitting an edge or duplicating a block, must update the block labels in every affected phi function; a stale label selects the wrong value.
Translating out of SSA form has known pitfalls, named the lost-copy problem and the swap problem in the compiler literature. The swap problem is the parallel-copy case of the worked example.
Questions answered
How does a phi node represent alternative reaching values?
A phi node represents alternative reaching values by associating each incoming control-flow edge with the value that reaches the merge point along that edge, and by taking, at run time, the value paired with the edge that execution actually used. In static single assignment (SSA) form, where every variable has exactly one definition, means that equals when execution arrives from block and when it arrives from block . With and , is 3 on one path and 7 on the other, never their average and never both.
All phi nodes at the start of a block select at the same moment, each from the value its operand had when execution left the predecessor block. That is how a loop header’s phi node takes the initial value when the loop is entered and the updated value each time execution returns along the back edge, the jump from the end of the loop body to its start. The phi node gives the merged value one name with one definition, so every later use points to exactly one definition. It says nothing about memory: whether two pointers refer to the same location remains a question for alias analysis.
Related entries: Control-flow graph (CFG) (§3.13), Reaching definitions (§3.16), Intermediate representation (IR) (§3.37), Def-use chain (§3.17), Alias analysis (§3.18), Basic block (§3.14). Further reading: [114, 269].