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