Sub topic 22
Static single assignment form
This chapter assumes you can read three-address code, split it into basic blocks, and draw a control flow graph. It also assumes you know what reaching definitions are and what dominators are. Static single assignment form (SSA) is an intermediate representation. Each variable has exactly one assignment in the program text. GATE rarely asks about it, but the questions it does ask are mechanical: rename the variables, count the phi functions, or say where a phi function goes.
The idea
In SSA form, every assignment creates a new version of its variable, written with a subscript. The two uses of a below refer to different values, so they get different names.
Original SSA
a = 1 a1 = 1
a = a + 2 a2 = a1 + 2
b = a * 2 b1 = a2 * 2Inside one basic block, renaming is easy. Each definition bumps the version number, and each use refers to the latest version. Straight-line code needs nothing more.
The following script performs this renaming for one block. A name that has no earlier definition in the block, such as a constant or a live-in variable, is left unchanged.
def rename_block(stmts):
version = {}
out = []
def use(v):
return f"{v}{version[v]}" if v in version else v
for dest, args in stmts:
new_args = [use(a) for a in args]
version[dest] = version.get(dest, 0) + 1
out.append((f"{dest}{version[dest]}", new_args))
return out
block = [("a", ["1"]), ("a", ["a", "+", "2"]), ("b", ["a", "*", "2"])]
for dest, args in rename_block(block):
print(dest, "=", " ".join(args))
# a1 = 1
# a2 = a1 + 2
# b1 = a2 * 2Phi functions at join points
Renaming breaks down where two control paths meet. After an if/else, the program has two possible versions of x, and the code after the join cannot name just one. SSA solves this with a phi function (written φ, or phi in plain text). A phi function sits at the start of the join block and selects a value according to the edge control arrived by.
B1: if c goto B2 else B3
B2: x = 2
B3: x = 3
B4: y = x + 1B2: x1 = 2
B3: x2 = 3
B4: x3 = phi(x1, x2)
y1 = x3 + 1The operands of a phi function are listed in the same order as the block's predecessors. If B4's predecessors are (B2, B3), then x3 is x1 when control comes from B2 and x2 when it comes from B3.
Phi functions at loop headers
A loop header has two predecessors: the block before the loop and the back edge. Any variable assigned inside the loop needs a phi function there.
B1: i1 = 0
B2: i2 = phi(i1, i3)
if i2 < 10 goto B3 else B4
B3: i3 = i2 + 1
goto B2
B4: use i2The phi function in B2 uses i3, which is defined later in the text. This is legal because a phi function reads its operand along a specific edge. The code after the loop uses i2, since B2 dominates B4.
Where phi functions go
Placing a phi function for every variable at every join block is correct, but it produces many useless ones. The standard algorithm uses dominance frontiers.
- The dominance frontier of a block B is the set of blocks N such that B dominates a predecessor of N but does not strictly dominate N.
- For each variable v, put a phi function in the dominance frontier of every block that defines v.
- A phi function is itself a definition, so repeat the step until nothing new is added. This is the iterated dominance frontier.
- Then rename. Walk the dominator tree and keep a stack of current versions for each variable. At each phi function, fill in the operand for the edge being visited.
In the diamond, DF(B2) = DF(B3) = {B4}, so x gets a phi function in B4. B1 defines nothing that needs one. In the loop, B3 defines i, and DF(B3) = {B2}. B2 is also in its own frontier, so the phi function lands in B2 and nowhere else.
Why compilers use it
| Property | Effect |
|---|---|
| One definition per name | Each use has exactly one reaching definition, so def-use chains are direct. |
| No need to track kills | Constant propagation becomes a lookup. If `x3 = 5`, every use of `x3` is 5. |
| Unused names are visible | A name with no uses is dead code, found without a liveness analysis. |
| Phi functions add size | The program grows, and the construction needs the dominator tree. |
SSA does not remove the need for data flow reasoning. It builds the answer to reaching definitions into the names, which makes several of the optimizations from the previous chapter simpler.
Common errors
- Giving the subscript to the use instead of the definition. Every assignment, including a phi function, creates the next version.
- Writing phi operands in the wrong predecessor order.
- Adding phi functions for variables that are only defined on one side of a branch but never used after the join. Pruned SSA leaves these out, but minimal SSA keeps them. Check which form the question asks for.
- Forgetting that the phi function at a loop header takes a value from the back edge.
Exercise
A CFG has blocks B1 to B4. B1 branches to B2 and B3, and both go to B4. The variable x is assigned in B1, B2 and B3, and used in B4. How many phi functions for x does minimal SSA need, and where?
Show answer
One, in B4. DF(B2) = DF(B3) = {B4}, so B4 needs a phi function. DF(B1) is empty because B1 dominates every block. The result is x4 = phi(x2, x3), taking x2 from B2 and x3 from B3, and x1 is not an operand because B1's value is overwritten on both paths.
Exercise
In the loop example, why does B4 use i2 and not i3?
Show answer
B4 is reached only from B2, when the test fails. At that point the current value of i is the one produced by the phi function in B2, which is i2. The value i3 is defined in B3, which does not dominate B4.