Where Do We Place φ -Functions? Approaches to Placing φ -Functions Basic Rule Minimal – If two distinct (non-null) paths x → z and y → z converge at node z, and – As few as possible subject to the basic rule nodes x and y contain definitions of variable v, then a φ -function for v is inserted at z Briggs-Minimal – Same as minimal, except v must be live across some edge of the CFG v 1 :=... v 2 :=... x y Pruned – Same as minimal, except dead φ -functions are not inserted v 3 := φ (v 1 ,v 2 ) z ...v 3 ... What’s the difference between Briggs Minimal and Pruned SSA? CS553 Lecture Static Single Assignment Form 1 CS553 Lecture Static Single Assignment Form 2 Briggs Minimal vs. Pruned Machinery for Placing φ -Functions entry v = v = Recall Dominators Briggs Minimal will add a = v = v – d dom i if all paths from entry to node i include d φ function because v is live d d dom i across the blue edge, but Pruned – d sdom i if d dom i and d ≠ i SSA will not because the φ = v function is dead i Dominance Frontiers – The dominance frontier of a node d is the set of nodes that are “just = φ (v 0 ,v 1 ) barely” not dominated by d; i.e., the set of nodes n, such that – d dominates a predecessor p of n, and – d does not strictly dominate n v = v = Neither Briggs Minimal nor = v = v – DF(d) = {n | ∃ p ∈ pred(n), d dom p and d !sdom n} Pruned SSA will place a φ function in this case because v is not live across any CFG edge Notational Convenience – DF(S) = ∪ s ∈ S DF(s) Why would we ever use Briggs Minimal instead of Pruned SSA? CS553 Lecture Static Single Assignment Form 3 CS553 Lecture Static Single Assignment Form 4 1
Dominance Frontier Example Dominance Frontier Example II DF(d) = {n | ∃ p ∈ pred(n), d dom p and d !sdom n} DF(d) = {n | ∃ p ∈ pred(n), d dom p and d !sdom n} Nodes in Dom(5) Nodes in Dom(5) {5, 6, 7, 8} {5, 6, 7, 8} Dom(5) = Dom(5) = 1 1 DF(5) = {4, 5, 12, 13} DF(5) = {4, 5, 13} 2 5 5 9 2 5 5 3 6 7 10 11 3 6 7 4 8 12 4 8 13 13 What’s significant about the Dominance Frontier? In this new graph, node 4 is the first point of convergence between the entry and node 5, so do we need a φ - function at node 13? In SSA form, definitions must dominate uses CS553 Lecture Static Single Assignment Form 5 CS553 Lecture Static Single Assignment Form 6 SSA Exercise Dominance Frontiers Revisited 1 Suppose that node 3 defines variable x x ∈ Def(3) 2 v :=... 7 DF(3) = {5} 3 1 v := ... v := ... 3 4 8 9 1 2 4 2 3 5 10 v 4 := φ (v 1 ,v 2 ) 5 v 5 := φ (v 3 ,v 4 ) 6 6 DF(8) = {10} Do we need to insert a φ - function for x anywhere else? DF(9) = {10} DF(2) = {6} DF(d) = {n | ∃ p ∈ pred(n), d dom p and d !sdom n} Yes. At node 6. Why? DF({8,9}) = {10} DF(10) = {6} DF({2,8,9,10}) = {6,10} CS553 Lecture Static Single Assignment Form 7 CS553 Lecture Static Single Assignment Form 8 2
Algorithm for Inserting φ -Functions Dominance Frontiers and SSA Let for each variable v WorkList ← ∅ – DF 1 (S) = DF(S) EverOnWorkList ← ∅ – DF i+1 (S) = DF(S ∪ DF i (S)) AlreadyHasPhiFunc ← ∅ for each node n containing an assignment to v Put all defs of v on the worklist Iterated Dominance Frontier WorkList ← WorkList ∪ {n} – DF ∞ (S) EverOnWorkList ← WorkList while WorkList ≠ ∅ Theorem Remove some node n for WorkList – If S is the set of CFG nodes that define variable v, then DF ∞ (S) is the set for each d ∈ DF(n) of nodes that require φ -functions for v if d ∉ AlreadyHasPhiFunc Insert at most one φ function per node Insert a φ -function for v at d AlreadyHasPhiFunc ← AlreadyHasPhiFunc ∪ {d} if d ∉ EverOnWorkList Process each node at most once WorkList ← WorkList ∪ {d} EverOnWorkList ← EverOnWorkList ∪ {d} CS553 Lecture Static Single Assignment Form 9 CS553 Lecture Static Single Assignment Form 10 Variable Renaming Dominance Tree Example Basic idea The dominance tree shows the dominance relation – When we see a variable on the LHS, create a new name for it – When we see a variable on the RHS, use appropriate subscript 1 1 Easy for straightline code x = x 0 = 2 5 5 9 2 4 5 13 9 12 = x = x 0 x = x 1 = 3 6 7 10 11 = x = x 1 3 6 8 7 10 11 4 8 12 Dominance Tree Use a stack when there’s control flow – For each use of x, find the definition of x that dominates it 13 x = x 0 = Traverse the dominance tree CFG = x = x 0 CS553 Lecture Static Single Assignment Form 11 CS553 Lecture Static Single Assignment Form 12 3
Variable Renaming (cont) Variable Renaming Algorithm procedure Rename(block b) Data Structures if b previously visited return Call Rename(entry-node) – Stacks[v] ∀ v for each φ -function p in b Holds the subscript of most recent definition of variable v, initially empty GenName(LHS(p)) and replace v with v i , where i=Top(Stack[v]) – Counters[v] ∀ v for each statement s in b (in order) for each variable v ∈ RHS(s) Holds the current number of assignments to variable v; initially 0 replace v by v i , where i = Top(Stacks[v]) for each variable v ∈ LHS(s) pop Auxiliary Routine push 1 GenName(v) and replace v with v i , where i=Top(Stack[v]) for each s ∈ succ(b) (in CFG) procedure GenName(variable v) j ← position in s’s φ -function corresponding to block b i := Counters[v] 2 4 5 13 9 12 for each φ -function p in s push i onto Stacks[v] Φ ( , , ) replace the j th operand of RHS(p) by v i , where i = Top(Stack[v]) Counters[v] := i + 1 for each s ∈ child(b) (in DT) Recurse using Depth First Search 3 6 8 7 10 11 Rename(s) for each φ -function or statement t in b Use the Dominance Tree to remember the most Unwind stack when done with this node for each v i ∈ LHS(t) recent definition of each variable Pop(Stack[v]) CS553 Lecture Static Single Assignment Form 13 CS553 Lecture Static Single Assignment Form 14 Transformation from SSA Form Proposal – Restore original variable names ( i.e ., drop subscripts) – Delete all φ -functions Complications x 0 = − What if versions get out of order? x 1 = (simultaneously live ranges) = x 0 = x 1 Alternative − Perform dead code elimination (to prune φ -functions) − Replace φ -functions with copies in predecessors − Rely on register allocation coalescing to remove unnecessary copies CS553 Lecture Static Single Assignment Form 15 4
Recommend
More recommend