CS293S SSA & Dead Code Elimination Yufei Ding
Review of Last Class � Two other flow analysis (DFA) � Constant Propagation � Reaching definitions (def-use chain) � Static Single Assignment(SSA) � Maximal SSA (all variables in every joint block) � Minimal SSA (a def in block n results in an insertion in each of its DF(n)) � Semi-pruned SSA (similar as minimal SSA, but only focus on global variable definitions)
Maximal SSA
Dominance Frontiers Dominance Frontiers B 0 • DF(n ) is fringe just beyond the region n dominates B 1 • m Î DF(n) : iff n Ï (Dom(m) - {m}) but n Î D OM (p) for some p Î preds(m). i.e., n dominates m’s B 2 B 3 i.e., n doesn’t strictly some parent dominate m B 4 B 5 B 6 B 7 A node can be the dominance frontier of itself. Dominance frontier * e.g., for node n = 1. It dominates its own parent, of 3={7} node 7, but does not directly dominate itself. * it often indicates that there is a back edge. 4
Computing Dominance Frontiers • Only join points are in DF(n) for some n • Leads to a simple, intuitive algorithm for computing B 0 dominance frontiers B 0 For each join point x B 1 B 1 For each CFG predecessor of x (from the CFG) Walk up to ID OM (x ) in the dominator tree, adding x to DF(n) for each n in the walk except IDOM(x). B 2 B 3 B 2 B 3 B 4 B 5 B 4 B 5 B 6 B 6 • What if there is another back edge from B 7 to B 0 ? B 7 B 7 Control flow Dominance graph (CFG) Tree 5
Minimal SSA: Ø-functions insertion with DF for each variable x in the CFG work list = get all nodes (basic blocks) in which x is defined B 0 for each node n in work list for each node m in DF(n) x ¬ Ø(...) B 1 if (there is no ϕ - function for x in m) insert a ϕ - function for x in m B 2 B 3 work list = work list ∪ { m } x ¬ ... B 4 B 5 x ¬ Ø(...) B 6 • DF(4) is {6}, so ¬ in 4 forces Ø-function in 6 x ¬ Ø(...) • ¬ in 6 forces Ø-function in DF(6) = {7} B 7 • ¬ in 7 forces Ø-function in DF(7) = {1} • ¬ in 1 forces Ø-function in DF(1) = Ø (halt ) 6
Focus of This Class � Static Single Assignment(SSA) � Semi-pruned SSA � (similar as minimal SSA, but on only global variable) � Pruned SSA � (similar as semi-pruned SSA, but dead are removed) � Techniques for Removing ϕ -functions � Dead code elimiation
Semi-pruned SSA � Observation: a variable that is only live in a single node can never have a live Ø-function. � Therefore, the minimal technique can be further refined by first computing the set of global names – defined as the names that are live across more than one node – and producing Ø-functions for these names only.
Step 1: Global Variables a ¬ ••• b ¬ ••• Globals = ⋃ "## $ %$ &'( • UEVar(n) c ¬ ••• d ¬ ••• UEVar(n): Upper exposed variables in block n, i.e., i ¬ ••• i > 100 B 0 variables used before it is redefined in block n. (We have learned this in liveness analysis. ) B 1 a ¬ ••• c ¬ ••• • Example UEVAR (B7) = {a, b, c, d, i}; B 3 B 2 a ¬ ••• b ¬ ••• all others are empty set; d ¬ ••• c ¬ ••• d ¬ ••• Globals = {a, b, c, d, i}, (y, z are local names) B 4 B 5 d ¬ ••• c ¬ ••• • Get blocks where each of these Globals get b ¬ ••• defined B 6 Names a b c d i B 7 blocks 0,1,3 0,2,6 0,1,2,5 0,2,3,4 0,7 y ¬ a+b z ¬ c+d i ¬ i+1 i > 100
Phase 2: inserting ϕ -functions a ¬ ••• for each of the global name x b ¬ ••• c ¬ ••• work list = get all nodes in which x is defined d ¬ ••• i ¬ ••• for each node n in work list i > 100 B 0 for each node m in DF(n) Excluding local if (there is no ϕ - function for x in m) B 1 a ¬ ••• names avoids insert a ϕ - function for x to m c ¬ ••• Ø’s for y & z work list = work list ∪ { m } B 3 B 2 a ¬ ••• b ¬ ••• d ¬ ••• c ¬ ••• d ¬ ••• B 4 B 5 d ¬ ••• c ¬ ••• b ¬ ••• Names a b c d i B 6 blocks 0,1,3 0,2,6 0,1,2,5 0,2,3,4 0,7 B 7 y ¬ a+b z ¬ c+d i ¬ i+1 Block 0 1 2 3 4 5 6 7 i > 100 DF - 1 7 7 6 6 7 1
Phase 3: renaming variables Renaming is done by a pre-order traversal of the dominator tree, as follows: for each node b in the dominator tree 1. rename definitions and uses of variables in b 2. rename ϕ -functions parameters corresponding to b in all successors of n in the CFG. Rename(b) for each Ø-function in b, x ¬ Ø(…) rename x as NewName(x) for each operation “x ¬ y op z” in b One possible Implementation via a rewrite y as top(stack[y]) set of stacks and counters. rewrite z as top(stack[z]) 1. Get the root node n 0 of the rewrite x as NewName(x) CFG for each successor of b in the CFG 2. Call Rename(n 0 ) rewrite appropriate Ø parameters for each successor s of b in dom. tree Rename(s) for each operation “x ¬ y op z” in b pop(stack[x])
Example a ¬ ••• b ¬ ••• c ¬ ••• d ¬ ••• B 0 i ¬ ••• B 0 i > 100 B 1 B 1 a ¬ Ø(a,a) b ¬ Ø(b,b) c ¬ Ø(c,c) d ¬ Ø(d,d) B 2 B 3 i ¬ Ø(i,i) a ¬ ••• c ¬ ••• B 4 B 5 B 3 a ¬ ••• B 2 b ¬ ••• d ¬ ••• c ¬ ••• d ¬ ••• B 6 B 4 B 5 d ¬ ••• c ¬ ••• d ¬ Ø(d,d) B 7 c ¬ Ø(c,c) B 6 b ¬ ••• Dominance B 7 Tree a ¬ Ø(a,a) b ¬ Ø(b,b) c ¬ Ø(c,c) • pre-order traversal of the dominator d ¬ Ø(d,d) y ¬ a+b tree: {0,1,2,3,4,5,6,7} z ¬ c+d i ¬ i+1 i > 100
Example Dominance Tree a ¬ ••• b ¬ ••• B 0 c ¬ ••• d ¬ ••• B 0 i ¬ ••• i > 100 B 1 B 1 a ¬ Ø(a,a) b ¬ Ø(b,b) B 2 B 3 c ¬ Ø(c,c) d ¬ Ø(d,d) i ¬ Ø(i,i) a ¬ ••• B 4 B 5 c ¬ ••• B 6 B 3 B 2 a ¬ ••• b ¬ ••• d ¬ ••• c ¬ ••• B 7 d ¬ ••• B 4 B 5 d ¬ ••• c ¬ ••• Before processing B 0 d ¬ Ø(d,d) c ¬ Ø(c,c) b ¬ ••• a b c d i B 6 Counters 0 0 0 0 0 B 7 a ¬ Ø(a,a) i ≤ 100 b ¬ Ø(b,b) Stacks c ¬ Ø(c,c) d ¬ Ø(d,d) y ¬ a+b stacks for uses of variables • z ¬ c+d i ¬ i+1 counters for new definitions • i > 100
Example a 0 ¬ ••• Dominance Tree b 0 ¬ ••• B 0 c 0 ¬ ••• d 0 ¬ ••• B 0 i > 100 i 0 ¬ ••• B 1 B 1 a ¬ Ø(a 0 ,a) b ¬ Ø(b 0 ,b) c ¬ Ø(c 0 ,c) B 2 B 3 d ¬ Ø(d 0 ,d) i ¬ Ø(i 0 ,i) a ¬ ••• c ¬ ••• B 4 B 5 B 6 B 3 B 2 a ¬ ••• b ¬ ••• d ¬ ••• c ¬ ••• d ¬ ••• B 4 B 5 B 7 d ¬ ••• c ¬ ••• End of B 0 d ¬ Ø(d,d) c ¬ Ø(c,c) b ¬ ••• a b c d i B 6 Counters 1 1 1 1 1 B 7 Stacks a ¬ Ø(a,a) i ≤ 100 a 0 b 0 c 0 d 0 i 0 b ¬ Ø(b,b) c ¬ Ø(c,c) d ¬ Ø(d,d) y ¬ a+b z ¬ c+d i ¬ i+1 i > 100
Example Dominance Tree a 0 ¬ ••• b 0 ¬ ••• B 0 c 0 ¬ ••• d 0 ¬ ••• i 0 ¬ ••• B 0 i > 100 B 1 B 1 a 1 ¬ Ø(a 0 ,a) b 1 ¬ Ø(b 0 ,b) B 2 B 3 c 1 ¬ Ø(c 0 ,c) d 1 ¬ Ø(d 0 ,d) i 1 ¬ Ø(i 0 ,i) a 2 ¬ ••• B 4 B 5 c 2 ¬ ••• B 6 B 3 B 2 a ¬ ••• b ¬ ••• d ¬ ••• c ¬ ••• B 7 d ¬ ••• B 4 B 5 d ¬ ••• c ¬ ••• End of B 1 d ¬ Ø(d,d) c ¬ Ø(c,c) b ¬ ••• a b c d i B 6 Counters 3 2 3 2 2 B 7 Stacks a ¬ Ø(a,a) i ≤ 100 a 0 b 0 c 0 d 0 i 0 b ¬ Ø(b,b) c ¬ Ø(c,c) a 1 b 1 c 1 d 1 i 1 d ¬ Ø(d,d) y ¬ a+b a 2 c 2 z ¬ c+d i ¬ i+1 i > 100
Example Dominance Tree a 0 ¬ ••• b 0 ¬ ••• B 0 c 0 ¬ ••• d 0 ¬ ••• i 0 ¬ ••• B 0 i > 100 B 1 B 1 a 1 ¬ Ø(a 0 ,a) b 1 ¬ Ø(b 0 ,b) B 2 B 3 c 1 ¬ Ø(c 0 ,c) d 1 ¬ Ø(d 0 ,d) i 1 ¬ Ø(i 0 ,i) a 2 ¬ ••• B 4 B 5 c 2 ¬ ••• B 6 B 3 B 2 a ¬ ••• b 2 ¬ ••• d ¬ ••• c 3 ¬ ••• B 7 d 2 ¬ ••• B 4 B 5 d ¬ ••• c ¬ ••• d ¬ Ø(d,d) End of B 2 c ¬ Ø(c,c) b ¬ ••• a b c d i B 6 Counters 3 3 4 3 2 B 7 Stacks a ¬ Ø(a 2 ,a) i ≤ 100 a 0 b 0 c 0 d 0 i 0 b ¬ Ø(b 2 ,b) c ¬ Ø(c 3 ,c) a 1 b 1 c 1 d 1 i 1 d ¬ Ø(d 2 ,d) y ¬ a+b a 2 b 2 c 2 d 2 z ¬ c+d i ¬ i+1 c 3 i > 100
Example Dominance Tree a 0 ¬ ••• b 0 ¬ ••• B 0 c 0 ¬ ••• d 0 ¬ ••• B 0 i 0 ¬ ••• i > 100 B 1 B 1 a 1 ¬ Ø(a 0 ,a) b 1 ¬ Ø(b 0 ,b) B 2 B 3 c 1 ¬ Ø(c 0 ,c) d 1 ¬ Ø(d 0 ,d) i 1 ¬ Ø(i 0 ,i) a 2 ¬ ••• B 4 B 5 c 2 ¬ ••• B 6 B 3 a ¬ ••• B 2 b 2 ¬ ••• d ¬ ••• c 3 ¬ ••• B 7 d 2 ¬ ••• B 4 B 5 d ¬ ••• c ¬ ••• Before starting B 3 d ¬ Ø(d,d) c ¬ Ø(c,c) b ¬ ••• a b c d i B 6 Counters 3 3 4 3 2 B 7 Stacks a ¬ Ø(a 2 ,a) i ≤ 100 a 0 b 0 c 0 d 0 i 0 b ¬ Ø(b 2 ,b) c ¬ Ø(c 3 ,c) a 1 b 1 c 1 d 1 i 1 d ¬ Ø(d 2 ,d) y ¬ a+b a 2 c 2 z ¬ c+d i ¬ i+1 i > 100
Example Dominance Tree a 0 ¬ ••• b 0 ¬ ••• B 0 c 0 ¬ ••• d 0 ¬ ••• B 0 i 0 ¬ ••• i > 100 B 1 B 1 a 1 ¬ Ø(a 0 ,a) b 1 ¬ Ø(b 0 ,b) B 2 B 3 c 1 ¬ Ø(c 0 ,c) d 1 ¬ Ø(d 0 ,d) i 1 ¬ Ø(i 0 ,i) a 2 ¬ ••• B 4 B 5 c 2 ¬ ••• B 6 B 3 B 2 a 3 ¬ ••• b 2 ¬ ••• d 3 ¬ ••• c 3 ¬ ••• B 7 d 2 ¬ ••• B 4 B 5 d ¬ ••• c ¬ ••• d ¬ Ø(d,d) End of B 3 c ¬ Ø(c,c) b ¬ ••• a b c d i B 6 Counters 4 3 4 4 2 B 7 Stacks a ¬ Ø(a 2 ,a) a 0 b 0 c 0 d 0 i 0 b ¬ Ø(b 2 ,b) c ¬ Ø(c 3 ,c) a 1 b 1 c 1 d 1 i 1 d ¬ Ø(d 2 ,d) y ¬ a+b c 2 d 3 a 2 z ¬ c+d i ¬ i+1 a 3 i > 100
Recommend
More recommend