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.unionORpreds( 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.unionORpreds( 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.unionORpreds( 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.unionORpreds( 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().containsIgnorePreds( c.getStateToMatch() )
1086 changesToPass = Canonical.add( changesToPass, c );
1090 if( !changesToPass.isEmpty() ) {
1091 if( !nodePlannedChanges.containsKey( m ) ) {
1092 nodePlannedChanges.put( m, ChangeSet.factory() );
1095 ChangeSet currentChanges = nodePlannedChanges.get( m );
1097 if( !changesToPass.isSubset( currentChanges ) ) {
1099 nodePlannedChanges.put( m,
1100 Canonical.union( currentChanges,
1109 todoNodes.remove( n );
1112 // then apply all of the changes for each node at once
1113 Iterator itrMap = nodePlannedChanges.entrySet().iterator();
1114 while( itrMap.hasNext() ) {
1115 Map.Entry me = (Map.Entry) itrMap.next();
1116 HeapRegionNode n = (HeapRegionNode) me.getKey();
1117 ChangeSet C = (ChangeSet) me.getValue();
1119 // this propagation step is with respect to one change,
1120 // so we capture the full change from the old alpha:
1121 ReachSet localDelta = Canonical.applyChangeSet( n.getAlpha(),
1125 // but this propagation may be only one of many concurrent
1126 // possible changes, so keep a running union with the node's
1127 // partially updated new alpha set
1128 n.setAlphaNew( Canonical.unionORpreds( n.getAlphaNew(),
1133 nodesWithNewAlpha.add( n );
1136 propagateTokensOverEdges( todoEdges,
1143 protected void propagateTokensOverEdges( HashSet <RefEdge> todoEdges,
1144 Hashtable<RefEdge, ChangeSet> edgePlannedChanges,
1145 HashSet <RefEdge> edgesWithNewBeta ) {
1147 // first propagate all change tuples everywhere they can go
1148 while( !todoEdges.isEmpty() ) {
1149 RefEdge edgeE = todoEdges.iterator().next();
1150 todoEdges.remove( edgeE );
1152 if( !edgePlannedChanges.containsKey( edgeE ) ) {
1153 edgePlannedChanges.put( edgeE,
1158 ChangeSet C = edgePlannedChanges.get( edgeE );
1160 ChangeSet changesToPass = ChangeSet.factory();
1162 Iterator<ChangeTuple> itrC = C.iterator();
1163 while( itrC.hasNext() ) {
1164 ChangeTuple c = itrC.next();
1165 if( edgeE.getBeta().containsIgnorePreds( c.getStateToMatch() )
1168 changesToPass = Canonical.add( changesToPass, c );
1172 RefSrcNode rsn = edgeE.getSrc();
1174 if( !changesToPass.isEmpty() && rsn instanceof HeapRegionNode ) {
1175 HeapRegionNode n = (HeapRegionNode) rsn;
1177 Iterator<RefEdge> referItr = n.iteratorToReferencers();
1178 while( referItr.hasNext() ) {
1179 RefEdge edgeF = referItr.next();
1181 if( !edgePlannedChanges.containsKey( edgeF ) ) {
1182 edgePlannedChanges.put( edgeF,
1187 ChangeSet currentChanges = edgePlannedChanges.get( edgeF );
1189 if( !changesToPass.isSubset( currentChanges ) ) {
1190 todoEdges.add( edgeF );
1191 edgePlannedChanges.put( edgeF,
1192 Canonical.union( currentChanges,
1201 // then apply all of the changes for each edge at once
1202 Iterator itrMap = edgePlannedChanges.entrySet().iterator();
1203 while( itrMap.hasNext() ) {
1204 Map.Entry me = (Map.Entry) itrMap.next();
1205 RefEdge e = (RefEdge) me.getKey();
1206 ChangeSet C = (ChangeSet) me.getValue();
1208 // this propagation step is with respect to one change,
1209 // so we capture the full change from the old beta:
1210 ReachSet localDelta =
1211 Canonical.applyChangeSet( e.getBeta(),
1216 // but this propagation may be only one of many concurrent
1217 // possible changes, so keep a running union with the edge's
1218 // partially updated new beta set
1219 e.setBetaNew( Canonical.unionORpreds( e.getBetaNew(),
1224 edgesWithNewBeta.add( e );
1229 // used in makeCalleeView below to decide if there is
1230 // already an appropriate out-of-context edge in a callee
1231 // view graph for merging, or null if a new one will be added
1233 getOutOfContextReferenceTo( HeapRegionNode hrn,
1234 TypeDescriptor srcType,
1235 TypeDescriptor refType,
1237 assert belongsToThis( hrn );
1239 HeapRegionNode hrnInContext = id2hrn.get( hrn.getID() );
1240 if( hrnInContext == null ) {
1244 Iterator<RefEdge> refItr = hrnInContext.iteratorToReferencers();
1245 while( refItr.hasNext() ) {
1246 RefEdge re = refItr.next();
1248 assert belongsToThis( re.getSrc() );
1249 assert belongsToThis( re.getDst() );
1251 if( !(re.getSrc() instanceof HeapRegionNode) ) {
1255 HeapRegionNode hrnSrc = (HeapRegionNode) re.getSrc();
1256 if( !hrnSrc.isOutOfContext() ) {
1260 if( srcType == null ) {
1261 if( hrnSrc.getType() != null ) {
1265 if( !srcType.equals( hrnSrc.getType() ) ) {
1270 if( !re.typeEquals( refType ) ) {
1274 if( !re.fieldEquals( refField ) ) {
1278 // tada! We found it!
1285 // used below to convert a ReachSet to its callee-context
1286 // equivalent with respect to allocation sites in this graph
1287 protected ReachSet toCalleeContext( Set<ReachTuple> oocTuples,
1290 TempDescriptor tdSrc,
1293 TypeDescriptor type,
1295 boolean outOfContext
1297 ReachSet out = ReachSet.factory();
1299 Iterator<ReachState> itr = rs.iterator();
1300 while( itr.hasNext() ) {
1301 ReachState stateCaller = itr.next();
1303 ReachState stateCallee = stateCaller;
1305 Iterator<AllocSite> asItr = allocSites.iterator();
1306 while( asItr.hasNext() ) {
1307 AllocSite as = asItr.next();
1309 ReachState stateNew = ReachState.factory();
1310 Iterator<ReachTuple> rtItr = stateCallee.iterator();
1311 while( rtItr.hasNext() ) {
1312 ReachTuple rt = rtItr.next();
1314 // only translate this tuple if it is in the out-context bag
1315 if( !oocTuples.contains( rt ) ) {
1316 stateNew = Canonical.add( stateNew, rt );
1320 int age = as.getAgeCategory( rt.getHrnID() );
1322 // this is the current mapping, where 0, 1, 2S were allocated
1323 // in the current context, 0?, 1? and 2S? were allocated in a
1324 // previous context, and we're translating to a future context
1336 if( age == AllocSite.AGE_notInThisSite ) {
1337 // things not from the site just go back in
1338 stateNew = Canonical.add( stateNew, rt );
1340 } else if( age == AllocSite.AGE_summary ||
1343 // the in-context summary and all existing out-of-context
1345 stateNew = Canonical.add( stateNew,
1346 ReachTuple.factory( as.getSummary(),
1349 true // out-of-context
1353 // otherwise everything else just goes to an out-of-context
1354 // version, everything else the same
1355 Integer I = as.getAge( rt.getHrnID() );
1358 assert !rt.isMultiObject();
1360 stateNew = Canonical.add( stateNew,
1361 ReachTuple.factory( rt.getHrnID(),
1364 true // out-of-context
1370 stateCallee = stateNew;
1376 if( outOfContext ) {
1380 if( hrnID != null ) {
1381 assert tdSrc == null;
1382 assert hrnSrcID == null;
1383 assert hrnDstID == null;
1384 pred = ExistPred.factory( hrnID,
1387 assert tdSrc != null || hrnSrcID != null;
1388 assert hrnDstID != null;
1389 pred = ExistPred.factory( tdSrc,
1397 preds = ExistPredSet.factory( pred );
1400 stateCallee = Canonical.attach( stateCallee,
1403 out = Canonical.add( out,
1408 assert out.isCanonical();
1412 // used below to convert a ReachSet to its caller-context
1413 // equivalent with respect to allocation sites in this graph
1415 toCallerContext( ReachSet rs,
1416 Hashtable<ReachState, ExistPredSet> calleeStatesSatisfied
1418 ReachSet out = ReachSet.factory();
1420 Iterator<ReachState> itr = rs.iterator();
1421 while( itr.hasNext() ) {
1422 ReachState stateCallee = itr.next();
1424 if( calleeStatesSatisfied.containsKey( stateCallee ) ) {
1426 // starting from one callee state...
1427 ReachSet rsCaller = ReachSet.factory( stateCallee );
1429 // possibly branch it into many states, which any
1430 // allocation site might do, so lots of derived states
1431 Iterator<AllocSite> asItr = allocSites.iterator();
1432 while( asItr.hasNext() ) {
1433 AllocSite as = asItr.next();
1434 rsCaller = Canonical.toCallerContext( rsCaller, as );
1437 // then before adding each derived, now caller-context
1438 // states to the output, attach the appropriate pred
1439 // based on the source callee state
1440 Iterator<ReachState> stateItr = rsCaller.iterator();
1441 while( stateItr.hasNext() ) {
1442 ReachState stateCaller = stateItr.next();
1443 stateCaller = Canonical.attach( stateCaller,
1444 calleeStatesSatisfied.get( stateCallee )
1446 out = Canonical.add( out,
1453 assert out.isCanonical();
1457 // used below to convert a ReachSet to an equivalent
1458 // version with shadow IDs merged into unshadowed IDs
1459 protected ReachSet unshadow( ReachSet rs ) {
1461 Iterator<AllocSite> asItr = allocSites.iterator();
1462 while( asItr.hasNext() ) {
1463 AllocSite as = asItr.next();
1464 out = Canonical.unshadow( out, as );
1466 assert out.isCanonical();
1471 // use this method to make a new reach graph that is
1472 // what heap the FlatMethod callee from the FlatCall
1473 // would start with reaching from its arguments in
1476 makeCalleeView( FlatCall fc,
1477 FlatMethod fmCallee,
1478 Set<Integer> callerNodeIDsCopiedToCallee,
1479 boolean writeDebugDOTs
1483 // first traverse this context to find nodes and edges
1484 // that will be callee-reachable
1485 Set<HeapRegionNode> reachableCallerNodes =
1486 new HashSet<HeapRegionNode>();
1488 // caller edges between callee-reachable nodes
1489 Set<RefEdge> reachableCallerEdges =
1490 new HashSet<RefEdge>();
1492 // caller edges from arg vars, and the matching param index
1493 // because these become a special edge in callee
1494 Hashtable<RefEdge, Integer> reachableCallerArgEdges2paramIndex =
1495 new Hashtable<RefEdge, Integer>();
1497 // caller edges from local vars or callee-unreachable nodes
1498 // (out-of-context sources) to callee-reachable nodes
1499 Set<RefEdge> oocCallerEdges =
1500 new HashSet<RefEdge>();
1503 for( int i = 0; i < fmCallee.numParameters(); ++i ) {
1505 TempDescriptor tdArg = fc.getArgMatchingParamIndex( fmCallee, i );
1506 VariableNode vnArgCaller = this.getVariableNodeFromTemp( tdArg );
1508 Set<RefSrcNode> toVisitInCaller = new HashSet<RefSrcNode>();
1509 Set<RefSrcNode> visitedInCaller = new HashSet<RefSrcNode>();
1511 toVisitInCaller.add( vnArgCaller );
1513 while( !toVisitInCaller.isEmpty() ) {
1514 RefSrcNode rsnCaller = toVisitInCaller.iterator().next();
1515 toVisitInCaller.remove( rsnCaller );
1516 visitedInCaller.add( rsnCaller );
1518 Iterator<RefEdge> itrRefEdges = rsnCaller.iteratorToReferencees();
1519 while( itrRefEdges.hasNext() ) {
1520 RefEdge reCaller = itrRefEdges.next();
1521 HeapRegionNode hrnCaller = reCaller.getDst();
1523 callerNodeIDsCopiedToCallee.add( hrnCaller.getID() );
1524 reachableCallerNodes.add( hrnCaller );
1526 if( reCaller.getSrc() instanceof HeapRegionNode ) {
1527 reachableCallerEdges.add( reCaller );
1529 if( rsnCaller.equals( vnArgCaller ) ) {
1530 reachableCallerArgEdges2paramIndex.put( reCaller, i );
1532 oocCallerEdges.add( reCaller );
1536 if( !visitedInCaller.contains( hrnCaller ) ) {
1537 toVisitInCaller.add( hrnCaller );
1540 } // end edge iteration
1541 } // end visiting heap nodes in caller
1542 } // end iterating over parameters as starting points
1545 // now collect out-of-context reach tuples and
1546 // more out-of-context edges
1547 Set<ReachTuple> oocTuples = new HashSet<ReachTuple>();
1549 Iterator<Integer> itrInContext =
1550 callerNodeIDsCopiedToCallee.iterator();
1551 while( itrInContext.hasNext() ) {
1552 Integer hrnID = itrInContext.next();
1553 HeapRegionNode hrnCallerAndInContext = id2hrn.get( hrnID );
1555 Iterator<RefEdge> itrMightCross =
1556 hrnCallerAndInContext.iteratorToReferencers();
1557 while( itrMightCross.hasNext() ) {
1558 RefEdge edgeMightCross = itrMightCross.next();
1560 RefSrcNode rsnCallerAndOutContext =
1561 edgeMightCross.getSrc();
1563 if( rsnCallerAndOutContext instanceof VariableNode ) {
1564 // variables do not have out-of-context reach states,
1566 oocCallerEdges.add( edgeMightCross );
1570 HeapRegionNode hrnCallerAndOutContext =
1571 (HeapRegionNode) rsnCallerAndOutContext;
1573 // is this source node out-of-context?
1574 if( callerNodeIDsCopiedToCallee.contains( hrnCallerAndOutContext.getID() ) ) {
1575 // no, skip this edge
1580 oocCallerEdges.add( edgeMightCross );
1582 // add all reach tuples on the node to list
1583 // of things that are out-of-context: insight
1584 // if this node is reachable from someting that WAS
1585 // in-context, then this node should already be in-context
1586 Iterator<ReachState> stateItr = hrnCallerAndOutContext.getAlpha().iterator();
1587 while( stateItr.hasNext() ) {
1588 ReachState state = stateItr.next();
1590 Iterator<ReachTuple> rtItr = state.iterator();
1591 while( rtItr.hasNext() ) {
1592 ReachTuple rt = rtItr.next();
1594 oocTuples.add( rt );
1601 // the callee view is a new graph: DON'T MODIFY *THIS* graph
1602 ReachGraph rg = new ReachGraph();
1604 // add nodes to callee graph
1605 Iterator<HeapRegionNode> hrnItr = reachableCallerNodes.iterator();
1606 while( hrnItr.hasNext() ) {
1607 HeapRegionNode hrnCaller = hrnItr.next();
1609 assert callerNodeIDsCopiedToCallee.contains( hrnCaller.getID() );
1610 assert !rg.id2hrn.containsKey( hrnCaller.getID() );
1612 ExistPred pred = ExistPred.factory( hrnCaller.getID(), null );
1613 ExistPredSet preds = ExistPredSet.factory( pred );
1615 rg.createNewHeapRegionNode( hrnCaller.getID(),
1616 hrnCaller.isSingleObject(),
1617 hrnCaller.isNewSummary(),
1618 hrnCaller.isFlagged(),
1619 false, // out-of-context?
1620 hrnCaller.getType(),
1621 hrnCaller.getAllocSite(),
1622 toCalleeContext( oocTuples,
1623 hrnCaller.getInherent(), // in state
1624 hrnCaller.getID(), // node pred
1625 null, null, null, null, null, // edge pred
1626 false ), // ooc pred
1627 toCalleeContext( oocTuples,
1628 hrnCaller.getAlpha(), // in state
1629 hrnCaller.getID(), // node pred
1630 null, null, null, null, null, // edge pred
1631 false ), // ooc pred
1633 hrnCaller.getDescription()
1637 // add param edges to callee graph
1639 reachableCallerArgEdges2paramIndex.entrySet().iterator();
1640 while( argEdges.hasNext() ) {
1641 Map.Entry me = (Map.Entry) argEdges.next();
1642 RefEdge reArg = (RefEdge) me.getKey();
1643 Integer index = (Integer) me.getValue();
1645 TempDescriptor arg = fmCallee.getParameter( index );
1647 VariableNode vnCallee =
1648 rg.getVariableNodeFromTemp( arg );
1650 HeapRegionNode hrnDstCaller = reArg.getDst();
1651 HeapRegionNode hrnDstCallee = rg.id2hrn.get( hrnDstCaller.getID() );
1652 assert hrnDstCallee != null;
1655 ExistPred.factory( arg,
1657 hrnDstCallee.getID(),
1661 false ); // out-of-context
1663 ExistPredSet preds =
1664 ExistPredSet.factory( pred );
1667 new RefEdge( vnCallee,
1671 toCalleeContext( oocTuples,
1672 reArg.getBeta(), // in state
1676 hrnDstCallee.getID(), // edge pred
1677 reArg.getType(), // edge pred
1678 reArg.getField(), // edge pred
1679 false ), // ooc pred
1683 rg.addRefEdge( vnCallee,
1689 // add in-context edges to callee graph
1690 Iterator<RefEdge> reItr = reachableCallerEdges.iterator();
1691 while( reItr.hasNext() ) {
1692 RefEdge reCaller = reItr.next();
1693 RefSrcNode rsnCaller = reCaller.getSrc();
1694 assert rsnCaller instanceof HeapRegionNode;
1695 HeapRegionNode hrnSrcCaller = (HeapRegionNode) rsnCaller;
1696 HeapRegionNode hrnDstCaller = reCaller.getDst();
1698 HeapRegionNode hrnSrcCallee = rg.id2hrn.get( hrnSrcCaller.getID() );
1699 HeapRegionNode hrnDstCallee = rg.id2hrn.get( hrnDstCaller.getID() );
1700 assert hrnSrcCallee != null;
1701 assert hrnDstCallee != null;
1704 ExistPred.factory( null,
1705 hrnSrcCallee.getID(),
1706 hrnDstCallee.getID(),
1708 reCaller.getField(),
1710 false ); // out-of-context
1712 ExistPredSet preds =
1713 ExistPredSet.factory( pred );
1716 new RefEdge( hrnSrcCallee,
1719 reCaller.getField(),
1720 toCalleeContext( oocTuples,
1721 reCaller.getBeta(), // in state
1724 hrnSrcCallee.getID(), // edge pred
1725 hrnDstCallee.getID(), // edge pred
1726 reCaller.getType(), // edge pred
1727 reCaller.getField(), // edge pred
1728 false ), // ooc pred
1732 rg.addRefEdge( hrnSrcCallee,
1738 // add out-of-context edges to callee graph
1739 reItr = oocCallerEdges.iterator();
1740 while( reItr.hasNext() ) {
1741 RefEdge reCaller = reItr.next();
1742 RefSrcNode rsnCaller = reCaller.getSrc();
1743 HeapRegionNode hrnDstCaller = reCaller.getDst();
1744 HeapRegionNode hrnDstCallee = rg.id2hrn.get( hrnDstCaller.getID() );
1745 assert hrnDstCallee != null;
1747 TypeDescriptor oocNodeType;
1749 TempDescriptor oocPredSrcTemp = null;
1750 Integer oocPredSrcID = null;
1752 if( rsnCaller instanceof VariableNode ) {
1753 VariableNode vnCaller = (VariableNode) rsnCaller;
1755 oocReach = rsetEmpty;
1756 oocPredSrcTemp = vnCaller.getTempDescriptor();
1759 HeapRegionNode hrnSrcCaller = (HeapRegionNode) rsnCaller;
1760 assert !callerNodeIDsCopiedToCallee.contains( hrnSrcCaller.getID() );
1761 oocNodeType = hrnSrcCaller.getType();
1762 oocReach = hrnSrcCaller.getAlpha();
1763 oocPredSrcID = hrnSrcCaller.getID();
1767 ExistPred.factory( oocPredSrcTemp,
1769 hrnDstCallee.getID(),
1771 reCaller.getField(),
1773 true ); // out-of-context
1775 ExistPredSet preds =
1776 ExistPredSet.factory( pred );
1778 RefEdge oocEdgeExisting =
1779 rg.getOutOfContextReferenceTo( hrnDstCallee,
1785 if( oocEdgeExisting == null ) {
1786 // for consistency, map one out-of-context "identifier"
1787 // to one heap region node id, otherwise no convergence
1788 String oocid = "oocid"+
1790 hrnDstCallee.getIDString()+
1793 reCaller.getField();
1795 Integer oocHrnID = oocid2hrnid.get( oocid );
1797 HeapRegionNode hrnCalleeAndOutContext;
1799 if( oocHrnID == null ) {
1801 hrnCalleeAndOutContext =
1802 rg.createNewHeapRegionNode( null, // ID
1803 false, // single object?
1804 false, // new summary?
1806 true, // out-of-context?
1808 null, // alloc site, shouldn't be used
1809 toCalleeContext( oocTuples,
1810 oocReach, // in state
1812 null, null, null, null, null, // edge pred
1815 toCalleeContext( oocTuples,
1816 oocReach, // in state
1818 null, null, null, null, null, // edge pred
1825 oocid2hrnid.put( oocid, hrnCalleeAndOutContext.getID() );
1829 // the mapping already exists, so see if node is there
1830 hrnCalleeAndOutContext = rg.id2hrn.get( oocHrnID );
1832 if( hrnCalleeAndOutContext == null ) {
1834 hrnCalleeAndOutContext =
1835 rg.createNewHeapRegionNode( oocHrnID, // ID
1836 false, // single object?
1837 false, // new summary?
1839 true, // out-of-context?
1841 null, // alloc site, shouldn't be used
1842 toCalleeContext( oocTuples,
1843 oocReach, // in state
1845 null, null, null, null, null, // edge pred
1848 toCalleeContext( oocTuples,
1849 oocReach, // in state
1851 null, null, null, null, null, // edge pred
1860 rg.addRefEdge( hrnCalleeAndOutContext,
1862 new RefEdge( hrnCalleeAndOutContext,
1865 reCaller.getField(),
1866 toCalleeContext( oocTuples,
1867 reCaller.getBeta(), // in state
1869 oocPredSrcTemp, // edge pred
1870 oocPredSrcID, // edge pred
1871 hrnDstCaller.getID(), // edge pred
1872 reCaller.getType(), // edge pred
1873 reCaller.getField(), // edge pred
1881 // the out-of-context edge already exists
1882 oocEdgeExisting.setBeta( Canonical.unionORpreds( oocEdgeExisting.getBeta(),
1883 toCalleeContext( oocTuples,
1884 reCaller.getBeta(), // in state
1886 oocPredSrcTemp, // edge pred
1887 oocPredSrcID, // edge pred
1888 hrnDstCaller.getID(), // edge pred
1889 reCaller.getType(), // edge pred
1890 reCaller.getField(), // edge pred
1896 oocEdgeExisting.setPreds( Canonical.join( oocEdgeExisting.getPreds(),
1905 if( writeDebugDOTs ) {
1907 rg.writeGraph( "calleeview",
1908 true, // write labels (variables)
1909 true, // selectively hide intermediate temp vars
1910 true, // prune unreachable heap regions
1911 true, // hide subset reachability states
1912 true ); // hide edge taints
1913 } catch( IOException e ) {}
1919 private static Hashtable<String, Integer> oocid2hrnid =
1920 new Hashtable<String, Integer>();
1925 resolveMethodCall( FlatCall fc,
1926 FlatMethod fmCallee,
1927 ReachGraph rgCallee,
1928 Set<Integer> callerNodeIDsCopiedToCallee,
1929 boolean writeDebugDOTs
1933 if( writeDebugDOTs ) {
1935 rgCallee.writeGraph( "callee",
1936 true, // write labels (variables)
1937 true, // selectively hide intermediate temp vars
1938 true, // prune unreachable heap regions
1939 true, // hide subset reachability states
1940 true ); // hide edge taints
1941 writeGraph( "caller00In",
1942 true, // write labels (variables)
1943 true, // selectively hide intermediate temp vars
1944 true, // prune unreachable heap regions
1945 true, // hide subset reachability states
1946 true, // hide edge taints
1947 callerNodeIDsCopiedToCallee );
1948 } catch( IOException e ) {}
1952 // method call transfer function steps:
1953 // 1. Use current callee-reachable heap (CRH) to test callee
1954 // predicates and mark what will be coming in.
1955 // 2. Wipe CRH out of caller.
1956 // 3. Transplant marked callee parts in:
1957 // a) bring in nodes
1958 // b) bring in callee -> callee edges
1959 // c) resolve out-of-context -> callee edges
1960 // d) assign return value
1961 // 4. Collapse shadow nodes down
1962 // 5. Global sweep it.
1966 // 1. mark what callee elements have satisfied predicates
1967 Hashtable<HeapRegionNode, ExistPredSet> calleeNodesSatisfied =
1968 new Hashtable<HeapRegionNode, ExistPredSet>();
1970 Hashtable<RefEdge, ExistPredSet> calleeEdgesSatisfied =
1971 new Hashtable<RefEdge, ExistPredSet>();
1973 Hashtable<ReachState, ExistPredSet> calleeStatesSatisfied =
1974 new Hashtable<ReachState, ExistPredSet>();
1976 Hashtable< RefEdge, Set<RefSrcNode> > calleeEdges2oocCallerSrcMatches =
1977 new Hashtable< RefEdge, Set<RefSrcNode> >();
1979 Iterator meItr = rgCallee.id2hrn.entrySet().iterator();
1980 while( meItr.hasNext() ) {
1981 Map.Entry me = (Map.Entry) meItr.next();
1982 Integer id = (Integer) me.getKey();
1983 HeapRegionNode hrnCallee = (HeapRegionNode) me.getValue();
1985 // if a callee element's predicates are satisfied then a set
1986 // of CALLER predicates is returned: they are the predicates
1987 // that the callee element moved into the caller context
1988 // should have, and it is inefficient to find this again later
1989 ExistPredSet predsIfSatis =
1990 hrnCallee.getPreds().isSatisfiedBy( this,
1991 callerNodeIDsCopiedToCallee
1993 if( predsIfSatis != null ) {
1994 calleeNodesSatisfied.put( hrnCallee, predsIfSatis );
1996 // otherwise don't bother looking at edges to this node
2000 // since the node is coming over, find out which reach
2001 // states on it should come over, too
2002 Iterator<ReachState> stateItr = hrnCallee.getAlpha().iterator();
2003 while( stateItr.hasNext() ) {
2004 ReachState stateCallee = stateItr.next();
2007 stateCallee.getPreds().isSatisfiedBy( this,
2008 callerNodeIDsCopiedToCallee
2010 if( predsIfSatis != null ) {
2011 calleeStatesSatisfied.put( stateCallee, predsIfSatis );
2015 // then look at edges to the node
2016 Iterator<RefEdge> reItr = hrnCallee.iteratorToReferencers();
2017 while( reItr.hasNext() ) {
2018 RefEdge reCallee = reItr.next();
2019 RefSrcNode rsnCallee = reCallee.getSrc();
2021 // (caller local variables to in-context heap regions)
2022 // have an (out-of-context heap region -> in-context heap region)
2023 // abstraction in the callEE, so its true we never need to
2024 // look at a (var node -> heap region) edge in callee to bring
2025 // those over for the call site transfer. What about (param var->heap region)
2026 // edges in callee? They are dealt with below this loop.
2027 // So, yes, at this point skip (var->region) edges in callee
2028 if( rsnCallee instanceof VariableNode ) {
2032 // first see if the source is out-of-context, and only
2033 // proceed with this edge if we find some caller-context
2035 HeapRegionNode hrnSrcCallee = (HeapRegionNode) rsnCallee;
2036 boolean matchedOutOfContext = false;
2038 if( hrnSrcCallee.isOutOfContext() ) {
2040 assert !calleeEdges2oocCallerSrcMatches.containsKey( reCallee );
2041 Set<RefSrcNode> rsnCallers = new HashSet<RefSrcNode>();
2043 HeapRegionNode hrnDstCaller = this.id2hrn.get( hrnCallee.getID() );
2044 Iterator<RefEdge> reDstItr = hrnDstCaller.iteratorToReferencers();
2045 while( reDstItr.hasNext() ) {
2046 // the edge and field (either possibly null) must match
2047 RefEdge reCaller = reDstItr.next();
2049 if( !reCaller.typeEquals ( reCallee.getType() ) ||
2050 !reCaller.fieldEquals( reCallee.getField() )
2055 RefSrcNode rsnCaller = reCaller.getSrc();
2056 if( rsnCaller instanceof VariableNode ) {
2057 // a variable node matches an OOC region with null type
2058 if( hrnSrcCallee.getType() != null ) {
2063 // otherwise types should match
2064 HeapRegionNode hrnCallerSrc = (HeapRegionNode) rsnCaller;
2065 if( hrnSrcCallee.getType() == null ) {
2066 if( hrnCallerSrc.getType() != null ) {
2070 if( !hrnSrcCallee.getType().equals( hrnCallerSrc.getType() ) ) {
2076 rsnCallers.add( rsnCaller );
2077 matchedOutOfContext = true;
2080 if( !rsnCallers.isEmpty() ) {
2081 calleeEdges2oocCallerSrcMatches.put( reCallee, rsnCallers );
2085 if( hrnSrcCallee.isOutOfContext() &&
2086 !matchedOutOfContext ) {
2091 reCallee.getPreds().isSatisfiedBy( this,
2092 callerNodeIDsCopiedToCallee
2094 if( predsIfSatis != null ) {
2095 calleeEdgesSatisfied.put( reCallee, predsIfSatis );
2097 // since the edge is coming over, find out which reach
2098 // states on it should come over, too
2099 stateItr = reCallee.getBeta().iterator();
2100 while( stateItr.hasNext() ) {
2101 ReachState stateCallee = stateItr.next();
2104 stateCallee.getPreds().isSatisfiedBy( this,
2105 callerNodeIDsCopiedToCallee
2107 if( predsIfSatis != null ) {
2108 calleeStatesSatisfied.put( stateCallee, predsIfSatis );
2117 // test param -> HRN edges, also
2118 for( int i = 0; i < fmCallee.numParameters(); ++i ) {
2120 // parameter defined here is the symbol in the callee
2121 TempDescriptor tdParam = fmCallee.getParameter( i );
2122 VariableNode vnCallee = rgCallee.getVariableNodeFromTemp( tdParam );
2124 Iterator<RefEdge> reItr = vnCallee.iteratorToReferencees();
2125 while( reItr.hasNext() ) {
2126 RefEdge reCallee = reItr.next();
2128 ExistPredSet ifDst =
2129 reCallee.getDst().getPreds().isSatisfiedBy( this,
2130 callerNodeIDsCopiedToCallee
2132 if( ifDst == null ) {
2136 ExistPredSet predsIfSatis =
2137 reCallee.getPreds().isSatisfiedBy( this,
2138 callerNodeIDsCopiedToCallee
2140 if( predsIfSatis != null ) {
2141 calleeEdgesSatisfied.put( reCallee, predsIfSatis );
2143 // since the edge is coming over, find out which reach
2144 // states on it should come over, too
2145 Iterator<ReachState> stateItr = reCallee.getBeta().iterator();
2146 while( stateItr.hasNext() ) {
2147 ReachState stateCallee = stateItr.next();
2150 stateCallee.getPreds().isSatisfiedBy( this,
2151 callerNodeIDsCopiedToCallee
2153 if( predsIfSatis != null ) {
2154 calleeStatesSatisfied.put( stateCallee, predsIfSatis );
2165 if( writeDebugDOTs ) {
2167 writeGraph( "caller20BeforeWipe",
2168 true, // write labels (variables)
2169 true, // selectively hide intermediate temp vars
2170 true, // prune unreachable heap regions
2171 true, // hide subset reachability states
2172 true ); // hide edge taints
2173 } catch( IOException e ) {}
2177 // 2. predicates tested, ok to wipe out caller part
2178 Iterator<Integer> hrnItr = callerNodeIDsCopiedToCallee.iterator();
2179 while( hrnItr.hasNext() ) {
2180 Integer hrnID = hrnItr.next();
2181 HeapRegionNode hrnCaller = id2hrn.get( hrnID );
2182 assert hrnCaller != null;
2184 // when clearing off nodes, also eliminate variable
2186 wipeOut( hrnCaller, true );
2191 if( writeDebugDOTs ) {
2193 writeGraph( "caller30BeforeAddingNodes",
2194 true, // write labels (variables)
2195 true, // selectively hide intermediate temp vars
2196 true, // prune unreachable heap regions
2197 true, // hide subset reachability states
2198 true ); // hide edge taints
2199 } catch( IOException e ) {}
2203 // 3. callee elements with satisfied preds come in, note that
2204 // the mapping of elements satisfied to preds is like this:
2205 // A callee element EE has preds EEp that are satisfied by
2206 // some caller element ER. We bring EE into the caller
2207 // context as ERee with the preds of ER, namely ERp, which
2208 // in the following algorithm is the value in the mapping
2211 Iterator satisItr = calleeNodesSatisfied.entrySet().iterator();
2212 while( satisItr.hasNext() ) {
2213 Map.Entry me = (Map.Entry) satisItr.next();
2214 HeapRegionNode hrnCallee = (HeapRegionNode) me.getKey();
2215 ExistPredSet preds = (ExistPredSet) me.getValue();
2217 // TODO: I think its true that the current implementation uses
2218 // the type of the OOC region and the predicates OF THE EDGE from
2219 // it to link everything up in caller context, so that's why we're
2220 // skipping this... maybe that's a sillier way to do it?
2221 if( hrnCallee.isOutOfContext() ) {
2225 AllocSite as = hrnCallee.getAllocSite();
2226 allocSites.add( as );
2228 Integer hrnIDshadow = as.getShadowIDfromID( hrnCallee.getID() );
2230 HeapRegionNode hrnCaller = id2hrn.get( hrnIDshadow );
2231 if( hrnCaller == null ) {
2233 createNewHeapRegionNode( hrnIDshadow, // id or null to generate a new one
2234 hrnCallee.isSingleObject(), // single object?
2235 hrnCallee.isNewSummary(), // summary?
2236 hrnCallee.isFlagged(), // flagged?
2237 false, // out-of-context?
2238 hrnCallee.getType(), // type
2239 hrnCallee.getAllocSite(), // allocation site
2240 toCallerContext( hrnCallee.getInherent(),
2241 calleeStatesSatisfied ), // inherent reach
2242 null, // current reach
2243 predsEmpty, // predicates
2244 hrnCallee.getDescription() // description
2247 assert hrnCaller.isWiped();
2250 hrnCaller.setAlpha( toCallerContext( hrnCallee.getAlpha(),
2251 calleeStatesSatisfied
2255 hrnCaller.setPreds( preds );
2260 if( writeDebugDOTs ) {
2262 writeGraph( "caller31BeforeAddingEdges",
2263 true, // write labels (variables)
2264 true, // selectively hide intermediate temp vars
2265 true, // prune unreachable heap regions
2266 true, // hide subset reachability states
2267 true ); // hide edge taints
2268 } catch( IOException e ) {}
2272 // set these up during the next procedure so after
2273 // the caller has all of its nodes and edges put
2274 // back together we can propagate the callee's
2275 // reach changes backwards into the caller graph
2276 HashSet<RefEdge> edgesForPropagation = new HashSet<RefEdge>();
2278 Hashtable<RefEdge, ChangeSet> edgePlannedChanges =
2279 new Hashtable<RefEdge, ChangeSet>();
2282 // 3.b) callee -> callee edges AND out-of-context -> callee
2283 satisItr = calleeEdgesSatisfied.entrySet().iterator();
2284 while( satisItr.hasNext() ) {
2285 Map.Entry me = (Map.Entry) satisItr.next();
2286 RefEdge reCallee = (RefEdge) me.getKey();
2287 ExistPredSet preds = (ExistPredSet) me.getValue();
2289 HeapRegionNode hrnDstCallee = reCallee.getDst();
2290 AllocSite asDst = hrnDstCallee.getAllocSite();
2291 allocSites.add( asDst );
2293 Integer hrnIDDstShadow =
2294 asDst.getShadowIDfromID( hrnDstCallee.getID() );
2296 HeapRegionNode hrnDstCaller = id2hrn.get( hrnIDDstShadow );
2297 assert hrnDstCaller != null;
2300 RefSrcNode rsnCallee = reCallee.getSrc();
2302 Set<RefSrcNode> rsnCallers =
2303 new HashSet<RefSrcNode>();
2305 Set<RefSrcNode> oocCallers =
2306 calleeEdges2oocCallerSrcMatches.get( reCallee );
2308 boolean oocEdges = false;
2310 if( oocCallers == null ) {
2311 // there are no out-of-context matches, so it's
2312 // either a param/arg var or one in-context heap region
2313 if( rsnCallee instanceof VariableNode ) {
2314 // variable -> node in the callee should only
2315 // come into the caller if its from a param var
2316 VariableNode vnCallee = (VariableNode) rsnCallee;
2317 TempDescriptor tdParam = vnCallee.getTempDescriptor();
2318 TempDescriptor tdArg = fc.getArgMatchingParam( fmCallee,
2320 if( tdArg == null ) {
2321 // this means the variable isn't a parameter, its local
2322 // to the callee so we ignore it in call site transfer
2323 // shouldn't this NEVER HAPPEN?
2326 rsnCallers.add( this.getVariableNodeFromTemp( tdArg ) );
2330 // otherwise source is in context, one region
2331 HeapRegionNode hrnSrcCallee = (HeapRegionNode) rsnCallee;
2333 // translate an in-context node to shadow
2334 AllocSite asSrc = hrnSrcCallee.getAllocSite();
2335 allocSites.add( asSrc );
2337 Integer hrnIDSrcShadow =
2338 asSrc.getShadowIDfromID( hrnSrcCallee.getID() );
2340 HeapRegionNode hrnSrcCallerShadow =
2341 this.id2hrn.get( hrnIDSrcShadow );
2343 if( hrnSrcCallerShadow == null ) {
2344 hrnSrcCallerShadow =
2345 createNewHeapRegionNode( hrnIDSrcShadow, // id or null to generate a new one
2346 hrnSrcCallee.isSingleObject(), // single object?
2347 hrnSrcCallee.isNewSummary(), // summary?
2348 hrnSrcCallee.isFlagged(), // flagged?
2349 false, // out-of-context?
2350 hrnSrcCallee.getType(), // type
2351 hrnSrcCallee.getAllocSite(), // allocation site
2352 toCallerContext( hrnSrcCallee.getInherent(),
2353 calleeStatesSatisfied ), // inherent reach
2354 toCallerContext( hrnSrcCallee.getAlpha(),
2355 calleeStatesSatisfied ), // current reach
2356 predsEmpty, // predicates
2357 hrnSrcCallee.getDescription() // description
2361 rsnCallers.add( hrnSrcCallerShadow );
2365 // otherwise we have a set of out-of-context srcs
2366 // that should NOT be translated to shadow nodes
2367 assert !oocCallers.isEmpty();
2368 rsnCallers.addAll( oocCallers );
2372 // now make all caller edges we've identified from
2373 // this callee edge with a satisfied predicate
2374 assert !rsnCallers.isEmpty();
2375 Iterator<RefSrcNode> rsnItr = rsnCallers.iterator();
2376 while( rsnItr.hasNext() ) {
2377 RefSrcNode rsnCaller = rsnItr.next();
2379 RefEdge reCaller = new RefEdge( rsnCaller,
2382 reCallee.getField(),
2383 toCallerContext( reCallee.getBeta(),
2384 calleeStatesSatisfied ),
2388 ChangeSet cs = ChangeSet.factory();
2389 Iterator<ReachState> rsItr = reCaller.getBeta().iterator();
2390 while( rsItr.hasNext() ) {
2391 ReachState state = rsItr.next();
2392 ExistPredSet predsPreCallee = state.getPreds();
2394 if( state.isEmpty() ) {
2398 Iterator<ExistPred> predItr = predsPreCallee.iterator();
2399 while( predItr.hasNext() ) {
2400 ExistPred pred = predItr.next();
2401 ReachState old = pred.ne_state;
2407 cs = Canonical.add( cs,
2408 ChangeTuple.factory( old,
2415 // look to see if an edge with same field exists
2416 // and merge with it, otherwise just add the edge
2417 RefEdge edgeExisting = rsnCaller.getReferenceTo( hrnDstCaller,
2421 if( edgeExisting != null ) {
2422 edgeExisting.setBeta(
2423 Canonical.unionORpreds( edgeExisting.getBeta(),
2427 edgeExisting.setPreds(
2428 Canonical.join( edgeExisting.getPreds(),
2433 // for reach propagation
2434 if( !cs.isEmpty() ) {
2435 edgePlannedChanges.put(
2437 Canonical.union( edgePlannedChanges.get( edgeExisting ),
2444 addRefEdge( rsnCaller, hrnDstCaller, reCaller );
2446 // for reach propagation
2447 if( !cs.isEmpty() ) {
2448 edgesForPropagation.add( reCaller );
2449 assert !edgePlannedChanges.containsKey( reCaller );
2450 edgePlannedChanges.put( reCaller, cs );
2460 if( writeDebugDOTs ) {
2462 writeGraph( "caller35BeforeAssignReturnValue",
2463 true, // write labels (variables)
2464 true, // selectively hide intermediate temp vars
2465 true, // prune unreachable heap regions
2466 true, // hide subset reachability states
2467 true ); // hide edge taints
2468 } catch( IOException e ) {}
2473 // TODO: WAIT! THIS SHOULD BE MERGED INTO OTHER PARTS, BECAUSE
2474 // AS IT IS WE'RE NOT VERIFYING PREDICATES OF RETURN VALUE
2475 // EDGES, JUST BRINGING THEM ALL! It'll work for now, over approximation
2477 // 3.d) handle return value assignment if needed
2478 TempDescriptor returnTemp = fc.getReturnTemp();
2479 if( returnTemp != null && !returnTemp.getType().isImmutable() ) {
2481 VariableNode vnLhsCaller = getVariableNodeFromTemp( returnTemp );
2482 clearRefEdgesFrom( vnLhsCaller, null, null, true );
2484 VariableNode vnReturnCallee = rgCallee.getVariableNodeFromTemp( tdReturn );
2485 Iterator<RefEdge> reCalleeItr = vnReturnCallee.iteratorToReferencees();
2486 while( reCalleeItr.hasNext() ) {
2487 RefEdge reCallee = reCalleeItr.next();
2488 HeapRegionNode hrnDstCallee = reCallee.getDst();
2490 // some edge types are not possible return values when we can
2491 // see what type variable we are assigning it to
2492 if( !isSuperiorType( returnTemp.getType(), reCallee.getType() ) ) {
2493 System.out.println( "*** NOT EXPECTING TO SEE THIS: Throwing out "+
2494 reCallee+" for return temp "+returnTemp );
2499 AllocSite asDst = hrnDstCallee.getAllocSite();
2500 allocSites.add( asDst );
2502 Integer hrnIDDstShadow = asDst.getShadowIDfromID( hrnDstCallee.getID() );
2504 HeapRegionNode hrnDstCaller = id2hrn.get( hrnIDDstShadow );
2505 if( hrnDstCaller == null ) {
2507 createNewHeapRegionNode( hrnIDDstShadow, // id or null to generate a new one
2508 hrnDstCallee.isSingleObject(), // single object?
2509 hrnDstCallee.isNewSummary(), // summary?
2510 hrnDstCallee.isFlagged(), // flagged?
2511 false, // out-of-context?
2512 hrnDstCallee.getType(), // type
2513 hrnDstCallee.getAllocSite(), // allocation site
2514 toCallerContext( hrnDstCallee.getInherent(),
2515 calleeStatesSatisfied ), // inherent reach
2516 toCallerContext( hrnDstCallee.getAlpha(),
2517 calleeStatesSatisfied ), // current reach
2518 predsTrue, // predicates
2519 hrnDstCallee.getDescription() // description
2522 assert hrnDstCaller.isWiped();
2525 TypeDescriptor tdNewEdge =
2526 mostSpecificType( reCallee.getType(),
2527 hrnDstCallee.getType(),
2528 hrnDstCaller.getType()
2531 RefEdge reCaller = new RefEdge( vnLhsCaller,
2535 toCallerContext( reCallee.getBeta(),
2536 calleeStatesSatisfied ),
2540 addRefEdge( vnLhsCaller, hrnDstCaller, reCaller );
2546 if( writeDebugDOTs ) {
2548 writeGraph( "caller38propagateReach",
2549 true, // write labels (variables)
2550 true, // selectively hide intermediate temp vars
2551 true, // prune unreachable heap regions
2552 true, // hide subset reachability states
2553 true ); // hide edge taints
2554 } catch( IOException e ) {}
2557 // propagate callee reachability changes to the rest
2558 // of the caller graph edges
2559 HashSet<RefEdge> edgesUpdated = new HashSet<RefEdge>();
2561 propagateTokensOverEdges( edgesForPropagation, // source edges
2562 edgePlannedChanges, // map src edge to change set
2563 edgesUpdated ); // list of updated edges
2565 // commit beta' (beta<-betaNew)
2566 Iterator<RefEdge> edgeItr = edgesUpdated.iterator();
2567 while( edgeItr.hasNext() ) {
2568 edgeItr.next().applyBetaNew();
2576 if( writeDebugDOTs ) {
2578 writeGraph( "caller40BeforeShadowMerge",
2579 true, // write labels (variables)
2580 true, // selectively hide intermediate temp vars
2581 true, // prune unreachable heap regions
2582 true, // hide subset reachability states
2583 true ); // hide edge taints
2584 } catch( IOException e ) {}
2588 // 4) merge shadow nodes so alloc sites are back to k
2589 Iterator<AllocSite> asItr = rgCallee.allocSites.iterator();
2590 while( asItr.hasNext() ) {
2591 // for each allocation site do the following to merge
2592 // shadow nodes (newest from callee) with any existing
2593 // look for the newest normal and newest shadow "slot"
2594 // not being used, transfer normal to shadow. Keep
2595 // doing this until there are no more normal nodes, or
2596 // no empty shadow slots: then merge all remaining normal
2597 // nodes into the shadow summary. Finally, convert all
2598 // shadow to their normal versions.
2599 AllocSite as = asItr.next();
2602 while( ageNorm < allocationDepth &&
2603 ageShad < allocationDepth ) {
2605 // first, are there any normal nodes left?
2606 Integer idNorm = as.getIthOldest( ageNorm );
2607 HeapRegionNode hrnNorm = id2hrn.get( idNorm );
2608 if( hrnNorm == null ) {
2609 // no, this age of normal node not in the caller graph
2614 // yes, a normal node exists, is there an empty shadow
2615 // "slot" to transfer it onto?
2616 HeapRegionNode hrnShad = getIthNode( as, ageShad, true );
2617 if( !hrnShad.isWiped() ) {
2618 // no, this age of shadow node is not empty
2623 // yes, this shadow node is empty
2624 transferOnto( hrnNorm, hrnShad );
2629 // now, while there are still normal nodes but no shadow
2630 // slots, merge normal nodes into the shadow summary
2631 while( ageNorm < allocationDepth ) {
2633 // first, are there any normal nodes left?
2634 Integer idNorm = as.getIthOldest( ageNorm );
2635 HeapRegionNode hrnNorm = id2hrn.get( idNorm );
2636 if( hrnNorm == null ) {
2637 // no, this age of normal node not in the caller graph
2642 // yes, a normal node exists, so get the shadow summary
2643 HeapRegionNode summShad = getSummaryNode( as, true );
2644 mergeIntoSummary( hrnNorm, summShad );
2648 // if there is a normal summary, merge it into shadow summary
2649 Integer idNorm = as.getSummary();
2650 HeapRegionNode summNorm = id2hrn.get( idNorm );
2651 if( summNorm != null ) {
2652 HeapRegionNode summShad = getSummaryNode( as, true );
2653 mergeIntoSummary( summNorm, summShad );
2656 // finally, flip all existing shadow nodes onto the normal
2657 for( int i = 0; i < allocationDepth; ++i ) {
2658 Integer idShad = as.getIthOldestShadow( i );
2659 HeapRegionNode hrnShad = id2hrn.get( idShad );
2660 if( hrnShad != null ) {
2662 HeapRegionNode hrnNorm = getIthNode( as, i, false );
2663 assert hrnNorm.isWiped();
2664 transferOnto( hrnShad, hrnNorm );
2668 Integer idShad = as.getSummaryShadow();
2669 HeapRegionNode summShad = id2hrn.get( idShad );
2670 if( summShad != null ) {
2671 summNorm = getSummaryNode( as, false );
2672 transferOnto( summShad, summNorm );
2677 if( writeDebugDOTs ) {
2679 writeGraph( "caller45BeforeUnshadow",
2680 true, // write labels (variables)
2681 true, // selectively hide intermediate temp vars
2682 true, // prune unreachable heap regions
2683 true, // hide subset reachability states
2684 true ); // hide edge taints
2685 } catch( IOException e ) {}
2689 Iterator itrAllHRNodes = id2hrn.entrySet().iterator();
2690 while( itrAllHRNodes.hasNext() ) {
2691 Map.Entry me = (Map.Entry) itrAllHRNodes.next();
2692 HeapRegionNode hrn = (HeapRegionNode) me.getValue();
2694 hrn.setAlpha( unshadow( hrn.getAlpha() ) );
2696 Iterator<RefEdge> itrEdges = hrn.iteratorToReferencers();
2697 while( itrEdges.hasNext() ) {
2698 RefEdge re = itrEdges.next();
2699 re.setBeta( unshadow( re.getBeta() ) );
2705 if( writeDebugDOTs ) {
2707 writeGraph( "caller50BeforeGlobalSweep",
2708 true, // write labels (variables)
2709 true, // selectively hide intermediate temp vars
2710 true, // prune unreachable heap regions
2711 true, // hide subset reachability states
2712 true ); // hide edge taints
2713 } catch( IOException e ) {}
2718 if( !DISABLE_GLOBAL_SWEEP ) {
2724 if( writeDebugDOTs ) {
2726 writeGraph( "caller90AfterTransfer",
2727 true, // write labels (variables)
2728 true, // selectively hide intermediate temp vars
2729 true, // prune unreachable heap regions
2730 true, // hide subset reachability states
2731 true ); // hide edge taints
2732 } catch( IOException e ) {}
2738 ////////////////////////////////////////////////////
2740 // Abstract garbage collection simply removes
2741 // heap region nodes that are not mechanically
2742 // reachable from a root set. This step is
2743 // essential for testing node and edge existence
2744 // predicates efficiently
2746 ////////////////////////////////////////////////////
2747 public void abstractGarbageCollect( Set<TempDescriptor> liveSet ) {
2749 // calculate a root set, will be different for Java
2750 // version of analysis versus Bamboo version
2751 Set<RefSrcNode> toVisit = new HashSet<RefSrcNode>();
2753 // visit every variable in graph while building root
2754 // set, and do iterating on a copy, so we can remove
2755 // dead variables while we're at this
2756 Iterator makeCopyItr = td2vn.entrySet().iterator();
2757 Set entrysCopy = new HashSet();
2758 while( makeCopyItr.hasNext() ) {
2759 entrysCopy.add( makeCopyItr.next() );
2762 Iterator eItr = entrysCopy.iterator();
2763 while( eItr.hasNext() ) {
2764 Map.Entry me = (Map.Entry) eItr.next();
2765 TempDescriptor td = (TempDescriptor) me.getKey();
2766 VariableNode vn = (VariableNode) me.getValue();
2768 if( liveSet.contains( td ) ) {
2772 // dead var, remove completely from graph
2774 clearRefEdgesFrom( vn, null, null, true );
2778 // everything visited in a traversal is
2779 // considered abstractly live
2780 Set<RefSrcNode> visited = new HashSet<RefSrcNode>();
2782 while( !toVisit.isEmpty() ) {
2783 RefSrcNode rsn = toVisit.iterator().next();
2784 toVisit.remove( rsn );
2787 Iterator<RefEdge> hrnItr = rsn.iteratorToReferencees();
2788 while( hrnItr.hasNext() ) {
2789 RefEdge edge = hrnItr.next();
2790 HeapRegionNode hrn = edge.getDst();
2792 if( !visited.contains( hrn ) ) {
2798 // get a copy of the set to iterate over because
2799 // we're going to monkey with the graph when we
2800 // identify a garbage node
2801 Set<HeapRegionNode> hrnAllPrior = new HashSet<HeapRegionNode>();
2802 Iterator<HeapRegionNode> hrnItr = id2hrn.values().iterator();
2803 while( hrnItr.hasNext() ) {
2804 hrnAllPrior.add( hrnItr.next() );
2807 Iterator<HeapRegionNode> hrnAllItr = hrnAllPrior.iterator();
2808 while( hrnAllItr.hasNext() ) {
2809 HeapRegionNode hrn = hrnAllItr.next();
2811 if( !visited.contains( hrn ) ) {
2813 // heap region nodes are compared across ReachGraph
2814 // objects by their integer ID, so when discarding
2815 // garbage nodes we must also discard entries in
2816 // the ID -> heap region hashtable.
2817 id2hrn.remove( hrn.getID() );
2819 // RefEdge objects are two-way linked between
2820 // nodes, so when a node is identified as garbage,
2821 // actively clear references to and from it so
2822 // live nodes won't have dangling RefEdge's
2823 wipeOut( hrn, true );
2825 // if we just removed the last node from an allocation
2826 // site, it should be taken out of the ReachGraph's list
2827 AllocSite as = hrn.getAllocSite();
2828 if( !hasNodesOf( as ) ) {
2829 allocSites.remove( as );
2835 protected boolean hasNodesOf( AllocSite as ) {
2836 if( id2hrn.containsKey( as.getSummary() ) ) {
2840 for( int i = 0; i < allocationDepth; ++i ) {
2841 if( id2hrn.containsKey( as.getIthOldest( i ) ) ) {
2849 ////////////////////////////////////////////////////
2851 // This global sweep is an optional step to prune
2852 // reachability sets that are not internally
2853 // consistent with the global graph. It should be
2854 // invoked after strong updates or method calls.
2856 ////////////////////////////////////////////////////
2857 public void globalSweep() {
2859 // boldB is part of the phase 1 sweep
2860 // it has an in-context table and an out-of-context table
2861 Hashtable< Integer, Hashtable<RefEdge, ReachSet> > boldBic =
2862 new Hashtable< Integer, Hashtable<RefEdge, ReachSet> >();
2864 Hashtable< Integer, Hashtable<RefEdge, ReachSet> > boldBooc =
2865 new Hashtable< Integer, Hashtable<RefEdge, ReachSet> >();
2867 // visit every heap region to initialize alphaNew and betaNew,
2868 // and make a map of every hrnID to the source nodes it should
2869 // propagate forward from. In-context flagged hrnID's propagate
2870 // from only the in-context node they name, but out-of-context
2871 // ID's may propagate from several out-of-context nodes
2872 Hashtable< Integer, Set<HeapRegionNode> > icID2srcs =
2873 new Hashtable< Integer, Set<HeapRegionNode> >();
2875 Hashtable< Integer, Set<HeapRegionNode> > oocID2srcs =
2876 new Hashtable< Integer, Set<HeapRegionNode> >();
2879 Iterator itrHrns = id2hrn.entrySet().iterator();
2880 while( itrHrns.hasNext() ) {
2881 Map.Entry me = (Map.Entry) itrHrns.next();
2882 Integer hrnID = (Integer) me.getKey();
2883 HeapRegionNode hrn = (HeapRegionNode) me.getValue();
2885 // assert that this node and incoming edges have clean alphaNew
2886 // and betaNew sets, respectively
2887 assert rsetEmpty.equals( hrn.getAlphaNew() );
2889 Iterator<RefEdge> itrRers = hrn.iteratorToReferencers();
2890 while( itrRers.hasNext() ) {
2891 RefEdge edge = itrRers.next();
2892 assert rsetEmpty.equals( edge.getBetaNew() );
2895 // calculate boldB for this flagged node, or out-of-context node
2896 if( hrn.isFlagged() ) {
2897 assert !hrn.isOutOfContext();
2898 assert !icID2srcs.containsKey( hrn.getID() );
2899 Set<HeapRegionNode> srcs = new HashSet<HeapRegionNode>();
2901 icID2srcs.put( hrn.getID(), srcs );
2904 if( hrn.isOutOfContext() ) {
2905 assert !hrn.isFlagged();
2907 Iterator<ReachState> stateItr = hrn.getAlpha().iterator();
2908 while( stateItr.hasNext() ) {
2909 ReachState state = stateItr.next();
2911 Iterator<ReachTuple> rtItr = state.iterator();
2912 while( rtItr.hasNext() ) {
2913 ReachTuple rt = rtItr.next();
2914 assert rt.isOutOfContext();
2916 Set<HeapRegionNode> srcs = oocID2srcs.get( rt.getHrnID() );
2917 if( srcs == null ) {
2918 srcs = new HashSet<HeapRegionNode>();
2921 oocID2srcs.put( rt.getHrnID(), srcs );
2927 // calculate boldB for all hrnIDs identified by the above
2928 // node traversal, propagating from every source
2929 while( !icID2srcs.isEmpty() || !oocID2srcs.isEmpty() ) {
2932 Set<HeapRegionNode> srcs;
2935 if( !icID2srcs.isEmpty() ) {
2936 Map.Entry me = (Map.Entry) icID2srcs.entrySet().iterator().next();
2937 hrnID = (Integer) me.getKey();
2938 srcs = (Set<HeapRegionNode>) me.getValue();
2940 icID2srcs.remove( hrnID );
2943 assert !oocID2srcs.isEmpty();
2945 Map.Entry me = (Map.Entry) oocID2srcs.entrySet().iterator().next();
2946 hrnID = (Integer) me.getKey();
2947 srcs = (Set<HeapRegionNode>) me.getValue();
2949 oocID2srcs.remove( hrnID );
2953 Hashtable<RefEdge, ReachSet> boldB_f =
2954 new Hashtable<RefEdge, ReachSet>();
2956 Set<RefEdge> workSetEdges = new HashSet<RefEdge>();
2958 Iterator<HeapRegionNode> hrnItr = srcs.iterator();
2959 while( hrnItr.hasNext() ) {
2960 HeapRegionNode hrn = hrnItr.next();
2962 assert workSetEdges.isEmpty();
2964 // initial boldB_f constraints
2965 Iterator<RefEdge> itrRees = hrn.iteratorToReferencees();
2966 while( itrRees.hasNext() ) {
2967 RefEdge edge = itrRees.next();
2969 assert !boldB_f.containsKey( edge );
2970 boldB_f.put( edge, edge.getBeta() );
2972 assert !workSetEdges.contains( edge );
2973 workSetEdges.add( edge );
2976 // enforce the boldB_f constraint at edges until we reach a fixed point
2977 while( !workSetEdges.isEmpty() ) {
2978 RefEdge edge = workSetEdges.iterator().next();
2979 workSetEdges.remove( edge );
2981 Iterator<RefEdge> itrPrime = edge.getDst().iteratorToReferencees();
2982 while( itrPrime.hasNext() ) {
2983 RefEdge edgePrime = itrPrime.next();
2985 ReachSet prevResult = boldB_f.get( edgePrime );
2986 ReachSet intersection = Canonical.intersection( boldB_f.get( edge ),
2990 if( prevResult == null ||
2991 Canonical.unionORpreds( prevResult,
2992 intersection ).size()
2996 if( prevResult == null ) {
2997 boldB_f.put( edgePrime,
2998 Canonical.unionORpreds( edgePrime.getBeta(),
3003 boldB_f.put( edgePrime,
3004 Canonical.unionORpreds( prevResult,
3009 workSetEdges.add( edgePrime );
3016 boldBic.put( hrnID, boldB_f );
3018 boldBooc.put( hrnID, boldB_f );
3023 // use boldB to prune hrnIDs from alpha states that are impossible
3024 // and propagate the differences backwards across edges
3025 HashSet<RefEdge> edgesForPropagation = new HashSet<RefEdge>();
3027 Hashtable<RefEdge, ChangeSet> edgePlannedChanges =
3028 new Hashtable<RefEdge, ChangeSet>();
3031 itrHrns = id2hrn.entrySet().iterator();
3032 while( itrHrns.hasNext() ) {
3033 Map.Entry me = (Map.Entry) itrHrns.next();
3034 Integer hrnID = (Integer) me.getKey();
3035 HeapRegionNode hrn = (HeapRegionNode) me.getValue();
3037 // out-of-context nodes don't participate in the
3038 // global sweep, they serve as sources for the pass
3040 if( hrn.isOutOfContext() ) {
3044 // the inherent states of a region are the exception
3045 // to removal as the global sweep prunes
3046 ReachTuple rtException = ReachTuple.factory( hrnID,
3047 !hrn.isSingleObject(),
3048 ReachTuple.ARITY_ONE,
3049 false // out-of-context
3052 ChangeSet cts = ChangeSet.factory();
3054 // mark hrnIDs for removal
3055 Iterator<ReachState> stateItr = hrn.getAlpha().iterator();
3056 while( stateItr.hasNext() ) {
3057 ReachState stateOld = stateItr.next();
3059 ReachState markedHrnIDs = ReachState.factory();
3061 Iterator<ReachTuple> rtItr = stateOld.iterator();
3062 while( rtItr.hasNext() ) {
3063 ReachTuple rtOld = rtItr.next();
3065 // never remove the inherent hrnID from a flagged region
3066 // because it is trivially satisfied
3067 if( hrn.isFlagged() ) {
3068 if( rtOld == rtException ) {
3073 // does boldB allow this hrnID?
3074 boolean foundState = false;
3075 Iterator<RefEdge> incidentEdgeItr = hrn.iteratorToReferencers();
3076 while( incidentEdgeItr.hasNext() ) {
3077 RefEdge incidentEdge = incidentEdgeItr.next();
3079 Hashtable<RefEdge, ReachSet> B;
3080 if( rtOld.isOutOfContext() ) {
3081 B = boldBooc.get( rtOld.getHrnID() );
3083 assert id2hrn.containsKey( rtOld.getHrnID() );
3084 B = boldBic.get( rtOld.getHrnID() );
3088 ReachSet boldB_rtOld_incident = B.get( incidentEdge );
3089 if( boldB_rtOld_incident != null &&
3090 boldB_rtOld_incident.contains( stateOld ) ) {
3097 markedHrnIDs = Canonical.add( markedHrnIDs, rtOld );
3101 // if there is nothing marked, just move on
3102 if( markedHrnIDs.isEmpty() ) {
3103 hrn.setAlphaNew( Canonical.add( hrn.getAlphaNew(),
3110 // remove all marked hrnIDs and establish a change set that should
3111 // propagate backwards over edges from this node
3112 ReachState statePruned = ReachState.factory();
3113 rtItr = stateOld.iterator();
3114 while( rtItr.hasNext() ) {
3115 ReachTuple rtOld = rtItr.next();
3117 if( !markedHrnIDs.containsTuple( rtOld ) ) {
3118 statePruned = Canonical.add( statePruned, rtOld );
3121 assert !stateOld.equals( statePruned );
3123 hrn.setAlphaNew( Canonical.add( hrn.getAlphaNew(),
3127 ChangeTuple ct = ChangeTuple.factory( stateOld,
3130 cts = Canonical.add( cts, ct );
3133 // throw change tuple set on all incident edges
3134 if( !cts.isEmpty() ) {
3135 Iterator<RefEdge> incidentEdgeItr = hrn.iteratorToReferencers();
3136 while( incidentEdgeItr.hasNext() ) {
3137 RefEdge incidentEdge = incidentEdgeItr.next();
3139 edgesForPropagation.add( incidentEdge );
3141 if( edgePlannedChanges.get( incidentEdge ) == null ) {
3142 edgePlannedChanges.put( incidentEdge, cts );
3144 edgePlannedChanges.put(
3146 Canonical.union( edgePlannedChanges.get( incidentEdge ),
3155 HashSet<RefEdge> edgesUpdated = new HashSet<RefEdge>();
3157 propagateTokensOverEdges( edgesForPropagation,
3161 // at the end of the 1st phase reference edges have
3162 // beta, betaNew that correspond to beta and betaR
3164 // commit beta<-betaNew, so beta=betaR and betaNew
3165 // will represent the beta' calculation in 2nd phase
3167 // commit alpha<-alphaNew because it won't change
3168 HashSet<RefEdge> res = new HashSet<RefEdge>();
3170 Iterator<HeapRegionNode> nodeItr = id2hrn.values().iterator();
3171 while( nodeItr.hasNext() ) {
3172 HeapRegionNode hrn = nodeItr.next();
3173 hrn.applyAlphaNew();
3174 Iterator<RefEdge> itrRes = hrn.iteratorToReferencers();
3175 while( itrRes.hasNext() ) {
3176 res.add( itrRes.next() );
3182 Iterator<RefEdge> edgeItr = res.iterator();
3183 while( edgeItr.hasNext() ) {
3184 RefEdge edge = edgeItr.next();
3185 HeapRegionNode hrn = edge.getDst();
3187 // commit results of last phase
3188 if( edgesUpdated.contains( edge ) ) {
3189 edge.applyBetaNew();
3192 // compute intial condition of 2nd phase
3193 edge.setBetaNew( Canonical.intersection( edge.getBeta(),
3199 // every edge in the graph is the initial workset
3200 Set<RefEdge> edgeWorkSet = (Set) res.clone();
3201 while( !edgeWorkSet.isEmpty() ) {
3202 RefEdge edgePrime = edgeWorkSet.iterator().next();
3203 edgeWorkSet.remove( edgePrime );
3205 RefSrcNode rsn = edgePrime.getSrc();
3206 if( !(rsn instanceof HeapRegionNode) ) {
3209 HeapRegionNode hrn = (HeapRegionNode) rsn;
3211 Iterator<RefEdge> itrEdge = hrn.iteratorToReferencers();
3212 while( itrEdge.hasNext() ) {
3213 RefEdge edge = itrEdge.next();
3215 ReachSet prevResult = edge.getBetaNew();
3216 assert prevResult != null;
3218 ReachSet intersection =
3219 Canonical.intersection( edge.getBeta(),
3220 edgePrime.getBetaNew()
3223 if( Canonical.unionORpreds( prevResult,
3230 Canonical.unionORpreds( prevResult,
3234 edgeWorkSet.add( edge );
3239 // commit beta' (beta<-betaNew)
3240 edgeItr = res.iterator();
3241 while( edgeItr.hasNext() ) {
3242 edgeItr.next().applyBetaNew();
3248 ////////////////////////////////////////////////////
3249 // high-level merge operations
3250 ////////////////////////////////////////////////////
3251 public void merge_sameMethodContext( ReachGraph rg ) {
3252 // when merging two graphs that abstract the heap
3253 // of the same method context, we just call the
3254 // basic merge operation
3258 public void merge_diffMethodContext( ReachGraph rg ) {
3259 // when merging graphs for abstract heaps in
3260 // different method contexts we should:
3261 // 1) age the allocation sites?
3265 ////////////////////////////////////////////////////
3266 // in merge() and equals() methods the suffix A
3267 // represents the passed in graph and the suffix
3268 // B refers to the graph in this object
3269 // Merging means to take the incoming graph A and
3270 // merge it into B, so after the operation graph B
3271 // is the final result.
3272 ////////////////////////////////////////////////////
3273 protected void merge( ReachGraph rg ) {
3280 mergeRefEdges ( rg );
3281 mergeAllocSites( rg );
3284 protected void mergeNodes( ReachGraph rg ) {
3286 // start with heap region nodes
3287 Set sA = rg.id2hrn.entrySet();
3288 Iterator iA = sA.iterator();
3289 while( iA.hasNext() ) {
3290 Map.Entry meA = (Map.Entry) iA.next();
3291 Integer idA = (Integer) meA.getKey();
3292 HeapRegionNode hrnA = (HeapRegionNode) meA.getValue();
3294 // if this graph doesn't have a node the
3295 // incoming graph has, allocate it
3296 if( !id2hrn.containsKey( idA ) ) {
3297 HeapRegionNode hrnB = hrnA.copy();
3298 id2hrn.put( idA, hrnB );
3301 // otherwise this is a node present in both graphs
3302 // so make the new reachability set a union of the
3303 // nodes' reachability sets
3304 HeapRegionNode hrnB = id2hrn.get( idA );
3305 hrnB.setAlpha( Canonical.unionORpreds( hrnB.getAlpha(),
3310 hrnB.setPreds( Canonical.join( hrnB.getPreds(),
3317 // now add any variable nodes that are in graph B but
3319 sA = rg.td2vn.entrySet();
3321 while( iA.hasNext() ) {
3322 Map.Entry meA = (Map.Entry) iA.next();
3323 TempDescriptor tdA = (TempDescriptor) meA.getKey();
3324 VariableNode lnA = (VariableNode) meA.getValue();
3326 // if the variable doesn't exist in B, allocate and add it
3327 VariableNode lnB = getVariableNodeFromTemp( tdA );
3331 protected void mergeRefEdges( ReachGraph rg ) {
3333 // between heap regions
3334 Set sA = rg.id2hrn.entrySet();
3335 Iterator iA = sA.iterator();
3336 while( iA.hasNext() ) {
3337 Map.Entry meA = (Map.Entry) iA.next();
3338 Integer idA = (Integer) meA.getKey();
3339 HeapRegionNode hrnA = (HeapRegionNode) meA.getValue();
3341 Iterator<RefEdge> heapRegionsItrA = hrnA.iteratorToReferencees();
3342 while( heapRegionsItrA.hasNext() ) {
3343 RefEdge edgeA = heapRegionsItrA.next();
3344 HeapRegionNode hrnChildA = edgeA.getDst();
3345 Integer idChildA = hrnChildA.getID();
3347 // at this point we know an edge in graph A exists
3348 // idA -> idChildA, does this exist in B?
3349 assert id2hrn.containsKey( idA );
3350 HeapRegionNode hrnB = id2hrn.get( idA );
3351 RefEdge edgeToMerge = null;
3353 Iterator<RefEdge> heapRegionsItrB = hrnB.iteratorToReferencees();
3354 while( heapRegionsItrB.hasNext() &&
3355 edgeToMerge == null ) {
3357 RefEdge edgeB = heapRegionsItrB.next();
3358 HeapRegionNode hrnChildB = edgeB.getDst();
3359 Integer idChildB = hrnChildB.getID();
3361 // don't use the RefEdge.equals() here because
3362 // we're talking about existence between graphs,
3363 // not intragraph equal
3364 if( idChildB.equals( idChildA ) &&
3365 edgeB.typeAndFieldEquals( edgeA ) ) {
3367 edgeToMerge = edgeB;
3371 // if the edge from A was not found in B,
3373 if( edgeToMerge == null ) {
3374 assert id2hrn.containsKey( idChildA );
3375 HeapRegionNode hrnChildB = id2hrn.get( idChildA );
3376 edgeToMerge = edgeA.copy();
3377 edgeToMerge.setSrc( hrnB );
3378 edgeToMerge.setDst( hrnChildB );
3379 addRefEdge( hrnB, hrnChildB, edgeToMerge );
3381 // otherwise, the edge already existed in both graphs
3382 // so merge their reachability sets
3384 // just replace this beta set with the union
3385 assert edgeToMerge != null;
3386 edgeToMerge.setBeta(
3387 Canonical.unionORpreds( edgeToMerge.getBeta(),
3391 edgeToMerge.setPreds(
3392 Canonical.join( edgeToMerge.getPreds(),
3400 // and then again from variable nodes
3401 sA = rg.td2vn.entrySet();
3403 while( iA.hasNext() ) {
3404 Map.Entry meA = (Map.Entry) iA.next();
3405 TempDescriptor tdA = (TempDescriptor) meA.getKey();
3406 VariableNode vnA = (VariableNode) meA.getValue();
3408 Iterator<RefEdge> heapRegionsItrA = vnA.iteratorToReferencees();
3409 while( heapRegionsItrA.hasNext() ) {
3410 RefEdge edgeA = heapRegionsItrA.next();
3411 HeapRegionNode hrnChildA = edgeA.getDst();
3412 Integer idChildA = hrnChildA.getID();
3414 // at this point we know an edge in graph A exists
3415 // tdA -> idChildA, does this exist in B?
3416 assert td2vn.containsKey( tdA );
3417 VariableNode vnB = td2vn.get( tdA );
3418 RefEdge edgeToMerge = null;
3420 Iterator<RefEdge> heapRegionsItrB = vnB.iteratorToReferencees();
3421 while( heapRegionsItrB.hasNext() &&
3422 edgeToMerge == null ) {
3424 RefEdge edgeB = heapRegionsItrB.next();
3425 HeapRegionNode hrnChildB = edgeB.getDst();
3426 Integer idChildB = hrnChildB.getID();
3428 // don't use the RefEdge.equals() here because
3429 // we're talking about existence between graphs
3430 if( idChildB.equals( idChildA ) &&
3431 edgeB.typeAndFieldEquals( edgeA ) ) {
3433 edgeToMerge = edgeB;
3437 // if the edge from A was not found in B,
3439 if( edgeToMerge == null ) {
3440 assert id2hrn.containsKey( idChildA );
3441 HeapRegionNode hrnChildB = id2hrn.get( idChildA );
3442 edgeToMerge = edgeA.copy();
3443 edgeToMerge.setSrc( vnB );
3444 edgeToMerge.setDst( hrnChildB );
3445 addRefEdge( vnB, hrnChildB, edgeToMerge );
3447 // otherwise, the edge already existed in both graphs
3448 // so merge their reachability sets
3450 // just replace this beta set with the union
3451 edgeToMerge.setBeta( Canonical.unionORpreds( edgeToMerge.getBeta(),
3455 edgeToMerge.setPreds( Canonical.join( edgeToMerge.getPreds(),
3464 protected void mergeAllocSites( ReachGraph rg ) {
3465 allocSites.addAll( rg.allocSites );
3469 // it is necessary in the equals() member functions
3470 // to "check both ways" when comparing the data
3471 // structures of two graphs. For instance, if all
3472 // edges between heap region nodes in graph A are
3473 // present and equal in graph B it is not sufficient
3474 // to say the graphs are equal. Consider that there
3475 // may be edges in graph B that are not in graph A.
3476 // the only way to know that all edges in both graphs
3477 // are equally present is to iterate over both data
3478 // structures and compare against the other graph.
3479 public boolean equals( ReachGraph rg ) {
3485 if( !areHeapRegionNodesEqual( rg ) ) {
3489 if( !areVariableNodesEqual( rg ) ) {
3493 if( !areRefEdgesEqual( rg ) ) {
3497 // if everything is equal up to this point,
3498 // assert that allocSites is also equal--
3499 // this data is redundant but kept for efficiency
3500 assert allocSites.equals( rg.allocSites );
3506 protected boolean areHeapRegionNodesEqual( ReachGraph rg ) {
3508 if( !areallHRNinAalsoinBandequal( this, rg ) ) {
3512 if( !areallHRNinAalsoinBandequal( rg, this ) ) {
3519 static protected boolean areallHRNinAalsoinBandequal( ReachGraph rgA,
3521 Set sA = rgA.id2hrn.entrySet();
3522 Iterator iA = sA.iterator();
3523 while( iA.hasNext() ) {
3524 Map.Entry meA = (Map.Entry) iA.next();
3525 Integer idA = (Integer) meA.getKey();
3526 HeapRegionNode hrnA = (HeapRegionNode) meA.getValue();
3528 if( !rgB.id2hrn.containsKey( idA ) ) {
3532 HeapRegionNode hrnB = rgB.id2hrn.get( idA );
3533 if( !hrnA.equalsIncludingAlphaAndPreds( hrnB ) ) {
3542 protected boolean areVariableNodesEqual( ReachGraph rg ) {
3544 if( !areallVNinAalsoinBandequal( this, rg ) ) {
3548 if( !areallVNinAalsoinBandequal( rg, this ) ) {
3555 static protected boolean areallVNinAalsoinBandequal( ReachGraph rgA,
3557 Set sA = rgA.td2vn.entrySet();
3558 Iterator iA = sA.iterator();
3559 while( iA.hasNext() ) {
3560 Map.Entry meA = (Map.Entry) iA.next();
3561 TempDescriptor tdA = (TempDescriptor) meA.getKey();
3563 if( !rgB.td2vn.containsKey( tdA ) ) {
3572 protected boolean areRefEdgesEqual( ReachGraph rg ) {
3573 if( !areallREinAandBequal( this, rg ) ) {
3580 static protected boolean areallREinAandBequal( ReachGraph rgA,
3583 // check all the heap region->heap region edges
3584 Set sA = rgA.id2hrn.entrySet();
3585 Iterator iA = sA.iterator();
3586 while( iA.hasNext() ) {
3587 Map.Entry meA = (Map.Entry) iA.next();
3588 Integer idA = (Integer) meA.getKey();
3589 HeapRegionNode hrnA = (HeapRegionNode) meA.getValue();
3591 // we should have already checked that the same
3592 // heap regions exist in both graphs
3593 assert rgB.id2hrn.containsKey( idA );
3595 if( !areallREfromAequaltoB( rgA, hrnA, rgB ) ) {
3599 // then check every edge in B for presence in A, starting
3600 // from the same parent HeapRegionNode
3601 HeapRegionNode hrnB = rgB.id2hrn.get( idA );
3603 if( !areallREfromAequaltoB( rgB, hrnB, rgA ) ) {
3608 // then check all the variable->heap region edges
3609 sA = rgA.td2vn.entrySet();
3611 while( iA.hasNext() ) {
3612 Map.Entry meA = (Map.Entry) iA.next();
3613 TempDescriptor tdA = (TempDescriptor) meA.getKey();
3614 VariableNode vnA = (VariableNode) meA.getValue();
3616 // we should have already checked that the same
3617 // label nodes exist in both graphs
3618 assert rgB.td2vn.containsKey( tdA );
3620 if( !areallREfromAequaltoB( rgA, vnA, rgB ) ) {
3624 // then check every edge in B for presence in A, starting
3625 // from the same parent VariableNode
3626 VariableNode vnB = rgB.td2vn.get( tdA );
3628 if( !areallREfromAequaltoB( rgB, vnB, rgA ) ) {
3637 static protected boolean areallREfromAequaltoB( ReachGraph rgA,
3641 Iterator<RefEdge> itrA = rnA.iteratorToReferencees();
3642 while( itrA.hasNext() ) {
3643 RefEdge edgeA = itrA.next();
3644 HeapRegionNode hrnChildA = edgeA.getDst();
3645 Integer idChildA = hrnChildA.getID();
3647 assert rgB.id2hrn.containsKey( idChildA );
3649 // at this point we know an edge in graph A exists
3650 // rnA -> idChildA, does this exact edge exist in B?
3651 boolean edgeFound = false;
3653 RefSrcNode rnB = null;
3654 if( rnA instanceof HeapRegionNode ) {
3655 HeapRegionNode hrnA = (HeapRegionNode) rnA;
3656 rnB = rgB.id2hrn.get( hrnA.getID() );
3658 VariableNode vnA = (VariableNode) rnA;
3659 rnB = rgB.td2vn.get( vnA.getTempDescriptor() );
3662 Iterator<RefEdge> itrB = rnB.iteratorToReferencees();
3663 while( itrB.hasNext() ) {
3664 RefEdge edgeB = itrB.next();
3665 HeapRegionNode hrnChildB = edgeB.getDst();
3666 Integer idChildB = hrnChildB.getID();
3668 if( idChildA.equals( idChildB ) &&
3669 edgeA.typeAndFieldEquals( edgeB ) ) {
3671 // there is an edge in the right place with the right field,
3672 // but do they have the same attributes?
3673 if( edgeA.getBeta().equals( edgeB.getBeta() ) &&
3674 edgeA.equalsPreds( edgeB )
3691 // this analysis no longer has the "match anything"
3692 // type which was represented by null
3693 protected TypeDescriptor mostSpecificType( TypeDescriptor td1,
3694 TypeDescriptor td2 ) {
3698 if( td1.isNull() ) {
3701 if( td2.isNull() ) {
3704 return typeUtil.mostSpecific( td1, td2 );
3707 protected TypeDescriptor mostSpecificType( TypeDescriptor td1,
3709 TypeDescriptor td3 ) {
3711 return mostSpecificType( td1,
3712 mostSpecificType( td2, td3 )
3716 protected TypeDescriptor mostSpecificType( TypeDescriptor td1,
3719 TypeDescriptor td4 ) {
3721 return mostSpecificType( mostSpecificType( td1, td2 ),
3722 mostSpecificType( td3, td4 )
3726 protected boolean isSuperiorType( TypeDescriptor possibleSuper,
3727 TypeDescriptor possibleChild ) {
3728 assert possibleSuper != null;
3729 assert possibleChild != null;
3731 if( possibleSuper.isNull() ||
3732 possibleChild.isNull() ) {
3736 return typeUtil.isSuperorType( possibleSuper, possibleChild );
3740 protected boolean hasMatchingField( HeapRegionNode src,
3743 TypeDescriptor tdSrc = src.getType();
3744 assert tdSrc != null;
3746 if( tdSrc.isArray() ) {
3747 TypeDescriptor td = edge.getType();
3750 TypeDescriptor tdSrcDeref = tdSrc.dereference();
3751 assert tdSrcDeref != null;
3753 if( !typeUtil.isSuperorType( tdSrcDeref, td ) ) {
3757 return edge.getField().equals( DisjointAnalysis.arrayElementFieldName );
3760 // if it's not a class, it doesn't have any fields to match
3761 if( !tdSrc.isClass() ) {
3765 ClassDescriptor cd = tdSrc.getClassDesc();
3766 while( cd != null ) {
3767 Iterator fieldItr = cd.getFields();
3769 while( fieldItr.hasNext() ) {
3770 FieldDescriptor fd = (FieldDescriptor) fieldItr.next();
3772 if( fd.getType().equals( edge.getType() ) &&
3773 fd.getSymbol().equals( edge.getField() ) ) {
3778 cd = cd.getSuperDesc();
3781 // otherwise it is a class with fields
3782 // but we didn't find a match
3786 protected boolean hasMatchingType( RefEdge edge,
3787 HeapRegionNode dst ) {
3789 // if the region has no type, matches everything
3790 TypeDescriptor tdDst = dst.getType();
3791 assert tdDst != null;
3793 // if the type is not a class or an array, don't
3794 // match because primitives are copied, no aliases
3795 ClassDescriptor cdDst = tdDst.getClassDesc();
3796 if( cdDst == null && !tdDst.isArray() ) {
3800 // if the edge type is null, it matches everything
3801 TypeDescriptor tdEdge = edge.getType();
3802 assert tdEdge != null;
3804 return typeUtil.isSuperorType( tdEdge, tdDst );
3809 public void writeGraph( String graphName,
3810 boolean writeLabels,
3811 boolean labelSelect,
3812 boolean pruneGarbage,
3813 boolean hideSubsetReachability,
3814 boolean hideEdgeTaints
3815 ) throws java.io.IOException {
3816 writeGraph( graphName,
3820 hideSubsetReachability,
3825 public void writeGraph( String graphName,
3826 boolean writeLabels,
3827 boolean labelSelect,
3828 boolean pruneGarbage,
3829 boolean hideSubsetReachability,
3830 boolean hideEdgeTaints,
3831 Set<Integer> callerNodeIDsCopiedToCallee
3832 ) throws java.io.IOException {
3834 // remove all non-word characters from the graph name so
3835 // the filename and identifier in dot don't cause errors
3836 graphName = graphName.replaceAll( "[\\W]", "" );
3839 new BufferedWriter( new FileWriter( graphName+".dot" ) );
3841 bw.write( "digraph "+graphName+" {\n" );
3844 // this is an optional step to form the callee-reachable
3845 // "cut-out" into a DOT cluster for visualization
3846 if( callerNodeIDsCopiedToCallee != null ) {
3848 bw.write( " subgraph cluster0 {\n" );
3849 bw.write( " color=blue;\n" );
3851 Iterator i = id2hrn.entrySet().iterator();
3852 while( i.hasNext() ) {
3853 Map.Entry me = (Map.Entry) i.next();
3854 HeapRegionNode hrn = (HeapRegionNode) me.getValue();
3856 if( callerNodeIDsCopiedToCallee.contains( hrn.getID() ) ) {
3857 bw.write( " "+hrn.toString()+
3858 hrn.toStringDOT( hideSubsetReachability )+
3868 Set<HeapRegionNode> visited = new HashSet<HeapRegionNode>();
3870 // then visit every heap region node
3871 Iterator i = id2hrn.entrySet().iterator();
3872 while( i.hasNext() ) {
3873 Map.Entry me = (Map.Entry) i.next();
3874 HeapRegionNode hrn = (HeapRegionNode) me.getValue();
3876 // only visit nodes worth writing out--for instance
3877 // not every node at an allocation is referenced
3878 // (think of it as garbage-collected), etc.
3879 if( !pruneGarbage ||
3880 hrn.isOutOfContext()
3883 if( !visited.contains( hrn ) ) {
3884 traverseHeapRegionNodes( hrn,
3888 hideSubsetReachability,
3890 callerNodeIDsCopiedToCallee );
3895 bw.write( " graphTitle[label=\""+graphName+"\",shape=box];\n" );
3898 // then visit every label node, useful for debugging
3900 i = td2vn.entrySet().iterator();
3901 while( i.hasNext() ) {
3902 Map.Entry me = (Map.Entry) i.next();
3903 VariableNode vn = (VariableNode) me.getValue();
3906 String labelStr = vn.getTempDescriptorString();
3907 if( labelStr.startsWith( "___temp" ) ||
3908 labelStr.startsWith( "___dst" ) ||
3909 labelStr.startsWith( "___srctmp" ) ||
3910 labelStr.startsWith( "___neverused" )
3916 Iterator<RefEdge> heapRegionsItr = vn.iteratorToReferencees();
3917 while( heapRegionsItr.hasNext() ) {
3918 RefEdge edge = heapRegionsItr.next();
3919 HeapRegionNode hrn = edge.getDst();
3921 if( !visited.contains( hrn ) ) {
3922 traverseHeapRegionNodes( hrn,
3926 hideSubsetReachability,
3928 callerNodeIDsCopiedToCallee );
3931 bw.write( " "+vn.toString()+
3932 " -> "+hrn.toString()+
3933 edge.toStringDOT( hideSubsetReachability, "" )+
3943 protected void traverseHeapRegionNodes( HeapRegionNode hrn,
3946 Set<HeapRegionNode> visited,
3947 boolean hideSubsetReachability,
3948 boolean hideEdgeTaints,
3949 Set<Integer> callerNodeIDsCopiedToCallee
3950 ) throws java.io.IOException {
3952 if( visited.contains( hrn ) ) {
3957 // if we're drawing the callee-view subgraph, only
3958 // write out the node info if it hasn't already been
3960 if( callerNodeIDsCopiedToCallee == null ||
3961 !callerNodeIDsCopiedToCallee.contains( hrn.getID() )
3963 bw.write( " "+hrn.toString()+
3964 hrn.toStringDOT( hideSubsetReachability )+
3968 Iterator<RefEdge> childRegionsItr = hrn.iteratorToReferencees();
3969 while( childRegionsItr.hasNext() ) {
3970 RefEdge edge = childRegionsItr.next();
3971 HeapRegionNode hrnChild = edge.getDst();
3973 if( callerNodeIDsCopiedToCallee != null &&
3974 (edge.getSrc() instanceof HeapRegionNode) ) {
3975 HeapRegionNode hrnSrc = (HeapRegionNode) edge.getSrc();
3976 if( callerNodeIDsCopiedToCallee.contains( hrnSrc.getID() ) &&
3977 callerNodeIDsCopiedToCallee.contains( edge.getDst().getID() )
3979 bw.write( " "+hrn.toString()+
3980 " -> "+hrnChild.toString()+
3981 edge.toStringDOT( hideSubsetReachability, ",color=blue" )+
3983 } else if( !callerNodeIDsCopiedToCallee.contains( hrnSrc.getID() ) &&
3984 callerNodeIDsCopiedToCallee.contains( edge.getDst().getID() )
3986 bw.write( " "+hrn.toString()+
3987 " -> "+hrnChild.toString()+
3988 edge.toStringDOT( hideSubsetReachability, ",color=blue,style=dashed" )+
3991 bw.write( " "+hrn.toString()+
3992 " -> "+hrnChild.toString()+
3993 edge.toStringDOT( hideSubsetReachability, "" )+
3997 bw.write( " "+hrn.toString()+
3998 " -> "+hrnChild.toString()+
3999 edge.toStringDOT( hideSubsetReachability, "" )+
4003 traverseHeapRegionNodes( hrnChild,
4007 hideSubsetReachability,
4009 callerNodeIDsCopiedToCallee );
4013 public Set<HeapRegionNode> findCommonReachableNodes(HeapRegionNode hrn1,
4014 HeapRegionNode hrn2) {
4016 Set<HeapRegionNode> reachableNodes1 = new HashSet<HeapRegionNode>();
4017 Set<HeapRegionNode> reachableNodes2 = new HashSet<HeapRegionNode>();
4019 Set<HeapRegionNode> todoNodes1 = new HashSet<HeapRegionNode>();
4020 todoNodes1.add(hrn1);
4022 Set<HeapRegionNode> todoNodes2 = new HashSet<HeapRegionNode>();
4023 todoNodes2.add(hrn2);
4025 // follow links until all reachable nodes have been found
4026 while (!todoNodes1.isEmpty()) {
4027 HeapRegionNode hrn = todoNodes1.iterator().next();
4028 todoNodes1.remove(hrn);
4029 reachableNodes1.add(hrn);
4031 Iterator<RefEdge> edgeItr = hrn.iteratorToReferencees();
4032 while (edgeItr.hasNext()) {
4033 RefEdge edge = edgeItr.next();
4035 if (!reachableNodes1.contains(edge.getDst())) {
4036 todoNodes1.add(edge.getDst());
4041 while (!todoNodes2.isEmpty()) {
4042 HeapRegionNode hrn = todoNodes2.iterator().next();
4043 todoNodes2.remove(hrn);
4044 reachableNodes2.add(hrn);
4046 Iterator<RefEdge> edgeItr = hrn.iteratorToReferencees();
4047 while (edgeItr.hasNext()) {
4048 RefEdge edge = edgeItr.next();
4050 if (!reachableNodes2.contains(edge.getDst())) {
4051 todoNodes2.add(edge.getDst());
4056 Set<HeapRegionNode> intersection =
4057 new HashSet<HeapRegionNode>( reachableNodes1 );
4059 intersection.retainAll( reachableNodes2 );
4061 return intersection;
4064 public Set<HeapRegionNode> mayReachSharedObjects(HeapRegionNode hrn1,
4065 HeapRegionNode hrn2) {
4066 assert hrn1 != null;
4067 assert hrn2 != null;
4069 // then get the various tokens for these heap regions
4070 ReachTuple h1 = ReachTuple.factory(hrn1.getID(),
4071 !hrn1.isSingleObject(), ReachTuple.ARITY_ONE, false);
4073 ReachTuple h1plus = ReachTuple.factory(hrn1.getID(), !hrn1
4074 .isSingleObject(), ReachTuple.ARITY_ONEORMORE, false);
4076 ReachTuple h1star = ReachTuple.factory(hrn1.getID(), !hrn1
4077 .isSingleObject(), ReachTuple.ARITY_ZEROORMORE, false);
4079 ReachTuple h2 = ReachTuple.factory(hrn2.getID(),
4080 !hrn2.isSingleObject(), ReachTuple.ARITY_ONE, false);
4082 ReachTuple h2plus = ReachTuple.factory(hrn2.getID(), !hrn2
4083 .isSingleObject(), ReachTuple.ARITY_ONEORMORE, false);
4085 ReachTuple h2star = ReachTuple.factory(hrn2.getID(), !hrn2
4086 .isSingleObject(), ReachTuple.ARITY_ZEROORMORE, false);
4088 // then get the merged beta of all out-going edges from these heap
4091 ReachSet beta1 = ReachSet.factory();
4092 Iterator<RefEdge> itrEdge = hrn1.iteratorToReferencees();
4093 while (itrEdge.hasNext()) {
4094 RefEdge edge = itrEdge.next();
4095 beta1 = Canonical.unionORpreds(beta1, edge.getBeta());
4098 ReachSet beta2 = ReachSet.factory();
4099 itrEdge = hrn2.iteratorToReferencees();
4100 while (itrEdge.hasNext()) {
4101 RefEdge edge = itrEdge.next();
4102 beta2 = Canonical.unionORpreds(beta2, edge.getBeta());
4105 boolean aliasDetected = false;
4107 // only do this one if they are different tokens
4108 if (h1 != h2 && beta1.containsStateWithBoth(h1, h2)) {
4109 aliasDetected = true;
4111 if (beta1.containsStateWithBoth(h1plus, h2)) {
4112 aliasDetected = true;
4114 if (beta1.containsStateWithBoth(h1star, h2)) {
4115 aliasDetected = true;
4117 if (beta1.containsStateWithBoth(h1, h2plus)) {
4118 aliasDetected = true;
4120 if (beta1.containsStateWithBoth(h1plus, h2plus)) {
4121 aliasDetected = true;
4123 if (beta1.containsStateWithBoth(h1star, h2plus)) {
4124 aliasDetected = true;
4126 if (beta1.containsStateWithBoth(h1, h2star)) {
4127 aliasDetected = true;
4129 if (beta1.containsStateWithBoth(h1plus, h2star)) {
4130 aliasDetected = true;
4132 if (beta1.containsStateWithBoth(h1star, h2star)) {
4133 aliasDetected = true;
4136 if (h1 != h2 && beta2.containsStateWithBoth(h1, h2)) {
4137 aliasDetected = true;
4139 if (beta2.containsStateWithBoth(h1plus, h2)) {
4140 aliasDetected = true;
4142 if (beta2.containsStateWithBoth(h1star, h2)) {
4143 aliasDetected = true;
4145 if (beta2.containsStateWithBoth(h1, h2plus)) {
4146 aliasDetected = true;
4148 if (beta2.containsStateWithBoth(h1plus, h2plus)) {
4149 aliasDetected = true;
4151 if (beta2.containsStateWithBoth(h1star, h2plus)) {
4152 aliasDetected = true;
4154 if (beta2.containsStateWithBoth(h1, h2star)) {
4155 aliasDetected = true;
4157 if (beta2.containsStateWithBoth(h1plus, h2star)) {
4158 aliasDetected = true;
4160 if (beta2.containsStateWithBoth(h1star, h2star)) {
4161 aliasDetected = true;
4164 Set<HeapRegionNode> common = new HashSet<HeapRegionNode>();
4165 if (aliasDetected) {
4166 common = findCommonReachableNodes(hrn1, hrn2);
4167 if (!(DISABLE_STRONG_UPDATES || DISABLE_GLOBAL_SWEEP)) {
4168 assert !common.isEmpty();
4175 public Set<HeapRegionNode> mayReachSharedObjects(FlatMethod fm,
4176 Integer paramIndex1, Integer paramIndex2) {
4178 // get parameter's heap regions
4179 TempDescriptor paramTemp1 = fm.getParameter(paramIndex1.intValue());
4180 VariableNode argVar1 = getVariableNodeFromTemp(paramTemp1);
4181 RefEdge argEdge1 = argVar1.iteratorToReferencees().next();
4182 HeapRegionNode hrnParam1 = argEdge1.getDst();
4184 TempDescriptor paramTemp2 = fm.getParameter(paramIndex2.intValue());
4185 VariableNode argVar2 = getVariableNodeFromTemp(paramTemp2);
4186 RefEdge argEdge2 = argVar2.iteratorToReferencees().next();
4187 HeapRegionNode hrnParam2 = argEdge2.getDst();
4189 Set<HeapRegionNode> common = new HashSet<HeapRegionNode>();
4190 common.addAll(mayReachSharedObjects(hrnParam1, hrnParam2));
4195 public Set<HeapRegionNode> mayReachSharedObjects(FlatMethod fm,
4196 Integer paramIndex, AllocSite as) {
4198 // get parameter's heap regions
4199 TempDescriptor paramTemp = fm.getParameter(paramIndex.intValue());
4200 VariableNode argVar = getVariableNodeFromTemp(paramTemp);
4201 RefEdge argEdge = argVar.iteratorToReferencees().next();
4202 HeapRegionNode hrnParam = argEdge.getDst();
4205 assert id2hrn.containsKey(as.getSummary());
4206 HeapRegionNode hrnSummary = id2hrn.get(as.getSummary());
4207 assert hrnSummary != null;
4209 Set<HeapRegionNode> common = mayReachSharedObjects(hrnParam, hrnSummary);
4211 // check for other nodes
4212 for (int i = 0; i < as.getAllocationDepth(); ++i) {
4214 assert id2hrn.containsKey(as.getIthOldest(i));
4215 HeapRegionNode hrnIthOldest = id2hrn.get(as.getIthOldest(i));
4216 assert hrnIthOldest != null;
4218 common = mayReachSharedObjects(hrnParam, hrnIthOldest);
4225 public Set<HeapRegionNode> mayReachSharedObjects(AllocSite as1,
4228 // get summary node 1's alpha
4229 Integer idSum1 = as1.getSummary();
4230 assert id2hrn.containsKey(idSum1);
4231 HeapRegionNode hrnSum1 = id2hrn.get(idSum1);
4232 assert hrnSum1 != null;
4234 // get summary node 2's alpha
4235 Integer idSum2 = as2.getSummary();
4236 assert id2hrn.containsKey(idSum2);
4237 HeapRegionNode hrnSum2 = id2hrn.get(idSum2);
4238 assert hrnSum2 != null;
4240 Set<HeapRegionNode> common = mayReachSharedObjects(hrnSum1, hrnSum2);
4242 // check sum2 against alloc1 nodes
4243 for (int i = 0; i < as1.getAllocationDepth(); ++i) {
4244 Integer idI1 = as1.getIthOldest(i);
4245 assert id2hrn.containsKey(idI1);
4246 HeapRegionNode hrnI1 = id2hrn.get(idI1);
4247 assert hrnI1 != null;
4249 common.addAll(mayReachSharedObjects(hrnI1, hrnSum2));
4252 // check sum1 against alloc2 nodes
4253 for (int i = 0; i < as2.getAllocationDepth(); ++i) {
4254 Integer idI2 = as2.getIthOldest(i);
4255 assert id2hrn.containsKey(idI2);
4256 HeapRegionNode hrnI2 = id2hrn.get(idI2);
4257 assert hrnI2 != null;
4259 common.addAll(mayReachSharedObjects(hrnSum1, hrnI2));
4261 // while we're at it, do an inner loop for alloc2 vs alloc1 nodes
4262 for (int j = 0; j < as1.getAllocationDepth(); ++j) {
4263 Integer idI1 = as1.getIthOldest(j);
4265 // if these are the same site, don't look for the same token, no
4267 // different tokens of the same site could alias together though
4268 if (idI1.equals(idI2)) {
4272 HeapRegionNode hrnI1 = id2hrn.get(idI1);
4274 common.addAll(mayReachSharedObjects(hrnI1, hrnI2));