420 likes | 698 Views
Advanced Compiler Design – Assignment 1. SSA Construction & Destruction. Michael Fäs michael.faes@inf.ethz.ch (Original Slides by Luca Della Toffola ). SSA Pipeline. Build the control-flow graph of the program Convert the program into SSA form Apply optimizations
E N D
Advanced Compiler Design – Assignment 1 SSA Construction & Destruction Michael Fäs michael.faes@inf.ethz.ch (Original Slides by Luca Della Toffola)
SSA Pipeline • Build the control-flow graph of the program • ConverttheprogramintoSSA form • Apply optimizations • Convert the program back to non-SSA form • Generate code from the control-flow graph
Assignment 1 Tasks • Build the control-flow graph of the program • ConverttheprogramintoSSA form • Apply optimizations • Convert the program back to non-SSA form • Generate code from the control-flow graph
SSA Construction • Compute Dominance Frontier • Using Dominator Tree • Insert ɸ-operations where necessary • In join nodes with different incoming assignments • Use the dominance frontier • Add versions to variables (renaming)
SSA Destruction • Replace ɸ-operations with copy statements in predecessors
Assignment 1 Dominance Relations Review
Dominator A node ydominatesn if ylies in every path from the root n0 to n. • Dom(n) = the set of all dominators of n • Note: • Each node dominates itself • The root node dominates all other nodes
root A B C D E F G H
Strict Dominator A node ystrictly dominates n if ydominates nand n ≠ y. • SDom(n) = the set of strictdominators of n • Note: • SDom(n) = Dom(n) \ {n}
root A B C D E F G H
Immediate Dominator Node yimmediately dominates n if yis the closest strict dominator of non any path from n0to n. • IDom(n) = the immediate dominator of n • Note: • Every node (except the root)has exactly one immediate dominator
root A B C D E F G H
Dominator Tree • Contains all nodes of the CFG with edges that reflectthe dominance relation • Each node n is a child of its IDom(n) • Bottom-up traversal results in Dom(n)
CFG Dominator Tree A A B B D H C D E C E G F G F H
Assignment 1 – SSA Construction Dominator Tree Construction
Iterative Dominator Algorithm Idea: Iteratively solve the following equations: • Implementation notes: • Initialize Dom(n) with all nodes, then remove • Walk over the CFG, repeat until no changes.
Dominator Algorithm: Example {A} A Dom(root) = {root} forninnodes \{root}: Dom(n) = nodes {A,B,C,D,E} {A,C} C {A,B,C,D,E} {A,B} whilechanges: forninnodes: pre = intersect(Dom(preds(n))) new = union(pre, n) Dom(n) = new B {A,B,C,D,E} {A,C,D} D E {A,B,C,D,E} {A,E}
Computing Dominators • Shown algorithm • Simple but not very efficient • How to get IDom(n) from Dom(n)? • Efficientalgorithm described in A Simple, Fast Dominance Algorithm by Cooper et al. • Same idea, still very simple • Not required for getting full marks
Assignment 1 – SSA Construction Dominance Frontier Construction
Dominance Frontier DF(n) = all nodes ysuch that ndominates a predecessor of ybut does not strictly dominatey. Intuition: The Dominance Frontier of n is where n’s dominance “ends”.
Dominance Frontier: Intuition A H B C F D I E G J
Dominance Frontier: Example DF(A) = {} A DF(C) = {C, D} B C E DF(B) = {D} DF(E) = {C} D DF(D) = {} n dominates a predecessor of yand nnotstrictly dominatesy
Computing the DF • Idea: • Given node n, do not compute DF(n) directly • Instead, find nodes y for which n is in DF(y) • Simple algorithm based on two observations
Computing the DF: Observation 1 Predecessors of a node nhave nin their DF unlesstheydominaten. A 1 I B 2 C D 3 II
Computing the DF: Observation 2 Dominators of predecessors of a node nhave nin their DF unless they dominate n. A 1 B D 2 … … … C E 3 F 4
Dominance Frontier CFG Dominator Tree forninnodes: if |preds(n)| > 1: forpinpreds(n): r = p whiler != Idom(n): DF(r) += n r = Idom(r) A A C E B C B D D E DF(A) = {} DF(B) = {D} DF(C) = {C,D} DF(E) = {C} DF(D) = {} n = C DF(E) = {C} DF(C) = {C,D} n = D DF(B) = {D} DF(C) = {D}
Assignment 1 – SSA Construction ɸ-Function insertion & Variable Versioning
ɸ-Function Insertion BB1 x= 0 • ɸ-functions need to be inserted in all join nodes with different incoming assignments • ɸ-operations are assignments too! BB2 x = 1 Assignments require a ɸ-function to be inserted in the DF nodes of the block containing the assignment BB3 write(x) BB4 x = ɸ(x,x) write(x);
DF and ɸ-Functions: Example x = 1 A H B x = 4 write(x) C F D I E x = ɸ(x,x) x = ɸ(x,x) write(x) G J x = ɸ(x,x,x) write(x)
Variable Versioning • Need to add versions to all variables • S.t. each version is assigned only once (statically) • Make sure that always the “newest” version of a variable is used • Traverse the CFG, keep track of current and highest variableversions • Pro tip: Dominator tree... • Replace all uses of unversioned variables • Also in ɸ-functions!
Variable Versioning: Example BB1 x= 0 1 BB2 x = 1 BB3 write(x) 2 1 BB4 x= ɸ(x,x) BB5 x = x + 1 4 1 3 3 4 BB6 x = ɸ(x,x) write(x); 2 5 3 5
Assignment 1 SSA Destruction
SSA Destruction • Replace ɸ-operations with copy statements in predecessors
Removing ɸ-Functions: Example 1 BB1 x1 = 0 BB2 x2 = 1 x3 = x2 BB3 write(x1) x3 = x1 BB4 x3 = ɸ(x2,x1) write(x3)
Removing ɸ-Functions: Example 2 • Processmayinsertredundantassignments: x3 = x1... x3 = x2 • Solution: Insert empty blocks where necessary BB1 x1 = 0 x3 = x1 BB2 x2 = 1 x3 = x2 BB3 x3 = ɸ(x2,x1) write(x3)
Removing ɸ-Functions: Example 2 • Processmayinsertredundantassignments: x3 = x1... x3 = x2 • Solution: Insert empty blocks where necessary • Already done in CFG construction phase in A1 fragment BB1 x1 = 0 BB2 x2 = 1 x3 = x2 BB4 x3= x1 BB3 x3 = ɸ(x2,x1) write(x3)
Assignment 1 PracticalTips(Framework)
Framework: Dominator Tree • Computation of Idom(n) and DF(n) in cd.cfg.Dominator class • Dominator tree edges directly in BasicBlock: cd.ir.BasicBlock dominatorTreeParent: BB dominatorTreeChildren:List<BB> dominanceFrontier: Set<BB> Idom(n)
Framework: Variable Versions • VariableSymbol has new field: version • Newconstructorto create a new version:VariableSymbol(VariableSymbolv0sym, int version)
Framework: ɸ-Operations • ɸ-operations represented by class cd.ir.Phi write(x) cd.ir.Phi v0sym: VariableSymbol lhs: VariableSymbol rhs: List<Expr> x3 = ɸ(x1,x2) write(x3)
Framework: ɸ-Operations • BasicBlockcontains all attached ɸ-ops • Map key is original variable (version 0) • Add the new symbols to the method locals cd.ir.BasicBlock phis: Map<VS,Phi>
? Questions