Global Value Numbering
Total Page:16
File Type:pdf, Size:1020Kb
Reuse Optimization Local Value Numbering Announcement Idea − HW2 is due Monday! I will not accept late HW2 turnins − Each variable, expression, and constant is assigned a unique number − When we encounter a variable, expression or constant, see if it’s already Idea been assigned a number − Eliminate redundant operations in the dynamic execution of instructions − If so, use the value for that number How do redundancies arise? − If not, assign a new number − Loop invariant code (e.g., index calculation for arrays) − Same number ⇒ same value − Sequence of similar operations (e.g., method lookup) b → #1 #3 − Same value be generated in multiple places in the code Example a := b + c c → #2 b + c is #1 + # 2 → #3 Types of reuse optimization d := b a → #3 − Value numbering b := a − Common subexpression elimination d → #1 e := d + c a is − Partial redundancy elimination d + c #1 + # 2 → #3 e → #3 CS553 Lecture Value Numbering 2 CS553 Lecture Value Numbering 3 Local Value Numbering (cont) Global Value Numbering Temporaries may be necessary How do we handle control flow? b → #1 a := b + c c → #2 w = 5 w = 8 a := b b + c is #1 + # 2 → #3 x = 5 x = 8 d := a + c a → #3 #1 a + c is #1 + # 2 → #3 w → #1 w → #2 d → #3 y = w+1 x → #1 x → #2 z = x+1 . 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 4 CS553 Lecture Value Numbering 5 1 Global Value Numbering (cont) Role of SSA Form Idea [Alpern, Wegman, and Zadeck 1988] SSA form is helpful − Partition program variables into congruence classes − Allows us to avoid data-flow analysis − All variables in a particular congruence class have the same value − Variables correspond to values − SSA form is helpful a = b a1 = b . a = c a = c Approaches to computing congruence classes a not congruent to 2 Congruence classes: . anything {a1,b}, {a2,c}, {a3,d} − Pessimistic a = d a3 = d − 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 6 CS553 Lecture Value Numbering 7 Basis Pessimistic Global Value Numbering Idea Idea − If x and y are congruent then f(x) and f(y) are congruent − Initially each variable is in its own congruence class ta = a − Consider each assignment statement s (reverse postorder in CFG) x and y are tb = b − Update LHS value number with hash of RHS x = f(a,b) congruent − Identical value number ⇒ congruence y = f(ta,tb) − Use this fact to combine (pessimistic) or split (optimistic) classes Why reverse postorder? Problem − Ensures that when we consider an assignment statement, we have already − This is not true for φ-functions considered definitions that reach the RHS operands a1 & b1 congruent? a a1 = x1 a2 = y1 b1 = x1 b2 = y1 a & b congruent? n m 2 2 b Postorder: d, c, e, b, f, a c e a3 = φn (a1,a2) b3 = φm (b1,b2) f a3 & b3 congruent? Solution: Label φ-functions with join point d CS553 Lecture Value Numbering 8 CS553 Lecture Value Numbering 9 2 Algorithm Snag! for each assignment of the form: “x = f(a,b)” Problem ValNum[x] ← UniqueValue() // same for a and b − Our algorithm assumes that we consider operands before variables that for each assignment of the form: “x = f(a,b)” (in reverse postorder) depend upon it ValNum[x] ← Hash(f ⊕ ValNum[a] ⊕ ValNum[b]) − Can’t deal with code containing loops! i1 = 1 a1 #1 Solution b1 #2 − Ignore back edges i1 #3 w1 = b1 w2 = a1 w1 #4 #2 − Make conservative (worst case) assumption for previously unseen x1 = b1 x2 = a1 x1 #5 #2 variable (i.e., assume its in it’s own congruence class) w2 #6 #1 x #7 #1 w3 = φn(w1,w2) 2 w #8 φn(#2,#1) → #12 x3 = φn(x1,x2) 3 φ (#2,#1) → #12 y = w +i x3 #9 n 1 3 1 +(#12,#3) #13 z = x +i y1 #10 → 1 3 1 +(#12,#3) #13 z1 #11 → CS553 Lecture Value Numbering 10 CS553 Lecture Value Numbering 11 Optimistic Global Value Numbering Splitting Idea Initially − Initially all variables in one congruence class − Variables computed using the same function are placed in the same class − Split congruence classes when evidence of non-congruence arises P P’ x1 = f(a1,b1) − Variables that are computed using different functions . x1 y1 z1 − Variables that are computed using functions with non-congruent y1 = f(c1,d1) operands . z1 = f(e1,f1) Iteratively Q − Split classes when corresponding operands are in different classes a1 c1 − Example: a1 and c1 are congruent, but e1 is congruent to neither CS553 Lecture Value Numbering 12 CS553 Lecture Value Numbering 13 3 Splitting (cont) Algorithm Definitions worklist ← ∅ for each function f − Suppose P and Q are sets representing congruence classes Cf ← ∅ − Q splits P for each i into two sets for each assignment of the form “x = f(a,b)” th − P \ Q contains variables in P whose i operand is in Q i Cf ← Cf ∪ { x } th − P /i Q contains variables in P whose i operand is not in Q worklist ← worklist ∪ {Cf} − Q properly splits P if neither resulting set is empty CC ← CC ∪ {Cf} while worklist ≠ ∅ Delete some D from worklist P Q x1 = f(a1,b1) for each class C properly split by D (at operand i) . CC ← CC – C x1 y1 z1 a1 c1 worklist worklist – C y1 = f(c1,d1) ← . Create new congruence classes Cj ←{C \i D} and Ck ←{C /i D} z = f(e ,f ) CC ← CC ∪ Cj ∪ Ck 1 1 1 P \ Q P / Q 1 1 worklist ← worklist ∪ Cj ∪ Ck Note: see paper for optimization CS553 Lecture Value Numbering 14 CS553 Lecture Value Numbering 15 Example Comparing Optimistic and Pessimistic SSA code Congruence classes Differences S {x } − Handling of loops x0 = 1 0 0 S {y } − Pessimistic makes worst-case assumptions on back edges y0 = 2 1 0 S2 {x ,y ,z } − Optimistic requires actual contradiction to split classes x1 = x0+1 1 1 1 S {x ,z } y1 = y0+1 3 1 1 z = x +1 S4 {y1} 1 0 w0 = 5 x0 = 5 Worklist: S0={x0}, S1={y0}, S2={x1,y1,z1,} S3={x1,z1}, S4={y1} S0 psplit S0? no S0 psplit S1? no S0 psplit S2? yes! w1=φ(w0,w2) x1=φ(x0,x2) S \ S = { , } = S 2 1 0 x1 z1 3 w2 = w1+1 x2 = x1+1 S2 /1 S0 = {y1} = S4 CS553 Lecture Value Numbering 16 CS553 Lecture Value Numbering 17 4 Role of SSA Next Time Single global result Lecture − Single def reaches each use − Midterm Review − No data (flow value) at each point − Send questions you would like answered at the Midterm Review No data flow analysis − Optimistic: Iterate over congruence classes, not CFG nodes − Pessimistic: Visit each assignment once φ-functions − Make data-flow merging explicit − Treat like normal functions CS553 Lecture Value Numbering 18 CS553 Lecture Value Numbering 19 5.