1 package Analysis.SSJava;
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;
9 import java.util.Stack;
11 import Analysis.Liveness;
12 import Analysis.CallGraph.CallGraph;
13 import Analysis.Loops.LoopFinder;
15 import IR.FieldDescriptor;
16 import IR.MethodDescriptor;
19 import IR.TypeDescriptor;
20 import IR.TypeExtension;
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;
35 public class DefinitelyWrittenCheck {
37 SSJavaAnalysis ssjava;
45 // maps a flat node to its WrittenSet: this keeps all heap path overwritten
47 private Hashtable<FlatNode, Set<NTuple<Descriptor>>> mapFlatNodeToMustWriteSet;
49 // maps a temp descriptor to its heap path
50 // each temp descriptor has a unique heap path since we do not allow any
52 private Hashtable<Descriptor, NTuple<Descriptor>> mapHeapPath;
54 // maps a temp descriptor to its composite location
55 private Hashtable<TempDescriptor, NTuple<Location>> mapDescriptorToLocationPath;
57 // maps a flat method to the READ that is the set of heap path that is
58 // expected to be written before method invocation
59 private Hashtable<FlatMethod, Set<NTuple<Descriptor>>> mapFlatMethodToReadSet;
61 // maps a flat method to the must-write set that is the set of heap path that
62 // is overwritten on every possible path during method invocation
63 private Hashtable<FlatMethod, Set<NTuple<Descriptor>>> mapFlatMethodToMustWriteSet;
65 // maps a flat method to the DELETE SET that is a set of heap path to shared
67 // written to but not overwritten by the higher value
68 private Hashtable<FlatMethod, SharedLocMap> mapFlatMethodToDeleteSet;
70 private Hashtable<FlatMethod, SharedLocMap> mapFlatMethodToMustClearMap;
72 // maps a flat method to the S SET that is a set of heap path to shared
73 // locations that are overwritten by the higher value
74 private Hashtable<FlatMethod, SharedLocMap> mapFlatMethodToSharedLocMap;
76 // maps a flat method to the may-wirte set that is the set of heap path that
77 // might be written to
78 private Hashtable<FlatMethod, Set<NTuple<Descriptor>>> mapFlatMethodToMayWriteSet;
80 // maps a call site to the read set contributed by all callees
81 private Hashtable<FlatNode, Set<NTuple<Descriptor>>> mapFlatNodeToBoundReadSet;
83 // maps a call site to the must write set contributed by all callees
84 private Hashtable<FlatNode, Set<NTuple<Descriptor>>> mapFlatNodeToBoundMustWriteSet;
86 // maps a call site to the may read set contributed by all callees
87 private Hashtable<FlatNode, Set<NTuple<Descriptor>>> mapFlatNodeToBoundMayWriteSet;
89 // points to method containing SSJAVA Loop
90 private MethodDescriptor methodContainingSSJavaLoop;
92 // maps a flatnode to definitely written analysis mapping M
93 private Hashtable<FlatNode, Hashtable<NTuple<Descriptor>, Set<WriteAge>>> mapFlatNodetoEventLoopMap;
95 // maps shared location to the set of descriptors which belong to the shared
98 // keep current descriptors to visit in fixed-point interprocedural analysis,
99 private Stack<MethodDescriptor> methodDescriptorsToVisitStack;
101 // when analyzing flatcall, need to re-schedule set of callee
102 private Set<MethodDescriptor> calleesToEnqueue;
104 private Set<ReadSummary> possibleCalleeReadSummarySetToCaller;
106 public static final String arrayElementFieldName = "___element_";
107 static protected Hashtable<TypeDescriptor, FieldDescriptor> mapTypeToArrayField;
109 // maps a method descriptor to the merged incoming caller's current
111 // it is for setting clearance flag when all read set is overwritten
112 private Hashtable<MethodDescriptor, ReadSummary> mapMethodDescriptorToReadSummary;
114 private Hashtable<MethodDescriptor, MultiSourceMap<NTuple<Location>, NTuple<Descriptor>>> mapMethodToSharedLocCoverSet;
116 private Hashtable<FlatNode, SharedLocMap> mapFlatNodeToSharedLocMapping;
117 private Hashtable<FlatNode, SharedLocMap> mapFlatNodeToDeleteSet;
118 private Hashtable<FlatNode, SharedLocMap> mapFlatNodeToMustClearMap;
120 private LoopFinder ssjavaLoop;
121 private Set<FlatNode> loopIncElements;
123 private Set<NTuple<Descriptor>> calleeUnionBoundReadSet;
124 private Set<NTuple<Descriptor>> calleeIntersectBoundMustWriteSet;
125 private Set<NTuple<Descriptor>> calleeUnionBoundMayWriteSet;
126 private SharedLocMap calleeUnionBoundDeleteSet;
127 private SharedLocMap calleeIntersectBoundSharedSet;
128 private SharedLocMap calleeIntersectBoundMustClearSet;
130 Set<TempDescriptor> liveInTempSetToEventLoop;
132 private Hashtable<Descriptor, Location> mapDescToLocation;
134 private TempDescriptor LOCAL;
136 public static int MAXAGE = 1;
138 public DefinitelyWrittenCheck(SSJavaAnalysis ssjava, State state) {
140 this.ssjava = ssjava;
141 this.callGraph = ssjava.getCallGraph();
142 this.mapFlatNodeToMustWriteSet = new Hashtable<FlatNode, Set<NTuple<Descriptor>>>();
143 this.mapHeapPath = new Hashtable<Descriptor, NTuple<Descriptor>>();
144 this.mapDescriptorToLocationPath = new Hashtable<TempDescriptor, NTuple<Location>>();
145 this.mapFlatMethodToReadSet = new Hashtable<FlatMethod, Set<NTuple<Descriptor>>>();
146 this.mapFlatMethodToMustWriteSet = new Hashtable<FlatMethod, Set<NTuple<Descriptor>>>();
147 this.mapFlatMethodToMayWriteSet = new Hashtable<FlatMethod, Set<NTuple<Descriptor>>>();
148 this.mapFlatNodetoEventLoopMap =
149 new Hashtable<FlatNode, Hashtable<NTuple<Descriptor>, Set<WriteAge>>>();
150 this.calleeUnionBoundReadSet = new HashSet<NTuple<Descriptor>>();
151 this.calleeIntersectBoundMustWriteSet = new HashSet<NTuple<Descriptor>>();
152 this.calleeUnionBoundMayWriteSet = new HashSet<NTuple<Descriptor>>();
154 this.methodDescriptorsToVisitStack = new Stack<MethodDescriptor>();
155 this.calleesToEnqueue = new HashSet<MethodDescriptor>();
156 this.mapTypeToArrayField = new Hashtable<TypeDescriptor, FieldDescriptor>();
157 this.LOCAL = new TempDescriptor("LOCAL");
158 this.mapDescToLocation = new Hashtable<Descriptor, Location>();
159 this.possibleCalleeReadSummarySetToCaller = new HashSet<ReadSummary>();
160 this.mapMethodDescriptorToReadSummary = new Hashtable<MethodDescriptor, ReadSummary>();
161 this.mapFlatNodeToBoundReadSet = new Hashtable<FlatNode, Set<NTuple<Descriptor>>>();
162 this.mapFlatNodeToBoundMustWriteSet = new Hashtable<FlatNode, Set<NTuple<Descriptor>>>();
163 this.mapFlatNodeToBoundMayWriteSet = new Hashtable<FlatNode, Set<NTuple<Descriptor>>>();
164 this.mapFlatNodeToSharedLocMapping = new Hashtable<FlatNode, SharedLocMap>();
165 this.mapFlatMethodToDeleteSet = new Hashtable<FlatMethod, SharedLocMap>();
166 this.calleeUnionBoundDeleteSet = new SharedLocMap();
167 this.calleeIntersectBoundSharedSet = new SharedLocMap();
168 this.mapFlatMethodToSharedLocMap = new Hashtable<FlatMethod, SharedLocMap>();
169 this.mapMethodToSharedLocCoverSet =
170 new Hashtable<MethodDescriptor, MultiSourceMap<NTuple<Location>, NTuple<Descriptor>>>();
171 this.mapFlatNodeToDeleteSet = new Hashtable<FlatNode, SharedLocMap>();
172 this.liveness = new Liveness();
173 this.liveInTempSetToEventLoop = new HashSet<TempDescriptor>();
174 this.mapFlatNodeToMustClearMap = new Hashtable<FlatNode, SharedLocMap>();
175 this.calleeIntersectBoundMustClearSet = new SharedLocMap();
176 this.mapFlatMethodToMustClearMap = new Hashtable<FlatMethod, SharedLocMap>();
179 public void definitelyWrittenCheck() {
180 if (!ssjava.getAnnotationRequireSet().isEmpty()) {
183 methodReadWriteSetAnalysis();
184 computeSharedCoverSet();
193 private void sharedLocAnalysis() {
195 // perform method READ/OVERWRITE analysis
196 LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
198 // current descriptors to visit in fixed-point interprocedural analysis,
200 // dependency in the call graph
201 methodDescriptorsToVisitStack.clear();
203 descriptorListToAnalyze.removeFirst();
205 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
206 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
208 while (!descriptorListToAnalyze.isEmpty()) {
209 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
210 methodDescriptorsToVisitStack.add(md);
213 // analyze scheduled methods until there are no more to visit
214 while (!methodDescriptorsToVisitStack.isEmpty()) {
215 // start to analyze leaf node
216 MethodDescriptor md = methodDescriptorsToVisitStack.pop();
217 FlatMethod fm = state.getMethodFlat(md);
219 SharedLocMap sharedLocMap = new SharedLocMap();
220 SharedLocMap deleteSet = new SharedLocMap();
221 SharedLocMap mustClearMap = new SharedLocMap();
223 sharedLoc_analyzeMethod(fm, sharedLocMap, deleteSet, mustClearMap);
224 SharedLocMap prevSharedLocMap = mapFlatMethodToSharedLocMap.get(fm);
225 SharedLocMap prevDeleteSet = mapFlatMethodToDeleteSet.get(fm);
226 SharedLocMap prevMustClearMap = mapFlatMethodToMustClearMap.get(fm);
228 if (!(deleteSet.equals(prevDeleteSet) && sharedLocMap.equals(prevSharedLocMap) && mustClearMap
229 .equals(prevMustClearMap))) {
230 mapFlatMethodToSharedLocMap.put(fm, sharedLocMap);
231 mapFlatMethodToDeleteSet.put(fm, deleteSet);
232 mapFlatMethodToMustClearMap.put(fm, mustClearMap);
234 // results for callee changed, so enqueue dependents caller for
237 Iterator<MethodDescriptor> depsItr = ssjava.getDependents(md).iterator();
238 while (depsItr.hasNext()) {
239 MethodDescriptor methodNext = depsItr.next();
240 if (!methodDescriptorsToVisitStack.contains(methodNext)
241 && methodDescriptorToVistSet.contains(methodNext)) {
242 methodDescriptorsToVisitStack.add(methodNext);
251 sharedLoc_analyzeEventLoop();
255 private void sharedLoc_analyzeEventLoop() {
256 if (state.SSJAVADEBUG) {
257 System.out.println("SSJAVA: Definite clearance for shared locations Analyzing: eventloop");
259 SharedLocMap sharedLocMap = new SharedLocMap();
260 SharedLocMap deleteSet = new SharedLocMap();
261 SharedLocMap mustClearMap = new SharedLocMap();
262 sharedLoc_analyzeBody(state.getMethodFlat(methodContainingSSJavaLoop),
263 ssjava.getSSJavaLoopEntrance(), sharedLocMap, deleteSet, mustClearMap, true);
267 private void sharedLoc_analyzeMethod(FlatMethod fm, SharedLocMap sharedLocMap,
268 SharedLocMap deleteSet, SharedLocMap mustClearMap) {
269 if (state.SSJAVADEBUG) {
270 System.out.println("SSJAVA: Definite clearance for shared locations Analyzing: " + fm);
273 sharedLoc_analyzeBody(fm, fm, sharedLocMap, deleteSet, mustClearMap, false);
277 private void sharedLoc_analyzeBody(FlatMethod fm, FlatNode startNode, SharedLocMap sharedLocMap,
278 SharedLocMap deleteSet, SharedLocMap mustClearMap, boolean isEventLoopBody) {
280 // intraprocedural analysis
281 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
282 flatNodesToVisit.add(startNode);
284 while (!flatNodesToVisit.isEmpty()) {
285 FlatNode fn = flatNodesToVisit.iterator().next();
286 flatNodesToVisit.remove(fn);
288 SharedLocMap currSharedSet = new SharedLocMap();
289 SharedLocMap currDeleteSet = new SharedLocMap();
290 SharedLocMap currMustClearMap = new SharedLocMap();
292 for (int i = 0; i < fn.numPrev(); i++) {
293 FlatNode prevFn = fn.getPrev(i);
294 SharedLocMap inSharedLoc = mapFlatNodeToSharedLocMapping.get(prevFn);
295 if (inSharedLoc != null) {
296 mergeSharedLocMap(currSharedSet, inSharedLoc);
299 SharedLocMap inDeleteLoc = mapFlatNodeToDeleteSet.get(prevFn);
300 if (inDeleteLoc != null) {
301 mergeDeleteSet(currDeleteSet, inDeleteLoc);
304 SharedLocMap inMustClearMap = mapFlatNodeToMustClearMap.get(prevFn);
305 if (inMustClearMap != null) {
306 mergeSharedLocMap(currMustClearMap, inMustClearMap);
311 sharedLoc_nodeActions(fm, fn, currSharedSet, currDeleteSet, currMustClearMap, sharedLocMap,
312 deleteSet, mustClearMap, isEventLoopBody);
314 SharedLocMap prevSharedSet = mapFlatNodeToSharedLocMapping.get(fn);
315 SharedLocMap prevDeleteSet = mapFlatNodeToDeleteSet.get(fn);
316 SharedLocMap prevMustClearMap = mapFlatNodeToMustClearMap.get(fn);
318 if (!(currSharedSet.equals(prevSharedSet) && currDeleteSet.equals(prevDeleteSet) && currMustClearMap
319 .equals(prevMustClearMap))) {
320 mapFlatNodeToSharedLocMapping.put(fn, currSharedSet);
321 mapFlatNodeToDeleteSet.put(fn, currDeleteSet);
322 mapFlatNodeToMustClearMap.put(fn, currMustClearMap);
323 for (int i = 0; i < fn.numNext(); i++) {
324 FlatNode nn = fn.getNext(i);
325 if ((!isEventLoopBody) || loopIncElements.contains(nn)) {
326 flatNodesToVisit.add(nn);
336 private void sharedLoc_nodeActions(FlatMethod fm, FlatNode fn, SharedLocMap curr,
337 SharedLocMap currDeleteSet, SharedLocMap currMustClearMap, SharedLocMap sharedLocMap,
338 SharedLocMap deleteSet, SharedLocMap mustClearMap, boolean isEventLoopBody) {
340 MethodDescriptor md = fm.getMethod();
342 SharedLocMap killSet = new SharedLocMap();
343 SharedLocMap genSet = new SharedLocMap();
349 NTuple<Location> fieldLocTuple = null;
350 Location fieldLoc = null;
351 boolean isHigherWriteCase = false;
355 case FKind.FlatOpNode: {
357 if (isEventLoopBody) {
358 FlatOpNode fon = (FlatOpNode) fn;
360 if (fon.getOp().getOp() == Operation.ASSIGN) {
364 if (!lhs.getSymbol().startsWith("neverused") && !lhs.getSymbol().startsWith("leftop")
365 && !lhs.getSymbol().startsWith("rightop") && rhs.getType().isImmutable()) {
367 if (mapHeapPath.containsKey(rhs)) {
368 Location dstLoc = getLocation(lhs);
369 if (dstLoc != null && ssjava.isSharedLocation(dstLoc)) {
370 NTuple<Descriptor> lhsHeapPath = computePath(lhs);
371 NTuple<Location> lhsLocTuple = mapDescriptorToLocationPath.get(lhs);
373 Location srcLoc = getLocation(lhs);
375 // computing gen/kill set
376 computeKILLSetForWrite(curr, killSet, lhsLocTuple, lhsHeapPath);
378 if (!ssjava.isSameHeightWrite(fn)) {
379 computeGENSetForHigherWrite(curr, killSet, lhsLocTuple, lhsHeapPath);
380 updateDeleteSetForHigherWrite(currDeleteSet, lhsLocTuple, lhsHeapPath);
382 computeGENSetForSameHeightWrite(curr, killSet, lhsLocTuple, lhsHeapPath);
383 updateDeleteSetForSameHeightWrite(currDeleteSet, lhsLocTuple, lhsHeapPath);
400 case FKind.FlatSetFieldNode:
401 case FKind.FlatSetElementNode: {
403 if (fn.kind() == FKind.FlatSetFieldNode) {
404 FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
406 fld = fsfn.getField();
408 fieldLoc = (Location) fld.getType().getExtension();
413 if (!isEventLoopBody && fieldLoc.getDescriptor().equals(md)) {
414 // if the field belongs to the local lattice, no reason to calculate
419 fieldLocTuple = new NTuple<Location>();
420 if (fld.isStatic()) {
422 // in this case, fld has TOP location
423 Location topLocation = Location.createTopLocation(md);
424 fieldLocTuple.add(topLocation);
426 fieldLocTuple.addAll(deriveGlobalLocationTuple(md));
427 if (fn.kind() == FKind.FlatSetFieldNode) {
428 fieldLocTuple.add((Location) fld.getType().getExtension());
433 fieldLocTuple.addAll(deriveLocationTuple(md, lhs));
434 if (fn.kind() == FKind.FlatSetFieldNode) {
435 fieldLocTuple.add((Location) fld.getType().getExtension());
439 // shared loc extension
440 Location srcLoc = getLocation(rhs);
441 if (ssjava.isSharedLocation(fieldLoc)) {
442 // only care the case that loc(f) is shared location
445 // NTuple<Location> fieldLocTuple = new NTuple<Location>();
446 // fieldLocTuple.addAll(mapDescriptorToLocationPath.get(lhs));
447 // fieldLocTuple.add(fieldLoc);
449 NTuple<Descriptor> fldHeapPath = new NTuple<Descriptor>();
450 fldHeapPath.addAll(computePath(lhs));
451 if (fn.kind() == FKind.FlatSetFieldNode) {
452 fldHeapPath.add(fld);
455 // computing gen/kill set
456 computeKILLSetForWrite(curr, killSet, fieldLocTuple, fldHeapPath);
458 if (!ssjava.isSameHeightWrite(fn)) {
459 computeGENSetForHigherWrite(curr, genSet, fieldLocTuple, fldHeapPath);
460 updateDeleteSetForHigherWrite(currDeleteSet, fieldLocTuple, fldHeapPath);
462 isHigherWriteCase = true;
465 computeGENSetForSameHeightWrite(curr, genSet, fieldLocTuple, fldHeapPath);
466 updateDeleteSetForSameHeightWrite(currDeleteSet, fieldLocTuple, fldHeapPath);
474 case FKind.FlatCall: {
475 FlatCall fc = (FlatCall) fn;
477 bindHeapPathCallerArgWithCaleeParamForSharedLoc(fm.getMethod(), fc);
479 // computing gen/kill set
480 generateKILLSetForFlatCall(curr, killSet);
481 generateGENSetForFlatCall(curr, genSet);
483 Set<NTuple<Location>> locTupleSet = calleeIntersectBoundMustClearSet.keySet();
484 for (Iterator iterator = locTupleSet.iterator(); iterator.hasNext();) {
485 NTuple<Location> locTupleKey = (NTuple<Location>) iterator.next();
486 currMustClearMap.addWrite(locTupleKey, calleeIntersectBoundMustClearSet.get(locTupleKey));
492 case FKind.FlatExit: {
493 // merge the current delete/shared loc mapping
494 mergeSharedLocMap(sharedLocMap, curr);
495 mergeDeleteSet(deleteSet, currDeleteSet);
496 mergeSharedLocMap(mustClearMap, currMustClearMap);
502 computeNewMapping(curr, killSet, genSet);
503 if (isHigherWriteCase) {
504 // check all locations with the same shared location are cleared out at this point
505 Set<NTuple<Descriptor>> writtenSet = curr.get(fieldLocTuple);
506 Set<Descriptor> requirementSet = ssjava.getSharedDescSet(fieldLoc);
508 if (checkAllSharedLocationsAreOverwritten(requirementSet, writtenSet)) {
509 currMustClearMap.addWrite(fieldLocTuple, writtenSet);
514 private boolean checkAllSharedLocationsAreOverwritten(Set<Descriptor> sharedDescSet,
515 Set<NTuple<Descriptor>> writtenSet) {
517 if (sharedDescSet == null || writtenSet == null) {
520 Set<Descriptor> writtenDescSet = new HashSet<Descriptor>();
521 for (Iterator iterator = writtenSet.iterator(); iterator.hasNext();) {
522 NTuple<Descriptor> tuple = (NTuple<Descriptor>) iterator.next();
523 writtenDescSet.add(tuple.get(tuple.size() - 1));
526 return writtenDescSet.containsAll(sharedDescSet);
527 // return sharedDescSet.containsAll(writtenDescSet);
531 private void generateGENSetForFlatCall(SharedLocMap curr, SharedLocMap genSet) {
533 Set<NTuple<Location>> locTupleSet = calleeIntersectBoundSharedSet.keySet();
534 for (Iterator iterator = locTupleSet.iterator(); iterator.hasNext();) {
535 NTuple<Location> locTupleKey = (NTuple<Location>) iterator.next();
536 genSet.addWrite(locTupleKey, curr.get(locTupleKey));
537 genSet.addWrite(locTupleKey, calleeIntersectBoundSharedSet.get(locTupleKey));
539 genSet.removeWriteAll(locTupleKey, calleeUnionBoundDeleteSet.get(locTupleKey));
544 private void generateKILLSetForFlatCall(SharedLocMap curr, SharedLocMap killSet) {
546 Set<NTuple<Location>> locTupleSet = calleeIntersectBoundSharedSet.keySet();
547 for (Iterator iterator = locTupleSet.iterator(); iterator.hasNext();) {
548 NTuple<Location> locTupleKey = (NTuple<Location>) iterator.next();
549 killSet.addWrite(locTupleKey, curr.get(locTupleKey));
554 private void mergeDeleteSet(SharedLocMap currDeleteSet, SharedLocMap inDeleteLoc) {
556 Set<NTuple<Location>> locTupleKeySet = inDeleteLoc.keySet();
558 for (Iterator iterator = locTupleKeySet.iterator(); iterator.hasNext();) {
559 NTuple<Location> locTupleKey = (NTuple<Location>) iterator.next();
561 Set<NTuple<Descriptor>> inSet = inDeleteLoc.get(locTupleKey);
562 currDeleteSet.addWrite(locTupleKey, inSet);
567 private void computeNewMapping(SharedLocMap curr, SharedLocMap killSet, SharedLocMap genSet) {
572 private void updateDeleteSetForHigherWrite(SharedLocMap currDeleteSet, NTuple<Location> locTuple,
573 NTuple<Descriptor> hp) {
574 currDeleteSet.removeWrite(locTuple, hp);
577 private void updateDeleteSetForSameHeightWrite(SharedLocMap currDeleteSet,
578 NTuple<Location> locTuple, NTuple<Descriptor> hp) {
579 currDeleteSet.addWrite(locTuple, hp);
582 private void computeGENSetForHigherWrite(SharedLocMap curr, SharedLocMap genSet,
583 NTuple<Location> locTuple, NTuple<Descriptor> hp) {
584 Set<NTuple<Descriptor>> currWriteSet = curr.get(locTuple);
586 if (currWriteSet != null) {
587 genSet.addWrite(locTuple, currWriteSet);
589 genSet.addWrite(locTuple, hp);
592 private void computeGENSetForSameHeightWrite(SharedLocMap curr, SharedLocMap genSet,
593 NTuple<Location> locTuple, NTuple<Descriptor> hp) {
594 Set<NTuple<Descriptor>> currWriteSet = curr.get(locTuple);
596 if (currWriteSet != null) {
597 genSet.addWrite(locTuple, currWriteSet);
599 genSet.removeWrite(locTuple, hp);
602 private void computeKILLSetForWrite(SharedLocMap curr, SharedLocMap killSet,
603 NTuple<Location> locTuple, NTuple<Descriptor> hp) {
605 Set<NTuple<Descriptor>> writeSet = curr.get(locTuple);
606 if (writeSet != null) {
607 killSet.addWrite(locTuple, writeSet);
612 private void mergeSharedLocMap(SharedLocMap currSharedSet, SharedLocMap in) {
614 Set<NTuple<Location>> locTupleKeySet = in.keySet();
615 for (Iterator iterator = locTupleKeySet.iterator(); iterator.hasNext();) {
616 NTuple<Location> locTupleKey = (NTuple<Location>) iterator.next();
618 Set<NTuple<Descriptor>> inSet = in.get(locTupleKey);
619 Set<NTuple<Descriptor>> currSet = currSharedSet.get(locTupleKey);
620 if (currSet == null) {
621 currSet = new HashSet<NTuple<Descriptor>>();
622 currSet.addAll(inSet);
623 currSharedSet.addWrite(locTupleKey, currSet);
625 currSet.retainAll(inSet);
630 private void computeSharedCoverSet() {
631 LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
633 // current descriptors to visit in fixed-point interprocedural analysis,
635 // dependency in the call graph
636 methodDescriptorsToVisitStack.clear();
638 descriptorListToAnalyze.removeFirst();
640 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
641 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
643 while (!descriptorListToAnalyze.isEmpty()) {
644 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
645 methodDescriptorsToVisitStack.add(md);
648 // analyze scheduled methods until there are no more to visit
649 while (!methodDescriptorsToVisitStack.isEmpty()) {
650 MethodDescriptor md = methodDescriptorsToVisitStack.pop();
651 FlatMethod fm = state.getMethodFlat(md);
652 computeSharedCoverSet_analyzeMethod(fm, md.equals(methodContainingSSJavaLoop));
655 computeSharedCoverSetForEventLoop();
659 private void computeSharedCoverSetForEventLoop() {
660 computeSharedCoverSet_analyzeMethod(state.getMethodFlat(methodContainingSSJavaLoop), true);
663 private void computeSharedCoverSet_analyzeMethod(FlatMethod fm, boolean onlyVisitSSJavaLoop) {
665 MethodDescriptor md = fm.getMethod();
667 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
669 Set<FlatNode> visited = new HashSet<FlatNode>();
671 if (onlyVisitSSJavaLoop) {
672 flatNodesToVisit.add(ssjava.getSSJavaLoopEntrance());
674 flatNodesToVisit.add(fm);
677 while (!flatNodesToVisit.isEmpty()) {
678 FlatNode fn = flatNodesToVisit.iterator().next();
679 flatNodesToVisit.remove(fn);
682 computeSharedCoverSet_nodeActions(md, fn, onlyVisitSSJavaLoop);
684 for (int i = 0; i < fn.numNext(); i++) {
685 FlatNode nn = fn.getNext(i);
687 if (!visited.contains(nn)) {
688 if (!onlyVisitSSJavaLoop || (onlyVisitSSJavaLoop && loopIncElements.contains(nn))) {
689 flatNodesToVisit.add(nn);
699 private void computeSharedCoverSet_nodeActions(MethodDescriptor md, FlatNode fn,
700 boolean isEventLoopBody) {
707 case FKind.FlatLiteralNode: {
708 FlatLiteralNode fln = (FlatLiteralNode) fn;
711 NTuple<Location> lhsLocTuple = new NTuple<Location>();
712 lhsLocTuple.add(Location.createTopLocation(md));
713 mapDescriptorToLocationPath.put(lhs, lhsLocTuple);
715 if (lhs.getType().isPrimitive() && !lhs.getSymbol().startsWith("neverused")
716 && !lhs.getSymbol().startsWith("srctmp")) {
717 // only need to care about composite location case here
718 if (lhs.getType().getExtension() instanceof SSJavaType) {
719 CompositeLocation compLoc = ((SSJavaType) lhs.getType().getExtension()).getCompLoc();
720 Location lastLocElement = compLoc.get(compLoc.getSize() - 1);
727 case FKind.FlatOpNode: {
728 FlatOpNode fon = (FlatOpNode) fn;
729 // for a normal assign node, need to propagate lhs's location path to
731 if (fon.getOp().getOp() == Operation.ASSIGN) {
735 if (!lhs.getSymbol().startsWith("neverused") && !lhs.getSymbol().startsWith("leftop")
736 && !lhs.getSymbol().startsWith("rightop")) {
738 if (mapHeapPath.containsKey(rhs)) {
739 NTuple<Location> rhsLocTuple = new NTuple<Location>();
740 NTuple<Location> lhsLocTuple = new NTuple<Location>();
741 if (mapDescriptorToLocationPath.containsKey(rhs)) {
742 mapDescriptorToLocationPath.put(lhs, deriveLocationTuple(md, rhs));
743 lhsLocTuple = mapDescriptorToLocationPath.get(lhs);
746 if (rhs.getType().getExtension() != null
747 && rhs.getType().getExtension() instanceof SSJavaType) {
749 if (((SSJavaType) rhs.getType().getExtension()).getCompLoc() != null) {
750 rhsLocTuple.addAll(((SSJavaType) rhs.getType().getExtension()).getCompLoc()
755 NTuple<Location> locTuple = deriveLocationTuple(md, rhs);
756 if (locTuple != null) {
757 rhsLocTuple.addAll(locTuple);
760 if (rhsLocTuple.size() > 0) {
761 mapDescriptorToLocationPath.put(rhs, rhsLocTuple);
765 if (lhs.getType().getExtension() != null
766 && lhs.getType().getExtension() instanceof SSJavaType) {
767 lhsLocTuple.addAll(((SSJavaType) lhs.getType().getExtension()).getCompLoc()
769 mapDescriptorToLocationPath.put(lhs, lhsLocTuple);
770 } else if (mapDescriptorToLocationPath.get(rhs) != null) {
771 // propagate rhs's location to lhs
772 lhsLocTuple.addAll(mapDescriptorToLocationPath.get(rhs));
773 mapDescriptorToLocationPath.put(lhs, lhsLocTuple);
777 if (isEventLoopBody && lhs.getType().isPrimitive()
778 && !lhs.getSymbol().startsWith("srctmp")) {
780 NTuple<Descriptor> lhsHeapPath = computePath(lhs);
782 if (lhsLocTuple != null) {
783 addMayWrittenSet(md, lhsLocTuple, lhsHeapPath);
797 case FKind.FlatSetFieldNode:
798 case FKind.FlatSetElementNode: {
802 if (fn.kind() == FKind.FlatSetFieldNode) {
803 FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
805 fld = fsfn.getField();
808 FlatSetElementNode fsen = (FlatSetElementNode) fn;
811 TypeDescriptor td = lhs.getType().dereference();
812 fld = getArrayField(td);
815 NTuple<Location> lhsLocTuple = new NTuple<Location>();
816 if (fld.isStatic()) {
818 // in this case, fld has TOP location
819 Location topLocation = Location.createTopLocation(md);
820 lhsLocTuple.add(topLocation);
822 lhsLocTuple.addAll(deriveGlobalLocationTuple(md));
825 lhsLocTuple.addAll(deriveLocationTuple(md, lhs));
828 mapDescriptorToLocationPath.put(lhs, lhsLocTuple);
830 NTuple<Location> fieldLocTuple = new NTuple<Location>();
831 fieldLocTuple.addAll(lhsLocTuple);
833 if (fn.kind() == FKind.FlatSetFieldNode) {
834 fieldLocTuple.add((Location) fld.getType().getExtension());
837 if (mapHeapPath.containsKey(lhs)) {
838 // fields reachable from the param can have heap path entry.
839 NTuple<Descriptor> lhsHeapPath = new NTuple<Descriptor>();
840 lhsHeapPath.addAll(mapHeapPath.get(lhs));
842 Location fieldLocation;
843 if (fn.kind() == FKind.FlatSetFieldNode) {
844 fieldLocation = getLocation(fld);
846 fieldLocation = getLocation(lhsHeapPath.get(getArrayBaseDescriptorIdx(lhsHeapPath)));
849 // Location fieldLocation = getLocation(lhs);
850 if (!isEventLoopBody && fieldLocation.getDescriptor().equals(md)) {
851 // if the field belongs to the local lattice, no reason to calculate
856 if (ssjava.isSharedLocation(fieldLocation)) {
858 NTuple<Descriptor> fieldHeapPath = new NTuple<Descriptor>();
859 fieldHeapPath.addAll(computePath(lhs));
860 if (fn.kind() == FKind.FlatSetFieldNode) {
861 fieldHeapPath.add(fld);
864 addMayWrittenSet(md, fieldLocTuple, fieldHeapPath);
872 case FKind.FlatElementNode:
873 case FKind.FlatFieldNode: {
877 if (fn.kind() == FKind.FlatFieldNode) {
878 FlatFieldNode ffn = (FlatFieldNode) fn;
881 fld = ffn.getField();
883 FlatElementNode fen = (FlatElementNode) fn;
886 TypeDescriptor td = rhs.getType().dereference();
887 fld = getArrayField(td);
890 NTuple<Location> locTuple = new NTuple<Location>();
892 if (fld.isStatic()) {
895 // in this case, fld has TOP location
896 Location topLocation = Location.createTopLocation(md);
897 locTuple.add(topLocation);
899 locTuple.addAll(deriveGlobalLocationTuple(md));
900 if (fn.kind() == FKind.FlatFieldNode) {
901 locTuple.add((Location) fld.getType().getExtension());
906 locTuple.addAll(deriveLocationTuple(md, rhs));
907 if (fn.kind() == FKind.FlatFieldNode) {
908 locTuple.add((Location) fld.getType().getExtension());
912 mapDescriptorToLocationPath.put(lhs, locTuple);
917 case FKind.FlatCall: {
919 FlatCall fc = (FlatCall) fn;
921 bindLocationPathCallerArgWithCalleeParam(md, fc);
926 case FKind.FlatNew: {
928 FlatNew fnew = (FlatNew) fn;
929 TempDescriptor dst = fnew.getDst();
930 NTuple<Location> locTuple = deriveLocationTuple(md, dst);
932 if (locTuple != null) {
933 NTuple<Location> dstLocTuple = new NTuple<Location>();
934 dstLocTuple.addAll(locTuple);
935 mapDescriptorToLocationPath.put(dst, dstLocTuple);
943 private void addMayWrittenSet(MethodDescriptor md, NTuple<Location> locTuple,
944 NTuple<Descriptor> heapPath) {
946 MultiSourceMap<NTuple<Location>, NTuple<Descriptor>> map = mapMethodToSharedLocCoverSet.get(md);
948 map = new MultiSourceMap<NTuple<Location>, NTuple<Descriptor>>();
949 mapMethodToSharedLocCoverSet.put(md, map);
952 Set<NTuple<Descriptor>> writeSet = map.get(locTuple);
953 if (writeSet == null) {
954 writeSet = new HashSet<NTuple<Descriptor>>();
955 map.put(locTuple, writeSet);
957 writeSet.add(heapPath);
961 private void bindLocationPathCallerArgWithCalleeParam(MethodDescriptor mdCaller, FlatCall fc) {
963 if (ssjava.isSSJavaUtil(fc.getMethod().getClassDesc())) {
965 // have write effects on the first argument
966 TempDescriptor arg = fc.getArg(0);
967 NTuple<Location> argLocationPath = deriveLocationTuple(mdCaller, arg);
968 NTuple<Descriptor> argHeapPath = computePath(arg);
969 addMayWrittenSet(mdCaller, argLocationPath, argHeapPath);
970 } else if (ssjava.needTobeAnnotated(fc.getMethod())) {
972 // if arg is not primitive type, we need to propagate maywritten set to
973 // the caller's location path
975 MethodDescriptor mdCallee = fc.getMethod();
976 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
977 setPossibleCallees.addAll(callGraph.getMethods(mdCallee));
979 // create mapping from arg idx to its heap paths
980 Hashtable<Integer, NTuple<Descriptor>> mapArgIdx2CallerArgHeapPath =
981 new Hashtable<Integer, NTuple<Descriptor>>();
983 // create mapping from arg idx to its location paths
984 Hashtable<Integer, NTuple<Location>> mapArgIdx2CallerArgLocationPath =
985 new Hashtable<Integer, NTuple<Location>>();
987 if (fc.getThis() != null) {
989 if (mapHeapPath.containsKey(fc.getThis())) {
991 // setup heap path for 'this'
992 NTuple<Descriptor> thisHeapPath = new NTuple<Descriptor>();
993 thisHeapPath.addAll(mapHeapPath.get(fc.getThis()));
994 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(0), thisHeapPath);
996 // setup location path for 'this'
997 NTuple<Location> thisLocationPath = deriveLocationTuple(mdCaller, fc.getThis());
998 mapArgIdx2CallerArgLocationPath.put(Integer.valueOf(0), thisLocationPath);
1003 for (int i = 0; i < fc.numArgs(); i++) {
1004 TempDescriptor arg = fc.getArg(i);
1005 // create mapping arg to loc path
1007 if (mapHeapPath.containsKey(arg)) {
1009 NTuple<Descriptor> argHeapPath = mapHeapPath.get(arg);
1010 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(i + 1), argHeapPath);
1012 NTuple<Location> argLocationPath = deriveLocationTuple(mdCaller, arg);
1013 mapArgIdx2CallerArgLocationPath.put(Integer.valueOf(i + 1), argLocationPath);
1018 for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
1019 MethodDescriptor callee = (MethodDescriptor) iterator.next();
1020 FlatMethod calleeFlatMethod = state.getMethodFlat(callee);
1022 // binding caller's args and callee's params
1024 Hashtable<NTuple<Descriptor>, NTuple<Descriptor>> mapParamHeapPathToCallerArgHeapPath =
1025 new Hashtable<NTuple<Descriptor>, NTuple<Descriptor>>();
1027 Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc =
1028 new Hashtable<Integer, TempDescriptor>();
1030 if (calleeFlatMethod.getMethod().isStatic()) {
1031 // static method does not have implicit 'this' arg
1035 for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
1036 TempDescriptor param = calleeFlatMethod.getParameter(i);
1037 mapParamIdx2ParamTempDesc.put(Integer.valueOf(i + offset), param);
1039 NTuple<Descriptor> calleeHeapPath = computePath(param);
1041 NTuple<Descriptor> argHeapPath =
1042 mapArgIdx2CallerArgHeapPath.get(Integer.valueOf(i + offset));
1044 if (argHeapPath != null) {
1045 mapParamHeapPathToCallerArgHeapPath.put(calleeHeapPath, argHeapPath);
1051 Set<Integer> keySet = mapArgIdx2CallerArgLocationPath.keySet();
1052 for (Iterator iterator2 = keySet.iterator(); iterator2.hasNext();) {
1053 Integer idx = (Integer) iterator2.next();
1055 NTuple<Location> callerArgLocationPath = mapArgIdx2CallerArgLocationPath.get(idx);
1057 TempDescriptor calleeParam = mapParamIdx2ParamTempDesc.get(idx);
1058 NTuple<Location> calleeLocationPath = deriveLocationTuple(mdCallee, calleeParam);
1060 NTuple<Descriptor> callerArgHeapPath = mapArgIdx2CallerArgHeapPath.get(idx);
1061 NTuple<Descriptor> calleeHeapPath = computePath(calleeParam);
1063 if (!calleeParam.getType().isPrimitive()) {
1064 createNewMappingOfMayWrittenSet(mdCaller, callee, callerArgHeapPath,
1065 callerArgLocationPath, calleeHeapPath, calleeLocationPath,
1066 mapParamHeapPathToCallerArgHeapPath);
1076 private Hashtable<NTuple<Location>, Set<NTuple<Descriptor>>> getMappingByStartedWith(
1077 MultiSourceMap<NTuple<Location>, NTuple<Descriptor>> map, NTuple<Location> in) {
1079 Hashtable<NTuple<Location>, Set<NTuple<Descriptor>>> matchedMapping =
1080 new Hashtable<NTuple<Location>, Set<NTuple<Descriptor>>>();
1082 Set<NTuple<Location>> keySet = map.keySet();
1084 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1085 NTuple<Location> key = (NTuple<Location>) iterator.next();
1086 if (key.startsWith(in)) {
1087 matchedMapping.put(key, map.get(key));
1091 return matchedMapping;
1095 private void createNewMappingOfMayWrittenSet(MethodDescriptor caller, MethodDescriptor callee,
1096 NTuple<Descriptor> callerArgHeapPath, NTuple<Location> callerArgLocPath,
1097 NTuple<Descriptor> calleeParamHeapPath, NTuple<Location> calleeParamLocPath,
1098 Hashtable<NTuple<Descriptor>, NTuple<Descriptor>> mapParamHeapPathToCallerArgHeapPath) {
1100 // propagate may-written-set associated with the key that is started with
1101 // calleepath to the caller
1102 // 1) makes a new key by combining caller path and callee path(except local
1103 // loc element of param)
1104 // 2) create new mapping of may-written-set of callee path to caller path
1106 // extract all may written effect accessed through callee param path
1107 MultiSourceMap<NTuple<Location>, NTuple<Descriptor>> calleeMapping =
1108 mapMethodToSharedLocCoverSet.get(callee);
1110 if (calleeMapping == null) {
1114 MultiSourceMap<NTuple<Location>, NTuple<Descriptor>> callerMapping =
1115 mapMethodToSharedLocCoverSet.get(caller);
1117 if (callerMapping == null) {
1118 callerMapping = new MultiSourceMap<NTuple<Location>, NTuple<Descriptor>>();
1119 mapMethodToSharedLocCoverSet.put(caller, callerMapping);
1122 Hashtable<NTuple<Location>, Set<NTuple<Descriptor>>> paramMapping =
1123 getMappingByStartedWith(calleeMapping, calleeParamLocPath);
1125 Set<NTuple<Location>> calleeKeySet = paramMapping.keySet();
1127 for (Iterator iterator = calleeKeySet.iterator(); iterator.hasNext();) {
1128 NTuple<Location> calleeKey = (NTuple<Location>) iterator.next();
1130 Set<NTuple<Descriptor>> calleeMayWriteSet = paramMapping.get(calleeKey);
1132 if (calleeMayWriteSet != null) {
1134 Set<NTuple<Descriptor>> boundMayWriteSet = new HashSet<NTuple<Descriptor>>();
1136 Set<NTuple<Descriptor>> boundSet =
1137 convertToCallerMayWriteSet(calleeParamHeapPath, calleeMayWriteSet, callerMapping,
1138 mapParamHeapPathToCallerArgHeapPath);
1140 boundMayWriteSet.addAll(boundSet);
1142 NTuple<Location> newKey = new NTuple<Location>();
1143 newKey.addAll(callerArgLocPath);
1144 // need to replace the local location with the caller's path so skip the
1145 // local location of the parameter
1146 for (int i = 1; i < calleeKey.size(); i++) {
1147 newKey.add(calleeKey.get(i));
1150 callerMapping.union(newKey, boundMayWriteSet);
1157 private Set<NTuple<Descriptor>> convertToCallerMayWriteSet(
1158 NTuple<Descriptor> calleeParamHeapPath, Set<NTuple<Descriptor>> calleeMayWriteSet,
1159 MultiSourceMap<NTuple<Location>, NTuple<Descriptor>> callerMapping,
1160 Hashtable<NTuple<Descriptor>, NTuple<Descriptor>> mapParamHeapPathToCallerArgHeapPath) {
1162 Set<NTuple<Descriptor>> boundSet = new HashSet<NTuple<Descriptor>>();
1164 // replace callee's param path with caller's arg path
1165 for (Iterator iterator = calleeMayWriteSet.iterator(); iterator.hasNext();) {
1166 NTuple<Descriptor> calleeWriteHeapPath = (NTuple<Descriptor>) iterator.next();
1168 NTuple<Descriptor> writeHeapPathParamHeapPath = calleeWriteHeapPath.subList(0, 1);
1170 NTuple<Descriptor> callerArgHeapPath =
1171 mapParamHeapPathToCallerArgHeapPath.get(writeHeapPathParamHeapPath);
1173 NTuple<Descriptor> boundHeapPath = new NTuple<Descriptor>();
1174 boundHeapPath.addAll(callerArgHeapPath);
1176 for (int i = 1; i < calleeWriteHeapPath.size(); i++) {
1177 boundHeapPath.add(calleeWriteHeapPath.get(i));
1180 boundSet.add(boundHeapPath);
1187 private Location getLocation(Descriptor d) {
1189 if (d instanceof FieldDescriptor) {
1190 TypeExtension te = ((FieldDescriptor) d).getType().getExtension();
1192 return (Location) te;
1195 assert d instanceof TempDescriptor;
1196 TempDescriptor td = (TempDescriptor) d;
1198 TypeExtension te = td.getType().getExtension();
1200 if (te instanceof SSJavaType) {
1201 SSJavaType ssType = (SSJavaType) te;
1202 if (ssType.getCompLoc() != null) {
1203 CompositeLocation comp = ssType.getCompLoc();
1204 return comp.get(comp.getSize() - 1);
1209 return (Location) te;
1214 return mapDescToLocation.get(d);
1217 private void eventLoopAnalysis() {
1218 // perform second stage analysis: intraprocedural analysis ensure that
1220 // variables are definitely written in-between the same read
1222 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
1223 flatNodesToVisit.add(ssjava.getSSJavaLoopEntrance());
1225 while (!flatNodesToVisit.isEmpty()) {
1226 FlatNode fn = (FlatNode) flatNodesToVisit.iterator().next();
1227 flatNodesToVisit.remove(fn);
1229 Hashtable<NTuple<Descriptor>, Set<WriteAge>> prev = mapFlatNodetoEventLoopMap.get(fn);
1231 Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr =
1232 new Hashtable<NTuple<Descriptor>, Set<WriteAge>>();
1233 for (int i = 0; i < fn.numPrev(); i++) {
1234 FlatNode nn = fn.getPrev(i);
1235 Hashtable<NTuple<Descriptor>, Set<WriteAge>> in = mapFlatNodetoEventLoopMap.get(nn);
1241 eventLoopAnalysis_nodeAction(fn, curr, ssjava.getSSJavaLoopEntrance());
1243 // if a new result, schedule forward nodes for analysis
1244 if (!curr.equals(prev)) {
1245 mapFlatNodetoEventLoopMap.put(fn, curr);
1247 for (int i = 0; i < fn.numNext(); i++) {
1248 FlatNode nn = fn.getNext(i);
1249 if (loopIncElements.contains(nn)) {
1250 flatNodesToVisit.add(nn);
1258 private void union(Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr,
1259 Hashtable<NTuple<Descriptor>, Set<WriteAge>> in) {
1261 Set<NTuple<Descriptor>> inKeySet = in.keySet();
1262 for (Iterator iterator = inKeySet.iterator(); iterator.hasNext();) {
1263 NTuple<Descriptor> inKey = (NTuple<Descriptor>) iterator.next();
1264 Set<WriteAge> inSet = in.get(inKey);
1266 Set<WriteAge> currSet = curr.get(inKey);
1268 if (currSet == null) {
1269 currSet = new HashSet<WriteAge>();
1270 curr.put(inKey, currSet);
1272 currSet.addAll(inSet);
1277 private void eventLoopAnalysis_nodeAction(FlatNode fn,
1278 Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr, FlatNode loopEntrance) {
1280 Hashtable<NTuple<Descriptor>, Set<WriteAge>> readWriteKillSet =
1281 new Hashtable<NTuple<Descriptor>, Set<WriteAge>>();
1282 Hashtable<NTuple<Descriptor>, Set<WriteAge>> readWriteGenSet =
1283 new Hashtable<NTuple<Descriptor>, Set<WriteAge>>();
1285 if (fn.equals(loopEntrance)) {
1286 // it reaches loop entrance: changes all flag to true
1287 Set<NTuple<Descriptor>> keySet = curr.keySet();
1288 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1289 NTuple<Descriptor> key = (NTuple<Descriptor>) iterator.next();
1290 Set<WriteAge> writeAgeSet = curr.get(key);
1292 Set<WriteAge> incSet = new HashSet<WriteAge>();
1293 incSet.addAll(writeAgeSet);
1294 writeAgeSet.clear();
1296 for (Iterator iterator2 = incSet.iterator(); iterator2.hasNext();) {
1297 WriteAge writeAge = (WriteAge) iterator2.next();
1298 WriteAge newWriteAge = writeAge.copy();
1300 writeAgeSet.add(newWriteAge);
1308 FieldDescriptor fld;
1310 switch (fn.kind()) {
1312 case FKind.FlatOpNode: {
1313 FlatOpNode fon = (FlatOpNode) fn;
1314 lhs = fon.getDest();
1315 rhs = fon.getLeft();
1317 if (fon.getOp().getOp() == Operation.ASSIGN) {
1319 if (!lhs.getSymbol().startsWith("neverused") && !lhs.getSymbol().startsWith("leftop")
1320 && !lhs.getSymbol().startsWith("rightop")) {
1322 boolean hasWriteEffect = false;
1324 if (rhs.getType().getExtension() instanceof SSJavaType
1325 && lhs.getType().getExtension() instanceof SSJavaType) {
1327 CompositeLocation rhsCompLoc =
1328 ((SSJavaType) rhs.getType().getExtension()).getCompLoc();
1330 CompositeLocation lhsCompLoc =
1331 ((SSJavaType) lhs.getType().getExtension()).getCompLoc();
1333 if (lhsCompLoc != rhsCompLoc) {
1334 // have a write effect!
1335 hasWriteEffect = true;
1338 } else if (lhs.getType().isImmutable()) {
1339 hasWriteEffect = true;
1342 if (hasWriteEffect && mapHeapPath.containsKey(lhs)) {
1344 NTuple<Descriptor> lhsHeapPath = new NTuple<Descriptor>();
1345 lhsHeapPath.addAll(mapHeapPath.get(lhs));
1347 Location lhsLoc = getLocation(lhs);
1348 if (ssjava.isSharedLocation(lhsLoc)) {
1350 NTuple<Descriptor> varHeapPath = computePath(lhs);
1351 NTuple<Location> varLocTuple = mapDescriptorToLocationPath.get(lhs);
1353 Set<NTuple<Descriptor>> writtenSet =
1354 mapFlatNodeToSharedLocMapping.get(fn).get(varLocTuple);
1356 Set<NTuple<Descriptor>> mustClearSet =
1357 mapFlatNodeToMustClearMap.get(fn).get(varLocTuple);
1359 if (isCovered(varLocTuple, writtenSet, mustClearSet)) {
1360 computeKILLSetForSharedWrite(curr, writtenSet, readWriteKillSet);
1361 computeGENSetForSharedAllCoverWrite(curr, writtenSet, readWriteGenSet);
1363 computeGENSetForSharedNonCoverWrite(curr, varHeapPath, readWriteGenSet);
1368 computeKILLSetForWrite(curr, lhsHeapPath, readWriteKillSet);
1369 computeGENSetForWrite(lhsHeapPath, readWriteGenSet);
1372 Set<WriteAge> writeAgeSet = curr.get(lhsHeapPath);
1373 checkWriteAgeSet(writeAgeSet, lhsHeapPath, fn);
1383 case FKind.FlatFieldNode:
1384 case FKind.FlatElementNode: {
1386 if (fn.kind() == FKind.FlatFieldNode) {
1387 FlatFieldNode ffn = (FlatFieldNode) fn;
1390 fld = ffn.getField();
1392 FlatElementNode fen = (FlatElementNode) fn;
1395 TypeDescriptor td = rhs.getType().dereference();
1396 fld = getArrayField(td);
1400 NTuple<Descriptor> srcHeapPath = mapHeapPath.get(rhs);
1401 NTuple<Descriptor> fldHeapPath;
1402 if (srcHeapPath != null) {
1403 fldHeapPath = new NTuple<Descriptor>(srcHeapPath.getList());
1405 // if srcHeapPath is null, it is static reference
1406 fldHeapPath = new NTuple<Descriptor>();
1407 fldHeapPath.add(rhs);
1409 fldHeapPath.add(fld);
1411 Set<WriteAge> writeAgeSet = curr.get(fldHeapPath);
1413 checkWriteAgeSet(writeAgeSet, fldHeapPath, fn);
1418 case FKind.FlatSetFieldNode:
1419 case FKind.FlatSetElementNode: {
1421 if (fn.kind() == FKind.FlatSetFieldNode) {
1422 FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
1423 lhs = fsfn.getDst();
1424 fld = fsfn.getField();
1426 FlatSetElementNode fsen = (FlatSetElementNode) fn;
1427 lhs = fsen.getDst();
1428 rhs = fsen.getSrc();
1429 TypeDescriptor td = lhs.getType().dereference();
1430 fld = getArrayField(td);
1434 NTuple<Descriptor> lhsHeapPath = mapHeapPath.get(lhs);
1435 if (lhsHeapPath != null) {
1437 NTuple<Descriptor> fldHeapPath = new NTuple<Descriptor>(lhsHeapPath.getList());
1438 if (fn.kind() == FKind.FlatSetFieldNode) {
1439 fldHeapPath.add(fld);
1442 // shared loc extension
1444 if (fn.kind() == FKind.FlatSetFieldNode) {
1445 fieldLoc = (Location) fld.getType().getExtension();
1447 NTuple<Location> locTuple = mapDescriptorToLocationPath.get(lhs);
1448 fieldLoc = locTuple.get(locTuple.size() - 1);
1451 if (ssjava.isSharedLocation(fieldLoc)) {
1453 NTuple<Location> fieldLocTuple = new NTuple<Location>();
1454 fieldLocTuple.addAll(mapDescriptorToLocationPath.get(lhs));
1455 if (fn.kind() == FKind.FlatSetFieldNode) {
1456 fieldLocTuple.add(fieldLoc);
1459 Set<NTuple<Descriptor>> writtenSet =
1460 mapFlatNodeToSharedLocMapping.get(fn).get(fieldLocTuple);
1461 if (isCovered(fieldLocTuple, writtenSet)) {
1462 computeKILLSetForSharedWrite(curr, writtenSet, readWriteKillSet);
1463 computeGENSetForSharedAllCoverWrite(curr, writtenSet, readWriteGenSet);
1465 computeGENSetForSharedNonCoverWrite(curr, fldHeapPath, readWriteGenSet);
1469 computeKILLSetForWrite(curr, fldHeapPath, readWriteKillSet);
1470 computeGENSetForWrite(fldHeapPath, readWriteGenSet);
1478 case FKind.FlatCall: {
1479 FlatCall fc = (FlatCall) fn;
1480 SharedLocMap sharedLocMap = mapFlatNodeToSharedLocMapping.get(fc);
1481 SharedLocMap mustClearMap = mapFlatNodeToMustClearMap.get(fc);
1482 generateKILLSetForFlatCall(fc, curr, sharedLocMap, mustClearMap, readWriteKillSet);
1483 generateGENSetForFlatCall(fc, sharedLocMap, mustClearMap, readWriteGenSet);
1490 computeNewMapping(curr, readWriteKillSet, readWriteGenSet);
1491 if (fn instanceof FlatCall) {
1492 checkManyRead((FlatCall) fn, curr);
1499 private void computeGENSetForSharedNonCoverWrite(
1500 Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr, NTuple<Descriptor> heapPath,
1501 Hashtable<NTuple<Descriptor>, Set<WriteAge>> genSet) {
1503 Set<WriteAge> writeAgeSet = genSet.get(heapPath);
1504 if (writeAgeSet == null) {
1505 writeAgeSet = new HashSet<WriteAge>();
1506 genSet.put(heapPath, writeAgeSet);
1509 writeAgeSet.add(new WriteAge(1));
1513 private void computeGENSetForSharedAllCoverWrite(
1514 Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr, Set<NTuple<Descriptor>> writtenSet,
1515 Hashtable<NTuple<Descriptor>, Set<WriteAge>> genSet) {
1517 for (Iterator iterator = writtenSet.iterator(); iterator.hasNext();) {
1518 NTuple<Descriptor> writeHeapPath = (NTuple<Descriptor>) iterator.next();
1520 Set<WriteAge> writeAgeSet = new HashSet<WriteAge>();
1521 writeAgeSet.add(new WriteAge(0));
1523 genSet.put(writeHeapPath, writeAgeSet);
1528 private void computeKILLSetForSharedWrite(Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr,
1529 Set<NTuple<Descriptor>> writtenSet, Hashtable<NTuple<Descriptor>, Set<WriteAge>> killSet) {
1531 for (Iterator iterator = writtenSet.iterator(); iterator.hasNext();) {
1532 NTuple<Descriptor> writeHeapPath = (NTuple<Descriptor>) iterator.next();
1533 Set<WriteAge> writeSet = curr.get(writeHeapPath);
1534 if (writeSet != null) {
1535 killSet.put(writeHeapPath, writeSet);
1541 private boolean isCovered(NTuple<Location> locTuple, Set<NTuple<Descriptor>> curWrittenSet) {
1543 Set<NTuple<Descriptor>> coverSet =
1544 mapMethodToSharedLocCoverSet.get(methodContainingSSJavaLoop).get(locTuple);
1546 if (curWrittenSet == null) {
1550 return curWrittenSet.containsAll(coverSet);
1553 private boolean isCovered(NTuple<Location> locTuple, Set<NTuple<Descriptor>> curWrittenSet,
1554 Set<NTuple<Descriptor>> mustClearSet) {
1556 Set<NTuple<Descriptor>> coverSet =
1557 mapMethodToSharedLocCoverSet.get(methodContainingSSJavaLoop).get(locTuple);
1559 if (mustClearSet != null && mustClearSet.containsAll(coverSet)) {
1563 if (curWrittenSet == null) {
1567 return curWrittenSet.containsAll(coverSet);
1570 private void checkManyRead(FlatCall fc, Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr) {
1571 Set<NTuple<Descriptor>> boundReadSet = mapFlatNodeToBoundReadSet.get(fc);
1572 for (Iterator iterator = boundReadSet.iterator(); iterator.hasNext();) {
1573 NTuple<Descriptor> readHeapPath = (NTuple<Descriptor>) iterator.next();
1574 Set<WriteAge> writeAgeSet = curr.get(readHeapPath);
1575 checkWriteAgeSet(writeAgeSet, readHeapPath, fc);
1580 private void checkWriteAgeSet(Set<WriteAge> writeAgeSet, NTuple<Descriptor> path, FlatNode fn) {
1582 if (writeAgeSet != null) {
1583 for (Iterator iterator = writeAgeSet.iterator(); iterator.hasNext();) {
1584 WriteAge writeAge = (WriteAge) iterator.next();
1585 if (writeAge.getAge() > MAXAGE) {
1586 generateErrorMessage(path, fn);
1592 private void generateErrorMessage(NTuple<Descriptor> path, FlatNode fn) {
1594 Descriptor lastDesc = path.get(getArrayBaseDescriptorIdx(path));
1595 if (ssjava.isSharedLocation(getLocation(lastDesc))) {
1597 NTuple<Location> locPathTuple = getLocationTuple(path);
1598 Set<NTuple<Descriptor>> coverSet =
1599 mapMethodToSharedLocCoverSet.get(methodContainingSSJavaLoop).get(locPathTuple);
1600 throw new Error("Shared memory locations, which is reachable through references " + path
1601 + ", are not completely overwritten by the higher values at "
1602 + methodContainingSSJavaLoop.getClassDesc().getSourceFileName() + "::" + fn.getNumLine()
1603 + ".\nThe following memory locations belong to the same shared locations:" + coverSet);
1607 "Memory location, which is reachable through references "
1609 + ", who comes back to the same read statement without being overwritten at the out-most iteration at "
1610 + methodContainingSSJavaLoop.getClassDesc().getSourceFileName() + "::"
1616 private void generateGENSetForFlatCall(FlatCall fc, SharedLocMap sharedLocMap,
1617 SharedLocMap mustClearMap, Hashtable<NTuple<Descriptor>, Set<WriteAge>> GENSet) {
1619 Set<NTuple<Descriptor>> boundMayWriteSet = mapFlatNodeToBoundMayWriteSet.get(fc);
1621 for (Iterator iterator = boundMayWriteSet.iterator(); iterator.hasNext();) {
1622 NTuple<Descriptor> heapPath = (NTuple<Descriptor>) iterator.next();
1624 if (!isSharedLocation(heapPath)) {
1625 addWriteAgeToSet(heapPath, GENSet, new WriteAge(0));
1627 // if the current heap path is shared location
1629 NTuple<Location> locTuple = getLocationTuple(heapPath);
1631 Set<NTuple<Descriptor>> sharedWriteHeapPathSet = sharedLocMap.get(locTuple);
1633 if (isCovered(locTuple, sharedLocMap.get(locTuple), mustClearMap.get(locTuple))) {
1634 // if it is covered, add all of heap paths belong to the same shared
1635 // loc with write age 0
1636 for (Iterator iterator2 = sharedWriteHeapPathSet.iterator(); iterator2.hasNext();) {
1637 NTuple<Descriptor> sharedHeapPath = (NTuple<Descriptor>) iterator2.next();
1638 addWriteAgeToSet(sharedHeapPath, GENSet, new WriteAge(0));
1642 // if not covered, add write age 1 to the heap path that is
1643 // may-written but not covered
1644 addWriteAgeToSet(heapPath, GENSet, new WriteAge(1));
1653 private void addWriteAgeToSet(NTuple<Descriptor> heapPath,
1654 Hashtable<NTuple<Descriptor>, Set<WriteAge>> map, WriteAge age) {
1656 Set<WriteAge> currSet = map.get(heapPath);
1657 if (currSet == null) {
1658 currSet = new HashSet<WriteAge>();
1659 map.put(heapPath, currSet);
1665 private void generateKILLSetForFlatCall(FlatCall fc,
1666 Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr, SharedLocMap sharedLocMap,
1667 SharedLocMap mustClearMap, Hashtable<NTuple<Descriptor>, Set<WriteAge>> KILLSet) {
1669 Set<NTuple<Descriptor>> boundMustWriteSet = mapFlatNodeToBoundMustWriteSet.get(fc);
1671 for (Iterator iterator = boundMustWriteSet.iterator(); iterator.hasNext();) {
1672 NTuple<Descriptor> heapPath = (NTuple<Descriptor>) iterator.next();
1674 if (isSharedLocation(heapPath)) {
1675 NTuple<Location> locTuple = getLocationTuple(heapPath);
1677 if (isCovered(locTuple, sharedLocMap.get(locTuple), mustClearMap.get(locTuple))
1678 && curr.containsKey(heapPath)) {
1679 // if it is shared loc and corresponding shared loc has been covered
1680 KILLSet.put(heapPath, curr.get(heapPath));
1684 for (Enumeration<NTuple<Descriptor>> e = curr.keys(); e.hasMoreElements();) {
1685 NTuple<Descriptor> key = e.nextElement();
1686 if (key.startsWith(heapPath)) {
1687 KILLSet.put(key, curr.get(key));
1697 private int getArrayBaseDescriptorIdx(NTuple<Descriptor> heapPath) {
1699 for (int i = heapPath.size() - 1; i >= 0; i--) {
1700 if (!heapPath.get(i).getSymbol().equals(arrayElementFieldName)) {
1709 private boolean isSharedLocation(NTuple<Descriptor> heapPath) {
1711 Descriptor d = heapPath.get(getArrayBaseDescriptorIdx(heapPath));
1713 return ssjava.isSharedLocation(getLocation(heapPath.get(getArrayBaseDescriptorIdx(heapPath))));
1717 private NTuple<Location> getLocationTuple(NTuple<Descriptor> heapPath) {
1719 NTuple<Location> locTuple = new NTuple<Location>();
1721 locTuple.addAll(mapDescriptorToLocationPath.get(heapPath.get(0)));
1723 for (int i = 1; i <= getArrayBaseDescriptorIdx(heapPath); i++) {
1724 locTuple.add(getLocation(heapPath.get(i)));
1730 private void computeNewMapping(Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr,
1731 Hashtable<NTuple<Descriptor>, Set<WriteAge>> KILLSet,
1732 Hashtable<NTuple<Descriptor>, Set<WriteAge>> GENSet) {
1734 for (Enumeration<NTuple<Descriptor>> e = KILLSet.keys(); e.hasMoreElements();) {
1735 NTuple<Descriptor> key = e.nextElement();
1737 Set<WriteAge> writeAgeSet = curr.get(key);
1738 if (writeAgeSet == null) {
1739 writeAgeSet = new HashSet<WriteAge>();
1740 curr.put(key, writeAgeSet);
1742 writeAgeSet.removeAll(KILLSet.get(key));
1745 for (Enumeration<NTuple<Descriptor>> e = GENSet.keys(); e.hasMoreElements();) {
1746 NTuple<Descriptor> key = e.nextElement();
1748 Set<WriteAge> currWriteAgeSet = curr.get(key);
1749 if (currWriteAgeSet == null) {
1750 currWriteAgeSet = new HashSet<WriteAge>();
1751 curr.put(key, currWriteAgeSet);
1753 currWriteAgeSet.addAll(GENSet.get(key));
1758 private void computeGENSetForWrite(NTuple<Descriptor> fldHeapPath,
1759 Hashtable<NTuple<Descriptor>, Set<WriteAge>> GENSet) {
1761 // generate write age 0 for the field being written to
1762 Set<WriteAge> writeAgeSet = new HashSet<WriteAge>();
1763 writeAgeSet.add(new WriteAge(0));
1764 GENSet.put(fldHeapPath, writeAgeSet);
1768 private void computeKILLSetForWrite(Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr,
1769 NTuple<Descriptor> hp, Hashtable<NTuple<Descriptor>, Set<WriteAge>> KILLSet) {
1771 // removes all of heap path that starts with prefix 'hp'
1772 // since any reference overwrite along heap path gives overwriting side
1773 // effects on the value
1775 Set<NTuple<Descriptor>> keySet = curr.keySet();
1776 for (Iterator<NTuple<Descriptor>> iter = keySet.iterator(); iter.hasNext();) {
1777 NTuple<Descriptor> key = iter.next();
1778 if (key.startsWith(hp)) {
1779 KILLSet.put(key, curr.get(key));
1785 private void bindHeapPathCallerArgWithCalleeParam(FlatCall fc) {
1786 // compute all possible callee set
1787 // transform all READ/WRITE set from the any possible
1788 // callees to the caller
1789 calleeUnionBoundReadSet.clear();
1790 calleeIntersectBoundMustWriteSet.clear();
1791 calleeUnionBoundMayWriteSet.clear();
1793 if (ssjava.isSSJavaUtil(fc.getMethod().getClassDesc())) {
1794 // ssjava util case!
1795 // have write effects on the first argument
1796 TempDescriptor arg = fc.getArg(0);
1797 NTuple<Descriptor> argHeapPath = computePath(arg);
1798 calleeIntersectBoundMustWriteSet.add(argHeapPath);
1799 calleeUnionBoundMayWriteSet.add(argHeapPath);
1801 MethodDescriptor mdCallee = fc.getMethod();
1802 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
1803 setPossibleCallees.addAll(callGraph.getMethods(mdCallee));
1805 // create mapping from arg idx to its heap paths
1806 Hashtable<Integer, NTuple<Descriptor>> mapArgIdx2CallerArgHeapPath =
1807 new Hashtable<Integer, NTuple<Descriptor>>();
1809 // arg idx is starting from 'this' arg
1810 if (fc.getThis() != null) {
1811 NTuple<Descriptor> thisHeapPath = mapHeapPath.get(fc.getThis());
1812 if (thisHeapPath != null) {
1813 // if 'this' does not have heap path, it is local reference
1814 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(0), thisHeapPath);
1818 for (int i = 0; i < fc.numArgs(); i++) {
1819 TempDescriptor arg = fc.getArg(i);
1820 NTuple<Descriptor> argHeapPath = computePath(arg);
1821 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(i + 1), argHeapPath);
1824 for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
1825 MethodDescriptor callee = (MethodDescriptor) iterator.next();
1826 FlatMethod calleeFlatMethod = state.getMethodFlat(callee);
1828 // binding caller's args and callee's params
1830 Set<NTuple<Descriptor>> calleeReadSet = mapFlatMethodToReadSet.get(calleeFlatMethod);
1831 if (calleeReadSet == null) {
1832 calleeReadSet = new HashSet<NTuple<Descriptor>>();
1833 mapFlatMethodToReadSet.put(calleeFlatMethod, calleeReadSet);
1836 Set<NTuple<Descriptor>> calleeMustWriteSet =
1837 mapFlatMethodToMustWriteSet.get(calleeFlatMethod);
1839 if (calleeMustWriteSet == null) {
1840 calleeMustWriteSet = new HashSet<NTuple<Descriptor>>();
1841 mapFlatMethodToMustWriteSet.put(calleeFlatMethod, calleeMustWriteSet);
1844 Set<NTuple<Descriptor>> calleeMayWriteSet =
1845 mapFlatMethodToMayWriteSet.get(calleeFlatMethod);
1847 if (calleeMayWriteSet == null) {
1848 calleeMayWriteSet = new HashSet<NTuple<Descriptor>>();
1849 mapFlatMethodToMayWriteSet.put(calleeFlatMethod, calleeMayWriteSet);
1852 Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc =
1853 new Hashtable<Integer, TempDescriptor>();
1855 if (calleeFlatMethod.getMethod().isStatic()) {
1856 // static method does not have implicit 'this' arg
1859 for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
1860 TempDescriptor param = calleeFlatMethod.getParameter(i);
1861 mapParamIdx2ParamTempDesc.put(Integer.valueOf(i + offset), param);
1864 Set<NTuple<Descriptor>> calleeBoundReadSet =
1865 bindSet(calleeReadSet, mapParamIdx2ParamTempDesc, mapArgIdx2CallerArgHeapPath);
1866 // union of the current read set and the current callee's
1868 calleeUnionBoundReadSet.addAll(calleeBoundReadSet);
1870 Set<NTuple<Descriptor>> calleeBoundMustWriteSet =
1871 bindSet(calleeMustWriteSet, mapParamIdx2ParamTempDesc, mapArgIdx2CallerArgHeapPath);
1872 // intersection of the current overwrite set and the current
1875 merge(calleeIntersectBoundMustWriteSet, calleeBoundMustWriteSet);
1877 Set<NTuple<Descriptor>> boundWriteSetFromCallee =
1878 bindSet(calleeMayWriteSet, mapParamIdx2ParamTempDesc, mapArgIdx2CallerArgHeapPath);
1879 calleeUnionBoundMayWriteSet.addAll(boundWriteSetFromCallee);
1886 private void bindHeapPathCallerArgWithCaleeParamForSharedLoc(MethodDescriptor mdCaller,
1889 calleeIntersectBoundSharedSet.clear();
1890 calleeUnionBoundDeleteSet.clear();
1892 if (ssjava.isSSJavaUtil(fc.getMethod().getClassDesc())) {
1893 // ssjava util case!
1894 // have write effects on the first argument
1895 TempDescriptor arg = fc.getArg(0);
1896 NTuple<Descriptor> argHeapPath = computePath(arg);
1898 // convert heap path to location path
1899 NTuple<Location> argLocTuple = new NTuple<Location>();
1900 argLocTuple.addAll(deriveLocationTuple(mdCaller, (TempDescriptor) argHeapPath.get(0)));
1901 for (int i = 1; i < argHeapPath.size(); i++) {
1902 argLocTuple.add(getLocation(argHeapPath.get(i)));
1905 calleeIntersectBoundSharedSet.addWrite(argLocTuple, argHeapPath);
1907 } else if (ssjava.needTobeAnnotated(fc.getMethod())) {
1909 // if arg is not primitive type, we need to propagate maywritten set to
1910 // the caller's location path
1912 MethodDescriptor mdCallee = fc.getMethod();
1913 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
1914 setPossibleCallees.addAll(callGraph.getMethods(mdCallee));
1916 // create mapping from arg idx to its heap paths
1917 Hashtable<Integer, NTuple<Descriptor>> mapArgIdx2CallerArgHeapPath =
1918 new Hashtable<Integer, NTuple<Descriptor>>();
1920 // arg idx is starting from 'this' arg
1921 if (fc.getThis() != null) {
1922 NTuple<Descriptor> thisHeapPath = mapHeapPath.get(fc.getThis());
1923 if (thisHeapPath == null) {
1924 // method is called without creating new flat node representing 'this'
1925 thisHeapPath = new NTuple<Descriptor>();
1926 thisHeapPath.add(fc.getThis());
1929 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(0), thisHeapPath);
1932 for (int i = 0; i < fc.numArgs(); i++) {
1933 TempDescriptor arg = fc.getArg(i);
1934 NTuple<Descriptor> argHeapPath = computePath(arg);
1935 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(i + 1), argHeapPath);
1938 // create mapping from arg idx to its location paths
1939 Hashtable<Integer, NTuple<Location>> mapArgIdx2CallerAgLocationPath =
1940 new Hashtable<Integer, NTuple<Location>>();
1942 // arg idx is starting from 'this' arg
1943 if (fc.getThis() != null) {
1944 NTuple<Location> thisLocationPath = deriveLocationTuple(fc.getMethod(), fc.getThis());
1945 if (thisLocationPath != null) {
1946 mapArgIdx2CallerAgLocationPath.put(Integer.valueOf(0), thisLocationPath);
1950 for (int i = 0; i < fc.numArgs(); i++) {
1951 TempDescriptor arg = fc.getArg(i);
1952 NTuple<Location> argLocationPath = deriveLocationTuple(mdCaller, arg);
1953 if (argLocationPath != null) {
1954 mapArgIdx2CallerAgLocationPath.put(Integer.valueOf(i + 1), argLocationPath);
1958 for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
1959 MethodDescriptor callee = (MethodDescriptor) iterator.next();
1960 FlatMethod calleeFlatMethod = state.getMethodFlat(callee);
1962 // binding caller's args and callee's params
1964 Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc =
1965 new Hashtable<Integer, TempDescriptor>();
1967 if (calleeFlatMethod.getMethod().isStatic()) {
1968 // static method does not have implicit 'this' arg
1971 for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
1972 TempDescriptor param = calleeFlatMethod.getParameter(i);
1973 mapParamIdx2ParamTempDesc.put(Integer.valueOf(i + offset), param);
1976 Set<Integer> keySet = mapArgIdx2CallerAgLocationPath.keySet();
1977 for (Iterator iterator2 = keySet.iterator(); iterator2.hasNext();) {
1978 Integer idx = (Integer) iterator2.next();
1979 NTuple<Location> callerArgLocationPath = mapArgIdx2CallerAgLocationPath.get(idx);
1980 NTuple<Descriptor> callerArgHeapPath = mapArgIdx2CallerArgHeapPath.get(idx);
1982 TempDescriptor calleeParam = mapParamIdx2ParamTempDesc.get(idx);
1983 NTuple<Location> calleeLocationPath = deriveLocationTuple(mdCallee, calleeParam);
1984 SharedLocMap calleeDeleteSet = mapFlatMethodToDeleteSet.get(calleeFlatMethod);
1985 SharedLocMap calleeSharedLocMap = mapFlatMethodToSharedLocMap.get(calleeFlatMethod);
1986 SharedLocMap calleeMustClearMap = mapFlatMethodToMustClearMap.get(calleeFlatMethod);
1988 if (calleeDeleteSet != null) {
1989 createNewMappingOfDeleteSet(callerArgLocationPath, callerArgHeapPath,
1990 calleeLocationPath, calleeDeleteSet);
1993 if (calleeSharedLocMap != null) {
1994 createNewMappingOfSharedSet(callerArgLocationPath, callerArgHeapPath,
1995 calleeLocationPath, calleeSharedLocMap);
1998 if (calleeMustClearMap != null) {
1999 createNewMappingOfMustClearMap(callerArgLocationPath, callerArgHeapPath,
2000 calleeLocationPath, calleeMustClearMap);
2010 private void createNewMappingOfMustClearMap(NTuple<Location> callerArgLocationPath,
2011 NTuple<Descriptor> callerArgHeapPath, NTuple<Location> calleeLocationPath,
2012 SharedLocMap calleeMustClearMap) {
2014 SharedLocMap calleeParamSharedSet =
2015 calleeMustClearMap.getHeapPathStartedWith(calleeLocationPath);
2017 Set<NTuple<Location>> keySet = calleeParamSharedSet.keySet();
2018 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2019 NTuple<Location> calleeLocTupleKey = (NTuple<Location>) iterator.next();
2020 Set<NTuple<Descriptor>> heapPathSet = calleeParamSharedSet.get(calleeLocTupleKey);
2021 Set<NTuple<Descriptor>> boundHeapPathSet = new HashSet<NTuple<Descriptor>>();
2022 for (Iterator iterator2 = heapPathSet.iterator(); iterator2.hasNext();) {
2023 NTuple<Descriptor> calleeHeapPath = (NTuple<Descriptor>) iterator2.next();
2024 boundHeapPathSet.add(bindHeapPath(callerArgHeapPath, calleeHeapPath));
2026 calleeIntersectBoundMustClearSet.intersect(
2027 bindLocationPath(callerArgLocationPath, calleeLocTupleKey), boundHeapPathSet);
2032 private void createNewMappingOfDeleteSet(NTuple<Location> callerArgLocationPath,
2033 NTuple<Descriptor> callerArgHeapPath, NTuple<Location> calleeLocationPath,
2034 SharedLocMap calleeDeleteSet) {
2036 SharedLocMap calleeParamDeleteSet = calleeDeleteSet.getHeapPathStartedWith(calleeLocationPath);
2038 Set<NTuple<Location>> keySet = calleeParamDeleteSet.keySet();
2039 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2040 NTuple<Location> calleeLocTupleKey = (NTuple<Location>) iterator.next();
2041 Set<NTuple<Descriptor>> heapPathSet = calleeParamDeleteSet.get(calleeLocTupleKey);
2042 for (Iterator iterator2 = heapPathSet.iterator(); iterator2.hasNext();) {
2043 NTuple<Descriptor> calleeHeapPath = (NTuple<Descriptor>) iterator2.next();
2044 calleeUnionBoundDeleteSet.addWrite(
2045 bindLocationPath(callerArgLocationPath, calleeLocTupleKey),
2046 bindHeapPath(callerArgHeapPath, calleeHeapPath));
2052 private void createNewMappingOfSharedSet(NTuple<Location> callerArgLocationPath,
2053 NTuple<Descriptor> callerArgHeapPath, NTuple<Location> calleeLocationPath,
2054 SharedLocMap calleeSharedLocMap) {
2056 SharedLocMap calleeParamSharedSet =
2057 calleeSharedLocMap.getHeapPathStartedWith(calleeLocationPath);
2059 Set<NTuple<Location>> keySet = calleeParamSharedSet.keySet();
2060 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2061 NTuple<Location> calleeLocTupleKey = (NTuple<Location>) iterator.next();
2062 Set<NTuple<Descriptor>> heapPathSet = calleeParamSharedSet.get(calleeLocTupleKey);
2063 Set<NTuple<Descriptor>> boundHeapPathSet = new HashSet<NTuple<Descriptor>>();
2064 for (Iterator iterator2 = heapPathSet.iterator(); iterator2.hasNext();) {
2065 NTuple<Descriptor> calleeHeapPath = (NTuple<Descriptor>) iterator2.next();
2066 boundHeapPathSet.add(bindHeapPath(callerArgHeapPath, calleeHeapPath));
2068 calleeIntersectBoundSharedSet.intersect(
2069 bindLocationPath(callerArgLocationPath, calleeLocTupleKey), boundHeapPathSet);
2074 private NTuple<Location> bindLocationPath(NTuple<Location> start, NTuple<Location> end) {
2075 NTuple<Location> locPath = new NTuple<Location>();
2076 locPath.addAll(start);
2077 for (int i = 1; i < end.size(); i++) {
2078 locPath.add(end.get(i));
2083 private NTuple<Descriptor> bindHeapPath(NTuple<Descriptor> start, NTuple<Descriptor> end) {
2084 NTuple<Descriptor> heapPath = new NTuple<Descriptor>();
2085 heapPath.addAll(start);
2086 for (int i = 1; i < end.size(); i++) {
2087 heapPath.add(end.get(i));
2092 private void initialize() {
2093 // First, identify ssjava loop entrace
2095 // no need to analyze method having ssjava loop
2096 methodContainingSSJavaLoop = ssjava.getMethodContainingSSJavaLoop();
2098 FlatMethod fm = state.getMethodFlat(methodContainingSSJavaLoop);
2099 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
2100 flatNodesToVisit.add(fm);
2102 LoopFinder loopFinder = new LoopFinder(fm);
2104 while (!flatNodesToVisit.isEmpty()) {
2105 FlatNode fn = flatNodesToVisit.iterator().next();
2106 flatNodesToVisit.remove(fn);
2108 String label = (String) state.fn2labelMap.get(fn);
2109 if (label != null) {
2111 if (label.equals(ssjava.SSJAVA)) {
2112 ssjava.setSSJavaLoopEntrance(fn);
2117 for (int i = 0; i < fn.numNext(); i++) {
2118 FlatNode nn = fn.getNext(i);
2119 flatNodesToVisit.add(nn);
2123 assert ssjava.getSSJavaLoopEntrance() != null;
2125 // assume that ssjava loop is top-level loop in method, not nested loop
2126 Set nestedLoop = loopFinder.nestedLoops();
2127 for (Iterator loopIter = nestedLoop.iterator(); loopIter.hasNext();) {
2128 LoopFinder lf = (LoopFinder) loopIter.next();
2129 if (lf.loopEntrances().iterator().next().equals(ssjava.getSSJavaLoopEntrance())) {
2134 assert ssjavaLoop != null;
2136 loopIncElements = (Set<FlatNode>) ssjavaLoop.loopIncElements();
2138 // perform topological sort over the set of methods accessed by the main
2140 // Set<MethodDescriptor> methodDescriptorsToAnalyze = new
2141 // HashSet<MethodDescriptor>();
2142 // methodDescriptorsToAnalyze.addAll(ssjava.getAnnotationRequireSet());
2143 // sortedDescriptors = topologicalSort(methodDescriptorsToAnalyze);
2145 liveInTempSetToEventLoop =
2146 liveness.getLiveInTemps(state.getMethodFlat(methodContainingSSJavaLoop),
2147 ssjava.getSSJavaLoopEntrance());
2150 private void methodReadWriteSetAnalysis() {
2151 // perform method READ/OVERWRITE analysis
2152 LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
2154 // current descriptors to visit in fixed-point interprocedural analysis,
2156 // dependency in the call graph
2157 methodDescriptorsToVisitStack.clear();
2159 descriptorListToAnalyze.removeFirst();
2161 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
2162 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
2164 while (!descriptorListToAnalyze.isEmpty()) {
2165 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
2166 methodDescriptorsToVisitStack.add(md);
2169 // analyze scheduled methods until there are no more to visit
2170 while (!methodDescriptorsToVisitStack.isEmpty()) {
2171 // start to analyze leaf node
2172 MethodDescriptor md = methodDescriptorsToVisitStack.pop();
2173 FlatMethod fm = state.getMethodFlat(md);
2175 Set<NTuple<Descriptor>> readSet = new HashSet<NTuple<Descriptor>>();
2176 Set<NTuple<Descriptor>> mustWriteSet = new HashSet<NTuple<Descriptor>>();
2177 Set<NTuple<Descriptor>> mayWriteSet = new HashSet<NTuple<Descriptor>>();
2179 methodReadWriteSet_analyzeMethod(fm, readSet, mustWriteSet, mayWriteSet);
2181 Set<NTuple<Descriptor>> prevRead = mapFlatMethodToReadSet.get(fm);
2182 Set<NTuple<Descriptor>> prevMustWrite = mapFlatMethodToMustWriteSet.get(fm);
2183 Set<NTuple<Descriptor>> prevMayWrite = mapFlatMethodToMayWriteSet.get(fm);
2185 if (!(readSet.equals(prevRead) && mustWriteSet.equals(prevMustWrite) && mayWriteSet
2186 .equals(prevMayWrite))) {
2187 mapFlatMethodToReadSet.put(fm, readSet);
2188 mapFlatMethodToMustWriteSet.put(fm, mustWriteSet);
2189 mapFlatMethodToMayWriteSet.put(fm, mayWriteSet);
2191 // results for callee changed, so enqueue dependents caller for
2194 Iterator<MethodDescriptor> depsItr = ssjava.getDependents(md).iterator();
2195 while (depsItr.hasNext()) {
2196 MethodDescriptor methodNext = depsItr.next();
2197 if (!methodDescriptorsToVisitStack.contains(methodNext)
2198 && methodDescriptorToVistSet.contains(methodNext)) {
2199 methodDescriptorsToVisitStack.add(methodNext);
2208 methodReadWriteSetAnalysisToEventLoopBody();
2212 private void methodReadWriteSet_analyzeMethod(FlatMethod fm, Set<NTuple<Descriptor>> readSet,
2213 Set<NTuple<Descriptor>> mustWriteSet, Set<NTuple<Descriptor>> mayWriteSet) {
2214 if (state.SSJAVADEBUG) {
2215 System.out.println("SSJAVA: Definitely written Analyzing: " + fm);
2218 methodReadWriteSet_analyzeBody(fm, readSet, mustWriteSet, mayWriteSet, false);
2222 private void methodReadWriteSetAnalysisToEventLoopBody() {
2224 // perform method read/write analysis for Event Loop Body
2226 FlatMethod flatMethodContainingSSJavaLoop = state.getMethodFlat(methodContainingSSJavaLoop);
2228 if (state.SSJAVADEBUG) {
2229 System.out.println("SSJAVA: Definitely written Event Loop Analyzing: "
2230 + flatMethodContainingSSJavaLoop);
2233 Set<NTuple<Descriptor>> readSet = new HashSet<NTuple<Descriptor>>();
2234 Set<NTuple<Descriptor>> mustWriteSet = new HashSet<NTuple<Descriptor>>();
2235 Set<NTuple<Descriptor>> mayWriteSet = new HashSet<NTuple<Descriptor>>();
2237 mapFlatMethodToReadSet.put(flatMethodContainingSSJavaLoop, readSet);
2238 mapFlatMethodToMustWriteSet.put(flatMethodContainingSSJavaLoop, mustWriteSet);
2239 mapFlatMethodToMayWriteSet.put(flatMethodContainingSSJavaLoop, mayWriteSet);
2241 for (Iterator iterator = liveInTempSetToEventLoop.iterator(); iterator.hasNext();) {
2242 TempDescriptor liveIn = (TempDescriptor) iterator.next();
2243 NTuple<Descriptor> heapPath = new NTuple<Descriptor>();
2244 heapPath.add(liveIn);
2245 mapHeapPath.put(liveIn, heapPath);
2248 methodReadWriteSet_analyzeBody(ssjava.getSSJavaLoopEntrance(), readSet, mustWriteSet,
2253 private void methodReadWriteSet_analyzeBody(FlatNode startNode, Set<NTuple<Descriptor>> readSet,
2254 Set<NTuple<Descriptor>> mustWriteSet, Set<NTuple<Descriptor>> mayWriteSet,
2255 boolean isEventLoopBody) {
2257 // intraprocedural analysis
2258 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
2259 flatNodesToVisit.add(startNode);
2261 while (!flatNodesToVisit.isEmpty()) {
2262 FlatNode fn = flatNodesToVisit.iterator().next();
2263 flatNodesToVisit.remove(fn);
2265 Set<NTuple<Descriptor>> currMustWriteSet = new HashSet<NTuple<Descriptor>>();
2267 for (int i = 0; i < fn.numPrev(); i++) {
2268 FlatNode prevFn = fn.getPrev(i);
2269 Set<NTuple<Descriptor>> in = mapFlatNodeToMustWriteSet.get(prevFn);
2271 merge(currMustWriteSet, in);
2275 methodReadWriteSet_nodeActions(fn, currMustWriteSet, readSet, mustWriteSet, mayWriteSet,
2278 Set<NTuple<Descriptor>> mustSetPrev = mapFlatNodeToMustWriteSet.get(fn);
2280 if (!currMustWriteSet.equals(mustSetPrev)) {
2281 mapFlatNodeToMustWriteSet.put(fn, currMustWriteSet);
2282 for (int i = 0; i < fn.numNext(); i++) {
2283 FlatNode nn = fn.getNext(i);
2284 if ((!isEventLoopBody) || loopIncElements.contains(nn)) {
2285 flatNodesToVisit.add(nn);
2295 private void methodReadWriteSet_nodeActions(FlatNode fn,
2296 Set<NTuple<Descriptor>> currMustWriteSet, Set<NTuple<Descriptor>> readSet,
2297 Set<NTuple<Descriptor>> mustWriteSet, Set<NTuple<Descriptor>> mayWriteSet,
2298 boolean isEventLoopBody) {
2302 FieldDescriptor fld;
2304 switch (fn.kind()) {
2305 case FKind.FlatMethod: {
2307 // set up initial heap paths for method parameters
2308 FlatMethod fm = (FlatMethod) fn;
2309 for (int i = 0; i < fm.numParameters(); i++) {
2310 TempDescriptor param = fm.getParameter(i);
2311 NTuple<Descriptor> heapPath = new NTuple<Descriptor>();
2312 heapPath.add(param);
2313 mapHeapPath.put(param, heapPath);
2318 case FKind.FlatOpNode: {
2319 FlatOpNode fon = (FlatOpNode) fn;
2320 // for a normal assign node, need to propagate lhs's heap path to
2323 if (fon.getOp().getOp() == Operation.ASSIGN) {
2324 rhs = fon.getLeft();
2325 lhs = fon.getDest();
2327 NTuple<Descriptor> rhsHeapPath = mapHeapPath.get(rhs);
2329 // if (lhs.getType().isPrimitive()) {
2330 // NTuple<Descriptor> lhsHeapPath = new NTuple<Descriptor>();
2331 // lhsHeapPath.add(lhs);
2332 // mapHeapPath.put(lhs, lhsHeapPath);
2335 if (rhsHeapPath != null && (!lhs.getType().isPrimitive())) {
2336 mapHeapPath.put(lhs, mapHeapPath.get(rhs));
2339 // if (isEventLoopBody) {
2340 // NTuple<Descriptor> lhsHeapPath = new NTuple<Descriptor>();
2341 // lhsHeapPath.add(rhs);
2342 // mapHeapPath.put(lhs, lhsHeapPath);
2348 // shared loc extension
2349 if (isEventLoopBody) {
2350 if (!lhs.getSymbol().startsWith("neverused") && rhs.getType().isImmutable()) {
2352 if (rhs.getType().getExtension() instanceof Location
2353 && lhs.getType().getExtension() instanceof CompositeLocation) {
2355 Location rhsLoc = (Location) rhs.getType().getExtension();
2357 CompositeLocation lhsCompLoc = (CompositeLocation) lhs.getType().getExtension();
2358 Location dstLoc = lhsCompLoc.get(lhsCompLoc.getSize() - 1);
2360 NTuple<Descriptor> heapPath = new NTuple<Descriptor>();
2361 for (int i = 0; i < rhsHeapPath.size() - 1; i++) {
2362 heapPath.add(rhsHeapPath.get(i));
2365 NTuple<Descriptor> writeHeapPath = new NTuple<Descriptor>();
2366 writeHeapPath.addAll(heapPath);
2367 writeHeapPath.add(lhs);
2377 case FKind.FlatElementNode:
2378 case FKind.FlatFieldNode: {
2382 if (fn.kind() == FKind.FlatFieldNode) {
2383 FlatFieldNode ffn = (FlatFieldNode) fn;
2386 fld = ffn.getField();
2388 FlatElementNode fen = (FlatElementNode) fn;
2391 TypeDescriptor td = rhs.getType().dereference();
2392 fld = getArrayField(td);
2395 if (fld.isFinal()) {
2396 // if field is final no need to check
2401 NTuple<Descriptor> srcHeapPath = mapHeapPath.get(rhs);
2402 if (srcHeapPath != null) {
2403 // if lhs srcHeapPath is null, it means that it is not reachable from
2404 // callee's parameters. so just ignore it
2406 NTuple<Descriptor> readingHeapPath = new NTuple<Descriptor>(srcHeapPath.getList());
2407 if (fn.kind() == FKind.FlatFieldNode) {
2408 readingHeapPath.add(fld);
2411 mapHeapPath.put(lhs, readingHeapPath);
2414 if (fld.getType().isImmutable()) {
2415 // if WT doesnot have hp(x.f), add hp(x.f) to READ
2416 if (!currMustWriteSet.contains(readingHeapPath)) {
2417 readSet.add(readingHeapPath);
2421 // no need to kill hp(x.f) from WT
2427 case FKind.FlatSetFieldNode:
2428 case FKind.FlatSetElementNode: {
2432 if (fn.kind() == FKind.FlatSetFieldNode) {
2433 FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
2434 lhs = fsfn.getDst();
2435 fld = fsfn.getField();
2436 rhs = fsfn.getSrc();
2438 FlatSetElementNode fsen = (FlatSetElementNode) fn;
2439 lhs = fsen.getDst();
2440 rhs = fsen.getSrc();
2441 TypeDescriptor td = lhs.getType().dereference();
2442 fld = getArrayField(td);
2446 NTuple<Descriptor> lhsHeapPath = mapHeapPath.get(lhs);
2448 if (lhsHeapPath != null) {
2449 // if lhs heap path is null, it means that it is not reachable from
2450 // callee's parameters. so just ignore it
2451 NTuple<Descriptor> fldHeapPath = new NTuple<Descriptor>(lhsHeapPath.getList());
2452 if (fn.kind() != FKind.FlatSetElementNode) {
2453 fldHeapPath.add(fld);
2455 // mapHeapPath.put(fld, fldHeapPath);
2458 // need to add hp(y) to WT
2459 if (fn.kind() != FKind.FlatSetElementNode) {
2460 currMustWriteSet.add(fldHeapPath);
2462 mayWriteSet.add(fldHeapPath);
2469 case FKind.FlatCall: {
2471 FlatCall fc = (FlatCall) fn;
2473 bindHeapPathCallerArgWithCalleeParam(fc);
2475 Set<NTuple<Descriptor>> boundReadSet = new HashSet<NTuple<Descriptor>>();
2476 boundReadSet.addAll(calleeUnionBoundReadSet);
2478 Set<NTuple<Descriptor>> boundMustWriteSet = new HashSet<NTuple<Descriptor>>();
2479 boundMustWriteSet.addAll(calleeIntersectBoundMustWriteSet);
2481 Set<NTuple<Descriptor>> boundMayWriteSet = new HashSet<NTuple<Descriptor>>();
2482 boundMayWriteSet.addAll(calleeUnionBoundMayWriteSet);
2484 mapFlatNodeToBoundReadSet.put(fn, boundReadSet);
2485 mapFlatNodeToBoundMustWriteSet.put(fn, boundMustWriteSet);
2486 mapFlatNodeToBoundMayWriteSet.put(fn, boundMayWriteSet);
2488 // add heap path, which is an element of READ_bound set and is not
2490 // element of WT set, to the caller's READ set
2491 for (Iterator iterator = calleeUnionBoundReadSet.iterator(); iterator.hasNext();) {
2492 NTuple<Descriptor> read = (NTuple<Descriptor>) iterator.next();
2493 if (!currMustWriteSet.contains(read)) {
2498 // add heap path, which is an element of OVERWRITE_bound set, to the
2500 for (Iterator iterator = calleeIntersectBoundMustWriteSet.iterator(); iterator.hasNext();) {
2501 NTuple<Descriptor> write = (NTuple<Descriptor>) iterator.next();
2502 currMustWriteSet.add(write);
2505 // add heap path, which is an element of WRITE_BOUND set, to the
2506 // caller's writeSet
2507 for (Iterator iterator = calleeUnionBoundMayWriteSet.iterator(); iterator.hasNext();) {
2508 NTuple<Descriptor> write = (NTuple<Descriptor>) iterator.next();
2509 mayWriteSet.add(write);
2515 case FKind.FlatExit: {
2516 // merge the current written set with OVERWRITE set
2517 merge(mustWriteSet, currMustWriteSet);
2525 static public FieldDescriptor getArrayField(TypeDescriptor td) {
2526 FieldDescriptor fd = mapTypeToArrayField.get(td);
2529 new FieldDescriptor(new Modifiers(Modifiers.PUBLIC), td, arrayElementFieldName, null,
2531 mapTypeToArrayField.put(td, fd);
2536 private void merge(Set<NTuple<Descriptor>> curr, Set<NTuple<Descriptor>> in) {
2537 if (curr.isEmpty()) {
2538 // set has a special initial value which covers all possible
2540 // For the first time of intersection, we can take all previous set
2543 // otherwise, current set is the intersection of the two sets
2549 // combine two heap path
2550 private NTuple<Descriptor> combine(NTuple<Descriptor> callerIn, NTuple<Descriptor> calleeIn) {
2551 NTuple<Descriptor> combined = new NTuple<Descriptor>();
2553 for (int i = 0; i < callerIn.size(); i++) {
2554 combined.add(callerIn.get(i));
2557 // the first element of callee's heap path represents parameter
2558 // so we skip the first one since it is already added from caller's heap
2560 for (int i = 1; i < calleeIn.size(); i++) {
2561 combined.add(calleeIn.get(i));
2567 private Set<NTuple<Descriptor>> bindSet(Set<NTuple<Descriptor>> calleeSet,
2568 Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc,
2569 Hashtable<Integer, NTuple<Descriptor>> mapCallerArgIdx2HeapPath) {
2571 Set<NTuple<Descriptor>> boundedCalleeSet = new HashSet<NTuple<Descriptor>>();
2573 Set<Integer> keySet = mapCallerArgIdx2HeapPath.keySet();
2574 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2575 Integer idx = (Integer) iterator.next();
2577 NTuple<Descriptor> callerArgHeapPath = mapCallerArgIdx2HeapPath.get(idx);
2578 TempDescriptor calleeParam = mapParamIdx2ParamTempDesc.get(idx);
2579 for (Iterator iterator2 = calleeSet.iterator(); iterator2.hasNext();) {
2580 NTuple<Descriptor> element = (NTuple<Descriptor>) iterator2.next();
2581 if (element.startsWith(calleeParam)) {
2582 NTuple<Descriptor> boundElement = combine(callerArgHeapPath, element);
2583 boundedCalleeSet.add(boundElement);
2589 return boundedCalleeSet;
2593 private NTuple<Descriptor> computePath(Descriptor td) {
2594 // generate proper path fot input td
2595 // if td is local variable, it just generate one element tuple path
2596 if (mapHeapPath.containsKey(td)) {
2597 NTuple<Descriptor> rtrHeapPath = new NTuple<Descriptor>();
2598 rtrHeapPath.addAll(mapHeapPath.get(td));
2601 NTuple<Descriptor> rtrHeapPath = new NTuple<Descriptor>();
2602 rtrHeapPath.add(td);
2607 private NTuple<Location> deriveThisLocationTuple(MethodDescriptor md) {
2608 String thisLocIdentifier = ssjava.getMethodLattice(md).getThisLoc();
2609 Location thisLoc = new Location(md, thisLocIdentifier);
2610 NTuple<Location> locTuple = new NTuple<Location>();
2611 locTuple.add(thisLoc);
2615 private NTuple<Location> deriveGlobalLocationTuple(MethodDescriptor md) {
2616 String globalLocIdentifier = ssjava.getMethodLattice(md).getGlobalLoc();
2617 Location globalLoc = new Location(md, globalLocIdentifier);
2618 NTuple<Location> locTuple = new NTuple<Location>();
2619 locTuple.add(globalLoc);
2623 private NTuple<Location> deriveLocationTuple(MethodDescriptor md, TempDescriptor td) {
2624 assert td.getType() != null;
2626 if (mapDescriptorToLocationPath.containsKey(td)) {
2627 NTuple<Location> locPath = mapDescriptorToLocationPath.get(td);
2628 NTuple<Location> rtrPath = new NTuple<Location>();
2629 rtrPath.addAll(locPath);
2632 if (td.getSymbol().startsWith("this")) {
2633 NTuple<Location> thisPath = deriveThisLocationTuple(md);
2634 NTuple<Location> rtrPath = new NTuple<Location>();
2635 rtrPath.addAll(thisPath);
2639 if (td.getType().getExtension() != null) {
2640 SSJavaType ssJavaType = (SSJavaType) td.getType().getExtension();
2641 if (ssJavaType.getCompLoc() != null) {
2642 NTuple<Location> rtrPath = new NTuple<Location>();
2643 rtrPath.addAll(ssJavaType.getCompLoc().getTuple());