static single assignment form
play

Static Single Assignment Form Last Time Static single assignment - PDF document

Static Single Assignment Form Last Time Static single assignment (SSA) form Today Applications of SSA CS553 Lecture Static Single Assignment Form 1 Dead Code Elimination for SSA Dead code elimination while a


  1. Static Single Assignment Form � Last Time – � Static single assignment (SSA) form � Today – � Applications of SSA CS553 Lecture Static Single Assignment Form 1 Dead Code Elimination for SSA � Dead code elimination while � a variable v with no uses and whose def has no other side effects Delete the statement s that defines v for each of s’s uses w Delete the use from list of uses of variable w x = a + b If y becomes dead and there are no other uses of x, then the assignment to s y = x + 3 x becomes dead, too – � Contrast this approach with one that uses liveness analysis – � This algorithm updates information incrementally – � With liveness, we need to invoke liveness and dead code elimination iteratively until we reach a fixed point CS553 Lecture Static Single Assignment Form 2

  2. Implementing Simple Constant Propagation � Standard worklist algorithm – � Identifies simple constants – � For each program point, maintains one constant value for each variable x = 1 � Problem ! � Inefficient, since constants may have to be propagated through irrelevant nodes y = x � Solution ! � Exploit a sparse dependence representation ( e.g., SSA) CS553 Lecture Static Single Assignment Form 3 Sparse Simple Constant Propagation � Reif and Lewis algorithm – � Identifies simple constants – � Faster than Simple Constants algorithm � SSA edges x = 1 ! � Explicitly connect defs with uses ! � How would you do this? � Main Idea ! � Iterate over SSA edges instead of over all CFG edges y = x CS553 Lecture Static Single Assignment Form 4

  3. Sparse Simple Constants Algorithm (Ch. 19 in Appel) worklist = all statements in SSA while worklist � � Remove some statement S from worklist if S is x = phi(c,c,...,c) for some constant c replace S with v = c if S is x=c for some constant c delete S from program for each statement T that uses x substitute c for x in T worklist = worklist union {T} CS553 Lecture Static Single Assignment Form 5 Copy Propagation � Algorithm worklist = all statements in SSA while worklist � � Remove some statement S from worklist if S is x = phi(y) or x = y for each statement T that uses x replace all use of x with y worklist = worklist union {T} delete S CS553 Lecture Static Single Assignment Form 6

  4. Reuse Optimization � Idea – � Eliminate redundant operations in the dynamic execution of instructions � How do redundancies arise? – � Loop invariant code ( e.g., index calculation for arrays) – � Sequence of similar operations ( e.g., method lookup) – � Same value be generated in multiple places in the code � Types of reuse optimization – � Value numbering – � Common subexpression elimination – � Partial redundancy elimination CS553 Lecture Value Numbering 7 Local Value Numbering � Idea – � Each variable, expression, and constant is assigned a unique number – � When we encounter a variable, expression or constant, see if it’s already been assigned a number – � If so, use the value for that number – � If not, assign a new number – � Same number � same value � Example � b � #1 � � #3 � � c � #2 � a := b + c � � b + c is #1 + # 2 � #3 � d := b � � a � # 3 � b := a � � d � #1 � � a � e := d + c � � d + c is #1 + # 2 � #3 � � e � #3 � CS553 Lecture Value Numbering 8

  5. Local Value Numbering (cont) � Temporaries may be necessary � b � #1 � a := b + c � c � #2 a := b � b + c is #1 + # 2 � #3 � d := a + c � a � # 3 � � #1 � � � a + c is #1 + # 2 � #3 � � d � #3 � � b � #1 � t := b + c � c � #2 a := b � � b + c is #1 + # 2 � #3 � d := b + c � t � t � # 3 � � a � # 1 � � a + c is #1 + # 2 � #3 � � d � #3 � CS553 Lecture Value Numbering 9 Global Value Numbering � How do we handle control flow? w = 5 w = 8 x = 5 x = 8 w � #1 � w � #2 � y = w+1 x � #1 � x � #2 � z = x+1 . . . . . . CS553 Lecture Value Numbering 10

  6. Global Value Numbering (cont) � Idea [Alpern, Wegman, and Zadeck 1988] – � Partition program variables into congruence classes – � All variables in a particular congruence class have the same value – � SSA form is helpful � Approaches to computing congruence classes – � Pessimistic – � Assume no variables are congruent (start with n classes) – � Iteratively coalesce classes that are determined to be congruent – � Optimistic – � Assume all variables are congruent (start with one class) – � Iteratively partition variables that contradict assumption – � Slower but better results CS553 Lecture Value Numbering 11 Role of SSA Form � SSA form is helpful – � Allows us to avoid data-flow analysis – � Variables correspond to values a = b a 1 = b . . . . . . Congruence classes: a not congruent to a = c a 2 = c { a 1 , b }, { a 2 , c }, { a 3 , d } anything . . . . . . a = d a 3 = d CS553 Lecture Value Numbering 12

  7. Basis � Idea – � If x and y are congruent then f(x) and f(y) are congruent ta = a x and y are tb = b congruent x = f(a,b) y = f(ta,tb) – � Use this fact to combine (pessimistic) or split (optimistic) classes � Problem – � This is not true for � -functions a 1 & b 1 congruent? a 1 = x 1 a 2 = y 1 b 1 = x 1 b 2 = y 1 a 2 & b 2 congruent? n m a 3 = � (a 1 ,a 2 ) b 3 = � (b 1 ,b 2 ) n m a 3 & b 3 congruent? � Solution: Label � -functions with join point CS553 Lecture Value Numbering 13 Pessimistic Global Value Numbering � Idea – � Initially each variable is in its own congruence class – � Consider each assignment statement s (reverse postorder in CFG) – � Update LHS value number with hash of RHS – � Identical value number � congruence � Why reverse postorder? – � Ensures that when we consider an assignment statement, we have already considered definitions that reach the RHS operands a b Postorder: d, c, e, b, f, a c e f d CS553 Lecture Value Numbering 14

  8. Algorithm for each assignment of the form: “ x = f(a,b) ” ValNum[x] � UniqueValue() // same for a and b for each assignment of the form: “ x = f(a,b) ” (in reverse postorder) ValNum[x] � Hash(f � ValNum[a] � ValNum[b]) #1 a 1 i 1 = 1 #2 b 1 #3 i 1 w 1 = b 1 w 2 = a 1 #4 w 1 #2 x 1 = b 1 x 2 = a 1 x 1 #5 #2 #1 w 2 #6 w 3 = � n (w 1 , w 2 ) #7 #1 x 2 x 3 = � n (x 1 , x 2 ) #8 � n (#2,#1) � #12 w 3 y 1 = w 3 +i 1 � n (#2,#1) � #12 x 3 #9 z 1 = x 3 +i 1 #10 y 1 +(#12,#3) � #13 #11 z 1 +(#12,#3) � #13 CS553 Lecture Value Numbering 15 Snag! � Problem – � Our algorithm assumes that we consider operands before variables that depend upon it – � Can’t deal with code containing loops! � Solution – � Ignore back edges – � Make conservative (worst case) assumption for previously unseen variable ( i.e., assume its in it’s own congruence class) CS553 Lecture Value Numbering 16

  9. Optimistic Global Value Numbering � Idea – � Initially all variables in one congruence class – � Split congruence classes when evidence of non-congruence arises – � Variables that are computed using different functions – � Variables that are computed using functions with non-congruent operands CS553 Lecture Value Numbering 17 Splitting � Initially – � Variables computed using the same function are placed in the same class P P’ x 1 = f(a 1 ,b 1 ) . . . x 1 y 1 z 1 y 1 = f(c 1 ,d 1 ) . . . z 1 = f(e 1 ,f 1 ) � Iteratively Q – � Split classes when corresponding operands are in different classes a 1 c 1 – � Example: assume a 1 and c 1 are congruent, but e 1 is congruent to neither CS553 Lecture Value Numbering 18

  10. Splitting (cont) � Definitions – � Suppose P and Q are sets representing congruence classes – � Q splits P for each i into two sets – � P \ i Q contains variables in P whose i th operand is in Q – � P / i Q contains variables in P whose i th operand is not in Q – � Q properly splits P if neither resulting set is empty P Q x 1 = f(a 1 ,b 1 ) . . . x 1 y 1 z 1 a 1 c 1 y 1 = f(c 1 ,d 1 ) . . . z 1 = f(e 1 ,f 1 ) P \ 1 Q P / 1 Q CS553 Lecture Value Numbering 19 Example SSA code Congruence classes S 0 { x 0 } x 0 = 1 S 1 { y 0 } y 0 = 2 S 2 { x 1 , y 1 , z 1 } x 1 = x 0 +1 S 3 { x 1 ,z 1 } y 1 = y 0 +1 S 4 { y 1 } z 1 = x 0 +1 Worklist: S 0 ={ x 0 }, S 1 ={ y 0 }, S 2 ={ x 1 , y 1 ,z 1 } S 3 ={ x 1 ,z 1 }, S 4 ={ y 1 } S 0 psplit S 0 ? no S 0 psplit S 1 ? no S 0 psplit S 2 ? yes! S 2 \ 1 S 0 = { x 1 , z 1 } = S 3 S 2 / 1 S 0 = { y 1 } = S 4 CS553 Lecture Value Numbering 20

  11. Algorithm worklist � � for each function f C f � � for each assignment of the form “x = f(a,b)” C f � C f � { x } worklist � worklist � {C f } CC � CC � {C f } while worklist � � Delete some D from worklist for each class C properly split by D (at operand i) CC � CC – C worklist � worklist – C Create new congruence classes C j � {C \ i D} and C k � {C / i D} CC � CC � C j � C k worklist � worklist � C j � C k Note: see paper for optimization CS553 Lecture Value Numbering 21 Comparing Optimistic and Pessimistic � Differences – � Handling of loops – � Pessimistic makes worst-case assumptions on back edges – � Optimistic requires actual contradiction to split classes w 0 = 5 x 0 = 5 w 1 = � (w 0 ,w 2 ) x 1 = � (x 0 ,x 2 ) w 2 = w 1 +1 x 2 = x 1 +1 CS553 Lecture Value Numbering 22

Recommend


More recommend