Comp ositional Pointer and Escap e Analysis for Multithreaded Java
Programs
Martin Rinard John Whaley
Lab oratory for Computer Science IBM Tokyo Research Lab oratory
Massachusetts Institute of Technology Network Computing Platform
Cambridge, MA 02139 1623-14 Shimotsuruma
Yamato-shi, Kanagawa-ken 242-8502 Japan
[email protected] [email protected]
For example, an ob ject escap es if it is reachable from an un- Abstract
analyzed thread running in parallel with the current thread
This pap er presents a new combined p ointer and escap e
or returned to an unanalyzed region of the program.
analysis algorithm for Java programs with unstructured mul-
Combining p oints-to and escap e information in the same
tithreading. The algorithm is based on the abstraction of
analysis enables the algorithm to represent all p otential in-
paral lel interaction graphs, whichcharacterize the p oints-to
teractions b etween the analyzed and unanalyzed regions of
and escap e relationships b etween ob jects and the ordering
the program. The algorithm represents these interactions,
relationships between actions p erformed by multiple par-
in part, by distinguishing b etween two kinds of edges: in-
allel threads. To our knowledge, this algorithm is the rst
side edges, which represent references created within the cur-
interpro cedural, ow-sensitive p ointer analysis algorithm ca-
rently analyzed region, and outside edges, which represent
pable of extracting the p oints-to relationships generated by
references created outside this region. Each outside edge
the interactions between unstructured parallel threads. It
represents a p otential interaction in which the analyzed re-
is also the rst algorithm capable of analyzing interactions
gion reads a reference created in an unanalyzed region. Each
between threads to extract precise escap e information even
inside edge from escap ed no de represents a p otential inter-
for ob jects accessible to multiple threads.
action in which the analyzed region creates a reference that
Wehave implemented our analysis in the IBM Jalap eno ~
an unanalyzed region may read.
dynamic compiler for Java and used the analysis results
Representing p otential interactions with inside and out-
to eliminate redundant synchronization. For our b ench-
side edges leads to an analysis that is comp ositional in two
mark programs, the thread interaction analysis signi cantly
senses:
improves the e ectiveness of the synchronization elimina-
Metho d Comp ositionality: The algorithm analyzes
tion algorithm as compared with previously published tech-
each metho d once to derive a single parameterized
niques, which do not analyze these interactions.
analysis result that records all p otential interactions of
1
the metho d with its callers. At each call site, the al-
1 Intro duction
gorithm matches outside edges from the callee against
inside edges from the caller to compute the e ect of
This pap er presents a new, combined p ointer and escap e
the callee on the p oints-to and escap e information of
analysis algorithm for multithreaded programs. To our knowl-
the caller.
edge, this algorithm is the rst interpro cedural, ow-sensitive
p ointer analysis algorithm for programs with the unstruc-
Thread Comp ositionality: The algorithm analyzes
tured form of multithreading present in Java and similar
each thread once to derive an analysis result that records
languages. It is also the rst algorithm capable of analyz-
all of the p otential interactions of the thread with other
ing interactions between threads to extract precise escap e
parallel threads. The analysis can then combine analy-
information even for ob jects accessible to multiple threads.
sis results from multiple parallel threads by matching
each outside edge from each thread against all cor-
resp onding inside edges from parallel threads. The
1.1 Analysis Overview
result is a single parallel interaction graph that com-
The analysis is based on an abstraction we call paral lel in-
pletely characterizes the p oints-to and escap e informa-
teraction graphs. The no des in this graph represent ob jects;
tion generated by the combined parallel execution of
the edges between no des represent references between ob-
the threads. Unlike previously published algorithms,
jects. For each no de, the analysis also records information
which use an iterative xed-p oint algorithm to com-
that characterizes how it escap es the current analysis region.
pute the interactions [37 , 16 ], the algorithm presented
in this pap er can compute the interactions between
two parallel threads with a single pass over the paral-
lel interaction graphs from the threads.
Finally, the combination of p oints-to and escap e infor-
mation in the same analysis leads to an algorithm that is
1
Recursive metho ds require an iterative algorithm that may ana-
lyze metho ds multiple times to reach a xed p oint.
2 Example designed to analyze arbitrary regions of complete or incom-
plete programs, with the analysis result b ecoming more pre-
In this section we present an example that illustrates how
cise as more of the program is analyzed. Atevery stage in
the analysis works. Figure 1 presents the Java co de for the
the analysis, the current parallel interaction graph provides
example. The sum metho d in the Sum class computes the
complete information ab out the p oints-to relationships for
sum of the numb ers from 0 to n, storing the result into a
ob jects that do not escap e the currently analyzed region
destination accumulator a. It computes this sum by creating
of the program. The algorithm can therefore obtain useful
aworker thread to compute the sum of the even numb ers
analysis information without analyzing the entire program.
while it computes the sum of the o dd numb ers. When they
nish, b oth threads add their contribution into the destina-
1.2 Analysis Uses
tion accumulator. The sum metho d rst constructs a work
Parallel interaction graphs also record the actions that each
vector v of Integers for the worker thread to sum up, then
thread p erforms and contain ordering information for these
initializes the worker ob ject to p oint to the work vector and
actions relative to the actions p erformed by other threads.
the destination accumulator. It starts the worker thread
Optimization and analysis algorithms can use this informa-
running by invoking its start metho d, which invokes the
tion to determine that actions from di erent threads can
run metho d in a new thread running in parallel with the
never execute concurrently and are therefore indep endent.
current thread. This mechanism of initializing a thread ob-
Our compiler uses this ordering information to improve the
ject to p oint to its conceptual parameters is the standard
precision of the thread interaction analysis. It also uses this
way for Java programs to provide threads with the informa-
information to implement a synchronization elimination op-
tion they need to initiate their computation.
timization | if all lo ck acquire and release actions on a
given ob ject are indep endent, they have no e ect on the
class Accumulator {
computation and can b e removed.
int value = 0;
The analysis also provides information that is generally
synchronized void addint v {
useful to compilers and program analysis to ols for multi-
value += v;
threaded programs. Potential applications of our analysis
}
include: sophisticated software engineering to ols such as
}
static race detectors and program slicers [28 , 36]; memory
class Sum {
system optimizations such as prefetching and moving com-
public static void sumint n, Accumulator a {
putation to remote data; automatic batching of long latency
1: Vector v = new Vector;
le system op erations; memory bank disambiguation in com-
for int i = 0; i < n; i += 2 {
pilers for distributed memory machines [8 ]; memory mo dule
v.addElementnew Integeri;
splitting in compilers that generate hardware directly from
}
high-level languages [6 ]; lo ck coarsening [33 , 22 ]; synchro-
2: Worker t = new Worker;
nization elimination and stack allo cation[41 , 10 , 12 , 15 ]; and
t.initv,a;
to provide information required to apply traditional com-
t.start;
piler optimizations such as constant propagation, common
int s = 0;
sub expression elimination, register allo cation, co de motion
for int i = 1; i < n; i+= 2 {
and induction variable elimination to multithreaded pro-
s = s + i;
grams.
}
a.adds;
1.3 Contributions
}
}
This pap er makes the following contributions:
class Worker extends Thread {
Analysis Algorithm: It presents a new combined
Vector work;
p ointer and escap e analysis algorithm for multithreaded
Accumulator dest;
programs. The algorithm is comp ositional at b oth the
void initVector v, Accumulator a {
metho d and thread levels and is designed to deliver
work = v;
useful information without analyzing the entire pro-
dest = a;
gram.
}
public void run {
Analysis Uses: It shows how to use the action order-
3: Enumeration e = work.elements;
ing information present in parallel interaction graphs
int s = 0;
to p erform a synchronization elimination optimization.
while e.hasMoreElements {
Integer i = Integer e.nextElement;
Exp erimental Results: It presents exp erimental re-
s = s + i.intValue;
sults from a prototyp e implementation of the algo-
}
rithms. These results show that the algorithm can
4: dest.adds;
eliminate a signi cantnumb er of synchronization op-
}
erations.
}
The remainder of the pap er is organized as follows. Sec-
tion 2 presents an example that illustrates how the analy-
sis works. Sections 3 through 11 present the analysis algo-
Figure 1: Sum Example
rithms. Section 14 presents exp erimental results, Section 15
presents related work, and we conclude in Section 16.
We contrast the unstructured form of multithreading in
this example with the structured, fork-join form of multi-
Figure 2: Callee-Caller Interaction Between init and sum
Figure 3: Parallel Thread Interaction Between run and sum
Figure 4: Result of Parallel Thread Interaction
In addition to the p oints-to information, the parallel in- threading found in, for example, the Cilk programming lan-
teraction graph also records the synchronization actions that guage [11 ]. Once the thread is the example is created, it exe-
the metho d p erforms and the ob jects to which the actions cutes indep endently of its parent thread and in parallel with
are applied. In this case, the run metho d synchronizes on the rest of its parent thread's computation. Cilk threads, on
the work vector and the destination accumulator. The ac- the other hand, must join with their parent thread, complet-
tions hsync ; 3i and hsync; 4i record these synchronizations. ing b efore their parent thread returns from the pro cedure in
The synchronization on the work vector happ ens inside the which they were spawned.
enumeration's nextElement metho d | Java library classes We now illustrate the analysis by discussing its op era-
suchas Vector often come with the synchronization required tion on this example. We start with the init metho d. This
for correct execution in the face of concurrent access by par- metho d is passed the work vector and destination accumula-
allel threads. In this case, however, the synchronizations tor and initializes the worker to p oint to them. The analysis
are unnecessary. Even though the work vector is accessed result for this metho d is a paral lel interaction graph. Fig-
bymultiple threads, the accesses are separated temp orally ure 2 contains the p oints-to graph from the parallel interac-
by thread start events. Among other things, this example tion graph at the end of the init metho d. In general, our
will showhow the analysis detects the indep endence of the p oints-to graphs contain two kinds of no des: inside no des,
synchronization actions on the work vector. which represent ob jects created during the computation of
We next move to the parallel interaction graph at the end the metho d, and outside no des, which represent ob jects cre-
of the sum metho d. In addition to the p oints-to and action ated outside its computation. All of the no des in the p oints-
information, the graph records the threads that the metho d to graph for the init metho d represent the receiver or ob-
starts and ordering information between the metho d's ac- jects passed into the metho d as parameters. These no des are
tion and started threads. In this case, sum starts the worker therefore outside no des. Our p oints-to graphs also contain
thread, which is represented in the analysis bynode 2. The two kinds of edges: inside edges, which represent references
ordering relation hsync ; 0ijj2 records the fact that a syn- created during the computation of the metho d, and out-
chronization action on ob ject 0 the destination accumula- side edges, which represent references created outside the
tor may execute in parallel with the actions of the worker computation. Because the init metho d reads no references
thread. Note that there is no parallel ordering relation b e- created by other metho ds or threads, all of the edges in its
tween the worker thread and sum's synchronization actions p oints-to graph are inside edges.
on the work vector ob ject. This absence indicates that all
of these actions o ccur b efore the worker thread starts, and
2.1 Interaction Between Caller and Callee
therefore do not execute in parallel with any of the thread's
Figure 2 also presents the p oints-to graph from the sum
actions.
metho d just b efore the call to init. This graph contains
To mo del the parallel interaction, the analysis constructs
one outside no de no de 0, which represents the destination
a bidirectional mapping b etween the no des of the parallel
accumulator passed as a parameter to sum. It also contains
interaction graphs. Initially, the receiver ob ject of the run
two inside no des | no de 1, which represents the work vec-
metho d is mapp ed to the worker thread ob ject from the sum
tor, and no de 2, which represents the worker thread ob ject.
metho d. The analysis then matches outside edges from one
Each inside no de corresp onds to an ob ject creation site and
graph to corresp onding inside edges from the other graph,
represents all ob jects created at that site. In the example, we
using the match to extend the mapping from outside no des
lab el inside no des with the line numb er of the corresp onding
to inside no des. When the mapping is complete, it is used
ob ject creation site in Figure 1.
to combine the graph from the run metho d into the graph
We next discuss how the analysis combines this p oints-to
from the sum metho d. Figure 4 presents the combined graph.
graph with the p oints-to graph from the init metho d to de-
In addition to the p oints-to information, the combination
rive the p oints-to graph after the call to init. The algorithm
algorithm also translates the synchronization actions from
uses the corresp ondence b etween the formal and actual pa-
the worker thread into the new parallel interaction graph. It
rameters to construct a mapping from the outside no des of
tags each action with the thread that p erforms the action.
the init metho d to the no des of the sum metho d. This
So, for example, the action hsync ; 0; 2 i indicates that no de 2
mapping is then used to translate the inside edges from the
the worker thread's no de may p erform a synchronization
init metho d into the p oints-to graph from the sum metho d.
action on an ob ject represented bynode 0.
Figure 2 presents the result of this mapping, which yields
The run metho d also invokes the work.elements metho d,
the p oints-to graph after the call to init.
which creates an enumerator ob ject to enumerate through
elements of the vector. The enumerator ob ject is represented
by an inside no de. Note that b ecause the enumeration ob-
2.2 Interaction Between Parallel Threads
ject is captured in the run metho d, it cannot a ect the com-
We next discuss how the analysis computes the interaction
putation outside this metho d. Therefore, the algorithm do es
between the two threads in the example. Figure 3 presents
not transfer its inside no de into the combined graph.
the parallel interaction graph from the end of the worker
thread's run metho d. This metho d loads the work and dest
2.3 Information in the Combined Graph
references from the receiver ob ject. Because these references
were created outside the run metho d, the analysis uses an
The compiler can extract the following information from the
outside edge to represent each reference. Each of these out-
combined graph. First, the work vector no de is captured in
side edges p oints to sp eci c kind of outside no de called a load
this graph. Even though it is accessed bymultiple threads,
node. In general, there is one load no de for each statement
it is not reachable from outside the total computation of
in the program that loads a reference from an escap ed ob-
the sum metho d. The combined graph therefore completely
ject; that load no de represents all of the ob jects to which the
characterizes the p oints-to information and actions involv-
reference may p oint. In Figure 3, wehave lab eled the load
ing the work vector ob ject. Second, b oth the thread exe-
no des with the numb er of the corresp onding load statement
cuting the sum metho d and the worker thread synchronize from Figure 1.
on the work vector ob ject. But the ordering information The analysis represents the control ow relationships b e-
indicates that none of these synchronizations can o ccur con- tween statements as follows: predst is the set of state-
currently. The synchronization actions have no e ect on the ments that may execute immediately b efore st, and succst
2
computation and can therefore b e removed. Finally, b oth is the set of statements that may execute immediately after
thread synchronize on the destination accumulator ob ject. st . There are two program p oints for each statement st,
But in this case, the synchronization actions from the sum the program p oint st immediately b efore st executes, and
thread may execute in parallel with the synchronization ac- the program p oint st immediately after st executes. The
tions from the worker thread. The compiler will not b e able control ow graph for each metho d op starts with an enter
to remove the synchronization from the destination accumu- statement enter and ends with an exit statement exit .
op op
lator ob ject. The interpro cedural analysis uses call graph information
to compute sets of metho ds that maybeinvoked at metho d
invo cation sites. For each metho d invo cation site m 2 M ,
3 Analysis Abstractions
callees m is the set of metho ds that m mayinvoke. Given a
metho d op, callers op is the set of metho d invo cation sites
In this section we formally present the basic abstractions
that mayinvoke op. The current implementation obtains
that the analysis uses: the program and ob ject represen-
this call graph information using a variant of class hierarchy
tations, p oints-to escap e graphs, and parallel interaction
analysis [17 ], but the algorithm can use any conservative
graphs.
approximation to the actual call graph generated when the
program runs.
3.1 Program Representation
The algorithm represents the program using the following
3.2 Object Representation
analysis ob jects. There is a set l 2 L of lo cal variables and
The analysis represents the ob jects that the program ma-
a set p 2 P of formal parameter variables. There is one for-
nipulates using a set n 2 N of no des. There are several
mal parameter variable for each formal parameter of each
kinds of no des:
metho d in the program. There is also a set cl 2 CL of class
names. The analysis mo dels static class variables using a
There is a set N of inside no des. Inside no des rep-
I
one-of-a-kind no de for each class; the elds of this no de are
resent inside objects, which are ob jects created within
the static class variables for the corresp onding class. The
the current analysis scop e and accessed via references
analysis therefore treats the class name cl as a read-only
created within the current analysis scop e. This set
variable that p oints to the corresp onding one-of-a-kind no de
consists of two subsets:
that contains the class's static class variables. Together, the
{ No des in N represent ob jects created by the cur-
I
lo cal, formal parameter, and class name variables makeup
rent thread. There is one no de in N for each ob-
I
the set v 2 V = L [ P [ CL of variables. There is also a set
ject creation site; that no de represents all ob jects
f 2 F of ob ject elds and a set op 2 OP of metho ds. Ob-
created at that site by the current thread.
ject elds are accessed using syntax of the form v:f . Static
class variables are accessed using syntax of the form cl:f .
N represent ob jects created by threads { No des in
I
Each metho d has a receiver class cl and a formal parameter
running in parallel with the current thread. There
list p ;:::; p . We adopt the convention that parameter p
N for each ob ject creation site; is one no de in
I
0 k 0
p oints to the receiver ob ject of the metho d.
that no de represents all ob jects created at that
The algorithm represents the computation of each metho d
site by threads running in parallel with the cur-
using a control ow graph. The no des of these graphs are
rent thread.
statements st 2 ST . We assume the program has b een pre-
Two no des n 2 N and n 2 N are corresp onding
1 I 2 I
pro cessed so that all statements relevant to the analysis are
no des if they represent ob jects created at the same
in one of the following forms:
ob ject creation site.
A copy statement l = v.
In Java, each thread corresp onds to an ob ject that im-
plements the Runnable interface. The set N N
T I
A load statement l = l :f .
1 2
of runnable no des represents runnable ob jects. N
T
represents runnable ob jects created by the current N
I
A store statement l :f = l .
1 2
thread, and N N represents runnable ob jects cre-
T I
A monitor acquire statement acquire l.
ated by threads running in parallel with the current
thread. N = N [ N .
T T T
A monitor release statement release l.
There is a set N of outside no des. Outside no des rep-
O
A return statement return l , which identi es the re-
resent outside objects, which are ob jects created out-
turn value l of the metho d.
side the current analysis scop e or accessed via refer-
ences created outside the current analysis scop e. This
An ob ject creation site of the form l = new cl .
set consists of several subsets:
A metho d invo cation site m 2 M of the form
{ There is a set N of load no des. When a load
L
l = l :opl ;::: ;l .
0 1
k
statement executes, it loads a value from a eld
A thread start site of the form l:start .
in an ob ject. If the loaded value is a reference, the
analysis must represent the ob ject that the refer-
2
To satisfy the Java memory mo del, the compiler mayhaveto
ence p oints to. Each load no de represents outside
leave memory barriers b ehind at the old synchronization p oints.
ob jects whose references are loaded by the corre-
sp onding load statement. There are two kinds of load no des:
I N F N [ V N is a set of inside edges. In- N contains one no de for each load statement
L
side edges represent references created inside the cur- in the program. That no de represents outside
rent analysis scop e. ob jects whose references are loaded at that
statementby the current thread.
N [N [N [M
P C T
e : N ! 2 is an escap e function that
N contains one no de for each load statement
L
records the escap e information for each no de. Anode
in the program. That no de represents outside
escap es if it is reachable from a parameter no de n 2
P
ob jects whose references are loaded at that
N , a static class variable represented by a eld of a
P
statementby threads running in parallel with
class no de n 2 N , a thread no de n 2 N run-
C C T T
the current thread.
ning in parallel with the current thread, or an ob ject
Two no des n 2 N and n 2 N are corresp ond-
1 L 2 L
passed as a parameter to or returned from an unana-
ing no des if they represent outside ob jects whose
lyzed metho d invo cation site.
references are loaded at the same load statement.
r N is a return set that represents the set of ob-
{ There is a set of return no des N . When the al-
R
jects that may b e returned by the currently analyzed
gorithm skips the analysis of a metho d invo cation
metho d. All no des in the return set escap e to the caller
site, it uses a return no de to represent the return
of the analyzed metho d.
value of the metho d invoked at that site. There
are two kinds of return no des:
Both O and I are graphs with edges lab eled with a eld
from F . We de ne the following op erations on no des of the
contains one no de for each skipp ed metho d N
R
graphs:
invo cation site in the program. That no de
represents ob jects returned by metho ds in-
0
edgesToI; n = fhv;ni2I g[fhhn ; f i;ni2 I g
voked at that site by the current thread.
edgesFromI; v = fhv;ni2I g
N contains one no de for each skipp ed metho d
0
R
edgesFromI; n = fhhn; fi;n i2 I g
invo cation site in the program. That no de
edges I; v = edgesFromI; v
represents ob jects returned by metho ds in-
edgesI; n = edgesToI; n [ edgesFromI; n
voked at that site by threads running in par-
I v = fn:h v;ni2 I g
allel with the current thread.
0 0
I n; f = fn :hhn; fi;n i2 I g
Two no des n 2 N and n 2 N are corresp ond-
1 R 2 R
For eachnode n, the escap e function en and the return
ing no des if they represent ob jects returned at the
set r together record all of the di erentways the no de and
same skipp ed metho d invo cation site.
the ob jects that it represents may escap e from the current
{ N : There is one parameter no de n 2 N for each
P P
analysis scop e. Here are the p ossibilities:
formal parameter in the program. Each param-
eter no de represents the ob ject that its parame-
If a parameter no de n 2 en, then n represents an
p
ter p oints to during the execution of the analyzed
ob ject that may b e reachable from p.
metho d. The receiver ob ject is treated as the rst
If a class no de n 2 en, then n represents an ob-
parameter of each metho d. Given a parameter p ,
cl
ject that may b e reachable from one of the static class
the corresp onding parameter no de is n . There
p
variables of the class cl .
is always an inside edge from p to n .
p
{ N : There is one class no de n 2 N for each class
C C
If a thread no de n 2 en, then n represents an ob ject
T
in the program. The elds of this no de represent
that may b e reachable from a runnable ob ject repre-
the static class variables of its class. Given a class
sented by n .
T
cl, the corresp onding class no de is n . There is
cl
If a metho d invo cation site m 2 en, then n represents
always an inside edge from cl to n .
cl
an ob ject that may b e reachable from the parameters
The set N = N [N [N , and the set N = N [N [N .
I L R I L R
or the return value of an unanalyzed metho d invoked
Given a no de n 2 N , n represents the corresp onding no de
at m.
N . Givenanoden 2 N , n represents the corresp onding in
If n 2 r , then n represents an ob ject that may be
. no de in N
returned to the caller of the analyzed metho d.
The analysis represents each array with a single no de.
This no de has a eld elements , which represents all of the
The escap e information must satisfy the escap e informa-
elements of the array. Because the p oints-to information for
tion propagation invariant that if n p oints to n , then n
1 2 2
all of the array elements is merged into this eld, the analysis
escap es in at least all of the ways that n escap es. We for-
1
do es not make a distinction b etween di erent elements of the
malize this invariant with the following inference rule, which
same array.
states that if there is an edge from n to n , en en .
1 2 1 2
When the analysis adds an edge to the p oints-to escap e
3.3 Points-To Escap e Graphs
graph, it may need to up date the escap e information.
A p oints-to escap e graph is a quadruple of the form hO ; I ; e; r i,
hhn ; fi;n i2 O [ I
1 2
where
en en
1 2
O N F N is a set of outside edges. Outside
L
We say that a no de n violates the propagation con-
1
edges represent references created outside the current
straint if there is an edge from n to n and en 6 en .
1 2 1 2
analysis scop e, either by the caller, by a thread run-
During the analysis of a metho d, the algorithm may add
ning in parallel with the current thread, or by an un-
edges to the p oints-to escap e graph. These edges may make
analyzed invoked metho d.
the up dated no des the no des that the new edges p oint from
5 Parallel Interaction Graphs temp orarily violate the propagation constraint. Whenever it
adds a new edge, the analysis uses the propagate hO ; I ; e; r i;S
The algorithm uses a data ow analysis to generate, at each
algorithm in Figure 5 to propagate escap e information from
program p oint in the metho d, a parallel interaction graph
the no des in S to restore the invariant and pro duce a new
0
hG; ; ; i.
escap e function e that satis es the propagation constraint.
This algorithm takes a p oints-to escap e graph and a set S
G is a p oints-to escap e graph that summarizes the
of no des that may violate the constraint, then uses a work-
p oints-to and escap e information for the current thread.
list approach to propagate the escap e information from the
up dated no des to the other no des in the graph.
The parallel thread map : N !f0; 1; 2g counts the
T
numb er of instances of each thread that may execute
in parallel with the current thread at the current pro-
propagate hO ; I ; e; r i;S
gram p oint. If n = 1, then at most one instance of
Initialize worklist and new escape function
n may execute in parallel with the current thread at
0
e = e
the current program p oint; if n = 2, then multiple
W = S
instances of n may execute in parallel with the current
while W 6= ;do
thread at the current program p oint. We de ne the
Remove a node from worklist
following op erations:
W = W fn g
1
{ x y = min2;x + y
Propagate escape information to al l nodes
that n points to
1
2 if x 2
for all hhn ; fi;n i2O [ I do
{ x y =
1 2
max0;x y otherwise
Restoreconstraint for n
2
0 0 0
n =e n [ e n e
2 2 1
The action set A records the set of actions exe-
0
if e n changed then
2
cuted by the analyzed computation.
W = W [fn g
2
0
The parallel action relation A N records or- returne
T
dering information b etween the actions of the current
thread and threads that execute in parallel with the
Figure 5: Escap e Information Propagation Algorithm
current thread. Sp eci cally, ha; n i2 if a mayhave
T
happ ened after at least one of the thread ob jects rep-
resented by n started executing. In this case, the
Given our abstraction of p oints-to escap e graphs, we can
T
actions of a thread ob ject represented by n may af-
de ne the concepts of escap ed and captured no des as follows:
T
fect a.
escap ed hO ; I ; e; r i;nifen 6= ; or n 2 r , and
removehG; ; ; i;S denotes the parallel interaction graph
capturedhO ; I ; e; r i;nifen=; and n 62 r .
obtained by removing all of the no des in S from hG; ; ; i.
The analysis uses parallel interaction graphs to compute
the interactions b etween parallel threads. During the anal-
4 Actions
ysis, one of the threads is the current thread; conceptually,
no des from the other threads moveinto the context of the
The algorithm is designed to record various actions that the
current thread. In the analyis context of the current thread,
program p erforms on ob jects. Each action consists of an
all of the no des from the other threads come from outside
action lab el that identi es the kind of action p erformed, a
the current thread. The analysis mo dels this by replacing
no de that represents the ob ject on which the action was
each no de from an other thread with its corresp onding ver-
p erformed, and an optional thread no de that represents the
sion from outside the current thread. Given a parallel inter-
thread that p erformed the action. For the purp oses of this
action graph hhO ; I ; e; r i;; ;i, hh O; I; e; r i; ; ; i is the
pap er, the set of action lab els is b 2 B = fld ; sync g. The
parallel interaction graph with no des replaced with the cor-
set of actions is a 2 A =B N [ B N N . Here is
T
resp onding no des from outside the current thread, de ned
the meaning of the actions:
as follows:
hsync ;ni records a synchronization action either a
n if n 2 N
monitor acquire or release p erformed by the current
n =
n otherwise
thread on an ob ject represented by n.
S = fn:n 2 S g
hsync ;n;n i records a synchronization action p erformed
T
O = fhhn ; fi;n i:hhn ; fi;n i2O g
1 2 1 2
by a thread represented by n .
T
I = fhhn ; fi;n i:hhn ; fi;n i2I g[
1 2 1 2
fhv;ni:hv;ni2I g
hld;ni records a load by the current thread on an es-
cap ed no de. The result of the load is a reference to an
en [ en if n 2 N
ob ject represented by the load no de n.
en =
; if n 2 N
en otherwise
hld;n;n i records a load p erformed by the thread n .
T T
r = fn:n 2 r g
if n 2 N n n
It is straightforward to augment the set of action lab els and
n =
0 otherwise
the analysis to record arbitrary actions such as reading and
writing ob jects or invoking a given metho d on an ob ject.
hb ;ni if a = hb;ni
a =
A
It is also straightforward to generalize the set of actions to
hb ;n;n i if a = hb;n;n i
T T
include actions p erformed on multiple ob jects.
= f a:a 2 g
A
= fh a;n i:h a; n i2 g A
statement, a graphical representation of existing edges, a The following op eration removes a set of no des S from a
graphical representation of the existing edges plus the new parallel interaction graph hhO ; I ; e; r i;; ;i.
edges that the statement generates, and a set of side condi-
0 0 0 0 0 0 0
hhO ;I ;e ;r i; ; ; i = removehhO ; I ; e; r i;; ;i;S
tions. The interpretation of eachrow is that whenever the
p oints-to escap e graph contains the existing edges and the
where
side conditions are satis ed, the transfer function for the
statement generates the new edges. Wewould like to p oint
0
S = N S
out several asp ects of the intrapro cedural analysis:
0 0 0
O = O \ S F S
0 0 0
I = I \ S F S
start Statements: At each start statement of the
0 0
e n = en \ S [ M
form l.start, l may p oint to several thread no des.
0 0
r = r \ S
The analysis adds all of these no des to the new parallel
0
thread map.
n if n 2 S
0
n =
0 otherwise
Synchronization: The transfer function for synchro-
0 0 0 0
= \ B S [ B S S
nization statements adds a synchronization action to
0 0 0 0 0
= \ B S [ B S S S
the new parallel interaction graph. This synchroniza-
tion action is recorded as executing in parallel with all
6 Intrapro cedural Analysis
of the thread no des in the current parallel thread map.
This ordering information is used later in the analysis
The analysis of each metho d op starts with the initial par-
to help determine if synchronization actions on a given
allel interaction graph hhO ;I ;e ;r i; ; ; i, de ned as
0 0 0 0 0 0 0
no de are indep endent.
follows:
Outside Edges: A load statementmay add an out-
side edge to the current parallel interaction graph. In
In I , each formal parameter p oints to its corresp ond-
0
this case, the transfer function also records the fact
ing parameter no de and each class p oints to its corre-
that the outside edge is created in parallel with all
sp onding class no de.
of the thread no des in the parallel thread map. This
I = fhp;n i:p 2 Pg[fhcl;n i:cl 2 CLg
ordering information is used during the thread interac-
0 p
cl
tion algorithm to ensure that these outside edges are
not matched with inside edges from threads whose ex-
The initial set of outside edges is empty: O = ;
0
ecution starts after the execution of the load statement
The initial escap e function e is set up so that each
that generated the outside edge.
0
parameter or class no de is marked as escaping via it-
We next present the data ow analysis framework from
self.
the intrapro cedural analysis. This framework includes the
fn g if n 2 N [ N
P C
e n=
0
transfer functions for the basic statements and the de nition
; otherwise
of the con uence op erator at merge p oints in the control- ow
graph.
The initial return set and action set are empty: r = ;,
0
and = ;.
0
6.1 Copy Statements
Initially there are no threads running in parallel with
A copy statement of the form l = v makes l p oint to the
the current thread. 8n 2 N : n =0.
T T 0 T
ob ject that v p oints to. The transfer function up dates I to
The initial parallel action relation is empty: = ;.
re ect this change by killing the current set of edges from l,
0
then generating additional inside edges from l to all of the
The algorithm analyzes the metho d under the assump-
no des that v p oints to.
tion that the parameters and static class variables all p oint
Kill = edges I; l to di erent ob jects. If the metho d maybeinvoked in a call-
I
Gen = fl gI v ing context in which some of these p ointers p oint to the same
I
0
ob ject, this ob ject will b e represented bymultiple no des dur-
I = I Kill [ Gen
I I
ing the analysis of the metho d. In this case, the analysis
describ ed b elow in Section 9 will merge the corresp onding
6.2 Load Statements
outside ob jects when it combines the nal analysis result for
A load statement of the form l = l :f makes l p ointtothe
1 2 1
the metho d into the calling context at the metho d invo ca-
ob ject that l :f p oints to. The analysis mo dels this change
2
tion site. Because the combination algorithm retains all of
by constructing a set S of no des that represent all of the
the edges from the merged ob jects, it conservatively mo dels
ob jects to which l :f may p oint, then generating additional
2
the actual e ect of the metho d.
inside edges from l to every no de in this set.
1
The intrapro cedural analysis is a data ow analysis that
All no des accessible via inside edges from l :f should
2
propagates parallel interaction graphs through the state-
clearly b e in S . But if l p oints to an escap ed no de, other
2
ments of the metho d's control ow graph. The transfer func-
0 0 0 0 0 0 0
parts of the program such as the caller or threads executing
tion hhO ;I ;e ;r i; ; ; i = [[ st ]] hhO ; I ; e; r i; ; ;i de-
in parallel with the current thread can access the referenced
nes the e ect of each statement st on the current parallel
ob ject and store values in its elds. In particular, the value
interaction graph. Most of the statements rst kill a set
in l :f may have b een written by the caller or a thread
2
of inside edges, then generate additional inside and outside
running in parallel with the current thread | in other words,
edges. Figure 6 graphically presents the rules that deter-
l :f may contain a reference created outside of the current
2
mine the sets of generated edges for the di erent kinds of
analysis scop e. The analysis uses an outside edge to mo del
statements. Eachrow in this gure contains four items: a
Figure 6: Generated Edges for Basic Statements
this reference. The outside edge p oints to the load no de for 6.5 Object Creation Sites
the load statement, which is the outside no de that represents
An ob ject creation site of the form l = new cl allo cates a
the ob jects that the reference may p oint to.
new ob ject and makes l p oint to the ob ject. The analysis
The analysis must therefore consider two cases: the case
represents all ob jects allo cated at a sp eci c creation site
when l do es not p oint to an escap ed no de, and the case
2
with the creation site's inside no de n. The transfer function
when l do es p oint to an escap ed no de. The algorithm
2
mo dels the e ect of the statementby killing all edges from
determines which case applies by computing S , the set of
E
l, then generating an inside edge from l to n.
escap ed no des to which l p oints. S is the set of no des
2 I
accessible via inside edges from l :f .
2
Kill = edges I; l
I
Gen = fhl;nig
S = fn 2 I l :escap ed hO ; I ; e; r i;n g
I
E 2 2 2
0
I = I Kill [ Gen
S = [fI n ; f:n 2 I l g
I I
I 2 2 2
If S = ; i.e., l do es not p oint to an escap ed no de,
E 2
6.6 Return Statements
S = S and the transfer function simply kills all edges from
I
l , then generates inside edges from l to all of the no des
1 1
A return statement return l sp eci es the return value for
in S .
the metho d. The immediate successor of each return state-
Kill = edges I; l
I 1
ment is the exit statement of the metho d. The analysis
Gen = fl gS
I 1
mo dels the e ect of the return statementby up dating r to
0
I = I Kill [ Gen
I I
include all of the no des that l p oints to.
If S 6= ; i.e., l p oints to at least one escap ed no de,
E 2
0
r = I l
S = S [fn g, where n is the load no de for the load
I L L
statement. In addition to killing all edges from l , then
1
generating inside edges from l to all of the no des in S , the
1
6.7 Control-Flow Join Points
transfer function also generates outside edges from the es-
To analyze a statement, the algorithm rst computes the
cap ed no des to n and propagates the escap e information
L
join of the parallel interaction graphs owing into the state-
from the escap ed no des through n . It also generates a
L
ment from all of its predecessors. It then applies the transfer
load action hld ;ni and up dates the parallel action relation
function to obtain a new parallel interaction graph at the
to record the fact that the action may execute in parallel
p oint after the statement. The join op eration t is de ned
with thread ob jects represented by the current set of paral-
as follows.
lel thread no des. In this case, the new outside edges may
represent references created by these parallel thread ob jects.
hhO ; I ; e; r i;; ;i =
Kill = edges I; l
hhO ;I ;e ;r i; ; ; it hhO ;I ;e ;r i; ; ; i
I 1
1 1 1 1 1 1 1 2 2 2 2 2 2 2
Gen = fl gS
I 1
0
where O = O [ O , I = I [ I , 8n 2 N:en=e n [ e n,
I = I Kill [ Gen
1 2 1 2 1 2
I I
r = r [ r , 8n 2 N: n = max n; n, = [ ,
Gen = S ffg fn g
1 2 1 2 1 2
O E L
0
and = [ .
O = O [ Gen
1 2
O
0 0 0
The corresp onding partial order v is
e = propagate hO ;I ;e;ri;S
E
0
= [fhld ;n ig
L
0
hhO ;I ;e ;r i; ; ; iv hhO ;I ;e ;r i; ; ; i
1 1 1 1 1 1 1 2 2 2 2 2 2 2
= [ fh ld;n igfn : n > 0g
L T T
if O O , I I , 8n 2 N:e n e n, r r ,
1 2 1 2 1 2 1 2
6.3 Store Statements
8n 2 N: n n, , and . Bottom is
1 2 1 2 1 2
hh;; ;;e ; ;i; ; ;; ;i, where 8n 2 N:e n = ; and 8n 2
A store statement of the form l :f = l nds the ob ject
? ? ?
1 2
N: n=0.
to which l p oints, then makes the f eld of this ob ject
?
1
p oint to same ob ject as l . The analysis mo dels the e ect
2
of this assignmentby nding the set of no des that l p oints
1
6.8 Analysis Results
to, then generating inside edges from all of these no des to
The analysis of each metho d pro duces analysis results st
the no des that l p oints to. It also propagates the escap e
2
and st b efore and after each statement st in the metho d's
information from all of the no des that l p oints to through
1
control ow graph. The analysis result satis es the fol-
the no des that l p oints to.
2
lowing equations:
Gen = I l ff g I l
I 1 2
0
I = I [ Gen
I
enter = hhO ;I ;e ;r i; ; ; i
op 0 0 0 0 0 0 0
0 0 0
0 0
e = propagate hO ;I ;e;ri;Il
1
st = tf st :st 2 predstg
st = [[ st ]] st
6.4 Acquire and Release Statements
The nal analysis result of metho d op is the analysis result
An acquire statement of the form acquire l nds the ob-
at the program p oint after the exit no de, i.e., exit .
op
ject to which l p oints, then acquires that ob ject's lo ck. A
As describ ed b elow in Section 9.4, the analysis solves these
release statement of the form release l nds the ob ject to
equations using a standard worklist algorithm.
which l p oints, then releases that ob ject's lo ck. The analy-
sis mo dels the e ect of these statements by nding the set
7 Matching Inside and Outside Edges
of no des that l p oints to, then recording synchronization
actions on all of these no des.
In the interpro cedural analysis, outside edges in the callee's
0
= [ fsync gI l
parallel interaction graph represent inside edges in the caller's
0
= [ fsync gI l fn : n > 0g
T T
parallel interaction graph. To compute the e ect of a metho d
call, the analysis matches the callee's outside edges against 8 Combining Points-To Escap e Graphs
the corresp onding inside edges from the caller. In the inter-
Once the matching algorithm has set up the mapping b e-
thread analysis, outside edges in the parallel interaction
tween inside and outside no des in the two graphs, the combi-
graphs of each thread represent inside edges in the parallel
nation algorithm uses the mapping to generate a new p oints-
interaction graph of the other thread. To compute the inter-
to escap e graph that re ects the nal p oints-to and escap e
actions, the analysis matches outside edges from each thread
relationships generated by the interaction.
against inside edges from the other thread. The matching
The combination algorithm takes two p oints-to escap e
pro cess is conceptually similar in b oth cases. This section
graphs hO ;I ;e ;r i i 2f1; 2g and two initial mappings
i i i i
discusses the matching algorithm we use for b oth the inter-
: N ! N . It pro duces the nal p oints-to escap e graph
i
pro cedural and the inter-thread analyses.
0 0 0 0
hO ;I ;e ;r i. The combination algorithm p erforms two ba-
The matching algorithm takes two p oints-to escap e graphs
sic tasks: it traces out reachable edges and no des from the
hO ;I ;e ;r i i 2f1; 2g and two initial mappings : N !
i i i i i
0
two input p oints-to escap e graphs so that they are present
N . It pro duces two new mappings : N ! N that extend
i
in the nal graph, and it uses the mappings b etween out-
the initial mappings to map the outside no des in each graph
side and inside no des to translate inside edges into the nal
to the corresp onding no des in the other graph.
graph.
0 0
h ; i = matchhO ;I ;e ;r i; hO ;I ;e ;r i; ;
1 1 1 1 2 2 2 2 1 2
1 2
0 0 0 0 0 0
hhO ;I ;e ;r i; ; i =
1 2
We formulate the mapping using set inclusion constraints [1 ].
combinehO ;I ;e ;r i; hO ;I ;e ;r i; ;
1 1 1 1 2 2 2 2 1 2
This formulation enables us to present a compact, simple
As for the mapping algorithm, we sp ecify the combina-
sp eci cation of the mapping result using a set of constraint
tion result using a system of set inclusion constraints. Fig-
rules. Figure 7 presents the constraints that the matching
ure 8 presents the constraints that sp ecify the result of the
algorithm must satisfy. Note that these contraints use the
combination algorithm. These constraints extend the initial
i to represent the complement of i; i.e. 1 = 2, notation
0
mappings to two new mappings : N ! N . These new
2=1. The constraints basically sp ecify that if an outside
i
0
mappings have the prop erty that n 2 nif n is reachable
edge from one graph matches an inside edge from the other
i
in the nal p oints-to escap e graph and should be present
graph, then the mappings must map the outside edge's no de
in that graph. These constraints start with a set of no des
to the inside edge's no de. This no de mapping p otentially
that will b e mapp ed into the combined graph. They then
enables more edge matchings.
trace out the reachable no des to determine the complete
Figure 9 presents the algorithm that solves these con-
set of no des that should b e present in the combined graph.
straints. It op erates by rep eatedly nding a no de in one
Figures 10 and 11 present an algorithm for solving the con-
graph that is already mapp ed to a no de in the other graph.
straint system in Figure 8. We highlight several prop erties
It then checks if there is an inside edge from one of these
of this algorithm:
no des that it can match up with a corresp onding outside
edge from the other no de. If so, it up dates one of the map-
Base No des: The analysis starts with a set of base
pings to re ect the fact that the no de that the outside edge
no des mapp ed into the combined graph. In the case of
p oints to is mapp ed to the no de that the inside edge p oints
caller/callee interaction, all of the no des from the caller
to.
are mapp ed directly into the combined graph. The
class no des from the callee are also mapp ed directly,
while the parameter no des are mapp ed indirectly to
matchhO ;I ;e ;r i; hO ;I ;e ;r i; ;
1 1 1 1 2 2 2 2 1 2
mo del the semantics of metho d invo cation.
Initialize worklists and results
for i =1; 2do
0
Inside No des: An inside no de from one of the parallel
=
i
i
interaction graphs is present in the combined graph if
W = fhn ;n i:n 2 n g
i 1 3 3 i 1
it is reachable from the base no des.
D = ;
i
while cho ose hn ;n i2W do
1 3 i
Inside Edges: If two no des are mapp ed into the com-
Remove a pair from worklist
bined graph, the analysis uses the mapping to translate
W = W fhn ;n ig
i i 1 3
insided edges b etween the two no des into the graph.
D = D [fhn ;n ig
i i 1 3
Check outside edges for rule 2
Load No des: A load no de is mapp ed into the com-
for all hhn ; f i;n i2O do
bined graph if it is reachable from an escap ed base
1 2 i
for all hhn ; fi;n i2I do
no de.
3 4
i
0 0
n [fn g n =
2 4 2
i i
Outside Edges: Outside edges are translated into
if hn ;n i62D then
2 4 i
the combined graph if the no de that they come from
W = W [fhn ;n ig
i i 2 4
is mapp ed into the graph and at least one of the no des
Check inside edges for rule 3
that it maps to is escap ed in the graph.
for all hhn ; f i;n i2I do
1 2 i
for all hhn ; fi;n i2O do
3 4
i
0 0
n = n [fn g
4 4 2
i i
8.0.1 Constraint Solution Algorithm
if hn ;n i62D then
4 2
i
= W [hn ;n i W
4 2
We next discuss the constraint solution algorithm in Fig-
i i
0 0
return h ; i
ures 10 and 11 for the constraint system in Figure 8. The al-
1 2
gorithm directly re ects the structure of the inference rules.
At each step it detects an inference rule antecedent that b e-
Figure 9: Algorithm for Matching Inside and Outside No des
comes true, then takes action to ensure that the consequent is also true.
Figure 7: Constraints for Matching Inside and Outside Edges
Figure 8: Constraints for Combining Points-to Escap e Graphs
The mapNo den ;n;i pro cedure in Figure 10 is invoked
1
whenever the algorithm maps a no de n from p oints-to es-
1
cap e graph i toanoden in the new graph. It rst matches
inside edges involving n in p oints-to escap e graph i to inside
1
edges involving n in the new graph. The pro cedure checks
mapNo den ;n;i
1
any edges to n that have already b een previously translated
1
if hn ;n;ii62 D then
1
into the new graph to see if they should also b e translated to
D = D [fhn ;n;iig
1
p ointton in the new graph. It also checks all of the inside
0 0
n = n [fng
1 1
i i
edges from n to see if they should b e translated into the
1
Add delayed inside edges for rule 7
new graph. The pro cedure then checks outside edges from
0 0
I = I [ n fng
i 1
n to see if they should b e translated into the new graph.
1
0 0
0
S = fn 2 N:h n ; fi2 n g
1
Finally, the pro cedure up dates the new escap e function e
Check inside edges for rules 5 and 7
to re ect the e ect of the newly mapp ed no des and newly
for all hhn ; fi;n i2 I
1 2 i
translated edges.
Add inside edges for rule 7
Figure 11 presents the driver for the constraint solution
0 0 0
I
I = I [fhn; fig n
2
i
algorithm. It maintains a worklist W of inside no des from
i
S = S [fng
graph i that should be mapp ed into the new graph, and
O
n = n [fhn; f ig
i 2 i 2
a worklist W of outside edges that should be translated
i
Check conditions for rule 5
into the new graph if the edge's source no de is escap ed in
if n 2 N [ N
2 I R
the new graph. When the algorithm pro cesses a no de from
I I
I
W = W [fn g
2
W , it calls mapNo de to map that no de into the new graph.
i i
i
O
Check outside edges for rule 6
When the algorithm pro cesses an outside edge from W ,it
i
for all hhn ; fi;n i2 O
1 2 i
translates the edge into the new graph and maps the edge's
O O
W = W [fhhn; fi;n ig
target no de into the new graph.
2
i i
Update escape information for rules 8 and 9
There is a slight complication in the algorithm. As the al-
0 0
e n=e n [ e n
gorithm executes, it p erio dically translates inside edges into
i 1
0 0 0 0 0
e = propagate hO ;I ;e ;r i;S
the new graph. Whenever a no de n from graph i is mapp ed
1
to n, the algorithm translates each inside edge hhn ; fi;n i
1 2
from graph i into the new graph. This translation pro cess in-
serts a corresp onding inside edge from n to each no de that
0
Figure 10: Algorithm for Mapping One No de to Another
n maps to; i.e., to each no de in n . The algorithm
2 2
i
must ensure that when it completes, there is one such edge
0
combinehO ;I ;e ;r i; hO ;I ;e ;r i; ;
1 1 1 1 2 2 2 2 1 2
n . But when the for each no de in the nal set of no des
2
i
Initialize worklists and results
algorithm rst translates hhn ; fi;n i into the new graph,
1 2
0
D = ;
n may not be complete. In this case, the algorithm
2
i
for i =1; 2do
will eventually map n to more no des, increasing the set of
2
O I
0
i = h;; ;i ;W hW
n . There should b e edges from n to all of the no des in
2 i i
i
0
for all n 2 N do n=;
no des in the nal n , not just to those no de that were
i
2
i
0
0
for all n 2 N do n=;
presentin n when the algorithm mapp ed hhn ; fi;n i
2 1 2
i
i
0 0 0
hO ;I ;r i = h;; ;; ;i
into the new graph.
0
for all n 2 N do e n=;
The algorithm ensures that all of these edges are present
Cal l mapNo de for existing mappings
in the nal graph by building a set n of delayed actions.
i 2
for all hn ;n;ii such that n 2 n do
Each delayed action consists of a no de in the new graph and
1 i 1
mapNo den ;n;i
a eld in that no de. Whenever the no de n is mapp ed to a
1
2
0 0 0 0
done = false
new no de n i.e., the algorithm sets n = n [fn g,
2 2
i i
do
the algorithm establishes a new inside edge for each delayed
I
action. The new edge go es from the no de in the action to
if cho ose n 2 W then
i
0
I I
the newly mapp ed no de n . These edges ensure that the
W = W fng
i i
nal set of inside edges satis es the constraints.
mapNo den; n; i
O
such that else if cho ose hhn; fi;n i2 W
1
i
0 0 0
escap ed hO ;I ;e ; ;i;n then
9 Interpro cedural Analysis
I I