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();
187 // System.out.println("$$$=" +
188 // mapMethodToSharedLocCoverSet.get(methodContainingSSJavaLoop));
198 private void sharedLocAnalysis() {
200 // perform method READ/OVERWRITE analysis
201 LinkedList<MethodDescriptor> descriptorListToAnalyze =
202 (LinkedList<MethodDescriptor>) sortedDescriptors.clone();
204 // current descriptors to visit in fixed-point interprocedural analysis,
206 // dependency in the call graph
207 methodDescriptorsToVisitStack.clear();
209 descriptorListToAnalyze.removeFirst();
211 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
212 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
214 while (!descriptorListToAnalyze.isEmpty()) {
215 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
216 methodDescriptorsToVisitStack.add(md);
219 // analyze scheduled methods until there are no more to visit
220 while (!methodDescriptorsToVisitStack.isEmpty()) {
221 // start to analyze leaf node
222 MethodDescriptor md = methodDescriptorsToVisitStack.pop();
223 FlatMethod fm = state.getMethodFlat(md);
225 SharedLocMap sharedLocMap = new SharedLocMap();
226 SharedLocMap deleteSet = new SharedLocMap();
228 sharedLoc_analyzeMethod(fm, sharedLocMap, deleteSet);
229 SharedLocMap prevSharedLocMap = mapFlatMethodToSharedLocMap.get(fm);
230 SharedLocMap prevDeleteSet = mapFlatMethodToDeleteSet.get(fm);
232 if (!(deleteSet.equals(prevDeleteSet) && sharedLocMap.equals(prevSharedLocMap))) {
233 mapFlatMethodToSharedLocMap.put(fm, sharedLocMap);
234 mapFlatMethodToDeleteSet.put(fm, deleteSet);
236 // results for callee changed, so enqueue dependents caller for
239 Iterator<MethodDescriptor> depsItr = getDependents(md).iterator();
240 while (depsItr.hasNext()) {
241 MethodDescriptor methodNext = depsItr.next();
242 if (!methodDescriptorsToVisitStack.contains(methodNext)
243 && methodDescriptorToVistSet.contains(methodNext)) {
244 methodDescriptorsToVisitStack.add(methodNext);
253 sharedLoc_analyzeEventLoop();
257 private void sharedLoc_analyzeEventLoop() {
258 if (state.SSJAVADEBUG) {
259 System.out.println("SSJAVA: Definite clearance for shared locations Analyzing: eventloop");
261 SharedLocMap sharedLocMap = new SharedLocMap();
262 SharedLocMap deleteSet = new SharedLocMap();
263 sharedLoc_analyzeBody(state.getMethodFlat(methodContainingSSJavaLoop),
264 ssjava.getSSJavaLoopEntrance(), sharedLocMap, deleteSet, true);
268 private void sharedLoc_analyzeMethod(FlatMethod fm, SharedLocMap sharedLocMap,
269 SharedLocMap deleteSet) {
270 if (state.SSJAVADEBUG) {
271 System.out.println("SSJAVA: Definite clearance for shared locations Analyzing: " + fm);
274 sharedLoc_analyzeBody(fm, fm, sharedLocMap, deleteSet, false);
278 private void sharedLoc_analyzeBody(FlatMethod fm, FlatNode startNode, SharedLocMap sharedLocMap,
279 SharedLocMap deleteSet, boolean isEventLoopBody) {
281 // intraprocedural analysis
282 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
283 flatNodesToVisit.add(startNode);
285 while (!flatNodesToVisit.isEmpty()) {
286 FlatNode fn = flatNodesToVisit.iterator().next();
287 flatNodesToVisit.remove(fn);
289 SharedLocMap currSharedSet = new SharedLocMap();
290 SharedLocMap currDeleteSet = new SharedLocMap();
292 for (int i = 0; i < fn.numPrev(); i++) {
293 FlatNode prevFn = fn.getPrev(i);
294 SharedLocMap inSharedLoc = mapFlatNodeToSharedLocMapping.get(prevFn);
295 if (inSharedLoc != null) {
296 mergeSharedLocMap(currSharedSet, inSharedLoc);
299 SharedLocMap inDeleteLoc = mapFlatNodeToDeleteSet.get(prevFn);
300 if (inDeleteLoc != null) {
301 mergeDeleteSet(currDeleteSet, inDeleteLoc);
305 sharedLoc_nodeActions(fm, fn, currSharedSet, currDeleteSet, sharedLocMap, deleteSet,
308 SharedLocMap prevSharedSet = mapFlatNodeToSharedLocMapping.get(fn);
309 SharedLocMap prevDeleteSet = mapFlatNodeToDeleteSet.get(fn);
311 if (!(currSharedSet.equals(prevSharedSet) && currDeleteSet.equals(prevDeleteSet))) {
312 mapFlatNodeToSharedLocMapping.put(fn, currSharedSet);
313 mapFlatNodeToDeleteSet.put(fn, currDeleteSet);
314 for (int i = 0; i < fn.numNext(); i++) {
315 FlatNode nn = fn.getNext(i);
316 if ((!isEventLoopBody) || loopIncElements.contains(nn)) {
317 flatNodesToVisit.add(nn);
327 private void sharedLoc_nodeActions(FlatMethod fm, FlatNode fn, SharedLocMap curr,
328 SharedLocMap currDeleteSet, SharedLocMap sharedLocMap, SharedLocMap deleteSet,
329 boolean isEventLoopBody) {
331 MethodDescriptor md = fm.getMethod();
333 SharedLocMap killSet = new SharedLocMap();
334 SharedLocMap genSet = new SharedLocMap();
342 case FKind.FlatOpNode: {
344 if (isEventLoopBody) {
345 FlatOpNode fon = (FlatOpNode) fn;
347 if (fon.getOp().getOp() == Operation.ASSIGN) {
351 if (!lhs.getSymbol().startsWith("neverused") && !lhs.getSymbol().startsWith("leftop")
352 && !lhs.getSymbol().startsWith("rightop") && rhs.getType().isImmutable()) {
354 if (mapHeapPath.containsKey(rhs)) {
355 Location dstLoc = getLocation(lhs);
356 if (dstLoc != null && ssjava.isSharedLocation(dstLoc)) {
357 NTuple<Descriptor> lhsHeapPath = computePath(lhs);
358 NTuple<Location> lhsLocTuple = mapDescriptorToLocationPath.get(lhs);
360 Location srcLoc = getLocation(lhs);
362 // computing gen/kill set
363 computeKILLSetForWrite(curr, killSet, lhsLocTuple, lhsHeapPath);
364 if (!dstLoc.equals(srcLoc)) {
365 computeGENSetForHigherWrite(curr, killSet, lhsLocTuple, lhsHeapPath);
366 updateDeleteSetForHigherWrite(currDeleteSet, lhsLocTuple, lhsHeapPath);
368 computeGENSetForSameHeightWrite(curr, killSet, lhsLocTuple, lhsHeapPath);
369 updateDeleteSetForSameHeightWrite(currDeleteSet, lhsLocTuple, lhsHeapPath);
372 // System.out.println("VAR WRITE:" + fn);
373 // System.out.println("lhsLocTuple=" + lhsLocTuple +
374 // " lhsHeapPath=" + lhsHeapPath);
375 // System.out.println("dstLoc=" + dstLoc + " srcLoc=" + srcLoc);
376 // System.out.println("KILLSET=" + killSet);
377 // System.out.println("GENSet=" + genSet);
378 // System.out.println("DELETESET=" + currDeleteSet);
394 case FKind.FlatSetFieldNode:
395 case FKind.FlatSetElementNode: {
398 if (fn.kind() == FKind.FlatSetFieldNode) {
399 FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
401 fld = fsfn.getField();
403 fieldLoc = (Location) fld.getType().getExtension();
408 if (!isEventLoopBody && fieldLoc.getDescriptor().equals(md)) {
409 // if the field belongs to the local lattice, no reason to calculate
414 NTuple<Location> fieldLocTuple = new NTuple<Location>();
415 if (fld.isStatic()) {
417 // in this case, fld has TOP location
418 Location topLocation = Location.createTopLocation(md);
419 fieldLocTuple.add(topLocation);
421 fieldLocTuple.addAll(deriveGlobalLocationTuple(md));
422 if (fn.kind() == FKind.FlatSetFieldNode) {
423 fieldLocTuple.add((Location) fld.getType().getExtension());
428 fieldLocTuple.addAll(deriveLocationTuple(md, lhs));
429 if (fn.kind() == FKind.FlatSetFieldNode) {
430 fieldLocTuple.add((Location) fld.getType().getExtension());
434 // shared loc extension
435 Location srcLoc = getLocation(rhs);
436 if (ssjava.isSharedLocation(fieldLoc)) {
437 // only care the case that loc(f) is shared location
440 // NTuple<Location> fieldLocTuple = new NTuple<Location>();
441 // fieldLocTuple.addAll(mapDescriptorToLocationPath.get(lhs));
442 // fieldLocTuple.add(fieldLoc);
444 NTuple<Descriptor> fldHeapPath = new NTuple<Descriptor>();
445 fldHeapPath.addAll(computePath(lhs));
446 if (fn.kind() == FKind.FlatSetFieldNode) {
447 fldHeapPath.add(fld);
450 // computing gen/kill set
451 computeKILLSetForWrite(curr, killSet, fieldLocTuple, fldHeapPath);
453 if (!ssjava.isSameHeightWrite(fn)) {
454 computeGENSetForHigherWrite(curr, genSet, fieldLocTuple, fldHeapPath);
455 updateDeleteSetForHigherWrite(currDeleteSet, fieldLocTuple, fldHeapPath);
457 computeGENSetForSameHeightWrite(curr, genSet, fieldLocTuple, fldHeapPath);
458 updateDeleteSetForSameHeightWrite(currDeleteSet, fieldLocTuple, fldHeapPath);
461 // System.out.println("################");
462 // System.out.println("FIELD WRITE:" + fn);
463 // System.out.println("FldHeapPath=" + fldHeapPath);
464 // System.out.println("fieldLocTuple=" + fieldLocTuple + " srcLoc=" +
466 // System.out.println("KILLSET=" + killSet);
467 // System.out.println("GENSet=" + genSet);
468 // System.out.println("DELETESET=" + currDeleteSet);
474 case FKind.FlatCall: {
475 FlatCall fc = (FlatCall) fn;
477 bindHeapPathCallerArgWithCaleeParamForSharedLoc(fm.getMethod(), fc);
479 // computing gen/kill set
480 generateKILLSetForFlatCall(curr, killSet);
481 generateGENSetForFlatCall(curr, genSet);
483 // System.out.println("#FLATCALL=" + fc);
484 // System.out.println("KILLSET=" + killSet);
485 // System.out.println("GENSet=" + genSet);
486 // System.out.println("bound DELETE Set=" + calleeUnionBoundDeleteSet);
491 case FKind.FlatExit: {
492 // merge the current delete/shared loc mapping
493 mergeSharedLocMap(sharedLocMap, curr);
494 mergeDeleteSet(deleteSet, currDeleteSet);
496 // System.out.println("#FLATEXIT sharedLocMap=" + sharedLocMap);
502 computeNewMapping(curr, killSet, genSet);
503 if (!curr.map.isEmpty()) {
504 // System.out.println(fn + "#######" + curr);
509 private void generateGENSetForFlatCall(SharedLocMap curr, SharedLocMap genSet) {
511 Set<NTuple<Location>> locTupleSet = calleeIntersectBoundSharedSet.keySet();
512 for (Iterator iterator = locTupleSet.iterator(); iterator.hasNext();) {
513 NTuple<Location> locTupleKey = (NTuple<Location>) iterator.next();
514 genSet.addWrite(locTupleKey, curr.get(locTupleKey));
515 genSet.addWrite(locTupleKey, calleeIntersectBoundSharedSet.get(locTupleKey));
517 genSet.removeWriteAll(locTupleKey, calleeUnionBoundDeleteSet.get(locTupleKey));
522 private void generateKILLSetForFlatCall(SharedLocMap curr, SharedLocMap killSet) {
524 Set<NTuple<Location>> locTupleSet = calleeIntersectBoundSharedSet.keySet();
525 for (Iterator iterator = locTupleSet.iterator(); iterator.hasNext();) {
526 NTuple<Location> locTupleKey = (NTuple<Location>) iterator.next();
527 killSet.addWrite(locTupleKey, curr.get(locTupleKey));
532 private void mergeDeleteSet(SharedLocMap currDeleteSet, SharedLocMap inDeleteLoc) {
534 Set<NTuple<Location>> locTupleKeySet = inDeleteLoc.keySet();
536 for (Iterator iterator = locTupleKeySet.iterator(); iterator.hasNext();) {
537 NTuple<Location> locTupleKey = (NTuple<Location>) iterator.next();
539 Set<NTuple<Descriptor>> inSet = inDeleteLoc.get(locTupleKey);
540 currDeleteSet.addWrite(locTupleKey, inSet);
545 private void computeNewMapping(SharedLocMap curr, SharedLocMap killSet, SharedLocMap genSet) {
550 private void updateDeleteSetForHigherWrite(SharedLocMap currDeleteSet, NTuple<Location> locTuple,
551 NTuple<Descriptor> hp) {
552 currDeleteSet.removeWrite(locTuple, hp);
555 private void updateDeleteSetForSameHeightWrite(SharedLocMap currDeleteSet,
556 NTuple<Location> locTuple, NTuple<Descriptor> hp) {
557 currDeleteSet.addWrite(locTuple, hp);
560 private void computeGENSetForHigherWrite(SharedLocMap curr, SharedLocMap genSet,
561 NTuple<Location> locTuple, NTuple<Descriptor> hp) {
562 Set<NTuple<Descriptor>> currWriteSet = curr.get(locTuple);
564 if (currWriteSet != null) {
565 genSet.addWrite(locTuple, currWriteSet);
568 genSet.addWrite(locTuple, hp);
571 private void computeGENSetForSameHeightWrite(SharedLocMap curr, SharedLocMap genSet,
572 NTuple<Location> locTuple, NTuple<Descriptor> hp) {
573 Set<NTuple<Descriptor>> currWriteSet = curr.get(locTuple);
575 if (currWriteSet != null) {
576 genSet.addWrite(locTuple, currWriteSet);
578 genSet.removeWrite(locTuple, hp);
581 private void computeKILLSetForWrite(SharedLocMap curr, SharedLocMap killSet,
582 NTuple<Location> locTuple, NTuple<Descriptor> hp) {
584 Set<NTuple<Descriptor>> writeSet = curr.get(locTuple);
585 if (writeSet != null) {
586 killSet.addWrite(locTuple, writeSet);
591 private void mergeSharedLocMap(SharedLocMap currSharedSet, SharedLocMap in) {
593 Set<NTuple<Location>> locTupleKeySet = in.keySet();
594 for (Iterator iterator = locTupleKeySet.iterator(); iterator.hasNext();) {
595 NTuple<Location> locTupleKey = (NTuple<Location>) iterator.next();
597 Set<NTuple<Descriptor>> inSet = in.get(locTupleKey);
598 Set<NTuple<Descriptor>> currSet = currSharedSet.get(locTupleKey);
599 if (currSet == null) {
600 currSet = new HashSet<NTuple<Descriptor>>();
601 currSet.addAll(inSet);
602 currSharedSet.addWrite(locTupleKey, currSet);
604 currSet.retainAll(inSet);
609 private void computeSharedCoverSet() {
610 LinkedList<MethodDescriptor> descriptorListToAnalyze =
611 (LinkedList<MethodDescriptor>) sortedDescriptors.clone();
613 // current descriptors to visit in fixed-point interprocedural analysis,
615 // dependency in the call graph
616 methodDescriptorsToVisitStack.clear();
618 descriptorListToAnalyze.removeFirst();
620 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
621 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
623 while (!descriptorListToAnalyze.isEmpty()) {
624 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
625 methodDescriptorsToVisitStack.add(md);
628 // analyze scheduled methods until there are no more to visit
629 while (!methodDescriptorsToVisitStack.isEmpty()) {
630 MethodDescriptor md = methodDescriptorsToVisitStack.pop();
631 FlatMethod fm = state.getMethodFlat(md);
632 computeSharedCoverSet_analyzeMethod(fm, md.equals(methodContainingSSJavaLoop));
635 computeSharedCoverSetForEventLoop();
639 private void computeSharedCoverSetForEventLoop() {
640 computeSharedCoverSet_analyzeMethod(state.getMethodFlat(methodContainingSSJavaLoop), true);
643 private void computeSharedCoverSet_analyzeMethod(FlatMethod fm, boolean onlyVisitSSJavaLoop) {
645 System.out.println("\n###");
646 System.out.println("computeSharedCoverSet_analyzeMethod=" + fm);
647 MethodDescriptor md = fm.getMethod();
649 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
651 Set<FlatNode> visited = new HashSet<FlatNode>();
653 if (onlyVisitSSJavaLoop) {
654 flatNodesToVisit.add(ssjava.getSSJavaLoopEntrance());
656 flatNodesToVisit.add(fm);
659 while (!flatNodesToVisit.isEmpty()) {
660 FlatNode fn = flatNodesToVisit.iterator().next();
661 flatNodesToVisit.remove(fn);
664 computeSharedCoverSet_nodeActions(md, fn, onlyVisitSSJavaLoop);
666 for (int i = 0; i < fn.numNext(); i++) {
667 FlatNode nn = fn.getNext(i);
669 if (!visited.contains(nn)) {
670 if (!onlyVisitSSJavaLoop || (onlyVisitSSJavaLoop && loopIncElements.contains(nn))) {
671 flatNodesToVisit.add(nn);
681 private void computeSharedCoverSet_nodeActions(MethodDescriptor md, FlatNode fn,
682 boolean isEventLoopBody) {
689 case FKind.FlatLiteralNode: {
690 FlatLiteralNode fln = (FlatLiteralNode) fn;
693 NTuple<Location> lhsLocTuple = new NTuple<Location>();
694 lhsLocTuple.add(Location.createTopLocation(md));
695 mapDescriptorToLocationPath.put(lhs, lhsLocTuple);
697 if (lhs.getType().isPrimitive() && !lhs.getSymbol().startsWith("neverused")
698 && !lhs.getSymbol().startsWith("srctmp")) {
699 // only need to care about composite location case here
700 if (lhs.getType().getExtension() instanceof SSJavaType) {
701 CompositeLocation compLoc = ((SSJavaType) lhs.getType().getExtension()).getCompLoc();
702 Location lastLocElement = compLoc.get(compLoc.getSize() - 1);
709 case FKind.FlatOpNode: {
710 FlatOpNode fon = (FlatOpNode) fn;
711 // for a normal assign node, need to propagate lhs's location path to
713 if (fon.getOp().getOp() == Operation.ASSIGN) {
717 if (!lhs.getSymbol().startsWith("neverused") && !lhs.getSymbol().startsWith("leftop")
718 && !lhs.getSymbol().startsWith("rightop")) {
720 if (mapHeapPath.containsKey(rhs)) {
721 NTuple<Location> rhsLocTuple = new NTuple<Location>();
722 NTuple<Location> lhsLocTuple = new NTuple<Location>();
723 if (mapDescriptorToLocationPath.containsKey(rhs)) {
724 mapDescriptorToLocationPath.put(lhs, deriveLocationTuple(md, rhs));
725 lhsLocTuple = mapDescriptorToLocationPath.get(lhs);
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);
759 if (isEventLoopBody && lhs.getType().isPrimitive()
760 && !lhs.getSymbol().startsWith("srctmp")) {
762 NTuple<Descriptor> lhsHeapPath = computePath(lhs);
764 if (lhsLocTuple != null) {
765 addMayWrittenSet(md, lhsLocTuple, lhsHeapPath);
779 case FKind.FlatSetFieldNode:
780 case FKind.FlatSetElementNode: {
784 if (fn.kind() == FKind.FlatSetFieldNode) {
785 FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
787 fld = fsfn.getField();
790 FlatSetElementNode fsen = (FlatSetElementNode) fn;
793 TypeDescriptor td = lhs.getType().dereference();
794 fld = getArrayField(td);
797 NTuple<Location> lhsLocTuple = new NTuple<Location>();
798 lhsLocTuple.addAll(deriveLocationTuple(md, lhs));
799 mapDescriptorToLocationPath.put(lhs, lhsLocTuple);
801 NTuple<Location> fieldLocTuple = new NTuple<Location>();
802 fieldLocTuple.addAll(lhsLocTuple);
804 if (fn.kind() == FKind.FlatSetFieldNode) {
805 fieldLocTuple.add((Location) fld.getType().getExtension());
808 if (mapHeapPath.containsKey(lhs)) {
809 // fields reachable from the param can have heap path entry.
810 NTuple<Descriptor> lhsHeapPath = new NTuple<Descriptor>();
811 lhsHeapPath.addAll(mapHeapPath.get(lhs));
813 Location fieldLocation;
814 if (fn.kind() == FKind.FlatSetFieldNode) {
815 fieldLocation = getLocation(fld);
817 fieldLocation = getLocation(lhsHeapPath.get(getArrayBaseDescriptorIdx(lhsHeapPath)));
820 // Location fieldLocation = getLocation(lhs);
821 if (!isEventLoopBody && fieldLocation.getDescriptor().equals(md)) {
822 // if the field belongs to the local lattice, no reason to calculate
827 if (ssjava.isSharedLocation(fieldLocation)) {
829 NTuple<Descriptor> fieldHeapPath = new NTuple<Descriptor>();
830 fieldHeapPath.addAll(computePath(lhs));
831 if (fn.kind() == FKind.FlatSetFieldNode) {
832 fieldHeapPath.add(fld);
835 addMayWrittenSet(md, fieldLocTuple, fieldHeapPath);
843 case FKind.FlatElementNode:
844 case FKind.FlatFieldNode: {
848 if (fn.kind() == FKind.FlatFieldNode) {
849 FlatFieldNode ffn = (FlatFieldNode) fn;
852 fld = ffn.getField();
854 FlatElementNode fen = (FlatElementNode) fn;
857 TypeDescriptor td = rhs.getType().dereference();
858 fld = getArrayField(td);
861 NTuple<Location> locTuple = new NTuple<Location>();
863 if (fld.isStatic()) {
866 // in this case, fld has TOP location
867 Location topLocation = Location.createTopLocation(md);
868 locTuple.add(topLocation);
870 locTuple.addAll(deriveGlobalLocationTuple(md));
871 if (fn.kind() == FKind.FlatFieldNode) {
872 locTuple.add((Location) fld.getType().getExtension());
877 locTuple.addAll(deriveLocationTuple(md, rhs));
878 if (fn.kind() == FKind.FlatFieldNode) {
879 locTuple.add((Location) fld.getType().getExtension());
883 mapDescriptorToLocationPath.put(lhs, locTuple);
888 case FKind.FlatCall: {
890 FlatCall fc = (FlatCall) fn;
892 bindLocationPathCallerArgWithCalleeParam(md, fc);
897 case FKind.FlatNew: {
899 FlatNew fnew = (FlatNew) fn;
900 TempDescriptor dst = fnew.getDst();
901 NTuple<Location> locTuple = deriveLocationTuple(md, dst);
903 if (locTuple != null) {
904 NTuple<Location> dstLocTuple = new NTuple<Location>();
905 dstLocTuple.addAll(locTuple);
906 mapDescriptorToLocationPath.put(dst, dstLocTuple);
914 private void addMayWrittenSet(MethodDescriptor md, NTuple<Location> locTuple,
915 NTuple<Descriptor> heapPath) {
917 MultiSourceMap<NTuple<Location>, NTuple<Descriptor>> map = mapMethodToSharedLocCoverSet.get(md);
919 map = new MultiSourceMap<NTuple<Location>, NTuple<Descriptor>>();
920 mapMethodToSharedLocCoverSet.put(md, map);
923 Set<NTuple<Descriptor>> writeSet = map.get(locTuple);
924 if (writeSet == null) {
925 writeSet = new HashSet<NTuple<Descriptor>>();
926 map.put(locTuple, writeSet);
928 writeSet.add(heapPath);
932 private void bindLocationPathCallerArgWithCalleeParam(MethodDescriptor mdCaller, FlatCall fc) {
934 if (ssjava.isSSJavaUtil(fc.getMethod().getClassDesc())) {
936 // have write effects on the first argument
937 TempDescriptor arg = fc.getArg(0);
938 NTuple<Location> argLocationPath = deriveLocationTuple(mdCaller, arg);
939 NTuple<Descriptor> argHeapPath = computePath(arg);
940 addMayWrittenSet(mdCaller, argLocationPath, argHeapPath);
941 } else if (ssjava.needTobeAnnotated(fc.getMethod())) {
943 // if arg is not primitive type, we need to propagate maywritten set to
944 // the caller's location path
946 MethodDescriptor mdCallee = fc.getMethod();
947 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
948 setPossibleCallees.addAll(callGraph.getMethods(mdCallee));
950 // create mapping from arg idx to its heap paths
951 Hashtable<Integer, NTuple<Descriptor>> mapArgIdx2CallerArgHeapPath =
952 new Hashtable<Integer, NTuple<Descriptor>>();
954 // create mapping from arg idx to its location paths
955 Hashtable<Integer, NTuple<Location>> mapArgIdx2CallerArgLocationPath =
956 new Hashtable<Integer, NTuple<Location>>();
958 if (fc.getThis() != null) {
960 if (mapHeapPath.containsKey(fc.getThis())) {
962 // setup heap path for 'this'
963 NTuple<Descriptor> thisHeapPath = new NTuple<Descriptor>();
964 thisHeapPath.addAll(mapHeapPath.get(fc.getThis()));
965 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(0), thisHeapPath);
967 // setup location path for 'this'
968 NTuple<Location> thisLocationPath = deriveLocationTuple(mdCaller, fc.getThis());
969 mapArgIdx2CallerArgLocationPath.put(Integer.valueOf(0), thisLocationPath);
974 for (int i = 0; i < fc.numArgs(); i++) {
975 TempDescriptor arg = fc.getArg(i);
976 // create mapping arg to loc path
978 if (mapHeapPath.containsKey(arg)) {
980 NTuple<Descriptor> argHeapPath = mapHeapPath.get(arg);
981 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(i + 1), argHeapPath);
983 NTuple<Location> argLocationPath = deriveLocationTuple(mdCaller, arg);
984 mapArgIdx2CallerArgLocationPath.put(Integer.valueOf(i + 1), argLocationPath);
989 for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
990 MethodDescriptor callee = (MethodDescriptor) iterator.next();
991 FlatMethod calleeFlatMethod = state.getMethodFlat(callee);
993 // binding caller's args and callee's params
995 Hashtable<NTuple<Descriptor>, NTuple<Descriptor>> mapParamHeapPathToCallerArgHeapPath =
996 new Hashtable<NTuple<Descriptor>, NTuple<Descriptor>>();
998 Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc =
999 new Hashtable<Integer, TempDescriptor>();
1001 if (calleeFlatMethod.getMethod().isStatic()) {
1002 // static method does not have implicit 'this' arg
1006 for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
1007 TempDescriptor param = calleeFlatMethod.getParameter(i);
1008 mapParamIdx2ParamTempDesc.put(Integer.valueOf(i + offset), param);
1010 NTuple<Descriptor> calleeHeapPath = computePath(param);
1012 NTuple<Descriptor> argHeapPath =
1013 mapArgIdx2CallerArgHeapPath.get(Integer.valueOf(i + offset));
1015 if (argHeapPath != null) {
1016 mapParamHeapPathToCallerArgHeapPath.put(calleeHeapPath, argHeapPath);
1022 Set<Integer> keySet = mapArgIdx2CallerArgLocationPath.keySet();
1023 for (Iterator iterator2 = keySet.iterator(); iterator2.hasNext();) {
1024 Integer idx = (Integer) iterator2.next();
1026 NTuple<Location> callerArgLocationPath = mapArgIdx2CallerArgLocationPath.get(idx);
1028 TempDescriptor calleeParam = mapParamIdx2ParamTempDesc.get(idx);
1029 NTuple<Location> calleeLocationPath = deriveLocationTuple(mdCallee, calleeParam);
1031 NTuple<Descriptor> callerArgHeapPath = mapArgIdx2CallerArgHeapPath.get(idx);
1032 NTuple<Descriptor> calleeHeapPath = computePath(calleeParam);
1034 if (!calleeParam.getType().isPrimitive()) {
1035 createNewMappingOfMayWrittenSet(mdCaller, callee, callerArgHeapPath,
1036 callerArgLocationPath, calleeHeapPath, calleeLocationPath,
1037 mapParamHeapPathToCallerArgHeapPath);
1047 private Hashtable<NTuple<Location>, Set<NTuple<Descriptor>>> getMappingByStartedWith(
1048 MultiSourceMap<NTuple<Location>, NTuple<Descriptor>> map, NTuple<Location> in) {
1050 Hashtable<NTuple<Location>, Set<NTuple<Descriptor>>> matchedMapping =
1051 new Hashtable<NTuple<Location>, Set<NTuple<Descriptor>>>();
1053 Set<NTuple<Location>> keySet = map.keySet();
1055 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1056 NTuple<Location> key = (NTuple<Location>) iterator.next();
1057 if (key.startsWith(in)) {
1058 matchedMapping.put(key, map.get(key));
1062 return matchedMapping;
1066 private void createNewMappingOfMayWrittenSet(MethodDescriptor caller, MethodDescriptor callee,
1067 NTuple<Descriptor> callerArgHeapPath, NTuple<Location> callerArgLocPath,
1068 NTuple<Descriptor> calleeParamHeapPath, NTuple<Location> calleeParamLocPath,
1069 Hashtable<NTuple<Descriptor>, NTuple<Descriptor>> mapParamHeapPathToCallerArgHeapPath) {
1071 // propagate may-written-set associated with the key that is started with
1072 // calleepath to the caller
1073 // 1) makes a new key by combining caller path and callee path(except local
1074 // loc element of param)
1075 // 2) create new mapping of may-written-set of callee path to caller path
1077 // extract all may written effect accessed through callee param path
1078 MultiSourceMap<NTuple<Location>, NTuple<Descriptor>> calleeMapping =
1079 mapMethodToSharedLocCoverSet.get(callee);
1081 if (calleeMapping == null) {
1085 MultiSourceMap<NTuple<Location>, NTuple<Descriptor>> callerMapping =
1086 mapMethodToSharedLocCoverSet.get(caller);
1088 if (callerMapping == null) {
1089 callerMapping = new MultiSourceMap<NTuple<Location>, NTuple<Descriptor>>();
1090 mapMethodToSharedLocCoverSet.put(caller, callerMapping);
1093 Hashtable<NTuple<Location>, Set<NTuple<Descriptor>>> paramMapping =
1094 getMappingByStartedWith(calleeMapping, calleeParamLocPath);
1096 Set<NTuple<Location>> calleeKeySet = paramMapping.keySet();
1098 for (Iterator iterator = calleeKeySet.iterator(); iterator.hasNext();) {
1099 NTuple<Location> calleeKey = (NTuple<Location>) iterator.next();
1101 Set<NTuple<Descriptor>> calleeMayWriteSet = paramMapping.get(calleeKey);
1103 if (calleeMayWriteSet != null) {
1105 Set<NTuple<Descriptor>> boundMayWriteSet = new HashSet<NTuple<Descriptor>>();
1107 Set<NTuple<Descriptor>> boundSet =
1108 convertToCallerMayWriteSet(calleeParamHeapPath, calleeMayWriteSet, callerMapping,
1109 mapParamHeapPathToCallerArgHeapPath);
1111 boundMayWriteSet.addAll(boundSet);
1113 NTuple<Location> newKey = new NTuple<Location>();
1114 newKey.addAll(callerArgLocPath);
1115 // need to replace the local location with the caller's path so skip the
1116 // local location of the parameter
1117 for (int i = 1; i < calleeKey.size(); i++) {
1118 newKey.add(calleeKey.get(i));
1121 callerMapping.union(newKey, boundMayWriteSet);
1128 private Set<NTuple<Descriptor>> convertToCallerMayWriteSet(
1129 NTuple<Descriptor> calleeParamHeapPath, Set<NTuple<Descriptor>> calleeMayWriteSet,
1130 MultiSourceMap<NTuple<Location>, NTuple<Descriptor>> callerMapping,
1131 Hashtable<NTuple<Descriptor>, NTuple<Descriptor>> mapParamHeapPathToCallerArgHeapPath) {
1133 Set<NTuple<Descriptor>> boundSet = new HashSet<NTuple<Descriptor>>();
1135 // replace callee's param path with caller's arg path
1136 for (Iterator iterator = calleeMayWriteSet.iterator(); iterator.hasNext();) {
1137 NTuple<Descriptor> calleeWriteHeapPath = (NTuple<Descriptor>) iterator.next();
1139 NTuple<Descriptor> writeHeapPathParamHeapPath = calleeWriteHeapPath.subList(0, 1);
1141 NTuple<Descriptor> callerArgHeapPath =
1142 mapParamHeapPathToCallerArgHeapPath.get(writeHeapPathParamHeapPath);
1144 NTuple<Descriptor> boundHeapPath = new NTuple<Descriptor>();
1145 boundHeapPath.addAll(callerArgHeapPath);
1147 for (int i = 1; i < calleeWriteHeapPath.size(); i++) {
1148 boundHeapPath.add(calleeWriteHeapPath.get(i));
1151 boundSet.add(boundHeapPath);
1158 private Location getLocation(Descriptor d) {
1160 if (d instanceof FieldDescriptor) {
1161 TypeExtension te = ((FieldDescriptor) d).getType().getExtension();
1163 return (Location) te;
1166 assert d instanceof TempDescriptor;
1167 TempDescriptor td = (TempDescriptor) d;
1169 TypeExtension te = td.getType().getExtension();
1171 if (te instanceof SSJavaType) {
1172 SSJavaType ssType = (SSJavaType) te;
1173 if (ssType.getCompLoc() != null) {
1174 CompositeLocation comp = ssType.getCompLoc();
1175 return comp.get(comp.getSize() - 1);
1180 return (Location) te;
1185 return mapDescToLocation.get(d);
1188 private void eventLoopAnalysis() {
1189 // perform second stage analysis: intraprocedural analysis ensure that
1191 // variables are definitely written in-between the same read
1193 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
1194 flatNodesToVisit.add(ssjava.getSSJavaLoopEntrance());
1196 while (!flatNodesToVisit.isEmpty()) {
1197 FlatNode fn = (FlatNode) flatNodesToVisit.iterator().next();
1198 flatNodesToVisit.remove(fn);
1200 Hashtable<NTuple<Descriptor>, Set<WriteAge>> prev = mapFlatNodetoEventLoopMap.get(fn);
1202 Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr =
1203 new Hashtable<NTuple<Descriptor>, Set<WriteAge>>();
1204 for (int i = 0; i < fn.numPrev(); i++) {
1205 FlatNode nn = fn.getPrev(i);
1206 Hashtable<NTuple<Descriptor>, Set<WriteAge>> in = mapFlatNodetoEventLoopMap.get(nn);
1212 eventLoopAnalysis_nodeAction(fn, curr, ssjava.getSSJavaLoopEntrance());
1214 // if a new result, schedule forward nodes for analysis
1215 if (!curr.equals(prev)) {
1216 mapFlatNodetoEventLoopMap.put(fn, curr);
1218 for (int i = 0; i < fn.numNext(); i++) {
1219 FlatNode nn = fn.getNext(i);
1220 if (loopIncElements.contains(nn)) {
1221 flatNodesToVisit.add(nn);
1229 private void union(Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr,
1230 Hashtable<NTuple<Descriptor>, Set<WriteAge>> in) {
1232 Set<NTuple<Descriptor>> inKeySet = in.keySet();
1233 for (Iterator iterator = inKeySet.iterator(); iterator.hasNext();) {
1234 NTuple<Descriptor> inKey = (NTuple<Descriptor>) iterator.next();
1235 Set<WriteAge> inSet = in.get(inKey);
1237 Set<WriteAge> currSet = curr.get(inKey);
1239 if (currSet == null) {
1240 currSet = new HashSet<WriteAge>();
1241 curr.put(inKey, currSet);
1243 currSet.addAll(inSet);
1248 private void eventLoopAnalysis_nodeAction(FlatNode fn,
1249 Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr, FlatNode loopEntrance) {
1251 Hashtable<NTuple<Descriptor>, Set<WriteAge>> readWriteKillSet =
1252 new Hashtable<NTuple<Descriptor>, Set<WriteAge>>();
1253 Hashtable<NTuple<Descriptor>, Set<WriteAge>> readWriteGenSet =
1254 new Hashtable<NTuple<Descriptor>, Set<WriteAge>>();
1256 if (fn.equals(loopEntrance)) {
1257 // it reaches loop entrance: changes all flag to true
1258 Set<NTuple<Descriptor>> keySet = curr.keySet();
1259 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1260 NTuple<Descriptor> key = (NTuple<Descriptor>) iterator.next();
1261 Set<WriteAge> writeAgeSet = curr.get(key);
1263 Set<WriteAge> incSet = new HashSet<WriteAge>();
1264 incSet.addAll(writeAgeSet);
1265 writeAgeSet.clear();
1267 for (Iterator iterator2 = incSet.iterator(); iterator2.hasNext();) {
1268 WriteAge writeAge = (WriteAge) iterator2.next();
1269 WriteAge newWriteAge = writeAge.copy();
1271 writeAgeSet.add(newWriteAge);
1279 FieldDescriptor fld;
1281 switch (fn.kind()) {
1283 case FKind.FlatOpNode: {
1284 FlatOpNode fon = (FlatOpNode) fn;
1285 lhs = fon.getDest();
1286 rhs = fon.getLeft();
1288 if (fon.getOp().getOp() == Operation.ASSIGN) {
1290 if (!lhs.getSymbol().startsWith("neverused") && !lhs.getSymbol().startsWith("leftop")
1291 && !lhs.getSymbol().startsWith("rightop")) {
1293 boolean hasWriteEffect = false;
1295 if (rhs.getType().getExtension() instanceof SSJavaType
1296 && lhs.getType().getExtension() instanceof SSJavaType) {
1298 CompositeLocation rhsCompLoc =
1299 ((SSJavaType) rhs.getType().getExtension()).getCompLoc();
1301 CompositeLocation lhsCompLoc =
1302 ((SSJavaType) lhs.getType().getExtension()).getCompLoc();
1304 if (lhsCompLoc != rhsCompLoc) {
1305 // have a write effect!
1306 hasWriteEffect = true;
1309 } else if (lhs.getType().isImmutable()) {
1310 hasWriteEffect = true;
1313 if (hasWriteEffect && mapHeapPath.containsKey(lhs)) {
1315 NTuple<Descriptor> lhsHeapPath = new NTuple<Descriptor>();
1316 lhsHeapPath.addAll(mapHeapPath.get(lhs));
1318 Location lhsLoc = getLocation(lhs);
1319 if (ssjava.isSharedLocation(lhsLoc)) {
1321 NTuple<Descriptor> varHeapPath = computePath(lhs);
1322 NTuple<Location> varLocTuple = mapDescriptorToLocationPath.get(lhs);
1324 Set<NTuple<Descriptor>> writtenSet =
1325 mapFlatNodeToSharedLocMapping.get(fn).get(varLocTuple);
1327 if (isCovered(varLocTuple, writtenSet)) {
1328 computeKILLSetForSharedWrite(curr, writtenSet, readWriteKillSet);
1329 computeGENSetForSharedAllCoverWrite(curr, writtenSet, readWriteGenSet);
1331 computeGENSetForSharedNonCoverWrite(curr, varHeapPath, readWriteGenSet);
1336 computeKILLSetForWrite(curr, lhsHeapPath, readWriteKillSet);
1337 computeGENSetForWrite(lhsHeapPath, readWriteGenSet);
1340 // System.out.println("write effect on =" + lhsHeapPath);
1341 // System.out.println("#KILLSET=" + readWriteKillSet);
1342 // System.out.println("#GENSet=" + readWriteGenSet + "\n");
1344 Set<WriteAge> writeAgeSet = curr.get(lhsHeapPath);
1345 checkWriteAgeSet(writeAgeSet, lhsHeapPath, fn);
1355 case FKind.FlatFieldNode:
1356 case FKind.FlatElementNode: {
1358 if (fn.kind() == FKind.FlatFieldNode) {
1359 FlatFieldNode ffn = (FlatFieldNode) fn;
1362 fld = ffn.getField();
1364 FlatElementNode fen = (FlatElementNode) fn;
1367 TypeDescriptor td = rhs.getType().dereference();
1368 fld = getArrayField(td);
1372 NTuple<Descriptor> srcHeapPath = mapHeapPath.get(rhs);
1373 NTuple<Descriptor> fldHeapPath;
1374 if (srcHeapPath != null) {
1375 fldHeapPath = new NTuple<Descriptor>(srcHeapPath.getList());
1377 // if srcHeapPath is null, it is static reference
1378 fldHeapPath = new NTuple<Descriptor>();
1379 fldHeapPath.add(rhs);
1381 fldHeapPath.add(fld);
1383 Set<WriteAge> writeAgeSet = curr.get(fldHeapPath);
1385 checkWriteAgeSet(writeAgeSet, fldHeapPath, fn);
1390 case FKind.FlatSetFieldNode:
1391 case FKind.FlatSetElementNode: {
1393 if (fn.kind() == FKind.FlatSetFieldNode) {
1394 FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
1395 lhs = fsfn.getDst();
1396 fld = fsfn.getField();
1398 FlatSetElementNode fsen = (FlatSetElementNode) fn;
1399 lhs = fsen.getDst();
1400 rhs = fsen.getSrc();
1401 TypeDescriptor td = lhs.getType().dereference();
1402 fld = getArrayField(td);
1406 NTuple<Descriptor> lhsHeapPath = mapHeapPath.get(lhs);
1407 if (lhsHeapPath != null) {
1409 NTuple<Descriptor> fldHeapPath = new NTuple<Descriptor>(lhsHeapPath.getList());
1410 if (fn.kind() == FKind.FlatSetFieldNode) {
1411 fldHeapPath.add(fld);
1414 // shared loc extension
1416 if (fn.kind() == FKind.FlatSetFieldNode) {
1417 fieldLoc = (Location) fld.getType().getExtension();
1419 NTuple<Location> locTuple = mapDescriptorToLocationPath.get(lhs);
1420 fieldLoc = locTuple.get(locTuple.size() - 1);
1423 if (ssjava.isSharedLocation(fieldLoc)) {
1425 NTuple<Location> fieldLocTuple = new NTuple<Location>();
1426 fieldLocTuple.addAll(mapDescriptorToLocationPath.get(lhs));
1427 if (fn.kind() == FKind.FlatSetFieldNode) {
1428 fieldLocTuple.add(fieldLoc);
1431 Set<NTuple<Descriptor>> writtenSet =
1432 mapFlatNodeToSharedLocMapping.get(fn).get(fieldLocTuple);
1434 if (isCovered(fieldLocTuple, writtenSet)) {
1435 computeKILLSetForSharedWrite(curr, writtenSet, readWriteKillSet);
1436 computeGENSetForSharedAllCoverWrite(curr, writtenSet, readWriteGenSet);
1438 computeGENSetForSharedNonCoverWrite(curr, fldHeapPath, readWriteGenSet);
1442 computeKILLSetForWrite(curr, fldHeapPath, readWriteKillSet);
1443 computeGENSetForWrite(fldHeapPath, readWriteGenSet);
1446 // System.out.println("KILLSET=" + readWriteKillSet);
1447 // System.out.println("GENSet=" + readWriteGenSet);
1454 case FKind.FlatCall: {
1455 FlatCall fc = (FlatCall) fn;
1457 SharedLocMap sharedLocMap = mapFlatNodeToSharedLocMapping.get(fc);
1458 // System.out.println("FLATCALL:" + fn);
1459 generateKILLSetForFlatCall(fc, curr, sharedLocMap, readWriteKillSet);
1460 generateGENSetForFlatCall(fc, sharedLocMap, readWriteGenSet);
1462 // System.out.println("KILLSET=" + readWriteKillSet);
1463 // System.out.println("GENSet=" + readWriteGenSet);
1470 computeNewMapping(curr, readWriteKillSet, readWriteGenSet);
1471 if (fn instanceof FlatCall) {
1472 checkManyRead((FlatCall) fn, curr);
1475 // System.out.println("#######" + curr);
1481 private void computeGENSetForSharedNonCoverWrite(
1482 Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr, NTuple<Descriptor> heapPath,
1483 Hashtable<NTuple<Descriptor>, Set<WriteAge>> genSet) {
1485 Set<WriteAge> writeAgeSet = genSet.get(heapPath);
1486 if (writeAgeSet == null) {
1487 writeAgeSet = new HashSet<WriteAge>();
1488 genSet.put(heapPath, writeAgeSet);
1491 writeAgeSet.add(new WriteAge(1));
1495 private void computeGENSetForSharedAllCoverWrite(
1496 Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr, Set<NTuple<Descriptor>> writtenSet,
1497 Hashtable<NTuple<Descriptor>, Set<WriteAge>> genSet) {
1499 for (Iterator iterator = writtenSet.iterator(); iterator.hasNext();) {
1500 NTuple<Descriptor> writeHeapPath = (NTuple<Descriptor>) iterator.next();
1502 Set<WriteAge> writeAgeSet = new HashSet<WriteAge>();
1503 writeAgeSet.add(new WriteAge(0));
1505 genSet.put(writeHeapPath, writeAgeSet);
1510 private void computeKILLSetForSharedWrite(Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr,
1511 Set<NTuple<Descriptor>> writtenSet, Hashtable<NTuple<Descriptor>, Set<WriteAge>> killSet) {
1513 for (Iterator iterator = writtenSet.iterator(); iterator.hasNext();) {
1514 NTuple<Descriptor> writeHeapPath = (NTuple<Descriptor>) iterator.next();
1515 Set<WriteAge> writeSet = curr.get(writeHeapPath);
1516 if (writeSet != null) {
1517 killSet.put(writeHeapPath, writeSet);
1523 private boolean isCovered(NTuple<Location> locTuple, Set<NTuple<Descriptor>> inSet) {
1525 if (inSet == null) {
1529 Set<NTuple<Descriptor>> coverSet =
1530 mapMethodToSharedLocCoverSet.get(methodContainingSSJavaLoop).get(locTuple);
1532 // System.out.println("locTuple=" + locTuple + " coverSet=" + coverSet +
1533 // " currSet=" + inSet);
1535 return inSet.containsAll(coverSet);
1538 private void checkManyRead(FlatCall fc, Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr) {
1540 Set<NTuple<Descriptor>> boundReadSet = mapFlatNodeToBoundReadSet.get(fc);
1542 for (Iterator iterator = boundReadSet.iterator(); iterator.hasNext();) {
1543 NTuple<Descriptor> readHeapPath = (NTuple<Descriptor>) iterator.next();
1544 Set<WriteAge> writeAgeSet = curr.get(readHeapPath);
1545 checkWriteAgeSet(writeAgeSet, readHeapPath, fc);
1550 private void checkWriteAgeSet(Set<WriteAge> writeAgeSet, NTuple<Descriptor> path, FlatNode fn) {
1552 // System.out.println("# CHECK WRITE AGE of " + path + " from set=" +
1555 if (writeAgeSet != null) {
1556 for (Iterator iterator = writeAgeSet.iterator(); iterator.hasNext();) {
1557 WriteAge writeAge = (WriteAge) iterator.next();
1558 if (writeAge.getAge() > MAXAGE) {
1559 generateErrorMessage(path, fn);
1565 private void generateErrorMessage(NTuple<Descriptor> path, FlatNode fn) {
1567 Descriptor lastDesc = path.get(getArrayBaseDescriptorIdx(path));
1568 if (ssjava.isSharedLocation(getLocation(lastDesc))) {
1570 NTuple<Location> locPathTuple = getLocationTuple(path);
1571 Set<NTuple<Descriptor>> coverSet =
1572 mapMethodToSharedLocCoverSet.get(methodContainingSSJavaLoop).get(locPathTuple);
1573 throw new Error("Shared memory locations, which is reachable through references " + path
1574 + ", are not completely overwritten by the higher values at "
1575 + methodContainingSSJavaLoop.getClassDesc().getSourceFileName() + "::" + fn.getNumLine()
1576 + ".\nThe following memory locations belong to the same shared locations:" + coverSet);
1580 "Memory location, which is reachable through references "
1582 + ", who comes back to the same read statement without being overwritten at the out-most iteration at "
1583 + methodContainingSSJavaLoop.getClassDesc().getSourceFileName() + "::"
1589 private void generateGENSetForFlatCall(FlatCall fc, SharedLocMap sharedLocMap,
1590 Hashtable<NTuple<Descriptor>, Set<WriteAge>> GENSet) {
1592 Set<NTuple<Descriptor>> boundMayWriteSet = mapFlatNodeToBoundMayWriteSet.get(fc);
1593 // System.out.println("boundMayWriteSet=" + boundMayWriteSet);
1595 for (Iterator iterator = boundMayWriteSet.iterator(); iterator.hasNext();) {
1596 NTuple<Descriptor> heapPath = (NTuple<Descriptor>) iterator.next();
1598 if (!isSharedLocation(heapPath)) {
1599 addWriteAgeToSet(heapPath, GENSet, new WriteAge(0));
1601 // if the current heap path is shared location
1603 NTuple<Location> locTuple = getLocationTuple(heapPath);
1605 Set<NTuple<Descriptor>> sharedWriteHeapPathSet = sharedLocMap.get(locTuple);
1607 if (isCovered(locTuple, sharedLocMap.get(locTuple))) {
1608 // if it is covered, add all of heap paths belong to the same shared
1609 // loc with write age 0
1611 for (Iterator iterator2 = sharedWriteHeapPathSet.iterator(); iterator2.hasNext();) {
1612 NTuple<Descriptor> sharedHeapPath = (NTuple<Descriptor>) iterator2.next();
1613 addWriteAgeToSet(sharedHeapPath, GENSet, new WriteAge(0));
1617 // if not covered, add write age 1 to the heap path that is
1618 // may-written but not covered
1619 addWriteAgeToSet(heapPath, GENSet, new WriteAge(1));
1628 private void addWriteAgeToSet(NTuple<Descriptor> heapPath,
1629 Hashtable<NTuple<Descriptor>, Set<WriteAge>> map, WriteAge age) {
1631 Set<WriteAge> currSet = map.get(heapPath);
1632 if (currSet == null) {
1633 currSet = new HashSet<WriteAge>();
1634 map.put(heapPath, currSet);
1640 private void generateKILLSetForFlatCall(FlatCall fc,
1641 Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr, SharedLocMap sharedLocMap,
1642 Hashtable<NTuple<Descriptor>, Set<WriteAge>> KILLSet) {
1644 Set<NTuple<Descriptor>> boundMustWriteSet = mapFlatNodeToBoundMustWriteSet.get(fc);
1645 System.out.println("#boundMustWriteSet=" + boundMustWriteSet);
1647 for (Iterator iterator = boundMustWriteSet.iterator(); iterator.hasNext();) {
1648 NTuple<Descriptor> heapPath = (NTuple<Descriptor>) iterator.next();
1650 if (isSharedLocation(heapPath)) {
1651 NTuple<Location> locTuple = getLocationTuple(heapPath);
1653 if (isCovered(locTuple, sharedLocMap.get(locTuple)) && curr.containsKey(heapPath)) {
1654 // if it is shared loc and corresponding shared loc has been covered
1655 KILLSet.put(heapPath, curr.get(heapPath));
1659 for (Enumeration<NTuple<Descriptor>> e = curr.keys(); e.hasMoreElements();) {
1660 NTuple<Descriptor> key = e.nextElement();
1661 if (key.startsWith(heapPath)) {
1662 KILLSet.put(key, curr.get(key));
1672 private int getArrayBaseDescriptorIdx(NTuple<Descriptor> heapPath) {
1674 for (int i = heapPath.size() - 1; i >= 0; i--) {
1675 if (!heapPath.get(i).getSymbol().equals(arrayElementFieldName)) {
1684 private boolean isSharedLocation(NTuple<Descriptor> heapPath) {
1686 Descriptor d = heapPath.get(getArrayBaseDescriptorIdx(heapPath));
1688 return ssjava.isSharedLocation(getLocation(heapPath.get(getArrayBaseDescriptorIdx(heapPath))));
1692 private NTuple<Location> getLocationTuple(NTuple<Descriptor> heapPath) {
1694 NTuple<Location> locTuple = new NTuple<Location>();
1696 locTuple.addAll(mapDescriptorToLocationPath.get(heapPath.get(0)));
1698 for (int i = 1; i <= getArrayBaseDescriptorIdx(heapPath); i++) {
1699 locTuple.add(getLocation(heapPath.get(i)));
1705 private void computeNewMapping(Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr,
1706 Hashtable<NTuple<Descriptor>, Set<WriteAge>> KILLSet,
1707 Hashtable<NTuple<Descriptor>, Set<WriteAge>> GENSet) {
1709 for (Enumeration<NTuple<Descriptor>> e = KILLSet.keys(); e.hasMoreElements();) {
1710 NTuple<Descriptor> key = e.nextElement();
1712 Set<WriteAge> writeAgeSet = curr.get(key);
1713 if (writeAgeSet == null) {
1714 writeAgeSet = new HashSet<WriteAge>();
1715 curr.put(key, writeAgeSet);
1717 writeAgeSet.removeAll(KILLSet.get(key));
1720 for (Enumeration<NTuple<Descriptor>> e = GENSet.keys(); e.hasMoreElements();) {
1721 NTuple<Descriptor> key = e.nextElement();
1723 Set<WriteAge> currWriteAgeSet = curr.get(key);
1724 if (currWriteAgeSet == null) {
1725 currWriteAgeSet = new HashSet<WriteAge>();
1726 curr.put(key, currWriteAgeSet);
1728 currWriteAgeSet.addAll(GENSet.get(key));
1733 private void computeGENSetForWrite(NTuple<Descriptor> fldHeapPath,
1734 Hashtable<NTuple<Descriptor>, Set<WriteAge>> GENSet) {
1736 // generate write age 0 for the field being written to
1737 Set<WriteAge> writeAgeSet = new HashSet<WriteAge>();
1738 writeAgeSet.add(new WriteAge(0));
1739 GENSet.put(fldHeapPath, writeAgeSet);
1743 private void computeKILLSetForWrite(Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr,
1744 NTuple<Descriptor> hp, Hashtable<NTuple<Descriptor>, Set<WriteAge>> KILLSet) {
1746 // removes all of heap path that starts with prefix 'hp'
1747 // since any reference overwrite along heap path gives overwriting side
1748 // effects on the value
1750 Set<NTuple<Descriptor>> keySet = curr.keySet();
1751 for (Iterator<NTuple<Descriptor>> iter = keySet.iterator(); iter.hasNext();) {
1752 NTuple<Descriptor> key = iter.next();
1753 if (key.startsWith(hp)) {
1754 KILLSet.put(key, curr.get(key));
1760 private void bindHeapPathCallerArgWithCalleeParam(FlatCall fc) {
1761 // compute all possible callee set
1762 // transform all READ/WRITE set from the any possible
1763 // callees to the caller
1764 calleeUnionBoundReadSet.clear();
1765 calleeIntersectBoundMustWriteSet.clear();
1766 calleeUnionBoundMayWriteSet.clear();
1768 if (ssjava.isSSJavaUtil(fc.getMethod().getClassDesc())) {
1769 // ssjava util case!
1770 // have write effects on the first argument
1771 TempDescriptor arg = fc.getArg(0);
1772 NTuple<Descriptor> argHeapPath = computePath(arg);
1773 calleeIntersectBoundMustWriteSet.add(argHeapPath);
1774 calleeUnionBoundMayWriteSet.add(argHeapPath);
1776 MethodDescriptor mdCallee = fc.getMethod();
1777 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
1778 setPossibleCallees.addAll(callGraph.getMethods(mdCallee));
1780 // create mapping from arg idx to its heap paths
1781 Hashtable<Integer, NTuple<Descriptor>> mapArgIdx2CallerArgHeapPath =
1782 new Hashtable<Integer, NTuple<Descriptor>>();
1784 // arg idx is starting from 'this' arg
1785 if (fc.getThis() != null) {
1786 NTuple<Descriptor> thisHeapPath = mapHeapPath.get(fc.getThis());
1787 if (thisHeapPath != null) {
1788 // if 'this' does not have heap path, it is local reference
1789 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(0), thisHeapPath);
1793 for (int i = 0; i < fc.numArgs(); i++) {
1794 TempDescriptor arg = fc.getArg(i);
1795 NTuple<Descriptor> argHeapPath = computePath(arg);
1796 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(i + 1), argHeapPath);
1799 for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
1800 MethodDescriptor callee = (MethodDescriptor) iterator.next();
1801 FlatMethod calleeFlatMethod = state.getMethodFlat(callee);
1803 // binding caller's args and callee's params
1805 Set<NTuple<Descriptor>> calleeReadSet = mapFlatMethodToReadSet.get(calleeFlatMethod);
1806 if (calleeReadSet == null) {
1807 calleeReadSet = new HashSet<NTuple<Descriptor>>();
1808 mapFlatMethodToReadSet.put(calleeFlatMethod, calleeReadSet);
1811 Set<NTuple<Descriptor>> calleeMustWriteSet =
1812 mapFlatMethodToMustWriteSet.get(calleeFlatMethod);
1814 if (calleeMustWriteSet == null) {
1815 calleeMustWriteSet = new HashSet<NTuple<Descriptor>>();
1816 mapFlatMethodToMustWriteSet.put(calleeFlatMethod, calleeMustWriteSet);
1819 Set<NTuple<Descriptor>> calleeMayWriteSet =
1820 mapFlatMethodToMayWriteSet.get(calleeFlatMethod);
1822 if (calleeMayWriteSet == null) {
1823 calleeMayWriteSet = new HashSet<NTuple<Descriptor>>();
1824 mapFlatMethodToMayWriteSet.put(calleeFlatMethod, calleeMayWriteSet);
1827 Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc =
1828 new Hashtable<Integer, TempDescriptor>();
1830 if (calleeFlatMethod.getMethod().isStatic()) {
1831 // static method does not have implicit 'this' arg
1834 for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
1835 TempDescriptor param = calleeFlatMethod.getParameter(i);
1836 mapParamIdx2ParamTempDesc.put(Integer.valueOf(i + offset), param);
1839 Set<NTuple<Descriptor>> calleeBoundReadSet =
1840 bindSet(calleeReadSet, mapParamIdx2ParamTempDesc, mapArgIdx2CallerArgHeapPath);
1841 // union of the current read set and the current callee's
1843 calleeUnionBoundReadSet.addAll(calleeBoundReadSet);
1845 Set<NTuple<Descriptor>> calleeBoundMustWriteSet =
1846 bindSet(calleeMustWriteSet, mapParamIdx2ParamTempDesc, mapArgIdx2CallerArgHeapPath);
1847 // intersection of the current overwrite set and the current
1850 merge(calleeIntersectBoundMustWriteSet, calleeBoundMustWriteSet);
1852 Set<NTuple<Descriptor>> boundWriteSetFromCallee =
1853 bindSet(calleeMayWriteSet, mapParamIdx2ParamTempDesc, mapArgIdx2CallerArgHeapPath);
1854 calleeUnionBoundMayWriteSet.addAll(boundWriteSetFromCallee);
1861 private void bindHeapPathCallerArgWithCaleeParamForSharedLoc(MethodDescriptor mdCaller,
1864 calleeIntersectBoundSharedSet.clear();
1865 calleeUnionBoundDeleteSet.clear();
1867 if (ssjava.isSSJavaUtil(fc.getMethod().getClassDesc())) {
1868 // ssjava util case!
1869 // have write effects on the first argument
1870 TempDescriptor arg = fc.getArg(0);
1871 NTuple<Descriptor> argHeapPath = computePath(arg);
1873 // convert heap path to location path
1874 NTuple<Location> argLocTuple = new NTuple<Location>();
1875 argLocTuple.addAll(deriveLocationTuple(mdCaller, (TempDescriptor) argHeapPath.get(0)));
1876 for (int i = 1; i < argHeapPath.size(); i++) {
1877 argLocTuple.add(getLocation(argHeapPath.get(i)));
1880 calleeIntersectBoundSharedSet.addWrite(argLocTuple, argHeapPath);
1882 } else if (ssjava.needTobeAnnotated(fc.getMethod())) {
1884 // if arg is not primitive type, we need to propagate maywritten set to
1885 // the caller's location path
1887 MethodDescriptor mdCallee = fc.getMethod();
1888 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
1889 setPossibleCallees.addAll(callGraph.getMethods(mdCallee));
1891 // create mapping from arg idx to its heap paths
1892 Hashtable<Integer, NTuple<Descriptor>> mapArgIdx2CallerArgHeapPath =
1893 new Hashtable<Integer, NTuple<Descriptor>>();
1895 // arg idx is starting from 'this' arg
1896 if (fc.getThis() != null) {
1897 NTuple<Descriptor> thisHeapPath = mapHeapPath.get(fc.getThis());
1898 if (thisHeapPath == null) {
1899 // method is called without creating new flat node representing 'this'
1900 thisHeapPath = new NTuple<Descriptor>();
1901 thisHeapPath.add(fc.getThis());
1904 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(0), thisHeapPath);
1907 for (int i = 0; i < fc.numArgs(); i++) {
1908 TempDescriptor arg = fc.getArg(i);
1909 NTuple<Descriptor> argHeapPath = computePath(arg);
1910 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(i + 1), argHeapPath);
1913 // create mapping from arg idx to its location paths
1914 Hashtable<Integer, NTuple<Location>> mapArgIdx2CallerAgLocationPath =
1915 new Hashtable<Integer, NTuple<Location>>();
1917 // arg idx is starting from 'this' arg
1918 if (fc.getThis() != null) {
1919 NTuple<Location> thisLocationPath = deriveLocationTuple(mdCaller, fc.getThis());
1920 if (thisLocationPath != null) {
1921 mapArgIdx2CallerAgLocationPath.put(Integer.valueOf(0), thisLocationPath);
1925 for (int i = 0; i < fc.numArgs(); i++) {
1926 TempDescriptor arg = fc.getArg(i);
1927 NTuple<Location> argLocationPath = deriveLocationTuple(mdCaller, arg);
1928 if (argLocationPath != null) {
1929 mapArgIdx2CallerAgLocationPath.put(Integer.valueOf(i + 1), argLocationPath);
1933 for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
1934 MethodDescriptor callee = (MethodDescriptor) iterator.next();
1935 FlatMethod calleeFlatMethod = state.getMethodFlat(callee);
1937 // binding caller's args and callee's params
1939 Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc =
1940 new Hashtable<Integer, TempDescriptor>();
1942 if (calleeFlatMethod.getMethod().isStatic()) {
1943 // static method does not have implicit 'this' arg
1946 for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
1947 TempDescriptor param = calleeFlatMethod.getParameter(i);
1948 mapParamIdx2ParamTempDesc.put(Integer.valueOf(i + offset), param);
1951 Set<Integer> keySet = mapArgIdx2CallerAgLocationPath.keySet();
1952 for (Iterator iterator2 = keySet.iterator(); iterator2.hasNext();) {
1953 Integer idx = (Integer) iterator2.next();
1954 NTuple<Location> callerArgLocationPath = mapArgIdx2CallerAgLocationPath.get(idx);
1955 NTuple<Descriptor> callerArgHeapPath = mapArgIdx2CallerArgHeapPath.get(idx);
1957 TempDescriptor calleeParam = mapParamIdx2ParamTempDesc.get(idx);
1958 NTuple<Location> calleeLocationPath = deriveLocationTuple(mdCallee, calleeParam);
1959 SharedLocMap calleeDeleteSet = mapFlatMethodToDeleteSet.get(calleeFlatMethod);
1960 SharedLocMap calleeSharedLocMap = mapFlatMethodToSharedLocMap.get(calleeFlatMethod);
1962 if (calleeDeleteSet != null) {
1963 createNewMappingOfDeleteSet(callerArgLocationPath, callerArgHeapPath,
1964 calleeLocationPath, calleeDeleteSet);
1967 if (calleeSharedLocMap != null) {
1968 createNewMappingOfSharedSet(callerArgLocationPath, callerArgHeapPath,
1969 calleeLocationPath, calleeSharedLocMap);
1979 private void createNewMappingOfDeleteSet(NTuple<Location> callerArgLocationPath,
1980 NTuple<Descriptor> callerArgHeapPath, NTuple<Location> calleeLocationPath,
1981 SharedLocMap calleeDeleteSet) {
1983 SharedLocMap calleeParamDeleteSet = calleeDeleteSet.getHeapPathStartedWith(calleeLocationPath);
1985 Set<NTuple<Location>> keySet = calleeParamDeleteSet.keySet();
1986 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1987 NTuple<Location> calleeLocTupleKey = (NTuple<Location>) iterator.next();
1988 Set<NTuple<Descriptor>> heapPathSet = calleeParamDeleteSet.get(calleeLocTupleKey);
1989 for (Iterator iterator2 = heapPathSet.iterator(); iterator2.hasNext();) {
1990 NTuple<Descriptor> calleeHeapPath = (NTuple<Descriptor>) iterator2.next();
1991 calleeUnionBoundDeleteSet.addWrite(
1992 bindLocationPath(callerArgLocationPath, calleeLocTupleKey),
1993 bindHeapPath(callerArgHeapPath, calleeHeapPath));
1999 private void createNewMappingOfSharedSet(NTuple<Location> callerArgLocationPath,
2000 NTuple<Descriptor> callerArgHeapPath, NTuple<Location> calleeLocationPath,
2001 SharedLocMap calleeSharedLocMap) {
2003 SharedLocMap calleeParamSharedSet =
2004 calleeSharedLocMap.getHeapPathStartedWith(calleeLocationPath);
2006 Set<NTuple<Location>> keySet = calleeParamSharedSet.keySet();
2007 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2008 NTuple<Location> calleeLocTupleKey = (NTuple<Location>) iterator.next();
2009 Set<NTuple<Descriptor>> heapPathSet = calleeParamSharedSet.get(calleeLocTupleKey);
2010 Set<NTuple<Descriptor>> boundHeapPathSet = new HashSet<NTuple<Descriptor>>();
2011 for (Iterator iterator2 = heapPathSet.iterator(); iterator2.hasNext();) {
2012 NTuple<Descriptor> calleeHeapPath = (NTuple<Descriptor>) iterator2.next();
2013 boundHeapPathSet.add(bindHeapPath(callerArgHeapPath, calleeHeapPath));
2015 calleeIntersectBoundSharedSet.intersect(
2016 bindLocationPath(callerArgLocationPath, calleeLocTupleKey), boundHeapPathSet);
2021 private NTuple<Location> bindLocationPath(NTuple<Location> start, NTuple<Location> end) {
2022 NTuple<Location> locPath = new NTuple<Location>();
2023 locPath.addAll(start);
2024 for (int i = 1; i < end.size(); i++) {
2025 locPath.add(end.get(i));
2030 private NTuple<Descriptor> bindHeapPath(NTuple<Descriptor> start, NTuple<Descriptor> end) {
2031 NTuple<Descriptor> heapPath = new NTuple<Descriptor>();
2032 heapPath.addAll(start);
2033 for (int i = 1; i < end.size(); i++) {
2034 heapPath.add(end.get(i));
2039 private void initialize() {
2040 // First, identify ssjava loop entrace
2042 // no need to analyze method having ssjava loop
2043 methodContainingSSJavaLoop = ssjava.getMethodContainingSSJavaLoop();
2045 FlatMethod fm = state.getMethodFlat(methodContainingSSJavaLoop);
2046 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
2047 flatNodesToVisit.add(fm);
2049 LoopFinder loopFinder = new LoopFinder(fm);
2051 while (!flatNodesToVisit.isEmpty()) {
2052 FlatNode fn = flatNodesToVisit.iterator().next();
2053 flatNodesToVisit.remove(fn);
2055 String label = (String) state.fn2labelMap.get(fn);
2056 if (label != null) {
2058 if (label.equals(ssjava.SSJAVA)) {
2059 ssjava.setSSJavaLoopEntrance(fn);
2064 for (int i = 0; i < fn.numNext(); i++) {
2065 FlatNode nn = fn.getNext(i);
2066 flatNodesToVisit.add(nn);
2070 assert ssjava.getSSJavaLoopEntrance() != null;
2072 // assume that ssjava loop is top-level loop in method, not nested loop
2073 Set nestedLoop = loopFinder.nestedLoops();
2074 for (Iterator loopIter = nestedLoop.iterator(); loopIter.hasNext();) {
2075 LoopFinder lf = (LoopFinder) loopIter.next();
2076 if (lf.loopEntrances().iterator().next().equals(ssjava.getSSJavaLoopEntrance())) {
2081 assert ssjavaLoop != null;
2083 loopIncElements = (Set<FlatNode>) ssjavaLoop.loopIncElements();
2085 // perform topological sort over the set of methods accessed by the main
2087 Set<MethodDescriptor> methodDescriptorsToAnalyze = new HashSet<MethodDescriptor>();
2088 methodDescriptorsToAnalyze.addAll(ssjava.getAnnotationRequireSet());
2089 sortedDescriptors = topologicalSort(methodDescriptorsToAnalyze);
2091 liveInTempSetToEventLoop =
2092 liveness.getLiveInTemps(state.getMethodFlat(methodContainingSSJavaLoop),
2093 ssjava.getSSJavaLoopEntrance());
2096 private void methodReadWriteSetAnalysis() {
2097 // perform method READ/OVERWRITE analysis
2098 LinkedList<MethodDescriptor> descriptorListToAnalyze =
2099 (LinkedList<MethodDescriptor>) sortedDescriptors.clone();
2101 // current descriptors to visit in fixed-point interprocedural analysis,
2103 // dependency in the call graph
2104 methodDescriptorsToVisitStack.clear();
2106 descriptorListToAnalyze.removeFirst();
2108 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
2109 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
2111 while (!descriptorListToAnalyze.isEmpty()) {
2112 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
2113 methodDescriptorsToVisitStack.add(md);
2116 // analyze scheduled methods until there are no more to visit
2117 while (!methodDescriptorsToVisitStack.isEmpty()) {
2118 // start to analyze leaf node
2119 MethodDescriptor md = methodDescriptorsToVisitStack.pop();
2120 FlatMethod fm = state.getMethodFlat(md);
2122 Set<NTuple<Descriptor>> readSet = new HashSet<NTuple<Descriptor>>();
2123 Set<NTuple<Descriptor>> mustWriteSet = new HashSet<NTuple<Descriptor>>();
2124 Set<NTuple<Descriptor>> mayWriteSet = new HashSet<NTuple<Descriptor>>();
2126 methodReadWriteSet_analyzeMethod(fm, readSet, mustWriteSet, mayWriteSet);
2128 Set<NTuple<Descriptor>> prevRead = mapFlatMethodToReadSet.get(fm);
2129 Set<NTuple<Descriptor>> prevMustWrite = mapFlatMethodToMustWriteSet.get(fm);
2130 Set<NTuple<Descriptor>> prevMayWrite = mapFlatMethodToMayWriteSet.get(fm);
2132 if (!(readSet.equals(prevRead) && mustWriteSet.equals(prevMustWrite) && mayWriteSet
2133 .equals(prevMayWrite))) {
2134 mapFlatMethodToReadSet.put(fm, readSet);
2135 mapFlatMethodToMustWriteSet.put(fm, mustWriteSet);
2136 mapFlatMethodToMayWriteSet.put(fm, mayWriteSet);
2138 // results for callee changed, so enqueue dependents caller for
2141 Iterator<MethodDescriptor> depsItr = getDependents(md).iterator();
2142 while (depsItr.hasNext()) {
2143 MethodDescriptor methodNext = depsItr.next();
2144 if (!methodDescriptorsToVisitStack.contains(methodNext)
2145 && methodDescriptorToVistSet.contains(methodNext)) {
2146 methodDescriptorsToVisitStack.add(methodNext);
2155 methodReadWriteSetAnalysisToEventLoopBody();
2159 private void methodReadWriteSet_analyzeMethod(FlatMethod fm, Set<NTuple<Descriptor>> readSet,
2160 Set<NTuple<Descriptor>> mustWriteSet, Set<NTuple<Descriptor>> mayWriteSet) {
2161 if (state.SSJAVADEBUG) {
2162 System.out.println("SSJAVA: Definitely written Analyzing: " + fm);
2165 methodReadWriteSet_analyzeBody(fm, readSet, mustWriteSet, mayWriteSet, false);
2169 private void methodReadWriteSetAnalysisToEventLoopBody() {
2171 // perform method read/write analysis for Event Loop Body
2173 FlatMethod flatMethodContainingSSJavaLoop = state.getMethodFlat(methodContainingSSJavaLoop);
2175 if (state.SSJAVADEBUG) {
2176 System.out.println("SSJAVA: Definitely written Event Loop Analyzing: "
2177 + flatMethodContainingSSJavaLoop);
2180 Set<NTuple<Descriptor>> readSet = new HashSet<NTuple<Descriptor>>();
2181 Set<NTuple<Descriptor>> mustWriteSet = new HashSet<NTuple<Descriptor>>();
2182 Set<NTuple<Descriptor>> mayWriteSet = new HashSet<NTuple<Descriptor>>();
2184 mapFlatMethodToReadSet.put(flatMethodContainingSSJavaLoop, readSet);
2185 mapFlatMethodToMustWriteSet.put(flatMethodContainingSSJavaLoop, mustWriteSet);
2186 mapFlatMethodToMayWriteSet.put(flatMethodContainingSSJavaLoop, mayWriteSet);
2188 for (Iterator iterator = liveInTempSetToEventLoop.iterator(); iterator.hasNext();) {
2189 TempDescriptor liveIn = (TempDescriptor) iterator.next();
2190 NTuple<Descriptor> heapPath = new NTuple<Descriptor>();
2191 heapPath.add(liveIn);
2192 mapHeapPath.put(liveIn, heapPath);
2195 methodReadWriteSet_analyzeBody(ssjava.getSSJavaLoopEntrance(), readSet, mustWriteSet,
2200 private void methodReadWriteSet_analyzeBody(FlatNode startNode, Set<NTuple<Descriptor>> readSet,
2201 Set<NTuple<Descriptor>> mustWriteSet, Set<NTuple<Descriptor>> mayWriteSet,
2202 boolean isEventLoopBody) {
2204 // intraprocedural analysis
2205 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
2206 flatNodesToVisit.add(startNode);
2208 while (!flatNodesToVisit.isEmpty()) {
2209 FlatNode fn = flatNodesToVisit.iterator().next();
2210 flatNodesToVisit.remove(fn);
2212 Set<NTuple<Descriptor>> currMustWriteSet = new HashSet<NTuple<Descriptor>>();
2214 for (int i = 0; i < fn.numPrev(); i++) {
2215 FlatNode prevFn = fn.getPrev(i);
2216 Set<NTuple<Descriptor>> in = mapFlatNodeToMustWriteSet.get(prevFn);
2218 merge(currMustWriteSet, in);
2222 methodReadWriteSet_nodeActions(fn, currMustWriteSet, readSet, mustWriteSet, mayWriteSet,
2225 Set<NTuple<Descriptor>> mustSetPrev = mapFlatNodeToMustWriteSet.get(fn);
2227 if (!currMustWriteSet.equals(mustSetPrev)) {
2228 mapFlatNodeToMustWriteSet.put(fn, currMustWriteSet);
2229 for (int i = 0; i < fn.numNext(); i++) {
2230 FlatNode nn = fn.getNext(i);
2231 if ((!isEventLoopBody) || loopIncElements.contains(nn)) {
2232 flatNodesToVisit.add(nn);
2242 private void methodReadWriteSet_nodeActions(FlatNode fn,
2243 Set<NTuple<Descriptor>> currMustWriteSet, Set<NTuple<Descriptor>> readSet,
2244 Set<NTuple<Descriptor>> mustWriteSet, Set<NTuple<Descriptor>> mayWriteSet,
2245 boolean isEventLoopBody) {
2249 FieldDescriptor fld;
2251 switch (fn.kind()) {
2252 case FKind.FlatMethod: {
2254 // set up initial heap paths for method parameters
2255 FlatMethod fm = (FlatMethod) fn;
2256 for (int i = 0; i < fm.numParameters(); i++) {
2257 TempDescriptor param = fm.getParameter(i);
2258 NTuple<Descriptor> heapPath = new NTuple<Descriptor>();
2259 heapPath.add(param);
2260 mapHeapPath.put(param, heapPath);
2265 case FKind.FlatOpNode: {
2266 FlatOpNode fon = (FlatOpNode) fn;
2267 // for a normal assign node, need to propagate lhs's heap path to
2270 if (fon.getOp().getOp() == Operation.ASSIGN) {
2271 rhs = fon.getLeft();
2272 lhs = fon.getDest();
2274 NTuple<Descriptor> rhsHeapPath = mapHeapPath.get(rhs);
2276 // if (lhs.getType().isPrimitive()) {
2277 // NTuple<Descriptor> lhsHeapPath = new NTuple<Descriptor>();
2278 // lhsHeapPath.add(lhs);
2279 // mapHeapPath.put(lhs, lhsHeapPath);
2282 if (rhsHeapPath != null && (!lhs.getType().isPrimitive())) {
2283 mapHeapPath.put(lhs, mapHeapPath.get(rhs));
2286 // if (isEventLoopBody) {
2287 // NTuple<Descriptor> lhsHeapPath = new NTuple<Descriptor>();
2288 // lhsHeapPath.add(rhs);
2289 // mapHeapPath.put(lhs, lhsHeapPath);
2295 // shared loc extension
2296 if (isEventLoopBody) {
2297 if (!lhs.getSymbol().startsWith("neverused") && rhs.getType().isImmutable()) {
2299 if (rhs.getType().getExtension() instanceof Location
2300 && lhs.getType().getExtension() instanceof CompositeLocation) {
2302 Location rhsLoc = (Location) rhs.getType().getExtension();
2304 CompositeLocation lhsCompLoc = (CompositeLocation) lhs.getType().getExtension();
2305 Location dstLoc = lhsCompLoc.get(lhsCompLoc.getSize() - 1);
2307 NTuple<Descriptor> heapPath = new NTuple<Descriptor>();
2308 for (int i = 0; i < rhsHeapPath.size() - 1; i++) {
2309 heapPath.add(rhsHeapPath.get(i));
2312 NTuple<Descriptor> writeHeapPath = new NTuple<Descriptor>();
2313 writeHeapPath.addAll(heapPath);
2314 writeHeapPath.add(lhs);
2324 case FKind.FlatElementNode:
2325 case FKind.FlatFieldNode: {
2329 if (fn.kind() == FKind.FlatFieldNode) {
2330 FlatFieldNode ffn = (FlatFieldNode) fn;
2333 fld = ffn.getField();
2335 FlatElementNode fen = (FlatElementNode) fn;
2338 TypeDescriptor td = rhs.getType().dereference();
2339 fld = getArrayField(td);
2342 if (fld.isFinal()) {
2343 // if field is final no need to check
2348 NTuple<Descriptor> srcHeapPath = mapHeapPath.get(rhs);
2349 if (srcHeapPath != null) {
2350 // if lhs srcHeapPath is null, it means that it is not reachable from
2351 // callee's parameters. so just ignore it
2353 NTuple<Descriptor> readingHeapPath = new NTuple<Descriptor>(srcHeapPath.getList());
2354 if (fn.kind() == FKind.FlatFieldNode) {
2355 readingHeapPath.add(fld);
2358 mapHeapPath.put(lhs, readingHeapPath);
2361 if (fld.getType().isImmutable()) {
2362 // if WT doesnot have hp(x.f), add hp(x.f) to READ
2363 if (!currMustWriteSet.contains(readingHeapPath)) {
2364 readSet.add(readingHeapPath);
2368 // no need to kill hp(x.f) from WT
2374 case FKind.FlatSetFieldNode:
2375 case FKind.FlatSetElementNode: {
2379 if (fn.kind() == FKind.FlatSetFieldNode) {
2380 FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
2381 lhs = fsfn.getDst();
2382 fld = fsfn.getField();
2383 rhs = fsfn.getSrc();
2385 FlatSetElementNode fsen = (FlatSetElementNode) fn;
2386 lhs = fsen.getDst();
2387 rhs = fsen.getSrc();
2388 TypeDescriptor td = lhs.getType().dereference();
2389 fld = getArrayField(td);
2393 NTuple<Descriptor> lhsHeapPath = mapHeapPath.get(lhs);
2395 if (lhsHeapPath != null) {
2396 // if lhs heap path is null, it means that it is not reachable from
2397 // callee's parameters. so just ignore it
2398 NTuple<Descriptor> fldHeapPath = new NTuple<Descriptor>(lhsHeapPath.getList());
2399 if (fn.kind() != FKind.FlatSetElementNode) {
2400 fldHeapPath.add(fld);
2402 // mapHeapPath.put(fld, fldHeapPath);
2405 // need to add hp(y) to WT
2406 if (fn.kind() != FKind.FlatSetElementNode) {
2407 currMustWriteSet.add(fldHeapPath);
2409 mayWriteSet.add(fldHeapPath);
2416 case FKind.FlatCall: {
2418 FlatCall fc = (FlatCall) fn;
2420 bindHeapPathCallerArgWithCalleeParam(fc);
2422 Set<NTuple<Descriptor>> boundReadSet = new HashSet<NTuple<Descriptor>>();
2423 boundReadSet.addAll(calleeUnionBoundReadSet);
2425 Set<NTuple<Descriptor>> boundMustWriteSet = new HashSet<NTuple<Descriptor>>();
2426 boundMustWriteSet.addAll(calleeIntersectBoundMustWriteSet);
2428 Set<NTuple<Descriptor>> boundMayWriteSet = new HashSet<NTuple<Descriptor>>();
2429 boundMayWriteSet.addAll(calleeUnionBoundMayWriteSet);
2431 mapFlatNodeToBoundReadSet.put(fn, boundReadSet);
2432 mapFlatNodeToBoundMustWriteSet.put(fn, boundMustWriteSet);
2433 mapFlatNodeToBoundMayWriteSet.put(fn, boundMayWriteSet);
2435 // add heap path, which is an element of READ_bound set and is not
2437 // element of WT set, to the caller's READ set
2438 for (Iterator iterator = calleeUnionBoundReadSet.iterator(); iterator.hasNext();) {
2439 NTuple<Descriptor> read = (NTuple<Descriptor>) iterator.next();
2440 if (!currMustWriteSet.contains(read)) {
2445 // add heap path, which is an element of OVERWRITE_bound set, to the
2447 for (Iterator iterator = calleeIntersectBoundMustWriteSet.iterator(); iterator.hasNext();) {
2448 NTuple<Descriptor> write = (NTuple<Descriptor>) iterator.next();
2449 currMustWriteSet.add(write);
2452 // add heap path, which is an element of WRITE_BOUND set, to the
2453 // caller's writeSet
2454 for (Iterator iterator = calleeUnionBoundMayWriteSet.iterator(); iterator.hasNext();) {
2455 NTuple<Descriptor> write = (NTuple<Descriptor>) iterator.next();
2456 mayWriteSet.add(write);
2462 case FKind.FlatExit: {
2463 // merge the current written set with OVERWRITE set
2464 merge(mustWriteSet, currMustWriteSet);
2472 static public FieldDescriptor getArrayField(TypeDescriptor td) {
2473 FieldDescriptor fd = mapTypeToArrayField.get(td);
2476 new FieldDescriptor(new Modifiers(Modifiers.PUBLIC), td, arrayElementFieldName, null,
2478 mapTypeToArrayField.put(td, fd);
2483 private void merge(Set<NTuple<Descriptor>> curr, Set<NTuple<Descriptor>> in) {
2484 if (curr.isEmpty()) {
2485 // set has a special initial value which covers all possible
2487 // For the first time of intersection, we can take all previous set
2490 // otherwise, current set is the intersection of the two sets
2496 // combine two heap path
2497 private NTuple<Descriptor> combine(NTuple<Descriptor> callerIn, NTuple<Descriptor> calleeIn) {
2498 NTuple<Descriptor> combined = new NTuple<Descriptor>();
2500 for (int i = 0; i < callerIn.size(); i++) {
2501 combined.add(callerIn.get(i));
2504 // the first element of callee's heap path represents parameter
2505 // so we skip the first one since it is already added from caller's heap
2507 for (int i = 1; i < calleeIn.size(); i++) {
2508 combined.add(calleeIn.get(i));
2514 private Set<NTuple<Descriptor>> bindSet(Set<NTuple<Descriptor>> calleeSet,
2515 Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc,
2516 Hashtable<Integer, NTuple<Descriptor>> mapCallerArgIdx2HeapPath) {
2518 Set<NTuple<Descriptor>> boundedCalleeSet = new HashSet<NTuple<Descriptor>>();
2520 Set<Integer> keySet = mapCallerArgIdx2HeapPath.keySet();
2521 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2522 Integer idx = (Integer) iterator.next();
2524 NTuple<Descriptor> callerArgHeapPath = mapCallerArgIdx2HeapPath.get(idx);
2525 TempDescriptor calleeParam = mapParamIdx2ParamTempDesc.get(idx);
2526 for (Iterator iterator2 = calleeSet.iterator(); iterator2.hasNext();) {
2527 NTuple<Descriptor> element = (NTuple<Descriptor>) iterator2.next();
2528 if (element.startsWith(calleeParam)) {
2529 NTuple<Descriptor> boundElement = combine(callerArgHeapPath, element);
2530 boundedCalleeSet.add(boundElement);
2536 return boundedCalleeSet;
2540 // Borrowed it from disjoint analysis
2541 private LinkedList<MethodDescriptor> topologicalSort(Set<MethodDescriptor> toSort) {
2543 Set<MethodDescriptor> discovered = new HashSet<MethodDescriptor>();
2545 LinkedList<MethodDescriptor> sorted = new LinkedList<MethodDescriptor>();
2547 Iterator<MethodDescriptor> itr = toSort.iterator();
2548 while (itr.hasNext()) {
2549 MethodDescriptor d = itr.next();
2551 if (!discovered.contains(d)) {
2552 dfsVisit(d, toSort, sorted, discovered);
2559 // While we're doing DFS on call graph, remember
2560 // dependencies for efficient queuing of methods
2561 // during interprocedural analysis:
2563 // a dependent of a method decriptor d for this analysis is:
2564 // 1) a method or task that invokes d
2565 // 2) in the descriptorsToAnalyze set
2566 private void dfsVisit(MethodDescriptor md, Set<MethodDescriptor> toSort,
2567 LinkedList<MethodDescriptor> sorted, Set<MethodDescriptor> discovered) {
2571 Iterator itr = callGraph.getCallerSet(md).iterator();
2572 while (itr.hasNext()) {
2573 MethodDescriptor dCaller = (MethodDescriptor) itr.next();
2574 // only consider callers in the original set to analyze
2575 if (!toSort.contains(dCaller)) {
2578 if (!discovered.contains(dCaller)) {
2579 addDependent(md, // callee
2583 dfsVisit(dCaller, toSort, sorted, discovered);
2587 // for leaf-nodes last now!
2591 // a dependent of a method decriptor d for this analysis is:
2592 // 1) a method or task that invokes d
2593 // 2) in the descriptorsToAnalyze set
2594 private void addDependent(MethodDescriptor callee, MethodDescriptor caller) {
2595 Set<MethodDescriptor> deps = mapDescriptorToSetDependents.get(callee);
2597 deps = new HashSet<MethodDescriptor>();
2600 mapDescriptorToSetDependents.put(callee, deps);
2603 private Set<MethodDescriptor> getDependents(MethodDescriptor callee) {
2604 Set<MethodDescriptor> deps = mapDescriptorToSetDependents.get(callee);
2606 deps = new HashSet<MethodDescriptor>();
2607 mapDescriptorToSetDependents.put(callee, deps);
2612 private NTuple<Descriptor> computePath(Descriptor td) {
2613 // generate proper path fot input td
2614 // if td is local variable, it just generate one element tuple path
2615 if (mapHeapPath.containsKey(td)) {
2616 NTuple<Descriptor> rtrHeapPath = new NTuple<Descriptor>();
2617 rtrHeapPath.addAll(mapHeapPath.get(td));
2620 NTuple<Descriptor> rtrHeapPath = new NTuple<Descriptor>();
2621 rtrHeapPath.add(td);
2626 private NTuple<Location> deriveThisLocationTuple(MethodDescriptor md) {
2627 String thisLocIdentifier = ssjava.getMethodLattice(md).getThisLoc();
2628 Location thisLoc = new Location(md, thisLocIdentifier);
2629 NTuple<Location> locTuple = new NTuple<Location>();
2630 locTuple.add(thisLoc);
2634 private NTuple<Location> deriveGlobalLocationTuple(MethodDescriptor md) {
2635 String globalLocIdentifier = ssjava.getMethodLattice(md).getGlobalLoc();
2636 Location globalLoc = new Location(md, globalLocIdentifier);
2637 NTuple<Location> locTuple = new NTuple<Location>();
2638 locTuple.add(globalLoc);
2642 private NTuple<Location> deriveLocationTuple(MethodDescriptor md, TempDescriptor td) {
2644 assert td.getType() != null;
2646 if (mapDescriptorToLocationPath.containsKey(td)) {
2647 NTuple<Location> locPath = mapDescriptorToLocationPath.get(td);
2648 NTuple<Location> rtrPath = new NTuple<Location>();
2649 rtrPath.addAll(locPath);
2652 if (td.getSymbol().startsWith("this")) {
2653 NTuple<Location> thisPath = deriveThisLocationTuple(md);
2654 NTuple<Location> rtrPath = new NTuple<Location>();
2655 rtrPath.addAll(thisPath);
2659 if (td.getType().getExtension() != null) {
2660 SSJavaType ssJavaType = (SSJavaType) td.getType().getExtension();
2661 if (ssJavaType.getCompLoc() != null) {
2662 NTuple<Location> rtrPath = new NTuple<Location>();
2663 rtrPath.addAll(ssJavaType.getCompLoc().getTuple());