1 package Analysis.Disjoint;
5 import Util.UtilAlgorithms;
9 public class ReachGraph {
11 // use to disable improvements for comparison
12 protected static final boolean DISABLE_STRONG_UPDATES = false;
13 protected static final boolean DISABLE_GLOBAL_SWEEP = false;
15 // a special out-of-scope temp
16 protected static final TempDescriptor tdReturn = new TempDescriptor( "_Return___" );
18 // some frequently used reachability constants
19 protected static final ReachState rstateEmpty = ReachState.factory();
20 protected static final ReachSet rsetEmpty = ReachSet.factory();
21 protected static final ReachSet rsetWithEmptyState = ReachSet.factory( rstateEmpty );
23 // predicate constants
24 protected static final ExistPred predTrue = ExistPred.factory(); // if no args, true
25 protected static final ExistPredSet predsEmpty = ExistPredSet.factory();
26 protected static final ExistPredSet predsTrue = ExistPredSet.factory( predTrue );
29 // from DisjointAnalysis for convenience
30 protected static int allocationDepth = -1;
31 protected static TypeUtil typeUtil = null;
34 // variable and heap region nodes indexed by unique ID
35 public Hashtable<Integer, HeapRegionNode> id2hrn;
36 public Hashtable<TempDescriptor, VariableNode > td2vn;
38 // convenient set of alloc sites for all heap regions
39 // present in the graph without having to search
40 public HashSet<AllocSite> allocSites;
43 id2hrn = new Hashtable<Integer, HeapRegionNode>();
44 td2vn = new Hashtable<TempDescriptor, VariableNode >();
45 allocSites = new HashSet<AllocSite>();
49 // temp descriptors are globally unique and map to
50 // exactly one variable node, easy
51 protected VariableNode getVariableNodeFromTemp( TempDescriptor td ) {
54 if( !td2vn.containsKey( td ) ) {
55 td2vn.put( td, new VariableNode( td ) );
58 return td2vn.get( td );
61 public boolean hasVariable( TempDescriptor td ) {
62 return td2vn.containsKey( td );
66 // this suite of methods can be used to assert a
67 // very important property of ReachGraph objects:
68 // some element, HeapRegionNode, RefEdge etc.
69 // should be referenced by at most ONE ReachGraph!!
70 // If a heap region or edge or variable should be
71 // in another graph, make a new object with
72 // equivalent properties for a new graph
73 public boolean belongsToThis( RefSrcNode rsn ) {
74 if( rsn instanceof VariableNode ) {
75 VariableNode vn = (VariableNode) rsn;
76 return this.td2vn.get( vn.getTempDescriptor() ) == vn;
78 HeapRegionNode hrn = (HeapRegionNode) rsn;
79 return this.id2hrn.get( hrn.getID() ) == hrn;
84 // the reason for this method is to have the option
85 // of creating new heap regions with specific IDs, or
86 // duplicating heap regions with specific IDs (especially
87 // in the merge() operation) or to create new heap
88 // regions with a new unique ID
89 protected HeapRegionNode
90 createNewHeapRegionNode( Integer id,
91 boolean isSingleObject,
94 boolean isOutOfContext,
103 boolean markForAnalysis = isFlagged;
105 TypeDescriptor typeToUse = null;
106 if( allocSite != null ) {
107 typeToUse = allocSite.getType();
108 allocSites.add( allocSite );
113 if( allocSite != null && allocSite.getDisjointAnalysisId() != null ) {
114 markForAnalysis = true;
118 id = DisjointAnalysis.generateUniqueHeapRegionNodeID();
121 if( inherent == null ) {
122 if( markForAnalysis ) {
126 ReachTuple.factory( id,
128 ReachTuple.ARITY_ONE,
129 false // out-of-context
134 inherent = rsetWithEmptyState;
138 if( alpha == null ) {
142 if( preds == null ) {
143 // TODO: do this right? For out-of-context nodes?
144 preds = ExistPredSet.factory();
147 HeapRegionNode hrn = new HeapRegionNode( id,
158 id2hrn.put( id, hrn );
164 ////////////////////////////////////////////////
166 // Low-level referencee and referencer methods
168 // These methods provide the lowest level for
169 // creating references between reachability nodes
170 // and handling the details of maintaining both
171 // list of referencers and referencees.
173 ////////////////////////////////////////////////
174 protected void addRefEdge( RefSrcNode referencer,
175 HeapRegionNode referencee,
177 assert referencer != null;
178 assert referencee != null;
180 assert edge.getSrc() == referencer;
181 assert edge.getDst() == referencee;
182 assert belongsToThis( referencer );
183 assert belongsToThis( referencee );
185 // edges are getting added twice to graphs now, the
186 // kind that should have abstract facts merged--use
187 // this check to prevent that
188 assert referencer.getReferenceTo( referencee,
193 referencer.addReferencee( edge );
194 referencee.addReferencer( edge );
197 protected void removeRefEdge( RefEdge e ) {
198 removeRefEdge( e.getSrc(),
204 protected void removeRefEdge( RefSrcNode referencer,
205 HeapRegionNode referencee,
208 assert referencer != null;
209 assert referencee != null;
211 RefEdge edge = referencer.getReferenceTo( referencee,
215 assert edge == referencee.getReferenceFrom( referencer,
219 referencer.removeReferencee( edge );
220 referencee.removeReferencer( edge );
223 protected void clearRefEdgesFrom( RefSrcNode referencer,
226 boolean removeAll ) {
227 assert referencer != null;
229 // get a copy of the set to iterate over, otherwise
230 // we will be trying to take apart the set as we
231 // are iterating over it, which won't work
232 Iterator<RefEdge> i = referencer.iteratorToReferenceesClone();
233 while( i.hasNext() ) {
234 RefEdge edge = i.next();
237 (edge.typeEquals( type ) && edge.fieldEquals( field ))
240 HeapRegionNode referencee = edge.getDst();
242 removeRefEdge( referencer,
250 protected void clearRefEdgesTo( HeapRegionNode referencee,
253 boolean removeAll ) {
254 assert referencee != null;
256 // get a copy of the set to iterate over, otherwise
257 // we will be trying to take apart the set as we
258 // are iterating over it, which won't work
259 Iterator<RefEdge> i = referencee.iteratorToReferencersClone();
260 while( i.hasNext() ) {
261 RefEdge edge = i.next();
264 (edge.typeEquals( type ) && edge.fieldEquals( field ))
267 RefSrcNode referencer = edge.getSrc();
269 removeRefEdge( referencer,
277 protected void clearNonVarRefEdgesTo( HeapRegionNode referencee ) {
278 assert referencee != null;
280 // get a copy of the set to iterate over, otherwise
281 // we will be trying to take apart the set as we
282 // are iterating over it, which won't work
283 Iterator<RefEdge> i = referencee.iteratorToReferencersClone();
284 while( i.hasNext() ) {
285 RefEdge edge = i.next();
286 RefSrcNode referencer = edge.getSrc();
287 if( !(referencer instanceof VariableNode) ) {
288 removeRefEdge( referencer,
297 ////////////////////////////////////////////////////
299 // Assignment Operation Methods
301 // These methods are high-level operations for
302 // modeling program assignment statements using
303 // the low-level reference create/remove methods
306 ////////////////////////////////////////////////////
308 public void assignTempXEqualToTempY( TempDescriptor x,
310 assignTempXEqualToCastedTempY( x, y, null );
313 public void assignTempXEqualToCastedTempY( TempDescriptor x,
315 TypeDescriptor tdCast ) {
317 VariableNode lnX = getVariableNodeFromTemp( x );
318 VariableNode lnY = getVariableNodeFromTemp( y );
320 clearRefEdgesFrom( lnX, null, null, true );
322 // note it is possible that the types of temps in the
323 // flat node to analyze will reveal that some typed
324 // edges in the reachability graph are impossible
325 Set<RefEdge> impossibleEdges = new HashSet<RefEdge>();
327 Iterator<RefEdge> itrYhrn = lnY.iteratorToReferencees();
328 while( itrYhrn.hasNext() ) {
329 RefEdge edgeY = itrYhrn.next();
330 HeapRegionNode referencee = edgeY.getDst();
331 RefEdge edgeNew = edgeY.copy();
333 if( !isSuperiorType( x.getType(), edgeY.getType() ) ) {
334 impossibleEdges.add( edgeY );
338 edgeNew.setSrc( lnX );
340 if( tdCast == null ) {
341 edgeNew.setType( mostSpecificType( y.getType(),
347 edgeNew.setType( mostSpecificType( y.getType(),
349 referencee.getType(),
355 edgeNew.setField( null );
357 addRefEdge( lnX, referencee, edgeNew );
360 Iterator<RefEdge> itrImp = impossibleEdges.iterator();
361 while( itrImp.hasNext() ) {
362 RefEdge edgeImp = itrImp.next();
363 removeRefEdge( edgeImp );
368 public void assignTempXEqualToTempYFieldF( TempDescriptor x,
370 FieldDescriptor f ) {
371 VariableNode lnX = getVariableNodeFromTemp( x );
372 VariableNode lnY = getVariableNodeFromTemp( y );
374 clearRefEdgesFrom( lnX, null, null, true );
376 // note it is possible that the types of temps in the
377 // flat node to analyze will reveal that some typed
378 // edges in the reachability graph are impossible
379 Set<RefEdge> impossibleEdges = new HashSet<RefEdge>();
381 Iterator<RefEdge> itrYhrn = lnY.iteratorToReferencees();
382 while( itrYhrn.hasNext() ) {
383 RefEdge edgeY = itrYhrn.next();
384 HeapRegionNode hrnY = edgeY.getDst();
385 ReachSet betaY = edgeY.getBeta();
387 Iterator<RefEdge> itrHrnFhrn = hrnY.iteratorToReferencees();
388 while( itrHrnFhrn.hasNext() ) {
389 RefEdge edgeHrn = itrHrnFhrn.next();
390 HeapRegionNode hrnHrn = edgeHrn.getDst();
391 ReachSet betaHrn = edgeHrn.getBeta();
393 // prune edges that are not a matching field
394 if( edgeHrn.getType() != null &&
395 !edgeHrn.getField().equals( f.getSymbol() )
400 // check for impossible edges
401 if( !isSuperiorType( x.getType(), edgeHrn.getType() ) ) {
402 impossibleEdges.add( edgeHrn );
406 TypeDescriptor tdNewEdge =
407 mostSpecificType( edgeHrn.getType(),
411 RefEdge edgeNew = new RefEdge( lnX,
415 Canonical.intersection( betaY, betaHrn ),
419 addRefEdge( lnX, hrnHrn, edgeNew );
423 Iterator<RefEdge> itrImp = impossibleEdges.iterator();
424 while( itrImp.hasNext() ) {
425 RefEdge edgeImp = itrImp.next();
426 removeRefEdge( edgeImp );
429 // anytime you might remove edges between heap regions
430 // you must global sweep to clean up broken reachability
431 if( !impossibleEdges.isEmpty() ) {
432 if( !DISABLE_GLOBAL_SWEEP ) {
439 public void assignTempXFieldFEqualToTempY( TempDescriptor x,
443 VariableNode lnX = getVariableNodeFromTemp( x );
444 VariableNode lnY = getVariableNodeFromTemp( y );
446 HashSet<HeapRegionNode> nodesWithNewAlpha = new HashSet<HeapRegionNode>();
447 HashSet<RefEdge> edgesWithNewBeta = new HashSet<RefEdge>();
449 // note it is possible that the types of temps in the
450 // flat node to analyze will reveal that some typed
451 // edges in the reachability graph are impossible
452 Set<RefEdge> impossibleEdges = new HashSet<RefEdge>();
454 // first look for possible strong updates and remove those edges
455 boolean strongUpdate = false;
457 Iterator<RefEdge> itrXhrn = lnX.iteratorToReferencees();
458 while( itrXhrn.hasNext() ) {
459 RefEdge edgeX = itrXhrn.next();
460 HeapRegionNode hrnX = edgeX.getDst();
462 // we can do a strong update here if one of two cases holds
464 f != DisjointAnalysis.getArrayField( f.getType() ) &&
465 ( (hrnX.getNumReferencers() == 1) || // case 1
466 (hrnX.isSingleObject() && lnX.getNumReferencees() == 1) // case 2
469 if( !DISABLE_STRONG_UPDATES ) {
471 clearRefEdgesFrom( hrnX, f.getType(), f.getSymbol(), false );
476 // then do all token propagation
477 itrXhrn = lnX.iteratorToReferencees();
478 while( itrXhrn.hasNext() ) {
479 RefEdge edgeX = itrXhrn.next();
480 HeapRegionNode hrnX = edgeX.getDst();
481 ReachSet betaX = edgeX.getBeta();
482 ReachSet R = Canonical.intersection( hrnX.getAlpha(),
486 Iterator<RefEdge> itrYhrn = lnY.iteratorToReferencees();
487 while( itrYhrn.hasNext() ) {
488 RefEdge edgeY = itrYhrn.next();
489 HeapRegionNode hrnY = edgeY.getDst();
490 ReachSet O = edgeY.getBeta();
492 // check for impossible edges
493 if( !isSuperiorType( f.getType(), edgeY.getType() ) ) {
494 impossibleEdges.add( edgeY );
498 // propagate tokens over nodes starting from hrnSrc, and it will
499 // take care of propagating back up edges from any touched nodes
500 ChangeSet Cy = Canonical.unionUpArityToChangeSet( O, R );
501 propagateTokensOverNodes( hrnY, Cy, nodesWithNewAlpha, edgesWithNewBeta );
503 // then propagate back just up the edges from hrn
504 ChangeSet Cx = Canonical.unionUpArityToChangeSet( R, O );
505 HashSet<RefEdge> todoEdges = new HashSet<RefEdge>();
507 Hashtable<RefEdge, ChangeSet> edgePlannedChanges =
508 new Hashtable<RefEdge, ChangeSet>();
510 Iterator<RefEdge> referItr = hrnX.iteratorToReferencers();
511 while( referItr.hasNext() ) {
512 RefEdge edgeUpstream = referItr.next();
513 todoEdges.add( edgeUpstream );
514 edgePlannedChanges.put( edgeUpstream, Cx );
517 propagateTokensOverEdges( todoEdges,
524 // apply the updates to reachability
525 Iterator<HeapRegionNode> nodeItr = nodesWithNewAlpha.iterator();
526 while( nodeItr.hasNext() ) {
527 nodeItr.next().applyAlphaNew();
530 Iterator<RefEdge> edgeItr = edgesWithNewBeta.iterator();
531 while( edgeItr.hasNext() ) {
532 edgeItr.next().applyBetaNew();
536 // then go back through and add the new edges
537 itrXhrn = lnX.iteratorToReferencees();
538 while( itrXhrn.hasNext() ) {
539 RefEdge edgeX = itrXhrn.next();
540 HeapRegionNode hrnX = edgeX.getDst();
542 Iterator<RefEdge> itrYhrn = lnY.iteratorToReferencees();
543 while( itrYhrn.hasNext() ) {
544 RefEdge edgeY = itrYhrn.next();
545 HeapRegionNode hrnY = edgeY.getDst();
547 // skip impossible edges here, we already marked them
548 // when computing reachability propagations above
549 if( !isSuperiorType( f.getType(), edgeY.getType() ) ) {
553 // prepare the new reference edge hrnX.f -> hrnY
554 TypeDescriptor tdNewEdge =
555 mostSpecificType( y.getType(),
560 RefEdge edgeNew = new RefEdge( hrnX,
564 Canonical.pruneBy( edgeY.getBeta(),
570 // look to see if an edge with same field exists
571 // and merge with it, otherwise just add the edge
572 RefEdge edgeExisting = hrnX.getReferenceTo( hrnY,
576 if( edgeExisting != null ) {
577 edgeExisting.setBeta(
578 Canonical.union( edgeExisting.getBeta(),
582 edgeExisting.setPreds(
583 Canonical.join( edgeExisting.getPreds(),
589 addRefEdge( hrnX, hrnY, edgeNew );
594 Iterator<RefEdge> itrImp = impossibleEdges.iterator();
595 while( itrImp.hasNext() ) {
596 RefEdge edgeImp = itrImp.next();
597 removeRefEdge( edgeImp );
600 // if there was a strong update, make sure to improve
601 // reachability with a global sweep
602 if( strongUpdate || !impossibleEdges.isEmpty() ) {
603 if( !DISABLE_GLOBAL_SWEEP ) {
610 public void assignReturnEqualToTemp( TempDescriptor x ) {
612 VariableNode lnR = getVariableNodeFromTemp( tdReturn );
613 VariableNode lnX = getVariableNodeFromTemp( x );
615 clearRefEdgesFrom( lnR, null, null, true );
617 Iterator<RefEdge> itrXhrn = lnX.iteratorToReferencees();
618 while( itrXhrn.hasNext() ) {
619 RefEdge edgeX = itrXhrn.next();
620 HeapRegionNode referencee = edgeX.getDst();
621 RefEdge edgeNew = edgeX.copy();
622 edgeNew.setSrc( lnR );
624 addRefEdge( lnR, referencee, edgeNew );
629 public void assignTempEqualToNewAlloc( TempDescriptor x,
636 // after the age operation the newest (or zero-ith oldest)
637 // node associated with the allocation site should have
638 // no references to it as if it were a newly allocated
640 Integer idNewest = as.getIthOldest( 0 );
641 HeapRegionNode hrnNewest = id2hrn.get( idNewest );
642 assert hrnNewest != null;
644 VariableNode lnX = getVariableNodeFromTemp( x );
645 clearRefEdgesFrom( lnX, null, null, true );
647 // make a new reference to allocated node
648 TypeDescriptor type = as.getType();
651 new RefEdge( lnX, // source
655 hrnNewest.getAlpha(), // beta
656 predsTrue // predicates
659 addRefEdge( lnX, hrnNewest, edgeNew );
663 // use the allocation site (unique to entire analysis) to
664 // locate the heap region nodes in this reachability graph
665 // that should be aged. The process models the allocation
666 // of new objects and collects all the oldest allocations
667 // in a summary node to allow for a finite analysis
669 // There is an additional property of this method. After
670 // running it on a particular reachability graph (many graphs
671 // may have heap regions related to the same allocation site)
672 // the heap region node objects in this reachability graph will be
673 // allocated. Therefore, after aging a graph for an allocation
674 // site, attempts to retrieve the heap region nodes using the
675 // integer id's contained in the allocation site should always
676 // return non-null heap regions.
677 public void age( AllocSite as ) {
679 // keep track of allocation sites that are represented
680 // in this graph for efficiency with other operations
681 allocSites.add( as );
683 // if there is a k-th oldest node, it merges into
685 Integer idK = as.getOldest();
686 if( id2hrn.containsKey( idK ) ) {
687 HeapRegionNode hrnK = id2hrn.get( idK );
689 // retrieve the summary node, or make it
691 HeapRegionNode hrnSummary = getSummaryNode( as, false );
693 mergeIntoSummary( hrnK, hrnSummary );
696 // move down the line of heap region nodes
697 // clobbering the ith and transferring all references
698 // to and from i-1 to node i.
699 for( int i = allocationDepth - 1; i > 0; --i ) {
701 // only do the transfer if the i-1 node exists
702 Integer idImin1th = as.getIthOldest( i - 1 );
703 if( id2hrn.containsKey( idImin1th ) ) {
704 HeapRegionNode hrnImin1 = id2hrn.get( idImin1th );
705 if( hrnImin1.isWiped() ) {
706 // there is no info on this node, just skip
710 // either retrieve or make target of transfer
711 HeapRegionNode hrnI = getIthNode( as, i, false );
713 transferOnto( hrnImin1, hrnI );
718 // as stated above, the newest node should have had its
719 // references moved over to the second oldest, so we wipe newest
720 // in preparation for being the new object to assign something to
721 HeapRegionNode hrn0 = getIthNode( as, 0, false );
722 wipeOut( hrn0, true );
724 // now tokens in reachability sets need to "age" also
725 Iterator itrAllVariableNodes = td2vn.entrySet().iterator();
726 while( itrAllVariableNodes.hasNext() ) {
727 Map.Entry me = (Map.Entry) itrAllVariableNodes.next();
728 VariableNode ln = (VariableNode) me.getValue();
730 Iterator<RefEdge> itrEdges = ln.iteratorToReferencees();
731 while( itrEdges.hasNext() ) {
732 ageTuplesFrom( as, itrEdges.next() );
736 Iterator itrAllHRNodes = id2hrn.entrySet().iterator();
737 while( itrAllHRNodes.hasNext() ) {
738 Map.Entry me = (Map.Entry) itrAllHRNodes.next();
739 HeapRegionNode hrnToAge = (HeapRegionNode) me.getValue();
741 ageTuplesFrom( as, hrnToAge );
743 Iterator<RefEdge> itrEdges = hrnToAge.iteratorToReferencees();
744 while( itrEdges.hasNext() ) {
745 ageTuplesFrom( as, itrEdges.next() );
750 // after tokens have been aged, reset newest node's reachability
751 // and a brand new node has a "true" predicate
752 hrn0.setAlpha( hrn0.getInherent() );
753 hrn0.setPreds( predsTrue );
757 // either retrieve or create the needed heap region node
758 protected HeapRegionNode getSummaryNode( AllocSite as,
763 idSummary = as.getSummaryShadow();
765 idSummary = as.getSummary();
768 HeapRegionNode hrnSummary = id2hrn.get( idSummary );
770 if( hrnSummary == null ) {
772 boolean hasFlags = false;
773 if( as.getType().isClass() ) {
774 hasFlags = as.getType().getClassDesc().hasFlags();
778 hasFlags = as.getFlag();
781 String strDesc = as.toStringForDOT()+"\\nsummary";
783 strDesc += " shadow";
787 createNewHeapRegionNode( idSummary, // id or null to generate a new one
788 false, // single object?
790 hasFlags, // flagged?
791 false, // out-of-context?
792 as.getType(), // type
793 as, // allocation site
794 null, // inherent reach
795 null, // current reach
796 predsEmpty, // predicates
797 strDesc // description
804 // either retrieve or create the needed heap region node
805 protected HeapRegionNode getIthNode( AllocSite as,
811 idIth = as.getIthOldestShadow( i );
813 idIth = as.getIthOldest( i );
816 HeapRegionNode hrnIth = id2hrn.get( idIth );
818 if( hrnIth == null ) {
820 boolean hasFlags = false;
821 if( as.getType().isClass() ) {
822 hasFlags = as.getType().getClassDesc().hasFlags();
826 hasFlags = as.getFlag();
829 String strDesc = as.toStringForDOT()+"\\n"+i+" oldest";
831 strDesc += " shadow";
834 hrnIth = createNewHeapRegionNode( idIth, // id or null to generate a new one
835 true, // single object?
837 hasFlags, // flagged?
838 false, // out-of-context?
839 as.getType(), // type
840 as, // allocation site
841 null, // inherent reach
842 null, // current reach
843 predsEmpty, // predicates
844 strDesc // description
852 protected void mergeIntoSummary( HeapRegionNode hrn,
853 HeapRegionNode hrnSummary ) {
854 assert hrnSummary.isNewSummary();
856 // assert that these nodes belong to THIS graph
857 assert belongsToThis( hrn );
858 assert belongsToThis( hrnSummary );
860 assert hrn != hrnSummary;
862 // transfer references _from_ hrn over to hrnSummary
863 Iterator<RefEdge> itrReferencee = hrn.iteratorToReferencees();
864 while( itrReferencee.hasNext() ) {
865 RefEdge edge = itrReferencee.next();
866 RefEdge edgeMerged = edge.copy();
867 edgeMerged.setSrc( hrnSummary );
869 HeapRegionNode hrnReferencee = edge.getDst();
870 RefEdge edgeSummary =
871 hrnSummary.getReferenceTo( hrnReferencee,
876 if( edgeSummary == null ) {
877 // the merge is trivial, nothing to be done
878 addRefEdge( hrnSummary, hrnReferencee, edgeMerged );
881 // otherwise an edge from the referencer to hrnSummary exists already
882 // and the edge referencer->hrn should be merged with it
884 Canonical.union( edgeMerged.getBeta(),
885 edgeSummary.getBeta()
888 edgeSummary.setPreds(
889 Canonical.join( edgeMerged.getPreds(),
890 edgeSummary.getPreds()
896 // next transfer references _to_ hrn over to hrnSummary
897 Iterator<RefEdge> itrReferencer = hrn.iteratorToReferencers();
898 while( itrReferencer.hasNext() ) {
899 RefEdge edge = itrReferencer.next();
900 RefEdge edgeMerged = edge.copy();
901 edgeMerged.setDst( hrnSummary );
903 RefSrcNode onReferencer = edge.getSrc();
904 RefEdge edgeSummary =
905 onReferencer.getReferenceTo( hrnSummary,
910 if( edgeSummary == null ) {
911 // the merge is trivial, nothing to be done
912 addRefEdge( onReferencer, hrnSummary, edgeMerged );
915 // otherwise an edge from the referencer to alpha_S exists already
916 // and the edge referencer->alpha_K should be merged with it
918 Canonical.union( edgeMerged.getBeta(),
919 edgeSummary.getBeta()
922 edgeSummary.setPreds(
923 Canonical.join( edgeMerged.getPreds(),
924 edgeSummary.getPreds()
930 // then merge hrn reachability into hrnSummary
932 Canonical.union( hrnSummary.getAlpha(),
938 Canonical.join( hrnSummary.getPreds(),
943 // and afterward, this node is gone
944 wipeOut( hrn, true );
948 protected void transferOnto( HeapRegionNode hrnA,
949 HeapRegionNode hrnB ) {
951 assert belongsToThis( hrnA );
952 assert belongsToThis( hrnB );
955 // clear references in and out of node b?
956 assert hrnB.isWiped();
958 // copy each: (edge in and out of A) to B
959 Iterator<RefEdge> itrReferencee = hrnA.iteratorToReferencees();
960 while( itrReferencee.hasNext() ) {
961 RefEdge edge = itrReferencee.next();
962 HeapRegionNode hrnReferencee = edge.getDst();
963 RefEdge edgeNew = edge.copy();
964 edgeNew.setSrc( hrnB );
965 edgeNew.setDst( hrnReferencee );
967 addRefEdge( hrnB, hrnReferencee, edgeNew );
970 Iterator<RefEdge> itrReferencer = hrnA.iteratorToReferencers();
971 while( itrReferencer.hasNext() ) {
972 RefEdge edge = itrReferencer.next();
973 RefSrcNode rsnReferencer = edge.getSrc();
974 RefEdge edgeNew = edge.copy();
975 edgeNew.setSrc( rsnReferencer );
976 edgeNew.setDst( hrnB );
978 addRefEdge( rsnReferencer, hrnB, edgeNew );
981 // replace hrnB reachability and preds with hrnA's
982 hrnB.setAlpha( hrnA.getAlpha() );
983 hrnB.setPreds( hrnA.getPreds() );
985 // after transfer, wipe out source
986 wipeOut( hrnA, true );
990 // the purpose of this method is to conceptually "wipe out"
991 // a heap region from the graph--purposefully not called REMOVE
992 // because the node is still hanging around in the graph, just
993 // not mechanically connected or have any reach or predicate
994 // information on it anymore--lots of ops can use this
995 protected void wipeOut( HeapRegionNode hrn,
996 boolean wipeVariableReferences ) {
998 assert belongsToThis( hrn );
1000 clearRefEdgesFrom( hrn, null, null, true );
1002 if( wipeVariableReferences ) {
1003 clearRefEdgesTo( hrn, null, null, true );
1005 clearNonVarRefEdgesTo( hrn );
1008 hrn.setAlpha( rsetEmpty );
1009 hrn.setPreds( predsEmpty );
1013 protected void ageTuplesFrom( AllocSite as, RefEdge edge ) {
1015 Canonical.ageTuplesFrom( edge.getBeta(),
1021 protected void ageTuplesFrom( AllocSite as, HeapRegionNode hrn ) {
1023 Canonical.ageTuplesFrom( hrn.getAlpha(),
1031 protected void propagateTokensOverNodes( HeapRegionNode nPrime,
1033 HashSet<HeapRegionNode> nodesWithNewAlpha,
1034 HashSet<RefEdge> edgesWithNewBeta ) {
1036 HashSet<HeapRegionNode> todoNodes
1037 = new HashSet<HeapRegionNode>();
1038 todoNodes.add( nPrime );
1040 HashSet<RefEdge> todoEdges
1041 = new HashSet<RefEdge>();
1043 Hashtable<HeapRegionNode, ChangeSet> nodePlannedChanges
1044 = new Hashtable<HeapRegionNode, ChangeSet>();
1045 nodePlannedChanges.put( nPrime, c0 );
1047 Hashtable<RefEdge, ChangeSet> edgePlannedChanges
1048 = new Hashtable<RefEdge, ChangeSet>();
1050 // first propagate change sets everywhere they can go
1051 while( !todoNodes.isEmpty() ) {
1052 HeapRegionNode n = todoNodes.iterator().next();
1053 ChangeSet C = nodePlannedChanges.get( n );
1055 Iterator<RefEdge> referItr = n.iteratorToReferencers();
1056 while( referItr.hasNext() ) {
1057 RefEdge edge = referItr.next();
1058 todoEdges.add( edge );
1060 if( !edgePlannedChanges.containsKey( edge ) ) {
1061 edgePlannedChanges.put( edge,
1066 edgePlannedChanges.put( edge,
1067 Canonical.union( edgePlannedChanges.get( edge ),
1073 Iterator<RefEdge> refeeItr = n.iteratorToReferencees();
1074 while( refeeItr.hasNext() ) {
1075 RefEdge edgeF = refeeItr.next();
1076 HeapRegionNode m = edgeF.getDst();
1078 ChangeSet changesToPass = ChangeSet.factory();
1080 Iterator<ChangeTuple> itrCprime = C.iterator();
1081 while( itrCprime.hasNext() ) {
1082 ChangeTuple c = itrCprime.next();
1083 if( edgeF.getBeta().contains( c.getSetToMatch() ) ) {
1084 changesToPass = Canonical.union( changesToPass, c );
1088 if( !changesToPass.isEmpty() ) {
1089 if( !nodePlannedChanges.containsKey( m ) ) {
1090 nodePlannedChanges.put( m, ChangeSet.factory() );
1093 ChangeSet currentChanges = nodePlannedChanges.get( m );
1095 if( !changesToPass.isSubset( currentChanges ) ) {
1097 nodePlannedChanges.put( m,
1098 Canonical.union( currentChanges,
1107 todoNodes.remove( n );
1110 // then apply all of the changes for each node at once
1111 Iterator itrMap = nodePlannedChanges.entrySet().iterator();
1112 while( itrMap.hasNext() ) {
1113 Map.Entry me = (Map.Entry) itrMap.next();
1114 HeapRegionNode n = (HeapRegionNode) me.getKey();
1115 ChangeSet C = (ChangeSet) me.getValue();
1117 // this propagation step is with respect to one change,
1118 // so we capture the full change from the old alpha:
1119 ReachSet localDelta = Canonical.applyChangeSet( n.getAlpha(),
1123 // but this propagation may be only one of many concurrent
1124 // possible changes, so keep a running union with the node's
1125 // partially updated new alpha set
1126 n.setAlphaNew( Canonical.union( n.getAlphaNew(),
1131 nodesWithNewAlpha.add( n );
1134 propagateTokensOverEdges( todoEdges,
1141 protected void propagateTokensOverEdges( HashSet <RefEdge> todoEdges,
1142 Hashtable<RefEdge, ChangeSet> edgePlannedChanges,
1143 HashSet <RefEdge> edgesWithNewBeta ) {
1145 // first propagate all change tuples everywhere they can go
1146 while( !todoEdges.isEmpty() ) {
1147 RefEdge edgeE = todoEdges.iterator().next();
1148 todoEdges.remove( edgeE );
1150 if( !edgePlannedChanges.containsKey( edgeE ) ) {
1151 edgePlannedChanges.put( edgeE,
1156 ChangeSet C = edgePlannedChanges.get( edgeE );
1158 ChangeSet changesToPass = ChangeSet.factory();
1160 Iterator<ChangeTuple> itrC = C.iterator();
1161 while( itrC.hasNext() ) {
1162 ChangeTuple c = itrC.next();
1163 if( edgeE.getBeta().contains( c.getSetToMatch() ) ) {
1164 changesToPass = Canonical.union( changesToPass, c );
1168 RefSrcNode rsn = edgeE.getSrc();
1170 if( !changesToPass.isEmpty() && rsn instanceof HeapRegionNode ) {
1171 HeapRegionNode n = (HeapRegionNode) rsn;
1173 Iterator<RefEdge> referItr = n.iteratorToReferencers();
1174 while( referItr.hasNext() ) {
1175 RefEdge edgeF = referItr.next();
1177 if( !edgePlannedChanges.containsKey( edgeF ) ) {
1178 edgePlannedChanges.put( edgeF,
1183 ChangeSet currentChanges = edgePlannedChanges.get( edgeF );
1185 if( !changesToPass.isSubset( currentChanges ) ) {
1186 todoEdges.add( edgeF );
1187 edgePlannedChanges.put( edgeF,
1188 Canonical.union( currentChanges,
1197 // then apply all of the changes for each edge at once
1198 Iterator itrMap = edgePlannedChanges.entrySet().iterator();
1199 while( itrMap.hasNext() ) {
1200 Map.Entry me = (Map.Entry) itrMap.next();
1201 RefEdge e = (RefEdge) me.getKey();
1202 ChangeSet C = (ChangeSet) me.getValue();
1204 // this propagation step is with respect to one change,
1205 // so we capture the full change from the old beta:
1206 ReachSet localDelta =
1207 Canonical.applyChangeSet( e.getBeta(),
1212 // but this propagation may be only one of many concurrent
1213 // possible changes, so keep a running union with the edge's
1214 // partially updated new beta set
1215 e.setBetaNew( Canonical.union( e.getBetaNew(),
1220 edgesWithNewBeta.add( e );
1225 // used in makeCalleeView below to decide if there is
1226 // already an appropriate out-of-context edge in a callee
1227 // view graph for merging, or null if a new one will be added
1229 getOutOfContextReferenceTo( HeapRegionNode hrn,
1230 TypeDescriptor srcType,
1231 TypeDescriptor refType,
1233 assert belongsToThis( hrn );
1235 HeapRegionNode hrnInContext = id2hrn.get( hrn.getID() );
1236 if( hrnInContext == null ) {
1240 Iterator<RefEdge> refItr = hrnInContext.iteratorToReferencers();
1241 while( refItr.hasNext() ) {
1242 RefEdge re = refItr.next();
1244 assert belongsToThis( re.getSrc() );
1245 assert belongsToThis( re.getDst() );
1247 if( !(re.getSrc() instanceof HeapRegionNode) ) {
1251 HeapRegionNode hrnSrc = (HeapRegionNode) re.getSrc();
1252 if( !hrnSrc.isOutOfContext() ) {
1256 if( srcType == null ) {
1257 if( hrnSrc.getType() != null ) {
1261 if( !srcType.equals( hrnSrc.getType() ) ) {
1266 if( !re.typeEquals( refType ) ) {
1270 if( !re.fieldEquals( refField ) ) {
1274 // tada! We found it!
1281 // used below to convert a ReachSet to its callee-context
1282 // equivalent with respect to allocation sites in this graph
1283 protected ReachSet toCalleeContext( Set<ReachTuple> oocTuples,
1286 TempDescriptor tdSrc,
1289 TypeDescriptor type,
1291 boolean outOfContext
1293 ReachSet out = ReachSet.factory();
1295 Iterator<ReachState> itr = rs.iterator();
1296 while( itr.hasNext() ) {
1297 ReachState stateCaller = itr.next();
1299 ReachState stateCallee = stateCaller;
1301 Iterator<AllocSite> asItr = allocSites.iterator();
1302 while( asItr.hasNext() ) {
1303 AllocSite as = asItr.next();
1305 ReachState stateNew = ReachState.factory();
1306 Iterator<ReachTuple> rtItr = stateCallee.iterator();
1307 while( rtItr.hasNext() ) {
1308 ReachTuple rt = rtItr.next();
1310 // only translate this tuple if it is in the out-context bag
1311 if( !oocTuples.contains( rt ) ) {
1312 stateNew = Canonical.union( stateNew, rt );
1316 int age = as.getAgeCategory( rt.getHrnID() );
1318 // this is the current mapping, where 0, 1, 2S were allocated
1319 // in the current context, 0?, 1? and 2S? were allocated in a
1320 // previous context, and we're translating to a future context
1332 if( age == AllocSite.AGE_notInThisSite ) {
1333 // things not from the site just go back in
1334 stateNew = Canonical.union( stateNew, rt );
1336 } else if( age == AllocSite.AGE_summary ||
1339 // the in-context summary and all existing out-of-context
1341 stateNew = Canonical.union( stateNew,
1342 ReachTuple.factory( as.getSummary(),
1345 true // out-of-context
1349 // otherwise everything else just goes to an out-of-context
1350 // version, everything else the same
1351 Integer I = as.getAge( rt.getHrnID() );
1354 assert !rt.isMultiObject();
1356 stateNew = Canonical.union( stateNew,
1357 ReachTuple.factory( rt.getHrnID(),
1360 true // out-of-context
1366 stateCallee = stateNew;
1372 if( outOfContext ) {
1376 if( hrnID != null ) {
1377 assert tdSrc == null;
1378 assert hrnSrcID == null;
1379 assert hrnDstID == null;
1380 pred = ExistPred.factory( hrnID,
1383 assert tdSrc != null || hrnSrcID != null;
1384 assert hrnDstID != null;
1385 pred = ExistPred.factory( tdSrc,
1393 preds = ExistPredSet.factory( pred );
1396 stateCallee = Canonical.attach( stateCallee,
1399 out = Canonical.add( out,
1404 assert out.isCanonical();
1408 // used below to convert a ReachSet to its caller-context
1409 // equivalent with respect to allocation sites in this graph
1411 toCallerContext( ReachSet rs,
1412 Hashtable<ReachState, ExistPredSet> calleeStatesSatisfied
1414 ReachSet out = ReachSet.factory();
1416 Iterator<ReachState> itr = rs.iterator();
1417 while( itr.hasNext() ) {
1418 ReachState stateCallee = itr.next();
1420 if( calleeStatesSatisfied.containsKey( stateCallee ) ) {
1422 // starting from one callee state...
1423 ReachSet rsCaller = ReachSet.factory( stateCallee );
1425 // possibly branch it into many states, which any
1426 // allocation site might do, so lots of derived states
1427 Iterator<AllocSite> asItr = allocSites.iterator();
1428 while( asItr.hasNext() ) {
1429 AllocSite as = asItr.next();
1430 rsCaller = Canonical.toCallerContext( rs, as );
1433 // then before adding each derived, now caller-context
1434 // states to the output, attach the appropriate pred
1435 // based on the source callee state
1436 Iterator<ReachState> stateItr = rsCaller.iterator();
1437 while( stateItr.hasNext() ) {
1438 ReachState stateCaller = stateItr.next();
1439 stateCaller = Canonical.attach( stateCaller,
1440 calleeStatesSatisfied.get( stateCallee )
1442 out = Canonical.union( out,
1449 assert out.isCanonical();
1453 // used below to convert a ReachSet to an equivalent
1454 // version with shadow IDs merged into unshadowed IDs
1455 protected ReachSet unshadow( ReachSet rs ) {
1457 Iterator<AllocSite> asItr = allocSites.iterator();
1458 while( asItr.hasNext() ) {
1459 AllocSite as = asItr.next();
1460 out = Canonical.unshadow( out, as );
1462 assert out.isCanonical();
1467 // use this method to make a new reach graph that is
1468 // what heap the FlatMethod callee from the FlatCall
1469 // would start with reaching from its arguments in
1472 makeCalleeView( FlatCall fc,
1473 FlatMethod fmCallee,
1474 Set<Integer> callerNodeIDsCopiedToCallee,
1475 boolean writeDebugDOTs
1479 // first traverse this context to find nodes and edges
1480 // that will be callee-reachable
1481 Set<HeapRegionNode> reachableCallerNodes =
1482 new HashSet<HeapRegionNode>();
1484 // caller edges between callee-reachable nodes
1485 Set<RefEdge> reachableCallerEdges =
1486 new HashSet<RefEdge>();
1488 // caller edges from arg vars, and the matching param index
1489 // because these become a special edge in callee
1490 Hashtable<RefEdge, Integer> reachableCallerArgEdges2paramIndex =
1491 new Hashtable<RefEdge, Integer>();
1493 // caller edges from local vars or callee-unreachable nodes
1494 // (out-of-context sources) to callee-reachable nodes
1495 Set<RefEdge> oocCallerEdges =
1496 new HashSet<RefEdge>();
1499 for( int i = 0; i < fmCallee.numParameters(); ++i ) {
1501 TempDescriptor tdArg = fc.getArgMatchingParamIndex( fmCallee, i );
1502 VariableNode vnArgCaller = this.getVariableNodeFromTemp( tdArg );
1504 Set<RefSrcNode> toVisitInCaller = new HashSet<RefSrcNode>();
1505 Set<RefSrcNode> visitedInCaller = new HashSet<RefSrcNode>();
1507 toVisitInCaller.add( vnArgCaller );
1509 while( !toVisitInCaller.isEmpty() ) {
1510 RefSrcNode rsnCaller = toVisitInCaller.iterator().next();
1511 toVisitInCaller.remove( rsnCaller );
1512 visitedInCaller.add( rsnCaller );
1514 Iterator<RefEdge> itrRefEdges = rsnCaller.iteratorToReferencees();
1515 while( itrRefEdges.hasNext() ) {
1516 RefEdge reCaller = itrRefEdges.next();
1517 HeapRegionNode hrnCaller = reCaller.getDst();
1519 callerNodeIDsCopiedToCallee.add( hrnCaller.getID() );
1520 reachableCallerNodes.add( hrnCaller );
1522 if( reCaller.getSrc() instanceof HeapRegionNode ) {
1523 reachableCallerEdges.add( reCaller );
1525 if( rsnCaller.equals( vnArgCaller ) ) {
1526 reachableCallerArgEdges2paramIndex.put( reCaller, i );
1528 oocCallerEdges.add( reCaller );
1532 if( !visitedInCaller.contains( hrnCaller ) ) {
1533 toVisitInCaller.add( hrnCaller );
1536 } // end edge iteration
1537 } // end visiting heap nodes in caller
1538 } // end iterating over parameters as starting points
1541 // now collect out-of-context reach tuples and
1542 // more out-of-context edges
1543 Set<ReachTuple> oocTuples = new HashSet<ReachTuple>();
1545 Iterator<Integer> itrInContext =
1546 callerNodeIDsCopiedToCallee.iterator();
1547 while( itrInContext.hasNext() ) {
1548 Integer hrnID = itrInContext.next();
1549 HeapRegionNode hrnCallerAndInContext = id2hrn.get( hrnID );
1551 Iterator<RefEdge> itrMightCross =
1552 hrnCallerAndInContext.iteratorToReferencers();
1553 while( itrMightCross.hasNext() ) {
1554 RefEdge edgeMightCross = itrMightCross.next();
1556 RefSrcNode rsnCallerAndOutContext =
1557 edgeMightCross.getSrc();
1559 if( rsnCallerAndOutContext instanceof VariableNode ) {
1560 // variables do not have out-of-context reach states,
1562 oocCallerEdges.add( edgeMightCross );
1566 HeapRegionNode hrnCallerAndOutContext =
1567 (HeapRegionNode) rsnCallerAndOutContext;
1569 // is this source node out-of-context?
1570 if( callerNodeIDsCopiedToCallee.contains( hrnCallerAndOutContext.getID() ) ) {
1571 // no, skip this edge
1576 oocCallerEdges.add( edgeMightCross );
1578 // add all reach tuples on the node to list
1579 // of things that are out-of-context: insight
1580 // if this node is reachable from someting that WAS
1581 // in-context, then this node should already be in-context
1582 Iterator<ReachState> stateItr = hrnCallerAndOutContext.getAlpha().iterator();
1583 while( stateItr.hasNext() ) {
1584 ReachState state = stateItr.next();
1586 Iterator<ReachTuple> rtItr = state.iterator();
1587 while( rtItr.hasNext() ) {
1588 ReachTuple rt = rtItr.next();
1590 oocTuples.add( rt );
1597 // the callee view is a new graph: DON'T MODIFY *THIS* graph
1598 ReachGraph rg = new ReachGraph();
1600 // add nodes to callee graph
1601 Iterator<HeapRegionNode> hrnItr = reachableCallerNodes.iterator();
1602 while( hrnItr.hasNext() ) {
1603 HeapRegionNode hrnCaller = hrnItr.next();
1605 assert callerNodeIDsCopiedToCallee.contains( hrnCaller.getID() );
1606 assert !rg.id2hrn.containsKey( hrnCaller.getID() );
1608 ExistPred pred = ExistPred.factory( hrnCaller.getID(), null );
1609 ExistPredSet preds = ExistPredSet.factory( pred );
1611 rg.createNewHeapRegionNode( hrnCaller.getID(),
1612 hrnCaller.isSingleObject(),
1613 hrnCaller.isNewSummary(),
1614 hrnCaller.isFlagged(),
1615 false, // out-of-context?
1616 hrnCaller.getType(),
1617 hrnCaller.getAllocSite(),
1618 toCalleeContext( oocTuples,
1619 hrnCaller.getInherent(), // in state
1620 hrnCaller.getID(), // node pred
1621 null, null, null, null, null, // edge pred
1622 false ), // ooc pred
1623 toCalleeContext( oocTuples,
1624 hrnCaller.getAlpha(), // in state
1625 hrnCaller.getID(), // node pred
1626 null, null, null, null, null, // edge pred
1627 false ), // ooc pred
1629 hrnCaller.getDescription()
1633 // add param edges to callee graph
1635 reachableCallerArgEdges2paramIndex.entrySet().iterator();
1636 while( argEdges.hasNext() ) {
1637 Map.Entry me = (Map.Entry) argEdges.next();
1638 RefEdge reArg = (RefEdge) me.getKey();
1639 Integer index = (Integer) me.getValue();
1641 TempDescriptor arg = fmCallee.getParameter( index );
1643 VariableNode vnCallee =
1644 rg.getVariableNodeFromTemp( arg );
1646 HeapRegionNode hrnDstCaller = reArg.getDst();
1647 HeapRegionNode hrnDstCallee = rg.id2hrn.get( hrnDstCaller.getID() );
1648 assert hrnDstCallee != null;
1651 ExistPred.factory( arg,
1653 hrnDstCallee.getID(),
1657 false ); // out-of-context
1659 ExistPredSet preds =
1660 ExistPredSet.factory( pred );
1663 new RefEdge( vnCallee,
1667 toCalleeContext( oocTuples,
1668 reArg.getBeta(), // in state
1672 hrnDstCallee.getID(), // edge pred
1673 reArg.getType(), // edge pred
1674 reArg.getField(), // edge pred
1675 false ), // ooc pred
1679 rg.addRefEdge( vnCallee,
1685 // add in-context edges to callee graph
1686 Iterator<RefEdge> reItr = reachableCallerEdges.iterator();
1687 while( reItr.hasNext() ) {
1688 RefEdge reCaller = reItr.next();
1689 RefSrcNode rsnCaller = reCaller.getSrc();
1690 assert rsnCaller instanceof HeapRegionNode;
1691 HeapRegionNode hrnSrcCaller = (HeapRegionNode) rsnCaller;
1692 HeapRegionNode hrnDstCaller = reCaller.getDst();
1694 HeapRegionNode hrnSrcCallee = rg.id2hrn.get( hrnSrcCaller.getID() );
1695 HeapRegionNode hrnDstCallee = rg.id2hrn.get( hrnDstCaller.getID() );
1696 assert hrnSrcCallee != null;
1697 assert hrnDstCallee != null;
1700 ExistPred.factory( null,
1701 hrnSrcCallee.getID(),
1702 hrnDstCallee.getID(),
1704 reCaller.getField(),
1706 false ); // out-of-context
1708 ExistPredSet preds =
1709 ExistPredSet.factory( pred );
1712 new RefEdge( hrnSrcCallee,
1715 reCaller.getField(),
1716 toCalleeContext( oocTuples,
1717 reCaller.getBeta(), // in state
1720 hrnSrcCallee.getID(), // edge pred
1721 hrnDstCallee.getID(), // edge pred
1722 reCaller.getType(), // edge pred
1723 reCaller.getField(), // edge pred
1724 false ), // ooc pred
1728 rg.addRefEdge( hrnSrcCallee,
1734 // add out-of-context edges to callee graph
1735 reItr = oocCallerEdges.iterator();
1736 while( reItr.hasNext() ) {
1737 RefEdge reCaller = reItr.next();
1738 RefSrcNode rsnCaller = reCaller.getSrc();
1739 HeapRegionNode hrnDstCaller = reCaller.getDst();
1740 HeapRegionNode hrnDstCallee = rg.id2hrn.get( hrnDstCaller.getID() );
1741 assert hrnDstCallee != null;
1743 TypeDescriptor oocNodeType;
1745 TempDescriptor oocPredSrcTemp = null;
1746 Integer oocPredSrcID = null;
1748 if( rsnCaller instanceof VariableNode ) {
1749 VariableNode vnCaller = (VariableNode) rsnCaller;
1751 oocReach = rsetEmpty;
1752 oocPredSrcTemp = vnCaller.getTempDescriptor();
1755 HeapRegionNode hrnSrcCaller = (HeapRegionNode) rsnCaller;
1756 assert !callerNodeIDsCopiedToCallee.contains( hrnSrcCaller.getID() );
1757 oocNodeType = hrnSrcCaller.getType();
1758 oocReach = hrnSrcCaller.getAlpha();
1759 oocPredSrcID = hrnSrcCaller.getID();
1763 ExistPred.factory( oocPredSrcTemp,
1765 hrnDstCallee.getID(),
1767 reCaller.getField(),
1769 true ); // out-of-context
1771 ExistPredSet preds =
1772 ExistPredSet.factory( pred );
1774 RefEdge oocEdgeExisting =
1775 rg.getOutOfContextReferenceTo( hrnDstCallee,
1781 if( oocEdgeExisting == null ) {
1782 // for consistency, map one out-of-context "identifier"
1783 // to one heap region node id, otherwise no convergence
1784 String oocid = "oocid"+
1786 hrnDstCallee.getIDString()+
1789 reCaller.getField();
1791 Integer oocHrnID = oocid2hrnid.get( oocid );
1793 HeapRegionNode hrnCalleeAndOutContext;
1795 if( oocHrnID == null ) {
1797 hrnCalleeAndOutContext =
1798 rg.createNewHeapRegionNode( null, // ID
1799 false, // single object?
1800 false, // new summary?
1802 true, // out-of-context?
1804 null, // alloc site, shouldn't be used
1805 toCalleeContext( oocTuples,
1806 oocReach, // in state
1808 null, null, null, null, null, // edge pred
1811 toCalleeContext( oocTuples,
1812 oocReach, // in state
1814 null, null, null, null, null, // edge pred
1821 oocid2hrnid.put( oocid, hrnCalleeAndOutContext.getID() );
1825 // the mapping already exists, so see if node is there
1826 hrnCalleeAndOutContext = rg.id2hrn.get( oocHrnID );
1828 if( hrnCalleeAndOutContext == null ) {
1830 hrnCalleeAndOutContext =
1831 rg.createNewHeapRegionNode( oocHrnID, // ID
1832 false, // single object?
1833 false, // new summary?
1835 true, // out-of-context?
1837 null, // alloc site, shouldn't be used
1838 toCalleeContext( oocTuples,
1839 oocReach, // in state
1841 null, null, null, null, null, // edge pred
1844 toCalleeContext( oocTuples,
1845 oocReach, // in state
1847 null, null, null, null, null, // edge pred
1856 rg.addRefEdge( hrnCalleeAndOutContext,
1858 new RefEdge( hrnCalleeAndOutContext,
1861 reCaller.getField(),
1862 toCalleeContext( oocTuples,
1863 reCaller.getBeta(), // in state
1865 oocPredSrcTemp, // edge pred
1866 oocPredSrcID, // edge pred
1867 hrnDstCaller.getID(), // edge pred
1868 reCaller.getType(), // edge pred
1869 reCaller.getField(), // edge pred
1877 // the out-of-context edge already exists
1878 oocEdgeExisting.setBeta( Canonical.union( oocEdgeExisting.getBeta(),
1879 toCalleeContext( oocTuples,
1880 reCaller.getBeta(), // in state
1882 oocPredSrcTemp, // edge pred
1883 oocPredSrcID, // edge pred
1884 hrnDstCaller.getID(), // edge pred
1885 reCaller.getType(), // edge pred
1886 reCaller.getField(), // edge pred
1892 oocEdgeExisting.setPreds( Canonical.join( oocEdgeExisting.getPreds(),
1901 if( writeDebugDOTs ) {
1903 rg.writeGraph( "calleeview",
1904 true, // write labels (variables)
1905 true, // selectively hide intermediate temp vars
1906 true, // prune unreachable heap regions
1907 true, // hide subset reachability states
1908 true ); // hide edge taints
1909 } catch( IOException e ) {}
1915 private static Hashtable<String, Integer> oocid2hrnid =
1916 new Hashtable<String, Integer>();
1921 resolveMethodCall( FlatCall fc,
1922 FlatMethod fmCallee,
1923 ReachGraph rgCallee,
1924 Set<Integer> callerNodeIDsCopiedToCallee,
1925 boolean writeDebugDOTs
1929 if( writeDebugDOTs ) {
1931 rgCallee.writeGraph( "callee",
1932 true, // write labels (variables)
1933 true, // selectively hide intermediate temp vars
1934 true, // prune unreachable heap regions
1935 true, // hide subset reachability states
1936 true ); // hide edge taints
1937 writeGraph( "caller00In",
1938 true, // write labels (variables)
1939 true, // selectively hide intermediate temp vars
1940 true, // prune unreachable heap regions
1941 true, // hide subset reachability states
1942 true, // hide edge taints
1943 callerNodeIDsCopiedToCallee );
1944 } catch( IOException e ) {}
1948 // method call transfer function steps:
1949 // 1. Use current callee-reachable heap (CRH) to test callee
1950 // predicates and mark what will be coming in.
1951 // 2. Wipe CRH out of caller.
1952 // 3. Transplant marked callee parts in:
1953 // a) bring in nodes
1954 // b) bring in callee -> callee edges
1955 // c) resolve out-of-context -> callee edges
1956 // d) assign return value
1957 // 4. Collapse shadow nodes down
1958 // 5. Global sweep it.
1962 // 1. mark what callee elements have satisfied predicates
1963 Hashtable<HeapRegionNode, ExistPredSet> calleeNodesSatisfied =
1964 new Hashtable<HeapRegionNode, ExistPredSet>();
1966 Hashtable<RefEdge, ExistPredSet> calleeEdgesSatisfied =
1967 new Hashtable<RefEdge, ExistPredSet>();
1969 Hashtable<ReachState, ExistPredSet> calleeStatesSatisfied =
1970 new Hashtable<ReachState, ExistPredSet>();
1972 Hashtable< RefEdge, Set<RefSrcNode> > calleeEdges2oocCallerSrcMatches =
1973 new Hashtable< RefEdge, Set<RefSrcNode> >();
1975 Iterator meItr = rgCallee.id2hrn.entrySet().iterator();
1976 while( meItr.hasNext() ) {
1977 Map.Entry me = (Map.Entry) meItr.next();
1978 Integer id = (Integer) me.getKey();
1979 HeapRegionNode hrnCallee = (HeapRegionNode) me.getValue();
1981 // if a callee element's predicates are satisfied then a set
1982 // of CALLER predicates is returned: they are the predicates
1983 // that the callee element moved into the caller context
1984 // should have, and it is inefficient to find this again later
1985 ExistPredSet predsIfSatis =
1986 hrnCallee.getPreds().isSatisfiedBy( this,
1987 callerNodeIDsCopiedToCallee
1989 if( predsIfSatis != null ) {
1990 calleeNodesSatisfied.put( hrnCallee, predsIfSatis );
1992 // otherwise don't bother looking at edges to this node
1996 // since the node is coming over, find out which reach
1997 // states on it should come over, too
1998 Iterator<ReachState> stateItr = hrnCallee.getAlpha().iterator();
1999 while( stateItr.hasNext() ) {
2000 ReachState stateCallee = stateItr.next();
2003 stateCallee.getPreds().isSatisfiedBy( this,
2004 callerNodeIDsCopiedToCallee
2006 if( predsIfSatis != null ) {
2007 calleeStatesSatisfied.put( stateCallee, predsIfSatis );
2011 // then look at edges to the node
2012 Iterator<RefEdge> reItr = hrnCallee.iteratorToReferencers();
2013 while( reItr.hasNext() ) {
2014 RefEdge reCallee = reItr.next();
2015 RefSrcNode rsnCallee = reCallee.getSrc();
2017 // (caller local variables to in-context heap regions)
2018 // have an (out-of-context heap region -> in-context heap region)
2019 // abstraction in the callEE, so its true we never need to
2020 // look at a (var node -> heap region) edge in callee to bring
2021 // those over for the call site transfer. What about (param var->heap region)
2022 // edges in callee? They are dealt with below this loop.
2023 // So, yes, at this point skip (var->region) edges in callee
2024 if( rsnCallee instanceof VariableNode ) {
2028 // first see if the source is out-of-context, and only
2029 // proceed with this edge if we find some caller-context
2031 HeapRegionNode hrnSrcCallee = (HeapRegionNode) rsnCallee;
2032 boolean matchedOutOfContext = false;
2034 if( hrnSrcCallee.isOutOfContext() ) {
2036 assert !calleeEdges2oocCallerSrcMatches.containsKey( reCallee );
2037 Set<RefSrcNode> rsnCallers = new HashSet<RefSrcNode>();
2039 HeapRegionNode hrnDstCaller = this.id2hrn.get( hrnCallee.getID() );
2040 Iterator<RefEdge> reDstItr = hrnDstCaller.iteratorToReferencers();
2041 while( reDstItr.hasNext() ) {
2042 // the edge and field (either possibly null) must match
2043 RefEdge reCaller = reDstItr.next();
2045 if( !reCaller.typeEquals ( reCallee.getType() ) ||
2046 !reCaller.fieldEquals( reCallee.getField() )
2051 RefSrcNode rsnCaller = reCaller.getSrc();
2052 if( rsnCaller instanceof VariableNode ) {
2053 // a variable node matches an OOC region with null type
2054 if( hrnSrcCallee.getType() != null ) {
2059 // otherwise types should match
2060 HeapRegionNode hrnCallerSrc = (HeapRegionNode) rsnCaller;
2061 if( hrnSrcCallee.getType() == null ) {
2062 if( hrnCallerSrc.getType() != null ) {
2066 if( !hrnSrcCallee.getType().equals( hrnCallerSrc.getType() ) ) {
2072 rsnCallers.add( rsnCaller );
2073 matchedOutOfContext = true;
2076 if( !rsnCallers.isEmpty() ) {
2077 calleeEdges2oocCallerSrcMatches.put( reCallee, rsnCallers );
2081 if( hrnSrcCallee.isOutOfContext() &&
2082 !matchedOutOfContext ) {
2087 reCallee.getPreds().isSatisfiedBy( this,
2088 callerNodeIDsCopiedToCallee
2090 if( predsIfSatis != null ) {
2091 calleeEdgesSatisfied.put( reCallee, predsIfSatis );
2093 // since the edge is coming over, find out which reach
2094 // states on it should come over, too
2095 stateItr = reCallee.getBeta().iterator();
2096 while( stateItr.hasNext() ) {
2097 ReachState stateCallee = stateItr.next();
2100 stateCallee.getPreds().isSatisfiedBy( this,
2101 callerNodeIDsCopiedToCallee
2103 if( predsIfSatis != null ) {
2104 calleeStatesSatisfied.put( stateCallee, predsIfSatis );
2113 // test param -> HRN edges, also
2114 for( int i = 0; i < fmCallee.numParameters(); ++i ) {
2116 // parameter defined here is the symbol in the callee
2117 TempDescriptor tdParam = fmCallee.getParameter( i );
2118 VariableNode vnCallee = rgCallee.getVariableNodeFromTemp( tdParam );
2120 Iterator<RefEdge> reItr = vnCallee.iteratorToReferencees();
2121 while( reItr.hasNext() ) {
2122 RefEdge reCallee = reItr.next();
2124 ExistPredSet ifDst =
2125 reCallee.getDst().getPreds().isSatisfiedBy( this,
2126 callerNodeIDsCopiedToCallee
2128 if( ifDst == null ) {
2132 ExistPredSet predsIfSatis =
2133 reCallee.getPreds().isSatisfiedBy( this,
2134 callerNodeIDsCopiedToCallee
2136 if( predsIfSatis != null ) {
2137 calleeEdgesSatisfied.put( reCallee, predsIfSatis );
2139 // since the edge is coming over, find out which reach
2140 // states on it should come over, too
2141 Iterator<ReachState> stateItr = reCallee.getBeta().iterator();
2142 while( stateItr.hasNext() ) {
2143 ReachState stateCallee = stateItr.next();
2146 stateCallee.getPreds().isSatisfiedBy( this,
2147 callerNodeIDsCopiedToCallee
2149 if( predsIfSatis != null ) {
2150 calleeStatesSatisfied.put( stateCallee, predsIfSatis );
2161 if( writeDebugDOTs ) {
2163 writeGraph( "caller20BeforeWipe",
2164 true, // write labels (variables)
2165 true, // selectively hide intermediate temp vars
2166 true, // prune unreachable heap regions
2167 true, // hide subset reachability states
2168 true ); // hide edge taints
2169 } catch( IOException e ) {}
2173 // 2. predicates tested, ok to wipe out caller part
2174 Iterator<Integer> hrnItr = callerNodeIDsCopiedToCallee.iterator();
2175 while( hrnItr.hasNext() ) {
2176 Integer hrnID = hrnItr.next();
2177 HeapRegionNode hrnCaller = id2hrn.get( hrnID );
2178 assert hrnCaller != null;
2180 // when clearing off nodes, also eliminate variable
2182 wipeOut( hrnCaller, true );
2187 if( writeDebugDOTs ) {
2189 writeGraph( "caller30BeforeAddingNodes",
2190 true, // write labels (variables)
2191 true, // selectively hide intermediate temp vars
2192 true, // prune unreachable heap regions
2193 true, // hide subset reachability states
2194 true ); // hide edge taints
2195 } catch( IOException e ) {}
2199 // 3. callee elements with satisfied preds come in, note that
2200 // the mapping of elements satisfied to preds is like this:
2201 // A callee element EE has preds EEp that are satisfied by
2202 // some caller element ER. We bring EE into the caller
2203 // context as ERee with the preds of ER, namely ERp, which
2204 // in the following algorithm is the value in the mapping
2207 Iterator satisItr = calleeNodesSatisfied.entrySet().iterator();
2208 while( satisItr.hasNext() ) {
2209 Map.Entry me = (Map.Entry) satisItr.next();
2210 HeapRegionNode hrnCallee = (HeapRegionNode) me.getKey();
2211 ExistPredSet preds = (ExistPredSet) me.getValue();
2213 // TODO: I think its true that the current implementation uses
2214 // the type of the OOC region and the predicates OF THE EDGE from
2215 // it to link everything up in caller context, so that's why we're
2216 // skipping this... maybe that's a sillier way to do it?
2217 if( hrnCallee.isOutOfContext() ) {
2221 AllocSite as = hrnCallee.getAllocSite();
2222 allocSites.add( as );
2224 Integer hrnIDshadow = as.getShadowIDfromID( hrnCallee.getID() );
2226 HeapRegionNode hrnCaller = id2hrn.get( hrnIDshadow );
2227 if( hrnCaller == null ) {
2229 createNewHeapRegionNode( hrnIDshadow, // id or null to generate a new one
2230 hrnCallee.isSingleObject(), // single object?
2231 hrnCallee.isNewSummary(), // summary?
2232 hrnCallee.isFlagged(), // flagged?
2233 false, // out-of-context?
2234 hrnCallee.getType(), // type
2235 hrnCallee.getAllocSite(), // allocation site
2236 toCallerContext( hrnCallee.getInherent(),
2237 calleeStatesSatisfied ), // inherent reach
2238 null, // current reach
2239 predsEmpty, // predicates
2240 hrnCallee.getDescription() // description
2243 assert hrnCaller.isWiped();
2246 hrnCaller.setAlpha( toCallerContext( hrnCallee.getAlpha(),
2247 calleeStatesSatisfied
2251 hrnCaller.setPreds( preds );
2256 if( writeDebugDOTs ) {
2258 writeGraph( "caller31BeforeAddingEdges",
2259 true, // write labels (variables)
2260 true, // selectively hide intermediate temp vars
2261 true, // prune unreachable heap regions
2262 true, // hide subset reachability states
2263 true ); // hide edge taints
2264 } catch( IOException e ) {}
2268 // set these up during the next procedure so after
2269 // the caller has all of its nodes and edges put
2270 // back together we can propagate the callee's
2271 // reach changes backwards into the caller graph
2272 HashSet<RefEdge> edgesForPropagation = new HashSet<RefEdge>();
2274 Hashtable<RefEdge, ChangeSet> edgePlannedChanges =
2275 new Hashtable<RefEdge, ChangeSet>();
2278 // 3.b) callee -> callee edges AND out-of-context -> callee
2279 satisItr = calleeEdgesSatisfied.entrySet().iterator();
2280 while( satisItr.hasNext() ) {
2281 Map.Entry me = (Map.Entry) satisItr.next();
2282 RefEdge reCallee = (RefEdge) me.getKey();
2283 ExistPredSet preds = (ExistPredSet) me.getValue();
2285 HeapRegionNode hrnDstCallee = reCallee.getDst();
2286 AllocSite asDst = hrnDstCallee.getAllocSite();
2287 allocSites.add( asDst );
2289 Integer hrnIDDstShadow =
2290 asDst.getShadowIDfromID( hrnDstCallee.getID() );
2292 HeapRegionNode hrnDstCaller = id2hrn.get( hrnIDDstShadow );
2293 assert hrnDstCaller != null;
2296 RefSrcNode rsnCallee = reCallee.getSrc();
2298 Set<RefSrcNode> rsnCallers =
2299 new HashSet<RefSrcNode>();
2301 Set<RefSrcNode> oocCallers =
2302 calleeEdges2oocCallerSrcMatches.get( reCallee );
2304 boolean oocEdges = false;
2306 if( oocCallers == null ) {
2307 // there are no out-of-context matches, so it's
2308 // either a param/arg var or one in-context heap region
2309 if( rsnCallee instanceof VariableNode ) {
2310 // variable -> node in the callee should only
2311 // come into the caller if its from a param var
2312 VariableNode vnCallee = (VariableNode) rsnCallee;
2313 TempDescriptor tdParam = vnCallee.getTempDescriptor();
2314 TempDescriptor tdArg = fc.getArgMatchingParam( fmCallee,
2316 if( tdArg == null ) {
2317 // this means the variable isn't a parameter, its local
2318 // to the callee so we ignore it in call site transfer
2319 // shouldn't this NEVER HAPPEN?
2322 rsnCallers.add( this.getVariableNodeFromTemp( tdArg ) );
2326 // otherwise source is in context, one region
2327 HeapRegionNode hrnSrcCallee = (HeapRegionNode) rsnCallee;
2329 // translate an in-context node to shadow
2330 AllocSite asSrc = hrnSrcCallee.getAllocSite();
2331 allocSites.add( asSrc );
2333 Integer hrnIDSrcShadow =
2334 asSrc.getShadowIDfromID( hrnSrcCallee.getID() );
2336 HeapRegionNode hrnSrcCallerShadow =
2337 this.id2hrn.get( hrnIDSrcShadow );
2339 if( hrnSrcCallerShadow == null ) {
2340 hrnSrcCallerShadow =
2341 createNewHeapRegionNode( hrnIDSrcShadow, // id or null to generate a new one
2342 hrnSrcCallee.isSingleObject(), // single object?
2343 hrnSrcCallee.isNewSummary(), // summary?
2344 hrnSrcCallee.isFlagged(), // flagged?
2345 false, // out-of-context?
2346 hrnSrcCallee.getType(), // type
2347 hrnSrcCallee.getAllocSite(), // allocation site
2348 toCallerContext( hrnSrcCallee.getInherent(),
2349 calleeStatesSatisfied ), // inherent reach
2350 toCallerContext( hrnSrcCallee.getAlpha(),
2351 calleeStatesSatisfied ), // current reach
2352 predsEmpty, // predicates
2353 hrnSrcCallee.getDescription() // description
2357 rsnCallers.add( hrnSrcCallerShadow );
2361 // otherwise we have a set of out-of-context srcs
2362 // that should NOT be translated to shadow nodes
2363 assert !oocCallers.isEmpty();
2364 rsnCallers.addAll( oocCallers );
2368 // now make all caller edges we've identified from
2369 // this callee edge with a satisfied predicate
2370 assert !rsnCallers.isEmpty();
2371 Iterator<RefSrcNode> rsnItr = rsnCallers.iterator();
2372 while( rsnItr.hasNext() ) {
2373 RefSrcNode rsnCaller = rsnItr.next();
2375 RefEdge reCaller = new RefEdge( rsnCaller,
2378 reCallee.getField(),
2379 toCallerContext( reCallee.getBeta(),
2380 calleeStatesSatisfied ),
2384 ChangeSet cs = ChangeSet.factory();
2385 Iterator<ReachState> rsItr = reCaller.getBeta().iterator();
2386 while( rsItr.hasNext() ) {
2387 ReachState state = rsItr.next();
2388 ExistPredSet preds2 = state.getPreds();
2389 assert preds2.preds.size() == 1;
2391 if( state.isEmpty() ) {
2395 ExistPred pred = preds2.preds.iterator().next();
2396 ReachState old = pred.ne_state;
2404 cs = Canonical.union( cs,
2405 ChangeTuple.factory( old,
2411 // look to see if an edge with same field exists
2412 // and merge with it, otherwise just add the edge
2413 RefEdge edgeExisting = rsnCaller.getReferenceTo( hrnDstCaller,
2417 if( edgeExisting != null ) {
2418 edgeExisting.setBeta(
2419 Canonical.union( edgeExisting.getBeta(),
2423 edgeExisting.setPreds(
2424 Canonical.join( edgeExisting.getPreds(),
2429 // for reach propagation
2430 if( !cs.isEmpty() ) {
2431 edgePlannedChanges.put(
2433 Canonical.union( edgePlannedChanges.get( edgeExisting ),
2440 addRefEdge( rsnCaller, hrnDstCaller, reCaller );
2442 // for reach propagation
2443 if( !cs.isEmpty() ) {
2444 edgesForPropagation.add( reCaller );
2445 assert !edgePlannedChanges.containsKey( reCaller );
2446 edgePlannedChanges.put( reCaller, cs );
2456 if( writeDebugDOTs ) {
2458 writeGraph( "caller35BeforeAssignReturnValue",
2459 true, // write labels (variables)
2460 true, // selectively hide intermediate temp vars
2461 true, // prune unreachable heap regions
2462 true, // hide subset reachability states
2463 true ); // hide edge taints
2464 } catch( IOException e ) {}
2469 // TODO: WAIT! THIS SHOULD BE MERGED INTO OTHER PARTS, BECAUSE
2470 // AS IT IS WE'RE NOT VERIFYING PREDICATES OF RETURN VALUE
2471 // EDGES, JUST BRINGING THEM ALL! It'll work for now, over approximation
2473 // 3.d) handle return value assignment if needed
2474 TempDescriptor returnTemp = fc.getReturnTemp();
2475 if( returnTemp != null && !returnTemp.getType().isImmutable() ) {
2477 VariableNode vnLhsCaller = getVariableNodeFromTemp( returnTemp );
2478 clearRefEdgesFrom( vnLhsCaller, null, null, true );
2480 VariableNode vnReturnCallee = rgCallee.getVariableNodeFromTemp( tdReturn );
2481 Iterator<RefEdge> reCalleeItr = vnReturnCallee.iteratorToReferencees();
2482 while( reCalleeItr.hasNext() ) {
2483 RefEdge reCallee = reCalleeItr.next();
2484 HeapRegionNode hrnDstCallee = reCallee.getDst();
2486 // some edge types are not possible return values when we can
2487 // see what type variable we are assigning it to
2488 if( !isSuperiorType( returnTemp.getType(), reCallee.getType() ) ) {
2489 System.out.println( "*** NOT EXPECTING TO SEE THIS: Throwing out "+
2490 reCallee+" for return temp "+returnTemp );
2495 AllocSite asDst = hrnDstCallee.getAllocSite();
2496 allocSites.add( asDst );
2498 Integer hrnIDDstShadow = asDst.getShadowIDfromID( hrnDstCallee.getID() );
2500 HeapRegionNode hrnDstCaller = id2hrn.get( hrnIDDstShadow );
2501 if( hrnDstCaller == null ) {
2503 createNewHeapRegionNode( hrnIDDstShadow, // id or null to generate a new one
2504 hrnDstCallee.isSingleObject(), // single object?
2505 hrnDstCallee.isNewSummary(), // summary?
2506 hrnDstCallee.isFlagged(), // flagged?
2507 false, // out-of-context?
2508 hrnDstCallee.getType(), // type
2509 hrnDstCallee.getAllocSite(), // allocation site
2510 toCallerContext( hrnDstCallee.getInherent(),
2511 calleeStatesSatisfied ), // inherent reach
2512 toCallerContext( hrnDstCallee.getAlpha(),
2513 calleeStatesSatisfied ), // current reach
2514 predsTrue, // predicates
2515 hrnDstCallee.getDescription() // description
2518 assert hrnDstCaller.isWiped();
2521 TypeDescriptor tdNewEdge =
2522 mostSpecificType( reCallee.getType(),
2523 hrnDstCallee.getType(),
2524 hrnDstCaller.getType()
2527 RefEdge reCaller = new RefEdge( vnLhsCaller,
2531 toCallerContext( reCallee.getBeta(),
2532 calleeStatesSatisfied ),
2536 addRefEdge( vnLhsCaller, hrnDstCaller, reCaller );
2542 if( writeDebugDOTs ) {
2544 writeGraph( "caller38propagateReach",
2545 true, // write labels (variables)
2546 true, // selectively hide intermediate temp vars
2547 true, // prune unreachable heap regions
2548 true, // hide subset reachability states
2549 true ); // hide edge taints
2550 } catch( IOException e ) {}
2553 // propagate callee reachability changes to the rest
2554 // of the caller graph edges
2555 HashSet<RefEdge> edgesUpdated = new HashSet<RefEdge>();
2557 propagateTokensOverEdges( edgesForPropagation, // source edges
2558 edgePlannedChanges, // map src edge to change set
2559 edgesUpdated ); // list of updated edges
2561 // commit beta' (beta<-betaNew)
2562 Iterator<RefEdge> edgeItr = edgesUpdated.iterator();
2563 while( edgeItr.hasNext() ) {
2564 edgeItr.next().applyBetaNew();
2572 if( writeDebugDOTs ) {
2574 writeGraph( "caller40BeforeShadowMerge",
2575 true, // write labels (variables)
2576 true, // selectively hide intermediate temp vars
2577 true, // prune unreachable heap regions
2578 true, // hide subset reachability states
2579 true ); // hide edge taints
2580 } catch( IOException e ) {}
2584 // 4) merge shadow nodes so alloc sites are back to k
2585 Iterator<AllocSite> asItr = rgCallee.allocSites.iterator();
2586 while( asItr.hasNext() ) {
2587 // for each allocation site do the following to merge
2588 // shadow nodes (newest from callee) with any existing
2589 // look for the newest normal and newest shadow "slot"
2590 // not being used, transfer normal to shadow. Keep
2591 // doing this until there are no more normal nodes, or
2592 // no empty shadow slots: then merge all remaining normal
2593 // nodes into the shadow summary. Finally, convert all
2594 // shadow to their normal versions.
2595 AllocSite as = asItr.next();
2598 while( ageNorm < allocationDepth &&
2599 ageShad < allocationDepth ) {
2601 // first, are there any normal nodes left?
2602 Integer idNorm = as.getIthOldest( ageNorm );
2603 HeapRegionNode hrnNorm = id2hrn.get( idNorm );
2604 if( hrnNorm == null ) {
2605 // no, this age of normal node not in the caller graph
2610 // yes, a normal node exists, is there an empty shadow
2611 // "slot" to transfer it onto?
2612 HeapRegionNode hrnShad = getIthNode( as, ageShad, true );
2613 if( !hrnShad.isWiped() ) {
2614 // no, this age of shadow node is not empty
2619 // yes, this shadow node is empty
2620 transferOnto( hrnNorm, hrnShad );
2625 // now, while there are still normal nodes but no shadow
2626 // slots, merge normal nodes into the shadow summary
2627 while( ageNorm < allocationDepth ) {
2629 // first, are there any normal nodes left?
2630 Integer idNorm = as.getIthOldest( ageNorm );
2631 HeapRegionNode hrnNorm = id2hrn.get( idNorm );
2632 if( hrnNorm == null ) {
2633 // no, this age of normal node not in the caller graph
2638 // yes, a normal node exists, so get the shadow summary
2639 HeapRegionNode summShad = getSummaryNode( as, true );
2640 mergeIntoSummary( hrnNorm, summShad );
2644 // if there is a normal summary, merge it into shadow summary
2645 Integer idNorm = as.getSummary();
2646 HeapRegionNode summNorm = id2hrn.get( idNorm );
2647 if( summNorm != null ) {
2648 HeapRegionNode summShad = getSummaryNode( as, true );
2649 mergeIntoSummary( summNorm, summShad );
2652 // finally, flip all existing shadow nodes onto the normal
2653 for( int i = 0; i < allocationDepth; ++i ) {
2654 Integer idShad = as.getIthOldestShadow( i );
2655 HeapRegionNode hrnShad = id2hrn.get( idShad );
2656 if( hrnShad != null ) {
2658 HeapRegionNode hrnNorm = getIthNode( as, i, false );
2659 assert hrnNorm.isWiped();
2660 transferOnto( hrnShad, hrnNorm );
2664 Integer idShad = as.getSummaryShadow();
2665 HeapRegionNode summShad = id2hrn.get( idShad );
2666 if( summShad != null ) {
2667 summNorm = getSummaryNode( as, false );
2668 transferOnto( summShad, summNorm );
2673 if( writeDebugDOTs ) {
2675 writeGraph( "caller45BeforeUnshadow",
2676 true, // write labels (variables)
2677 true, // selectively hide intermediate temp vars
2678 true, // prune unreachable heap regions
2679 true, // hide subset reachability states
2680 true ); // hide edge taints
2681 } catch( IOException e ) {}
2685 Iterator itrAllHRNodes = id2hrn.entrySet().iterator();
2686 while( itrAllHRNodes.hasNext() ) {
2687 Map.Entry me = (Map.Entry) itrAllHRNodes.next();
2688 HeapRegionNode hrn = (HeapRegionNode) me.getValue();
2690 hrn.setAlpha( unshadow( hrn.getAlpha() ) );
2692 Iterator<RefEdge> itrEdges = hrn.iteratorToReferencers();
2693 while( itrEdges.hasNext() ) {
2694 RefEdge re = itrEdges.next();
2695 re.setBeta( unshadow( re.getBeta() ) );
2701 if( writeDebugDOTs ) {
2703 writeGraph( "caller50BeforeGlobalSweep",
2704 true, // write labels (variables)
2705 true, // selectively hide intermediate temp vars
2706 true, // prune unreachable heap regions
2707 true, // hide subset reachability states
2708 true ); // hide edge taints
2709 } catch( IOException e ) {}
2714 if( !DISABLE_GLOBAL_SWEEP ) {
2720 if( writeDebugDOTs ) {
2722 writeGraph( "caller90AfterTransfer",
2723 true, // write labels (variables)
2724 true, // selectively hide intermediate temp vars
2725 true, // prune unreachable heap regions
2726 true, // hide subset reachability states
2727 true ); // hide edge taints
2728 } catch( IOException e ) {}
2734 ////////////////////////////////////////////////////
2736 // Abstract garbage collection simply removes
2737 // heap region nodes that are not mechanically
2738 // reachable from a root set. This step is
2739 // essential for testing node and edge existence
2740 // predicates efficiently
2742 ////////////////////////////////////////////////////
2743 public void abstractGarbageCollect( Set<TempDescriptor> liveSet ) {
2745 // calculate a root set, will be different for Java
2746 // version of analysis versus Bamboo version
2747 Set<RefSrcNode> toVisit = new HashSet<RefSrcNode>();
2749 // visit every variable in graph while building root
2750 // set, and do iterating on a copy, so we can remove
2751 // dead variables while we're at this
2752 Iterator makeCopyItr = td2vn.entrySet().iterator();
2753 Set entrysCopy = new HashSet();
2754 while( makeCopyItr.hasNext() ) {
2755 entrysCopy.add( makeCopyItr.next() );
2758 Iterator eItr = entrysCopy.iterator();
2759 while( eItr.hasNext() ) {
2760 Map.Entry me = (Map.Entry) eItr.next();
2761 TempDescriptor td = (TempDescriptor) me.getKey();
2762 VariableNode vn = (VariableNode) me.getValue();
2764 if( liveSet.contains( td ) ) {
2768 // dead var, remove completely from graph
2770 clearRefEdgesFrom( vn, null, null, true );
2774 // everything visited in a traversal is
2775 // considered abstractly live
2776 Set<RefSrcNode> visited = new HashSet<RefSrcNode>();
2778 while( !toVisit.isEmpty() ) {
2779 RefSrcNode rsn = toVisit.iterator().next();
2780 toVisit.remove( rsn );
2783 Iterator<RefEdge> hrnItr = rsn.iteratorToReferencees();
2784 while( hrnItr.hasNext() ) {
2785 RefEdge edge = hrnItr.next();
2786 HeapRegionNode hrn = edge.getDst();
2788 if( !visited.contains( hrn ) ) {
2794 // get a copy of the set to iterate over because
2795 // we're going to monkey with the graph when we
2796 // identify a garbage node
2797 Set<HeapRegionNode> hrnAllPrior = new HashSet<HeapRegionNode>();
2798 Iterator<HeapRegionNode> hrnItr = id2hrn.values().iterator();
2799 while( hrnItr.hasNext() ) {
2800 hrnAllPrior.add( hrnItr.next() );
2803 Iterator<HeapRegionNode> hrnAllItr = hrnAllPrior.iterator();
2804 while( hrnAllItr.hasNext() ) {
2805 HeapRegionNode hrn = hrnAllItr.next();
2807 if( !visited.contains( hrn ) ) {
2809 // heap region nodes are compared across ReachGraph
2810 // objects by their integer ID, so when discarding
2811 // garbage nodes we must also discard entries in
2812 // the ID -> heap region hashtable.
2813 id2hrn.remove( hrn.getID() );
2815 // RefEdge objects are two-way linked between
2816 // nodes, so when a node is identified as garbage,
2817 // actively clear references to and from it so
2818 // live nodes won't have dangling RefEdge's
2819 wipeOut( hrn, true );
2821 // if we just removed the last node from an allocation
2822 // site, it should be taken out of the ReachGraph's list
2823 AllocSite as = hrn.getAllocSite();
2824 if( !hasNodesOf( as ) ) {
2825 allocSites.remove( as );
2831 protected boolean hasNodesOf( AllocSite as ) {
2832 if( id2hrn.containsKey( as.getSummary() ) ) {
2836 for( int i = 0; i < allocationDepth; ++i ) {
2837 if( id2hrn.containsKey( as.getIthOldest( i ) ) ) {
2845 ////////////////////////////////////////////////////
2847 // This global sweep is an optional step to prune
2848 // reachability sets that are not internally
2849 // consistent with the global graph. It should be
2850 // invoked after strong updates or method calls.
2852 ////////////////////////////////////////////////////
2853 public void globalSweep() {
2855 // boldB is part of the phase 1 sweep
2856 // it has an in-context table and an out-of-context table
2857 Hashtable< Integer, Hashtable<RefEdge, ReachSet> > boldBic =
2858 new Hashtable< Integer, Hashtable<RefEdge, ReachSet> >();
2860 Hashtable< Integer, Hashtable<RefEdge, ReachSet> > boldBooc =
2861 new Hashtable< Integer, Hashtable<RefEdge, ReachSet> >();
2863 // visit every heap region to initialize alphaNew and betaNew,
2864 // and make a map of every hrnID to the source nodes it should
2865 // propagate forward from. In-context flagged hrnID's propagate
2866 // from only the in-context node they name, but out-of-context
2867 // ID's may propagate from several out-of-context nodes
2868 Hashtable< Integer, Set<HeapRegionNode> > icID2srcs =
2869 new Hashtable< Integer, Set<HeapRegionNode> >();
2871 Hashtable< Integer, Set<HeapRegionNode> > oocID2srcs =
2872 new Hashtable< Integer, Set<HeapRegionNode> >();
2875 Iterator itrHrns = id2hrn.entrySet().iterator();
2876 while( itrHrns.hasNext() ) {
2877 Map.Entry me = (Map.Entry) itrHrns.next();
2878 Integer hrnID = (Integer) me.getKey();
2879 HeapRegionNode hrn = (HeapRegionNode) me.getValue();
2881 // assert that this node and incoming edges have clean alphaNew
2882 // and betaNew sets, respectively
2883 assert rsetEmpty.equals( hrn.getAlphaNew() );
2885 Iterator<RefEdge> itrRers = hrn.iteratorToReferencers();
2886 while( itrRers.hasNext() ) {
2887 RefEdge edge = itrRers.next();
2888 assert rsetEmpty.equals( edge.getBetaNew() );
2891 // calculate boldB for this flagged node, or out-of-context node
2892 if( hrn.isFlagged() ) {
2893 assert !hrn.isOutOfContext();
2894 assert !icID2srcs.containsKey( hrn.getID() );
2895 Set<HeapRegionNode> srcs = new HashSet<HeapRegionNode>();
2897 icID2srcs.put( hrn.getID(), srcs );
2900 if( hrn.isOutOfContext() ) {
2901 assert !hrn.isFlagged();
2903 Iterator<ReachState> stateItr = hrn.getAlpha().iterator();
2904 while( stateItr.hasNext() ) {
2905 ReachState state = stateItr.next();
2907 Iterator<ReachTuple> rtItr = state.iterator();
2908 while( rtItr.hasNext() ) {
2909 ReachTuple rt = rtItr.next();
2910 assert rt.isOutOfContext();
2912 Set<HeapRegionNode> srcs = oocID2srcs.get( rt.getHrnID() );
2913 if( srcs == null ) {
2914 srcs = new HashSet<HeapRegionNode>();
2917 oocID2srcs.put( rt.getHrnID(), srcs );
2923 // calculate boldB for all hrnIDs identified by the above
2924 // node traversal, propagating from every source
2925 while( !icID2srcs.isEmpty() || !oocID2srcs.isEmpty() ) {
2928 Set<HeapRegionNode> srcs;
2931 if( !icID2srcs.isEmpty() ) {
2932 Map.Entry me = (Map.Entry) icID2srcs.entrySet().iterator().next();
2933 hrnID = (Integer) me.getKey();
2934 srcs = (Set<HeapRegionNode>) me.getValue();
2936 icID2srcs.remove( hrnID );
2939 assert !oocID2srcs.isEmpty();
2941 Map.Entry me = (Map.Entry) oocID2srcs.entrySet().iterator().next();
2942 hrnID = (Integer) me.getKey();
2943 srcs = (Set<HeapRegionNode>) me.getValue();
2945 oocID2srcs.remove( hrnID );
2949 Hashtable<RefEdge, ReachSet> boldB_f =
2950 new Hashtable<RefEdge, ReachSet>();
2952 Set<RefEdge> workSetEdges = new HashSet<RefEdge>();
2954 Iterator<HeapRegionNode> hrnItr = srcs.iterator();
2955 while( hrnItr.hasNext() ) {
2956 HeapRegionNode hrn = hrnItr.next();
2958 assert workSetEdges.isEmpty();
2960 // initial boldB_f constraints
2961 Iterator<RefEdge> itrRees = hrn.iteratorToReferencees();
2962 while( itrRees.hasNext() ) {
2963 RefEdge edge = itrRees.next();
2965 assert !boldB_f.containsKey( edge );
2966 boldB_f.put( edge, edge.getBeta() );
2968 assert !workSetEdges.contains( edge );
2969 workSetEdges.add( edge );
2972 // enforce the boldB_f constraint at edges until we reach a fixed point
2973 while( !workSetEdges.isEmpty() ) {
2974 RefEdge edge = workSetEdges.iterator().next();
2975 workSetEdges.remove( edge );
2977 Iterator<RefEdge> itrPrime = edge.getDst().iteratorToReferencees();
2978 while( itrPrime.hasNext() ) {
2979 RefEdge edgePrime = itrPrime.next();
2981 ReachSet prevResult = boldB_f.get( edgePrime );
2982 ReachSet intersection = Canonical.intersection( boldB_f.get( edge ),
2986 if( prevResult == null ||
2987 Canonical.union( prevResult,
2988 intersection ).size() > prevResult.size() ) {
2990 if( prevResult == null ) {
2991 boldB_f.put( edgePrime,
2992 Canonical.union( edgePrime.getBeta(),
2997 boldB_f.put( edgePrime,
2998 Canonical.union( prevResult,
3003 workSetEdges.add( edgePrime );
3010 boldBic.put( hrnID, boldB_f );
3012 boldBooc.put( hrnID, boldB_f );
3017 // use boldB to prune hrnIDs from alpha states that are impossible
3018 // and propagate the differences backwards across edges
3019 HashSet<RefEdge> edgesForPropagation = new HashSet<RefEdge>();
3021 Hashtable<RefEdge, ChangeSet> edgePlannedChanges =
3022 new Hashtable<RefEdge, ChangeSet>();
3025 itrHrns = id2hrn.entrySet().iterator();
3026 while( itrHrns.hasNext() ) {
3027 Map.Entry me = (Map.Entry) itrHrns.next();
3028 Integer hrnID = (Integer) me.getKey();
3029 HeapRegionNode hrn = (HeapRegionNode) me.getValue();
3031 // out-of-context nodes don't participate in the
3032 // global sweep, they serve as sources for the pass
3034 if( hrn.isOutOfContext() ) {
3038 // the inherent states of a region are the exception
3039 // to removal as the global sweep prunes
3040 ReachTuple rtException = ReachTuple.factory( hrnID,
3041 !hrn.isSingleObject(),
3042 ReachTuple.ARITY_ONE,
3043 false // out-of-context
3046 ChangeSet cts = ChangeSet.factory();
3048 // mark hrnIDs for removal
3049 Iterator<ReachState> stateItr = hrn.getAlpha().iterator();
3050 while( stateItr.hasNext() ) {
3051 ReachState stateOld = stateItr.next();
3053 ReachState markedHrnIDs = ReachState.factory();
3055 Iterator<ReachTuple> rtItr = stateOld.iterator();
3056 while( rtItr.hasNext() ) {
3057 ReachTuple rtOld = rtItr.next();
3059 // never remove the inherent hrnID from a flagged region
3060 // because it is trivially satisfied
3061 if( hrn.isFlagged() ) {
3062 if( rtOld == rtException ) {
3067 // does boldB allow this hrnID?
3068 boolean foundState = false;
3069 Iterator<RefEdge> incidentEdgeItr = hrn.iteratorToReferencers();
3070 while( incidentEdgeItr.hasNext() ) {
3071 RefEdge incidentEdge = incidentEdgeItr.next();
3073 Hashtable<RefEdge, ReachSet> B;
3074 if( rtOld.isOutOfContext() ) {
3075 B = boldBooc.get( rtOld.getHrnID() );
3077 assert id2hrn.containsKey( rtOld.getHrnID() );
3078 B = boldBic.get( rtOld.getHrnID() );
3082 ReachSet boldB_rtOld_incident = B.get( incidentEdge );
3083 if( boldB_rtOld_incident != null &&
3084 boldB_rtOld_incident.contains( stateOld ) ) {
3091 markedHrnIDs = Canonical.add( markedHrnIDs, rtOld );
3095 // if there is nothing marked, just move on
3096 if( markedHrnIDs.isEmpty() ) {
3097 hrn.setAlphaNew( Canonical.union( hrn.getAlphaNew(),
3104 // remove all marked hrnIDs and establish a change set that should
3105 // propagate backwards over edges from this node
3106 ReachState statePruned = ReachState.factory();
3107 rtItr = stateOld.iterator();
3108 while( rtItr.hasNext() ) {
3109 ReachTuple rtOld = rtItr.next();
3111 if( !markedHrnIDs.containsTuple( rtOld ) ) {
3112 statePruned = Canonical.union( statePruned, rtOld );
3115 assert !stateOld.equals( statePruned );
3117 hrn.setAlphaNew( Canonical.union( hrn.getAlphaNew(),
3121 ChangeTuple ct = ChangeTuple.factory( stateOld,
3124 cts = Canonical.union( cts, ct );
3127 // throw change tuple set on all incident edges
3128 if( !cts.isEmpty() ) {
3129 Iterator<RefEdge> incidentEdgeItr = hrn.iteratorToReferencers();
3130 while( incidentEdgeItr.hasNext() ) {
3131 RefEdge incidentEdge = incidentEdgeItr.next();
3133 edgesForPropagation.add( incidentEdge );
3135 if( edgePlannedChanges.get( incidentEdge ) == null ) {
3136 edgePlannedChanges.put( incidentEdge, cts );
3138 edgePlannedChanges.put(
3140 Canonical.union( edgePlannedChanges.get( incidentEdge ),
3149 HashSet<RefEdge> edgesUpdated = new HashSet<RefEdge>();
3151 propagateTokensOverEdges( edgesForPropagation,
3155 // at the end of the 1st phase reference edges have
3156 // beta, betaNew that correspond to beta and betaR
3158 // commit beta<-betaNew, so beta=betaR and betaNew
3159 // will represent the beta' calculation in 2nd phase
3161 // commit alpha<-alphaNew because it won't change
3162 HashSet<RefEdge> res = new HashSet<RefEdge>();
3164 Iterator<HeapRegionNode> nodeItr = id2hrn.values().iterator();
3165 while( nodeItr.hasNext() ) {
3166 HeapRegionNode hrn = nodeItr.next();
3167 hrn.applyAlphaNew();
3168 Iterator<RefEdge> itrRes = hrn.iteratorToReferencers();
3169 while( itrRes.hasNext() ) {
3170 res.add( itrRes.next() );
3176 Iterator<RefEdge> edgeItr = res.iterator();
3177 while( edgeItr.hasNext() ) {
3178 RefEdge edge = edgeItr.next();
3179 HeapRegionNode hrn = edge.getDst();
3181 // commit results of last phase
3182 if( edgesUpdated.contains( edge ) ) {
3183 edge.applyBetaNew();
3186 // compute intial condition of 2nd phase
3187 edge.setBetaNew( Canonical.intersection( edge.getBeta(),
3193 // every edge in the graph is the initial workset
3194 Set<RefEdge> edgeWorkSet = (Set) res.clone();
3195 while( !edgeWorkSet.isEmpty() ) {
3196 RefEdge edgePrime = edgeWorkSet.iterator().next();
3197 edgeWorkSet.remove( edgePrime );
3199 RefSrcNode rsn = edgePrime.getSrc();
3200 if( !(rsn instanceof HeapRegionNode) ) {
3203 HeapRegionNode hrn = (HeapRegionNode) rsn;
3205 Iterator<RefEdge> itrEdge = hrn.iteratorToReferencers();
3206 while( itrEdge.hasNext() ) {
3207 RefEdge edge = itrEdge.next();
3209 ReachSet prevResult = edge.getBetaNew();
3210 assert prevResult != null;
3212 ReachSet intersection =
3213 Canonical.intersection( edge.getBeta(),
3214 edgePrime.getBetaNew()
3217 if( Canonical.union( prevResult,
3219 ).size() > prevResult.size() ) {
3221 Canonical.union( prevResult,
3225 edgeWorkSet.add( edge );
3230 // commit beta' (beta<-betaNew)
3231 edgeItr = res.iterator();
3232 while( edgeItr.hasNext() ) {
3233 edgeItr.next().applyBetaNew();
3239 ////////////////////////////////////////////////////
3240 // high-level merge operations
3241 ////////////////////////////////////////////////////
3242 public void merge_sameMethodContext( ReachGraph rg ) {
3243 // when merging two graphs that abstract the heap
3244 // of the same method context, we just call the
3245 // basic merge operation
3249 public void merge_diffMethodContext( ReachGraph rg ) {
3250 // when merging graphs for abstract heaps in
3251 // different method contexts we should:
3252 // 1) age the allocation sites?
3256 ////////////////////////////////////////////////////
3257 // in merge() and equals() methods the suffix A
3258 // represents the passed in graph and the suffix
3259 // B refers to the graph in this object
3260 // Merging means to take the incoming graph A and
3261 // merge it into B, so after the operation graph B
3262 // is the final result.
3263 ////////////////////////////////////////////////////
3264 protected void merge( ReachGraph rg ) {
3271 mergeRefEdges ( rg );
3272 mergeAllocSites( rg );
3275 protected void mergeNodes( ReachGraph rg ) {
3277 // start with heap region nodes
3278 Set sA = rg.id2hrn.entrySet();
3279 Iterator iA = sA.iterator();
3280 while( iA.hasNext() ) {
3281 Map.Entry meA = (Map.Entry) iA.next();
3282 Integer idA = (Integer) meA.getKey();
3283 HeapRegionNode hrnA = (HeapRegionNode) meA.getValue();
3285 // if this graph doesn't have a node the
3286 // incoming graph has, allocate it
3287 if( !id2hrn.containsKey( idA ) ) {
3288 HeapRegionNode hrnB = hrnA.copy();
3289 id2hrn.put( idA, hrnB );
3292 // otherwise this is a node present in both graphs
3293 // so make the new reachability set a union of the
3294 // nodes' reachability sets
3295 HeapRegionNode hrnB = id2hrn.get( idA );
3296 hrnB.setAlpha( Canonical.union( hrnB.getAlpha(),
3301 hrnB.setPreds( Canonical.join( hrnB.getPreds(),
3308 // now add any variable nodes that are in graph B but
3310 sA = rg.td2vn.entrySet();
3312 while( iA.hasNext() ) {
3313 Map.Entry meA = (Map.Entry) iA.next();
3314 TempDescriptor tdA = (TempDescriptor) meA.getKey();
3315 VariableNode lnA = (VariableNode) meA.getValue();
3317 // if the variable doesn't exist in B, allocate and add it
3318 VariableNode lnB = getVariableNodeFromTemp( tdA );
3322 protected void mergeRefEdges( ReachGraph rg ) {
3324 // between heap regions
3325 Set sA = rg.id2hrn.entrySet();
3326 Iterator iA = sA.iterator();
3327 while( iA.hasNext() ) {
3328 Map.Entry meA = (Map.Entry) iA.next();
3329 Integer idA = (Integer) meA.getKey();
3330 HeapRegionNode hrnA = (HeapRegionNode) meA.getValue();
3332 Iterator<RefEdge> heapRegionsItrA = hrnA.iteratorToReferencees();
3333 while( heapRegionsItrA.hasNext() ) {
3334 RefEdge edgeA = heapRegionsItrA.next();
3335 HeapRegionNode hrnChildA = edgeA.getDst();
3336 Integer idChildA = hrnChildA.getID();
3338 // at this point we know an edge in graph A exists
3339 // idA -> idChildA, does this exist in B?
3340 assert id2hrn.containsKey( idA );
3341 HeapRegionNode hrnB = id2hrn.get( idA );
3342 RefEdge edgeToMerge = null;
3344 Iterator<RefEdge> heapRegionsItrB = hrnB.iteratorToReferencees();
3345 while( heapRegionsItrB.hasNext() &&
3346 edgeToMerge == null ) {
3348 RefEdge edgeB = heapRegionsItrB.next();
3349 HeapRegionNode hrnChildB = edgeB.getDst();
3350 Integer idChildB = hrnChildB.getID();
3352 // don't use the RefEdge.equals() here because
3353 // we're talking about existence between graphs,
3354 // not intragraph equal
3355 if( idChildB.equals( idChildA ) &&
3356 edgeB.typeAndFieldEquals( edgeA ) ) {
3358 edgeToMerge = edgeB;
3362 // if the edge from A was not found in B,
3364 if( edgeToMerge == null ) {
3365 assert id2hrn.containsKey( idChildA );
3366 HeapRegionNode hrnChildB = id2hrn.get( idChildA );
3367 edgeToMerge = edgeA.copy();
3368 edgeToMerge.setSrc( hrnB );
3369 edgeToMerge.setDst( hrnChildB );
3370 addRefEdge( hrnB, hrnChildB, edgeToMerge );
3372 // otherwise, the edge already existed in both graphs
3373 // so merge their reachability sets
3375 // just replace this beta set with the union
3376 assert edgeToMerge != null;
3377 edgeToMerge.setBeta(
3378 Canonical.union( edgeToMerge.getBeta(),
3382 edgeToMerge.setPreds(
3383 Canonical.join( edgeToMerge.getPreds(),
3391 // and then again from variable nodes
3392 sA = rg.td2vn.entrySet();
3394 while( iA.hasNext() ) {
3395 Map.Entry meA = (Map.Entry) iA.next();
3396 TempDescriptor tdA = (TempDescriptor) meA.getKey();
3397 VariableNode vnA = (VariableNode) meA.getValue();
3399 Iterator<RefEdge> heapRegionsItrA = vnA.iteratorToReferencees();
3400 while( heapRegionsItrA.hasNext() ) {
3401 RefEdge edgeA = heapRegionsItrA.next();
3402 HeapRegionNode hrnChildA = edgeA.getDst();
3403 Integer idChildA = hrnChildA.getID();
3405 // at this point we know an edge in graph A exists
3406 // tdA -> idChildA, does this exist in B?
3407 assert td2vn.containsKey( tdA );
3408 VariableNode vnB = td2vn.get( tdA );
3409 RefEdge edgeToMerge = null;
3411 Iterator<RefEdge> heapRegionsItrB = vnB.iteratorToReferencees();
3412 while( heapRegionsItrB.hasNext() &&
3413 edgeToMerge == null ) {
3415 RefEdge edgeB = heapRegionsItrB.next();
3416 HeapRegionNode hrnChildB = edgeB.getDst();
3417 Integer idChildB = hrnChildB.getID();
3419 // don't use the RefEdge.equals() here because
3420 // we're talking about existence between graphs
3421 if( idChildB.equals( idChildA ) &&
3422 edgeB.typeAndFieldEquals( edgeA ) ) {
3424 edgeToMerge = edgeB;
3428 // if the edge from A was not found in B,
3430 if( edgeToMerge == null ) {
3431 assert id2hrn.containsKey( idChildA );
3432 HeapRegionNode hrnChildB = id2hrn.get( idChildA );
3433 edgeToMerge = edgeA.copy();
3434 edgeToMerge.setSrc( vnB );
3435 edgeToMerge.setDst( hrnChildB );
3436 addRefEdge( vnB, hrnChildB, edgeToMerge );
3438 // otherwise, the edge already existed in both graphs
3439 // so merge their reachability sets
3441 // just replace this beta set with the union
3442 edgeToMerge.setBeta( Canonical.union( edgeToMerge.getBeta(),
3446 edgeToMerge.setPreds( Canonical.join( edgeToMerge.getPreds(),
3455 protected void mergeAllocSites( ReachGraph rg ) {
3456 allocSites.addAll( rg.allocSites );
3460 // it is necessary in the equals() member functions
3461 // to "check both ways" when comparing the data
3462 // structures of two graphs. For instance, if all
3463 // edges between heap region nodes in graph A are
3464 // present and equal in graph B it is not sufficient
3465 // to say the graphs are equal. Consider that there
3466 // may be edges in graph B that are not in graph A.
3467 // the only way to know that all edges in both graphs
3468 // are equally present is to iterate over both data
3469 // structures and compare against the other graph.
3470 public boolean equals( ReachGraph rg ) {
3476 if( !areHeapRegionNodesEqual( rg ) ) {
3480 if( !areVariableNodesEqual( rg ) ) {
3484 if( !areRefEdgesEqual( rg ) ) {
3488 // if everything is equal up to this point,
3489 // assert that allocSites is also equal--
3490 // this data is redundant but kept for efficiency
3491 assert allocSites.equals( rg.allocSites );
3497 protected boolean areHeapRegionNodesEqual( ReachGraph rg ) {
3499 if( !areallHRNinAalsoinBandequal( this, rg ) ) {
3503 if( !areallHRNinAalsoinBandequal( rg, this ) ) {
3510 static protected boolean areallHRNinAalsoinBandequal( ReachGraph rgA,
3512 Set sA = rgA.id2hrn.entrySet();
3513 Iterator iA = sA.iterator();
3514 while( iA.hasNext() ) {
3515 Map.Entry meA = (Map.Entry) iA.next();
3516 Integer idA = (Integer) meA.getKey();
3517 HeapRegionNode hrnA = (HeapRegionNode) meA.getValue();
3519 if( !rgB.id2hrn.containsKey( idA ) ) {
3523 HeapRegionNode hrnB = rgB.id2hrn.get( idA );
3524 if( !hrnA.equalsIncludingAlphaAndPreds( hrnB ) ) {
3533 protected boolean areVariableNodesEqual( ReachGraph rg ) {
3535 if( !areallVNinAalsoinBandequal( this, rg ) ) {
3539 if( !areallVNinAalsoinBandequal( rg, this ) ) {
3546 static protected boolean areallVNinAalsoinBandequal( ReachGraph rgA,
3548 Set sA = rgA.td2vn.entrySet();
3549 Iterator iA = sA.iterator();
3550 while( iA.hasNext() ) {
3551 Map.Entry meA = (Map.Entry) iA.next();
3552 TempDescriptor tdA = (TempDescriptor) meA.getKey();
3554 if( !rgB.td2vn.containsKey( tdA ) ) {
3563 protected boolean areRefEdgesEqual( ReachGraph rg ) {
3564 if( !areallREinAandBequal( this, rg ) ) {
3571 static protected boolean areallREinAandBequal( ReachGraph rgA,
3574 // check all the heap region->heap region edges
3575 Set sA = rgA.id2hrn.entrySet();
3576 Iterator iA = sA.iterator();
3577 while( iA.hasNext() ) {
3578 Map.Entry meA = (Map.Entry) iA.next();
3579 Integer idA = (Integer) meA.getKey();
3580 HeapRegionNode hrnA = (HeapRegionNode) meA.getValue();
3582 // we should have already checked that the same
3583 // heap regions exist in both graphs
3584 assert rgB.id2hrn.containsKey( idA );
3586 if( !areallREfromAequaltoB( rgA, hrnA, rgB ) ) {
3590 // then check every edge in B for presence in A, starting
3591 // from the same parent HeapRegionNode
3592 HeapRegionNode hrnB = rgB.id2hrn.get( idA );
3594 if( !areallREfromAequaltoB( rgB, hrnB, rgA ) ) {
3599 // then check all the variable->heap region edges
3600 sA = rgA.td2vn.entrySet();
3602 while( iA.hasNext() ) {
3603 Map.Entry meA = (Map.Entry) iA.next();
3604 TempDescriptor tdA = (TempDescriptor) meA.getKey();
3605 VariableNode vnA = (VariableNode) meA.getValue();
3607 // we should have already checked that the same
3608 // label nodes exist in both graphs
3609 assert rgB.td2vn.containsKey( tdA );
3611 if( !areallREfromAequaltoB( rgA, vnA, rgB ) ) {
3615 // then check every edge in B for presence in A, starting
3616 // from the same parent VariableNode
3617 VariableNode vnB = rgB.td2vn.get( tdA );
3619 if( !areallREfromAequaltoB( rgB, vnB, rgA ) ) {
3628 static protected boolean areallREfromAequaltoB( ReachGraph rgA,
3632 Iterator<RefEdge> itrA = rnA.iteratorToReferencees();
3633 while( itrA.hasNext() ) {
3634 RefEdge edgeA = itrA.next();
3635 HeapRegionNode hrnChildA = edgeA.getDst();
3636 Integer idChildA = hrnChildA.getID();
3638 assert rgB.id2hrn.containsKey( idChildA );
3640 // at this point we know an edge in graph A exists
3641 // rnA -> idChildA, does this exact edge exist in B?
3642 boolean edgeFound = false;
3644 RefSrcNode rnB = null;
3645 if( rnA instanceof HeapRegionNode ) {
3646 HeapRegionNode hrnA = (HeapRegionNode) rnA;
3647 rnB = rgB.id2hrn.get( hrnA.getID() );
3649 VariableNode vnA = (VariableNode) rnA;
3650 rnB = rgB.td2vn.get( vnA.getTempDescriptor() );
3653 Iterator<RefEdge> itrB = rnB.iteratorToReferencees();
3654 while( itrB.hasNext() ) {
3655 RefEdge edgeB = itrB.next();
3656 HeapRegionNode hrnChildB = edgeB.getDst();
3657 Integer idChildB = hrnChildB.getID();
3659 if( idChildA.equals( idChildB ) &&
3660 edgeA.typeAndFieldEquals( edgeB ) ) {
3662 // there is an edge in the right place with the right field,
3663 // but do they have the same attributes?
3664 if( edgeA.getBeta().equals( edgeB.getBeta() ) &&
3665 edgeA.equalsPreds( edgeB )
3682 // this analysis no longer has the "match anything"
3683 // type which was represented by null
3684 protected TypeDescriptor mostSpecificType( TypeDescriptor td1,
3685 TypeDescriptor td2 ) {
3689 if( td1.isNull() ) {
3692 if( td2.isNull() ) {
3695 return typeUtil.mostSpecific( td1, td2 );
3698 protected TypeDescriptor mostSpecificType( TypeDescriptor td1,
3700 TypeDescriptor td3 ) {
3702 return mostSpecificType( td1,
3703 mostSpecificType( td2, td3 )
3707 protected TypeDescriptor mostSpecificType( TypeDescriptor td1,
3710 TypeDescriptor td4 ) {
3712 return mostSpecificType( mostSpecificType( td1, td2 ),
3713 mostSpecificType( td3, td4 )
3717 protected boolean isSuperiorType( TypeDescriptor possibleSuper,
3718 TypeDescriptor possibleChild ) {
3719 assert possibleSuper != null;
3720 assert possibleChild != null;
3722 if( possibleSuper.isNull() ||
3723 possibleChild.isNull() ) {
3727 return typeUtil.isSuperorType( possibleSuper, possibleChild );
3731 protected boolean hasMatchingField( HeapRegionNode src,
3734 TypeDescriptor tdSrc = src.getType();
3735 assert tdSrc != null;
3737 if( tdSrc.isArray() ) {
3738 TypeDescriptor td = edge.getType();
3741 TypeDescriptor tdSrcDeref = tdSrc.dereference();
3742 assert tdSrcDeref != null;
3744 if( !typeUtil.isSuperorType( tdSrcDeref, td ) ) {
3748 return edge.getField().equals( DisjointAnalysis.arrayElementFieldName );
3751 // if it's not a class, it doesn't have any fields to match
3752 if( !tdSrc.isClass() ) {
3756 ClassDescriptor cd = tdSrc.getClassDesc();
3757 while( cd != null ) {
3758 Iterator fieldItr = cd.getFields();
3760 while( fieldItr.hasNext() ) {
3761 FieldDescriptor fd = (FieldDescriptor) fieldItr.next();
3763 if( fd.getType().equals( edge.getType() ) &&
3764 fd.getSymbol().equals( edge.getField() ) ) {
3769 cd = cd.getSuperDesc();
3772 // otherwise it is a class with fields
3773 // but we didn't find a match
3777 protected boolean hasMatchingType( RefEdge edge,
3778 HeapRegionNode dst ) {
3780 // if the region has no type, matches everything
3781 TypeDescriptor tdDst = dst.getType();
3782 assert tdDst != null;
3784 // if the type is not a class or an array, don't
3785 // match because primitives are copied, no aliases
3786 ClassDescriptor cdDst = tdDst.getClassDesc();
3787 if( cdDst == null && !tdDst.isArray() ) {
3791 // if the edge type is null, it matches everything
3792 TypeDescriptor tdEdge = edge.getType();
3793 assert tdEdge != null;
3795 return typeUtil.isSuperorType( tdEdge, tdDst );
3800 public void writeGraph( String graphName,
3801 boolean writeLabels,
3802 boolean labelSelect,
3803 boolean pruneGarbage,
3804 boolean hideSubsetReachability,
3805 boolean hideEdgeTaints
3806 ) throws java.io.IOException {
3807 writeGraph( graphName,
3811 hideSubsetReachability,
3816 public void writeGraph( String graphName,
3817 boolean writeLabels,
3818 boolean labelSelect,
3819 boolean pruneGarbage,
3820 boolean hideSubsetReachability,
3821 boolean hideEdgeTaints,
3822 Set<Integer> callerNodeIDsCopiedToCallee
3823 ) throws java.io.IOException {
3825 // remove all non-word characters from the graph name so
3826 // the filename and identifier in dot don't cause errors
3827 graphName = graphName.replaceAll( "[\\W]", "" );
3830 new BufferedWriter( new FileWriter( graphName+".dot" ) );
3832 bw.write( "digraph "+graphName+" {\n" );
3835 // this is an optional step to form the callee-reachable
3836 // "cut-out" into a DOT cluster for visualization
3837 if( callerNodeIDsCopiedToCallee != null ) {
3839 bw.write( " subgraph cluster0 {\n" );
3840 bw.write( " color=blue;\n" );
3842 Iterator i = id2hrn.entrySet().iterator();
3843 while( i.hasNext() ) {
3844 Map.Entry me = (Map.Entry) i.next();
3845 HeapRegionNode hrn = (HeapRegionNode) me.getValue();
3847 if( callerNodeIDsCopiedToCallee.contains( hrn.getID() ) ) {
3848 bw.write( " "+hrn.toString()+
3849 hrn.toStringDOT( hideSubsetReachability )+
3859 Set<HeapRegionNode> visited = new HashSet<HeapRegionNode>();
3861 // then visit every heap region node
3862 Iterator i = id2hrn.entrySet().iterator();
3863 while( i.hasNext() ) {
3864 Map.Entry me = (Map.Entry) i.next();
3865 HeapRegionNode hrn = (HeapRegionNode) me.getValue();
3867 // only visit nodes worth writing out--for instance
3868 // not every node at an allocation is referenced
3869 // (think of it as garbage-collected), etc.
3870 if( !pruneGarbage ||
3871 hrn.isOutOfContext()
3874 if( !visited.contains( hrn ) ) {
3875 traverseHeapRegionNodes( hrn,
3879 hideSubsetReachability,
3881 callerNodeIDsCopiedToCallee );
3886 bw.write( " graphTitle[label=\""+graphName+"\",shape=box];\n" );
3889 // then visit every label node, useful for debugging
3891 i = td2vn.entrySet().iterator();
3892 while( i.hasNext() ) {
3893 Map.Entry me = (Map.Entry) i.next();
3894 VariableNode vn = (VariableNode) me.getValue();
3897 String labelStr = vn.getTempDescriptorString();
3898 if( labelStr.startsWith( "___temp" ) ||
3899 labelStr.startsWith( "___dst" ) ||
3900 labelStr.startsWith( "___srctmp" ) ||
3901 labelStr.startsWith( "___neverused" )
3907 Iterator<RefEdge> heapRegionsItr = vn.iteratorToReferencees();
3908 while( heapRegionsItr.hasNext() ) {
3909 RefEdge edge = heapRegionsItr.next();
3910 HeapRegionNode hrn = edge.getDst();
3912 if( !visited.contains( hrn ) ) {
3913 traverseHeapRegionNodes( hrn,
3917 hideSubsetReachability,
3919 callerNodeIDsCopiedToCallee );
3922 bw.write( " "+vn.toString()+
3923 " -> "+hrn.toString()+
3924 edge.toStringDOT( hideSubsetReachability, "" )+
3934 protected void traverseHeapRegionNodes( HeapRegionNode hrn,
3937 Set<HeapRegionNode> visited,
3938 boolean hideSubsetReachability,
3939 boolean hideEdgeTaints,
3940 Set<Integer> callerNodeIDsCopiedToCallee
3941 ) throws java.io.IOException {
3943 if( visited.contains( hrn ) ) {
3948 // if we're drawing the callee-view subgraph, only
3949 // write out the node info if it hasn't already been
3951 if( callerNodeIDsCopiedToCallee == null ||
3952 !callerNodeIDsCopiedToCallee.contains( hrn.getID() )
3954 bw.write( " "+hrn.toString()+
3955 hrn.toStringDOT( hideSubsetReachability )+
3959 Iterator<RefEdge> childRegionsItr = hrn.iteratorToReferencees();
3960 while( childRegionsItr.hasNext() ) {
3961 RefEdge edge = childRegionsItr.next();
3962 HeapRegionNode hrnChild = edge.getDst();
3964 if( callerNodeIDsCopiedToCallee != null &&
3965 (edge.getSrc() instanceof HeapRegionNode) ) {
3966 HeapRegionNode hrnSrc = (HeapRegionNode) edge.getSrc();
3967 if( callerNodeIDsCopiedToCallee.contains( hrnSrc.getID() ) &&
3968 callerNodeIDsCopiedToCallee.contains( edge.getDst().getID() )
3970 bw.write( " "+hrn.toString()+
3971 " -> "+hrnChild.toString()+
3972 edge.toStringDOT( hideSubsetReachability, ",color=blue" )+
3974 } else if( !callerNodeIDsCopiedToCallee.contains( hrnSrc.getID() ) &&
3975 callerNodeIDsCopiedToCallee.contains( edge.getDst().getID() )
3977 bw.write( " "+hrn.toString()+
3978 " -> "+hrnChild.toString()+
3979 edge.toStringDOT( hideSubsetReachability, ",color=blue,style=dashed" )+
3982 bw.write( " "+hrn.toString()+
3983 " -> "+hrnChild.toString()+
3984 edge.toStringDOT( hideSubsetReachability, "" )+
3988 bw.write( " "+hrn.toString()+
3989 " -> "+hrnChild.toString()+
3990 edge.toStringDOT( hideSubsetReachability, "" )+
3994 traverseHeapRegionNodes( hrnChild,
3998 hideSubsetReachability,
4000 callerNodeIDsCopiedToCallee );