Section 3.38

Static single assignment (SSA)

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 x1x_1 or x2x_2. Where control-flow paths that assigned different versions meet, a phi function, written ϕ\phi, 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 x1=3x_1 = 3, the right path assigns x2=7x_2 = 7, and the statement after the if needs a single name. SSA form adds a new definition there, x3=ϕ(x1,x2)x_3 = \phi(x_1, x_2), which means: x3x_3 is x1x_1 if execution came from the left path and x2x_2 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 x1=3x_1 = 3, every use of x1x_1 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.

  1. Place phi functions. A block needs a phi function for variable xx when different definitions of xx can reach it along different edges. The standard placement uses dominance. Block AA dominates block BB when every path from the program’s entry to BB passes through AA; AA strictly dominates BB when, in addition, A≠BA \ne B. The dominance frontier of AA is the set of blocks BB such that AA dominates a predecessor of BB but does not strictly dominate BB. A phi function for xx goes into every block in the dominance frontier of every block that assigns xx. Each new phi function is itself an assignment of xx, 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].

  2. 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 xx, including each phi function, a fresh version number, and rewrite each use of xx 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 x3=ϕ(x1,x2)x_3 = \phi(x_1, x_2), the left block ends with the copy x3←x1x_3 \leftarrow x_1 and the right block with x3←x2x_3 \leftarrow x_2. Register allocation then often gives x1x_1, x2x_2 and x3x_3 the same register, which removes the copies. Two complications need care.

Worked example

A merge. Block B0B_0 tests a condition cc and jumps to block B1B_1 or block B2B_2. Block B1B_1 sets x1=3x_1 = 3, block B2B_2 sets x2=7x_2 = 7, and both jump to block B3B_3, which computes x3=ϕ(B1:x1,B2:x2)x_3 = \phi(B_1{:}\ x_1,\ B_2{:}\ x_2) and then y1=x3+1y_1 = x_3 + 1. Block B1B_1 dominates a predecessor of B3B_3, namely itself, but does not dominate B3B_3, so B3B_3 is in the dominance frontier of B1B_1, and likewise of B2B_2; that is why the phi function stands in B3B_3. If cc is true, execution runs B0,B1,B3B_0, B_1, B_3: x3=3x_3 = 3 and y1=3+1=4y_1 = 3 + 1 = 4. If cc is false, execution runs B0,B2,B3B_0, B_2, B_3: x3=7x_3 = 7 and y1=7+1=8y_1 = 7 + 1 = 8. The average (3+7)/2=5(3 + 7) / 2 = 5 would give y1=6y_1 = 6, a value that no execution produces.

An analysis that forgets which path was taken summarizes x3x_3 either as the set {3,7}\{3, 7\} or, in interval analysis §3.9, as the range [3,7][3, 7]. 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.

Execution enters B1B_1 from B0B_0, so i1=0i_1 = 0; since 0<30 < 3, B2B_2 computes i2=1i_2 = 1. Entering B1B_1 from B2B_2 gives i1=1i_1 = 1, then i2=2i_2 = 2; then i1=2i_1 = 2 and i2=3i_2 = 3; then i1=3i_1 = 3, and since 3<33 < 3 is false, B3B_3 returns 3. The phi function ran 4 times: once from B0B_0, and 3 times along the back edge from B2B_2, the jump that returns to the start of the loop. The version i1i_1 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 a1=ϕ(B0:a0,B2:b1)a_1 = \phi(B_0{:}\ a_0,\ B_2{:}\ b_1) and b1=ϕ(B0:b0,B2:a1)b_1 = \phi(B_0{:}\ b_0,\ B_2{:}\ a_1). Suppose a1=1a_1 = 1 and b1=2b_1 = 2 when execution leaves B2B_2. Selecting simultaneously gives the new a1=2a_1 = 2 and the new b1=1b_1 = 1, the intended exchange. Copies done one after another at the end of B2B_2, first a1←b1a_1 \leftarrow b_1 and then b1←a1b_1 \leftarrow a_1, give a1=2a_1 = 2 and then b1=2b_1 = 2: the value 1 is lost. A correct translation saves one value first: t←a1t \leftarrow a_1, a1←b1a_1 \leftarrow b_1, b1←tb_1 \leftarrow t, which gives a1=2a_1 = 2 and b1=1b_1 = 1.

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.

  1. Single definition: each variable is defined by exactly one instruction or phi function in the program text.

  2. Phi selection: let block BB have predecessors P1,…,PkP_1, \dots, P_k and begin with the phi functions ym=ϕ(P1:am,1,…,Pk:am,k)y_m = \phi(P_1{:}\ a_{m,1}, \dots, P_k{:}\ a_{m,k}) for m=1,…,nm = 1, \dots, n. When execution enters BB along the edge from PjP_j, every ymy_m receives the value that am,ja_{m,j} held when execution left PjP_j. All nn selections happen at once, so no phi function sees the new value of another.

  3. Dominance, required by the usual strict form: the definition of each variable dominates each of its uses. A phi operand am,ja_{m,j} counts as used at the end of PjP_j, so the definition of am,ja_{m,j} must dominate PjP_j.

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, x3=ϕ(B1:x1,B2:x2)x_3 = \phi(B_1{:}\ x_1,\ B_2{:}\ x_2) means that x3x_3 equals x1x_1 when execution arrives from block B1B_1 and x2x_2 when it arrives from block B2B_2. With x1=3x_1 = 3 and x2=7x_2 = 7, x3x_3 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].