Search

Search papers and lessons

Phi Function

Level 3 · 8 min read

A phi function is a special statement placed where two paths of a program meet; it picks, at run time, the value that arrived along the path actually taken.

On this page

  • Colour = topic mix
  • Systems
  • Discrete math
  • Sign in to see your progress

TL;DR

A phi function, written ϕ\phi, 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 BB have predecessor blocks P1,P2,…,PkP_1, P_2, \dots, P_k in the Control-Flow Graph. Suppose a variable vv has versions viv_{i} that reach the end of each PjP_j. A phi statement at BB has the form

vnew=ϕ(va,vb,… )v_{\text{new}} = \phi(v_{a}, v_{b}, \dots)

where the argument in position jj is the version of vv that is live at the end of predecessor PjP_j. The symbol ϕ\phi is not a real function that is called at run time. It is a notation for "select by incoming edge".

Definition: Phi function

A phi function at block BB with predecessors P1,…,PkP_1, \dots, P_k is a statement v=ϕ(v1,…,vk)v = \phi(v_1, \dots, v_k) such that, when control arrives at BB from PjP_j, the value of vv is vjv_j.

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

Example
Example 1: a single join (tiny)

Code: x = 1, then if c: x = 2, then the join.

  1. Block B0 ends with x1 = 1 and a test on c.
  2. Block B1 (then-branch) contains x2 = 2.
  3. The join block B2 has two predecessors, B0 (when c is false) and B1. So it contains x3 = phi(x1, x2), with x1 listed for the B0 edge and x2 for the B1 edge.

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.

Example
Example 2: an if-else with a value used later (standard)

Code:

if c:
    x = 10
else:
    x = 20
y = x + 5
  1. The then-branch defines x1 = 10 and the else-branch defines x2 = 20.
  2. The join block contains x3 = phi(x1, x2) and then y1 = x3 + 5.
  3. Suppose c is true. The then-edge is taken, so x3 takes x1, which is 10. Then y1 = 10 + 5 = 15.
  4. Suppose c is false. The else-edge is taken, so x3 takes x2, which is 20. Then y1 = 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.

Example
Example 3: a loop header (realistic)

Code:

i = 0
while i < n:
    i = i + 1

The loop header is a block with two predecessors: the block before the loop and the loop body, which jumps back to the header.

  1. Before the loop, i1 = 0.
  2. The header holds i2 = phi(i1, i3), where i1 is listed for the entry edge and i3 for the back edge.
  3. The body computes i3 = i2 + 1 and jumps back.
  4. On the first test, the entry edge was taken, so i2 = i1 = 0. Each later pass takes the back edge, so i2 becomes the previous i3.

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

SituationWhat goes wrongBetter choice
A phi is placed in a block with one predecessorIt has nothing to choose, so it only adds noiseOnly place phis at blocks with two or more predecessors
Argument order does not match predecessor orderThe wrong value is used on some paths, silentlyKeep the argument list tied to the predecessor list when editing the graph
Phi statements left in code sent to a machineNo hardware instruction matches a phiRemove phis before code generation by copying values onto incoming edges
Many phis in a large functionCompile time and memory growRemove 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.

Sources