1 package Analysis.SSJava;
3 import java.io.BufferedWriter;
4 import java.io.FileWriter;
5 import java.io.IOException;
6 import java.util.Enumeration;
7 import java.util.HashSet;
8 import java.util.Hashtable;
9 import java.util.Iterator;
10 import java.util.LinkedList;
12 import java.util.Stack;
14 import Analysis.CallGraph.CallGraph;
15 import Analysis.Loops.LoopFinder;
17 import IR.FieldDescriptor;
18 import IR.MethodDescriptor;
21 import IR.TypeDescriptor;
22 import IR.TypeExtension;
24 import IR.Flat.FlatCall;
25 import IR.Flat.FlatElementNode;
26 import IR.Flat.FlatFieldNode;
27 import IR.Flat.FlatLiteralNode;
28 import IR.Flat.FlatMethod;
29 import IR.Flat.FlatNode;
30 import IR.Flat.FlatOpNode;
31 import IR.Flat.FlatSetElementNode;
32 import IR.Flat.FlatSetFieldNode;
33 import IR.Flat.TempDescriptor;
34 import IR.Tree.Modifiers;
37 public class DefinitelyWrittenCheck {
39 SSJavaAnalysis ssjava;
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;
50 // maps a flat node to its WrittenSet: this keeps all heap path overwritten
52 private Hashtable<FlatNode, Set<NTuple<Descriptor>>> mapFlatNodeToMustWriteSet;
54 // maps a temp descriptor to its heap path
55 // each temp descriptor has a unique heap path since we do not allow any
57 private Hashtable<Descriptor, NTuple<Descriptor>> mapHeapPath;
59 // maps a temp descriptor to its composite location
60 private Hashtable<TempDescriptor, NTuple<Location>> mapDescriptorToLocationPath;
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;
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;
70 // maps a flat method to the DELETE SET that is a set of heap path to shared
72 // written to but not overwritten by the higher value
73 private Hashtable<FlatMethod, SharedLocMap> mapFlatMethodToDeleteSet;
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;
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;
83 // maps a call site to the read set contributed by all callees
84 private Hashtable<FlatNode, Set<NTuple<Descriptor>>> mapFlatNodeToBoundReadSet;
86 // maps a call site to the must write set contributed by all callees
87 private Hashtable<FlatNode, Set<NTuple<Descriptor>>> mapFlatNodeToBoundMustWriteSet;
89 // maps a call site to the may read set contributed by all callees
90 private Hashtable<FlatNode, Set<NTuple<Descriptor>>> mapFlatNodeToBoundMayWriteSet;
92 // points to method containing SSJAVA Loop
93 private MethodDescriptor methodContainingSSJavaLoop;
95 // maps a flatnode to definitely written analysis mapping M
96 private Hashtable<FlatNode, Hashtable<NTuple<Descriptor>, Set<WriteAge>>> mapFlatNodetoEventLoopMap;
98 // maps shared location to the set of descriptors which belong to the shared
101 // keep current descriptors to visit in fixed-point interprocedural analysis,
102 private Stack<MethodDescriptor> methodDescriptorsToVisitStack;
104 // when analyzing flatcall, need to re-schedule set of callee
105 private Set<MethodDescriptor> calleesToEnqueue;
107 private Set<ReadSummary> possibleCalleeReadSummarySetToCaller;
109 public static final String arrayElementFieldName = "___element_";
110 static protected Hashtable<TypeDescriptor, FieldDescriptor> mapTypeToArrayField;
112 // maps a method descriptor to the merged incoming caller's current
114 // it is for setting clearance flag when all read set is overwritten
115 private Hashtable<MethodDescriptor, ReadSummary> mapMethodDescriptorToReadSummary;
117 private Hashtable<MethodDescriptor, MultiSourceMap<NTuple<Location>, NTuple<Descriptor>>> mapMethodToSharedLocCoverSet;
119 private Hashtable<FlatNode, SharedLocMap> mapFlatNodeToSharedLocMapping;
120 private Hashtable<FlatNode, SharedLocMap> mapFlatNodeToDeleteSet;
122 private Hashtable<Location, Set<Descriptor>> mapSharedLocationToCoverSet;
124 private LinkedList<MethodDescriptor> sortedDescriptors;
126 private FlatNode ssjavaLoopEntrance;
127 private LoopFinder ssjavaLoop;
128 private Set<FlatNode> loopIncElements;
130 private Set<NTuple<Descriptor>> calleeUnionBoundReadSet;
131 private Set<NTuple<Descriptor>> calleeIntersectBoundMustWriteSet;
132 private Set<NTuple<Descriptor>> calleeUnionBoundMayWriteSet;
133 private SharedLocMap calleeUnionBoundDeleteSet;
134 private SharedLocMap calleeIntersectBoundSharedSet;
136 private Hashtable<Descriptor, Location> mapDescToLocation;
138 private TempDescriptor LOCAL;
140 public static int MAXAGE = 1;
142 public DefinitelyWrittenCheck(SSJavaAnalysis ssjava, State state) {
144 this.ssjava = ssjava;
145 this.callGraph = ssjava.getCallGraph();
146 this.mapFlatNodeToMustWriteSet = new Hashtable<FlatNode, Set<NTuple<Descriptor>>>();
147 this.mapDescriptorToSetDependents = new Hashtable<Descriptor, Set<MethodDescriptor>>();
148 this.mapHeapPath = new Hashtable<Descriptor, NTuple<Descriptor>>();
149 this.mapDescriptorToLocationPath = new Hashtable<TempDescriptor, NTuple<Location>>();
150 this.mapFlatMethodToReadSet = new Hashtable<FlatMethod, Set<NTuple<Descriptor>>>();
151 this.mapFlatMethodToMustWriteSet = new Hashtable<FlatMethod, Set<NTuple<Descriptor>>>();
152 this.mapFlatMethodToMayWriteSet = new Hashtable<FlatMethod, Set<NTuple<Descriptor>>>();
153 this.mapFlatNodetoEventLoopMap =
154 new Hashtable<FlatNode, Hashtable<NTuple<Descriptor>, Set<WriteAge>>>();
155 this.calleeUnionBoundReadSet = new HashSet<NTuple<Descriptor>>();
156 this.calleeIntersectBoundMustWriteSet = new HashSet<NTuple<Descriptor>>();
157 this.calleeUnionBoundMayWriteSet = new HashSet<NTuple<Descriptor>>();
159 this.methodDescriptorsToVisitStack = new Stack<MethodDescriptor>();
160 this.calleesToEnqueue = new HashSet<MethodDescriptor>();
161 this.mapTypeToArrayField = new Hashtable<TypeDescriptor, FieldDescriptor>();
162 this.LOCAL = new TempDescriptor("LOCAL");
163 this.mapDescToLocation = new Hashtable<Descriptor, Location>();
164 this.possibleCalleeReadSummarySetToCaller = new HashSet<ReadSummary>();
165 this.mapMethodDescriptorToReadSummary = new Hashtable<MethodDescriptor, ReadSummary>();
166 this.mapFlatNodeToBoundReadSet = new Hashtable<FlatNode, Set<NTuple<Descriptor>>>();
167 this.mapFlatNodeToBoundMustWriteSet = new Hashtable<FlatNode, Set<NTuple<Descriptor>>>();
168 this.mapFlatNodeToBoundMayWriteSet = new Hashtable<FlatNode, Set<NTuple<Descriptor>>>();
169 this.mapSharedLocationToCoverSet = new Hashtable<Location, Set<Descriptor>>();
170 this.mapFlatNodeToSharedLocMapping = new Hashtable<FlatNode, SharedLocMap>();
171 this.mapFlatMethodToDeleteSet = new Hashtable<FlatMethod, SharedLocMap>();
172 this.calleeUnionBoundDeleteSet = new SharedLocMap();
173 this.calleeIntersectBoundSharedSet = new SharedLocMap();
174 this.mapFlatMethodToSharedLocMap = new Hashtable<FlatMethod, SharedLocMap>();
175 this.mapMethodToSharedLocCoverSet =
176 new Hashtable<MethodDescriptor, MultiSourceMap<NTuple<Location>, NTuple<Descriptor>>>();
177 this.mapFlatNodeToDeleteSet = new Hashtable<FlatNode, SharedLocMap>();
180 public void definitelyWrittenCheck() {
181 if (!ssjava.getAnnotationRequireSet().isEmpty()) {
184 methodReadWriteSetAnalysis();
185 computeSharedCoverSet();
194 private void sharedLocAnalysis() {
196 // perform method READ/OVERWRITE analysis
197 LinkedList<MethodDescriptor> descriptorListToAnalyze =
198 (LinkedList<MethodDescriptor>) sortedDescriptors.clone();
200 // current descriptors to visit in fixed-point interprocedural analysis,
202 // dependency in the call graph
203 methodDescriptorsToVisitStack.clear();
205 descriptorListToAnalyze.removeFirst();
207 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
208 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
210 while (!descriptorListToAnalyze.isEmpty()) {
211 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
212 methodDescriptorsToVisitStack.add(md);
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);
221 SharedLocMap sharedLocMap = new SharedLocMap();
222 SharedLocMap deleteSet = new SharedLocMap();
224 sharedLoc_analyzeMethod(fm, sharedLocMap, deleteSet);
225 SharedLocMap prevSharedLocMap = mapFlatMethodToSharedLocMap.get(fm);
226 SharedLocMap prevDeleteSet = mapFlatMethodToDeleteSet.get(fm);
228 if (!(deleteSet.equals(prevDeleteSet) && sharedLocMap.equals(prevSharedLocMap))) {
229 mapFlatMethodToSharedLocMap.put(fm, sharedLocMap);
230 mapFlatMethodToDeleteSet.put(fm, deleteSet);
232 // results for callee changed, so enqueue dependents caller for
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);
249 sharedLoc_analyzeEventLoop();
253 private void sharedLoc_analyzeEventLoop() {
254 if (state.SSJAVADEBUG) {
255 System.out.println("SSJAVA: Definite clearance for shared locations Analyzing: eventloop");
257 SharedLocMap sharedLocMap = new SharedLocMap();
258 SharedLocMap deleteSet = new SharedLocMap();
259 sharedLoc_analyzeBody(state.getMethodFlat(methodContainingSSJavaLoop), ssjavaLoopEntrance,
260 sharedLocMap, deleteSet, true);
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);
270 sharedLoc_analyzeBody(fm, fm, sharedLocMap, deleteSet, false);
274 private void sharedLoc_analyzeBody(FlatMethod fm, FlatNode startNode, SharedLocMap sharedLocMap,
275 SharedLocMap deleteSet, boolean isEventLoopBody) {
277 // intraprocedural analysis
278 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
279 flatNodesToVisit.add(startNode);
281 while (!flatNodesToVisit.isEmpty()) {
282 FlatNode fn = flatNodesToVisit.iterator().next();
283 flatNodesToVisit.remove(fn);
285 SharedLocMap currSharedSet = new SharedLocMap();
286 SharedLocMap currDeleteSet = new SharedLocMap();
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);
295 SharedLocMap inDeleteLoc = mapFlatNodeToDeleteSet.get(prevFn);
296 if (inDeleteLoc != null) {
297 mergeDeleteSet(currDeleteSet, inDeleteLoc);
301 sharedLoc_nodeActions(fm, fn, currSharedSet, currDeleteSet, sharedLocMap, deleteSet,
304 SharedLocMap prevSharedSet = mapFlatNodeToSharedLocMapping.get(fn);
305 SharedLocMap prevDeleteSet = mapFlatNodeToDeleteSet.get(fn);
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);
323 private void sharedLoc_nodeActions(FlatMethod fm, FlatNode fn, SharedLocMap curr,
324 SharedLocMap currDeleteSet, SharedLocMap sharedLocMap, SharedLocMap deleteSet,
325 boolean isEventLoopBody) {
327 SharedLocMap killSet = new SharedLocMap();
328 SharedLocMap genSet = new SharedLocMap();
336 case FKind.FlatOpNode: {
338 if (isEventLoopBody) {
339 FlatOpNode fon = (FlatOpNode) fn;
341 if (fon.getOp().getOp() == Operation.ASSIGN) {
345 if (!lhs.getSymbol().startsWith("neverused") && rhs.getType().isImmutable()) {
347 Location dstLoc = getLocation(lhs);
348 if (dstLoc != null && ssjava.isSharedLocation(dstLoc)) {
349 NTuple<Descriptor> lhsHeapPath = computePath(lhs);
350 NTuple<Location> lhsLocTuple = mapDescriptorToLocationPath.get(lhs);
352 Location srcLoc = getLocation(lhs);
354 // computing gen/kill set
355 computeKILLSetForWrite(curr, killSet, lhsLocTuple, lhsHeapPath);
356 if (!dstLoc.equals(srcLoc)) {
357 computeGENSetForHigherWrite(curr, killSet, lhsLocTuple, lhsHeapPath);
358 updateDeleteSetForHigherWrite(currDeleteSet, lhsLocTuple, lhsHeapPath);
360 computeGENSetForSameHeightWrite(curr, killSet, lhsLocTuple, lhsHeapPath);
361 updateDeleteSetForSameHeightWrite(currDeleteSet, lhsLocTuple, lhsHeapPath);
364 // System.out.println("VAR WRITE:" + fn);
365 // System.out.println("lhsLocTuple=" + lhsLocTuple +
368 // System.out.println("dstLoc=" + dstLoc + " srcLoc=" + srcLoc);
369 // System.out.println("KILLSET=" + killSet);
370 // System.out.println("GENSet=" + genSet);
371 // System.out.println("DELETESET=" + currDeleteSet);
383 case FKind.FlatSetFieldNode:
384 case FKind.FlatSetElementNode: {
386 if (fn.kind() == FKind.FlatSetFieldNode) {
387 FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
389 fld = fsfn.getField();
392 FlatSetElementNode fsen = (FlatSetElementNode) fn;
395 TypeDescriptor td = lhs.getType().dereference();
396 fld = getArrayField(td);
399 // shared loc extension
400 Location srcLoc = getLocation(rhs);
401 Location fieldLoc = (Location) fld.getType().getExtension();
402 if (ssjava.isSharedLocation(fieldLoc)) {
403 // only care the case that loc(f) is shared location
406 NTuple<Location> fieldLocTuple = new NTuple<Location>();
407 fieldLocTuple.addAll(mapDescriptorToLocationPath.get(lhs));
408 fieldLocTuple.add(fieldLoc);
410 NTuple<Descriptor> fldHeapPath = computePath(fld);
412 // computing gen/kill set
413 computeKILLSetForWrite(curr, killSet, fieldLocTuple, fldHeapPath);
414 if (!fieldLoc.equals(srcLoc)) {
415 computeGENSetForHigherWrite(curr, genSet, fieldLocTuple, fldHeapPath);
416 updateDeleteSetForHigherWrite(currDeleteSet, fieldLocTuple, fldHeapPath);
418 computeGENSetForSameHeightWrite(curr, genSet, fieldLocTuple, fldHeapPath);
419 updateDeleteSetForSameHeightWrite(currDeleteSet, fieldLocTuple, fldHeapPath);
422 // System.out.println("################");
423 // System.out.println("FIELD WRITE:" + fn);
424 // System.out.println("FldHeapPath=" + fldHeapPath);
425 // System.out.println("fieldLocTuple=" + fieldLocTuple + " srcLoc=" +
427 // System.out.println("KILLSET=" + killSet);
428 // System.out.println("GENSet=" + genSet);
429 // System.out.println("DELETESET=" + currDeleteSet);
435 case FKind.FlatCall: {
436 FlatCall fc = (FlatCall) fn;
438 bindHeapPathCallerArgWithCaleeParamForSharedLoc(fm.getMethod(), fc);
440 // computing gen/kill set
441 generateKILLSetForFlatCall(curr, killSet);
442 generateGENSetForFlatCall(curr, genSet);
444 // System.out.println("#FLATCALL=" + fc);
445 // System.out.println("KILLSET=" + killSet);
446 // System.out.println("GENSet=" + genSet);
447 // System.out.println("bound DELETE Set=" + calleeUnionBoundDeleteSet);
452 case FKind.FlatExit: {
453 // merge the current delete/shared loc mapping
454 mergeSharedLocMap(sharedLocMap, curr);
455 mergeDeleteSet(deleteSet, currDeleteSet);
457 // System.out.println("#FLATEXIT sharedLocMap=" + sharedLocMap);
463 computeNewMapping(curr, killSet, genSet);
464 // System.out.println("#######" + curr);
468 private void generateGENSetForFlatCall(SharedLocMap curr, SharedLocMap genSet) {
470 Set<NTuple<Location>> locTupleSet = calleeIntersectBoundSharedSet.keySet();
471 for (Iterator iterator = locTupleSet.iterator(); iterator.hasNext();) {
472 NTuple<Location> locTupleKey = (NTuple<Location>) iterator.next();
473 genSet.addWrite(locTupleKey, curr.get(locTupleKey));
474 genSet.addWrite(locTupleKey, calleeIntersectBoundSharedSet.get(locTupleKey));
476 genSet.removeWriteAll(locTupleKey, calleeUnionBoundDeleteSet.get(locTupleKey));
481 private void generateKILLSetForFlatCall(SharedLocMap curr, SharedLocMap killSet) {
483 Set<NTuple<Location>> locTupleSet = calleeIntersectBoundSharedSet.keySet();
484 for (Iterator iterator = locTupleSet.iterator(); iterator.hasNext();) {
485 NTuple<Location> locTupleKey = (NTuple<Location>) iterator.next();
486 killSet.addWrite(locTupleKey, curr.get(locTupleKey));
491 private void mergeDeleteSet(SharedLocMap currDeleteSet, SharedLocMap inDeleteLoc) {
493 Set<NTuple<Location>> locTupleKeySet = inDeleteLoc.keySet();
495 for (Iterator iterator = locTupleKeySet.iterator(); iterator.hasNext();) {
496 NTuple<Location> locTupleKey = (NTuple<Location>) iterator.next();
498 Set<NTuple<Descriptor>> inSet = inDeleteLoc.get(locTupleKey);
499 currDeleteSet.addWrite(locTupleKey, inSet);
504 private void computeNewMapping(SharedLocMap curr, SharedLocMap killSet, SharedLocMap genSet) {
509 private void updateDeleteSetForHigherWrite(SharedLocMap currDeleteSet, NTuple<Location> locTuple,
510 NTuple<Descriptor> hp) {
511 currDeleteSet.removeWrite(locTuple, hp);
514 private void updateDeleteSetForSameHeightWrite(SharedLocMap currDeleteSet,
515 NTuple<Location> locTuple, NTuple<Descriptor> hp) {
516 currDeleteSet.addWrite(locTuple, hp);
519 private void computeGENSetForHigherWrite(SharedLocMap curr, SharedLocMap genSet,
520 NTuple<Location> locTuple, NTuple<Descriptor> hp) {
521 Set<NTuple<Descriptor>> currWriteSet = curr.get(locTuple);
523 if (currWriteSet != null) {
524 genSet.addWrite(locTuple, currWriteSet);
527 genSet.addWrite(locTuple, hp);
530 private void computeGENSetForSameHeightWrite(SharedLocMap curr, SharedLocMap genSet,
531 NTuple<Location> locTuple, NTuple<Descriptor> hp) {
532 Set<NTuple<Descriptor>> currWriteSet = curr.get(locTuple);
534 if (currWriteSet != null) {
535 genSet.addWrite(locTuple, currWriteSet);
537 genSet.removeWrite(locTuple, hp);
540 private void computeKILLSetForWrite(SharedLocMap curr, SharedLocMap killSet,
541 NTuple<Location> locTuple, NTuple<Descriptor> hp) {
543 Set<NTuple<Descriptor>> writeSet = curr.get(locTuple);
544 if (writeSet != null) {
545 killSet.addWrite(locTuple, writeSet);
550 private void mergeSharedLocMap(SharedLocMap currSharedSet, SharedLocMap in) {
552 Set<NTuple<Location>> locTupleKeySet = in.keySet();
553 for (Iterator iterator = locTupleKeySet.iterator(); iterator.hasNext();) {
554 NTuple<Location> locTupleKey = (NTuple<Location>) iterator.next();
556 Set<NTuple<Descriptor>> inSet = in.get(locTupleKey);
557 Set<NTuple<Descriptor>> currSet = currSharedSet.get(locTupleKey);
558 if (currSet == null) {
559 currSet = new HashSet<NTuple<Descriptor>>();
560 currSet.addAll(inSet);
561 currSharedSet.addWrite(locTupleKey, currSet);
563 currSet.retainAll(inSet);
568 private void computeSharedCoverSet() {
569 LinkedList<MethodDescriptor> descriptorListToAnalyze =
570 (LinkedList<MethodDescriptor>) sortedDescriptors.clone();
572 // current descriptors to visit in fixed-point interprocedural analysis,
574 // dependency in the call graph
575 methodDescriptorsToVisitStack.clear();
577 descriptorListToAnalyze.removeFirst();
579 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
580 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
582 while (!descriptorListToAnalyze.isEmpty()) {
583 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
584 methodDescriptorsToVisitStack.add(md);
587 // analyze scheduled methods until there are no more to visit
588 while (!methodDescriptorsToVisitStack.isEmpty()) {
589 MethodDescriptor md = methodDescriptorsToVisitStack.pop();
590 FlatMethod fm = state.getMethodFlat(md);
591 computeSharedCoverSet_analyzeMethod(fm, md.equals(methodContainingSSJavaLoop));
594 computeSharedCoverSetForEventLoop();
598 private void computeSharedCoverSetForEventLoop() {
599 computeSharedCoverSet_analyzeMethod(state.getMethodFlat(methodContainingSSJavaLoop), true);
602 private void computeSharedCoverSet_analyzeMethod(FlatMethod fm, boolean onlyVisitSSJavaLoop) {
604 MethodDescriptor md = fm.getMethod();
605 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
607 Set<FlatNode> visited = new HashSet<FlatNode>();
609 if (onlyVisitSSJavaLoop) {
610 flatNodesToVisit.add(ssjavaLoopEntrance);
612 flatNodesToVisit.add(fm);
615 while (!flatNodesToVisit.isEmpty()) {
616 FlatNode fn = flatNodesToVisit.iterator().next();
617 flatNodesToVisit.remove(fn);
620 computeSharedCoverSet_nodeActions(md, fn);
622 for (int i = 0; i < fn.numNext(); i++) {
623 FlatNode nn = fn.getNext(i);
625 if (!visited.contains(nn)) {
626 if (!onlyVisitSSJavaLoop || (onlyVisitSSJavaLoop && loopIncElements.contains(nn))) {
627 flatNodesToVisit.add(nn);
637 private void computeSharedCoverSet_nodeActions(MethodDescriptor md, FlatNode fn) {
644 case FKind.FlatLiteralNode: {
645 FlatLiteralNode fln = (FlatLiteralNode) fn;
648 if (lhs.getType().isPrimitive() && !lhs.getSymbol().startsWith("neverused")
649 && !lhs.getSymbol().startsWith("srctmp")) {
650 // only need to care about composite location case here
651 if (lhs.getType().getExtension() instanceof SSJavaType) {
652 CompositeLocation compLoc = ((SSJavaType) lhs.getType().getExtension()).getCompLoc();
653 Location lastLocElement = compLoc.get(compLoc.getSize() - 1);
654 // check if the last one is shared loc
655 if (ssjava.isSharedLocation(lastLocElement)) {
656 addSharedLocDescriptor(lastLocElement, lhs);
664 case FKind.FlatOpNode: {
665 FlatOpNode fon = (FlatOpNode) fn;
666 // for a normal assign node, need to propagate lhs's location path to
668 if (fon.getOp().getOp() == Operation.ASSIGN) {
672 if (lhs.getType().isPrimitive() && !lhs.getSymbol().startsWith("neverused")
673 && !lhs.getSymbol().startsWith("srctmp") && !lhs.getSymbol().startsWith("leftop")
674 && !lhs.getSymbol().startsWith("rightop")) {
676 NTuple<Location> lhsLocTuple = new NTuple<Location>();
677 lhsLocTuple.addAll(deriveLocationTuple(md, rhs));
679 NTuple<Descriptor> lhsHeapPath = computePath(lhs);
681 addMayWrittenSet(md, lhsLocTuple, lhsHeapPath);
685 if (mapDescriptorToLocationPath.containsKey(rhs)) {
686 mapDescriptorToLocationPath.put(lhs, mapDescriptorToLocationPath.get(rhs));
688 if (rhs.getType().getExtension() instanceof SSJavaType) {
689 NTuple<Location> rhsLocTuple =
690 ((SSJavaType) rhs.getType().getExtension()).getCompLoc().getTuple();
692 NTuple<Location> lhsLocTuple = new NTuple<Location>();
693 lhsLocTuple.addAll(rhsLocTuple);
695 mapDescriptorToLocationPath.put(rhs, rhsLocTuple);
696 mapDescriptorToLocationPath.put(lhs, lhsLocTuple);
705 case FKind.FlatSetFieldNode:
706 case FKind.FlatSetElementNode: {
710 if (fn.kind() == FKind.FlatSetFieldNode) {
711 FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
713 fld = fsfn.getField();
716 FlatSetElementNode fsen = (FlatSetElementNode) fn;
719 TypeDescriptor td = lhs.getType().dereference();
720 fld = getArrayField(td);
723 Location fieldLocation = (Location) fld.getType().getExtension();
724 if (ssjava.isSharedLocation(fieldLocation)) {
725 addSharedLocDescriptor(fieldLocation, fld);
727 NTuple<Location> locTuple = new NTuple<Location>();
728 locTuple.addAll(deriveLocationTuple(md, lhs));
729 locTuple.add(fieldLocation);
731 NTuple<Descriptor> fieldHeapPath = new NTuple<Descriptor>();
732 fieldHeapPath.addAll(computePath(lhs));
733 fieldHeapPath.add(fld);
735 // mapLocationPathToMayWrittenSet.put(locTuple, null, fld);
736 addMayWrittenSet(md, locTuple, fieldHeapPath);
743 case FKind.FlatElementNode:
744 case FKind.FlatFieldNode: {
748 if (fn.kind() == FKind.FlatFieldNode) {
749 FlatFieldNode ffn = (FlatFieldNode) fn;
752 fld = ffn.getField();
754 FlatElementNode fen = (FlatElementNode) fn;
757 TypeDescriptor td = rhs.getType().dereference();
758 fld = getArrayField(td);
762 // if field is final no need to check
766 NTuple<Location> locTuple = new NTuple<Location>();
767 locTuple.addAll(deriveLocationTuple(md, rhs));
768 locTuple.add((Location) fld.getType().getExtension());
770 mapDescriptorToLocationPath.put(lhs, locTuple);
775 case FKind.FlatCall: {
777 FlatCall fc = (FlatCall) fn;
778 bindLocationPathCallerArgWithCalleeParam(md, fc);
786 private void addMayWrittenSet(MethodDescriptor md, NTuple<Location> locTuple,
787 NTuple<Descriptor> heapPath) {
789 MultiSourceMap<NTuple<Location>, NTuple<Descriptor>> map = mapMethodToSharedLocCoverSet.get(md);
791 map = new MultiSourceMap<NTuple<Location>, NTuple<Descriptor>>();
792 mapMethodToSharedLocCoverSet.put(md, map);
795 Set<NTuple<Descriptor>> writeSet = map.get(locTuple);
796 if (writeSet == null) {
797 writeSet = new HashSet<NTuple<Descriptor>>();
798 map.put(locTuple, writeSet);
800 writeSet.add(heapPath);
804 private void bindLocationPathCallerArgWithCalleeParam(MethodDescriptor mdCaller, FlatCall fc) {
806 if (ssjava.isSSJavaUtil(fc.getMethod().getClassDesc())) {
808 // have write effects on the first argument
809 TempDescriptor arg = fc.getArg(0);
810 NTuple<Location> argLocationPath = deriveLocationTuple(mdCaller, arg);
811 NTuple<Descriptor> argHeapPath = computePath(arg);
812 addMayWrittenSet(mdCaller, argLocationPath, argHeapPath);
815 // if arg is not primitive type, we need to propagate maywritten set to
816 // the caller's location path
818 MethodDescriptor mdCallee = fc.getMethod();
819 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
820 setPossibleCallees.addAll(callGraph.getMethods(mdCallee));
822 // create mapping from arg idx to its heap paths
823 Hashtable<Integer, NTuple<Descriptor>> mapArgIdx2CallerArgHeapPath =
824 new Hashtable<Integer, NTuple<Descriptor>>();
826 // create mapping from arg idx to its location paths
827 Hashtable<Integer, NTuple<Location>> mapArgIdx2CallerArgLocationPath =
828 new Hashtable<Integer, NTuple<Location>>();
830 // arg idx is starting from 'this' arg
831 if (fc.getThis() != null) {
832 // loc path for 'this'
833 NTuple<Location> thisLocationPath = deriveLocationTuple(mdCaller, fc.getThis());
834 mapArgIdx2CallerArgLocationPath.put(Integer.valueOf(0), thisLocationPath);
836 // heap path for 'this'
837 NTuple<Descriptor> thisHeapPath = mapHeapPath.get(fc.getThis());
838 if (thisHeapPath == null) {
839 // method is called without creating new flat node representing 'this'
840 thisHeapPath = new NTuple<Descriptor>();
841 thisHeapPath.add(fc.getThis());
843 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(0), thisHeapPath);
846 for (int i = 0; i < fc.numArgs(); i++) {
847 TempDescriptor arg = fc.getArg(i);
848 // create mapping arg to loc path
849 NTuple<Location> argLocationPath = deriveLocationTuple(mdCaller, arg);
850 mapArgIdx2CallerArgLocationPath.put(Integer.valueOf(i + 1), argLocationPath);
852 // create mapping arg to heap path
853 NTuple<Descriptor> argHeapPath = computePath(arg);
854 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(i + 1), argHeapPath);
857 Hashtable<Integer, Set<NTuple<Descriptor>>> mapParamIdx2WriteSet =
858 new Hashtable<Integer, Set<NTuple<Descriptor>>>();
860 for (int i = 0; i < fc.numArgs() + 1; i++) {
861 mapParamIdx2WriteSet.put(Integer.valueOf(i), new HashSet<NTuple<Descriptor>>());
864 for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
865 MethodDescriptor callee = (MethodDescriptor) iterator.next();
866 FlatMethod calleeFlatMethod = state.getMethodFlat(callee);
868 // binding caller's args and callee's params
870 Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc =
871 new Hashtable<Integer, TempDescriptor>();
873 if (calleeFlatMethod.getMethod().isStatic()) {
874 // static method does not have implicit 'this' arg
877 for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
878 TempDescriptor param = calleeFlatMethod.getParameter(i);
879 mapParamIdx2ParamTempDesc.put(Integer.valueOf(i + offset), param);
882 Set<Integer> keySet = mapArgIdx2CallerArgLocationPath.keySet();
883 for (Iterator iterator2 = keySet.iterator(); iterator2.hasNext();) {
884 Integer idx = (Integer) iterator2.next();
885 NTuple<Location> callerArgLocationPath = mapArgIdx2CallerArgLocationPath.get(idx);
887 TempDescriptor calleeParam = mapParamIdx2ParamTempDesc.get(idx);
889 NTuple<Descriptor> callerArgHeapPath = mapArgIdx2CallerArgHeapPath.get(idx);
890 NTuple<Location> calleeLocationPath = deriveLocationTuple(mdCallee, calleeParam);
891 NTuple<Descriptor> calleeHeapPath = computePath(calleeParam);
893 createNewMappingOfMayWrittenSet(mdCaller, callee, callerArgHeapPath,
894 callerArgLocationPath, calleeHeapPath, calleeLocationPath,
895 mapParamIdx2WriteSet.get(idx));
905 private Hashtable<NTuple<Location>, Set<NTuple<Descriptor>>> getMappingByStartedWith(
906 MultiSourceMap<NTuple<Location>, NTuple<Descriptor>> map, NTuple<Location> in) {
908 Hashtable<NTuple<Location>, Set<NTuple<Descriptor>>> matchedMapping =
909 new Hashtable<NTuple<Location>, Set<NTuple<Descriptor>>>();
911 Set<NTuple<Location>> keySet = map.keySet();
913 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
914 NTuple<Location> key = (NTuple<Location>) iterator.next();
915 if (key.startsWith(in)) {
916 matchedMapping.put(key, map.get(key));
920 return matchedMapping;
924 private void createNewMappingOfMayWrittenSet(MethodDescriptor caller, MethodDescriptor callee,
925 NTuple<Descriptor> callerArgHeapPath, NTuple<Location> callerArgLocPath,
926 NTuple<Descriptor> calleeParamHeapPath, NTuple<Location> calleeParamLocPath,
927 Set<NTuple<Descriptor>> writeSet) {
929 // propagate may-written-set associated with the key that is started with
930 // calleepath to the caller
931 // 1) makes a new key by combining caller path and callee path(except local
932 // loc element of param)
933 // 2) create new mapping of may-written-set of callee path to caller path
935 // extract all may written effect accessed through callee param path
936 MultiSourceMap<NTuple<Location>, NTuple<Descriptor>> calleeMapping =
937 mapMethodToSharedLocCoverSet.get(callee);
939 MultiSourceMap<NTuple<Location>, NTuple<Descriptor>> callerMapping =
940 mapMethodToSharedLocCoverSet.get(caller);
942 if (calleeMapping == null) {
946 Hashtable<NTuple<Location>, Set<NTuple<Descriptor>>> paramMapping =
947 getMappingByStartedWith(calleeMapping, calleeParamLocPath);
949 Set<NTuple<Location>> calleeKeySet = calleeMapping.keySet();
950 for (Iterator iterator = calleeKeySet.iterator(); iterator.hasNext();) {
951 NTuple<Location> calleeKey = (NTuple<Location>) iterator.next();
952 Set<NTuple<Descriptor>> calleeMayWriteSet = paramMapping.get(calleeKey);
954 if (calleeMayWriteSet != null) {
956 Set<NTuple<Descriptor>> boundWriteSet =
957 convertCallerMayWriteSet(callerArgHeapPath, calleeParamHeapPath, calleeMayWriteSet);
959 writeSet.addAll(boundWriteSet);
961 NTuple<Location> newKey = new NTuple<Location>();
962 newKey.addAll(callerArgLocPath);
963 // need to replace the local location with the caller's path so skip the
964 // local location of the parameter
965 for (int i = 1; i < calleeKey.size(); i++) {
966 newKey.add(calleeKey.get(i));
969 callerMapping.union(newKey, writeSet);
970 // mapLocationPathToMayWrittenSet.put(calleeKey, newKey, writeSet);
977 private Set<NTuple<Descriptor>> convertCallerMayWriteSet(NTuple<Descriptor> callerArgHeapPath,
978 NTuple<Descriptor> calleeParamHeapPath, Set<NTuple<Descriptor>> calleeMayWriteSet) {
980 Set<NTuple<Descriptor>> boundSet = new HashSet<NTuple<Descriptor>>();
982 // replace callee's param path with caller's arg path
983 for (Iterator iterator = calleeMayWriteSet.iterator(); iterator.hasNext();) {
984 NTuple<Descriptor> calleeWriteHeapPath = (NTuple<Descriptor>) iterator.next();
986 NTuple<Descriptor> boundHeapPath = new NTuple<Descriptor>();
987 boundHeapPath.addAll(callerArgHeapPath);
989 int startIdx = calleeParamHeapPath.size();
991 for (int i = startIdx; i < calleeWriteHeapPath.size(); i++) {
992 boundHeapPath.add(calleeWriteHeapPath.get(i));
995 boundSet.add(boundHeapPath);
1002 private void addSharedLocDescriptor(Location sharedLoc, Descriptor desc) {
1004 Set<Descriptor> descSet = mapSharedLocationToCoverSet.get(sharedLoc);
1005 if (descSet == null) {
1006 descSet = new HashSet<Descriptor>();
1007 mapSharedLocationToCoverSet.put(sharedLoc, descSet);
1014 private Location getLocation(Descriptor d) {
1016 if (d instanceof FieldDescriptor) {
1017 TypeExtension te = ((FieldDescriptor) d).getType().getExtension();
1019 return (Location) te;
1022 assert d instanceof TempDescriptor;
1023 TempDescriptor td = (TempDescriptor) d;
1025 TypeExtension te = td.getType().getExtension();
1027 if (te instanceof SSJavaType) {
1028 SSJavaType ssType = (SSJavaType) te;
1029 CompositeLocation comp = ssType.getCompLoc();
1030 return comp.get(comp.getSize() - 1);
1032 return (Location) te;
1037 return mapDescToLocation.get(d);
1040 private void eventLoopAnalysis() {
1041 // perform second stage analysis: intraprocedural analysis ensure that
1043 // variables are definitely written in-between the same read
1045 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
1046 flatNodesToVisit.add(ssjavaLoopEntrance);
1048 while (!flatNodesToVisit.isEmpty()) {
1049 FlatNode fn = (FlatNode) flatNodesToVisit.iterator().next();
1050 flatNodesToVisit.remove(fn);
1052 Hashtable<NTuple<Descriptor>, Set<WriteAge>> prev = mapFlatNodetoEventLoopMap.get(fn);
1054 Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr =
1055 new Hashtable<NTuple<Descriptor>, Set<WriteAge>>();
1056 for (int i = 0; i < fn.numPrev(); i++) {
1057 FlatNode nn = fn.getPrev(i);
1058 Hashtable<NTuple<Descriptor>, Set<WriteAge>> in = mapFlatNodetoEventLoopMap.get(nn);
1064 eventLoopAnalysis_nodeAction(fn, curr, ssjavaLoopEntrance);
1066 // if a new result, schedule forward nodes for analysis
1067 if (!curr.equals(prev)) {
1068 mapFlatNodetoEventLoopMap.put(fn, curr);
1070 for (int i = 0; i < fn.numNext(); i++) {
1071 FlatNode nn = fn.getNext(i);
1072 if (loopIncElements.contains(nn)) {
1073 flatNodesToVisit.add(nn);
1081 private void union(Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr,
1082 Hashtable<NTuple<Descriptor>, Set<WriteAge>> in) {
1084 Set<NTuple<Descriptor>> inKeySet = in.keySet();
1085 for (Iterator iterator = inKeySet.iterator(); iterator.hasNext();) {
1086 NTuple<Descriptor> inKey = (NTuple<Descriptor>) iterator.next();
1087 Set<WriteAge> inSet = in.get(inKey);
1089 Set<WriteAge> currSet = curr.get(inKey);
1091 if (currSet == null) {
1092 currSet = new HashSet<WriteAge>();
1093 curr.put(inKey, currSet);
1095 currSet.addAll(inSet);
1100 private void eventLoopAnalysis_nodeAction(FlatNode fn,
1101 Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr, FlatNode loopEntrance) {
1103 Hashtable<NTuple<Descriptor>, Set<WriteAge>> readWriteKillSet =
1104 new Hashtable<NTuple<Descriptor>, Set<WriteAge>>();
1105 Hashtable<NTuple<Descriptor>, Set<WriteAge>> readWriteGenSet =
1106 new Hashtable<NTuple<Descriptor>, Set<WriteAge>>();
1108 if (fn.equals(loopEntrance)) {
1109 // it reaches loop entrance: changes all flag to true
1110 Set<NTuple<Descriptor>> keySet = curr.keySet();
1111 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1112 NTuple<Descriptor> key = (NTuple<Descriptor>) iterator.next();
1113 Set<WriteAge> writeAgeSet = curr.get(key);
1115 Set<WriteAge> incSet = new HashSet<WriteAge>();
1116 incSet.addAll(writeAgeSet);
1117 writeAgeSet.clear();
1119 for (Iterator iterator2 = incSet.iterator(); iterator2.hasNext();) {
1120 WriteAge writeAge = (WriteAge) iterator2.next();
1121 WriteAge newWriteAge = writeAge.copy();
1123 writeAgeSet.add(newWriteAge);
1127 // System.out.println("EVENT LOOP ENTRY=" + curr);
1132 FieldDescriptor fld;
1134 switch (fn.kind()) {
1136 case FKind.FlatOpNode: {
1137 FlatOpNode fon = (FlatOpNode) fn;
1138 lhs = fon.getDest();
1139 rhs = fon.getLeft();
1141 if (fon.getOp().getOp() == Operation.ASSIGN) {
1143 if (!lhs.getSymbol().startsWith("neverused")) {
1144 NTuple<Descriptor> rhsHeapPath = computePath(rhs);
1145 if (!rhs.getType().isImmutable()) {
1146 mapHeapPath.put(lhs, rhsHeapPath);
1149 // NTuple<Descriptor> lhsHeapPath = computePath(lhs);
1150 NTuple<Descriptor> path = new NTuple<Descriptor>();
1153 // System.out.println("#VARIABLE WRITE:" + fn);
1155 Location lhsLoc = getLocation(lhs);
1156 if (ssjava.isSharedLocation(lhsLoc)) {
1158 NTuple<Descriptor> varHeapPath = computePath(lhs);
1159 NTuple<Location> varLocTuple = mapDescriptorToLocationPath.get(lhs);
1161 Set<NTuple<Descriptor>> writtenSet =
1162 mapFlatNodeToSharedLocMapping.get(fn).get(varLocTuple);
1164 if (isCovered(varLocTuple, writtenSet)) {
1165 computeKILLSetForSharedWrite(curr, writtenSet, readWriteKillSet);
1166 computeGENSetForSharedAllCoverWrite(curr, writtenSet, readWriteGenSet);
1168 computeGENSetForSharedNonCoverWrite(curr, varHeapPath, readWriteGenSet);
1172 computeKILLSetForWrite(curr, path, readWriteKillSet);
1173 computeGENSetForWrite(path, readWriteGenSet);
1176 // System.out.println("#KILLSET=" + readWriteKillSet);
1177 // System.out.println("#GENSet=" + readWriteGenSet);
1188 case FKind.FlatFieldNode:
1189 case FKind.FlatElementNode: {
1191 if (fn.kind() == FKind.FlatFieldNode) {
1192 FlatFieldNode ffn = (FlatFieldNode) fn;
1195 fld = ffn.getField();
1197 FlatElementNode fen = (FlatElementNode) fn;
1200 TypeDescriptor td = rhs.getType().dereference();
1201 fld = getArrayField(td);
1205 NTuple<Descriptor> srcHeapPath = mapHeapPath.get(rhs);
1206 NTuple<Descriptor> fldHeapPath;
1207 if (srcHeapPath != null) {
1208 fldHeapPath = new NTuple<Descriptor>(srcHeapPath.getList());
1210 // if srcHeapPath is null, it is static reference
1211 fldHeapPath = new NTuple<Descriptor>();
1212 fldHeapPath.add(rhs);
1214 fldHeapPath.add(fld);
1216 Set<WriteAge> writeAgeSet = curr.get(fldHeapPath);
1217 checkWriteAgeSet(writeAgeSet, fldHeapPath, fn);
1222 case FKind.FlatSetFieldNode:
1223 case FKind.FlatSetElementNode: {
1225 if (fn.kind() == FKind.FlatSetFieldNode) {
1226 FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
1227 lhs = fsfn.getDst();
1228 fld = fsfn.getField();
1230 FlatSetElementNode fsen = (FlatSetElementNode) fn;
1231 lhs = fsen.getDst();
1232 rhs = fsen.getSrc();
1233 TypeDescriptor td = lhs.getType().dereference();
1234 fld = getArrayField(td);
1237 // System.out.println("FIELD WRITE:" + fn);
1240 NTuple<Descriptor> lhsHeapPath = computePath(lhs);
1241 NTuple<Descriptor> fldHeapPath = new NTuple<Descriptor>(lhsHeapPath.getList());
1242 fldHeapPath.add(fld);
1244 // shared loc extension
1245 Location fieldLoc = (Location) fld.getType().getExtension();
1246 if (ssjava.isSharedLocation(fieldLoc)) {
1248 NTuple<Location> fieldLocTuple = new NTuple<Location>();
1249 fieldLocTuple.addAll(mapDescriptorToLocationPath.get(lhs));
1250 fieldLocTuple.add(fieldLoc);
1252 Set<NTuple<Descriptor>> writtenSet =
1253 mapFlatNodeToSharedLocMapping.get(fn).get(fieldLocTuple);
1255 if (isCovered(fieldLocTuple, writtenSet)) {
1256 computeKILLSetForSharedWrite(curr, writtenSet, readWriteKillSet);
1257 computeGENSetForSharedAllCoverWrite(curr, writtenSet, readWriteGenSet);
1259 computeGENSetForSharedNonCoverWrite(curr, fldHeapPath, readWriteGenSet);
1263 computeKILLSetForWrite(curr, fldHeapPath, readWriteKillSet);
1264 computeGENSetForWrite(fldHeapPath, readWriteGenSet);
1267 // System.out.println("KILLSET=" + readWriteKillSet);
1268 // System.out.println("GENSet=" + readWriteGenSet);
1273 case FKind.FlatCall: {
1274 FlatCall fc = (FlatCall) fn;
1276 // System.out.println("FLATCALL:" + fn);
1278 SharedLocMap sharedLocMap = mapFlatNodeToSharedLocMapping.get(fc);
1279 generateKILLSetForFlatCall(fc, curr, sharedLocMap, readWriteKillSet);
1280 generateGENSetForFlatCall(fc, sharedLocMap, readWriteGenSet);
1282 // System.out.println("KILLSET=" + readWriteKillSet);
1283 // System.out.println("GENSet=" + readWriteGenSet);
1285 checkManyRead(fc, curr);
1291 computeNewMapping(curr, readWriteKillSet, readWriteGenSet);
1292 // System.out.println("#######" + curr);
1298 private void computeGENSetForSharedNonCoverWrite(
1299 Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr, NTuple<Descriptor> heapPath,
1300 Hashtable<NTuple<Descriptor>, Set<WriteAge>> genSet) {
1302 Set<WriteAge> writeAgeSet = genSet.get(heapPath);
1303 if (writeAgeSet == null) {
1304 writeAgeSet = new HashSet<WriteAge>();
1305 genSet.put(heapPath, writeAgeSet);
1308 writeAgeSet.add(new WriteAge(1));
1312 private void computeGENSetForSharedAllCoverWrite(
1313 Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr, Set<NTuple<Descriptor>> writtenSet,
1314 Hashtable<NTuple<Descriptor>, Set<WriteAge>> genSet) {
1316 for (Iterator iterator = writtenSet.iterator(); iterator.hasNext();) {
1317 NTuple<Descriptor> writeHeapPath = (NTuple<Descriptor>) iterator.next();
1319 Set<WriteAge> writeAgeSet = new HashSet<WriteAge>();
1320 writeAgeSet.add(new WriteAge(0));
1322 genSet.put(writeHeapPath, writeAgeSet);
1327 private void computeKILLSetForSharedWrite(Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr,
1328 Set<NTuple<Descriptor>> writtenSet, Hashtable<NTuple<Descriptor>, Set<WriteAge>> killSet) {
1330 for (Iterator iterator = writtenSet.iterator(); iterator.hasNext();) {
1331 NTuple<Descriptor> writeHeapPath = (NTuple<Descriptor>) iterator.next();
1332 Set<WriteAge> writeSet = curr.get(writeHeapPath);
1333 if (writeSet != null) {
1334 killSet.put(writeHeapPath, writeSet);
1340 private boolean isCovered(NTuple<Location> locTuple, Set<NTuple<Descriptor>> inSet) {
1342 if (inSet == null) {
1346 Set<NTuple<Descriptor>> coverSet =
1347 mapMethodToSharedLocCoverSet.get(methodContainingSSJavaLoop).get(locTuple);
1349 return inSet.containsAll(coverSet);
1352 private void checkManyRead(FlatCall fc, Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr) {
1354 Set<NTuple<Descriptor>> boundReadSet = mapFlatNodeToBoundReadSet.get(fc);
1356 for (Iterator iterator = boundReadSet.iterator(); iterator.hasNext();) {
1357 NTuple<Descriptor> readHeapPath = (NTuple<Descriptor>) iterator.next();
1358 Set<WriteAge> writeAgeSet = curr.get(readHeapPath);
1359 checkWriteAgeSet(writeAgeSet, readHeapPath, fc);
1364 private void checkWriteAgeSet(Set<WriteAge> writeAgeSet, NTuple<Descriptor> path, FlatNode fn) {
1365 if (writeAgeSet != null) {
1366 for (Iterator iterator = writeAgeSet.iterator(); iterator.hasNext();) {
1367 WriteAge writeAge = (WriteAge) iterator.next();
1368 if (writeAge.getAge() >= MAXAGE) {
1370 "Memory location, which is reachable through references "
1372 + ", who comes back to the same read statement without being overwritten at the out-most iteration at "
1373 + methodContainingSSJavaLoop.getClassDesc().getSourceFileName() + "::"
1380 private void generateGENSetForFlatCall(FlatCall fc, SharedLocMap sharedLocMap,
1381 Hashtable<NTuple<Descriptor>, Set<WriteAge>> GENSet) {
1383 Set<NTuple<Descriptor>> boundMayWriteSet = mapFlatNodeToBoundMayWriteSet.get(fc);
1385 for (Iterator iterator = boundMayWriteSet.iterator(); iterator.hasNext();) {
1386 NTuple<Descriptor> heapPath = (NTuple<Descriptor>) iterator.next();
1388 if (!isSharedLocation(heapPath)) {
1389 addWriteAgeToSet(heapPath, GENSet, new WriteAge(0));
1391 // if the current heap path is shared location
1393 NTuple<Location> locTuple = getLocationTuple(heapPath, sharedLocMap);
1395 Set<NTuple<Descriptor>> sharedWriteHeapPathSet = sharedLocMap.get(locTuple);
1397 if (isCovered(locTuple, sharedLocMap.get(locTuple))) {
1398 // if it is covered, add all of heap paths belong to the same shared
1399 // loc with write age 0
1401 for (Iterator iterator2 = sharedWriteHeapPathSet.iterator(); iterator2.hasNext();) {
1402 NTuple<Descriptor> sharedHeapPath = (NTuple<Descriptor>) iterator2.next();
1403 addWriteAgeToSet(sharedHeapPath, GENSet, new WriteAge(0));
1407 // if not covered, add write age 1 to the heap path that is
1408 // may-written but not covered
1409 addWriteAgeToSet(heapPath, GENSet, new WriteAge(1));
1418 private void addWriteAgeToSet(NTuple<Descriptor> heapPath,
1419 Hashtable<NTuple<Descriptor>, Set<WriteAge>> map, WriteAge age) {
1421 Set<WriteAge> currSet = map.get(heapPath);
1422 if (currSet == null) {
1423 currSet = new HashSet<WriteAge>();
1424 map.put(heapPath, currSet);
1430 private void generateKILLSetForFlatCall(FlatCall fc,
1431 Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr, SharedLocMap sharedLocMap,
1432 Hashtable<NTuple<Descriptor>, Set<WriteAge>> KILLSet) {
1434 Set<NTuple<Descriptor>> boundMustWriteSet = mapFlatNodeToBoundMustWriteSet.get(fc);
1436 for (Iterator iterator = boundMustWriteSet.iterator(); iterator.hasNext();) {
1437 NTuple<Descriptor> heapPath = (NTuple<Descriptor>) iterator.next();
1439 if (isSharedLocation(heapPath)) {
1440 NTuple<Location> locTuple = getLocationTuple(heapPath, sharedLocMap);
1442 if (isCovered(locTuple, sharedLocMap.get(locTuple))) {
1443 // if it is shared loc and corresponding shared loc has been covered
1444 KILLSet.put(heapPath, curr.get(heapPath));
1447 if (curr.get(heapPath) != null) {
1448 KILLSet.put(heapPath, curr.get(heapPath));
1456 private boolean isSharedLocation(NTuple<Descriptor> heapPath) {
1457 return ssjava.isSharedLocation(getLocation(heapPath.get(heapPath.size() - 1)));
1460 private NTuple<Location> getLocationTuple(NTuple<Descriptor> heapPath, SharedLocMap sharedLocMap) {
1462 NTuple<Location> locTuple = new NTuple<Location>();
1464 locTuple.addAll(mapDescriptorToLocationPath.get(heapPath.get(0)));
1465 for (int i = 1; i < heapPath.size(); i++) {
1466 locTuple.add(getLocation(heapPath.get(i)));
1472 private void computeNewMapping(Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr,
1473 Hashtable<NTuple<Descriptor>, Set<WriteAge>> KILLSet,
1474 Hashtable<NTuple<Descriptor>, Set<WriteAge>> GENSet) {
1476 for (Enumeration<NTuple<Descriptor>> e = KILLSet.keys(); e.hasMoreElements();) {
1477 NTuple<Descriptor> key = e.nextElement();
1479 Set<WriteAge> writeAgeSet = curr.get(key);
1480 if (writeAgeSet == null) {
1481 writeAgeSet = new HashSet<WriteAge>();
1482 curr.put(key, writeAgeSet);
1484 writeAgeSet.removeAll(KILLSet.get(key));
1487 for (Enumeration<NTuple<Descriptor>> e = GENSet.keys(); e.hasMoreElements();) {
1488 NTuple<Descriptor> key = e.nextElement();
1490 Set<WriteAge> currWriteAgeSet = curr.get(key);
1491 if (currWriteAgeSet == null) {
1492 currWriteAgeSet = new HashSet<WriteAge>();
1493 curr.put(key, currWriteAgeSet);
1495 currWriteAgeSet.addAll(GENSet.get(key));
1500 private void computeGENSetForWrite(NTuple<Descriptor> fldHeapPath,
1501 Hashtable<NTuple<Descriptor>, Set<WriteAge>> GENSet) {
1503 // generate write age 0 for the field being written to
1504 Set<WriteAge> writeAgeSet = new HashSet<WriteAge>();
1505 writeAgeSet.add(new WriteAge(0));
1506 GENSet.put(fldHeapPath, writeAgeSet);
1510 private void computeKILLSetForWrite(Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr,
1511 NTuple<Descriptor> hp, Hashtable<NTuple<Descriptor>, Set<WriteAge>> KILLSet) {
1513 // removes all of heap path that starts with prefix 'hp'
1514 // since any reference overwrite along heap path gives overwriting side
1515 // effects on the value
1517 Set<NTuple<Descriptor>> keySet = curr.keySet();
1518 for (Iterator<NTuple<Descriptor>> iter = keySet.iterator(); iter.hasNext();) {
1519 NTuple<Descriptor> key = iter.next();
1520 if (key.startsWith(hp)) {
1521 KILLSet.put(key, curr.get(key));
1527 private void bindHeapPathCallerArgWithCalleeParam(FlatCall fc) {
1528 // compute all possible callee set
1529 // transform all READ/WRITE set from the any possible
1530 // callees to the caller
1531 calleeUnionBoundReadSet.clear();
1532 calleeIntersectBoundMustWriteSet.clear();
1533 calleeUnionBoundMayWriteSet.clear();
1535 if (ssjava.isSSJavaUtil(fc.getMethod().getClassDesc())) {
1536 // ssjava util case!
1537 // have write effects on the first argument
1538 TempDescriptor arg = fc.getArg(0);
1539 NTuple<Descriptor> argHeapPath = computePath(arg);
1540 calleeIntersectBoundMustWriteSet.add(argHeapPath);
1541 calleeUnionBoundMayWriteSet.add(argHeapPath);
1543 MethodDescriptor mdCallee = fc.getMethod();
1544 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
1545 setPossibleCallees.addAll(callGraph.getMethods(mdCallee));
1547 // create mapping from arg idx to its heap paths
1548 Hashtable<Integer, NTuple<Descriptor>> mapArgIdx2CallerArgHeapPath =
1549 new Hashtable<Integer, NTuple<Descriptor>>();
1551 // arg idx is starting from 'this' arg
1552 if (fc.getThis() != null) {
1553 NTuple<Descriptor> thisHeapPath = mapHeapPath.get(fc.getThis());
1554 if (thisHeapPath == null) {
1555 // method is called without creating new flat node representing 'this'
1556 thisHeapPath = new NTuple<Descriptor>();
1557 thisHeapPath.add(fc.getThis());
1560 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(0), thisHeapPath);
1563 for (int i = 0; i < fc.numArgs(); i++) {
1564 TempDescriptor arg = fc.getArg(i);
1565 NTuple<Descriptor> argHeapPath = computePath(arg);
1566 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(i + 1), argHeapPath);
1569 for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
1570 MethodDescriptor callee = (MethodDescriptor) iterator.next();
1571 FlatMethod calleeFlatMethod = state.getMethodFlat(callee);
1573 // binding caller's args and callee's params
1575 Set<NTuple<Descriptor>> calleeReadSet = mapFlatMethodToReadSet.get(calleeFlatMethod);
1576 if (calleeReadSet == null) {
1577 calleeReadSet = new HashSet<NTuple<Descriptor>>();
1578 mapFlatMethodToReadSet.put(calleeFlatMethod, calleeReadSet);
1581 Set<NTuple<Descriptor>> calleeMustWriteSet =
1582 mapFlatMethodToMustWriteSet.get(calleeFlatMethod);
1584 if (calleeMustWriteSet == null) {
1585 calleeMustWriteSet = new HashSet<NTuple<Descriptor>>();
1586 mapFlatMethodToMustWriteSet.put(calleeFlatMethod, calleeMustWriteSet);
1589 Set<NTuple<Descriptor>> calleeMayWriteSet =
1590 mapFlatMethodToMayWriteSet.get(calleeFlatMethod);
1592 if (calleeMayWriteSet == null) {
1593 calleeMayWriteSet = new HashSet<NTuple<Descriptor>>();
1594 mapFlatMethodToMayWriteSet.put(calleeFlatMethod, calleeMayWriteSet);
1597 Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc =
1598 new Hashtable<Integer, TempDescriptor>();
1600 if (calleeFlatMethod.getMethod().isStatic()) {
1601 // static method does not have implicit 'this' arg
1604 for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
1605 TempDescriptor param = calleeFlatMethod.getParameter(i);
1606 mapParamIdx2ParamTempDesc.put(Integer.valueOf(i + offset), param);
1609 Set<NTuple<Descriptor>> calleeBoundReadSet =
1610 bindSet(calleeReadSet, mapParamIdx2ParamTempDesc, mapArgIdx2CallerArgHeapPath);
1611 // union of the current read set and the current callee's
1613 calleeUnionBoundReadSet.addAll(calleeBoundReadSet);
1615 Set<NTuple<Descriptor>> calleeBoundMustWriteSet =
1616 bindSet(calleeMustWriteSet, mapParamIdx2ParamTempDesc, mapArgIdx2CallerArgHeapPath);
1617 // intersection of the current overwrite set and the current
1620 merge(calleeIntersectBoundMustWriteSet, calleeBoundMustWriteSet);
1622 Set<NTuple<Descriptor>> boundWriteSetFromCallee =
1623 bindSet(calleeMayWriteSet, mapParamIdx2ParamTempDesc, mapArgIdx2CallerArgHeapPath);
1624 calleeUnionBoundMayWriteSet.addAll(boundWriteSetFromCallee);
1631 private void bindHeapPathCallerArgWithCaleeParamForSharedLoc(MethodDescriptor mdCaller,
1634 calleeIntersectBoundSharedSet.clear();
1635 calleeUnionBoundDeleteSet.clear();
1637 // if arg is not primitive type, we need to propagate maywritten set to
1638 // the caller's location path
1640 MethodDescriptor mdCallee = fc.getMethod();
1641 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
1642 setPossibleCallees.addAll(callGraph.getMethods(mdCallee));
1644 // create mapping from arg idx to its heap paths
1645 Hashtable<Integer, NTuple<Descriptor>> mapArgIdx2CallerArgHeapPath =
1646 new Hashtable<Integer, NTuple<Descriptor>>();
1648 // arg idx is starting from 'this' arg
1649 if (fc.getThis() != null) {
1650 NTuple<Descriptor> thisHeapPath = mapHeapPath.get(fc.getThis());
1651 if (thisHeapPath == null) {
1652 // method is called without creating new flat node representing 'this'
1653 thisHeapPath = new NTuple<Descriptor>();
1654 thisHeapPath.add(fc.getThis());
1657 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(0), thisHeapPath);
1660 for (int i = 0; i < fc.numArgs(); i++) {
1661 TempDescriptor arg = fc.getArg(i);
1662 NTuple<Descriptor> argHeapPath = computePath(arg);
1663 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(i + 1), argHeapPath);
1666 // create mapping from arg idx to its location paths
1667 Hashtable<Integer, NTuple<Location>> mapArgIdx2CallerAgLocationPath =
1668 new Hashtable<Integer, NTuple<Location>>();
1670 // arg idx is starting from 'this' arg
1671 if (fc.getThis() != null) {
1672 NTuple<Location> thisLocationPath = deriveLocationTuple(mdCaller, fc.getThis());
1673 mapArgIdx2CallerAgLocationPath.put(Integer.valueOf(0), thisLocationPath);
1676 for (int i = 0; i < fc.numArgs(); i++) {
1677 TempDescriptor arg = fc.getArg(i);
1678 NTuple<Location> argLocationPath = deriveLocationTuple(mdCaller, arg);
1679 mapArgIdx2CallerAgLocationPath.put(Integer.valueOf(i + 1), argLocationPath);
1682 for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
1683 MethodDescriptor callee = (MethodDescriptor) iterator.next();
1684 FlatMethod calleeFlatMethod = state.getMethodFlat(callee);
1686 // binding caller's args and callee's params
1688 Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc =
1689 new Hashtable<Integer, TempDescriptor>();
1691 if (calleeFlatMethod.getMethod().isStatic()) {
1692 // static method does not have implicit 'this' arg
1695 for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
1696 TempDescriptor param = calleeFlatMethod.getParameter(i);
1697 mapParamIdx2ParamTempDesc.put(Integer.valueOf(i + offset), param);
1700 Set<Integer> keySet = mapArgIdx2CallerAgLocationPath.keySet();
1701 for (Iterator iterator2 = keySet.iterator(); iterator2.hasNext();) {
1702 Integer idx = (Integer) iterator2.next();
1703 NTuple<Location> callerArgLocationPath = mapArgIdx2CallerAgLocationPath.get(idx);
1704 NTuple<Descriptor> callerArgHeapPath = mapArgIdx2CallerArgHeapPath.get(idx);
1706 TempDescriptor calleeParam = mapParamIdx2ParamTempDesc.get(idx);
1707 NTuple<Location> calleeLocationPath = deriveLocationTuple(mdCallee, calleeParam);
1708 SharedLocMap calleeDeleteSet = mapFlatMethodToDeleteSet.get(calleeFlatMethod);
1709 SharedLocMap calleeSharedLocMap = mapFlatMethodToSharedLocMap.get(calleeFlatMethod);
1711 if (calleeDeleteSet != null) {
1712 createNewMappingOfDeleteSet(callerArgLocationPath, callerArgHeapPath, calleeLocationPath,
1716 if (calleeSharedLocMap != null) {
1717 createNewMappingOfSharedSet(callerArgLocationPath, callerArgHeapPath, calleeLocationPath,
1718 calleeSharedLocMap);
1727 private void createNewMappingOfDeleteSet(NTuple<Location> callerArgLocationPath,
1728 NTuple<Descriptor> callerArgHeapPath, NTuple<Location> calleeLocationPath,
1729 SharedLocMap calleeDeleteSet) {
1731 SharedLocMap calleeParamDeleteSet = calleeDeleteSet.getHeapPathStartedWith(calleeLocationPath);
1733 Set<NTuple<Location>> keySet = calleeParamDeleteSet.keySet();
1734 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1735 NTuple<Location> calleeLocTupleKey = (NTuple<Location>) iterator.next();
1736 Set<NTuple<Descriptor>> heapPathSet = calleeParamDeleteSet.get(calleeLocTupleKey);
1737 for (Iterator iterator2 = heapPathSet.iterator(); iterator2.hasNext();) {
1738 NTuple<Descriptor> calleeHeapPath = (NTuple<Descriptor>) iterator2.next();
1739 calleeUnionBoundDeleteSet.addWrite(
1740 bindLocationPath(callerArgLocationPath, calleeLocTupleKey),
1741 bindHeapPath(callerArgHeapPath, calleeHeapPath));
1747 private void createNewMappingOfSharedSet(NTuple<Location> callerArgLocationPath,
1748 NTuple<Descriptor> callerArgHeapPath, NTuple<Location> calleeLocationPath,
1749 SharedLocMap calleeSharedLocMap) {
1751 SharedLocMap calleeParamSharedSet =
1752 calleeSharedLocMap.getHeapPathStartedWith(calleeLocationPath);
1754 Set<NTuple<Location>> keySet = calleeParamSharedSet.keySet();
1755 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1756 NTuple<Location> calleeLocTupleKey = (NTuple<Location>) iterator.next();
1757 Set<NTuple<Descriptor>> heapPathSet = calleeParamSharedSet.get(calleeLocTupleKey);
1758 Set<NTuple<Descriptor>> boundHeapPathSet = new HashSet<NTuple<Descriptor>>();
1759 for (Iterator iterator2 = heapPathSet.iterator(); iterator2.hasNext();) {
1760 NTuple<Descriptor> calleeHeapPath = (NTuple<Descriptor>) iterator2.next();
1761 boundHeapPathSet.add(bindHeapPath(callerArgHeapPath, calleeHeapPath));
1763 calleeIntersectBoundSharedSet.intersect(
1764 bindLocationPath(callerArgLocationPath, calleeLocTupleKey), boundHeapPathSet);
1769 private NTuple<Location> bindLocationPath(NTuple<Location> start, NTuple<Location> end) {
1770 NTuple<Location> locPath = new NTuple<Location>();
1771 locPath.addAll(start);
1772 for (int i = 1; i < end.size(); i++) {
1773 locPath.add(end.get(i));
1778 private NTuple<Descriptor> bindHeapPath(NTuple<Descriptor> start, NTuple<Descriptor> end) {
1779 NTuple<Descriptor> heapPath = new NTuple<Descriptor>();
1780 heapPath.addAll(start);
1781 for (int i = 1; i < end.size(); i++) {
1782 heapPath.add(end.get(i));
1787 private void initialize() {
1788 // First, identify ssjava loop entrace
1790 // no need to analyze method having ssjava loop
1791 methodContainingSSJavaLoop = ssjava.getMethodContainingSSJavaLoop();
1793 FlatMethod fm = state.getMethodFlat(methodContainingSSJavaLoop);
1794 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
1795 flatNodesToVisit.add(fm);
1797 LoopFinder loopFinder = new LoopFinder(fm);
1799 while (!flatNodesToVisit.isEmpty()) {
1800 FlatNode fn = flatNodesToVisit.iterator().next();
1801 flatNodesToVisit.remove(fn);
1803 String label = (String) state.fn2labelMap.get(fn);
1804 if (label != null) {
1806 if (label.equals(ssjava.SSJAVA)) {
1807 ssjavaLoopEntrance = fn;
1812 for (int i = 0; i < fn.numNext(); i++) {
1813 FlatNode nn = fn.getNext(i);
1814 flatNodesToVisit.add(nn);
1818 assert ssjavaLoopEntrance != null;
1820 // assume that ssjava loop is top-level loop in method, not nested loop
1821 Set nestedLoop = loopFinder.nestedLoops();
1822 for (Iterator loopIter = nestedLoop.iterator(); loopIter.hasNext();) {
1823 LoopFinder lf = (LoopFinder) loopIter.next();
1824 if (lf.loopEntrances().iterator().next().equals(ssjavaLoopEntrance)) {
1829 assert ssjavaLoop != null;
1831 loopIncElements = (Set<FlatNode>) ssjavaLoop.loopIncElements();
1833 // perform topological sort over the set of methods accessed by the main
1835 Set<MethodDescriptor> methodDescriptorsToAnalyze = new HashSet<MethodDescriptor>();
1836 methodDescriptorsToAnalyze.addAll(ssjava.getAnnotationRequireSet());
1837 sortedDescriptors = topologicalSort(methodDescriptorsToAnalyze);
1840 private void methodReadWriteSetAnalysis() {
1841 // perform method READ/OVERWRITE analysis
1842 LinkedList<MethodDescriptor> descriptorListToAnalyze =
1843 (LinkedList<MethodDescriptor>) sortedDescriptors.clone();
1845 // current descriptors to visit in fixed-point interprocedural analysis,
1847 // dependency in the call graph
1848 methodDescriptorsToVisitStack.clear();
1850 descriptorListToAnalyze.removeFirst();
1852 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
1853 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
1855 while (!descriptorListToAnalyze.isEmpty()) {
1856 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
1857 methodDescriptorsToVisitStack.add(md);
1860 // analyze scheduled methods until there are no more to visit
1861 while (!methodDescriptorsToVisitStack.isEmpty()) {
1862 // start to analyze leaf node
1863 MethodDescriptor md = methodDescriptorsToVisitStack.pop();
1864 FlatMethod fm = state.getMethodFlat(md);
1866 Set<NTuple<Descriptor>> readSet = new HashSet<NTuple<Descriptor>>();
1867 Set<NTuple<Descriptor>> mustWriteSet = new HashSet<NTuple<Descriptor>>();
1868 Set<NTuple<Descriptor>> mayWriteSet = new HashSet<NTuple<Descriptor>>();
1870 methodReadWriteSet_analyzeMethod(fm, readSet, mustWriteSet, mayWriteSet);
1872 Set<NTuple<Descriptor>> prevRead = mapFlatMethodToReadSet.get(fm);
1873 Set<NTuple<Descriptor>> prevMustWrite = mapFlatMethodToMustWriteSet.get(fm);
1874 Set<NTuple<Descriptor>> prevMayWrite = mapFlatMethodToMayWriteSet.get(fm);
1876 if (!(readSet.equals(prevRead) && mustWriteSet.equals(prevMustWrite) && mayWriteSet
1877 .equals(prevMayWrite))) {
1878 mapFlatMethodToReadSet.put(fm, readSet);
1879 mapFlatMethodToMustWriteSet.put(fm, mustWriteSet);
1880 mapFlatMethodToMayWriteSet.put(fm, mayWriteSet);
1882 // results for callee changed, so enqueue dependents caller for
1885 Iterator<MethodDescriptor> depsItr = getDependents(md).iterator();
1886 while (depsItr.hasNext()) {
1887 MethodDescriptor methodNext = depsItr.next();
1888 if (!methodDescriptorsToVisitStack.contains(methodNext)
1889 && methodDescriptorToVistSet.contains(methodNext)) {
1890 methodDescriptorsToVisitStack.add(methodNext);
1899 methodReadWriteSetAnalysisToEventLoopBody();
1903 private void methodReadWriteSet_analyzeMethod(FlatMethod fm, Set<NTuple<Descriptor>> readSet,
1904 Set<NTuple<Descriptor>> mustWriteSet, Set<NTuple<Descriptor>> mayWriteSet) {
1905 if (state.SSJAVADEBUG) {
1906 System.out.println("SSJAVA: Definitely written Analyzing: " + fm);
1909 methodReadWriteSet_analyzeBody(fm, readSet, mustWriteSet, mayWriteSet, false);
1913 private void methodReadWriteSetAnalysisToEventLoopBody() {
1915 // perform method read/write analysis for Event Loop Body
1917 FlatMethod flatMethodContainingSSJavaLoop = state.getMethodFlat(methodContainingSSJavaLoop);
1919 if (state.SSJAVADEBUG) {
1920 System.out.println("SSJAVA: Definitely written Event Loop Analyzing: "
1921 + flatMethodContainingSSJavaLoop);
1924 Set<NTuple<Descriptor>> readSet = new HashSet<NTuple<Descriptor>>();
1925 Set<NTuple<Descriptor>> mustWriteSet = new HashSet<NTuple<Descriptor>>();
1926 Set<NTuple<Descriptor>> mayWriteSet = new HashSet<NTuple<Descriptor>>();
1928 mapFlatMethodToReadSet.put(flatMethodContainingSSJavaLoop, readSet);
1929 mapFlatMethodToMustWriteSet.put(flatMethodContainingSSJavaLoop, mustWriteSet);
1930 mapFlatMethodToMayWriteSet.put(flatMethodContainingSSJavaLoop, mayWriteSet);
1932 methodReadWriteSet_analyzeBody(ssjavaLoopEntrance, readSet, mustWriteSet, mayWriteSet, true);
1936 private void methodReadWriteSet_analyzeBody(FlatNode startNode, Set<NTuple<Descriptor>> readSet,
1937 Set<NTuple<Descriptor>> mustWriteSet, Set<NTuple<Descriptor>> mayWriteSet,
1938 boolean isEventLoopBody) {
1940 // intraprocedural analysis
1941 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
1942 flatNodesToVisit.add(startNode);
1944 while (!flatNodesToVisit.isEmpty()) {
1945 FlatNode fn = flatNodesToVisit.iterator().next();
1946 flatNodesToVisit.remove(fn);
1948 Set<NTuple<Descriptor>> currMustWriteSet = new HashSet<NTuple<Descriptor>>();
1950 for (int i = 0; i < fn.numPrev(); i++) {
1951 FlatNode prevFn = fn.getPrev(i);
1952 Set<NTuple<Descriptor>> in = mapFlatNodeToMustWriteSet.get(prevFn);
1954 merge(currMustWriteSet, in);
1958 methodReadWriteSet_nodeActions(fn, currMustWriteSet, readSet, mustWriteSet, mayWriteSet,
1961 Set<NTuple<Descriptor>> mustSetPrev = mapFlatNodeToMustWriteSet.get(fn);
1963 if (!currMustWriteSet.equals(mustSetPrev)) {
1964 mapFlatNodeToMustWriteSet.put(fn, currMustWriteSet);
1965 for (int i = 0; i < fn.numNext(); i++) {
1966 FlatNode nn = fn.getNext(i);
1967 if ((!isEventLoopBody) || loopIncElements.contains(nn)) {
1968 flatNodesToVisit.add(nn);
1978 private void methodReadWriteSet_nodeActions(FlatNode fn,
1979 Set<NTuple<Descriptor>> currMustWriteSet, Set<NTuple<Descriptor>> readSet,
1980 Set<NTuple<Descriptor>> mustWriteSet, Set<NTuple<Descriptor>> mayWriteSet,
1981 boolean isEventLoopBody) {
1985 FieldDescriptor fld;
1987 switch (fn.kind()) {
1988 case FKind.FlatMethod: {
1990 // set up initial heap paths for method parameters
1991 FlatMethod fm = (FlatMethod) fn;
1992 for (int i = 0; i < fm.numParameters(); i++) {
1993 TempDescriptor param = fm.getParameter(i);
1994 NTuple<Descriptor> heapPath = new NTuple<Descriptor>();
1995 heapPath.add(param);
1996 mapHeapPath.put(param, heapPath);
2001 case FKind.FlatOpNode: {
2002 FlatOpNode fon = (FlatOpNode) fn;
2003 // for a normal assign node, need to propagate lhs's heap path to
2006 if (fon.getOp().getOp() == Operation.ASSIGN) {
2007 rhs = fon.getLeft();
2008 lhs = fon.getDest();
2010 NTuple<Descriptor> rhsHeapPath = mapHeapPath.get(rhs);
2012 if (lhs.getType().isPrimitive()) {
2013 NTuple<Descriptor> lhsHeapPath = new NTuple<Descriptor>();
2014 lhsHeapPath.add(lhs);
2015 mapHeapPath.put(lhs, lhsHeapPath);
2016 } else if (rhsHeapPath != null) {
2017 mapHeapPath.put(lhs, mapHeapPath.get(rhs));
2019 NTuple<Descriptor> heapPath = new NTuple<Descriptor>();
2021 mapHeapPath.put(lhs, heapPath);
2024 // shared loc extension
2025 if (isEventLoopBody) {
2026 if (!lhs.getSymbol().startsWith("neverused") && rhs.getType().isImmutable()) {
2028 if (rhs.getType().getExtension() instanceof Location
2029 && lhs.getType().getExtension() instanceof CompositeLocation) {
2031 Location rhsLoc = (Location) rhs.getType().getExtension();
2033 CompositeLocation lhsCompLoc = (CompositeLocation) lhs.getType().getExtension();
2034 Location dstLoc = lhsCompLoc.get(lhsCompLoc.getSize() - 1);
2036 NTuple<Descriptor> heapPath = new NTuple<Descriptor>();
2037 for (int i = 0; i < rhsHeapPath.size() - 1; i++) {
2038 heapPath.add(rhsHeapPath.get(i));
2041 NTuple<Descriptor> writeHeapPath = new NTuple<Descriptor>();
2042 writeHeapPath.addAll(heapPath);
2043 writeHeapPath.add(lhs);
2045 System.out.println("VAR WRITE:" + fn);
2046 System.out.println("LHS TYPE EXTENSION=" + lhs.getType().getExtension());
2047 System.out.println("RHS TYPE EXTENSION=" + rhs.getType().getExtension()
2048 + " HEAPPATH=" + rhsHeapPath);
2050 // computing gen/kill set
2051 // computeKILLSetForWrite(currSharedLocMapping, heapPath, dstLoc,
2052 // killSetSharedLoc);
2053 // if (!dstLoc.equals(rhsLoc)) {
2054 // computeGENSetForHigherWrite(currSharedLocMapping, heapPath,
2056 // genSetSharedLoc);
2057 // deleteSet.remove(writeHeapPath);
2059 // computeGENSetForSharedWrite(currSharedLocMapping, heapPath,
2061 // genSetSharedLoc);
2062 // deleteSet.add(writeHeapPath);
2073 case FKind.FlatElementNode:
2074 case FKind.FlatFieldNode: {
2078 if (fn.kind() == FKind.FlatFieldNode) {
2079 FlatFieldNode ffn = (FlatFieldNode) fn;
2082 fld = ffn.getField();
2084 FlatElementNode fen = (FlatElementNode) fn;
2087 TypeDescriptor td = rhs.getType().dereference();
2088 fld = getArrayField(td);
2091 if (fld.isFinal()) {
2092 // if field is final no need to check
2097 NTuple<Descriptor> srcHeapPath = mapHeapPath.get(rhs);
2098 if (srcHeapPath != null) {
2099 // if lhs srcHeapPath is null, it means that it is not reachable from
2100 // callee's parameters. so just ignore it
2102 NTuple<Descriptor> readingHeapPath = new NTuple<Descriptor>(srcHeapPath.getList());
2103 readingHeapPath.add(fld);
2104 mapHeapPath.put(lhs, readingHeapPath);
2107 if (fld.getType().isImmutable()) {
2108 // if WT doesnot have hp(x.f), add hp(x.f) to READ
2109 if (!currMustWriteSet.contains(readingHeapPath)) {
2110 readSet.add(readingHeapPath);
2114 // no need to kill hp(x.f) from WT
2120 case FKind.FlatSetFieldNode:
2121 case FKind.FlatSetElementNode: {
2125 if (fn.kind() == FKind.FlatSetFieldNode) {
2126 FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
2127 lhs = fsfn.getDst();
2128 fld = fsfn.getField();
2129 rhs = fsfn.getSrc();
2131 FlatSetElementNode fsen = (FlatSetElementNode) fn;
2132 lhs = fsen.getDst();
2133 rhs = fsen.getSrc();
2134 TypeDescriptor td = lhs.getType().dereference();
2135 fld = getArrayField(td);
2139 NTuple<Descriptor> lhsHeapPath = mapHeapPath.get(lhs);
2141 if (lhsHeapPath != null) {
2142 // if lhs heap path is null, it means that it is not reachable from
2143 // callee's parameters. so just ignore it
2144 NTuple<Descriptor> fldHeapPath = new NTuple<Descriptor>(lhsHeapPath.getList());
2145 fldHeapPath.add(fld);
2146 mapHeapPath.put(fld, fldHeapPath);
2149 // need to add hp(y) to WT
2150 currMustWriteSet.add(fldHeapPath);
2151 mayWriteSet.add(fldHeapPath);
2158 case FKind.FlatCall: {
2160 FlatCall fc = (FlatCall) fn;
2162 bindHeapPathCallerArgWithCalleeParam(fc);
2164 mapFlatNodeToBoundReadSet.put(fn, calleeUnionBoundReadSet);
2165 mapFlatNodeToBoundMustWriteSet.put(fn, calleeIntersectBoundMustWriteSet);
2166 mapFlatNodeToBoundMayWriteSet.put(fn, calleeUnionBoundMayWriteSet);
2168 // add heap path, which is an element of READ_bound set and is not
2170 // element of WT set, to the caller's READ set
2171 for (Iterator iterator = calleeUnionBoundReadSet.iterator(); iterator.hasNext();) {
2172 NTuple<Descriptor> read = (NTuple<Descriptor>) iterator.next();
2173 if (!currMustWriteSet.contains(read)) {
2178 // add heap path, which is an element of OVERWRITE_bound set, to the
2180 for (Iterator iterator = calleeIntersectBoundMustWriteSet.iterator(); iterator.hasNext();) {
2181 NTuple<Descriptor> write = (NTuple<Descriptor>) iterator.next();
2182 currMustWriteSet.add(write);
2185 // add heap path, which is an element of WRITE_BOUND set, to the
2186 // caller's writeSet
2187 for (Iterator iterator = calleeUnionBoundMayWriteSet.iterator(); iterator.hasNext();) {
2188 NTuple<Descriptor> write = (NTuple<Descriptor>) iterator.next();
2189 mayWriteSet.add(write);
2195 case FKind.FlatExit: {
2196 // merge the current written set with OVERWRITE set
2197 merge(mustWriteSet, currMustWriteSet);
2205 static public FieldDescriptor getArrayField(TypeDescriptor td) {
2206 FieldDescriptor fd = mapTypeToArrayField.get(td);
2209 new FieldDescriptor(new Modifiers(Modifiers.PUBLIC), td, arrayElementFieldName, null,
2211 mapTypeToArrayField.put(td, fd);
2216 private void merge(Set<NTuple<Descriptor>> curr, Set<NTuple<Descriptor>> in) {
2217 if (curr.isEmpty()) {
2218 // set has a special initial value which covers all possible
2220 // For the first time of intersection, we can take all previous set
2223 // otherwise, current set is the intersection of the two sets
2229 // combine two heap path
2230 private NTuple<Descriptor> combine(NTuple<Descriptor> callerIn, NTuple<Descriptor> calleeIn) {
2231 NTuple<Descriptor> combined = new NTuple<Descriptor>();
2233 for (int i = 0; i < callerIn.size(); i++) {
2234 combined.add(callerIn.get(i));
2237 // the first element of callee's heap path represents parameter
2238 // so we skip the first one since it is already added from caller's heap
2240 for (int i = 1; i < calleeIn.size(); i++) {
2241 combined.add(calleeIn.get(i));
2247 private Set<NTuple<Descriptor>> bindSet(Set<NTuple<Descriptor>> calleeSet,
2248 Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc,
2249 Hashtable<Integer, NTuple<Descriptor>> mapCallerArgIdx2HeapPath) {
2251 Set<NTuple<Descriptor>> boundedCalleeSet = new HashSet<NTuple<Descriptor>>();
2253 Set<Integer> keySet = mapCallerArgIdx2HeapPath.keySet();
2254 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2255 Integer idx = (Integer) iterator.next();
2257 NTuple<Descriptor> callerArgHeapPath = mapCallerArgIdx2HeapPath.get(idx);
2258 TempDescriptor calleeParam = mapParamIdx2ParamTempDesc.get(idx);
2259 for (Iterator iterator2 = calleeSet.iterator(); iterator2.hasNext();) {
2260 NTuple<Descriptor> element = (NTuple<Descriptor>) iterator2.next();
2261 if (element.startsWith(calleeParam)) {
2262 NTuple<Descriptor> boundElement = combine(callerArgHeapPath, element);
2263 boundedCalleeSet.add(boundElement);
2269 return boundedCalleeSet;
2273 // Borrowed it from disjoint analysis
2274 private LinkedList<MethodDescriptor> topologicalSort(Set<MethodDescriptor> toSort) {
2276 Set<MethodDescriptor> discovered = new HashSet<MethodDescriptor>();
2278 LinkedList<MethodDescriptor> sorted = new LinkedList<MethodDescriptor>();
2280 Iterator<MethodDescriptor> itr = toSort.iterator();
2281 while (itr.hasNext()) {
2282 MethodDescriptor d = itr.next();
2284 if (!discovered.contains(d)) {
2285 dfsVisit(d, toSort, sorted, discovered);
2292 // While we're doing DFS on call graph, remember
2293 // dependencies for efficient queuing of methods
2294 // during interprocedural analysis:
2296 // a dependent of a method decriptor d for this analysis is:
2297 // 1) a method or task that invokes d
2298 // 2) in the descriptorsToAnalyze set
2299 private void dfsVisit(MethodDescriptor md, Set<MethodDescriptor> toSort,
2300 LinkedList<MethodDescriptor> sorted, Set<MethodDescriptor> discovered) {
2304 Iterator itr = callGraph.getCallerSet(md).iterator();
2305 while (itr.hasNext()) {
2306 MethodDescriptor dCaller = (MethodDescriptor) itr.next();
2307 // only consider callers in the original set to analyze
2308 if (!toSort.contains(dCaller)) {
2311 if (!discovered.contains(dCaller)) {
2312 addDependent(md, // callee
2316 dfsVisit(dCaller, toSort, sorted, discovered);
2320 // for leaf-nodes last now!
2324 // a dependent of a method decriptor d for this analysis is:
2325 // 1) a method or task that invokes d
2326 // 2) in the descriptorsToAnalyze set
2327 private void addDependent(MethodDescriptor callee, MethodDescriptor caller) {
2328 Set<MethodDescriptor> deps = mapDescriptorToSetDependents.get(callee);
2330 deps = new HashSet<MethodDescriptor>();
2333 mapDescriptorToSetDependents.put(callee, deps);
2336 private Set<MethodDescriptor> getDependents(MethodDescriptor callee) {
2337 Set<MethodDescriptor> deps = mapDescriptorToSetDependents.get(callee);
2339 deps = new HashSet<MethodDescriptor>();
2340 mapDescriptorToSetDependents.put(callee, deps);
2345 private NTuple<Descriptor> computePath(Descriptor td) {
2346 // generate proper path fot input td
2347 // if td is local variable, it just generate one element tuple path
2348 if (mapHeapPath.containsKey(td)) {
2349 return mapHeapPath.get(td);
2351 NTuple<Descriptor> path = new NTuple<Descriptor>();
2357 private NTuple<Location> deriveThisLocationTuple(MethodDescriptor md) {
2358 String thisLocIdentifier = ssjava.getMethodLattice(md).getThisLoc();
2359 Location thisLoc = new Location(md, thisLocIdentifier);
2360 NTuple<Location> locTuple = new NTuple<Location>();
2361 locTuple.add(thisLoc);
2365 private NTuple<Location> deriveLocationTuple(MethodDescriptor md, TempDescriptor td) {
2367 assert td.getType() != null;
2369 if (mapDescriptorToLocationPath.containsKey(td)) {
2370 return mapDescriptorToLocationPath.get(td);
2372 if (td.getSymbol().startsWith("this")) {
2373 return deriveThisLocationTuple(md);
2375 NTuple<Location> locTuple =
2376 ((SSJavaType) td.getType().getExtension()).getCompLoc().getTuple();