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;
16 import IR.ClassDescriptor;
18 import IR.FieldDescriptor;
19 import IR.MethodDescriptor;
22 import IR.TypeDescriptor;
23 import IR.TypeExtension;
25 import IR.Flat.FlatCall;
26 import IR.Flat.FlatElementNode;
27 import IR.Flat.FlatFieldNode;
28 import IR.Flat.FlatLiteralNode;
29 import IR.Flat.FlatMethod;
30 import IR.Flat.FlatNode;
31 import IR.Flat.FlatOpNode;
32 import IR.Flat.FlatSetElementNode;
33 import IR.Flat.FlatSetFieldNode;
34 import IR.Flat.TempDescriptor;
35 import IR.Tree.Modifiers;
38 public class DefinitelyWrittenCheck {
40 SSJavaAnalysis ssjava;
46 // maps a descriptor to its known dependents: namely
47 // methods or tasks that call the descriptor's method
48 // AND are part of this analysis (reachable from main)
49 private Hashtable<Descriptor, Set<MethodDescriptor>> mapDescriptorToSetDependents;
51 // maps a flat node to its WrittenSet: this keeps all heap path overwritten
53 private Hashtable<FlatNode, Set<NTuple<Descriptor>>> mapFlatNodeToMustWriteSet;
55 // maps a temp descriptor to its heap path
56 // each temp descriptor has a unique heap path since we do not allow any
58 private Hashtable<Descriptor, NTuple<Descriptor>> mapHeapPath;
60 // maps a temp descriptor to its composite location
61 private Hashtable<Descriptor, NTuple<Location>> mapDescriptorToComposteLocation;
63 // maps a flat method to the READ that is the set of heap path that is
64 // expected to be written before method invocation
65 private Hashtable<FlatMethod, Set<NTuple<Descriptor>>> mapFlatMethodToReadSet;
67 // maps a flat method to the must-write set that is the set of heap path that
68 // is overwritten on every possible path during method invocation
69 private Hashtable<FlatMethod, Set<NTuple<Descriptor>>> mapFlatMethodToMustWriteSet;
71 // maps a flat method to the DELETE SET that is a set of heap path to shared
73 // written to but not overwritten by the higher value
74 private Hashtable<FlatMethod, Set<NTuple<Descriptor>>> mapFlatMethodToDeleteSet;
76 // maps a flat method to the S SET that is a set of heap path to shared
77 // locations that are overwritten by the higher value
78 private Hashtable<FlatMethod, SharedLocMappingSet> mapFlatMethodToSharedLocMappingSet;
80 // maps a flat method to the may-wirte set that is the set of heap path that
81 // might be written to
82 private Hashtable<FlatMethod, Set<NTuple<Descriptor>>> mapFlatMethodToMayWriteSet;
84 // maps a call site to the read set contributed by all callees
85 private Hashtable<FlatNode, Set<NTuple<Descriptor>>> mapFlatNodeToBoundReadSet;
87 // maps a call site to the must write set contributed by all callees
88 private Hashtable<FlatNode, Set<NTuple<Descriptor>>> mapFlatNodeToBoundMustWriteSet;
90 // maps a call site to the may read set contributed by all callees
91 private Hashtable<FlatNode, Set<NTuple<Descriptor>>> mapFlatNodeToBoundMayWriteSet;
93 // points to method containing SSJAVA Loop
94 private MethodDescriptor methodContainingSSJavaLoop;
96 // maps a flatnode to definitely written analysis mapping M
97 private Hashtable<FlatNode, Hashtable<NTuple<Descriptor>, Set<WriteAge>>> mapFlatNodetoEventLoopMap;
99 // maps a method descriptor to its current summary during the analysis
100 // then analysis reaches fixed-point, this mapping will have the final summary
101 // for each method descriptor
102 private Hashtable<MethodDescriptor, ClearingSummary> mapMethodDescriptorToCompleteClearingSummary;
104 // maps a method descriptor to the merged incoming caller's current
105 // overwritten status
106 private Hashtable<MethodDescriptor, ClearingSummary> mapMethodDescriptorToInitialClearingSummary;
108 // maps a flat node to current partial results
109 private Hashtable<FlatNode, ClearingSummary> mapFlatNodeToClearingSummary;
111 // maps shared location to the set of descriptors which belong to the shared
114 // keep current descriptors to visit in fixed-point interprocedural analysis,
115 private Stack<MethodDescriptor> methodDescriptorsToVisitStack;
117 // when analyzing flatcall, need to re-schedule set of callee
118 private Set<MethodDescriptor> calleesToEnqueue;
120 private Set<ReadSummary> possibleCalleeReadSummarySetToCaller;
122 public static final String arrayElementFieldName = "___element_";
123 static protected Hashtable<TypeDescriptor, FieldDescriptor> mapTypeToArrayField;
125 private Set<ClearingSummary> possibleCalleeCompleteSummarySetToCaller;
127 // maps a method descriptor to the merged incoming caller's current
129 // it is for setting clearance flag when all read set is overwritten
130 private Hashtable<MethodDescriptor, ReadSummary> mapMethodDescriptorToReadSummary;
132 private Hashtable<FlatNode, SharedLocMappingSet> mapFlatNodeToSharedLocMapping;
134 private Hashtable<Location, Set<Descriptor>> mapSharedLocationToCoverSet;
136 private Hashtable<NTuple<Location>, Set<Descriptor>> mapSharedLocationTupleToMayWriteSet;
138 private LinkedList<MethodDescriptor> sortedDescriptors;
140 private FlatNode ssjavaLoopEntrance;
141 private LoopFinder ssjavaLoop;
142 private Set<FlatNode> loopIncElements;
144 private Set<NTuple<Descriptor>> calleeUnionBoundReadSet;
145 private Set<NTuple<Descriptor>> calleeIntersectBoundMustWriteSet;
146 private Set<NTuple<Descriptor>> calleeUnionBoundMayWriteSet;
147 private Set<NTuple<Descriptor>> calleeUnionBoundDeleteSet;
148 private SharedLocMappingSet calleeIntersectBoundSharedSet;
150 private Hashtable<Descriptor, Location> mapDescToLocation;
152 private TempDescriptor LOCAL;
154 public static int MAXAGE = 1;
156 public DefinitelyWrittenCheck(SSJavaAnalysis ssjava, State state) {
158 this.ssjava = ssjava;
159 this.callGraph = ssjava.getCallGraph();
160 this.mapFlatNodeToMustWriteSet = new Hashtable<FlatNode, Set<NTuple<Descriptor>>>();
161 this.mapDescriptorToSetDependents = new Hashtable<Descriptor, Set<MethodDescriptor>>();
162 this.mapHeapPath = new Hashtable<Descriptor, NTuple<Descriptor>>();
163 this.mapDescriptorToComposteLocation = new Hashtable<Descriptor, NTuple<Location>>();
164 this.mapFlatMethodToReadSet = new Hashtable<FlatMethod, Set<NTuple<Descriptor>>>();
165 this.mapFlatMethodToMustWriteSet = new Hashtable<FlatMethod, Set<NTuple<Descriptor>>>();
166 this.mapFlatMethodToMayWriteSet = new Hashtable<FlatMethod, Set<NTuple<Descriptor>>>();
167 this.mapFlatNodetoEventLoopMap =
168 new Hashtable<FlatNode, Hashtable<NTuple<Descriptor>, Set<WriteAge>>>();
169 this.calleeUnionBoundReadSet = new HashSet<NTuple<Descriptor>>();
170 this.calleeIntersectBoundMustWriteSet = new HashSet<NTuple<Descriptor>>();
171 this.calleeUnionBoundMayWriteSet = new HashSet<NTuple<Descriptor>>();
173 this.mapMethodDescriptorToCompleteClearingSummary =
174 new Hashtable<MethodDescriptor, ClearingSummary>();
175 this.mapMethodDescriptorToInitialClearingSummary =
176 new Hashtable<MethodDescriptor, ClearingSummary>();
177 this.methodDescriptorsToVisitStack = new Stack<MethodDescriptor>();
178 this.calleesToEnqueue = new HashSet<MethodDescriptor>();
179 this.possibleCalleeCompleteSummarySetToCaller = new HashSet<ClearingSummary>();
180 this.mapTypeToArrayField = new Hashtable<TypeDescriptor, FieldDescriptor>();
181 this.LOCAL = new TempDescriptor("LOCAL");
182 this.mapDescToLocation = new Hashtable<Descriptor, Location>();
183 this.possibleCalleeReadSummarySetToCaller = new HashSet<ReadSummary>();
184 this.mapMethodDescriptorToReadSummary = new Hashtable<MethodDescriptor, ReadSummary>();
185 this.mapFlatNodeToBoundReadSet = new Hashtable<FlatNode, Set<NTuple<Descriptor>>>();
186 this.mapFlatNodeToBoundMustWriteSet = new Hashtable<FlatNode, Set<NTuple<Descriptor>>>();
187 this.mapFlatNodeToBoundMayWriteSet = new Hashtable<FlatNode, Set<NTuple<Descriptor>>>();
188 this.mapSharedLocationToCoverSet = new Hashtable<Location, Set<Descriptor>>();
189 this.mapFlatNodeToSharedLocMapping = new Hashtable<FlatNode, SharedLocMappingSet>();
190 this.mapFlatMethodToDeleteSet = new Hashtable<FlatMethod, Set<NTuple<Descriptor>>>();
191 this.calleeUnionBoundDeleteSet = new HashSet<NTuple<Descriptor>>();
192 this.calleeIntersectBoundSharedSet = new SharedLocMappingSet();
193 this.mapFlatMethodToSharedLocMappingSet = new Hashtable<FlatMethod, SharedLocMappingSet>();
194 this.mapSharedLocationTupleToMayWriteSet = new Hashtable<NTuple<Location>, Set<Descriptor>>();
197 public void definitelyWrittenCheck() {
198 if (!ssjava.getAnnotationRequireSet().isEmpty()) {
200 computeSharedCoverSet();
202 System.out.println("#");
203 System.out.println(mapSharedLocationTupleToMayWriteSet);
205 // methodReadWriteSetAnalysis();
207 // sharedLocAnalysis();
209 // eventLoopAnalysis();
212 // methodReadWriteSetAnalysis();
213 // methodReadWriteSetAnalysisToEventLoopBody();
214 // eventLoopAnalysis();
216 // sharedLocationAnalysis();
217 // checkSharedLocationResult();
221 private void sharedLocAnalysis() {
223 // perform method READ/OVERWRITE analysis
224 LinkedList<MethodDescriptor> descriptorListToAnalyze =
225 (LinkedList<MethodDescriptor>) sortedDescriptors.clone();
227 // current descriptors to visit in fixed-point interprocedural analysis,
229 // dependency in the call graph
230 methodDescriptorsToVisitStack.clear();
232 descriptorListToAnalyze.removeFirst();
234 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
235 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
237 while (!descriptorListToAnalyze.isEmpty()) {
238 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
239 methodDescriptorsToVisitStack.add(md);
242 // analyze scheduled methods until there are no more to visit
243 while (!methodDescriptorsToVisitStack.isEmpty()) {
244 // start to analyze leaf node
245 MethodDescriptor md = methodDescriptorsToVisitStack.pop();
246 FlatMethod fm = state.getMethodFlat(md);
248 Set<NTuple<Descriptor>> deleteSet = new HashSet<NTuple<Descriptor>>();
250 sharedLoc_analyzeMethod(fm, deleteSet);
251 System.out.println("deleteSet result=" + deleteSet);
253 Set<NTuple<Descriptor>> prevDeleteSet = mapFlatMethodToDeleteSet.get(fm);
255 if (!deleteSet.equals(prevDeleteSet)) {
256 mapFlatMethodToDeleteSet.put(fm, deleteSet);
258 // results for callee changed, so enqueue dependents caller for
261 Iterator<MethodDescriptor> depsItr = getDependents(md).iterator();
262 while (depsItr.hasNext()) {
263 MethodDescriptor methodNext = depsItr.next();
264 if (!methodDescriptorsToVisitStack.contains(methodNext)
265 && methodDescriptorToVistSet.contains(methodNext)) {
266 methodDescriptorsToVisitStack.add(methodNext);
277 private void sharedLoc_analyzeMethod(FlatMethod fm, Set<NTuple<Descriptor>> deleteSet) {
278 if (state.SSJAVADEBUG) {
279 System.out.println("SSJAVA: Definite clearance for shared locations Analyzing: " + fm);
282 sharedLoc_analyzeBody(fm, deleteSet, false);
286 private void sharedLoc_analyzeBody(FlatNode startNode, Set<NTuple<Descriptor>> deleteSet,
287 boolean isEventLoopBody) {
289 // intraprocedural analysis
290 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
291 flatNodesToVisit.add(startNode);
293 while (!flatNodesToVisit.isEmpty()) {
294 FlatNode fn = flatNodesToVisit.iterator().next();
295 flatNodesToVisit.remove(fn);
297 SharedLocMappingSet currSharedSet = new SharedLocMappingSet();
299 for (int i = 0; i < fn.numPrev(); i++) {
300 FlatNode prevFn = fn.getPrev(i);
301 SharedLocMappingSet in = mapFlatNodeToSharedLocMapping.get(prevFn);
303 merge(currSharedSet, in);
307 sharedLoc_nodeActions(fn, currSharedSet, deleteSet, isEventLoopBody);
309 SharedLocMappingSet mustSetPrev = mapFlatNodeToSharedLocMapping.get(fn);
310 if (!currSharedSet.equals(mustSetPrev)) {
311 mapFlatNodeToSharedLocMapping.put(fn, currSharedSet);
312 for (int i = 0; i < fn.numNext(); i++) {
313 FlatNode nn = fn.getNext(i);
314 if ((!isEventLoopBody) || loopIncElements.contains(nn)) {
315 flatNodesToVisit.add(nn);
325 private void sharedLoc_nodeActions(FlatNode fn, SharedLocMappingSet curr,
326 Set<NTuple<Descriptor>> deleteSet, boolean isEventLoopBody) {
328 SharedLocMappingSet killSet = new SharedLocMappingSet();
329 SharedLocMappingSet genSet = new SharedLocMappingSet();
337 case FKind.FlatOpNode: {
339 if (isEventLoopBody) {
340 FlatOpNode fon = (FlatOpNode) fn;
344 if (!lhs.getSymbol().startsWith("neverused")) {
346 if (rhs.getType().isImmutable()) {
347 NTuple<Descriptor> rhsHeapPath = computePath(rhs);
349 if (rhs.getType().getExtension() instanceof Location
350 && lhs.getType().getExtension() instanceof CompositeLocation) {
352 Location rhsLoc = (Location) rhs.getType().getExtension();
354 CompositeLocation lhsCompLoc = (CompositeLocation) lhs.getType().getExtension();
355 Location dstLoc = lhsCompLoc.get(lhsCompLoc.getSize() - 1);
357 NTuple<Descriptor> heapPath = new NTuple<Descriptor>();
358 for (int i = 0; i < rhsHeapPath.size() - 1; i++) {
359 heapPath.add(rhsHeapPath.get(i));
362 NTuple<Descriptor> writeHeapPath = new NTuple<Descriptor>();
363 writeHeapPath.addAll(heapPath);
364 writeHeapPath.add(lhs);
366 System.out.println("VAR WRITE:" + fn);
367 System.out.println("LHS TYPE EXTENSION=" + lhs.getType().getExtension());
368 System.out.println("RHS TYPE EXTENSION=" + rhs.getType().getExtension()
369 + " HEAPPATH=" + rhsHeapPath);
371 // computing gen/kill set
372 computeKILLSetForWrite(curr, heapPath, dstLoc, killSet);
373 if (!dstLoc.equals(rhsLoc)) {
374 computeGENSetForHigherWrite(curr, heapPath, dstLoc, lhs, genSet);
375 deleteSet.remove(writeHeapPath);
377 computeGENSetForSharedWrite(curr, heapPath, dstLoc, lhs, genSet);
378 deleteSet.add(writeHeapPath);
383 // System.out.println("fieldLoc=" + fieldLoc + " srcLoc=" + srcLoc);
384 System.out.println("KILLSET=" + killSet);
385 System.out.println("GENSet=" + genSet);
386 System.out.println("DELETESET=" + deleteSet);
395 case FKind.FlatSetFieldNode:
396 case FKind.FlatSetElementNode: {
398 if (fn.kind() == FKind.FlatSetFieldNode) {
399 FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
401 fld = fsfn.getField();
404 FlatSetElementNode fsen = (FlatSetElementNode) fn;
407 TypeDescriptor td = lhs.getType().dereference();
408 fld = getArrayField(td);
411 // shared loc extension
412 Location srcLoc = getLocation(rhs);
413 Location fieldLoc = (Location) fld.getType().getExtension();
414 if (ssjava.isSharedLocation(fieldLoc)) {
415 // only care the case that loc(f) is shared location
417 NTuple<Descriptor> lhsHeapPath = computePath(lhs);
418 NTuple<Descriptor> fldHeapPath = new NTuple<Descriptor>(lhsHeapPath.getList());
419 fldHeapPath.add(fld);
421 // computing gen/kill set
422 computeKILLSetForWrite(curr, lhsHeapPath, fieldLoc, killSet);
423 if (!fieldLoc.equals(srcLoc)) {
424 System.out.println("LOC IS DIFFERENT");
425 computeGENSetForHigherWrite(curr, lhsHeapPath, fieldLoc, fld, genSet);
426 deleteSet.remove(fldHeapPath);
428 computeGENSetForSharedWrite(curr, lhsHeapPath, fieldLoc, fld, genSet);
429 deleteSet.add(fldHeapPath);
433 System.out.println("################");
434 System.out.println("FIELD WRITE:" + fn);
435 System.out.println("fieldLoc=" + fieldLoc + " srcLoc=" + srcLoc);
436 System.out.println("KILLSET=" + killSet);
437 System.out.println("GENSet=" + genSet);
438 System.out.println("DELETESET=" + deleteSet);
443 case FKind.FlatCall: {
444 FlatCall fc = (FlatCall) fn;
446 bindHeapPathCallerArgWithCaleeParamForSharedLoc(fc);
448 // generateKILLSetForFlatCall(fc, curr, readWriteKillSet);
449 // generateGENSetForFlatCall(fc, readWriteGenSet);
451 // System.out.println
452 // // only care the case that loc(f) is shared location
454 // NTuple<Descriptor> lhsHeapPath = computePath(lhs);
455 // NTuple<Descriptor> fldHeapPath = new
456 // NTuple<Descriptor>(lhsHeapPath.getList());
457 // fldHeapPath.add(fld);
459 // // computing gen/kill set
460 // computeKILLSetForWrite(curr, lhsHeapPath, fieldLoc, killSet);
461 // if (!fieldLoc.equals(srcLoc)) {
462 // System.out.println("LOC IS DIFFERENT");
463 // computeGENSetForHigherWrite(curr, lhsHeapPath, fieldLoc, fld, genSet);
464 // deleteSet.remove(fldHeapPath);
466 // computeGENSetForSharedWrite(curr, lhsHeapPath, fieldLoc, fld, genSet);
467 // deleteSet.add(fldHeapPath);
469 // ("FLATCALL:" + fn);
470 // System.out.println("bound DELETE Set=" + calleeUnionBoundDeleteSet);
471 // // System.out.println("KILLSET=" + KILLSet);
472 // // System.out.println("GENSet=" + GENSet);
479 // computeNewMapping(curr, readWriteKillSet, readWriteGenSet);
480 // System.out.println("#######" + curr);
484 private void computeKILLSetForWrite(SharedLocMappingSet curr, NTuple<Descriptor> hp,
485 Location loc, SharedLocMappingSet killSet) {
487 Set<Descriptor> currWriteSet = curr.getWriteSet(hp, loc);
488 if (!currWriteSet.isEmpty()) {
489 killSet.addWriteSet(hp, loc, currWriteSet);
494 private void computeGENSetForHigherWrite(SharedLocMappingSet curr, NTuple<Descriptor> hp,
495 Location loc, Descriptor desc, SharedLocMappingSet genSet) {
497 Set<Descriptor> genWriteSet = new HashSet<Descriptor>();
498 genWriteSet.addAll(curr.getWriteSet(hp, loc));
499 genWriteSet.add(desc);
501 genSet.addWriteSet(hp, loc, genWriteSet);
505 private void computeGENSetForSharedWrite(SharedLocMappingSet curr, NTuple<Descriptor> hp,
506 Location loc, Descriptor desc, SharedLocMappingSet genSet) {
508 Set<Descriptor> genWriteSet = new HashSet<Descriptor>();
509 genWriteSet.addAll(curr.getWriteSet(hp, loc));
510 genWriteSet.remove(desc);
512 if (!genWriteSet.isEmpty()) {
513 genSet.addWriteSet(hp, loc, genWriteSet);
517 private void merge(SharedLocMappingSet currSharedSet, SharedLocMappingSet in) {
519 Set<NTuple<Descriptor>> hpKeySet = in.getHeapPathKeySet();
520 for (Iterator iterator = hpKeySet.iterator(); iterator.hasNext();) {
521 NTuple<Descriptor> hpKey = (NTuple<Descriptor>) iterator.next();
522 Set<Location> locSet = in.getLocationKeySet(hpKey);
523 for (Iterator iterator2 = locSet.iterator(); iterator2.hasNext();) {
524 Location locKey = (Location) iterator2.next();
525 Set<Descriptor> writeSet = in.getWriteSet(hpKey, locKey);
526 currSharedSet.intersectWriteSet(hpKey, locKey, writeSet);
532 private void checkSharedLocationResult() {
534 // mapping of method containing ssjava loop has the final result of
535 // shared location analysis
537 ClearingSummary result =
538 mapMethodDescriptorToCompleteClearingSummary.get(methodContainingSSJavaLoop);
540 String str = generateNotClearedResult(result);
541 if (str.length() > 0) {
543 "Following concrete locations of the shared abstract location are not cleared at the same time:\n"
549 private String generateNotClearedResult(ClearingSummary result) {
550 Set<NTuple<Descriptor>> keySet = result.keySet();
552 StringBuffer str = new StringBuffer();
553 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
554 NTuple<Descriptor> hpKey = (NTuple<Descriptor>) iterator.next();
555 SharedStatus status = result.get(hpKey);
556 Hashtable<Location, Pair<Set<Descriptor>, Boolean>> map = status.getMap();
557 Set<Location> locKeySet = map.keySet();
558 for (Iterator iterator2 = locKeySet.iterator(); iterator2.hasNext();) {
559 Location locKey = (Location) iterator2.next();
560 if (status.haveWriteEffect(locKey)) {
561 Pair<Set<Descriptor>, Boolean> pair = map.get(locKey);
562 if (!pair.getSecond().booleanValue()) {
564 str.append("- Concrete locations of the shared location '" + locKey
565 + "' are not cleared out, which are reachable through the heap path '" + hpKey
572 return str.toString();
576 private void writeReadMapFile() {
578 String fileName = "SharedLocationReadMap";
581 BufferedWriter bw = new BufferedWriter(new FileWriter(fileName + ".txt"));
583 Set<MethodDescriptor> keySet = mapMethodDescriptorToReadSummary.keySet();
584 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
585 MethodDescriptor mdKey = (MethodDescriptor) iterator.next();
586 ReadSummary summary = mapMethodDescriptorToReadSummary.get(mdKey);
587 bw.write("Method " + mdKey + "::\n");
588 bw.write(summary + "\n\n");
591 } catch (IOException e) {
597 private void sharedLocationAnalysis() {
598 // verify that all concrete locations of shared location are cleared out at
599 // the same time once per the out-most loop
601 computeSharedCoverSet();
603 if (state.SSJAVADEBUG) {
607 // methodDescriptorsToVisitStack.clear();
608 // methodDescriptorsToVisitStack.add(sortedDescriptors.peekFirst());
610 LinkedList<MethodDescriptor> descriptorListToAnalyze =
611 (LinkedList<MethodDescriptor>) sortedDescriptors.clone();
613 // current descriptors to visit in fixed-point interprocedural analysis,
615 // dependency in the call graph
616 methodDescriptorsToVisitStack.clear();
618 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
619 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
621 while (!descriptorListToAnalyze.isEmpty()) {
622 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
623 methodDescriptorsToVisitStack.add(md);
626 // analyze scheduled methods until there are no more to visit
627 while (!methodDescriptorsToVisitStack.isEmpty()) {
628 MethodDescriptor md = methodDescriptorsToVisitStack.pop();
630 ClearingSummary completeSummary =
631 sharedLocation_analyzeMethod(md, (md.equals(methodContainingSSJavaLoop)));
633 ClearingSummary prevCompleteSummary = mapMethodDescriptorToCompleteClearingSummary.get(md);
635 if (!completeSummary.equals(prevCompleteSummary)) {
637 mapMethodDescriptorToCompleteClearingSummary.put(md, completeSummary);
639 // results for callee changed, so enqueue dependents caller for
641 Iterator<MethodDescriptor> depsItr = getDependents(md).iterator();
642 while (depsItr.hasNext()) {
643 MethodDescriptor methodNext = depsItr.next();
644 if (!methodDescriptorsToVisitStack.contains(methodNext)) {
645 methodDescriptorsToVisitStack.add(methodNext);
649 // if there is set of callee to be analyzed,
650 // add this set into the top of stack
651 Iterator<MethodDescriptor> calleeIter = calleesToEnqueue.iterator();
652 while (calleeIter.hasNext()) {
653 MethodDescriptor mdNext = calleeIter.next();
654 if (!methodDescriptorsToVisitStack.contains(mdNext)) {
655 methodDescriptorsToVisitStack.add(mdNext);
658 calleesToEnqueue.clear();
666 private ClearingSummary sharedLocation_analyzeMethod(MethodDescriptor md,
667 boolean onlyVisitSSJavaLoop) {
669 if (state.SSJAVADEBUG) {
670 System.out.println("SSJAVA: Definite clearance for shared locations Analyzing: " + md);
673 FlatMethod fm = state.getMethodFlat(md);
675 // intraprocedural analysis
676 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
678 // start a new mapping of partial results for each flat node
679 mapFlatNodeToClearingSummary = new Hashtable<FlatNode, ClearingSummary>();
681 if (onlyVisitSSJavaLoop) {
682 flatNodesToVisit.add(ssjavaLoopEntrance);
684 flatNodesToVisit.add(fm);
687 Set<FlatNode> returnNodeSet = new HashSet<FlatNode>();
689 while (!flatNodesToVisit.isEmpty()) {
690 FlatNode fn = flatNodesToVisit.iterator().next();
691 flatNodesToVisit.remove(fn);
693 ClearingSummary curr = new ClearingSummary();
695 Set<ClearingSummary> prevSet = new HashSet<ClearingSummary>();
696 for (int i = 0; i < fn.numPrev(); i++) {
697 FlatNode prevFn = fn.getPrev(i);
698 ClearingSummary in = mapFlatNodeToClearingSummary.get(prevFn);
703 mergeSharedLocationAnaylsis(curr, prevSet);
705 sharedLocation_nodeActions(md, fn, curr, returnNodeSet, onlyVisitSSJavaLoop);
706 ClearingSummary clearingPrev = mapFlatNodeToClearingSummary.get(fn);
708 if (!curr.equals(clearingPrev)) {
709 mapFlatNodeToClearingSummary.put(fn, curr);
711 for (int i = 0; i < fn.numNext(); i++) {
712 FlatNode nn = fn.getNext(i);
714 if (!onlyVisitSSJavaLoop || (onlyVisitSSJavaLoop && loopIncElements.contains(nn))) {
715 flatNodesToVisit.add(nn);
723 ClearingSummary completeSummary = new ClearingSummary();
724 Set<ClearingSummary> summarySet = new HashSet<ClearingSummary>();
726 if (onlyVisitSSJavaLoop) {
727 // when analyzing ssjava loop,
728 // complete summary is merging of all previous nodes of ssjava loop
730 for (int i = 0; i < ssjavaLoopEntrance.numPrev(); i++) {
731 ClearingSummary frnSummary =
732 mapFlatNodeToClearingSummary.get(ssjavaLoopEntrance.getPrev(i));
733 if (frnSummary != null) {
734 summarySet.add(frnSummary);
738 // merging all exit node summary into the complete summary
739 if (!returnNodeSet.isEmpty()) {
740 for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
741 FlatNode frn = (FlatNode) iterator.next();
742 ClearingSummary frnSummary = mapFlatNodeToClearingSummary.get(frn);
743 summarySet.add(frnSummary);
747 mergeSharedLocationAnaylsis(completeSummary, summarySet);
749 return completeSummary;
752 private void sharedLocation_nodeActions(MethodDescriptor md, FlatNode fn, ClearingSummary curr,
753 Set<FlatNode> returnNodeSet, boolean isSSJavaLoop) {
760 case FKind.FlatMethod: {
761 FlatMethod fm = (FlatMethod) fn;
763 ClearingSummary summaryFromCaller =
764 mapMethodDescriptorToInitialClearingSummary.get(fm.getMethod());
766 Set<ClearingSummary> inSet = new HashSet<ClearingSummary>();
767 if (summaryFromCaller != null) {
768 inSet.add(summaryFromCaller);
769 mergeSharedLocationAnaylsis(curr, inSet);
775 case FKind.FlatOpNode: {
776 FlatOpNode fon = (FlatOpNode) fn;
780 if (fon.getOp().getOp() == Operation.ASSIGN) {
781 if (rhs.getType().isImmutable() && isSSJavaLoop) {
782 // in ssjavaloop, we need to take care about reading local variables!
783 NTuple<Descriptor> rhsHeapPath = new NTuple<Descriptor>();
784 NTuple<Descriptor> lhsHeapPath = new NTuple<Descriptor>();
785 rhsHeapPath.add(LOCAL);
786 lhsHeapPath.add(LOCAL);
787 if (!lhs.getSymbol().startsWith("neverused")) {
788 readLocation(md, curr, rhsHeapPath, getLocation(rhs), rhs);
789 writeLocation(md, curr, lhsHeapPath, getLocation(lhs), lhs);
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);
816 NTuple<Descriptor> lhsHeapPath = computePath(lhs);
817 NTuple<Descriptor> fldHeapPath = new NTuple<Descriptor>(lhsHeapPath.getList());
818 if (fld.getType().isImmutable()) {
820 writeLocation(md, curr, fldHeapPath, getLocation(fld), fld);
822 Descriptor desc = fldHeapPath.get(fldHeapPath.size() - 1);
823 if (desc instanceof FieldDescriptor) {
824 NTuple<Descriptor> arrayPath = new NTuple<Descriptor>();
825 for (int i = 0; i < fldHeapPath.size() - 1; i++) {
826 arrayPath.add(fldHeapPath.get(i));
828 SharedStatus state = getState(curr, arrayPath);
829 state.setWriteEffect(getLocation(desc));
833 // updates reference field case:
834 fldHeapPath.add(fld);
835 updateWriteEffectOnReferenceField(curr, fldHeapPath);
841 case FKind.FlatCall: {
843 FlatCall fc = (FlatCall) fn;
845 if (ssjava.isSSJavaUtil(fc.getMethod().getClassDesc())) {
847 // have write effects on the first argument
849 if (fc.getArg(0).getType().isArray()) {
850 // updates reference field case:
851 // 2. if there exists a tuple t in sharing summary that starts with
852 // hp(x) then, set flag of tuple t to 'true'
853 NTuple<Descriptor> argHeapPath = computePath(fc.getArg(0));
855 Location loc = getLocation(fc.getArg(0));
856 NTuple<Descriptor> newHeapPath = new NTuple<Descriptor>();
857 for (int i = 0; i < argHeapPath.size() - 1; i++) {
858 newHeapPath.add(argHeapPath.get(i));
860 fld = (FieldDescriptor) argHeapPath.get(argHeapPath.size() - 1);
861 argHeapPath = newHeapPath;
863 writeLocation(md, curr, argHeapPath, loc, fld);
867 // find out the set of callees
868 MethodDescriptor mdCallee = fc.getMethod();
869 FlatMethod fmCallee = state.getMethodFlat(mdCallee);
870 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
871 setPossibleCallees.addAll(callGraph.getMethods(mdCallee));
873 possibleCalleeCompleteSummarySetToCaller.clear();
875 for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
876 MethodDescriptor mdPossibleCallee = (MethodDescriptor) iterator.next();
877 FlatMethod calleeFlatMethod = state.getMethodFlat(mdPossibleCallee);
879 addDependent(mdPossibleCallee, // callee
882 calleesToEnqueue.add(mdPossibleCallee);
884 // updates possible callee's initial summary using caller's current
886 ClearingSummary prevCalleeInitSummary =
887 mapMethodDescriptorToInitialClearingSummary.get(mdPossibleCallee);
889 ClearingSummary calleeInitSummary =
890 bindHeapPathOfCalleeCallerEffects(fc, calleeFlatMethod, curr);
892 Set<ClearingSummary> inSet = new HashSet<ClearingSummary>();
893 if (prevCalleeInitSummary != null) {
894 inSet.add(prevCalleeInitSummary);
895 mergeSharedLocationAnaylsis(calleeInitSummary, inSet);
898 // if changes, update the init summary
899 // and reschedule the callee for analysis
900 if (!calleeInitSummary.equals(prevCalleeInitSummary)) {
902 if (!methodDescriptorsToVisitStack.contains(mdPossibleCallee)) {
903 methodDescriptorsToVisitStack.add(mdPossibleCallee);
906 mapMethodDescriptorToInitialClearingSummary.put(mdPossibleCallee, calleeInitSummary);
911 // contribute callee's writing effects to the caller
912 mergeSharedLocationAnaylsis(curr, possibleCalleeCompleteSummarySetToCaller);
919 case FKind.FlatReturnNode: {
920 returnNodeSet.add(fn);
928 private void updateWriteEffectOnReferenceField(ClearingSummary curr, NTuple<Descriptor> heapPath) {
930 // 2. if there exists a tuple t in sharing summary that starts with
931 // hp(x) then, set flag of tuple t to 'true'
932 Set<NTuple<Descriptor>> hpKeySet = curr.keySet();
933 for (Iterator iterator = hpKeySet.iterator(); iterator.hasNext();) {
934 NTuple<Descriptor> hpKey = (NTuple<Descriptor>) iterator.next();
935 if (hpKey.startsWith(heapPath)) {
936 curr.get(hpKey).updateFlag(true);
942 private ClearingSummary bindHeapPathOfCalleeCallerEffects(FlatCall fc,
943 FlatMethod calleeFlatMethod, ClearingSummary curr) {
945 ClearingSummary boundSet = new ClearingSummary();
947 // create mapping from arg idx to its heap paths
948 Hashtable<Integer, NTuple<Descriptor>> mapArgIdx2CallerArgHeapPath =
949 new Hashtable<Integer, NTuple<Descriptor>>();
951 if (fc.getThis() != null) {
952 // arg idx is starting from 'this' arg
953 NTuple<Descriptor> thisHeapPath = mapHeapPath.get(fc.getThis());
954 if (thisHeapPath == null) {
955 // method is called without creating new flat node representing 'this'
956 thisHeapPath = new NTuple<Descriptor>();
957 thisHeapPath.add(fc.getThis());
960 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(0), thisHeapPath);
963 for (int i = 0; i < fc.numArgs(); i++) {
964 TempDescriptor arg = fc.getArg(i);
965 NTuple<Descriptor> argHeapPath = computePath(arg);
966 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(i + 1), argHeapPath);
969 Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc =
970 new Hashtable<Integer, TempDescriptor>();
972 if (calleeFlatMethod.getMethod().isStatic()) {
973 // static method does not have implicit 'this' arg
976 for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
977 TempDescriptor param = calleeFlatMethod.getParameter(i);
978 mapParamIdx2ParamTempDesc.put(Integer.valueOf(i + offset), param);
981 // binding caller's writing effects to callee's params
982 for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
983 NTuple<Descriptor> argHeapPath = mapArgIdx2CallerArgHeapPath.get(Integer.valueOf(i));
985 if (argHeapPath != null) {
986 // if method is static, the first argument is nulll because static
987 // method does not have implicit "THIS" arg
988 TempDescriptor calleeParamHeapPath = mapParamIdx2ParamTempDesc.get(Integer.valueOf(i));
990 // iterate over caller's writing effect set
991 Set<NTuple<Descriptor>> hpKeySet = curr.keySet();
992 for (Iterator iterator = hpKeySet.iterator(); iterator.hasNext();) {
993 NTuple<Descriptor> hpKey = (NTuple<Descriptor>) iterator.next();
994 // current element is reachable caller's arg
995 // so need to bind it to the caller's side and add it to the
998 if (hpKey.startsWith(argHeapPath)) {
999 NTuple<Descriptor> boundHeapPath = replace(hpKey, argHeapPath, calleeParamHeapPath);
1000 boundSet.put(boundHeapPath, curr.get(hpKey).clone());
1008 // contribute callee's complete summary into the caller's current summary
1009 ClearingSummary calleeCompleteSummary =
1010 mapMethodDescriptorToCompleteClearingSummary.get(calleeFlatMethod.getMethod());
1011 if (calleeCompleteSummary != null) {
1012 ClearingSummary boundCalleeEfffects = new ClearingSummary();
1013 for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
1014 NTuple<Descriptor> argHeapPath = mapArgIdx2CallerArgHeapPath.get(Integer.valueOf(i));
1016 if (argHeapPath != null) {
1017 // if method is static, the first argument is nulll because static
1018 // method does not have implicit "THIS" arg
1019 TempDescriptor calleeParamHeapPath = mapParamIdx2ParamTempDesc.get(Integer.valueOf(i));
1021 // iterate over callee's writing effect set
1022 Set<NTuple<Descriptor>> hpKeySet = calleeCompleteSummary.keySet();
1023 for (Iterator iterator = hpKeySet.iterator(); iterator.hasNext();) {
1024 NTuple<Descriptor> hpKey = (NTuple<Descriptor>) iterator.next();
1025 // current element is reachable caller's arg
1026 // so need to bind it to the caller's side and add it to the
1029 if (hpKey.startsWith(calleeParamHeapPath)) {
1031 NTuple<Descriptor> boundHeapPathForCaller = replace(hpKey, argHeapPath);
1033 boundCalleeEfffects.put(boundHeapPathForCaller, calleeCompleteSummary.get(hpKey)
1042 possibleCalleeCompleteSummarySetToCaller.add(boundCalleeEfffects);
1048 private NTuple<Descriptor> replace(NTuple<Descriptor> hpKey, NTuple<Descriptor> argHeapPath) {
1050 // replace the head of heap path with caller's arg path
1051 // for example, heap path 'param.a.b' in callee's side will be replaced with
1052 // (corresponding arg heap path).a.b for caller's side
1054 NTuple<Descriptor> bound = new NTuple<Descriptor>();
1056 for (int i = 0; i < argHeapPath.size(); i++) {
1057 bound.add(argHeapPath.get(i));
1060 for (int i = 1; i < hpKey.size(); i++) {
1061 bound.add(hpKey.get(i));
1067 private NTuple<Descriptor> replace(NTuple<Descriptor> effectHeapPath,
1068 NTuple<Descriptor> argHeapPath, TempDescriptor calleeParamHeapPath) {
1069 // replace the head of caller's heap path with callee's param heap path
1071 NTuple<Descriptor> boundHeapPath = new NTuple<Descriptor>();
1072 boundHeapPath.add(calleeParamHeapPath);
1074 for (int i = argHeapPath.size(); i < effectHeapPath.size(); i++) {
1075 boundHeapPath.add(effectHeapPath.get(i));
1078 return boundHeapPath;
1081 private void computeSharedCoverSet() {
1082 LinkedList<MethodDescriptor> descriptorListToAnalyze =
1083 (LinkedList<MethodDescriptor>) sortedDescriptors.clone();
1085 // current descriptors to visit in fixed-point interprocedural analysis,
1087 // dependency in the call graph
1088 methodDescriptorsToVisitStack.clear();
1090 descriptorListToAnalyze.removeFirst();
1092 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
1093 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
1095 while (!descriptorListToAnalyze.isEmpty()) {
1096 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
1097 methodDescriptorsToVisitStack.add(md);
1100 // analyze scheduled methods until there are no more to visit
1101 while (!methodDescriptorsToVisitStack.isEmpty()) {
1102 MethodDescriptor md = methodDescriptorsToVisitStack.pop();
1104 FlatMethod fm = state.getMethodFlat(md);
1106 computeSharedCoverSet_analyzeMethod(fm, md.equals(methodContainingSSJavaLoop));
1112 private void computeSharedCoverSet_analyzeMethod(FlatMethod fm, boolean onlyVisitSSJavaLoop) {
1114 MethodDescriptor md = fm.getMethod();
1115 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
1117 Set<FlatNode> visited = new HashSet<FlatNode>();
1119 if (onlyVisitSSJavaLoop) {
1120 flatNodesToVisit.add(ssjavaLoopEntrance);
1122 flatNodesToVisit.add(fm);
1125 while (!flatNodesToVisit.isEmpty()) {
1126 FlatNode fn = flatNodesToVisit.iterator().next();
1127 flatNodesToVisit.remove(fn);
1130 computeSharedCoverSet_nodeActions(md, fn);
1132 for (int i = 0; i < fn.numNext(); i++) {
1133 FlatNode nn = fn.getNext(i);
1135 if (!visited.contains(nn)) {
1136 if (!onlyVisitSSJavaLoop || (onlyVisitSSJavaLoop && loopIncElements.contains(nn))) {
1137 flatNodesToVisit.add(nn);
1147 private void computeSharedCoverSet_nodeActions(MethodDescriptor md, FlatNode fn) {
1150 FieldDescriptor fld;
1152 switch (fn.kind()) {
1154 case FKind.FlatLiteralNode: {
1155 FlatLiteralNode fln = (FlatLiteralNode) fn;
1158 if (lhs.getType().isPrimitive() && !lhs.getSymbol().startsWith("neverused")
1159 && !lhs.getSymbol().startsWith("srctmp")) {
1160 // only need to care about composite location case here
1161 if (lhs.getType().getExtension() instanceof SSJavaType) {
1162 CompositeLocation compLoc = ((SSJavaType) lhs.getType().getExtension()).getCompLoc();
1163 Location lastLocElement = compLoc.get(compLoc.getSize() - 1);
1164 // check if the last one is shared loc
1165 if (ssjava.isSharedLocation(lastLocElement)) {
1166 addSharedLocDescriptor(lastLocElement, lhs);
1174 case FKind.FlatOpNode: {
1175 FlatOpNode fon = (FlatOpNode) fn;
1176 // for a normal assign node, need to propagate lhs's location path to
1178 if (fon.getOp().getOp() == Operation.ASSIGN) {
1179 rhs = fon.getLeft();
1180 lhs = fon.getDest();
1182 if (lhs.getType().isPrimitive() && !lhs.getSymbol().startsWith("neverused")
1183 && !lhs.getSymbol().startsWith("srctmp")) {
1185 System.out.println("FN=" + fn);
1186 NTuple<Location> loc = deriveLocationTuple(md, rhs);
1187 System.out.println("LOC TUPLE=" + loc);
1189 addDescriptorToSharedLocMayWriteSet(loc, lhs);
1191 // // only need to care about composite location case here
1192 // if (lhs.getType().getExtension() instanceof SSJavaType) {
1193 // CompositeLocation compLoc = ((SSJavaType)
1194 // lhs.getType().getExtension()).getCompLoc();
1195 // Location lastLocElement = compLoc.get(compLoc.getSize() - 1);
1196 // // check if the last one is shared loc
1197 // if (ssjava.isSharedLocation(lastLocElement)) {
1198 // addSharedLocDescriptor(lastLocElement, lhs);
1207 case FKind.FlatSetFieldNode:
1208 case FKind.FlatSetElementNode: {
1212 if (fn.kind() == FKind.FlatSetFieldNode) {
1213 FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
1214 lhs = fsfn.getDst();
1215 fld = fsfn.getField();
1216 rhs = fsfn.getSrc();
1218 FlatSetElementNode fsen = (FlatSetElementNode) fn;
1219 lhs = fsen.getDst();
1220 rhs = fsen.getSrc();
1221 TypeDescriptor td = lhs.getType().dereference();
1222 fld = getArrayField(td);
1225 Location fieldLocation = (Location) fld.getType().getExtension();
1226 if (ssjava.isSharedLocation(fieldLocation)) {
1227 addSharedLocDescriptor(fieldLocation, fld);
1229 System.out.println("FIELD WRITE FN=" + fn);
1230 NTuple<Location> locTuple = deriveLocationTuple(md, lhs);
1231 locTuple.addAll(deriveLocationTuple(md, fld));
1232 System.out.println("LOC TUPLE=" + locTuple);
1233 addDescriptorToSharedLocMayWriteSet(locTuple, fld);
1240 case FKind.FlatElementNode:
1241 case FKind.FlatFieldNode: {
1245 if (fn.kind() == FKind.FlatFieldNode) {
1246 FlatFieldNode ffn = (FlatFieldNode) fn;
1249 fld = ffn.getField();
1251 FlatElementNode fen = (FlatElementNode) fn;
1254 TypeDescriptor td = rhs.getType().dereference();
1255 fld = getArrayField(td);
1258 if (fld.isFinal()) {
1259 // if field is final no need to check
1263 System.out.println("FN=" + fn);
1264 NTuple<Location> locTuple = deriveLocationTuple(md, rhs);
1265 locTuple.addAll(deriveLocationTuple(md, fld));
1266 System.out.println("LOC TUPLE=" + locTuple);
1267 mapDescriptorToComposteLocation.put(lhs, locTuple);
1268 System.out.println("mapping " + lhs + " to " + locTuple);
1276 private void addDescriptorToSharedLocMayWriteSet(NTuple<Location> locTuple, Descriptor d) {
1278 Set<Descriptor> mayWriteSet = mapSharedLocationTupleToMayWriteSet.get(locTuple);
1279 if (mayWriteSet == null) {
1280 mayWriteSet = new HashSet<Descriptor>();
1281 mapSharedLocationTupleToMayWriteSet.put(locTuple, mayWriteSet);
1287 private void addSharedLocDescriptor(Location sharedLoc, Descriptor desc) {
1289 Set<Descriptor> descSet = mapSharedLocationToCoverSet.get(sharedLoc);
1290 if (descSet == null) {
1291 descSet = new HashSet<Descriptor>();
1292 mapSharedLocationToCoverSet.put(sharedLoc, descSet);
1295 System.out.println("add " + desc + " to shared loc" + sharedLoc);
1300 private void mergeReadLocationAnaylsis(ReadSummary curr, Set<ReadSummary> inSet) {
1302 if (inSet.size() == 0) {
1306 for (Iterator inIterator = inSet.iterator(); inIterator.hasNext();) {
1307 ReadSummary inSummary = (ReadSummary) inIterator.next();
1308 curr.merge(inSummary);
1313 private boolean hasReadingEffectOnSharedLocation(MethodDescriptor md, NTuple<Descriptor> hp,
1314 Location loc, Descriptor d) {
1316 ReadSummary summary = mapMethodDescriptorToReadSummary.get(md);
1318 if (summary != null) {
1319 Hashtable<Location, Set<Descriptor>> map = summary.get(hp);
1321 Set<Descriptor> descSec = map.get(loc);
1322 if (descSec != null) {
1323 return descSec.contains(d);
1331 private Location getLocation(Descriptor d) {
1333 System.out.println("GETLOCATION d=" + d + " d=" + d.getClass());
1335 if (d instanceof FieldDescriptor) {
1336 TypeExtension te = ((FieldDescriptor) d).getType().getExtension();
1338 return (Location) te;
1341 assert d instanceof TempDescriptor;
1342 TempDescriptor td = (TempDescriptor) d;
1344 TypeExtension te = td.getType().getExtension();
1346 if (te instanceof SSJavaType) {
1347 SSJavaType ssType = (SSJavaType) te;
1348 CompositeLocation comp = ssType.getCompLoc();
1349 return comp.get(comp.getSize() - 1);
1351 return (Location) te;
1356 return mapDescToLocation.get(d);
1359 private void writeLocation(MethodDescriptor md, ClearingSummary curr, NTuple<Descriptor> hp,
1360 Location loc, Descriptor d) {
1362 SharedStatus state = getState(curr, hp);
1363 if (loc != null && hasReadingEffectOnSharedLocation(md, hp, loc, d)) {
1364 // 1. add field x to the clearing set
1366 state.addVar(loc, d);
1368 // 3. if the set v contains all of variables belonging to the shared
1369 // location, set flag to true
1370 if (isOverWrittenAllDescsOfSharedLoc(md, hp, loc, state.getVarSet(loc))) {
1371 state.updateFlag(loc, true);
1374 state.setWriteEffect(loc);
1378 private boolean isOverWrittenAllDescsOfSharedLoc(MethodDescriptor md, NTuple<Descriptor> hp,
1379 Location loc, Set<Descriptor> writtenSet) {
1381 ReadSummary summary = mapMethodDescriptorToReadSummary.get(md);
1383 if (summary != null) {
1384 Hashtable<Location, Set<Descriptor>> map = summary.get(hp);
1386 Set<Descriptor> descSet = map.get(loc);
1387 if (descSet != null) {
1388 return writtenSet.containsAll(descSet);
1395 private void readLocation(MethodDescriptor md, ClearingSummary curr, NTuple<Descriptor> hp,
1396 Location loc, Descriptor d) {
1397 // remove reading var x from written set
1398 if (loc != null && hasReadingEffectOnSharedLocation(md, hp, loc, d)) {
1399 SharedStatus state = getState(curr, hp);
1400 state.removeVar(loc, d);
1404 private SharedStatus getState(ClearingSummary curr, NTuple<Descriptor> hp) {
1405 SharedStatus state = curr.get(hp);
1406 if (state == null) {
1407 state = new SharedStatus();
1408 curr.put(hp, state);
1413 private void eventLoopAnalysis() {
1414 // perform second stage analysis: intraprocedural analysis ensure that
1416 // variables are definitely written in-between the same read
1418 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
1419 flatNodesToVisit.add(ssjavaLoopEntrance);
1421 while (!flatNodesToVisit.isEmpty()) {
1422 FlatNode fn = (FlatNode) flatNodesToVisit.iterator().next();
1423 flatNodesToVisit.remove(fn);
1425 Hashtable<NTuple<Descriptor>, Set<WriteAge>> prev = mapFlatNodetoEventLoopMap.get(fn);
1427 Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr =
1428 new Hashtable<NTuple<Descriptor>, Set<WriteAge>>();
1429 for (int i = 0; i < fn.numPrev(); i++) {
1430 FlatNode nn = fn.getPrev(i);
1431 Hashtable<NTuple<Descriptor>, Set<WriteAge>> in = mapFlatNodetoEventLoopMap.get(nn);
1437 eventLoopAnalysis_nodeAction(fn, curr, ssjavaLoopEntrance);
1439 // if a new result, schedule forward nodes for analysis
1440 if (!curr.equals(prev)) {
1441 mapFlatNodetoEventLoopMap.put(fn, curr);
1443 for (int i = 0; i < fn.numNext(); i++) {
1444 FlatNode nn = fn.getNext(i);
1445 if (loopIncElements.contains(nn)) {
1446 flatNodesToVisit.add(nn);
1454 private void union(Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr,
1455 Hashtable<NTuple<Descriptor>, Set<WriteAge>> in) {
1457 Set<NTuple<Descriptor>> inKeySet = in.keySet();
1458 for (Iterator iterator = inKeySet.iterator(); iterator.hasNext();) {
1459 NTuple<Descriptor> inKey = (NTuple<Descriptor>) iterator.next();
1460 Set<WriteAge> inSet = in.get(inKey);
1462 Set<WriteAge> currSet = curr.get(inKey);
1464 if (currSet == null) {
1465 currSet = new HashSet<WriteAge>();
1466 curr.put(inKey, currSet);
1468 currSet.addAll(inSet);
1473 private void eventLoopAnalysis_nodeAction(FlatNode fn,
1474 Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr, FlatNode loopEntrance) {
1476 Hashtable<NTuple<Descriptor>, Set<WriteAge>> readWriteKillSet =
1477 new Hashtable<NTuple<Descriptor>, Set<WriteAge>>();
1478 Hashtable<NTuple<Descriptor>, Set<WriteAge>> readWriteGenSet =
1479 new Hashtable<NTuple<Descriptor>, Set<WriteAge>>();
1481 if (fn.equals(loopEntrance)) {
1482 // it reaches loop entrance: changes all flag to true
1483 Set<NTuple<Descriptor>> keySet = curr.keySet();
1484 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1485 NTuple<Descriptor> key = (NTuple<Descriptor>) iterator.next();
1486 Set<WriteAge> writeAgeSet = curr.get(key);
1488 Set<WriteAge> incSet = new HashSet<WriteAge>();
1489 incSet.addAll(writeAgeSet);
1490 writeAgeSet.clear();
1492 for (Iterator iterator2 = incSet.iterator(); iterator2.hasNext();) {
1493 WriteAge writeAge = (WriteAge) iterator2.next();
1494 WriteAge newWriteAge = writeAge.copy();
1496 writeAgeSet.add(newWriteAge);
1500 // System.out.println("EVENT LOOP ENTRY=" + curr);
1505 FieldDescriptor fld;
1507 switch (fn.kind()) {
1509 case FKind.FlatOpNode: {
1510 FlatOpNode fon = (FlatOpNode) fn;
1511 lhs = fon.getDest();
1512 rhs = fon.getLeft();
1514 if (!lhs.getSymbol().startsWith("neverused")) {
1515 NTuple<Descriptor> rhsHeapPath = computePath(rhs);
1516 if (!rhs.getType().isImmutable()) {
1517 mapHeapPath.put(lhs, rhsHeapPath);
1520 // NTuple<Descriptor> lhsHeapPath = computePath(lhs);
1521 NTuple<Descriptor> path = new NTuple<Descriptor>();
1524 // System.out.println("WRITE VARIABLE=" + path + " from=" + lhs);
1526 computeKILLSetForWrite(curr, path, readWriteKillSet);
1527 computeGENSetForWrite(path, readWriteGenSet);
1529 // System.out.println("#VARIABLE WRITE:" + fn);
1530 // System.out.println("#KILLSET=" + KILLSet);
1531 // System.out.println("#GENSet=" + GENSet);
1539 case FKind.FlatFieldNode:
1540 case FKind.FlatElementNode: {
1542 if (fn.kind() == FKind.FlatFieldNode) {
1543 FlatFieldNode ffn = (FlatFieldNode) fn;
1546 fld = ffn.getField();
1548 FlatElementNode fen = (FlatElementNode) fn;
1551 TypeDescriptor td = rhs.getType().dereference();
1552 fld = getArrayField(td);
1556 NTuple<Descriptor> srcHeapPath = mapHeapPath.get(rhs);
1557 NTuple<Descriptor> fldHeapPath;
1558 if (srcHeapPath != null) {
1559 fldHeapPath = new NTuple<Descriptor>(srcHeapPath.getList());
1561 // if srcHeapPath is null, it is static reference
1562 fldHeapPath = new NTuple<Descriptor>();
1563 fldHeapPath.add(rhs);
1565 fldHeapPath.add(fld);
1567 Set<WriteAge> writeAgeSet = curr.get(fldHeapPath);
1568 checkWriteAgeSet(writeAgeSet, fldHeapPath, fn);
1573 case FKind.FlatSetFieldNode:
1574 case FKind.FlatSetElementNode: {
1576 if (fn.kind() == FKind.FlatSetFieldNode) {
1577 FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
1578 lhs = fsfn.getDst();
1579 fld = fsfn.getField();
1581 FlatSetElementNode fsen = (FlatSetElementNode) fn;
1582 lhs = fsen.getDst();
1583 rhs = fsen.getSrc();
1584 TypeDescriptor td = lhs.getType().dereference();
1585 fld = getArrayField(td);
1589 NTuple<Descriptor> lhsHeapPath = computePath(lhs);
1590 NTuple<Descriptor> fldHeapPath = new NTuple<Descriptor>(lhsHeapPath.getList());
1591 fldHeapPath.add(fld);
1593 computeKILLSetForWrite(curr, fldHeapPath, readWriteKillSet);
1594 computeGENSetForWrite(fldHeapPath, readWriteGenSet);
1596 // System.out.println("FIELD WRITE:" + fn);
1597 // System.out.println("KILLSET=" + KILLSet);
1598 // System.out.println("GENSet=" + GENSet);
1603 case FKind.FlatCall: {
1604 FlatCall fc = (FlatCall) fn;
1606 generateKILLSetForFlatCall(fc, curr, readWriteKillSet);
1607 generateGENSetForFlatCall(fc, readWriteGenSet);
1609 // System.out.println("FLATCALL:" + fn);
1610 // System.out.println("KILLSET=" + KILLSet);
1611 // System.out.println("GENSet=" + GENSet);
1618 computeNewMapping(curr, readWriteKillSet, readWriteGenSet);
1619 // System.out.println("#######" + curr);
1625 private void checkWriteAgeSet(Set<WriteAge> writeAgeSet, NTuple<Descriptor> path, FlatNode fn) {
1626 if (writeAgeSet != null) {
1627 for (Iterator iterator = writeAgeSet.iterator(); iterator.hasNext();) {
1628 WriteAge writeAge = (WriteAge) iterator.next();
1629 if (writeAge.getAge() >= MAXAGE) {
1631 "Memory location, which is reachable through references "
1633 + ", who comes back to the same read statement without being overwritten at the out-most iteration at "
1634 + methodContainingSSJavaLoop.getClassDesc().getSourceFileName() + "::"
1641 private void generateGENSetForFlatCall(FlatCall fc,
1642 Hashtable<NTuple<Descriptor>, Set<WriteAge>> GENSet) {
1644 Set<NTuple<Descriptor>> boundMayWriteSet = mapFlatNodeToBoundMayWriteSet.get(fc);
1646 for (Iterator iterator = boundMayWriteSet.iterator(); iterator.hasNext();) {
1647 NTuple<Descriptor> key = (NTuple<Descriptor>) iterator.next();
1648 // TODO: shared location
1649 Set<WriteAge> set = new HashSet<WriteAge>();
1650 set.add(new WriteAge(0));
1651 GENSet.put(key, set);
1656 private void generateKILLSetForFlatCall(FlatCall fc,
1657 Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr,
1658 Hashtable<NTuple<Descriptor>, Set<WriteAge>> KILLSet) {
1660 Set<NTuple<Descriptor>> boundMustWriteSet = mapFlatNodeToBoundMustWriteSet.get(fc);
1662 for (Iterator iterator = boundMustWriteSet.iterator(); iterator.hasNext();) {
1663 NTuple<Descriptor> key = (NTuple<Descriptor>) iterator.next();
1664 // TODO: shared location
1665 if (curr.get(key) != null) {
1666 KILLSet.put(key, curr.get(key));
1672 private void computeNewMapping(SharedLocMappingSet curr, SharedLocMappingSet KILLSet,
1673 SharedLocMappingSet GENSet) {
1678 private void computeNewMapping(Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr,
1679 Hashtable<NTuple<Descriptor>, Set<WriteAge>> KILLSet,
1680 Hashtable<NTuple<Descriptor>, Set<WriteAge>> GENSet) {
1682 for (Enumeration<NTuple<Descriptor>> e = KILLSet.keys(); e.hasMoreElements();) {
1683 NTuple<Descriptor> key = e.nextElement();
1685 Set<WriteAge> writeAgeSet = curr.get(key);
1686 if (writeAgeSet == null) {
1687 writeAgeSet = new HashSet<WriteAge>();
1688 curr.put(key, writeAgeSet);
1690 writeAgeSet.removeAll(KILLSet.get(key));
1693 for (Enumeration<NTuple<Descriptor>> e = GENSet.keys(); e.hasMoreElements();) {
1694 NTuple<Descriptor> key = e.nextElement();
1695 curr.put(key, GENSet.get(key));
1700 private void computeGENSetForWrite(NTuple<Descriptor> fldHeapPath,
1701 Hashtable<NTuple<Descriptor>, Set<WriteAge>> GENSet) {
1703 // generate write age 0 for the field being written to
1704 Set<WriteAge> writeAgeSet = new HashSet<WriteAge>();
1705 writeAgeSet.add(new WriteAge(0));
1706 GENSet.put(fldHeapPath, writeAgeSet);
1710 private void readValue(FlatNode fn, NTuple<Descriptor> hp,
1711 Hashtable<NTuple<Descriptor>, Hashtable<FlatNode, Boolean>> curr) {
1712 Hashtable<FlatNode, Boolean> gen = curr.get(hp);
1714 gen = new Hashtable<FlatNode, Boolean>();
1717 Boolean currentStatus = gen.get(fn);
1718 if (currentStatus == null) {
1719 gen.put(fn, Boolean.FALSE);
1721 checkFlag(currentStatus.booleanValue(), fn, hp);
1726 private void computeKILLSetForWrite(Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr,
1727 NTuple<Descriptor> hp, Hashtable<NTuple<Descriptor>, Set<WriteAge>> KILLSet) {
1729 // removes all of heap path that starts with prefix 'hp'
1730 // since any reference overwrite along heap path gives overwriting side
1731 // effects on the value
1733 Set<NTuple<Descriptor>> keySet = curr.keySet();
1734 for (Iterator<NTuple<Descriptor>> iter = keySet.iterator(); iter.hasNext();) {
1735 NTuple<Descriptor> key = iter.next();
1736 if (key.startsWith(hp)) {
1737 KILLSet.put(key, curr.get(key));
1743 private void bindHeapPathCallerArgWithCaleeParam(FlatCall fc) {
1744 // compute all possible callee set
1745 // transform all READ/WRITE set from the any possible
1746 // callees to the caller
1747 calleeUnionBoundReadSet.clear();
1748 calleeIntersectBoundMustWriteSet.clear();
1749 calleeUnionBoundMayWriteSet.clear();
1751 if (ssjava.isSSJavaUtil(fc.getMethod().getClassDesc())) {
1752 // ssjava util case!
1753 // have write effects on the first argument
1754 TempDescriptor arg = fc.getArg(0);
1755 NTuple<Descriptor> argHeapPath = computePath(arg);
1756 calleeIntersectBoundMustWriteSet.add(argHeapPath);
1757 calleeUnionBoundMayWriteSet.add(argHeapPath);
1759 MethodDescriptor mdCallee = fc.getMethod();
1760 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
1761 setPossibleCallees.addAll(callGraph.getMethods(mdCallee));
1763 // create mapping from arg idx to its heap paths
1764 Hashtable<Integer, NTuple<Descriptor>> mapArgIdx2CallerArgHeapPath =
1765 new Hashtable<Integer, NTuple<Descriptor>>();
1767 // arg idx is starting from 'this' arg
1768 if (fc.getThis() != null) {
1769 NTuple<Descriptor> thisHeapPath = mapHeapPath.get(fc.getThis());
1770 if (thisHeapPath == null) {
1771 // method is called without creating new flat node representing 'this'
1772 thisHeapPath = new NTuple<Descriptor>();
1773 thisHeapPath.add(fc.getThis());
1776 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(0), thisHeapPath);
1779 for (int i = 0; i < fc.numArgs(); i++) {
1780 TempDescriptor arg = fc.getArg(i);
1781 NTuple<Descriptor> argHeapPath = computePath(arg);
1782 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(i + 1), argHeapPath);
1785 for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
1786 MethodDescriptor callee = (MethodDescriptor) iterator.next();
1787 FlatMethod calleeFlatMethod = state.getMethodFlat(callee);
1789 // binding caller's args and callee's params
1791 Set<NTuple<Descriptor>> calleeReadSet = mapFlatMethodToReadSet.get(calleeFlatMethod);
1792 if (calleeReadSet == null) {
1793 calleeReadSet = new HashSet<NTuple<Descriptor>>();
1794 mapFlatMethodToReadSet.put(calleeFlatMethod, calleeReadSet);
1797 Set<NTuple<Descriptor>> calleeMustWriteSet =
1798 mapFlatMethodToMustWriteSet.get(calleeFlatMethod);
1800 if (calleeMustWriteSet == null) {
1801 calleeMustWriteSet = new HashSet<NTuple<Descriptor>>();
1802 mapFlatMethodToMustWriteSet.put(calleeFlatMethod, calleeMustWriteSet);
1805 Set<NTuple<Descriptor>> calleeMayWriteSet =
1806 mapFlatMethodToMayWriteSet.get(calleeFlatMethod);
1808 if (calleeMayWriteSet == null) {
1809 calleeMayWriteSet = new HashSet<NTuple<Descriptor>>();
1810 mapFlatMethodToMayWriteSet.put(calleeFlatMethod, calleeMayWriteSet);
1813 Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc =
1814 new Hashtable<Integer, TempDescriptor>();
1816 if (calleeFlatMethod.getMethod().isStatic()) {
1817 // static method does not have implicit 'this' arg
1820 for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
1821 TempDescriptor param = calleeFlatMethod.getParameter(i);
1822 mapParamIdx2ParamTempDesc.put(Integer.valueOf(i + offset), param);
1825 Set<NTuple<Descriptor>> calleeBoundReadSet =
1826 bindSet(calleeReadSet, mapParamIdx2ParamTempDesc, mapArgIdx2CallerArgHeapPath);
1827 // union of the current read set and the current callee's
1829 calleeUnionBoundReadSet.addAll(calleeBoundReadSet);
1831 Set<NTuple<Descriptor>> calleeBoundMustWriteSet =
1832 bindSet(calleeMustWriteSet, mapParamIdx2ParamTempDesc, mapArgIdx2CallerArgHeapPath);
1833 // intersection of the current overwrite set and the current
1836 merge(calleeIntersectBoundMustWriteSet, calleeBoundMustWriteSet);
1838 Set<NTuple<Descriptor>> boundWriteSetFromCallee =
1839 bindSet(calleeMayWriteSet, mapParamIdx2ParamTempDesc, mapArgIdx2CallerArgHeapPath);
1840 calleeUnionBoundMayWriteSet.addAll(boundWriteSetFromCallee);
1847 private void bindHeapPathCallerArgWithCaleeParamForSharedLoc(FlatCall fc) {
1848 // compute all possible callee set
1849 // transform all DELETE set from the any possible
1850 // callees to the caller
1851 calleeUnionBoundDeleteSet.clear();
1852 calleeIntersectBoundSharedSet.clear();
1854 MethodDescriptor mdCallee = fc.getMethod();
1855 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
1856 setPossibleCallees.addAll(callGraph.getMethods(mdCallee));
1858 // create mapping from arg idx to its heap paths
1859 Hashtable<Integer, NTuple<Descriptor>> mapArgIdx2CallerArgHeapPath =
1860 new Hashtable<Integer, NTuple<Descriptor>>();
1862 // arg idx is starting from 'this' arg
1863 if (fc.getThis() != null) {
1864 NTuple<Descriptor> thisHeapPath = mapHeapPath.get(fc.getThis());
1865 if (thisHeapPath == null) {
1866 // method is called without creating new flat node representing 'this'
1867 thisHeapPath = new NTuple<Descriptor>();
1868 thisHeapPath.add(fc.getThis());
1871 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(0), thisHeapPath);
1874 for (int i = 0; i < fc.numArgs(); i++) {
1875 TempDescriptor arg = fc.getArg(i);
1876 NTuple<Descriptor> argHeapPath = computePath(arg);
1877 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(i + 1), argHeapPath);
1880 for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
1881 MethodDescriptor callee = (MethodDescriptor) iterator.next();
1882 FlatMethod calleeFlatMethod = state.getMethodFlat(callee);
1884 // binding caller's args and callee's params
1886 Set<NTuple<Descriptor>> calleeReadSet = mapFlatMethodToDeleteSet.get(calleeFlatMethod);
1887 if (calleeReadSet == null) {
1888 calleeReadSet = new HashSet<NTuple<Descriptor>>();
1889 mapFlatMethodToDeleteSet.put(calleeFlatMethod, calleeReadSet);
1892 Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc =
1893 new Hashtable<Integer, TempDescriptor>();
1895 if (calleeFlatMethod.getMethod().isStatic()) {
1896 // static method does not have implicit 'this' arg
1899 for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
1900 TempDescriptor param = calleeFlatMethod.getParameter(i);
1901 mapParamIdx2ParamTempDesc.put(Integer.valueOf(i + offset), param);
1904 Set<NTuple<Descriptor>> calleeBoundDeleteSet =
1905 bindSet(calleeReadSet, mapParamIdx2ParamTempDesc, mapArgIdx2CallerArgHeapPath);
1906 // union of the current read set and the current callee's
1908 calleeUnionBoundDeleteSet.addAll(calleeBoundDeleteSet);
1910 SharedLocMappingSet calleeSharedLocMap =
1911 mapFlatMethodToSharedLocMappingSet.get(calleeFlatMethod);
1913 Set<NTuple<Descriptor>> calleeHeapPathKeySet = calleeSharedLocMap.getHeapPathKeySet();
1915 for (Iterator iterator2 = calleeHeapPathKeySet.iterator(); iterator2.hasNext();) {
1916 NTuple<Descriptor> calleeHeapPathKey = (NTuple<Descriptor>) iterator2.next();
1918 NTuple<Descriptor> calleeBoundHeapPathKey =
1919 bind(calleeHeapPathKey, mapParamIdx2ParamTempDesc, mapArgIdx2CallerArgHeapPath);
1921 Set<Location> calleeLocSet = calleeSharedLocMap.getLocationKeySet(calleeHeapPathKey);
1923 for (Iterator iterator3 = calleeLocSet.iterator(); iterator3.hasNext();) {
1924 Location calleeLocKey = (Location) iterator3.next();
1925 Set<Descriptor> calleeWriteSet =
1926 calleeSharedLocMap.getWriteSet(calleeHeapPathKey, calleeLocKey);
1928 calleeIntersectBoundSharedSet.intersectWriteSet(calleeBoundHeapPathKey, calleeLocKey,
1939 private NTuple<Descriptor> bind(NTuple<Descriptor> calleeHeapPathKey,
1940 Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc,
1941 Hashtable<Integer, NTuple<Descriptor>> mapCallerArgIdx2HeapPath) {
1943 Set<Integer> keySet = mapCallerArgIdx2HeapPath.keySet();
1944 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1945 Integer idx = (Integer) iterator.next();
1946 NTuple<Descriptor> callerArgHeapPath = mapCallerArgIdx2HeapPath.get(idx);
1947 TempDescriptor calleeParam = mapParamIdx2ParamTempDesc.get(idx);
1948 if (calleeHeapPathKey.startsWith(calleeParam)) {
1949 NTuple<Descriptor> boundElement = combine(callerArgHeapPath, calleeHeapPathKey);
1950 return boundElement;
1956 private void checkFlag(boolean booleanValue, FlatNode fn, NTuple<Descriptor> hp) {
1958 // the definitely written analysis only takes care about locations that
1959 // are written to inside of the SSJava loop
1960 for (Iterator iterator = calleeUnionBoundMayWriteSet.iterator(); iterator.hasNext();) {
1961 NTuple<Descriptor> write = (NTuple<Descriptor>) iterator.next();
1962 if (hp.startsWith(write)) {
1963 // it has write effect!
1967 + "There is a variable, which is reachable through references "
1969 + ", who comes back to the same read statement without being overwritten at the out-most iteration at "
1970 + methodContainingSSJavaLoop.getClassDesc().getSourceFileName() + "::"
1978 private void initialize() {
1979 // First, identify ssjava loop entrace
1981 // no need to analyze method having ssjava loop
1982 methodContainingSSJavaLoop = ssjava.getMethodContainingSSJavaLoop();
1984 FlatMethod fm = state.getMethodFlat(methodContainingSSJavaLoop);
1985 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
1986 flatNodesToVisit.add(fm);
1988 LoopFinder loopFinder = new LoopFinder(fm);
1990 while (!flatNodesToVisit.isEmpty()) {
1991 FlatNode fn = flatNodesToVisit.iterator().next();
1992 flatNodesToVisit.remove(fn);
1994 String label = (String) state.fn2labelMap.get(fn);
1995 if (label != null) {
1997 if (label.equals(ssjava.SSJAVA)) {
1998 ssjavaLoopEntrance = fn;
2003 for (int i = 0; i < fn.numNext(); i++) {
2004 FlatNode nn = fn.getNext(i);
2005 flatNodesToVisit.add(nn);
2009 assert ssjavaLoopEntrance != null;
2011 // assume that ssjava loop is top-level loop in method, not nested loop
2012 Set nestedLoop = loopFinder.nestedLoops();
2013 for (Iterator loopIter = nestedLoop.iterator(); loopIter.hasNext();) {
2014 LoopFinder lf = (LoopFinder) loopIter.next();
2015 if (lf.loopEntrances().iterator().next().equals(ssjavaLoopEntrance)) {
2020 assert ssjavaLoop != null;
2022 loopIncElements = (Set<FlatNode>) ssjavaLoop.loopIncElements();
2024 // perform topological sort over the set of methods accessed by the main
2026 Set<MethodDescriptor> methodDescriptorsToAnalyze = new HashSet<MethodDescriptor>();
2027 methodDescriptorsToAnalyze.addAll(ssjava.getAnnotationRequireSet());
2028 sortedDescriptors = topologicalSort(methodDescriptorsToAnalyze);
2031 private void methodReadWriteSetAnalysis() {
2032 // perform method READ/OVERWRITE analysis
2033 LinkedList<MethodDescriptor> descriptorListToAnalyze =
2034 (LinkedList<MethodDescriptor>) sortedDescriptors.clone();
2036 // current descriptors to visit in fixed-point interprocedural analysis,
2038 // dependency in the call graph
2039 methodDescriptorsToVisitStack.clear();
2041 descriptorListToAnalyze.removeFirst();
2043 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
2044 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
2046 while (!descriptorListToAnalyze.isEmpty()) {
2047 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
2048 methodDescriptorsToVisitStack.add(md);
2051 // analyze scheduled methods until there are no more to visit
2052 while (!methodDescriptorsToVisitStack.isEmpty()) {
2053 // start to analyze leaf node
2054 MethodDescriptor md = methodDescriptorsToVisitStack.pop();
2055 FlatMethod fm = state.getMethodFlat(md);
2057 Set<NTuple<Descriptor>> readSet = new HashSet<NTuple<Descriptor>>();
2058 Set<NTuple<Descriptor>> mustWriteSet = new HashSet<NTuple<Descriptor>>();
2059 Set<NTuple<Descriptor>> mayWriteSet = new HashSet<NTuple<Descriptor>>();
2060 SharedLocMappingSet sharedLocMapping = new SharedLocMappingSet();
2061 Set<NTuple<Descriptor>> deleteSet = new HashSet<NTuple<Descriptor>>();
2063 methodReadWriteSet_analyzeMethod(fm, readSet, mustWriteSet, mayWriteSet, sharedLocMapping,
2066 Set<NTuple<Descriptor>> prevRead = mapFlatMethodToReadSet.get(fm);
2067 Set<NTuple<Descriptor>> prevMustWrite = mapFlatMethodToMustWriteSet.get(fm);
2068 Set<NTuple<Descriptor>> prevMayWrite = mapFlatMethodToMayWriteSet.get(fm);
2069 SharedLocMappingSet prevSharedLocMapping = mapFlatMethodToSharedLocMappingSet.get(fm);
2070 Set<NTuple<Descriptor>> prevDeleteSet = mapFlatMethodToDeleteSet.get(fm);
2072 if (!(readSet.equals(prevRead) && mustWriteSet.equals(prevMustWrite)
2073 && mayWriteSet.equals(prevMayWrite) && sharedLocMapping.equals(prevSharedLocMapping) && deleteSet
2074 .equals(prevDeleteSet))) {
2075 mapFlatMethodToReadSet.put(fm, readSet);
2076 mapFlatMethodToMustWriteSet.put(fm, mustWriteSet);
2077 mapFlatMethodToMayWriteSet.put(fm, mayWriteSet);
2078 mapFlatMethodToSharedLocMappingSet.put(fm, sharedLocMapping);
2079 mapFlatMethodToDeleteSet.put(fm, deleteSet);
2081 // results for callee changed, so enqueue dependents caller for
2084 Iterator<MethodDescriptor> depsItr = getDependents(md).iterator();
2085 while (depsItr.hasNext()) {
2086 MethodDescriptor methodNext = depsItr.next();
2087 if (!methodDescriptorsToVisitStack.contains(methodNext)
2088 && methodDescriptorToVistSet.contains(methodNext)) {
2089 methodDescriptorsToVisitStack.add(methodNext);
2098 methodReadWriteSetAnalysisToEventLoopBody();
2102 private void methodReadWriteSet_analyzeMethod(FlatMethod fm, Set<NTuple<Descriptor>> readSet,
2103 Set<NTuple<Descriptor>> mustWriteSet, Set<NTuple<Descriptor>> mayWriteSet,
2104 SharedLocMappingSet sharedLocMapping, Set<NTuple<Descriptor>> deleteSet) {
2105 if (state.SSJAVADEBUG) {
2106 System.out.println("SSJAVA: Definitely written Analyzing: " + fm);
2109 methodReadWriteSet_analyzeBody(fm, readSet, mustWriteSet, mayWriteSet, sharedLocMapping,
2114 private void methodReadWriteSetAnalysisToEventLoopBody() {
2116 // perform method read/write analysis for Event Loop Body
2118 FlatMethod flatMethodContainingSSJavaLoop = state.getMethodFlat(methodContainingSSJavaLoop);
2120 if (state.SSJAVADEBUG) {
2121 System.out.println("SSJAVA: Definitely written Event Loop Analyzing: "
2122 + flatMethodContainingSSJavaLoop);
2125 Set<NTuple<Descriptor>> readSet = new HashSet<NTuple<Descriptor>>();
2126 Set<NTuple<Descriptor>> mustWriteSet = new HashSet<NTuple<Descriptor>>();
2127 Set<NTuple<Descriptor>> mayWriteSet = new HashSet<NTuple<Descriptor>>();
2128 SharedLocMappingSet sharedLocMapping = new SharedLocMappingSet();
2129 Set<NTuple<Descriptor>> deleteSet = new HashSet<NTuple<Descriptor>>();
2131 mapFlatMethodToReadSet.put(flatMethodContainingSSJavaLoop, readSet);
2132 mapFlatMethodToMustWriteSet.put(flatMethodContainingSSJavaLoop, mustWriteSet);
2133 mapFlatMethodToMayWriteSet.put(flatMethodContainingSSJavaLoop, mayWriteSet);
2134 mapFlatMethodToSharedLocMappingSet.put(flatMethodContainingSSJavaLoop, sharedLocMapping);
2135 mapFlatMethodToDeleteSet.put(flatMethodContainingSSJavaLoop, deleteSet);
2137 methodReadWriteSet_analyzeBody(ssjavaLoopEntrance, readSet, mustWriteSet, mayWriteSet,
2138 sharedLocMapping, deleteSet, true);
2142 private void methodReadWriteSet_analyzeBody(FlatNode startNode, Set<NTuple<Descriptor>> readSet,
2143 Set<NTuple<Descriptor>> mustWriteSet, Set<NTuple<Descriptor>> mayWriteSet,
2144 SharedLocMappingSet sharedLocMapping, Set<NTuple<Descriptor>> deleteSet,
2145 boolean isEventLoopBody) {
2147 // intraprocedural analysis
2148 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
2149 flatNodesToVisit.add(startNode);
2151 while (!flatNodesToVisit.isEmpty()) {
2152 FlatNode fn = flatNodesToVisit.iterator().next();
2153 flatNodesToVisit.remove(fn);
2155 SharedLocMappingSet currSharedLocMapping = new SharedLocMappingSet();
2156 Set<NTuple<Descriptor>> currMustWriteSet = new HashSet<NTuple<Descriptor>>();
2158 for (int i = 0; i < fn.numPrev(); i++) {
2159 FlatNode prevFn = fn.getPrev(i);
2160 Set<NTuple<Descriptor>> in = mapFlatNodeToMustWriteSet.get(prevFn);
2161 SharedLocMappingSet inSharedLoc = mapFlatNodeToSharedLocMapping.get(prevFn);
2163 merge(currMustWriteSet, in);
2164 merge(currSharedLocMapping, inSharedLoc);
2168 methodReadWriteSet_nodeActions(fn, currMustWriteSet, readSet, mustWriteSet, mayWriteSet,
2169 currSharedLocMapping, sharedLocMapping, deleteSet, isEventLoopBody);
2171 SharedLocMappingSet prevSharedLocSet = mapFlatNodeToSharedLocMapping.get(fn);
2172 Set<NTuple<Descriptor>> mustSetPrev = mapFlatNodeToMustWriteSet.get(fn);
2174 if ((!currMustWriteSet.equals(mustSetPrev))
2175 || (!currSharedLocMapping.equals(prevSharedLocSet))) {
2176 mapFlatNodeToMustWriteSet.put(fn, currMustWriteSet);
2177 mapFlatNodeToSharedLocMapping.put(fn, currSharedLocMapping);
2178 for (int i = 0; i < fn.numNext(); i++) {
2179 FlatNode nn = fn.getNext(i);
2180 if ((!isEventLoopBody) || loopIncElements.contains(nn)) {
2181 flatNodesToVisit.add(nn);
2191 private void methodReadWriteSet_nodeActions(FlatNode fn,
2192 Set<NTuple<Descriptor>> currMustWriteSet, Set<NTuple<Descriptor>> readSet,
2193 Set<NTuple<Descriptor>> mustWriteSet, Set<NTuple<Descriptor>> mayWriteSet,
2194 SharedLocMappingSet currSharedLocMapping, SharedLocMappingSet sharedLocMapping,
2195 Set<NTuple<Descriptor>> deleteSet, boolean isEventLoopBody) {
2197 SharedLocMappingSet killSetSharedLoc = new SharedLocMappingSet();
2198 SharedLocMappingSet genSetSharedLoc = new SharedLocMappingSet();
2202 FieldDescriptor fld;
2204 switch (fn.kind()) {
2205 case FKind.FlatMethod: {
2207 // set up initial heap paths for method parameters
2208 FlatMethod fm = (FlatMethod) fn;
2209 for (int i = 0; i < fm.numParameters(); i++) {
2210 TempDescriptor param = fm.getParameter(i);
2211 NTuple<Descriptor> heapPath = new NTuple<Descriptor>();
2212 heapPath.add(param);
2213 mapHeapPath.put(param, heapPath);
2218 case FKind.FlatOpNode: {
2219 FlatOpNode fon = (FlatOpNode) fn;
2220 // for a normal assign node, need to propagate lhs's heap path to
2222 if (fon.getOp().getOp() == Operation.ASSIGN) {
2223 rhs = fon.getLeft();
2224 lhs = fon.getDest();
2226 NTuple<Descriptor> rhsHeapPath = mapHeapPath.get(rhs);
2227 if (rhsHeapPath != null) {
2228 mapHeapPath.put(lhs, mapHeapPath.get(rhs));
2230 NTuple<Descriptor> heapPath = new NTuple<Descriptor>();
2232 mapHeapPath.put(lhs, heapPath);
2235 // shared loc extension
2236 if (isEventLoopBody) {
2237 if (!lhs.getSymbol().startsWith("neverused") && rhs.getType().isImmutable()) {
2239 if (rhs.getType().getExtension() instanceof Location
2240 && lhs.getType().getExtension() instanceof CompositeLocation) {
2242 Location rhsLoc = (Location) rhs.getType().getExtension();
2244 CompositeLocation lhsCompLoc = (CompositeLocation) lhs.getType().getExtension();
2245 Location dstLoc = lhsCompLoc.get(lhsCompLoc.getSize() - 1);
2247 NTuple<Descriptor> heapPath = new NTuple<Descriptor>();
2248 for (int i = 0; i < rhsHeapPath.size() - 1; i++) {
2249 heapPath.add(rhsHeapPath.get(i));
2252 NTuple<Descriptor> writeHeapPath = new NTuple<Descriptor>();
2253 writeHeapPath.addAll(heapPath);
2254 writeHeapPath.add(lhs);
2256 System.out.println("VAR WRITE:" + fn);
2257 System.out.println("LHS TYPE EXTENSION=" + lhs.getType().getExtension());
2258 System.out.println("RHS TYPE EXTENSION=" + rhs.getType().getExtension()
2259 + " HEAPPATH=" + rhsHeapPath);
2261 // computing gen/kill set
2262 computeKILLSetForWrite(currSharedLocMapping, heapPath, dstLoc, killSetSharedLoc);
2263 if (!dstLoc.equals(rhsLoc)) {
2264 computeGENSetForHigherWrite(currSharedLocMapping, heapPath, dstLoc, lhs,
2266 deleteSet.remove(writeHeapPath);
2268 computeGENSetForSharedWrite(currSharedLocMapping, heapPath, dstLoc, lhs,
2270 deleteSet.add(writeHeapPath);
2281 case FKind.FlatElementNode:
2282 case FKind.FlatFieldNode: {
2286 if (fn.kind() == FKind.FlatFieldNode) {
2287 FlatFieldNode ffn = (FlatFieldNode) fn;
2290 fld = ffn.getField();
2292 FlatElementNode fen = (FlatElementNode) fn;
2295 TypeDescriptor td = rhs.getType().dereference();
2296 fld = getArrayField(td);
2299 if (fld.isFinal()) {
2300 // if field is final no need to check
2305 NTuple<Descriptor> srcHeapPath = mapHeapPath.get(rhs);
2306 if (srcHeapPath != null) {
2307 // if lhs srcHeapPath is null, it means that it is not reachable from
2308 // callee's parameters. so just ignore it
2310 NTuple<Descriptor> readingHeapPath = new NTuple<Descriptor>(srcHeapPath.getList());
2311 readingHeapPath.add(fld);
2312 mapHeapPath.put(lhs, readingHeapPath);
2315 if (fld.getType().isImmutable()) {
2316 // if WT doesnot have hp(x.f), add hp(x.f) to READ
2317 if (!currMustWriteSet.contains(readingHeapPath)) {
2318 readSet.add(readingHeapPath);
2322 // no need to kill hp(x.f) from WT
2328 case FKind.FlatSetFieldNode:
2329 case FKind.FlatSetElementNode: {
2333 if (fn.kind() == FKind.FlatSetFieldNode) {
2334 SharedLocMappingSet killSet = new SharedLocMappingSet();
2335 SharedLocMappingSet genSet = new SharedLocMappingSet();
2336 FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
2337 lhs = fsfn.getDst();
2338 fld = fsfn.getField();
2339 rhs = fsfn.getSrc();
2341 FlatSetElementNode fsen = (FlatSetElementNode) fn;
2342 lhs = fsen.getDst();
2343 rhs = fsen.getSrc();
2344 TypeDescriptor td = lhs.getType().dereference();
2345 fld = getArrayField(td);
2349 NTuple<Descriptor> lhsHeapPath = mapHeapPath.get(lhs);
2351 if (lhsHeapPath != null) {
2352 // if lhs heap path is null, it means that it is not reachable from
2353 // callee's parameters. so just ignore it
2354 NTuple<Descriptor> fldHeapPath = new NTuple<Descriptor>(lhsHeapPath.getList());
2355 fldHeapPath.add(fld);
2356 mapHeapPath.put(fld, fldHeapPath);
2359 // need to add hp(y) to WT
2360 currMustWriteSet.add(fldHeapPath);
2361 mayWriteSet.add(fldHeapPath);
2363 // shared loc extension
2364 Location srcLoc = getLocation(rhs);
2365 Location fieldLoc = (Location) fld.getType().getExtension();
2366 if (ssjava.isSharedLocation(fieldLoc)) {
2367 // only care the case that loc(f) is shared location
2370 computeKILLSetForWrite(currSharedLocMapping, lhsHeapPath, fieldLoc, killSetSharedLoc);
2371 if (!fieldLoc.equals(srcLoc)) {
2372 computeGENSetForHigherWrite(currSharedLocMapping, lhsHeapPath, fieldLoc, fld,
2374 deleteSet.remove(fldHeapPath);
2376 computeGENSetForSharedWrite(currSharedLocMapping, lhsHeapPath, fieldLoc, fld,
2378 deleteSet.add(fldHeapPath);
2382 System.out.println("################");
2383 System.out.println("FIELD WRITE:" + fn);
2384 System.out.println("fieldLoc=" + fieldLoc + " srcLoc=" + srcLoc);
2385 System.out.println("KILLSET=" + killSetSharedLoc);
2386 System.out.println("GENSet=" + genSetSharedLoc);
2387 System.out.println("DELETESET=" + deleteSet);
2394 case FKind.FlatCall: {
2396 FlatCall fc = (FlatCall) fn;
2398 bindHeapPathCallerArgWithCaleeParam(fc);
2400 mapFlatNodeToBoundReadSet.put(fn, calleeUnionBoundReadSet);
2401 mapFlatNodeToBoundMustWriteSet.put(fn, calleeIntersectBoundMustWriteSet);
2402 mapFlatNodeToBoundMayWriteSet.put(fn, calleeUnionBoundMayWriteSet);
2404 // add heap path, which is an element of READ_bound set and is not
2406 // element of WT set, to the caller's READ set
2407 for (Iterator iterator = calleeUnionBoundReadSet.iterator(); iterator.hasNext();) {
2408 NTuple<Descriptor> read = (NTuple<Descriptor>) iterator.next();
2409 if (!currMustWriteSet.contains(read)) {
2414 // add heap path, which is an element of OVERWRITE_bound set, to the
2416 for (Iterator iterator = calleeIntersectBoundMustWriteSet.iterator(); iterator.hasNext();) {
2417 NTuple<Descriptor> write = (NTuple<Descriptor>) iterator.next();
2418 currMustWriteSet.add(write);
2421 // add heap path, which is an element of WRITE_BOUND set, to the
2422 // caller's writeSet
2423 for (Iterator iterator = calleeUnionBoundMayWriteSet.iterator(); iterator.hasNext();) {
2424 NTuple<Descriptor> write = (NTuple<Descriptor>) iterator.next();
2425 mayWriteSet.add(write);
2428 // shared loc extension
2429 bindHeapPathCallerArgWithCaleeParamForSharedLoc(fc);
2431 generateKILLSharedSetForFlatCall(currSharedLocMapping, killSetSharedLoc);
2432 generateGENSharedSetForFlatCall(currSharedLocMapping, genSetSharedLoc);
2434 System.out.println("### Analyzing FC=" + fc);
2435 System.out.println("### BOUNDSET=" + calleeIntersectBoundSharedSet);
2436 System.out.println("### GEN=" + genSetSharedLoc);
2437 System.out.println("### KILL=" + killSetSharedLoc);
2441 case FKind.FlatExit: {
2442 // merge the current written set with OVERWRITE set
2443 merge(mustWriteSet, currMustWriteSet);
2445 // shared loc extension
2446 merge(sharedLocMapping, currSharedLocMapping);
2452 computeNewMapping(currSharedLocMapping, killSetSharedLoc, genSetSharedLoc);
2456 private void generateGENSharedSetForFlatCall(SharedLocMappingSet currSharedLocMapping,
2457 SharedLocMappingSet genSetSharedLoc) {
2459 Set<NTuple<Descriptor>> hpKeySet = calleeIntersectBoundSharedSet.getHeapPathKeySet();
2460 for (Iterator iterator = hpKeySet.iterator(); iterator.hasNext();) {
2461 NTuple<Descriptor> hpKey = (NTuple<Descriptor>) iterator.next();
2462 Set<Location> locKeySet = calleeIntersectBoundSharedSet.getLocationKeySet(hpKey);
2463 for (Iterator iterator2 = locKeySet.iterator(); iterator2.hasNext();) {
2464 Location locKey = (Location) iterator2.next();
2466 Set<Descriptor> calleeBoundWriteSet =
2467 calleeIntersectBoundSharedSet.getWriteSet(hpKey, locKey);
2468 System.out.println("calleeBoundWriteSet=" + calleeBoundWriteSet + " hp=" + hpKey + " loc="
2470 Set<Descriptor> removeSet = computeRemoveSet(hpKey, locKey);
2472 Set<Descriptor> currWriteSet = currSharedLocMapping.getWriteSet(hpKey, locKey);
2474 genSetSharedLoc.addWriteSet(hpKey, locKey, currWriteSet);
2475 genSetSharedLoc.addWriteSet(hpKey, locKey, calleeBoundWriteSet);
2476 genSetSharedLoc.removeWriteSet(hpKey, locKey, removeSet);
2483 public NTuple<Descriptor> getPrefix(NTuple<Descriptor> in) {
2484 return in.subList(0, in.size() - 1);
2487 public NTuple<Descriptor> getSuffix(NTuple<Descriptor> in) {
2488 return in.subList(in.size() - 1, in.size());
2491 private Set<Descriptor> computeRemoveSet(NTuple<Descriptor> hpKey, Location locKey) {
2492 Set<Descriptor> removeSet = new HashSet<Descriptor>();
2494 for (Iterator iterator = calleeUnionBoundDeleteSet.iterator(); iterator.hasNext();) {
2495 NTuple<Descriptor> removeHeapPath = (NTuple<Descriptor>) iterator.next();
2496 if (getPrefix(removeHeapPath).equals(hpKey)) {
2497 removeSet.add(getSuffix(removeHeapPath).get(0));
2504 private void generateKILLSharedSetForFlatCall(SharedLocMappingSet currSharedLocMapping,
2505 SharedLocMappingSet killSetSharedLoc) {
2507 Set<NTuple<Descriptor>> hpKeySet = calleeIntersectBoundSharedSet.getHeapPathKeySet();
2508 for (Iterator iterator = hpKeySet.iterator(); iterator.hasNext();) {
2509 NTuple<Descriptor> hpKey = (NTuple<Descriptor>) iterator.next();
2510 Set<Location> locKeySet = calleeIntersectBoundSharedSet.getLocationKeySet(hpKey);
2511 for (Iterator iterator2 = locKeySet.iterator(); iterator2.hasNext();) {
2512 Location locKey = (Location) iterator2.next();
2513 Set<Descriptor> currWriteSet = currSharedLocMapping.getWriteSet(hpKey, locKey);
2514 killSetSharedLoc.addWriteSet(hpKey, locKey, currWriteSet);
2519 static public FieldDescriptor getArrayField(TypeDescriptor td) {
2520 FieldDescriptor fd = mapTypeToArrayField.get(td);
2523 new FieldDescriptor(new Modifiers(Modifiers.PUBLIC), td, arrayElementFieldName, null,
2525 mapTypeToArrayField.put(td, fd);
2530 private void mergeSharedLocationAnaylsis(ClearingSummary curr, Set<ClearingSummary> inSet) {
2531 if (inSet.size() == 0) {
2534 Hashtable<Pair<NTuple<Descriptor>, Location>, Boolean> mapHeapPathLoc2Flag =
2535 new Hashtable<Pair<NTuple<Descriptor>, Location>, Boolean>();
2537 for (Iterator inIterator = inSet.iterator(); inIterator.hasNext();) {
2539 ClearingSummary inTable = (ClearingSummary) inIterator.next();
2541 Set<NTuple<Descriptor>> keySet = inTable.keySet();
2543 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2544 NTuple<Descriptor> hpKey = (NTuple<Descriptor>) iterator.next();
2545 SharedStatus inState = inTable.get(hpKey);
2546 SharedStatus currState = curr.get(hpKey);
2547 if (currState == null) {
2548 currState = new SharedStatus();
2549 curr.put(hpKey, currState);
2552 currState.merge(inState);
2554 Set<Location> locSet = inState.getMap().keySet();
2555 for (Iterator iterator2 = locSet.iterator(); iterator2.hasNext();) {
2556 Location loc = (Location) iterator2.next();
2557 Pair<Set<Descriptor>, Boolean> pair = inState.getMap().get(loc);
2558 boolean inFlag = pair.getSecond().booleanValue();
2560 Pair<NTuple<Descriptor>, Location> flagKey =
2561 new Pair<NTuple<Descriptor>, Location>(hpKey, loc);
2562 Boolean current = mapHeapPathLoc2Flag.get(flagKey);
2563 if (current == null) {
2564 current = new Boolean(true);
2566 boolean newInFlag = current.booleanValue() & inFlag;
2567 mapHeapPathLoc2Flag.put(flagKey, Boolean.valueOf(newInFlag));
2574 // merge flag status
2575 Set<NTuple<Descriptor>> hpKeySet = curr.keySet();
2576 for (Iterator iterator = hpKeySet.iterator(); iterator.hasNext();) {
2577 NTuple<Descriptor> hpKey = (NTuple<Descriptor>) iterator.next();
2578 SharedStatus currState = curr.get(hpKey);
2579 Set<Location> locKeySet = currState.getMap().keySet();
2580 for (Iterator iterator2 = locKeySet.iterator(); iterator2.hasNext();) {
2581 Location locKey = (Location) iterator2.next();
2582 Pair<Set<Descriptor>, Boolean> pair = currState.getMap().get(locKey);
2583 boolean currentFlag = pair.getSecond().booleanValue();
2584 Boolean inFlag = mapHeapPathLoc2Flag.get(new Pair(hpKey, locKey));
2585 if (inFlag != null) {
2586 boolean newFlag = currentFlag | inFlag.booleanValue();
2587 if (currentFlag != newFlag) {
2588 currState.getMap().put(locKey, new Pair(pair.getFirst(), new Boolean(newFlag)));
2596 private void merge(Set<NTuple<Descriptor>> curr, Set<NTuple<Descriptor>> in) {
2597 if (curr.isEmpty()) {
2598 // set has a special initial value which covers all possible
2600 // For the first time of intersection, we can take all previous set
2603 // otherwise, current set is the intersection of the two sets
2609 // combine two heap path
2610 private NTuple<Descriptor> combine(NTuple<Descriptor> callerIn, NTuple<Descriptor> calleeIn) {
2611 NTuple<Descriptor> combined = new NTuple<Descriptor>();
2613 for (int i = 0; i < callerIn.size(); i++) {
2614 combined.add(callerIn.get(i));
2617 // the first element of callee's heap path represents parameter
2618 // so we skip the first one since it is already added from caller's heap
2620 for (int i = 1; i < calleeIn.size(); i++) {
2621 combined.add(calleeIn.get(i));
2627 private Set<NTuple<Descriptor>> bindSet(Set<NTuple<Descriptor>> calleeSet,
2628 Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc,
2629 Hashtable<Integer, NTuple<Descriptor>> mapCallerArgIdx2HeapPath) {
2631 Set<NTuple<Descriptor>> boundedCalleeSet = new HashSet<NTuple<Descriptor>>();
2633 Set<Integer> keySet = mapCallerArgIdx2HeapPath.keySet();
2634 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2635 Integer idx = (Integer) iterator.next();
2637 NTuple<Descriptor> callerArgHeapPath = mapCallerArgIdx2HeapPath.get(idx);
2638 TempDescriptor calleeParam = mapParamIdx2ParamTempDesc.get(idx);
2639 for (Iterator iterator2 = calleeSet.iterator(); iterator2.hasNext();) {
2640 NTuple<Descriptor> element = (NTuple<Descriptor>) iterator2.next();
2641 if (element.startsWith(calleeParam)) {
2642 NTuple<Descriptor> boundElement = combine(callerArgHeapPath, element);
2643 boundedCalleeSet.add(boundElement);
2649 return boundedCalleeSet;
2653 // Borrowed it from disjoint analysis
2654 private LinkedList<MethodDescriptor> topologicalSort(Set<MethodDescriptor> toSort) {
2656 Set<MethodDescriptor> discovered = new HashSet<MethodDescriptor>();
2658 LinkedList<MethodDescriptor> sorted = new LinkedList<MethodDescriptor>();
2660 Iterator<MethodDescriptor> itr = toSort.iterator();
2661 while (itr.hasNext()) {
2662 MethodDescriptor d = itr.next();
2664 if (!discovered.contains(d)) {
2665 dfsVisit(d, toSort, sorted, discovered);
2672 // While we're doing DFS on call graph, remember
2673 // dependencies for efficient queuing of methods
2674 // during interprocedural analysis:
2676 // a dependent of a method decriptor d for this analysis is:
2677 // 1) a method or task that invokes d
2678 // 2) in the descriptorsToAnalyze set
2679 private void dfsVisit(MethodDescriptor md, Set<MethodDescriptor> toSort,
2680 LinkedList<MethodDescriptor> sorted, Set<MethodDescriptor> discovered) {
2684 Iterator itr = callGraph.getCallerSet(md).iterator();
2685 while (itr.hasNext()) {
2686 MethodDescriptor dCaller = (MethodDescriptor) itr.next();
2687 // only consider callers in the original set to analyze
2688 if (!toSort.contains(dCaller)) {
2691 if (!discovered.contains(dCaller)) {
2692 addDependent(md, // callee
2696 dfsVisit(dCaller, toSort, sorted, discovered);
2700 // for leaf-nodes last now!
2704 // a dependent of a method decriptor d for this analysis is:
2705 // 1) a method or task that invokes d
2706 // 2) in the descriptorsToAnalyze set
2707 private void addDependent(MethodDescriptor callee, MethodDescriptor caller) {
2708 Set<MethodDescriptor> deps = mapDescriptorToSetDependents.get(callee);
2710 deps = new HashSet<MethodDescriptor>();
2713 mapDescriptorToSetDependents.put(callee, deps);
2716 private Set<MethodDescriptor> getDependents(MethodDescriptor callee) {
2717 Set<MethodDescriptor> deps = mapDescriptorToSetDependents.get(callee);
2719 deps = new HashSet<MethodDescriptor>();
2720 mapDescriptorToSetDependents.put(callee, deps);
2725 private NTuple<Descriptor> computePath(TempDescriptor td) {
2726 // generate proper path fot input td
2727 // if td is local variable, it just generate one element tuple path
2728 if (mapHeapPath.containsKey(td)) {
2729 return mapHeapPath.get(td);
2731 NTuple<Descriptor> path = new NTuple<Descriptor>();
2737 private NTuple<Location> deriveLocationTuple(MethodDescriptor md, TempDescriptor td) {
2739 assert td.getType() != null;
2741 if (mapDescriptorToComposteLocation.containsKey(td)) {
2742 return mapDescriptorToComposteLocation.get(td);
2744 if (td.getSymbol().startsWith("this")) {
2745 String thisLocIdentifier = ssjava.getMethodLattice(md).getThisLoc();
2746 Location thisLoc = new Location(md, thisLocIdentifier);
2747 NTuple<Location> locTuple = new NTuple<Location>();
2748 locTuple.add(thisLoc);
2751 return ((SSJavaType) td.getType().getExtension()).getCompLoc().getTuple();
2757 private NTuple<Location> deriveLocationTuple(MethodDescriptor md, FieldDescriptor fld) {
2759 assert fld.getType() != null;
2761 Location fieldLoc = (Location) fld.getType().getExtension();
2762 NTuple<Location> locTuple = new NTuple<Location>();
2763 locTuple.add(fieldLoc);