big fix for tracking latest ownership graph associated with a flat node during a...
[IRC.git] / Robust / src / Analysis / OwnershipAnalysis / OwnershipAnalysis.java
1 package Analysis.OwnershipAnalysis;
2
3 import Analysis.CallGraph.*;
4 import IR.*;
5 import IR.Flat.*;
6 import IR.Tree.Modifiers;
7 import java.util.*;
8 import java.io.*;
9
10
11 public class OwnershipAnalysis {
12
13
14   ///////////////////////////////////////////
15   //
16   //  Public interface to discover possible
17   //  aliases in the program under analysis
18   //
19   ///////////////////////////////////////////
20
21   public HashSet<AllocationSite>
22   getFlaggedAllocationSitesReachableFromTask(TaskDescriptor td) {
23     return getFlaggedAllocationSitesReachableFromTaskPRIVATE(td);
24   }
25
26   public AllocationSite getAllocationSiteFromFlatNew(FlatNew fn) {
27     return getAllocationSiteFromFlatNewPRIVATE(fn);
28   }
29
30   public boolean createsPotentialAliases(Descriptor taskOrMethod,
31                                          int paramIndex1,
32                                          int paramIndex2) {
33
34     OwnershipGraph og = mapDescriptorToCompleteOwnershipGraph.get(taskOrMethod);
35     assert(og != null);
36     return og.hasPotentialAlias(paramIndex1, paramIndex2);
37   }
38
39   public boolean createsPotentialAliases(Descriptor taskOrMethod,
40                                          int paramIndex,
41                                          AllocationSite alloc) {
42
43     OwnershipGraph og = mapDescriptorToCompleteOwnershipGraph.get(taskOrMethod);
44     assert(og != null);
45     return og.hasPotentialAlias(paramIndex, alloc);
46   }
47
48   public boolean createsPotentialAliases(Descriptor taskOrMethod,
49                                          AllocationSite alloc,
50                                          int paramIndex) {
51
52     OwnershipGraph og = mapDescriptorToCompleteOwnershipGraph.get(taskOrMethod);
53     assert(og != null);
54     return og.hasPotentialAlias(paramIndex, alloc);
55   }
56
57   public boolean createsPotentialAliases(Descriptor taskOrMethod,
58                                          AllocationSite alloc1,
59                                          AllocationSite alloc2) {
60
61     OwnershipGraph og = mapDescriptorToCompleteOwnershipGraph.get(taskOrMethod);
62     assert(og != null);
63     return og.hasPotentialAlias(alloc1, alloc2);
64   }
65
66   // use the methods given above to check every possible alias
67   // between task parameters and flagged allocation sites reachable
68   // from the task
69   public void writeAllAliases(String outputFile) throws java.io.IOException {
70
71     BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile) );
72
73     bw.write("Conducting ownership analysis with allocation depth = "+allocationDepth);
74
75     // look through every task for potential aliases
76     Iterator taskItr = state.getTaskSymbolTable().getDescriptorsIterator();
77     while( taskItr.hasNext() ) {
78       TaskDescriptor td = (TaskDescriptor) taskItr.next();
79
80       bw.write("\n---------"+td+"--------\n");
81
82       HashSet<AllocationSite> allocSites = getFlaggedAllocationSitesReachableFromTask(td);
83
84       // for each task parameter, check for aliases with
85       // other task parameters and every allocation site
86       // reachable from this task
87       boolean foundSomeAlias = false;
88
89       FlatMethod fm = state.getMethodFlat(td);
90       for( int i = 0; i < fm.numParameters(); ++i ) {
91
92         // for the ith parameter check for aliases to all
93         // higher numbered parameters
94         for( int j = i + 1; j < fm.numParameters(); ++j ) {
95           if( createsPotentialAliases(td, i, j) ) {
96             foundSomeAlias = true;
97             bw.write("Potential alias between parameters "+i+" and "+j+".\n");
98           }
99         }
100
101         // for the ith parameter, check for aliases against
102         // the set of allocation sites reachable from this
103         // task context
104         Iterator allocItr = allocSites.iterator();
105         while( allocItr.hasNext() ) {
106           AllocationSite as = (AllocationSite) allocItr.next();
107           if( createsPotentialAliases(td, i, as) ) {
108             foundSomeAlias = true;
109             bw.write("Potential alias between parameter "+i+" and "+as+".\n");
110           }
111         }
112       }
113
114       // for each allocation site check for aliases with
115       // other allocation sites in the context of execution
116       // of this task
117       HashSet<AllocationSite> outerChecked = new HashSet<AllocationSite>();
118       Iterator allocItr1 = allocSites.iterator();
119       while( allocItr1.hasNext() ) {
120         AllocationSite as1 = (AllocationSite) allocItr1.next();
121
122         Iterator allocItr2 = allocSites.iterator();
123         while( allocItr2.hasNext() ) {
124           AllocationSite as2 = (AllocationSite) allocItr2.next();
125
126           if( !outerChecked.contains(as2) &&
127               createsPotentialAliases(td, as1, as2) ) {
128             foundSomeAlias = true;
129             bw.write("Potential alias between "+as1+" and "+as2+".\n");
130           }
131         }
132
133         outerChecked.add(as1);
134       }
135
136       if( !foundSomeAlias ) {
137         bw.write("Task "+td+" contains no aliases between flagged objects.\n");
138       }
139     }
140
141     bw.close();
142   }
143
144   ///////////////////////////////////////////
145   //
146   // end public interface
147   //
148   ///////////////////////////////////////////
149
150
151
152
153
154
155
156
157   // data from the compiler
158   private State state;
159   private TypeUtil typeUtil;
160   private CallGraph callGraph;
161   private int allocationDepth;
162
163   // used to identify HeapRegionNode objects
164   // A unique ID equates an object in one
165   // ownership graph with an object in another
166   // graph that logically represents the same
167   // heap region
168   // start at 10 and incerement to leave some
169   // reserved IDs for special purposes
170   static private int uniqueIDcount = 10;
171
172
173   // Use these data structures to track progress of
174   // processing all methods in the program, and by methods
175   // TaskDescriptor and MethodDescriptor are combined
176   // together, with a common parent class Descriptor
177   private Hashtable<FlatMethod, OwnershipGraph>           mapFlatMethodToInitialParamAllocGraph;
178   private Hashtable<Descriptor, OwnershipGraph>           mapDescriptorToCompleteOwnershipGraph;
179   private Hashtable<FlatNew,    AllocationSite>           mapFlatNewToAllocationSite;
180   private Hashtable<Descriptor, HashSet<AllocationSite> > mapDescriptorToAllocationSiteSet;
181   private Hashtable<Descriptor, Integer>                  mapDescriptorToNumUpdates;
182
183   // Use these data structures to track progress of one pass of
184   // processing the FlatNodes of a particular method
185   private HashSet  <FlatNode>                 flatNodesToVisit;
186   private Hashtable<FlatNode, OwnershipGraph> mapFlatNodeToOwnershipGraph;
187   private HashSet  <FlatReturnNode>           returnNodesToCombineForCompleteOwnershipGraph;
188
189   // descriptorsToAnalyze identifies the set of tasks and methods
190   // that are reachable from the program tasks, this set is initialized
191   // and then remains static
192   private HashSet<Descriptor> descriptorsToAnalyze;
193
194   // descriptorsToVisit is initialized to descriptorsToAnalyze and is
195   // reduced by visiting a descriptor during analysis.  When dependents
196   // must be scheduled, only those contained in descriptorsToAnalyze
197   // should be re-added to this set
198   private HashSet<Descriptor> descriptorsToVisit;
199
200   // a special field descriptor for all array elements
201   private static FieldDescriptor fdElement = new FieldDescriptor(new Modifiers(Modifiers.PUBLIC),
202                                                                  new TypeDescriptor("Array[]"),
203                                                                  "elements",
204                                                                  null,
205                                                                  false);
206   // for controlling DOT file output
207   private boolean writeDOTs;
208   private boolean writeAllDOTs;
209
210
211
212   // this analysis generates an ownership graph for every task
213   // in the program
214   public OwnershipAnalysis(State state,
215                            TypeUtil tu,
216                            CallGraph callGraph,
217                            int allocationDepth,
218                            boolean writeDOTs,
219                            boolean writeAllDOTs,
220                            String aliasFile) throws java.io.IOException {
221
222     this.state           = state;
223     this.typeUtil        = tu;
224     this.callGraph       = callGraph;
225     this.allocationDepth = allocationDepth;
226     this.writeDOTs       = writeDOTs;
227     this.writeAllDOTs    = writeAllDOTs;
228
229     descriptorsToAnalyze = new HashSet<Descriptor>();
230
231     mapFlatMethodToInitialParamAllocGraph =
232       new Hashtable<FlatMethod, OwnershipGraph>();
233
234     mapDescriptorToCompleteOwnershipGraph =
235       new Hashtable<Descriptor, OwnershipGraph>();
236
237     mapFlatNewToAllocationSite =
238       new Hashtable<FlatNew, AllocationSite>();
239
240     mapDescriptorToAllocationSiteSet =
241       new Hashtable<Descriptor, HashSet<AllocationSite> >();
242
243     if( writeAllDOTs ) {
244       mapDescriptorToNumUpdates = new Hashtable<Descriptor, Integer>();
245     }
246
247     // initialize methods to visit as the set of all tasks in the
248     // program and then any method that could be called starting
249     // from those tasks
250     Iterator taskItr = state.getTaskSymbolTable().getDescriptorsIterator();
251     while( taskItr.hasNext() ) {
252       Descriptor d = (Descriptor) taskItr.next();
253       scheduleAllCallees(d);
254     }
255
256     // before beginning analysis, initialize every scheduled method
257     // with an ownership graph that has populated parameter index tables
258     // by analyzing the first node which is always a FlatMethod node
259     Iterator<Descriptor> dItr = descriptorsToAnalyze.iterator();
260     while( dItr.hasNext() ) {
261       Descriptor d  = dItr.next();
262       OwnershipGraph og = new OwnershipGraph(allocationDepth, typeUtil);
263
264       FlatMethod fm;
265       if( d instanceof MethodDescriptor ) {
266         fm = state.getMethodFlat( (MethodDescriptor) d);
267       } else {
268         assert d instanceof TaskDescriptor;
269         fm = state.getMethodFlat( (TaskDescriptor) d);
270       }
271
272       System.out.println("Previsiting " + d);
273
274       og = analyzeFlatNode(d, fm, null, og);
275       setGraphForDescriptor(d, og);
276     }
277
278     System.out.println("");
279
280     // as mentioned above, analyze methods one-by-one, possibly revisiting
281     // a method if the methods that it calls are updated
282     analyzeMethods();
283
284     System.out.println("");
285
286     if( aliasFile != null ) {
287       writeAllAliases(aliasFile);
288     }
289   }
290
291   // called from the constructor to help initialize the set
292   // of methods that needs to be analyzed by ownership analysis
293   private void scheduleAllCallees(Descriptor d) {
294     if( descriptorsToAnalyze.contains(d) ) {
295       return;
296     }
297     descriptorsToAnalyze.add(d);
298
299     // start with all method calls to further schedule
300     Set moreMethodsToCheck = moreMethodsToCheck = callGraph.getMethodCalls(d);
301
302     if( d instanceof MethodDescriptor ) {
303       // see if this method has virtual dispatch
304       Set virtualMethods = callGraph.getMethods( (MethodDescriptor)d);
305       moreMethodsToCheck.addAll(virtualMethods);
306     }
307
308     // keep following any further methods identified in
309     // the call chain
310     Iterator methItr = moreMethodsToCheck.iterator();
311     while( methItr.hasNext() ) {
312       Descriptor m = (Descriptor) methItr.next();
313       scheduleAllCallees(m);
314     }
315   }
316
317
318   // manage the set of tasks and methods to be analyzed
319   // and be sure to reschedule tasks/methods when the methods
320   // they call are updated
321   private void analyzeMethods() throws java.io.IOException {
322
323     descriptorsToVisit = (HashSet<Descriptor>)descriptorsToAnalyze.clone();
324
325     while( !descriptorsToVisit.isEmpty() ) {
326       Descriptor d = (Descriptor) descriptorsToVisit.iterator().next();
327       descriptorsToVisit.remove(d);
328
329       // because the task or method descriptor just extracted
330       // was in the "to visit" set it either hasn't been analyzed
331       // yet, or some method that it depends on has been
332       // updated.  Recompute a complete ownership graph for
333       // this task/method and compare it to any previous result.
334       // If there is a change detected, add any methods/tasks
335       // that depend on this one to the "to visit" set.
336
337       System.out.println("Analyzing " + d);
338
339       FlatMethod fm;
340       if( d instanceof MethodDescriptor ) {
341         fm = state.getMethodFlat( (MethodDescriptor) d);
342       } else {
343         assert d instanceof TaskDescriptor;
344         fm = state.getMethodFlat( (TaskDescriptor) d);
345       }
346
347       OwnershipGraph og = analyzeFlatMethod(d, fm);
348       OwnershipGraph ogPrev = mapDescriptorToCompleteOwnershipGraph.get(d);
349       if( !og.equals(ogPrev) ) {
350
351         setGraphForDescriptor(d, og);
352
353         // only methods have dependents, tasks cannot
354         // be invoked by any user program calls
355         if( d instanceof MethodDescriptor ) {
356           MethodDescriptor md = (MethodDescriptor) d;
357           Set dependents = callGraph.getCallerSet(md);
358           if( dependents != null ) {
359             Iterator depItr = dependents.iterator();
360             while( depItr.hasNext() ) {
361               Descriptor dependent = (Descriptor) depItr.next();
362               if( descriptorsToAnalyze.contains(dependent) ) {
363                 descriptorsToVisit.add(dependent);
364               }
365             }
366           }
367         }
368       }
369     }
370
371   }
372
373
374   // keep passing the Descriptor of the method along for debugging
375   // and dot file writing
376   private OwnershipGraph
377   analyzeFlatMethod(Descriptor mDesc,
378                     FlatMethod flatm) throws java.io.IOException {
379
380     // initialize flat nodes to visit as the flat method
381     // because all other nodes in this flat method are
382     // decendents of the flat method itself
383
384     flatNodesToVisit = new HashSet<FlatNode>();
385     flatNodesToVisit.add(flatm);
386
387     // initilize the mapping of flat nodes in this flat method to
388     // ownership graph results to an empty mapping
389     mapFlatNodeToOwnershipGraph = new Hashtable<FlatNode, OwnershipGraph>();
390
391     // initialize the set of return nodes that will be combined as
392     // the final ownership graph result to return as an empty set
393     returnNodesToCombineForCompleteOwnershipGraph = new HashSet<FlatReturnNode>();
394
395
396     while( !flatNodesToVisit.isEmpty() ) {
397       FlatNode fn = (FlatNode) flatNodesToVisit.iterator().next();
398       flatNodesToVisit.remove(fn);
399
400       // perform this node's contributions to the ownership
401       // graph on a new copy, then compare it to the old graph
402       // at this node to see if anything was updated.
403       OwnershipGraph og = new OwnershipGraph(allocationDepth, typeUtil);
404
405       // start by merging all node's parents' graphs
406       for( int i = 0; i < fn.numPrev(); ++i ) {
407         FlatNode pn = fn.getPrev(i);    
408         if( mapFlatNodeToOwnershipGraph.containsKey(pn) ) {
409           OwnershipGraph ogParent = mapFlatNodeToOwnershipGraph.get(pn);
410           og.merge(ogParent);
411         }
412       }
413
414       // apply the analysis of the flat node to the
415       // ownership graph made from the merge of the
416       // parent graphs
417       og = analyzeFlatNode(mDesc,
418                            fn,
419                            returnNodesToCombineForCompleteOwnershipGraph,
420                            og);
421
422       // if the results of the new graph are different from
423       // the current graph at this node, replace the graph
424       // with the update and enqueue the children for
425       // processing
426       OwnershipGraph ogPrev = mapFlatNodeToOwnershipGraph.get(fn);
427       if( !og.equals(ogPrev) ) {
428         mapFlatNodeToOwnershipGraph.put(fn, og);
429
430         for( int i = 0; i < fn.numNext(); i++ ) {
431           FlatNode nn = fn.getNext(i);
432           flatNodesToVisit.add(nn);
433         }
434       }
435     }
436
437     // end by merging all return nodes into a complete
438     // ownership graph that represents all possible heap
439     // states after the flat method returns
440     OwnershipGraph completeGraph = new OwnershipGraph(allocationDepth, typeUtil);
441     Iterator retItr = returnNodesToCombineForCompleteOwnershipGraph.iterator();
442     while( retItr.hasNext() ) {
443       FlatReturnNode frn = (FlatReturnNode) retItr.next();
444       assert mapFlatNodeToOwnershipGraph.containsKey(frn);
445       OwnershipGraph ogr = mapFlatNodeToOwnershipGraph.get(frn);
446       completeGraph.merge(ogr);
447     }
448     return completeGraph;
449   }
450
451
452   private OwnershipGraph
453   analyzeFlatNode(Descriptor methodDesc,
454                   FlatNode fn,
455                   HashSet<FlatReturnNode> setRetNodes,
456                   OwnershipGraph og) throws java.io.IOException {
457
458     TempDescriptor lhs;
459     TempDescriptor rhs;
460     FieldDescriptor fld;
461
462     // use node type to decide what alterations to make
463     // to the ownership graph
464     switch( fn.kind() ) {
465
466     case FKind.FlatMethod:
467       FlatMethod fm = (FlatMethod) fn;
468
469       // there should only be one FlatMethod node as the
470       // parent of all other FlatNode objects, so take
471       // the opportunity to construct the initial graph by
472       // adding parameters labels to new heap regions
473       // AND this should be done once globally so that the
474       // parameter IDs are consistent between analysis
475       // iterations, so if this step has been done already
476       // just merge in the cached version
477       OwnershipGraph ogInitParamAlloc = mapFlatMethodToInitialParamAllocGraph.get(fm);
478       if( ogInitParamAlloc == null ) {
479
480         // analyze this node one time globally
481         for( int i = 0; i < fm.numParameters(); ++i ) {
482           TempDescriptor tdParam = fm.getParameter(i);
483           og.assignTempEqualToParamAlloc(tdParam,
484                                          methodDesc instanceof TaskDescriptor,
485                                          new Integer(i) );
486         }
487
488         // then remember it
489         OwnershipGraph ogResult = new OwnershipGraph(allocationDepth, typeUtil);
490         ogResult.merge(og);
491         mapFlatMethodToInitialParamAllocGraph.put(fm, ogResult);
492
493       } else {
494         // or just leverage the cached copy
495         og.merge(ogInitParamAlloc);
496       }
497       break;
498
499     case FKind.FlatOpNode:
500       FlatOpNode fon = (FlatOpNode) fn;
501       if( fon.getOp().getOp() == Operation.ASSIGN ) {
502         lhs = fon.getDest();
503         rhs = fon.getLeft();
504         og.assignTempXEqualToTempY(lhs, rhs);
505       }
506       break;
507
508     case FKind.FlatFieldNode:
509       FlatFieldNode ffn = (FlatFieldNode) fn;
510       lhs = ffn.getDst();
511       rhs = ffn.getSrc();
512       fld = ffn.getField();
513       if( !fld.getType().isPrimitive() ) {
514         og.assignTempXEqualToTempYFieldF(lhs, rhs, fld);
515       }
516       break;
517
518     case FKind.FlatSetFieldNode:
519       FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
520       lhs = fsfn.getDst();
521       fld = fsfn.getField();
522       rhs = fsfn.getSrc();
523       og.assignTempXFieldFEqualToTempY(lhs, fld, rhs);
524       break;
525
526     case FKind.FlatElementNode:
527       FlatElementNode fen = (FlatElementNode) fn;
528       lhs = fen.getDst();
529       rhs = fen.getSrc();
530       if( !lhs.getType().isPrimitive() ) {
531         og.assignTempXEqualToTempYFieldF(lhs, rhs, fdElement);
532       }
533       break;
534
535     case FKind.FlatSetElementNode:
536       FlatSetElementNode fsen = (FlatSetElementNode) fn;
537       lhs = fsen.getDst();
538       rhs = fsen.getSrc();
539       if( !rhs.getType().isPrimitive() ) {
540         og.assignTempXFieldFEqualToTempY(lhs, fdElement, rhs);
541       }
542       break;
543
544     case FKind.FlatNew:
545       FlatNew fnn = (FlatNew) fn;
546       lhs = fnn.getDst();
547       AllocationSite as = getAllocationSiteFromFlatNewPRIVATE(fnn);
548
549       og.assignTempEqualToNewAlloc(lhs, as);
550       break;
551
552     case FKind.FlatCall:
553       FlatCall fc = (FlatCall) fn;
554       MethodDescriptor md = fc.getMethod();
555       FlatMethod flatm = state.getMethodFlat(md);
556       OwnershipGraph ogMergeOfAllPossibleCalleeResults = new OwnershipGraph(allocationDepth, typeUtil);
557
558       if( md.isStatic() ) {
559         // a static method is simply always the same, makes life easy
560         OwnershipGraph onlyPossibleCallee = mapDescriptorToCompleteOwnershipGraph.get(md);
561         ogMergeOfAllPossibleCalleeResults = og;
562         ogMergeOfAllPossibleCalleeResults.resolveMethodCall(fc, md.isStatic(), flatm, onlyPossibleCallee);
563       } else {
564         // if the method descriptor is virtual, then there could be a
565         // set of possible methods that will actually be invoked, so
566         // find all of them and merge all of their results together
567         TypeDescriptor typeDesc = fc.getThis().getType();
568         Set possibleCallees = callGraph.getMethods(md, typeDesc);
569
570         Iterator i = possibleCallees.iterator();
571         while( i.hasNext() ) {
572           MethodDescriptor possibleMd = (MethodDescriptor) i.next();
573
574           // don't alter the working graph (og) until we compute a result for every
575           // possible callee, merge them all together, then set og to that
576           OwnershipGraph ogCopy = new OwnershipGraph(allocationDepth, typeUtil);
577           ogCopy.merge(og);
578
579           OwnershipGraph ogPotentialCallee = mapDescriptorToCompleteOwnershipGraph.get(possibleMd);
580           ogCopy.resolveMethodCall(fc, md.isStatic(), flatm, ogPotentialCallee);
581           ogMergeOfAllPossibleCalleeResults.merge(ogCopy);
582         }
583       }
584
585       og = ogMergeOfAllPossibleCalleeResults;      
586       break;
587
588     case FKind.FlatReturnNode:
589       FlatReturnNode frn = (FlatReturnNode) fn;
590       rhs = frn.getReturnTemp();
591
592       if( rhs != null ) {
593         og.assignReturnEqualToTemp(rhs);
594       }
595
596       setRetNodes.add(frn);
597       break;
598     }
599
600     return og;
601   }
602
603
604   // insert a call to debugSnapshot() somewhere in the analysis to get
605   // successive captures of the analysis state
606   int debugCounter = 0;
607   int numIterationsIn = 30;
608   int numIterationsToCapture = 20;
609   void debugSnapshot( OwnershipGraph og, FlatNode fn ) {
610     ++debugCounter;
611     if( debugCounter > numIterationsIn ) {
612       System.out.println( "   @@@ capturing debug "+(debugCounter-numIterationsIn)+" @@@" );
613       String graphName = String.format("test%04d",debugCounter-numIterationsIn);
614       if( fn != null ) {
615         graphName = graphName+fn;
616       }
617       try {
618         og.writeGraph( graphName, true, true, true, false, false );
619       } catch( Exception e ) {
620         System.out.println( "Error writing debug capture." );
621         System.exit( 0 );       
622       }
623     }
624     if( debugCounter == numIterationsIn + numIterationsToCapture ) {
625       System.out.println( "Stopping analysis after debug captures." );
626       System.exit( 0 );
627     }
628   }
629
630
631
632   // this method should generate integers strictly greater than zero!
633   // special "shadow" regions are made from a heap region by negating
634   // the ID
635   static public Integer generateUniqueHeapRegionNodeID() {
636     ++uniqueIDcount;
637     return new Integer(uniqueIDcount);
638   }
639
640
641   private void setGraphForDescriptor(Descriptor d, OwnershipGraph og)
642   throws IOException {
643
644     mapDescriptorToCompleteOwnershipGraph.put(d, og);
645
646     // arguments to writeGraph are:
647     // boolean writeLabels,
648     // boolean labelSelect,
649     // boolean pruneGarbage,
650     // boolean writeReferencers
651     // boolean writeParamMappings
652
653     if( writeDOTs ) {
654
655       if( !writeAllDOTs ) {
656         og.writeGraph(d, true, true, true, false, false);
657
658       } else {
659         if( !mapDescriptorToNumUpdates.containsKey(d) ) {
660           mapDescriptorToNumUpdates.put(d, new Integer(0) );
661         }
662         Integer n = mapDescriptorToNumUpdates.get(d);
663         og.writeGraph(d, n, true, true, true, false, false);
664         mapDescriptorToNumUpdates.put(d, n + 1);
665       }
666     }
667   }
668
669
670   // return just the allocation site associated with one FlatNew node
671   private AllocationSite getAllocationSiteFromFlatNewPRIVATE(FlatNew fn) {
672
673     if( !mapFlatNewToAllocationSite.containsKey(fn) ) {
674       AllocationSite as = new AllocationSite(allocationDepth, fn.getType() );
675
676       // the newest nodes are single objects
677       for( int i = 0; i < allocationDepth; ++i ) {
678         Integer id = generateUniqueHeapRegionNodeID();
679         as.setIthOldest(i, id);
680       }
681
682       // the oldest node is a summary node
683       Integer idSummary = generateUniqueHeapRegionNodeID();
684       as.setSummary(idSummary);
685
686       mapFlatNewToAllocationSite.put(fn, as);
687     }
688
689     return mapFlatNewToAllocationSite.get(fn);
690   }
691
692
693   // return all allocation sites in the method (there is one allocation
694   // site per FlatNew node in a method)
695   private HashSet<AllocationSite> getAllocationSiteSet(Descriptor d) {
696     if( !mapDescriptorToAllocationSiteSet.containsKey(d) ) {
697       buildAllocationSiteSet(d);
698     }
699
700     return mapDescriptorToAllocationSiteSet.get(d);
701
702   }
703
704   private void buildAllocationSiteSet(Descriptor d) {
705     HashSet<AllocationSite> s = new HashSet<AllocationSite>();
706
707     FlatMethod fm;
708     if( d instanceof MethodDescriptor ) {
709       fm = state.getMethodFlat( (MethodDescriptor) d);
710     } else {
711       assert d instanceof TaskDescriptor;
712       fm = state.getMethodFlat( (TaskDescriptor) d);
713     }
714
715     // visit every node in this FlatMethod's IR graph
716     // and make a set of the allocation sites from the
717     // FlatNew node's visited
718     HashSet<FlatNode> visited = new HashSet<FlatNode>();
719     HashSet<FlatNode> toVisit = new HashSet<FlatNode>();
720     toVisit.add(fm);
721
722     while( !toVisit.isEmpty() ) {
723       FlatNode n = toVisit.iterator().next();
724
725       if( n instanceof FlatNew ) {
726         s.add(getAllocationSiteFromFlatNewPRIVATE( (FlatNew) n) );
727       }
728
729       toVisit.remove(n);
730       visited.add(n);
731
732       for( int i = 0; i < n.numNext(); ++i ) {
733         FlatNode child = n.getNext(i);
734         if( !visited.contains(child) ) {
735           toVisit.add(child);
736         }
737       }
738     }
739
740     mapDescriptorToAllocationSiteSet.put(d, s);
741   }
742
743
744   private HashSet<AllocationSite>
745   getFlaggedAllocationSitesReachableFromTaskPRIVATE(TaskDescriptor td) {
746
747     HashSet<AllocationSite> asSetTotal = new HashSet<AllocationSite>();
748     HashSet<Descriptor>     toVisit    = new HashSet<Descriptor>();
749     HashSet<Descriptor>     visited    = new HashSet<Descriptor>();
750
751     toVisit.add(td);
752
753     // traverse this task and all methods reachable from this task
754     while( !toVisit.isEmpty() ) {
755       Descriptor d = toVisit.iterator().next();
756       toVisit.remove(d);
757       visited.add(d);
758
759       HashSet<AllocationSite> asSet = getAllocationSiteSet(d);
760       Iterator asItr = asSet.iterator();
761       while( asItr.hasNext() ) {
762         AllocationSite as = (AllocationSite) asItr.next();
763         TypeDescriptor typed = as.getType();
764         if( typed != null ) {
765           ClassDescriptor cd = typed.getClassDesc();
766           if( cd != null && cd.hasFlags() ) {
767             asSetTotal.add(as);
768           }
769         }
770       }
771
772       // enqueue callees of this method to be searched for
773       // allocation sites also
774       Set callees = callGraph.getCalleeSet(d);
775       if( callees != null ) {
776         Iterator methItr = callees.iterator();
777         while( methItr.hasNext() ) {
778           MethodDescriptor md = (MethodDescriptor) methItr.next();
779
780           if( !visited.contains(md) ) {
781             toVisit.add(md);
782           }
783         }
784       }
785     }
786
787
788     return asSetTotal;
789   }
790 }