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>();
209 // initialize methods to visit as the set of all tasks in the
210 // program and then any method that could be called starting
212 Iterator taskItr = state.getTaskSymbolTable().getDescriptorsIterator();
213 while( taskItr.hasNext() ) {
214 Descriptor d = (Descriptor) taskItr.next();
215 descriptorsToVisit.add( d );
217 // recursively find all callees from this task
218 scheduleAllCallees( calleesScheduled, d );
221 // as mentioned above, analyze methods one-by-one, possibly revisiting
222 // a method if the methods that it calls are updated
226 // called from the constructor to help initialize the set
227 // of methods that needs to be analyzed by ownership analysis
228 private void scheduleAllCallees( HashSet<Descriptor> calleesScheduled,
230 if( calleesScheduled.contains( d ) ) {
233 calleesScheduled.add( d );
235 Set callees = callGraph.getCalleeSet( d );
236 if( callees == null ) {
240 Iterator methItr = callees.iterator();
241 while( methItr.hasNext() ) {
242 MethodDescriptor md = (MethodDescriptor) methItr.next();
243 descriptorsToVisit.add( md );
245 // recursively find all callees from this task
246 scheduleAllCallees( calleesScheduled, md );
251 // manage the set of tasks and methods to be analyzed
252 // and be sure to reschedule tasks/methods when the methods
253 // they call are updated
254 private void analyzeMethods() throws java.io.IOException {
256 while( !descriptorsToVisit.isEmpty() ) {
257 Descriptor d = (Descriptor) descriptorsToVisit.iterator().next();
258 descriptorsToVisit.remove( d );
260 // because the task or method descriptor just extracted
261 // was in the "to visit" set it either hasn't been analyzed
262 // yet, or some method that it depends on has been
263 // updated. Recompute a complete ownership graph for
264 // this task/method and compare it to any previous result.
265 // If there is a change detected, add any methods/tasks
266 // that depend on this one to the "to visit" set.
268 System.out.println( "Analyzing " + d );
271 if( d instanceof MethodDescriptor ) {
272 fm = state.getMethodFlat( (MethodDescriptor) d );
274 assert d instanceof TaskDescriptor;
275 fm = state.getMethodFlat( (TaskDescriptor) d );
278 OwnershipGraph og = analyzeFlatMethod( d, fm );
279 OwnershipGraph ogPrev = mapDescriptorToCompleteOwnershipGraph.get( d );
280 if( !og.equals( ogPrev ) ) {
281 mapDescriptorToCompleteOwnershipGraph.put( d, og );
283 og.writeGraph( d, true, true, true, false );
285 // only methods have dependents, tasks cannot
286 // be invoked by any user program calls
287 if( d instanceof MethodDescriptor ) {
288 MethodDescriptor md = (MethodDescriptor) d;
289 Set dependents = callGraph.getCallerSet( md );
290 if( dependents != null ) {
291 descriptorsToVisit.addAll( dependents );
300 // keep passing the Descriptor of the method along for debugging
301 // and dot file writing
302 private OwnershipGraph
303 analyzeFlatMethod( Descriptor mDesc,
304 FlatMethod flatm ) throws java.io.IOException {
306 // initialize flat nodes to visit as the flat method
307 // because all other nodes in this flat method are
308 // decendents of the flat method itself
309 flatNodesToVisit = new HashSet<FlatNode>();
310 flatNodesToVisit.add( flatm );
312 // initilize the mapping of flat nodes in this flat method to
313 // ownership graph results to an empty mapping
314 mapFlatNodeToOwnershipGraph = new Hashtable<FlatNode, OwnershipGraph>();
316 // initialize the set of return nodes that will be combined as
317 // the final ownership graph result to return as an empty set
318 returnNodesToCombineForCompleteOwnershipGraph = new HashSet<FlatReturnNode>();
325 while( !flatNodesToVisit.isEmpty() ) {
326 FlatNode fn = (FlatNode) flatNodesToVisit.iterator().next();
327 flatNodesToVisit.remove( fn );
329 // perform this node's contributions to the ownership
330 // graph on a new copy, then compare it to the old graph
331 // at this node to see if anything was updated.
332 OwnershipGraph og = new OwnershipGraph( allocationDepth );
334 // start by merging all node's parents' graphs
335 for( int i = 0; i < fn.numPrev(); ++i ) {
336 FlatNode pn = fn.getPrev( i );
337 OwnershipGraph ogParent = getGraphFromFlatNode( pn );
338 og.merge( ogParent );
341 // apply the analysis of the flat node to the
342 // ownership graph made from the merge of the
344 analyzeFlatNode( mDesc,
346 returnNodesToCombineForCompleteOwnershipGraph,
349 // if the results of the new graph are different from
350 // the current graph at this node, replace the graph
351 // with the update and enqueue the children for
353 OwnershipGraph ogPrev = getGraphFromFlatNode( fn );
355 if( !og.equals( ogPrev ) ) {
356 setGraphForFlatNode( fn, og );
363 String s = String.format( "%04d", x );
364 og.writeGraph( "debug"+s, false, false );
372 for( int i = 0; i < fn.numNext(); i++ ) {
373 FlatNode nn = fn.getNext( i );
374 flatNodesToVisit.add( nn );
379 // end by merging all return nodes into a complete
380 // ownership graph that represents all possible heap
381 // states after the flat method returns
382 OwnershipGraph completeGraph = new OwnershipGraph( allocationDepth );
383 Iterator retItr = returnNodesToCombineForCompleteOwnershipGraph.iterator();
384 while( retItr.hasNext() ) {
385 FlatReturnNode frn = (FlatReturnNode) retItr.next();
386 OwnershipGraph ogr = getGraphFromFlatNode( frn );
387 completeGraph.merge( ogr );
389 return completeGraph;
394 analyzeFlatNode( Descriptor methodDesc,
396 HashSet<FlatReturnNode> setRetNodes,
397 OwnershipGraph og ) throws java.io.IOException {
403 // use node type to decide what alterations to make
404 // to the ownership graph
405 switch( fn.kind() ) {
407 case FKind.FlatMethod:
408 FlatMethod fm = (FlatMethod) fn;
410 // there should only be one FlatMethod node as the
411 // parent of all other FlatNode objects, so take
412 // the opportunity to construct the initial graph by
413 // adding parameters labels to new heap regions
414 for( int i = 0; i < fm.numParameters(); ++i ) {
415 TempDescriptor tdParam = fm.getParameter( i );
416 og.assignTempToParameterAllocation( methodDesc instanceof TaskDescriptor,
423 case FKind.FlatOpNode:
424 FlatOpNode fon = (FlatOpNode) fn;
425 if( fon.getOp().getOp() == Operation.ASSIGN ) {
428 og.assignTempToTemp( src, dst );
432 case FKind.FlatFieldNode:
433 FlatFieldNode ffn = (FlatFieldNode) fn;
436 fld = ffn.getField();
437 if( !fld.getType().isPrimitive() ) {
438 og.assignTempToField( src, dst, fld );
442 case FKind.FlatSetFieldNode:
443 FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
446 fld = fsfn.getField();
447 og.assignFieldToTemp( src, dst, fld );
451 FlatNew fnn = (FlatNew) fn;
453 AllocationSite as = getAllocationSiteFromFlatNewPRIVATE( fnn );
455 og.assignTempToNewAllocation( dst, as );
459 FlatCall fc = (FlatCall) fn;
460 MethodDescriptor md = fc.getMethod();
461 FlatMethod flatm = state.getMethodFlat( md );
462 //HashSet<AllocationSite> allocSiteSet = getAllocationSiteSet( md );
463 OwnershipGraph ogAllPossibleCallees = new OwnershipGraph( allocationDepth );
465 if( md.isStatic() ) {
466 // a static method is simply always the same, makes life easy
467 OwnershipGraph onlyPossibleCallee = mapDescriptorToCompleteOwnershipGraph.get( md );
468 ogAllPossibleCallees.merge( onlyPossibleCallee );
471 if( onlyPossibleCallee != null ) {
472 onlyPossibleCallee.writeGraph( "only", false, false );
473 System.out.println( "There was only one possible callee, "+md );
478 // if the method descriptor is virtual, then there could be a
479 // set of possible methods that will actually be invoked, so
480 // find all of them and merge all of their graphs together
481 TypeDescriptor typeDesc = fc.getThis().getType();
482 Set possibleCallees = callGraph.getMethods( md, typeDesc );
486 Iterator i = possibleCallees.iterator();
487 while( i.hasNext() ) {
488 MethodDescriptor possibleMd = (MethodDescriptor) i.next();
489 //allocSiteSet.addAll( getAllocationSiteSet( possibleMd ) );
490 OwnershipGraph ogPotentialCallee = mapDescriptorToCompleteOwnershipGraph.get( possibleMd );
493 if( ogPotentialCallee != null ) {
494 ogPotentialCallee.writeGraph( "potential"+j, false, false );
499 ogAllPossibleCallees.merge( ogPotentialCallee );
502 //System.out.println( "There were "+j+" potential callees merged together." );
505 //System.out.println( "AllocationSiteSet has "+allocSiteSet.size()+" items." );
507 // now we should have the following information to resolve this method call:
509 // 1. A FlatCall fc to query for the caller's context (argument labels, etc)
511 // 2. Whether the method is static; if not we need to deal with the "this" pointer
513 // *******************************************************************************************
514 // 3. The original FlatMethod flatm to query for callee's context (paramter labels)
515 // NOTE! I assume FlatMethod before virtual dispatch accurately describes all possible methods!
516 // *******************************************************************************************
518 // 4. The OwnershipGraph ogAllPossibleCallees is a merge of every ownership graph of all the possible
519 // methods to capture any possible references made.
521 // 5. The Set of AllocationSite objects, allocSiteSet that is the set of allocation sites from
522 // every possible method we might have chosen
524 og.resolveMethodCall( fc, md.isStatic(), flatm, ogAllPossibleCallees );
527 case FKind.FlatReturnNode:
528 FlatReturnNode frn = (FlatReturnNode) fn;
529 setRetNodes.add( frn );
535 // this method should generate integers strictly greater than zero!
536 // special "shadow" regions are made from a heap region by negating
538 static public Integer generateUniqueHeapRegionNodeID() {
540 return new Integer( uniqueIDcount );
544 private OwnershipGraph getGraphFromFlatNode( FlatNode fn ) {
545 if( !mapFlatNodeToOwnershipGraph.containsKey( fn ) ) {
546 mapFlatNodeToOwnershipGraph.put( fn, new OwnershipGraph( allocationDepth ) );
549 return mapFlatNodeToOwnershipGraph.get( fn );
552 private void setGraphForFlatNode( FlatNode fn, OwnershipGraph og ) {
553 mapFlatNodeToOwnershipGraph.put( fn, og );
561 // return just the allocation site associated with one FlatNew node
562 private AllocationSite getAllocationSiteFromFlatNewPRIVATE( FlatNew fn ) {
564 if( !mapFlatNewToAllocationSite.containsKey( fn ) ) {
565 AllocationSite as = new AllocationSite( allocationDepth, fn.getType() );
567 // the newest nodes are single objects
568 for( int i = 0; i < allocationDepth; ++i ) {
569 Integer id = generateUniqueHeapRegionNodeID();
570 as.setIthOldest( i, id );
573 // the oldest node is a summary node
574 Integer idSummary = generateUniqueHeapRegionNodeID();
575 as.setSummary( idSummary );
577 mapFlatNewToAllocationSite.put( fn, as );
580 return mapFlatNewToAllocationSite.get( fn );
584 // return all allocation sites in the method (there is one allocation
585 // site per FlatNew node in a method)
586 private HashSet<AllocationSite> getAllocationSiteSet( Descriptor d ) {
587 if( !mapDescriptorToAllocationSiteSet.containsKey( d ) ) {
588 buildAllocationSiteSet( d );
591 return mapDescriptorToAllocationSiteSet.get( d );
595 private void buildAllocationSiteSet( Descriptor d ) {
596 HashSet<AllocationSite> s = new HashSet<AllocationSite>();
599 if( d instanceof MethodDescriptor ) {
600 fm = state.getMethodFlat( (MethodDescriptor) d );
602 assert d instanceof TaskDescriptor;
603 fm = state.getMethodFlat( (TaskDescriptor) d );
606 // visit every node in this FlatMethod's IR graph
607 // and make a set of the allocation sites from the
608 // FlatNew node's visited
609 HashSet<FlatNode> visited = new HashSet<FlatNode>();
610 HashSet<FlatNode> toVisit = new HashSet<FlatNode>();
613 while( !toVisit.isEmpty() ) {
614 FlatNode n = toVisit.iterator().next();
616 if( n instanceof FlatNew ) {
617 s.add( getAllocationSiteFromFlatNewPRIVATE( (FlatNew) n ) );
623 for( int i = 0; i < n.numNext(); ++i ) {
624 FlatNode child = n.getNext( i );
625 if( !visited.contains( child ) ) {
626 toVisit.add( child );
631 mapDescriptorToAllocationSiteSet.put( d, s );
635 private HashSet<AllocationSite>
636 getFlaggedAllocationSitesReachableFromTaskPRIVATE( TaskDescriptor td ) {
638 HashSet<AllocationSite> asSetTotal = new HashSet<AllocationSite>();
639 HashSet<Descriptor> toVisit = new HashSet<Descriptor>();
640 HashSet<Descriptor> visited = new HashSet<Descriptor>();
644 // traverse this task and all methods reachable from this task
645 while( !toVisit.isEmpty() ) {
646 Descriptor d = toVisit.iterator().next();
650 HashSet<AllocationSite> asSet = getAllocationSiteSet( d );
651 Iterator asItr = asSet.iterator();
652 while( asItr.hasNext() ) {
653 AllocationSite as = (AllocationSite) asItr.next();
654 if( as.getType().getClassDesc().hasFlags() ) {
655 asSetTotal.add( as );
659 // enqueue callees of this method to be searched for
660 // allocation sites also
661 Set callees = callGraph.getCalleeSet( d );
662 if( callees != null ) {
663 Iterator methItr = callees.iterator();
664 while( methItr.hasNext() ) {
665 MethodDescriptor md = (MethodDescriptor) methItr.next();
667 if( !visited.contains( md ) ) {
680 private HashSet<Integer> getHeapRegionIDset( OwnershipGraph og,
683 assert og.paramIndex2id.containsKey( paramIndex );
684 Integer idParam = og.paramIndex2id.get( paramIndex );
686 HashSet<Integer> idSet = new HashSet<Integer>();
687 idSet.add( idParam );
693 private HashSet<Integer> getHeapRegionIDset( AllocationSite alloc ) {
695 HashSet<Integer> idSet = new HashSet<Integer>();
697 for( int i = 0; i < alloc.getAllocationDepth(); ++i ) {
698 Integer id = alloc.getIthOldest( i );
702 Integer idSummary = alloc.getSummary();
703 idSet.add( idSummary );
708 private HashSet<Integer> getHeapRegionIDset( HashSet<AllocationSite> allocSet ) {
710 HashSet<Integer> idSet = new HashSet<Integer>();
712 Iterator allocItr = allocSet.iterator();
713 while( allocItr.hasNext() ) {
714 AllocationSite alloc = (AllocationSite) allocItr.next();
716 for( int i = 0; i < alloc.getAllocationDepth(); ++i ) {
717 Integer id = alloc.getIthOldest( i );
721 Integer idSummary = alloc.getSummary();
722 idSet.add( idSummary );
728 private boolean createsPotentialAliases( OwnershipGraph og,
729 HashSet<Integer> idSetA,
730 HashSet<Integer> idSetB ) {
731 boolean potentialAlias = false;
733 // first expand set B into the set of all heap region node ID's
734 // reachable from the nodes in set B
735 HashSet<Integer> idSetReachableFromB = og.getReachableSet( idSetB );
737 // then see if anything in A can reach a node in the set reachable
738 // from B. If so, there is a potential alias.
739 Iterator i = idSetA.iterator();
740 while( i.hasNext() ) {
741 Integer id = (Integer) i.next();
742 if( og.canIdReachSet( id, idSetB ) ) {