CPA IN ACTION
The key insight of CPA is turning the analysis of each call site into a case analysis. During the analysis of a call site, the algorithm computes the Cartesian product of the types of the actual arguments (including the receiver) and then analyzes each tuple in the product as an independent case. This eliminates the dependency on iteration of many other type inference algorithms, as exact type information for each case is immediately available. The type information is used to curb undesired type merging and to avoid redundant analysis through the sharing of cases.
Agesen makes the important observation that there is no such thing as a polymorphic method; only polymorphic call sites may exist. A call site that can invoke a method f()
with either int
or float
receivers, for example, will sometimes invoke f
with an integer receiver and other times with a floating point receiver. But, given a particular invocation, the receiver is either an int
or a float
; it cannot be both. CPA makes use of this property by generating templates in which all formal arguments have monomorphic types.
Let receiver.f(argument)
be a call site, and let R and A represent the type sets of the receiver and argument, respectively. Suppose that R = {r1, r2,..., rm} and A = {a1, a2,..., an}. To analyze the call site, CPA takes the Cartesian product of the receiver types and the argument types:
R × A = {(r1, a1),..., (r1, an),..., (ri, aj),..., (rm, a1),..., (rm, an)}
The algorithm then produces an f
template for each element of R × A. If previous analysis has already generated a template for a given (r, a) pair, it is reused; otherwise, a new template is created and will be available for future sends. The union of the result types of the templates connected to a call site yields the type of that call site!
Each method has what Agesen refers to as a "repository of templates". All template specializations of the particular method are included in this repository, regardless of the call sites that initiated their creation. Such a relation is maintained to facilitate the sharing of templates among different call sites with Cartesian products having tuples in common.
With the templates and repositories in place, Agesen's approach to the analysis of a call site can be summarized by four steps:
- Determine the methods that the call may invoke (callees).
- Generate the Cartesian product for current type information. (See the following subsection, "Avoiding Iteration".)
- For each callee M and each tuple in the Cartesian product, look up the tuple in M's template repository. If no match is present, create a new template and add it to the repository.
- Connect the send to the (old or newly created) template.
Template repositories are not filled in advance; rather, templates are added gradually as new argument combinations are encountered during analysis of the target program's call sites.
AVOIDING ITERATION
How can CPA use the types of receivers and arguments while these types are still being computed? Other algorithms have tackled the problem through iterating and approximating types based on previous iterations, but CPA uses neither iteration nor approximation. The Cartesian product (unlike a type comparison to determine whether a template may be shared) can be computed correctly - gradually, but correctly - even if the types are not fully known until analysis has been completed. When a call site is processed initially, the Cartesian product computed is that of the current types of the receiver and arguments. The call site is connected to the resulting templates. If one or more of the types involved later grows, CPA returns to the call site and extends the Cartesian product, adding the new combinations. Because types grow monotonically during type inference, such an approach produces a correct result. (The Cartesian product is a monotonic function; if either |A| or |B| increases, |A × B| increases.)
ADDITIONAL READING
For more information on CPA and its advantages over other type inference algorithms in handling parametric polymorphism, see the following documents. Information on resolution of dynamic dispatch and inheritance in CPA is also provided:
Home |
DMP |
Project |
Journal |
Contact
Relevant Java Classes |
Delete Call Site |
Template Call Graph |
Research |
Final Report
Previous (Polymorphism)
Next (Flow-Sensitive Analysis of Variable Accesses)
©2002, Katie Heise