STATIC ANALYSIS�
Key Concepts
Is a Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
Yes!
On all reaching
definitions
a = 4
Fact/Property
Analysis: Set of Facts/Properties
Key Concepts
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
No!
One reaching
definition with
b = 1
One reaching
definition with
b = 2
Merging Facts
Merging Facts
b = 1;
b = 1;
b = 1;
b = 1;
b = 1;
b = 1;
b = 1;
b = 1;
Constant
Sign
Interval
Set
Merging Facts
b = 1;
b = 2;
b = 1;
b = 2;
b = 1;
b = 2;
b = 1;
b = 2;
Constant
Sign
Interval
Set
Merging Facts
b = 0;
b = 1;
b = 0;
b = 1;
b = 0;
b = 1;
b = 0;
b = 1;
Constant
Sign
Interval
Set
Merging Facts
b = -1;
b = 1;
b = -1;
b = 1;
b = -1;
b = 1;
b = -1;
b = 1;
Constant
Sign
Interval
Set
Part 2
Merging Facts
Partial Orders
Saman Amarasinghe 15 6.035 ©MIT Fall 1998
Top and Bottom
Saman Amarasinghe 16 6.035 ©MIT Fall 1998
Lattice of Facts/Properties (Hasse Diagram)
Constant
Sign
Bounded Interval
Bounded Set
…
…
Upper Bounds
Saman Amarasinghe 18 6.035 ©MIT Fall 1998
Lattice of Facts/Properties
Constant
Sign
…
…
Bounded Interval
Bounded Set
Lower Bounds
Saman Amarasinghe 20 6.035 ©MIT Fall 1998
Lattice of Facts/Properties
Constant
Sign
…
…
Bounded Interval
Bounded Set
Merging Facts (use join operator)
b = -1;
b = 1;
b = -1;
b = 1;
b = -1;
b = 1;
b = -1;
b = 1;
Constant
Sign
Interval
Set
Lattice of Facts/Properties
Constant
Sign
…
…
Bounded Interval
Bounded Set
Lattice (as Hasse Diagram)
Saman Amarasinghe 24 6.035 ©MIT Fall 1998
Key Concepts
Algorithm
Is a Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
Yes!
On all reaching
definitions
a = 4
Transfer Functions
Transfer Function - Constant
Transfer Function - Sign
Computing transfer function
Part 2
Key Concepts
Forward Dataflow Analysis
Program Equations
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Chaotic Iteration
y=0;
t=1;
x=x+1;
y=y+2;
t=t+2;
y=t-1;
end
P2
P1
P3
P5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
Add three? No, already in queue
1
4
0
2
3
5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
1
4
0
2
3
5
Programs to systems of equations
Need to combine the properties that are true on each branch
Equations on a Control Flow Graph
y=0;
t=1;
x=x+1;
y=y+2;
t=t+2;
y=t-1;
end
<-P1
<-P2
<-P3
<-P2
<-P5
P2
P1
P3
P5
Checkpoint
What about termination?
Key Concepts
Termination
i = i + 1;
i = 0
i < n
Termination
i = i + 1;
i = 0
i < n
Termination
i = i + 1;
i = 0
i < n
Constant
Termination
i = i + 1;
i = 0
i < n
Termination
i = i + 1;
i = 0
i < n
Termination
i = i + 1;
i = 0
i < n
Reaches fixpoint!
Termination
i = i + 1;
i = 0
i < n
i = i + 1;
i = 0
i < n
Reaches fixpoint!
Termination
i = i + 1;
i = 0
i < n
i = i + 1;
i = 0
i < n
Reaches fixpoint!
Termination
i = i + 1;
i = 0
i < n
i = i + 1;
i = 0
i < n
Reaches fixpoint!
Termination
i = i + 1;
i = 0
i < n
i = i + 1;
i = 0
i < n
Reaches fixpoint!
Termination
i = i + 1;
i = 0
i < n
i = i + 1;
i = 0
i < n
Reaches fixpoint!
Termination
i = i + 1;
i = 0
i < n
i = i + 1;
i = 0
i < n
Reaches fixpoint!
Termination
i = i + 1;
i = 0
i < n
i = i + 1;
i = 0
i < n
Reaches fixpoint!
Termination
i = i + 1;
i = 0
i < n
i = i + 1;
i = 0
i < n
Reaches fixpoint!
Runs forever!
Termination
Monotonicity
Ascending Chains
Saman Amarasinghe 98 6.035 ©MIT Fall 1998
…
…
Complete Lattice
Saman Amarasinghe 99 6.035 ©MIT Fall 1998
…
…
Termination
Potential Failures
Saman Amarasinghe 101 6.035 ©MIT Fall 1998
�
Incomplete Lattice
Example of a lattice that is not complete?
[l1,u1] ∨ [l2, u2] = [min(l1,l2), max(u1,u2)]
Intervals over Z ∪ {+∞,−∞ } is complete
…
…
Infinite Ascending Chains
Saman Amarasinghe 103 6.035 ©MIT Fall 1998
…
…
Knaster-Tarski Theorem
Values (Constant Propagation)
…
…
Types (Eliminate type checks)
Int
String
None
Record
Function
Types (Eliminate type checks)
Register Allocation (Stack Caching)
Depth | Register |
1 | %r12 |
2 | %r13 |
3 | %r14 |
4 | %r15 |
Stack Positions (Stack Caching)
Static Analysis Implementations
Looking forward
Your static analysis can impose significant overhead for dynamic compilation. Make sure the expense is worth it.
An alternative, is dynamic analysis.
Dynamically record trace of program and facts that hold, generate code just as the same in – but insert guards that jump to unoptimized code in case program behavior changes and facts are no longer true.
Not an easy approach in general: which guards to generate and where? Surprise: requires static analysis.
Complete Lattice
Saman Amarasinghe 112 6.035 ©MIT Fall 1998
Finite Ascending Chains
A set S is a chain if ∀x,y∈S. y ≤ x or x ≤ y
All chains in P must be finite
Liveness Analysis
Escape analysis
(may escape)
(private)
Escape analysis
[{v1}=p, {v2}=p, {v3}=p]
[{v1}=me, {v2}=p, {v3}=p]
[{v1}=me, {v2}=p, {v3}=me]
fun(x, y){
t = {v1: x + y};
v = foo(t);
t = {v2: x – y} ;
v.x = {v3: t.v2 + 1};
return v;
}
Three records created:
{v1: x + y},
{v2: x - y},� {v3: t.v2 + 1}
Example: Reaching Definitions
Saman Amarasinghe 117 6.035 ©MIT Fall 1998
Reaching Definitions and Constant Propagation
Saman Amarasinghe 118 6.035 ©MIT Fall 1998
Is a Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
Yes!
On all reaching
definitions
a = 4
Constant Propagation Transform
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + 4*b;
i = i + 1;
return s
Yes!
On all reaching
definitions
a = 4
Is b Constant in s = s+a*b?
s = 0;
a = 4;
i = 0;
k == 0
b = 1;
b = 2;
i < n
s = s + a*b;
i = i + 1;
return s
No!
One reaching
definition with
b = 1
One reaching
definition with
b = 2
Covering
Saman Amarasinghe 123 6.035 ©MIT Fall 1998
Lattice (as Hasse Diagram)
�If y covers x
Can be arbitrarily deep
Saman Amarasinghe 124 6.035 ©MIT Fall 1998
111
011
101
110
010
001
000
100
(standard boolean lattice, also called hypercube)
Lattices
Saman Amarasinghe 125 6.035 ©MIT Fall 1998
Facts
Integer
Record
…
Facts
Integer
Record
…
Types
Constants
Stack Depth
Product Latices