Proving Little Theorems Data-Flow Equations Major Examples
description
Transcript of Proving Little Theorems Data-Flow Equations Major Examples
![Page 1: Proving Little Theorems Data-Flow Equations Major Examples](https://reader035.fdocuments.in/reader035/viewer/2022081604/568166d6550346895ddaed73/html5/thumbnails/1.jpg)
Data-Flow AnalysisProving Little TheoremsData-Flow EquationsMajor Examples
Jeffrey D. UllmanStanford University
![Page 2: Proving Little Theorems Data-Flow Equations Major Examples](https://reader035.fdocuments.in/reader035/viewer/2022081604/568166d6550346895ddaed73/html5/thumbnails/2.jpg)
2
An Obvious Theoremboolean x = true;while (x) { . . . // no change to x} Doesn’t terminate. Proof: only assignment to x is at top, so x is
always true.
![Page 3: Proving Little Theorems Data-Flow Equations Major Examples](https://reader035.fdocuments.in/reader035/viewer/2022081604/568166d6550346895ddaed73/html5/thumbnails/3.jpg)
3
As a Flow Graph
x = true
if x == true
“body”
![Page 4: Proving Little Theorems Data-Flow Equations Major Examples](https://reader035.fdocuments.in/reader035/viewer/2022081604/568166d6550346895ddaed73/html5/thumbnails/4.jpg)
4
Formulation: Reaching Definitions
Each place some variable x is (or could be) assigned is a definition of that variable.
Ask: For this use of x, where could x last have been defined?
In our example: only at x = true.
![Page 5: Proving Little Theorems Data-Flow Equations Major Examples](https://reader035.fdocuments.in/reader035/viewer/2022081604/568166d6550346895ddaed73/html5/thumbnails/5.jpg)
5
Example: Reaching Definitions
d1: x = true
if x == true
d2: a = 10
d2
d1
d1d2d1
![Page 6: Proving Little Theorems Data-Flow Equations Major Examples](https://reader035.fdocuments.in/reader035/viewer/2022081604/568166d6550346895ddaed73/html5/thumbnails/6.jpg)
6
Clincher Since at x == true, d1 is the only definition
of x that reaches, it must be that x is true at that point.
The conditional is not really a conditional and we are left with:
d1: x = true
d2: a = 10
![Page 7: Proving Little Theorems Data-Flow Equations Major Examples](https://reader035.fdocuments.in/reader035/viewer/2022081604/568166d6550346895ddaed73/html5/thumbnails/7.jpg)
7
Not Always That Easyint i = 2; int j = 3;while (i != j) { if (i < j) i += 2; else j += 2;} Do we ever get out of the loop? We’ll develop techniques for this problem, but
later …
![Page 8: Proving Little Theorems Data-Flow Equations Major Examples](https://reader035.fdocuments.in/reader035/viewer/2022081604/568166d6550346895ddaed73/html5/thumbnails/8.jpg)
8
The Flow Graph
d1: i = 2d2: j = 3
if i != j
if i < j
d4: j = j+2d3: i = i+2d2, d3 d1, d4
d1, d2
d1, d2
d1, d2 d1, d2
d3, d4
, d3, d4
, d3, d4
, d3, d4
, d4
, d3
![Page 9: Proving Little Theorems Data-Flow Equations Major Examples](https://reader035.fdocuments.in/reader035/viewer/2022081604/568166d6550346895ddaed73/html5/thumbnails/9.jpg)
9
DFA Is Sufficient Only In this example, i can be defined in two
places, and j in two places. No obvious way to discover that i!=j is
always true. But OK, because reaching definitions is
sufficient to catch most opportunities for constant folding (replacement of a variable by its only possible value).
![Page 10: Proving Little Theorems Data-Flow Equations Major Examples](https://reader035.fdocuments.in/reader035/viewer/2022081604/568166d6550346895ddaed73/html5/thumbnails/10.jpg)
10
Here, It Helps to Be Conservative 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.
![Page 11: Proving Little Theorems Data-Flow Equations Major Examples](https://reader035.fdocuments.in/reader035/viewer/2022081604/568166d6550346895ddaed73/html5/thumbnails/11.jpg)
11
Exampleboolean x = true;while (x) { . . . *p = false; . . .} Is it possible that p points to x? We must assume so, unless we can prove
otherwise.
![Page 12: Proving Little Theorems Data-Flow Equations Major Examples](https://reader035.fdocuments.in/reader035/viewer/2022081604/568166d6550346895ddaed73/html5/thumbnails/12.jpg)
12
As a Flow Graph
d1: x = true
if x == true
d2: *p = false
d1
d2
Anotherdef of x
![Page 13: Proving Little Theorems Data-Flow Equations Major Examples](https://reader035.fdocuments.in/reader035/viewer/2022081604/568166d6550346895ddaed73/html5/thumbnails/13.jpg)
13
Possible Resolution Just as data-flow analysis of “reaching
definitions” can tell what definitions of x might reach a point, another DFA can discover 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.
![Page 14: Proving Little Theorems Data-Flow Equations Major Examples](https://reader035.fdocuments.in/reader035/viewer/2022081604/568166d6550346895ddaed73/html5/thumbnails/14.jpg)
14
Reaching Definitions Formalized
Points in a flow graph are the beginnings and ends of blocks.
Can use one block per statement, so every statement has its own point.
A definition d of a variable x is said to reach a point p in a flow graph if:
1. There is some path from the entry of the flow graph to p that has d on the path, and
2. After the last occurrence of d, x might not be redefined.
![Page 15: Proving Little Theorems Data-Flow Equations Major Examples](https://reader035.fdocuments.in/reader035/viewer/2022081604/568166d6550346895ddaed73/html5/thumbnails/15.jpg)
15
Data-Flow Equations Variables:
1. IN(B) = set of definitions reaching the beginning of block B.
2. OUT(B) = set of definitions reaching the end of B.
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 IN(B) and
what goes on in block B.
![Page 16: Proving Little Theorems Data-Flow Equations Major Examples](https://reader035.fdocuments.in/reader035/viewer/2022081604/568166d6550346895ddaed73/html5/thumbnails/16.jpg)
16
Confluence EquationsIN(B) = ∪predecessors P of B OUT(P)
P2
B
P1
{d1, d2, d3}
{d2, d3}{d1, d2}
![Page 17: Proving Little Theorems Data-Flow Equations Major Examples](https://reader035.fdocuments.in/reader035/viewer/2022081604/568166d6550346895ddaed73/html5/thumbnails/17.jpg)
17
Transfer Equations Generate (“Gen”) a definition in the block if its
variable is not surely rewritten later in the basic block.
Kill a definition if its variable is surely rewritten in the block.
An internal definition may be both killed and generated. Example: x definitely defined at d and later redefined
at d’. d kills d’, and vice-versa, but only d’ is Gen’d. For any block B:
OUT(B) = (IN(B) – Kill(B)) ∪ Gen(B)
![Page 18: Proving Little Theorems Data-Flow Equations Major Examples](https://reader035.fdocuments.in/reader035/viewer/2022081604/568166d6550346895ddaed73/html5/thumbnails/18.jpg)
18
Example: Gen and Kill
d1: y = 3 d2: x = y+zd3: *p = 10d4: y = 5
IN = {d2(x), d3(y), d3(z), d5(y), d6(y), d7(z)}
Kill includes {d1(y), d2(x),d3(y), d5(y), d6(y),…}
Gen = {d2(x), d3(x), d3(z),…, d4(y)}
OUT = {d2(x), d3(x), d3(z),…, d4(y), d7(z)}
![Page 19: Proving Little Theorems Data-Flow Equations Major Examples](https://reader035.fdocuments.in/reader035/viewer/2022081604/568166d6550346895ddaed73/html5/thumbnails/19.jpg)
19
Iterative Solution to Equations
For an n-block flow graph, there are 2n equations in 2n unknowns.
Alas, the solution is not unique. Standard theory assumes a field of
constants; sets are not a field. Use iterative solution to get the least
fixedpoint. Identifies any def that might reach a point.
![Page 20: Proving Little Theorems Data-Flow Equations Major Examples](https://reader035.fdocuments.in/reader035/viewer/2022081604/568166d6550346895ddaed73/html5/thumbnails/20.jpg)
20
Iterative Solution – (2)IN(entry) = ∅;for (each block B) do OUT(B)= ∅;
while (changes occur) do for (each block B) do { IN(B) = ∪predecessors P of B OUT(P); OUT(B) = (IN(B) – Kill(B)) ∪ Gen(B); }
![Page 21: Proving Little Theorems Data-Flow Equations Major Examples](https://reader035.fdocuments.in/reader035/viewer/2022081604/568166d6550346895ddaed73/html5/thumbnails/21.jpg)
21
Example: Reaching Definitions
d1: x = 5
if x == 10
d2: x = 15
B1
B3
B2
IN(B1) = {}OUT(B1) = {
OUT(B2) = {
OUT(B3) = {
d1}
IN(B2) = {
d1,d1,
IN(B3) = {
d1,d2}
d2}d2}
d2}
Gen(B1) = {d1}Kill(B1) = {d1, d2}Gen(B2) = { }Kill(B2) = { }
Gen(B3) = {d2}Kill(B3) = {d1, d2}
Interesting question: What if d2 were x = 10?
![Page 22: Proving Little Theorems Data-Flow Equations Major Examples](https://reader035.fdocuments.in/reader035/viewer/2022081604/568166d6550346895ddaed73/html5/thumbnails/22.jpg)
22
Aside: Notice the Conservatism Uses the most conservative assumption about
when a definition is killed or gen’d. Gen if possible definition; Kill only when sure.
Also the conservative assumption that any path in the flow graph can actually be taken.
Fine, as long as the optimization is triggered by limitations on the set of RD’s, not by the assumption that a def reaches.
![Page 23: Proving Little Theorems Data-Flow Equations Major Examples](https://reader035.fdocuments.in/reader035/viewer/2022081604/568166d6550346895ddaed73/html5/thumbnails/23.jpg)
23
Conservatism – (2) Example: (Nonconservative application) Catch
use-before-def by inserting dummy defs at the entry and seeing if they reach.
What problem discussed by JW in the first lecture does this look like?
d0: x = ??
use of x
No certaindefinitionof x
![Page 24: Proving Little Theorems Data-Flow Equations Major Examples](https://reader035.fdocuments.in/reader035/viewer/2022081604/568166d6550346895ddaed73/html5/thumbnails/24.jpg)
24
Why It Works A definition d reaches the beginning of block B
if there is some path from the entry to B along which d appears and is not later killed.
Do an induction along the path from d to B that d is in IN and OUT of each block along the path.
Key points:1. Transfer function passes d from IN to OUT if d is
not killed.2. Confluence = union, so d in OUT at any predecessor
-> d in IN for the successor block.
![Page 25: Proving Little Theorems Data-Flow Equations Major Examples](https://reader035.fdocuments.in/reader035/viewer/2022081604/568166d6550346895ddaed73/html5/thumbnails/25.jpg)
25
Available Expressions An expression x+y is available at a point if
whatever path has been taken to that point from the entry, x+y has surely been evaluated, and neither x nor y has even possibly been redefined.
Useful for global common-subexpression elimination.
The equations for AE are essentially the same as for RD, with one exception: Confluence of paths involves intersection of sets of
expressions rather than union.
![Page 26: Proving Little Theorems Data-Flow Equations Major Examples](https://reader035.fdocuments.in/reader035/viewer/2022081604/568166d6550346895ddaed73/html5/thumbnails/26.jpg)
26
Gen(B) and Kill(B) An expression x+y is generated if it is surely
computed in B, and afterwards there is no possibility that either x or y is redefined.
An expression x+y is killed if it is not surely generated in B and either x or y is possibly redefined.
![Page 27: Proving Little Theorems Data-Flow Equations Major Examples](https://reader035.fdocuments.in/reader035/viewer/2022081604/568166d6550346895ddaed73/html5/thumbnails/27.jpg)
27
Example: Gen and Kill for AE
x = x+yz = a+b
Generatesa+b
Kills x+y,w*x, etc.
Kills z-w,x+z, etc.
Note: x+y notGenerated becauseIt is killed later (at sameStatement, in fact).
![Page 28: Proving Little Theorems Data-Flow Equations Major Examples](https://reader035.fdocuments.in/reader035/viewer/2022081604/568166d6550346895ddaed73/html5/thumbnails/28.jpg)
28
Transfer and Confluence Equations Transfer is the same idea:
OUT(B) = (IN(B) – Kill(B)) ∪ Gen(B)
Confluence involves intersection, because an expression is available coming into a block if and only if it is available coming out of each predecessor.
IN(B) = ∩predecessors P of B OUT(P)
![Page 29: Proving Little Theorems Data-Flow Equations Major Examples](https://reader035.fdocuments.in/reader035/viewer/2022081604/568166d6550346895ddaed73/html5/thumbnails/29.jpg)
29
Iterative SolutionIN(entry) = ∅;for (each block B) do OUT(B)= ALL;
while (changes occur) do for (each block B) do { IN(B) = ∩predecessors P of B OUT(P); OUT(B) = (IN(B) – Kill(B)) ∪ Gen(B); }
We eliminate expressionsfor any reason we find, butif we find no reason, thenthe expression really isavailable.
![Page 30: Proving Little Theorems Data-Flow Equations Major Examples](https://reader035.fdocuments.in/reader035/viewer/2022081604/568166d6550346895ddaed73/html5/thumbnails/30.jpg)
30
Why It Works An expression x+y is unavailable at beginning
of block B if there is a path from the entry to B that either:
1. Never evaluates x+y, or2. Kills x+y after its last evaluation.
Case 1: Along this path, IN(entry) = ∅, x+y never in GEN, plus intersection as confluence proves not in IN(B).
Case 2: After the last Gen then Kill of x+y, this expression is in no OUT or IN.
![Page 31: Proving Little Theorems Data-Flow Equations Major Examples](https://reader035.fdocuments.in/reader035/viewer/2022081604/568166d6550346895ddaed73/html5/thumbnails/31.jpg)
31
Example
point p
Entry
x+ynevergen’d
x+y killed
x+ynevergen’d
![Page 32: Proving Little Theorems Data-Flow Equations Major Examples](https://reader035.fdocuments.in/reader035/viewer/2022081604/568166d6550346895ddaed73/html5/thumbnails/32.jpg)
32
Subtle Point It is conservative to assume an expression
isn’t available, even if it is. But we don’t have to be “insanely
conservative.” If after considering all paths, and assuming x+y
killed by any possibility of redefinition, we still can’t find a path explaining its unavailability, then x+y is available.
![Page 33: Proving Little Theorems Data-Flow Equations Major Examples](https://reader035.fdocuments.in/reader035/viewer/2022081604/568166d6550346895ddaed73/html5/thumbnails/33.jpg)
33
Live Variable Analysis Variable x is live at a point p if on some path
from p, x may be used before it is redefined. Useful in code generation: if x is not live at a
point, there is no need to copy x from a register to memory.
![Page 34: Proving Little Theorems Data-Flow Equations Major Examples](https://reader035.fdocuments.in/reader035/viewer/2022081604/568166d6550346895ddaed73/html5/thumbnails/34.jpg)
34
Equations for Live Variables LV is essentially a “backwards” version of RD. In place of Gen(B): Use(B) = set of variables x
possibly used in B prior to any certain definition of x.
In place of Kill(B): Def(B) = set of variables x certainly defined before any possible use of x.
![Page 35: Proving Little Theorems Data-Flow Equations Major Examples](https://reader035.fdocuments.in/reader035/viewer/2022081604/568166d6550346895ddaed73/html5/thumbnails/35.jpg)
35
Transfer and Confluence Equations Transfer equations give IN’s in terms of OUT’s:
IN(B) = (OUT(B) – Def(B)) ∪ Use(B)
Confluence involves union over successors, so a variable is in OUT(B) if it is live on entry to any of B’s successors.
OUT(B) = ∪successors S of B IN(S)
![Page 36: Proving Little Theorems Data-Flow Equations Major Examples](https://reader035.fdocuments.in/reader035/viewer/2022081604/568166d6550346895ddaed73/html5/thumbnails/36.jpg)
36
Iterative Solution
OUT(exit) = ∅;for (each block B) do IN(B)= ∅;
while (changes occur) do for (each block B) do { OUT(B) = ∪successors S of B IN(S); IN(B) = (OUT(B) – Def(B)) ∪ Use(B); }