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.Liveness;
12 import Analysis.CallGraph.CallGraph;
13 import Analysis.Loops.LoopFinder;
15 import IR.FieldDescriptor;
16 import IR.MethodDescriptor;
19 import IR.TypeDescriptor;
20 import IR.TypeExtension;
22 import IR.Flat.FlatCall;
23 import IR.Flat.FlatElementNode;
24 import IR.Flat.FlatFieldNode;
25 import IR.Flat.FlatLiteralNode;
26 import IR.Flat.FlatMethod;
27 import IR.Flat.FlatNew;
28 import IR.Flat.FlatNode;
29 import IR.Flat.FlatOpNode;
30 import IR.Flat.FlatSetElementNode;
31 import IR.Flat.FlatSetFieldNode;
32 import IR.Flat.TempDescriptor;
33 import IR.Tree.Modifiers;
35 public class DefinitelyWrittenCheck {
37 SSJavaAnalysis ssjava;
45 // maps a descriptor to its known dependents: namely
46 // methods or tasks that call the descriptor's method
47 // AND are part of this analysis (reachable from main)
48 private Hashtable<Descriptor, Set<MethodDescriptor>> mapDescriptorToSetDependents;
50 // maps a flat node to its WrittenSet: this keeps all heap path overwritten
52 private Hashtable<FlatNode, Set<NTuple<Descriptor>>> mapFlatNodeToMustWriteSet;
54 // maps a temp descriptor to its heap path
55 // each temp descriptor has a unique heap path since we do not allow any
57 private Hashtable<Descriptor, NTuple<Descriptor>> mapHeapPath;
59 // maps a temp descriptor to its composite location
60 private Hashtable<TempDescriptor, NTuple<Location>> mapDescriptorToLocationPath;
62 // maps a flat method to the READ that is the set of heap path that is
63 // expected to be written before method invocation
64 private Hashtable<FlatMethod, Set<NTuple<Descriptor>>> mapFlatMethodToReadSet;
66 // maps a flat method to the must-write set that is the set of heap path that
67 // is overwritten on every possible path during method invocation
68 private Hashtable<FlatMethod, Set<NTuple<Descriptor>>> mapFlatMethodToMustWriteSet;
70 // maps a flat method to the DELETE SET that is a set of heap path to shared
72 // written to but not overwritten by the higher value
73 private Hashtable<FlatMethod, SharedLocMap> mapFlatMethodToDeleteSet;
75 // maps a flat method to the S SET that is a set of heap path to shared
76 // locations that are overwritten by the higher value
77 private Hashtable<FlatMethod, SharedLocMap> mapFlatMethodToSharedLocMap;
79 // maps a flat method to the may-wirte set that is the set of heap path that
80 // might be written to
81 private Hashtable<FlatMethod, Set<NTuple<Descriptor>>> mapFlatMethodToMayWriteSet;
83 // maps a call site to the read set contributed by all callees
84 private Hashtable<FlatNode, Set<NTuple<Descriptor>>> mapFlatNodeToBoundReadSet;
86 // maps a call site to the must write set contributed by all callees
87 private Hashtable<FlatNode, Set<NTuple<Descriptor>>> mapFlatNodeToBoundMustWriteSet;
89 // maps a call site to the may read set contributed by all callees
90 private Hashtable<FlatNode, Set<NTuple<Descriptor>>> mapFlatNodeToBoundMayWriteSet;
92 // points to method containing SSJAVA Loop
93 private MethodDescriptor methodContainingSSJavaLoop;
95 // maps a flatnode to definitely written analysis mapping M
96 private Hashtable<FlatNode, Hashtable<NTuple<Descriptor>, Set<WriteAge>>> mapFlatNodetoEventLoopMap;
98 // maps shared location to the set of descriptors which belong to the shared
101 // keep current descriptors to visit in fixed-point interprocedural analysis,
102 private Stack<MethodDescriptor> methodDescriptorsToVisitStack;
104 // when analyzing flatcall, need to re-schedule set of callee
105 private Set<MethodDescriptor> calleesToEnqueue;
107 private Set<ReadSummary> possibleCalleeReadSummarySetToCaller;
109 public static final String arrayElementFieldName = "___element_";
110 static protected Hashtable<TypeDescriptor, FieldDescriptor> mapTypeToArrayField;
112 // maps a method descriptor to the merged incoming caller's current
114 // it is for setting clearance flag when all read set is overwritten
115 private Hashtable<MethodDescriptor, ReadSummary> mapMethodDescriptorToReadSummary;
117 private Hashtable<MethodDescriptor, MultiSourceMap<NTuple<Location>, NTuple<Descriptor>>> mapMethodToSharedLocCoverSet;
119 private Hashtable<FlatNode, SharedLocMap> mapFlatNodeToSharedLocMapping;
120 private Hashtable<FlatNode, SharedLocMap> mapFlatNodeToDeleteSet;
122 private LinkedList<MethodDescriptor> sortedDescriptors;
124 private LoopFinder ssjavaLoop;
125 private Set<FlatNode> loopIncElements;
127 private Set<NTuple<Descriptor>> calleeUnionBoundReadSet;
128 private Set<NTuple<Descriptor>> calleeIntersectBoundMustWriteSet;
129 private Set<NTuple<Descriptor>> calleeUnionBoundMayWriteSet;
130 private SharedLocMap calleeUnionBoundDeleteSet;
131 private SharedLocMap calleeIntersectBoundSharedSet;
133 Set<TempDescriptor> liveInTempSetToEventLoop;
135 private Hashtable<Descriptor, Location> mapDescToLocation;
137 private TempDescriptor LOCAL;
139 public static int MAXAGE = 1;
141 public DefinitelyWrittenCheck(SSJavaAnalysis ssjava, State state) {
143 this.ssjava = ssjava;
144 this.callGraph = ssjava.getCallGraph();
145 this.mapFlatNodeToMustWriteSet = new Hashtable<FlatNode, Set<NTuple<Descriptor>>>();
146 this.mapDescriptorToSetDependents = new Hashtable<Descriptor, Set<MethodDescriptor>>();
147 this.mapHeapPath = new Hashtable<Descriptor, NTuple<Descriptor>>();
148 this.mapDescriptorToLocationPath = new Hashtable<TempDescriptor, NTuple<Location>>();
149 this.mapFlatMethodToReadSet = new Hashtable<FlatMethod, Set<NTuple<Descriptor>>>();
150 this.mapFlatMethodToMustWriteSet = new Hashtable<FlatMethod, Set<NTuple<Descriptor>>>();
151 this.mapFlatMethodToMayWriteSet = new Hashtable<FlatMethod, Set<NTuple<Descriptor>>>();
152 this.mapFlatNodetoEventLoopMap =
153 new Hashtable<FlatNode, Hashtable<NTuple<Descriptor>, Set<WriteAge>>>();
154 this.calleeUnionBoundReadSet = new HashSet<NTuple<Descriptor>>();
155 this.calleeIntersectBoundMustWriteSet = new HashSet<NTuple<Descriptor>>();
156 this.calleeUnionBoundMayWriteSet = new HashSet<NTuple<Descriptor>>();
158 this.methodDescriptorsToVisitStack = new Stack<MethodDescriptor>();
159 this.calleesToEnqueue = new HashSet<MethodDescriptor>();
160 this.mapTypeToArrayField = new Hashtable<TypeDescriptor, FieldDescriptor>();
161 this.LOCAL = new TempDescriptor("LOCAL");
162 this.mapDescToLocation = new Hashtable<Descriptor, Location>();
163 this.possibleCalleeReadSummarySetToCaller = new HashSet<ReadSummary>();
164 this.mapMethodDescriptorToReadSummary = new Hashtable<MethodDescriptor, ReadSummary>();
165 this.mapFlatNodeToBoundReadSet = new Hashtable<FlatNode, Set<NTuple<Descriptor>>>();
166 this.mapFlatNodeToBoundMustWriteSet = new Hashtable<FlatNode, Set<NTuple<Descriptor>>>();
167 this.mapFlatNodeToBoundMayWriteSet = new Hashtable<FlatNode, Set<NTuple<Descriptor>>>();
168 this.mapFlatNodeToSharedLocMapping = new Hashtable<FlatNode, SharedLocMap>();
169 this.mapFlatMethodToDeleteSet = new Hashtable<FlatMethod, SharedLocMap>();
170 this.calleeUnionBoundDeleteSet = new SharedLocMap();
171 this.calleeIntersectBoundSharedSet = new SharedLocMap();
172 this.mapFlatMethodToSharedLocMap = new Hashtable<FlatMethod, SharedLocMap>();
173 this.mapMethodToSharedLocCoverSet =
174 new Hashtable<MethodDescriptor, MultiSourceMap<NTuple<Location>, NTuple<Descriptor>>>();
175 this.mapFlatNodeToDeleteSet = new Hashtable<FlatNode, SharedLocMap>();
176 this.liveness = new Liveness();
177 this.liveInTempSetToEventLoop = new HashSet<TempDescriptor>();
180 public void definitelyWrittenCheck() {
181 if (!ssjava.getAnnotationRequireSet().isEmpty()) {
184 methodReadWriteSetAnalysis();
185 computeSharedCoverSet();
194 private void sharedLocAnalysis() {
196 // perform method READ/OVERWRITE analysis
197 LinkedList<MethodDescriptor> descriptorListToAnalyze =
198 (LinkedList<MethodDescriptor>) sortedDescriptors.clone();
200 // current descriptors to visit in fixed-point interprocedural analysis,
202 // dependency in the call graph
203 methodDescriptorsToVisitStack.clear();
205 descriptorListToAnalyze.removeFirst();
207 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
208 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
210 while (!descriptorListToAnalyze.isEmpty()) {
211 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
212 methodDescriptorsToVisitStack.add(md);
215 // analyze scheduled methods until there are no more to visit
216 while (!methodDescriptorsToVisitStack.isEmpty()) {
217 // start to analyze leaf node
218 MethodDescriptor md = methodDescriptorsToVisitStack.pop();
219 FlatMethod fm = state.getMethodFlat(md);
221 SharedLocMap sharedLocMap = new SharedLocMap();
222 SharedLocMap deleteSet = new SharedLocMap();
224 sharedLoc_analyzeMethod(fm, sharedLocMap, deleteSet);
225 SharedLocMap prevSharedLocMap = mapFlatMethodToSharedLocMap.get(fm);
226 SharedLocMap prevDeleteSet = mapFlatMethodToDeleteSet.get(fm);
228 if (!(deleteSet.equals(prevDeleteSet) && sharedLocMap.equals(prevSharedLocMap))) {
229 mapFlatMethodToSharedLocMap.put(fm, sharedLocMap);
230 mapFlatMethodToDeleteSet.put(fm, deleteSet);
232 // results for callee changed, so enqueue dependents caller for
235 Iterator<MethodDescriptor> depsItr = getDependents(md).iterator();
236 while (depsItr.hasNext()) {
237 MethodDescriptor methodNext = depsItr.next();
238 if (!methodDescriptorsToVisitStack.contains(methodNext)
239 && methodDescriptorToVistSet.contains(methodNext)) {
240 methodDescriptorsToVisitStack.add(methodNext);
249 sharedLoc_analyzeEventLoop();
253 private void sharedLoc_analyzeEventLoop() {
254 if (state.SSJAVADEBUG) {
255 System.out.println("SSJAVA: Definite clearance for shared locations Analyzing: eventloop");
257 SharedLocMap sharedLocMap = new SharedLocMap();
258 SharedLocMap deleteSet = new SharedLocMap();
259 sharedLoc_analyzeBody(state.getMethodFlat(methodContainingSSJavaLoop),
260 ssjava.getSSJavaLoopEntrance(), sharedLocMap, deleteSet, true);
264 private void sharedLoc_analyzeMethod(FlatMethod fm, SharedLocMap sharedLocMap,
265 SharedLocMap deleteSet) {
266 if (state.SSJAVADEBUG) {
267 System.out.println("SSJAVA: Definite clearance for shared locations Analyzing: " + fm);
270 sharedLoc_analyzeBody(fm, fm, sharedLocMap, deleteSet, false);
274 private void sharedLoc_analyzeBody(FlatMethod fm, FlatNode startNode, SharedLocMap sharedLocMap,
275 SharedLocMap deleteSet, boolean isEventLoopBody) {
277 // intraprocedural analysis
278 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
279 flatNodesToVisit.add(startNode);
281 while (!flatNodesToVisit.isEmpty()) {
282 FlatNode fn = flatNodesToVisit.iterator().next();
283 flatNodesToVisit.remove(fn);
285 SharedLocMap currSharedSet = new SharedLocMap();
286 SharedLocMap currDeleteSet = new SharedLocMap();
288 for (int i = 0; i < fn.numPrev(); i++) {
289 FlatNode prevFn = fn.getPrev(i);
290 SharedLocMap inSharedLoc = mapFlatNodeToSharedLocMapping.get(prevFn);
291 if (inSharedLoc != null) {
292 mergeSharedLocMap(currSharedSet, inSharedLoc);
295 SharedLocMap inDeleteLoc = mapFlatNodeToDeleteSet.get(prevFn);
296 if (inDeleteLoc != null) {
297 mergeDeleteSet(currDeleteSet, inDeleteLoc);
301 sharedLoc_nodeActions(fm, fn, currSharedSet, currDeleteSet, sharedLocMap, deleteSet,
304 SharedLocMap prevSharedSet = mapFlatNodeToSharedLocMapping.get(fn);
305 SharedLocMap prevDeleteSet = mapFlatNodeToDeleteSet.get(fn);
307 if (!(currSharedSet.equals(prevSharedSet) && currDeleteSet.equals(prevDeleteSet))) {
308 mapFlatNodeToSharedLocMapping.put(fn, currSharedSet);
309 mapFlatNodeToDeleteSet.put(fn, currDeleteSet);
310 for (int i = 0; i < fn.numNext(); i++) {
311 FlatNode nn = fn.getNext(i);
312 if ((!isEventLoopBody) || loopIncElements.contains(nn)) {
313 flatNodesToVisit.add(nn);
323 private void sharedLoc_nodeActions(FlatMethod fm, FlatNode fn, SharedLocMap curr,
324 SharedLocMap currDeleteSet, SharedLocMap sharedLocMap, SharedLocMap deleteSet,
325 boolean isEventLoopBody) {
327 MethodDescriptor md = fm.getMethod();
329 SharedLocMap killSet = new SharedLocMap();
330 SharedLocMap genSet = new SharedLocMap();
338 case FKind.FlatOpNode: {
340 if (isEventLoopBody) {
341 FlatOpNode fon = (FlatOpNode) fn;
343 if (fon.getOp().getOp() == Operation.ASSIGN) {
347 if (!lhs.getSymbol().startsWith("neverused") && !lhs.getSymbol().startsWith("leftop")
348 && !lhs.getSymbol().startsWith("rightop") && rhs.getType().isImmutable()) {
350 if (mapHeapPath.containsKey(rhs)) {
351 Location dstLoc = getLocation(lhs);
352 if (dstLoc != null && ssjava.isSharedLocation(dstLoc)) {
353 NTuple<Descriptor> lhsHeapPath = computePath(lhs);
354 NTuple<Location> lhsLocTuple = mapDescriptorToLocationPath.get(lhs);
356 Location srcLoc = getLocation(lhs);
358 // computing gen/kill set
359 computeKILLSetForWrite(curr, killSet, lhsLocTuple, lhsHeapPath);
361 if (!ssjava.isSameHeightWrite(fn)) {
362 computeGENSetForHigherWrite(curr, killSet, lhsLocTuple, lhsHeapPath);
363 updateDeleteSetForHigherWrite(currDeleteSet, lhsLocTuple, lhsHeapPath);
365 computeGENSetForSameHeightWrite(curr, killSet, lhsLocTuple, lhsHeapPath);
366 updateDeleteSetForSameHeightWrite(currDeleteSet, lhsLocTuple, lhsHeapPath);
369 // System.out.println("VAR WRITE:" + fn);
370 // System.out.println("lhsLocTuple=" + lhsLocTuple +
371 // " lhsHeapPath=" + lhsHeapPath);
372 // System.out.println("dstLoc=" + dstLoc + " srcLoc=" + srcLoc);
373 // System.out.println("KILLSET=" + killSet);
374 // System.out.println("GENSet=" + genSet);
375 // System.out.println("DELETESET=" + currDeleteSet);
391 case FKind.FlatSetFieldNode:
392 case FKind.FlatSetElementNode: {
395 if (fn.kind() == FKind.FlatSetFieldNode) {
396 FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
398 fld = fsfn.getField();
400 fieldLoc = (Location) fld.getType().getExtension();
405 if (!isEventLoopBody && fieldLoc.getDescriptor().equals(md)) {
406 // if the field belongs to the local lattice, no reason to calculate
411 NTuple<Location> fieldLocTuple = new NTuple<Location>();
412 if (fld.isStatic()) {
414 // in this case, fld has TOP location
415 Location topLocation = Location.createTopLocation(md);
416 fieldLocTuple.add(topLocation);
418 fieldLocTuple.addAll(deriveGlobalLocationTuple(md));
419 if (fn.kind() == FKind.FlatSetFieldNode) {
420 fieldLocTuple.add((Location) fld.getType().getExtension());
425 fieldLocTuple.addAll(deriveLocationTuple(md, lhs));
426 if (fn.kind() == FKind.FlatSetFieldNode) {
427 fieldLocTuple.add((Location) fld.getType().getExtension());
431 // shared loc extension
432 Location srcLoc = getLocation(rhs);
433 if (ssjava.isSharedLocation(fieldLoc)) {
434 // only care the case that loc(f) is shared location
437 // NTuple<Location> fieldLocTuple = new NTuple<Location>();
438 // fieldLocTuple.addAll(mapDescriptorToLocationPath.get(lhs));
439 // fieldLocTuple.add(fieldLoc);
441 NTuple<Descriptor> fldHeapPath = new NTuple<Descriptor>();
442 fldHeapPath.addAll(computePath(lhs));
443 if (fn.kind() == FKind.FlatSetFieldNode) {
444 fldHeapPath.add(fld);
447 // computing gen/kill set
448 computeKILLSetForWrite(curr, killSet, fieldLocTuple, fldHeapPath);
450 if (!ssjava.isSameHeightWrite(fn)) {
451 computeGENSetForHigherWrite(curr, genSet, fieldLocTuple, fldHeapPath);
452 updateDeleteSetForHigherWrite(currDeleteSet, fieldLocTuple, fldHeapPath);
454 computeGENSetForSameHeightWrite(curr, genSet, fieldLocTuple, fldHeapPath);
455 updateDeleteSetForSameHeightWrite(currDeleteSet, fieldLocTuple, fldHeapPath);
458 // System.out.println("################");
459 // System.out.println("FIELD WRITE:" + fn);
460 // System.out.println("FldHeapPath=" + fldHeapPath);
461 // System.out.println("fieldLocTuple=" + fieldLocTuple + " srcLoc=" +
463 // System.out.println("KILLSET=" + killSet);
464 // System.out.println("GENSet=" + genSet);
465 // System.out.println("DELETESET=" + currDeleteSet);
471 case FKind.FlatCall: {
472 FlatCall fc = (FlatCall) fn;
474 bindHeapPathCallerArgWithCaleeParamForSharedLoc(fm.getMethod(), fc);
476 // computing gen/kill set
477 generateKILLSetForFlatCall(curr, killSet);
478 generateGENSetForFlatCall(curr, genSet);
480 // System.out.println("#FLATCALL=" + fc);
481 // System.out.println("KILLSET=" + killSet);
482 // System.out.println("GENSet=" + genSet);
483 // System.out.println("bound DELETE Set=" + calleeUnionBoundDeleteSet);
488 case FKind.FlatExit: {
489 // merge the current delete/shared loc mapping
490 mergeSharedLocMap(sharedLocMap, curr);
491 mergeDeleteSet(deleteSet, currDeleteSet);
493 // System.out.println("#FLATEXIT sharedLocMap=" + sharedLocMap);
499 computeNewMapping(curr, killSet, genSet);
500 if (!curr.map.isEmpty()) {
501 // System.out.println(fn + "#######" + curr);
506 private void generateGENSetForFlatCall(SharedLocMap curr, SharedLocMap genSet) {
508 Set<NTuple<Location>> locTupleSet = calleeIntersectBoundSharedSet.keySet();
509 for (Iterator iterator = locTupleSet.iterator(); iterator.hasNext();) {
510 NTuple<Location> locTupleKey = (NTuple<Location>) iterator.next();
511 genSet.addWrite(locTupleKey, curr.get(locTupleKey));
512 genSet.addWrite(locTupleKey, calleeIntersectBoundSharedSet.get(locTupleKey));
514 genSet.removeWriteAll(locTupleKey, calleeUnionBoundDeleteSet.get(locTupleKey));
519 private void generateKILLSetForFlatCall(SharedLocMap curr, SharedLocMap killSet) {
521 Set<NTuple<Location>> locTupleSet = calleeIntersectBoundSharedSet.keySet();
522 for (Iterator iterator = locTupleSet.iterator(); iterator.hasNext();) {
523 NTuple<Location> locTupleKey = (NTuple<Location>) iterator.next();
524 killSet.addWrite(locTupleKey, curr.get(locTupleKey));
529 private void mergeDeleteSet(SharedLocMap currDeleteSet, SharedLocMap inDeleteLoc) {
531 Set<NTuple<Location>> locTupleKeySet = inDeleteLoc.keySet();
533 for (Iterator iterator = locTupleKeySet.iterator(); iterator.hasNext();) {
534 NTuple<Location> locTupleKey = (NTuple<Location>) iterator.next();
536 Set<NTuple<Descriptor>> inSet = inDeleteLoc.get(locTupleKey);
537 currDeleteSet.addWrite(locTupleKey, inSet);
542 private void computeNewMapping(SharedLocMap curr, SharedLocMap killSet, SharedLocMap genSet) {
547 private void updateDeleteSetForHigherWrite(SharedLocMap currDeleteSet, NTuple<Location> locTuple,
548 NTuple<Descriptor> hp) {
549 currDeleteSet.removeWrite(locTuple, hp);
552 private void updateDeleteSetForSameHeightWrite(SharedLocMap currDeleteSet,
553 NTuple<Location> locTuple, NTuple<Descriptor> hp) {
554 currDeleteSet.addWrite(locTuple, hp);
557 private void computeGENSetForHigherWrite(SharedLocMap curr, SharedLocMap genSet,
558 NTuple<Location> locTuple, NTuple<Descriptor> hp) {
559 Set<NTuple<Descriptor>> currWriteSet = curr.get(locTuple);
561 if (currWriteSet != null) {
562 genSet.addWrite(locTuple, currWriteSet);
565 genSet.addWrite(locTuple, hp);
568 private void computeGENSetForSameHeightWrite(SharedLocMap curr, SharedLocMap genSet,
569 NTuple<Location> locTuple, NTuple<Descriptor> hp) {
570 Set<NTuple<Descriptor>> currWriteSet = curr.get(locTuple);
572 if (currWriteSet != null) {
573 genSet.addWrite(locTuple, currWriteSet);
575 genSet.removeWrite(locTuple, hp);
578 private void computeKILLSetForWrite(SharedLocMap curr, SharedLocMap killSet,
579 NTuple<Location> locTuple, NTuple<Descriptor> hp) {
581 Set<NTuple<Descriptor>> writeSet = curr.get(locTuple);
582 if (writeSet != null) {
583 killSet.addWrite(locTuple, writeSet);
588 private void mergeSharedLocMap(SharedLocMap currSharedSet, SharedLocMap in) {
590 Set<NTuple<Location>> locTupleKeySet = in.keySet();
591 for (Iterator iterator = locTupleKeySet.iterator(); iterator.hasNext();) {
592 NTuple<Location> locTupleKey = (NTuple<Location>) iterator.next();
594 Set<NTuple<Descriptor>> inSet = in.get(locTupleKey);
595 Set<NTuple<Descriptor>> currSet = currSharedSet.get(locTupleKey);
596 if (currSet == null) {
597 currSet = new HashSet<NTuple<Descriptor>>();
598 currSet.addAll(inSet);
599 currSharedSet.addWrite(locTupleKey, currSet);
601 currSet.retainAll(inSet);
606 private void computeSharedCoverSet() {
607 LinkedList<MethodDescriptor> descriptorListToAnalyze =
608 (LinkedList<MethodDescriptor>) sortedDescriptors.clone();
610 // current descriptors to visit in fixed-point interprocedural analysis,
612 // dependency in the call graph
613 methodDescriptorsToVisitStack.clear();
615 descriptorListToAnalyze.removeFirst();
617 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
618 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
620 while (!descriptorListToAnalyze.isEmpty()) {
621 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
622 methodDescriptorsToVisitStack.add(md);
625 // analyze scheduled methods until there are no more to visit
626 while (!methodDescriptorsToVisitStack.isEmpty()) {
627 MethodDescriptor md = methodDescriptorsToVisitStack.pop();
628 FlatMethod fm = state.getMethodFlat(md);
629 computeSharedCoverSet_analyzeMethod(fm, md.equals(methodContainingSSJavaLoop));
632 computeSharedCoverSetForEventLoop();
636 private void computeSharedCoverSetForEventLoop() {
637 computeSharedCoverSet_analyzeMethod(state.getMethodFlat(methodContainingSSJavaLoop), true);
640 private void computeSharedCoverSet_analyzeMethod(FlatMethod fm, boolean onlyVisitSSJavaLoop) {
642 System.out.println("\n###");
643 System.out.println("computeSharedCoverSet_analyzeMethod=" + fm);
644 MethodDescriptor md = fm.getMethod();
646 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
648 Set<FlatNode> visited = new HashSet<FlatNode>();
650 if (onlyVisitSSJavaLoop) {
651 flatNodesToVisit.add(ssjava.getSSJavaLoopEntrance());
653 flatNodesToVisit.add(fm);
656 while (!flatNodesToVisit.isEmpty()) {
657 FlatNode fn = flatNodesToVisit.iterator().next();
658 flatNodesToVisit.remove(fn);
661 computeSharedCoverSet_nodeActions(md, fn, onlyVisitSSJavaLoop);
663 for (int i = 0; i < fn.numNext(); i++) {
664 FlatNode nn = fn.getNext(i);
666 if (!visited.contains(nn)) {
667 if (!onlyVisitSSJavaLoop || (onlyVisitSSJavaLoop && loopIncElements.contains(nn))) {
668 flatNodesToVisit.add(nn);
678 private void computeSharedCoverSet_nodeActions(MethodDescriptor md, FlatNode fn,
679 boolean isEventLoopBody) {
686 case FKind.FlatLiteralNode: {
687 FlatLiteralNode fln = (FlatLiteralNode) fn;
690 NTuple<Location> lhsLocTuple = new NTuple<Location>();
691 lhsLocTuple.add(Location.createTopLocation(md));
692 mapDescriptorToLocationPath.put(lhs, lhsLocTuple);
694 if (lhs.getType().isPrimitive() && !lhs.getSymbol().startsWith("neverused")
695 && !lhs.getSymbol().startsWith("srctmp")) {
696 // only need to care about composite location case here
697 if (lhs.getType().getExtension() instanceof SSJavaType) {
698 CompositeLocation compLoc = ((SSJavaType) lhs.getType().getExtension()).getCompLoc();
699 Location lastLocElement = compLoc.get(compLoc.getSize() - 1);
706 case FKind.FlatOpNode: {
707 FlatOpNode fon = (FlatOpNode) fn;
708 // for a normal assign node, need to propagate lhs's location path to
710 if (fon.getOp().getOp() == Operation.ASSIGN) {
714 if (!lhs.getSymbol().startsWith("neverused") && !lhs.getSymbol().startsWith("leftop")
715 && !lhs.getSymbol().startsWith("rightop")) {
717 if (mapHeapPath.containsKey(rhs)) {
718 NTuple<Location> rhsLocTuple = new NTuple<Location>();
719 NTuple<Location> lhsLocTuple = new NTuple<Location>();
720 if (mapDescriptorToLocationPath.containsKey(rhs)) {
721 mapDescriptorToLocationPath.put(lhs, deriveLocationTuple(md, rhs));
722 lhsLocTuple = mapDescriptorToLocationPath.get(lhs);
725 if (rhs.getType().getExtension() != null
726 && rhs.getType().getExtension() instanceof SSJavaType) {
728 if (((SSJavaType) rhs.getType().getExtension()).getCompLoc() != null) {
729 rhsLocTuple.addAll(((SSJavaType) rhs.getType().getExtension()).getCompLoc()
734 NTuple<Location> locTuple = deriveLocationTuple(md, rhs);
735 if (locTuple != null) {
736 rhsLocTuple.addAll(locTuple);
739 if (rhsLocTuple.size() > 0) {
740 mapDescriptorToLocationPath.put(rhs, rhsLocTuple);
744 if (lhs.getType().getExtension() != null
745 && lhs.getType().getExtension() instanceof SSJavaType) {
746 lhsLocTuple.addAll(((SSJavaType) lhs.getType().getExtension()).getCompLoc()
748 mapDescriptorToLocationPath.put(lhs, lhsLocTuple);
749 } else if (mapDescriptorToLocationPath.get(rhs) != null) {
750 // propagate rhs's location to lhs
751 lhsLocTuple.addAll(mapDescriptorToLocationPath.get(rhs));
752 mapDescriptorToLocationPath.put(lhs, lhsLocTuple);
756 if (isEventLoopBody && lhs.getType().isPrimitive()
757 && !lhs.getSymbol().startsWith("srctmp")) {
759 NTuple<Descriptor> lhsHeapPath = computePath(lhs);
761 if (lhsLocTuple != null) {
762 addMayWrittenSet(md, lhsLocTuple, lhsHeapPath);
776 case FKind.FlatSetFieldNode:
777 case FKind.FlatSetElementNode: {
781 if (fn.kind() == FKind.FlatSetFieldNode) {
782 FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
784 fld = fsfn.getField();
787 FlatSetElementNode fsen = (FlatSetElementNode) fn;
790 TypeDescriptor td = lhs.getType().dereference();
791 fld = getArrayField(td);
794 NTuple<Location> lhsLocTuple = new NTuple<Location>();
795 lhsLocTuple.addAll(deriveLocationTuple(md, lhs));
796 mapDescriptorToLocationPath.put(lhs, lhsLocTuple);
798 NTuple<Location> fieldLocTuple = new NTuple<Location>();
799 fieldLocTuple.addAll(lhsLocTuple);
801 if (fn.kind() == FKind.FlatSetFieldNode) {
802 fieldLocTuple.add((Location) fld.getType().getExtension());
805 if (mapHeapPath.containsKey(lhs)) {
806 // fields reachable from the param can have heap path entry.
807 NTuple<Descriptor> lhsHeapPath = new NTuple<Descriptor>();
808 lhsHeapPath.addAll(mapHeapPath.get(lhs));
810 Location fieldLocation;
811 if (fn.kind() == FKind.FlatSetFieldNode) {
812 fieldLocation = getLocation(fld);
814 fieldLocation = getLocation(lhsHeapPath.get(getArrayBaseDescriptorIdx(lhsHeapPath)));
817 // Location fieldLocation = getLocation(lhs);
818 if (!isEventLoopBody && fieldLocation.getDescriptor().equals(md)) {
819 // if the field belongs to the local lattice, no reason to calculate
824 if (ssjava.isSharedLocation(fieldLocation)) {
826 NTuple<Descriptor> fieldHeapPath = new NTuple<Descriptor>();
827 fieldHeapPath.addAll(computePath(lhs));
828 if (fn.kind() == FKind.FlatSetFieldNode) {
829 fieldHeapPath.add(fld);
832 addMayWrittenSet(md, fieldLocTuple, fieldHeapPath);
840 case FKind.FlatElementNode:
841 case FKind.FlatFieldNode: {
845 if (fn.kind() == FKind.FlatFieldNode) {
846 FlatFieldNode ffn = (FlatFieldNode) fn;
849 fld = ffn.getField();
851 FlatElementNode fen = (FlatElementNode) fn;
854 TypeDescriptor td = rhs.getType().dereference();
855 fld = getArrayField(td);
858 NTuple<Location> locTuple = new NTuple<Location>();
860 if (fld.isStatic()) {
863 // in this case, fld has TOP location
864 Location topLocation = Location.createTopLocation(md);
865 locTuple.add(topLocation);
867 locTuple.addAll(deriveGlobalLocationTuple(md));
868 if (fn.kind() == FKind.FlatFieldNode) {
869 locTuple.add((Location) fld.getType().getExtension());
874 locTuple.addAll(deriveLocationTuple(md, rhs));
875 if (fn.kind() == FKind.FlatFieldNode) {
876 locTuple.add((Location) fld.getType().getExtension());
880 mapDescriptorToLocationPath.put(lhs, locTuple);
885 case FKind.FlatCall: {
887 FlatCall fc = (FlatCall) fn;
889 bindLocationPathCallerArgWithCalleeParam(md, fc);
894 case FKind.FlatNew: {
896 FlatNew fnew = (FlatNew) fn;
897 TempDescriptor dst = fnew.getDst();
898 NTuple<Location> locTuple = deriveLocationTuple(md, dst);
900 if (locTuple != null) {
901 NTuple<Location> dstLocTuple = new NTuple<Location>();
902 dstLocTuple.addAll(locTuple);
903 mapDescriptorToLocationPath.put(dst, dstLocTuple);
911 private void addMayWrittenSet(MethodDescriptor md, NTuple<Location> locTuple,
912 NTuple<Descriptor> heapPath) {
914 MultiSourceMap<NTuple<Location>, NTuple<Descriptor>> map = mapMethodToSharedLocCoverSet.get(md);
916 map = new MultiSourceMap<NTuple<Location>, NTuple<Descriptor>>();
917 mapMethodToSharedLocCoverSet.put(md, map);
920 Set<NTuple<Descriptor>> writeSet = map.get(locTuple);
921 if (writeSet == null) {
922 writeSet = new HashSet<NTuple<Descriptor>>();
923 map.put(locTuple, writeSet);
925 writeSet.add(heapPath);
929 private void bindLocationPathCallerArgWithCalleeParam(MethodDescriptor mdCaller, FlatCall fc) {
931 if (ssjava.isSSJavaUtil(fc.getMethod().getClassDesc())) {
933 // have write effects on the first argument
934 TempDescriptor arg = fc.getArg(0);
935 NTuple<Location> argLocationPath = deriveLocationTuple(mdCaller, arg);
936 NTuple<Descriptor> argHeapPath = computePath(arg);
937 addMayWrittenSet(mdCaller, argLocationPath, argHeapPath);
938 } else if (ssjava.needTobeAnnotated(fc.getMethod())) {
940 // if arg is not primitive type, we need to propagate maywritten set to
941 // the caller's location path
943 MethodDescriptor mdCallee = fc.getMethod();
944 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
945 setPossibleCallees.addAll(callGraph.getMethods(mdCallee));
947 // create mapping from arg idx to its heap paths
948 Hashtable<Integer, NTuple<Descriptor>> mapArgIdx2CallerArgHeapPath =
949 new Hashtable<Integer, NTuple<Descriptor>>();
951 // create mapping from arg idx to its location paths
952 Hashtable<Integer, NTuple<Location>> mapArgIdx2CallerArgLocationPath =
953 new Hashtable<Integer, NTuple<Location>>();
955 if (fc.getThis() != null) {
957 if (mapHeapPath.containsKey(fc.getThis())) {
959 // setup heap path for 'this'
960 NTuple<Descriptor> thisHeapPath = new NTuple<Descriptor>();
961 thisHeapPath.addAll(mapHeapPath.get(fc.getThis()));
962 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(0), thisHeapPath);
964 // setup location path for 'this'
965 NTuple<Location> thisLocationPath = deriveLocationTuple(mdCaller, fc.getThis());
966 mapArgIdx2CallerArgLocationPath.put(Integer.valueOf(0), thisLocationPath);
971 for (int i = 0; i < fc.numArgs(); i++) {
972 TempDescriptor arg = fc.getArg(i);
973 // create mapping arg to loc path
975 if (mapHeapPath.containsKey(arg)) {
977 NTuple<Descriptor> argHeapPath = mapHeapPath.get(arg);
978 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(i + 1), argHeapPath);
980 NTuple<Location> argLocationPath = deriveLocationTuple(mdCaller, arg);
981 mapArgIdx2CallerArgLocationPath.put(Integer.valueOf(i + 1), argLocationPath);
986 for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
987 MethodDescriptor callee = (MethodDescriptor) iterator.next();
988 FlatMethod calleeFlatMethod = state.getMethodFlat(callee);
990 // binding caller's args and callee's params
992 Hashtable<NTuple<Descriptor>, NTuple<Descriptor>> mapParamHeapPathToCallerArgHeapPath =
993 new Hashtable<NTuple<Descriptor>, NTuple<Descriptor>>();
995 Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc =
996 new Hashtable<Integer, TempDescriptor>();
998 if (calleeFlatMethod.getMethod().isStatic()) {
999 // static method does not have implicit 'this' arg
1003 for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
1004 TempDescriptor param = calleeFlatMethod.getParameter(i);
1005 mapParamIdx2ParamTempDesc.put(Integer.valueOf(i + offset), param);
1007 NTuple<Descriptor> calleeHeapPath = computePath(param);
1009 NTuple<Descriptor> argHeapPath =
1010 mapArgIdx2CallerArgHeapPath.get(Integer.valueOf(i + offset));
1012 if (argHeapPath != null) {
1013 mapParamHeapPathToCallerArgHeapPath.put(calleeHeapPath, argHeapPath);
1019 Set<Integer> keySet = mapArgIdx2CallerArgLocationPath.keySet();
1020 for (Iterator iterator2 = keySet.iterator(); iterator2.hasNext();) {
1021 Integer idx = (Integer) iterator2.next();
1023 NTuple<Location> callerArgLocationPath = mapArgIdx2CallerArgLocationPath.get(idx);
1025 TempDescriptor calleeParam = mapParamIdx2ParamTempDesc.get(idx);
1026 NTuple<Location> calleeLocationPath = deriveLocationTuple(mdCallee, calleeParam);
1028 NTuple<Descriptor> callerArgHeapPath = mapArgIdx2CallerArgHeapPath.get(idx);
1029 NTuple<Descriptor> calleeHeapPath = computePath(calleeParam);
1031 if (!calleeParam.getType().isPrimitive()) {
1032 createNewMappingOfMayWrittenSet(mdCaller, callee, callerArgHeapPath,
1033 callerArgLocationPath, calleeHeapPath, calleeLocationPath,
1034 mapParamHeapPathToCallerArgHeapPath);
1044 private Hashtable<NTuple<Location>, Set<NTuple<Descriptor>>> getMappingByStartedWith(
1045 MultiSourceMap<NTuple<Location>, NTuple<Descriptor>> map, NTuple<Location> in) {
1047 Hashtable<NTuple<Location>, Set<NTuple<Descriptor>>> matchedMapping =
1048 new Hashtable<NTuple<Location>, Set<NTuple<Descriptor>>>();
1050 Set<NTuple<Location>> keySet = map.keySet();
1052 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1053 NTuple<Location> key = (NTuple<Location>) iterator.next();
1054 if (key.startsWith(in)) {
1055 matchedMapping.put(key, map.get(key));
1059 return matchedMapping;
1063 private void createNewMappingOfMayWrittenSet(MethodDescriptor caller, MethodDescriptor callee,
1064 NTuple<Descriptor> callerArgHeapPath, NTuple<Location> callerArgLocPath,
1065 NTuple<Descriptor> calleeParamHeapPath, NTuple<Location> calleeParamLocPath,
1066 Hashtable<NTuple<Descriptor>, NTuple<Descriptor>> mapParamHeapPathToCallerArgHeapPath) {
1068 // propagate may-written-set associated with the key that is started with
1069 // calleepath to the caller
1070 // 1) makes a new key by combining caller path and callee path(except local
1071 // loc element of param)
1072 // 2) create new mapping of may-written-set of callee path to caller path
1074 // extract all may written effect accessed through callee param path
1075 MultiSourceMap<NTuple<Location>, NTuple<Descriptor>> calleeMapping =
1076 mapMethodToSharedLocCoverSet.get(callee);
1078 if (calleeMapping == null) {
1082 MultiSourceMap<NTuple<Location>, NTuple<Descriptor>> callerMapping =
1083 mapMethodToSharedLocCoverSet.get(caller);
1085 if (callerMapping == null) {
1086 callerMapping = new MultiSourceMap<NTuple<Location>, NTuple<Descriptor>>();
1087 mapMethodToSharedLocCoverSet.put(caller, callerMapping);
1090 Hashtable<NTuple<Location>, Set<NTuple<Descriptor>>> paramMapping =
1091 getMappingByStartedWith(calleeMapping, calleeParamLocPath);
1093 Set<NTuple<Location>> calleeKeySet = paramMapping.keySet();
1095 for (Iterator iterator = calleeKeySet.iterator(); iterator.hasNext();) {
1096 NTuple<Location> calleeKey = (NTuple<Location>) iterator.next();
1098 Set<NTuple<Descriptor>> calleeMayWriteSet = paramMapping.get(calleeKey);
1100 if (calleeMayWriteSet != null) {
1102 Set<NTuple<Descriptor>> boundMayWriteSet = new HashSet<NTuple<Descriptor>>();
1104 Set<NTuple<Descriptor>> boundSet =
1105 convertToCallerMayWriteSet(calleeParamHeapPath, calleeMayWriteSet, callerMapping,
1106 mapParamHeapPathToCallerArgHeapPath);
1108 boundMayWriteSet.addAll(boundSet);
1110 NTuple<Location> newKey = new NTuple<Location>();
1111 newKey.addAll(callerArgLocPath);
1112 // need to replace the local location with the caller's path so skip the
1113 // local location of the parameter
1114 for (int i = 1; i < calleeKey.size(); i++) {
1115 newKey.add(calleeKey.get(i));
1118 callerMapping.union(newKey, boundMayWriteSet);
1125 private Set<NTuple<Descriptor>> convertToCallerMayWriteSet(
1126 NTuple<Descriptor> calleeParamHeapPath, Set<NTuple<Descriptor>> calleeMayWriteSet,
1127 MultiSourceMap<NTuple<Location>, NTuple<Descriptor>> callerMapping,
1128 Hashtable<NTuple<Descriptor>, NTuple<Descriptor>> mapParamHeapPathToCallerArgHeapPath) {
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> writeHeapPathParamHeapPath = calleeWriteHeapPath.subList(0, 1);
1138 NTuple<Descriptor> callerArgHeapPath =
1139 mapParamHeapPathToCallerArgHeapPath.get(writeHeapPathParamHeapPath);
1141 NTuple<Descriptor> boundHeapPath = new NTuple<Descriptor>();
1142 boundHeapPath.addAll(callerArgHeapPath);
1144 for (int i = 1; i < calleeWriteHeapPath.size(); i++) {
1145 boundHeapPath.add(calleeWriteHeapPath.get(i));
1148 boundSet.add(boundHeapPath);
1155 private Location getLocation(Descriptor d) {
1157 if (d instanceof FieldDescriptor) {
1158 TypeExtension te = ((FieldDescriptor) d).getType().getExtension();
1160 return (Location) te;
1163 assert d instanceof TempDescriptor;
1164 TempDescriptor td = (TempDescriptor) d;
1166 TypeExtension te = td.getType().getExtension();
1168 if (te instanceof SSJavaType) {
1169 SSJavaType ssType = (SSJavaType) te;
1170 if (ssType.getCompLoc() != null) {
1171 CompositeLocation comp = ssType.getCompLoc();
1172 return comp.get(comp.getSize() - 1);
1177 return (Location) te;
1182 return mapDescToLocation.get(d);
1185 private void eventLoopAnalysis() {
1186 // perform second stage analysis: intraprocedural analysis ensure that
1188 // variables are definitely written in-between the same read
1190 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
1191 flatNodesToVisit.add(ssjava.getSSJavaLoopEntrance());
1193 while (!flatNodesToVisit.isEmpty()) {
1194 FlatNode fn = (FlatNode) flatNodesToVisit.iterator().next();
1195 flatNodesToVisit.remove(fn);
1197 Hashtable<NTuple<Descriptor>, Set<WriteAge>> prev = mapFlatNodetoEventLoopMap.get(fn);
1199 Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr =
1200 new Hashtable<NTuple<Descriptor>, Set<WriteAge>>();
1201 for (int i = 0; i < fn.numPrev(); i++) {
1202 FlatNode nn = fn.getPrev(i);
1203 Hashtable<NTuple<Descriptor>, Set<WriteAge>> in = mapFlatNodetoEventLoopMap.get(nn);
1209 eventLoopAnalysis_nodeAction(fn, curr, ssjava.getSSJavaLoopEntrance());
1211 // if a new result, schedule forward nodes for analysis
1212 if (!curr.equals(prev)) {
1213 mapFlatNodetoEventLoopMap.put(fn, curr);
1215 for (int i = 0; i < fn.numNext(); i++) {
1216 FlatNode nn = fn.getNext(i);
1217 if (loopIncElements.contains(nn)) {
1218 flatNodesToVisit.add(nn);
1226 private void union(Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr,
1227 Hashtable<NTuple<Descriptor>, Set<WriteAge>> in) {
1229 Set<NTuple<Descriptor>> inKeySet = in.keySet();
1230 for (Iterator iterator = inKeySet.iterator(); iterator.hasNext();) {
1231 NTuple<Descriptor> inKey = (NTuple<Descriptor>) iterator.next();
1232 Set<WriteAge> inSet = in.get(inKey);
1234 Set<WriteAge> currSet = curr.get(inKey);
1236 if (currSet == null) {
1237 currSet = new HashSet<WriteAge>();
1238 curr.put(inKey, currSet);
1240 currSet.addAll(inSet);
1245 private void eventLoopAnalysis_nodeAction(FlatNode fn,
1246 Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr, FlatNode loopEntrance) {
1248 Hashtable<NTuple<Descriptor>, Set<WriteAge>> readWriteKillSet =
1249 new Hashtable<NTuple<Descriptor>, Set<WriteAge>>();
1250 Hashtable<NTuple<Descriptor>, Set<WriteAge>> readWriteGenSet =
1251 new Hashtable<NTuple<Descriptor>, Set<WriteAge>>();
1253 if (fn.equals(loopEntrance)) {
1254 // it reaches loop entrance: changes all flag to true
1255 Set<NTuple<Descriptor>> keySet = curr.keySet();
1256 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1257 NTuple<Descriptor> key = (NTuple<Descriptor>) iterator.next();
1258 Set<WriteAge> writeAgeSet = curr.get(key);
1260 Set<WriteAge> incSet = new HashSet<WriteAge>();
1261 incSet.addAll(writeAgeSet);
1262 writeAgeSet.clear();
1264 for (Iterator iterator2 = incSet.iterator(); iterator2.hasNext();) {
1265 WriteAge writeAge = (WriteAge) iterator2.next();
1266 WriteAge newWriteAge = writeAge.copy();
1268 writeAgeSet.add(newWriteAge);
1276 FieldDescriptor fld;
1278 switch (fn.kind()) {
1280 case FKind.FlatOpNode: {
1281 FlatOpNode fon = (FlatOpNode) fn;
1282 lhs = fon.getDest();
1283 rhs = fon.getLeft();
1285 if (fon.getOp().getOp() == Operation.ASSIGN) {
1287 if (!lhs.getSymbol().startsWith("neverused") && !lhs.getSymbol().startsWith("leftop")
1288 && !lhs.getSymbol().startsWith("rightop")) {
1290 boolean hasWriteEffect = false;
1292 if (rhs.getType().getExtension() instanceof SSJavaType
1293 && lhs.getType().getExtension() instanceof SSJavaType) {
1295 CompositeLocation rhsCompLoc =
1296 ((SSJavaType) rhs.getType().getExtension()).getCompLoc();
1298 CompositeLocation lhsCompLoc =
1299 ((SSJavaType) lhs.getType().getExtension()).getCompLoc();
1301 if (lhsCompLoc != rhsCompLoc) {
1302 // have a write effect!
1303 hasWriteEffect = true;
1306 } else if (lhs.getType().isImmutable()) {
1307 hasWriteEffect = true;
1310 if (hasWriteEffect && mapHeapPath.containsKey(lhs)) {
1312 NTuple<Descriptor> lhsHeapPath = new NTuple<Descriptor>();
1313 lhsHeapPath.addAll(mapHeapPath.get(lhs));
1315 Location lhsLoc = getLocation(lhs);
1316 if (ssjava.isSharedLocation(lhsLoc)) {
1318 NTuple<Descriptor> varHeapPath = computePath(lhs);
1319 NTuple<Location> varLocTuple = mapDescriptorToLocationPath.get(lhs);
1321 Set<NTuple<Descriptor>> writtenSet =
1322 mapFlatNodeToSharedLocMapping.get(fn).get(varLocTuple);
1324 if (isCovered(varLocTuple, writtenSet)) {
1325 computeKILLSetForSharedWrite(curr, writtenSet, readWriteKillSet);
1326 computeGENSetForSharedAllCoverWrite(curr, writtenSet, readWriteGenSet);
1328 computeGENSetForSharedNonCoverWrite(curr, varHeapPath, readWriteGenSet);
1333 computeKILLSetForWrite(curr, lhsHeapPath, readWriteKillSet);
1334 computeGENSetForWrite(lhsHeapPath, readWriteGenSet);
1337 // System.out.println("write effect on =" + lhsHeapPath);
1338 // System.out.println("#KILLSET=" + readWriteKillSet);
1339 // System.out.println("#GENSet=" + readWriteGenSet + "\n");
1341 Set<WriteAge> writeAgeSet = curr.get(lhsHeapPath);
1342 checkWriteAgeSet(writeAgeSet, lhsHeapPath, fn);
1352 case FKind.FlatFieldNode:
1353 case FKind.FlatElementNode: {
1355 if (fn.kind() == FKind.FlatFieldNode) {
1356 FlatFieldNode ffn = (FlatFieldNode) fn;
1359 fld = ffn.getField();
1361 FlatElementNode fen = (FlatElementNode) fn;
1364 TypeDescriptor td = rhs.getType().dereference();
1365 fld = getArrayField(td);
1369 NTuple<Descriptor> srcHeapPath = mapHeapPath.get(rhs);
1370 NTuple<Descriptor> fldHeapPath;
1371 if (srcHeapPath != null) {
1372 fldHeapPath = new NTuple<Descriptor>(srcHeapPath.getList());
1374 // if srcHeapPath is null, it is static reference
1375 fldHeapPath = new NTuple<Descriptor>();
1376 fldHeapPath.add(rhs);
1378 fldHeapPath.add(fld);
1380 Set<WriteAge> writeAgeSet = curr.get(fldHeapPath);
1382 checkWriteAgeSet(writeAgeSet, fldHeapPath, fn);
1387 case FKind.FlatSetFieldNode:
1388 case FKind.FlatSetElementNode: {
1390 if (fn.kind() == FKind.FlatSetFieldNode) {
1391 FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
1392 lhs = fsfn.getDst();
1393 fld = fsfn.getField();
1395 FlatSetElementNode fsen = (FlatSetElementNode) fn;
1396 lhs = fsen.getDst();
1397 rhs = fsen.getSrc();
1398 TypeDescriptor td = lhs.getType().dereference();
1399 fld = getArrayField(td);
1403 NTuple<Descriptor> lhsHeapPath = mapHeapPath.get(lhs);
1404 if (lhsHeapPath != null) {
1406 NTuple<Descriptor> fldHeapPath = new NTuple<Descriptor>(lhsHeapPath.getList());
1407 if (fn.kind() == FKind.FlatSetFieldNode) {
1408 fldHeapPath.add(fld);
1411 // shared loc extension
1413 if (fn.kind() == FKind.FlatSetFieldNode) {
1414 fieldLoc = (Location) fld.getType().getExtension();
1416 NTuple<Location> locTuple = mapDescriptorToLocationPath.get(lhs);
1417 fieldLoc = locTuple.get(locTuple.size() - 1);
1420 if (ssjava.isSharedLocation(fieldLoc)) {
1422 NTuple<Location> fieldLocTuple = new NTuple<Location>();
1423 fieldLocTuple.addAll(mapDescriptorToLocationPath.get(lhs));
1424 if (fn.kind() == FKind.FlatSetFieldNode) {
1425 fieldLocTuple.add(fieldLoc);
1428 Set<NTuple<Descriptor>> writtenSet =
1429 mapFlatNodeToSharedLocMapping.get(fn).get(fieldLocTuple);
1431 if (isCovered(fieldLocTuple, writtenSet)) {
1432 computeKILLSetForSharedWrite(curr, writtenSet, readWriteKillSet);
1433 computeGENSetForSharedAllCoverWrite(curr, writtenSet, readWriteGenSet);
1435 computeGENSetForSharedNonCoverWrite(curr, fldHeapPath, readWriteGenSet);
1439 computeKILLSetForWrite(curr, fldHeapPath, readWriteKillSet);
1440 computeGENSetForWrite(fldHeapPath, readWriteGenSet);
1443 // System.out.println("KILLSET=" + readWriteKillSet);
1444 // System.out.println("GENSet=" + readWriteGenSet);
1451 case FKind.FlatCall: {
1452 FlatCall fc = (FlatCall) fn;
1454 SharedLocMap sharedLocMap = mapFlatNodeToSharedLocMapping.get(fc);
1455 // System.out.println("FLATCALL:" + fn);
1456 generateKILLSetForFlatCall(fc, curr, sharedLocMap, readWriteKillSet);
1457 generateGENSetForFlatCall(fc, sharedLocMap, readWriteGenSet);
1459 // System.out.println("KILLSET=" + readWriteKillSet);
1460 // System.out.println("GENSet=" + readWriteGenSet);
1467 computeNewMapping(curr, readWriteKillSet, readWriteGenSet);
1468 if (fn instanceof FlatCall) {
1469 checkManyRead((FlatCall) fn, curr);
1472 // System.out.println("#######" + curr);
1478 private void computeGENSetForSharedNonCoverWrite(
1479 Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr, NTuple<Descriptor> heapPath,
1480 Hashtable<NTuple<Descriptor>, Set<WriteAge>> genSet) {
1482 Set<WriteAge> writeAgeSet = genSet.get(heapPath);
1483 if (writeAgeSet == null) {
1484 writeAgeSet = new HashSet<WriteAge>();
1485 genSet.put(heapPath, writeAgeSet);
1488 writeAgeSet.add(new WriteAge(1));
1492 private void computeGENSetForSharedAllCoverWrite(
1493 Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr, Set<NTuple<Descriptor>> writtenSet,
1494 Hashtable<NTuple<Descriptor>, Set<WriteAge>> genSet) {
1496 for (Iterator iterator = writtenSet.iterator(); iterator.hasNext();) {
1497 NTuple<Descriptor> writeHeapPath = (NTuple<Descriptor>) iterator.next();
1499 Set<WriteAge> writeAgeSet = new HashSet<WriteAge>();
1500 writeAgeSet.add(new WriteAge(0));
1502 genSet.put(writeHeapPath, writeAgeSet);
1507 private void computeKILLSetForSharedWrite(Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr,
1508 Set<NTuple<Descriptor>> writtenSet, Hashtable<NTuple<Descriptor>, Set<WriteAge>> killSet) {
1510 for (Iterator iterator = writtenSet.iterator(); iterator.hasNext();) {
1511 NTuple<Descriptor> writeHeapPath = (NTuple<Descriptor>) iterator.next();
1512 Set<WriteAge> writeSet = curr.get(writeHeapPath);
1513 if (writeSet != null) {
1514 killSet.put(writeHeapPath, writeSet);
1520 private boolean isCovered(NTuple<Location> locTuple, Set<NTuple<Descriptor>> inSet) {
1522 if (inSet == null) {
1526 Set<NTuple<Descriptor>> coverSet =
1527 mapMethodToSharedLocCoverSet.get(methodContainingSSJavaLoop).get(locTuple);
1529 // System.out.println("locTuple=" + locTuple + " coverSet=" + coverSet +
1530 // " currSet=" + inSet);
1532 return inSet.containsAll(coverSet);
1535 private void checkManyRead(FlatCall fc, Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr) {
1537 Set<NTuple<Descriptor>> boundReadSet = mapFlatNodeToBoundReadSet.get(fc);
1539 for (Iterator iterator = boundReadSet.iterator(); iterator.hasNext();) {
1540 NTuple<Descriptor> readHeapPath = (NTuple<Descriptor>) iterator.next();
1541 Set<WriteAge> writeAgeSet = curr.get(readHeapPath);
1542 checkWriteAgeSet(writeAgeSet, readHeapPath, fc);
1547 private void checkWriteAgeSet(Set<WriteAge> writeAgeSet, NTuple<Descriptor> path, FlatNode fn) {
1549 // System.out.println("# CHECK WRITE AGE of " + path + " from set=" +
1552 if (writeAgeSet != null) {
1553 for (Iterator iterator = writeAgeSet.iterator(); iterator.hasNext();) {
1554 WriteAge writeAge = (WriteAge) iterator.next();
1555 if (writeAge.getAge() > MAXAGE) {
1556 generateErrorMessage(path, fn);
1562 private void generateErrorMessage(NTuple<Descriptor> path, FlatNode fn) {
1564 Descriptor lastDesc = path.get(getArrayBaseDescriptorIdx(path));
1565 if (ssjava.isSharedLocation(getLocation(lastDesc))) {
1567 NTuple<Location> locPathTuple = getLocationTuple(path);
1568 Set<NTuple<Descriptor>> coverSet =
1569 mapMethodToSharedLocCoverSet.get(methodContainingSSJavaLoop).get(locPathTuple);
1570 throw new Error("Shared memory locations, which is reachable through references " + path
1571 + ", are not completely overwritten by the higher values at "
1572 + methodContainingSSJavaLoop.getClassDesc().getSourceFileName() + "::" + fn.getNumLine()
1573 + ".\nThe following memory locations belong to the same shared locations:" + coverSet);
1577 "Memory location, which is reachable through references "
1579 + ", who comes back to the same read statement without being overwritten at the out-most iteration at "
1580 + methodContainingSSJavaLoop.getClassDesc().getSourceFileName() + "::"
1586 private void generateGENSetForFlatCall(FlatCall fc, SharedLocMap sharedLocMap,
1587 Hashtable<NTuple<Descriptor>, Set<WriteAge>> GENSet) {
1589 Set<NTuple<Descriptor>> boundMayWriteSet = mapFlatNodeToBoundMayWriteSet.get(fc);
1590 // System.out.println("boundMayWriteSet=" + boundMayWriteSet);
1592 for (Iterator iterator = boundMayWriteSet.iterator(); iterator.hasNext();) {
1593 NTuple<Descriptor> heapPath = (NTuple<Descriptor>) iterator.next();
1595 if (!isSharedLocation(heapPath)) {
1596 addWriteAgeToSet(heapPath, GENSet, new WriteAge(0));
1598 // if the current heap path is shared location
1600 NTuple<Location> locTuple = getLocationTuple(heapPath);
1602 Set<NTuple<Descriptor>> sharedWriteHeapPathSet = sharedLocMap.get(locTuple);
1604 if (isCovered(locTuple, sharedLocMap.get(locTuple))) {
1605 // if it is covered, add all of heap paths belong to the same shared
1606 // loc with write age 0
1608 for (Iterator iterator2 = sharedWriteHeapPathSet.iterator(); iterator2.hasNext();) {
1609 NTuple<Descriptor> sharedHeapPath = (NTuple<Descriptor>) iterator2.next();
1610 addWriteAgeToSet(sharedHeapPath, GENSet, new WriteAge(0));
1614 // if not covered, add write age 1 to the heap path that is
1615 // may-written but not covered
1616 addWriteAgeToSet(heapPath, GENSet, new WriteAge(1));
1625 private void addWriteAgeToSet(NTuple<Descriptor> heapPath,
1626 Hashtable<NTuple<Descriptor>, Set<WriteAge>> map, WriteAge age) {
1628 Set<WriteAge> currSet = map.get(heapPath);
1629 if (currSet == null) {
1630 currSet = new HashSet<WriteAge>();
1631 map.put(heapPath, currSet);
1637 private void generateKILLSetForFlatCall(FlatCall fc,
1638 Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr, SharedLocMap sharedLocMap,
1639 Hashtable<NTuple<Descriptor>, Set<WriteAge>> KILLSet) {
1641 Set<NTuple<Descriptor>> boundMustWriteSet = mapFlatNodeToBoundMustWriteSet.get(fc);
1643 for (Iterator iterator = boundMustWriteSet.iterator(); iterator.hasNext();) {
1644 NTuple<Descriptor> heapPath = (NTuple<Descriptor>) iterator.next();
1646 if (isSharedLocation(heapPath)) {
1647 NTuple<Location> locTuple = getLocationTuple(heapPath);
1649 if (isCovered(locTuple, sharedLocMap.get(locTuple)) && curr.containsKey(heapPath)) {
1650 // if it is shared loc and corresponding shared loc has been covered
1651 KILLSet.put(heapPath, curr.get(heapPath));
1655 for (Enumeration<NTuple<Descriptor>> e = curr.keys(); e.hasMoreElements();) {
1656 NTuple<Descriptor> key = e.nextElement();
1657 if (key.startsWith(heapPath)) {
1658 KILLSet.put(key, curr.get(key));
1668 private int getArrayBaseDescriptorIdx(NTuple<Descriptor> heapPath) {
1670 for (int i = heapPath.size() - 1; i >= 0; i--) {
1671 if (!heapPath.get(i).getSymbol().equals(arrayElementFieldName)) {
1680 private boolean isSharedLocation(NTuple<Descriptor> heapPath) {
1682 Descriptor d = heapPath.get(getArrayBaseDescriptorIdx(heapPath));
1684 return ssjava.isSharedLocation(getLocation(heapPath.get(getArrayBaseDescriptorIdx(heapPath))));
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 // if 'this' does not have heap path, it is local reference
1785 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(0), thisHeapPath);
1789 for (int i = 0; i < fc.numArgs(); i++) {
1790 TempDescriptor arg = fc.getArg(i);
1791 NTuple<Descriptor> argHeapPath = computePath(arg);
1792 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(i + 1), argHeapPath);
1795 for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
1796 MethodDescriptor callee = (MethodDescriptor) iterator.next();
1797 FlatMethod calleeFlatMethod = state.getMethodFlat(callee);
1799 // binding caller's args and callee's params
1801 Set<NTuple<Descriptor>> calleeReadSet = mapFlatMethodToReadSet.get(calleeFlatMethod);
1802 if (calleeReadSet == null) {
1803 calleeReadSet = new HashSet<NTuple<Descriptor>>();
1804 mapFlatMethodToReadSet.put(calleeFlatMethod, calleeReadSet);
1807 Set<NTuple<Descriptor>> calleeMustWriteSet =
1808 mapFlatMethodToMustWriteSet.get(calleeFlatMethod);
1810 if (calleeMustWriteSet == null) {
1811 calleeMustWriteSet = new HashSet<NTuple<Descriptor>>();
1812 mapFlatMethodToMustWriteSet.put(calleeFlatMethod, calleeMustWriteSet);
1815 Set<NTuple<Descriptor>> calleeMayWriteSet =
1816 mapFlatMethodToMayWriteSet.get(calleeFlatMethod);
1818 if (calleeMayWriteSet == null) {
1819 calleeMayWriteSet = new HashSet<NTuple<Descriptor>>();
1820 mapFlatMethodToMayWriteSet.put(calleeFlatMethod, calleeMayWriteSet);
1823 Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc =
1824 new Hashtable<Integer, TempDescriptor>();
1826 if (calleeFlatMethod.getMethod().isStatic()) {
1827 // static method does not have implicit 'this' arg
1830 for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
1831 TempDescriptor param = calleeFlatMethod.getParameter(i);
1832 mapParamIdx2ParamTempDesc.put(Integer.valueOf(i + offset), param);
1835 Set<NTuple<Descriptor>> calleeBoundReadSet =
1836 bindSet(calleeReadSet, mapParamIdx2ParamTempDesc, mapArgIdx2CallerArgHeapPath);
1837 // union of the current read set and the current callee's
1839 calleeUnionBoundReadSet.addAll(calleeBoundReadSet);
1841 Set<NTuple<Descriptor>> calleeBoundMustWriteSet =
1842 bindSet(calleeMustWriteSet, mapParamIdx2ParamTempDesc, mapArgIdx2CallerArgHeapPath);
1843 // intersection of the current overwrite set and the current
1846 merge(calleeIntersectBoundMustWriteSet, calleeBoundMustWriteSet);
1848 Set<NTuple<Descriptor>> boundWriteSetFromCallee =
1849 bindSet(calleeMayWriteSet, mapParamIdx2ParamTempDesc, mapArgIdx2CallerArgHeapPath);
1850 calleeUnionBoundMayWriteSet.addAll(boundWriteSetFromCallee);
1857 private void bindHeapPathCallerArgWithCaleeParamForSharedLoc(MethodDescriptor mdCaller,
1860 calleeIntersectBoundSharedSet.clear();
1861 calleeUnionBoundDeleteSet.clear();
1863 if (ssjava.isSSJavaUtil(fc.getMethod().getClassDesc())) {
1864 // ssjava util case!
1865 // have write effects on the first argument
1866 TempDescriptor arg = fc.getArg(0);
1867 NTuple<Descriptor> argHeapPath = computePath(arg);
1869 // convert heap path to location path
1870 NTuple<Location> argLocTuple = new NTuple<Location>();
1871 argLocTuple.addAll(deriveLocationTuple(mdCaller, (TempDescriptor) argHeapPath.get(0)));
1872 for (int i = 1; i < argHeapPath.size(); i++) {
1873 argLocTuple.add(getLocation(argHeapPath.get(i)));
1876 calleeIntersectBoundSharedSet.addWrite(argLocTuple, argHeapPath);
1878 } else if (ssjava.needTobeAnnotated(fc.getMethod())) {
1880 // if arg is not primitive type, we need to propagate maywritten set to
1881 // the caller's location path
1883 MethodDescriptor mdCallee = fc.getMethod();
1884 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
1885 setPossibleCallees.addAll(callGraph.getMethods(mdCallee));
1887 // create mapping from arg idx to its heap paths
1888 Hashtable<Integer, NTuple<Descriptor>> mapArgIdx2CallerArgHeapPath =
1889 new Hashtable<Integer, NTuple<Descriptor>>();
1891 // arg idx is starting from 'this' arg
1892 if (fc.getThis() != null) {
1893 NTuple<Descriptor> thisHeapPath = mapHeapPath.get(fc.getThis());
1894 if (thisHeapPath == null) {
1895 // method is called without creating new flat node representing 'this'
1896 thisHeapPath = new NTuple<Descriptor>();
1897 thisHeapPath.add(fc.getThis());
1900 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(0), thisHeapPath);
1903 for (int i = 0; i < fc.numArgs(); i++) {
1904 TempDescriptor arg = fc.getArg(i);
1905 NTuple<Descriptor> argHeapPath = computePath(arg);
1906 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(i + 1), argHeapPath);
1909 // create mapping from arg idx to its location paths
1910 Hashtable<Integer, NTuple<Location>> mapArgIdx2CallerAgLocationPath =
1911 new Hashtable<Integer, NTuple<Location>>();
1913 // arg idx is starting from 'this' arg
1914 if (fc.getThis() != null) {
1915 NTuple<Location> thisLocationPath = deriveLocationTuple(mdCaller, fc.getThis());
1916 if (thisLocationPath != null) {
1917 mapArgIdx2CallerAgLocationPath.put(Integer.valueOf(0), thisLocationPath);
1921 for (int i = 0; i < fc.numArgs(); i++) {
1922 TempDescriptor arg = fc.getArg(i);
1923 NTuple<Location> argLocationPath = deriveLocationTuple(mdCaller, arg);
1924 if (argLocationPath != null) {
1925 mapArgIdx2CallerAgLocationPath.put(Integer.valueOf(i + 1), argLocationPath);
1929 for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
1930 MethodDescriptor callee = (MethodDescriptor) iterator.next();
1931 FlatMethod calleeFlatMethod = state.getMethodFlat(callee);
1933 // binding caller's args and callee's params
1935 Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc =
1936 new Hashtable<Integer, TempDescriptor>();
1938 if (calleeFlatMethod.getMethod().isStatic()) {
1939 // static method does not have implicit 'this' arg
1942 for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
1943 TempDescriptor param = calleeFlatMethod.getParameter(i);
1944 mapParamIdx2ParamTempDesc.put(Integer.valueOf(i + offset), param);
1947 Set<Integer> keySet = mapArgIdx2CallerAgLocationPath.keySet();
1948 for (Iterator iterator2 = keySet.iterator(); iterator2.hasNext();) {
1949 Integer idx = (Integer) iterator2.next();
1950 NTuple<Location> callerArgLocationPath = mapArgIdx2CallerAgLocationPath.get(idx);
1951 NTuple<Descriptor> callerArgHeapPath = mapArgIdx2CallerArgHeapPath.get(idx);
1953 TempDescriptor calleeParam = mapParamIdx2ParamTempDesc.get(idx);
1954 NTuple<Location> calleeLocationPath = deriveLocationTuple(mdCallee, calleeParam);
1955 SharedLocMap calleeDeleteSet = mapFlatMethodToDeleteSet.get(calleeFlatMethod);
1956 SharedLocMap calleeSharedLocMap = mapFlatMethodToSharedLocMap.get(calleeFlatMethod);
1958 if (calleeDeleteSet != null) {
1959 createNewMappingOfDeleteSet(callerArgLocationPath, callerArgHeapPath,
1960 calleeLocationPath, calleeDeleteSet);
1963 if (calleeSharedLocMap != null) {
1964 createNewMappingOfSharedSet(callerArgLocationPath, callerArgHeapPath,
1965 calleeLocationPath, calleeSharedLocMap);
1975 private void createNewMappingOfDeleteSet(NTuple<Location> callerArgLocationPath,
1976 NTuple<Descriptor> callerArgHeapPath, NTuple<Location> calleeLocationPath,
1977 SharedLocMap calleeDeleteSet) {
1979 SharedLocMap calleeParamDeleteSet = calleeDeleteSet.getHeapPathStartedWith(calleeLocationPath);
1981 Set<NTuple<Location>> keySet = calleeParamDeleteSet.keySet();
1982 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1983 NTuple<Location> calleeLocTupleKey = (NTuple<Location>) iterator.next();
1984 Set<NTuple<Descriptor>> heapPathSet = calleeParamDeleteSet.get(calleeLocTupleKey);
1985 for (Iterator iterator2 = heapPathSet.iterator(); iterator2.hasNext();) {
1986 NTuple<Descriptor> calleeHeapPath = (NTuple<Descriptor>) iterator2.next();
1987 calleeUnionBoundDeleteSet.addWrite(
1988 bindLocationPath(callerArgLocationPath, calleeLocTupleKey),
1989 bindHeapPath(callerArgHeapPath, calleeHeapPath));
1995 private void createNewMappingOfSharedSet(NTuple<Location> callerArgLocationPath,
1996 NTuple<Descriptor> callerArgHeapPath, NTuple<Location> calleeLocationPath,
1997 SharedLocMap calleeSharedLocMap) {
1999 SharedLocMap calleeParamSharedSet =
2000 calleeSharedLocMap.getHeapPathStartedWith(calleeLocationPath);
2002 Set<NTuple<Location>> keySet = calleeParamSharedSet.keySet();
2003 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2004 NTuple<Location> calleeLocTupleKey = (NTuple<Location>) iterator.next();
2005 Set<NTuple<Descriptor>> heapPathSet = calleeParamSharedSet.get(calleeLocTupleKey);
2006 Set<NTuple<Descriptor>> boundHeapPathSet = new HashSet<NTuple<Descriptor>>();
2007 for (Iterator iterator2 = heapPathSet.iterator(); iterator2.hasNext();) {
2008 NTuple<Descriptor> calleeHeapPath = (NTuple<Descriptor>) iterator2.next();
2009 boundHeapPathSet.add(bindHeapPath(callerArgHeapPath, calleeHeapPath));
2011 calleeIntersectBoundSharedSet.intersect(
2012 bindLocationPath(callerArgLocationPath, calleeLocTupleKey), boundHeapPathSet);
2017 private NTuple<Location> bindLocationPath(NTuple<Location> start, NTuple<Location> end) {
2018 NTuple<Location> locPath = new NTuple<Location>();
2019 locPath.addAll(start);
2020 for (int i = 1; i < end.size(); i++) {
2021 locPath.add(end.get(i));
2026 private NTuple<Descriptor> bindHeapPath(NTuple<Descriptor> start, NTuple<Descriptor> end) {
2027 NTuple<Descriptor> heapPath = new NTuple<Descriptor>();
2028 heapPath.addAll(start);
2029 for (int i = 1; i < end.size(); i++) {
2030 heapPath.add(end.get(i));
2035 private void initialize() {
2036 // First, identify ssjava loop entrace
2038 // no need to analyze method having ssjava loop
2039 methodContainingSSJavaLoop = ssjava.getMethodContainingSSJavaLoop();
2041 FlatMethod fm = state.getMethodFlat(methodContainingSSJavaLoop);
2042 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
2043 flatNodesToVisit.add(fm);
2045 LoopFinder loopFinder = new LoopFinder(fm);
2047 while (!flatNodesToVisit.isEmpty()) {
2048 FlatNode fn = flatNodesToVisit.iterator().next();
2049 flatNodesToVisit.remove(fn);
2051 String label = (String) state.fn2labelMap.get(fn);
2052 if (label != null) {
2054 if (label.equals(ssjava.SSJAVA)) {
2055 ssjava.setSSJavaLoopEntrance(fn);
2060 for (int i = 0; i < fn.numNext(); i++) {
2061 FlatNode nn = fn.getNext(i);
2062 flatNodesToVisit.add(nn);
2066 assert ssjava.getSSJavaLoopEntrance() != null;
2068 // assume that ssjava loop is top-level loop in method, not nested loop
2069 Set nestedLoop = loopFinder.nestedLoops();
2070 for (Iterator loopIter = nestedLoop.iterator(); loopIter.hasNext();) {
2071 LoopFinder lf = (LoopFinder) loopIter.next();
2072 if (lf.loopEntrances().iterator().next().equals(ssjava.getSSJavaLoopEntrance())) {
2077 assert ssjavaLoop != null;
2079 loopIncElements = (Set<FlatNode>) ssjavaLoop.loopIncElements();
2081 // perform topological sort over the set of methods accessed by the main
2083 Set<MethodDescriptor> methodDescriptorsToAnalyze = new HashSet<MethodDescriptor>();
2084 methodDescriptorsToAnalyze.addAll(ssjava.getAnnotationRequireSet());
2085 sortedDescriptors = topologicalSort(methodDescriptorsToAnalyze);
2087 liveInTempSetToEventLoop =
2088 liveness.getLiveInTemps(state.getMethodFlat(methodContainingSSJavaLoop),
2089 ssjava.getSSJavaLoopEntrance());
2092 private void methodReadWriteSetAnalysis() {
2093 // perform method READ/OVERWRITE analysis
2094 LinkedList<MethodDescriptor> descriptorListToAnalyze =
2095 (LinkedList<MethodDescriptor>) sortedDescriptors.clone();
2097 // current descriptors to visit in fixed-point interprocedural analysis,
2099 // dependency in the call graph
2100 methodDescriptorsToVisitStack.clear();
2102 descriptorListToAnalyze.removeFirst();
2104 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
2105 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
2107 while (!descriptorListToAnalyze.isEmpty()) {
2108 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
2109 methodDescriptorsToVisitStack.add(md);
2112 // analyze scheduled methods until there are no more to visit
2113 while (!methodDescriptorsToVisitStack.isEmpty()) {
2114 // start to analyze leaf node
2115 MethodDescriptor md = methodDescriptorsToVisitStack.pop();
2116 FlatMethod fm = state.getMethodFlat(md);
2118 Set<NTuple<Descriptor>> readSet = new HashSet<NTuple<Descriptor>>();
2119 Set<NTuple<Descriptor>> mustWriteSet = new HashSet<NTuple<Descriptor>>();
2120 Set<NTuple<Descriptor>> mayWriteSet = new HashSet<NTuple<Descriptor>>();
2122 methodReadWriteSet_analyzeMethod(fm, readSet, mustWriteSet, mayWriteSet);
2124 Set<NTuple<Descriptor>> prevRead = mapFlatMethodToReadSet.get(fm);
2125 Set<NTuple<Descriptor>> prevMustWrite = mapFlatMethodToMustWriteSet.get(fm);
2126 Set<NTuple<Descriptor>> prevMayWrite = mapFlatMethodToMayWriteSet.get(fm);
2128 if (!(readSet.equals(prevRead) && mustWriteSet.equals(prevMustWrite) && mayWriteSet
2129 .equals(prevMayWrite))) {
2130 mapFlatMethodToReadSet.put(fm, readSet);
2131 mapFlatMethodToMustWriteSet.put(fm, mustWriteSet);
2132 mapFlatMethodToMayWriteSet.put(fm, mayWriteSet);
2134 // results for callee changed, so enqueue dependents caller for
2137 Iterator<MethodDescriptor> depsItr = getDependents(md).iterator();
2138 while (depsItr.hasNext()) {
2139 MethodDescriptor methodNext = depsItr.next();
2140 if (!methodDescriptorsToVisitStack.contains(methodNext)
2141 && methodDescriptorToVistSet.contains(methodNext)) {
2142 methodDescriptorsToVisitStack.add(methodNext);
2151 methodReadWriteSetAnalysisToEventLoopBody();
2155 private void methodReadWriteSet_analyzeMethod(FlatMethod fm, Set<NTuple<Descriptor>> readSet,
2156 Set<NTuple<Descriptor>> mustWriteSet, Set<NTuple<Descriptor>> mayWriteSet) {
2157 if (state.SSJAVADEBUG) {
2158 System.out.println("SSJAVA: Definitely written Analyzing: " + fm);
2161 methodReadWriteSet_analyzeBody(fm, readSet, mustWriteSet, mayWriteSet, false);
2165 private void methodReadWriteSetAnalysisToEventLoopBody() {
2167 // perform method read/write analysis for Event Loop Body
2169 FlatMethod flatMethodContainingSSJavaLoop = state.getMethodFlat(methodContainingSSJavaLoop);
2171 if (state.SSJAVADEBUG) {
2172 System.out.println("SSJAVA: Definitely written Event Loop Analyzing: "
2173 + flatMethodContainingSSJavaLoop);
2176 Set<NTuple<Descriptor>> readSet = new HashSet<NTuple<Descriptor>>();
2177 Set<NTuple<Descriptor>> mustWriteSet = new HashSet<NTuple<Descriptor>>();
2178 Set<NTuple<Descriptor>> mayWriteSet = new HashSet<NTuple<Descriptor>>();
2180 mapFlatMethodToReadSet.put(flatMethodContainingSSJavaLoop, readSet);
2181 mapFlatMethodToMustWriteSet.put(flatMethodContainingSSJavaLoop, mustWriteSet);
2182 mapFlatMethodToMayWriteSet.put(flatMethodContainingSSJavaLoop, mayWriteSet);
2184 for (Iterator iterator = liveInTempSetToEventLoop.iterator(); iterator.hasNext();) {
2185 TempDescriptor liveIn = (TempDescriptor) iterator.next();
2186 NTuple<Descriptor> heapPath = new NTuple<Descriptor>();
2187 heapPath.add(liveIn);
2188 mapHeapPath.put(liveIn, heapPath);
2191 methodReadWriteSet_analyzeBody(ssjava.getSSJavaLoopEntrance(), readSet, mustWriteSet,
2196 private void methodReadWriteSet_analyzeBody(FlatNode startNode, Set<NTuple<Descriptor>> readSet,
2197 Set<NTuple<Descriptor>> mustWriteSet, Set<NTuple<Descriptor>> mayWriteSet,
2198 boolean isEventLoopBody) {
2200 // intraprocedural analysis
2201 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
2202 flatNodesToVisit.add(startNode);
2204 while (!flatNodesToVisit.isEmpty()) {
2205 FlatNode fn = flatNodesToVisit.iterator().next();
2206 flatNodesToVisit.remove(fn);
2208 Set<NTuple<Descriptor>> currMustWriteSet = new HashSet<NTuple<Descriptor>>();
2210 for (int i = 0; i < fn.numPrev(); i++) {
2211 FlatNode prevFn = fn.getPrev(i);
2212 Set<NTuple<Descriptor>> in = mapFlatNodeToMustWriteSet.get(prevFn);
2214 merge(currMustWriteSet, in);
2218 methodReadWriteSet_nodeActions(fn, currMustWriteSet, readSet, mustWriteSet, mayWriteSet,
2221 Set<NTuple<Descriptor>> mustSetPrev = mapFlatNodeToMustWriteSet.get(fn);
2223 if (!currMustWriteSet.equals(mustSetPrev)) {
2224 mapFlatNodeToMustWriteSet.put(fn, currMustWriteSet);
2225 for (int i = 0; i < fn.numNext(); i++) {
2226 FlatNode nn = fn.getNext(i);
2227 if ((!isEventLoopBody) || loopIncElements.contains(nn)) {
2228 flatNodesToVisit.add(nn);
2238 private void methodReadWriteSet_nodeActions(FlatNode fn,
2239 Set<NTuple<Descriptor>> currMustWriteSet, Set<NTuple<Descriptor>> readSet,
2240 Set<NTuple<Descriptor>> mustWriteSet, Set<NTuple<Descriptor>> mayWriteSet,
2241 boolean isEventLoopBody) {
2245 FieldDescriptor fld;
2247 switch (fn.kind()) {
2248 case FKind.FlatMethod: {
2250 // set up initial heap paths for method parameters
2251 FlatMethod fm = (FlatMethod) fn;
2252 for (int i = 0; i < fm.numParameters(); i++) {
2253 TempDescriptor param = fm.getParameter(i);
2254 NTuple<Descriptor> heapPath = new NTuple<Descriptor>();
2255 heapPath.add(param);
2256 mapHeapPath.put(param, heapPath);
2261 case FKind.FlatOpNode: {
2262 FlatOpNode fon = (FlatOpNode) fn;
2263 // for a normal assign node, need to propagate lhs's heap path to
2266 if (fon.getOp().getOp() == Operation.ASSIGN) {
2267 rhs = fon.getLeft();
2268 lhs = fon.getDest();
2270 NTuple<Descriptor> rhsHeapPath = mapHeapPath.get(rhs);
2272 // if (lhs.getType().isPrimitive()) {
2273 // NTuple<Descriptor> lhsHeapPath = new NTuple<Descriptor>();
2274 // lhsHeapPath.add(lhs);
2275 // mapHeapPath.put(lhs, lhsHeapPath);
2278 if (rhsHeapPath != null && (!lhs.getType().isPrimitive())) {
2279 mapHeapPath.put(lhs, mapHeapPath.get(rhs));
2282 // if (isEventLoopBody) {
2283 // NTuple<Descriptor> lhsHeapPath = new NTuple<Descriptor>();
2284 // lhsHeapPath.add(rhs);
2285 // mapHeapPath.put(lhs, lhsHeapPath);
2291 // shared loc extension
2292 if (isEventLoopBody) {
2293 if (!lhs.getSymbol().startsWith("neverused") && rhs.getType().isImmutable()) {
2295 if (rhs.getType().getExtension() instanceof Location
2296 && lhs.getType().getExtension() instanceof CompositeLocation) {
2298 Location rhsLoc = (Location) rhs.getType().getExtension();
2300 CompositeLocation lhsCompLoc = (CompositeLocation) lhs.getType().getExtension();
2301 Location dstLoc = lhsCompLoc.get(lhsCompLoc.getSize() - 1);
2303 NTuple<Descriptor> heapPath = new NTuple<Descriptor>();
2304 for (int i = 0; i < rhsHeapPath.size() - 1; i++) {
2305 heapPath.add(rhsHeapPath.get(i));
2308 NTuple<Descriptor> writeHeapPath = new NTuple<Descriptor>();
2309 writeHeapPath.addAll(heapPath);
2310 writeHeapPath.add(lhs);
2320 case FKind.FlatElementNode:
2321 case FKind.FlatFieldNode: {
2325 if (fn.kind() == FKind.FlatFieldNode) {
2326 FlatFieldNode ffn = (FlatFieldNode) fn;
2329 fld = ffn.getField();
2331 FlatElementNode fen = (FlatElementNode) fn;
2334 TypeDescriptor td = rhs.getType().dereference();
2335 fld = getArrayField(td);
2338 if (fld.isFinal()) {
2339 // if field is final no need to check
2344 NTuple<Descriptor> srcHeapPath = mapHeapPath.get(rhs);
2345 if (srcHeapPath != null) {
2346 // if lhs srcHeapPath is null, it means that it is not reachable from
2347 // callee's parameters. so just ignore it
2349 NTuple<Descriptor> readingHeapPath = new NTuple<Descriptor>(srcHeapPath.getList());
2350 if (fn.kind() == FKind.FlatFieldNode) {
2351 readingHeapPath.add(fld);
2354 mapHeapPath.put(lhs, readingHeapPath);
2357 if (fld.getType().isImmutable()) {
2358 // if WT doesnot have hp(x.f), add hp(x.f) to READ
2359 if (!currMustWriteSet.contains(readingHeapPath)) {
2360 readSet.add(readingHeapPath);
2364 // no need to kill hp(x.f) from WT
2370 case FKind.FlatSetFieldNode:
2371 case FKind.FlatSetElementNode: {
2375 if (fn.kind() == FKind.FlatSetFieldNode) {
2376 FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
2377 lhs = fsfn.getDst();
2378 fld = fsfn.getField();
2379 rhs = fsfn.getSrc();
2381 FlatSetElementNode fsen = (FlatSetElementNode) fn;
2382 lhs = fsen.getDst();
2383 rhs = fsen.getSrc();
2384 TypeDescriptor td = lhs.getType().dereference();
2385 fld = getArrayField(td);
2389 NTuple<Descriptor> lhsHeapPath = mapHeapPath.get(lhs);
2391 if (lhsHeapPath != null) {
2392 // if lhs heap path is null, it means that it is not reachable from
2393 // callee's parameters. so just ignore it
2394 NTuple<Descriptor> fldHeapPath = new NTuple<Descriptor>(lhsHeapPath.getList());
2395 if (fn.kind() != FKind.FlatSetElementNode) {
2396 fldHeapPath.add(fld);
2398 // mapHeapPath.put(fld, fldHeapPath);
2401 // need to add hp(y) to WT
2402 if (fn.kind() != FKind.FlatSetElementNode) {
2403 currMustWriteSet.add(fldHeapPath);
2405 mayWriteSet.add(fldHeapPath);
2412 case FKind.FlatCall: {
2414 FlatCall fc = (FlatCall) fn;
2416 bindHeapPathCallerArgWithCalleeParam(fc);
2418 Set<NTuple<Descriptor>> boundReadSet = new HashSet<NTuple<Descriptor>>();
2419 boundReadSet.addAll(calleeUnionBoundReadSet);
2421 Set<NTuple<Descriptor>> boundMustWriteSet = new HashSet<NTuple<Descriptor>>();
2422 boundMustWriteSet.addAll(calleeIntersectBoundMustWriteSet);
2424 Set<NTuple<Descriptor>> boundMayWriteSet = new HashSet<NTuple<Descriptor>>();
2425 boundMayWriteSet.addAll(calleeUnionBoundMayWriteSet);
2427 mapFlatNodeToBoundReadSet.put(fn, boundReadSet);
2428 mapFlatNodeToBoundMustWriteSet.put(fn, boundMustWriteSet);
2429 mapFlatNodeToBoundMayWriteSet.put(fn, boundMayWriteSet);
2431 // add heap path, which is an element of READ_bound set and is not
2433 // element of WT set, to the caller's READ set
2434 for (Iterator iterator = calleeUnionBoundReadSet.iterator(); iterator.hasNext();) {
2435 NTuple<Descriptor> read = (NTuple<Descriptor>) iterator.next();
2436 if (!currMustWriteSet.contains(read)) {
2441 // add heap path, which is an element of OVERWRITE_bound set, to the
2443 for (Iterator iterator = calleeIntersectBoundMustWriteSet.iterator(); iterator.hasNext();) {
2444 NTuple<Descriptor> write = (NTuple<Descriptor>) iterator.next();
2445 currMustWriteSet.add(write);
2448 // add heap path, which is an element of WRITE_BOUND set, to the
2449 // caller's writeSet
2450 for (Iterator iterator = calleeUnionBoundMayWriteSet.iterator(); iterator.hasNext();) {
2451 NTuple<Descriptor> write = (NTuple<Descriptor>) iterator.next();
2452 mayWriteSet.add(write);
2458 case FKind.FlatExit: {
2459 // merge the current written set with OVERWRITE set
2460 merge(mustWriteSet, currMustWriteSet);
2468 static public FieldDescriptor getArrayField(TypeDescriptor td) {
2469 FieldDescriptor fd = mapTypeToArrayField.get(td);
2472 new FieldDescriptor(new Modifiers(Modifiers.PUBLIC), td, arrayElementFieldName, null,
2474 mapTypeToArrayField.put(td, fd);
2479 private void merge(Set<NTuple<Descriptor>> curr, Set<NTuple<Descriptor>> in) {
2480 if (curr.isEmpty()) {
2481 // set has a special initial value which covers all possible
2483 // For the first time of intersection, we can take all previous set
2486 // otherwise, current set is the intersection of the two sets
2492 // combine two heap path
2493 private NTuple<Descriptor> combine(NTuple<Descriptor> callerIn, NTuple<Descriptor> calleeIn) {
2494 NTuple<Descriptor> combined = new NTuple<Descriptor>();
2496 for (int i = 0; i < callerIn.size(); i++) {
2497 combined.add(callerIn.get(i));
2500 // the first element of callee's heap path represents parameter
2501 // so we skip the first one since it is already added from caller's heap
2503 for (int i = 1; i < calleeIn.size(); i++) {
2504 combined.add(calleeIn.get(i));
2510 private Set<NTuple<Descriptor>> bindSet(Set<NTuple<Descriptor>> calleeSet,
2511 Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc,
2512 Hashtable<Integer, NTuple<Descriptor>> mapCallerArgIdx2HeapPath) {
2514 Set<NTuple<Descriptor>> boundedCalleeSet = new HashSet<NTuple<Descriptor>>();
2516 Set<Integer> keySet = mapCallerArgIdx2HeapPath.keySet();
2517 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2518 Integer idx = (Integer) iterator.next();
2520 NTuple<Descriptor> callerArgHeapPath = mapCallerArgIdx2HeapPath.get(idx);
2521 TempDescriptor calleeParam = mapParamIdx2ParamTempDesc.get(idx);
2522 for (Iterator iterator2 = calleeSet.iterator(); iterator2.hasNext();) {
2523 NTuple<Descriptor> element = (NTuple<Descriptor>) iterator2.next();
2524 if (element.startsWith(calleeParam)) {
2525 NTuple<Descriptor> boundElement = combine(callerArgHeapPath, element);
2526 boundedCalleeSet.add(boundElement);
2532 return boundedCalleeSet;
2536 // Borrowed it from disjoint analysis
2537 private LinkedList<MethodDescriptor> topologicalSort(Set<MethodDescriptor> toSort) {
2539 Set<MethodDescriptor> discovered = new HashSet<MethodDescriptor>();
2541 LinkedList<MethodDescriptor> sorted = new LinkedList<MethodDescriptor>();
2543 Iterator<MethodDescriptor> itr = toSort.iterator();
2544 while (itr.hasNext()) {
2545 MethodDescriptor d = itr.next();
2547 if (!discovered.contains(d)) {
2548 dfsVisit(d, toSort, sorted, discovered);
2555 // While we're doing DFS on call graph, remember
2556 // dependencies for efficient queuing of methods
2557 // during interprocedural analysis:
2559 // a dependent of a method decriptor d for this analysis is:
2560 // 1) a method or task that invokes d
2561 // 2) in the descriptorsToAnalyze set
2562 private void dfsVisit(MethodDescriptor md, Set<MethodDescriptor> toSort,
2563 LinkedList<MethodDescriptor> sorted, Set<MethodDescriptor> discovered) {
2567 Iterator itr = callGraph.getCallerSet(md).iterator();
2568 while (itr.hasNext()) {
2569 MethodDescriptor dCaller = (MethodDescriptor) itr.next();
2570 // only consider callers in the original set to analyze
2571 if (!toSort.contains(dCaller)) {
2574 if (!discovered.contains(dCaller)) {
2575 addDependent(md, // callee
2579 dfsVisit(dCaller, toSort, sorted, discovered);
2583 // for leaf-nodes last now!
2587 // a dependent of a method decriptor d for this analysis is:
2588 // 1) a method or task that invokes d
2589 // 2) in the descriptorsToAnalyze set
2590 private void addDependent(MethodDescriptor callee, MethodDescriptor caller) {
2591 Set<MethodDescriptor> deps = mapDescriptorToSetDependents.get(callee);
2593 deps = new HashSet<MethodDescriptor>();
2596 mapDescriptorToSetDependents.put(callee, deps);
2599 private Set<MethodDescriptor> getDependents(MethodDescriptor callee) {
2600 Set<MethodDescriptor> deps = mapDescriptorToSetDependents.get(callee);
2602 deps = new HashSet<MethodDescriptor>();
2603 mapDescriptorToSetDependents.put(callee, deps);
2608 private NTuple<Descriptor> computePath(Descriptor td) {
2609 // generate proper path fot input td
2610 // if td is local variable, it just generate one element tuple path
2611 if (mapHeapPath.containsKey(td)) {
2612 NTuple<Descriptor> rtrHeapPath = new NTuple<Descriptor>();
2613 rtrHeapPath.addAll(mapHeapPath.get(td));
2616 NTuple<Descriptor> rtrHeapPath = new NTuple<Descriptor>();
2617 rtrHeapPath.add(td);
2622 private NTuple<Location> deriveThisLocationTuple(MethodDescriptor md) {
2623 String thisLocIdentifier = ssjava.getMethodLattice(md).getThisLoc();
2624 Location thisLoc = new Location(md, thisLocIdentifier);
2625 NTuple<Location> locTuple = new NTuple<Location>();
2626 locTuple.add(thisLoc);
2630 private NTuple<Location> deriveGlobalLocationTuple(MethodDescriptor md) {
2631 String globalLocIdentifier = ssjava.getMethodLattice(md).getGlobalLoc();
2632 Location globalLoc = new Location(md, globalLocIdentifier);
2633 NTuple<Location> locTuple = new NTuple<Location>();
2634 locTuple.add(globalLoc);
2638 private NTuple<Location> deriveLocationTuple(MethodDescriptor md, TempDescriptor td) {
2640 assert td.getType() != null;
2642 if (mapDescriptorToLocationPath.containsKey(td)) {
2643 NTuple<Location> locPath = mapDescriptorToLocationPath.get(td);
2644 NTuple<Location> rtrPath = new NTuple<Location>();
2645 rtrPath.addAll(locPath);
2648 if (td.getSymbol().startsWith("this")) {
2649 NTuple<Location> thisPath = deriveThisLocationTuple(md);
2650 NTuple<Location> rtrPath = new NTuple<Location>();
2651 rtrPath.addAll(thisPath);
2655 if (td.getType().getExtension() != null) {
2656 SSJavaType ssJavaType = (SSJavaType) td.getType().getExtension();
2657 if (ssJavaType.getCompLoc() != null) {
2658 NTuple<Location> rtrPath = new NTuple<Location>();
2659 rtrPath.addAll(ssJavaType.getCompLoc().getTuple());