Compiler Construction Lecture 16 Data-Flow Analysis.
-
Upload
christine-douglas -
Category
Documents
-
view
222 -
download
2
Transcript of Compiler Construction Lecture 16 Data-Flow Analysis.
![Page 1: Compiler Construction Lecture 16 Data-Flow Analysis.](https://reader035.fdocuments.in/reader035/viewer/2022062321/56649e4d5503460f94b43a0b/html5/thumbnails/1.jpg)
Compiler ConstructionLecture 16
Data-Flow Analysis
![Page 2: Compiler Construction Lecture 16 Data-Flow Analysis.](https://reader035.fdocuments.in/reader035/viewer/2022062321/56649e4d5503460f94b43a0b/html5/thumbnails/2.jpg)
Goal of Data-Flow Analysis
Automatically compute information about the program• Use it to report errors to user (like type errors)• Use it to optimize the programWorks on control-flow graphs: x = 1 while (x < 10) { x = x + 2 }
![Page 3: Compiler Construction Lecture 16 Data-Flow Analysis.](https://reader035.fdocuments.in/reader035/viewer/2022062321/56649e4d5503460f94b43a0b/html5/thumbnails/3.jpg)
How We Define It
• Abstract Domain D (Data-Flow Facts):which information to compute?– Example: interval for each variable x:[a,b], y:[a’,b’]
• Transfer Functions [[st]] for each statement st, how this statement affects the facts– Example:
![Page 4: Compiler Construction Lecture 16 Data-Flow Analysis.](https://reader035.fdocuments.in/reader035/viewer/2022062321/56649e4d5503460f94b43a0b/html5/thumbnails/4.jpg)
Find Transfer Function: Plus
Ifand we execute x= x+ythen
Suppose we have only two integer variables: x,y
So we can let a’= a+c b’ = b+d c’=c d’ = d
![Page 5: Compiler Construction Lecture 16 Data-Flow Analysis.](https://reader035.fdocuments.in/reader035/viewer/2022062321/56649e4d5503460f94b43a0b/html5/thumbnails/5.jpg)
Find Transfer Function: Minus
Ifand we execute y= x-ythen
Suppose we have only two integer variables: x,y
So we can let a’= a b’ = b c’= a - d d’ = b - c
![Page 6: Compiler Construction Lecture 16 Data-Flow Analysis.](https://reader035.fdocuments.in/reader035/viewer/2022062321/56649e4d5503460f94b43a0b/html5/thumbnails/6.jpg)
Transfer Functions for Tests
if (x > 1) {
y = 1 / x} else {
y = 42}
![Page 7: Compiler Construction Lecture 16 Data-Flow Analysis.](https://reader035.fdocuments.in/reader035/viewer/2022062321/56649e4d5503460f94b43a0b/html5/thumbnails/7.jpg)
Merging Data-Flow Facts
if (x > 0) {
y = x + 100
} else {
y = -x – 50
} join
![Page 8: Compiler Construction Lecture 16 Data-Flow Analysis.](https://reader035.fdocuments.in/reader035/viewer/2022062321/56649e4d5503460f94b43a0b/html5/thumbnails/8.jpg)
Handling Loops: Iterate Until Stabilizes
x = 1
while (x < 10) {
x = x + 2
}
Compiler learnedsome facts!
![Page 9: Compiler Construction Lecture 16 Data-Flow Analysis.](https://reader035.fdocuments.in/reader035/viewer/2022062321/56649e4d5503460f94b43a0b/html5/thumbnails/9.jpg)
Data-Flow Analysis Algorithmvar facts : Map[Vertex,Domain] = Map.withDefault(empty)facts(entry) = initialValues // changewhile (there was change) pick edge (v1,statmt,v2) from CFG such that facts(v1) was changed facts(v2)=facts(v2) join [[statmt]](facts(v1))}Order does not matter for the end result, as long as we do not permanently neglect any edge whose source was changed.
![Page 10: Compiler Construction Lecture 16 Data-Flow Analysis.](https://reader035.fdocuments.in/reader035/viewer/2022062321/56649e4d5503460f94b43a0b/html5/thumbnails/10.jpg)
Handling Loops: Iterate Until Stabilizes
x = 1
while (x < 10) {
x = x + 2
}
Compiler learnedsome facts!
![Page 11: Compiler Construction Lecture 16 Data-Flow Analysis.](https://reader035.fdocuments.in/reader035/viewer/2022062321/56649e4d5503460f94b43a0b/html5/thumbnails/11.jpg)
Handling Loops: Iterate Until Stabilizes
x = 1
while (x < n) { x = x + 2 }
Compiler learnssome facts, but only after long time
n = 100000
![Page 12: Compiler Construction Lecture 16 Data-Flow Analysis.](https://reader035.fdocuments.in/reader035/viewer/2022062321/56649e4d5503460f94b43a0b/html5/thumbnails/12.jpg)
Handling Loops: Iterate Until Stabilizes
var x : BigInt = 1
while (x < n) { x = x + 2 }
For unknown program inputs it may be practically impossible to know how long it takes
var n : BigInt = readInput()
Solutions - smaller domain, e.g. only certain intervals [a,b] where a,b in {-∞,-127,-1,0,1,127,∞} - widening techniques (make it less precise on demand)
![Page 13: Compiler Construction Lecture 16 Data-Flow Analysis.](https://reader035.fdocuments.in/reader035/viewer/2022062321/56649e4d5503460f94b43a0b/html5/thumbnails/13.jpg)
Size of analysis domainInterval analysis: D1 = { [a,b] | a ≤ b, a,b{-M,-127,-1,0,1,127,M-1}} U {}Constant propagation: D2 = { [a,a] | a{-M,-(M-1),…,-2,-1,0,1,2,3,…,M-1}} U {}
suppose M is 263
|D1| =
|D2| =
![Page 14: Compiler Construction Lecture 16 Data-Flow Analysis.](https://reader035.fdocuments.in/reader035/viewer/2022062321/56649e4d5503460f94b43a0b/html5/thumbnails/14.jpg)
How many steps does the analysis take to finish (converge)?
Interval analysis: D1 = { [a,b] | a ≤ b, a,b{-M,-127,-1,0,1,127,M-1}} U {}Constant propagation: D2 = { [a,a] | a{-M,-(M-1),…,-2,-1,0,1,2,3,…,M-1}} U {}
suppose M is 263
With D1 takes at most steps.
With D2 takes at most steps.
![Page 15: Compiler Construction Lecture 16 Data-Flow Analysis.](https://reader035.fdocuments.in/reader035/viewer/2022062321/56649e4d5503460f94b43a0b/html5/thumbnails/15.jpg)
Termination Given by Length of Chains
Interval analysis: D1 = { [a,b] | a ≤ b, a,b{-M,-127,-1,0,1,127,M-1}} U {}
Constant propagation: D2 = { [a,a] | a{-M,…,-2,-1,0,1,2,3,…,M-1}} U {} U {T}
suppose M is 263
Domain is a lattice. Maximal chain length = lattice height
![Page 16: Compiler Construction Lecture 16 Data-Flow Analysis.](https://reader035.fdocuments.in/reader035/viewer/2022062321/56649e4d5503460f94b43a0b/html5/thumbnails/16.jpg)
Lattice for intervals [a,b] wherea,b{-M,-127,0,127,M-1}
![Page 17: Compiler Construction Lecture 16 Data-Flow Analysis.](https://reader035.fdocuments.in/reader035/viewer/2022062321/56649e4d5503460f94b43a0b/html5/thumbnails/17.jpg)
Lattice
Partially ordered set (D, )
• Every there exists the least element s.t. , (lub, join, )
• It has a top (T) element and a bottom element ()
Lattice for ()
![Page 18: Compiler Construction Lecture 16 Data-Flow Analysis.](https://reader035.fdocuments.in/reader035/viewer/2022062321/56649e4d5503460f94b43a0b/html5/thumbnails/18.jpg)
Data-Flow Analysis Ingredients
Given some concrete domain :• Abstract Domain forming a lattice– An Abstraction Function – A Concretization Function
• The program semantics within A transfer function [[ _ ]]
![Page 19: Compiler Construction Lecture 16 Data-Flow Analysis.](https://reader035.fdocuments.in/reader035/viewer/2022062321/56649e4d5503460f94b43a0b/html5/thumbnails/19.jpg)
Transfer Function
Given a statement and an abstract pre-state, compute the abstract post-state.
Needs to be monotonous:
Pre-state
Post-state
S
![Page 20: Compiler Construction Lecture 16 Data-Flow Analysis.](https://reader035.fdocuments.in/reader035/viewer/2022062321/56649e4d5503460f94b43a0b/html5/thumbnails/20.jpg)
Abstraction/Concretization
Concrete Abstract
Abstraction Function
Concretization Function
𝐶1 𝐴1
𝐶2 𝐴2