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