Lecture 015

Dataflow Analysis

Say if we want to optimize the following program

x = 1
if y:
  pass
else:
  pass
z = x

optimize to

x = 1
if y:
  pass
else:
  pass
z = 1

It is possible because in neither of the branch, x is assigned. But if only one of the branch assign new value to x, optimization will not work. However, if both branch do reassignment to the same value, we can still optimize.

The condition in which optimization is possible is not trivial to check. It is difficult for loops. Checking condition requires global dataflow analysis.

Global Optimization

Constant Propagation

Value Interpretation
\perp statement never execute
C X = constant
T X is not constant

Example figuring out whether we can propagate X

Example figuring out whether we can propagate X

To automatically analyze the program, we define

Rule 1

Rule 1

Rule 2

Rule 2

Rule 3

Rule 3

Rule 4

Rule 4

Rule 5

Rule 5

Rule 6: note that it has lower priority than rule 5

Rule 6: note that it has lower priority than rule 5

Rule 7: lower priority than rule 5

Rule 7: lower priority than rule 5

Rule 8

Rule 8

Example: initialize begging to T, and all other points to klzzwxh:0011, from top to bottom apply rules above to get the final analysis.

Example: initialize begging to T, and all other points to \perp, from top to bottom apply rules above to get the final analysis.

Loop Analysis

It is still possible to analyze rule by initially setting all variables to \perp.

A Optimizable Loop

A Optimizable Loop

Orderings

Ordering: we define \perp < c < T and so we can have operation lub() on them

Now we can summarize the rules to

C(s, x, in) = lib \{\C(p, x, out) | p \text{ is a predecessor of } s}

This ordering also shows that our algorithm must terminate (ie. no oscillation) because the value only increase. Therefore the algorithm cost is bounded to 4 \times \test{the number of program statements}

Liveness Analysis

Liveness: we want to be able to illuminate unused assignments (dead statement). A statement s with variable x is life if:

if a statement is not live, then its dead

Rule 1: if any later statement use klzzwxh:0020, klzzwxh:0021 is live

Rule 1: if any later statement use x, x is live

Rule 2: a statement is live with respect to klzzwxh:0023 if it reads klzzwxh:0024

Rule 2: a statement is live with respect to x if it reads x

Rule 3: a statement is dead with respect to klzzwxh:0026 if it write to klzzwxh:0027 (lower priority)

Rule 3: a statement is dead with respect to x if it write to x (lower priority)

Rule 4: a statement is unchanged with respect to klzzwxh:0029 if it does not mention klzzwxh:0030

Rule 4: a statement is unchanged with respect to x if it does not mention x

Since we initialize every point to false and true can never be changed to false. Our algorithm terminates.

Constant propagation is a forward analysis while liveness analysis is a backward analysis.

Table of Content