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