TL;DR
A phi function, written , is a statement that a compiler places where two paths of a program join (a block that control can reach from two or more earlier blocks). It has one input for each incoming path. At run time it returns the input that belongs to the path the program actually took.
It exists so that a variable can keep one name even though it may hold different values depending on the route taken to reach a point.
Where you'll see it: in the intermediate code of optimizing compilers, where it is the one statement that is allowed to choose between several earlier definitions.
Intuition
Start from a question you already know
Imagine a sign on a road that says "the price is the one you saw on the last sign you passed." Two roads merge, and each carries its own last sign. At the merge, a driver needs to know which sign to trust. The phi function is that rule written into the program. Compilers call the blocks that lead into a join its predecessors.
Consider a variable x that gets one of two values depending on a test:
if c:
x = 10
else:
x = 20
print(x)After the if, the compiler cannot say whether x is 10 or 20, because it depends on c. Without a phi, it would have to name x once at the top and once at the bottom and track a changing name in a way that breaks the one-name-one-value idea. With a phi, it writes:
if c:
x1 = 10
else:
x2 = 20
x3 = phi(x1, x2) # x1 if we came from the then-branch, x2 from the else-branch
print(x3)The phi line is the join point. It does not compute anything new. It only records which incoming value to use.
Why the compiler wants it
A compiler's analyses ask questions like "where does this value come from?" If every variable has one definition, the answer is one lookup. The phi keeps that property even at merges. Without it, every analysis would need special rules for branches and loops.
The math
Let a block have predecessor blocks in the Control-Flow Graph. Suppose a variable has versions that reach the end of each . A phi statement at has the form
where the argument in position is the version of that is live at the end of predecessor . The symbol is not a real function that is called at run time. It is a notation for "select by incoming edge".
A phi function at block with predecessors is a statement such that, when control arrives at from , the value of is .
Two facts follow directly from the definition:
- A phi appears only at the start of a block that has at least two predecessors. A block with one predecessor has no choice to make, so it never needs one.
- The number of arguments equals the number of predecessors. If a predecessor does not define the variable on any path, the argument is the value that reaches the end of that predecessor, which may be the value from further upstream.
Running a phi
To execute v = phi(v1, v2), the runtime looks at the edge that was just taken. If it came from the first predecessor, v takes the value of v1. The phi must be read all at once: when several phis sit at the top of one block, they all use the values from the edge taken before any of them is assigned.
Worked examples
Code: x = 1, then if c: x = 2, then the join.
- Block
B0ends withx1 = 1and a test onc. - Block
B1(then-branch) containsx2 = 2. - The join block
B2has two predecessors,B0(whencis false) andB1. So it containsx3 = phi(x1, x2), withx1listed for theB0edge andx2for theB1edge.
Interpretation: if c is false, the edge from B0 is taken, so x3 = x1 = 1. If c is true, x3 = x2 = 2. The phi lists its arguments in the same order as the predecessors, so the choice is unambiguous.
Code:
if c:
x = 10
else:
x = 20
y = x + 5- The then-branch defines
x1 = 10and the else-branch definesx2 = 20. - The join block contains
x3 = phi(x1, x2)and theny1 = x3 + 5. - Suppose
cis true. The then-edge is taken, sox3takesx1, which is10. Theny1 = 10 + 5 = 15. - Suppose
cis false. The else-edge is taken, sox3takesx2, which is20. Theny1 = 20 + 5 = 25.
Interpretation: the single line y1 = x3 + 5 works for both paths. The phi is the only place that knows about the branch, which keeps the rest of the code simple.
Code:
i = 0
while i < n:
i = i + 1The loop header is a block with two predecessors: the block before the loop and the loop body, which jumps back to the header.
- Before the loop,
i1 = 0. - The header holds
i2 = phi(i1, i3), wherei1is listed for the entry edge andi3for the back edge. - The body computes
i3 = i2 + 1and jumps back. - On the first test, the entry edge was taken, so
i2 = i1 = 0. Each later pass takes the back edge, soi2becomes the previousi3.
Interpretation: with n = 3, the header sees i2 = 0, then 1, then 2, then 3, when the test fails and the loop exits. The phi at the header is what lets one name carry the counter around the loop.
Tradeoffs and failure modes
| Situation | What goes wrong | Better choice |
|---|---|---|
| A phi is placed in a block with one predecessor | It has nothing to choose, so it only adds noise | Only place phis at blocks with two or more predecessors |
| Argument order does not match predecessor order | The wrong value is used on some paths, silently | Keep the argument list tied to the predecessor list when editing the graph |
| Phi statements left in code sent to a machine | No hardware instruction matches a phi | Remove phis before code generation by copying values onto incoming edges |
| Many phis in a large function | Compile time and memory grow | Remove phis whose results are never used |
History
The phi notation was introduced in the late 1980s as part of the work on static single-assignment form, which lets a program be analysed as if each variable had one definition. Ron Cytron and his co-authors gave an efficient method for placing phi functions in 1991, and that method is still the basis of many production compilers.
Recap
A phi function sits at a join, has one argument per incoming edge, and returns the argument for the edge that was taken. It lets a variable keep one name across branches and loops, so that the compiler's analyses keep working on single definitions.
Check yourself
QuestionRecall: where in a program must a phi function appear?Show answer
At the start of a block that has at least two predecessors, which is a point where control paths merge. A block with a single predecessor never needs one, because there is no choice to make.
QuestionComputation: `x1 = 3` on one path and `x2 = 7` on the other path, merged into `x3 = phi(x1, x2)`. If the path through `x2` was taken, what is `x3`?Show answer
x3 is 7, because the phi returns the argument for the edge that was taken, and that argument is x2.
QuestionWhy: why does a phi need one argument per predecessor rather than one shared value?Show answer
Each predecessor can carry a different value of the variable. The phi must record which value applies on each incoming edge, because the value is determined by the path the program took, which is only known at run time.
QuestionApplied: a loop header has a phi `i2 = phi(i1, i3)` and the loop runs three times. What values does `i2` take across the test, in order?Show answer
It starts at i1 (the value before the loop), then takes the back-edge value i3 from each completed pass. If i1 = 0 and each pass adds 1, i2 takes the values 0, 1, 2, 3 across the four tests, and the loop exits when i2 reaches the limit.