1 / 42

Advanced Compiler Design – Assignment 1

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

shawna
Télécharger la présentation

Advanced Compiler Design – Assignment 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Advanced Compiler Design – Assignment 1 SSA Construction & Destruction Michael Fäs michael.faes@inf.ethz.ch (Original Slides by Luca Della Toffola)

  2. 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

  3. 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

  4. 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)

  5. SSA Destruction • Replace ɸ-operations with copy statements in predecessors

  6. Assignment 1 Dominance Relations Review

  7. 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

  8. root A B C D E F G H

  9. 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}

  10. root A B C D E F G H

  11. 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

  12. root A B C D E F G H

  13. 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)

  14. CFG Dominator Tree A A B B D H C D E C E G F G F H

  15. Assignment 1 – SSA Construction Dominator Tree Construction

  16. 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.

  17. 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}

  18. 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

  19. Assignment 1 – SSA Construction Dominance Frontier Construction

  20. 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”.

  21. Dominance Frontier: Intuition A H B C F D I E G J

  22. 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

  23. 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

  24. Computing the DF: Observation 1 Predecessors of a node nhave nin their DF unlesstheydominaten. A 1 I B 2 C D 3 II

  25. 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

  26. 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}

  27. Assignment 1 – SSA Construction ɸ-Function insertion & Variable Versioning

  28. ɸ-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);

  29. 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)

  30. 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!

  31. 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

  32. Assignment 1 SSA Destruction

  33. SSA Destruction • Replace ɸ-operations with copy statements in predecessors

  34. Removing ɸ-Functions: Example 1 BB1 x1 = 0 BB2 x2 = 1 x3 = x2 BB3 write(x1) x3 = x1 BB4 x3 = ɸ(x2,x1) write(x3)

  35. 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)

  36. 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)

  37. Assignment 1 PracticalTips(Framework)

  38. 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)

  39. Framework: Variable Versions • VariableSymbol has new field: version • Newconstructorto create a new version:VariableSymbol(VariableSymbolv0sym, int version)

  40. 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)

  41. 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>

  42. ? Questions

More Related