fix another bug: callee propagated wrong write effects associated with its local...
[IRC.git] / Robust / src / Analysis / SSJava / DefinitelyWrittenCheck.java
1 package Analysis.SSJava;
2
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;
8 import java.util.Set;
9 import java.util.Stack;
10
11 import Analysis.CallGraph.CallGraph;
12 import Analysis.Loops.LoopFinder;
13 import IR.Descriptor;
14 import IR.FieldDescriptor;
15 import IR.MethodDescriptor;
16 import IR.Operation;
17 import IR.State;
18 import IR.TypeDescriptor;
19 import IR.TypeExtension;
20 import IR.Flat.FKind;
21 import IR.Flat.FlatCall;
22 import IR.Flat.FlatElementNode;
23 import IR.Flat.FlatFieldNode;
24 import IR.Flat.FlatLiteralNode;
25 import IR.Flat.FlatMethod;
26 import IR.Flat.FlatNew;
27 import IR.Flat.FlatNode;
28 import IR.Flat.FlatOpNode;
29 import IR.Flat.FlatSetElementNode;
30 import IR.Flat.FlatSetFieldNode;
31 import IR.Flat.TempDescriptor;
32 import IR.Tree.Modifiers;
33
34 public class DefinitelyWrittenCheck {
35
36   SSJavaAnalysis ssjava;
37   State state;
38   CallGraph callGraph;
39
40   int debugcount = 0;
41
42   // maps a descriptor to its known dependents: namely
43   // methods or tasks that call the descriptor's method
44   // AND are part of this analysis (reachable from main)
45   private Hashtable<Descriptor, Set<MethodDescriptor>> mapDescriptorToSetDependents;
46
47   // maps a flat node to its WrittenSet: this keeps all heap path overwritten
48   // previously.
49   private Hashtable<FlatNode, Set<NTuple<Descriptor>>> mapFlatNodeToMustWriteSet;
50
51   // maps a temp descriptor to its heap path
52   // each temp descriptor has a unique heap path since we do not allow any
53   // alias.
54   private Hashtable<Descriptor, NTuple<Descriptor>> mapHeapPath;
55
56   // maps a temp descriptor to its composite location
57   private Hashtable<TempDescriptor, NTuple<Location>> mapDescriptorToLocationPath;
58
59   // maps a flat method to the READ that is the set of heap path that is
60   // expected to be written before method invocation
61   private Hashtable<FlatMethod, Set<NTuple<Descriptor>>> mapFlatMethodToReadSet;
62
63   // maps a flat method to the must-write set that is the set of heap path that
64   // is overwritten on every possible path during method invocation
65   private Hashtable<FlatMethod, Set<NTuple<Descriptor>>> mapFlatMethodToMustWriteSet;
66
67   // maps a flat method to the DELETE SET that is a set of heap path to shared
68   // locations that are
69   // written to but not overwritten by the higher value
70   private Hashtable<FlatMethod, SharedLocMap> mapFlatMethodToDeleteSet;
71
72   // maps a flat method to the S SET that is a set of heap path to shared
73   // locations that are overwritten by the higher value
74   private Hashtable<FlatMethod, SharedLocMap> mapFlatMethodToSharedLocMap;
75
76   // maps a flat method to the may-wirte set that is the set of heap path that
77   // might be written to
78   private Hashtable<FlatMethod, Set<NTuple<Descriptor>>> mapFlatMethodToMayWriteSet;
79
80   // maps a call site to the read set contributed by all callees
81   private Hashtable<FlatNode, Set<NTuple<Descriptor>>> mapFlatNodeToBoundReadSet;
82
83   // maps a call site to the must write set contributed by all callees
84   private Hashtable<FlatNode, Set<NTuple<Descriptor>>> mapFlatNodeToBoundMustWriteSet;
85
86   // maps a call site to the may read set contributed by all callees
87   private Hashtable<FlatNode, Set<NTuple<Descriptor>>> mapFlatNodeToBoundMayWriteSet;
88
89   // points to method containing SSJAVA Loop
90   private MethodDescriptor methodContainingSSJavaLoop;
91
92   // maps a flatnode to definitely written analysis mapping M
93   private Hashtable<FlatNode, Hashtable<NTuple<Descriptor>, Set<WriteAge>>> mapFlatNodetoEventLoopMap;
94
95   // maps shared location to the set of descriptors which belong to the shared
96   // location
97
98   // keep current descriptors to visit in fixed-point interprocedural analysis,
99   private Stack<MethodDescriptor> methodDescriptorsToVisitStack;
100
101   // when analyzing flatcall, need to re-schedule set of callee
102   private Set<MethodDescriptor> calleesToEnqueue;
103
104   private Set<ReadSummary> possibleCalleeReadSummarySetToCaller;
105
106   public static final String arrayElementFieldName = "___element_";
107   static protected Hashtable<TypeDescriptor, FieldDescriptor> mapTypeToArrayField;
108
109   // maps a method descriptor to the merged incoming caller's current
110   // reading status
111   // it is for setting clearance flag when all read set is overwritten
112   private Hashtable<MethodDescriptor, ReadSummary> mapMethodDescriptorToReadSummary;
113
114   private Hashtable<MethodDescriptor, MultiSourceMap<NTuple<Location>, NTuple<Descriptor>>> mapMethodToSharedLocCoverSet;
115
116   private Hashtable<FlatNode, SharedLocMap> mapFlatNodeToSharedLocMapping;
117   private Hashtable<FlatNode, SharedLocMap> mapFlatNodeToDeleteSet;
118
119   private LinkedList<MethodDescriptor> sortedDescriptors;
120
121   private LoopFinder ssjavaLoop;
122   private Set<FlatNode> loopIncElements;
123
124   private Set<NTuple<Descriptor>> calleeUnionBoundReadSet;
125   private Set<NTuple<Descriptor>> calleeIntersectBoundMustWriteSet;
126   private Set<NTuple<Descriptor>> calleeUnionBoundMayWriteSet;
127   private SharedLocMap calleeUnionBoundDeleteSet;
128   private SharedLocMap calleeIntersectBoundSharedSet;
129
130   private Hashtable<Descriptor, Location> mapDescToLocation;
131
132   private TempDescriptor LOCAL;
133
134   public static int MAXAGE = 1;
135
136   public DefinitelyWrittenCheck(SSJavaAnalysis ssjava, State state) {
137     this.state = state;
138     this.ssjava = ssjava;
139     this.callGraph = ssjava.getCallGraph();
140     this.mapFlatNodeToMustWriteSet = new Hashtable<FlatNode, Set<NTuple<Descriptor>>>();
141     this.mapDescriptorToSetDependents = new Hashtable<Descriptor, Set<MethodDescriptor>>();
142     this.mapHeapPath = new Hashtable<Descriptor, NTuple<Descriptor>>();
143     this.mapDescriptorToLocationPath = new Hashtable<TempDescriptor, NTuple<Location>>();
144     this.mapFlatMethodToReadSet = new Hashtable<FlatMethod, Set<NTuple<Descriptor>>>();
145     this.mapFlatMethodToMustWriteSet = new Hashtable<FlatMethod, Set<NTuple<Descriptor>>>();
146     this.mapFlatMethodToMayWriteSet = new Hashtable<FlatMethod, Set<NTuple<Descriptor>>>();
147     this.mapFlatNodetoEventLoopMap =
148         new Hashtable<FlatNode, Hashtable<NTuple<Descriptor>, Set<WriteAge>>>();
149     this.calleeUnionBoundReadSet = new HashSet<NTuple<Descriptor>>();
150     this.calleeIntersectBoundMustWriteSet = new HashSet<NTuple<Descriptor>>();
151     this.calleeUnionBoundMayWriteSet = new HashSet<NTuple<Descriptor>>();
152
153     this.methodDescriptorsToVisitStack = new Stack<MethodDescriptor>();
154     this.calleesToEnqueue = new HashSet<MethodDescriptor>();
155     this.mapTypeToArrayField = new Hashtable<TypeDescriptor, FieldDescriptor>();
156     this.LOCAL = new TempDescriptor("LOCAL");
157     this.mapDescToLocation = new Hashtable<Descriptor, Location>();
158     this.possibleCalleeReadSummarySetToCaller = new HashSet<ReadSummary>();
159     this.mapMethodDescriptorToReadSummary = new Hashtable<MethodDescriptor, ReadSummary>();
160     this.mapFlatNodeToBoundReadSet = new Hashtable<FlatNode, Set<NTuple<Descriptor>>>();
161     this.mapFlatNodeToBoundMustWriteSet = new Hashtable<FlatNode, Set<NTuple<Descriptor>>>();
162     this.mapFlatNodeToBoundMayWriteSet = new Hashtable<FlatNode, Set<NTuple<Descriptor>>>();
163     this.mapFlatNodeToSharedLocMapping = new Hashtable<FlatNode, SharedLocMap>();
164     this.mapFlatMethodToDeleteSet = new Hashtable<FlatMethod, SharedLocMap>();
165     this.calleeUnionBoundDeleteSet = new SharedLocMap();
166     this.calleeIntersectBoundSharedSet = new SharedLocMap();
167     this.mapFlatMethodToSharedLocMap = new Hashtable<FlatMethod, SharedLocMap>();
168     this.mapMethodToSharedLocCoverSet =
169         new Hashtable<MethodDescriptor, MultiSourceMap<NTuple<Location>, NTuple<Descriptor>>>();
170     this.mapFlatNodeToDeleteSet = new Hashtable<FlatNode, SharedLocMap>();
171   }
172
173   public void definitelyWrittenCheck() {
174     if (!ssjava.getAnnotationRequireSet().isEmpty()) {
175       initialize();
176
177       methodReadWriteSetAnalysis();
178       computeSharedCoverSet();
179
180       // System.out.println("$$$=" +
181       // mapMethodToSharedLocCoverSet.get(methodContainingSSJavaLoop));
182       // System.exit(0);
183
184       sharedLocAnalysis();
185
186       eventLoopAnalysis();
187
188     }
189   }
190
191   private void sharedLocAnalysis() {
192
193     // perform method READ/OVERWRITE analysis
194     LinkedList<MethodDescriptor> descriptorListToAnalyze =
195         (LinkedList<MethodDescriptor>) sortedDescriptors.clone();
196
197     // current descriptors to visit in fixed-point interprocedural analysis,
198     // prioritized by
199     // dependency in the call graph
200     methodDescriptorsToVisitStack.clear();
201
202     descriptorListToAnalyze.removeFirst();
203
204     Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
205     methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
206
207     while (!descriptorListToAnalyze.isEmpty()) {
208       MethodDescriptor md = descriptorListToAnalyze.removeFirst();
209       methodDescriptorsToVisitStack.add(md);
210     }
211
212     // analyze scheduled methods until there are no more to visit
213     while (!methodDescriptorsToVisitStack.isEmpty()) {
214       // start to analyze leaf node
215       MethodDescriptor md = methodDescriptorsToVisitStack.pop();
216       FlatMethod fm = state.getMethodFlat(md);
217
218       SharedLocMap sharedLocMap = new SharedLocMap();
219       SharedLocMap deleteSet = new SharedLocMap();
220
221       sharedLoc_analyzeMethod(fm, sharedLocMap, deleteSet);
222       SharedLocMap prevSharedLocMap = mapFlatMethodToSharedLocMap.get(fm);
223       SharedLocMap prevDeleteSet = mapFlatMethodToDeleteSet.get(fm);
224
225       if (!(deleteSet.equals(prevDeleteSet) && sharedLocMap.equals(prevSharedLocMap))) {
226         mapFlatMethodToSharedLocMap.put(fm, sharedLocMap);
227         mapFlatMethodToDeleteSet.put(fm, deleteSet);
228
229         // results for callee changed, so enqueue dependents caller for
230         // further
231         // analysis
232         Iterator<MethodDescriptor> depsItr = getDependents(md).iterator();
233         while (depsItr.hasNext()) {
234           MethodDescriptor methodNext = depsItr.next();
235           if (!methodDescriptorsToVisitStack.contains(methodNext)
236               && methodDescriptorToVistSet.contains(methodNext)) {
237             methodDescriptorsToVisitStack.add(methodNext);
238           }
239
240         }
241
242       }
243
244     }
245
246     sharedLoc_analyzeEventLoop();
247
248   }
249
250   private void sharedLoc_analyzeEventLoop() {
251     if (state.SSJAVADEBUG) {
252       System.out.println("SSJAVA: Definite clearance for shared locations Analyzing: eventloop");
253     }
254     SharedLocMap sharedLocMap = new SharedLocMap();
255     SharedLocMap deleteSet = new SharedLocMap();
256     sharedLoc_analyzeBody(state.getMethodFlat(methodContainingSSJavaLoop),
257         ssjava.getSSJavaLoopEntrance(), sharedLocMap, deleteSet, true);
258
259   }
260
261   private void sharedLoc_analyzeMethod(FlatMethod fm, SharedLocMap sharedLocMap,
262       SharedLocMap deleteSet) {
263     if (state.SSJAVADEBUG) {
264       System.out.println("SSJAVA: Definite clearance for shared locations Analyzing: " + fm);
265     }
266
267     sharedLoc_analyzeBody(fm, fm, sharedLocMap, deleteSet, false);
268
269   }
270
271   private void sharedLoc_analyzeBody(FlatMethod fm, FlatNode startNode, SharedLocMap sharedLocMap,
272       SharedLocMap deleteSet, boolean isEventLoopBody) {
273
274     // intraprocedural analysis
275     Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
276     flatNodesToVisit.add(startNode);
277
278     while (!flatNodesToVisit.isEmpty()) {
279       FlatNode fn = flatNodesToVisit.iterator().next();
280       flatNodesToVisit.remove(fn);
281
282       SharedLocMap currSharedSet = new SharedLocMap();
283       SharedLocMap currDeleteSet = new SharedLocMap();
284
285       for (int i = 0; i < fn.numPrev(); i++) {
286         FlatNode prevFn = fn.getPrev(i);
287         SharedLocMap inSharedLoc = mapFlatNodeToSharedLocMapping.get(prevFn);
288         if (inSharedLoc != null) {
289           mergeSharedLocMap(currSharedSet, inSharedLoc);
290         }
291
292         SharedLocMap inDeleteLoc = mapFlatNodeToDeleteSet.get(prevFn);
293         if (inDeleteLoc != null) {
294           mergeDeleteSet(currDeleteSet, inDeleteLoc);
295         }
296       }
297
298       sharedLoc_nodeActions(fm, fn, currSharedSet, currDeleteSet, sharedLocMap, deleteSet,
299           isEventLoopBody);
300
301       SharedLocMap prevSharedSet = mapFlatNodeToSharedLocMapping.get(fn);
302       SharedLocMap prevDeleteSet = mapFlatNodeToDeleteSet.get(fn);
303
304       if (!(currSharedSet.equals(prevSharedSet) && currDeleteSet.equals(prevDeleteSet))) {
305         mapFlatNodeToSharedLocMapping.put(fn, currSharedSet);
306         mapFlatNodeToDeleteSet.put(fn, currDeleteSet);
307         for (int i = 0; i < fn.numNext(); i++) {
308           FlatNode nn = fn.getNext(i);
309           if ((!isEventLoopBody) || loopIncElements.contains(nn)) {
310             flatNodesToVisit.add(nn);
311           }
312
313         }
314       }
315
316     }
317
318   }
319
320   private void sharedLoc_nodeActions(FlatMethod fm, FlatNode fn, SharedLocMap curr,
321       SharedLocMap currDeleteSet, SharedLocMap sharedLocMap, SharedLocMap deleteSet,
322       boolean isEventLoopBody) {
323
324     MethodDescriptor md = fm.getMethod();
325
326     SharedLocMap killSet = new SharedLocMap();
327     SharedLocMap genSet = new SharedLocMap();
328
329     TempDescriptor lhs;
330     TempDescriptor rhs;
331     FieldDescriptor fld;
332
333     switch (fn.kind()) {
334
335     case FKind.FlatOpNode: {
336
337       if (isEventLoopBody) {
338         FlatOpNode fon = (FlatOpNode) fn;
339
340         if (fon.getOp().getOp() == Operation.ASSIGN) {
341           lhs = fon.getDest();
342           rhs = fon.getLeft();
343
344           if (!lhs.getSymbol().startsWith("neverused") && !lhs.getSymbol().startsWith("leftop")
345               && !lhs.getSymbol().startsWith("rightop") && rhs.getType().isImmutable()) {
346
347             Location dstLoc = getLocation(lhs);
348             if (dstLoc != null && ssjava.isSharedLocation(dstLoc)) {
349               NTuple<Descriptor> lhsHeapPath = computePath(lhs);
350               NTuple<Location> lhsLocTuple = mapDescriptorToLocationPath.get(lhs);
351
352               Location srcLoc = getLocation(lhs);
353
354               // computing gen/kill set
355               computeKILLSetForWrite(curr, killSet, lhsLocTuple, lhsHeapPath);
356               if (!dstLoc.equals(srcLoc)) {
357                 computeGENSetForHigherWrite(curr, killSet, lhsLocTuple, lhsHeapPath);
358                 updateDeleteSetForHigherWrite(currDeleteSet, lhsLocTuple, lhsHeapPath);
359               } else {
360                 computeGENSetForSameHeightWrite(curr, killSet, lhsLocTuple, lhsHeapPath);
361                 updateDeleteSetForSameHeightWrite(currDeleteSet, lhsLocTuple, lhsHeapPath);
362               }
363
364               // System.out.println("VAR WRITE:" + fn);
365               // System.out.println("lhsLocTuple=" + lhsLocTuple +
366               // " lhsHeapPath=" + lhsHeapPath);
367               // System.out.println("dstLoc=" + dstLoc + " srcLoc=" + srcLoc);
368               // System.out.println("KILLSET=" + killSet);
369               // System.out.println("GENSet=" + genSet);
370               // System.out.println("DELETESET=" + currDeleteSet);
371
372             }
373
374           }
375
376         }
377
378       }
379
380     }
381       break;
382
383     case FKind.FlatSetFieldNode:
384     case FKind.FlatSetElementNode: {
385
386       Location fieldLoc;
387       if (fn.kind() == FKind.FlatSetFieldNode) {
388         FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
389         lhs = fsfn.getDst();
390         fld = fsfn.getField();
391         rhs = fsfn.getSrc();
392         fieldLoc = (Location) fld.getType().getExtension();
393       } else {
394         break;
395       }
396
397       if (!isEventLoopBody && fieldLoc.getDescriptor().equals(md)) {
398         // if the field belongs to the local lattice, no reason to calculate
399         // shared location
400         break;
401       }
402
403       NTuple<Location> fieldLocTuple = new NTuple<Location>();
404       if (fld.isStatic()) {
405         if (fld.isFinal()) {
406           // in this case, fld has TOP location
407           Location topLocation = Location.createTopLocation(md);
408           fieldLocTuple.add(topLocation);
409         } else {
410           fieldLocTuple.addAll(deriveGlobalLocationTuple(md));
411           if (fn.kind() == FKind.FlatSetFieldNode) {
412             fieldLocTuple.add((Location) fld.getType().getExtension());
413           }
414         }
415
416       } else {
417         fieldLocTuple.addAll(deriveLocationTuple(md, lhs));
418         if (fn.kind() == FKind.FlatSetFieldNode) {
419           fieldLocTuple.add((Location) fld.getType().getExtension());
420         }
421       }
422
423       // shared loc extension
424       Location srcLoc = getLocation(rhs);
425       if (ssjava.isSharedLocation(fieldLoc)) {
426         // only care the case that loc(f) is shared location
427         // write(field)
428
429         // NTuple<Location> fieldLocTuple = new NTuple<Location>();
430         // fieldLocTuple.addAll(mapDescriptorToLocationPath.get(lhs));
431         // fieldLocTuple.add(fieldLoc);
432
433         NTuple<Descriptor> fldHeapPath = new NTuple<Descriptor>();
434         fldHeapPath.addAll(computePath(lhs));
435         if (fn.kind() == FKind.FlatSetFieldNode) {
436           fldHeapPath.add(fld);
437         }
438
439         // computing gen/kill set
440         computeKILLSetForWrite(curr, killSet, fieldLocTuple, fldHeapPath);
441         if (!fieldLoc.equals(srcLoc)) {
442           computeGENSetForHigherWrite(curr, genSet, fieldLocTuple, fldHeapPath);
443           updateDeleteSetForHigherWrite(currDeleteSet, fieldLocTuple, fldHeapPath);
444         } else {
445           computeGENSetForSameHeightWrite(curr, genSet, fieldLocTuple, fldHeapPath);
446           updateDeleteSetForSameHeightWrite(currDeleteSet, fieldLocTuple, fldHeapPath);
447         }
448
449         // System.out.println("################");
450         // System.out.println("FIELD WRITE:" + fn);
451         // System.out.println("FldHeapPath=" + fldHeapPath);
452         // System.out.println("fieldLocTuple=" + fieldLocTuple + " srcLoc=" +
453         // srcLoc);
454         // System.out.println("KILLSET=" + killSet);
455         // System.out.println("GENSet=" + genSet);
456         // System.out.println("DELETESET=" + currDeleteSet);
457       }
458
459     }
460       break;
461
462     case FKind.FlatCall: {
463       FlatCall fc = (FlatCall) fn;
464
465       bindHeapPathCallerArgWithCaleeParamForSharedLoc(fm.getMethod(), fc);
466
467       // computing gen/kill set
468       generateKILLSetForFlatCall(curr, killSet);
469       generateGENSetForFlatCall(curr, genSet);
470
471       // System.out.println("#FLATCALL=" + fc);
472       // System.out.println("KILLSET=" + killSet);
473       // System.out.println("GENSet=" + genSet);
474       // System.out.println("bound DELETE Set=" + calleeUnionBoundDeleteSet);
475
476     }
477       break;
478
479     case FKind.FlatExit: {
480       // merge the current delete/shared loc mapping
481       mergeSharedLocMap(sharedLocMap, curr);
482       mergeDeleteSet(deleteSet, currDeleteSet);
483
484       // System.out.println("#FLATEXIT sharedLocMap=" + sharedLocMap);
485     }
486       break;
487
488     }
489
490     computeNewMapping(curr, killSet, genSet);
491     if (!curr.map.isEmpty()) {
492       // System.out.println(fn + "#######" + curr);
493     }
494
495   }
496
497   private void generateGENSetForFlatCall(SharedLocMap curr, SharedLocMap genSet) {
498
499     Set<NTuple<Location>> locTupleSet = calleeIntersectBoundSharedSet.keySet();
500     for (Iterator iterator = locTupleSet.iterator(); iterator.hasNext();) {
501       NTuple<Location> locTupleKey = (NTuple<Location>) iterator.next();
502       genSet.addWrite(locTupleKey, curr.get(locTupleKey));
503       genSet.addWrite(locTupleKey, calleeIntersectBoundSharedSet.get(locTupleKey));
504
505       genSet.removeWriteAll(locTupleKey, calleeUnionBoundDeleteSet.get(locTupleKey));
506     }
507
508   }
509
510   private void generateKILLSetForFlatCall(SharedLocMap curr, SharedLocMap killSet) {
511
512     Set<NTuple<Location>> locTupleSet = calleeIntersectBoundSharedSet.keySet();
513     for (Iterator iterator = locTupleSet.iterator(); iterator.hasNext();) {
514       NTuple<Location> locTupleKey = (NTuple<Location>) iterator.next();
515       killSet.addWrite(locTupleKey, curr.get(locTupleKey));
516     }
517
518   }
519
520   private void mergeDeleteSet(SharedLocMap currDeleteSet, SharedLocMap inDeleteLoc) {
521
522     Set<NTuple<Location>> locTupleKeySet = inDeleteLoc.keySet();
523
524     for (Iterator iterator = locTupleKeySet.iterator(); iterator.hasNext();) {
525       NTuple<Location> locTupleKey = (NTuple<Location>) iterator.next();
526
527       Set<NTuple<Descriptor>> inSet = inDeleteLoc.get(locTupleKey);
528       currDeleteSet.addWrite(locTupleKey, inSet);
529
530     }
531   }
532
533   private void computeNewMapping(SharedLocMap curr, SharedLocMap killSet, SharedLocMap genSet) {
534     curr.kill(killSet);
535     curr.gen(genSet);
536   }
537
538   private void updateDeleteSetForHigherWrite(SharedLocMap currDeleteSet, NTuple<Location> locTuple,
539       NTuple<Descriptor> hp) {
540     currDeleteSet.removeWrite(locTuple, hp);
541   }
542
543   private void updateDeleteSetForSameHeightWrite(SharedLocMap currDeleteSet,
544       NTuple<Location> locTuple, NTuple<Descriptor> hp) {
545     currDeleteSet.addWrite(locTuple, hp);
546   }
547
548   private void computeGENSetForHigherWrite(SharedLocMap curr, SharedLocMap genSet,
549       NTuple<Location> locTuple, NTuple<Descriptor> hp) {
550     Set<NTuple<Descriptor>> currWriteSet = curr.get(locTuple);
551
552     if (currWriteSet != null) {
553       genSet.addWrite(locTuple, currWriteSet);
554     }
555
556     genSet.addWrite(locTuple, hp);
557   }
558
559   private void computeGENSetForSameHeightWrite(SharedLocMap curr, SharedLocMap genSet,
560       NTuple<Location> locTuple, NTuple<Descriptor> hp) {
561     Set<NTuple<Descriptor>> currWriteSet = curr.get(locTuple);
562
563     if (currWriteSet != null) {
564       genSet.addWrite(locTuple, currWriteSet);
565     }
566     genSet.removeWrite(locTuple, hp);
567   }
568
569   private void computeKILLSetForWrite(SharedLocMap curr, SharedLocMap killSet,
570       NTuple<Location> locTuple, NTuple<Descriptor> hp) {
571
572     Set<NTuple<Descriptor>> writeSet = curr.get(locTuple);
573     if (writeSet != null) {
574       killSet.addWrite(locTuple, writeSet);
575     }
576
577   }
578
579   private void mergeSharedLocMap(SharedLocMap currSharedSet, SharedLocMap in) {
580
581     Set<NTuple<Location>> locTupleKeySet = in.keySet();
582     for (Iterator iterator = locTupleKeySet.iterator(); iterator.hasNext();) {
583       NTuple<Location> locTupleKey = (NTuple<Location>) iterator.next();
584
585       Set<NTuple<Descriptor>> inSet = in.get(locTupleKey);
586       Set<NTuple<Descriptor>> currSet = currSharedSet.get(locTupleKey);
587       if (currSet == null) {
588         currSet = new HashSet<NTuple<Descriptor>>();
589         currSet.addAll(inSet);
590         currSharedSet.addWrite(locTupleKey, currSet);
591       }
592       currSet.retainAll(inSet);
593     }
594
595   }
596
597   private void computeSharedCoverSet() {
598     LinkedList<MethodDescriptor> descriptorListToAnalyze =
599         (LinkedList<MethodDescriptor>) sortedDescriptors.clone();
600
601     // current descriptors to visit in fixed-point interprocedural analysis,
602     // prioritized by
603     // dependency in the call graph
604     methodDescriptorsToVisitStack.clear();
605
606     descriptorListToAnalyze.removeFirst();
607
608     Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
609     methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
610
611     while (!descriptorListToAnalyze.isEmpty()) {
612       MethodDescriptor md = descriptorListToAnalyze.removeFirst();
613       methodDescriptorsToVisitStack.add(md);
614     }
615
616     // analyze scheduled methods until there are no more to visit
617     while (!methodDescriptorsToVisitStack.isEmpty()) {
618       MethodDescriptor md = methodDescriptorsToVisitStack.pop();
619       FlatMethod fm = state.getMethodFlat(md);
620       computeSharedCoverSet_analyzeMethod(fm, md.equals(methodContainingSSJavaLoop));
621     }
622
623     computeSharedCoverSetForEventLoop();
624
625   }
626
627   private void computeSharedCoverSetForEventLoop() {
628     computeSharedCoverSet_analyzeMethod(state.getMethodFlat(methodContainingSSJavaLoop), true);
629   }
630
631   private void computeSharedCoverSet_analyzeMethod(FlatMethod fm, boolean onlyVisitSSJavaLoop) {
632
633     System.out.println("\n###");
634     System.out.println("computeSharedCoverSet_analyzeMethod=" + fm);
635     MethodDescriptor md = fm.getMethod();
636
637     Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
638
639     Set<FlatNode> visited = new HashSet<FlatNode>();
640
641     if (onlyVisitSSJavaLoop) {
642       flatNodesToVisit.add(ssjava.getSSJavaLoopEntrance());
643     } else {
644       flatNodesToVisit.add(fm);
645     }
646
647     while (!flatNodesToVisit.isEmpty()) {
648       FlatNode fn = flatNodesToVisit.iterator().next();
649       flatNodesToVisit.remove(fn);
650       visited.add(fn);
651
652       computeSharedCoverSet_nodeActions(md, fn, onlyVisitSSJavaLoop);
653
654       for (int i = 0; i < fn.numNext(); i++) {
655         FlatNode nn = fn.getNext(i);
656
657         if (!visited.contains(nn)) {
658           if (!onlyVisitSSJavaLoop || (onlyVisitSSJavaLoop && loopIncElements.contains(nn))) {
659             flatNodesToVisit.add(nn);
660           }
661         }
662
663       }
664
665     }
666
667   }
668
669   private void computeSharedCoverSet_nodeActions(MethodDescriptor md, FlatNode fn,
670       boolean isEventLoopBody) {
671     TempDescriptor lhs;
672     TempDescriptor rhs;
673     FieldDescriptor fld;
674
675     switch (fn.kind()) {
676
677     case FKind.FlatLiteralNode: {
678       FlatLiteralNode fln = (FlatLiteralNode) fn;
679       lhs = fln.getDst();
680
681       NTuple<Location> lhsLocTuple = new NTuple<Location>();
682       lhsLocTuple.add(Location.createTopLocation(md));
683       mapDescriptorToLocationPath.put(lhs, lhsLocTuple);
684
685       if (lhs.getType().isPrimitive() && !lhs.getSymbol().startsWith("neverused")
686           && !lhs.getSymbol().startsWith("srctmp")) {
687         // only need to care about composite location case here
688         if (lhs.getType().getExtension() instanceof SSJavaType) {
689           CompositeLocation compLoc = ((SSJavaType) lhs.getType().getExtension()).getCompLoc();
690           Location lastLocElement = compLoc.get(compLoc.getSize() - 1);
691         }
692       }
693
694     }
695       break;
696
697     case FKind.FlatOpNode: {
698       FlatOpNode fon = (FlatOpNode) fn;
699       // for a normal assign node, need to propagate lhs's location path to
700       // rhs
701       if (fon.getOp().getOp() == Operation.ASSIGN) {
702         rhs = fon.getLeft();
703         lhs = fon.getDest();
704
705         if (!lhs.getSymbol().startsWith("neverused") && !lhs.getSymbol().startsWith("leftop")
706             && !lhs.getSymbol().startsWith("rightop")) {
707
708           NTuple<Location> rhsLocTuple = new NTuple<Location>();
709           NTuple<Location> lhsLocTuple = new NTuple<Location>();
710           if (mapDescriptorToLocationPath.containsKey(rhs)) {
711             mapDescriptorToLocationPath.put(lhs, deriveLocationTuple(md, rhs));
712             lhsLocTuple = mapDescriptorToLocationPath.get(lhs);
713           } else {
714             // rhs side
715             if (rhs.getType().getExtension() != null
716                 && rhs.getType().getExtension() instanceof SSJavaType) {
717
718               if (((SSJavaType) rhs.getType().getExtension()).getCompLoc() != null) {
719                 rhsLocTuple.addAll(((SSJavaType) rhs.getType().getExtension()).getCompLoc()
720                     .getTuple());
721               }
722
723             } else {
724               NTuple<Location> locTuple = deriveLocationTuple(md, rhs);
725               if (locTuple != null) {
726                 rhsLocTuple.addAll(locTuple);
727               }
728             }
729             if (rhsLocTuple.size() > 0) {
730               mapDescriptorToLocationPath.put(rhs, rhsLocTuple);
731             }
732
733             // lhs side
734             if (lhs.getType().getExtension() != null
735                 && lhs.getType().getExtension() instanceof SSJavaType) {
736               lhsLocTuple.addAll(((SSJavaType) lhs.getType().getExtension()).getCompLoc()
737                   .getTuple());
738               mapDescriptorToLocationPath.put(lhs, lhsLocTuple);
739             } else if (mapDescriptorToLocationPath.get(rhs) != null) {
740               // propagate rhs's location to lhs
741               lhsLocTuple.addAll(mapDescriptorToLocationPath.get(rhs));
742               mapDescriptorToLocationPath.put(lhs, lhsLocTuple);
743             }
744           }
745
746           if (isEventLoopBody && lhs.getType().isPrimitive()
747               && !lhs.getSymbol().startsWith("srctmp")) {
748
749             NTuple<Descriptor> lhsHeapPath = computePath(lhs);
750
751             if (lhsLocTuple != null) {
752               addMayWrittenSet(md, lhsLocTuple, lhsHeapPath);
753             }
754
755           }
756         }
757
758       }
759     }
760       break;
761
762     case FKind.FlatSetFieldNode:
763     case FKind.FlatSetElementNode: {
764
765       // x.f=y;
766
767       if (fn.kind() == FKind.FlatSetFieldNode) {
768         FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
769         lhs = fsfn.getDst();
770         fld = fsfn.getField();
771         rhs = fsfn.getSrc();
772       } else {
773         FlatSetElementNode fsen = (FlatSetElementNode) fn;
774         lhs = fsen.getDst();
775         rhs = fsen.getSrc();
776         TypeDescriptor td = lhs.getType().dereference();
777         fld = getArrayField(td);
778       }
779
780       NTuple<Location> fieldLocTuple = new NTuple<Location>();
781       fieldLocTuple.addAll(deriveLocationTuple(md, lhs));
782       if (fn.kind() == FKind.FlatSetFieldNode) {
783         fieldLocTuple.add((Location) fld.getType().getExtension());
784       }
785
786       if (mapHeapPath.containsKey(lhs)) {
787         // fields reachable from the param can have heap path entry.
788         NTuple<Descriptor> lhsHeapPath = new NTuple<Descriptor>();
789         lhsHeapPath.addAll(mapHeapPath.get(lhs));
790
791         Location fieldLocation;
792         if (fn.kind() == FKind.FlatSetFieldNode) {
793           fieldLocation = getLocation(fld);
794         } else {
795           fieldLocation = getLocation(lhsHeapPath.get(getArrayBaseDescriptorIdx(lhsHeapPath)));
796         }
797
798         // Location fieldLocation = getLocation(lhs);
799         if (!isEventLoopBody && fieldLocation.getDescriptor().equals(md)) {
800           // if the field belongs to the local lattice, no reason to calculate
801           // shared location
802           break;
803         }
804
805         if (ssjava.isSharedLocation(fieldLocation)) {
806
807           NTuple<Descriptor> fieldHeapPath = new NTuple<Descriptor>();
808           fieldHeapPath.addAll(computePath(lhs));
809           if (fn.kind() == FKind.FlatSetFieldNode) {
810             fieldHeapPath.add(fld);
811           }
812
813           addMayWrittenSet(md, fieldLocTuple, fieldHeapPath);
814
815         }
816       }
817
818     }
819       break;
820
821     case FKind.FlatElementNode:
822     case FKind.FlatFieldNode: {
823
824       // x=y.f;
825
826       if (fn.kind() == FKind.FlatFieldNode) {
827         FlatFieldNode ffn = (FlatFieldNode) fn;
828         lhs = ffn.getDst();
829         rhs = ffn.getSrc();
830         fld = ffn.getField();
831       } else {
832         FlatElementNode fen = (FlatElementNode) fn;
833         lhs = fen.getDst();
834         rhs = fen.getSrc();
835         TypeDescriptor td = rhs.getType().dereference();
836         fld = getArrayField(td);
837       }
838
839       NTuple<Location> locTuple = new NTuple<Location>();
840
841       if (fld.isStatic()) {
842
843         if (fld.isFinal()) {
844           // in this case, fld has TOP location
845           Location topLocation = Location.createTopLocation(md);
846           locTuple.add(topLocation);
847         } else {
848           locTuple.addAll(deriveGlobalLocationTuple(md));
849           if (fn.kind() == FKind.FlatFieldNode) {
850             locTuple.add((Location) fld.getType().getExtension());
851           }
852         }
853
854       } else {
855         locTuple.addAll(deriveLocationTuple(md, rhs));
856         if (fn.kind() == FKind.FlatFieldNode) {
857           locTuple.add((Location) fld.getType().getExtension());
858         }
859       }
860
861       mapDescriptorToLocationPath.put(lhs, locTuple);
862
863     }
864       break;
865
866     case FKind.FlatCall: {
867
868       FlatCall fc = (FlatCall) fn;
869
870       bindLocationPathCallerArgWithCalleeParam(md, fc);
871
872     }
873       break;
874
875     case FKind.FlatNew: {
876
877       FlatNew fnew = (FlatNew) fn;
878       TempDescriptor dst = fnew.getDst();
879       NTuple<Location> locTuple = deriveLocationTuple(md, dst);
880
881       if (locTuple != null) {
882         NTuple<Location> dstLocTuple = new NTuple<Location>();
883         dstLocTuple.addAll(locTuple);
884         mapDescriptorToLocationPath.put(dst, dstLocTuple);
885       }
886
887     }
888       break;
889     }
890   }
891
892   private void addMayWrittenSet(MethodDescriptor md, NTuple<Location> locTuple,
893       NTuple<Descriptor> heapPath) {
894
895     MultiSourceMap<NTuple<Location>, NTuple<Descriptor>> map = mapMethodToSharedLocCoverSet.get(md);
896     if (map == null) {
897       map = new MultiSourceMap<NTuple<Location>, NTuple<Descriptor>>();
898       mapMethodToSharedLocCoverSet.put(md, map);
899     }
900
901     Set<NTuple<Descriptor>> writeSet = map.get(locTuple);
902     if (writeSet == null) {
903       writeSet = new HashSet<NTuple<Descriptor>>();
904       map.put(locTuple, writeSet);
905     }
906     writeSet.add(heapPath);
907
908   }
909
910   private void bindLocationPathCallerArgWithCalleeParam(MethodDescriptor mdCaller, FlatCall fc) {
911
912     if (ssjava.isSSJavaUtil(fc.getMethod().getClassDesc())) {
913       // ssjava util case!
914       // have write effects on the first argument
915       TempDescriptor arg = fc.getArg(0);
916       NTuple<Location> argLocationPath = deriveLocationTuple(mdCaller, arg);
917       NTuple<Descriptor> argHeapPath = computePath(arg);
918       addMayWrittenSet(mdCaller, argLocationPath, argHeapPath);
919     } else if (ssjava.needTobeAnnotated(fc.getMethod())) {
920
921       // if arg is not primitive type, we need to propagate maywritten set to
922       // the caller's location path
923
924       MethodDescriptor mdCallee = fc.getMethod();
925       Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
926       setPossibleCallees.addAll(callGraph.getMethods(mdCallee));
927
928       // create mapping from arg idx to its heap paths
929       Hashtable<Integer, NTuple<Descriptor>> mapArgIdx2CallerArgHeapPath =
930           new Hashtable<Integer, NTuple<Descriptor>>();
931
932       // create mapping from arg idx to its location paths
933       Hashtable<Integer, NTuple<Location>> mapArgIdx2CallerArgLocationPath =
934           new Hashtable<Integer, NTuple<Location>>();
935
936       if (fc.getThis() != null) {
937
938         if (mapHeapPath.containsKey(fc.getThis())) {
939
940           // setup heap path for 'this'
941           NTuple<Descriptor> thisHeapPath = new NTuple<Descriptor>();
942           thisHeapPath.addAll(mapHeapPath.get(fc.getThis()));
943           mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(0), thisHeapPath);
944
945           // setup location path for 'this'
946           NTuple<Location> thisLocationPath = deriveLocationTuple(mdCaller, fc.getThis());
947           mapArgIdx2CallerArgLocationPath.put(Integer.valueOf(0), thisLocationPath);
948
949         }
950       }
951
952       for (int i = 0; i < fc.numArgs(); i++) {
953         TempDescriptor arg = fc.getArg(i);
954         // create mapping arg to loc path
955
956         if (mapHeapPath.containsKey(arg)) {
957           // setup heap path
958           NTuple<Descriptor> argHeapPath = mapHeapPath.get(arg);
959           mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(i + 1), argHeapPath);
960           // setup loc path
961           NTuple<Location> argLocationPath = deriveLocationTuple(mdCaller, arg);
962           mapArgIdx2CallerArgLocationPath.put(Integer.valueOf(i + 1), argLocationPath);
963         }
964
965       }
966
967       for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
968         MethodDescriptor callee = (MethodDescriptor) iterator.next();
969         FlatMethod calleeFlatMethod = state.getMethodFlat(callee);
970
971         // binding caller's args and callee's params
972
973         Hashtable<NTuple<Descriptor>, NTuple<Descriptor>> mapParamHeapPathToCallerArgHeapPath =
974             new Hashtable<NTuple<Descriptor>, NTuple<Descriptor>>();
975
976         Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc =
977             new Hashtable<Integer, TempDescriptor>();
978         int offset = 0;
979         if (calleeFlatMethod.getMethod().isStatic()) {
980           // static method does not have implicit 'this' arg
981           offset = 1;
982         }
983
984         for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
985           TempDescriptor param = calleeFlatMethod.getParameter(i);
986           mapParamIdx2ParamTempDesc.put(Integer.valueOf(i + offset), param);
987
988           NTuple<Descriptor> calleeHeapPath = computePath(param);
989
990           NTuple<Descriptor> argHeapPath =
991               mapArgIdx2CallerArgHeapPath.get(Integer.valueOf(i + offset));
992
993           if (argHeapPath != null) {
994             mapParamHeapPathToCallerArgHeapPath.put(calleeHeapPath, argHeapPath);
995
996           }
997
998         }
999
1000         Set<Integer> keySet = mapArgIdx2CallerArgLocationPath.keySet();
1001         for (Iterator iterator2 = keySet.iterator(); iterator2.hasNext();) {
1002           Integer idx = (Integer) iterator2.next();
1003
1004           NTuple<Location> callerArgLocationPath = mapArgIdx2CallerArgLocationPath.get(idx);
1005
1006           TempDescriptor calleeParam = mapParamIdx2ParamTempDesc.get(idx);
1007           NTuple<Location> calleeLocationPath = deriveLocationTuple(mdCallee, calleeParam);
1008
1009           NTuple<Descriptor> callerArgHeapPath = mapArgIdx2CallerArgHeapPath.get(idx);
1010           NTuple<Descriptor> calleeHeapPath = computePath(calleeParam);
1011
1012           if (!calleeParam.getType().isPrimitive()) {
1013             createNewMappingOfMayWrittenSet(mdCaller, callee, callerArgHeapPath,
1014                 callerArgLocationPath, calleeHeapPath, calleeLocationPath,
1015                 mapParamHeapPathToCallerArgHeapPath);
1016           }
1017         }
1018
1019       }
1020
1021     }
1022
1023   }
1024
1025   private Hashtable<NTuple<Location>, Set<NTuple<Descriptor>>> getMappingByStartedWith(
1026       MultiSourceMap<NTuple<Location>, NTuple<Descriptor>> map, NTuple<Location> in) {
1027
1028     Hashtable<NTuple<Location>, Set<NTuple<Descriptor>>> matchedMapping =
1029         new Hashtable<NTuple<Location>, Set<NTuple<Descriptor>>>();
1030
1031     Set<NTuple<Location>> keySet = map.keySet();
1032
1033     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1034       NTuple<Location> key = (NTuple<Location>) iterator.next();
1035       if (key.startsWith(in)) {
1036         matchedMapping.put(key, map.get(key));
1037       }
1038     }
1039
1040     return matchedMapping;
1041
1042   }
1043
1044   private void createNewMappingOfMayWrittenSet(MethodDescriptor caller, MethodDescriptor callee,
1045       NTuple<Descriptor> callerArgHeapPath, NTuple<Location> callerArgLocPath,
1046       NTuple<Descriptor> calleeParamHeapPath, NTuple<Location> calleeParamLocPath,
1047       Hashtable<NTuple<Descriptor>, NTuple<Descriptor>> mapParamHeapPathToCallerArgHeapPath) {
1048
1049     // propagate may-written-set associated with the key that is started with
1050     // calleepath to the caller
1051     // 1) makes a new key by combining caller path and callee path(except local
1052     // loc element of param)
1053     // 2) create new mapping of may-written-set of callee path to caller path
1054
1055     // extract all may written effect accessed through callee param path
1056     MultiSourceMap<NTuple<Location>, NTuple<Descriptor>> calleeMapping =
1057         mapMethodToSharedLocCoverSet.get(callee);
1058
1059     if (calleeMapping == null) {
1060       return;
1061     }
1062
1063     MultiSourceMap<NTuple<Location>, NTuple<Descriptor>> callerMapping =
1064         mapMethodToSharedLocCoverSet.get(caller);
1065
1066     if (callerMapping == null) {
1067       callerMapping = new MultiSourceMap<NTuple<Location>, NTuple<Descriptor>>();
1068       mapMethodToSharedLocCoverSet.put(caller, callerMapping);
1069     }
1070
1071     Hashtable<NTuple<Location>, Set<NTuple<Descriptor>>> paramMapping =
1072         getMappingByStartedWith(calleeMapping, calleeParamLocPath);
1073
1074     Set<NTuple<Location>> calleeKeySet = paramMapping.keySet();
1075
1076     for (Iterator iterator = calleeKeySet.iterator(); iterator.hasNext();) {
1077       NTuple<Location> calleeKey = (NTuple<Location>) iterator.next();
1078
1079       Set<NTuple<Descriptor>> calleeMayWriteSet = paramMapping.get(calleeKey);
1080
1081       if (calleeMayWriteSet != null) {
1082
1083         Set<NTuple<Descriptor>> boundMayWriteSet = new HashSet<NTuple<Descriptor>>();
1084
1085         Set<NTuple<Descriptor>> boundSet =
1086             convertToCallerMayWriteSet(calleeParamHeapPath, calleeMayWriteSet, callerMapping,
1087                 mapParamHeapPathToCallerArgHeapPath);
1088
1089         boundMayWriteSet.addAll(boundSet);
1090
1091         NTuple<Location> newKey = new NTuple<Location>();
1092         newKey.addAll(callerArgLocPath);
1093         // need to replace the local location with the caller's path so skip the
1094         // local location of the parameter
1095         for (int i = 1; i < calleeKey.size(); i++) {
1096           newKey.add(calleeKey.get(i));
1097         }
1098
1099         callerMapping.union(newKey, boundMayWriteSet);
1100       }
1101
1102     }
1103
1104   }
1105
1106   private Set<NTuple<Descriptor>> convertToCallerMayWriteSet(
1107       NTuple<Descriptor> calleeParamHeapPath, Set<NTuple<Descriptor>> calleeMayWriteSet,
1108       MultiSourceMap<NTuple<Location>, NTuple<Descriptor>> callerMapping,
1109       Hashtable<NTuple<Descriptor>, NTuple<Descriptor>> mapParamHeapPathToCallerArgHeapPath) {
1110
1111     Set<NTuple<Descriptor>> boundSet = new HashSet<NTuple<Descriptor>>();
1112
1113     // replace callee's param path with caller's arg path
1114     for (Iterator iterator = calleeMayWriteSet.iterator(); iterator.hasNext();) {
1115       NTuple<Descriptor> calleeWriteHeapPath = (NTuple<Descriptor>) iterator.next();
1116
1117       NTuple<Descriptor> writeHeapPathParamHeapPath = calleeWriteHeapPath.subList(0, 1);
1118
1119       NTuple<Descriptor> callerArgHeapPath =
1120           mapParamHeapPathToCallerArgHeapPath.get(writeHeapPathParamHeapPath);
1121
1122       NTuple<Descriptor> boundHeapPath = new NTuple<Descriptor>();
1123       boundHeapPath.addAll(callerArgHeapPath);
1124
1125       for (int i = 1; i < calleeWriteHeapPath.size(); i++) {
1126         boundHeapPath.add(calleeWriteHeapPath.get(i));
1127       }
1128
1129       boundSet.add(boundHeapPath);
1130
1131     }
1132
1133     return boundSet;
1134   }
1135
1136   private Location getLocation(Descriptor d) {
1137
1138     if (d instanceof FieldDescriptor) {
1139       TypeExtension te = ((FieldDescriptor) d).getType().getExtension();
1140       if (te != null) {
1141         return (Location) te;
1142       }
1143     } else {
1144       assert d instanceof TempDescriptor;
1145       TempDescriptor td = (TempDescriptor) d;
1146
1147       TypeExtension te = td.getType().getExtension();
1148       if (te != null) {
1149         if (te instanceof SSJavaType) {
1150           SSJavaType ssType = (SSJavaType) te;
1151           if (ssType.getCompLoc() != null) {
1152             CompositeLocation comp = ssType.getCompLoc();
1153             return comp.get(comp.getSize() - 1);
1154           } else {
1155             return null;
1156           }
1157         } else {
1158           return (Location) te;
1159         }
1160       }
1161     }
1162
1163     return mapDescToLocation.get(d);
1164   }
1165
1166   private void eventLoopAnalysis() {
1167     // perform second stage analysis: intraprocedural analysis ensure that
1168     // all
1169     // variables are definitely written in-between the same read
1170
1171     Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
1172     flatNodesToVisit.add(ssjava.getSSJavaLoopEntrance());
1173
1174     while (!flatNodesToVisit.isEmpty()) {
1175       FlatNode fn = (FlatNode) flatNodesToVisit.iterator().next();
1176       flatNodesToVisit.remove(fn);
1177
1178       Hashtable<NTuple<Descriptor>, Set<WriteAge>> prev = mapFlatNodetoEventLoopMap.get(fn);
1179
1180       Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr =
1181           new Hashtable<NTuple<Descriptor>, Set<WriteAge>>();
1182       for (int i = 0; i < fn.numPrev(); i++) {
1183         FlatNode nn = fn.getPrev(i);
1184         Hashtable<NTuple<Descriptor>, Set<WriteAge>> in = mapFlatNodetoEventLoopMap.get(nn);
1185         if (in != null) {
1186           union(curr, in);
1187         }
1188       }
1189
1190       eventLoopAnalysis_nodeAction(fn, curr, ssjava.getSSJavaLoopEntrance());
1191
1192       // if a new result, schedule forward nodes for analysis
1193       if (!curr.equals(prev)) {
1194         mapFlatNodetoEventLoopMap.put(fn, curr);
1195
1196         for (int i = 0; i < fn.numNext(); i++) {
1197           FlatNode nn = fn.getNext(i);
1198           if (loopIncElements.contains(nn)) {
1199             flatNodesToVisit.add(nn);
1200           }
1201
1202         }
1203       }
1204     }
1205   }
1206
1207   private void union(Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr,
1208       Hashtable<NTuple<Descriptor>, Set<WriteAge>> in) {
1209
1210     Set<NTuple<Descriptor>> inKeySet = in.keySet();
1211     for (Iterator iterator = inKeySet.iterator(); iterator.hasNext();) {
1212       NTuple<Descriptor> inKey = (NTuple<Descriptor>) iterator.next();
1213       Set<WriteAge> inSet = in.get(inKey);
1214
1215       Set<WriteAge> currSet = curr.get(inKey);
1216
1217       if (currSet == null) {
1218         currSet = new HashSet<WriteAge>();
1219         curr.put(inKey, currSet);
1220       }
1221       currSet.addAll(inSet);
1222     }
1223
1224   }
1225
1226   private void eventLoopAnalysis_nodeAction(FlatNode fn,
1227       Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr, FlatNode loopEntrance) {
1228
1229     Hashtable<NTuple<Descriptor>, Set<WriteAge>> readWriteKillSet =
1230         new Hashtable<NTuple<Descriptor>, Set<WriteAge>>();
1231     Hashtable<NTuple<Descriptor>, Set<WriteAge>> readWriteGenSet =
1232         new Hashtable<NTuple<Descriptor>, Set<WriteAge>>();
1233
1234     if (fn.equals(loopEntrance)) {
1235       // it reaches loop entrance: changes all flag to true
1236       Set<NTuple<Descriptor>> keySet = curr.keySet();
1237       for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1238         NTuple<Descriptor> key = (NTuple<Descriptor>) iterator.next();
1239         Set<WriteAge> writeAgeSet = curr.get(key);
1240
1241         Set<WriteAge> incSet = new HashSet<WriteAge>();
1242         incSet.addAll(writeAgeSet);
1243         writeAgeSet.clear();
1244
1245         for (Iterator iterator2 = incSet.iterator(); iterator2.hasNext();) {
1246           WriteAge writeAge = (WriteAge) iterator2.next();
1247           WriteAge newWriteAge = writeAge.copy();
1248           newWriteAge.inc();
1249           writeAgeSet.add(newWriteAge);
1250         }
1251
1252       }
1253
1254     } else {
1255       TempDescriptor lhs;
1256       TempDescriptor rhs;
1257       FieldDescriptor fld;
1258
1259       switch (fn.kind()) {
1260
1261       case FKind.FlatOpNode: {
1262         FlatOpNode fon = (FlatOpNode) fn;
1263         lhs = fon.getDest();
1264         rhs = fon.getLeft();
1265
1266         if (fon.getOp().getOp() == Operation.ASSIGN) {
1267
1268           if (!lhs.getSymbol().startsWith("neverused") && !lhs.getSymbol().startsWith("leftop")
1269               && !lhs.getSymbol().startsWith("rightop")) {
1270
1271             boolean hasWriteEffect = false;
1272
1273             if (rhs.getType().getExtension() instanceof SSJavaType
1274                 && lhs.getType().getExtension() instanceof SSJavaType) {
1275
1276               CompositeLocation rhsCompLoc =
1277                   ((SSJavaType) rhs.getType().getExtension()).getCompLoc();
1278
1279               CompositeLocation lhsCompLoc =
1280                   ((SSJavaType) lhs.getType().getExtension()).getCompLoc();
1281
1282               if (lhsCompLoc != rhsCompLoc) {
1283                 // have a write effect!
1284                 hasWriteEffect = true;
1285               }
1286
1287             } else if (lhs.getType().isImmutable()) {
1288               hasWriteEffect = true;
1289             }
1290
1291             if (hasWriteEffect) {
1292               // write(lhs)
1293               NTuple<Descriptor> rhsHeapPath = computePath(rhs);
1294               NTuple<Descriptor> lhsHeapPath = new NTuple<Descriptor>();
1295               lhsHeapPath.addAll(rhsHeapPath);
1296
1297               Location lhsLoc = getLocation(lhs);
1298               if (ssjava.isSharedLocation(lhsLoc)) {
1299
1300                 NTuple<Descriptor> varHeapPath = computePath(lhs);
1301                 NTuple<Location> varLocTuple = mapDescriptorToLocationPath.get(lhs);
1302
1303                 Set<NTuple<Descriptor>> writtenSet =
1304                     mapFlatNodeToSharedLocMapping.get(fn).get(varLocTuple);
1305
1306                 if (isCovered(varLocTuple, writtenSet)) {
1307                   computeKILLSetForSharedWrite(curr, writtenSet, readWriteKillSet);
1308                   computeGENSetForSharedAllCoverWrite(curr, writtenSet, readWriteGenSet);
1309                 } else {
1310                   computeGENSetForSharedNonCoverWrite(curr, varHeapPath, readWriteGenSet);
1311                 }
1312
1313               } else {
1314
1315                 computeKILLSetForWrite(curr, lhsHeapPath, readWriteKillSet);
1316                 computeGENSetForWrite(lhsHeapPath, readWriteGenSet);
1317               }
1318
1319               // System.out.println("write effect on =" + lhsHeapPath);
1320               // System.out.println("#KILLSET=" + readWriteKillSet);
1321               // System.out.println("#GENSet=" + readWriteGenSet + "\n");
1322
1323               Set<WriteAge> writeAgeSet = curr.get(lhsHeapPath);
1324               checkWriteAgeSet(writeAgeSet, lhsHeapPath, fn);
1325             }
1326
1327           }
1328
1329         }
1330
1331       }
1332         break;
1333
1334       case FKind.FlatFieldNode:
1335       case FKind.FlatElementNode: {
1336
1337         if (fn.kind() == FKind.FlatFieldNode) {
1338           FlatFieldNode ffn = (FlatFieldNode) fn;
1339           lhs = ffn.getDst();
1340           rhs = ffn.getSrc();
1341           fld = ffn.getField();
1342         } else {
1343           FlatElementNode fen = (FlatElementNode) fn;
1344           lhs = fen.getDst();
1345           rhs = fen.getSrc();
1346           TypeDescriptor td = rhs.getType().dereference();
1347           fld = getArrayField(td);
1348         }
1349
1350         // read field
1351         NTuple<Descriptor> srcHeapPath = mapHeapPath.get(rhs);
1352         NTuple<Descriptor> fldHeapPath;
1353         if (srcHeapPath != null) {
1354           fldHeapPath = new NTuple<Descriptor>(srcHeapPath.getList());
1355         } else {
1356           // if srcHeapPath is null, it is static reference
1357           fldHeapPath = new NTuple<Descriptor>();
1358           fldHeapPath.add(rhs);
1359         }
1360         fldHeapPath.add(fld);
1361
1362         Set<WriteAge> writeAgeSet = curr.get(fldHeapPath);
1363
1364         checkWriteAgeSet(writeAgeSet, fldHeapPath, fn);
1365
1366       }
1367         break;
1368
1369       case FKind.FlatSetFieldNode:
1370       case FKind.FlatSetElementNode: {
1371
1372         if (fn.kind() == FKind.FlatSetFieldNode) {
1373           FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
1374           lhs = fsfn.getDst();
1375           fld = fsfn.getField();
1376         } else {
1377           FlatSetElementNode fsen = (FlatSetElementNode) fn;
1378           lhs = fsen.getDst();
1379           rhs = fsen.getSrc();
1380           TypeDescriptor td = lhs.getType().dereference();
1381           fld = getArrayField(td);
1382         }
1383
1384         // write(field)
1385         NTuple<Descriptor> lhsHeapPath = computePath(lhs);
1386         NTuple<Descriptor> fldHeapPath = new NTuple<Descriptor>(lhsHeapPath.getList());
1387         if (fn.kind() == FKind.FlatSetFieldNode) {
1388           fldHeapPath.add(fld);
1389         }
1390
1391         // shared loc extension
1392         Location fieldLoc;
1393         if (fn.kind() == FKind.FlatSetFieldNode) {
1394           fieldLoc = (Location) fld.getType().getExtension();
1395         } else {
1396           NTuple<Location> locTuple = mapDescriptorToLocationPath.get(lhs);
1397           fieldLoc = locTuple.get(locTuple.size() - 1);
1398         }
1399
1400         if (ssjava.isSharedLocation(fieldLoc)) {
1401
1402           NTuple<Location> fieldLocTuple = new NTuple<Location>();
1403           fieldLocTuple.addAll(mapDescriptorToLocationPath.get(lhs));
1404           if (fn.kind() == FKind.FlatSetFieldNode) {
1405             fieldLocTuple.add(fieldLoc);
1406           }
1407
1408           Set<NTuple<Descriptor>> writtenSet =
1409               mapFlatNodeToSharedLocMapping.get(fn).get(fieldLocTuple);
1410
1411           if (isCovered(fieldLocTuple, writtenSet)) {
1412             computeKILLSetForSharedWrite(curr, writtenSet, readWriteKillSet);
1413             computeGENSetForSharedAllCoverWrite(curr, writtenSet, readWriteGenSet);
1414           } else {
1415             computeGENSetForSharedNonCoverWrite(curr, fldHeapPath, readWriteGenSet);
1416           }
1417
1418         } else {
1419           computeKILLSetForWrite(curr, fldHeapPath, readWriteKillSet);
1420           computeGENSetForWrite(fldHeapPath, readWriteGenSet);
1421         }
1422
1423         // System.out.println("KILLSET=" + readWriteKillSet);
1424         // System.out.println("GENSet=" + readWriteGenSet);
1425
1426       }
1427         break;
1428
1429       case FKind.FlatCall: {
1430         FlatCall fc = (FlatCall) fn;
1431
1432         SharedLocMap sharedLocMap = mapFlatNodeToSharedLocMapping.get(fc);
1433         // System.out.println("FLATCALL:" + fn);
1434         generateKILLSetForFlatCall(fc, curr, sharedLocMap, readWriteKillSet);
1435         generateGENSetForFlatCall(fc, sharedLocMap, readWriteGenSet);
1436
1437         // System.out.println("KILLSET=" + readWriteKillSet);
1438         // System.out.println("GENSet=" + readWriteGenSet);
1439
1440         checkManyRead(fc, curr);
1441       }
1442         break;
1443
1444       }
1445
1446       computeNewMapping(curr, readWriteKillSet, readWriteGenSet);
1447       // System.out.println("#######" + curr);
1448
1449     }
1450
1451   }
1452
1453   private void computeGENSetForSharedNonCoverWrite(
1454       Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr, NTuple<Descriptor> heapPath,
1455       Hashtable<NTuple<Descriptor>, Set<WriteAge>> genSet) {
1456
1457     Set<WriteAge> writeAgeSet = genSet.get(heapPath);
1458     if (writeAgeSet == null) {
1459       writeAgeSet = new HashSet<WriteAge>();
1460       genSet.put(heapPath, writeAgeSet);
1461     }
1462
1463     writeAgeSet.add(new WriteAge(1));
1464
1465   }
1466
1467   private void computeGENSetForSharedAllCoverWrite(
1468       Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr, Set<NTuple<Descriptor>> writtenSet,
1469       Hashtable<NTuple<Descriptor>, Set<WriteAge>> genSet) {
1470
1471     for (Iterator iterator = writtenSet.iterator(); iterator.hasNext();) {
1472       NTuple<Descriptor> writeHeapPath = (NTuple<Descriptor>) iterator.next();
1473
1474       Set<WriteAge> writeAgeSet = new HashSet<WriteAge>();
1475       writeAgeSet.add(new WriteAge(0));
1476
1477       genSet.put(writeHeapPath, writeAgeSet);
1478     }
1479
1480   }
1481
1482   private void computeKILLSetForSharedWrite(Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr,
1483       Set<NTuple<Descriptor>> writtenSet, Hashtable<NTuple<Descriptor>, Set<WriteAge>> killSet) {
1484
1485     for (Iterator iterator = writtenSet.iterator(); iterator.hasNext();) {
1486       NTuple<Descriptor> writeHeapPath = (NTuple<Descriptor>) iterator.next();
1487       Set<WriteAge> writeSet = curr.get(writeHeapPath);
1488       if (writeSet != null) {
1489         killSet.put(writeHeapPath, writeSet);
1490       }
1491     }
1492
1493   }
1494
1495   private boolean isCovered(NTuple<Location> locTuple, Set<NTuple<Descriptor>> inSet) {
1496
1497     if (inSet == null) {
1498       return false;
1499     }
1500
1501     Set<NTuple<Descriptor>> coverSet =
1502         mapMethodToSharedLocCoverSet.get(methodContainingSSJavaLoop).get(locTuple);
1503
1504     return inSet.containsAll(coverSet);
1505   }
1506
1507   private void checkManyRead(FlatCall fc, Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr) {
1508
1509     Set<NTuple<Descriptor>> boundReadSet = mapFlatNodeToBoundReadSet.get(fc);
1510
1511     for (Iterator iterator = boundReadSet.iterator(); iterator.hasNext();) {
1512       NTuple<Descriptor> readHeapPath = (NTuple<Descriptor>) iterator.next();
1513       Set<WriteAge> writeAgeSet = curr.get(readHeapPath);
1514       checkWriteAgeSet(writeAgeSet, readHeapPath, fc);
1515     }
1516
1517   }
1518
1519   private void checkWriteAgeSet(Set<WriteAge> writeAgeSet, NTuple<Descriptor> path, FlatNode fn) {
1520
1521     // System.out.println("# CHECK WRITE AGE of " + path + " from set=" +
1522     // writeAgeSet);
1523
1524     if (writeAgeSet != null) {
1525       for (Iterator iterator = writeAgeSet.iterator(); iterator.hasNext();) {
1526         WriteAge writeAge = (WriteAge) iterator.next();
1527         if (writeAge.getAge() > MAXAGE) {
1528           generateErrorMessage(path, fn);
1529         }
1530       }
1531     }
1532   }
1533
1534   private void generateErrorMessage(NTuple<Descriptor> path, FlatNode fn) {
1535
1536     Descriptor lastDesc = path.get(getArrayBaseDescriptorIdx(path));
1537     if (ssjava.isSharedLocation(getLocation(lastDesc))) {
1538
1539       NTuple<Location> locPathTuple = getLocationTuple(path);
1540       Set<NTuple<Descriptor>> coverSet =
1541           mapMethodToSharedLocCoverSet.get(methodContainingSSJavaLoop).get(locPathTuple);
1542       throw new Error("Shared memory locations, which is reachable through references " + path
1543           + ", are not completely overwritten by the higher values at "
1544           + methodContainingSSJavaLoop.getClassDesc().getSourceFileName() + "::" + fn.getNumLine()
1545           + ".\nThe following memory locations belong to the same shared locations:" + coverSet);
1546
1547     } else {
1548       throw new Error(
1549           "Memory location, which is reachable through references "
1550               + path
1551               + ", who comes back to the same read statement without being overwritten at the out-most iteration at "
1552               + methodContainingSSJavaLoop.getClassDesc().getSourceFileName() + "::"
1553               + fn.getNumLine());
1554     }
1555
1556   }
1557
1558   private void generateGENSetForFlatCall(FlatCall fc, SharedLocMap sharedLocMap,
1559       Hashtable<NTuple<Descriptor>, Set<WriteAge>> GENSet) {
1560
1561     Set<NTuple<Descriptor>> boundMayWriteSet = mapFlatNodeToBoundMayWriteSet.get(fc);
1562     // System.out.println("boundMayWriteSet=" + boundMayWriteSet);
1563
1564     for (Iterator iterator = boundMayWriteSet.iterator(); iterator.hasNext();) {
1565       NTuple<Descriptor> heapPath = (NTuple<Descriptor>) iterator.next();
1566
1567       if (!isSharedLocation(heapPath)) {
1568         addWriteAgeToSet(heapPath, GENSet, new WriteAge(0));
1569       } else {
1570         // if the current heap path is shared location
1571
1572         NTuple<Location> locTuple = getLocationTuple(heapPath);
1573
1574         Set<NTuple<Descriptor>> sharedWriteHeapPathSet = sharedLocMap.get(locTuple);
1575
1576         if (isCovered(locTuple, sharedLocMap.get(locTuple))) {
1577           // if it is covered, add all of heap paths belong to the same shared
1578           // loc with write age 0
1579
1580           for (Iterator iterator2 = sharedWriteHeapPathSet.iterator(); iterator2.hasNext();) {
1581             NTuple<Descriptor> sharedHeapPath = (NTuple<Descriptor>) iterator2.next();
1582             addWriteAgeToSet(sharedHeapPath, GENSet, new WriteAge(0));
1583           }
1584
1585         } else {
1586           // if not covered, add write age 1 to the heap path that is
1587           // may-written but not covered
1588           addWriteAgeToSet(heapPath, GENSet, new WriteAge(1));
1589         }
1590
1591       }
1592
1593     }
1594
1595   }
1596
1597   private void addWriteAgeToSet(NTuple<Descriptor> heapPath,
1598       Hashtable<NTuple<Descriptor>, Set<WriteAge>> map, WriteAge age) {
1599
1600     Set<WriteAge> currSet = map.get(heapPath);
1601     if (currSet == null) {
1602       currSet = new HashSet<WriteAge>();
1603       map.put(heapPath, currSet);
1604     }
1605
1606     currSet.add(age);
1607   }
1608
1609   private void generateKILLSetForFlatCall(FlatCall fc,
1610       Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr, SharedLocMap sharedLocMap,
1611       Hashtable<NTuple<Descriptor>, Set<WriteAge>> KILLSet) {
1612
1613     Set<NTuple<Descriptor>> boundMustWriteSet = mapFlatNodeToBoundMustWriteSet.get(fc);
1614     System.out.println("boundMustWriteSet=" + boundMustWriteSet);
1615
1616     for (Iterator iterator = boundMustWriteSet.iterator(); iterator.hasNext();) {
1617       NTuple<Descriptor> heapPath = (NTuple<Descriptor>) iterator.next();
1618
1619       if (isSharedLocation(heapPath)) {
1620         NTuple<Location> locTuple = getLocationTuple(heapPath);
1621
1622         if (isCovered(locTuple, sharedLocMap.get(locTuple))) {
1623           // if it is shared loc and corresponding shared loc has been covered
1624           KILLSet.put(heapPath, curr.get(heapPath));
1625         }
1626       } else {
1627
1628         for (Enumeration<NTuple<Descriptor>> e = curr.keys(); e.hasMoreElements();) {
1629           NTuple<Descriptor> key = e.nextElement();
1630           if (key.startsWith(heapPath)) {
1631             KILLSet.put(key, curr.get(key));
1632           }
1633         }
1634
1635       }
1636
1637     }
1638
1639   }
1640
1641   private int getArrayBaseDescriptorIdx(NTuple<Descriptor> heapPath) {
1642
1643     for (int i = heapPath.size() - 1; i >= 0; i--) {
1644       if (!heapPath.get(i).getSymbol().equals(arrayElementFieldName)) {
1645         return i;
1646       }
1647     }
1648
1649     return -1;
1650
1651   }
1652
1653   private boolean isSharedLocation(NTuple<Descriptor> heapPath) {
1654
1655     Descriptor d = heapPath.get(getArrayBaseDescriptorIdx(heapPath));
1656
1657     return ssjava.isSharedLocation(getLocation(heapPath.get(getArrayBaseDescriptorIdx(heapPath))));
1658
1659   }
1660
1661   private NTuple<Location> getLocationTuple(NTuple<Descriptor> heapPath) {
1662
1663     NTuple<Location> locTuple = new NTuple<Location>();
1664
1665     locTuple.addAll(mapDescriptorToLocationPath.get(heapPath.get(0)));
1666
1667     for (int i = 1; i <= getArrayBaseDescriptorIdx(heapPath); i++) {
1668       locTuple.add(getLocation(heapPath.get(i)));
1669     }
1670
1671     return locTuple;
1672   }
1673
1674   private void computeNewMapping(Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr,
1675       Hashtable<NTuple<Descriptor>, Set<WriteAge>> KILLSet,
1676       Hashtable<NTuple<Descriptor>, Set<WriteAge>> GENSet) {
1677
1678     for (Enumeration<NTuple<Descriptor>> e = KILLSet.keys(); e.hasMoreElements();) {
1679       NTuple<Descriptor> key = e.nextElement();
1680
1681       Set<WriteAge> writeAgeSet = curr.get(key);
1682       if (writeAgeSet == null) {
1683         writeAgeSet = new HashSet<WriteAge>();
1684         curr.put(key, writeAgeSet);
1685       }
1686       writeAgeSet.removeAll(KILLSet.get(key));
1687     }
1688
1689     for (Enumeration<NTuple<Descriptor>> e = GENSet.keys(); e.hasMoreElements();) {
1690       NTuple<Descriptor> key = e.nextElement();
1691
1692       Set<WriteAge> currWriteAgeSet = curr.get(key);
1693       if (currWriteAgeSet == null) {
1694         currWriteAgeSet = new HashSet<WriteAge>();
1695         curr.put(key, currWriteAgeSet);
1696       }
1697       currWriteAgeSet.addAll(GENSet.get(key));
1698     }
1699
1700   }
1701
1702   private void computeGENSetForWrite(NTuple<Descriptor> fldHeapPath,
1703       Hashtable<NTuple<Descriptor>, Set<WriteAge>> GENSet) {
1704
1705     // generate write age 0 for the field being written to
1706     Set<WriteAge> writeAgeSet = new HashSet<WriteAge>();
1707     writeAgeSet.add(new WriteAge(0));
1708     GENSet.put(fldHeapPath, writeAgeSet);
1709
1710   }
1711
1712   private void computeKILLSetForWrite(Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr,
1713       NTuple<Descriptor> hp, Hashtable<NTuple<Descriptor>, Set<WriteAge>> KILLSet) {
1714
1715     // removes all of heap path that starts with prefix 'hp'
1716     // since any reference overwrite along heap path gives overwriting side
1717     // effects on the value
1718
1719     Set<NTuple<Descriptor>> keySet = curr.keySet();
1720     for (Iterator<NTuple<Descriptor>> iter = keySet.iterator(); iter.hasNext();) {
1721       NTuple<Descriptor> key = iter.next();
1722       if (key.startsWith(hp)) {
1723         KILLSet.put(key, curr.get(key));
1724       }
1725     }
1726
1727   }
1728
1729   private void bindHeapPathCallerArgWithCalleeParam(FlatCall fc) {
1730     // compute all possible callee set
1731     // transform all READ/WRITE set from the any possible
1732     // callees to the caller
1733     calleeUnionBoundReadSet.clear();
1734     calleeIntersectBoundMustWriteSet.clear();
1735     calleeUnionBoundMayWriteSet.clear();
1736
1737     if (ssjava.isSSJavaUtil(fc.getMethod().getClassDesc())) {
1738       // ssjava util case!
1739       // have write effects on the first argument
1740       TempDescriptor arg = fc.getArg(0);
1741       NTuple<Descriptor> argHeapPath = computePath(arg);
1742       calleeIntersectBoundMustWriteSet.add(argHeapPath);
1743       calleeUnionBoundMayWriteSet.add(argHeapPath);
1744     } else {
1745       MethodDescriptor mdCallee = fc.getMethod();
1746       Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
1747       setPossibleCallees.addAll(callGraph.getMethods(mdCallee));
1748
1749       // create mapping from arg idx to its heap paths
1750       Hashtable<Integer, NTuple<Descriptor>> mapArgIdx2CallerArgHeapPath =
1751           new Hashtable<Integer, NTuple<Descriptor>>();
1752
1753       // arg idx is starting from 'this' arg
1754       if (fc.getThis() != null) {
1755         NTuple<Descriptor> thisHeapPath = mapHeapPath.get(fc.getThis());
1756         if (thisHeapPath != null) {
1757           // if 'this' does not have heap path, it is local reference
1758           mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(0), thisHeapPath);
1759         }
1760       }
1761
1762       for (int i = 0; i < fc.numArgs(); i++) {
1763         TempDescriptor arg = fc.getArg(i);
1764         NTuple<Descriptor> argHeapPath = computePath(arg);
1765         mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(i + 1), argHeapPath);
1766       }
1767
1768       for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
1769         MethodDescriptor callee = (MethodDescriptor) iterator.next();
1770         FlatMethod calleeFlatMethod = state.getMethodFlat(callee);
1771
1772         // binding caller's args and callee's params
1773
1774         Set<NTuple<Descriptor>> calleeReadSet = mapFlatMethodToReadSet.get(calleeFlatMethod);
1775         if (calleeReadSet == null) {
1776           calleeReadSet = new HashSet<NTuple<Descriptor>>();
1777           mapFlatMethodToReadSet.put(calleeFlatMethod, calleeReadSet);
1778         }
1779
1780         Set<NTuple<Descriptor>> calleeMustWriteSet =
1781             mapFlatMethodToMustWriteSet.get(calleeFlatMethod);
1782
1783         if (calleeMustWriteSet == null) {
1784           calleeMustWriteSet = new HashSet<NTuple<Descriptor>>();
1785           mapFlatMethodToMustWriteSet.put(calleeFlatMethod, calleeMustWriteSet);
1786         }
1787
1788         Set<NTuple<Descriptor>> calleeMayWriteSet =
1789             mapFlatMethodToMayWriteSet.get(calleeFlatMethod);
1790
1791         if (calleeMayWriteSet == null) {
1792           calleeMayWriteSet = new HashSet<NTuple<Descriptor>>();
1793           mapFlatMethodToMayWriteSet.put(calleeFlatMethod, calleeMayWriteSet);
1794         }
1795
1796         Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc =
1797             new Hashtable<Integer, TempDescriptor>();
1798         int offset = 0;
1799         if (calleeFlatMethod.getMethod().isStatic()) {
1800           // static method does not have implicit 'this' arg
1801           offset = 1;
1802         }
1803         for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
1804           TempDescriptor param = calleeFlatMethod.getParameter(i);
1805           mapParamIdx2ParamTempDesc.put(Integer.valueOf(i + offset), param);
1806         }
1807
1808         Set<NTuple<Descriptor>> calleeBoundReadSet =
1809             bindSet(calleeReadSet, mapParamIdx2ParamTempDesc, mapArgIdx2CallerArgHeapPath);
1810         // union of the current read set and the current callee's
1811         // read set
1812         calleeUnionBoundReadSet.addAll(calleeBoundReadSet);
1813
1814         Set<NTuple<Descriptor>> calleeBoundMustWriteSet =
1815             bindSet(calleeMustWriteSet, mapParamIdx2ParamTempDesc, mapArgIdx2CallerArgHeapPath);
1816         // intersection of the current overwrite set and the current
1817         // callee's
1818         // overwrite set
1819         merge(calleeIntersectBoundMustWriteSet, calleeBoundMustWriteSet);
1820
1821         Set<NTuple<Descriptor>> boundWriteSetFromCallee =
1822             bindSet(calleeMayWriteSet, mapParamIdx2ParamTempDesc, mapArgIdx2CallerArgHeapPath);
1823         calleeUnionBoundMayWriteSet.addAll(boundWriteSetFromCallee);
1824       }
1825
1826     }
1827
1828   }
1829
1830   private void bindHeapPathCallerArgWithCaleeParamForSharedLoc(MethodDescriptor mdCaller,
1831       FlatCall fc) {
1832
1833     calleeIntersectBoundSharedSet.clear();
1834     calleeUnionBoundDeleteSet.clear();
1835
1836     if (ssjava.isSSJavaUtil(fc.getMethod().getClassDesc())) {
1837       // ssjava util case!
1838       // have write effects on the first argument
1839       TempDescriptor arg = fc.getArg(0);
1840       NTuple<Descriptor> argHeapPath = computePath(arg);
1841
1842       // convert heap path to location path
1843       NTuple<Location> argLocTuple = new NTuple<Location>();
1844       argLocTuple.addAll(deriveLocationTuple(mdCaller, (TempDescriptor) argHeapPath.get(0)));
1845       for (int i = 1; i < argHeapPath.size(); i++) {
1846         argLocTuple.add(getLocation(argHeapPath.get(i)));
1847       }
1848
1849       calleeIntersectBoundSharedSet.addWrite(argLocTuple, argHeapPath);
1850
1851     } else if (ssjava.needTobeAnnotated(fc.getMethod())) {
1852
1853       // if arg is not primitive type, we need to propagate maywritten set to
1854       // the caller's location path
1855
1856       MethodDescriptor mdCallee = fc.getMethod();
1857       Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
1858       setPossibleCallees.addAll(callGraph.getMethods(mdCallee));
1859
1860       // create mapping from arg idx to its heap paths
1861       Hashtable<Integer, NTuple<Descriptor>> mapArgIdx2CallerArgHeapPath =
1862           new Hashtable<Integer, NTuple<Descriptor>>();
1863
1864       // arg idx is starting from 'this' arg
1865       if (fc.getThis() != null) {
1866         NTuple<Descriptor> thisHeapPath = mapHeapPath.get(fc.getThis());
1867         if (thisHeapPath == null) {
1868           // method is called without creating new flat node representing 'this'
1869           thisHeapPath = new NTuple<Descriptor>();
1870           thisHeapPath.add(fc.getThis());
1871         }
1872
1873         mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(0), thisHeapPath);
1874       }
1875
1876       for (int i = 0; i < fc.numArgs(); i++) {
1877         TempDescriptor arg = fc.getArg(i);
1878         NTuple<Descriptor> argHeapPath = computePath(arg);
1879         mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(i + 1), argHeapPath);
1880       }
1881
1882       // create mapping from arg idx to its location paths
1883       Hashtable<Integer, NTuple<Location>> mapArgIdx2CallerAgLocationPath =
1884           new Hashtable<Integer, NTuple<Location>>();
1885
1886       // arg idx is starting from 'this' arg
1887       if (fc.getThis() != null) {
1888         NTuple<Location> thisLocationPath = deriveLocationTuple(mdCaller, fc.getThis());
1889         if (thisLocationPath != null) {
1890           mapArgIdx2CallerAgLocationPath.put(Integer.valueOf(0), thisLocationPath);
1891         }
1892       }
1893
1894       for (int i = 0; i < fc.numArgs(); i++) {
1895         TempDescriptor arg = fc.getArg(i);
1896         NTuple<Location> argLocationPath = deriveLocationTuple(mdCaller, arg);
1897         if (argLocationPath != null) {
1898           mapArgIdx2CallerAgLocationPath.put(Integer.valueOf(i + 1), argLocationPath);
1899         }
1900       }
1901
1902       for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
1903         MethodDescriptor callee = (MethodDescriptor) iterator.next();
1904         FlatMethod calleeFlatMethod = state.getMethodFlat(callee);
1905
1906         // binding caller's args and callee's params
1907
1908         Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc =
1909             new Hashtable<Integer, TempDescriptor>();
1910         int offset = 0;
1911         if (calleeFlatMethod.getMethod().isStatic()) {
1912           // static method does not have implicit 'this' arg
1913           offset = 1;
1914         }
1915         for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
1916           TempDescriptor param = calleeFlatMethod.getParameter(i);
1917           mapParamIdx2ParamTempDesc.put(Integer.valueOf(i + offset), param);
1918         }
1919
1920         Set<Integer> keySet = mapArgIdx2CallerAgLocationPath.keySet();
1921         for (Iterator iterator2 = keySet.iterator(); iterator2.hasNext();) {
1922           Integer idx = (Integer) iterator2.next();
1923           NTuple<Location> callerArgLocationPath = mapArgIdx2CallerAgLocationPath.get(idx);
1924           NTuple<Descriptor> callerArgHeapPath = mapArgIdx2CallerArgHeapPath.get(idx);
1925
1926           TempDescriptor calleeParam = mapParamIdx2ParamTempDesc.get(idx);
1927           NTuple<Location> calleeLocationPath = deriveLocationTuple(mdCallee, calleeParam);
1928           SharedLocMap calleeDeleteSet = mapFlatMethodToDeleteSet.get(calleeFlatMethod);
1929           SharedLocMap calleeSharedLocMap = mapFlatMethodToSharedLocMap.get(calleeFlatMethod);
1930
1931           if (calleeDeleteSet != null) {
1932             createNewMappingOfDeleteSet(callerArgLocationPath, callerArgHeapPath,
1933                 calleeLocationPath, calleeDeleteSet);
1934           }
1935
1936           if (calleeSharedLocMap != null) {
1937             createNewMappingOfSharedSet(callerArgLocationPath, callerArgHeapPath,
1938                 calleeLocationPath, calleeSharedLocMap);
1939           }
1940
1941         }
1942
1943       }
1944     }
1945
1946   }
1947
1948   private void createNewMappingOfDeleteSet(NTuple<Location> callerArgLocationPath,
1949       NTuple<Descriptor> callerArgHeapPath, NTuple<Location> calleeLocationPath,
1950       SharedLocMap calleeDeleteSet) {
1951
1952     SharedLocMap calleeParamDeleteSet = calleeDeleteSet.getHeapPathStartedWith(calleeLocationPath);
1953
1954     Set<NTuple<Location>> keySet = calleeParamDeleteSet.keySet();
1955     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1956       NTuple<Location> calleeLocTupleKey = (NTuple<Location>) iterator.next();
1957       Set<NTuple<Descriptor>> heapPathSet = calleeParamDeleteSet.get(calleeLocTupleKey);
1958       for (Iterator iterator2 = heapPathSet.iterator(); iterator2.hasNext();) {
1959         NTuple<Descriptor> calleeHeapPath = (NTuple<Descriptor>) iterator2.next();
1960         calleeUnionBoundDeleteSet.addWrite(
1961             bindLocationPath(callerArgLocationPath, calleeLocTupleKey),
1962             bindHeapPath(callerArgHeapPath, calleeHeapPath));
1963       }
1964     }
1965
1966   }
1967
1968   private void createNewMappingOfSharedSet(NTuple<Location> callerArgLocationPath,
1969       NTuple<Descriptor> callerArgHeapPath, NTuple<Location> calleeLocationPath,
1970       SharedLocMap calleeSharedLocMap) {
1971
1972     SharedLocMap calleeParamSharedSet =
1973         calleeSharedLocMap.getHeapPathStartedWith(calleeLocationPath);
1974
1975     Set<NTuple<Location>> keySet = calleeParamSharedSet.keySet();
1976     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1977       NTuple<Location> calleeLocTupleKey = (NTuple<Location>) iterator.next();
1978       Set<NTuple<Descriptor>> heapPathSet = calleeParamSharedSet.get(calleeLocTupleKey);
1979       Set<NTuple<Descriptor>> boundHeapPathSet = new HashSet<NTuple<Descriptor>>();
1980       for (Iterator iterator2 = heapPathSet.iterator(); iterator2.hasNext();) {
1981         NTuple<Descriptor> calleeHeapPath = (NTuple<Descriptor>) iterator2.next();
1982         boundHeapPathSet.add(bindHeapPath(callerArgHeapPath, calleeHeapPath));
1983       }
1984       calleeIntersectBoundSharedSet.intersect(
1985           bindLocationPath(callerArgLocationPath, calleeLocTupleKey), boundHeapPathSet);
1986     }
1987
1988   }
1989
1990   private NTuple<Location> bindLocationPath(NTuple<Location> start, NTuple<Location> end) {
1991     NTuple<Location> locPath = new NTuple<Location>();
1992     locPath.addAll(start);
1993     for (int i = 1; i < end.size(); i++) {
1994       locPath.add(end.get(i));
1995     }
1996     return locPath;
1997   }
1998
1999   private NTuple<Descriptor> bindHeapPath(NTuple<Descriptor> start, NTuple<Descriptor> end) {
2000     NTuple<Descriptor> heapPath = new NTuple<Descriptor>();
2001     heapPath.addAll(start);
2002     for (int i = 1; i < end.size(); i++) {
2003       heapPath.add(end.get(i));
2004     }
2005     return heapPath;
2006   }
2007
2008   private void initialize() {
2009     // First, identify ssjava loop entrace
2010
2011     // no need to analyze method having ssjava loop
2012     methodContainingSSJavaLoop = ssjava.getMethodContainingSSJavaLoop();
2013
2014     FlatMethod fm = state.getMethodFlat(methodContainingSSJavaLoop);
2015     Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
2016     flatNodesToVisit.add(fm);
2017
2018     LoopFinder loopFinder = new LoopFinder(fm);
2019
2020     while (!flatNodesToVisit.isEmpty()) {
2021       FlatNode fn = flatNodesToVisit.iterator().next();
2022       flatNodesToVisit.remove(fn);
2023
2024       String label = (String) state.fn2labelMap.get(fn);
2025       if (label != null) {
2026
2027         if (label.equals(ssjava.SSJAVA)) {
2028           ssjava.setSSJavaLoopEntrance(fn);
2029           break;
2030         }
2031       }
2032
2033       for (int i = 0; i < fn.numNext(); i++) {
2034         FlatNode nn = fn.getNext(i);
2035         flatNodesToVisit.add(nn);
2036       }
2037     }
2038
2039     assert ssjava.getSSJavaLoopEntrance() != null;
2040
2041     // assume that ssjava loop is top-level loop in method, not nested loop
2042     Set nestedLoop = loopFinder.nestedLoops();
2043     for (Iterator loopIter = nestedLoop.iterator(); loopIter.hasNext();) {
2044       LoopFinder lf = (LoopFinder) loopIter.next();
2045       if (lf.loopEntrances().iterator().next().equals(ssjava.getSSJavaLoopEntrance())) {
2046         ssjavaLoop = lf;
2047       }
2048     }
2049
2050     assert ssjavaLoop != null;
2051
2052     loopIncElements = (Set<FlatNode>) ssjavaLoop.loopIncElements();
2053
2054     // perform topological sort over the set of methods accessed by the main
2055     // event loop
2056     Set<MethodDescriptor> methodDescriptorsToAnalyze = new HashSet<MethodDescriptor>();
2057     methodDescriptorsToAnalyze.addAll(ssjava.getAnnotationRequireSet());
2058     sortedDescriptors = topologicalSort(methodDescriptorsToAnalyze);
2059   }
2060
2061   private void methodReadWriteSetAnalysis() {
2062     // perform method READ/OVERWRITE analysis
2063     LinkedList<MethodDescriptor> descriptorListToAnalyze =
2064         (LinkedList<MethodDescriptor>) sortedDescriptors.clone();
2065
2066     // current descriptors to visit in fixed-point interprocedural analysis,
2067     // prioritized by
2068     // dependency in the call graph
2069     methodDescriptorsToVisitStack.clear();
2070
2071     descriptorListToAnalyze.removeFirst();
2072
2073     Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
2074     methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
2075
2076     while (!descriptorListToAnalyze.isEmpty()) {
2077       MethodDescriptor md = descriptorListToAnalyze.removeFirst();
2078       methodDescriptorsToVisitStack.add(md);
2079     }
2080
2081     // analyze scheduled methods until there are no more to visit
2082     while (!methodDescriptorsToVisitStack.isEmpty()) {
2083       // start to analyze leaf node
2084       MethodDescriptor md = methodDescriptorsToVisitStack.pop();
2085       FlatMethod fm = state.getMethodFlat(md);
2086
2087       Set<NTuple<Descriptor>> readSet = new HashSet<NTuple<Descriptor>>();
2088       Set<NTuple<Descriptor>> mustWriteSet = new HashSet<NTuple<Descriptor>>();
2089       Set<NTuple<Descriptor>> mayWriteSet = new HashSet<NTuple<Descriptor>>();
2090
2091       methodReadWriteSet_analyzeMethod(fm, readSet, mustWriteSet, mayWriteSet);
2092
2093       Set<NTuple<Descriptor>> prevRead = mapFlatMethodToReadSet.get(fm);
2094       Set<NTuple<Descriptor>> prevMustWrite = mapFlatMethodToMustWriteSet.get(fm);
2095       Set<NTuple<Descriptor>> prevMayWrite = mapFlatMethodToMayWriteSet.get(fm);
2096
2097       if (!(readSet.equals(prevRead) && mustWriteSet.equals(prevMustWrite) && mayWriteSet
2098           .equals(prevMayWrite))) {
2099         mapFlatMethodToReadSet.put(fm, readSet);
2100         mapFlatMethodToMustWriteSet.put(fm, mustWriteSet);
2101         mapFlatMethodToMayWriteSet.put(fm, mayWriteSet);
2102
2103         // results for callee changed, so enqueue dependents caller for
2104         // further
2105         // analysis
2106         Iterator<MethodDescriptor> depsItr = getDependents(md).iterator();
2107         while (depsItr.hasNext()) {
2108           MethodDescriptor methodNext = depsItr.next();
2109           if (!methodDescriptorsToVisitStack.contains(methodNext)
2110               && methodDescriptorToVistSet.contains(methodNext)) {
2111             methodDescriptorsToVisitStack.add(methodNext);
2112           }
2113
2114         }
2115
2116       }
2117
2118     }
2119
2120     methodReadWriteSetAnalysisToEventLoopBody();
2121
2122   }
2123
2124   private void methodReadWriteSet_analyzeMethod(FlatMethod fm, Set<NTuple<Descriptor>> readSet,
2125       Set<NTuple<Descriptor>> mustWriteSet, Set<NTuple<Descriptor>> mayWriteSet) {
2126     if (state.SSJAVADEBUG) {
2127       System.out.println("SSJAVA: Definitely written Analyzing: " + fm);
2128     }
2129
2130     methodReadWriteSet_analyzeBody(fm, readSet, mustWriteSet, mayWriteSet, false);
2131
2132   }
2133
2134   private void methodReadWriteSetAnalysisToEventLoopBody() {
2135
2136     // perform method read/write analysis for Event Loop Body
2137
2138     FlatMethod flatMethodContainingSSJavaLoop = state.getMethodFlat(methodContainingSSJavaLoop);
2139
2140     if (state.SSJAVADEBUG) {
2141       System.out.println("SSJAVA: Definitely written Event Loop Analyzing: "
2142           + flatMethodContainingSSJavaLoop);
2143     }
2144
2145     Set<NTuple<Descriptor>> readSet = new HashSet<NTuple<Descriptor>>();
2146     Set<NTuple<Descriptor>> mustWriteSet = new HashSet<NTuple<Descriptor>>();
2147     Set<NTuple<Descriptor>> mayWriteSet = new HashSet<NTuple<Descriptor>>();
2148
2149     mapFlatMethodToReadSet.put(flatMethodContainingSSJavaLoop, readSet);
2150     mapFlatMethodToMustWriteSet.put(flatMethodContainingSSJavaLoop, mustWriteSet);
2151     mapFlatMethodToMayWriteSet.put(flatMethodContainingSSJavaLoop, mayWriteSet);
2152
2153     methodReadWriteSet_analyzeBody(ssjava.getSSJavaLoopEntrance(), readSet, mustWriteSet,
2154         mayWriteSet, true);
2155
2156   }
2157
2158   private void methodReadWriteSet_analyzeBody(FlatNode startNode, Set<NTuple<Descriptor>> readSet,
2159       Set<NTuple<Descriptor>> mustWriteSet, Set<NTuple<Descriptor>> mayWriteSet,
2160       boolean isEventLoopBody) {
2161
2162     // intraprocedural analysis
2163     Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
2164     flatNodesToVisit.add(startNode);
2165
2166     while (!flatNodesToVisit.isEmpty()) {
2167       FlatNode fn = flatNodesToVisit.iterator().next();
2168       flatNodesToVisit.remove(fn);
2169
2170       Set<NTuple<Descriptor>> currMustWriteSet = new HashSet<NTuple<Descriptor>>();
2171
2172       for (int i = 0; i < fn.numPrev(); i++) {
2173         FlatNode prevFn = fn.getPrev(i);
2174         Set<NTuple<Descriptor>> in = mapFlatNodeToMustWriteSet.get(prevFn);
2175         if (in != null) {
2176           merge(currMustWriteSet, in);
2177         }
2178       }
2179
2180       methodReadWriteSet_nodeActions(fn, currMustWriteSet, readSet, mustWriteSet, mayWriteSet,
2181           isEventLoopBody);
2182
2183       Set<NTuple<Descriptor>> mustSetPrev = mapFlatNodeToMustWriteSet.get(fn);
2184
2185       if (!currMustWriteSet.equals(mustSetPrev)) {
2186         mapFlatNodeToMustWriteSet.put(fn, currMustWriteSet);
2187         for (int i = 0; i < fn.numNext(); i++) {
2188           FlatNode nn = fn.getNext(i);
2189           if ((!isEventLoopBody) || loopIncElements.contains(nn)) {
2190             flatNodesToVisit.add(nn);
2191           }
2192
2193         }
2194       }
2195
2196     }
2197
2198   }
2199
2200   private void methodReadWriteSet_nodeActions(FlatNode fn,
2201       Set<NTuple<Descriptor>> currMustWriteSet, Set<NTuple<Descriptor>> readSet,
2202       Set<NTuple<Descriptor>> mustWriteSet, Set<NTuple<Descriptor>> mayWriteSet,
2203       boolean isEventLoopBody) {
2204
2205     TempDescriptor lhs;
2206     TempDescriptor rhs;
2207     FieldDescriptor fld;
2208
2209     switch (fn.kind()) {
2210     case FKind.FlatMethod: {
2211
2212       // set up initial heap paths for method parameters
2213       FlatMethod fm = (FlatMethod) fn;
2214       for (int i = 0; i < fm.numParameters(); i++) {
2215         TempDescriptor param = fm.getParameter(i);
2216         NTuple<Descriptor> heapPath = new NTuple<Descriptor>();
2217         heapPath.add(param);
2218         mapHeapPath.put(param, heapPath);
2219       }
2220     }
2221       break;
2222
2223     case FKind.FlatOpNode: {
2224       FlatOpNode fon = (FlatOpNode) fn;
2225       // for a normal assign node, need to propagate lhs's heap path to
2226       // rhs
2227
2228       if (fon.getOp().getOp() == Operation.ASSIGN) {
2229         rhs = fon.getLeft();
2230         lhs = fon.getDest();
2231
2232         NTuple<Descriptor> rhsHeapPath = mapHeapPath.get(rhs);
2233
2234         // if (lhs.getType().isPrimitive()) {
2235         // NTuple<Descriptor> lhsHeapPath = new NTuple<Descriptor>();
2236         // lhsHeapPath.add(lhs);
2237         // mapHeapPath.put(lhs, lhsHeapPath);
2238         // } else
2239
2240         if (rhsHeapPath != null) {
2241           mapHeapPath.put(lhs, mapHeapPath.get(rhs));
2242         } else {
2243           if (isEventLoopBody) {
2244             NTuple<Descriptor> heapPath = new NTuple<Descriptor>();
2245             heapPath.add(rhs);
2246             mapHeapPath.put(lhs, heapPath);
2247           } else {
2248             break;
2249           }
2250         }
2251
2252         // shared loc extension
2253         if (isEventLoopBody) {
2254           if (!lhs.getSymbol().startsWith("neverused") && rhs.getType().isImmutable()) {
2255
2256             if (rhs.getType().getExtension() instanceof Location
2257                 && lhs.getType().getExtension() instanceof CompositeLocation) {
2258               // rhs is field!
2259               Location rhsLoc = (Location) rhs.getType().getExtension();
2260
2261               CompositeLocation lhsCompLoc = (CompositeLocation) lhs.getType().getExtension();
2262               Location dstLoc = lhsCompLoc.get(lhsCompLoc.getSize() - 1);
2263
2264               NTuple<Descriptor> heapPath = new NTuple<Descriptor>();
2265               for (int i = 0; i < rhsHeapPath.size() - 1; i++) {
2266                 heapPath.add(rhsHeapPath.get(i));
2267               }
2268
2269               NTuple<Descriptor> writeHeapPath = new NTuple<Descriptor>();
2270               writeHeapPath.addAll(heapPath);
2271               writeHeapPath.add(lhs);
2272
2273             }
2274           }
2275         }
2276
2277       }
2278     }
2279       break;
2280
2281     case FKind.FlatElementNode:
2282     case FKind.FlatFieldNode: {
2283
2284       // x=y.f;
2285
2286       if (fn.kind() == FKind.FlatFieldNode) {
2287         FlatFieldNode ffn = (FlatFieldNode) fn;
2288         lhs = ffn.getDst();
2289         rhs = ffn.getSrc();
2290         fld = ffn.getField();
2291       } else {
2292         FlatElementNode fen = (FlatElementNode) fn;
2293         lhs = fen.getDst();
2294         rhs = fen.getSrc();
2295         TypeDescriptor td = rhs.getType().dereference();
2296         fld = getArrayField(td);
2297       }
2298
2299       if (fld.isFinal()) {
2300         // if field is final no need to check
2301         break;
2302       }
2303
2304       // set up heap path
2305       NTuple<Descriptor> srcHeapPath = mapHeapPath.get(rhs);
2306       if (srcHeapPath != null) {
2307         // if lhs srcHeapPath is null, it means that it is not reachable from
2308         // callee's parameters. so just ignore it
2309
2310         NTuple<Descriptor> readingHeapPath = new NTuple<Descriptor>(srcHeapPath.getList());
2311         if (fn.kind() == FKind.FlatFieldNode) {
2312           readingHeapPath.add(fld);
2313         }
2314
2315         mapHeapPath.put(lhs, readingHeapPath);
2316
2317         // read (x.f)
2318         if (fld.getType().isImmutable()) {
2319           // if WT doesnot have hp(x.f), add hp(x.f) to READ
2320           if (!currMustWriteSet.contains(readingHeapPath)) {
2321             readSet.add(readingHeapPath);
2322           }
2323         }
2324
2325         // no need to kill hp(x.f) from WT
2326       }
2327
2328     }
2329       break;
2330
2331     case FKind.FlatSetFieldNode:
2332     case FKind.FlatSetElementNode: {
2333
2334       // x.f=y;
2335
2336       if (fn.kind() == FKind.FlatSetFieldNode) {
2337         FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
2338         lhs = fsfn.getDst();
2339         fld = fsfn.getField();
2340         rhs = fsfn.getSrc();
2341       } else {
2342         FlatSetElementNode fsen = (FlatSetElementNode) fn;
2343         lhs = fsen.getDst();
2344         rhs = fsen.getSrc();
2345         TypeDescriptor td = lhs.getType().dereference();
2346         fld = getArrayField(td);
2347       }
2348
2349       // set up heap path
2350       NTuple<Descriptor> lhsHeapPath = mapHeapPath.get(lhs);
2351
2352       if (lhsHeapPath != null) {
2353         // if lhs heap path is null, it means that it is not reachable from
2354         // callee's parameters. so just ignore it
2355         NTuple<Descriptor> fldHeapPath = new NTuple<Descriptor>(lhsHeapPath.getList());
2356         if (fn.kind() != FKind.FlatSetElementNode) {
2357           fldHeapPath.add(fld);
2358         }
2359         // mapHeapPath.put(fld, fldHeapPath);
2360
2361         // write(x.f)
2362         // need to add hp(y) to WT
2363         if (fn.kind() != FKind.FlatSetElementNode) {
2364           currMustWriteSet.add(fldHeapPath);
2365         }
2366         mayWriteSet.add(fldHeapPath);
2367
2368       }
2369
2370     }
2371       break;
2372
2373     case FKind.FlatCall: {
2374
2375       FlatCall fc = (FlatCall) fn;
2376
2377       bindHeapPathCallerArgWithCalleeParam(fc);
2378
2379       Set<NTuple<Descriptor>> boundReadSet = new HashSet<NTuple<Descriptor>>();
2380       boundReadSet.addAll(calleeUnionBoundReadSet);
2381
2382       Set<NTuple<Descriptor>> boundMustWriteSet = new HashSet<NTuple<Descriptor>>();
2383       boundMustWriteSet.addAll(calleeIntersectBoundMustWriteSet);
2384
2385       Set<NTuple<Descriptor>> boundMayWriteSet = new HashSet<NTuple<Descriptor>>();
2386       boundMayWriteSet.addAll(calleeUnionBoundMayWriteSet);
2387
2388       mapFlatNodeToBoundReadSet.put(fn, boundReadSet);
2389       mapFlatNodeToBoundMustWriteSet.put(fn, boundMustWriteSet);
2390       mapFlatNodeToBoundMayWriteSet.put(fn, boundMayWriteSet);
2391
2392       // add heap path, which is an element of READ_bound set and is not
2393       // an
2394       // element of WT set, to the caller's READ set
2395       for (Iterator iterator = calleeUnionBoundReadSet.iterator(); iterator.hasNext();) {
2396         NTuple<Descriptor> read = (NTuple<Descriptor>) iterator.next();
2397         if (!currMustWriteSet.contains(read)) {
2398           readSet.add(read);
2399         }
2400       }
2401
2402       // add heap path, which is an element of OVERWRITE_bound set, to the
2403       // caller's WT set
2404       for (Iterator iterator = calleeIntersectBoundMustWriteSet.iterator(); iterator.hasNext();) {
2405         NTuple<Descriptor> write = (NTuple<Descriptor>) iterator.next();
2406         currMustWriteSet.add(write);
2407       }
2408
2409       // add heap path, which is an element of WRITE_BOUND set, to the
2410       // caller's writeSet
2411       for (Iterator iterator = calleeUnionBoundMayWriteSet.iterator(); iterator.hasNext();) {
2412         NTuple<Descriptor> write = (NTuple<Descriptor>) iterator.next();
2413         mayWriteSet.add(write);
2414       }
2415
2416     }
2417       break;
2418
2419     case FKind.FlatExit: {
2420       // merge the current written set with OVERWRITE set
2421       merge(mustWriteSet, currMustWriteSet);
2422     }
2423       break;
2424
2425     }
2426
2427   }
2428
2429   static public FieldDescriptor getArrayField(TypeDescriptor td) {
2430     FieldDescriptor fd = mapTypeToArrayField.get(td);
2431     if (fd == null) {
2432       fd =
2433           new FieldDescriptor(new Modifiers(Modifiers.PUBLIC), td, arrayElementFieldName, null,
2434               false);
2435       mapTypeToArrayField.put(td, fd);
2436     }
2437     return fd;
2438   }
2439
2440   private void merge(Set<NTuple<Descriptor>> curr, Set<NTuple<Descriptor>> in) {
2441     if (curr.isEmpty()) {
2442       // set has a special initial value which covers all possible
2443       // elements
2444       // For the first time of intersection, we can take all previous set
2445       curr.addAll(in);
2446     } else {
2447       // otherwise, current set is the intersection of the two sets
2448       curr.retainAll(in);
2449     }
2450
2451   }
2452
2453   // combine two heap path
2454   private NTuple<Descriptor> combine(NTuple<Descriptor> callerIn, NTuple<Descriptor> calleeIn) {
2455     NTuple<Descriptor> combined = new NTuple<Descriptor>();
2456
2457     for (int i = 0; i < callerIn.size(); i++) {
2458       combined.add(callerIn.get(i));
2459     }
2460
2461     // the first element of callee's heap path represents parameter
2462     // so we skip the first one since it is already added from caller's heap
2463     // path
2464     for (int i = 1; i < calleeIn.size(); i++) {
2465       combined.add(calleeIn.get(i));
2466     }
2467
2468     return combined;
2469   }
2470
2471   private Set<NTuple<Descriptor>> bindSet(Set<NTuple<Descriptor>> calleeSet,
2472       Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc,
2473       Hashtable<Integer, NTuple<Descriptor>> mapCallerArgIdx2HeapPath) {
2474
2475     Set<NTuple<Descriptor>> boundedCalleeSet = new HashSet<NTuple<Descriptor>>();
2476
2477     Set<Integer> keySet = mapCallerArgIdx2HeapPath.keySet();
2478     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2479       Integer idx = (Integer) iterator.next();
2480
2481       NTuple<Descriptor> callerArgHeapPath = mapCallerArgIdx2HeapPath.get(idx);
2482       TempDescriptor calleeParam = mapParamIdx2ParamTempDesc.get(idx);
2483       for (Iterator iterator2 = calleeSet.iterator(); iterator2.hasNext();) {
2484         NTuple<Descriptor> element = (NTuple<Descriptor>) iterator2.next();
2485         if (element.startsWith(calleeParam)) {
2486           NTuple<Descriptor> boundElement = combine(callerArgHeapPath, element);
2487           boundedCalleeSet.add(boundElement);
2488         }
2489
2490       }
2491
2492     }
2493     return boundedCalleeSet;
2494
2495   }
2496
2497   // Borrowed it from disjoint analysis
2498   private LinkedList<MethodDescriptor> topologicalSort(Set<MethodDescriptor> toSort) {
2499
2500     Set<MethodDescriptor> discovered = new HashSet<MethodDescriptor>();
2501
2502     LinkedList<MethodDescriptor> sorted = new LinkedList<MethodDescriptor>();
2503
2504     Iterator<MethodDescriptor> itr = toSort.iterator();
2505     while (itr.hasNext()) {
2506       MethodDescriptor d = itr.next();
2507
2508       if (!discovered.contains(d)) {
2509         dfsVisit(d, toSort, sorted, discovered);
2510       }
2511     }
2512
2513     return sorted;
2514   }
2515
2516   // While we're doing DFS on call graph, remember
2517   // dependencies for efficient queuing of methods
2518   // during interprocedural analysis:
2519   //
2520   // a dependent of a method decriptor d for this analysis is:
2521   // 1) a method or task that invokes d
2522   // 2) in the descriptorsToAnalyze set
2523   private void dfsVisit(MethodDescriptor md, Set<MethodDescriptor> toSort,
2524       LinkedList<MethodDescriptor> sorted, Set<MethodDescriptor> discovered) {
2525
2526     discovered.add(md);
2527
2528     Iterator itr = callGraph.getCallerSet(md).iterator();
2529     while (itr.hasNext()) {
2530       MethodDescriptor dCaller = (MethodDescriptor) itr.next();
2531       // only consider callers in the original set to analyze
2532       if (!toSort.contains(dCaller)) {
2533         continue;
2534       }
2535       if (!discovered.contains(dCaller)) {
2536         addDependent(md, // callee
2537             dCaller // caller
2538         );
2539
2540         dfsVisit(dCaller, toSort, sorted, discovered);
2541       }
2542     }
2543
2544     // for leaf-nodes last now!
2545     sorted.addLast(md);
2546   }
2547
2548   // a dependent of a method decriptor d for this analysis is:
2549   // 1) a method or task that invokes d
2550   // 2) in the descriptorsToAnalyze set
2551   private void addDependent(MethodDescriptor callee, MethodDescriptor caller) {
2552     Set<MethodDescriptor> deps = mapDescriptorToSetDependents.get(callee);
2553     if (deps == null) {
2554       deps = new HashSet<MethodDescriptor>();
2555     }
2556     deps.add(caller);
2557     mapDescriptorToSetDependents.put(callee, deps);
2558   }
2559
2560   private Set<MethodDescriptor> getDependents(MethodDescriptor callee) {
2561     Set<MethodDescriptor> deps = mapDescriptorToSetDependents.get(callee);
2562     if (deps == null) {
2563       deps = new HashSet<MethodDescriptor>();
2564       mapDescriptorToSetDependents.put(callee, deps);
2565     }
2566     return deps;
2567   }
2568
2569   private NTuple<Descriptor> computePath(Descriptor td) {
2570     // generate proper path fot input td
2571     // if td is local variable, it just generate one element tuple path
2572     if (mapHeapPath.containsKey(td)) {
2573       NTuple<Descriptor> rtrHeapPath = new NTuple<Descriptor>();
2574       rtrHeapPath.addAll(mapHeapPath.get(td));
2575       return rtrHeapPath;
2576     } else {
2577       NTuple<Descriptor> rtrHeapPath = new NTuple<Descriptor>();
2578       rtrHeapPath.add(td);
2579       return rtrHeapPath;
2580     }
2581   }
2582
2583   private NTuple<Location> deriveThisLocationTuple(MethodDescriptor md) {
2584     String thisLocIdentifier = ssjava.getMethodLattice(md).getThisLoc();
2585     Location thisLoc = new Location(md, thisLocIdentifier);
2586     NTuple<Location> locTuple = new NTuple<Location>();
2587     locTuple.add(thisLoc);
2588     return locTuple;
2589   }
2590
2591   private NTuple<Location> deriveGlobalLocationTuple(MethodDescriptor md) {
2592     String globalLocIdentifier = ssjava.getMethodLattice(md).getGlobalLoc();
2593     Location globalLoc = new Location(md, globalLocIdentifier);
2594     NTuple<Location> locTuple = new NTuple<Location>();
2595     locTuple.add(globalLoc);
2596     return locTuple;
2597   }
2598
2599   private NTuple<Location> deriveLocationTuple(MethodDescriptor md, TempDescriptor td) {
2600
2601     assert td.getType() != null;
2602
2603     if (mapDescriptorToLocationPath.containsKey(td)) {
2604       NTuple<Location> locPath = mapDescriptorToLocationPath.get(td);
2605       NTuple<Location> rtrPath = new NTuple<Location>();
2606       rtrPath.addAll(locPath);
2607       return rtrPath;
2608     } else {
2609       if (td.getSymbol().startsWith("this")) {
2610         NTuple<Location> thisPath = deriveThisLocationTuple(md);
2611         NTuple<Location> rtrPath = new NTuple<Location>();
2612         rtrPath.addAll(thisPath);
2613         return rtrPath;
2614       } else {
2615
2616         if (td.getType().getExtension() != null) {
2617           SSJavaType ssJavaType = (SSJavaType) td.getType().getExtension();
2618           if (ssJavaType.getCompLoc() != null) {
2619             NTuple<Location> rtrPath = new NTuple<Location>();
2620             rtrPath.addAll(ssJavaType.getCompLoc().getTuple());
2621             return rtrPath;
2622           }
2623         }
2624
2625         return null;
2626
2627       }
2628     }
2629   }
2630 }