Docsity

Data-Flow Analysis: Proving Theorems and Reaching Definitions, Slides of Compilers

Partial preview of the text

Download Data-Flow Analysis: Proving Theorems and Reaching Definitions and more Slides Compilers in PDF only on Docsity!

Data-Flow Analysis

Proving Little Theorems Data-Flow Equations Major Examples

An Obvious Theorem

boolean x = true;

while (x) {

... // no change to x

}

  • Doesn’t terminate.
  • Proof: only assignment to x is at top, so x is always true.

Formulation: Reaching Definitions

  • Each place some variable x is assigned is a definition.
  • Ask: for this use of x, where could x last have been defined.
  • In our example: only at x=true.

Example: Reaching Definitions

5

d1: x = true

if x == true

d2 : a = 10

d (^2)

d (^1)

d (^1) d 2 d (^1)

Not Always That Easy

int i = 2; int j = 3;

while (i != j) {

if (i < j) i += 2; else j += 2;

}

  • We’ll develop techniques for this problem, but later …

The Flow Graph

8

d1: i = 2 d2: j = 3

if i != j

if i < j

d3: i = i+2 d4: j = j+

d 1 , d 2 , d 3 , d (^4)

d (^1) d 3 d (^4)

d (^2)

d 2 , d 3 , d (^4)

d d^1 , d^3 , d^4 d 1 , d 2 , d 3 , d 4 1 , d 2 , d 3 , d (^4)

Be Conservative!

  • (Code optimization only)
  • It’s OK to discover a subset of the opportunities to make some code-improving transformation.
  • It’s not OK to think you have an opportunity that you don’t really have.

Example: Be Conservative

boolean x = true;

while (x) {

... *p = false;...

}

  • Is it possible that p points to x?

Possible Resolution

  • Just as data-flow analysis of “reaching definitions” can tell what definitions of x might reach a point, another DFA can eliminate cases where p definitely does not point to x.
  • Example: the only definition of p is p = &y and there is no possibility that y is an alias of x.

Reaching Definitions Formalized

  • A definition d of a variable x is said to reach a point p in a flow graph if:
  1. Every path from the entry of the flow graph to p has d on the path, and
  2. After the last occurrence of d there is no possibility that x is redefined.

Data-Flow Equations --- (2)

  • Variables:
    1. IN(B) = set of definitions reaching the beginning of block B.
    2. OUT(B) = set of definitions reaching the end of B.

Data-Flow Equations --- (3)

  • Two kinds of equations:
    1. Confluence equations : IN(B) in terms of outs of predecessors of B.
    2. Transfer equations : OUT(B) in terms of of IN(B) and what goes on in block B.

Transfer Equations

  • Generate a definition in the block if its variable is not definitely rewritten later in the basic block.
  • Kill a definition if its variable is definitely rewritten in the block.
  • An internal definition may be both killed and generated.

Example: Gen and Kill

20

d 1 : y = 3 d 2 : x = y+z d 3 : *p = 10 d 4 : y = 5

IN = {d 2 (x), d 3 (y), d 3 (z), d 5 (y), d 6 (y), d 7 (z)}

Kill includes {d 1 (x), d 2 (x), d 3 (y), d 5 (y), d 6 (y),…}

Gen = {d 2 (x), d 3 (x), d 3 (z),…, d 4 (y)}

OUT = {d 2 (x), d 3 (x), d 3 (z),…, d 4 (y), d 7 (z)}