Sub topic 18
Static Single Assignment Form and GATE Problem Strategies
Assume you know basic control-flow graphs, dominance, set operations, and three-address code. Static single assignment (SSA) form is an intermediate representation in which each variable is assigned exactly once. Different assignments receive different names, such as x1, x2, and x3.
SSA makes data dependencies explicit. A use of x2 always refers to one specific assignment. This helps compilers perform constant propagation, dead code elimination, and other optimizations.
Building SSA form
Consider this program:
int choose(int p) {
int x;
if (p > 0)
x = 10;
else
x = 20;
return x + 1;
}The two assignments to x reach the same join point. SSA inserts a special phi function there:
if (p > 0)
x1 = 10;
else
x2 = 20;
x3 = phi(x1, x2);
return x3 + 1;The phi function chooses a value based on which control-flow edge was taken. It is not an ordinary run-time function call. It represents the merging of definitions at a control-flow join.
A phi node is needed when different definitions of the same source variable can reach a block. It is not needed merely because a variable is used more than once. In a loop, a phi often merges the initial value with the value produced by the previous iteration:
i1 = 0
L:
i2 = phi(i1, i3)
i3 = i2 + 1
if i3 < n goto LThe first argument represents the entry edge. The second represents the back edge.
GATE problem patterns and solving strategies
GATE questions commonly test whether a phi node is required, which definition reaches a use, or whether an optimization is safe.
| Pattern | Reliable method |
|---|---|
| Need for a `phi` node | Find a join with multiple reaching definitions |
| Dead assignment | Check whether its value has any later use |
| Constant propagation | Replace a variable only when every reaching definition has the same constant |
| Loop-invariant code | Move an expression only when its operands and memory effects remain unchanged |
For SSA questions, first draw the control-flow graph. Mark each assignment with a version. At every join, inspect definitions arriving along incoming edges. Then trace each use to its unique version.
For optimization questions, distinguish may information from must information. Reaching definitions uses a union because a definition may arrive along any path. Available expressions uses an intersection because an expression must be available along every path before replacement is safe. Live-variable analysis works backward and asks whether a value may be used later.
Exercise
A block is reached from two predecessors. The first defines a1, and the second defines a2. The block uses a. What SSA operation is required?
Show answer
Rename the use to a new version, such as a3, and insert a3 = phi(a1, a2) at the block's beginning.