factored use-def chains and static single assignment forms
DESCRIPTION
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 - PowerPoint PPT PresentationTRANSCRIPT
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 + r3r6 = r4 - r5
r6 = r2 + r6r7 = r4
r4 = 4r6 = 8
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?
r612 = r2103+r6117,9
r714 = r413?,8
r48 = 4r69 = 8
r6117,9
use defs
3
Converting to FUD Form
r23 = r21?+r32?
r67 = r45?-r56?
r612 = r2103+r6117,9
r714 = r413?,8
r48 = 4r69 = 8
r23 = r21?+r32?
r67 = r45?-r56?
(r4)10?,8
(r6)117,9
r614 = r2123+r61311
r716 = r41510
r48 = 4r69 = 8
4
Converting to SSA Form
r2 = r2 + r3r6 = r4 - r5
r6 = r2 + r6r7 = r4
r4 = 4r6 = 8
r22 = r21 + r31
r61 = r41 - r51
r43 = (r41,r42)r63 = (r61,r62)r64 = r22 + r63
r71 = r43
r42 = 4r62 = 8
5
Converting Loops to SSA Form
r1 = r1 + 1
…
r1 = 0
r12 = (r11,r13)r13 = r12 + 1…
r11 = 0
6
FUD/SSA Pros and Cons
Pro 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 XZ and YZ 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 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 nodesZ = 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
Algorithm1. Compute dominator tree2. 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
BB1
BB3BB2
BB4 BB5
BB6
BB7
BB0
BB1
BB2 BB7
BB4 BB6BB5
BB3
Dominator tree
BB Dominatedby
0 0
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
15
Computing Dominance Frontiers (Example 1)
BB0
BB1
BB3BB2
BB4 BB5
BB6
BB7
BB0
BB1
BB2 BB7
BB4 BB6BB5
BB3
BB DF
0 -
1 -
2 7
3 7
4 6
5 6
6 7
7 1
Dominatorfrontiers
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)
16
Computing Dominance Frontiers (Example 2)
BB0
D = BB1
BB3Y = BB2
BB4 BB5
BB6
X = BB7
BB0
BB1
BB2 BB7
BB4 BB6BB5
BB3
BB DF
0 -
1 -
2 7
3 7
4 6
5 6
6 7
7 1
Dominatorfrontiers
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)
17
Computing Dominance Frontiers (Example 3)
BB0
D = BB1
Y = BB3
BB4 BB5
Y = BB6
X = BB7
BB0
BB1
BB2 BB7
BB4 BB6BB5
BB3
BB DF
0 -
1 -
2 7
3 7
4 6
5 6
6 7
7 1
Dominatorfrontiers
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)
BB2
18
Computing Dominance Frontiers (Example 4)
BB0
D = BB3
Y = BB4 BB5
X = BB6
BB0
BB1
BB2 BB7
BB4 BB6BB5
BB3
BB DF
0 -
1 -
2 7
3 7
4 6
5 6
6 7
7 1
Dominatorfrontiers
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)
BB1
BB2
BB7
19
Computing Dominance Frontiers (Example 5)
D = BB0
BB5
BB0
BB1
BB2 BB7
BB4 BB6BB5
BB3
BB DF
0 -
1 -
2 7
3 7
4 6
5 6
6 7
7 1
Dominatorfrontiers
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)
X = BB1
BB2
Y = BB7
BB3
BB4
BB6
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 = b =c =i =
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
BB DF
0 -
1 -
2 7
3 7
4 6
5 6
6 7
7 1
BB0
a = c = BB1
a = d =
b =c = d =
d = c =
b =
i =
BB2 BB3
BB5BB4
BB6
BB7
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
a = (a,a)b = (b,b)c = (c,c)d = (d,d)i = (i,i)
c = (c,c)d = (d,d)
a = (a,a)b = (b,b)c = (c,c)d = (d,d)
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 stackfor 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
a = b =c =i =
BB0
a = c = BB1
a = d =
b =c = d =
d = c =
b =
i =
BB2 BB3
BB5BB4
BB6
BB7
a = (a,a)b = (b,b)c = (c,c)d = (d,d)i = (i,i)
c = (c,c)d = (d,d)
a = (a,a)b = (b,b)c = (c,c)d = (d,d)
Var: a b c d i
Ctr: 1 1 1 1 1
Stk: a0 b0 c0 d0 i0
BB0
BB1
BB2 BB7
BB4 BB6BB5
BB3
Preorder traversalof dominator tree
25
Renaming Variables:After BB0
a0 = b0 =c0 =i0 =
BB0
a = c = BB1
a = d =
b =c = d =
d = c =
b =
i =
BB2 BB3
BB5BB4
BB6
BB7
a = (a0,a)b = (b0,b)c = (c0,c)d = (d0,d)i = (i0,i)
c = (c,c)d = (d,d)
a = (a,a)b = (b,b)c = (c,c)d = (d,d)
Var: a b c d i
Ctr: 1 1 1 1 1
Stk: a0 b0 c0 d0 i0
26
Renaming Variables:After BB1
a0 = b0 =c0 =i0 =
BB0
a2 = c2 =
BB1
a = d =
b =c = d =
d = c =
b =
i =
BB2 BB3
BB5BB4
BB6
BB7
a1 = (a0,a)b1 = (b0,b)c1 = (c0,c)d1 = (d0,d)i1 = (i0,i)
c = (c,c)d = (d,d)
a = (a,a)b = (b,b)c = (c,c)d = (d,d)
Var: a b c d i
Ctr: 3 2 3 2 2
Stk: a0
a1
a2
b0
b1
c0
c1
c2
d0
d1
i0
i1
27
Renaming Variables:After BB2
a0 = b0 =c0 =i0 =
BB0
a2 = c2 =
BB1
a = d =
b2 =c3 = d2 =
d = c =
b =
i =
BB2 BB3
BB5BB4
BB6
BB7
a1 = (a0,a)b1 = (b0,b)c1 = (c0,c)d1 = (d0,d)i1 = (i0,i)
c = (c,c)d = (d,d)
a = (a2,a)b = (b2,b)c = (c3,c)d = (d2,d)
Var: a b c d i
Ctr: 3 3 4 3 2
Stk: a0
a1
a2
b0
b1
b2
c0
c1
c2
c3
d0
d1
d2
i0
i1
28
Renaming Variables:Backtrack (Before BB3)
a0 = b0 =c0 =i0 =
BB0
a2 = c2 =
BB1
a = d =
b2 =c3 = d2 =
d = c =
b =
i =
BB2 BB3
BB5BB4
BB6
BB7
a1 = (a0,a)b1 = (b0,b)c1 = (c0,c)d1 = (d0,d)i1 = (i0,i)
c = (c,c)d = (d,d)
a = (a2,a)b = (b2,b)c = (c3,c)d = (d2,d)
Var: a b c d i
Ctr: 3 3 4 3 2
Stk: a0
a1
a2
b0
b1
c0
c1
c2
d0
d1
i0
i1
29
Renaming Variables:After BB3
a0 = b0 =c0 =i0 =
BB0
a2 = c2 =
BB1
a3 = d3 =
b2 =c3 = d2 =
d = c =
b =
i =
BB2 BB3
BB5BB4
BB6
BB7
a1 = (a0,a)b1 = (b0,b)c1 = (c0,c)d1 = (d0,d)i1 = (i0,i)
c = (c,c)d = (d,d)
a = (a2,a)b = (b2,b)c = (c3,c)d = (d2,d)
Var: a b c d i
Ctr: 4 3 4 4 2
Stk: a0
a1
a2
a3
b0
b1
c0
c1
c2
d0
d1
d3
i0
i1
30
Renaming Variables:After BB4
a0 = b0 =c0 =i0 =
BB0
a2 = c2 =
BB1
a3 = d3 =
b2 =c3 = d2 =
d4 = c =
b =
i =
BB2 BB3
BB5BB4
BB6
BB7
a1 = (a0,a)b1 = (b0,b)c1 = (c0,c)d1 = (d0,d)i1 = (i0,i)
c = (c2,c)d = (d4,d)
a = (a2,a)b = (b2,b)c = (c3,c)d = (d2,d)
Var: a b c d i
Ctr: 4 3 4 5 2
Stk: a0
a1
a2
a3
b0
b1
c0
c1
c2
d0
d1
d3
d4
i0
i1
31
Renaming Variables:Backtrack to BB3 & After BB5
a0 = b0 =c0 =i0 =
BB0
a2 = c2 =
BB1
a3 = d3 =
b2 =c3 = d2 =
d4 = c4 =
b =
i =
BB2 BB3
BB5BB4
BB6
BB7
a1 = (a0,a)b1 = (b0,b)c1 = (c0,c)d1 = (d0,d)i1 = (i0,i)
c = (c2,c4)d = (d4,d3)
a = (a2,a)b = (b2,b)c = (c3,c)d = (d2,d)
Var: a b c d i
Ctr: 4 3 5 5 2
Stk: a0
a1
a2
a3
b0
b1
c0
c1
c2
c4
d0
d1
d3
i0
i1
32
Renaming Variables:Backtrack to BB3 & After BB6
a0 = b0 =c0 =i0 =
BB0
a2 = c2 =
BB1
a3 = d3 =
b2 =c3 = d2 =
d4 = c4 =
b3 =
i =
BB2 BB3
BB5BB4
BB6
BB7
a1 = (a0,a)b1 = (b0,b)c1 = (c0,c)d1 = (d0,d)i1 = (i0,i)
c5 = (c2,c4)d5 = (d4,d3)
a = (a2,a3)b = (b2,b3)c = (c3,c5)d = (d2,d5)
Var: a b c d i
Ctr: 4 4 6 6 2
Stk: a0
a1
a2
b0
b1
b3
c0
c1
c2
c5
d0
d1
d5
i0
i1
33
Renaming Variables:Backtrack to BB1 & After BB7
a0 = b0 =c0 =i0 =
BB0
a2 = c2 =
BB1
a3 = d3 =
b2 =c3 = d2 =
d4 = c4 =
b3 =
i2 =
BB2 BB3
BB5BB4
BB6
BB7
a1 = (a0,a2)b1 = (b0,b4)c1 = (c0,c6)d1 = (d0,d6)i1 = (i0,i2)
c5 = (c2,c4)d5 = (d4,d3)
a4 = (a2,a3)b4 = (b2,b3)c6 = (c3,c5)d6 = (d2,d5)
Var: a b c d i
Ctr: 5 5 7 7 3
Stk: a0
a1
a2
a4
b0
b1
b4
c0
c1
c2
c6
d0
d1
d6
i0
i1
i2
34
Homework:Convert to SSA Form
a = b =
BB1
b = a =
c =
b =
a = c =
BB0
BB2 BB3
BB4
BB5
Tasks:1. Compute dominator tree2. Compute dominance
frontiers3. Find global names4. Insert nodes5. Rename variables