Static Single Assignment Form and Phi Functions
Static Single Assignment (SSA) form is an intermediate representation in which each name is assigned exactly once. When several control-flow paths can provide a value, a φ-function (pronounced “phi”) creates one new name by selecting the value that arrived from the path actually taken.
This representation gives compiler optimizations a direct, explicit view of where every value came from.
The problem before SSA
Consider ordinary code:
x = 10
if enabled:
x = 20
print(x)
The print might use either assignment to x. A compiler must determine which assignment can reach that use. This is a data-flow analysis: an analysis that tracks facts, such as possible values or definitions, as they move through a program.
In larger code, the same variable may be assigned in many branches and loops. An optimization such as constant propagation—replacing a variable with a known constant—must repeatedly answer questions like these:
- Which assignments can reach this use?
- Can two assignments reach it along different paths?
- If they can, do they produce the same value?
- Does a later assignment make an earlier one irrelevant?
Without SSA, these questions are usually represented as sets of possible “reaching definitions,” where a definition is an operation that assigns a value. Every analysis must account for variables being overwritten and for definitions merging at branches.
One assignment, one version
SSA gives each assignment a distinct name, often by adding a number. The example can be represented conceptually as:
x1 = 10- if
enabled, thenx2 = 20 - otherwise, keep
x1 x3 = φ(x1, x2)- use
x3
The original source variable x has become several SSA names: x1, x2, and x3. Each name is assigned once. A use refers to exactly one definition, so the compiler no longer needs to search through every earlier assignment to discover what it might mean.
SSA names are names for values in the compiler’s intermediate representation, not necessarily separate storage locations in the final machine code. Later compiler stages can assign several SSA names to the same register or stack slot when their lifetimes do not overlap.
What a φ-function does
A φ-function belongs at a control-flow join: a point where two or more possible execution paths meet. The compiler models a program as a control-flow graph, or CFG, whose nodes are basic blocks—straight-line sequences with one entry and one exit—and whose edges represent possible transfers of control.
For the example, the CFG has an entry block, a block for the then branch, a block for the path where enabled is false, and a block containing print. The final block has two incoming edges. Its φ-function has one input associated with each incoming edge:
x3 = φ(x1 from false edge, x2 from true edge)
If execution arrived from the true branch, x3 gets x2. If it arrived from the false branch, x3 gets x1.
This is not normally a function call executed like an ordinary source-language function. It is a statement of the value-flow rule at a join. In a lower-level representation, the compiler implements the choice by arranging copies or register assignments along the incoming edges. The φ-function itself makes the choice explicit while analysis is taking place.
A useful way to read it is: a φ-function chooses a value based on which predecessor block executed immediately before the current block. It does not choose based on the value of a condition after the fact, and it does not merge values by adding or combining them.
Loops need φ-functions too
Loops show why φ-functions are more than branch notation:
x = 0
while condition():
x = x + 1
print(x)
Conceptually, the loop-header value of x is:
x1 = 0on the first entryx2 = φ(x1, x3)at the loop headerx3 = x2 + 1in the loop body
On the first iteration, the φ-function selects x1. On later iterations, it selects x3 from the preceding iteration. The cycle in the CFG therefore becomes an explicit cycle in the value definitions, while every SSA name still has only one assignment.
Why optimizations become simpler
SSA turns an implicit search problem into an explicit graph of values. A use of x3 has one definition. The compiler can follow that definition directly, rather than maintaining a changing set of assignments to x.
That helps several common optimizations:
- Constant propagation: if an SSA name is defined as a constant, its uses can often be replaced directly. A φ-function exposes whether all incoming values are the same constant.
- Dead-code elimination: if a computed SSA name has no uses, its defining instruction may be removable, provided it has no required side effects.
- Copy propagation: if
x2is only a copy of another value, uses can refer to the original value. - Value numbering and common-subexpression elimination: expressions can be compared using their operand definitions, making equivalent computations easier to recognize.
- Register allocation preparation: each SSA name represents a value with a more precise lifetime, which helps the compiler reason about when storage can be reused.
SSA does not make every compiler problem disappear. Memory is harder: two names may refer to the same object, and loads and stores need separate alias and memory analyses. Compilers therefore often use SSA for scalar values while representing memory with additional mechanisms.
You may encounter SSA indirectly in compiler diagnostics, optimization reports, debugger output, or intermediate representations such as those used by LLVM and many just-in-time compilers. The numbered names and phi operations are the visible trace of a central idea: assign every value a single origin, then make control-flow merges explicit instead of forcing every optimization to rediscover them.