1e31d7d6407ba0a4866edfc687219357423a17f3
[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       // arg idx is starting from 'this' arg
937       if (fc.getThis() != null) {
938         // loc path for 'this'
939         NTuple<Location> thisLocationPath = deriveLocationTuple(mdCaller, fc.getThis());
940         if (thisLocationPath != null) {
941           mapArgIdx2CallerArgLocationPath.put(Integer.valueOf(0), thisLocationPath);
942
943           // heap path for 'this'
944           NTuple<Descriptor> thisHeapPath = new NTuple<Descriptor>();
945           if (mapHeapPath.containsKey(fc.getThis())) {
946             thisHeapPath.addAll(mapHeapPath.get(fc.getThis()));
947           } else {
948             // method is called without creating new flat node representing
949             // 'this'
950             thisHeapPath.add(fc.getThis());
951           }
952           mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(0), thisHeapPath);
953         }
954       }
955
956       for (int i = 0; i < fc.numArgs(); i++) {
957         TempDescriptor arg = fc.getArg(i);
958         // create mapping arg to loc path
959         NTuple<Location> argLocationPath = deriveLocationTuple(mdCaller, arg);
960         if (argLocationPath != null) {
961           mapArgIdx2CallerArgLocationPath.put(Integer.valueOf(i + 1), argLocationPath);
962           // create mapping arg to heap path
963           NTuple<Descriptor> argHeapPath = computePath(arg);
964           mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(i + 1), argHeapPath);
965         }
966
967       }
968
969       for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
970         MethodDescriptor callee = (MethodDescriptor) iterator.next();
971         FlatMethod calleeFlatMethod = state.getMethodFlat(callee);
972
973         // binding caller's args and callee's params
974
975         Hashtable<NTuple<Descriptor>, NTuple<Descriptor>> mapParamHeapPathToCallerArgHeapPath =
976             new Hashtable<NTuple<Descriptor>, NTuple<Descriptor>>();
977
978         Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc =
979             new Hashtable<Integer, TempDescriptor>();
980         int offset = 0;
981         if (calleeFlatMethod.getMethod().isStatic()) {
982           // static method does not have implicit 'this' arg
983           offset = 1;
984         }
985
986         for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
987           TempDescriptor param = calleeFlatMethod.getParameter(i);
988           mapParamIdx2ParamTempDesc.put(Integer.valueOf(i + offset), param);
989
990           NTuple<Descriptor> calleeHeapPath = computePath(param);
991
992           NTuple<Descriptor> argHeapPath =
993               mapArgIdx2CallerArgHeapPath.get(Integer.valueOf(i + offset));
994
995           if (argHeapPath != null) {
996             mapParamHeapPathToCallerArgHeapPath.put(calleeHeapPath, argHeapPath);
997
998           }
999
1000         }
1001
1002         Set<Integer> keySet = mapArgIdx2CallerArgLocationPath.keySet();
1003         for (Iterator iterator2 = keySet.iterator(); iterator2.hasNext();) {
1004           Integer idx = (Integer) iterator2.next();
1005
1006           NTuple<Location> callerArgLocationPath = mapArgIdx2CallerArgLocationPath.get(idx);
1007
1008           TempDescriptor calleeParam = mapParamIdx2ParamTempDesc.get(idx);
1009           NTuple<Location> calleeLocationPath = deriveLocationTuple(mdCallee, calleeParam);
1010
1011           NTuple<Descriptor> callerArgHeapPath = mapArgIdx2CallerArgHeapPath.get(idx);
1012           NTuple<Descriptor> calleeHeapPath = computePath(calleeParam);
1013
1014           if (!calleeParam.getType().isPrimitive()) {
1015             createNewMappingOfMayWrittenSet(mdCaller, callee, callerArgHeapPath,
1016                 callerArgLocationPath, calleeHeapPath, calleeLocationPath,
1017                 mapParamHeapPathToCallerArgHeapPath);
1018           }
1019         }
1020
1021       }
1022
1023     }
1024
1025   }
1026
1027   private Hashtable<NTuple<Location>, Set<NTuple<Descriptor>>> getMappingByStartedWith(
1028       MultiSourceMap<NTuple<Location>, NTuple<Descriptor>> map, NTuple<Location> in) {
1029
1030     Hashtable<NTuple<Location>, Set<NTuple<Descriptor>>> matchedMapping =
1031         new Hashtable<NTuple<Location>, Set<NTuple<Descriptor>>>();
1032
1033     Set<NTuple<Location>> keySet = map.keySet();
1034
1035     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1036       NTuple<Location> key = (NTuple<Location>) iterator.next();
1037       if (key.startsWith(in)) {
1038         matchedMapping.put(key, map.get(key));
1039       }
1040     }
1041
1042     return matchedMapping;
1043
1044   }
1045
1046   private void createNewMappingOfMayWrittenSet(MethodDescriptor caller, MethodDescriptor callee,
1047       NTuple<Descriptor> callerArgHeapPath, NTuple<Location> callerArgLocPath,
1048       NTuple<Descriptor> calleeParamHeapPath, NTuple<Location> calleeParamLocPath,
1049       Hashtable<NTuple<Descriptor>, NTuple<Descriptor>> mapParamHeapPathToCallerArgHeapPath) {
1050
1051     // propagate may-written-set associated with the key that is started with
1052     // calleepath to the caller
1053     // 1) makes a new key by combining caller path and callee path(except local
1054     // loc element of param)
1055     // 2) create new mapping of may-written-set of callee path to caller path
1056
1057     // extract all may written effect accessed through callee param path
1058     MultiSourceMap<NTuple<Location>, NTuple<Descriptor>> calleeMapping =
1059         mapMethodToSharedLocCoverSet.get(callee);
1060
1061     if (calleeMapping == null) {
1062       return;
1063     }
1064
1065     MultiSourceMap<NTuple<Location>, NTuple<Descriptor>> callerMapping =
1066         mapMethodToSharedLocCoverSet.get(caller);
1067
1068     if (callerMapping == null) {
1069       callerMapping = new MultiSourceMap<NTuple<Location>, NTuple<Descriptor>>();
1070       mapMethodToSharedLocCoverSet.put(caller, callerMapping);
1071     }
1072
1073     Hashtable<NTuple<Location>, Set<NTuple<Descriptor>>> paramMapping =
1074         getMappingByStartedWith(calleeMapping, calleeParamLocPath);
1075
1076     Set<NTuple<Location>> calleeKeySet = paramMapping.keySet();
1077
1078     for (Iterator iterator = calleeKeySet.iterator(); iterator.hasNext();) {
1079       NTuple<Location> calleeKey = (NTuple<Location>) iterator.next();
1080
1081       Set<NTuple<Descriptor>> calleeMayWriteSet = paramMapping.get(calleeKey);
1082
1083       if (calleeMayWriteSet != null) {
1084
1085         Set<NTuple<Descriptor>> boundMayWriteSet = new HashSet<NTuple<Descriptor>>();
1086
1087         Set<NTuple<Descriptor>> boundSet =
1088             convertToCallerMayWriteSet(calleeParamHeapPath, calleeMayWriteSet, callerMapping,
1089                 mapParamHeapPathToCallerArgHeapPath);
1090
1091         boundMayWriteSet.addAll(boundSet);
1092
1093         NTuple<Location> newKey = new NTuple<Location>();
1094         newKey.addAll(callerArgLocPath);
1095         // need to replace the local location with the caller's path so skip the
1096         // local location of the parameter
1097         for (int i = 1; i < calleeKey.size(); i++) {
1098           newKey.add(calleeKey.get(i));
1099         }
1100
1101         callerMapping.union(newKey, boundMayWriteSet);
1102       }
1103
1104     }
1105
1106   }
1107
1108   private Set<NTuple<Descriptor>> convertToCallerMayWriteSet(
1109       NTuple<Descriptor> calleeParamHeapPath, Set<NTuple<Descriptor>> calleeMayWriteSet,
1110       MultiSourceMap<NTuple<Location>, NTuple<Descriptor>> callerMapping,
1111       Hashtable<NTuple<Descriptor>, NTuple<Descriptor>> mapParamHeapPathToCallerArgHeapPath) {
1112
1113     Set<NTuple<Descriptor>> boundSet = new HashSet<NTuple<Descriptor>>();
1114
1115     // replace callee's param path with caller's arg path
1116     for (Iterator iterator = calleeMayWriteSet.iterator(); iterator.hasNext();) {
1117       NTuple<Descriptor> calleeWriteHeapPath = (NTuple<Descriptor>) iterator.next();
1118
1119       NTuple<Descriptor> writeHeapPathParamHeapPath = calleeWriteHeapPath.subList(0, 1);
1120
1121       NTuple<Descriptor> callerArgHeapPath =
1122           mapParamHeapPathToCallerArgHeapPath.get(writeHeapPathParamHeapPath);
1123
1124       NTuple<Descriptor> boundHeapPath = new NTuple<Descriptor>();
1125       boundHeapPath.addAll(callerArgHeapPath);
1126
1127       for (int i = 1; i < calleeWriteHeapPath.size(); i++) {
1128         boundHeapPath.add(calleeWriteHeapPath.get(i));
1129       }
1130
1131       boundSet.add(boundHeapPath);
1132
1133     }
1134
1135     return boundSet;
1136   }
1137
1138   private Location getLocation(Descriptor d) {
1139
1140     if (d instanceof FieldDescriptor) {
1141       TypeExtension te = ((FieldDescriptor) d).getType().getExtension();
1142       if (te != null) {
1143         return (Location) te;
1144       }
1145     } else {
1146       assert d instanceof TempDescriptor;
1147       TempDescriptor td = (TempDescriptor) d;
1148
1149       TypeExtension te = td.getType().getExtension();
1150       if (te != null) {
1151         if (te instanceof SSJavaType) {
1152           SSJavaType ssType = (SSJavaType) te;
1153           if (ssType.getCompLoc() != null) {
1154             CompositeLocation comp = ssType.getCompLoc();
1155             return comp.get(comp.getSize() - 1);
1156           } else {
1157             return null;
1158           }
1159         } else {
1160           return (Location) te;
1161         }
1162       }
1163     }
1164
1165     return mapDescToLocation.get(d);
1166   }
1167
1168   private void eventLoopAnalysis() {
1169     // perform second stage analysis: intraprocedural analysis ensure that
1170     // all
1171     // variables are definitely written in-between the same read
1172
1173     Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
1174     flatNodesToVisit.add(ssjava.getSSJavaLoopEntrance());
1175
1176     while (!flatNodesToVisit.isEmpty()) {
1177       FlatNode fn = (FlatNode) flatNodesToVisit.iterator().next();
1178       flatNodesToVisit.remove(fn);
1179
1180       Hashtable<NTuple<Descriptor>, Set<WriteAge>> prev = mapFlatNodetoEventLoopMap.get(fn);
1181
1182       Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr =
1183           new Hashtable<NTuple<Descriptor>, Set<WriteAge>>();
1184       for (int i = 0; i < fn.numPrev(); i++) {
1185         FlatNode nn = fn.getPrev(i);
1186         Hashtable<NTuple<Descriptor>, Set<WriteAge>> in = mapFlatNodetoEventLoopMap.get(nn);
1187         if (in != null) {
1188           union(curr, in);
1189         }
1190       }
1191
1192       eventLoopAnalysis_nodeAction(fn, curr, ssjava.getSSJavaLoopEntrance());
1193
1194       // if a new result, schedule forward nodes for analysis
1195       if (!curr.equals(prev)) {
1196         mapFlatNodetoEventLoopMap.put(fn, curr);
1197
1198         for (int i = 0; i < fn.numNext(); i++) {
1199           FlatNode nn = fn.getNext(i);
1200           if (loopIncElements.contains(nn)) {
1201             flatNodesToVisit.add(nn);
1202           }
1203
1204         }
1205       }
1206     }
1207   }
1208
1209   private void union(Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr,
1210       Hashtable<NTuple<Descriptor>, Set<WriteAge>> in) {
1211
1212     Set<NTuple<Descriptor>> inKeySet = in.keySet();
1213     for (Iterator iterator = inKeySet.iterator(); iterator.hasNext();) {
1214       NTuple<Descriptor> inKey = (NTuple<Descriptor>) iterator.next();
1215       Set<WriteAge> inSet = in.get(inKey);
1216
1217       Set<WriteAge> currSet = curr.get(inKey);
1218
1219       if (currSet == null) {
1220         currSet = new HashSet<WriteAge>();
1221         curr.put(inKey, currSet);
1222       }
1223       currSet.addAll(inSet);
1224     }
1225
1226   }
1227
1228   private void eventLoopAnalysis_nodeAction(FlatNode fn,
1229       Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr, FlatNode loopEntrance) {
1230
1231     Hashtable<NTuple<Descriptor>, Set<WriteAge>> readWriteKillSet =
1232         new Hashtable<NTuple<Descriptor>, Set<WriteAge>>();
1233     Hashtable<NTuple<Descriptor>, Set<WriteAge>> readWriteGenSet =
1234         new Hashtable<NTuple<Descriptor>, Set<WriteAge>>();
1235
1236     if (fn.equals(loopEntrance)) {
1237       // it reaches loop entrance: changes all flag to true
1238       Set<NTuple<Descriptor>> keySet = curr.keySet();
1239       for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1240         NTuple<Descriptor> key = (NTuple<Descriptor>) iterator.next();
1241         Set<WriteAge> writeAgeSet = curr.get(key);
1242
1243         Set<WriteAge> incSet = new HashSet<WriteAge>();
1244         incSet.addAll(writeAgeSet);
1245         writeAgeSet.clear();
1246
1247         for (Iterator iterator2 = incSet.iterator(); iterator2.hasNext();) {
1248           WriteAge writeAge = (WriteAge) iterator2.next();
1249           WriteAge newWriteAge = writeAge.copy();
1250           newWriteAge.inc();
1251           writeAgeSet.add(newWriteAge);
1252         }
1253
1254       }
1255
1256     } else {
1257       TempDescriptor lhs;
1258       TempDescriptor rhs;
1259       FieldDescriptor fld;
1260
1261       switch (fn.kind()) {
1262
1263       case FKind.FlatOpNode: {
1264         FlatOpNode fon = (FlatOpNode) fn;
1265         lhs = fon.getDest();
1266         rhs = fon.getLeft();
1267
1268         if (fon.getOp().getOp() == Operation.ASSIGN) {
1269
1270           if (!lhs.getSymbol().startsWith("neverused") && !lhs.getSymbol().startsWith("leftop")
1271               && !lhs.getSymbol().startsWith("rightop")) {
1272
1273             boolean hasWriteEffect = false;
1274
1275             if (rhs.getType().getExtension() instanceof SSJavaType
1276                 && lhs.getType().getExtension() instanceof SSJavaType) {
1277
1278               CompositeLocation rhsCompLoc =
1279                   ((SSJavaType) rhs.getType().getExtension()).getCompLoc();
1280
1281               CompositeLocation lhsCompLoc =
1282                   ((SSJavaType) lhs.getType().getExtension()).getCompLoc();
1283
1284               if (lhsCompLoc != rhsCompLoc) {
1285                 // have a write effect!
1286                 hasWriteEffect = true;
1287               }
1288
1289             } else if (lhs.getType().isImmutable()) {
1290               hasWriteEffect = true;
1291             }
1292
1293             if (hasWriteEffect) {
1294               // write(lhs)
1295               NTuple<Descriptor> rhsHeapPath = computePath(rhs);
1296               NTuple<Descriptor> lhsHeapPath = new NTuple<Descriptor>();
1297               lhsHeapPath.addAll(rhsHeapPath);
1298
1299               Location lhsLoc = getLocation(lhs);
1300               if (ssjava.isSharedLocation(lhsLoc)) {
1301
1302                 NTuple<Descriptor> varHeapPath = computePath(lhs);
1303                 NTuple<Location> varLocTuple = mapDescriptorToLocationPath.get(lhs);
1304
1305                 Set<NTuple<Descriptor>> writtenSet =
1306                     mapFlatNodeToSharedLocMapping.get(fn).get(varLocTuple);
1307
1308                 if (isCovered(varLocTuple, writtenSet)) {
1309                   computeKILLSetForSharedWrite(curr, writtenSet, readWriteKillSet);
1310                   computeGENSetForSharedAllCoverWrite(curr, writtenSet, readWriteGenSet);
1311                 } else {
1312                   computeGENSetForSharedNonCoverWrite(curr, varHeapPath, readWriteGenSet);
1313                 }
1314
1315               } else {
1316
1317                 computeKILLSetForWrite(curr, lhsHeapPath, readWriteKillSet);
1318                 computeGENSetForWrite(lhsHeapPath, readWriteGenSet);
1319               }
1320
1321               // System.out.println("write effect on =" + lhsHeapPath);
1322               // System.out.println("#KILLSET=" + readWriteKillSet);
1323               // System.out.println("#GENSet=" + readWriteGenSet + "\n");
1324
1325               Set<WriteAge> writeAgeSet = curr.get(lhsHeapPath);
1326               checkWriteAgeSet(writeAgeSet, lhsHeapPath, fn);
1327             }
1328
1329           }
1330
1331         }
1332
1333       }
1334         break;
1335
1336       case FKind.FlatFieldNode:
1337       case FKind.FlatElementNode: {
1338
1339         if (fn.kind() == FKind.FlatFieldNode) {
1340           FlatFieldNode ffn = (FlatFieldNode) fn;
1341           lhs = ffn.getDst();
1342           rhs = ffn.getSrc();
1343           fld = ffn.getField();
1344         } else {
1345           FlatElementNode fen = (FlatElementNode) fn;
1346           lhs = fen.getDst();
1347           rhs = fen.getSrc();
1348           TypeDescriptor td = rhs.getType().dereference();
1349           fld = getArrayField(td);
1350         }
1351
1352         // read field
1353         NTuple<Descriptor> srcHeapPath = mapHeapPath.get(rhs);
1354         NTuple<Descriptor> fldHeapPath;
1355         if (srcHeapPath != null) {
1356           fldHeapPath = new NTuple<Descriptor>(srcHeapPath.getList());
1357         } else {
1358           // if srcHeapPath is null, it is static reference
1359           fldHeapPath = new NTuple<Descriptor>();
1360           fldHeapPath.add(rhs);
1361         }
1362         fldHeapPath.add(fld);
1363
1364         Set<WriteAge> writeAgeSet = curr.get(fldHeapPath);
1365
1366         checkWriteAgeSet(writeAgeSet, fldHeapPath, fn);
1367
1368       }
1369         break;
1370
1371       case FKind.FlatSetFieldNode:
1372       case FKind.FlatSetElementNode: {
1373
1374         if (fn.kind() == FKind.FlatSetFieldNode) {
1375           FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
1376           lhs = fsfn.getDst();
1377           fld = fsfn.getField();
1378         } else {
1379           FlatSetElementNode fsen = (FlatSetElementNode) fn;
1380           lhs = fsen.getDst();
1381           rhs = fsen.getSrc();
1382           TypeDescriptor td = lhs.getType().dereference();
1383           fld = getArrayField(td);
1384         }
1385
1386         // write(field)
1387         NTuple<Descriptor> lhsHeapPath = computePath(lhs);
1388         NTuple<Descriptor> fldHeapPath = new NTuple<Descriptor>(lhsHeapPath.getList());
1389         if (fn.kind() == FKind.FlatSetFieldNode) {
1390           fldHeapPath.add(fld);
1391         }
1392
1393         // shared loc extension
1394         Location fieldLoc;
1395         if (fn.kind() == FKind.FlatSetFieldNode) {
1396           fieldLoc = (Location) fld.getType().getExtension();
1397         } else {
1398           NTuple<Location> locTuple = mapDescriptorToLocationPath.get(lhs);
1399           fieldLoc = locTuple.get(locTuple.size() - 1);
1400         }
1401
1402         if (ssjava.isSharedLocation(fieldLoc)) {
1403
1404           NTuple<Location> fieldLocTuple = new NTuple<Location>();
1405           fieldLocTuple.addAll(mapDescriptorToLocationPath.get(lhs));
1406           if (fn.kind() == FKind.FlatSetFieldNode) {
1407             fieldLocTuple.add(fieldLoc);
1408           }
1409
1410           Set<NTuple<Descriptor>> writtenSet =
1411               mapFlatNodeToSharedLocMapping.get(fn).get(fieldLocTuple);
1412
1413           if (isCovered(fieldLocTuple, writtenSet)) {
1414             computeKILLSetForSharedWrite(curr, writtenSet, readWriteKillSet);
1415             computeGENSetForSharedAllCoverWrite(curr, writtenSet, readWriteGenSet);
1416           } else {
1417             computeGENSetForSharedNonCoverWrite(curr, fldHeapPath, readWriteGenSet);
1418           }
1419
1420         } else {
1421           computeKILLSetForWrite(curr, fldHeapPath, readWriteKillSet);
1422           computeGENSetForWrite(fldHeapPath, readWriteGenSet);
1423         }
1424
1425         // System.out.println("KILLSET=" + readWriteKillSet);
1426         // System.out.println("GENSet=" + readWriteGenSet);
1427
1428       }
1429         break;
1430
1431       case FKind.FlatCall: {
1432         FlatCall fc = (FlatCall) fn;
1433
1434         SharedLocMap sharedLocMap = mapFlatNodeToSharedLocMapping.get(fc);
1435         // System.out.println("FLATCALL:" + fn);
1436         generateKILLSetForFlatCall(fc, curr, sharedLocMap, readWriteKillSet);
1437         generateGENSetForFlatCall(fc, sharedLocMap, readWriteGenSet);
1438
1439         // System.out.println("KILLSET=" + readWriteKillSet);
1440         // System.out.println("GENSet=" + readWriteGenSet);
1441
1442         checkManyRead(fc, curr);
1443       }
1444         break;
1445
1446       }
1447
1448       computeNewMapping(curr, readWriteKillSet, readWriteGenSet);
1449       // System.out.println("#######" + curr);
1450
1451     }
1452
1453   }
1454
1455   private void computeGENSetForSharedNonCoverWrite(
1456       Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr, NTuple<Descriptor> heapPath,
1457       Hashtable<NTuple<Descriptor>, Set<WriteAge>> genSet) {
1458
1459     Set<WriteAge> writeAgeSet = genSet.get(heapPath);
1460     if (writeAgeSet == null) {
1461       writeAgeSet = new HashSet<WriteAge>();
1462       genSet.put(heapPath, writeAgeSet);
1463     }
1464
1465     writeAgeSet.add(new WriteAge(1));
1466
1467   }
1468
1469   private void computeGENSetForSharedAllCoverWrite(
1470       Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr, Set<NTuple<Descriptor>> writtenSet,
1471       Hashtable<NTuple<Descriptor>, Set<WriteAge>> genSet) {
1472
1473     for (Iterator iterator = writtenSet.iterator(); iterator.hasNext();) {
1474       NTuple<Descriptor> writeHeapPath = (NTuple<Descriptor>) iterator.next();
1475
1476       Set<WriteAge> writeAgeSet = new HashSet<WriteAge>();
1477       writeAgeSet.add(new WriteAge(0));
1478
1479       genSet.put(writeHeapPath, writeAgeSet);
1480     }
1481
1482   }
1483
1484   private void computeKILLSetForSharedWrite(Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr,
1485       Set<NTuple<Descriptor>> writtenSet, Hashtable<NTuple<Descriptor>, Set<WriteAge>> killSet) {
1486
1487     for (Iterator iterator = writtenSet.iterator(); iterator.hasNext();) {
1488       NTuple<Descriptor> writeHeapPath = (NTuple<Descriptor>) iterator.next();
1489       Set<WriteAge> writeSet = curr.get(writeHeapPath);
1490       if (writeSet != null) {
1491         killSet.put(writeHeapPath, writeSet);
1492       }
1493     }
1494
1495   }
1496
1497   private boolean isCovered(NTuple<Location> locTuple, Set<NTuple<Descriptor>> inSet) {
1498
1499     if (inSet == null) {
1500       return false;
1501     }
1502
1503     Set<NTuple<Descriptor>> coverSet =
1504         mapMethodToSharedLocCoverSet.get(methodContainingSSJavaLoop).get(locTuple);
1505
1506     return inSet.containsAll(coverSet);
1507   }
1508
1509   private void checkManyRead(FlatCall fc, Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr) {
1510
1511     Set<NTuple<Descriptor>> boundReadSet = mapFlatNodeToBoundReadSet.get(fc);
1512
1513     for (Iterator iterator = boundReadSet.iterator(); iterator.hasNext();) {
1514       NTuple<Descriptor> readHeapPath = (NTuple<Descriptor>) iterator.next();
1515       Set<WriteAge> writeAgeSet = curr.get(readHeapPath);
1516       checkWriteAgeSet(writeAgeSet, readHeapPath, fc);
1517     }
1518
1519   }
1520
1521   private void checkWriteAgeSet(Set<WriteAge> writeAgeSet, NTuple<Descriptor> path, FlatNode fn) {
1522
1523     // System.out.println("# CHECK WRITE AGE of " + path + " from set=" +
1524     // writeAgeSet);
1525
1526     if (writeAgeSet != null) {
1527       for (Iterator iterator = writeAgeSet.iterator(); iterator.hasNext();) {
1528         WriteAge writeAge = (WriteAge) iterator.next();
1529         if (writeAge.getAge() > MAXAGE) {
1530           generateErrorMessage(path, fn);
1531         }
1532       }
1533     }
1534   }
1535
1536   private void generateErrorMessage(NTuple<Descriptor> path, FlatNode fn) {
1537
1538     Descriptor lastDesc = path.get(getArrayBaseDescriptorIdx(path));
1539     if (ssjava.isSharedLocation(getLocation(lastDesc))) {
1540
1541       NTuple<Location> locPathTuple = getLocationTuple(path);
1542       Set<NTuple<Descriptor>> coverSet =
1543           mapMethodToSharedLocCoverSet.get(methodContainingSSJavaLoop).get(locPathTuple);
1544       throw new Error("Shared memory locations, which is reachable through references " + path
1545           + ", are not completely overwritten by the higher values at "
1546           + methodContainingSSJavaLoop.getClassDesc().getSourceFileName() + "::" + fn.getNumLine()
1547           + ".\nThe following memory locations belong to the same shared locations:" + coverSet);
1548
1549     } else {
1550       throw new Error(
1551           "Memory location, which is reachable through references "
1552               + path
1553               + ", who comes back to the same read statement without being overwritten at the out-most iteration at "
1554               + methodContainingSSJavaLoop.getClassDesc().getSourceFileName() + "::"
1555               + fn.getNumLine());
1556     }
1557
1558   }
1559
1560   private void generateGENSetForFlatCall(FlatCall fc, SharedLocMap sharedLocMap,
1561       Hashtable<NTuple<Descriptor>, Set<WriteAge>> GENSet) {
1562
1563     Set<NTuple<Descriptor>> boundMayWriteSet = mapFlatNodeToBoundMayWriteSet.get(fc);
1564     // System.out.println("boundMayWriteSet=" + boundMayWriteSet);
1565
1566     for (Iterator iterator = boundMayWriteSet.iterator(); iterator.hasNext();) {
1567       NTuple<Descriptor> heapPath = (NTuple<Descriptor>) iterator.next();
1568
1569       if (!isSharedLocation(heapPath)) {
1570         addWriteAgeToSet(heapPath, GENSet, new WriteAge(0));
1571       } else {
1572         // if the current heap path is shared location
1573
1574         NTuple<Location> locTuple = getLocationTuple(heapPath);
1575
1576         Set<NTuple<Descriptor>> sharedWriteHeapPathSet = sharedLocMap.get(locTuple);
1577
1578         if (isCovered(locTuple, sharedLocMap.get(locTuple))) {
1579           // if it is covered, add all of heap paths belong to the same shared
1580           // loc with write age 0
1581
1582           for (Iterator iterator2 = sharedWriteHeapPathSet.iterator(); iterator2.hasNext();) {
1583             NTuple<Descriptor> sharedHeapPath = (NTuple<Descriptor>) iterator2.next();
1584             addWriteAgeToSet(sharedHeapPath, GENSet, new WriteAge(0));
1585           }
1586
1587         } else {
1588           // if not covered, add write age 1 to the heap path that is
1589           // may-written but not covered
1590           addWriteAgeToSet(heapPath, GENSet, new WriteAge(1));
1591         }
1592
1593       }
1594
1595     }
1596
1597   }
1598
1599   private void addWriteAgeToSet(NTuple<Descriptor> heapPath,
1600       Hashtable<NTuple<Descriptor>, Set<WriteAge>> map, WriteAge age) {
1601
1602     Set<WriteAge> currSet = map.get(heapPath);
1603     if (currSet == null) {
1604       currSet = new HashSet<WriteAge>();
1605       map.put(heapPath, currSet);
1606     }
1607
1608     currSet.add(age);
1609   }
1610
1611   private void generateKILLSetForFlatCall(FlatCall fc,
1612       Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr, SharedLocMap sharedLocMap,
1613       Hashtable<NTuple<Descriptor>, Set<WriteAge>> KILLSet) {
1614
1615     Set<NTuple<Descriptor>> boundMustWriteSet = mapFlatNodeToBoundMustWriteSet.get(fc);
1616     System.out.println("boundMustWriteSet=" + boundMustWriteSet);
1617
1618     for (Iterator iterator = boundMustWriteSet.iterator(); iterator.hasNext();) {
1619       NTuple<Descriptor> heapPath = (NTuple<Descriptor>) iterator.next();
1620
1621       if (isSharedLocation(heapPath)) {
1622         NTuple<Location> locTuple = getLocationTuple(heapPath);
1623
1624         if (isCovered(locTuple, sharedLocMap.get(locTuple))) {
1625           // if it is shared loc and corresponding shared loc has been covered
1626           KILLSet.put(heapPath, curr.get(heapPath));
1627         }
1628       } else {
1629
1630         for (Enumeration<NTuple<Descriptor>> e = curr.keys(); e.hasMoreElements();) {
1631           NTuple<Descriptor> key = e.nextElement();
1632           if (key.startsWith(heapPath)) {
1633             KILLSet.put(key, curr.get(key));
1634           }
1635         }
1636
1637       }
1638
1639     }
1640
1641   }
1642
1643   private int getArrayBaseDescriptorIdx(NTuple<Descriptor> heapPath) {
1644
1645     for (int i = heapPath.size() - 1; i >= 0; i--) {
1646       if (!heapPath.get(i).getSymbol().equals(arrayElementFieldName)) {
1647         return i;
1648       }
1649     }
1650
1651     return -1;
1652
1653   }
1654
1655   private boolean isSharedLocation(NTuple<Descriptor> heapPath) {
1656
1657     Descriptor d = heapPath.get(getArrayBaseDescriptorIdx(heapPath));
1658
1659     return ssjava.isSharedLocation(getLocation(heapPath.get(getArrayBaseDescriptorIdx(heapPath))));
1660
1661   }
1662
1663   private NTuple<Location> getLocationTuple(NTuple<Descriptor> heapPath) {
1664
1665     NTuple<Location> locTuple = new NTuple<Location>();
1666
1667     locTuple.addAll(mapDescriptorToLocationPath.get(heapPath.get(0)));
1668
1669     for (int i = 1; i <= getArrayBaseDescriptorIdx(heapPath); i++) {
1670       locTuple.add(getLocation(heapPath.get(i)));
1671     }
1672
1673     return locTuple;
1674   }
1675
1676   private void computeNewMapping(Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr,
1677       Hashtable<NTuple<Descriptor>, Set<WriteAge>> KILLSet,
1678       Hashtable<NTuple<Descriptor>, Set<WriteAge>> GENSet) {
1679
1680     for (Enumeration<NTuple<Descriptor>> e = KILLSet.keys(); e.hasMoreElements();) {
1681       NTuple<Descriptor> key = e.nextElement();
1682
1683       Set<WriteAge> writeAgeSet = curr.get(key);
1684       if (writeAgeSet == null) {
1685         writeAgeSet = new HashSet<WriteAge>();
1686         curr.put(key, writeAgeSet);
1687       }
1688       writeAgeSet.removeAll(KILLSet.get(key));
1689     }
1690
1691     for (Enumeration<NTuple<Descriptor>> e = GENSet.keys(); e.hasMoreElements();) {
1692       NTuple<Descriptor> key = e.nextElement();
1693
1694       Set<WriteAge> currWriteAgeSet = curr.get(key);
1695       if (currWriteAgeSet == null) {
1696         currWriteAgeSet = new HashSet<WriteAge>();
1697         curr.put(key, currWriteAgeSet);
1698       }
1699       currWriteAgeSet.addAll(GENSet.get(key));
1700     }
1701
1702   }
1703
1704   private void computeGENSetForWrite(NTuple<Descriptor> fldHeapPath,
1705       Hashtable<NTuple<Descriptor>, Set<WriteAge>> GENSet) {
1706
1707     // generate write age 0 for the field being written to
1708     Set<WriteAge> writeAgeSet = new HashSet<WriteAge>();
1709     writeAgeSet.add(new WriteAge(0));
1710     GENSet.put(fldHeapPath, writeAgeSet);
1711
1712   }
1713
1714   private void computeKILLSetForWrite(Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr,
1715       NTuple<Descriptor> hp, Hashtable<NTuple<Descriptor>, Set<WriteAge>> KILLSet) {
1716
1717     // removes all of heap path that starts with prefix 'hp'
1718     // since any reference overwrite along heap path gives overwriting side
1719     // effects on the value
1720
1721     Set<NTuple<Descriptor>> keySet = curr.keySet();
1722     for (Iterator<NTuple<Descriptor>> iter = keySet.iterator(); iter.hasNext();) {
1723       NTuple<Descriptor> key = iter.next();
1724       if (key.startsWith(hp)) {
1725         KILLSet.put(key, curr.get(key));
1726       }
1727     }
1728
1729   }
1730
1731   private void bindHeapPathCallerArgWithCalleeParam(FlatCall fc) {
1732     // compute all possible callee set
1733     // transform all READ/WRITE set from the any possible
1734     // callees to the caller
1735     calleeUnionBoundReadSet.clear();
1736     calleeIntersectBoundMustWriteSet.clear();
1737     calleeUnionBoundMayWriteSet.clear();
1738
1739     if (ssjava.isSSJavaUtil(fc.getMethod().getClassDesc())) {
1740       // ssjava util case!
1741       // have write effects on the first argument
1742       TempDescriptor arg = fc.getArg(0);
1743       NTuple<Descriptor> argHeapPath = computePath(arg);
1744       calleeIntersectBoundMustWriteSet.add(argHeapPath);
1745       calleeUnionBoundMayWriteSet.add(argHeapPath);
1746     } else {
1747       MethodDescriptor mdCallee = fc.getMethod();
1748       Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
1749       setPossibleCallees.addAll(callGraph.getMethods(mdCallee));
1750
1751       // create mapping from arg idx to its heap paths
1752       Hashtable<Integer, NTuple<Descriptor>> mapArgIdx2CallerArgHeapPath =
1753           new Hashtable<Integer, NTuple<Descriptor>>();
1754
1755       // arg idx is starting from 'this' arg
1756       if (fc.getThis() != null) {
1757         NTuple<Descriptor> thisHeapPath = mapHeapPath.get(fc.getThis());
1758         if (thisHeapPath == null) {
1759           // method is called without creating new flat node representing 'this'
1760           thisHeapPath = new NTuple<Descriptor>();
1761           thisHeapPath.add(fc.getThis());
1762         }
1763
1764         mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(0), thisHeapPath);
1765       }
1766
1767       for (int i = 0; i < fc.numArgs(); i++) {
1768         TempDescriptor arg = fc.getArg(i);
1769         NTuple<Descriptor> argHeapPath = computePath(arg);
1770         mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(i + 1), argHeapPath);
1771       }
1772
1773       for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
1774         MethodDescriptor callee = (MethodDescriptor) iterator.next();
1775         FlatMethod calleeFlatMethod = state.getMethodFlat(callee);
1776
1777         // binding caller's args and callee's params
1778
1779         Set<NTuple<Descriptor>> calleeReadSet = mapFlatMethodToReadSet.get(calleeFlatMethod);
1780         if (calleeReadSet == null) {
1781           calleeReadSet = new HashSet<NTuple<Descriptor>>();
1782           mapFlatMethodToReadSet.put(calleeFlatMethod, calleeReadSet);
1783         }
1784
1785         Set<NTuple<Descriptor>> calleeMustWriteSet =
1786             mapFlatMethodToMustWriteSet.get(calleeFlatMethod);
1787
1788         if (calleeMustWriteSet == null) {
1789           calleeMustWriteSet = new HashSet<NTuple<Descriptor>>();
1790           mapFlatMethodToMustWriteSet.put(calleeFlatMethod, calleeMustWriteSet);
1791         }
1792
1793         Set<NTuple<Descriptor>> calleeMayWriteSet =
1794             mapFlatMethodToMayWriteSet.get(calleeFlatMethod);
1795
1796         if (calleeMayWriteSet == null) {
1797           calleeMayWriteSet = new HashSet<NTuple<Descriptor>>();
1798           mapFlatMethodToMayWriteSet.put(calleeFlatMethod, calleeMayWriteSet);
1799         }
1800
1801         Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc =
1802             new Hashtable<Integer, TempDescriptor>();
1803         int offset = 0;
1804         if (calleeFlatMethod.getMethod().isStatic()) {
1805           // static method does not have implicit 'this' arg
1806           offset = 1;
1807         }
1808         for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
1809           TempDescriptor param = calleeFlatMethod.getParameter(i);
1810           mapParamIdx2ParamTempDesc.put(Integer.valueOf(i + offset), param);
1811         }
1812
1813         Set<NTuple<Descriptor>> calleeBoundReadSet =
1814             bindSet(calleeReadSet, mapParamIdx2ParamTempDesc, mapArgIdx2CallerArgHeapPath);
1815         // union of the current read set and the current callee's
1816         // read set
1817         calleeUnionBoundReadSet.addAll(calleeBoundReadSet);
1818
1819         Set<NTuple<Descriptor>> calleeBoundMustWriteSet =
1820             bindSet(calleeMustWriteSet, mapParamIdx2ParamTempDesc, mapArgIdx2CallerArgHeapPath);
1821         // intersection of the current overwrite set and the current
1822         // callee's
1823         // overwrite set
1824         merge(calleeIntersectBoundMustWriteSet, calleeBoundMustWriteSet);
1825
1826         Set<NTuple<Descriptor>> boundWriteSetFromCallee =
1827             bindSet(calleeMayWriteSet, mapParamIdx2ParamTempDesc, mapArgIdx2CallerArgHeapPath);
1828         calleeUnionBoundMayWriteSet.addAll(boundWriteSetFromCallee);
1829       }
1830
1831     }
1832
1833   }
1834
1835   private void bindHeapPathCallerArgWithCaleeParamForSharedLoc(MethodDescriptor mdCaller,
1836       FlatCall fc) {
1837
1838     calleeIntersectBoundSharedSet.clear();
1839     calleeUnionBoundDeleteSet.clear();
1840
1841     if (ssjava.isSSJavaUtil(fc.getMethod().getClassDesc())) {
1842       // ssjava util case!
1843       // have write effects on the first argument
1844       TempDescriptor arg = fc.getArg(0);
1845       NTuple<Descriptor> argHeapPath = computePath(arg);
1846
1847       // convert heap path to location path
1848       NTuple<Location> argLocTuple = new NTuple<Location>();
1849       argLocTuple.addAll(deriveLocationTuple(mdCaller, (TempDescriptor) argHeapPath.get(0)));
1850       for (int i = 1; i < argHeapPath.size(); i++) {
1851         argLocTuple.add(getLocation(argHeapPath.get(i)));
1852       }
1853
1854       calleeIntersectBoundSharedSet.addWrite(argLocTuple, argHeapPath);
1855
1856     } else if (ssjava.needTobeAnnotated(fc.getMethod())) {
1857
1858       // if arg is not primitive type, we need to propagate maywritten set to
1859       // the caller's location path
1860
1861       MethodDescriptor mdCallee = fc.getMethod();
1862       Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
1863       setPossibleCallees.addAll(callGraph.getMethods(mdCallee));
1864
1865       // create mapping from arg idx to its heap paths
1866       Hashtable<Integer, NTuple<Descriptor>> mapArgIdx2CallerArgHeapPath =
1867           new Hashtable<Integer, NTuple<Descriptor>>();
1868
1869       // arg idx is starting from 'this' arg
1870       if (fc.getThis() != null) {
1871         NTuple<Descriptor> thisHeapPath = mapHeapPath.get(fc.getThis());
1872         if (thisHeapPath == null) {
1873           // method is called without creating new flat node representing 'this'
1874           thisHeapPath = new NTuple<Descriptor>();
1875           thisHeapPath.add(fc.getThis());
1876         }
1877
1878         mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(0), thisHeapPath);
1879       }
1880
1881       for (int i = 0; i < fc.numArgs(); i++) {
1882         TempDescriptor arg = fc.getArg(i);
1883         NTuple<Descriptor> argHeapPath = computePath(arg);
1884         mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(i + 1), argHeapPath);
1885       }
1886
1887       // create mapping from arg idx to its location paths
1888       Hashtable<Integer, NTuple<Location>> mapArgIdx2CallerAgLocationPath =
1889           new Hashtable<Integer, NTuple<Location>>();
1890
1891       // arg idx is starting from 'this' arg
1892       if (fc.getThis() != null) {
1893         NTuple<Location> thisLocationPath = deriveLocationTuple(mdCaller, fc.getThis());
1894         if (thisLocationPath != null) {
1895           mapArgIdx2CallerAgLocationPath.put(Integer.valueOf(0), thisLocationPath);
1896         }
1897       }
1898
1899       for (int i = 0; i < fc.numArgs(); i++) {
1900         TempDescriptor arg = fc.getArg(i);
1901         NTuple<Location> argLocationPath = deriveLocationTuple(mdCaller, arg);
1902         if (argLocationPath != null) {
1903           mapArgIdx2CallerAgLocationPath.put(Integer.valueOf(i + 1), argLocationPath);
1904         }
1905       }
1906
1907       for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
1908         MethodDescriptor callee = (MethodDescriptor) iterator.next();
1909         FlatMethod calleeFlatMethod = state.getMethodFlat(callee);
1910
1911         // binding caller's args and callee's params
1912
1913         Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc =
1914             new Hashtable<Integer, TempDescriptor>();
1915         int offset = 0;
1916         if (calleeFlatMethod.getMethod().isStatic()) {
1917           // static method does not have implicit 'this' arg
1918           offset = 1;
1919         }
1920         for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
1921           TempDescriptor param = calleeFlatMethod.getParameter(i);
1922           mapParamIdx2ParamTempDesc.put(Integer.valueOf(i + offset), param);
1923         }
1924
1925         Set<Integer> keySet = mapArgIdx2CallerAgLocationPath.keySet();
1926         for (Iterator iterator2 = keySet.iterator(); iterator2.hasNext();) {
1927           Integer idx = (Integer) iterator2.next();
1928           NTuple<Location> callerArgLocationPath = mapArgIdx2CallerAgLocationPath.get(idx);
1929           NTuple<Descriptor> callerArgHeapPath = mapArgIdx2CallerArgHeapPath.get(idx);
1930
1931           TempDescriptor calleeParam = mapParamIdx2ParamTempDesc.get(idx);
1932           NTuple<Location> calleeLocationPath = deriveLocationTuple(mdCallee, calleeParam);
1933           SharedLocMap calleeDeleteSet = mapFlatMethodToDeleteSet.get(calleeFlatMethod);
1934           SharedLocMap calleeSharedLocMap = mapFlatMethodToSharedLocMap.get(calleeFlatMethod);
1935
1936           if (calleeDeleteSet != null) {
1937             createNewMappingOfDeleteSet(callerArgLocationPath, callerArgHeapPath,
1938                 calleeLocationPath, calleeDeleteSet);
1939           }
1940
1941           if (calleeSharedLocMap != null) {
1942             createNewMappingOfSharedSet(callerArgLocationPath, callerArgHeapPath,
1943                 calleeLocationPath, calleeSharedLocMap);
1944           }
1945
1946         }
1947
1948       }
1949     }
1950
1951   }
1952
1953   private void createNewMappingOfDeleteSet(NTuple<Location> callerArgLocationPath,
1954       NTuple<Descriptor> callerArgHeapPath, NTuple<Location> calleeLocationPath,
1955       SharedLocMap calleeDeleteSet) {
1956
1957     SharedLocMap calleeParamDeleteSet = calleeDeleteSet.getHeapPathStartedWith(calleeLocationPath);
1958
1959     Set<NTuple<Location>> keySet = calleeParamDeleteSet.keySet();
1960     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1961       NTuple<Location> calleeLocTupleKey = (NTuple<Location>) iterator.next();
1962       Set<NTuple<Descriptor>> heapPathSet = calleeParamDeleteSet.get(calleeLocTupleKey);
1963       for (Iterator iterator2 = heapPathSet.iterator(); iterator2.hasNext();) {
1964         NTuple<Descriptor> calleeHeapPath = (NTuple<Descriptor>) iterator2.next();
1965         calleeUnionBoundDeleteSet.addWrite(
1966             bindLocationPath(callerArgLocationPath, calleeLocTupleKey),
1967             bindHeapPath(callerArgHeapPath, calleeHeapPath));
1968       }
1969     }
1970
1971   }
1972
1973   private void createNewMappingOfSharedSet(NTuple<Location> callerArgLocationPath,
1974       NTuple<Descriptor> callerArgHeapPath, NTuple<Location> calleeLocationPath,
1975       SharedLocMap calleeSharedLocMap) {
1976
1977     SharedLocMap calleeParamSharedSet =
1978         calleeSharedLocMap.getHeapPathStartedWith(calleeLocationPath);
1979
1980     Set<NTuple<Location>> keySet = calleeParamSharedSet.keySet();
1981     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1982       NTuple<Location> calleeLocTupleKey = (NTuple<Location>) iterator.next();
1983       Set<NTuple<Descriptor>> heapPathSet = calleeParamSharedSet.get(calleeLocTupleKey);
1984       Set<NTuple<Descriptor>> boundHeapPathSet = new HashSet<NTuple<Descriptor>>();
1985       for (Iterator iterator2 = heapPathSet.iterator(); iterator2.hasNext();) {
1986         NTuple<Descriptor> calleeHeapPath = (NTuple<Descriptor>) iterator2.next();
1987         boundHeapPathSet.add(bindHeapPath(callerArgHeapPath, calleeHeapPath));
1988       }
1989       calleeIntersectBoundSharedSet.intersect(
1990           bindLocationPath(callerArgLocationPath, calleeLocTupleKey), boundHeapPathSet);
1991     }
1992
1993   }
1994
1995   private NTuple<Location> bindLocationPath(NTuple<Location> start, NTuple<Location> end) {
1996     NTuple<Location> locPath = new NTuple<Location>();
1997     locPath.addAll(start);
1998     for (int i = 1; i < end.size(); i++) {
1999       locPath.add(end.get(i));
2000     }
2001     return locPath;
2002   }
2003
2004   private NTuple<Descriptor> bindHeapPath(NTuple<Descriptor> start, NTuple<Descriptor> end) {
2005     NTuple<Descriptor> heapPath = new NTuple<Descriptor>();
2006     heapPath.addAll(start);
2007     for (int i = 1; i < end.size(); i++) {
2008       heapPath.add(end.get(i));
2009     }
2010     return heapPath;
2011   }
2012
2013   private void initialize() {
2014     // First, identify ssjava loop entrace
2015
2016     // no need to analyze method having ssjava loop
2017     methodContainingSSJavaLoop = ssjava.getMethodContainingSSJavaLoop();
2018
2019     FlatMethod fm = state.getMethodFlat(methodContainingSSJavaLoop);
2020     Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
2021     flatNodesToVisit.add(fm);
2022
2023     LoopFinder loopFinder = new LoopFinder(fm);
2024
2025     while (!flatNodesToVisit.isEmpty()) {
2026       FlatNode fn = flatNodesToVisit.iterator().next();
2027       flatNodesToVisit.remove(fn);
2028
2029       String label = (String) state.fn2labelMap.get(fn);
2030       if (label != null) {
2031
2032         if (label.equals(ssjava.SSJAVA)) {
2033           ssjava.setSSJavaLoopEntrance(fn);
2034           break;
2035         }
2036       }
2037
2038       for (int i = 0; i < fn.numNext(); i++) {
2039         FlatNode nn = fn.getNext(i);
2040         flatNodesToVisit.add(nn);
2041       }
2042     }
2043
2044     assert ssjava.getSSJavaLoopEntrance() != null;
2045
2046     // assume that ssjava loop is top-level loop in method, not nested loop
2047     Set nestedLoop = loopFinder.nestedLoops();
2048     for (Iterator loopIter = nestedLoop.iterator(); loopIter.hasNext();) {
2049       LoopFinder lf = (LoopFinder) loopIter.next();
2050       if (lf.loopEntrances().iterator().next().equals(ssjava.getSSJavaLoopEntrance())) {
2051         ssjavaLoop = lf;
2052       }
2053     }
2054
2055     assert ssjavaLoop != null;
2056
2057     loopIncElements = (Set<FlatNode>) ssjavaLoop.loopIncElements();
2058
2059     // perform topological sort over the set of methods accessed by the main
2060     // event loop
2061     Set<MethodDescriptor> methodDescriptorsToAnalyze = new HashSet<MethodDescriptor>();
2062     methodDescriptorsToAnalyze.addAll(ssjava.getAnnotationRequireSet());
2063     sortedDescriptors = topologicalSort(methodDescriptorsToAnalyze);
2064   }
2065
2066   private void methodReadWriteSetAnalysis() {
2067     // perform method READ/OVERWRITE analysis
2068     LinkedList<MethodDescriptor> descriptorListToAnalyze =
2069         (LinkedList<MethodDescriptor>) sortedDescriptors.clone();
2070
2071     // current descriptors to visit in fixed-point interprocedural analysis,
2072     // prioritized by
2073     // dependency in the call graph
2074     methodDescriptorsToVisitStack.clear();
2075
2076     descriptorListToAnalyze.removeFirst();
2077
2078     Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
2079     methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
2080
2081     while (!descriptorListToAnalyze.isEmpty()) {
2082       MethodDescriptor md = descriptorListToAnalyze.removeFirst();
2083       methodDescriptorsToVisitStack.add(md);
2084     }
2085
2086     // analyze scheduled methods until there are no more to visit
2087     while (!methodDescriptorsToVisitStack.isEmpty()) {
2088       // start to analyze leaf node
2089       MethodDescriptor md = methodDescriptorsToVisitStack.pop();
2090       FlatMethod fm = state.getMethodFlat(md);
2091
2092       Set<NTuple<Descriptor>> readSet = new HashSet<NTuple<Descriptor>>();
2093       Set<NTuple<Descriptor>> mustWriteSet = new HashSet<NTuple<Descriptor>>();
2094       Set<NTuple<Descriptor>> mayWriteSet = new HashSet<NTuple<Descriptor>>();
2095
2096       methodReadWriteSet_analyzeMethod(fm, readSet, mustWriteSet, mayWriteSet);
2097
2098       Set<NTuple<Descriptor>> prevRead = mapFlatMethodToReadSet.get(fm);
2099       Set<NTuple<Descriptor>> prevMustWrite = mapFlatMethodToMustWriteSet.get(fm);
2100       Set<NTuple<Descriptor>> prevMayWrite = mapFlatMethodToMayWriteSet.get(fm);
2101
2102       if (!(readSet.equals(prevRead) && mustWriteSet.equals(prevMustWrite) && mayWriteSet
2103           .equals(prevMayWrite))) {
2104         mapFlatMethodToReadSet.put(fm, readSet);
2105         mapFlatMethodToMustWriteSet.put(fm, mustWriteSet);
2106         mapFlatMethodToMayWriteSet.put(fm, mayWriteSet);
2107
2108         // results for callee changed, so enqueue dependents caller for
2109         // further
2110         // analysis
2111         Iterator<MethodDescriptor> depsItr = getDependents(md).iterator();
2112         while (depsItr.hasNext()) {
2113           MethodDescriptor methodNext = depsItr.next();
2114           if (!methodDescriptorsToVisitStack.contains(methodNext)
2115               && methodDescriptorToVistSet.contains(methodNext)) {
2116             methodDescriptorsToVisitStack.add(methodNext);
2117           }
2118
2119         }
2120
2121       }
2122
2123     }
2124
2125     methodReadWriteSetAnalysisToEventLoopBody();
2126
2127   }
2128
2129   private void methodReadWriteSet_analyzeMethod(FlatMethod fm, Set<NTuple<Descriptor>> readSet,
2130       Set<NTuple<Descriptor>> mustWriteSet, Set<NTuple<Descriptor>> mayWriteSet) {
2131     if (state.SSJAVADEBUG) {
2132       System.out.println("SSJAVA: Definitely written Analyzing: " + fm);
2133     }
2134
2135     methodReadWriteSet_analyzeBody(fm, readSet, mustWriteSet, mayWriteSet, false);
2136
2137   }
2138
2139   private void methodReadWriteSetAnalysisToEventLoopBody() {
2140
2141     // perform method read/write analysis for Event Loop Body
2142
2143     FlatMethod flatMethodContainingSSJavaLoop = state.getMethodFlat(methodContainingSSJavaLoop);
2144
2145     if (state.SSJAVADEBUG) {
2146       System.out.println("SSJAVA: Definitely written Event Loop Analyzing: "
2147           + flatMethodContainingSSJavaLoop);
2148     }
2149
2150     Set<NTuple<Descriptor>> readSet = new HashSet<NTuple<Descriptor>>();
2151     Set<NTuple<Descriptor>> mustWriteSet = new HashSet<NTuple<Descriptor>>();
2152     Set<NTuple<Descriptor>> mayWriteSet = new HashSet<NTuple<Descriptor>>();
2153
2154     mapFlatMethodToReadSet.put(flatMethodContainingSSJavaLoop, readSet);
2155     mapFlatMethodToMustWriteSet.put(flatMethodContainingSSJavaLoop, mustWriteSet);
2156     mapFlatMethodToMayWriteSet.put(flatMethodContainingSSJavaLoop, mayWriteSet);
2157
2158     methodReadWriteSet_analyzeBody(ssjava.getSSJavaLoopEntrance(), readSet, mustWriteSet,
2159         mayWriteSet, true);
2160
2161   }
2162
2163   private void methodReadWriteSet_analyzeBody(FlatNode startNode, Set<NTuple<Descriptor>> readSet,
2164       Set<NTuple<Descriptor>> mustWriteSet, Set<NTuple<Descriptor>> mayWriteSet,
2165       boolean isEventLoopBody) {
2166
2167     // intraprocedural analysis
2168     Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
2169     flatNodesToVisit.add(startNode);
2170
2171     while (!flatNodesToVisit.isEmpty()) {
2172       FlatNode fn = flatNodesToVisit.iterator().next();
2173       flatNodesToVisit.remove(fn);
2174
2175       Set<NTuple<Descriptor>> currMustWriteSet = new HashSet<NTuple<Descriptor>>();
2176
2177       for (int i = 0; i < fn.numPrev(); i++) {
2178         FlatNode prevFn = fn.getPrev(i);
2179         Set<NTuple<Descriptor>> in = mapFlatNodeToMustWriteSet.get(prevFn);
2180         if (in != null) {
2181           merge(currMustWriteSet, in);
2182         }
2183       }
2184
2185       methodReadWriteSet_nodeActions(fn, currMustWriteSet, readSet, mustWriteSet, mayWriteSet,
2186           isEventLoopBody);
2187
2188       Set<NTuple<Descriptor>> mustSetPrev = mapFlatNodeToMustWriteSet.get(fn);
2189
2190       if (!currMustWriteSet.equals(mustSetPrev)) {
2191         mapFlatNodeToMustWriteSet.put(fn, currMustWriteSet);
2192         for (int i = 0; i < fn.numNext(); i++) {
2193           FlatNode nn = fn.getNext(i);
2194           if ((!isEventLoopBody) || loopIncElements.contains(nn)) {
2195             flatNodesToVisit.add(nn);
2196           }
2197
2198         }
2199       }
2200
2201     }
2202
2203   }
2204
2205   private void methodReadWriteSet_nodeActions(FlatNode fn,
2206       Set<NTuple<Descriptor>> currMustWriteSet, Set<NTuple<Descriptor>> readSet,
2207       Set<NTuple<Descriptor>> mustWriteSet, Set<NTuple<Descriptor>> mayWriteSet,
2208       boolean isEventLoopBody) {
2209
2210     TempDescriptor lhs;
2211     TempDescriptor rhs;
2212     FieldDescriptor fld;
2213
2214     switch (fn.kind()) {
2215     case FKind.FlatMethod: {
2216
2217       // set up initial heap paths for method parameters
2218       FlatMethod fm = (FlatMethod) fn;
2219       for (int i = 0; i < fm.numParameters(); i++) {
2220         TempDescriptor param = fm.getParameter(i);
2221         NTuple<Descriptor> heapPath = new NTuple<Descriptor>();
2222         heapPath.add(param);
2223         mapHeapPath.put(param, heapPath);
2224       }
2225     }
2226       break;
2227
2228     case FKind.FlatOpNode: {
2229       FlatOpNode fon = (FlatOpNode) fn;
2230       // for a normal assign node, need to propagate lhs's heap path to
2231       // rhs
2232
2233       if (fon.getOp().getOp() == Operation.ASSIGN) {
2234         rhs = fon.getLeft();
2235         lhs = fon.getDest();
2236
2237         NTuple<Descriptor> rhsHeapPath = mapHeapPath.get(rhs);
2238
2239         // if (lhs.getType().isPrimitive()) {
2240         // NTuple<Descriptor> lhsHeapPath = new NTuple<Descriptor>();
2241         // lhsHeapPath.add(lhs);
2242         // mapHeapPath.put(lhs, lhsHeapPath);
2243         // } else
2244
2245         if (rhsHeapPath != null) {
2246           mapHeapPath.put(lhs, mapHeapPath.get(rhs));
2247         } else {
2248           if (isEventLoopBody) {
2249             NTuple<Descriptor> heapPath = new NTuple<Descriptor>();
2250             heapPath.add(rhs);
2251             mapHeapPath.put(lhs, heapPath);
2252           } else {
2253             break;
2254           }
2255         }
2256
2257         // shared loc extension
2258         if (isEventLoopBody) {
2259           if (!lhs.getSymbol().startsWith("neverused") && rhs.getType().isImmutable()) {
2260
2261             if (rhs.getType().getExtension() instanceof Location
2262                 && lhs.getType().getExtension() instanceof CompositeLocation) {
2263               // rhs is field!
2264               Location rhsLoc = (Location) rhs.getType().getExtension();
2265
2266               CompositeLocation lhsCompLoc = (CompositeLocation) lhs.getType().getExtension();
2267               Location dstLoc = lhsCompLoc.get(lhsCompLoc.getSize() - 1);
2268
2269               NTuple<Descriptor> heapPath = new NTuple<Descriptor>();
2270               for (int i = 0; i < rhsHeapPath.size() - 1; i++) {
2271                 heapPath.add(rhsHeapPath.get(i));
2272               }
2273
2274               NTuple<Descriptor> writeHeapPath = new NTuple<Descriptor>();
2275               writeHeapPath.addAll(heapPath);
2276               writeHeapPath.add(lhs);
2277
2278             }
2279           }
2280         }
2281
2282       }
2283     }
2284       break;
2285
2286     case FKind.FlatElementNode:
2287     case FKind.FlatFieldNode: {
2288
2289       // x=y.f;
2290
2291       if (fn.kind() == FKind.FlatFieldNode) {
2292         FlatFieldNode ffn = (FlatFieldNode) fn;
2293         lhs = ffn.getDst();
2294         rhs = ffn.getSrc();
2295         fld = ffn.getField();
2296       } else {
2297         FlatElementNode fen = (FlatElementNode) fn;
2298         lhs = fen.getDst();
2299         rhs = fen.getSrc();
2300         TypeDescriptor td = rhs.getType().dereference();
2301         fld = getArrayField(td);
2302       }
2303
2304       if (fld.isFinal()) {
2305         // if field is final no need to check
2306         break;
2307       }
2308
2309       // set up heap path
2310       NTuple<Descriptor> srcHeapPath = mapHeapPath.get(rhs);
2311       if (srcHeapPath != null) {
2312         // if lhs srcHeapPath is null, it means that it is not reachable from
2313         // callee's parameters. so just ignore it
2314
2315         NTuple<Descriptor> readingHeapPath = new NTuple<Descriptor>(srcHeapPath.getList());
2316         if (fn.kind() == FKind.FlatFieldNode) {
2317           readingHeapPath.add(fld);
2318         }
2319
2320         mapHeapPath.put(lhs, readingHeapPath);
2321
2322         // read (x.f)
2323         if (fld.getType().isImmutable()) {
2324           // if WT doesnot have hp(x.f), add hp(x.f) to READ
2325           if (!currMustWriteSet.contains(readingHeapPath)) {
2326             readSet.add(readingHeapPath);
2327           }
2328         }
2329
2330         // no need to kill hp(x.f) from WT
2331       }
2332
2333     }
2334       break;
2335
2336     case FKind.FlatSetFieldNode:
2337     case FKind.FlatSetElementNode: {
2338
2339       // x.f=y;
2340
2341       if (fn.kind() == FKind.FlatSetFieldNode) {
2342         FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
2343         lhs = fsfn.getDst();
2344         fld = fsfn.getField();
2345         rhs = fsfn.getSrc();
2346       } else {
2347         FlatSetElementNode fsen = (FlatSetElementNode) fn;
2348         lhs = fsen.getDst();
2349         rhs = fsen.getSrc();
2350         TypeDescriptor td = lhs.getType().dereference();
2351         fld = getArrayField(td);
2352       }
2353
2354       // set up heap path
2355       NTuple<Descriptor> lhsHeapPath = mapHeapPath.get(lhs);
2356
2357       if (lhsHeapPath != null) {
2358         // if lhs heap path is null, it means that it is not reachable from
2359         // callee's parameters. so just ignore it
2360         NTuple<Descriptor> fldHeapPath = new NTuple<Descriptor>(lhsHeapPath.getList());
2361         if (fn.kind() != FKind.FlatSetElementNode) {
2362           fldHeapPath.add(fld);
2363         }
2364         // mapHeapPath.put(fld, fldHeapPath);
2365
2366         // write(x.f)
2367         // need to add hp(y) to WT
2368         if (fn.kind() != FKind.FlatSetElementNode) {
2369           currMustWriteSet.add(fldHeapPath);
2370         }
2371         mayWriteSet.add(fldHeapPath);
2372
2373       }
2374
2375     }
2376       break;
2377
2378     case FKind.FlatCall: {
2379
2380       FlatCall fc = (FlatCall) fn;
2381
2382       bindHeapPathCallerArgWithCalleeParam(fc);
2383
2384       Set<NTuple<Descriptor>> boundReadSet = new HashSet<NTuple<Descriptor>>();
2385       boundReadSet.addAll(calleeUnionBoundReadSet);
2386
2387       Set<NTuple<Descriptor>> boundMustWriteSet = new HashSet<NTuple<Descriptor>>();
2388       boundMustWriteSet.addAll(calleeIntersectBoundMustWriteSet);
2389
2390       Set<NTuple<Descriptor>> boundMayWriteSet = new HashSet<NTuple<Descriptor>>();
2391       boundMayWriteSet.addAll(calleeUnionBoundMayWriteSet);
2392
2393       mapFlatNodeToBoundReadSet.put(fn, boundReadSet);
2394       mapFlatNodeToBoundMustWriteSet.put(fn, boundMustWriteSet);
2395       mapFlatNodeToBoundMayWriteSet.put(fn, boundMayWriteSet);
2396
2397       // add heap path, which is an element of READ_bound set and is not
2398       // an
2399       // element of WT set, to the caller's READ set
2400       for (Iterator iterator = calleeUnionBoundReadSet.iterator(); iterator.hasNext();) {
2401         NTuple<Descriptor> read = (NTuple<Descriptor>) iterator.next();
2402         if (!currMustWriteSet.contains(read)) {
2403           readSet.add(read);
2404         }
2405       }
2406
2407       // add heap path, which is an element of OVERWRITE_bound set, to the
2408       // caller's WT set
2409       for (Iterator iterator = calleeIntersectBoundMustWriteSet.iterator(); iterator.hasNext();) {
2410         NTuple<Descriptor> write = (NTuple<Descriptor>) iterator.next();
2411         currMustWriteSet.add(write);
2412       }
2413
2414       // add heap path, which is an element of WRITE_BOUND set, to the
2415       // caller's writeSet
2416       for (Iterator iterator = calleeUnionBoundMayWriteSet.iterator(); iterator.hasNext();) {
2417         NTuple<Descriptor> write = (NTuple<Descriptor>) iterator.next();
2418         mayWriteSet.add(write);
2419       }
2420
2421     }
2422       break;
2423
2424     case FKind.FlatExit: {
2425       // merge the current written set with OVERWRITE set
2426       merge(mustWriteSet, currMustWriteSet);
2427     }
2428       break;
2429
2430     }
2431
2432   }
2433
2434   static public FieldDescriptor getArrayField(TypeDescriptor td) {
2435     FieldDescriptor fd = mapTypeToArrayField.get(td);
2436     if (fd == null) {
2437       fd =
2438           new FieldDescriptor(new Modifiers(Modifiers.PUBLIC), td, arrayElementFieldName, null,
2439               false);
2440       mapTypeToArrayField.put(td, fd);
2441     }
2442     return fd;
2443   }
2444
2445   private void merge(Set<NTuple<Descriptor>> curr, Set<NTuple<Descriptor>> in) {
2446     if (curr.isEmpty()) {
2447       // set has a special initial value which covers all possible
2448       // elements
2449       // For the first time of intersection, we can take all previous set
2450       curr.addAll(in);
2451     } else {
2452       // otherwise, current set is the intersection of the two sets
2453       curr.retainAll(in);
2454     }
2455
2456   }
2457
2458   // combine two heap path
2459   private NTuple<Descriptor> combine(NTuple<Descriptor> callerIn, NTuple<Descriptor> calleeIn) {
2460     NTuple<Descriptor> combined = new NTuple<Descriptor>();
2461
2462     for (int i = 0; i < callerIn.size(); i++) {
2463       combined.add(callerIn.get(i));
2464     }
2465
2466     // the first element of callee's heap path represents parameter
2467     // so we skip the first one since it is already added from caller's heap
2468     // path
2469     for (int i = 1; i < calleeIn.size(); i++) {
2470       combined.add(calleeIn.get(i));
2471     }
2472
2473     return combined;
2474   }
2475
2476   private Set<NTuple<Descriptor>> bindSet(Set<NTuple<Descriptor>> calleeSet,
2477       Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc,
2478       Hashtable<Integer, NTuple<Descriptor>> mapCallerArgIdx2HeapPath) {
2479
2480     Set<NTuple<Descriptor>> boundedCalleeSet = new HashSet<NTuple<Descriptor>>();
2481
2482     Set<Integer> keySet = mapCallerArgIdx2HeapPath.keySet();
2483     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2484       Integer idx = (Integer) iterator.next();
2485
2486       NTuple<Descriptor> callerArgHeapPath = mapCallerArgIdx2HeapPath.get(idx);
2487       TempDescriptor calleeParam = mapParamIdx2ParamTempDesc.get(idx);
2488       for (Iterator iterator2 = calleeSet.iterator(); iterator2.hasNext();) {
2489         NTuple<Descriptor> element = (NTuple<Descriptor>) iterator2.next();
2490         if (element.startsWith(calleeParam)) {
2491           NTuple<Descriptor> boundElement = combine(callerArgHeapPath, element);
2492           boundedCalleeSet.add(boundElement);
2493         }
2494
2495       }
2496
2497     }
2498     return boundedCalleeSet;
2499
2500   }
2501
2502   // Borrowed it from disjoint analysis
2503   private LinkedList<MethodDescriptor> topologicalSort(Set<MethodDescriptor> toSort) {
2504
2505     Set<MethodDescriptor> discovered = new HashSet<MethodDescriptor>();
2506
2507     LinkedList<MethodDescriptor> sorted = new LinkedList<MethodDescriptor>();
2508
2509     Iterator<MethodDescriptor> itr = toSort.iterator();
2510     while (itr.hasNext()) {
2511       MethodDescriptor d = itr.next();
2512
2513       if (!discovered.contains(d)) {
2514         dfsVisit(d, toSort, sorted, discovered);
2515       }
2516     }
2517
2518     return sorted;
2519   }
2520
2521   // While we're doing DFS on call graph, remember
2522   // dependencies for efficient queuing of methods
2523   // during interprocedural analysis:
2524   //
2525   // a dependent of a method decriptor d for this analysis is:
2526   // 1) a method or task that invokes d
2527   // 2) in the descriptorsToAnalyze set
2528   private void dfsVisit(MethodDescriptor md, Set<MethodDescriptor> toSort,
2529       LinkedList<MethodDescriptor> sorted, Set<MethodDescriptor> discovered) {
2530
2531     discovered.add(md);
2532
2533     Iterator itr = callGraph.getCallerSet(md).iterator();
2534     while (itr.hasNext()) {
2535       MethodDescriptor dCaller = (MethodDescriptor) itr.next();
2536       // only consider callers in the original set to analyze
2537       if (!toSort.contains(dCaller)) {
2538         continue;
2539       }
2540       if (!discovered.contains(dCaller)) {
2541         addDependent(md, // callee
2542             dCaller // caller
2543         );
2544
2545         dfsVisit(dCaller, toSort, sorted, discovered);
2546       }
2547     }
2548
2549     // for leaf-nodes last now!
2550     sorted.addLast(md);
2551   }
2552
2553   // a dependent of a method decriptor d for this analysis is:
2554   // 1) a method or task that invokes d
2555   // 2) in the descriptorsToAnalyze set
2556   private void addDependent(MethodDescriptor callee, MethodDescriptor caller) {
2557     Set<MethodDescriptor> deps = mapDescriptorToSetDependents.get(callee);
2558     if (deps == null) {
2559       deps = new HashSet<MethodDescriptor>();
2560     }
2561     deps.add(caller);
2562     mapDescriptorToSetDependents.put(callee, deps);
2563   }
2564
2565   private Set<MethodDescriptor> getDependents(MethodDescriptor callee) {
2566     Set<MethodDescriptor> deps = mapDescriptorToSetDependents.get(callee);
2567     if (deps == null) {
2568       deps = new HashSet<MethodDescriptor>();
2569       mapDescriptorToSetDependents.put(callee, deps);
2570     }
2571     return deps;
2572   }
2573
2574   private NTuple<Descriptor> computePath(Descriptor td) {
2575     // generate proper path fot input td
2576     // if td is local variable, it just generate one element tuple path
2577     if (mapHeapPath.containsKey(td)) {
2578       NTuple<Descriptor> rtrHeapPath = new NTuple<Descriptor>();
2579       rtrHeapPath.addAll(mapHeapPath.get(td));
2580       return rtrHeapPath;
2581     } else {
2582       NTuple<Descriptor> rtrHeapPath = new NTuple<Descriptor>();
2583       rtrHeapPath.add(td);
2584       return rtrHeapPath;
2585     }
2586   }
2587
2588   private NTuple<Location> deriveThisLocationTuple(MethodDescriptor md) {
2589     String thisLocIdentifier = ssjava.getMethodLattice(md).getThisLoc();
2590     Location thisLoc = new Location(md, thisLocIdentifier);
2591     NTuple<Location> locTuple = new NTuple<Location>();
2592     locTuple.add(thisLoc);
2593     return locTuple;
2594   }
2595
2596   private NTuple<Location> deriveGlobalLocationTuple(MethodDescriptor md) {
2597     String globalLocIdentifier = ssjava.getMethodLattice(md).getGlobalLoc();
2598     Location globalLoc = new Location(md, globalLocIdentifier);
2599     NTuple<Location> locTuple = new NTuple<Location>();
2600     locTuple.add(globalLoc);
2601     return locTuple;
2602   }
2603
2604   private NTuple<Location> deriveLocationTuple(MethodDescriptor md, TempDescriptor td) {
2605
2606     assert td.getType() != null;
2607
2608     if (mapDescriptorToLocationPath.containsKey(td)) {
2609       NTuple<Location> locPath = mapDescriptorToLocationPath.get(td);
2610       NTuple<Location> rtrPath = new NTuple<Location>();
2611       rtrPath.addAll(locPath);
2612       return rtrPath;
2613     } else {
2614       if (td.getSymbol().startsWith("this")) {
2615         NTuple<Location> thisPath = deriveThisLocationTuple(md);
2616         NTuple<Location> rtrPath = new NTuple<Location>();
2617         rtrPath.addAll(thisPath);
2618         return rtrPath;
2619       } else {
2620
2621         if (td.getType().getExtension() != null) {
2622           SSJavaType ssJavaType = (SSJavaType) td.getType().getExtension();
2623           if (ssJavaType.getCompLoc() != null) {
2624             NTuple<Location> rtrPath = new NTuple<Location>();
2625             rtrPath.addAll(ssJavaType.getCompLoc().getTuple());
2626             return rtrPath;
2627           }
2628         }
2629
2630         return null;
2631
2632       }
2633     }
2634   }
2635 }