arthrop0d

Brief articles on varied topics.

Static Single Assignment Form

compilerscomputer science

Static single assignment (SSA) form is an internal program representation in which each variable is assigned exactly once, and each later use refers to one specific assignment. When control flow can provide different values, a phi node selects the value arriving from the path that was actually taken.

SSA is not a programming language feature. It is a form a compiler uses between parsing source code and generating machine code.

The problem SSA solves

In ordinary code, one source-level variable can be assigned many times:

value = 10
if enabled:
    value = 20
value = value + 1

A compiler analyzing the final value must determine which assignment reached the addition. That depends on control flow: if enabled is true, the value is 20; otherwise it is 10. As programs become larger, loops, nested branches, and early returns make this question a general data-flow analysis problem—the problem of tracking where values come from as execution moves through a program.

Without SSA, an optimizer often has to reason about a variable as a location that may have been overwritten. It must repeatedly ask which definitions can reach each use, whether two assignments might refer to the same value, and whether a rewrite remains valid on every path.

SSA makes those relationships explicit.

One assignment, many versions

“Assigned once” does not mean the source variable can be assigned only once. It means the compiler gives each distinct assignment a new version:

value_1 = 10
if enabled:
    value_2 = 20
value_3 = value_2 or value_1
value_4 = value_3 + 1

The exact notation varies by compiler. The important part is that value_1, value_2, value_3, and value_4 are separate names, and each name has one definition.

Every use can therefore point directly to its definition. If an optimizer sees value_4, it knows it came from the addition using value_3; it does not need to search backward through every assignment to value.

This is the “static” in SSA: the versions describe the program’s possible control-flow structure before the program runs. It does not mean the value is constant. A version can still hold different runtime data on different executions; it simply has one place where that version is defined.

Control-flow joins need phi nodes

The previous example still needs a precise way to express the value after the if. The two incoming paths define different values:

entry:
    value_1 = 10
    branch enabled, then, else

then:
    value_2 = 20
    jump merge

else:
    jump merge

merge:
    value_3 = phi(value_2, value_1)
    value_4 = value_3 + 1

A basic block is a straight-line sequence of instructions with one entry and exits at its end. Here, then and else are basic blocks that both flow into merge.

The phi node in merge says:

The operands are associated with incoming edges—the connections from predecessor blocks—not merely with an arbitrary ordering. A phi node is conceptually evaluated when control enters the block, based on which predecessor was executed. It is not normally a machine instruction or a function call that runs and chooses among values at runtime. The compiler later eliminates phi nodes when converting SSA into executable code, often by inserting copies along incoming edges.

The phi node is necessary because a single name such as value would otherwise have two possible definitions at the join. SSA preserves the one-definition rule by creating value_3 as the merged result.

A phi node does not merge values because they are equal. It merges possible definitions because control flow has joined.

Loops use phi nodes too

Loops show why phi nodes are more than an if construct. Consider a counter:

count = 0
while count < limit:
    count = count + 1

In SSA-like form, the loop-carried value can be represented as:

loop:
    count_1 = phi(count_0, count_2)
    continue = count_1 < limit
    branch continue, body, exit

body:
    count_2 = count_1 + 1
    jump loop

count_0 is the initial value from before the loop. count_2 is the value produced by one iteration and carried into the next. The phi node chooses the initial value on the first entry and the previous iteration’s value on later entries.

This may look circular: count_2 contributes to the definition of count_1, which is used to compute count_2. The cycle represents repeated execution, not a circular calculation within one iteration.

Why optimizers like SSA

SSA gives optimizers a clean definition-use graph: each value has one definition, and the compiler can see all of its uses. That simplifies several transformations:

For example, if an SSA value is defined as x_2 = 4 * 5, the compiler can replace its uses with 20. If x_2 is never used, it can remove the calculation without confusing it with another assignment to source-level x.

SSA usually applies most directly to compiler-managed scalar values. A variable that refers to mutable memory may still require loads and stores, because the memory can change through aliases—different references that reach the same storage. Compilers may use a related technique called memory SSA to represent those memory versions.

Eventually, the compiler must lower SSA into a form where hardware registers and memory locations can be reused. During that process it resolves phi nodes and may combine several SSA versions into one physical register when their lifetimes do not overlap. SSA is therefore a temporary organizational form, but one that makes the difficult reasoning visible and tractable.

When a compiler log mentions an SSA construction pass, a phi insertion problem, or an SSA value in an optimization diagnostic, it is referring to this intermediate representation: uniquely defined versions connected explicitly at control-flow joins.

← All articles