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