cs293s static single assignment ssa
play

CS293S Static Single-Assignment (SSA) Yufei Ding Summary Domain - PowerPoint PPT Presentation

CS293S Static Single-Assignment (SSA) Yufei Ding Summary Domain Direction Uses AVAIL Expressions Forward GCSE LIVEOUT Variables Backward Register alloc. Detect uninit. Construct SSA Useless-store Elim. VERYBUSY Expressions


  1. CS293S Static Single-Assignment (SSA) Yufei Ding

  2. Summary Domain Direction Uses AVAIL Expressions Forward GCSE LIVEOUT Variables Backward Register alloc. Detect uninit. Construct SSA Useless-store Elim. VERYBUSY Expressions Backward Hoisting CONSTANT Pairs <v,c> Forward Constant folding 2

  3. Reaching Definitions � A definition d of some variable v reaches operation i if there is at least one path leading from that definition to operation i before v is redefined. � REACHES(n): the set of definitions that reach the start of node n. � DEDEF(n): the set of downward-exposed definitions in n. � i.e. their defined variables are not redefined before leaving n. � DEFKILL(n): all definitions killed by a definition in n. REACHES(n o ) = Ø REACHES(n) = È m Î pred(n) DEDEF(m) È (REACHES(m) Ç DEFK ILL (m))

  4. Dominance � Recall that n dominates m iff n is on every path from n 0 to m � Every node dominates itself D OM ( n 0 ) = { n 0 } Initially, D OM (n) = all, " n ≠ n 0 � A rapid data-flow framework D OM ( n ) = { n } È ( Ç p Î preds(n) D OM ( p )) � n’s immediate dominator is its closest dominator except itself, IDOM(n)† † ID OM (n ) ≠ n, unless n is n 0 , by convention. 4

  5. Example D OM (n) = { n } È ( Ç p Î preds(n) D OM (p)) Progress of iterative solution for D OM B 0 B 1 B 2 B 3 Results of iterative solution for D OM B 4 B 5 B 6 0 B 7 Control Flow Graph 5

  6. Example Progress of iterative solution for D OM B 0 B 1 B 2 B 3 Results of iterative solution for D OM B 4 B 5 B 6 0 B 7 Dominance Tree 6

  7. SSA Construction � An important technique for many data flow and control flow analyses � A great example for showing the uses of data flow analysis � Many different data-flow problems � SSA is a variant form of the program that encodes both data flow and control flow directly into the IR. � Can serve as the basis for a large set of transformations 7

  8. Outline � Review of the SSA concept � Simple way to construct a (maximal) SSA � Better way to construct (semi-pruned) SSA � SSA construction algorithm 8

  9. Review SSA-form � Each name is defined exactly once x ¬ 17 - 4 � Each use refers to exactly one name x ¬ a + b What’s hard x ¬ y - z � Joins in the CFG are hard x ¬ 13 Building SSA Form � Insert Ø-functions at joint points z ¬ x * q � Rename all values for uniqueness s ¬ w - x 9

  10. Ø-function A Ø-function is a special kind of copy that selects one of its parameters. We assume that all Ø-functions in a block will execute at the same time: order doesn’t matter. y 1 ¬ ... y 2 ¬ ... y 3 ¬ Ø(y 1 ,y 2 ) Real machines do not implement a Ø-function directly in hardware. 10

  11. SSA Construction Algorithm ( High-level sketch ) 1. Insert Ø-functions 2. Rename values … that’s all ... … of course, there is some bookkeeping to be done ... 11

  12. SSA Construction Algorithm ( Lower-level sketch ) 1. Insert Ø-functions at every join for every name appearing in the CFG 2. Solve reaching definitions 3. Rename each use to the def that reaches x ¬ 17 - 4 ( will be unique ) x ¬ a + b x ¬ y - z x ¬ 13 z ¬ x * q s ¬ w - x 12

  13. Problem � Too many Ø-functions Produces “maximal” SSA � Precision � Space � Time x ¬ 17 - 4 y ¬ 17 - 4 � How to eliminate the useless Ø-functions? � Algorithms based on dominance x ¬ a + b x ¬ y - z x ¬ 13 z ¬ x * q s ¬ w - x

  14. For a definition of x defined in a block n, it is enough to insert Ø- functions for that definition in the blocks that are right outside the dominated region of n. A x ¬ x ¬ A A Ï Dom(D) B C A Ï Dom(p(D)) B insertion in D D because of B but not A no insertion in D D A Î Dom(D) x ¬ A A Ï Dom(D) A Î Dom(p(D)) i.e. D Î DF(A) B thus, insert Ø-function in D D 14

  15. 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 p B 2 B 3 i.e., n doesn’t strictly dominate m B 4 B 5 B 6 B 7 Control Flow Graph 15

  16. 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 For each join point x ( i.e., |preds(x)| > 1 ) B 1 For each CFG predecessor of x Walk up to ID OM (x ) in the dominator tree, adding each node y in the walk to DF(n) except IDOM(x). B 2 B 3 B 4 B 5 B 0 B 6 B 1 B 7 B 2 B 3 Control Flow Graph B 4 B 5 B 6 Dominance Tree 16 B 7

  17. B 0 Example B 1 B 2 B 3 B 4 B 5 B 0 B 6 x ¬ Ø(...) B 1 B 7 B 2 B 3 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) = {1} (halt ) 17

  18. SSA Construction Algorithm 1. Insert Ø-functions a.) calculate dominance frontiers b.) find global names (names appearing in >1 blocks) for each name, build a list of blocks that define it c.) insert Ø-functions " global name n " block b in which n is assigned " block d in b ’s dominance frontier insert a Ø-function for n in d add d to n ’s list of defining blocks 18

  19. B 0 i > 100 i ¬ ••• Example B 1 a ¬ Ø(a,a) Excluding b ¬ Ø(b,b) c ¬ Ø(c,c) local names d ¬ Ø(d,d) With all the Ø-functions avoids Ø’s i ¬ Ø(i,i) for y & z a ¬ ••• • Lots of new ops c ¬ ••• • Renaming is next B 3 B 2 a ¬ ••• b ¬ ••• d ¬ ••• c ¬ ••• d ¬ ••• B 4 B 5 d ¬ ••• c ¬ ••• d ¬ Ø(d,d) c ¬ Ø(c,c) b ¬ ••• B 6 B 7 a ¬ Ø(a,a) b ¬ Ø(b,b) c ¬ Ø(c,c) d ¬ Ø(d,d) y ¬ a+b z ¬ c+d Assume a, b, i ¬ i+1 c, & d defined before B 0 i > 100

  20. SSA Construction Algorithm ( Details ) 2. Rename variables in a pre-order walk over dominator tree (use an array of stacks, one stack per global name) Staring with the root block, b 1 counter per name for subscripts a.) generate unique names for each Ø-function and push them on the appropriate stacks b.) rewrite each operation in the block i. Rewrite uses of global names with the current version (from the stack) ii. Rewrite definition by inventing & pushing new name c.) fill in Ø-function parameters of successor blocks d.) recurse on b ’s children in the dominance tree Reset the state e.) < on exit from b > pop names generated in b from stacks 20

  21. SSA Construction Algorithm ( Details ) Adding all the details ... Rename(b) for each Ø-function in b, x ¬ Ø(…) for each global name i rename x as NewName(x) counter[i] ¬ 0 for each operation “x ¬ y op z” in b stack[i] ¬ Ø rewrite y as top(stack[y]) call Rename(n 0 ) rewrite z as top(stack[z]) rewrite x as NewName(x) NewName(n) i ¬ counter[n] for each successor of b in the CFG counter[n] ¬ counter[n] + 1 rewrite appropriate Ø parameters push n i onto stack[n] for each successor s of b in dom. tree return n i Rename(s) for each operation “x ¬ y op z” in b pop(stack[x]) 21

  22. B 0 i > 100 i ¬ ••• Example B 1 a ¬ Ø(a,a) b ¬ Ø(b,b) c ¬ Ø(c,c) d ¬ Ø(d,d) i ¬ Ø(i,i) Before processing B 0 a ¬ ••• c ¬ ••• Assume a, b, c, & d defined B 3 B 2 a ¬ ••• b ¬ ••• before B 0 d ¬ ••• c ¬ ••• d ¬ ••• B 4 B 5 d ¬ ••• c ¬ ••• d ¬ Ø(d,d) c ¬ Ø(c,c) b ¬ ••• a b c d i B 6 Counters 1 1 1 1 0 B 7 Stacks a ¬ Ø(a,a) i ≤ 100 a 0 b 0 c 0 d 0 b ¬ Ø(b,b) c ¬ Ø(c,c) d ¬ Ø(d,d) y ¬ a+b z ¬ c+d Assume a, b, i has not been i ¬ i+1 c, & d defined defined before B 0 i > 100

  23. B 0 i > 100 i 0 ¬ ••• Example B 1 a ¬ Ø(a 0 ,a) b ¬ Ø(b 0 ,b) c ¬ Ø(c 0 ,c) d ¬ Ø(d 0 ,d) i ¬ Ø(i 0 ,i) End of B 0 a ¬ ••• c ¬ ••• B 3 B 2 a ¬ ••• b ¬ ••• d ¬ ••• c ¬ ••• d ¬ ••• B 4 B 5 d ¬ ••• c ¬ ••• 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 Assume a, b, i ¬ i+1 c, & d defined before B 0 i > 100

  24. B 0 i > 100 i 0 ¬ ••• Example B 1 a 1 ¬ Ø(a 0 ,a) b 1 ¬ Ø(b 0 ,b) c 1 ¬ Ø(c 0 ,c) d 1 ¬ Ø(d 0 ,d) i 1 ¬ Ø(i 0 ,i) End of B 1 a 2 ¬ ••• c 2 ¬ ••• B 3 B 2 a ¬ ••• b ¬ ••• d ¬ ••• c ¬ ••• d ¬ ••• B 4 B 5 d ¬ ••• c ¬ ••• 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 Assume a, b, i ¬ i+1 c, & d defined before B 0 i > 100

  25. B 0 i > 100 i 0 ¬ ••• Example B 1 a 1 ¬ Ø(a 0 ,a) b 1 ¬ Ø(b 0 ,b) c 1 ¬ Ø(c 0 ,c) d 1 ¬ Ø(d 0 ,d) i 1 ¬ Ø(i 0 ,i) End of B 2 a 2 ¬ ••• c 2 ¬ ••• B 3 B 2 a ¬ ••• b 2 ¬ ••• d ¬ ••• c 3 ¬ ••• d 2 ¬ ••• B 4 B 5 d ¬ ••• c ¬ ••• 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 d 2 a 2 b 2 c 2 z ¬ c+d Assume a, b, i ¬ i+1 c 3 c, & d defined before B 0 i > 100

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend