1 package Analysis.OwnershipAnalysis;
3 import Analysis.CallGraph.*;
10 public class OwnershipAnalysis {
12 ///////////////////////////////////////////
14 // Public interface to discover possible
15 // aliases in the program under analysis
17 ///////////////////////////////////////////
18 public HashSet<AllocationSite>
19 getFlaggedAllocationSitesReachableFromTask( TaskDescriptor td ) {
21 return getFlaggedAllocationSitesReachableFromTaskPRIVATE( td );
24 public AllocationSite getAllocationSiteFromFlatNew( FlatNew fn ) {
25 return getAllocationSiteFromFlatNewPRIVATE( fn );
28 public boolean createsPotentialAliases( Descriptor taskOrMethod,
32 OwnershipGraph og = mapDescriptorToCompleteOwnershipGraph.get( taskOrMethod );
35 return createsPotentialAliases( og,
36 getHeapRegionIDset( og, paramIndex1 ),
37 getHeapRegionIDset( og, paramIndex2 ) );
40 public boolean createsPotentialAliases( Descriptor taskOrMethod,
42 AllocationSite alloc ) {
44 OwnershipGraph og = mapDescriptorToCompleteOwnershipGraph.get( taskOrMethod );
47 return createsPotentialAliases( og,
48 getHeapRegionIDset( og, paramIndex ),
49 getHeapRegionIDset( alloc ) );
52 public boolean createsPotentialAliases( Descriptor taskOrMethod,
56 OwnershipGraph og = mapDescriptorToCompleteOwnershipGraph.get( taskOrMethod );
59 return createsPotentialAliases( og,
60 getHeapRegionIDset( og, paramIndex ),
61 getHeapRegionIDset( alloc ) );
64 public boolean createsPotentialAliases( Descriptor taskOrMethod,
65 AllocationSite alloc1,
66 AllocationSite alloc2 ) {
68 OwnershipGraph og = mapDescriptorToCompleteOwnershipGraph.get( taskOrMethod );
71 return createsPotentialAliases( og,
72 getHeapRegionIDset( alloc1 ),
73 getHeapRegionIDset( alloc2 ) );
76 public boolean createsPotentialAliases( Descriptor taskOrMethod,
78 HashSet<AllocationSite> allocSet ) {
80 OwnershipGraph og = mapDescriptorToCompleteOwnershipGraph.get( taskOrMethod );
83 return createsPotentialAliases( og,
84 getHeapRegionIDset( alloc ),
85 getHeapRegionIDset( allocSet ) );
88 // use the methods given above to check every possible alias
89 // between task parameters and flagged allocation sites reachable
91 public void writeAllAliases( String outputFile ) throws java.io.IOException {
93 BufferedWriter bw = new BufferedWriter( new FileWriter( outputFile ) );
95 // look through every task for potential aliases
96 Iterator taskItr = state.getTaskSymbolTable().getDescriptorsIterator();
97 while( taskItr.hasNext() ) {
98 TaskDescriptor td = (TaskDescriptor) taskItr.next();
100 HashSet<AllocationSite> allocSites = getFlaggedAllocationSitesReachableFromTask( td );
102 // for each task parameter, check for aliases with
103 // other task parameters and every allocation site
104 // reachable from this task
105 FlatMethod fm = state.getMethodFlat( td );
106 for( int i = 0; i < fm.numParameters(); ++i ) {
108 // for the ith parameter check for aliases to all
109 // higher numbered parameters
110 for( int j = i + 1; j < fm.numParameters(); ++j ) {
111 if( createsPotentialAliases( td, i, j ) ) {
112 bw.write( "Task "+td+" potentially aliases parameters "+i+" and "+j+".\n" );
116 // for the ith parameter, check for aliases against
117 // the set of allocation sites reachable from this
119 Iterator allocItr = allocSites.iterator();
120 while( allocItr.hasNext() ) {
121 AllocationSite as = (AllocationSite) allocItr.next();
122 if( createsPotentialAliases( td, i, as ) ) {
123 bw.write( "Task "+td+" potentially aliases parameter "+i+" and "+as+".\n" );
128 // for each allocation site check for aliases with
129 // other allocation sites in the context of execution
131 Iterator allocItr = allocSites.iterator();
132 while( allocItr.hasNext() ) {
133 AllocationSite as = (AllocationSite) allocItr.next();
134 if( createsPotentialAliases( td, as, allocSites ) ) {
135 bw.write( "Task "+td+" potentially aliases "+as+" and the rest of the set.\n" );
143 ///////////////////////////////////////////
145 // end public interface
147 ///////////////////////////////////////////
156 // data from the compiler
158 private CallGraph callGraph;
159 private int allocationDepth;
161 // used to identify HeapRegionNode objects
162 // A unique ID equates an object in one
163 // ownership graph with an object in another
164 // graph that logically represents the same
166 static private int uniqueIDcount = 0;
169 // Use these data structures to track progress of
170 // processing all methods in the program, and by methods
171 // TaskDescriptor and MethodDescriptor are combined
172 // together, with a common parent class Descriptor
173 private HashSet <Descriptor> descriptorsToVisit;
174 private Hashtable<Descriptor, OwnershipGraph> mapDescriptorToCompleteOwnershipGraph;
175 private Hashtable<FlatNew, AllocationSite> mapFlatNewToAllocationSite;
176 private Hashtable<Descriptor, HashSet<AllocationSite> > mapDescriptorToAllocationSiteSet;
178 // Use these data structures to track progress of one pass of
179 // processing the FlatNodes of a particular method
180 private HashSet <FlatNode> flatNodesToVisit;
181 private Hashtable<FlatNode, OwnershipGraph> mapFlatNodeToOwnershipGraph;
182 private HashSet <FlatReturnNode> returnNodesToCombineForCompleteOwnershipGraph;
185 // this analysis generates an ownership graph for every task
187 public OwnershipAnalysis( State state,
189 int allocationDepth ) throws java.io.IOException {
191 this.callGraph = callGraph;
192 this.allocationDepth = allocationDepth;
194 descriptorsToVisit = new HashSet<Descriptor>();
196 mapDescriptorToCompleteOwnershipGraph =
197 new Hashtable<Descriptor, OwnershipGraph>();
199 mapFlatNewToAllocationSite =
200 new Hashtable<FlatNew, AllocationSite>();
202 mapDescriptorToAllocationSiteSet =
203 new Hashtable<Descriptor, HashSet<AllocationSite> >();
205 // use this set to prevent infinite recursion when
206 // traversing the call graph
207 HashSet<Descriptor> calleesScheduled = new HashSet<Descriptor>();
210 // initialize methods to visit as the set of all tasks in the
211 // program and then any method that could be called starting
213 Iterator taskItr = state.getTaskSymbolTable().getDescriptorsIterator();
214 while( taskItr.hasNext() ) {
215 Descriptor d = (Descriptor) taskItr.next();
216 descriptorsToVisit.add( d );
218 // recursively find all callees from this task
219 scheduleAllCallees( calleesScheduled, d );
222 // before beginning analysis, initialize every scheduled method
223 // with an ownership graph that has populated parameter index tables
224 // by analyzing the first node which is always a FlatMethod node
225 Iterator<Descriptor> dItr = calleesScheduled.iterator();
226 while( dItr.hasNext() ) {
227 Descriptor d = dItr.next();
228 OwnershipGraph og = new OwnershipGraph( allocationDepth );
231 if( d instanceof MethodDescriptor ) {
232 fm = state.getMethodFlat( (MethodDescriptor) d );
234 assert d instanceof TaskDescriptor;
235 fm = state.getMethodFlat( (TaskDescriptor) d );
238 analyzeFlatNode( d, fm, null, og );
239 mapDescriptorToCompleteOwnershipGraph.put( d, og );
242 // as mentioned above, analyze methods one-by-one, possibly revisiting
243 // a method if the methods that it calls are updated
247 // called from the constructor to help initialize the set
248 // of methods that needs to be analyzed by ownership analysis
249 private void scheduleAllCallees( HashSet<Descriptor> calleesScheduled,
251 if( calleesScheduled.contains( d ) ) {
254 calleesScheduled.add( d );
256 Set callees = callGraph.getCalleeSet( d );
257 if( callees == null ) {
261 Iterator methItr = callees.iterator();
262 while( methItr.hasNext() ) {
263 MethodDescriptor md = (MethodDescriptor) methItr.next();
264 descriptorsToVisit.add( md );
266 // recursively find all callees from this task
267 scheduleAllCallees( calleesScheduled, md );
272 // manage the set of tasks and methods to be analyzed
273 // and be sure to reschedule tasks/methods when the methods
274 // they call are updated
275 private void analyzeMethods() throws java.io.IOException {
277 while( !descriptorsToVisit.isEmpty() ) {
278 Descriptor d = (Descriptor) descriptorsToVisit.iterator().next();
279 descriptorsToVisit.remove( d );
281 // because the task or method descriptor just extracted
282 // was in the "to visit" set it either hasn't been analyzed
283 // yet, or some method that it depends on has been
284 // updated. Recompute a complete ownership graph for
285 // this task/method and compare it to any previous result.
286 // If there is a change detected, add any methods/tasks
287 // that depend on this one to the "to visit" set.
289 System.out.println( "Analyzing " + d );
292 if( d instanceof MethodDescriptor ) {
293 fm = state.getMethodFlat( (MethodDescriptor) d );
295 assert d instanceof TaskDescriptor;
296 fm = state.getMethodFlat( (TaskDescriptor) d );
299 OwnershipGraph og = analyzeFlatMethod( d, fm );
300 OwnershipGraph ogPrev = mapDescriptorToCompleteOwnershipGraph.get( d );
301 if( !og.equals( ogPrev ) ) {
302 mapDescriptorToCompleteOwnershipGraph.put( d, og );
304 og.writeGraph( d, true, true, true, false );
306 // only methods have dependents, tasks cannot
307 // be invoked by any user program calls
308 if( d instanceof MethodDescriptor ) {
309 MethodDescriptor md = (MethodDescriptor) d;
310 Set dependents = callGraph.getCallerSet( md );
311 if( dependents != null ) {
312 descriptorsToVisit.addAll( dependents );
321 // keep passing the Descriptor of the method along for debugging
322 // and dot file writing
323 private OwnershipGraph
324 analyzeFlatMethod( Descriptor mDesc,
325 FlatMethod flatm ) throws java.io.IOException {
327 // initialize flat nodes to visit as the flat method
328 // because all other nodes in this flat method are
329 // decendents of the flat method itself
330 flatNodesToVisit = new HashSet<FlatNode>();
331 flatNodesToVisit.add( flatm );
333 // initilize the mapping of flat nodes in this flat method to
334 // ownership graph results to an empty mapping
335 mapFlatNodeToOwnershipGraph = new Hashtable<FlatNode, OwnershipGraph>();
337 // initialize the set of return nodes that will be combined as
338 // the final ownership graph result to return as an empty set
339 returnNodesToCombineForCompleteOwnershipGraph = new HashSet<FlatReturnNode>();
341 while( !flatNodesToVisit.isEmpty() ) {
342 FlatNode fn = (FlatNode) flatNodesToVisit.iterator().next();
343 flatNodesToVisit.remove( fn );
345 // perform this node's contributions to the ownership
346 // graph on a new copy, then compare it to the old graph
347 // at this node to see if anything was updated.
348 OwnershipGraph og = new OwnershipGraph( allocationDepth );
350 // start by merging all node's parents' graphs
351 for( int i = 0; i < fn.numPrev(); ++i ) {
352 FlatNode pn = fn.getPrev( i );
353 OwnershipGraph ogParent = getGraphFromFlatNode( pn );
354 og.merge( ogParent );
357 // apply the analysis of the flat node to the
358 // ownership graph made from the merge of the
360 analyzeFlatNode( mDesc,
362 returnNodesToCombineForCompleteOwnershipGraph,
365 // if the results of the new graph are different from
366 // the current graph at this node, replace the graph
367 // with the update and enqueue the children for
369 OwnershipGraph ogPrev = getGraphFromFlatNode( fn );
371 if( !og.equals( ogPrev ) ) {
372 setGraphForFlatNode( fn, og );
374 for( int i = 0; i < fn.numNext(); i++ ) {
375 FlatNode nn = fn.getNext( i );
376 flatNodesToVisit.add( nn );
381 // end by merging all return nodes into a complete
382 // ownership graph that represents all possible heap
383 // states after the flat method returns
384 OwnershipGraph completeGraph = new OwnershipGraph( allocationDepth );
385 Iterator retItr = returnNodesToCombineForCompleteOwnershipGraph.iterator();
386 while( retItr.hasNext() ) {
387 FlatReturnNode frn = (FlatReturnNode) retItr.next();
388 OwnershipGraph ogr = getGraphFromFlatNode( frn );
389 completeGraph.merge( ogr );
391 return completeGraph;
396 analyzeFlatNode( Descriptor methodDesc,
398 HashSet<FlatReturnNode> setRetNodes,
399 OwnershipGraph og ) throws java.io.IOException {
405 // use node type to decide what alterations to make
406 // to the ownership graph
407 switch( fn.kind() ) {
409 case FKind.FlatMethod:
410 FlatMethod fm = (FlatMethod) fn;
412 // there should only be one FlatMethod node as the
413 // parent of all other FlatNode objects, so take
414 // the opportunity to construct the initial graph by
415 // adding parameters labels to new heap regions
416 for( int i = 0; i < fm.numParameters(); ++i ) {
417 TempDescriptor tdParam = fm.getParameter( i );
418 og.assignTempToParameterAllocation( methodDesc instanceof TaskDescriptor,
425 case FKind.FlatOpNode:
426 FlatOpNode fon = (FlatOpNode) fn;
427 if( fon.getOp().getOp() == Operation.ASSIGN ) {
430 og.assignTempToTemp( src, dst );
434 case FKind.FlatFieldNode:
435 FlatFieldNode ffn = (FlatFieldNode) fn;
438 fld = ffn.getField();
439 if( !fld.getType().isPrimitive() ) {
440 og.assignTempToField( src, dst, fld );
444 case FKind.FlatSetFieldNode:
445 FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
448 fld = fsfn.getField();
449 og.assignFieldToTemp( src, dst, fld );
453 FlatNew fnn = (FlatNew) fn;
455 AllocationSite as = getAllocationSiteFromFlatNewPRIVATE( fnn );
457 og.assignTempToNewAllocation( dst, as );
461 FlatCall fc = (FlatCall) fn;
462 MethodDescriptor md = fc.getMethod();
463 FlatMethod flatm = state.getMethodFlat( md );
464 //HashSet<AllocationSite> allocSiteSet = getAllocationSiteSet( md );
465 OwnershipGraph ogAllPossibleCallees = new OwnershipGraph( allocationDepth );
467 if( md.isStatic() ) {
468 // a static method is simply always the same, makes life easy
469 OwnershipGraph onlyPossibleCallee = mapDescriptorToCompleteOwnershipGraph.get( md );
470 ogAllPossibleCallees.merge( onlyPossibleCallee );
473 if( onlyPossibleCallee != null ) {
474 onlyPossibleCallee.writeGraph( "only", false, false );
475 System.out.println( "There was only one possible callee, "+md );
480 // if the method descriptor is virtual, then there could be a
481 // set of possible methods that will actually be invoked, so
482 // find all of them and merge all of their graphs together
483 TypeDescriptor typeDesc = fc.getThis().getType();
484 Set possibleCallees = callGraph.getMethods( md, typeDesc );
488 Iterator i = possibleCallees.iterator();
489 while( i.hasNext() ) {
490 MethodDescriptor possibleMd = (MethodDescriptor) i.next();
491 //allocSiteSet.addAll( getAllocationSiteSet( possibleMd ) );
492 OwnershipGraph ogPotentialCallee = mapDescriptorToCompleteOwnershipGraph.get( possibleMd );
495 if( ogPotentialCallee != null ) {
496 ogPotentialCallee.writeGraph( "potential"+j, false, false );
501 ogAllPossibleCallees.merge( ogPotentialCallee );
504 //System.out.println( "There were "+j+" potential callees merged together." );
507 //System.out.println( "AllocationSiteSet has "+allocSiteSet.size()+" items." );
509 // now we should have the following information to resolve this method call:
511 // 1. A FlatCall fc to query for the caller's context (argument labels, etc)
513 // 2. Whether the method is static; if not we need to deal with the "this" pointer
515 // *******************************************************************************************
516 // 3. The original FlatMethod flatm to query for callee's context (paramter labels)
517 // NOTE! I assume FlatMethod before virtual dispatch accurately describes all possible methods!
518 // *******************************************************************************************
520 // 4. The OwnershipGraph ogAllPossibleCallees is a merge of every ownership graph of all the possible
521 // methods to capture any possible references made.
523 // 5. The Set of AllocationSite objects, allocSiteSet that is the set of allocation sites from
524 // every possible method we might have chosen
526 og.resolveMethodCall( fc, md.isStatic(), flatm, ogAllPossibleCallees );
529 case FKind.FlatReturnNode:
530 FlatReturnNode frn = (FlatReturnNode) fn;
531 setRetNodes.add( frn );
537 // this method should generate integers strictly greater than zero!
538 // special "shadow" regions are made from a heap region by negating
540 static public Integer generateUniqueHeapRegionNodeID() {
542 return new Integer( uniqueIDcount );
546 private OwnershipGraph getGraphFromFlatNode( FlatNode fn ) {
547 if( !mapFlatNodeToOwnershipGraph.containsKey( fn ) ) {
548 mapFlatNodeToOwnershipGraph.put( fn, new OwnershipGraph( allocationDepth ) );
551 return mapFlatNodeToOwnershipGraph.get( fn );
554 private void setGraphForFlatNode( FlatNode fn, OwnershipGraph og ) {
555 mapFlatNodeToOwnershipGraph.put( fn, og );
563 // return just the allocation site associated with one FlatNew node
564 private AllocationSite getAllocationSiteFromFlatNewPRIVATE( FlatNew fn ) {
566 if( !mapFlatNewToAllocationSite.containsKey( fn ) ) {
567 AllocationSite as = new AllocationSite( allocationDepth, fn.getType() );
569 // the newest nodes are single objects
570 for( int i = 0; i < allocationDepth; ++i ) {
571 Integer id = generateUniqueHeapRegionNodeID();
572 as.setIthOldest( i, id );
575 // the oldest node is a summary node
576 Integer idSummary = generateUniqueHeapRegionNodeID();
577 as.setSummary( idSummary );
579 mapFlatNewToAllocationSite.put( fn, as );
582 return mapFlatNewToAllocationSite.get( fn );
586 // return all allocation sites in the method (there is one allocation
587 // site per FlatNew node in a method)
588 private HashSet<AllocationSite> getAllocationSiteSet( Descriptor d ) {
589 if( !mapDescriptorToAllocationSiteSet.containsKey( d ) ) {
590 buildAllocationSiteSet( d );
593 return mapDescriptorToAllocationSiteSet.get( d );
597 private void buildAllocationSiteSet( Descriptor d ) {
598 HashSet<AllocationSite> s = new HashSet<AllocationSite>();
601 if( d instanceof MethodDescriptor ) {
602 fm = state.getMethodFlat( (MethodDescriptor) d );
604 assert d instanceof TaskDescriptor;
605 fm = state.getMethodFlat( (TaskDescriptor) d );
608 // visit every node in this FlatMethod's IR graph
609 // and make a set of the allocation sites from the
610 // FlatNew node's visited
611 HashSet<FlatNode> visited = new HashSet<FlatNode>();
612 HashSet<FlatNode> toVisit = new HashSet<FlatNode>();
615 while( !toVisit.isEmpty() ) {
616 FlatNode n = toVisit.iterator().next();
618 if( n instanceof FlatNew ) {
619 s.add( getAllocationSiteFromFlatNewPRIVATE( (FlatNew) n ) );
625 for( int i = 0; i < n.numNext(); ++i ) {
626 FlatNode child = n.getNext( i );
627 if( !visited.contains( child ) ) {
628 toVisit.add( child );
633 mapDescriptorToAllocationSiteSet.put( d, s );
637 private HashSet<AllocationSite>
638 getFlaggedAllocationSitesReachableFromTaskPRIVATE( TaskDescriptor td ) {
640 HashSet<AllocationSite> asSetTotal = new HashSet<AllocationSite>();
641 HashSet<Descriptor> toVisit = new HashSet<Descriptor>();
642 HashSet<Descriptor> visited = new HashSet<Descriptor>();
646 // traverse this task and all methods reachable from this task
647 while( !toVisit.isEmpty() ) {
648 Descriptor d = toVisit.iterator().next();
652 HashSet<AllocationSite> asSet = getAllocationSiteSet( d );
653 Iterator asItr = asSet.iterator();
654 while( asItr.hasNext() ) {
655 AllocationSite as = (AllocationSite) asItr.next();
656 if( as.getType().getClassDesc().hasFlags() ) {
657 asSetTotal.add( as );
661 // enqueue callees of this method to be searched for
662 // allocation sites also
663 Set callees = callGraph.getCalleeSet( d );
664 if( callees != null ) {
665 Iterator methItr = callees.iterator();
666 while( methItr.hasNext() ) {
667 MethodDescriptor md = (MethodDescriptor) methItr.next();
669 if( !visited.contains( md ) ) {
682 private HashSet<Integer> getHeapRegionIDset( OwnershipGraph og,
685 assert og.paramIndex2id.containsKey( paramIndex );
686 Integer idParam = og.paramIndex2id.get( paramIndex );
688 HashSet<Integer> idSet = new HashSet<Integer>();
689 idSet.add( idParam );
695 private HashSet<Integer> getHeapRegionIDset( AllocationSite alloc ) {
697 HashSet<Integer> idSet = new HashSet<Integer>();
699 for( int i = 0; i < alloc.getAllocationDepth(); ++i ) {
700 Integer id = alloc.getIthOldest( i );
704 Integer idSummary = alloc.getSummary();
705 idSet.add( idSummary );
710 private HashSet<Integer> getHeapRegionIDset( HashSet<AllocationSite> allocSet ) {
712 HashSet<Integer> idSet = new HashSet<Integer>();
714 Iterator allocItr = allocSet.iterator();
715 while( allocItr.hasNext() ) {
716 AllocationSite alloc = (AllocationSite) allocItr.next();
718 for( int i = 0; i < alloc.getAllocationDepth(); ++i ) {
719 Integer id = alloc.getIthOldest( i );
723 Integer idSummary = alloc.getSummary();
724 idSet.add( idSummary );
730 private boolean createsPotentialAliases( OwnershipGraph og,
731 HashSet<Integer> idSetA,
732 HashSet<Integer> idSetB ) {
733 boolean potentialAlias = false;
735 // first expand set B into the set of all heap region node ID's
736 // reachable from the nodes in set B
737 HashSet<Integer> idSetReachableFromB = og.getReachableSet( idSetB );
739 // then see if anything in A can reach a node in the set reachable
740 // from B. If so, there is a potential alias.
741 Iterator i = idSetA.iterator();
742 while( i.hasNext() ) {
743 Integer id = (Integer) i.next();
744 if( og.canIdReachSet( id, idSetB ) ) {