1 package Analysis.SSJava;
3 import java.util.Enumeration;
4 import java.util.HashSet;
5 import java.util.Hashtable;
6 import java.util.Iterator;
7 import java.util.LinkedList;
9 import java.util.Stack;
11 import Analysis.CallGraph.CallGraph;
12 import Analysis.Loops.LoopFinder;
14 import IR.FieldDescriptor;
15 import IR.MethodDescriptor;
18 import IR.TypeDescriptor;
19 import IR.TypeExtension;
21 import IR.Flat.FlatCall;
22 import IR.Flat.FlatElementNode;
23 import IR.Flat.FlatFieldNode;
24 import IR.Flat.FlatLiteralNode;
25 import IR.Flat.FlatMethod;
26 import IR.Flat.FlatNew;
27 import IR.Flat.FlatNode;
28 import IR.Flat.FlatOpNode;
29 import IR.Flat.FlatSetElementNode;
30 import IR.Flat.FlatSetFieldNode;
31 import IR.Flat.TempDescriptor;
32 import IR.Tree.Modifiers;
34 public class DefinitelyWrittenCheck {
36 SSJavaAnalysis ssjava;
42 // maps a descriptor to its known dependents: namely
43 // methods or tasks that call the descriptor's method
44 // AND are part of this analysis (reachable from main)
45 private Hashtable<Descriptor, Set<MethodDescriptor>> mapDescriptorToSetDependents;
47 // maps a flat node to its WrittenSet: this keeps all heap path overwritten
49 private Hashtable<FlatNode, Set<NTuple<Descriptor>>> mapFlatNodeToMustWriteSet;
51 // maps a temp descriptor to its heap path
52 // each temp descriptor has a unique heap path since we do not allow any
54 private Hashtable<Descriptor, NTuple<Descriptor>> mapHeapPath;
56 // maps a temp descriptor to its composite location
57 private Hashtable<TempDescriptor, NTuple<Location>> mapDescriptorToLocationPath;
59 // maps a flat method to the READ that is the set of heap path that is
60 // expected to be written before method invocation
61 private Hashtable<FlatMethod, Set<NTuple<Descriptor>>> mapFlatMethodToReadSet;
63 // maps a flat method to the must-write set that is the set of heap path that
64 // is overwritten on every possible path during method invocation
65 private Hashtable<FlatMethod, Set<NTuple<Descriptor>>> mapFlatMethodToMustWriteSet;
67 // maps a flat method to the DELETE SET that is a set of heap path to shared
69 // written to but not overwritten by the higher value
70 private Hashtable<FlatMethod, SharedLocMap> mapFlatMethodToDeleteSet;
72 // maps a flat method to the S SET that is a set of heap path to shared
73 // locations that are overwritten by the higher value
74 private Hashtable<FlatMethod, SharedLocMap> mapFlatMethodToSharedLocMap;
76 // maps a flat method to the may-wirte set that is the set of heap path that
77 // might be written to
78 private Hashtable<FlatMethod, Set<NTuple<Descriptor>>> mapFlatMethodToMayWriteSet;
80 // maps a call site to the read set contributed by all callees
81 private Hashtable<FlatNode, Set<NTuple<Descriptor>>> mapFlatNodeToBoundReadSet;
83 // maps a call site to the must write set contributed by all callees
84 private Hashtable<FlatNode, Set<NTuple<Descriptor>>> mapFlatNodeToBoundMustWriteSet;
86 // maps a call site to the may read set contributed by all callees
87 private Hashtable<FlatNode, Set<NTuple<Descriptor>>> mapFlatNodeToBoundMayWriteSet;
89 // points to method containing SSJAVA Loop
90 private MethodDescriptor methodContainingSSJavaLoop;
92 // maps a flatnode to definitely written analysis mapping M
93 private Hashtable<FlatNode, Hashtable<NTuple<Descriptor>, Set<WriteAge>>> mapFlatNodetoEventLoopMap;
95 // maps shared location to the set of descriptors which belong to the shared
98 // keep current descriptors to visit in fixed-point interprocedural analysis,
99 private Stack<MethodDescriptor> methodDescriptorsToVisitStack;
101 // when analyzing flatcall, need to re-schedule set of callee
102 private Set<MethodDescriptor> calleesToEnqueue;
104 private Set<ReadSummary> possibleCalleeReadSummarySetToCaller;
106 public static final String arrayElementFieldName = "___element_";
107 static protected Hashtable<TypeDescriptor, FieldDescriptor> mapTypeToArrayField;
109 // maps a method descriptor to the merged incoming caller's current
111 // it is for setting clearance flag when all read set is overwritten
112 private Hashtable<MethodDescriptor, ReadSummary> mapMethodDescriptorToReadSummary;
114 private Hashtable<MethodDescriptor, MultiSourceMap<NTuple<Location>, NTuple<Descriptor>>> mapMethodToSharedLocCoverSet;
116 private Hashtable<FlatNode, SharedLocMap> mapFlatNodeToSharedLocMapping;
117 private Hashtable<FlatNode, SharedLocMap> mapFlatNodeToDeleteSet;
119 private Hashtable<Location, Set<Descriptor>> mapSharedLocationToCoverSet;
121 private LinkedList<MethodDescriptor> sortedDescriptors;
123 private LoopFinder ssjavaLoop;
124 private Set<FlatNode> loopIncElements;
126 private Set<NTuple<Descriptor>> calleeUnionBoundReadSet;
127 private Set<NTuple<Descriptor>> calleeIntersectBoundMustWriteSet;
128 private Set<NTuple<Descriptor>> calleeUnionBoundMayWriteSet;
129 private SharedLocMap calleeUnionBoundDeleteSet;
130 private SharedLocMap calleeIntersectBoundSharedSet;
132 private Hashtable<Descriptor, Location> mapDescToLocation;
134 private TempDescriptor LOCAL;
136 public static int MAXAGE = 1;
138 public DefinitelyWrittenCheck(SSJavaAnalysis ssjava, State state) {
140 this.ssjava = ssjava;
141 this.callGraph = ssjava.getCallGraph();
142 this.mapFlatNodeToMustWriteSet = new Hashtable<FlatNode, Set<NTuple<Descriptor>>>();
143 this.mapDescriptorToSetDependents = new Hashtable<Descriptor, Set<MethodDescriptor>>();
144 this.mapHeapPath = new Hashtable<Descriptor, NTuple<Descriptor>>();
145 this.mapDescriptorToLocationPath = new Hashtable<TempDescriptor, NTuple<Location>>();
146 this.mapFlatMethodToReadSet = new Hashtable<FlatMethod, Set<NTuple<Descriptor>>>();
147 this.mapFlatMethodToMustWriteSet = new Hashtable<FlatMethod, Set<NTuple<Descriptor>>>();
148 this.mapFlatMethodToMayWriteSet = new Hashtable<FlatMethod, Set<NTuple<Descriptor>>>();
149 this.mapFlatNodetoEventLoopMap =
150 new Hashtable<FlatNode, Hashtable<NTuple<Descriptor>, Set<WriteAge>>>();
151 this.calleeUnionBoundReadSet = new HashSet<NTuple<Descriptor>>();
152 this.calleeIntersectBoundMustWriteSet = new HashSet<NTuple<Descriptor>>();
153 this.calleeUnionBoundMayWriteSet = new HashSet<NTuple<Descriptor>>();
155 this.methodDescriptorsToVisitStack = new Stack<MethodDescriptor>();
156 this.calleesToEnqueue = new HashSet<MethodDescriptor>();
157 this.mapTypeToArrayField = new Hashtable<TypeDescriptor, FieldDescriptor>();
158 this.LOCAL = new TempDescriptor("LOCAL");
159 this.mapDescToLocation = new Hashtable<Descriptor, Location>();
160 this.possibleCalleeReadSummarySetToCaller = new HashSet<ReadSummary>();
161 this.mapMethodDescriptorToReadSummary = new Hashtable<MethodDescriptor, ReadSummary>();
162 this.mapFlatNodeToBoundReadSet = new Hashtable<FlatNode, Set<NTuple<Descriptor>>>();
163 this.mapFlatNodeToBoundMustWriteSet = new Hashtable<FlatNode, Set<NTuple<Descriptor>>>();
164 this.mapFlatNodeToBoundMayWriteSet = new Hashtable<FlatNode, Set<NTuple<Descriptor>>>();
165 this.mapSharedLocationToCoverSet = new Hashtable<Location, Set<Descriptor>>();
166 this.mapFlatNodeToSharedLocMapping = new Hashtable<FlatNode, SharedLocMap>();
167 this.mapFlatMethodToDeleteSet = new Hashtable<FlatMethod, SharedLocMap>();
168 this.calleeUnionBoundDeleteSet = new SharedLocMap();
169 this.calleeIntersectBoundSharedSet = new SharedLocMap();
170 this.mapFlatMethodToSharedLocMap = new Hashtable<FlatMethod, SharedLocMap>();
171 this.mapMethodToSharedLocCoverSet =
172 new Hashtable<MethodDescriptor, MultiSourceMap<NTuple<Location>, NTuple<Descriptor>>>();
173 this.mapFlatNodeToDeleteSet = new Hashtable<FlatNode, SharedLocMap>();
176 public void definitelyWrittenCheck() {
177 if (!ssjava.getAnnotationRequireSet().isEmpty()) {
180 methodReadWriteSetAnalysis();
181 computeSharedCoverSet();
190 private void sharedLocAnalysis() {
192 // perform method READ/OVERWRITE analysis
193 LinkedList<MethodDescriptor> descriptorListToAnalyze =
194 (LinkedList<MethodDescriptor>) sortedDescriptors.clone();
196 // current descriptors to visit in fixed-point interprocedural analysis,
198 // dependency in the call graph
199 methodDescriptorsToVisitStack.clear();
201 descriptorListToAnalyze.removeFirst();
203 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
204 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
206 while (!descriptorListToAnalyze.isEmpty()) {
207 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
208 methodDescriptorsToVisitStack.add(md);
211 // analyze scheduled methods until there are no more to visit
212 while (!methodDescriptorsToVisitStack.isEmpty()) {
213 // start to analyze leaf node
214 MethodDescriptor md = methodDescriptorsToVisitStack.pop();
215 FlatMethod fm = state.getMethodFlat(md);
217 SharedLocMap sharedLocMap = new SharedLocMap();
218 SharedLocMap deleteSet = new SharedLocMap();
220 sharedLoc_analyzeMethod(fm, sharedLocMap, deleteSet);
221 SharedLocMap prevSharedLocMap = mapFlatMethodToSharedLocMap.get(fm);
222 SharedLocMap prevDeleteSet = mapFlatMethodToDeleteSet.get(fm);
224 if (!(deleteSet.equals(prevDeleteSet) && sharedLocMap.equals(prevSharedLocMap))) {
225 mapFlatMethodToSharedLocMap.put(fm, sharedLocMap);
226 mapFlatMethodToDeleteSet.put(fm, deleteSet);
228 // results for callee changed, so enqueue dependents caller for
231 Iterator<MethodDescriptor> depsItr = getDependents(md).iterator();
232 while (depsItr.hasNext()) {
233 MethodDescriptor methodNext = depsItr.next();
234 if (!methodDescriptorsToVisitStack.contains(methodNext)
235 && methodDescriptorToVistSet.contains(methodNext)) {
236 methodDescriptorsToVisitStack.add(methodNext);
245 sharedLoc_analyzeEventLoop();
249 private void sharedLoc_analyzeEventLoop() {
250 if (state.SSJAVADEBUG) {
251 System.out.println("SSJAVA: Definite clearance for shared locations Analyzing: eventloop");
253 SharedLocMap sharedLocMap = new SharedLocMap();
254 SharedLocMap deleteSet = new SharedLocMap();
255 sharedLoc_analyzeBody(state.getMethodFlat(methodContainingSSJavaLoop),
256 ssjava.getSSJavaLoopEntrance(), sharedLocMap, deleteSet, true);
260 private void sharedLoc_analyzeMethod(FlatMethod fm, SharedLocMap sharedLocMap,
261 SharedLocMap deleteSet) {
262 if (state.SSJAVADEBUG) {
263 System.out.println("SSJAVA: Definite clearance for shared locations Analyzing: " + fm);
266 sharedLoc_analyzeBody(fm, fm, sharedLocMap, deleteSet, false);
270 private void sharedLoc_analyzeBody(FlatMethod fm, FlatNode startNode, SharedLocMap sharedLocMap,
271 SharedLocMap deleteSet, boolean isEventLoopBody) {
273 // intraprocedural analysis
274 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
275 flatNodesToVisit.add(startNode);
277 while (!flatNodesToVisit.isEmpty()) {
278 FlatNode fn = flatNodesToVisit.iterator().next();
279 flatNodesToVisit.remove(fn);
281 SharedLocMap currSharedSet = new SharedLocMap();
282 SharedLocMap currDeleteSet = new SharedLocMap();
284 for (int i = 0; i < fn.numPrev(); i++) {
285 FlatNode prevFn = fn.getPrev(i);
286 SharedLocMap inSharedLoc = mapFlatNodeToSharedLocMapping.get(prevFn);
287 if (inSharedLoc != null) {
288 mergeSharedLocMap(currSharedSet, inSharedLoc);
291 SharedLocMap inDeleteLoc = mapFlatNodeToDeleteSet.get(prevFn);
292 if (inDeleteLoc != null) {
293 mergeDeleteSet(currDeleteSet, inDeleteLoc);
297 sharedLoc_nodeActions(fm, fn, currSharedSet, currDeleteSet, sharedLocMap, deleteSet,
300 SharedLocMap prevSharedSet = mapFlatNodeToSharedLocMapping.get(fn);
301 SharedLocMap prevDeleteSet = mapFlatNodeToDeleteSet.get(fn);
303 if (!(currSharedSet.equals(prevSharedSet) && currDeleteSet.equals(prevDeleteSet))) {
304 mapFlatNodeToSharedLocMapping.put(fn, currSharedSet);
305 mapFlatNodeToDeleteSet.put(fn, currDeleteSet);
306 for (int i = 0; i < fn.numNext(); i++) {
307 FlatNode nn = fn.getNext(i);
308 if ((!isEventLoopBody) || loopIncElements.contains(nn)) {
309 flatNodesToVisit.add(nn);
319 private void sharedLoc_nodeActions(FlatMethod fm, FlatNode fn, SharedLocMap curr,
320 SharedLocMap currDeleteSet, SharedLocMap sharedLocMap, SharedLocMap deleteSet,
321 boolean isEventLoopBody) {
323 MethodDescriptor md = fm.getMethod();
325 SharedLocMap killSet = new SharedLocMap();
326 SharedLocMap genSet = new SharedLocMap();
334 case FKind.FlatOpNode: {
336 if (isEventLoopBody) {
337 FlatOpNode fon = (FlatOpNode) fn;
339 if (fon.getOp().getOp() == Operation.ASSIGN) {
343 if (!lhs.getSymbol().startsWith("neverused") && !lhs.getSymbol().startsWith("leftop")
344 && !lhs.getSymbol().startsWith("rightop") && rhs.getType().isImmutable()) {
346 Location dstLoc = getLocation(lhs);
347 if (dstLoc != null && ssjava.isSharedLocation(dstLoc)) {
348 NTuple<Descriptor> lhsHeapPath = computePath(lhs);
349 NTuple<Location> lhsLocTuple = mapDescriptorToLocationPath.get(lhs);
351 Location srcLoc = getLocation(lhs);
353 // computing gen/kill set
354 computeKILLSetForWrite(curr, killSet, lhsLocTuple, lhsHeapPath);
355 if (!dstLoc.equals(srcLoc)) {
356 computeGENSetForHigherWrite(curr, killSet, lhsLocTuple, lhsHeapPath);
357 updateDeleteSetForHigherWrite(currDeleteSet, lhsLocTuple, lhsHeapPath);
359 computeGENSetForSameHeightWrite(curr, killSet, lhsLocTuple, lhsHeapPath);
360 updateDeleteSetForSameHeightWrite(currDeleteSet, lhsLocTuple, lhsHeapPath);
363 // System.out.println("VAR WRITE:" + fn);
364 // System.out.println("lhsLocTuple=" + lhsLocTuple +
365 // " lhsHeapPath=" + lhsHeapPath);
366 // System.out.println("dstLoc=" + dstLoc + " srcLoc=" + srcLoc);
367 // System.out.println("KILLSET=" + killSet);
368 // System.out.println("GENSet=" + genSet);
369 // System.out.println("DELETESET=" + currDeleteSet);
382 case FKind.FlatSetFieldNode:
383 case FKind.FlatSetElementNode: {
386 if (fn.kind() == FKind.FlatSetFieldNode) {
387 FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
389 fld = fsfn.getField();
391 fieldLoc = (Location) fld.getType().getExtension();
393 FlatSetElementNode fsen = (FlatSetElementNode) fn;
396 TypeDescriptor td = lhs.getType().dereference();
397 fld = getArrayField(td);
399 NTuple<Location> locTuple = deriveLocationTuple(md, lhs);
400 fieldLoc = locTuple.get(locTuple.size() - 1);
404 if (!isEventLoopBody && fieldLoc.getDescriptor().equals(md)) {
405 // if the field belongs to the local lattice, no reason to calculate
410 NTuple<Location> fieldLocTuple = new NTuple<Location>();
411 if (fld.isStatic()) {
413 // in this case, fld has TOP location
414 Location topLocation = Location.createTopLocation(md);
415 fieldLocTuple.add(topLocation);
417 fieldLocTuple.addAll(deriveGlobalLocationTuple(md));
418 if (fn.kind() == FKind.FlatSetFieldNode) {
419 fieldLocTuple.add((Location) fld.getType().getExtension());
424 fieldLocTuple.addAll(deriveLocationTuple(md, lhs));
425 if (fn.kind() == FKind.FlatSetFieldNode) {
426 fieldLocTuple.add((Location) fld.getType().getExtension());
430 // shared loc extension
431 Location srcLoc = getLocation(rhs);
432 if (ssjava.isSharedLocation(fieldLoc)) {
433 // only care the case that loc(f) is shared location
436 // NTuple<Location> fieldLocTuple = new NTuple<Location>();
437 // fieldLocTuple.addAll(mapDescriptorToLocationPath.get(lhs));
438 // fieldLocTuple.add(fieldLoc);
440 NTuple<Descriptor> fldHeapPath = new NTuple<Descriptor>();
441 fldHeapPath.addAll(computePath(lhs));
442 if (fn.kind() == FKind.FlatSetFieldNode) {
443 fldHeapPath.add(fld);
446 // computing gen/kill set
447 computeKILLSetForWrite(curr, killSet, fieldLocTuple, fldHeapPath);
448 if (!fieldLoc.equals(srcLoc)) {
449 computeGENSetForHigherWrite(curr, genSet, fieldLocTuple, fldHeapPath);
450 updateDeleteSetForHigherWrite(currDeleteSet, fieldLocTuple, fldHeapPath);
452 computeGENSetForSameHeightWrite(curr, genSet, fieldLocTuple, fldHeapPath);
453 updateDeleteSetForSameHeightWrite(currDeleteSet, fieldLocTuple, fldHeapPath);
456 // System.out.println("################");
457 // System.out.println("FIELD WRITE:" + fn);
458 // System.out.println("FldHeapPath=" + fldHeapPath);
459 // System.out.println("fieldLocTuple=" + fieldLocTuple + " srcLoc=" +
461 // System.out.println("KILLSET=" + killSet);
462 // System.out.println("GENSet=" + genSet);
463 // System.out.println("DELETESET=" + currDeleteSet);
469 case FKind.FlatCall: {
470 FlatCall fc = (FlatCall) fn;
472 // if (ssjava.needTobeAnnotated(fc.getMethod())) {
473 // System.out.println("#FLATCALL=" + fc);
475 bindHeapPathCallerArgWithCaleeParamForSharedLoc(fm.getMethod(), fc);
477 // computing gen/kill set
478 generateKILLSetForFlatCall(curr, killSet);
479 generateGENSetForFlatCall(curr, genSet);
482 // System.out.println("#FLATCALL=" + fc);
483 // System.out.println("KILLSET=" + killSet);
484 // System.out.println("GENSet=" + genSet);
485 // System.out.println("bound DELETE Set=" + calleeUnionBoundDeleteSet);
490 case FKind.FlatExit: {
491 // merge the current delete/shared loc mapping
492 mergeSharedLocMap(sharedLocMap, curr);
493 mergeDeleteSet(deleteSet, currDeleteSet);
495 // System.out.println("#FLATEXIT sharedLocMap=" + sharedLocMap);
501 computeNewMapping(curr, killSet, genSet);
502 if (!curr.map.isEmpty()) {
503 // System.out.println(fn + "#######" + curr);
508 private void generateGENSetForFlatCall(SharedLocMap curr, SharedLocMap genSet) {
510 Set<NTuple<Location>> locTupleSet = calleeIntersectBoundSharedSet.keySet();
511 for (Iterator iterator = locTupleSet.iterator(); iterator.hasNext();) {
512 NTuple<Location> locTupleKey = (NTuple<Location>) iterator.next();
513 genSet.addWrite(locTupleKey, curr.get(locTupleKey));
514 genSet.addWrite(locTupleKey, calleeIntersectBoundSharedSet.get(locTupleKey));
516 genSet.removeWriteAll(locTupleKey, calleeUnionBoundDeleteSet.get(locTupleKey));
521 private void generateKILLSetForFlatCall(SharedLocMap curr, SharedLocMap killSet) {
523 Set<NTuple<Location>> locTupleSet = calleeIntersectBoundSharedSet.keySet();
524 for (Iterator iterator = locTupleSet.iterator(); iterator.hasNext();) {
525 NTuple<Location> locTupleKey = (NTuple<Location>) iterator.next();
526 killSet.addWrite(locTupleKey, curr.get(locTupleKey));
531 private void mergeDeleteSet(SharedLocMap currDeleteSet, SharedLocMap inDeleteLoc) {
533 Set<NTuple<Location>> locTupleKeySet = inDeleteLoc.keySet();
535 for (Iterator iterator = locTupleKeySet.iterator(); iterator.hasNext();) {
536 NTuple<Location> locTupleKey = (NTuple<Location>) iterator.next();
538 Set<NTuple<Descriptor>> inSet = inDeleteLoc.get(locTupleKey);
539 currDeleteSet.addWrite(locTupleKey, inSet);
544 private void computeNewMapping(SharedLocMap curr, SharedLocMap killSet, SharedLocMap genSet) {
549 private void updateDeleteSetForHigherWrite(SharedLocMap currDeleteSet, NTuple<Location> locTuple,
550 NTuple<Descriptor> hp) {
551 currDeleteSet.removeWrite(locTuple, hp);
554 private void updateDeleteSetForSameHeightWrite(SharedLocMap currDeleteSet,
555 NTuple<Location> locTuple, NTuple<Descriptor> hp) {
556 currDeleteSet.addWrite(locTuple, hp);
559 private void computeGENSetForHigherWrite(SharedLocMap curr, SharedLocMap genSet,
560 NTuple<Location> locTuple, NTuple<Descriptor> hp) {
561 Set<NTuple<Descriptor>> currWriteSet = curr.get(locTuple);
563 if (currWriteSet != null) {
564 genSet.addWrite(locTuple, currWriteSet);
567 genSet.addWrite(locTuple, hp);
570 private void computeGENSetForSameHeightWrite(SharedLocMap curr, SharedLocMap genSet,
571 NTuple<Location> locTuple, NTuple<Descriptor> hp) {
572 Set<NTuple<Descriptor>> currWriteSet = curr.get(locTuple);
574 if (currWriteSet != null) {
575 genSet.addWrite(locTuple, currWriteSet);
577 genSet.removeWrite(locTuple, hp);
580 private void computeKILLSetForWrite(SharedLocMap curr, SharedLocMap killSet,
581 NTuple<Location> locTuple, NTuple<Descriptor> hp) {
583 Set<NTuple<Descriptor>> writeSet = curr.get(locTuple);
584 if (writeSet != null) {
585 killSet.addWrite(locTuple, writeSet);
590 private void mergeSharedLocMap(SharedLocMap currSharedSet, SharedLocMap in) {
592 Set<NTuple<Location>> locTupleKeySet = in.keySet();
593 for (Iterator iterator = locTupleKeySet.iterator(); iterator.hasNext();) {
594 NTuple<Location> locTupleKey = (NTuple<Location>) iterator.next();
596 Set<NTuple<Descriptor>> inSet = in.get(locTupleKey);
597 Set<NTuple<Descriptor>> currSet = currSharedSet.get(locTupleKey);
598 if (currSet == null) {
599 currSet = new HashSet<NTuple<Descriptor>>();
600 currSet.addAll(inSet);
601 currSharedSet.addWrite(locTupleKey, currSet);
603 currSet.retainAll(inSet);
608 private void computeSharedCoverSet() {
609 LinkedList<MethodDescriptor> descriptorListToAnalyze =
610 (LinkedList<MethodDescriptor>) sortedDescriptors.clone();
612 // current descriptors to visit in fixed-point interprocedural analysis,
614 // dependency in the call graph
615 methodDescriptorsToVisitStack.clear();
617 descriptorListToAnalyze.removeFirst();
619 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
620 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
622 while (!descriptorListToAnalyze.isEmpty()) {
623 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
624 methodDescriptorsToVisitStack.add(md);
627 // analyze scheduled methods until there are no more to visit
628 while (!methodDescriptorsToVisitStack.isEmpty()) {
629 MethodDescriptor md = methodDescriptorsToVisitStack.pop();
630 FlatMethod fm = state.getMethodFlat(md);
631 computeSharedCoverSet_analyzeMethod(fm, md.equals(methodContainingSSJavaLoop));
634 computeSharedCoverSetForEventLoop();
638 private void computeSharedCoverSetForEventLoop() {
639 computeSharedCoverSet_analyzeMethod(state.getMethodFlat(methodContainingSSJavaLoop), true);
642 private void computeSharedCoverSet_analyzeMethod(FlatMethod fm, boolean onlyVisitSSJavaLoop) {
644 System.out.println("computeSharedCoverSet_analyzeMethod=" + fm);
645 MethodDescriptor md = fm.getMethod();
647 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
649 Set<FlatNode> visited = new HashSet<FlatNode>();
651 if (onlyVisitSSJavaLoop) {
652 flatNodesToVisit.add(ssjava.getSSJavaLoopEntrance());
654 flatNodesToVisit.add(fm);
657 while (!flatNodesToVisit.isEmpty()) {
658 FlatNode fn = flatNodesToVisit.iterator().next();
659 flatNodesToVisit.remove(fn);
662 computeSharedCoverSet_nodeActions(md, fn, onlyVisitSSJavaLoop);
664 for (int i = 0; i < fn.numNext(); i++) {
665 FlatNode nn = fn.getNext(i);
667 if (!visited.contains(nn)) {
668 if (!onlyVisitSSJavaLoop || (onlyVisitSSJavaLoop && loopIncElements.contains(nn))) {
669 flatNodesToVisit.add(nn);
679 private void computeSharedCoverSet_nodeActions(MethodDescriptor md, FlatNode fn,
680 boolean isEventLoopBody) {
687 case FKind.FlatLiteralNode: {
688 FlatLiteralNode fln = (FlatLiteralNode) fn;
691 NTuple<Location> lhsLocTuple = new NTuple<Location>();
692 lhsLocTuple.add(Location.createTopLocation(md));
693 mapDescriptorToLocationPath.put(lhs, lhsLocTuple);
695 if (lhs.getType().isPrimitive() && !lhs.getSymbol().startsWith("neverused")
696 && !lhs.getSymbol().startsWith("srctmp")) {
697 // only need to care about composite location case here
698 if (lhs.getType().getExtension() instanceof SSJavaType) {
699 CompositeLocation compLoc = ((SSJavaType) lhs.getType().getExtension()).getCompLoc();
700 Location lastLocElement = compLoc.get(compLoc.getSize() - 1);
701 // check if the last one is shared loc
702 if (ssjava.isSharedLocation(lastLocElement)) {
703 addSharedLocDescriptor(lastLocElement, lhs);
711 case FKind.FlatOpNode: {
712 FlatOpNode fon = (FlatOpNode) fn;
713 // for a normal assign node, need to propagate lhs's location path to
715 if (fon.getOp().getOp() == Operation.ASSIGN) {
719 if (!lhs.getSymbol().startsWith("neverused") && !lhs.getSymbol().startsWith("leftop")
720 && !lhs.getSymbol().startsWith("rightop")) {
722 NTuple<Location> rhsLocTuple = new NTuple<Location>();
723 NTuple<Location> lhsLocTuple = new NTuple<Location>();
724 if (mapDescriptorToLocationPath.containsKey(rhs)) {
725 mapDescriptorToLocationPath.put(lhs, deriveLocationTuple(md, rhs));
728 if (rhs.getType().getExtension() != null
729 && rhs.getType().getExtension() instanceof SSJavaType) {
731 if (((SSJavaType) rhs.getType().getExtension()).getCompLoc() != null) {
732 rhsLocTuple.addAll(((SSJavaType) rhs.getType().getExtension()).getCompLoc()
737 NTuple<Location> locTuple = deriveLocationTuple(md, rhs);
738 if (locTuple != null) {
739 rhsLocTuple.addAll(locTuple);
742 if (rhsLocTuple.size() > 0) {
743 mapDescriptorToLocationPath.put(rhs, rhsLocTuple);
747 if (lhs.getType().getExtension() != null
748 && lhs.getType().getExtension() instanceof SSJavaType) {
749 lhsLocTuple.addAll(((SSJavaType) lhs.getType().getExtension()).getCompLoc()
751 mapDescriptorToLocationPath.put(lhs, lhsLocTuple);
752 } else if (mapDescriptorToLocationPath.get(rhs) != null) {
753 // propagate rhs's location to lhs
754 lhsLocTuple.addAll(mapDescriptorToLocationPath.get(rhs));
755 mapDescriptorToLocationPath.put(lhs, lhsLocTuple);
760 if (lhs.getType().isPrimitive() && !lhs.getSymbol().startsWith("srctmp")) {
762 NTuple<Descriptor> lhsHeapPath = computePath(lhs);
764 if (lhsLocTuple != null) {
765 addMayWrittenSet(md, lhsLocTuple, lhsHeapPath);
775 case FKind.FlatSetFieldNode:
776 case FKind.FlatSetElementNode: {
780 if (fn.kind() == FKind.FlatSetFieldNode) {
781 FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
783 fld = fsfn.getField();
786 FlatSetElementNode fsen = (FlatSetElementNode) fn;
789 TypeDescriptor td = lhs.getType().dereference();
790 fld = getArrayField(td);
793 Location fieldLocation;
794 if (fn.kind() == FKind.FlatSetFieldNode) {
795 fieldLocation = (Location) fld.getType().getExtension();
797 NTuple<Location> locTuple = mapDescriptorToLocationPath.get(lhs);
798 fieldLocation = locTuple.get(locTuple.size() - 1);
801 if (!isEventLoopBody && fieldLocation.getDescriptor().equals(md)) {
802 // if the field belongs to the local lattice, no reason to calculate
807 NTuple<Location> fieldLocTuple = new NTuple<Location>();
808 if (fld.isStatic()) {
810 // in this case, fld has TOP location
811 Location topLocation = Location.createTopLocation(md);
812 fieldLocTuple.add(topLocation);
814 fieldLocTuple.addAll(deriveGlobalLocationTuple(md));
815 if (fn.kind() == FKind.FlatSetFieldNode) {
816 fieldLocTuple.add((Location) fld.getType().getExtension());
821 fieldLocTuple.addAll(deriveLocationTuple(md, lhs));
822 if (fn.kind() == FKind.FlatSetFieldNode) {
823 fieldLocTuple.add((Location) fld.getType().getExtension());
827 NTuple<Location> lTuple = deriveLocationTuple(md, lhs);
828 if (lTuple != null) {
829 NTuple<Location> lhsLocTuple = new NTuple<Location>();
830 lhsLocTuple.addAll(lTuple);
831 mapDescriptorToLocationPath.put(lhs, lhsLocTuple);
834 if (ssjava.isSharedLocation(fieldLocation)) {
835 addSharedLocDescriptor(fieldLocation, fld);
837 NTuple<Descriptor> fieldHeapPath = new NTuple<Descriptor>();
838 fieldHeapPath.addAll(computePath(lhs));
839 if (fn.kind() == FKind.FlatSetFieldNode) {
840 fieldHeapPath.add(fld);
843 // mapLocationPathToMayWrittenSet.put(locTuple, null, fld);
844 addMayWrittenSet(md, fieldLocTuple, fieldHeapPath);
851 case FKind.FlatElementNode:
852 case FKind.FlatFieldNode: {
856 if (fn.kind() == FKind.FlatFieldNode) {
857 FlatFieldNode ffn = (FlatFieldNode) fn;
860 fld = ffn.getField();
862 FlatElementNode fen = (FlatElementNode) fn;
865 TypeDescriptor td = rhs.getType().dereference();
866 fld = getArrayField(td);
869 NTuple<Location> locTuple = new NTuple<Location>();
871 if (fld.isStatic()) {
874 // in this case, fld has TOP location
875 Location topLocation = Location.createTopLocation(md);
876 locTuple.add(topLocation);
878 locTuple.addAll(deriveGlobalLocationTuple(md));
879 if (fn.kind() == FKind.FlatFieldNode) {
880 locTuple.add((Location) fld.getType().getExtension());
885 locTuple.addAll(deriveLocationTuple(md, rhs));
886 if (fn.kind() == FKind.FlatFieldNode) {
887 locTuple.add((Location) fld.getType().getExtension());
891 mapDescriptorToLocationPath.put(lhs, locTuple);
896 case FKind.FlatCall: {
898 FlatCall fc = (FlatCall) fn;
900 // if (ssjava.needTobeAnnotated(fc.getMethod())) {
901 bindLocationPathCallerArgWithCalleeParam(md, fc);
907 case FKind.FlatNew: {
909 FlatNew fnew = (FlatNew) fn;
910 TempDescriptor dst = fnew.getDst();
911 NTuple<Location> locTuple = deriveLocationTuple(md, dst);
913 if (locTuple != null) {
914 NTuple<Location> dstLocTuple = new NTuple<Location>();
915 dstLocTuple.addAll(locTuple);
916 mapDescriptorToLocationPath.put(dst, dstLocTuple);
924 private void addMayWrittenSet(MethodDescriptor md, NTuple<Location> locTuple,
925 NTuple<Descriptor> heapPath) {
927 MultiSourceMap<NTuple<Location>, NTuple<Descriptor>> map = mapMethodToSharedLocCoverSet.get(md);
929 map = new MultiSourceMap<NTuple<Location>, NTuple<Descriptor>>();
930 mapMethodToSharedLocCoverSet.put(md, map);
933 Set<NTuple<Descriptor>> writeSet = map.get(locTuple);
934 if (writeSet == null) {
935 writeSet = new HashSet<NTuple<Descriptor>>();
936 map.put(locTuple, writeSet);
938 writeSet.add(heapPath);
942 private void bindLocationPathCallerArgWithCalleeParam(MethodDescriptor mdCaller, FlatCall fc) {
944 if (ssjava.isSSJavaUtil(fc.getMethod().getClassDesc())) {
946 // have write effects on the first argument
947 TempDescriptor arg = fc.getArg(0);
948 NTuple<Location> argLocationPath = deriveLocationTuple(mdCaller, arg);
949 NTuple<Descriptor> argHeapPath = computePath(arg);
950 addMayWrittenSet(mdCaller, argLocationPath, argHeapPath);
953 // if arg is not primitive type, we need to propagate maywritten set to
954 // the caller's location path
956 MethodDescriptor mdCallee = fc.getMethod();
957 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
958 setPossibleCallees.addAll(callGraph.getMethods(mdCallee));
960 // create mapping from arg idx to its heap paths
961 Hashtable<Integer, NTuple<Descriptor>> mapArgIdx2CallerArgHeapPath =
962 new Hashtable<Integer, NTuple<Descriptor>>();
964 // create mapping from arg idx to its location paths
965 Hashtable<Integer, NTuple<Location>> mapArgIdx2CallerArgLocationPath =
966 new Hashtable<Integer, NTuple<Location>>();
968 // arg idx is starting from 'this' arg
969 if (fc.getThis() != null) {
970 // loc path for 'this'
971 NTuple<Location> thisLocationPath = deriveLocationTuple(mdCaller, fc.getThis());
972 if (thisLocationPath != null) {
973 mapArgIdx2CallerArgLocationPath.put(Integer.valueOf(0), thisLocationPath);
975 // heap path for 'this'
976 NTuple<Descriptor> thisHeapPath = new NTuple<Descriptor>();
977 if (mapHeapPath.containsKey(fc.getThis())) {
978 thisHeapPath.addAll(mapHeapPath.get(fc.getThis()));
980 // method is called without creating new flat node representing
982 thisHeapPath.add(fc.getThis());
984 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(0), thisHeapPath);
989 for (int i = 0; i < fc.numArgs(); i++) {
990 TempDescriptor arg = fc.getArg(i);
991 // create mapping arg to loc path
992 NTuple<Location> argLocationPath = deriveLocationTuple(mdCaller, arg);
993 if (argLocationPath != null) {
994 mapArgIdx2CallerArgLocationPath.put(Integer.valueOf(i + 1), argLocationPath);
995 // create mapping arg to heap path
996 NTuple<Descriptor> argHeapPath = computePath(arg);
997 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(i + 1), argHeapPath);
1002 Hashtable<Integer, Set<NTuple<Descriptor>>> mapParamIdx2WriteSet =
1003 new Hashtable<Integer, Set<NTuple<Descriptor>>>();
1005 for (int i = 0; i < fc.numArgs() + 1; i++) {
1006 mapParamIdx2WriteSet.put(Integer.valueOf(i), new HashSet<NTuple<Descriptor>>());
1009 for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
1010 MethodDescriptor callee = (MethodDescriptor) iterator.next();
1011 FlatMethod calleeFlatMethod = state.getMethodFlat(callee);
1013 // binding caller's args and callee's params
1015 Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc =
1016 new Hashtable<Integer, TempDescriptor>();
1018 if (calleeFlatMethod.getMethod().isStatic()) {
1019 // static method does not have implicit 'this' arg
1022 for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
1023 TempDescriptor param = calleeFlatMethod.getParameter(i);
1024 mapParamIdx2ParamTempDesc.put(Integer.valueOf(i + offset), param);
1027 Set<Integer> keySet = mapArgIdx2CallerArgLocationPath.keySet();
1028 for (Iterator iterator2 = keySet.iterator(); iterator2.hasNext();) {
1029 Integer idx = (Integer) iterator2.next();
1030 NTuple<Location> callerArgLocationPath = mapArgIdx2CallerArgLocationPath.get(idx);
1032 TempDescriptor calleeParam = mapParamIdx2ParamTempDesc.get(idx);
1034 NTuple<Descriptor> callerArgHeapPath = mapArgIdx2CallerArgHeapPath.get(idx);
1035 NTuple<Location> calleeLocationPath = deriveLocationTuple(mdCallee, calleeParam);
1036 NTuple<Descriptor> calleeHeapPath = computePath(calleeParam);
1038 createNewMappingOfMayWrittenSet(mdCaller, callee, callerArgHeapPath,
1039 callerArgLocationPath, calleeHeapPath, calleeLocationPath,
1040 mapParamIdx2WriteSet.get(idx));
1050 private Hashtable<NTuple<Location>, Set<NTuple<Descriptor>>> getMappingByStartedWith(
1051 MultiSourceMap<NTuple<Location>, NTuple<Descriptor>> map, NTuple<Location> in) {
1053 Hashtable<NTuple<Location>, Set<NTuple<Descriptor>>> matchedMapping =
1054 new Hashtable<NTuple<Location>, Set<NTuple<Descriptor>>>();
1056 Set<NTuple<Location>> keySet = map.keySet();
1058 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1059 NTuple<Location> key = (NTuple<Location>) iterator.next();
1060 if (key.startsWith(in)) {
1061 matchedMapping.put(key, map.get(key));
1065 return matchedMapping;
1069 private void createNewMappingOfMayWrittenSet(MethodDescriptor caller, MethodDescriptor callee,
1070 NTuple<Descriptor> callerArgHeapPath, NTuple<Location> callerArgLocPath,
1071 NTuple<Descriptor> calleeParamHeapPath, NTuple<Location> calleeParamLocPath,
1072 Set<NTuple<Descriptor>> writeSet) {
1074 // propagate may-written-set associated with the key that is started with
1075 // calleepath to the caller
1076 // 1) makes a new key by combining caller path and callee path(except local
1077 // loc element of param)
1078 // 2) create new mapping of may-written-set of callee path to caller path
1080 // extract all may written effect accessed through callee param path
1081 MultiSourceMap<NTuple<Location>, NTuple<Descriptor>> calleeMapping =
1082 mapMethodToSharedLocCoverSet.get(callee);
1084 MultiSourceMap<NTuple<Location>, NTuple<Descriptor>> callerMapping =
1085 mapMethodToSharedLocCoverSet.get(caller);
1087 if (callerMapping == null) {
1088 callerMapping = new MultiSourceMap<NTuple<Location>, NTuple<Descriptor>>();
1089 mapMethodToSharedLocCoverSet.put(caller, callerMapping);
1092 if (calleeMapping == null) {
1096 Hashtable<NTuple<Location>, Set<NTuple<Descriptor>>> paramMapping =
1097 getMappingByStartedWith(calleeMapping, calleeParamLocPath);
1099 Set<NTuple<Location>> calleeKeySet = calleeMapping.keySet();
1100 for (Iterator iterator = calleeKeySet.iterator(); iterator.hasNext();) {
1101 NTuple<Location> calleeKey = (NTuple<Location>) iterator.next();
1102 Set<NTuple<Descriptor>> calleeMayWriteSet = paramMapping.get(calleeKey);
1104 if (calleeMayWriteSet != null) {
1106 Set<NTuple<Descriptor>> boundWriteSet =
1107 convertCallerMayWriteSet(callerArgHeapPath, calleeParamHeapPath, calleeMayWriteSet);
1109 writeSet.addAll(boundWriteSet);
1111 NTuple<Location> newKey = new NTuple<Location>();
1112 newKey.addAll(callerArgLocPath);
1113 // need to replace the local location with the caller's path so skip the
1114 // local location of the parameter
1115 for (int i = 1; i < calleeKey.size(); i++) {
1116 newKey.add(calleeKey.get(i));
1119 callerMapping.union(newKey, writeSet);
1120 // mapLocationPathToMayWrittenSet.put(calleeKey, newKey, writeSet);
1127 private Set<NTuple<Descriptor>> convertCallerMayWriteSet(NTuple<Descriptor> callerArgHeapPath,
1128 NTuple<Descriptor> calleeParamHeapPath, Set<NTuple<Descriptor>> calleeMayWriteSet) {
1130 Set<NTuple<Descriptor>> boundSet = new HashSet<NTuple<Descriptor>>();
1132 // replace callee's param path with caller's arg path
1133 for (Iterator iterator = calleeMayWriteSet.iterator(); iterator.hasNext();) {
1134 NTuple<Descriptor> calleeWriteHeapPath = (NTuple<Descriptor>) iterator.next();
1136 NTuple<Descriptor> boundHeapPath = new NTuple<Descriptor>();
1137 boundHeapPath.addAll(callerArgHeapPath);
1139 int startIdx = calleeParamHeapPath.size();
1141 for (int i = startIdx; i < calleeWriteHeapPath.size(); i++) {
1142 boundHeapPath.add(calleeWriteHeapPath.get(i));
1145 boundSet.add(boundHeapPath);
1152 private void addSharedLocDescriptor(Location sharedLoc, Descriptor desc) {
1154 Set<Descriptor> descSet = mapSharedLocationToCoverSet.get(sharedLoc);
1155 if (descSet == null) {
1156 descSet = new HashSet<Descriptor>();
1157 mapSharedLocationToCoverSet.put(sharedLoc, descSet);
1164 private Location getLocation(Descriptor d) {
1166 if (d instanceof FieldDescriptor) {
1167 TypeExtension te = ((FieldDescriptor) d).getType().getExtension();
1169 return (Location) te;
1172 assert d instanceof TempDescriptor;
1173 TempDescriptor td = (TempDescriptor) d;
1175 TypeExtension te = td.getType().getExtension();
1177 if (te instanceof SSJavaType) {
1178 SSJavaType ssType = (SSJavaType) te;
1179 if (ssType.getCompLoc() != null) {
1180 CompositeLocation comp = ssType.getCompLoc();
1181 return comp.get(comp.getSize() - 1);
1186 return (Location) te;
1191 return mapDescToLocation.get(d);
1194 private void eventLoopAnalysis() {
1195 // perform second stage analysis: intraprocedural analysis ensure that
1197 // variables are definitely written in-between the same read
1199 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
1200 flatNodesToVisit.add(ssjava.getSSJavaLoopEntrance());
1202 while (!flatNodesToVisit.isEmpty()) {
1203 FlatNode fn = (FlatNode) flatNodesToVisit.iterator().next();
1204 flatNodesToVisit.remove(fn);
1206 Hashtable<NTuple<Descriptor>, Set<WriteAge>> prev = mapFlatNodetoEventLoopMap.get(fn);
1208 Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr =
1209 new Hashtable<NTuple<Descriptor>, Set<WriteAge>>();
1210 for (int i = 0; i < fn.numPrev(); i++) {
1211 FlatNode nn = fn.getPrev(i);
1212 Hashtable<NTuple<Descriptor>, Set<WriteAge>> in = mapFlatNodetoEventLoopMap.get(nn);
1218 eventLoopAnalysis_nodeAction(fn, curr, ssjava.getSSJavaLoopEntrance());
1220 // if a new result, schedule forward nodes for analysis
1221 if (!curr.equals(prev)) {
1222 mapFlatNodetoEventLoopMap.put(fn, curr);
1224 for (int i = 0; i < fn.numNext(); i++) {
1225 FlatNode nn = fn.getNext(i);
1226 if (loopIncElements.contains(nn)) {
1227 flatNodesToVisit.add(nn);
1235 private void union(Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr,
1236 Hashtable<NTuple<Descriptor>, Set<WriteAge>> in) {
1238 Set<NTuple<Descriptor>> inKeySet = in.keySet();
1239 for (Iterator iterator = inKeySet.iterator(); iterator.hasNext();) {
1240 NTuple<Descriptor> inKey = (NTuple<Descriptor>) iterator.next();
1241 Set<WriteAge> inSet = in.get(inKey);
1243 Set<WriteAge> currSet = curr.get(inKey);
1245 if (currSet == null) {
1246 currSet = new HashSet<WriteAge>();
1247 curr.put(inKey, currSet);
1249 currSet.addAll(inSet);
1254 private void eventLoopAnalysis_nodeAction(FlatNode fn,
1255 Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr, FlatNode loopEntrance) {
1257 Hashtable<NTuple<Descriptor>, Set<WriteAge>> readWriteKillSet =
1258 new Hashtable<NTuple<Descriptor>, Set<WriteAge>>();
1259 Hashtable<NTuple<Descriptor>, Set<WriteAge>> readWriteGenSet =
1260 new Hashtable<NTuple<Descriptor>, Set<WriteAge>>();
1262 if (fn.equals(loopEntrance)) {
1263 // it reaches loop entrance: changes all flag to true
1264 Set<NTuple<Descriptor>> keySet = curr.keySet();
1265 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1266 NTuple<Descriptor> key = (NTuple<Descriptor>) iterator.next();
1267 Set<WriteAge> writeAgeSet = curr.get(key);
1269 Set<WriteAge> incSet = new HashSet<WriteAge>();
1270 incSet.addAll(writeAgeSet);
1271 writeAgeSet.clear();
1273 for (Iterator iterator2 = incSet.iterator(); iterator2.hasNext();) {
1274 WriteAge writeAge = (WriteAge) iterator2.next();
1275 WriteAge newWriteAge = writeAge.copy();
1277 writeAgeSet.add(newWriteAge);
1285 FieldDescriptor fld;
1287 switch (fn.kind()) {
1289 case FKind.FlatOpNode: {
1290 FlatOpNode fon = (FlatOpNode) fn;
1291 lhs = fon.getDest();
1292 rhs = fon.getLeft();
1294 if (fon.getOp().getOp() == Operation.ASSIGN) {
1296 if (!lhs.getSymbol().startsWith("neverused") && !lhs.getSymbol().startsWith("leftop")
1297 && !lhs.getSymbol().startsWith("rightop")) {
1299 boolean hasWriteEffect = false;
1300 NTuple<Descriptor> rhsHeapPath = computePath(rhs);
1302 if (rhs.getType().getExtension() instanceof SSJavaType
1303 && lhs.getType().getExtension() instanceof SSJavaType) {
1305 CompositeLocation rhsCompLoc =
1306 ((SSJavaType) rhs.getType().getExtension()).getCompLoc();
1308 CompositeLocation lhsCompLoc =
1309 ((SSJavaType) lhs.getType().getExtension()).getCompLoc();
1311 if (lhsCompLoc != rhsCompLoc) {
1312 // have a write effect!
1313 hasWriteEffect = true;
1316 } else if (lhs.getType().isImmutable()) {
1317 hasWriteEffect = true;
1320 if (hasWriteEffect) {
1322 NTuple<Descriptor> lhsPath = new NTuple<Descriptor>();
1324 Location lhsLoc = getLocation(lhs);
1325 if (ssjava.isSharedLocation(lhsLoc)) {
1327 NTuple<Descriptor> varHeapPath = computePath(lhs);
1328 NTuple<Location> varLocTuple = mapDescriptorToLocationPath.get(lhs);
1330 Set<NTuple<Descriptor>> writtenSet =
1331 mapFlatNodeToSharedLocMapping.get(fn).get(varLocTuple);
1333 if (isCovered(varLocTuple, writtenSet)) {
1334 computeKILLSetForSharedWrite(curr, writtenSet, readWriteKillSet);
1335 computeGENSetForSharedAllCoverWrite(curr, writtenSet, readWriteGenSet);
1337 computeGENSetForSharedNonCoverWrite(curr, varHeapPath, readWriteGenSet);
1342 computeKILLSetForWrite(curr, lhsPath, readWriteKillSet);
1343 computeGENSetForWrite(lhsPath, readWriteGenSet);
1346 // System.out.println("#KILLSET=" + readWriteKillSet);
1347 // System.out.println("#GENSet=" + readWriteGenSet + "\n");
1349 Set<WriteAge> writeAgeSet = curr.get(lhsPath);
1350 checkWriteAgeSet(writeAgeSet, lhsPath, fn);
1360 case FKind.FlatFieldNode:
1361 case FKind.FlatElementNode: {
1363 if (fn.kind() == FKind.FlatFieldNode) {
1364 FlatFieldNode ffn = (FlatFieldNode) fn;
1367 fld = ffn.getField();
1369 FlatElementNode fen = (FlatElementNode) fn;
1372 TypeDescriptor td = rhs.getType().dereference();
1373 fld = getArrayField(td);
1377 NTuple<Descriptor> srcHeapPath = mapHeapPath.get(rhs);
1378 NTuple<Descriptor> fldHeapPath;
1379 if (srcHeapPath != null) {
1380 fldHeapPath = new NTuple<Descriptor>(srcHeapPath.getList());
1382 // if srcHeapPath is null, it is static reference
1383 fldHeapPath = new NTuple<Descriptor>();
1384 fldHeapPath.add(rhs);
1386 fldHeapPath.add(fld);
1388 Set<WriteAge> writeAgeSet = curr.get(fldHeapPath);
1390 checkWriteAgeSet(writeAgeSet, fldHeapPath, fn);
1395 case FKind.FlatSetFieldNode:
1396 case FKind.FlatSetElementNode: {
1398 if (fn.kind() == FKind.FlatSetFieldNode) {
1399 FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
1400 lhs = fsfn.getDst();
1401 fld = fsfn.getField();
1403 FlatSetElementNode fsen = (FlatSetElementNode) fn;
1404 lhs = fsen.getDst();
1405 rhs = fsen.getSrc();
1406 TypeDescriptor td = lhs.getType().dereference();
1407 fld = getArrayField(td);
1410 // System.out.println("FIELD WRITE:" + fn);
1413 NTuple<Descriptor> lhsHeapPath = computePath(lhs);
1414 NTuple<Descriptor> fldHeapPath = new NTuple<Descriptor>(lhsHeapPath.getList());
1415 if (fn.kind() == FKind.FlatSetFieldNode) {
1416 fldHeapPath.add(fld);
1419 // shared loc extension
1420 Location fieldLoc = (Location) fld.getType().getExtension();
1421 if (ssjava.isSharedLocation(fieldLoc)) {
1423 NTuple<Location> fieldLocTuple = new NTuple<Location>();
1424 fieldLocTuple.addAll(mapDescriptorToLocationPath.get(lhs));
1425 if (fn.kind() == FKind.FlatSetFieldNode) {
1426 fieldLocTuple.add(fieldLoc);
1429 Set<NTuple<Descriptor>> writtenSet =
1430 mapFlatNodeToSharedLocMapping.get(fn).get(fieldLocTuple);
1432 if (isCovered(fieldLocTuple, writtenSet)) {
1433 computeKILLSetForSharedWrite(curr, writtenSet, readWriteKillSet);
1434 computeGENSetForSharedAllCoverWrite(curr, writtenSet, readWriteGenSet);
1436 computeGENSetForSharedNonCoverWrite(curr, fldHeapPath, readWriteGenSet);
1440 computeKILLSetForWrite(curr, fldHeapPath, readWriteKillSet);
1441 computeGENSetForWrite(fldHeapPath, readWriteGenSet);
1444 // System.out.println("KILLSET=" + readWriteKillSet);
1445 // System.out.println("GENSet=" + readWriteGenSet);
1450 case FKind.FlatCall: {
1451 FlatCall fc = (FlatCall) fn;
1453 SharedLocMap sharedLocMap = mapFlatNodeToSharedLocMapping.get(fc);
1454 // System.out.println("FLATCALL:" + fn);
1455 generateKILLSetForFlatCall(fc, curr, sharedLocMap, readWriteKillSet);
1456 generateGENSetForFlatCall(fc, sharedLocMap, readWriteGenSet);
1458 // System.out.println("KILLSET=" + readWriteKillSet);
1459 // System.out.println("GENSet=" + readWriteGenSet);
1461 checkManyRead(fc, curr);
1467 computeNewMapping(curr, readWriteKillSet, readWriteGenSet);
1468 // System.out.println("#######" + curr);
1474 private void computeGENSetForSharedNonCoverWrite(
1475 Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr, NTuple<Descriptor> heapPath,
1476 Hashtable<NTuple<Descriptor>, Set<WriteAge>> genSet) {
1478 Set<WriteAge> writeAgeSet = genSet.get(heapPath);
1479 if (writeAgeSet == null) {
1480 writeAgeSet = new HashSet<WriteAge>();
1481 genSet.put(heapPath, writeAgeSet);
1484 writeAgeSet.add(new WriteAge(1));
1488 private void computeGENSetForSharedAllCoverWrite(
1489 Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr, Set<NTuple<Descriptor>> writtenSet,
1490 Hashtable<NTuple<Descriptor>, Set<WriteAge>> genSet) {
1492 for (Iterator iterator = writtenSet.iterator(); iterator.hasNext();) {
1493 NTuple<Descriptor> writeHeapPath = (NTuple<Descriptor>) iterator.next();
1495 Set<WriteAge> writeAgeSet = new HashSet<WriteAge>();
1496 writeAgeSet.add(new WriteAge(0));
1498 genSet.put(writeHeapPath, writeAgeSet);
1503 private void computeKILLSetForSharedWrite(Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr,
1504 Set<NTuple<Descriptor>> writtenSet, Hashtable<NTuple<Descriptor>, Set<WriteAge>> killSet) {
1506 for (Iterator iterator = writtenSet.iterator(); iterator.hasNext();) {
1507 NTuple<Descriptor> writeHeapPath = (NTuple<Descriptor>) iterator.next();
1508 Set<WriteAge> writeSet = curr.get(writeHeapPath);
1509 if (writeSet != null) {
1510 killSet.put(writeHeapPath, writeSet);
1516 private boolean isCovered(NTuple<Location> locTuple, Set<NTuple<Descriptor>> inSet) {
1518 if (inSet == null) {
1522 Set<NTuple<Descriptor>> coverSet =
1523 mapMethodToSharedLocCoverSet.get(methodContainingSSJavaLoop).get(locTuple);
1525 return inSet.containsAll(coverSet);
1528 private void checkManyRead(FlatCall fc, Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr) {
1530 Set<NTuple<Descriptor>> boundReadSet = mapFlatNodeToBoundReadSet.get(fc);
1532 for (Iterator iterator = boundReadSet.iterator(); iterator.hasNext();) {
1533 NTuple<Descriptor> readHeapPath = (NTuple<Descriptor>) iterator.next();
1534 Set<WriteAge> writeAgeSet = curr.get(readHeapPath);
1535 checkWriteAgeSet(writeAgeSet, readHeapPath, fc);
1540 private void checkWriteAgeSet(Set<WriteAge> writeAgeSet, NTuple<Descriptor> path, FlatNode fn) {
1542 // System.out.println("# CHECK WRITE AGE of " + path + " from set=" +
1545 if (writeAgeSet != null) {
1546 for (Iterator iterator = writeAgeSet.iterator(); iterator.hasNext();) {
1547 WriteAge writeAge = (WriteAge) iterator.next();
1548 if (writeAge.getAge() > MAXAGE) {
1549 generateErrorMessage(path, fn);
1555 private void generateErrorMessage(NTuple<Descriptor> path, FlatNode fn) {
1557 Descriptor lastDesc = path.get(getArrayBaseDescriptorIdx(path));
1558 if (ssjava.isSharedLocation(getLocation(lastDesc))) {
1560 NTuple<Location> locPathTuple = getLocationTuple(path);
1561 Set<NTuple<Descriptor>> coverSet =
1562 mapMethodToSharedLocCoverSet.get(methodContainingSSJavaLoop).get(locPathTuple);
1563 throw new Error("Shared memory locations, which is reachable through references " + path
1564 + ", are not completely overwritten by the higher values at "
1565 + methodContainingSSJavaLoop.getClassDesc().getSourceFileName() + "::" + fn.getNumLine()
1566 + ".\nThe following memory locations belong to the same shared locations:" + coverSet);
1570 "Memory location, which is reachable through references "
1572 + ", who comes back to the same read statement without being overwritten at the out-most iteration at "
1573 + methodContainingSSJavaLoop.getClassDesc().getSourceFileName() + "::"
1579 private void generateGENSetForFlatCall(FlatCall fc, SharedLocMap sharedLocMap,
1580 Hashtable<NTuple<Descriptor>, Set<WriteAge>> GENSet) {
1582 Set<NTuple<Descriptor>> boundMayWriteSet = mapFlatNodeToBoundMayWriteSet.get(fc);
1583 // System.out.println("boundMayWriteSet=" + boundMayWriteSet);
1585 for (Iterator iterator = boundMayWriteSet.iterator(); iterator.hasNext();) {
1586 NTuple<Descriptor> heapPath = (NTuple<Descriptor>) iterator.next();
1588 if (!isSharedLocation(heapPath)) {
1589 addWriteAgeToSet(heapPath, GENSet, new WriteAge(0));
1591 // if the current heap path is shared location
1593 NTuple<Location> locTuple = getLocationTuple(heapPath);
1595 Set<NTuple<Descriptor>> sharedWriteHeapPathSet = sharedLocMap.get(locTuple);
1597 if (isCovered(locTuple, sharedLocMap.get(locTuple))) {
1598 // if it is covered, add all of heap paths belong to the same shared
1599 // loc with write age 0
1601 for (Iterator iterator2 = sharedWriteHeapPathSet.iterator(); iterator2.hasNext();) {
1602 NTuple<Descriptor> sharedHeapPath = (NTuple<Descriptor>) iterator2.next();
1603 addWriteAgeToSet(sharedHeapPath, GENSet, new WriteAge(0));
1607 // if not covered, add write age 1 to the heap path that is
1608 // may-written but not covered
1609 addWriteAgeToSet(heapPath, GENSet, new WriteAge(1));
1618 private void addWriteAgeToSet(NTuple<Descriptor> heapPath,
1619 Hashtable<NTuple<Descriptor>, Set<WriteAge>> map, WriteAge age) {
1621 Set<WriteAge> currSet = map.get(heapPath);
1622 if (currSet == null) {
1623 currSet = new HashSet<WriteAge>();
1624 map.put(heapPath, currSet);
1630 private void generateKILLSetForFlatCall(FlatCall fc,
1631 Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr, SharedLocMap sharedLocMap,
1632 Hashtable<NTuple<Descriptor>, Set<WriteAge>> KILLSet) {
1634 Set<NTuple<Descriptor>> boundMustWriteSet = mapFlatNodeToBoundMustWriteSet.get(fc);
1635 // System.out.println("boundMustWriteSet=" + boundMustWriteSet);
1637 for (Iterator iterator = boundMustWriteSet.iterator(); iterator.hasNext();) {
1638 NTuple<Descriptor> heapPath = (NTuple<Descriptor>) iterator.next();
1640 if (isSharedLocation(heapPath)) {
1641 NTuple<Location> locTuple = getLocationTuple(heapPath);
1643 if (isCovered(locTuple, sharedLocMap.get(locTuple))) {
1644 // if it is shared loc and corresponding shared loc has been covered
1645 KILLSet.put(heapPath, curr.get(heapPath));
1649 for (Enumeration<NTuple<Descriptor>> e = curr.keys(); e.hasMoreElements();) {
1650 NTuple<Descriptor> key = e.nextElement();
1651 if (key.startsWith(heapPath)) {
1652 KILLSet.put(key, curr.get(key));
1662 private int getArrayBaseDescriptorIdx(NTuple<Descriptor> heapPath) {
1664 for (int i = heapPath.size() - 1; i > 1; i--) {
1665 if (!heapPath.get(i).getSymbol().equals(arrayElementFieldName)) {
1674 private boolean isSharedLocation(NTuple<Descriptor> heapPath) {
1676 Descriptor d = heapPath.get(heapPath.size() - 1);
1678 if (d instanceof FieldDescriptor) {
1681 .isSharedLocation(getLocation(heapPath.get(getArrayBaseDescriptorIdx(heapPath))));
1684 return ssjava.isSharedLocation(getLocation(heapPath.get(heapPath.size() - 1)));
1688 private NTuple<Location> getLocationTuple(NTuple<Descriptor> heapPath) {
1690 NTuple<Location> locTuple = new NTuple<Location>();
1692 locTuple.addAll(mapDescriptorToLocationPath.get(heapPath.get(0)));
1694 for (int i = 1; i <= getArrayBaseDescriptorIdx(heapPath); i++) {
1695 locTuple.add(getLocation(heapPath.get(i)));
1701 private void computeNewMapping(Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr,
1702 Hashtable<NTuple<Descriptor>, Set<WriteAge>> KILLSet,
1703 Hashtable<NTuple<Descriptor>, Set<WriteAge>> GENSet) {
1705 for (Enumeration<NTuple<Descriptor>> e = KILLSet.keys(); e.hasMoreElements();) {
1706 NTuple<Descriptor> key = e.nextElement();
1708 Set<WriteAge> writeAgeSet = curr.get(key);
1709 if (writeAgeSet == null) {
1710 writeAgeSet = new HashSet<WriteAge>();
1711 curr.put(key, writeAgeSet);
1713 writeAgeSet.removeAll(KILLSet.get(key));
1716 for (Enumeration<NTuple<Descriptor>> e = GENSet.keys(); e.hasMoreElements();) {
1717 NTuple<Descriptor> key = e.nextElement();
1719 Set<WriteAge> currWriteAgeSet = curr.get(key);
1720 if (currWriteAgeSet == null) {
1721 currWriteAgeSet = new HashSet<WriteAge>();
1722 curr.put(key, currWriteAgeSet);
1724 currWriteAgeSet.addAll(GENSet.get(key));
1729 private void computeGENSetForWrite(NTuple<Descriptor> fldHeapPath,
1730 Hashtable<NTuple<Descriptor>, Set<WriteAge>> GENSet) {
1732 // generate write age 0 for the field being written to
1733 Set<WriteAge> writeAgeSet = new HashSet<WriteAge>();
1734 writeAgeSet.add(new WriteAge(0));
1735 GENSet.put(fldHeapPath, writeAgeSet);
1739 private void computeKILLSetForWrite(Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr,
1740 NTuple<Descriptor> hp, Hashtable<NTuple<Descriptor>, Set<WriteAge>> KILLSet) {
1742 // removes all of heap path that starts with prefix 'hp'
1743 // since any reference overwrite along heap path gives overwriting side
1744 // effects on the value
1746 Set<NTuple<Descriptor>> keySet = curr.keySet();
1747 for (Iterator<NTuple<Descriptor>> iter = keySet.iterator(); iter.hasNext();) {
1748 NTuple<Descriptor> key = iter.next();
1749 if (key.startsWith(hp)) {
1750 KILLSet.put(key, curr.get(key));
1756 private void bindHeapPathCallerArgWithCalleeParam(FlatCall fc) {
1757 // compute all possible callee set
1758 // transform all READ/WRITE set from the any possible
1759 // callees to the caller
1760 calleeUnionBoundReadSet.clear();
1761 calleeIntersectBoundMustWriteSet.clear();
1762 calleeUnionBoundMayWriteSet.clear();
1764 if (ssjava.isSSJavaUtil(fc.getMethod().getClassDesc())) {
1765 // ssjava util case!
1766 // have write effects on the first argument
1767 TempDescriptor arg = fc.getArg(0);
1768 NTuple<Descriptor> argHeapPath = computePath(arg);
1769 calleeIntersectBoundMustWriteSet.add(argHeapPath);
1770 calleeUnionBoundMayWriteSet.add(argHeapPath);
1772 MethodDescriptor mdCallee = fc.getMethod();
1773 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
1774 setPossibleCallees.addAll(callGraph.getMethods(mdCallee));
1776 // create mapping from arg idx to its heap paths
1777 Hashtable<Integer, NTuple<Descriptor>> mapArgIdx2CallerArgHeapPath =
1778 new Hashtable<Integer, NTuple<Descriptor>>();
1780 // arg idx is starting from 'this' arg
1781 if (fc.getThis() != null) {
1782 NTuple<Descriptor> thisHeapPath = mapHeapPath.get(fc.getThis());
1783 if (thisHeapPath == null) {
1784 // method is called without creating new flat node representing 'this'
1785 thisHeapPath = new NTuple<Descriptor>();
1786 thisHeapPath.add(fc.getThis());
1789 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(0), thisHeapPath);
1792 for (int i = 0; i < fc.numArgs(); i++) {
1793 TempDescriptor arg = fc.getArg(i);
1794 NTuple<Descriptor> argHeapPath = computePath(arg);
1795 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(i + 1), argHeapPath);
1798 for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
1799 MethodDescriptor callee = (MethodDescriptor) iterator.next();
1800 FlatMethod calleeFlatMethod = state.getMethodFlat(callee);
1802 // binding caller's args and callee's params
1804 Set<NTuple<Descriptor>> calleeReadSet = mapFlatMethodToReadSet.get(calleeFlatMethod);
1805 if (calleeReadSet == null) {
1806 calleeReadSet = new HashSet<NTuple<Descriptor>>();
1807 mapFlatMethodToReadSet.put(calleeFlatMethod, calleeReadSet);
1810 Set<NTuple<Descriptor>> calleeMustWriteSet =
1811 mapFlatMethodToMustWriteSet.get(calleeFlatMethod);
1813 if (calleeMustWriteSet == null) {
1814 calleeMustWriteSet = new HashSet<NTuple<Descriptor>>();
1815 mapFlatMethodToMustWriteSet.put(calleeFlatMethod, calleeMustWriteSet);
1818 Set<NTuple<Descriptor>> calleeMayWriteSet =
1819 mapFlatMethodToMayWriteSet.get(calleeFlatMethod);
1821 if (calleeMayWriteSet == null) {
1822 calleeMayWriteSet = new HashSet<NTuple<Descriptor>>();
1823 mapFlatMethodToMayWriteSet.put(calleeFlatMethod, calleeMayWriteSet);
1826 Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc =
1827 new Hashtable<Integer, TempDescriptor>();
1829 if (calleeFlatMethod.getMethod().isStatic()) {
1830 // static method does not have implicit 'this' arg
1833 for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
1834 TempDescriptor param = calleeFlatMethod.getParameter(i);
1835 mapParamIdx2ParamTempDesc.put(Integer.valueOf(i + offset), param);
1838 Set<NTuple<Descriptor>> calleeBoundReadSet =
1839 bindSet(calleeReadSet, mapParamIdx2ParamTempDesc, mapArgIdx2CallerArgHeapPath);
1840 // union of the current read set and the current callee's
1842 calleeUnionBoundReadSet.addAll(calleeBoundReadSet);
1844 Set<NTuple<Descriptor>> calleeBoundMustWriteSet =
1845 bindSet(calleeMustWriteSet, mapParamIdx2ParamTempDesc, mapArgIdx2CallerArgHeapPath);
1846 // intersection of the current overwrite set and the current
1849 merge(calleeIntersectBoundMustWriteSet, calleeBoundMustWriteSet);
1851 Set<NTuple<Descriptor>> boundWriteSetFromCallee =
1852 bindSet(calleeMayWriteSet, mapParamIdx2ParamTempDesc, mapArgIdx2CallerArgHeapPath);
1853 calleeUnionBoundMayWriteSet.addAll(boundWriteSetFromCallee);
1860 private void bindHeapPathCallerArgWithCaleeParamForSharedLoc(MethodDescriptor mdCaller,
1863 calleeIntersectBoundSharedSet.clear();
1864 calleeUnionBoundDeleteSet.clear();
1866 if (ssjava.isSSJavaUtil(fc.getMethod().getClassDesc())) {
1867 // ssjava util case!
1868 // have write effects on the first argument
1869 TempDescriptor arg = fc.getArg(0);
1870 NTuple<Descriptor> argHeapPath = computePath(arg);
1872 // convert heap path to location path
1873 NTuple<Location> argLocTuple = new NTuple<Location>();
1874 argLocTuple.addAll(deriveLocationTuple(mdCaller, (TempDescriptor) argHeapPath.get(0)));
1875 for (int i = 1; i < argHeapPath.size(); i++) {
1876 argLocTuple.add(getLocation(argHeapPath.get(i)));
1879 calleeIntersectBoundSharedSet.addWrite(argLocTuple, argHeapPath);
1883 // if arg is not primitive type, we need to propagate maywritten set to
1884 // the caller's location path
1886 MethodDescriptor mdCallee = fc.getMethod();
1887 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
1888 setPossibleCallees.addAll(callGraph.getMethods(mdCallee));
1890 // create mapping from arg idx to its heap paths
1891 Hashtable<Integer, NTuple<Descriptor>> mapArgIdx2CallerArgHeapPath =
1892 new Hashtable<Integer, NTuple<Descriptor>>();
1894 // arg idx is starting from 'this' arg
1895 if (fc.getThis() != null) {
1896 NTuple<Descriptor> thisHeapPath = mapHeapPath.get(fc.getThis());
1897 if (thisHeapPath == null) {
1898 // method is called without creating new flat node representing 'this'
1899 thisHeapPath = new NTuple<Descriptor>();
1900 thisHeapPath.add(fc.getThis());
1903 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(0), thisHeapPath);
1906 for (int i = 0; i < fc.numArgs(); i++) {
1907 TempDescriptor arg = fc.getArg(i);
1908 NTuple<Descriptor> argHeapPath = computePath(arg);
1909 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(i + 1), argHeapPath);
1912 // create mapping from arg idx to its location paths
1913 Hashtable<Integer, NTuple<Location>> mapArgIdx2CallerAgLocationPath =
1914 new Hashtable<Integer, NTuple<Location>>();
1916 // arg idx is starting from 'this' arg
1917 if (fc.getThis() != null) {
1918 NTuple<Location> thisLocationPath = deriveLocationTuple(mdCaller, fc.getThis());
1919 if (thisLocationPath != null) {
1920 mapArgIdx2CallerAgLocationPath.put(Integer.valueOf(0), thisLocationPath);
1924 for (int i = 0; i < fc.numArgs(); i++) {
1925 TempDescriptor arg = fc.getArg(i);
1926 NTuple<Location> argLocationPath = deriveLocationTuple(mdCaller, arg);
1927 if (argLocationPath != null) {
1928 mapArgIdx2CallerAgLocationPath.put(Integer.valueOf(i + 1), argLocationPath);
1932 for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
1933 MethodDescriptor callee = (MethodDescriptor) iterator.next();
1934 FlatMethod calleeFlatMethod = state.getMethodFlat(callee);
1936 // binding caller's args and callee's params
1938 Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc =
1939 new Hashtable<Integer, TempDescriptor>();
1941 if (calleeFlatMethod.getMethod().isStatic()) {
1942 // static method does not have implicit 'this' arg
1945 for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
1946 TempDescriptor param = calleeFlatMethod.getParameter(i);
1947 mapParamIdx2ParamTempDesc.put(Integer.valueOf(i + offset), param);
1950 Set<Integer> keySet = mapArgIdx2CallerAgLocationPath.keySet();
1951 for (Iterator iterator2 = keySet.iterator(); iterator2.hasNext();) {
1952 Integer idx = (Integer) iterator2.next();
1953 NTuple<Location> callerArgLocationPath = mapArgIdx2CallerAgLocationPath.get(idx);
1954 NTuple<Descriptor> callerArgHeapPath = mapArgIdx2CallerArgHeapPath.get(idx);
1956 TempDescriptor calleeParam = mapParamIdx2ParamTempDesc.get(idx);
1957 NTuple<Location> calleeLocationPath = deriveLocationTuple(mdCallee, calleeParam);
1958 SharedLocMap calleeDeleteSet = mapFlatMethodToDeleteSet.get(calleeFlatMethod);
1959 SharedLocMap calleeSharedLocMap = mapFlatMethodToSharedLocMap.get(calleeFlatMethod);
1961 if (calleeDeleteSet != null) {
1962 createNewMappingOfDeleteSet(callerArgLocationPath, callerArgHeapPath,
1963 calleeLocationPath, calleeDeleteSet);
1966 if (calleeSharedLocMap != null) {
1967 createNewMappingOfSharedSet(callerArgLocationPath, callerArgHeapPath,
1968 calleeLocationPath, calleeSharedLocMap);
1978 private void createNewMappingOfDeleteSet(NTuple<Location> callerArgLocationPath,
1979 NTuple<Descriptor> callerArgHeapPath, NTuple<Location> calleeLocationPath,
1980 SharedLocMap calleeDeleteSet) {
1982 SharedLocMap calleeParamDeleteSet = calleeDeleteSet.getHeapPathStartedWith(calleeLocationPath);
1984 Set<NTuple<Location>> keySet = calleeParamDeleteSet.keySet();
1985 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1986 NTuple<Location> calleeLocTupleKey = (NTuple<Location>) iterator.next();
1987 Set<NTuple<Descriptor>> heapPathSet = calleeParamDeleteSet.get(calleeLocTupleKey);
1988 for (Iterator iterator2 = heapPathSet.iterator(); iterator2.hasNext();) {
1989 NTuple<Descriptor> calleeHeapPath = (NTuple<Descriptor>) iterator2.next();
1990 calleeUnionBoundDeleteSet.addWrite(
1991 bindLocationPath(callerArgLocationPath, calleeLocTupleKey),
1992 bindHeapPath(callerArgHeapPath, calleeHeapPath));
1998 private void createNewMappingOfSharedSet(NTuple<Location> callerArgLocationPath,
1999 NTuple<Descriptor> callerArgHeapPath, NTuple<Location> calleeLocationPath,
2000 SharedLocMap calleeSharedLocMap) {
2002 SharedLocMap calleeParamSharedSet =
2003 calleeSharedLocMap.getHeapPathStartedWith(calleeLocationPath);
2005 Set<NTuple<Location>> keySet = calleeParamSharedSet.keySet();
2006 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2007 NTuple<Location> calleeLocTupleKey = (NTuple<Location>) iterator.next();
2008 Set<NTuple<Descriptor>> heapPathSet = calleeParamSharedSet.get(calleeLocTupleKey);
2009 Set<NTuple<Descriptor>> boundHeapPathSet = new HashSet<NTuple<Descriptor>>();
2010 for (Iterator iterator2 = heapPathSet.iterator(); iterator2.hasNext();) {
2011 NTuple<Descriptor> calleeHeapPath = (NTuple<Descriptor>) iterator2.next();
2012 boundHeapPathSet.add(bindHeapPath(callerArgHeapPath, calleeHeapPath));
2014 calleeIntersectBoundSharedSet.intersect(
2015 bindLocationPath(callerArgLocationPath, calleeLocTupleKey), boundHeapPathSet);
2020 private NTuple<Location> bindLocationPath(NTuple<Location> start, NTuple<Location> end) {
2021 NTuple<Location> locPath = new NTuple<Location>();
2022 locPath.addAll(start);
2023 for (int i = 1; i < end.size(); i++) {
2024 locPath.add(end.get(i));
2029 private NTuple<Descriptor> bindHeapPath(NTuple<Descriptor> start, NTuple<Descriptor> end) {
2030 NTuple<Descriptor> heapPath = new NTuple<Descriptor>();
2031 heapPath.addAll(start);
2032 for (int i = 1; i < end.size(); i++) {
2033 heapPath.add(end.get(i));
2038 private void initialize() {
2039 // First, identify ssjava loop entrace
2041 // no need to analyze method having ssjava loop
2042 methodContainingSSJavaLoop = ssjava.getMethodContainingSSJavaLoop();
2044 FlatMethod fm = state.getMethodFlat(methodContainingSSJavaLoop);
2045 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
2046 flatNodesToVisit.add(fm);
2048 LoopFinder loopFinder = new LoopFinder(fm);
2050 while (!flatNodesToVisit.isEmpty()) {
2051 FlatNode fn = flatNodesToVisit.iterator().next();
2052 flatNodesToVisit.remove(fn);
2054 String label = (String) state.fn2labelMap.get(fn);
2055 if (label != null) {
2057 if (label.equals(ssjava.SSJAVA)) {
2058 ssjava.setSSJavaLoopEntrance(fn);
2063 for (int i = 0; i < fn.numNext(); i++) {
2064 FlatNode nn = fn.getNext(i);
2065 flatNodesToVisit.add(nn);
2069 assert ssjava.getSSJavaLoopEntrance() != null;
2071 // assume that ssjava loop is top-level loop in method, not nested loop
2072 Set nestedLoop = loopFinder.nestedLoops();
2073 for (Iterator loopIter = nestedLoop.iterator(); loopIter.hasNext();) {
2074 LoopFinder lf = (LoopFinder) loopIter.next();
2075 if (lf.loopEntrances().iterator().next().equals(ssjava.getSSJavaLoopEntrance())) {
2080 assert ssjavaLoop != null;
2082 loopIncElements = (Set<FlatNode>) ssjavaLoop.loopIncElements();
2084 // perform topological sort over the set of methods accessed by the main
2086 Set<MethodDescriptor> methodDescriptorsToAnalyze = new HashSet<MethodDescriptor>();
2087 methodDescriptorsToAnalyze.addAll(ssjava.getAnnotationRequireSet());
2088 sortedDescriptors = topologicalSort(methodDescriptorsToAnalyze);
2091 private void methodReadWriteSetAnalysis() {
2092 // perform method READ/OVERWRITE analysis
2093 LinkedList<MethodDescriptor> descriptorListToAnalyze =
2094 (LinkedList<MethodDescriptor>) sortedDescriptors.clone();
2096 // current descriptors to visit in fixed-point interprocedural analysis,
2098 // dependency in the call graph
2099 methodDescriptorsToVisitStack.clear();
2101 descriptorListToAnalyze.removeFirst();
2103 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
2104 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
2106 while (!descriptorListToAnalyze.isEmpty()) {
2107 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
2108 methodDescriptorsToVisitStack.add(md);
2111 // analyze scheduled methods until there are no more to visit
2112 while (!methodDescriptorsToVisitStack.isEmpty()) {
2113 // start to analyze leaf node
2114 MethodDescriptor md = methodDescriptorsToVisitStack.pop();
2115 FlatMethod fm = state.getMethodFlat(md);
2117 Set<NTuple<Descriptor>> readSet = new HashSet<NTuple<Descriptor>>();
2118 Set<NTuple<Descriptor>> mustWriteSet = new HashSet<NTuple<Descriptor>>();
2119 Set<NTuple<Descriptor>> mayWriteSet = new HashSet<NTuple<Descriptor>>();
2121 methodReadWriteSet_analyzeMethod(fm, readSet, mustWriteSet, mayWriteSet);
2123 Set<NTuple<Descriptor>> prevRead = mapFlatMethodToReadSet.get(fm);
2124 Set<NTuple<Descriptor>> prevMustWrite = mapFlatMethodToMustWriteSet.get(fm);
2125 Set<NTuple<Descriptor>> prevMayWrite = mapFlatMethodToMayWriteSet.get(fm);
2127 if (!(readSet.equals(prevRead) && mustWriteSet.equals(prevMustWrite) && mayWriteSet
2128 .equals(prevMayWrite))) {
2129 mapFlatMethodToReadSet.put(fm, readSet);
2130 mapFlatMethodToMustWriteSet.put(fm, mustWriteSet);
2131 mapFlatMethodToMayWriteSet.put(fm, mayWriteSet);
2133 // results for callee changed, so enqueue dependents caller for
2136 Iterator<MethodDescriptor> depsItr = getDependents(md).iterator();
2137 while (depsItr.hasNext()) {
2138 MethodDescriptor methodNext = depsItr.next();
2139 if (!methodDescriptorsToVisitStack.contains(methodNext)
2140 && methodDescriptorToVistSet.contains(methodNext)) {
2141 methodDescriptorsToVisitStack.add(methodNext);
2150 methodReadWriteSetAnalysisToEventLoopBody();
2154 private void methodReadWriteSet_analyzeMethod(FlatMethod fm, Set<NTuple<Descriptor>> readSet,
2155 Set<NTuple<Descriptor>> mustWriteSet, Set<NTuple<Descriptor>> mayWriteSet) {
2156 if (state.SSJAVADEBUG) {
2157 System.out.println("SSJAVA: Definitely written Analyzing: " + fm);
2160 methodReadWriteSet_analyzeBody(fm, readSet, mustWriteSet, mayWriteSet, false);
2164 private void methodReadWriteSetAnalysisToEventLoopBody() {
2166 // perform method read/write analysis for Event Loop Body
2168 FlatMethod flatMethodContainingSSJavaLoop = state.getMethodFlat(methodContainingSSJavaLoop);
2170 if (state.SSJAVADEBUG) {
2171 System.out.println("SSJAVA: Definitely written Event Loop Analyzing: "
2172 + flatMethodContainingSSJavaLoop);
2175 Set<NTuple<Descriptor>> readSet = new HashSet<NTuple<Descriptor>>();
2176 Set<NTuple<Descriptor>> mustWriteSet = new HashSet<NTuple<Descriptor>>();
2177 Set<NTuple<Descriptor>> mayWriteSet = new HashSet<NTuple<Descriptor>>();
2179 mapFlatMethodToReadSet.put(flatMethodContainingSSJavaLoop, readSet);
2180 mapFlatMethodToMustWriteSet.put(flatMethodContainingSSJavaLoop, mustWriteSet);
2181 mapFlatMethodToMayWriteSet.put(flatMethodContainingSSJavaLoop, mayWriteSet);
2183 methodReadWriteSet_analyzeBody(ssjava.getSSJavaLoopEntrance(), readSet, mustWriteSet,
2188 private void methodReadWriteSet_analyzeBody(FlatNode startNode, Set<NTuple<Descriptor>> readSet,
2189 Set<NTuple<Descriptor>> mustWriteSet, Set<NTuple<Descriptor>> mayWriteSet,
2190 boolean isEventLoopBody) {
2192 // intraprocedural analysis
2193 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
2194 flatNodesToVisit.add(startNode);
2196 while (!flatNodesToVisit.isEmpty()) {
2197 FlatNode fn = flatNodesToVisit.iterator().next();
2198 flatNodesToVisit.remove(fn);
2200 Set<NTuple<Descriptor>> currMustWriteSet = new HashSet<NTuple<Descriptor>>();
2202 for (int i = 0; i < fn.numPrev(); i++) {
2203 FlatNode prevFn = fn.getPrev(i);
2204 Set<NTuple<Descriptor>> in = mapFlatNodeToMustWriteSet.get(prevFn);
2206 merge(currMustWriteSet, in);
2210 methodReadWriteSet_nodeActions(fn, currMustWriteSet, readSet, mustWriteSet, mayWriteSet,
2213 Set<NTuple<Descriptor>> mustSetPrev = mapFlatNodeToMustWriteSet.get(fn);
2215 if (!currMustWriteSet.equals(mustSetPrev)) {
2216 mapFlatNodeToMustWriteSet.put(fn, currMustWriteSet);
2217 for (int i = 0; i < fn.numNext(); i++) {
2218 FlatNode nn = fn.getNext(i);
2219 if ((!isEventLoopBody) || loopIncElements.contains(nn)) {
2220 flatNodesToVisit.add(nn);
2230 private void methodReadWriteSet_nodeActions(FlatNode fn,
2231 Set<NTuple<Descriptor>> currMustWriteSet, Set<NTuple<Descriptor>> readSet,
2232 Set<NTuple<Descriptor>> mustWriteSet, Set<NTuple<Descriptor>> mayWriteSet,
2233 boolean isEventLoopBody) {
2237 FieldDescriptor fld;
2239 switch (fn.kind()) {
2240 case FKind.FlatMethod: {
2242 // set up initial heap paths for method parameters
2243 FlatMethod fm = (FlatMethod) fn;
2244 for (int i = 0; i < fm.numParameters(); i++) {
2245 TempDescriptor param = fm.getParameter(i);
2246 NTuple<Descriptor> heapPath = new NTuple<Descriptor>();
2247 heapPath.add(param);
2248 mapHeapPath.put(param, heapPath);
2253 case FKind.FlatOpNode: {
2254 FlatOpNode fon = (FlatOpNode) fn;
2255 // for a normal assign node, need to propagate lhs's heap path to
2258 if (fon.getOp().getOp() == Operation.ASSIGN) {
2259 rhs = fon.getLeft();
2260 lhs = fon.getDest();
2262 NTuple<Descriptor> rhsHeapPath = mapHeapPath.get(rhs);
2264 if (lhs.getType().isPrimitive()) {
2265 NTuple<Descriptor> lhsHeapPath = new NTuple<Descriptor>();
2266 lhsHeapPath.add(lhs);
2267 mapHeapPath.put(lhs, lhsHeapPath);
2268 } else if (rhsHeapPath != null) {
2269 mapHeapPath.put(lhs, mapHeapPath.get(rhs));
2271 NTuple<Descriptor> heapPath = new NTuple<Descriptor>();
2273 mapHeapPath.put(lhs, heapPath);
2276 // shared loc extension
2277 if (isEventLoopBody) {
2278 if (!lhs.getSymbol().startsWith("neverused") && rhs.getType().isImmutable()) {
2280 if (rhs.getType().getExtension() instanceof Location
2281 && lhs.getType().getExtension() instanceof CompositeLocation) {
2283 Location rhsLoc = (Location) rhs.getType().getExtension();
2285 CompositeLocation lhsCompLoc = (CompositeLocation) lhs.getType().getExtension();
2286 Location dstLoc = lhsCompLoc.get(lhsCompLoc.getSize() - 1);
2288 NTuple<Descriptor> heapPath = new NTuple<Descriptor>();
2289 for (int i = 0; i < rhsHeapPath.size() - 1; i++) {
2290 heapPath.add(rhsHeapPath.get(i));
2293 NTuple<Descriptor> writeHeapPath = new NTuple<Descriptor>();
2294 writeHeapPath.addAll(heapPath);
2295 writeHeapPath.add(lhs);
2305 case FKind.FlatElementNode:
2306 case FKind.FlatFieldNode: {
2310 if (fn.kind() == FKind.FlatFieldNode) {
2311 FlatFieldNode ffn = (FlatFieldNode) fn;
2314 fld = ffn.getField();
2316 FlatElementNode fen = (FlatElementNode) fn;
2319 TypeDescriptor td = rhs.getType().dereference();
2320 fld = getArrayField(td);
2323 if (fld.isFinal()) {
2324 // if field is final no need to check
2329 NTuple<Descriptor> srcHeapPath = mapHeapPath.get(rhs);
2330 if (srcHeapPath != null) {
2331 // if lhs srcHeapPath is null, it means that it is not reachable from
2332 // callee's parameters. so just ignore it
2334 NTuple<Descriptor> readingHeapPath = new NTuple<Descriptor>(srcHeapPath.getList());
2335 if (fn.kind() == FKind.FlatFieldNode) {
2336 readingHeapPath.add(fld);
2339 mapHeapPath.put(lhs, readingHeapPath);
2342 if (fld.getType().isImmutable()) {
2343 // if WT doesnot have hp(x.f), add hp(x.f) to READ
2344 if (!currMustWriteSet.contains(readingHeapPath)) {
2345 readSet.add(readingHeapPath);
2349 // no need to kill hp(x.f) from WT
2355 case FKind.FlatSetFieldNode:
2356 case FKind.FlatSetElementNode: {
2360 if (fn.kind() == FKind.FlatSetFieldNode) {
2361 FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
2362 lhs = fsfn.getDst();
2363 fld = fsfn.getField();
2364 rhs = fsfn.getSrc();
2366 FlatSetElementNode fsen = (FlatSetElementNode) fn;
2367 lhs = fsen.getDst();
2368 rhs = fsen.getSrc();
2369 TypeDescriptor td = lhs.getType().dereference();
2370 fld = getArrayField(td);
2374 NTuple<Descriptor> lhsHeapPath = mapHeapPath.get(lhs);
2376 if (lhsHeapPath != null) {
2377 // if lhs heap path is null, it means that it is not reachable from
2378 // callee's parameters. so just ignore it
2379 NTuple<Descriptor> fldHeapPath = new NTuple<Descriptor>(lhsHeapPath.getList());
2380 if (fn.kind() != FKind.FlatSetElementNode) {
2381 fldHeapPath.add(fld);
2383 // mapHeapPath.put(fld, fldHeapPath);
2386 // need to add hp(y) to WT
2387 if (fn.kind() != FKind.FlatSetElementNode) {
2388 currMustWriteSet.add(fldHeapPath);
2390 mayWriteSet.add(fldHeapPath);
2397 case FKind.FlatCall: {
2399 FlatCall fc = (FlatCall) fn;
2401 bindHeapPathCallerArgWithCalleeParam(fc);
2403 Set<NTuple<Descriptor>> boundReadSet = new HashSet<NTuple<Descriptor>>();
2404 boundReadSet.addAll(calleeUnionBoundReadSet);
2406 Set<NTuple<Descriptor>> boundMustWriteSet = new HashSet<NTuple<Descriptor>>();
2407 boundMustWriteSet.addAll(calleeIntersectBoundMustWriteSet);
2409 Set<NTuple<Descriptor>> boundMayWriteSet = new HashSet<NTuple<Descriptor>>();
2410 boundMayWriteSet.addAll(calleeUnionBoundMayWriteSet);
2412 mapFlatNodeToBoundReadSet.put(fn, boundReadSet);
2413 mapFlatNodeToBoundMustWriteSet.put(fn, boundMustWriteSet);
2414 mapFlatNodeToBoundMayWriteSet.put(fn, boundMayWriteSet);
2416 // add heap path, which is an element of READ_bound set and is not
2418 // element of WT set, to the caller's READ set
2419 for (Iterator iterator = calleeUnionBoundReadSet.iterator(); iterator.hasNext();) {
2420 NTuple<Descriptor> read = (NTuple<Descriptor>) iterator.next();
2421 if (!currMustWriteSet.contains(read)) {
2426 // add heap path, which is an element of OVERWRITE_bound set, to the
2428 for (Iterator iterator = calleeIntersectBoundMustWriteSet.iterator(); iterator.hasNext();) {
2429 NTuple<Descriptor> write = (NTuple<Descriptor>) iterator.next();
2430 currMustWriteSet.add(write);
2433 // add heap path, which is an element of WRITE_BOUND set, to the
2434 // caller's writeSet
2435 for (Iterator iterator = calleeUnionBoundMayWriteSet.iterator(); iterator.hasNext();) {
2436 NTuple<Descriptor> write = (NTuple<Descriptor>) iterator.next();
2437 mayWriteSet.add(write);
2443 case FKind.FlatExit: {
2444 // merge the current written set with OVERWRITE set
2445 merge(mustWriteSet, currMustWriteSet);
2453 static public FieldDescriptor getArrayField(TypeDescriptor td) {
2454 FieldDescriptor fd = mapTypeToArrayField.get(td);
2457 new FieldDescriptor(new Modifiers(Modifiers.PUBLIC), td, arrayElementFieldName, null,
2459 mapTypeToArrayField.put(td, fd);
2464 private void merge(Set<NTuple<Descriptor>> curr, Set<NTuple<Descriptor>> in) {
2465 if (curr.isEmpty()) {
2466 // set has a special initial value which covers all possible
2468 // For the first time of intersection, we can take all previous set
2471 // otherwise, current set is the intersection of the two sets
2477 // combine two heap path
2478 private NTuple<Descriptor> combine(NTuple<Descriptor> callerIn, NTuple<Descriptor> calleeIn) {
2479 NTuple<Descriptor> combined = new NTuple<Descriptor>();
2481 for (int i = 0; i < callerIn.size(); i++) {
2482 combined.add(callerIn.get(i));
2485 // the first element of callee's heap path represents parameter
2486 // so we skip the first one since it is already added from caller's heap
2488 for (int i = 1; i < calleeIn.size(); i++) {
2489 combined.add(calleeIn.get(i));
2495 private Set<NTuple<Descriptor>> bindSet(Set<NTuple<Descriptor>> calleeSet,
2496 Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc,
2497 Hashtable<Integer, NTuple<Descriptor>> mapCallerArgIdx2HeapPath) {
2499 Set<NTuple<Descriptor>> boundedCalleeSet = new HashSet<NTuple<Descriptor>>();
2501 Set<Integer> keySet = mapCallerArgIdx2HeapPath.keySet();
2502 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2503 Integer idx = (Integer) iterator.next();
2505 NTuple<Descriptor> callerArgHeapPath = mapCallerArgIdx2HeapPath.get(idx);
2506 TempDescriptor calleeParam = mapParamIdx2ParamTempDesc.get(idx);
2507 for (Iterator iterator2 = calleeSet.iterator(); iterator2.hasNext();) {
2508 NTuple<Descriptor> element = (NTuple<Descriptor>) iterator2.next();
2509 if (element.startsWith(calleeParam)) {
2510 NTuple<Descriptor> boundElement = combine(callerArgHeapPath, element);
2511 boundedCalleeSet.add(boundElement);
2517 return boundedCalleeSet;
2521 // Borrowed it from disjoint analysis
2522 private LinkedList<MethodDescriptor> topologicalSort(Set<MethodDescriptor> toSort) {
2524 Set<MethodDescriptor> discovered = new HashSet<MethodDescriptor>();
2526 LinkedList<MethodDescriptor> sorted = new LinkedList<MethodDescriptor>();
2528 Iterator<MethodDescriptor> itr = toSort.iterator();
2529 while (itr.hasNext()) {
2530 MethodDescriptor d = itr.next();
2532 if (!discovered.contains(d)) {
2533 dfsVisit(d, toSort, sorted, discovered);
2540 // While we're doing DFS on call graph, remember
2541 // dependencies for efficient queuing of methods
2542 // during interprocedural analysis:
2544 // a dependent of a method decriptor d for this analysis is:
2545 // 1) a method or task that invokes d
2546 // 2) in the descriptorsToAnalyze set
2547 private void dfsVisit(MethodDescriptor md, Set<MethodDescriptor> toSort,
2548 LinkedList<MethodDescriptor> sorted, Set<MethodDescriptor> discovered) {
2552 Iterator itr = callGraph.getCallerSet(md).iterator();
2553 while (itr.hasNext()) {
2554 MethodDescriptor dCaller = (MethodDescriptor) itr.next();
2555 // only consider callers in the original set to analyze
2556 if (!toSort.contains(dCaller)) {
2559 if (!discovered.contains(dCaller)) {
2560 addDependent(md, // callee
2564 dfsVisit(dCaller, toSort, sorted, discovered);
2568 // for leaf-nodes last now!
2572 // a dependent of a method decriptor d for this analysis is:
2573 // 1) a method or task that invokes d
2574 // 2) in the descriptorsToAnalyze set
2575 private void addDependent(MethodDescriptor callee, MethodDescriptor caller) {
2576 Set<MethodDescriptor> deps = mapDescriptorToSetDependents.get(callee);
2578 deps = new HashSet<MethodDescriptor>();
2581 mapDescriptorToSetDependents.put(callee, deps);
2584 private Set<MethodDescriptor> getDependents(MethodDescriptor callee) {
2585 Set<MethodDescriptor> deps = mapDescriptorToSetDependents.get(callee);
2587 deps = new HashSet<MethodDescriptor>();
2588 mapDescriptorToSetDependents.put(callee, deps);
2593 private NTuple<Descriptor> computePath(Descriptor td) {
2594 // generate proper path fot input td
2595 // if td is local variable, it just generate one element tuple path
2596 if (mapHeapPath.containsKey(td)) {
2597 NTuple<Descriptor> rtrHeapPath = new NTuple<Descriptor>();
2598 rtrHeapPath.addAll(mapHeapPath.get(td));
2601 NTuple<Descriptor> rtrHeapPath = new NTuple<Descriptor>();
2602 rtrHeapPath.add(td);
2607 private NTuple<Location> deriveThisLocationTuple(MethodDescriptor md) {
2608 String thisLocIdentifier = ssjava.getMethodLattice(md).getThisLoc();
2609 Location thisLoc = new Location(md, thisLocIdentifier);
2610 NTuple<Location> locTuple = new NTuple<Location>();
2611 locTuple.add(thisLoc);
2615 private NTuple<Location> deriveGlobalLocationTuple(MethodDescriptor md) {
2616 String globalLocIdentifier = ssjava.getMethodLattice(md).getGlobalLoc();
2617 Location globalLoc = new Location(md, globalLocIdentifier);
2618 NTuple<Location> locTuple = new NTuple<Location>();
2619 locTuple.add(globalLoc);
2623 private NTuple<Location> deriveLocationTuple(MethodDescriptor md, TempDescriptor td) {
2625 assert td.getType() != null;
2627 if (mapDescriptorToLocationPath.containsKey(td)) {
2628 NTuple<Location> locPath = mapDescriptorToLocationPath.get(td);
2629 NTuple<Location> rtrPath = new NTuple<Location>();
2630 rtrPath.addAll(locPath);
2633 if (td.getSymbol().startsWith("this")) {
2634 NTuple<Location> thisPath = deriveThisLocationTuple(md);
2636 NTuple<Location> rtrPath = new NTuple<Location>();
2637 rtrPath.addAll(thisPath);
2642 if (td.getType().getExtension() != null) {
2643 SSJavaType ssJavaType = (SSJavaType) td.getType().getExtension();
2644 if (ssJavaType.getCompLoc() != null) {
2645 NTuple<Location> rtrPath = new NTuple<Location>();
2646 rtrPath.addAll(ssJavaType.getCompLoc().getTuple());