Presentation is loading. Please wait.

Presentation is loading. Please wait.

Code Optimization Data Flow Analysis

Similar presentations


Presentation on theme: "Code Optimization Data Flow Analysis"— Presentation transcript:

1 Code Optimization Data Flow Analysis

2 Data Flow Analysis (DFA)
x := y + 2 z := y * 3 1 General framework Can be used for various optimization goals Some terms Basic block A sequence of statements without branch Point Before or after each statement In a basic block, n statements  n+1 points Path A sequence of points representing a possible execution flow Forward and Backward analysis Forward: (123)(45); (123)(67) Backward: (54)(321) 2 3 t := x * a t := y * b 4 6 5 7

3 Reachability Analysis
Whether a definition reaches a statement Can also be considered as define-use relation analysis Example: x := y + z x is defined, y and z are used But where were y and z defined? During execution, only one definition reaches a variable But during static analysis, there could be multiple definitions that may have defined a variable Kill: A definition of x right before p1 is killed before p2 if:  path from p1 to p2,  another definition of x on the path Reach: A definition of x right before p1 reaches point p2 if:  a path from p1 to p2  x is not killed along this path p1, p2 are points on the control flow graph, x is a variable

4 Reachability Analysis
Gen and Kill sets for a basic block B Gen[B[: All definitions that have not been killed in the block Kill[B]: All definitions on the set of variables that are defined in the block B1 d1: i := m-1 d2: j := n d3: a := u1 Gen[B1] = {d1, d2, d3} Kill[B1] = {d4, d5, d6, d7} Gen[B2] = {d4, d5} Kill [B2] = {d1, d2, d7} Gen[B3] = {d6} Kill [B3] = {d3} Gen[B4] = {d7} Kill [B4] = {d1, d4} B2 d4: i := i+1 d5: j :=j – 1 if (j) B3 d6: a := a+j B4 d7: i := i+a

5 Reachability Analysis
In and Out sets for a basic block In[B] = ? (Initialized to ) Out[B] = Gen[B]  (In[B] – Kill[B]) Initialization: In[B1] =  Out[B1] = {d1, d2, d3} In[B2] =  Out[B2] = {d4, d5} In[B3] =  Out[B3] = {d6} In[B4] =  Out[B4] = {d7} B1 d1: i := m-1 d2: j := n d3: a := u1 B2 d4: i := i+1 d5: j :=j – 1 if (j) B3 d6: a := a+j B4 d7: i := i+a

6 Reachability Analysis
How to proceed? Consider path B1-B2-B3 and path B1-B2-B4 In[B] = Out[P], P is the predecessor of B on the path Out[B] = Gen[B]  (In[B] – Kill[B]) Initialization: In[B1] =  Out[B1] = {d1, d2, d3} In[B2] =  Out[B2] = {d4, d5} In[B3] =  Out[B3] = {d6} In[B4] =  Out[B4] = {d7} B1-B2-B3/B4 In[B1] =  Out[B1] = {d1, d2, d3} In[B2] = {d1, d2, d3} Out[B2] = {d3, d4, d5} In[B3] = {d3, d4, d5} Out[B3] = {d4, d5, d6} In[B4] = {d3, d4, d5} Out[B4] = {d3, d5, d7} B1 d1: i := m-1 d2: j := n d3: a := u1 B2 d4: i := i+1 d5: j :=j – 1 if (j) B3 d6: a := a+j B4 d7: i := i+a

7 Reachability Analysis
How to proceed? How about path B1-B2-B3-B2-B3 and path B1-B2-B3-B2-B4 In[B] = Out[P], P is the predecessor of B on the path Out[B] = Gen[B]  (In[B] – Kill[B]) B1-B2-B3/B4 In[B1] =  Out[B1] = {d1, d2, d3} In[B2] = {d1, d2, d3} Out[B2] = {d3, d4, d5} In[B3] = {d3, d4, d5} Out[B3] = {d4, d5, d6} In[B4] = {d3, d4, d5} Out[B4] = {d3, d5, d7} B1 d1: i := m-1 d2: j := n d3: a := u1 B1-B2-B3-B2-B3/B4 In[B2] = {d4, d5, d6} Out[B2] = {d4, d5, d6} In[B3] = {d4, d5, d6} Out[B3] = {d4, d5, d6} In[B4] = {d4, d5, d6} Out[B4] = {d4, d5, d6, d7} B2 d4: i := i+1 d5: j :=j – 1 if (j) B3 d6: a := a+j B4 d7: i := i+a

8 Reachability Analysis
All paths  Need to consider B1-B2-B3-B2-B3-B2-B3-B2-B3-…  Intractable Reachability Analysis Different results from different paths Reach: A definition of x right before p1 reaches p2 if  a path from p1 to p2  x is not killed along this path  Final result =  (result from each path) B1-B2-B3/B4 In[B1] =  Out[B1] = {d1, d2, d3} In[B2] = {d1, d2, d3} Out[B2] = {d3, d4, d5} In[B3] = {d3, d4, d5} Out[B3] = {d4, d5, d6} In[B4] = {d3, d4, d5} Out[B4] = {d3, d5, d7} Union In[B1] =  Out[B1] = {d1, d2, d3} In[B2] = {d1, d2, d3, d4, d5, d6} Out[B2] = {d3, d4, d5, d6} In[B3] = {d3, d4, d5, d6} Out[B3] = {d4, d5, d6} In[B4] = {d3, d4, d5, d6} Out[B4] = {d3, d5 , d6, d7} B1-B2-B3-B2-B3/B4 In[B2] = {d4, d5, d6} Out[B2] = {d4, d5, d6} In[B3] = {d4, d5, d6} Out[B3] = {d4, d5, d6} In[B4] = {d4, d5, d6} Out[B4] = {d4, d5, d6, d7}

9 Reachability Analysis
A different way to consider how to proceed Consider all paths  In[B] =  Out[P], for all P, P is a predecessor of B Iterate till no more changes B1-B2-B3/B4 In[B1] =  Out[B1] = {d1, d2, d3} In[B2] = {d1, d2, d3} Out[B2] = {d3, d4, d5} In[B3] = {d3, d4, d5} Out[B3] = {d4, d5, d6} In[B4] = {d3, d4, d5} Out[B4] = {d3, d5, d7} In[B] =  Out[P] In[B1] =  Out[B1] = {d1, d2, d3} In[B2] = {d1, d2, d3, d4, d5, d6} Out[B2] = {d3, d4, d5, d6} In[B3] = {d3, d4, d5, d6} Out[B3] = {d4, d5, d6} In[B4] = {d3, d4, d5, d6} Out[B4] = {d3, d5 , d6, d7} B1 d1: i := m-1 d2: j := n d3: a := u1 B2 d4: i := i+1 d5: j :=j – 1 if (j) B3 d6: a := a+j B4 d7: i := a+j

10 Data Flow Analysis: MFP and MOP
Maximal fixed point (MFP) Consider a merging operations E.g., In[B] =  Out[P], for all P, P is a predecessor of B Iteratively update till no more changes Meet over all paths (MOP) Consider all paths, Final result =  (result from each path) Compare MFP and MOP Intuitively, MOP is definitely correct But MOP is intractable Does MFP = MOP  Not always If MFP = MOP, then using MFP is sufficient Under what condition MFP = MOP ??? Does MFP always terminate ???

11 DFA Framework Important components in DFA: (𝐷, 𝑉, 𝐹, ∆)
𝐷: Direction for the analysis 𝑉: Domain of DFA Possible values for In and Out sets Generally, 𝑉 is a power set of a basic set E.g., For reachability analysis, basic element set ={ all definitions } and In/Out sets are subsets of the basic set 𝐹: Transformation function, 𝑉× 𝐵 ∗ →𝑉 When passing through a basic block B∈ 𝐵 ∗ , how the In set is transformed into the Out set? 𝐵 ∗ is the set of all basic blocks in the control flow graph ∆: Merge operation (or Meet operation), 𝑉 𝑁 →𝑉, 𝑁≥2 When multiple paths meet at a basic block, how to compute the In set from the Out sets of the predecessors/successors of the basic block in forward/backward DFA?

12 DFA Framework Important components in DFA: (𝐷, 𝑉, 𝐹, ∆) Lattice
Elements in 𝑉 form a lattice by the ∆ operation With an upper bound Top With a lower bound Bottom Example ∆ = , Bottom = , Top = basic element set ∆ = , Top = , Bottom = basic element set Initialization = Bottom {a,b,c} {b,c} {a,c} {a,b} {b} {c} {a}   {a} {b} {c} {a,b} {a,c} {b,c} {a,b,c}

13 DFA Framework MFP (maximal fixed point) algorithm
Blocks in control flow graph: { 𝐵 0 , 𝐵 1 , …} 𝐵 0 is the entry block to the CFG Algorithm for forward analysis worklist = { 𝐵 0 }; In( 𝐵 0 ) = ; while (worklist is not empty) { remove block B from worklist; Out(B) = 𝐹 ( In(B), B ); foreach P[i], P[i] is a successor of B { In(P[i]) = In(P[i]) ∆ Out(B); if ( In P[i] has changed) then add P[i] to worklist; } } Algorithm for backward analysis is the same, except that: worklist should be initialized to the set of exit blocks In/Out is for the last/first point of the basic block

14 DFA Framework MOP (meet over paths) algorithm
Blocks in control flow graph: { 𝐵 0 , 𝐵 1 , …} 𝐵 0 is the entry block to the CFG Compute In[B], for a basic block in the CFG Algorithm for forward analysis 𝑃𝑆[B] = The set of all paths from the first point in 𝐵 0 to the first point in B foreach 𝑝 𝑖 ∈𝑃𝑆, 𝑝 𝑖 =( 𝐵 0 → 𝐵 𝑖 1 → 𝐵 𝑖 2 →…→ 𝐵 𝑖 𝑘 ) do In(B, 𝑝 𝑖 ) = 𝐹(…𝐹(𝐹(In( 𝐵 0 ), 𝐵 0 ), 𝐵 𝑖 1 ), …), 𝐵 𝑖 𝑘 ); In(B) = ∆ In(B, 𝑝 𝑖 ), for all 𝑖 Algorithm for backward analysis is the same, except that: In/Out is for the last/first point of the basic block

15 DFA Framework MFP and MOP 𝐵 𝑋 𝐵 0 𝐵 𝑀 𝐵 𝐵 𝑌
MOP: Out(B) = 𝐹 𝐾 𝑥 ∆ 𝐹 𝐾 (𝑦) MFP: Out(B) = 𝐹 𝐾 (𝑥∆𝑦) MFP = MOP iff 𝐹 𝐾 𝑥∆𝑦 = 𝐹 𝐾 (𝑥)∆ 𝐹 𝐾 (𝑦) This is the distributive property If the transformation function 𝐹 over the merge operator ∆ is distributive, then MFP = MOP  Can use MFP for DFA 𝐹 𝐾 (𝑥) 𝐵 𝑋 Out( 𝐵 𝑋 ) = 𝑥 𝐹 𝐾 (𝑥∆𝑦) 𝐵 0 𝐵 𝑀 𝐵 In( 𝐵 𝑀 ) = 𝑥∆𝑦 No branch 𝐵 𝑌 Out( 𝐵 𝑌 ) = 𝑦 𝐹 𝐾 (𝑦)

16 DFA Framework Can we do better than MOP? Ideal algorithm MFP = MOP
Only consider executable paths, not all paths MOP is intractable Ideal is even worse MFP = MOP Many analysis algorithms satisfies distributive property So, most of the DFA analysis uses MFP There are exceptions, constant folding 𝐵 𝑖 𝐵 𝑘 𝐵 0 if 𝑐 if 𝑐 𝐵 𝐵 𝑗 𝐵 𝑙

17 DFA Framework MFP termination?
If 𝑉 is finite, or the height of 𝑉 is finite If 𝐹 is idempotent and monotonic If 𝑥  𝑦 then 𝐹 𝑥,𝐵  𝐹 𝑦,𝐵 , for any 𝑥,𝑦∈𝑉 If ∆ is idempotent and monotonic 𝑥  𝑦 then 𝑥 ∆ 𝑧  𝑦 ∆ 𝑧 From 2, 3, Out(B) is monotonic over the progressive steps of MFP Out(B) has to monotonically go up the lattice or stay the same If Out(B) stays the same, for all B in B*, then MFP terminates If Out(B), for some B in B*, goes up in 𝑉’s lattice, since 𝑉’s lattice has a finite height, MFP has to terminate

18 DFA Framework MFP termination? Finite V, idempotent F and ∆
E.g., F(1) = 2; F (2) = 3, F(3) = 1 Will not terminate If ∆ is idempotent, commutative, associative 𝑥 ∆ 𝑥=𝑥 𝑥 ∆ 𝑦=𝑦 ∆ 𝑥 𝑥 ∆ 𝑦 ∆ 𝑧=𝑥 ∆ 𝑦 ∆ 𝑧 Let 𝑥∆𝑎= 𝑥 ′ , 𝑦∆𝑏=𝑦′, we can have 𝑥′∆y′=(𝑥∆𝑎)∆(𝑦∆𝑏)=(𝑥∆𝑦)∆(𝑎∆𝑏), but this is not useful, (𝑥∆𝑦)∆(𝑎∆𝑏) can be anything, not necessarily a superset or subset of (𝑥∆𝑦)

19 Reachability in DFA Framework
Reachability analysis 𝑉: Domain of DFA Basic set = the set of all definitions in the program 𝑉 is the power set of the basic set  𝑉 is finite 𝐷: Direction = forward ∆: Merge operation Union Idempotent & monotonic 𝐹: Transformation function Out[B] = Gen[B]  (In[B] – Kill[B]), Gen/Kill are constants 𝑥  (𝑥∪𝛿)  Gen[B]  (𝑥– Kill[B])  Gen[B]  ((𝑥∪𝛿)– Kill[B])  𝐹 is monotonic (Gen[B]  𝑥 – Kill[B]) ∪ ( Gen[B]  𝑦 – Kill[B]) = Gen[B]  ( 𝑥∪𝑦 ) – Kill[B]  𝐹 is distributive  Reachability MFP = Reachability MOP 𝐵 𝑋 In(𝐵) = 𝑥 Out(𝐵) = 𝐹(𝑥)∪𝐹(𝑦) In(𝐵) = 𝑥∪𝑦 𝐵 Out(𝐵) = 𝐹(𝑥∪𝑦) 𝐵 𝑌 In(𝐵) = 𝑦

20 Common Subexpressions
Global common subexpression analysis Consider a CFG with multiple blocks DFA: Available expression analysis Similar to reachability analysis Reachability of expressions <x op y>, instead of definitions Consider expensive operations Common subexpression elimination Local common subexpression analysis Within a basic block Construct a DAG for the subexpressions Will be covered later a := b + c x := b + c

21 Available Expressions in DFA Framework
Definitions Gen[B]: Definition z := x op y, but focus on x op y Kill[B]: Definition x := … kills x op y and y op x All definitions for x or for y kill subexpression defined in z := x op y How about the definition for z? Should z := … kill z := x op y? DFA components 𝐷: Direction = forward 𝑉: Domain of DFA Basic set = the set of all expressions in the program 𝑉 is the power set of the basic set  𝑉 is finite

22 Available Expressions in DFA Framework
DFA components ∆: Merge operation In[B] =  Out[P], for all P, P is a predecessor of B A subexpression is available only if it is generated on all paths and it is not killed on any path Clearly idempotent and monotonic 𝑥∩𝛿  𝑥  ( 𝑥∩𝛿 ∩𝑦)  (𝑥∩𝑦)  ∆ is monotonic 𝐹: Transformation function Out[B] = Gen[B]  (In[B] – Kill[B]) 𝑥∩𝛿  𝑥  Gen[B]  ( 𝑥∩𝛿 – Kill[B])  Gen[B]  (𝑥– Kill[B])  𝐹 is monotonic (Gen[B]  𝑥 – Kill[B]) ∩ ( Gen[B]  𝑦 – Kill[B]) = Gen[B]  (𝑥∩𝑦) – Kill[B]  𝐹 is distributive  Available expression analysis: MFP = MOP

23 Available Expression Analysis
Read (y, b) t := b * y a := b + c In = {} Out = {t:=b*y, a:=b+c} In = {t:=b*y, a:=b+c} Out = {t:=b*y, a,u:=b+c, s:=x+y} b:= x+y kills a:=b+c and t:=b*y If (a) In = {t:=b*y, a:=b+c} Out = {b:=x+y} B4 b+c and b*y are in B3 but not in B4, take  x+y is in both B3&B4 B3 s := x + y u := b + c b := x + y In = {s,b:=x+y} Out = {s,b,w:=x+y, a:=b+c} B5 v := b + c w := x + y

24 Available Expression Analysis -- Initialization
In[B1] =  This is by computation from  block If set initial In[B1] = Bottom  Incorrect result Initialization = Bottom = { all basic elements } B1 Read (y, b) t := b * y a := b + c If In[B6] is initialized to   Out[B6] =   In[B6] =   …  No expression can go through If (a) B4 B3 s := x + y u := b + c b := x + y B6 f = g + h B5 v := b + c w := x + y

25 Common Subexpression Elimination
Additional step after DFA Reachability analysis is only an analysis, no optimization follows Same for available expression analysis To optimize, should perform common subexpressions elimination Common expression elimination If <x op y> reaches s  b := x op y and s is in B For all s’, s’  z := x op y and s’ reaches s, do Replace s’ by t1 := x op y; z := t1 t1 should be the same for all s’ Replace s by b := t1

26 Common Subexpression Elimination
Read (y, b) t := b * y a := b + c 2 definitions for x+y reaches w:=x+y replace all prior definitions by t1:=x+y; ?:=t1; In B5, replace w:=x+y by w:=t1 If (a) B3 B4 t1 := x + y s := t1 u := b + c s := x + y u := b + c t1 := x + y b := t1 b := x + y B5 In = {s,b:=x+y} Out = {x,b,w:=x+y, a:=b+c} B5 v := b + c w := t1 v := b + c w := x + y s := x + y reaches w := x + y; Why need t1 assignment instead of w := s? This example makes it clear!

27 Common Subexpression Elimination
Modification Informal Read (y, b) t := b * y a := b + c In = {t:=b*y, a:=b+c} Out = {t:=b*y, a,u:=b+c, s:=x+y} If (a) B3 B4 a:=b+c reaches here only one definition, so replace u:=b+c by u:=a s := x + y u := a s := x + y u := b + c b := x + y B5 v := b + c In this case, no need to use t1, resulting in an extra copy!  Modify Merge operation to accommodate both types of eliminations In[B] =   Out[P], for all predecessor P   considers the subexpressions <x op y>   considers the set of variables the expression is assigned to

28 Common Subexpression Elimination
Another case For some expensive operations, it is better to reuse the result even if it is only from one branch Add the same operation on the other branch Probability for visiting left = p Cost of b^c = w Cost for copying u Original cost = wp + w New cost = (w+u)*p + w*(1–p) + u Saving = wp – u(1+p) t1 := b^c x = t1 x := b^c t1 = b^c y := t1 y := b^c

29 Copy Propagation Eliminate unnecessary copies Example Convert to:
s: x := y u: z := x * 3 Convert to: u: z := y * 3 Reduce a copy operation Especially important in one pass code generation process

30 Copy Propagation When can a copy be eliminated Basic definitions
For a copy statement s: x := y and another statement u: uses x (e.g., u: b := x op c) s reaches u on all paths s is the only definition for x that reaches u No definitions of y on the path from s to u either Basic definitions Gen[B] All copy statements generated in B and not killed in B Kill[B] Both x := … and y := … kill copy statement x := y

31 Copy Propagation DFA components 𝐷: Direction = forward
𝑉: Domain of DFA Basic set = the set of all copy definitions in the program 𝑉 is the power set of the basic set  𝑉 is finite ∆: Merge operation In[B] =  Out[P], for all P, P is a predecessor of B A copy statement is alive only if it is alive on all paths As long as it is killed on one path, the substitution is not allowed  has been analyzed to be idempotent & monotonic 𝐹: Transformation function Out[B] = Gen[B]  (In[B] – Kill[B]) Same as available expression analysis, 𝐹 is monotonic & distributive  Copy propagation: MFP = MOP

32 Copy Propagation Optimization after DFA
If x := y reaches a statement and the statement uses x Then substitute x by y How to eliminate the copy statement? Depends on whether all uses of x are substituted by y Cannot be eliminate x := y by copy propagation alone x := y may be eliminated during dead code elimination

33 Copy Propagation B1 d1: Read (y) If (y) B2 d2: x := y d3: c := 2 * x
d1 also kills d5 but d5 is not a copy stmt B1 d1: Read (y) If (y) Gen[B1] = {} In[B1] = {} Kill[B1] = {d2} Out[B1] = {} Gen[B2] = {d2} In[B2] = {} Kill[B2] = {} Out[B2] = {d2} B2 d2: x := y d3: c := 2 * x If (c) d2 reaches here Replace x by y Gen[B3] = {} In[B3] = {d2} Kill[B3] = {} Out[B3] = {d2} B3 d4: d := c + x Gen[B4] = {} In[B4] = {d2} Kill[B4] = {d2} Out[B4] = {} B4 d5: y := c * y Use  Gen[B5] = {} In[B5] = {} Kill[N5] = {} Out[B4] = {} B5 print (y)

34 Copy Propagation B1 d1: Read (y) If (y) B2 d2: x := y d3: c := 2 * x
Gen[B1] = {} In[B1] = {} Kill[B1] = {d2} Out[B1] = {} Gen[B2] = {d2} In[B2] = {} Kill[B2] = {} Out[B2] = {d2} B2 d2: x := y d3: c := 2 * x If (c) Use  Gen[B3] = {} In[B3] = {} Kill[B3] = {} Out[B3] = {} B3 d4: d := c + x Gen[B4] = {} In[B4] = {d2} Kill[B4] = {d2} Out[B4] = {} d2 does not reach here B4 d5: y := c * y Gen[B5] = {} In[B5] = {} Kill[N5] = {} Out[B4] = {} B5 print (y)

35 Constant Propagation and Folding
Concept Should have been clear from optimization introduction When can a variable be substituted by a constant When multiple “definitions” reach a “use” point of a variable If the definitions derive to the same constant for the variable, then the variable at the “use” point can be replaced by the constant If they are different, then the definitions are killed Basic definitions Gen[B] All definitions that can be statically evaluated to a constant Not killed in B Kill[B] for “x := constant” If x := … is not a constant assignment, then it kills x := constant x := constant2 kills x := constant1, if constan1 ≠ constant2

36 Constant Propagation and Folding
DFA components 𝐷: Direction = forward 𝑉: Domain of DFA Basic set = the set of all variables with some potential constant def There can be infinite number of constants Basic set is infinite  𝑉 is infinite ∆: Merge operation In[B] =  Out[P], for all P, P is a predecessor of B A variable can be assigned to a constant at compilation time only if such assignment is valid alone all paths 𝐹: Transformation function Out[B] = Gen[B]  (In[B] – Kill[B])  Copy propagation: MFP MOP Why??? Why in previous proof, F is distributive, but here it is not? Because Gen[B] here is not a constant, it depends on In[B] Because constants propagate

37 Constant Folding Example
If b > 0 Constant folding y := 5 Out: {x=3} In: {x=3} In: {x=3} y := x+2 x := 4 y := 0 x := x+1 Out: {x=4, y=5} Out: {x=4, y=0} In: {x=4} Constant folding a := 8 Y has inconsistent values a := 2*x b := y/x no constant folding since y = ? is gone Out: {x=4, a=8}

38 Constant Folding Example
If b > 0 Constant folding y := 5 Out: {x=3} In: {x=3} In: {x=3} y := x+2 x := 4 y := 0 x := x+1 Out: {x=4, y=5} Out: {x=4, y=0} In: {x=4} a := 2*x b := y/x Out: {x=4, a=8}

39 Constant Folding (MFP ≠ MOP)
x := 3 If b > 0 Out: {x=3} In: {x=3} In: {x=3} y := x+2 x := 4 y := 3 x := x+3 Out: {x=6, y=3} Out: {x=4, y=5} In: {} x, y have different constants from different paths z := x + y b := x / y Out: {}

40 Constant Folding (MFP ≠ MOP)
x := 3 If b > 0 Out: {x=3} In: {x=3} In: {x=3} y := x+2 x := 4 y := 3 x := x+3 Out: {x=6, y=3} Out: {x=4, y=5} In: {} z := x + y b := x / y Out: {} Out: { z=9, b=2 } Evaluate 2 paths separately, take intersection afterwards  𝐹 no longer satisfies distributivity!!! Why???

41 DFA Framework Again Safety and precision 𝐵 𝑋 𝐵 0 𝐵 𝑀 𝐵 𝐵 𝑌
Depend on the analysis cases Case: Something is alive only if it is alive along all paths Safety: A missing path  Unsafe Precision: An additional hidden path  Safe, but less precise In  case: Early merge + F is monotonic  𝐹 𝐾 𝑥∩𝑦  ( 𝐹 𝐾 𝑥 ∩ 𝐹 𝐾 𝑦 )  As though added a hidden path  Safe  Constant folding MFP is safe  Can use MFP 𝐹 𝐾 (𝑥) 𝐵 𝑋 Out( 𝐵 𝑋 ) = 𝑥 𝐹 𝐾 (𝑥∆𝑦) 𝐵 0 𝐵 𝑀 𝐵 In( 𝐵 𝑀 ) = 𝑥∆𝑦 No branch 𝐵 𝑌 Out( 𝐵 𝑌 ) = 𝑦 𝐹 𝐾 (𝑦)

42 Dead Code Elimination Dead code Dead code elimination
A definition for a variable x is dead if it is not used afterwards Either not used from the definition point to the end of the program Or not used till the point that a new definition is given Dead code elimination If a variable is defined and is not alive afterwards, then the definition is useless and can be eliminated Perform liveliness analysis to identify dead code Eliminate the dead code

43 Liveliness Analysis Compute the set of live variables
A variable x defined at point p1 is alive as long as there exists a point p2 such that x is not killed at least on one path from p1 to p2 Analysis has to be done backwards Basic definitions 𝐷: Direction = backward In[B]/Out[B] is the end/beginning point of block B Use[B] All variables that are used and are not killed by a definition Kill[B] x := … kills a live variable x

44 Liveliness Analysis DFA components 𝐷: Direction = backwards
𝑉: Domain of DFA Basic set = the set of all variables in the program 𝑉 is the power set of the basic set  𝑉 is finite ∆: Merge operation In[B] =  Out[A], for all A, A is a successor of B A variable is alive as long as it is alive alone one path 𝐹: Transformation function Out[B] = Use[B]  (In[B] – Kill[B])  Copy propagation: MFP = MOP Same proof as reachability analysis (the same union case)

45 Liveliness Analysis B1 Out[B1] = {x, y, z, c, d} In[B1] = {x, y, z, d}
if (c) x := y + 1 y := 2 * z if (d) Out[B2] = {y, z, d} In[B2] = {y, z} B2 B3 x := y + z z := x * 2 Out[B3] = {y, z} In[B3] = {} Out[B4] = {} In[B4] = {} z := 1 B4 B5 z := x print z Out[B5] = {x} In[B5] = {} Initialization: Live set at all exits = {}

46 Liveliness Analysis B1 Out[B1] = {x, y, z, c, d} In[B1] = {x, y, z, d}
In[B1] = {x, y, z, c, d} if (c) x := y + 1 y := 2 * z if (d) Out[B2] = {y, z, d} In[B2] = {y, z} Out[B2] = {y, z, c, d} In[B2] = {x, y, z, c, d} B2 B3 x := y + z z := x * 2 Out[B3] = {y, z} In[B3] = {} Out[B3] = {y, z, c, d} In[B3] = {x, y, c, d} Out[B4] = {} In[B4] = {} Out[B4] = {x, y, c, d} In[B4] = {x, y, z, c, d} z := 1 B4 B5 z := x print z Out[B5] = {x} In[B5] = {}

47 Dead Code Elimination B1 Out[B1] = {x, y, z, c, d}
In[B1] = {x, y, z, c, d} if (c) x := y + 1 y := 2 * z if (d) Out[B2] = {y, z, c, d} In[B2] = {x, y, z, c, d} B2 {x, z, c, d} {x, y, z, c, d} B3 x := y + z z := x * 2 Out[B3] = {y, z, c, d} In[B3] = {x, y, c, d} {x, y, c, d} Out[B4] = {x, y, c, d} In[B4] = {x, y, z, c, d} z := 1 B4 B5 z := x print z Out[B5] = {x} In[B5] = {} {z} definition z := x*2 is not used afterwards  dead code  remove

48 Code Optimization -- Summary
Read Chapter 9 Sections 9.2, 9.3, 9.4 Optimization of intermediate code Data flow analysis based on CFG Data flow analysis framework MFP and MOP Reachability analysis Common subexpression elimination Copy propagation Constant propagation and constant folding Dead code elimination


Download ppt "Code Optimization Data Flow Analysis"

Similar presentations


Ads by Google