1 package Analysis.OwnershipAnalysis;
3 import Analysis.CallGraph.*;
6 import IR.Tree.Modifiers;
11 public class OwnershipAnalysis {
14 ///////////////////////////////////////////
16 // Public interface to discover possible
17 // aliases in the program under analysis
19 ///////////////////////////////////////////
21 public HashSet<AllocationSite>
22 getFlaggedAllocationSitesReachableFromTask(TaskDescriptor td) {
23 return getFlaggedAllocationSitesReachableFromTaskPRIVATE(td);
26 public AllocationSite getAllocationSiteFromFlatNew(FlatNew fn) {
27 return getAllocationSiteFromFlatNewPRIVATE(fn);
31 public boolean createsPotentialAliases(Descriptor taskOrMethod,
35 OwnershipGraph og = getGraphOfAllContextsFromDescriptor(taskOrMethod);
37 return og.hasPotentialAlias(paramIndex1, paramIndex2);
40 public boolean createsPotentialAliases(Descriptor taskOrMethod,
42 AllocationSite alloc) {
44 OwnershipGraph og = getGraphOfAllContextsFromDescriptor(taskOrMethod);
46 return og.hasPotentialAlias(paramIndex, alloc);
49 public boolean createsPotentialAliases(Descriptor taskOrMethod,
53 OwnershipGraph og = getGraphOfAllContextsFromDescriptor(taskOrMethod);
55 return og.hasPotentialAlias(paramIndex, alloc);
58 public boolean createsPotentialAliases(Descriptor taskOrMethod,
59 AllocationSite alloc1,
60 AllocationSite alloc2) {
62 OwnershipGraph og = getGraphOfAllContextsFromDescriptor(taskOrMethod);
64 return og.hasPotentialAlias(alloc1, alloc2);
68 protected OwnershipGraph getGraphOfAllContextsFromDescriptor(Descriptor d) {
71 OwnershipGraph og = new OwnershipGraph( allocationDepth, typeUtil );
73 assert mapDescriptorToAllMethodContexts.containsKey( d );
74 HashSet<MethodContext> contexts = mapDescriptorToAllMethodContexts.get( d );
75 Iterator<MethodContext> mcItr = contexts.iterator();
76 while( mcItr.hasNext() ) {
77 MethodContext mc = mcItr.next();
79 OwnershipGraph ogContext = mapMethodContextToCompleteOwnershipGraph.get(mc);
80 assert ogContext != null;
82 og.merge( ogContext );
89 // use the methods given above to check every possible alias
90 // between task parameters and flagged allocation sites reachable
92 public void writeAllAliases(String outputFile) throws java.io.IOException {
94 BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile) );
96 bw.write("Conducting ownership analysis with allocation depth = "+allocationDepth);
98 // look through every task for potential aliases
99 Iterator taskItr = state.getTaskSymbolTable().getDescriptorsIterator();
100 while( taskItr.hasNext() ) {
101 TaskDescriptor td = (TaskDescriptor) taskItr.next();
103 bw.write("\n---------"+td+"--------\n");
105 HashSet<AllocationSite> allocSites = getFlaggedAllocationSitesReachableFromTask(td);
107 // for each task parameter, check for aliases with
108 // other task parameters and every allocation site
109 // reachable from this task
110 boolean foundSomeAlias = false;
112 FlatMethod fm = state.getMethodFlat(td);
113 for( int i = 0; i < fm.numParameters(); ++i ) {
115 // for the ith parameter check for aliases to all
116 // higher numbered parameters
117 for( int j = i + 1; j < fm.numParameters(); ++j ) {
118 if( createsPotentialAliases(td, i, j) ) {
119 foundSomeAlias = true;
120 bw.write("Potential alias between parameters "+i+" and "+j+".\n");
124 // for the ith parameter, check for aliases against
125 // the set of allocation sites reachable from this
127 Iterator allocItr = allocSites.iterator();
128 while( allocItr.hasNext() ) {
129 AllocationSite as = (AllocationSite) allocItr.next();
130 if( createsPotentialAliases(td, i, as) ) {
131 foundSomeAlias = true;
132 bw.write("Potential alias between parameter "+i+" and "+as.getFlatNew()+".\n");
137 // for each allocation site check for aliases with
138 // other allocation sites in the context of execution
140 HashSet<AllocationSite> outerChecked = new HashSet<AllocationSite>();
141 Iterator allocItr1 = allocSites.iterator();
142 while( allocItr1.hasNext() ) {
143 AllocationSite as1 = (AllocationSite) allocItr1.next();
145 Iterator allocItr2 = allocSites.iterator();
146 while( allocItr2.hasNext() ) {
147 AllocationSite as2 = (AllocationSite) allocItr2.next();
149 if( !outerChecked.contains(as2) &&
150 createsPotentialAliases(td, as1, as2) ) {
151 foundSomeAlias = true;
152 bw.write("Potential alias between "+as1.getFlatNew()+" and "+as2.getFlatNew()+".\n");
156 outerChecked.add(as1);
159 if( !foundSomeAlias ) {
160 bw.write("No aliases between flagged objects in Task "+td+".\n");
167 ///////////////////////////////////////////
169 // end public interface
171 ///////////////////////////////////////////
180 // data from the compiler
182 private TypeUtil typeUtil;
183 private CallGraph callGraph;
184 private int allocationDepth;
186 // used to identify HeapRegionNode objects
187 // A unique ID equates an object in one
188 // ownership graph with an object in another
189 // graph that logically represents the same
191 // start at 10 and incerement to leave some
192 // reserved IDs for special purposes
193 static private int uniqueIDcount = 10;
196 // Use these data structures to track progress of
197 // processing all methods in the program, and by methods
198 // TaskDescriptor and MethodDescriptor are combined
199 // together, with a common parent class Descriptor
200 private Hashtable<MethodContext, OwnershipGraph> mapMethodContextToInitialParamAllocGraph;
201 private Hashtable<MethodContext, OwnershipGraph> mapMethodContextToCompleteOwnershipGraph;
202 private Hashtable<FlatNew, AllocationSite> mapFlatNewToAllocationSite;
203 private Hashtable<Descriptor, HashSet<AllocationSite> > mapDescriptorToAllocationSiteSet;
204 private Hashtable<MethodContext, Integer> mapMethodContextToNumUpdates;
205 private Hashtable<Descriptor, HashSet<MethodContext> > mapDescriptorToAllMethodContexts;
207 // Use these data structures to track progress of one pass of
208 // processing the FlatNodes of a particular method
209 private HashSet <FlatNode> flatNodesToVisit;
210 private Hashtable<FlatNode, OwnershipGraph> mapFlatNodeToOwnershipGraph;
211 private HashSet <FlatReturnNode> returnNodesToCombineForCompleteOwnershipGraph;
213 // descriptorsToAnalyze identifies the set of tasks and methods
214 // that are reachable from the program tasks, this set is initialized
215 // and then remains static
216 private HashSet<Descriptor> descriptorsToAnalyze;
218 // descriptorsToVisit is initialized to descriptorsToAnalyze and is
219 // reduced by visiting a descriptor during analysis. When dependents
220 // must be scheduled, only those contained in descriptorsToAnalyze
221 // should be re-added to this set
222 private HashSet<MethodContext> methodContextsToVisit;
224 // a special field descriptor for all array elements
225 private static FieldDescriptor fdElement = new FieldDescriptor(new Modifiers(Modifiers.PUBLIC),
226 new TypeDescriptor("Array[]"),
231 // a special temp descriptor for setting more than one parameter label
232 // to the all-aliased-parameters heap region node
233 protected static TempDescriptor tdAliasedParams = new TempDescriptor("_AllAliasedParams___");
236 // for controlling DOT file output
237 private boolean writeDOTs;
238 private boolean writeAllDOTs;
242 // this analysis generates an ownership graph for every task
244 public OwnershipAnalysis(State state,
249 boolean writeAllDOTs,
250 String aliasFile) throws java.io.IOException {
254 this.callGraph = callGraph;
255 this.allocationDepth = allocationDepth;
256 this.writeDOTs = writeDOTs;
257 this.writeAllDOTs = writeAllDOTs;
259 descriptorsToAnalyze = new HashSet<Descriptor>();
261 mapMethodContextToInitialParamAllocGraph =
262 new Hashtable<MethodContext, OwnershipGraph>();
264 mapMethodContextToCompleteOwnershipGraph =
265 new Hashtable<MethodContext, OwnershipGraph>();
267 mapFlatNewToAllocationSite =
268 new Hashtable<FlatNew, AllocationSite>();
270 mapDescriptorToAllocationSiteSet =
271 new Hashtable<Descriptor, HashSet<AllocationSite> >();
273 mapDescriptorToAllMethodContexts =
274 new Hashtable<Descriptor, HashSet<MethodContext> >();
278 mapMethodContextToNumUpdates = new Hashtable<MethodContext, Integer>();
281 // initialize methods to visit as the set of all tasks in the
282 // program and then any method that could be called starting
284 Iterator taskItr = state.getTaskSymbolTable().getDescriptorsIterator();
285 while( taskItr.hasNext() ) {
286 Descriptor d = (Descriptor) taskItr.next();
287 scheduleAllCallees(d);
290 // before beginning analysis, initialize every scheduled method
291 // with an ownership graph that has populated parameter index tables
292 // by analyzing the first node which is always a FlatMethod node
293 Iterator<Descriptor> dItr = descriptorsToAnalyze.iterator();
294 while( dItr.hasNext() ) {
295 Descriptor d = dItr.next();
296 OwnershipGraph og = new OwnershipGraph(allocationDepth, typeUtil);
299 if( d instanceof MethodDescriptor ) {
300 fm = state.getMethodFlat( (MethodDescriptor) d);
302 assert d instanceof TaskDescriptor;
303 fm = state.getMethodFlat( (TaskDescriptor) d);
306 MethodContext mc = new MethodContext( d );
307 assert !mapDescriptorToAllMethodContexts.containsKey( d );
308 HashSet<MethodContext> s = new HashSet<MethodContext>();
310 mapDescriptorToAllMethodContexts.put( d, s );
312 System.out.println("Previsiting " + mc);
314 og = analyzeFlatNode(mc, fm, null, og);
315 setGraphForMethodContext(mc, og);
318 System.out.println("");
320 // as mentioned above, analyze methods one-by-one, possibly revisiting
321 // a method if the methods that it calls are updated
324 System.out.println("");
326 if( aliasFile != null ) {
327 writeAllAliases(aliasFile);
331 // called from the constructor to help initialize the set
332 // of methods that needs to be analyzed by ownership analysis
333 private void scheduleAllCallees(Descriptor d) {
334 if( descriptorsToAnalyze.contains(d) ) {
337 descriptorsToAnalyze.add(d);
339 // start with all method calls to further schedule
340 Set moreMethodsToCheck = moreMethodsToCheck = callGraph.getMethodCalls(d);
342 if( d instanceof MethodDescriptor ) {
343 // see if this method has virtual dispatch
344 Set virtualMethods = callGraph.getMethods( (MethodDescriptor)d);
345 moreMethodsToCheck.addAll(virtualMethods);
348 // keep following any further methods identified in
350 Iterator methItr = moreMethodsToCheck.iterator();
351 while( methItr.hasNext() ) {
352 Descriptor m = (Descriptor) methItr.next();
353 scheduleAllCallees(m);
358 // manage the set of tasks and methods to be analyzed
359 // and be sure to reschedule tasks/methods when the methods
360 // they call are updated
361 private void analyzeMethods() throws java.io.IOException {
363 methodContextsToVisit = new HashSet<MethodContext>();
364 Iterator<Descriptor> itrd2a = descriptorsToAnalyze.iterator();
365 while( itrd2a.hasNext() ) {
366 HashSet<MethodContext> mcs = mapDescriptorToAllMethodContexts.get( itrd2a.next() );
369 Iterator<MethodContext> itrmc = mcs.iterator();
370 while( itrmc.hasNext() ) {
371 methodContextsToVisit.add( itrmc.next() );
375 while( !methodContextsToVisit.isEmpty() ) {
376 MethodContext mc = methodContextsToVisit.iterator().next();
377 methodContextsToVisit.remove(mc);
380 // because the task or method descriptor just extracted
381 // was in the "to visit" set it either hasn't been analyzed
382 // yet, or some method that it depends on has been
383 // updated. Recompute a complete ownership graph for
384 // this task/method and compare it to any previous result.
385 // If there is a change detected, add any methods/tasks
386 // that depend on this one to the "to visit" set.
388 System.out.println("Analyzing " + mc);
390 Descriptor d = mc.getDescriptor();
392 if( d instanceof MethodDescriptor ) {
393 fm = state.getMethodFlat( (MethodDescriptor) d);
395 assert d instanceof TaskDescriptor;
396 fm = state.getMethodFlat( (TaskDescriptor) d);
399 OwnershipGraph og = analyzeFlatMethod(mc, fm);
400 OwnershipGraph ogPrev = mapMethodContextToCompleteOwnershipGraph.get(mc);
401 if( !og.equals(ogPrev) ) {
402 setGraphForMethodContext(mc, og);
404 // only methods have dependents, tasks cannot
405 // be invoked by any user program calls
406 if( d instanceof MethodDescriptor ) {
407 MethodDescriptor md = (MethodDescriptor) d;
408 Set dependents = callGraph.getCallerSet(md);
409 if( dependents != null ) {
410 Iterator depItr = dependents.iterator();
411 while( depItr.hasNext() ) {
412 Descriptor dependent = (Descriptor) depItr.next();
413 if( descriptorsToAnalyze.contains(dependent) ) {
415 HashSet<MethodContext> mcs = mapDescriptorToAllMethodContexts.get( dependent );
418 Iterator<MethodContext> itrmc = mcs.iterator();
419 while( itrmc.hasNext() ) {
420 methodContextsToVisit.add( itrmc.next() );
432 // keep passing the Descriptor of the method along for debugging
433 // and dot file writing
434 private OwnershipGraph
435 analyzeFlatMethod(MethodContext mc,
436 FlatMethod flatm) throws java.io.IOException {
438 // initialize flat nodes to visit as the flat method
439 // because it is the entry point
441 flatNodesToVisit = new HashSet<FlatNode>();
442 flatNodesToVisit.add(flatm);
444 // initilize the mapping of flat nodes in this flat method to
445 // ownership graph results to an empty mapping
446 mapFlatNodeToOwnershipGraph = new Hashtable<FlatNode, OwnershipGraph>();
448 // initialize the set of return nodes that will be combined as
449 // the final ownership graph result to return as an empty set
450 returnNodesToCombineForCompleteOwnershipGraph = new HashSet<FlatReturnNode>();
453 while( !flatNodesToVisit.isEmpty() ) {
454 FlatNode fn = (FlatNode) flatNodesToVisit.iterator().next();
455 flatNodesToVisit.remove(fn);
457 //System.out.println( " "+fn );
459 // perform this node's contributions to the ownership
460 // graph on a new copy, then compare it to the old graph
461 // at this node to see if anything was updated.
462 OwnershipGraph og = new OwnershipGraph(allocationDepth, typeUtil);
464 // start by merging all node's parents' graphs
465 for( int i = 0; i < fn.numPrev(); ++i ) {
466 FlatNode pn = fn.getPrev(i);
467 if( mapFlatNodeToOwnershipGraph.containsKey(pn) ) {
468 OwnershipGraph ogParent = mapFlatNodeToOwnershipGraph.get(pn);
473 // apply the analysis of the flat node to the
474 // ownership graph made from the merge of the
476 og = analyzeFlatNode(mc,
478 returnNodesToCombineForCompleteOwnershipGraph,
482 //debugSnapshot(og,fn);
486 // if the results of the new graph are different from
487 // the current graph at this node, replace the graph
488 // with the update and enqueue the children for
490 OwnershipGraph ogPrev = mapFlatNodeToOwnershipGraph.get(fn);
491 if( !og.equals(ogPrev) ) {
492 mapFlatNodeToOwnershipGraph.put(fn, og);
494 for( int i = 0; i < fn.numNext(); i++ ) {
495 FlatNode nn = fn.getNext(i);
496 flatNodesToVisit.add(nn);
501 // end by merging all return nodes into a complete
502 // ownership graph that represents all possible heap
503 // states after the flat method returns
504 OwnershipGraph completeGraph = new OwnershipGraph(allocationDepth, typeUtil);
505 Iterator retItr = returnNodesToCombineForCompleteOwnershipGraph.iterator();
506 while( retItr.hasNext() ) {
507 FlatReturnNode frn = (FlatReturnNode) retItr.next();
508 assert mapFlatNodeToOwnershipGraph.containsKey(frn);
509 OwnershipGraph ogr = mapFlatNodeToOwnershipGraph.get(frn);
510 completeGraph.merge(ogr);
513 return completeGraph;
517 private OwnershipGraph
518 analyzeFlatNode(MethodContext mc,
520 HashSet<FlatReturnNode> setRetNodes,
521 OwnershipGraph og) throws java.io.IOException {
527 // use node type to decide what alterations to make
528 // to the ownership graph
529 switch( fn.kind() ) {
531 case FKind.FlatMethod:
532 FlatMethod fm = (FlatMethod) fn;
534 // there should only be one FlatMethod node as the
535 // parent of all other FlatNode objects, so take
536 // the opportunity to construct the initial graph by
537 // adding parameters labels to new heap regions
538 // AND this should be done once globally so that the
539 // parameter IDs are consistent between analysis
540 // iterations, so if this step has been done already
541 // just merge in the cached version
542 OwnershipGraph ogInitParamAlloc = mapMethodContextToInitialParamAllocGraph.get(mc);
543 if( ogInitParamAlloc == null ) {
545 // if the method context has aliased parameters, make sure
546 // there is a blob region for all those param labels to
548 Set<Integer> aliasedParamIndices = mc.getAliasedParamIndices();
549 if( !aliasedParamIndices.isEmpty() ) {
550 og.makeAliasedParamHeapRegionNode( tdAliasedParams );
553 // set up each parameter
554 for( int i = 0; i < fm.numParameters(); ++i ) {
555 TempDescriptor tdParam = fm.getParameter( i );
556 Integer paramIndex = new Integer( i );
558 if( aliasedParamIndices.contains( paramIndex ) ) {
559 // just point this one to the alias blob
560 og.assignTempEqualToAliasedParam( tdParam,
564 // this parameter is not aliased to others, give it
565 // a fresh parameter heap region
567 og.assignTempEqualToParamAlloc(tdParam,
568 mc.getDescriptor() instanceof TaskDescriptor,
574 OwnershipGraph ogResult = new OwnershipGraph(allocationDepth, typeUtil);
576 mapMethodContextToInitialParamAllocGraph.put(mc, ogResult);
579 // or just leverage the cached copy
580 og.merge(ogInitParamAlloc);
584 case FKind.FlatOpNode:
585 FlatOpNode fon = (FlatOpNode) fn;
586 if( fon.getOp().getOp() == Operation.ASSIGN ) {
589 og.assignTempXEqualToTempY(lhs, rhs);
593 case FKind.FlatFieldNode:
594 FlatFieldNode ffn = (FlatFieldNode) fn;
597 fld = ffn.getField();
598 if( !fld.getType().isImmutable() ) {
599 og.assignTempXEqualToTempYFieldF(lhs, rhs, fld);
603 case FKind.FlatSetFieldNode:
604 FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
606 fld = fsfn.getField();
608 if( !fld.getType().isImmutable() ) {
609 og.assignTempXFieldFEqualToTempY(lhs, fld, rhs);
613 case FKind.FlatElementNode:
614 FlatElementNode fen = (FlatElementNode) fn;
617 if( !lhs.getType().isImmutable() ) {
618 og.assignTempXEqualToTempYFieldF(lhs, rhs, fdElement);
622 case FKind.FlatSetElementNode:
623 FlatSetElementNode fsen = (FlatSetElementNode) fn;
626 if( !rhs.getType().isImmutable() ) {
627 og.assignTempXFieldFEqualToTempY(lhs, fdElement, rhs);
632 FlatNew fnn = (FlatNew) fn;
634 if( !lhs.getType().isImmutable() ) {
635 AllocationSite as = getAllocationSiteFromFlatNewPRIVATE(fnn);
636 og.assignTempEqualToNewAlloc(lhs, as);
641 FlatCall fc = (FlatCall) fn;
642 MethodDescriptor md = fc.getMethod();
643 FlatMethod flatm = state.getMethodFlat(md);
644 OwnershipGraph ogMergeOfAllPossibleCalleeResults = new OwnershipGraph(allocationDepth, typeUtil);
646 if( md.isStatic() ) {
647 // a static method is simply always the same, makes life easy
648 ogMergeOfAllPossibleCalleeResults = og;
650 Set<Integer> aliasedParamIndices =
651 ogMergeOfAllPossibleCalleeResults.calculateAliasedParamSet(fc, md.isStatic(), flatm);
652 MethodContext mcNew = new MethodContext( md, aliasedParamIndices );
653 OwnershipGraph onlyPossibleCallee = mapMethodContextToCompleteOwnershipGraph.get( mcNew );
655 if( onlyPossibleCallee == null ) {
656 // if this method context has never been analyzed just schedule it for analysis
657 // and skip over this call site for now
658 methodContextsToVisit.add( mcNew );
661 ogMergeOfAllPossibleCalleeResults.resolveMethodCall(fc, md.isStatic(), flatm, onlyPossibleCallee);
665 // if the method descriptor is virtual, then there could be a
666 // set of possible methods that will actually be invoked, so
667 // find all of them and merge all of their results together
668 TypeDescriptor typeDesc = fc.getThis().getType();
669 Set possibleCallees = callGraph.getMethods(md, typeDesc);
671 Iterator i = possibleCallees.iterator();
672 while( i.hasNext() ) {
673 MethodDescriptor possibleMd = (MethodDescriptor) i.next();
674 FlatMethod pflatm = state.getMethodFlat(possibleMd);
676 // don't alter the working graph (og) until we compute a result for every
677 // possible callee, merge them all together, then set og to that
678 OwnershipGraph ogCopy = new OwnershipGraph(allocationDepth, typeUtil);
681 Set<Integer> aliasedParamIndices =
682 ogCopy.calculateAliasedParamSet(fc, possibleMd.isStatic(), pflatm);
683 MethodContext mcNew = new MethodContext( possibleMd, aliasedParamIndices );
684 OwnershipGraph ogPotentialCallee = mapMethodContextToCompleteOwnershipGraph.get( mcNew );
686 if( ogPotentialCallee == null ) {
687 // if this method context has never been analyzed just schedule it for analysis
688 // and skip over this call site for now
689 methodContextsToVisit.add( mcNew );
692 ogCopy.resolveMethodCall(fc, possibleMd.isStatic(), pflatm, ogPotentialCallee);
695 ogMergeOfAllPossibleCalleeResults.merge(ogCopy);
699 og = ogMergeOfAllPossibleCalleeResults;
702 case FKind.FlatReturnNode:
703 FlatReturnNode frn = (FlatReturnNode) fn;
704 rhs = frn.getReturnTemp();
705 if( rhs != null && !rhs.getType().isImmutable() ) {
706 og.assignReturnEqualToTemp(rhs);
708 setRetNodes.add(frn);
716 // insert a call to debugSnapshot() somewhere in the analysis to get
717 // successive captures of the analysis state
718 int debugCounter = 0;
719 int numStartCountReport = 0;
720 int freqCountReport = 1000;
721 int iterStartCapture = 20000;
722 int numIterToCapture = 400;
723 void debugSnapshot(OwnershipGraph og, FlatNode fn) {
725 if( debugCounter > numStartCountReport &&
726 debugCounter % freqCountReport == 0 ) {
727 System.out.println(" @@@ debug counter = "+debugCounter);
729 if( debugCounter > iterStartCapture ) {
730 System.out.println(" @@@ capturing debug "+(debugCounter-iterStartCapture)+" @@@");
731 String graphName = String.format("snap%04d",debugCounter-iterStartCapture);
733 graphName = graphName+fn;
736 og.writeGraph(graphName, true, true, false, false, false);
737 } catch( Exception e ) {
738 System.out.println("Error writing debug capture.");
742 if( debugCounter == iterStartCapture + numIterToCapture ) {
743 System.out.println("Stopping analysis after debug captures.");
750 // this method should generate integers strictly greater than zero!
751 // special "shadow" regions are made from a heap region by negating
753 static public Integer generateUniqueHeapRegionNodeID() {
755 return new Integer(uniqueIDcount);
759 private void setGraphForMethodContext(MethodContext mc, OwnershipGraph og)
762 mapMethodContextToCompleteOwnershipGraph.put(mc, og);
764 // arguments to writeGraph are:
765 // boolean writeLabels,
766 // boolean labelSelect,
767 // boolean pruneGarbage,
768 // boolean writeReferencers
769 // boolean writeParamMappings
773 if( !writeAllDOTs ) {
774 og.writeGraph(mc, true, true, true, false, false);
777 if( !mapMethodContextToNumUpdates.containsKey(mc) ) {
778 mapMethodContextToNumUpdates.put(mc, new Integer(0) );
780 Integer n = mapMethodContextToNumUpdates.get(mc);
781 og.writeGraph(mc, n, true, true, true, false, false);
782 mapMethodContextToNumUpdates.put(mc, n + 1);
788 // return just the allocation site associated with one FlatNew node
789 private AllocationSite getAllocationSiteFromFlatNewPRIVATE(FlatNew fn) {
791 if( !mapFlatNewToAllocationSite.containsKey(fn) ) {
792 AllocationSite as = new AllocationSite(allocationDepth, fn);
794 // the newest nodes are single objects
795 for( int i = 0; i < allocationDepth; ++i ) {
796 Integer id = generateUniqueHeapRegionNodeID();
797 as.setIthOldest(i, id);
800 // the oldest node is a summary node
801 Integer idSummary = generateUniqueHeapRegionNodeID();
802 as.setSummary(idSummary);
804 mapFlatNewToAllocationSite.put(fn, as);
807 return mapFlatNewToAllocationSite.get(fn);
811 // return all allocation sites in the method (there is one allocation
812 // site per FlatNew node in a method)
813 private HashSet<AllocationSite> getAllocationSiteSet(Descriptor d) {
814 if( !mapDescriptorToAllocationSiteSet.containsKey(d) ) {
815 buildAllocationSiteSet(d);
818 return mapDescriptorToAllocationSiteSet.get(d);
822 private void buildAllocationSiteSet(Descriptor d) {
823 HashSet<AllocationSite> s = new HashSet<AllocationSite>();
826 if( d instanceof MethodDescriptor ) {
827 fm = state.getMethodFlat( (MethodDescriptor) d);
829 assert d instanceof TaskDescriptor;
830 fm = state.getMethodFlat( (TaskDescriptor) d);
833 // visit every node in this FlatMethod's IR graph
834 // and make a set of the allocation sites from the
835 // FlatNew node's visited
836 HashSet<FlatNode> visited = new HashSet<FlatNode>();
837 HashSet<FlatNode> toVisit = new HashSet<FlatNode>();
840 while( !toVisit.isEmpty() ) {
841 FlatNode n = toVisit.iterator().next();
843 if( n instanceof FlatNew ) {
844 s.add(getAllocationSiteFromFlatNewPRIVATE( (FlatNew) n) );
850 for( int i = 0; i < n.numNext(); ++i ) {
851 FlatNode child = n.getNext(i);
852 if( !visited.contains(child) ) {
858 mapDescriptorToAllocationSiteSet.put(d, s);
862 private HashSet<AllocationSite>
863 getFlaggedAllocationSitesReachableFromTaskPRIVATE(TaskDescriptor td) {
865 HashSet<AllocationSite> asSetTotal = new HashSet<AllocationSite>();
866 HashSet<Descriptor> toVisit = new HashSet<Descriptor>();
867 HashSet<Descriptor> visited = new HashSet<Descriptor>();
871 // traverse this task and all methods reachable from this task
872 while( !toVisit.isEmpty() ) {
873 Descriptor d = toVisit.iterator().next();
877 HashSet<AllocationSite> asSet = getAllocationSiteSet(d);
878 Iterator asItr = asSet.iterator();
879 while( asItr.hasNext() ) {
880 AllocationSite as = (AllocationSite) asItr.next();
881 TypeDescriptor typed = as.getType();
882 if( typed != null ) {
883 ClassDescriptor cd = typed.getClassDesc();
884 if( cd != null && cd.hasFlags() ) {
890 // enqueue callees of this method to be searched for
891 // allocation sites also
892 Set callees = callGraph.getCalleeSet(d);
893 if( callees != null ) {
894 Iterator methItr = callees.iterator();
895 while( methItr.hasNext() ) {
896 MethodDescriptor md = (MethodDescriptor) methItr.next();
898 if( !visited.contains(md) ) {