Algorithm Search⌘K

Problemcore

Out-of-SSA translation

Also called SSA destruction, SSA deconstruction.

Replace the phi-functions of an SSA program by copies on incoming edges so that the program runs on a machine without phi-functions.

Out-of-SSA translation is the task of turning a program in static single assignment form back into ordinary code. Machines have no phi-functions, so each one must become copies: when control enters block bb from a predecessor pp, each phi-function of bb copies its operand for pp into its target. Straight after SSA construction this is easy, because all names of one variable can take the old name back and the phi-functions disappear. After optimizations such as copy propagation, names joined by a phi-function can be live at the same time, and the simple translation gives wrong code. The two classic failures are the lost copy and swap problems. So the task has two parts: produce correct code, and leave as few copies and new blocks as possible.

In plain words

A phi-function says that the value at this point depends on where control came from. A real machine cannot do that, so the compiler puts copies on each incoming path instead. Two things can go wrong. A copy at the end of the previous block also runs when that block goes somewhere else. And the phi-functions at the top of one block all take their values at the same moment, while copies run one after another, so one copy can overwrite a value that a later copy still needs. A good translation avoids both mistakes. Then it removes as many copies as it can, because each copy costs time and code size.

Definition

Input. A procedure in strict SSA form over a control flow graph G=(N,E,r)G = (N, E, r). A block bb can start with phi-functions x=ϕ(a1,…,ak)x = \phi(a_1, \ldots, a_k) that have one operand for each predecessor. An operand is a name or a constant.

Meaning of the phi-functions. When control moves along the edge (p,b)(p, b), all phi-functions of bb first read their operands for pp, and then all of them write their targets. So on that edge, the phi-functions x1,…,xmx_1, \ldots, x_m of bb act as one parallel copy

(x1,…,xm)←(a1p,…,amp).(x_1, \ldots, x_m) \leftarrow (a_{1p}, \ldots, a_{mp}).

Output. A procedure with no phi-functions. It can have new blocks on edges and new copy instructions. On every input it must return the same value, trap in the same way, or run forever, as the SSA procedure does.

Quality. Fewer copies are better, and fewer new blocks and jumps are better. A copy on a frequent edge costs more than a copy on a rare one.

Conventional SSA form. Two names are phi-related if one is the target and the other an operand of the same phi-function. The phi congruence classes are the classes of the smallest equivalence relation that contains this relation. Two names interfere if one is live at the definition of the other. A program is in conventional SSA form (CSSA) if no two names in the same class interfere. Otherwise it is in transformed SSA form (TSSA). In CSSA form, a correct translation needs no copies: give all names of a class one variable, and delete the phi-functions. Sreedhar, Ju, Gillies and Santhanam (1999) introduced these terms and showed that SSA form straight after construction is conventional. Copy propagation and value numbering can make it transformed.

A solution always exists. Split each critical edge whose target has phi-functions. Then on each edge (p,b)(p, b), either pp has one successor or bb has one predecessor. Put the parallel copy of the edge at the end of pp in the first case, and at the top of bb in the second, and turn it into sequential copies as in the Bounds section. This is correct for every strict SSA program. The copy runs exactly when the edge is taken. It reads each operand when the phi-function would read it. It writes xix_i just before bb starts, and that clobbers nothing: by strictness, every use of xix_i comes after the phi-function in bb. The rest of the problem is to do this with fewer copies and fewer new blocks.

Variants

  • With or without edge splitting. Splitting is simple, but it adds blocks and jumps, and some edges cannot be split. Sreedhar et al. (1999) and Boissinot et al. (2009) first make the program conventional with extra copies, and then rename. Neither splits edges.
  • Before or after register allocation. Most compilers leave SSA form before register allocation. SSA-based register allocation assigns registers first, and then the parallel copies are moves between registers. Cycles then need a free register or a swap instruction.
  • Machine constraints. Two-address instructions, fixed registers at calls, and edges that cannot be split add more copies. Chapter 21 of the SSA book by Rastello and Bouchez Tichadou covers these cases.
  • Fewest copies. After a correct translation, a coalescing pass gives one variable to names that do not interfere, and deletes the copies between them. This is the coalescing step of Chaitin's register allocator on the interference graph. Boissinot et al. also let two names share a variable when they always hold the same value, even if both are live.

Examples

Conventional SSA: renaming is enough

# params: c
B1:
  br c B2 B3
B2:
  x.1 = 1
  jump B4
B3:
  x.2 = 2
  jump B4
B4:
  x.3 = phi [B2: x.1, B3: x.2]
  ret x.3

The only class is {x.1,x.2,x.3}\{x.1, x.2, x.3\}, and its names are never live at the same time. Rename all three to x and delete the phi-function. The result is the program before SSA construction.

The lost copy

# params: n
B1:
  x.1 = 1
  jump B2
B2:
  x.2 = phi [B1: x.1, B2: x.3]
  x.3 = x.2 + 1
  t.1 = x.3 < n
  br t.1 B2 B3
B3:
  ret x.2

This program returns n−1n - 1 for n≥2n \ge 2. Copy propagation produced it from a program with y.1 = x.2 in B2 and ret y.1 in B3. Now x.2x.2 is still live when x.3x.3 is defined, so the program is not conventional. The copy x.2 = x.3 at the end of B2 also runs on the exit to B3, and the program returns 3 for n=3n = 3 instead of 2. Splitting the critical edge from B2 to B2 gives the copy its own block, and the result is correct again. The lost copy and swap problems page goes through both cases in detail.

The swap

# params: n
B1:
  a.1 = 1
  b.1 = 2
  i.1 = 1
  jump B2
B2:
  a.2 = phi [B1: a.1, B2: b.2]
  b.2 = phi [B1: b.1, B2: a.2]
  i.2 = phi [B1: i.1, B2: i.3]
  i.3 = i.2 + 1
  t.1 = i.2 < n
  br t.1 B2 B3
B3:
  r.1 = a.2 * 10
  r.2 = r.1 + b.2
  ret r.2

Each trip around the loop swaps aa and bb, so the program returns 12, 21, 12, ... for n=1,2,3,…n = 1, 2, 3, \ldots. On the back edge, the parallel copy is (a.2,b.2,i.2)←(b.2,a.2,i.3)(a.2, b.2, i.2) \leftarrow (b.2, a.2, i.3). Split the back edge into a new block B4. If B4 does a.2 = b.2 and then b.2 = a.2, both get the old value of b.2b.2, and the program returns 22 for n=2n = 2. A correct order needs one temporary:

B4:
  i.2 = i.3
  t = b.2
  b.2 = a.2
  a.2 = t
  jump B2

Known algorithms

method idea copies before coalescing
Cytron et al. (1991) a copy for each phi operand at the end of the predecessor, then dead code elimination and coalescing by coloring one per phi operand; wrong after copy folding
edge splitting with parallel copies split critical edges, put one parallel copy on each edge, then sequentialize one per phi operand, plus one per cycle
Briggs et al. (1998) use liveness to find copies whose target is still needed, order the copies on each edge, and use temporaries for cycles about one per phi operand
Sreedhar et al. (1999) add copies until the program is conventional, chosen with liveness and interference, then rename each class; no edge splitting at most one per phi operand and one per phi-function
Boissinot et al. (2009) add parallel copies for every phi-function, coalesce with interference tests that use values and dominance, then sequentialize; no edge splitting as Sreedhar's first method, then coalesced

Cytron et al. give the first method. It is correct while the program is conventional, which holds when no optimization ran in between. Briggs, Cooper, Harvey and Simpson (1998) showed how copy folding during renaming breaks it, with the lost copy and swap problems, and gave a repair. Sreedhar et al. (1999) define three methods. Method I copies every operand and every target. Method III uses liveness to copy only where two names in a class would interfere, and gives far fewer copies. Boissinot et al. (2009) start from Method I with parallel copies and leave the copy removal to a coalescer. Their interference test is fast because it uses the dominance order of definitions.

AlgorithmTimeSpacePropertiesCode
Sreedhar out-of-SSA translationO(F B N V + N^2 V^2) worst · averageO(N + V^2) extradeterministicofflineiterative1 language

Bounds

  • Copies. Splitting with parallel copies uses at most one copy per phi operand, plus one per cycle in each parallel copy. Sreedhar's Method I uses exactly one copy per phi operand and one per phi-function. Neither needs liveness.

  • New blocks. Edge splitting adds at most one block for each critical edge that enters a block with phi-functions. Sreedhar's and Boissinot's methods add none.

  • Time. Splitting and Method I take time linear in the size of the program. Methods that use liveness or interference need that analysis first.

  • A parallel copy needs at most one extra temporary per cycle. Let PP be a parallel copy di←sid_i \leftarrow s_i with different targets did_i, and drop the moves with di=sid_i = s_i. Draw an edge si→dis_i \to d_i for each move. Each location has at most one incoming edge, so each part of the graph is a tree, or a single cycle with trees hanging from it. Let cc be the number of cycles. Then PP can be done with ∣P∣+c|P| + c sequential copies and one extra location tt, and the same tt serves every cycle.

    Proof. Repeat two rules. Rule 1: if a pending move d←sd \leftarrow s has a target dd that no pending move reads, emit it and remove it. This is safe, because no move still needs the old value of dd. Rule 2: if rule 1 cannot apply but moves are pending, then every target is also a source. There are as many targets as moves and at most as many sources as moves, so the sources and targets are the same set, and each location is the source of exactly one pending move and the target of exactly one. So the pending moves form disjoint cycles. Pick a move d←sd \leftarrow s, emit t←st \leftarrow s, and change the move to d←td \leftarrow t. Now no pending move reads ss, so rule 1 applies to the move into ss, then to the move into its source, and so on around the cycle. The last move emitted on it is d←td \leftarrow t, so tt is free before rule 2 runs again. Rule 1 never emits a move on an unbroken cycle, because each target on a cycle is read by the next move. So rule 2 runs once per cycle, and the total is ∣P∣+c|P| + c copies. With a worklist of free targets this takes O(∣P∣)O(|P|) time.

  • The extra copy cannot be avoided. Take a parallel copy that is one cycle d2←d1,d3←d2,…,d1←dkd_2 \leftarrow d_1, d_3 \leftarrow d_2, \ldots, d_1 \leftarrow d_k with k≥2k \ge 2 and different values in the did_i. With only copies, it needs k+1k + 1 of them. Proof. Each did_i must end with a new value, so each is written at least once. With only kk copies, each did_i is written exactly once and no other location is written. The first copy writes some djd_j. At that moment the old value of djd_j is in no other location, so it is lost. But the final state needs it in dj+1d_{j+1}. A machine with a swap instruction can do the cycle with k−1k - 1 swaps and no temporary.

Reductions

  • From conventional SSA, the problem is renaming. Union the names of each phi congruence class and delete the phi-functions. So every method is a way to make the program conventional, by copies or by edge splits.
  • Copy removal is coalescing. After copies are in, deleting as many as possible is the coalescing problem of register allocation: merge names that do not interfere on the interference graph. The same tools apply.
  • Sequentializing a parallel copy is the decomposition of a functional graph into trees and cycles, as the proof above shows.
  • The inverse problem is SSA construction. Construction followed at once by destruction gives back the original program, up to names, because the result of construction is conventional.

Applications

Every compiler that optimizes in SSA form must leave it before it emits code. GCC leaves SSA form when it expands its GIMPLE code into RTL, and it first coalesces SSA names into variables. LLVM keeps SSA form on its machine code and replaces phi-functions by copies in the predecessor blocks just before register allocation. Compilers with SSA-based register allocation or SSA linear scan allocate first and then turn each phi-function into register moves, where the cycle rule above decides when a free register or a swap is needed.

References

  1. Ron Cytron, Jeanne Ferrante, Barry K. Rosen, Mark N. Wegman and F. Kenneth Zadeck, Efficiently computing static single assignment form and the control dependence graph, ACM Transactions on Programming Languages and Systems 13(4), 1991, pages 451-490, the translation out of SSA form, with copies at the ends of predecessor blocks, followed by dead code elimination and allocation by coloring to remove most copies
  2. Preston Briggs, Keith D. Cooper, Timothy J. Harvey and L. Taylor Simpson, Practical improvements to the construction and destruction of static single assignment form, Software: Practice and Experience 28(8), 1998, pages 859-881, the lost copy and swap problems that appear after copy folding, and a translation that uses liveness and orders the copies, with temporaries for cycles
  3. Vugranam C. Sreedhar, Roy Dz-Ching Ju, David M. Gillies and Vatsa Santhanam, Translating out of static single assignment form, Static Analysis Symposium (SAS 1999), LNCS 1694, conventional and transformed SSA form, phi congruence classes, and three methods of copy insertion that need no edge splitting
  4. Benoit Boissinot, Alain Darte, Fabrice Rastello, Benoît Dupont de Dinechin and Christophe Guillon, Revisiting out-of-SSA translation for correctness, code quality, and efficiency, International Symposium on Code Generation and Optimization (CGO 2009), pages 114-125, phi-functions as parallel copies, their sequentialization with one extra variable, and interference tests that use values and dominance
  5. Fabrice Rastello and Florent Bouchez Tichadou (editors), SSA-based Compiler Design, Springer, 2022, Chapter 3 (Standard Construction and Destruction Algorithms); Chapter 21 (SSA Destruction for Machine Code)
  6. Andrew W. Appel with Jens Palsberg, Modern Compiler Implementation in Java, second edition, Cambridge University Press, 2002, Chapter 19 (Static Single-Assignment Form), conversion out of SSA form

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a contract.yaml