400 likes | 557 Views
Optimizing Compilers CISC 673 Spring 2009 More Data flow analysis. John Cavazos University of Delaware. Overview. Last Time: Avail Expressions Using Sets Using Bit Vectors Additional Data Flow Problem Constant Propagation Generalization of Data Flow. Computing Available Expressions.
E N D
Optimizing CompilersCISC 673Spring 2009More Data flow analysis John Cavazos University of Delaware
Overview • Last Time: Avail Expressions Using Sets • Using Bit Vectors • Additional Data Flow Problem • Constant Propagation • Generalization of Data Flow
Computing Available Expressions AVAIL(b) = ppred(b)(DEEXPR(p) (AVAIL(p) EXPRKILL(p) )) AVAIL(n0) = Ø where preds(b) is the set of b’s predecessors in the CFG Remember these!
Avail Expressions Using Bit Vectors • 1 = avail; 0 = not • Boundary condition • Forward Flow on Entry • Backward Flow on Exit • Initial conditions • Merge operator
Avail Expressions Using Bit Vectors Note: Empty entry and exit blocks added to CFG To save space not pictured here! 1=avail 0=not (A+B)(X+Y) 10 = (A+B) avail (X+Y) not 11 = both avail …
Avail Expressions Using Bit Vectors What is Merge Operator? Remember original equation 1=avail 0=not (A+B)(X+Y) …
Avail Expressions Using Bit Vectors What is Boundary Condition? Remember original algorithm 1=avail 0=not (A+B)(X+Y) Merge: and (⋀) …
Avail Expressions Using Bit Vectors 00 Boundary Condition Now must initialize OUT of blocks. Initial Condition? 1=avail 0=not (A+B)(X+Y) Merge: and (⋀) …
Initial Conditions? • What is needed so loops work? • Needs to be identify for merge function X ⋀ ? = X
Initial Conditions? • What is needed so loops work? • Needs to be identify for merge function X ⋀ 1 = X
Avail Expressions Using Bit Vectors 00 in[b]=⋀p pred(b)out[p] 1=avail 0=not (A+B)(X+Y) Merge: and ─ out=gen ⋁ (in ⋀ kill) 1. 11 (⋀) 2. 3. 11 11 4. ─ … gen and kill are constants! 11
Avail Expressions Using Bit Vectors 00 in[b]=⋀p pred(b)out[p] 1=avail 0=not (A+B)(X+Y) Merge: and ─ out=gen ⋁ (in ⋀ kill) 1. out1= ?? 11 (⋀) 2. 3. 11 11 4. … 11
Avail Expressions Using Bit Vectors 00 in[b]=⋀p pred(b)out[p] 1=avail 0=not (A+B)(X+Y) Merge: and ─ out=gen ⋁ (in ⋀ kill) 1. 11 out1= 11 out2= ?? (⋀) 2. 3. 11 11 4. … 11
Avail Expressions Using Bit Vectors 00 in[b]=⋀p pred(b)out[p] 1=avail 0=not (A+B)(X+Y) Merge: and ─ out=gen ⋁ (in ⋀ kill) 1. 11 out1= 11 out2= 00 ⋁ (??⋀ ??) (⋀) 2. 3. 11 11 4. … 11
Avail Expressions Using Bit Vectors 00 in[b]=⋀p pred(b)out[p] 1=avail 0=not (A+B)(X+Y) Merge: and ─ out=gen ⋁ (in ⋀ kill) 1. 11 out1= 11 out2= 00 ⋁ (11⋀ ??) (⋀) 2. 3. 11 11 4. … 11
Avail Expressions Using Bit Vectors 00 in[b]=⋀p pred(b)out[p] 1=avail 0=not (A+B)(X+Y) Merge: and ─ out=gen ⋁ (in ⋀ kill) 1. 11 out1= 11 out2= 00 ⋁ (11⋀ 01) (⋀) 2. 3. = 01 out3= ?? 01 11 11 4. … 11
Avail Expressions Using Bit Vectors 00 in[b]=⋀p pred(b)out[p] 1=avail 0=not (A+B)(X+Y) Merge: and ─ out=gen ⋁ (in ⋀ kill) 1. 11 out1= 11 out2= 00 ⋁ (11⋀ 01) (⋀) 2. 3. = 01 01 out3= 01 ⋁ (??⋀ ??) 11 11 4. … 11
Avail Expressions Using Bit Vectors 00 in[b]=⋀p pred(b)out[p] 1=avail 0=not (A+B)(X+Y) Merge: and ─ out=gen ⋁ (in ⋀ kill) 1. 11 out1= 11 out2= 00 ⋁ (11⋀ 01) (⋀) 2. 3. = 01 out3= 01 ⋁ (11⋀ 11) 01 11 11 = 11 4. … 11
Avail Expressions Using Bit Vectors 00 in[b]=⋀p pred(b)out[p] 1=avail 0=not (A+B)(X+Y) Merge: and ─ out=gen ⋁ (in ⋀ kill) 1. 11 out1= 11 out2= 00 ⋁ (11⋀ 01) (⋀) 2. 3. = 01 out3= 01 ⋁ (11⋀ 11) 01 11 11 = 11 out4= ?? 4. … 11
Avail Expressions Using Bit Vectors 00 in[b]=⋀p pred(b)out[p] 1=avail 0=not (A+B)(X+Y) Merge: and ─ out=gen ⋁ (in ⋀ kill) 1. 11 out1= 11 out2= 00 ⋁ (11⋀ 01) (⋀) 2. 3. = 01 out3= 01 ⋁ (11⋀ 11) 01 11 11 = 11 out4= 00 ⋁ (01⋀ 11) 4. … = 01 01 11
Constant Propagation • Along every path to point p, • variable v has same “known” value • Specialize computation at p based on v’s value
… -2 -1 0 1 2 ... The Lattice of CP Constant Propagation Hasse diagram • Lattice (V, ≤) • Top ⊤ • Not a Constant • Bottom ⊥ • Unknown
Some Lattice Theory • Generalized Meet Operator ⊔ • Used at “merge” points • Avail Expression: • Least Upper Bound (LUB) • X ⊔ Y = least element Z s.t. • X ≤ Z and Y ≤ Z
{a+b,c+d,e+f,a+17,b+18}, {a+b,c+d,e+f,a+17} {a+b,c+d,e+f,b+18} {a+b,c+d,a+17,b+18} {a+b,e+f,a+17,b+18} {c+d,e+f,a+17,b+18} {a+b,c+d,e+f} {a+b,c+d,b+18} {a+b,c+d,a+17} {a+b,e+f,a+17} {a+b,e+f,b+18} {a+b,a+17,b+18} {c+d,e+f,a+17} {c+d,e+f,b+18} {c+d,a+17,b+18} {e+f,a+17,b+18}, {a+b,c+d} {a+b,a+17} {c+d,e+f} {c+d,b+18} {e+f,b+18} {a+b,e+f} {a+b,b+18} {c+d,a+17} {e+f,a+17} {a+17,b+18} {a+b} {c+d} {e+f} {a+17} {b+18} { } Concrete Example: Available Expressions meet
… -2 -1 0 1 2 ... The Lattice of CP More Lattice Theory: Const Prop • ⊥ ≤ anything • ∀x ⊥ ⊔ x = x • anything ≤ ⊤ • ∀x x ⊔ ⊤= ⊤ • ∀x x ⊔ x = x
Relate to Constant Prop?! • i and j are integer values • i ⊔ i = i • i ⊔ j = ⊤ if i ≠ j
What is X in Block 3? 1. 2. 3.
What is X in Block 3? 1. 2. 3. x is ⊤
Top ⊤ versus Bottom ⊥ • ⊤ means definitely NOT a constant • Inputs are ⊤ • ⊥ means Undefined • Could be constant but we do not know!
Constant Propagation Problem • Variable v has constant value c at point p, iff ∀ paths from entry to p, the most recently assigned value for v is c
Constant Prop Example Meet Operator = ⊔ Identity= ? X ⊔ ? = X
Constant Prop Example Meet Operator = ⊔ Identify= ⊥ X ⊔ ⊥ = X Initial Condition?
Constant Prop Example Meet Operator = ⊔ Identify= ⊥ X ⊔ ⊥ = X Initial Condition Boundary Condition? XYZ ⊥⊥⊥ ⊥⊥⊥ ⊥⊥⊥ ⊥⊥⊥
Constant Prop Example ⊥⊥⊥ Meet Operator = ⊔ Identify= ⊥ X ⊔ ⊥ = X Initial Condition Boundary Condition XYZ 1. ⊥⊥⊥ 2. 3. ⊥⊥⊥ ⊥⊥⊥ 4. ⊥⊥⊥
Constant Prop Example ⊥⊥⊥ XYZ out1= ??? 1. ⊥⊥⊥ 2. 3. ⊥⊥⊥ ⊥⊥⊥ 4. ⊥⊥⊥
Constant Prop Example ⊥⊥⊥ XYZ out1= 1⊥⊥ out2= ??? 1. 1⊥⊥ ⊥⊥⊥ 2. 3. ⊥⊥⊥ ⊥⊥⊥ 4. ⊥⊥⊥
Constant Prop Example ⊥⊥⊥ XYZ out1= 1⊥⊥ out2= 023 out3= ??? 1. 1⊥⊥ ⊥⊥⊥ 2. 3. ⊥⊥⊥ ⊥⊥⊥ 023 4. ⊥⊥⊥
Constant Prop Example ⊥⊥⊥ XYZ out1= 1⊥⊥ out2= 023 out3= 12⊥ out4= ??? 1. 1⊥⊥ ⊥⊥⊥ 2. 3. ⊥⊥⊥ ⊥⊥⊥ 12⊥ 023 4. ⊥⊥⊥
Constant Prop Example ⊥⊥⊥ XYZ out1= 1⊥⊥ out2= 023 out3= 12⊥ out4= ⊤23 1. 1⊥⊥ ⊥⊥⊥ 2. 3. ⊥⊥⊥ ⊥⊥⊥ Does this make sense? 12⊥ 023 4. ⊥⊥⊥ ⊤23
Next Time • Static-Single Assignment (SSA Form) • Read Efficiently computing static single assignment form and the control dependence graph, Cytron et al. http://portal.acm.org/citation.cfm?id=115320