Presentation is loading. Please wait.

Presentation is loading. Please wait.

Factored Use-Def Chains and Static Single Assignment Forms

Similar presentations


Presentation on theme: "Factored Use-Def Chains and Static Single Assignment Forms"— Presentation transcript:

1 Factored Use-Def Chains and Static Single Assignment Forms
Addresses use-def difficulty with optimization: Multiple defs of the same register Which def reaches use? Is RHS expression available? FUD/SSA forms: Each def of a variable is given a unique name All uses reached by that assignment are renamed DU chains become obvious FUD chains and SSA forms are similar r2 = r2 + r3 r6 = r4 - r5 r4 = 4 r6 = 8 r6 = r2 + r6 r7 = r4

2 Reaching Defs Keeping all reaching defs for each use is not space efficient FUD properties: Each use of a var is reached by a single def Insert pseudo assignments with  nodes where multiple defs meet r23 = r21?+r32? r67 = r45?-r56? r48 = 4 r69 = 8 r612 = r2103+r6117,9 r714 = r413?,8 r6117,9 use defs

3 Converting to FUD Form r23 = r21?+r32? r67 = r45?-r56?
(r4)10?,8 (r6)117,9 r614 = r2123+r61311 r716 = r41510

4 Converting to SSA Form r2 = r2 + r3 r6 = r4 - r5
r43 = (r41,r42) r63 = (r61,r62) r64 = r22 + r63 r71 = r43

5 Converting Loops to SSA Form
r1 = r … r12 = (r11,r13) r13 = r …

6 FUD/SSA Pros and Cons Pro Cons
Trivial to find what defs reach a use: each use has exactly one def Explicit merging of values at  nodes Simplifies optimizations that need use-def info Cons When transforming code, must either recompute (slow) or incrementally update (tedious)

7 Phi Nodes A  node merges two reaching definitions for a variable (or register) V A  node for V is required when two non-empty paths XZ and YZ converge at node Z, and nodes X and Y contain assignments to V More precisely: Z is a node with two distinct predecessors Y1 and Y2 p1 : X1* Y1 and p2 : X2* Y2 are two paths in the CFG p1 and p2 have no nodes in common And there are no defs of V along either p1 or p2 (except at X1 and X2)

8 FUD/SSA Construction Naïve algorithm Problem: too many  nodes
Insert  nodes at every join for every variable Solve reaching definitions Rename each use to the def that reaches it Problem: too many  nodes Precision Space Time

9 FUD Construction Step 1: -term placement using an algorithm that similar to SSA  insertion, but for FUD we also add a slicing edge Step 2: FUD chaining connects each variable use with its unique reaching definition, similar to SSA variable renaming

10 SSA Construction Step 1: Insert  nodes Step 2: Rename variables

11 Phi Node Insertion Algorithm
Compute dominance frontiers Find global names: Global if name is live on entry to some block For each name build a list of blocks that define it Insert  nodes (-term placement): For each global name N For each basic block B in which N is defined For each basic block D in B’s dominance frontier insert a  node for N in D add N to the list of defining basic blocks

12 Dominance Frontiers The dominance frontier of a node X is the set of nodes Z = DF(X) such that: If there are two predecessors Y1 and Y2 of Z and X dominates Y1 but not Y2 A node dominates itself A node X dominates a node Y if every path from entry to Y goes through X

13 Computing Dominance Frontiers
Algorithm Compute dominator tree For each join point X in the CFG For each predecessor of X in the CFG go up to the immediate dominator D of X in the dominator tree, adding X to DF(Y) for each Y up to D (excluding X and D)

14 Computing Dominance Frontiers (Example Dom. Tree)
BB0 BB Dominated by 1 0,1 2 0,1,2 3 0,1,3 4 0,1,3,4 5 0,1,3,5 6 0,1,3,6 7 0,1,7 BB0 BB1 BB1 BB2 BB3 BB2 BB3 BB7 BB4 BB5 BB4 BB5 BB6 Dominator tree BB6 BB7

15 Computing Dominance Frontiers (Example 1)
BB0 BB0 BB DF - 1 2 7 3 4 6 5 BB1 BB1 BB2 BB3 BB7 BB2 BB3 BB4 BB5 BB6 BB4 BB5 BB6 For each join point X in the CFG For each predecessor of X in the CFG go up to the immediate dominator D of X in the dominator tree, adding X to DF(Y) for each Y up to D (excluding X and D) Dominator frontiers BB7

16 Computing Dominance Frontiers (Example 2)
BB0 BB0 BB DF - 1 2 7 3 4 6 5 BB1 D = BB1 BB2 BB3 BB7 Y = BB2 BB3 BB4 BB5 BB6 BB4 BB5 BB6 For each join point X in the CFG For each predecessor of X in the CFG go up to the immediate dominator D of X in the dominator tree, adding X to DF(Y) for each Y up to D (excluding X and D) Dominator frontiers X = BB7

17 Computing Dominance Frontiers (Example 3)
BB0 BB0 BB DF - 1 2 7 3 4 6 5 BB1 D = BB1 BB2 BB3 BB7 BB2 Y = BB3 BB4 BB5 BB6 BB4 BB5 Y = BB6 For each join point X in the CFG For each predecessor of X in the CFG go up to the immediate dominator D of X in the dominator tree, adding X to DF(Y) for each Y up to D (excluding X and D) Dominator frontiers X = BB7

18 Computing Dominance Frontiers (Example 4)
BB0 BB0 BB DF - 1 2 7 3 4 6 5 BB1 BB1 BB2 BB3 BB7 BB2 D = BB3 BB4 BB5 BB6 Y = BB4 BB5 X = BB6 For each join point X in the CFG For each predecessor of X in the CFG go up to the immediate dominator D of X in the dominator tree, adding X to DF(Y) for each Y up to D (excluding X and D) Dominator frontiers BB7

19 Computing Dominance Frontiers (Example 5)
BB0 D = BB0 BB DF - 1 2 7 3 4 6 5 BB1 X = BB1 BB2 BB3 BB7 BB2 BB3 BB4 BB5 BB6 BB4 BB5 BB6 For each join point X in the CFG For each predecessor of X in the CFG go up to the immediate dominator D of X in the dominator tree, adding X to DF(Y) for each Y up to D (excluding X and D) Dominator frontiers Y = BB7

20 Phi Node Insertion Algorithm
Compute dominance frontiers Find global names: Global if name is live on entry to some block For each name build a list of blocks that define it Insert  nodes: For each global name N For each basic block B in which N is defined For each basic block D in B’s dominance frontier insert a  node for N in D add N to the list of defining basic blocks

21 Phi Node Insertion a is defined in 0, 1, 3:
add  in 7 then a is defined in 7: add  in 1 b is defined in 0, 2, 6: add  in 7 then b is defined in 7: add  in 1 c is defined in 0, 1, 2, 5: add  in 6, 7 then c is defined in 6, 7: add  in 1 d is defined in 2, 3, 4: add  in 6, 7 then d is defined in 6, 7: add  in 1 i is defined in 7: add  in 1 BB0 a = b = c = i = BB DF - 1 2 7 3 4 6 5 a = (a,a) b = (b,b) c = (c,c) d = (d,d) i = (i,i) BB1 a = c = BB2 BB3 b = c = d = a = d = BB4 BB5 d = c = a = (a,a) b = (b,b) c = (c,c) d = (d,d) c = (c,c) d = (d,d) BB6 b = For each global name N For each basic block B in which N is defined For each basic block D in B’s dominance frontier insert a  node for N in D add N to the list of defining basic blocks BB7 i =

22 SSA Construction Step 1: Insert  nodes  Step 2: Rename variables

23 Rename Variables Algorithm (outline)
Use an array of stacks, one per global variable V For each basic block B in a preorder traversal of the dominator tree: Generate unique names for each  node Rewrite each operation in the basic block: for uses of V: get current name from stack for defs of V: create and push new name Fill in  node parameters of successor blocks Recurse on B’s children in the dominator tree On exit from B: pop names generated in B from stacks

24 Renaming Variables: Initial State
BB0 a = b = c = i = Var: a b c d i Ctr: 1 Stk: a0 b0 c0 d0 i0 a = (a,a) b = (b,b) c = (c,c) d = (d,d) i = (i,i) BB1 a = c = BB2 BB3 b = c = d = a = d = BB0 BB4 BB5 d = c = BB1 a = (a,a) b = (b,b) c = (c,c) d = (d,d) c = (c,c) d = (d,d) BB6 BB2 BB3 BB7 b = BB4 BB5 BB6 BB7 i = Preorder traversal of dominator tree

25 Renaming Variables: After BB0
a0 = b0 = c0 = i0 = Var: a b c d i Ctr: 1 Stk: a0 b0 c0 d0 i0 a = (a0,a) b = (b0,b) c = (c0,c) d = (d0,d) i = (i0,i) BB1 a = c = BB2 BB3 b = c = d = a = d = BB4 BB5 d = c = a = (a,a) b = (b,b) c = (c,c) d = (d,d) c = (c,c) d = (d,d) BB6 b = BB7 i =

26 Renaming Variables: After BB1
a0 = b0 = c0 = i0 = Var: a b c d i Ctr: 3 2 Stk: a0 a1 a2 b0 b1 c0 c1 c2 d0 d1 i0 i1 a1 = (a0,a) b1 = (b0,b) c1 = (c0,c) d1 = (d0,d) i1 = (i0,i) BB1 a2 = c2 = BB2 BB3 b = c = d = a = d = BB4 BB5 d = c = a = (a,a) b = (b,b) c = (c,c) d = (d,d) c = (c,c) d = (d,d) BB6 b = BB7 i =

27 Renaming Variables: After BB2
a0 = b0 = c0 = i0 = Var: a b c d i Ctr: 3 4 2 Stk: a0 a1 a2 b0 b1 b2 c0 c1 c2 c3 d0 d1 d2 i0 i1 a1 = (a0,a) b1 = (b0,b) c1 = (c0,c) d1 = (d0,d) i1 = (i0,i) BB1 a2 = c2 = BB2 BB3 b2 = c3 = d2 = a = d = BB4 BB5 d = c = a = (a2,a) b = (b2,b) c = (c3,c) d = (d2,d) c = (c,c) d = (d,d) BB6 b = BB7 i =

28 Renaming Variables: Backtrack (Before BB3)
a0 = b0 = c0 = i0 = Var: a b c d i Ctr: 3 4 2 Stk: a0 a1 a2 b0 b1 c0 c1 c2 d0 d1 i0 i1 a1 = (a0,a) b1 = (b0,b) c1 = (c0,c) d1 = (d0,d) i1 = (i0,i) BB1 a2 = c2 = BB2 BB3 b2 = c3 = d2 = a = d = BB4 BB5 d = c = a = (a2,a) b = (b2,b) c = (c3,c) d = (d2,d) c = (c,c) d = (d,d) BB6 b = BB7 i =

29 Renaming Variables: After BB3
a0 = b0 = c0 = i0 = Var: a b c d i Ctr: 4 3 2 Stk: a0 a1 a2 a3 b0 b1 c0 c1 c2 d0 d1 d3 i0 i1 a1 = (a0,a) b1 = (b0,b) c1 = (c0,c) d1 = (d0,d) i1 = (i0,i) BB1 a2 = c2 = BB2 BB3 b2 = c3 = d2 = a3 = d3 = BB4 BB5 d = c = a = (a2,a) b = (b2,b) c = (c3,c) d = (d2,d) c = (c,c) d = (d,d) BB6 b = BB7 i =

30 Renaming Variables: After BB4
a0 = b0 = c0 = i0 = Var: a b c d i Ctr: 4 3 5 2 Stk: a0 a1 a2 a3 b0 b1 c0 c1 c2 d0 d1 d3 d4 i0 i1 a1 = (a0,a) b1 = (b0,b) c1 = (c0,c) d1 = (d0,d) i1 = (i0,i) BB1 a2 = c2 = BB2 BB3 b2 = c3 = d2 = a3 = d3 = BB4 BB5 d4 = c = a = (a2,a) b = (b2,b) c = (c3,c) d = (d2,d) c = (c2,c) d = (d4,d) BB6 b = BB7 i =

31 Renaming Variables: Backtrack to BB3 & After BB5
a0 = b0 = c0 = i0 = Var: a b c d i Ctr: 4 3 5 2 Stk: a0 a1 a2 a3 b0 b1 c0 c1 c2 c4 d0 d1 d3 i0 i1 a1 = (a0,a) b1 = (b0,b) c1 = (c0,c) d1 = (d0,d) i1 = (i0,i) BB1 a2 = c2 = BB2 BB3 b2 = c3 = d2 = a3 = d3 = BB4 BB5 d4 = c4 = a = (a2,a) b = (b2,b) c = (c3,c) d = (d2,d) c = (c2,c4) d = (d4,d3) BB6 b = BB7 i =

32 Renaming Variables: Backtrack to BB3 & After BB6
a0 = b0 = c0 = i0 = Var: a b c d i Ctr: 4 6 2 Stk: a0 a1 a2 b0 b1 b3 c0 c1 c2 c5 d0 d1 d5 i0 i1 a1 = (a0,a) b1 = (b0,b) c1 = (c0,c) d1 = (d0,d) i1 = (i0,i) BB1 a2 = c2 = BB2 BB3 b2 = c3 = d2 = a3 = d3 = BB4 BB5 d4 = c4 = a = (a2,a3) b = (b2,b3) c = (c3,c5) d = (d2,d5) c5 = (c2,c4) d5 = (d4,d3) BB6 b3 = BB7 i =

33 Renaming Variables: Backtrack to BB1 & After BB7
a0 = b0 = c0 = i0 = Var: a b c d i Ctr: 5 7 3 Stk: a0 a1 a2 a4 b0 b1 b4 c0 c1 c2 c6 d0 d1 d6 i0 i1 i2 a1 = (a0,a2) b1 = (b0,b4) c1 = (c0,c6) d1 = (d0,d6) i1 = (i0,i2) BB1 a2 = c2 = BB2 BB3 b2 = c3 = d2 = a3 = d3 = BB4 BB5 d4 = c4 = a4 = (a2,a3) b4 = (b2,b3) c6 = (c3,c5) d6 = (d2,d5) c5 = (c2,c4) d5 = (d4,d3) BB6 b3 = BB7 i2 =

34 Homework: Convert to SSA Form
Tasks: Compute dominator tree Compute dominance frontiers Find global names Insert  nodes Rename variables BB0 a = b = BB1 BB2 BB3 c = b = a = BB4 b = BB5 a = c =


Download ppt "Factored Use-Def Chains and Static Single Assignment Forms"

Similar presentations


Ads by Google