CS293S SSA & Dead Code Elimination

CS293S SSA & Dead Code Elimination

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 B0 • DF(n ) is fringe just beyond the region n dominates B1 • m ÎDF(n) : iff n Ï(Dom(m) - {m}) but n Î DOM(p) for some p Î preds(m). B2 B3 i.e., n dominates m’s i.e., n doesn’t strictly some parent dominate m B4 B5 B6 B7 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 B0 B0 dominance frontiers For each join point x B1 B1 For each CFG predecessor of x (from the CFG) Walk up to IDOM(x ) in the dominator tree, adding x to DF(n) for each n in the walk except IDOM(x). B2 B3 B2 B3 B4 B5 B4 B5 B6 B6 • What if there is another back edge from B7 to B0? B7 B7 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 B0 for each node n in work list x¬B1Ø(...) for each node m in DF(n) if (there is no ϕ-function for x in m) insert a ϕ-function for x in m B2 B3 work list = work list ∪ { m } x¬ ... B4 B5 x¬B Ø(...) 6 • DF(4) is {6}, so ¬ in 4 forces Ø-function in 6 x¬B7Ø(...) • ¬ in 6 forces Ø-function in DF(6) = {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 ¬ ••• c ¬ ••• • Globals = ⋃"## $ %$ &'( UEVar(n) d ¬ ••• UEVar(n): Upper exposed variables in block n, i.e., i ¬ ••• i > 100 B0 variables used before it is redefined in block n. (We have learned this in liveness analysis. ) B 1 a ¬ ••• c ¬ ••• • Example B B3 UEVAR (B7) = {a, b, c, d, i}; 2 b ¬ ••• a ¬ ••• c ¬ ••• d ¬ ••• all others are empty set; d ¬ ••• Globals = {a, b, c, d, i}, (y, z are local names) B4 B5 d ¬ ••• c ¬ ••• • Get blocks where each of these Globals get b ¬ ••• defined B6 Names a b c d i B7 y ¬ a+b blocks 0,1,3 0,2,6 0,1,2,5 0,2,3,4 0,7 z ¬ c+d i ¬ i+1 i > 100 Phase 2: inserting ϕ-functions a ¬ ••• b ¬ ••• for each of the global name x c ¬ ••• d ¬ ••• work list = get all nodes in which x is defined i ¬ ••• B0 i > 100 for each node n in work list Excluding local for each node m in DF(n) B 1 a ¬ ••• names avoids if (there is no ϕ-function for x in m) c ¬ ••• Ø’s for y & z insert a ϕ-function for x to m work list = work list ∪ { m } B B3 2 b ¬ ••• a ¬ ••• c ¬ ••• d ¬ ••• d ¬ ••• B4 B5 d ¬ ••• c ¬ ••• b ¬ ••• B6 Names a b c d i blocks 0,1,3 0,2,6 0,1,2,5 0,2,3,4 0,7 B7 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) One possible Implementation via a for each operation “x ¬ y op z” in b set of stacks and counters. rewrite y as top(stack[y]) rewrite z as top(stack[z]) 1. Get the root node n0 of the rewrite x as NewName(x) CFG for each successor of b in the CFG 2. Call Rename(n0) 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]) a ¬ ••• Example b ¬ ••• c ¬ ••• d ¬ ••• B0 i ¬ ••• i > 100 B0 B 1 a ¬ Ø(a,a) B1 b ¬ Ø(b,b) c ¬ Ø(c,c) d ¬ Ø(d,d) i ¬ Ø(i,i) B2 B3 a ¬ ••• c ¬ ••• B4 B5 B B3 2 b ¬ ••• a ¬ ••• c ¬ ••• d ¬ ••• d ¬ ••• B4 B5 B d ¬ ••• c ¬ ••• 6 d ¬ Ø(d,d) c ¬ Ø(c,c) B7 B6 b ¬ ••• Dominance B7 a ¬ Ø(a,a) Tree b ¬ Ø(b,b) c ¬ Ø(c,c) d ¬ Ø(d,d) • pre-order traversal of the dominator y ¬ a+b z ¬ c+d tree: {0,1,2,3,4,5,6,7} i ¬ i+1 i > 100 Example a ¬ ••• Dominance Tree b ¬ ••• c ¬ ••• B0 d ¬ ••• B0 i ¬ ••• i > 100 B1 B1 a ¬ Ø(a,a) b ¬ Ø(b,b) c ¬ Ø(c,c) B2 B3 d ¬ Ø(d,d) i ¬ Ø(i,i) a ¬ ••• c ¬ ••• B4 B5 B6 B B3 2 b ¬ ••• a ¬ ••• c ¬ ••• d ¬ ••• d ¬ ••• B B4 B5 7 d ¬ ••• c ¬ ••• d ¬ Ø(d,d) Before processing B c ¬ Ø(c,c) 0 b ¬ ••• B6 a b c d i Counters 0 0 0 0 0 B7 a ¬ Ø(a,a) i ≤ 100 b ¬ Ø(b,b) c ¬ Ø(c,c) Stacks d ¬ Ø(d,d) y ¬ a+b z ¬ c+d • stacks for uses of variables i ¬ i+1 • counters for new definitions i > 100 Example a0 ¬ ••• Dominance Tree b0 ¬ ••• c ¬ ••• 0 B0 B0 d0 ¬ ••• i > 100 i0 ¬ ••• B1 B1 a ¬ Ø(a0,a) b ¬Ø(b0,b) c ¬ Ø(c0,c) d ¬ Ø(d0,d) B2 B3 i ¬ Ø(i0,i) a ¬ ••• c ¬ ••• B4 B5 B2 B3 a ¬ ••• b ¬ ••• B6 c ¬ ••• d ¬ ••• d ¬ ••• B4 B5 d ¬ ••• c ¬ ••• B7 d ¬ Ø(d,d) End of B0 c ¬ Ø(c,c) b ¬ ••• B6 a b c d i Counters 1 1 1 1 1 B7 a ¬ Ø(a,a) i ≤ 100 Stacks a b c d i b ¬ Ø(b,b) 0 0 0 0 0 c ¬ Ø(c,c) d ¬ Ø(d,d) y ¬ a+b z ¬ c+d i ¬ i+1 i > 100 Example a0 ¬ ••• Dominance Tree b0 ¬ ••• c0 ¬ ••• B0 d0 ¬ ••• B0 i0 ¬ ••• i > 100 B1 B1 a1 ¬ Ø(a0,a) b1 ¬ Ø(b0,b) c1 ¬ Ø(c0,c) B2 B3 d1 ¬ Ø(d0,d) i1 ¬ Ø(i0,i) a2 ¬ ••• B4 B5 c2 ¬ ••• B6 B B3 2 b ¬ ••• a ¬ ••• c ¬ ••• d ¬ ••• d ¬ ••• B7 B4 B5 d ¬ ••• c ¬ ••• d ¬ Ø(d,d) End of B1 c ¬ Ø(c,c) b ¬ ••• B6 a b c d i Counters 3 2 3 2 2 B7 a ¬ Ø(a,a) i ≤ 100 Stacks a b c d i b ¬ Ø(b,b) 0 0 0 0 0 c ¬ Ø(c,c) a b c d i d ¬ Ø(d,d) 1 1 1 1 1 y ¬ a+b a c z ¬ c+d 2 2 i ¬ i+1 i > 100 Example a0 ¬ ••• Dominance Tree b0 ¬ ••• c0 ¬ ••• B0 d0 ¬ ••• B0 i0 ¬ ••• i > 100 B1 B1 a1 ¬ Ø(a0,a) b1 ¬ Ø(b0,b) c1 ¬ Ø(c0,c) B2 B3 d1 ¬ Ø(d0,d) i1 ¬ Ø(i0,i) a2 ¬ ••• B4 B5 c2 ¬ ••• B6 B2 B3 a ¬ ••• b2 ¬ ••• d ¬ ••• c3 ¬ ••• d2 ¬ ••• B7 B4 B5 d ¬ ••• c ¬ ••• d ¬ Ø(d,d) c ¬ Ø(c,c) End of B2 b ¬ ••• B6 a b c d i Counters 3 3 4 3 2 B7 a ¬ Ø(a2,a) i ≤ 100 Stacks a0 b0 c0 d0 i0 b ¬ Ø(b2,b) c ¬ Ø(c3,c) a1 b1 c1 d1 i1 d ¬ Ø(d2,d) y ¬ a+b a b c d z ¬ c+d 2 2 2 2 i ¬ i+1 c3 i > 100 Example a0 ¬ ••• Dominance Tree b0 ¬ ••• c0 ¬ ••• B0 d0 ¬ ••• B 0 i0 ¬ ••• i > 100 B1 B1 a1 ¬ Ø(a0,a) b1 ¬ Ø(b0,b) c1 ¬ Ø(c0,c) B2 B3 d1 ¬ Ø(d0,d) i1 ¬ Ø(i0,i) a2 ¬ ••• B B c2 ¬ ••• 4 5 B6 B2 B3 a ¬ ••• b2 ¬ ••• d ¬ ••• c3 ¬ ••• d ¬ ••• 2 B7 B4 B5 d ¬ ••• c ¬ ••• d ¬ Ø(d,d) Before starting B c ¬ Ø(c,c) 3 b ¬ ••• B6 a b c d i Counters 3 3 4 3 2 B7 a ¬ Ø(a2,a) i ≤ 100 Stacks a0 b0 c0 d0 i0 b ¬ Ø(b2,b) c ¬ Ø(c3,c) a1 b1 c1 d1 i1 d ¬ Ø(d2,d) y ¬ a+b a c z ¬ c+d 2 2 i ¬ i+1 i > 100 Example a0 ¬ ••• Dominance Tree b0 ¬ ••• c0 ¬ ••• B0 d0 ¬ ••• B 0 i0 ¬ ••• i > 100 B1 B1 a1 ¬ Ø(a0,a) b1 ¬ Ø(b0,b) c1 ¬ Ø(c0,c) B2 B3 d1 ¬ Ø(d0,d) i1 ¬ Ø(i0,i) a2 ¬ ••• B4 B5 c2 ¬ ••• B6 B B3 2 a3 ¬ ••• b2 ¬ ••• d3 ¬ ••• c3 ¬ ••• d2 ¬ ••• B7 B4 B5 d ¬ ••• c ¬ ••• d ¬ Ø(d,d) c ¬ Ø(c,c) End of B3 b ¬ ••• B6 a b c d i Counters 4 3 4 4 2 B7 a ¬ Ø(a2,a) Stacks a0 b0 c0 d0 i0 b ¬ Ø(b2,b) c ¬ Ø(c3,c) a1 b1 c1 d1 i1 d ¬ Ø(d2,d) y ¬ a+b a c d z ¬ c+d 2 2 3 i ¬ i+1 a3 i > 100 Example a0 ¬ ••• Dominance Tree b0 ¬ ••• B0 c0 ¬ ••• d0 ¬ ••• B0 i0 ¬ ••• i > 100 B1 B1 a1 ¬ Ø(a0,a) b1 ¬ Ø(b0,b) c1 ¬ Ø(c0,c) B2 B3 d1 ¬ Ø(d0,d) i1 ¬ Ø(i0,i) a ¬ ••• 2 B4 B5 c2 ¬ ••• B6 B B3 2 a3 ¬ ••• b2 ¬ ••• d3 ¬ ••• c3 ¬ ••• d2 ¬ ••• B7 B4 B5 d4 ¬ ••• c ¬ ••• End of B d ¬ Ø(d4,d) 4 c ¬ Ø(c2,c) b ¬ ••• B6 a b c d i Counters 4 3 4 5 2 B7 a ¬ Ø(a2,a) Stacks a0 b0 c0 d0 i0 b ¬ Ø(b2,b) c ¬ Ø(c3,c) a1 b1 c1 d1 i1 d ¬ Ø(d2,d) y ¬ a+b a c d z ¬ c+d 2 2 3 i ¬ i+1 a3 d4 i > 100 Example a0 ¬ ••• Dominance Tree b0 ¬ ••• B0 c0 ¬ ••• d0 ¬ ••• B0 i0 ¬ ••• i > 100 B1 B1 a1 ¬ Ø(a0,a) b1 ¬ Ø(b0,b) c1 ¬ Ø(c0,c) B2 B3 d1 ¬ Ø(d0,d) i1 ¬ Ø(i0,i) a ¬ ••• 2 B4 B5 c2 ¬ ••• B6 B B3 2 a3 ¬ ••• b2 ¬ ••• d3 ¬ ••• c3 ¬ ••• d2 ¬ ••• B7 B4 B5 d4 ¬ ••• c ¬ ••• Before processing B d ¬ Ø(d4,d) 5 c ¬ Ø(c2,c) b ¬ ••• B6 a b c d i Counters 4 3 4 5 2 B7 a ¬ Ø(a2,a) Stacks a0 b0 c0 d0 i0 b ¬ Ø(b2,b) c ¬ Ø(c3,c) a1 b1 c1 d1 i1 d ¬ Ø(d2,d) y ¬ a+b a c d z ¬ c+d 2 2 3 i ¬ i+1 a3 i > 100 Example a0 ¬ ••• Dominance Tree b0 ¬ ••• c0 ¬ ••• B0 d0 ¬ ••• i0 ¬ ••• B0 i > 100 B1 B1 a1 ¬ Ø(a0,a) b ¬ Ø(b ,b) 1 0 B B c1 ¬ Ø(c0,c) 2 3 d1 ¬ Ø(d0,d) i1 ¬ Ø(i0,i) a ¬ ••• 2 B4 B5 c2 ¬ ••• B6 B B3 2 a3 ¬ ••• b2 ¬ ••• d3 ¬ ••• c3 ¬ ••• d2 ¬ ••• B7 B4 B5 d4 ¬ ••• c4 ¬ ••• d ¬ Ø(d4,d3) End of B5 c ¬ Ø(c2,c4) b ¬ ••• B6 a b c d i Counters 4 3 5 5 2 B7 a ¬ Ø(a2,a) Stacks a0 b0 c0 d0 i0 b ¬ Ø(b2,b) c ¬ Ø(c3,c) a1 b1 c1 d1 i1 d ¬ Ø(d2,d) y ¬ a+b a c d z ¬ c+d 2 2 3 i ¬ i+1 a3 c4 i > 100 Example a0 ¬ ••• Dominance Tree b0 ¬ ••• c0 ¬ ••• B0 d0 ¬ ••• i0 ¬ ••• B0 i > 100 B1 B1 a1 ¬ Ø(a0,a) b ¬ Ø(b ,b) 1 0 B B c1 ¬ Ø(c0,c) 2 3 d1 ¬ Ø(d0,d) i1 ¬ Ø(i0,i) a ¬ ••• 2 B4 B5 c2 ¬ ••• B6 B B3 2 a3 ¬ ••• b2 ¬ ••• d3 ¬ ••• c3 ¬ ••• d2 ¬ ••• B7 B4 B5 d4 ¬ ••• c4 ¬ ••• Before B d ¬ Ø(d4,d3) 6 c ¬ Ø(c2,c4) b ¬ ••• B6 a b c d i Counters 4 3 5 5 2 B7 a ¬ Ø(a2,a) Stacks a0 b0 c0 d0 i0 b ¬ Ø(b2,b) c ¬ Ø(c3,c) a1 b1 c1 d1 i1 d ¬ Ø(d2,d) y ¬ a+b a c d z ¬ c+d 2 2 3 i ¬ i+1 a3 i > 100 Example a0 ¬ •••

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    42 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us