Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
A Sampler of Approaches
Transparencies based on Chapter 1 of the book: Flemming Nielson,
Hanne Riis Nielson and Chris Hankin: Principles of Program Analysis.
c Flemming Nielson & Hanne Riis Nielson & Chris
Springer Verlag 2005.
Hankin.
PPA Chapter 1
Compiler Optimisation
The classical use of program analysis is to facilitate the construction of
compilers generating optimal code.
We begin by outlining the structure of optimising compilers.
We then prepare the setting for a worked example where we optimise
a naive implementation of Algol-like arrays in a C-like language by performing a series of analyses and transformations.
PPA Chapter 1
lexical
analysis
string of
characters
syntactic
analysis
string of
tokens
static
semantic
checking
symbol table
&
syntax tree
code
generation
syntax
tree
machine
code
lexical
analysis
syntactic
analysis
machine
independent
optimisations
static
semantic
checking
code
generation
high-level or
intermediate-level
representation
machine
dependent
optimisations
low-level
representation
front end
program
optimiser
back end
@
@
@
@
@
@
@
@
@
@
@
-
program
analysis
transformation
@
@
@
-
Avoid redundant computations: reuse available results, move loop invariant computations out of loops, ...
Avoid superfluous computations: results known not to be needed, results
known already at compile time, ...
PPA Chapter 1
PPA Chapter 1
...
program with
C-like arrays
optimised
program with
C-like arrays
Base(A)
01
A
A
A
AU
0
1
...
m
i
in Algol:
A[i,j]
n
6
PPA Chapter 1
in C:
Cont(Base(A) + i * (m+1) + j)
C-like arrays:
i := 0;
while i <= n do
j := 0;
while j <= m do
A[i,j] := B[i,j] + C[i,j];
j := j+1
od;
i := i+1
od
i := 0;
while i <= n do
j := 0;
while j <= m do
temp := Base(A) + i * (m+1) + j;
Cont(temp) := Cont(Base(B) + i * (m+1) + j)
+ Cont(Base(C) + i * (m+1) + j);
j := j+1
od;
i := i+1
od
PPA Chapter 1
PPA Chapter 1
t1 := i * (m+1) + j;
temp := Base(A) + t1;
Cont(temp) := Cont(Base(B)+t1)
+ Cont(Base(C)+t1);
PPA Chapter 1
t2 := i * (m+1);
while j <= m do
t1 := t2 + j;
temp := ...
Cont(temp) := ...
j := ...
od
10
PPA Chapter 1
i := 0;
t3 := 0;
while i <= n do
j := 0;
t2 := t3;
while j <= m do ...
od
i := i + 1;
t3 := t3 + (m+1)
od
11
PPA Chapter 1
while j <= m do
t1 := t3 + j;
temp := ...;
Cont(temp) := ...;
j := ...
od
12
PPA Chapter 1
i := 0;
t3 := 0;
while i <= n do
j := 0;
while j <= m do
t1 := t3 + j;
temp := Base(A) + t1;
Cont(temp) := Cont(Base(B) + t1)
+ Cont(Base(C) + t1);
j := j+1
od;
i := i+1;
t3 := t3 + (m+1)
od
13
Transformation
Strength reduction
Copy propagation
PPA Chapter 1
14
we cannot expect
exact answers!
15
universe
.......................
.
.
..
.
......
.
............ .....
........
@
exact set of
configurations
or behaviours
Over-approximation
.............................
................
..........
..........
........
........ .............................................
.......
.
.
.
.
.
.
.......
.
......
.
.
.
.
.
.
.
.
.
.
.
.
......
....
...
.
.
.
.
.
.
....
...
..
..
.
.
...
...
...
...
...
...
..
..
...
...
..
..
.
..
...
...
.
...
.
...
...
.
.
.
.
...
...
.
.
.
.
...
...
..
..
....
....
...
....
......
....
......
....... .........
.
.
.
.
.
.
.
.
.
.
.
.
.
........ .........
.
.
......... ...............................
........
.
............
.........
.........................................
.......................
.
.
.
..
......
.
.
.....................
..
overapproximation
Slogans:
Under-approximation
.....................................
.............
..........
..........
........
.......
.......
.
.
.
.
.
.
.......
..
.
.
.
.
.
.....
..
.
.
.
...
..
.
...
...
...
..
...
..
.
...
..
...
.
.
...
.
.
....
...
.....
.....
.......
......
.
.......
.
.
.
.
.
........
........
..........
..........
................
..............................
.......................
.
.
..
.
......
.
............ .....
........
underapproximation
16
17
Technique:
Example:
idea
constructing an equation system
solving the equations
theoretical underpinnings
18
Example program
Program with labels for
elementary blocks:
[y := x]1
Flow
graph:
[z := 1]2
[y := x]1 ;
[z := 1]2 ;
y]4;
[y := y 1]5
od;
[y > 0]3
[y := 0]6
[z := z y]4
?
[y := y 1]5
?
[y := 0]6
19
[y := x]1
[z := 1]2
?
[y > 0]3
[y := 0]6
6
[z := z y]4
[y := y 1]5
%
?
20
[y := x]1 ;
[z := 1]2 ;
while [y > 0]3 do
[z := z y]4 ;
[y := y 1]5
od;
[y := 0]6
21
{(x, ?), (y, 1), (z, 2)} {(y, 5), (z, 4)}
[y := x]1 ;
[z := 1]2 ;
while [y > 0]3 do
[z := z y]4 ;
[y := y 1]5
od;
[y := 0]6
22
{(x, ?), (y, 1), (y, 5), (z, 2), (z, 4)} {(y, 5), (z, 4)}
{(x, ?), (y, 1), (y, 5), (z, 2), (z, 4)}
{(x, ?), (y, 1), (y, 5), (z, 2), (z, 4)}
[y := x]1 ;
[z := 1]2 ;
while [y > 0]3 do
[z := z y]4 ;
[y := y 1]5
od;
[y := 0]6
23
{(x, ?), (y, 1), (y, 5), (z, 2), (z, 4)}
{(x, ?), (y, 1), (y, 5), (z, 2), (z, 4)}
{(x, ?), (y, 1), (y, 5), (z, 2), (z, 4)}
[y := x]1 ;
[z := 1]2 ;
while [y > 0]3 do
[z := z y]4 ;
[y := y 1]5
od;
[y := 0]6
24
[y := x]1 ;
{(x, ?), (y, 1), (y, 5), (z, 2), (z, 4)}
{(x, ?), (y, 1), (y, 5), (z, 2), (z, 4)}
{(x, ?), (y, 1), (y, 5), (z, 2), (z, 4)}
[z := 1]2 ;
while [y > 0]3 do
[z := z y]4 ;
[y := y 1]5
od;
[y := 0]6
25
An unsafe solution
[y := x]1 ;
{(x, ?), (y, 1), (z, 2), (y, 5), (z, 4)}
[z := 1]2 ;
while [y > 0]3 do
[z := z y]4 ;
[y := y 1]5
od;
[y :=
0]6
26
extract
equations
from the
program
compute the
least solution
to the equations
Analysis information:
RD(`): information available at the entry of block `
27
RD(`)
[...]`1
[...]`2
RD(`1)
[x := a]`
@
@
R
@
RD(`)
RD(`2)
RD(`)
[...]`
28
[y := x]1 ;
[z := 1]2 ;
RD(3) = RD(3)
[y := 0]6
Lab = {1,2,3,4,5,6}
29
RD(2) = RD(1)
RD(4) = RD(3)
RD(5) = RD(4)
[y := x]1 ;
[z := 1]2 ;
while [y > 0]3 do
[z := z y]4 ;
[y := y 1]5
od;
[y :=
0]6
RD(6) = RD(3)
Lab = {1,2,3,4,5,6}
6 equations in
RD(1), , RD(6)
c F.Nielson & H.Riis Nielson & C.Hankin (May 2005)
30
12 equations:
RDj = Fj (RD(1), , RD(6))
one function:
F : P(Var Lab)12
P(Var Lab)12
RD(2) = RD(1)
RD(3) = RD(2) RD(5)
RD(4) = RD(3)
RD(5) = RD(4)
RD(6) = RD(3)
31
Iteration
while RDj 6= Fj (RD1, , RD12) for some j
do
RDj := Fj (RD1, , RD12)
32
x? , y? , z?
x? , y? , z?
x? , y? , z?
x? , y? , z?
x? , y? , z?
x? , y? , z?
..
.
x? , y1 , z?
x? , y1 , z?
x? , y1 , z?
x? , y1 , z?
..
.
x? , y1 , z2
x? , y1 , z2
..
.
..
.
..
.
..
.
The equations:
RD (1) = RD (1) \ {(y, `) | } {(y, 1)}
RD (2) = RD (2) \ {(z, `) | } {(z, 2)}
RD (3) = RD (3)
RD (4) = RD (4) \ {(z, `) | } {(z, 4)}
RD (5) = RD (5) \ {(y, `) | } {(y, 5)}
RD (6) = RD (6) \ {(y, `) | } {(y, 6)}
RD
0
1
2
3
4
5
6
..
.
x? , y1 , z?
x? , y1 , z?
x? , y1 , z?
x? , y1 , z?
x? , y1 , z?
..
.
x? , y1 , z2
x? , y1 , z2
x? , y1 , z2
..
.
x? , y1 , z2
..
.
..
.
..
.
..
.
33
f (V ) f (V 0)
V 0
...
............. ...................
.....
.......
....
.....
.
....
.
.
..
...
.
.
...
.
...
...
..
...
..
...
..
...
.
...
...
...
..
...
..
...
...
...
.
.
...
.
...
..
.
...
..
...
..
..
...
.
.
...
..
...
..
...
...
....
.
.
.
.....
.......
.....
............ ..................
......
34
35
.........................
.........
......
.....
......
.
.
.
.
....
.
..
....
.
.
.......
....
.
...
...
...
.
...
..
...
...
..
...
..
...
..
.
...
..
...
..
.
...
.
.
...
.
..
...
..
...
..
O
C
...
.
C
..
...
...
......
...
C
.
.
....
...
..... @
.....
....... I
............ ..................
......
...
36
37
Technique:
Example:
idea
constructing a constraint system
solving the constraints
theoretical underpinnings
38
g 7
x => x 7
y => y
z => 3
(g h)
f h
h 7
function
applications
x 7
f g
g h
f (g h)
function abstractions
that may be applied
g, h
f
g
f
39
Solutions
let f = fn
g = fn
h = fn
in f g + f
x => x 7
y => y
z => 3
(g h)
function
applications
x 7
f g
g h
f (g h)
function abstractions
that may be applied
g, h, f
f
g
f
An unsafe solution:
function abstractions
that may be applied
g, h
f
g
f
function
applications
x 7
f g
g h
f (g h)
function abstractions
that may be applied
g, h
f
g
f
40
x => x 7
y => y
z => 3
(g h)
g:
h:
7
3
function
applications
x 7
f g
g h
f (g h)
function abstractions
that may be applied
g, h
f
g
f
41
42
C
1
2
3
4
5
6
7
8
9
10
11
12
13
43
extract
constraints
from the
program
compute the
least solution
to the
constraints
Analysis information:
R(x): information available for the variable x
44
45
let-bound variables
let-bound variables evaluate to their abstractions
in (f5 g6)7
46
Variables
Variables evaluate to their abstract values
R(x) C(1)
R(y) C(4)
(
R(f) C(5)
R(g) C(6)
47
in (f5 g6)7
Conditional constraints
PPA Section 1.4
48
g = fn y => y4
in (f5 g6)7
49
11
sets:
R(x) C(1)
R(y) C(4)
R(f) C(5)
R(g) C(6)
(fn y => y4 ) C(1) C(2) R(y)
(fn y => y4 ) C(1) C(4) C(3)
(fn x => (x1 72 )3 ) C(1) C(2) R(x)
(fn x => (x1 72 )3 ) C(1) C(3) C(3)
(fn y => y4 ) C(5) C(6) R(y)
(fn y => y4 ) C(5) C(4) C(7)
(fn x =>
(x1
72 )3 )
F : P(Abstr)11 P(Abstr)11
50
72 )3 } R(f)
R(g)
F : P(Abstr)11 P(Abstr)11
is defined by
FR(f)( , Rf , ) =
...
Rf {fn x => (x1 72)3}
FC(1)(Rx, , C1, ) = C1 Rx
...
FR(y)( , Ry , , C1, C2, , C5, C6, ) =
Ry {a C2 | fn y => y4 C1 }
{a C6 | fn y => y4 C5 }
51
writing
X1 , , X11
for
R(x), , R(g), C(1), , C(7)
Iteration
while Xj 6= FXj (X1, , X11) for some j
do
Xj := FXj (X1, , X11)
The algorithm terminates and computes the least fixed point of F
In practice we propagate smaller contributions than FXj , e.g. a constraint at a time.
52
fn y => 4
fn y => 4
fn
fn
fn
fn
fn
fn
x =>
x =>
x =>
x =>
x =>
x =>
3
3
3
3
3
3
fn
fn
fn
fn
fn
y =>
y =>
y =>
y =>
y =>
4
4
4
4
4
(1)
(2)
R(x) C(1)
(6)
R(y) C(4)
R(f) C(5)
(3)
R(g) C(6)
(4)
C
0
1
2
3
4
5
6
fn y => 4
fn
fn
fn
fn
x =>
x =>
x =>
x =>
3
3
3
3
fn y => 4
fn y => 4
fn y => 4
53
(5)
f (V ) f (V 0)
V 0
....
............ ...................
.......
.....
....
.....
.
.
...
..
...
...
.
...
. .
...
...
...
..
...
..
...
..
...
..
..
...
..
...
...
.
...
..
...
..
.
.
...
.
...
..
..
...
..
...
.
.
...
..
...
..
...
....
...
.
.
.....
.....
.......
.......... ................
............
54
55
.........................
.........
......
.....
......
.
.
.
.
....
.
..
....
.
.
.......
....
.
...
...
...
.
...
..
...
...
..
...
..
...
..
.
...
..
...
..
.
...
.
.
...
.
..
...
..
...
..
O
C
...
.
C
..
...
...
......
...
C
.
.
....
...
..... @
.....
....... I
............ ..................
......
...
56
57
Abstract Interpretation
Technique:
Example:
Abstract Interpretation
idea
collecting semantics
Galois connections
Inducing the analysis
58
Abstract Interpretation
program
analysis
old
?
-
program
analysis
new
We have the analysis old: it has already been proved correct but it
is inefficient, or maybe even uncomputable
We want the analysis new: it has to be correct as well as efficient!
Can we develop new from old?
abstract interpretation !
PPA Section 1.5
59
collecting
reaching
calculatesemantics
definitions
CS
RD
The collecting semantics CS
collects the set of traces that can reach a given program point
has an easy correctness proof
is uncomputable
The reaching definitions analysis RD is as before
PPA Section 1.5
60
[y := x]1
{(x,?):(y,?):(z,?):(y,1)}
?
[z := 1]2
{(x,?):(y,?):(z,?):(y,1):(z,2)}
?
-
{(x,?):(y,?):(z,?):(y,1):(z,2),
[y > 0]3
[y := 0]6
(x,?):(y,?):(z,?):(y,1):(z,2):(z,4):(y,5),
[z := z y]4
(x,?):(y,?):(z,?):(y,1):(z,2):(z,4):(y,5):(z,4):(y,5),
}
[y := y 1]5
?
61
How to proceed
As before:
And furthermore:
62
CS(`)
[...]`1
[...]`2
CS(`1)
[x := a]`
CS(`)
@
@
R
@
CS(`2)
CS(`)
[...]`
63
[y := x]1 ;
[z := 1]2 ;
CS(3) = CS(3)
y]4;
[y := y 1]5
od;
[y := 0]6
6 equations in
CS(1), , CS(6)
PPA Section 1.5
64
CS(2) = CS(1)
CS(4) = CS(3)
CS(5) = CS(4)
CS(6) = CS(3)
[y := x]1 ;
[z := 1]2 ;
while [y >
0]3
[z := z
do
y]4;
[y := y
1]5
od;
[y := 0]6
6 equations in
CS(1), , CS(6)
PPA Section 1.5
65
CS(3) = CS(3)
CS(4) = {trace : (z, 4) | trace CS(4)}
CS(5) = {trace : (y, 5) | trace CS(5)}
CS(6) = {trace : (y, 6) | trace CS(6)}
12 equations:
CSj = Gj (CS(1), , CS(6))
one function:
CS(1) = {(x, ?) : (y, ?) : (z, ?)}
CS(2) = CS(1)
CS(3) = CS(2) CS(5)
CS(4) = CS(3)
G : P(Trace)12 P(Trace)12
CS(5) = CS(4)
CS(6) = CS(3)
66
(Y )
P(Trace)
(X) Y
........................
.........
.......
......
.....
.....
....
.
.
.
...
..
.
...
.
.
...
.
.
...
...
...
..
...
..
..
...
..
...
..
...
.
.
....
..
...
..
.
...
.
.
...
..
...
..
... ..
...
...
.
...
..
...
..
...
.
.
.
....
...
.....
.......
.....
............ .................
......
(X)
P(Var Lab)
: abstraction function
: concretisation function
67
(x,?):(y,?):(z,?):(y,1):(z,2)
C
C
CW
SRD(trace):
{(x,?),(y,1),(z,2)}
{(x,?):(y,?):(z,?):(y,1):(z,2),
(x,?):(y,?):(z,?):(y,1):(z,2):(z,4):(y,5)
}
?
SRD(X):
PPA Section 1.5
+
+
{(x,?),(y,1),(z,2),(z,4),(y,5)}
68
(X) = SRD(X)
(Y ) = {trace | SRD(trace ) Y }
.........................
......
.........
.....
......
....
.....
.
.
...
..
.
.
.
.......
.
.
...
...
.
...
..
...
...
...
..
...
.
...
...
.
...
..
...
.
.
...
.
.
...
..
...
..
...
..
.
.
...
..
...
..
...
..
...
.
.
.
....
...
.....
.....
.......
............ ..................
......
(Y )
P(Trace)
.........................
......
.........
.....
......
....
.....
.
.
...
..
.
...
.
.
...
.
.
...
...
...
..
...
..
...
..
..
...
..
...
..
..
...
.
...
..
.
...
.
.
.
...
.
...
..
... ..
..
...
.
.
...
..
...
..
...
...
....
.
.
.
.....
.....
.......
............ ..................
......
Galois connection:
X (Y )
(X) Y
(X)
P(Var Lab)
69
Known:
G(4) defined on P(Trace)
the Galois connection (, )
.....
............ ..................
......
.....
....
.....
.
.
....
..
...
..
.
'
.
...
.
...
...
...
..
...
..
..
...
..
...
..
...
.
...
...
.
...
..
...
.
.
...
.
.
...
..
...
&
.....
...
...
..
.
...
...
...
..
....
.
.
.
.....
....
......
......
.........
..........................
P(Trace)
.....
............ ..................
......
.....
....
.....
.
.
....
..
...
..
.
$
. -
...
.
...
...
...
..
...
..
..
...
..
...
..
...
.
...
...
.
...
..
...
.
.
...
.
.
...
..
...
..
%
...
..
...
...
.
...
...
...
..
....
.
.
.
.....
....
......
......
.........
..........................
F(4) = G(4)
G(4)
as F(4) = G(4)
P(Var Lab)
70
using = SRD
using = id
just as before!
71
Technique:
Example:
idea
annotated base types
annotated type constructors
72
types:
is the type of states;
all statements S have type
written ` S :
73
@
@
@
R
@
S:
C
C
Analysis:
C
C
C
S:
RD1
Idea:
C
CW
` final (S) :
RD(`) RD2
RD2
@
I
@
@
@
analysis information
for initial state
analysis information
for final state(s)
74
after
` S1 : RD1 RD2
` S2 : RD2 RD3
` S1; S2 : RD
RD
| {z1}
| {z3}
before
Implicit:
assumptions
conclusion
after
75
` S : RD RD
` while [b]` do S od : RD RD
Implicit:
76
The rule is essential for the rules for conditional and iteration to work
RD1 RD01: strengthen the analysis information for the initial state
RD02 RD2: weaken the analysis information for the final states
77
78
Implementation
annotated
type system
(axioms and
rules)
extract
constraints
from the program
compute the
least solution
to the constraints
?
79
Until now:
given a statement and a specific entry information RD we determine
the specific exit information RD
Now:
given a statement we determine how entry information is transformed into exit information
80
@
@
@
R
@
S:
C
C
Analysis:
C
C
S:
C
CW
X
I
@
RD
@
@
@
@
@
set of variables
definitely assigned by S
81
` [x:=a]` :
{x}
{(x,`)}
X1
X2
` S2 : RD
` S1 : RD
1
2
1 X2
` S1; S2 : (RD X\X
)RD
1
82
RD
1
X
` S : RD
` while [b]` do S od : RD
X1 X2 :
variables definitely assigned
RD1 RD2 :
potential reaching definitions
83
if X 0 X
(variables definite assigned)
and RD RD0 (potential reaching definitions)
RD
84
` [z:=z*y]4: {(z,4)}
{y}
` [y:=y-1]5: {(y,5)}
{y,z}
{(y,6),(z,2),(z,4)}
85
Implementation
annotated
type system
(axioms and
rules)
extract
constraints
from the program
compute the
least solution
to the constraints
?
86
Technique:
Example:
Effect systems
idea
simple type system
effect system
87
types
::= int | bool | 1 2
f has type 1 2 means that
f expects a parameter of type 1
f returns a value of type 2
88
g 7
f g
g 7
function
applications
x 7
f g
h g
f (h g)
function abstractions
that might be applied
during its execution
G, H
F, G
H, G
F, H, G
89
Simple types
let f = fnF x
g = fnG y
h = fnH z
in f g + f (h
=> x 7
=> y
=> z
g)
90
if (x) = x
[x 7 x] ` e :
` fn x => e : x
` e1 : 2 , ` e2 : 2
` e1 e2 :
91
Effect systems
Call Tracking analysis:
For each function application, which function abstractions might be
applied during its execution?
Idea:
Annotate the function arrows with sets of names of function abstractions that might be applied when calling the function.
1
2
92
{F,G}
f: (int
int)
int
g: int
h = fnH z => z
in f g + f (h g)
h: (int
int)
(int
int
&
{F, G, H}
{G}
int
{G}
{ H}
{z
{G}
int)
the effect
of executing
f g + f (g h)
93
if (x) = x
[x 7 x] ` e : &
` fn x => e : x
{}
&
` e1 : 2
& 1
` e2 : 2 & 2
evaluating the argument
` e1 e2 : & 1 2
94
95
Example (1)
let f = fnF x
g = fnG y
h = fnH z
in f g + f (h
=> x 7
=> y
=> z
g)
[x 7 x ] ` e : &
` fn x => e : x
{}
` e1 : 2
& 1
&
` e2 : 2 & 2
` e1 e2 : & 1 2
[x 7 int {G}
int] ` x : int {G}
int &
[x 7 int {G}
int] ` 7 : int &
[x 7 int {G}
int] ` x 7 : int & {G}
[ ] ` fnF x => x 7 : (int {G}
int) {F,G}
int &
96
Example (2)
let f = fnF x
g = fnG y
h = fnH z
in f g + f (h
[x 7 x ] ` e : &
=> x 7
=> y
=> z
g)
` fn x => e : x
{G}
int)
&
` e1 : 2
& 1
` e2 : 2 & 2
` e1 e2 : & 1 2
int)
{H}
` g : int
{G}
` h g : int
{G}
` h : (int
` f : (int
{}
{F,G}
int &
{G}
(int
{G}
int) &
int &
97
Implementation
annotated
type system
(axioms and
rules)
extract
constraints
from the program
compute the
least solution
to the constraints
?
98