1 package Analysis.Disjoint;
3 import Analysis.CallGraph.*;
4 import Analysis.Liveness;
5 import Analysis.ArrayReferencees;
8 import IR.Tree.Modifiers;
13 public class DisjointAnalysis {
15 ///////////////////////////////////////////
17 // Public interface to discover possible
18 // aliases in the program under analysis
20 ///////////////////////////////////////////
22 public HashSet<AllocSite>
23 getFlaggedAllocationSitesReachableFromTask(TaskDescriptor td) {
24 checkAnalysisComplete();
25 return getFlaggedAllocationSitesReachableFromTaskPRIVATE(td);
28 public AllocSite getAllocationSiteFromFlatNew(FlatNew fn) {
29 checkAnalysisComplete();
30 return getAllocSiteFromFlatNewPRIVATE(fn);
33 public AllocSite getAllocationSiteFromHeapRegionNodeID(Integer id) {
34 checkAnalysisComplete();
35 return mapHrnIdToAllocSite.get(id);
38 public Set<HeapRegionNode> createsPotentialAliases(Descriptor taskOrMethod,
41 checkAnalysisComplete();
42 ReachGraph rg=mapDescriptorToCompleteReachGraph.get(taskOrMethod);
43 FlatMethod fm=state.getMethodFlat(taskOrMethod);
45 return rg.mayReachSharedObjects(fm, paramIndex1, paramIndex2);
48 public Set<HeapRegionNode> createsPotentialAliases(Descriptor taskOrMethod,
49 int paramIndex, AllocSite alloc) {
50 checkAnalysisComplete();
51 ReachGraph rg = mapDescriptorToCompleteReachGraph.get(taskOrMethod);
52 FlatMethod fm=state.getMethodFlat(taskOrMethod);
54 return rg.mayReachSharedObjects(fm, paramIndex, alloc);
57 public Set<HeapRegionNode> createsPotentialAliases(Descriptor taskOrMethod,
58 AllocSite alloc, int paramIndex) {
59 checkAnalysisComplete();
60 ReachGraph rg = mapDescriptorToCompleteReachGraph.get(taskOrMethod);
61 FlatMethod fm=state.getMethodFlat(taskOrMethod);
63 return rg.mayReachSharedObjects(fm, paramIndex, alloc);
66 public Set<HeapRegionNode> createsPotentialAliases(Descriptor taskOrMethod,
67 AllocSite alloc1, AllocSite alloc2) {
68 checkAnalysisComplete();
69 ReachGraph rg = mapDescriptorToCompleteReachGraph.get(taskOrMethod);
71 return rg.mayReachSharedObjects(alloc1, alloc2);
74 public String prettyPrintNodeSet(Set<HeapRegionNode> s) {
75 checkAnalysisComplete();
79 Iterator<HeapRegionNode> i = s.iterator();
81 HeapRegionNode n = i.next();
83 AllocSite as = n.getAllocSite();
85 out += " " + n.toString() + ",\n";
87 out += " " + n.toString() + ": " + as.toStringVerbose()
96 // use the methods given above to check every possible alias
97 // between task parameters and flagged allocation sites reachable
99 public void writeAllAliases(String outputFile,
102 boolean tabularOutput,
105 throws java.io.IOException {
106 checkAnalysisComplete();
108 BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile));
110 if (!tabularOutput) {
111 bw.write("Conducting ownership analysis with allocation depth = "
112 + allocationDepth + "\n");
113 bw.write(timeReport + "\n");
118 // look through every task for potential aliases
119 Iterator taskItr = state.getTaskSymbolTable().getDescriptorsIterator();
120 while (taskItr.hasNext()) {
121 TaskDescriptor td = (TaskDescriptor) taskItr.next();
123 if (!tabularOutput) {
124 bw.write("\n---------" + td + "--------\n");
127 HashSet<AllocSite> allocSites = getFlaggedAllocationSitesReachableFromTask(td);
129 Set<HeapRegionNode> common;
131 // for each task parameter, check for aliases with
132 // other task parameters and every allocation site
133 // reachable from this task
134 boolean foundSomeAlias = false;
136 FlatMethod fm = state.getMethodFlat(td);
137 for (int i = 0; i < fm.numParameters(); ++i) {
139 // for the ith parameter check for aliases to all
140 // higher numbered parameters
141 for (int j = i + 1; j < fm.numParameters(); ++j) {
142 common = createsPotentialAliases(td, i, j);
143 if (!common.isEmpty()) {
144 foundSomeAlias = true;
145 if (!tabularOutput) {
146 bw.write("Potential alias between parameters " + i
147 + " and " + j + ".\n");
148 bw.write(prettyPrintNodeSet(common) + "\n");
155 // for the ith parameter, check for aliases against
156 // the set of allocation sites reachable from this
158 Iterator allocItr = allocSites.iterator();
159 while (allocItr.hasNext()) {
160 AllocSite as = (AllocSite) allocItr.next();
161 common = createsPotentialAliases(td, i, as);
162 if (!common.isEmpty()) {
163 foundSomeAlias = true;
164 if (!tabularOutput) {
165 bw.write("Potential alias between parameter " + i
166 + " and " + as.getFlatNew() + ".\n");
167 bw.write(prettyPrintNodeSet(common) + "\n");
175 // for each allocation site check for aliases with
176 // other allocation sites in the context of execution
178 HashSet<AllocSite> outerChecked = new HashSet<AllocSite>();
179 Iterator allocItr1 = allocSites.iterator();
180 while (allocItr1.hasNext()) {
181 AllocSite as1 = (AllocSite) allocItr1.next();
183 Iterator allocItr2 = allocSites.iterator();
184 while (allocItr2.hasNext()) {
185 AllocSite as2 = (AllocSite) allocItr2.next();
187 if (!outerChecked.contains(as2)) {
188 common = createsPotentialAliases(td, as1, as2);
190 if (!common.isEmpty()) {
191 foundSomeAlias = true;
192 if (!tabularOutput) {
193 bw.write("Potential alias between "
194 + as1.getFlatNew() + " and "
195 + as2.getFlatNew() + ".\n");
196 bw.write(prettyPrintNodeSet(common) + "\n");
204 outerChecked.add(as1);
207 if (!foundSomeAlias) {
208 if (!tabularOutput) {
209 bw.write("No aliases between flagged objects in Task " + td
216 if (!tabularOutput) {
217 bw.write("\n" + computeAliasContextHistogram());
219 bw.write(" & " + numAlias + " & " + justTime + " & " + numLines
220 + " & " + numMethodsAnalyzed() + " \\\\\n");
227 // this version of writeAllAliases is for Java programs that have no tasks
228 public void writeAllAliasesJava(String outputFile,
231 boolean tabularOutput,
234 throws java.io.IOException {
235 checkAnalysisComplete();
239 BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile));
241 bw.write("Conducting ownership analysis with allocation depth = "
242 + allocationDepth + "\n");
243 bw.write(timeReport + "\n\n");
245 boolean foundSomeAlias = false;
247 Descriptor d = typeUtil.getMain();
248 HashSet<AllocSite> allocSites = getFlaggedAllocationSites(d);
250 // for each allocation site check for aliases with
251 // other allocation sites in the context of execution
253 HashSet<AllocSite> outerChecked = new HashSet<AllocSite>();
254 Iterator allocItr1 = allocSites.iterator();
255 while (allocItr1.hasNext()) {
256 AllocSite as1 = (AllocSite) allocItr1.next();
258 Iterator allocItr2 = allocSites.iterator();
259 while (allocItr2.hasNext()) {
260 AllocSite as2 = (AllocSite) allocItr2.next();
262 if (!outerChecked.contains(as2)) {
263 Set<HeapRegionNode> common = createsPotentialAliases(d,
266 if (!common.isEmpty()) {
267 foundSomeAlias = true;
268 bw.write("Potential alias between "
269 + as1.getDisjointAnalysisId() + " and "
270 + as2.getDisjointAnalysisId() + ".\n");
271 bw.write(prettyPrintNodeSet(common) + "\n");
276 outerChecked.add(as1);
279 if (!foundSomeAlias) {
280 bw.write("No aliases between flagged objects found.\n");
283 // bw.write("\n" + computeAliasContextHistogram());
287 ///////////////////////////////////////////
289 // end public interface
291 ///////////////////////////////////////////
293 protected void checkAnalysisComplete() {
294 if( !analysisComplete ) {
295 throw new Error("Warning: public interface method called while analysis is running.");
300 // data from the compiler
302 public CallGraph callGraph;
303 public Liveness liveness;
304 public ArrayReferencees arrayReferencees;
305 public TypeUtil typeUtil;
306 public int allocationDepth;
308 // data structure for public interface
309 private Hashtable<Descriptor, HashSet<AllocSite> > mapDescriptorToAllocSiteSet;
312 // for public interface methods to warn that they
313 // are grabbing results during analysis
314 private boolean analysisComplete;
317 // used to identify HeapRegionNode objects
318 // A unique ID equates an object in one
319 // ownership graph with an object in another
320 // graph that logically represents the same
322 // start at 10 and increment to reserve some
323 // IDs for special purposes
324 static protected int uniqueIDcount = 10;
327 // An out-of-scope method created by the
328 // analysis that has no parameters, and
329 // appears to allocate the command line
330 // arguments, then invoke the source code's
331 // main method. The purpose of this is to
332 // provide the analysis with an explicit
333 // top-level context with no parameters
334 protected MethodDescriptor mdAnalysisEntry;
335 protected FlatMethod fmAnalysisEntry;
337 // main method defined by source program
338 protected MethodDescriptor mdSourceEntry;
340 // the set of task and/or method descriptors
341 // reachable in call graph
342 protected Set<Descriptor>
343 descriptorsToAnalyze;
345 // current descriptors to visit in fixed-point
346 // interprocedural analysis, prioritized by
347 // dependency in the call graph
348 protected PriorityQueue<DescriptorQWrapper>
351 // a duplication of the above structure, but
352 // for efficient testing of inclusion
353 protected HashSet<Descriptor>
354 descriptorsToVisitSet;
356 // storage for priorities (doesn't make sense)
357 // to add it to the Descriptor class, just in
359 protected Hashtable<Descriptor, Integer>
360 mapDescriptorToPriority;
363 // maps a descriptor to its current partial result
364 // from the intraprocedural fixed-point analysis--
365 // then the interprocedural analysis settles, this
366 // mapping will have the final results for each
368 protected Hashtable<Descriptor, ReachGraph>
369 mapDescriptorToCompleteReachGraph;
371 // maps a descriptor to its known dependents: namely
372 // methods or tasks that call the descriptor's method
373 // AND are part of this analysis (reachable from main)
374 protected Hashtable< Descriptor, Set<Descriptor> >
375 mapDescriptorToSetDependents;
377 // maps each flat new to one analysis abstraction
378 // allocate site object, these exist outside reach graphs
379 protected Hashtable<FlatNew, AllocSite>
380 mapFlatNewToAllocSite;
382 // maps intergraph heap region IDs to intergraph
383 // allocation sites that created them, a redundant
384 // structure for efficiency in some operations
385 protected Hashtable<Integer, AllocSite>
388 // maps a method to its initial heap model (IHM) that
389 // is the set of reachability graphs from every caller
390 // site, all merged together. The reason that we keep
391 // them separate is that any one call site's contribution
392 // to the IHM may changed along the path to the fixed point
393 protected Hashtable< Descriptor, Hashtable< FlatCall, ReachGraph > >
394 mapDescriptorToIHMcontributions;
396 // TODO -- CHANGE EDGE/TYPE/FIELD storage!
397 public static final String arrayElementFieldName = "___element_";
398 static protected Hashtable<TypeDescriptor, FieldDescriptor>
401 // for controlling DOT file output
402 protected boolean writeFinalDOTs;
403 protected boolean writeAllIncrementalDOTs;
405 // supporting DOT output--when we want to write every
406 // partial method result, keep a tally for generating
408 protected Hashtable<Descriptor, Integer>
409 mapDescriptorToNumUpdates;
411 //map task descriptor to initial task parameter
412 protected Hashtable<Descriptor, ReachGraph>
413 mapDescriptorToReachGraph;
416 // allocate various structures that are not local
417 // to a single class method--should be done once
418 protected void allocateStructures() {
419 descriptorsToAnalyze = new HashSet<Descriptor>();
421 mapDescriptorToCompleteReachGraph =
422 new Hashtable<Descriptor, ReachGraph>();
424 mapDescriptorToNumUpdates =
425 new Hashtable<Descriptor, Integer>();
427 mapDescriptorToSetDependents =
428 new Hashtable< Descriptor, Set<Descriptor> >();
430 mapFlatNewToAllocSite =
431 new Hashtable<FlatNew, AllocSite>();
433 mapDescriptorToIHMcontributions =
434 new Hashtable< Descriptor, Hashtable< FlatCall, ReachGraph > >();
436 mapHrnIdToAllocSite =
437 new Hashtable<Integer, AllocSite>();
439 mapTypeToArrayField =
440 new Hashtable <TypeDescriptor, FieldDescriptor>();
442 descriptorsToVisitQ =
443 new PriorityQueue<DescriptorQWrapper>();
445 descriptorsToVisitSet =
446 new HashSet<Descriptor>();
448 mapDescriptorToPriority =
449 new Hashtable<Descriptor, Integer>();
451 mapDescriptorToAllocSiteSet =
452 new Hashtable<Descriptor, HashSet<AllocSite> >();
454 mapDescriptorToReachGraph =
455 new Hashtable<Descriptor, ReachGraph>();
460 // this analysis generates a disjoint reachability
461 // graph for every reachable method in the program
462 public DisjointAnalysis( State s,
467 ) throws java.io.IOException {
468 init( s, tu, cg, l, ar );
471 protected void init( State state,
475 ArrayReferencees arrayReferencees
476 ) throws java.io.IOException {
478 analysisComplete = false;
481 this.typeUtil = typeUtil;
482 this.callGraph = callGraph;
483 this.liveness = liveness;
484 this.arrayReferencees = arrayReferencees;
485 this.allocationDepth = state.DISJOINTALLOCDEPTH;
486 this.writeFinalDOTs = state.DISJOINTWRITEDOTS && !state.DISJOINTWRITEALL;
487 this.writeAllIncrementalDOTs = state.DISJOINTWRITEDOTS && state.DISJOINTWRITEALL;
489 // set some static configuration for ReachGraphs
490 ReachGraph.allocationDepth = allocationDepth;
491 ReachGraph.typeUtil = typeUtil;
493 allocateStructures();
495 double timeStartAnalysis = (double) System.nanoTime();
497 // start interprocedural fixed-point computation
499 analysisComplete=true;
501 double timeEndAnalysis = (double) System.nanoTime();
502 double dt = (timeEndAnalysis - timeStartAnalysis)/(Math.pow( 10.0, 9.0 ) );
503 String treport = String.format( "The reachability analysis took %.3f sec.", dt );
504 String justtime = String.format( "%.2f", dt );
505 System.out.println( treport );
507 if( writeFinalDOTs && !writeAllIncrementalDOTs ) {
511 if( state.DISJOINTWRITEIHMS ) {
515 if( state.DISJOINTALIASFILE != null ) {
517 // not supporting tasks yet...
518 writeAllAliases(state.OWNERSHIPALIASFILE, treport, justtime, state.OWNERSHIPALIASTAB, state.lines);
521 writeAllAliasesJava( aliasFile,
524 state.DISJOINTALIASTAB,
532 // fixed-point computation over the call graph--when a
533 // method's callees are updated, it must be reanalyzed
534 protected void analyzeMethods() throws java.io.IOException {
537 // This analysis does not support Bamboo at the moment,
538 // but if it does in the future we would initialize the
539 // set of descriptors to analyze as the program-reachable
540 // tasks and the methods callable by them. For Java,
541 // just methods reachable from the main method.
542 System.out.println( "Bamboo..." );
543 Iterator taskItr = state.getTaskSymbolTable().getDescriptorsIterator();
545 while (taskItr.hasNext()) {
546 TaskDescriptor td = (TaskDescriptor) taskItr.next();
547 if (!descriptorsToAnalyze.contains(td)) {
548 descriptorsToAnalyze.add(td);
549 descriptorsToAnalyze.addAll(callGraph.getAllMethods(td));
554 // add all methods transitively reachable from the
555 // source's main to set for analysis
556 mdSourceEntry = typeUtil.getMain();
557 descriptorsToAnalyze.add( mdSourceEntry );
558 descriptorsToAnalyze.addAll(
559 callGraph.getAllMethods( mdSourceEntry )
562 // fabricate an empty calling context that will call
563 // the source's main, but call graph doesn't know
564 // about it, so explicitly add it
565 makeAnalysisEntryMethod( mdSourceEntry );
566 descriptorsToAnalyze.add( mdAnalysisEntry );
569 // topologically sort according to the call graph so
570 // leaf calls are ordered first, smarter analysis order
571 LinkedList<Descriptor> sortedDescriptors =
572 topologicalSort( descriptorsToAnalyze );
574 // add sorted descriptors to priority queue, and duplicate
575 // the queue as a set for efficiently testing whether some
576 // method is marked for analysis
578 Iterator<Descriptor> dItr = sortedDescriptors.iterator();
579 while( dItr.hasNext() ) {
580 Descriptor d = dItr.next();
581 mapDescriptorToPriority.put( d, new Integer( p ) );
582 descriptorsToVisitQ.add( new DescriptorQWrapper( p, d ) );
583 descriptorsToVisitSet.add( d );
587 // analyze methods from the priority queue until it is empty
588 while( !descriptorsToVisitQ.isEmpty() ) {
589 Descriptor d = descriptorsToVisitQ.poll().getDescriptor();
590 assert descriptorsToVisitSet.contains( d );
591 descriptorsToVisitSet.remove( d );
593 // because the task or method descriptor just extracted
594 // was in the "to visit" set it either hasn't been analyzed
595 // yet, or some method that it depends on has been
596 // updated. Recompute a complete reachability graph for
597 // this task/method and compare it to any previous result.
598 // If there is a change detected, add any methods/tasks
599 // that depend on this one to the "to visit" set.
601 System.out.println( "Analyzing " + d );
603 ReachGraph rg = analyzeMethod( d );
604 ReachGraph rgPrev = getPartial( d );
606 if( !rg.equals( rgPrev ) ) {
609 // results for d changed, so enqueue dependents
610 // of d for further analysis
611 Iterator<Descriptor> depsItr = getDependents( d ).iterator();
612 while( depsItr.hasNext() ) {
613 Descriptor dNext = depsItr.next();
620 protected ReachGraph analyzeMethod( Descriptor d )
621 throws java.io.IOException {
623 // get the flat code for this descriptor
625 if( d == mdAnalysisEntry ) {
626 fm = fmAnalysisEntry;
628 fm = state.getMethodFlat( d );
631 // intraprocedural work set
632 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
633 flatNodesToVisit.add( fm );
635 // mapping of current partial results
636 Hashtable<FlatNode, ReachGraph> mapFlatNodeToReachGraph =
637 new Hashtable<FlatNode, ReachGraph>();
639 // the set of return nodes partial results that will be combined as
640 // the final, conservative approximation of the entire method
641 HashSet<FlatReturnNode> setReturns = new HashSet<FlatReturnNode>();
643 while( !flatNodesToVisit.isEmpty() ) {
644 FlatNode fn = (FlatNode) flatNodesToVisit.iterator().next();
645 flatNodesToVisit.remove( fn );
647 //System.out.println( " "+fn );
649 // effect transfer function defined by this node,
650 // then compare it to the old graph at this node
651 // to see if anything was updated.
653 ReachGraph rg = new ReachGraph();
654 TaskDescriptor taskDesc;
655 if(fn instanceof FlatMethod && (taskDesc=((FlatMethod)fn).getTask())!=null){
656 if(mapDescriptorToReachGraph.containsKey(taskDesc)){
657 // retrieve existing reach graph if it is not first time
658 rg=mapDescriptorToReachGraph.get(taskDesc);
660 // create initial reach graph for a task
661 rg=createInitialTaskReachGraph((FlatMethod)fn);
663 mapDescriptorToReachGraph.put(taskDesc, rg);
667 // start by merging all node's parents' graphs
668 for( int i = 0; i < fn.numPrev(); ++i ) {
669 FlatNode pn = fn.getPrev( i );
670 if( mapFlatNodeToReachGraph.containsKey( pn ) ) {
671 ReachGraph rgParent = mapFlatNodeToReachGraph.get( pn );
672 // System.out.println("parent="+pn+"->"+rgParent);
673 rg.merge( rgParent );
677 // modify rg with appropriate transfer function
678 rg = analyzeFlatNode( d, fm, fn, setReturns, rg );
680 // if the results of the new graph are different from
681 // the current graph at this node, replace the graph
682 // with the update and enqueue the children
683 ReachGraph rgPrev = mapFlatNodeToReachGraph.get( fn );
684 if( !rg.equals( rgPrev ) ) {
685 mapFlatNodeToReachGraph.put( fn, rg );
687 for( int i = 0; i < fn.numNext(); i++ ) {
688 FlatNode nn = fn.getNext( i );
689 flatNodesToVisit.add( nn );
694 // end by merging all return nodes into a complete
695 // ownership graph that represents all possible heap
696 // states after the flat method returns
697 ReachGraph completeGraph = new ReachGraph();
699 assert !setReturns.isEmpty();
700 Iterator retItr = setReturns.iterator();
701 while( retItr.hasNext() ) {
702 FlatReturnNode frn = (FlatReturnNode) retItr.next();
704 assert mapFlatNodeToReachGraph.containsKey( frn );
705 ReachGraph rgRet = mapFlatNodeToReachGraph.get( frn );
707 completeGraph.merge( rgRet );
709 return completeGraph;
714 analyzeFlatNode( Descriptor d,
715 FlatMethod fmContaining,
717 HashSet<FlatReturnNode> setRetNodes,
719 ) throws java.io.IOException {
722 // any variables that are no longer live should be
723 // nullified in the graph to reduce edges
724 //rg.nullifyDeadVars( liveness.getLiveInTemps( fmContaining, fn ) );
731 // use node type to decide what transfer function
732 // to apply to the reachability graph
733 switch( fn.kind() ) {
735 case FKind.FlatMethod: {
736 // construct this method's initial heap model (IHM)
737 // since we're working on the FlatMethod, we know
738 // the incoming ReachGraph 'rg' is empty
740 Hashtable<FlatCall, ReachGraph> heapsFromCallers =
741 getIHMcontributions( d );
743 Set entrySet = heapsFromCallers.entrySet();
744 Iterator itr = entrySet.iterator();
745 while( itr.hasNext() ) {
746 Map.Entry me = (Map.Entry) itr.next();
747 FlatCall fc = (FlatCall) me.getKey();
748 ReachGraph rgContrib = (ReachGraph) me.getValue();
750 assert fc.getMethod().equals( d );
752 // some call sites are in same method context though,
753 // and all of them should be merged together first,
754 // then heaps from different contexts should be merged
755 // THIS ASSUMES DIFFERENT CONTEXTS NEED SPECIAL CONSIDERATION!
756 // such as, do allocation sites need to be aged?
758 rg.merge_diffMethodContext( rgContrib );
762 case FKind.FlatOpNode:
763 FlatOpNode fon = (FlatOpNode) fn;
764 if( fon.getOp().getOp() == Operation.ASSIGN ) {
767 rg.assignTempXEqualToTempY( lhs, rhs );
771 case FKind.FlatCastNode:
772 FlatCastNode fcn = (FlatCastNode) fn;
776 TypeDescriptor td = fcn.getType();
779 rg.assignTempXEqualToCastedTempY( lhs, rhs, td );
782 case FKind.FlatFieldNode:
783 FlatFieldNode ffn = (FlatFieldNode) fn;
786 fld = ffn.getField();
787 if( !fld.getType().isImmutable() || fld.getType().isArray() ) {
788 rg.assignTempXEqualToTempYFieldF( lhs, rhs, fld );
792 case FKind.FlatSetFieldNode:
793 FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
795 fld = fsfn.getField();
797 if( !fld.getType().isImmutable() || fld.getType().isArray() ) {
798 rg.assignTempXFieldFEqualToTempY( lhs, fld, rhs );
802 case FKind.FlatElementNode:
803 FlatElementNode fen = (FlatElementNode) fn;
806 if( !lhs.getType().isImmutable() || lhs.getType().isArray() ) {
808 assert rhs.getType() != null;
809 assert rhs.getType().isArray();
811 TypeDescriptor tdElement = rhs.getType().dereference();
812 FieldDescriptor fdElement = getArrayField( tdElement );
814 rg.assignTempXEqualToTempYFieldF( lhs, rhs, fdElement );
818 case FKind.FlatSetElementNode:
819 FlatSetElementNode fsen = (FlatSetElementNode) fn;
821 if( arrayReferencees.doesNotCreateNewReaching( fsen ) ) {
822 // skip this node if it cannot create new reachability paths
828 if( !rhs.getType().isImmutable() || rhs.getType().isArray() ) {
830 assert lhs.getType() != null;
831 assert lhs.getType().isArray();
833 TypeDescriptor tdElement = lhs.getType().dereference();
834 FieldDescriptor fdElement = getArrayField( tdElement );
836 rg.assignTempXFieldFEqualToTempY( lhs, fdElement, rhs );
841 FlatNew fnn = (FlatNew) fn;
843 if( !lhs.getType().isImmutable() || lhs.getType().isArray() ) {
844 AllocSite as = getAllocSiteFromFlatNewPRIVATE( fnn );
845 rg.assignTempEqualToNewAlloc( lhs, as );
849 case FKind.FlatCall: {
850 //TODO: temporal fix for task descriptor case
851 //MethodDescriptor mdCaller = fmContaining.getMethod();
853 if(fmContaining.getMethod()!=null){
854 mdCaller = fmContaining.getMethod();
856 mdCaller = fmContaining.getTask();
858 FlatCall fc = (FlatCall) fn;
859 MethodDescriptor mdCallee = fc.getMethod();
860 FlatMethod fmCallee = state.getMethodFlat( mdCallee );
862 boolean writeDebugDOTs =
863 mdCaller.getSymbol().equals( state.DISJOINTDEBUGCALLER ) &&
864 mdCallee.getSymbol().equals( state.DISJOINTDEBUGCALLEE );
867 // calculate the heap this call site can reach--note this is
868 // not used for the current call site transform, we are
869 // grabbing this heap model for future analysis of the callees,
870 // so if different results emerge we will return to this site
871 ReachGraph heapForThisCall_old =
872 getIHMcontribution( mdCallee, fc );
874 // the computation of the callee-reachable heap
875 // is useful for making the callee starting point
876 // and for applying the call site transfer function
877 Set<Integer> callerNodeIDsCopiedToCallee =
878 new HashSet<Integer>();
880 ReachGraph heapForThisCall_cur =
881 rg.makeCalleeView( fc,
883 callerNodeIDsCopiedToCallee,
887 if( !heapForThisCall_cur.equals( heapForThisCall_old ) ) {
888 // if heap at call site changed, update the contribution,
889 // and reschedule the callee for analysis
890 addIHMcontribution( mdCallee, fc, heapForThisCall_cur );
897 // the transformation for a call site should update the
898 // current heap abstraction with any effects from the callee,
899 // or if the method is virtual, the effects from any possible
900 // callees, so find the set of callees...
901 Set<MethodDescriptor> setPossibleCallees =
902 new HashSet<MethodDescriptor>();
904 if( mdCallee.isStatic() ) {
905 setPossibleCallees.add( mdCallee );
907 TypeDescriptor typeDesc = fc.getThis().getType();
908 setPossibleCallees.addAll( callGraph.getMethods( mdCallee,
913 ReachGraph rgMergeOfEffects = new ReachGraph();
915 Iterator<MethodDescriptor> mdItr = setPossibleCallees.iterator();
916 while( mdItr.hasNext() ) {
917 MethodDescriptor mdPossible = mdItr.next();
918 FlatMethod fmPossible = state.getMethodFlat( mdPossible );
920 addDependent( mdPossible, // callee
923 // don't alter the working graph (rg) until we compute a
924 // result for every possible callee, merge them all together,
925 // then set rg to that
926 ReachGraph rgCopy = new ReachGraph();
929 ReachGraph rgEffect = getPartial( mdPossible );
931 if( rgEffect == null ) {
932 // if this method has never been analyzed just schedule it
933 // for analysis and skip over this call site for now
934 enqueue( mdPossible );
936 rgCopy.resolveMethodCall( fc,
939 callerNodeIDsCopiedToCallee,
944 rgMergeOfEffects.merge( rgCopy );
948 // now that we've taken care of building heap models for
949 // callee analysis, finish this transformation
950 rg = rgMergeOfEffects;
954 case FKind.FlatReturnNode:
955 FlatReturnNode frn = (FlatReturnNode) fn;
956 rhs = frn.getReturnTemp();
957 if( rhs != null && !rhs.getType().isImmutable() ) {
958 rg.assignReturnEqualToTemp( rhs );
960 setRetNodes.add( frn );
966 // dead variables were removed before the above transfer function
967 // was applied, so eliminate heap regions and edges that are no
968 // longer part of the abstractly-live heap graph, and sweep up
969 // and reachability effects that are altered by the reduction
970 //rg.abstractGarbageCollect();
974 // at this point rg should be the correct update
975 // by an above transfer function, or untouched if
976 // the flat node type doesn't affect the heap
981 // this method should generate integers strictly greater than zero!
982 // special "shadow" regions are made from a heap region by negating
984 static public Integer generateUniqueHeapRegionNodeID() {
986 return new Integer( uniqueIDcount );
991 static public FieldDescriptor getArrayField( TypeDescriptor tdElement ) {
992 FieldDescriptor fdElement = mapTypeToArrayField.get( tdElement );
993 if( fdElement == null ) {
994 fdElement = new FieldDescriptor( new Modifiers( Modifiers.PUBLIC ),
996 arrayElementFieldName,
999 mapTypeToArrayField.put( tdElement, fdElement );
1006 private void writeFinalGraphs() {
1007 Set entrySet = mapDescriptorToCompleteReachGraph.entrySet();
1008 Iterator itr = entrySet.iterator();
1009 while( itr.hasNext() ) {
1010 Map.Entry me = (Map.Entry) itr.next();
1011 Descriptor d = (Descriptor) me.getKey();
1012 ReachGraph rg = (ReachGraph) me.getValue();
1015 rg.writeGraph( "COMPLETE"+d,
1016 true, // write labels (variables)
1017 true, // selectively hide intermediate temp vars
1018 true, // prune unreachable heap regions
1019 false, // hide subset reachability states
1020 true ); // hide edge taints
1021 } catch( IOException e ) {}
1025 private void writeFinalIHMs() {
1026 Iterator d2IHMsItr = mapDescriptorToIHMcontributions.entrySet().iterator();
1027 while( d2IHMsItr.hasNext() ) {
1028 Map.Entry me1 = (Map.Entry) d2IHMsItr.next();
1029 Descriptor d = (Descriptor) me1.getKey();
1030 Hashtable<FlatCall, ReachGraph> IHMs = (Hashtable<FlatCall, ReachGraph>) me1.getValue();
1032 Iterator fc2rgItr = IHMs.entrySet().iterator();
1033 while( fc2rgItr.hasNext() ) {
1034 Map.Entry me2 = (Map.Entry) fc2rgItr.next();
1035 FlatCall fc = (FlatCall) me2.getKey();
1036 ReachGraph rg = (ReachGraph) me2.getValue();
1039 rg.writeGraph( "IHMPARTFOR"+d+"FROM"+fc,
1040 true, // write labels (variables)
1041 false, // selectively hide intermediate temp vars
1042 false, // prune unreachable heap regions
1043 false, // hide subset reachability states
1044 true ); // hide edge taints
1045 } catch( IOException e ) {}
1053 // return just the allocation site associated with one FlatNew node
1054 protected AllocSite getAllocSiteFromFlatNewPRIVATE( FlatNew fnew ) {
1056 if( !mapFlatNewToAllocSite.containsKey( fnew ) ) {
1058 (AllocSite) Canonical.makeCanonical( new AllocSite( allocationDepth,
1060 fnew.getDisjointId()
1064 // the newest nodes are single objects
1065 for( int i = 0; i < allocationDepth; ++i ) {
1066 Integer id = generateUniqueHeapRegionNodeID();
1067 as.setIthOldest( i, id );
1068 mapHrnIdToAllocSite.put( id, as );
1071 // the oldest node is a summary node
1072 as.setSummary( generateUniqueHeapRegionNodeID() );
1074 mapFlatNewToAllocSite.put( fnew, as );
1077 return mapFlatNewToAllocSite.get( fnew );
1082 // return all allocation sites in the method (there is one allocation
1083 // site per FlatNew node in a method)
1084 protected HashSet<AllocSite> getAllocSiteSet(Descriptor d) {
1085 if( !mapDescriptorToAllocSiteSet.containsKey(d) ) {
1086 buildAllocSiteSet(d);
1089 return mapDescriptorToAllocSiteSet.get(d);
1095 protected void buildAllocSiteSet(Descriptor d) {
1096 HashSet<AllocSite> s = new HashSet<AllocSite>();
1098 FlatMethod fm = state.getMethodFlat( d );
1100 // visit every node in this FlatMethod's IR graph
1101 // and make a set of the allocation sites from the
1102 // FlatNew node's visited
1103 HashSet<FlatNode> visited = new HashSet<FlatNode>();
1104 HashSet<FlatNode> toVisit = new HashSet<FlatNode>();
1107 while( !toVisit.isEmpty() ) {
1108 FlatNode n = toVisit.iterator().next();
1110 if( n instanceof FlatNew ) {
1111 s.add(getAllocSiteFromFlatNewPRIVATE( (FlatNew) n) );
1114 toVisit.remove( n );
1117 for( int i = 0; i < n.numNext(); ++i ) {
1118 FlatNode child = n.getNext( i );
1119 if( !visited.contains( child ) ) {
1120 toVisit.add( child );
1125 mapDescriptorToAllocSiteSet.put( d, s );
1129 protected HashSet<AllocSite> getFlaggedAllocSites(Descriptor dIn) {
1131 HashSet<AllocSite> out = new HashSet<AllocSite>();
1132 HashSet<Descriptor> toVisit = new HashSet<Descriptor>();
1133 HashSet<Descriptor> visited = new HashSet<Descriptor>();
1137 while( !toVisit.isEmpty() ) {
1138 Descriptor d = toVisit.iterator().next();
1142 HashSet<AllocSite> asSet = getAllocSiteSet(d);
1143 Iterator asItr = asSet.iterator();
1144 while( asItr.hasNext() ) {
1145 AllocSite as = (AllocSite) asItr.next();
1146 if( as.getDisjointAnalysisId() != null ) {
1151 // enqueue callees of this method to be searched for
1152 // allocation sites also
1153 Set callees = callGraph.getCalleeSet(d);
1154 if( callees != null ) {
1155 Iterator methItr = callees.iterator();
1156 while( methItr.hasNext() ) {
1157 MethodDescriptor md = (MethodDescriptor) methItr.next();
1159 if( !visited.contains(md) ) {
1171 protected HashSet<AllocSite>
1172 getFlaggedAllocSitesReachableFromTaskPRIVATE(TaskDescriptor td) {
1174 HashSet<AllocSite> asSetTotal = new HashSet<AllocSite>();
1175 HashSet<Descriptor> toVisit = new HashSet<Descriptor>();
1176 HashSet<Descriptor> visited = new HashSet<Descriptor>();
1180 // traverse this task and all methods reachable from this task
1181 while( !toVisit.isEmpty() ) {
1182 Descriptor d = toVisit.iterator().next();
1186 HashSet<AllocSite> asSet = getAllocSiteSet(d);
1187 Iterator asItr = asSet.iterator();
1188 while( asItr.hasNext() ) {
1189 AllocSite as = (AllocSite) asItr.next();
1190 TypeDescriptor typed = as.getType();
1191 if( typed != null ) {
1192 ClassDescriptor cd = typed.getClassDesc();
1193 if( cd != null && cd.hasFlags() ) {
1199 // enqueue callees of this method to be searched for
1200 // allocation sites also
1201 Set callees = callGraph.getCalleeSet(d);
1202 if( callees != null ) {
1203 Iterator methItr = callees.iterator();
1204 while( methItr.hasNext() ) {
1205 MethodDescriptor md = (MethodDescriptor) methItr.next();
1207 if( !visited.contains(md) ) {
1221 protected String computeAliasContextHistogram() {
1223 Hashtable<Integer, Integer> mapNumContexts2NumDesc =
1224 new Hashtable<Integer, Integer>();
1226 Iterator itr = mapDescriptorToAllDescriptors.entrySet().iterator();
1227 while( itr.hasNext() ) {
1228 Map.Entry me = (Map.Entry) itr.next();
1229 HashSet<Descriptor> s = (HashSet<Descriptor>) me.getValue();
1231 Integer i = mapNumContexts2NumDesc.get( s.size() );
1233 i = new Integer( 0 );
1235 mapNumContexts2NumDesc.put( s.size(), i + 1 );
1241 itr = mapNumContexts2NumDesc.entrySet().iterator();
1242 while( itr.hasNext() ) {
1243 Map.Entry me = (Map.Entry) itr.next();
1244 Integer c0 = (Integer) me.getKey();
1245 Integer d0 = (Integer) me.getValue();
1247 s += String.format( "%4d methods had %4d unique alias contexts.\n", d0, c0 );
1250 s += String.format( "\n%4d total methods analayzed.\n", total );
1255 protected int numMethodsAnalyzed() {
1256 return descriptorsToAnalyze.size();
1263 // Take in source entry which is the program's compiled entry and
1264 // create a new analysis entry, a method that takes no parameters
1265 // and appears to allocate the command line arguments and call the
1266 // source entry with them. The purpose of this analysis entry is
1267 // to provide a top-level method context with no parameters left.
1268 protected void makeAnalysisEntryMethod( MethodDescriptor mdSourceEntry ) {
1270 Modifiers mods = new Modifiers();
1271 mods.addModifier( Modifiers.PUBLIC );
1272 mods.addModifier( Modifiers.STATIC );
1274 TypeDescriptor returnType =
1275 new TypeDescriptor( TypeDescriptor.VOID );
1277 this.mdAnalysisEntry =
1278 new MethodDescriptor( mods,
1280 "analysisEntryMethod"
1283 TempDescriptor cmdLineArgs =
1284 new TempDescriptor( "args",
1285 mdSourceEntry.getParamType( 0 )
1289 new FlatNew( mdSourceEntry.getParamType( 0 ),
1294 TempDescriptor[] sourceEntryArgs = new TempDescriptor[1];
1295 sourceEntryArgs[0] = cmdLineArgs;
1298 new FlatCall( mdSourceEntry,
1304 FlatReturnNode frn = new FlatReturnNode( null );
1306 FlatExit fe = new FlatExit();
1308 this.fmAnalysisEntry =
1309 new FlatMethod( mdAnalysisEntry,
1313 this.fmAnalysisEntry.addNext( fn );
1320 protected LinkedList<Descriptor> topologicalSort( Set<Descriptor> toSort ) {
1322 Set <Descriptor> discovered = new HashSet <Descriptor>();
1323 LinkedList<Descriptor> sorted = new LinkedList<Descriptor>();
1325 Iterator<Descriptor> itr = toSort.iterator();
1326 while( itr.hasNext() ) {
1327 Descriptor d = itr.next();
1329 if( !discovered.contains( d ) ) {
1330 dfsVisit( d, toSort, sorted, discovered );
1337 // While we're doing DFS on call graph, remember
1338 // dependencies for efficient queuing of methods
1339 // during interprocedural analysis:
1341 // a dependent of a method decriptor d for this analysis is:
1342 // 1) a method or task that invokes d
1343 // 2) in the descriptorsToAnalyze set
1344 protected void dfsVisit( Descriptor d,
1345 Set <Descriptor> toSort,
1346 LinkedList<Descriptor> sorted,
1347 Set <Descriptor> discovered ) {
1348 discovered.add( d );
1350 // only methods have callers, tasks never do
1351 if( d instanceof MethodDescriptor ) {
1353 MethodDescriptor md = (MethodDescriptor) d;
1355 // the call graph is not aware that we have a fabricated
1356 // analysis entry that calls the program source's entry
1357 if( md == mdSourceEntry ) {
1358 if( !discovered.contains( mdAnalysisEntry ) ) {
1359 addDependent( mdSourceEntry, // callee
1360 mdAnalysisEntry // caller
1362 dfsVisit( mdAnalysisEntry, toSort, sorted, discovered );
1366 // otherwise call graph guides DFS
1367 Iterator itr = callGraph.getCallerSet( md ).iterator();
1368 while( itr.hasNext() ) {
1369 Descriptor dCaller = (Descriptor) itr.next();
1371 // only consider callers in the original set to analyze
1372 if( !toSort.contains( dCaller ) ) {
1376 if( !discovered.contains( dCaller ) ) {
1377 addDependent( md, // callee
1381 dfsVisit( dCaller, toSort, sorted, discovered );
1386 sorted.addFirst( d );
1390 protected void enqueue( Descriptor d ) {
1391 if( !descriptorsToVisitSet.contains( d ) ) {
1392 Integer priority = mapDescriptorToPriority.get( d );
1393 descriptorsToVisitQ.add( new DescriptorQWrapper( priority,
1396 descriptorsToVisitSet.add( d );
1401 protected ReachGraph getPartial( Descriptor d ) {
1402 return mapDescriptorToCompleteReachGraph.get( d );
1405 protected void setPartial( Descriptor d, ReachGraph rg ) {
1406 mapDescriptorToCompleteReachGraph.put( d, rg );
1408 // when the flag for writing out every partial
1409 // result is set, we should spit out the graph,
1410 // but in order to give it a unique name we need
1411 // to track how many partial results for this
1412 // descriptor we've already written out
1413 if( writeAllIncrementalDOTs ) {
1414 if( !mapDescriptorToNumUpdates.containsKey( d ) ) {
1415 mapDescriptorToNumUpdates.put( d, new Integer( 0 ) );
1417 Integer n = mapDescriptorToNumUpdates.get( d );
1420 rg.writeGraph( d+"COMPLETE"+String.format( "%05d", n ),
1421 true, // write labels (variables)
1422 true, // selectively hide intermediate temp vars
1423 true, // prune unreachable heap regions
1424 false, // show back edges to confirm graph validity
1425 false, // show parameter indices (unmaintained!)
1426 true, // hide subset reachability states
1427 true); // hide edge taints
1428 } catch( IOException e ) {}
1430 mapDescriptorToNumUpdates.put( d, n + 1 );
1435 // a dependent of a method decriptor d for this analysis is:
1436 // 1) a method or task that invokes d
1437 // 2) in the descriptorsToAnalyze set
1438 protected void addDependent( Descriptor callee, Descriptor caller ) {
1439 Set<Descriptor> deps = mapDescriptorToSetDependents.get( callee );
1440 if( deps == null ) {
1441 deps = new HashSet<Descriptor>();
1444 mapDescriptorToSetDependents.put( callee, deps );
1447 protected Set<Descriptor> getDependents( Descriptor callee ) {
1448 Set<Descriptor> deps = mapDescriptorToSetDependents.get( callee );
1449 if( deps == null ) {
1450 deps = new HashSet<Descriptor>();
1451 mapDescriptorToSetDependents.put( callee, deps );
1457 public Hashtable<FlatCall, ReachGraph> getIHMcontributions( Descriptor d ) {
1459 Hashtable<FlatCall, ReachGraph> heapsFromCallers =
1460 mapDescriptorToIHMcontributions.get( d );
1462 if( heapsFromCallers == null ) {
1463 heapsFromCallers = new Hashtable<FlatCall, ReachGraph>();
1464 mapDescriptorToIHMcontributions.put( d, heapsFromCallers );
1467 return heapsFromCallers;
1470 public ReachGraph getIHMcontribution( Descriptor d,
1473 Hashtable<FlatCall, ReachGraph> heapsFromCallers =
1474 getIHMcontributions( d );
1476 if( !heapsFromCallers.containsKey( fc ) ) {
1477 heapsFromCallers.put( fc, new ReachGraph() );
1480 return heapsFromCallers.get( fc );
1483 public void addIHMcontribution( Descriptor d,
1487 Hashtable<FlatCall, ReachGraph> heapsFromCallers =
1488 getIHMcontributions( d );
1490 heapsFromCallers.put( fc, rg );
1493 private AllocSite createParameterAllocSite(ReachGraph rg, TempDescriptor tempDesc) {
1495 // create temp descriptor for each parameter variable
1496 FlatNew flatNew = new FlatNew(tempDesc.getType(), tempDesc, false);
1497 // create allocation site
1498 AllocSite as = (AllocSite) Canonical.makeCanonical(new AllocSite( allocationDepth, flatNew, flatNew.getDisjointId()));
1499 for (int i = 0; i < allocationDepth; ++i) {
1500 Integer id = generateUniqueHeapRegionNodeID();
1501 as.setIthOldest(i, id);
1502 mapHrnIdToAllocSite.put(id, as);
1504 // the oldest node is a summary node
1505 as.setSummary( generateUniqueHeapRegionNodeID() );
1513 private ReachGraph createInitialTaskReachGraph(FlatMethod fm) {
1514 ReachGraph rg = new ReachGraph();
1515 TaskDescriptor taskDesc = fm.getTask();
1517 for (int idx = 0; idx < taskDesc.numParameters(); idx++) {
1518 Descriptor paramDesc = taskDesc.getParameter(idx);
1519 TypeDescriptor paramTypeDesc = taskDesc.getParamType(idx);
1521 // setup data structure
1522 Set<HashMap<HeapRegionNode, FieldDescriptor>> workSet =
1523 new HashSet<HashMap<HeapRegionNode, FieldDescriptor>>();
1524 Hashtable<TypeDescriptor, HeapRegionNode> mapTypeToExistingSummaryNode =
1525 new Hashtable<TypeDescriptor, HeapRegionNode>();
1526 Set<String> doneSet = new HashSet<String>();
1528 TempDescriptor tempDesc = fm.getParameter(idx);
1530 AllocSite as = createParameterAllocSite(rg, tempDesc);
1531 VariableNode lnX = rg.getVariableNodeFromTemp(tempDesc);
1532 Integer idNewest = as.getIthOldest(0);
1533 HeapRegionNode hrnNewest = rg.id2hrn.get(idNewest);
1534 // make a new reference to allocated node
1535 RefEdge edgeNew = new RefEdge(lnX, // source
1537 taskDesc.getParamType(idx), // type
1539 hrnNewest.getAlpha(), // beta
1540 ExistPredSet.factory(rg.predTrue) // predicates
1542 rg.addRefEdge(lnX, hrnNewest, edgeNew);
1544 // set-up a work set for class field
1545 ClassDescriptor classDesc = paramTypeDesc.getClassDesc();
1546 for (Iterator it = classDesc.getFields(); it.hasNext();) {
1547 FieldDescriptor fd = (FieldDescriptor) it.next();
1548 TypeDescriptor fieldType = fd.getType();
1549 if (!fieldType.isImmutable() || fieldType.isArray()) {
1550 HashMap<HeapRegionNode, FieldDescriptor> newMap = new HashMap<HeapRegionNode, FieldDescriptor>();
1551 newMap.put(hrnNewest, fd);
1552 workSet.add(newMap);
1556 int uniqueIdentifier = 0;
1557 while (!workSet.isEmpty()) {
1558 HashMap<HeapRegionNode, FieldDescriptor> map = workSet
1560 workSet.remove(map);
1562 Set<HeapRegionNode> key = map.keySet();
1563 HeapRegionNode srcHRN = key.iterator().next();
1564 FieldDescriptor fd = map.get(srcHRN);
1565 TypeDescriptor type = fd.getType();
1566 String doneSetIdentifier = srcHRN.getIDString() + "_" + fd;
1568 if (!doneSet.contains(doneSetIdentifier)) {
1569 doneSet.add(doneSetIdentifier);
1570 if (!mapTypeToExistingSummaryNode.containsKey(type)) {
1571 // create new summary Node
1572 TempDescriptor td = new TempDescriptor("temp"
1573 + uniqueIdentifier, type);
1575 AllocSite allocSite;
1576 if(type.equals(paramTypeDesc)){
1577 //corresponding allocsite has already been created for a parameter variable.
1580 allocSite = createParameterAllocSite(rg, td);
1582 String strDesc = allocSite.toStringForDOT()
1584 HeapRegionNode hrnSummary =
1585 rg.createNewHeapRegionNode(allocSite.getSummary(), // id or null to generate a new one
1586 false, // single object?
1589 false, // out-of-context?
1590 allocSite.getType(), // type
1591 allocSite, // allocation site
1592 null, // inherent reach
1593 srcHRN.getAlpha(), // current reach
1594 ExistPredSet.factory(), // predicates
1595 strDesc // description
1597 rg.id2hrn.put(allocSite.getSummary(),hrnSummary);
1599 // make a new reference to summary node
1600 RefEdge edgeToSummary = new RefEdge(srcHRN, // source
1602 fd.getType(), // type
1603 fd.getSymbol(), // field name
1604 srcHRN.getAlpha(), // beta
1605 ExistPredSet.factory(rg.predTrue) // predicates
1608 rg.addRefEdge(srcHRN, hrnSummary, edgeToSummary);
1612 mapTypeToExistingSummaryNode.put(type, hrnSummary);
1614 // set-up a work set for fields of the class
1615 if(!type.isImmutable()){
1616 classDesc = type.getClassDesc();
1617 for (Iterator it = classDesc.getFields(); it.hasNext();) {
1618 FieldDescriptor typeFieldDesc = (FieldDescriptor) it.next();
1619 TypeDescriptor fieldType = typeFieldDesc.getType();
1620 if (!fieldType.isImmutable()) {
1621 doneSetIdentifier = hrnSummary.getIDString() + "_" + typeFieldDesc;
1622 if(!doneSet.contains(doneSetIdentifier)){
1623 // add new work item
1624 HashMap<HeapRegionNode, FieldDescriptor> newMap =
1625 new HashMap<HeapRegionNode, FieldDescriptor>();
1626 newMap.put(hrnSummary, typeFieldDesc);
1627 workSet.add(newMap);
1634 // if there exists corresponding summary node
1635 HeapRegionNode hrnDst=mapTypeToExistingSummaryNode.get(type);
1637 RefEdge edgeToSummary = new RefEdge(srcHRN, // source
1639 fd.getType(), // type
1640 fd.getSymbol(), // field name
1641 srcHRN.getAlpha(), // beta
1642 ExistPredSet.factory(rg.predTrue) // predicates
1644 rg.addRefEdge(srcHRN, hrnDst, edgeToSummary);
1650 // debugSnapshot(rg, fm, true);
1654 // return all allocation sites in the method (there is one allocation
1655 // site per FlatNew node in a method)
1656 private HashSet<AllocSite> getAllocationSiteSet(Descriptor d) {
1657 if( !mapDescriptorToAllocSiteSet.containsKey(d) ) {
1658 buildAllocationSiteSet(d);
1661 return mapDescriptorToAllocSiteSet.get(d);
1665 private void buildAllocationSiteSet(Descriptor d) {
1666 HashSet<AllocSite> s = new HashSet<AllocSite>();
1669 if( d instanceof MethodDescriptor ) {
1670 fm = state.getMethodFlat( (MethodDescriptor) d);
1672 assert d instanceof TaskDescriptor;
1673 fm = state.getMethodFlat( (TaskDescriptor) d);
1676 // visit every node in this FlatMethod's IR graph
1677 // and make a set of the allocation sites from the
1678 // FlatNew node's visited
1679 HashSet<FlatNode> visited = new HashSet<FlatNode>();
1680 HashSet<FlatNode> toVisit = new HashSet<FlatNode>();
1683 while( !toVisit.isEmpty() ) {
1684 FlatNode n = toVisit.iterator().next();
1686 if( n instanceof FlatNew ) {
1687 s.add(getAllocSiteFromFlatNewPRIVATE( (FlatNew) n) );
1693 for( int i = 0; i < n.numNext(); ++i ) {
1694 FlatNode child = n.getNext(i);
1695 if( !visited.contains(child) ) {
1701 mapDescriptorToAllocSiteSet.put(d, s);
1704 private HashSet<AllocSite> getFlaggedAllocationSites(Descriptor dIn) {
1706 HashSet<AllocSite> out = new HashSet<AllocSite>();
1707 HashSet<Descriptor> toVisit = new HashSet<Descriptor>();
1708 HashSet<Descriptor> visited = new HashSet<Descriptor>();
1712 while (!toVisit.isEmpty()) {
1713 Descriptor d = toVisit.iterator().next();
1717 HashSet<AllocSite> asSet = getAllocationSiteSet(d);
1718 Iterator asItr = asSet.iterator();
1719 while (asItr.hasNext()) {
1720 AllocSite as = (AllocSite) asItr.next();
1721 if (as.getDisjointAnalysisId() != null) {
1726 // enqueue callees of this method to be searched for
1727 // allocation sites also
1728 Set callees = callGraph.getCalleeSet(d);
1729 if (callees != null) {
1730 Iterator methItr = callees.iterator();
1731 while (methItr.hasNext()) {
1732 MethodDescriptor md = (MethodDescriptor) methItr.next();
1734 if (!visited.contains(md)) {
1745 private HashSet<AllocSite>
1746 getFlaggedAllocationSitesReachableFromTaskPRIVATE(TaskDescriptor td) {
1748 HashSet<AllocSite> asSetTotal = new HashSet<AllocSite>();
1749 HashSet<Descriptor> toVisit = new HashSet<Descriptor>();
1750 HashSet<Descriptor> visited = new HashSet<Descriptor>();
1754 // traverse this task and all methods reachable from this task
1755 while( !toVisit.isEmpty() ) {
1756 Descriptor d = toVisit.iterator().next();
1760 HashSet<AllocSite> asSet = getAllocationSiteSet(d);
1761 Iterator asItr = asSet.iterator();
1762 while( asItr.hasNext() ) {
1763 AllocSite as = (AllocSite) asItr.next();
1764 TypeDescriptor typed = as.getType();
1765 if( typed != null ) {
1766 ClassDescriptor cd = typed.getClassDesc();
1767 if( cd != null && cd.hasFlags() ) {
1773 // enqueue callees of this method to be searched for
1774 // allocation sites also
1775 Set callees = callGraph.getCalleeSet(d);
1776 if( callees != null ) {
1777 Iterator methItr = callees.iterator();
1778 while( methItr.hasNext() ) {
1779 MethodDescriptor md = (MethodDescriptor) methItr.next();
1781 if( !visited.contains(md) ) {
1797 // get successive captures of the analysis state
1798 boolean takeDebugSnapshots = false;
1799 String descSymbolDebug = "main";
1800 boolean stopAfterCapture = true;
1802 // increments every visit to debugSnapshot, don't fiddle with it
1803 int debugCounter = 0;
1805 // the value of debugCounter to start reporting the debugCounter
1806 // to the screen to let user know what debug iteration we're at
1807 int numStartCountReport = 0;
1809 // the frequency of debugCounter values to print out, 0 no report
1810 int freqCountReport = 0;
1812 // the debugCounter value at which to start taking snapshots
1813 int iterStartCapture = 0;
1815 // the number of snapshots to take
1816 int numIterToCapture = 300;
1818 void debugSnapshot( ReachGraph rg, FlatNode fn, boolean in ) {
1819 if( debugCounter > iterStartCapture + numIterToCapture ) {
1827 if( debugCounter > numStartCountReport &&
1828 freqCountReport > 0 &&
1829 debugCounter % freqCountReport == 0
1831 System.out.println( " @@@ debug counter = "+
1835 if( debugCounter > iterStartCapture ) {
1836 System.out.println( " @@@ capturing debug "+
1837 (debugCounter - iterStartCapture)+
1841 graphName = String.format( "snap%04din",
1842 debugCounter - iterStartCapture );
1844 graphName = String.format( "snap%04dout",
1845 debugCounter - iterStartCapture );
1848 graphName = graphName + fn;
1851 rg.writeGraph( graphName,
1852 true, // write labels (variables)
1853 true, // selectively hide intermediate temp vars
1854 true, // prune unreachable heap regions
1855 false, // hide subset reachability states
1856 true );// hide edge taints
1857 } catch( Exception e ) {
1858 System.out.println( "Error writing debug capture." );
1863 if( debugCounter == iterStartCapture + numIterToCapture &&
1866 System.out.println( "Stopping analysis after debug captures." );