Download Region Analysis and Transformation for Java Programs

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Region Analysis and Transformation for
Java Programs
Sigmund Cherem and Radu Rugina
Cornell University
Introduction
Goal
• Take a Java program and add region support
Regions for Memory Management
• Grouping objects in memory
• Removing all objects in a region at once
Benefits
• Data locality
• Efficient memory reclamation
• Alternative to garbage collection
o No run-time GC overhead
o Appealing for real-time programs
Region Analysis for Java
Region analysis for Java
Region inference
• Static compiler analysis and transformation
• Generate programs augmented with region annotations
o creating and removing regions
o allocating objects into regions
o passing regions as parameters
Features
• Regions are not lexically scoped
• System allows dangling references
Region Analysis for Java
Region analysis for Java
Region Constructs
• create r and remove r
• create r dynamically creates a new region and stores its handle
in r
• remove r dynamically removes region r and reclaims all of its
memory.
• new C(e1,..,en) in r
• for the allocation of objects into regions.
• T m<r1,..,rk>(T1 p1,..,Tn pn) {..}
• for method declarations.
• m <r1,..,rk> (e1,..,en)
• for method calls.
Region Analysis for Java
Region Compilation System
Java
Output
program
Region Analysis for Java
Region Compilation System
I. Group objects into regions
Java
I
Points-to
Analysis
Region Analysis for Java
Jreg
Region Compilation System
II. Determine region lifetimes
Java
I
Points-to
Analysis
Jreg
II
Region
Liveness
Analysis
Region Analysis for Java
Region Compilation System
III. Program transformation
Java
I
Points-to
Analysis
III
Region
Translation
II
Region
Liveness
Analysis
Region Analysis for Java
Jreg
Outline
Region Compilation System
Java
I
Points-to
Analysis
III
Region
Translation
II
Region
Liveness
Analysis
Region Analysis for Java
Jreg
Points-to analysis
Points-to analysis goals
• Group objects into regions
o Construct region points-to graphs for each method
• Characterize methods effects
o Accessed regions
o Regions with allocations
Region points-to graphs
• Nodes represent regions
• Edges represent points-to relations
Region Analysis for Java
Analysis algorithm
Intra-procedural analysis
• Flow insensitive, unification based pointer analysis algorithm
• One graph per method
Inter-procedural analysis
• Context sensitive
o Whole program analysis
• Propagate across call sites
o Bind regions of formal parameters to actual parameters
o Include callee’s effects and unifications in caller
Region Analysis for Java
Intra-procedural Analysis
V : set of variables
S : set of allocation sites in the method
F : set of class fields
P : set of statements in the method
L : set of node labels, consisting of allocation sites a ∈ S,
variables x ∈ V , and field accesses x.f ∈ V ×F
L = S∪ V ∪ (V × F )
LP : set of labels that occur in the method body
Region Analysis for Java
Intra-procedural Analysis
DEFINITION 1. A points-to graph is G = (N,E), where:
• N ⊆ P (LP ) is the set of region nodes such that each label occurs in at
most one node. If u ∈ L is a label, n(u) is the graph node that
contains u.
• E = N × F × N is a set of edges labeled with field names. <n, f, m>
denotes an edge between n and m on field f.
DEFINITION 2. A consistent unification points-to graph is a pointsto graph G = (N,E) such that:
• n(x.f) ∈ N ⇒ <n(x), f, n(x.f)> ∈ E, (consistency
condition).
• <n, f, m>, <n, f, m’> ∈ G ⇒ m = m’, (unification
condition).
Region Analysis for Java
Intra-procedural Analysis
Goal of a unification-based pointer analysis –
to build a consistent unification points-to graph
• unify (n, m) : unifies two nodes n and m in the graph. Given a
graph G = (N,E), it produces a new graph G’ = (N’,E’) such
that:
N’ = N − {n, m} ∪ {n ∪ m}
E’ = {<p’, f, q’ >| ∃<p, f, q> ∈ E . p ⊆ p’ ∧ q ⊆ q’}
• edge (n, f, m) : adds an edge between nodes n and m on field f.
Given G = (N,E), it produces a new graph G = (N,E ∪ <n,
f, m>).
Region Analysis for Java
The Algorithm
•
•
•
•
edge (n(x), f, n(x.f)) ∀(x.f) ∈ LP
unify (n(u), n(v)) ∀(u = v) ∈ P
unify (n(ret), n(x)) ∀(return x) ∈ P
Propagate unifications down in the graph, using rules (R1) and
(R2)
Region Analysis for Java
Example data-structure
List container
head
data
next
data
next
data
class List {
class Elem {
Elem head;
Object data;
Elem next;
}
}
Region Analysis for Java
class Object
Example data-structure
List container
head
data
next
data
next
data
class List {
class Elem {
Elem head;
Object data;
Elem next;
}
}
Region Analysis for Java
class Object
Regions points-to graphs
List reversal
public List reverse() {
List list = new List();
next
Element elem = this.head;
while (elem != null) {
list.addFirst(elem.data);
elem = elem.next;
}
1
head
this
2
elem
data
elem.data
return list;
}
4
head
list, ret
Region Analysis for Java
5
next
3
data
Inter-procedural analysis
Region aliasing
• Propagation of unifications at call sites
Given the a caller graph Gr = (Nr ,Er ) and a callee graph Ge
= (Ne ,Ee ), construct a result graph Gr’ = (Nr’ ,Er’ ) for the
caller such that:
a) Gr’ is a conservative approximation of Gr
b) the sub-graph of Gr’ rooted at the actual parameters is a
conservative approximation of the subgraph of Ge rooted at
the formal parameters of the callee.
The resulting graph Gr’ incorporates the effects of the call.
Region Analysis for Java
Inter-procedural analysis
• DEFINITION. A graph G = (N,E) is a
conservative approximation of G’ = (N’ ,E’) if
there exists a node mapping
α : N’ → N
s.t. <n, f, m>∈ E’ implies <α(n), f, α(m)>∈ E for
all n, m ∈ N’
Region Analysis for Java
The Algorithm
•
Call statement of the form -
x = y0 . m (y1, … , yn)
that invokes a method m with formal parameters this, p1, … , pn and return value ret.
• Map each formal parameter of the callee to its corresponding actual parameter at the call site:
α(n(pi)) = n(yi) ∀i = 1...n
α(n(this)) = n(y0)
α(n(ret)) = n(x)
• Unify actual nodes that correspond to parameter nodes that have been unified in the callee.
For this, rules (R3), (R4), and (R5) are used.
Region Analysis for Java
The Algorithm
• traverse the nodes in the callee graph Ge starting from the
formal parameters and inspect each edge exactly once.
Region Analysis for Java
Recursive Structures
• For recursive methods that manipulate recursive data structures,
rule (R9) may generate an unbounded number of fresh nodes.
• Solution
– maintain a type for each node in the graph
– ensure that target of new edge is not reachable from another node with
the same type
Region Analysis for Java
Virtual Method Calls
• invoked method is not statically known.
• compute a points-to graph for each method m that
incorporates the points-to information from all of other
methods that override m.
• add a dummy call from each method to the methods that
immediately override it in the class hierarchy.
• the points-to information will be transitively propagated
from the methods at the bottom to the overridden methods
at the top of the hierarchy.
• At a call site, the algorithm analyzes one single method
Region Analysis for Java
Tracking Allocations and Accesses
• Extend the points-to graph abstraction to a tuple
G = (N,E,A, T)
A ⊆ N is the set of allocation nodes and T ⊆ N is the set of accessed
nodes.
• mark each node n(a) corresponding to an allocation site a as an
allocation node.
• for each field access x.f or method call x.m(..)
• mark the node n(x) as an accessed node;
• mark each allocation node also as an accessed node.
• unification of two nodes yields an allocation (or accessed) node if
• one of the two original nodes was an allocation (or accessed) node.
• At inter-procedural level, for each allocation node n in the callee, the
mapped node m = α(n) is an allocation node in the caller (and similarly
for accesses).
Region Analysis for Java
Outline
Region Compilation System
Java
I
Points-to
Analysis
III
Region
Translation
II
Region
Liveness
Analysis
Region Analysis for Java
Jreg
Region liveness analysis
Region liveness analysis goals
• Determine program points when a region is needed
• Produce set of live regions per program point
Definition of region lifetime
• The region is reachable from a live variable, and
• The program may access the region in the future
Region Analysis for Java
Region liveness analysis
• Analysis technique
– Backwards dataflow analysis
– Keeps track of live variables and live regions
Region Analysis for Java
Outline
Region Compilation System
I
Points-to
Analysis
Input
Program
III
Region
Translation
II
Region
Liveness
Analysis
Region Analysis for Java
Output
Program
Region translation
Region translation goals
• Incorporate analysis results in the program
o
o
o
o
Define regions according to points-to analysis results
Create and remove regions using liveness results
Allocate objects in the assigned regions
Include region parameters at call sites
Translation process
• Single pass through the program
Region Analysis for Java
Region translation
• For each method, assign a region name r n to each allocation
node n in the points-to graph of that method.
• Inspect each construct in the program and generate a
corresponding region-annotated construct in the output
program
• Object allocations : translates each statement “new C”,
whose allocation site is a, into: “new C in r n(a)”
• Method declarations : Given a method m, identify all
allocation nodes that are reachable from formal parameters.
Add the formal region parameters of the method.
• Method calls.: At each call site, derive a mapping α between
the formal parameters of the invoked method and the actual
region parameters at the call site.
Region Analysis for Java
Region translation
• Use the region liveness information to place region
creation and removal statements.
Region Analysis for Java
Create and remove regions
Adding create and remove statements
• Use region liveness results
public void valueAt(List coeff, Complex x) {
next
head
List rev = coeffs.reverse();
Complex sum = new Complex(0,0);
while (!rev.empty()) {
Complex coeff = (Complex)rev.remove(0);
coeffs
1
x
}
System.out.println(sum.re + “,” + sum.im);
}
Region Analysis for Java
next
data
2
rev
Complex tmp = sum.mul(x);
sum = tmp.plus(coeff);
head
data
3
4
sum
new
tmp
Create and remove regions
Adding create and remove statements
• Use region liveness results
public void valueAt(List coeff, Complex x) {
next
head
List rev = coeffs.reverse();
Complex sum = new Complex(0,0);
while (!rev.empty()) {
Complex coeff = (Complex)rev.remove(0);
coeffs
1
x
}
System.out.println(sum.re + “,” + sum.im);
}
Region Analysis for Java
next
data
2
rev
Complex tmp = sum.mul(x);
sum = tmp.plus(coeff);
head
data
3
4
sum
new
tmp
Create and remove regions
Adding create and remove statements
• Use region liveness results
public void valueAt(List coeff, Complex x) {
next
create r1;
head
List rev = coeffs.reverse();
Complex sum = new Complex(0,0);
while (!rev.empty()) {
Complex coeff = (Complex)rev.remove(0);
coeffs
1
x
}
remove r1;
System.out.println(sum.re + “,” + sum.im);
}
Region Analysis for Java
next
data
2
rev
Complex tmp = sum.mul(x);
sum = tmp.plus(coeff);
head
data
3
4
sum
new
tmp
Create and remove regions
Adding create and remove statements
• Use region liveness results
public void valueAt(List coeff, Complex x) {
next
create r1;
head
List rev = coeffs.reverse();
Complex sum = new Complex(0,0);
while (!rev.empty()) {
Complex coeff = (Complex)rev.remove(0);
coeffs
1
x
}
remove r1;
System.out.println(sum.re + “,” + sum.im);
}
Region Analysis for Java
next
data
2
rev
Complex tmp = sum.mul(x);
sum = tmp.plus(coeff);
head
data
3
4
sum
new
tmp
Create and remove regions
Adding create and remove statements
• Use region liveness results
public void valueAt(List coeff, Complex x) {
next
create r1;
head
List rev = coeffs.reverse();
Complex sum = new Complex(0,0);
while (!rev.empty()) {
Complex coeff = (Complex)rev.remove(0);
create r4;
coeffs
1
x
remove r4;
}
remove r1;
System.out.println(sum.re + “,” + sum.im);
}
Region Analysis for Java
next
data
2
rev
Complex tmp = sum.mul(x);
sum = tmp.plus(coeff);
head
data
3
4
sum
new
tmp
Loop-carried dependencies
Loop-carried liveness dependency
public void valueAt(List coeff, Complex x) {
next
create r1;
head
List rev = coeffs.reverse();
Complex sum = new Complex(0,0);
while (!rev.empty()) {
Complex coeff = (Complex)rev.remove(0);
create r4;
coeffs
1
x
remove r4;
}
remove r1;
System.out.println(sum.re + “,” + sum.im);
}
Region Analysis for Java
next
data
2
rev
Complex tmp = sum.mul(x);
sum = tmp.plus(coeff);
head
data
3
4
sum
new
tmp
Loop-carried dependencies
Loop-carried liveness dependency
public void valueAt(List coeff, Complex x) {
next
create r1;
head
List rev = coeffs.reverse();
create r3;
Complex sum = new Complex(0,0);
while (!rev.empty()) {
Complex coeff = (Complex)rev.remove(0);
create r4;
coeffs
1
x
sum = tmp.plus(coeff);
remove r4;
}
remove r1;
System.out.println(sum.re + “,” + sum.im);
remove r3;
}
Region Analysis for Java
next
data
2
rev
Complex tmp = sum.mul(x);
remove r3;
create r3;
head
data
3
4
sum
new
tmp
Update allocation sites
Add regions at allocation sites
• Find corresponding region in points-to graph
public void valueAt(List coeff, Complex x) {
next
create r1;
head
List rev = coeffs.reverse();
create r3;
Complex sum = new Complex(0,0);
while (!rev.empty()) {
Complex coeff = (Complex)rev.remove(0);
create r4;
coeffs
1
x
sum = tmp.plus(coeff);
remove r4;
}
remove r1;
System.out.println(sum.re + “,” + sum.im);
remove r3;
}
Region Analysis for Java
next
data
2
rev
Complex tmp = sum.mul(x);
remove r3;
create r3;
head
data
3
4
sum
new
tmp
Update allocation sites
Add regions at allocation sites
• Find corresponding region in points-to graph
public void valueAt(List coeff, Complex x) {
next
create r1;
head
List rev = coeffs.reverse();
create r3;
Complex sum = new Complex(0,0) in r3;
while (!rev.empty()) {
Complex coeff = (Complex)rev.remove(0);
create r4;
coeffs
1
x
sum = tmp.plus(coeff);
remove r4;
}
remove r1;
System.out.println(sum.re + “,” + sum.im);
remove r3;
}
Region Analysis for Java
next
data
2
rev
Complex tmp = sum.mul(x);
remove r3;
create r3;
head
data
3
4
sum
new
tmp
Update allocation sites
Add Region Parameters at Call Sites
•
Find allocation nodes reachable from method parameters
and return value
next
public void valueAt(List coeff, Complex x) {
head
create r1;
List rev = coeffs.reverse();
create r3;
Complex sum = new Complex(0,0) in r3;
while (!rev.empty()) {
Complex coeff = (Complex)rev.remove(0);
coeffs
1
remove r3;
create r3;
sum = tmp.plus(coeff);
remove r4;
}
remove r1;
System.out.println(sum.re + “,” + sum.im);
remove r3;
}
Region Analysis for Java
next
data
2
rev
create r4;
Complex tmp = sum.mul(x);
head
data
x
3
4
sum
new
tmp
Update allocation sites
Add Region Parameters at Call Sites
•
Find allocation nodes reachable from method parameters
and return value
next
public void valueAt(List coeff, Complex x) {
head
create r1;
List rev = coeffs.reverse()<r1, r2>;
create r3;
Complex sum = new Complex(0,0) in r3;
while (!rev.empty()) {
Complex coeff = (Complex)rev.remove(0);
coeffs
1
remove r3;
create r3;
sum = tmp.plus(coeff);
remove r4;
}
remove r1;
System.out.println(sum.re + “,” + sum.im);
remove r3;
}
Region Analysis for Java
next
data
2
rev
create r4;
Complex tmp = sum.mul(x);
head
data
x
3
4
sum
new
tmp
Experimental results
Region Analysis for Java
Experimental results
Implementation
• Soot infrastructure for region analysis
• Bytecode level transformation
o Extended bytecodes with region instructions
• Extended the Kaffe VM to run region bytecodes
o Support new opcodes
o Region runtime support
Region Analysis for Java
Experimental results
Evaluation
• Olden benchmarks for Java
• Average size: 861 Methods each program
• Average analysis time: 3.5 sec
o Approximately 15% of total compilation time
Region Analysis for Java
Memory watermark
Kbytes
All memory
Max used with Jreg
35000
30000
25000
20000
15000
10000
5000
Region Analysis for Java
on
oi
vo
r
tsp
dd
ea
er
po
w
tre
pe
r
im
et
er
st
m
lth
he
a
3d
em
bi
so
rt
bh
0
Memory watermark
Kbytes
All memory
Max used with Jreg
Max used with GC
35000
30000
25000
20000
15000
10000
5000
Region Analysis for Java
on
oi
vo
r
tsp
dd
ea
er
po
w
tre
pe
r
im
et
er
st
m
lth
he
a
3d
em
bi
so
rt
bh
0
Conclusions
Transformation technique
• Input standard Java programs
• Output with explicit region support
Concrete analyses
• Reasoning about objects in regions
• Analysis to determine precise region lifetimes
o Non-lexically scoped regions
o Dangling references
Experimental results
• Significant memory savings for some applications
Region Analysis for Java
Thank You!
Region Analysis for Java
Related documents