Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced Compiler Design – Assignment 1 SSA Construction & Destruction Michael Fäs (Original Slides by Luca Della Toffola)

Similar presentations


Presentation on theme: "Advanced Compiler Design – Assignment 1 SSA Construction & Destruction Michael Fäs (Original Slides by Luca Della Toffola)"— 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 1.Build the control-flow graph of the program 2.Convert the program into SSA form 3.Apply optimizations 4.Convert the program back to non-SSA form 5.Generate code from the control-flow graph

3 Assignment 1 Tasks 1.Build the control-flow graph of the program 2.Convert the program into SSA form 3.Apply optimizations 4.Convert the program back to non-SSA form 5.Generate code from the control-flow graph (Due date: Mar 18, 2015)

4 SSA Construction 1.Compute Dominance Frontier Using Dominator Tree 2.Insert ɸ-operations where necessary In join nodes with different incoming assignments Use dominance frontier 3.Add versions to variables (renaming)

5 SSA Destruction 1.Replace ɸ-operations with copy statements in predecessor blocks

6 DOMINANCE RELATIONS REVIEW Assignment 1

7 Dominator A (control flow graph) node y dominates n if y lies in every path from the root n 0 to n. D OM ( n ) = the set of all dominators of n Note: Every node dominates itself The root node dominates all nodes

8 n D OM ( n ) A{root, A} B{root, B} C{root, A, C} D{root, D} E{root, B, E} F{root, A, C, F} G{root, D, G} H{root, H} A D B EC GF H root

9 SD OM ( n ) = the set of strict dominators of n Note: SD OM ( n ) = D OM ( n ) \ { n } Strict Dominator A node y strictly dominates n if y dominates n and n ≠ y.

10 A D B EC GF H root n SD OM ( n ) A{root} B C{root, A} D{root} E{root, B} F{root, A, C} G{root, D} H{root}

11 ID OM ( n ) = the immediate dominator of n Note: Every node (except the root) has exactly one immediate dominator Immediate Dominator Node y immediately dominates n if y is the closest strict dominator of n on any path from n 0 to n.

12 A D B EC GF H root n ID OM ( n ) A{root} B C{A} D{root} E{B} F{C} G{D} H{root}

13 Dominator Tree Contains all nodes of the CFG with edges that reflect the dominance relation Each node n is a child of its ID OM ( n ) Bottom-up traversal results in D OM ( n )

14 A D B EC GF H CFG A DB ECG F H Dominator Tree

15 DOMINATOR TREE CONSTRUCTION Assignment 1 – SSA Construction

16 Iterative Dominator Algorithm Idea: Solve the following data-flow problem: For n  n 0, initialize D OM ( n ) with all nodes Update D OM ( n ) according to equations Walk over the CFG, repeat until no changes.

17 {A,C,D} {A,E} {A,B} {A,C} A C B D E D OM ( root ) = { root } for n in nodes \ { root }: D OM ( n ) = nodes while changed : for n in nodes : pre = intersect(D OM ( preds ( n ))) new = union( pre, n ) D OM ( n ) = new Dominator Algorithm: Example {A} {A,B,C,D,E}

18 Computing Dominators Shown algorithm – Simple but not very efficient – How to get ID OM ( n ) from D OM ( n ) ? Efficient algorithm described in A Simple, Fast Dominance Algorithm by Cooper et al. – Same idea, still very simple – Not required for getting full marks – Also covers shown algorithm

19 DOMINANCE FRONTIER CONSTRUCTION Assignment 1 – SSA Construction

20 Dominance Frontier DF( n ) = all nodes y such that n dominates a predecessor of y but does not strictly dominate y. Intuition: The Dominance Frontier of n is where n ’s dominance “ends”. in the CFG

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

22 Dominance Frontier: Example A CE D B DF(A) = {} DF(B) = {D} DF(D) = {} DF(E) = {C} DF(C) = {C, D} n dominates a predecessor of y and n not strictly dominates y

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 n have n in their DF unless they dominate n. A C D B 1 3 2 I II

25 Computing the DF: Observation 2 Dominators of predecessors of a node n have n in their DF unless they dominate n. A E F C B D … … 1 4 3 2 …

26 for n in nodes : for p in preds ( n ): r = p while r != I DOM ( n ): DF( r ) += n r = I DOM ( r ) A C E D B A D C E B Dominator Tree CFG n = D DF(B) = {D} DF(C) = {D} n = C DF(E) = {C} DF(C) = {C,D} DF(A)= {} DF(B)= {D} DF(C)= {C,D} DF(E)= {C} DF(D)= {} Dominance Frontier

27 ɸ-FUNCTION INSERTION & VARIABLE VERSIONING Assignment 1 – SSA Construction

28 ɸ-Function Insertion ɸ-functions need to be inserted in all join nodes with different incoming assignments ɸ-operations are assignments too! Assignments require a ɸ-function to be inserted in the DF nodes of the block containing the assignment BB4 x = ɸ(x,x) write(x); BB1 x = 0 BB2 x = 1 BB3 write(x)

29 DF and ɸ-Functions: Example H A B F C D J I G E x = 1 x = 4 x = ɸ(x,x) write(x) x = ɸ(x,x,x) write(x)

30 BB4 x = ɸ(x,x) write(x); ɸ-Function Insertion: Example BB1 x = 0 COND: y==0 BB2 x = 1 BB3 write(x) truefalse x = 0; if(y == 0) { x = 1; } else { write(x); } write(x);

31 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 (newest) and highest variable versions Pro tip: Dominator tree... – Replace all uses of unversioned variables Also in ɸ-functions!

32 Variable Versioning: Example H A B F C D J I G E x = 1 x = 4 x = ɸ(x,x ) write(x ) x = ɸ(x,x,x ) write(x ) 1 1 312 2 412 4 52 3 4 5

33 Variable Versioning: Example BB6 x = ɸ(x,x ) write(x ); BB1 x = 0 BB2 x = 1 BB3 write(x ) BB5 x = x + 1 BB4 x = ɸ(x,x ) 1 1 1 2 2 3 3 4 4 35 5

34 SSA DESTRUCTION Assignment 1

35 SSA Destruction Replace ɸ-operations with copy statements in predecessors

36 Removing ɸ-Functions: Example 1 BB1 x 1 = 0 BB2 x 2 = 1 x 3 = x 2 BB3 write(x 1 ) x 3 = x 1 BB4 x 3 = ɸ(x 2,x 1 ) write(x 3 )

37 Removing ɸ-Functions: Example 2 BB1 x 1 = 0 x 3 = x 1 BB2 x 2 = 1 x 3 = x 2 BB3 x 3 = ɸ(x 2,x 1 ) write(x 3 ) Process may insert redundant assignments: x 3 = x 1... x 3 = x 2 Solution: Insert empty blocks where necessary

38 Removing ɸ-Functions: Example 2 BB1 x 1 = 0 BB2 x 2 = 1 x 3 = x 2 BB3 x 3 = ɸ(x 2,x 1 ) write(x 3 ) Process may insert redundant assignments: x 3 = x 1... x 3 = x 2 Solution: Insert empty blocks where necessary – Already done in CFG construction phase in A1 fragment BB4 x 3 = x 1

39 PRACTICAL TIPS (FRAMEWORK) Assignment 1

40 Framework: Dominator Tree Computation of I DOM ( n ) and DF( n ) in cd.cfg.Dominator class Dominator tree edges directly in BasicBlock : dominatorTreeParent: BB dominatorTreeChildren: List dominanceFrontier: Set cd.ir.BasicBlock I DOM ( n )

41 Framework: Variable Versions VariableSymbol has new field: version New constructor to create a new version: VariableSymbol(VariableSymbol v0sym, int version)

42 Framework: ɸ-Operations ɸ-operations represented by class cd.ir.Phi write(x) x 3 = ɸ(x 1,x 2 ) write(x 3 ) v0sym: VariableSymbol lhs: VariableSymbol rhs: List cd.ir.Phi

43 Framework: ɸ-Operations BasicBlock contains all attached ɸ-ops Map key is original variable (version 0) Add the new symbols to the method locals phis: Map cd.ir.BasicBlock

44 ? Questions


Download ppt "Advanced Compiler Design – Assignment 1 SSA Construction & Destruction Michael Fäs (Original Slides by Luca Della Toffola)"

Similar presentations


Ads by Google