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