1 package Analysis.SSJava;
3 import java.util.HashSet;
4 import java.util.Hashtable;
5 import java.util.Iterator;
6 import java.util.LinkedList;
8 import java.util.Stack;
10 import Analysis.CallGraph.CallGraph;
11 import Analysis.Loops.LoopFinder;
13 import IR.FieldDescriptor;
14 import IR.MethodDescriptor;
17 import IR.TypeDescriptor;
18 import IR.TypeExtension;
20 import IR.Flat.FlatCall;
21 import IR.Flat.FlatElementNode;
22 import IR.Flat.FlatFieldNode;
23 import IR.Flat.FlatLiteralNode;
24 import IR.Flat.FlatMethod;
25 import IR.Flat.FlatNode;
26 import IR.Flat.FlatOpNode;
27 import IR.Flat.FlatSetElementNode;
28 import IR.Flat.FlatSetFieldNode;
29 import IR.Flat.TempDescriptor;
30 import IR.Tree.Modifiers;
33 public class DefinitelyWrittenCheck {
35 SSJavaAnalysis ssjava;
41 // maps a descriptor to its known dependents: namely
42 // methods or tasks that call the descriptor's method
43 // AND are part of this analysis (reachable from main)
44 private Hashtable<Descriptor, Set<MethodDescriptor>> mapDescriptorToSetDependents;
46 // maps a flat node to its WrittenSet: this keeps all heap path overwritten
48 private Hashtable<FlatNode, Set<NTuple<Descriptor>>> mapFlatNodeToWrittenSet;
50 // maps a temp descriptor to its heap path
51 // each temp descriptor has a unique heap path since we do not allow any
53 private Hashtable<Descriptor, NTuple<Descriptor>> mapHeapPath;
55 // maps a flat method to the READ that is the set of heap path that is
56 // expected to be written before method invocation
57 private Hashtable<FlatMethod, Set<NTuple<Descriptor>>> mapFlatMethodToRead;
59 // maps a flat method to the OVERWRITE that is the set of heap path that is
60 // overwritten on every possible path during method invocation
61 private Hashtable<FlatMethod, Set<NTuple<Descriptor>>> mapFlatMethodToOverWrite;
63 // maps a flat method to the WRITE that is the set of heap path that is
65 private Hashtable<FlatMethod, Set<NTuple<Descriptor>>> mapFlatMethodToWrite;
67 // points to method containing SSJAVA Loop
68 private MethodDescriptor methodContainingSSJavaLoop;
70 // maps a flatnode to definitely written analysis mapping M
71 private Hashtable<FlatNode, Hashtable<NTuple<Descriptor>, Hashtable<FlatNode, Boolean>>> definitelyWrittenResults;
73 // maps a method descriptor to its current summary during the analysis
74 // then analysis reaches fixed-point, this mapping will have the final summary
75 // for each method descriptor
76 private Hashtable<MethodDescriptor, ClearingSummary> mapMethodDescriptorToCompleteClearingSummary;
78 // maps a method descriptor to the merged incoming caller's current
80 private Hashtable<MethodDescriptor, ClearingSummary> mapMethodDescriptorToInitialClearingSummary;
82 // maps a flat node to current partial results
83 private Hashtable<FlatNode, ClearingSummary> mapFlatNodeToClearingSummary;
85 // maps shared location to the set of descriptors which belong to the shared
88 // keep current descriptors to visit in fixed-point interprocedural analysis,
89 private Stack<MethodDescriptor> methodDescriptorsToVisitStack;
91 // when analyzing flatcall, need to re-schedule set of callee
92 private Set<MethodDescriptor> calleesToEnqueue;
94 // maps heap path to the mapping of a shared location and the set of reading
96 // it is for setting clearance flag when all read set is overwritten
97 private Hashtable<NTuple<Descriptor>, Hashtable<Location, Set<Descriptor>>> mapHeapPathToLocSharedDescReadSet;
99 public static final String arrayElementFieldName = "___element_";
100 static protected Hashtable<TypeDescriptor, FieldDescriptor> mapTypeToArrayField;
102 private Set<ClearingSummary> possibleCalleeCompleteSummarySetToCaller;
104 private LinkedList<MethodDescriptor> sortedDescriptors;
106 private FlatNode ssjavaLoopEntrance;
107 private LoopFinder ssjavaLoop;
108 private Set<FlatNode> loopIncElements;
110 private Set<NTuple<Descriptor>> calleeUnionBoundReadSet;
111 private Set<NTuple<Descriptor>> calleeIntersectBoundOverWriteSet;
112 private Set<NTuple<Descriptor>> calleeBoundWriteSet;
114 private Hashtable<Descriptor, Location> mapDescToLocation;
116 private TempDescriptor LOCAL;
118 public DefinitelyWrittenCheck(SSJavaAnalysis ssjava, State state) {
120 this.ssjava = ssjava;
121 this.callGraph = ssjava.getCallGraph();
122 this.mapFlatNodeToWrittenSet = new Hashtable<FlatNode, Set<NTuple<Descriptor>>>();
123 this.mapDescriptorToSetDependents = new Hashtable<Descriptor, Set<MethodDescriptor>>();
124 this.mapHeapPath = new Hashtable<Descriptor, NTuple<Descriptor>>();
125 this.mapFlatMethodToRead = new Hashtable<FlatMethod, Set<NTuple<Descriptor>>>();
126 this.mapFlatMethodToOverWrite = new Hashtable<FlatMethod, Set<NTuple<Descriptor>>>();
127 this.mapFlatMethodToWrite = new Hashtable<FlatMethod, Set<NTuple<Descriptor>>>();
128 this.definitelyWrittenResults =
129 new Hashtable<FlatNode, Hashtable<NTuple<Descriptor>, Hashtable<FlatNode, Boolean>>>();
130 this.calleeUnionBoundReadSet = new HashSet<NTuple<Descriptor>>();
131 this.calleeIntersectBoundOverWriteSet = new HashSet<NTuple<Descriptor>>();
132 this.calleeBoundWriteSet = new HashSet<NTuple<Descriptor>>();
134 this.mapMethodDescriptorToCompleteClearingSummary =
135 new Hashtable<MethodDescriptor, ClearingSummary>();
136 this.mapMethodDescriptorToInitialClearingSummary =
137 new Hashtable<MethodDescriptor, ClearingSummary>();
138 this.methodDescriptorsToVisitStack = new Stack<MethodDescriptor>();
139 this.calleesToEnqueue = new HashSet<MethodDescriptor>();
140 this.possibleCalleeCompleteSummarySetToCaller = new HashSet<ClearingSummary>();
141 this.mapTypeToArrayField = new Hashtable<TypeDescriptor, FieldDescriptor>();
142 this.LOCAL = new TempDescriptor("LOCAL");
143 this.mapDescToLocation = new Hashtable<Descriptor, Location>();
144 this.mapHeapPathToLocSharedDescReadSet =
145 new Hashtable<NTuple<Descriptor>, Hashtable<Location, Set<Descriptor>>>();
148 public void definitelyWrittenCheck() {
149 if (!ssjava.getAnnotationRequireSet().isEmpty()) {
150 methodReadOverWriteAnalysis();
152 sharedLocationAnalysis();
153 checkSharedLocationResult();
157 private void checkSharedLocationResult() {
159 // mapping of method containing ssjava loop has the final result of
160 // shared location analysis
162 ClearingSummary result =
163 mapMethodDescriptorToCompleteClearingSummary.get(methodContainingSSJavaLoop);
165 System.out.println("\n\n#RESULT=" + result);
167 Set<NTuple<Descriptor>> hpKeySet = result.keySet();
168 for (Iterator iterator = hpKeySet.iterator(); iterator.hasNext();) {
169 NTuple<Descriptor> hpKey = (NTuple<Descriptor>) iterator.next();
170 SharedStatus state = result.get(hpKey);
171 Set<Location> locKeySet = state.getLocationSet();
172 for (Iterator iterator2 = locKeySet.iterator(); iterator2.hasNext();) {
173 Location locKey = (Location) iterator2.next();
174 if (!state.getFlag(locKey)) {
175 printNotClearedResult(result);
177 "Some concrete locations of the shared abstract location are not cleared at the same time:");
184 private void printNotClearedResult(ClearingSummary result) {
185 Set<NTuple<Descriptor>> keySet = result.keySet();
187 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
188 NTuple<Descriptor> hpKey = (NTuple<Descriptor>) iterator.next();
189 SharedStatus status = result.get(hpKey);
190 Hashtable<Location, Pair<Set<Descriptor>, Boolean>> map = status.getMap();
191 Set<Location> locKeySet = map.keySet();
192 for (Iterator iterator2 = locKeySet.iterator(); iterator2.hasNext();) {
193 Location locKey = (Location) iterator2.next();
194 if (status.haveWriteEffect(locKey)) {
195 Pair<Set<Descriptor>, Boolean> pair = map.get(locKey);
196 if (!pair.getSecond().booleanValue()) {
198 System.out.println("Concrete locations of the shared location '" + locKey
199 + "' are not cleared out, which are reachable through the heap path '" + hpKey
208 private void sharedLocationAnalysis() {
209 // verify that all concrete locations of shared location are cleared out at
210 // the same time once per the out-most loop
212 computeReadSharedDescriptorSet();
214 System.out.println("###");
215 System.out.println("READ SHARED=" + mapHeapPathToLocSharedDescReadSet);
217 // methodDescriptorsToVisitStack.clear();
218 // methodDescriptorsToVisitStack.add(sortedDescriptors.peekFirst());
220 LinkedList<MethodDescriptor> descriptorListToAnalyze =
221 (LinkedList<MethodDescriptor>) sortedDescriptors.clone();
223 // current descriptors to visit in fixed-point interprocedural analysis,
225 // dependency in the call graph
226 methodDescriptorsToVisitStack.clear();
228 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
229 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
231 while (!descriptorListToAnalyze.isEmpty()) {
232 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
233 methodDescriptorsToVisitStack.add(md);
236 // analyze scheduled methods until there are no more to visit
237 while (!methodDescriptorsToVisitStack.isEmpty()) {
238 MethodDescriptor md = methodDescriptorsToVisitStack.pop();
240 ClearingSummary completeSummary =
241 sharedLocation_analyzeMethod(md, (md.equals(methodContainingSSJavaLoop)));
243 ClearingSummary prevCompleteSummary = mapMethodDescriptorToCompleteClearingSummary.get(md);
245 if (!completeSummary.equals(prevCompleteSummary)) {
247 ClearingSummary summaryFromCaller = mapMethodDescriptorToInitialClearingSummary.get(md);
248 // System.out.println("###");
249 // System.out.println("# summaryFromCaller=" + summaryFromCaller);
250 // System.out.println("# completeSummary=" + completeSummary);
251 // System.out.println("# prev=" + prevCompleteSummary);
252 // System.out.println("# changed!\n");
254 mapMethodDescriptorToCompleteClearingSummary.put(md, completeSummary);
256 // results for callee changed, so enqueue dependents caller for
258 Iterator<MethodDescriptor> depsItr = getDependents(md).iterator();
259 while (depsItr.hasNext()) {
260 MethodDescriptor methodNext = depsItr.next();
261 if (!methodDescriptorsToVisitStack.contains(methodNext)) {
262 methodDescriptorsToVisitStack.add(methodNext);
266 // if there is set of callee to be analyzed,
267 // add this set into the top of stack
268 Iterator<MethodDescriptor> calleeIter = calleesToEnqueue.iterator();
269 while (calleeIter.hasNext()) {
270 MethodDescriptor mdNext = calleeIter.next();
271 if (!methodDescriptorsToVisitStack.contains(mdNext)) {
272 methodDescriptorsToVisitStack.add(mdNext);
275 calleesToEnqueue.clear();
283 private ClearingSummary sharedLocation_analyzeMethod(MethodDescriptor md,
284 boolean onlyVisitSSJavaLoop) {
286 if (state.SSJAVADEBUG) {
287 System.out.println("Definite clearance for shared locations Analyzing: " + md + " "
288 + onlyVisitSSJavaLoop);
291 FlatMethod fm = state.getMethodFlat(md);
293 // intraprocedural analysis
294 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
296 // start a new mapping of partial results for each flat node
297 mapFlatNodeToClearingSummary = new Hashtable<FlatNode, ClearingSummary>();
299 if (onlyVisitSSJavaLoop) {
300 flatNodesToVisit.add(ssjavaLoopEntrance);
302 flatNodesToVisit.add(fm);
305 Set<FlatNode> returnNodeSet = new HashSet<FlatNode>();
307 while (!flatNodesToVisit.isEmpty()) {
308 FlatNode fn = flatNodesToVisit.iterator().next();
309 flatNodesToVisit.remove(fn);
311 ClearingSummary curr = new ClearingSummary();
313 Set<ClearingSummary> prevSet = new HashSet<ClearingSummary>();
314 for (int i = 0; i < fn.numPrev(); i++) {
315 FlatNode prevFn = fn.getPrev(i);
316 ClearingSummary in = mapFlatNodeToClearingSummary.get(prevFn);
321 mergeSharedLocationAnaylsis(curr, prevSet);
323 sharedLocation_nodeActions(md, fn, curr, returnNodeSet, onlyVisitSSJavaLoop);
324 ClearingSummary clearingPrev = mapFlatNodeToClearingSummary.get(fn);
326 if (!curr.equals(clearingPrev)) {
327 mapFlatNodeToClearingSummary.put(fn, curr);
329 for (int i = 0; i < fn.numNext(); i++) {
330 FlatNode nn = fn.getNext(i);
332 if (!onlyVisitSSJavaLoop || (onlyVisitSSJavaLoop && loopIncElements.contains(nn))) {
333 flatNodesToVisit.add(nn);
341 ClearingSummary completeSummary = new ClearingSummary();
342 Set<ClearingSummary> summarySet = new HashSet<ClearingSummary>();
344 if (onlyVisitSSJavaLoop) {
345 // when analyzing ssjava loop,
346 // complete summary is merging of all previous nodes of ssjava loop
348 for (int i = 0; i < ssjavaLoopEntrance.numPrev(); i++) {
349 ClearingSummary frnSummary =
350 mapFlatNodeToClearingSummary.get(ssjavaLoopEntrance.getPrev(i));
351 if (frnSummary != null) {
352 summarySet.add(frnSummary);
356 // merging all exit node summary into the complete summary
357 if (!returnNodeSet.isEmpty()) {
358 for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
359 FlatNode frn = (FlatNode) iterator.next();
360 ClearingSummary frnSummary = mapFlatNodeToClearingSummary.get(frn);
361 summarySet.add(frnSummary);
365 mergeSharedLocationAnaylsis(completeSummary, summarySet);
367 if (md.getSymbol().startsWith("decodeFrame")) {
368 System.out.println("#");
369 System.out.println("# method=" + md);
370 System.out.println("XXXXX summarySet=" + summarySet);
371 System.out.println("XXXXX completeSummary=" + completeSummary);
372 System.out.println("#");
375 return completeSummary;
378 private void sharedLocation_nodeActions(MethodDescriptor caller, FlatNode fn,
379 ClearingSummary curr, Set<FlatNode> returnNodeSet, boolean isSSJavaLoop) {
386 case FKind.FlatMethod: {
387 FlatMethod fm = (FlatMethod) fn;
389 ClearingSummary summaryFromCaller =
390 mapMethodDescriptorToInitialClearingSummary.get(fm.getMethod());
392 Set<ClearingSummary> inSet = new HashSet<ClearingSummary>();
393 if (summaryFromCaller != null) {
394 inSet.add(summaryFromCaller);
395 mergeSharedLocationAnaylsis(curr, inSet);
401 case FKind.FlatOpNode: {
402 FlatOpNode fon = (FlatOpNode) fn;
406 if (fon.getOp().getOp() == Operation.ASSIGN) {
407 if (rhs.getType().isImmutable() && isSSJavaLoop) {
408 // in ssjavaloop, we need to take care about reading local variables!
409 NTuple<Descriptor> rhsHeapPath = new NTuple<Descriptor>();
410 NTuple<Descriptor> lhsHeapPath = new NTuple<Descriptor>();
411 rhsHeapPath.add(LOCAL);
412 lhsHeapPath.add(LOCAL);
413 if (!lhs.getSymbol().startsWith("neverused")) {
414 readLocation(curr, rhsHeapPath, getLocation(rhs), rhs);
415 writeLocation(curr, lhsHeapPath, getLocation(lhs), lhs);
423 case FKind.FlatFieldNode:
424 case FKind.FlatElementNode: {
426 if (fn.kind() == FKind.FlatFieldNode) {
427 FlatFieldNode ffn = (FlatFieldNode) fn;
430 fld = ffn.getField();
432 FlatElementNode fen = (FlatElementNode) fn;
435 TypeDescriptor td = rhs.getType().dereference();
436 fld = getArrayField(td);
440 NTuple<Descriptor> srcHeapPath = mapHeapPath.get(rhs);
442 if (srcHeapPath != null) {
443 // if lhs srcHeapPath is null, it means that it is not reachable from
444 // callee's parameters. so just ignore it
445 NTuple<Descriptor> fldHeapPath = new NTuple<Descriptor>(srcHeapPath.getList());
447 // if (!fld.getType().isArray() && fld.getType().isImmutable()) {
448 // addReadDescriptor(fldHeapPath, getLocation(fld), fld);
450 // if (fn.kind() == FKind.FlatFieldNode) {
451 // mapDescToLocation.put(lhs, getLocation(fld));
452 // readLocation(curr, fldHeapPath, getLocation(fld), fld);
454 // fldHeapPath.add(fld);
456 // mapHeapPath.put(lhs, fldHeapPath);
459 if (!fld.getType().isArray() && fld.getType().isImmutable()) {
461 if (fn.kind() == FKind.FlatElementNode) {
462 // array element read case
463 loc = mapDescToLocation.get(rhs);
464 NTuple<Descriptor> newHeapPath = new NTuple<Descriptor>();
465 for (int i = 0; i < fldHeapPath.size() - 1; i++) {
466 newHeapPath.add(fldHeapPath.get(i));
469 Descriptor desc = fldHeapPath.get(fldHeapPath.size() - 1);
470 if (desc instanceof FieldDescriptor) {
471 fld = (FieldDescriptor) desc;
472 fldHeapPath = newHeapPath;
473 readLocation(curr, fldHeapPath, loc, fld);
476 loc = getLocation(fld);
477 readLocation(curr, fldHeapPath, loc, fld);
481 if (fn.kind() != FKind.FlatElementNode) {
482 // if it is multi dimensional array, do not need to add heap path
483 // because all accesses from the same array is represented by
485 fldHeapPath.add(fld);
487 mapHeapPath.put(lhs, fldHeapPath);
497 case FKind.FlatSetFieldNode:
498 case FKind.FlatSetElementNode: {
500 if (fn.kind() == FKind.FlatSetFieldNode) {
501 FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
503 fld = fsfn.getField();
506 FlatSetElementNode fsen = (FlatSetElementNode) fn;
509 TypeDescriptor td = lhs.getType().dereference();
510 fld = getArrayField(td);
514 NTuple<Descriptor> lhsHeapPath = computePath(lhs);
515 NTuple<Descriptor> fldHeapPath = new NTuple<Descriptor>(lhsHeapPath.getList());
516 if (fld.getType().isImmutable()) {
517 writeLocation(curr, fldHeapPath, getLocation(fld), fld);
519 Descriptor desc = fldHeapPath.get(fldHeapPath.size() - 1);
520 if (desc instanceof FieldDescriptor) {
521 NTuple<Descriptor> arrayPath = new NTuple<Descriptor>();
522 for (int i = 0; i < fldHeapPath.size() - 1; i++) {
523 arrayPath.add(fldHeapPath.get(i));
525 SharedStatus state = getState(curr, arrayPath);
526 state.setWriteEffect(getLocation(desc));
530 // updates reference field case:
531 fldHeapPath.add(fld);
532 updateWriteEffectOnReferenceField(curr, fldHeapPath);
538 case FKind.FlatCall: {
540 FlatCall fc = (FlatCall) fn;
542 if (ssjava.isSSJavaUtil(fc.getMethod().getClassDesc())) {
544 // have write effects on the first argument
546 if (fc.getArg(0).getType().isArray()) {
547 // updates reference field case:
548 // 2. if there exists a tuple t in sharing summary that starts with
549 // hp(x) then, set flag of tuple t to 'true'
550 NTuple<Descriptor> argHeapPath = computePath(fc.getArg(0));
552 Location loc = getLocation(fc.getArg(0));
553 NTuple<Descriptor> newHeapPath = new NTuple<Descriptor>();
554 for (int i = 0; i < argHeapPath.size() - 1; i++) {
555 newHeapPath.add(argHeapPath.get(i));
557 fld = (FieldDescriptor) argHeapPath.get(argHeapPath.size() - 1);
558 argHeapPath = newHeapPath;
560 writeLocation(curr, argHeapPath, loc, fld);
564 // find out the set of callees
565 MethodDescriptor mdCallee = fc.getMethod();
566 FlatMethod fmCallee = state.getMethodFlat(mdCallee);
567 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
568 // TypeDescriptor typeDesc = fc.getThis().getType();
569 setPossibleCallees.addAll(callGraph.getMethods(mdCallee));
571 possibleCalleeCompleteSummarySetToCaller.clear();
573 for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
574 MethodDescriptor mdPossibleCallee = (MethodDescriptor) iterator.next();
575 FlatMethod calleeFlatMethod = state.getMethodFlat(mdPossibleCallee);
577 addDependent(mdPossibleCallee, // callee
580 calleesToEnqueue.add(mdPossibleCallee);
582 // updates possible callee's initial summary using caller's current
584 ClearingSummary prevCalleeInitSummary =
585 mapMethodDescriptorToInitialClearingSummary.get(mdPossibleCallee);
587 ClearingSummary calleeInitSummary =
588 bindHeapPathOfCalleeCallerEffects(fc, calleeFlatMethod, curr);
590 Set<ClearingSummary> inSet = new HashSet<ClearingSummary>();
591 if (prevCalleeInitSummary != null) {
592 inSet.add(prevCalleeInitSummary);
593 mergeSharedLocationAnaylsis(calleeInitSummary, inSet);
596 // if changes, update the init summary
597 // and reschedule the callee for analysis
598 if (!calleeInitSummary.equals(prevCalleeInitSummary)) {
599 // System.out.println("#CALLEE INIT CHANGED=" + mdPossibleCallee);
600 // System.out.println("# prev=" + prevCalleeInitSummary);
601 // System.out.println("# current=" + calleeInitSummary);
602 // System.out.println("#");
604 if (!methodDescriptorsToVisitStack.contains(mdPossibleCallee)) {
605 methodDescriptorsToVisitStack.add(mdPossibleCallee);
608 mapMethodDescriptorToInitialClearingSummary.put(mdPossibleCallee, calleeInitSummary);
613 // contribute callee's writing effects to the caller
614 mergeSharedLocationAnaylsis(curr, possibleCalleeCompleteSummarySetToCaller);
615 if (fc.getMethod().getSymbol().startsWith("decode")) {
616 System.out.println("XXXXX callee " + fc + " summary="
617 + possibleCalleeCompleteSummarySetToCaller);
618 System.out.println("XXXXX curr=" + curr);
626 case FKind.FlatReturnNode: {
627 returnNodeSet.add(fn);
635 private void updateWriteEffectOnReferenceField(ClearingSummary curr, NTuple<Descriptor> heapPath) {
638 // System.out.println("UPDATE WRITE REF=" + heapPath);
639 // Descriptor d = heapPath.get(heapPath.size() - 1);
640 // boolean print = false;
641 // if (d instanceof FieldDescriptor) {
642 // FieldDescriptor fd = (FieldDescriptor) d;
643 // if (fd.getSymbol().equals("si")) {
644 // System.out.println("UPDATE PREV CURR=" + curr);
648 // 2. if there exists a tuple t in sharing summary that starts with
649 // hp(x) then, set flag of tuple t to 'true'
650 Set<NTuple<Descriptor>> hpKeySet = curr.keySet();
651 for (Iterator iterator = hpKeySet.iterator(); iterator.hasNext();) {
652 NTuple<Descriptor> hpKey = (NTuple<Descriptor>) iterator.next();
653 if (hpKey.startsWith(heapPath)) {
654 curr.get(hpKey).updateFlag(true);
659 // System.out.println("UPDATE CURR=" + curr);
663 private ClearingSummary bindHeapPathOfCalleeCallerEffects(FlatCall fc,
664 FlatMethod calleeFlatMethod, ClearingSummary curr) {
666 ClearingSummary boundSet = new ClearingSummary();
668 // create mapping from arg idx to its heap paths
669 Hashtable<Integer, NTuple<Descriptor>> mapArgIdx2CallerArgHeapPath =
670 new Hashtable<Integer, NTuple<Descriptor>>();
672 if (fc.getThis() != null) {
673 // arg idx is starting from 'this' arg
674 NTuple<Descriptor> thisHeapPath = mapHeapPath.get(fc.getThis());
675 if (thisHeapPath == null) {
676 // method is called without creating new flat node representing 'this'
677 thisHeapPath = new NTuple<Descriptor>();
678 thisHeapPath.add(fc.getThis());
681 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(0), thisHeapPath);
684 for (int i = 0; i < fc.numArgs(); i++) {
685 TempDescriptor arg = fc.getArg(i);
686 NTuple<Descriptor> argHeapPath = computePath(arg);
687 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(i + 1), argHeapPath);
690 Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc =
691 new Hashtable<Integer, TempDescriptor>();
693 if (calleeFlatMethod.getMethod().isStatic()) {
694 // static method does not have implicit 'this' arg
697 for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
698 TempDescriptor param = calleeFlatMethod.getParameter(i);
699 mapParamIdx2ParamTempDesc.put(Integer.valueOf(i + offset), param);
702 // binding caller's writing effects to callee's params
703 for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
704 NTuple<Descriptor> argHeapPath = mapArgIdx2CallerArgHeapPath.get(Integer.valueOf(i));
706 if (argHeapPath != null) {
707 // if method is static, the first argument is nulll because static
708 // method does not have implicit "THIS" arg
709 TempDescriptor calleeParamHeapPath = mapParamIdx2ParamTempDesc.get(Integer.valueOf(i));
711 // iterate over caller's writing effect set
712 Set<NTuple<Descriptor>> hpKeySet = curr.keySet();
713 for (Iterator iterator = hpKeySet.iterator(); iterator.hasNext();) {
714 NTuple<Descriptor> hpKey = (NTuple<Descriptor>) iterator.next();
715 // current element is reachable caller's arg
716 // so need to bind it to the caller's side and add it to the
719 if (hpKey.startsWith(argHeapPath)) {
720 NTuple<Descriptor> boundHeapPath = replace(hpKey, argHeapPath, calleeParamHeapPath);
721 boundSet.put(boundHeapPath, curr.get(hpKey).clone());
729 // contribute callee's complete summary into the caller's current summary
730 ClearingSummary calleeCompleteSummary =
731 mapMethodDescriptorToCompleteClearingSummary.get(calleeFlatMethod.getMethod());
732 if (calleeCompleteSummary != null) {
733 ClearingSummary boundCalleeEfffects = new ClearingSummary();
734 for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
735 // for (int i = 0; i < fc.numArgs(); i++) {
736 NTuple<Descriptor> argHeapPath = mapArgIdx2CallerArgHeapPath.get(Integer.valueOf(i));
738 if (argHeapPath != null) {
739 // if method is static, the first argument is nulll because static
740 // method does not have implicit "THIS" arg
741 TempDescriptor calleeParamHeapPath = mapParamIdx2ParamTempDesc.get(Integer.valueOf(i));
743 // iterate over callee's writing effect set
744 Set<NTuple<Descriptor>> hpKeySet = calleeCompleteSummary.keySet();
745 for (Iterator iterator = hpKeySet.iterator(); iterator.hasNext();) {
746 NTuple<Descriptor> hpKey = (NTuple<Descriptor>) iterator.next();
747 // current element is reachable caller's arg
748 // so need to bind it to the caller's side and add it to the
751 if (hpKey.startsWith(calleeParamHeapPath)) {
753 NTuple<Descriptor> boundHeapPathForCaller = replace(hpKey, argHeapPath);
755 boundCalleeEfffects.put(boundHeapPathForCaller, calleeCompleteSummary.get(hpKey)
764 possibleCalleeCompleteSummarySetToCaller.add(boundCalleeEfffects);
770 private NTuple<Descriptor> replace(NTuple<Descriptor> hpKey, NTuple<Descriptor> argHeapPath) {
772 // replace the head of heap path with caller's arg path
773 // for example, heap path 'param.a.b' in callee's side will be replaced with
774 // (corresponding arg heap path).a.b for caller's side
776 NTuple<Descriptor> bound = new NTuple<Descriptor>();
778 for (int i = 0; i < argHeapPath.size(); i++) {
779 bound.add(argHeapPath.get(i));
782 for (int i = 1; i < hpKey.size(); i++) {
783 bound.add(hpKey.get(i));
789 private NTuple<Descriptor> replace(NTuple<Descriptor> effectHeapPath,
790 NTuple<Descriptor> argHeapPath, TempDescriptor calleeParamHeapPath) {
791 // replace the head of caller's heap path with callee's param heap path
793 NTuple<Descriptor> boundHeapPath = new NTuple<Descriptor>();
794 boundHeapPath.add(calleeParamHeapPath);
796 for (int i = argHeapPath.size(); i < effectHeapPath.size(); i++) {
797 boundHeapPath.add(effectHeapPath.get(i));
800 return boundHeapPath;
803 private void computeReadSharedDescriptorSet() {
804 Set<MethodDescriptor> methodDescriptorsToAnalyze = new HashSet<MethodDescriptor>();
805 methodDescriptorsToAnalyze.addAll(ssjava.getAnnotationRequireSet());
807 for (Iterator iterator = methodDescriptorsToAnalyze.iterator(); iterator.hasNext();) {
808 MethodDescriptor md = (MethodDescriptor) iterator.next();
809 FlatMethod fm = state.getMethodFlat(md);
810 computeReadSharedDescriptorSet_analyzeMethod(fm, md.equals(methodContainingSSJavaLoop));
815 private void computeReadSharedDescriptorSet_analyzeMethod(FlatMethod fm,
816 boolean onlyVisitSSJavaLoop) {
818 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
819 Set<FlatNode> visited = new HashSet<FlatNode>();
821 if (onlyVisitSSJavaLoop) {
822 flatNodesToVisit.add(ssjavaLoopEntrance);
824 flatNodesToVisit.add(fm);
827 while (!flatNodesToVisit.isEmpty()) {
828 FlatNode fn = flatNodesToVisit.iterator().next();
829 flatNodesToVisit.remove(fn);
832 computeReadSharedDescriptorSet_nodeActions(fn, onlyVisitSSJavaLoop);
834 for (int i = 0; i < fn.numNext(); i++) {
835 FlatNode nn = fn.getNext(i);
836 if (!visited.contains(nn)) {
837 if (!onlyVisitSSJavaLoop || (onlyVisitSSJavaLoop && loopIncElements.contains(nn))) {
838 flatNodesToVisit.add(nn);
847 private void computeReadSharedDescriptorSet_nodeActions(FlatNode fn, boolean isSSJavaLoop) {
854 case FKind.FlatOpNode: {
855 FlatOpNode fon = (FlatOpNode) fn;
859 if (fon.getOp().getOp() == Operation.ASSIGN) {
860 if (rhs.getType().isImmutable() && isSSJavaLoop && (!rhs.getSymbol().startsWith("srctmp"))) {
861 // in ssjavaloop, we need to take care about reading local variables!
862 NTuple<Descriptor> rhsHeapPath = new NTuple<Descriptor>();
863 NTuple<Descriptor> lhsHeapPath = new NTuple<Descriptor>();
864 rhsHeapPath.add(LOCAL);
865 Location loc = getLocation(rhs);
866 addReadDescriptor(rhsHeapPath, loc, rhs);
873 case FKind.FlatFieldNode:
874 case FKind.FlatElementNode: {
876 if (fn.kind() == FKind.FlatFieldNode) {
877 FlatFieldNode ffn = (FlatFieldNode) fn;
880 fld = ffn.getField();
882 FlatElementNode fen = (FlatElementNode) fn;
885 TypeDescriptor td = rhs.getType().dereference();
886 fld = getArrayField(td);
889 if (fld.isStatic() && fld.isFinal()) {
894 NTuple<Descriptor> srcHeapPath = mapHeapPath.get(rhs);
895 if (srcHeapPath != null) {
896 // if srcHeapPath is null, it means that it is not reachable from
897 // callee's parameters. so just ignore it
899 NTuple<Descriptor> fldHeapPath = new NTuple<Descriptor>(srcHeapPath.getList());
901 if (!fld.getType().isArray() && fld.getType().isImmutable()) {
904 if (fn.kind() == FKind.FlatElementNode) {
905 // array element read case
906 loc = mapDescToLocation.get(rhs);
907 NTuple<Descriptor> newHeapPath = new NTuple<Descriptor>();
908 for (int i = 0; i < fldHeapPath.size() - 1; i++) {
909 newHeapPath.add(fldHeapPath.get(i));
912 Descriptor desc = fldHeapPath.get(fldHeapPath.size() - 1);
913 if (desc instanceof FieldDescriptor) {
914 fld = (FieldDescriptor) desc;
915 fldHeapPath = newHeapPath;
916 addReadDescriptor(fldHeapPath, loc, fld);
919 loc = getLocation(fld);
920 addReadDescriptor(fldHeapPath, loc, fld);
923 // propagate rhs's heap path to the lhs
925 if (fn.kind() == FKind.FlatElementNode) {
926 mapDescToLocation.put(lhs, getLocation(rhs));
928 fldHeapPath.add(fld);
930 mapHeapPath.put(lhs, fldHeapPath);
938 case FKind.FlatSetFieldNode:
939 case FKind.FlatSetElementNode: {
941 if (fn.kind() == FKind.FlatSetFieldNode) {
942 FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
944 fld = fsfn.getField();
946 FlatSetElementNode fsen = (FlatSetElementNode) fn;
949 TypeDescriptor td = lhs.getType().dereference();
950 fld = getArrayField(td);
954 NTuple<Descriptor> lhsHeapPath = computePath(lhs);
955 NTuple<Descriptor> fldHeapPath = new NTuple<Descriptor>(lhsHeapPath.getList());
956 // writeLocation(curr, fldHeapPath, fld, getLocation(fld));
964 private boolean hasReadingEffectOnSharedLocation(NTuple<Descriptor> hp, Location loc, Descriptor d) {
966 Hashtable<Location, Set<Descriptor>> mapLocToDescSet =
967 mapHeapPathToLocSharedDescReadSet.get(hp);
968 if (mapLocToDescSet == null) {
971 Set<Descriptor> setDesc = mapLocToDescSet.get(loc);
972 if (setDesc == null) {
975 return setDesc.contains(d);
981 private void addReadDescriptor(NTuple<Descriptor> hp, Location loc, Descriptor d) {
983 if (loc != null && ssjava.isSharedLocation(loc)) {
984 Hashtable<Location, Set<Descriptor>> mapLocToDescSet =
985 mapHeapPathToLocSharedDescReadSet.get(hp);
986 if (mapLocToDescSet == null) {
987 mapLocToDescSet = new Hashtable<Location, Set<Descriptor>>();
988 mapHeapPathToLocSharedDescReadSet.put(hp, mapLocToDescSet);
990 Set<Descriptor> descSet = mapLocToDescSet.get(loc);
991 if (descSet == null) {
992 descSet = new HashSet<Descriptor>();
993 mapLocToDescSet.put(loc, descSet);
1000 private Location getLocation(Descriptor d) {
1002 if (d instanceof FieldDescriptor) {
1003 TypeExtension te = ((FieldDescriptor) d).getType().getExtension();
1005 return (Location) te;
1008 assert d instanceof TempDescriptor;
1009 TempDescriptor td = (TempDescriptor) d;
1011 TypeExtension te = td.getType().getExtension();
1013 if (te instanceof CompositeLocation) {
1014 CompositeLocation comp = (CompositeLocation) te;
1016 return comp.get(comp.getSize() - 1);
1018 return (Location) te;
1023 return mapDescToLocation.get(d);
1026 private void writeLocation(ClearingSummary curr, NTuple<Descriptor> hp, Location loc, Descriptor d) {
1028 // System.out.println("# WRITE LOC hp=" + hp + " d=" + d + " loc=" + loc);
1029 SharedStatus state = getState(curr, hp);
1030 if (loc != null && hasReadingEffectOnSharedLocation(hp, loc, d)) {
1031 // 1. add field x to the clearing set
1033 state.addVar(loc, d);
1035 // 3. if the set v contains all of variables belonging to the shared
1036 // location, set flag to true
1037 if (overwrittenAllSharedConcreteLocation(hp, loc, state.getVarSet(loc))) {
1038 state.updateFlag(loc, true);
1041 state.setWriteEffect(loc);
1042 // System.out.println("# WRITE CURR=" + curr);
1046 private boolean overwrittenAllSharedConcreteLocation(NTuple<Descriptor> hp, Location loc,
1047 Set<Descriptor> writtenSet) {
1049 Hashtable<Location, Set<Descriptor>> mapLocToDescSet =
1050 mapHeapPathToLocSharedDescReadSet.get(hp);
1052 if (mapLocToDescSet != null) {
1053 Set<Descriptor> descSet = mapLocToDescSet.get(loc);
1054 if (writtenSet.containsAll(descSet)) {
1064 private void readLocation(ClearingSummary curr, NTuple<Descriptor> hp, Location loc, Descriptor d) {
1065 // remove reading var x from written set
1066 // System.out.println("# READ LOC hp=" + hp + " d=" + d + " loc=" + loc);
1067 if (loc != null && hasReadingEffectOnSharedLocation(hp, loc, d)) {
1068 SharedStatus state = getState(curr, hp);
1069 state.removeVar(loc, d);
1071 // System.out.println("# READ CURR=" + curr);
1074 private SharedStatus getState(ClearingSummary curr, NTuple<Descriptor> hp) {
1075 SharedStatus state = curr.get(hp);
1076 if (state == null) {
1077 state = new SharedStatus();
1078 curr.put(hp, state);
1083 private void writtenAnalyis() {
1084 // perform second stage analysis: intraprocedural analysis ensure that
1086 // variables are definitely written in-between the same read
1088 // First, identify ssjava loop entrace
1089 FlatMethod fm = state.getMethodFlat(methodContainingSSJavaLoop);
1090 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
1091 flatNodesToVisit.add(fm);
1093 LoopFinder loopFinder = new LoopFinder(fm);
1095 while (!flatNodesToVisit.isEmpty()) {
1096 FlatNode fn = flatNodesToVisit.iterator().next();
1097 flatNodesToVisit.remove(fn);
1099 String label = (String) state.fn2labelMap.get(fn);
1100 if (label != null) {
1102 if (label.equals(ssjava.SSJAVA)) {
1103 ssjavaLoopEntrance = fn;
1108 for (int i = 0; i < fn.numNext(); i++) {
1109 FlatNode nn = fn.getNext(i);
1110 flatNodesToVisit.add(nn);
1114 assert ssjavaLoopEntrance != null;
1116 // assume that ssjava loop is top-level loop in method, not nested loop
1117 Set nestedLoop = loopFinder.nestedLoops();
1118 for (Iterator loopIter = nestedLoop.iterator(); loopIter.hasNext();) {
1119 LoopFinder lf = (LoopFinder) loopIter.next();
1120 if (lf.loopEntrances().iterator().next().equals(ssjavaLoopEntrance)) {
1125 assert ssjavaLoop != null;
1127 writtenAnalysis_analyzeLoop();
1129 if (debugcount > 0) {
1135 private void writtenAnalysis_analyzeLoop() {
1137 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
1138 flatNodesToVisit.add(ssjavaLoopEntrance);
1140 loopIncElements = (Set<FlatNode>) ssjavaLoop.loopIncElements();
1142 while (!flatNodesToVisit.isEmpty()) {
1143 FlatNode fn = (FlatNode) flatNodesToVisit.iterator().next();
1144 flatNodesToVisit.remove(fn);
1146 Hashtable<NTuple<Descriptor>, Hashtable<FlatNode, Boolean>> prev =
1147 definitelyWrittenResults.get(fn);
1149 Hashtable<NTuple<Descriptor>, Hashtable<FlatNode, Boolean>> curr =
1150 new Hashtable<NTuple<Descriptor>, Hashtable<FlatNode, Boolean>>();
1151 for (int i = 0; i < fn.numPrev(); i++) {
1152 FlatNode nn = fn.getPrev(i);
1153 Hashtable<NTuple<Descriptor>, Hashtable<FlatNode, Boolean>> dwIn =
1154 definitelyWrittenResults.get(nn);
1160 writtenAnalysis_nodeAction(fn, curr, ssjavaLoopEntrance);
1162 // if a new result, schedule forward nodes for analysis
1163 if (!curr.equals(prev)) {
1164 definitelyWrittenResults.put(fn, curr);
1166 for (int i = 0; i < fn.numNext(); i++) {
1167 FlatNode nn = fn.getNext(i);
1168 if (loopIncElements.contains(nn)) {
1169 flatNodesToVisit.add(nn);
1177 private void writtenAnalysis_nodeAction(FlatNode fn,
1178 Hashtable<NTuple<Descriptor>, Hashtable<FlatNode, Boolean>> curr, FlatNode loopEntrance) {
1180 if (fn.equals(loopEntrance)) {
1181 // it reaches loop entrance: changes all flag to true
1182 Set<NTuple<Descriptor>> keySet = curr.keySet();
1183 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1184 NTuple<Descriptor> key = (NTuple<Descriptor>) iterator.next();
1185 Hashtable<FlatNode, Boolean> pair = curr.get(key);
1187 Set<FlatNode> pairKeySet = pair.keySet();
1188 for (Iterator iterator2 = pairKeySet.iterator(); iterator2.hasNext();) {
1189 FlatNode pairKey = (FlatNode) iterator2.next();
1190 pair.put(pairKey, Boolean.TRUE);
1197 FieldDescriptor fld;
1199 switch (fn.kind()) {
1200 case FKind.FlatOpNode: {
1201 FlatOpNode fon = (FlatOpNode) fn;
1202 lhs = fon.getDest();
1203 rhs = fon.getLeft();
1205 NTuple<Descriptor> rhsHeapPath = computePath(rhs);
1206 if (!rhs.getType().isImmutable()) {
1207 mapHeapPath.put(lhs, rhsHeapPath);
1209 if (fon.getOp().getOp() == Operation.ASSIGN) {
1211 readValue(fn, rhsHeapPath, curr);
1214 NTuple<Descriptor> lhsHeapPath = computePath(lhs);
1215 removeHeapPath(curr, lhsHeapPath);
1220 case FKind.FlatLiteralNode: {
1221 FlatLiteralNode fln = (FlatLiteralNode) fn;
1225 NTuple<Descriptor> lhsHeapPath = computePath(lhs);
1226 removeHeapPath(curr, lhsHeapPath);
1231 case FKind.FlatFieldNode:
1232 case FKind.FlatElementNode: {
1234 if (fn.kind() == FKind.FlatFieldNode) {
1235 FlatFieldNode ffn = (FlatFieldNode) fn;
1238 fld = ffn.getField();
1240 FlatElementNode fen = (FlatElementNode) fn;
1243 TypeDescriptor td = rhs.getType().dereference();
1244 fld = getArrayField(td);
1247 if (fld.isFinal() /* && fld.isStatic() */) {
1248 // if field is final and static, no need to check
1253 NTuple<Descriptor> srcHeapPath = mapHeapPath.get(rhs);
1254 NTuple<Descriptor> fldHeapPath = new NTuple<Descriptor>(srcHeapPath.getList());
1255 fldHeapPath.add(fld);
1257 if (fld.getType().isImmutable()) {
1258 readValue(fn, fldHeapPath, curr);
1261 // propagate rhs's heap path to the lhs
1262 mapHeapPath.put(lhs, fldHeapPath);
1267 case FKind.FlatSetFieldNode:
1268 case FKind.FlatSetElementNode: {
1270 if (fn.kind() == FKind.FlatSetFieldNode) {
1271 FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
1272 lhs = fsfn.getDst();
1273 fld = fsfn.getField();
1275 FlatSetElementNode fsen = (FlatSetElementNode) fn;
1276 lhs = fsen.getDst();
1277 rhs = fsen.getSrc();
1278 TypeDescriptor td = lhs.getType().dereference();
1279 fld = getArrayField(td);
1283 NTuple<Descriptor> lhsHeapPath = computePath(lhs);
1284 NTuple<Descriptor> fldHeapPath = new NTuple<Descriptor>(lhsHeapPath.getList());
1285 fldHeapPath.add(fld);
1286 removeHeapPath(curr, fldHeapPath);
1291 case FKind.FlatCall: {
1292 FlatCall fc = (FlatCall) fn;
1294 bindHeapPathCallerArgWithCaleeParam(fc);
1295 // add <hp,statement,false> in which hp is an element of
1297 // of callee: callee has 'read' requirement!
1299 for (Iterator iterator = calleeUnionBoundReadSet.iterator(); iterator.hasNext();) {
1300 NTuple<Descriptor> read = (NTuple<Descriptor>) iterator.next();
1301 Hashtable<FlatNode, Boolean> gen = curr.get(read);
1303 gen = new Hashtable<FlatNode, Boolean>();
1304 curr.put(read, gen);
1306 Boolean currentStatus = gen.get(fn);
1307 if (currentStatus == null) {
1308 gen.put(fn, Boolean.FALSE);
1310 checkFlag(currentStatus.booleanValue(), fn, read);
1314 // removes <hp,statement,flag> if hp is an element of
1316 // set of callee. it means that callee will overwrite it
1317 for (Iterator iterator = calleeIntersectBoundOverWriteSet.iterator(); iterator.hasNext();) {
1318 NTuple<Descriptor> write = (NTuple<Descriptor>) iterator.next();
1319 removeHeapPath(curr, write);
1329 private void readValue(FlatNode fn, NTuple<Descriptor> hp,
1330 Hashtable<NTuple<Descriptor>, Hashtable<FlatNode, Boolean>> curr) {
1331 Hashtable<FlatNode, Boolean> gen = curr.get(hp);
1333 gen = new Hashtable<FlatNode, Boolean>();
1336 Boolean currentStatus = gen.get(fn);
1337 if (currentStatus == null) {
1338 gen.put(fn, Boolean.FALSE);
1340 checkFlag(currentStatus.booleanValue(), fn, hp);
1345 private void removeHeapPath(Hashtable<NTuple<Descriptor>, Hashtable<FlatNode, Boolean>> curr,
1346 NTuple<Descriptor> hp) {
1348 // removes all of heap path that starts with prefix 'hp'
1349 // since any reference overwrite along heap path gives overwriting side
1350 // effects on the value
1352 Set<NTuple<Descriptor>> keySet = curr.keySet();
1353 for (Iterator<NTuple<Descriptor>> iter = keySet.iterator(); iter.hasNext();) {
1354 NTuple<Descriptor> key = iter.next();
1355 if (key.startsWith(hp)) {
1356 curr.put(key, new Hashtable<FlatNode, Boolean>());
1362 private void bindHeapPathCallerArgWithCaleeParam(FlatCall fc) {
1363 // compute all possible callee set
1364 // transform all READ/OVERWRITE set from the any possible
1367 calleeUnionBoundReadSet.clear();
1368 calleeIntersectBoundOverWriteSet.clear();
1369 calleeBoundWriteSet.clear();
1371 if (ssjava.isSSJavaUtil(fc.getMethod().getClassDesc())) {
1372 // ssjava util case!
1373 // have write effects on the first argument
1374 TempDescriptor arg = fc.getArg(0);
1375 NTuple<Descriptor> argHeapPath = computePath(arg);
1376 calleeIntersectBoundOverWriteSet.add(argHeapPath);
1378 MethodDescriptor mdCallee = fc.getMethod();
1379 // FlatMethod fmCallee = state.getMethodFlat(mdCallee);
1380 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
1381 // setPossibleCallees.addAll(callGraph.getMethods(mdCallee, typeDesc));
1382 setPossibleCallees.addAll(callGraph.getMethods(mdCallee));
1384 // create mapping from arg idx to its heap paths
1385 Hashtable<Integer, NTuple<Descriptor>> mapArgIdx2CallerArgHeapPath =
1386 new Hashtable<Integer, NTuple<Descriptor>>();
1388 // arg idx is starting from 'this' arg
1389 if (fc.getThis() != null) {
1390 NTuple<Descriptor> thisHeapPath = mapHeapPath.get(fc.getThis());
1391 if (thisHeapPath == null) {
1392 // method is called without creating new flat node representing 'this'
1393 thisHeapPath = new NTuple<Descriptor>();
1394 thisHeapPath.add(fc.getThis());
1397 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(0), thisHeapPath);
1400 for (int i = 0; i < fc.numArgs(); i++) {
1401 TempDescriptor arg = fc.getArg(i);
1402 NTuple<Descriptor> argHeapPath = computePath(arg);
1403 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(i + 1), argHeapPath);
1406 for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
1407 MethodDescriptor callee = (MethodDescriptor) iterator.next();
1408 FlatMethod calleeFlatMethod = state.getMethodFlat(callee);
1410 // binding caller's args and callee's params
1412 Set<NTuple<Descriptor>> calleeReadSet = mapFlatMethodToRead.get(calleeFlatMethod);
1413 if (calleeReadSet == null) {
1414 calleeReadSet = new HashSet<NTuple<Descriptor>>();
1415 mapFlatMethodToRead.put(calleeFlatMethod, calleeReadSet);
1418 Set<NTuple<Descriptor>> calleeOverWriteSet = mapFlatMethodToOverWrite.get(calleeFlatMethod);
1420 if (calleeOverWriteSet == null) {
1421 calleeOverWriteSet = new HashSet<NTuple<Descriptor>>();
1422 mapFlatMethodToOverWrite.put(calleeFlatMethod, calleeOverWriteSet);
1425 Set<NTuple<Descriptor>> calleeWriteSet = mapFlatMethodToWrite.get(calleeFlatMethod);
1427 if (calleeWriteSet == null) {
1428 calleeWriteSet = new HashSet<NTuple<Descriptor>>();
1429 mapFlatMethodToWrite.put(calleeFlatMethod, calleeWriteSet);
1432 Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc =
1433 new Hashtable<Integer, TempDescriptor>();
1435 if (calleeFlatMethod.getMethod().isStatic()) {
1436 // static method does not have implicit 'this' arg
1439 for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
1440 TempDescriptor param = calleeFlatMethod.getParameter(i);
1441 mapParamIdx2ParamTempDesc.put(Integer.valueOf(i + offset), param);
1444 Set<NTuple<Descriptor>> calleeBoundReadSet =
1445 bindSet(calleeReadSet, mapParamIdx2ParamTempDesc, mapArgIdx2CallerArgHeapPath);
1446 // union of the current read set and the current callee's
1448 calleeUnionBoundReadSet.addAll(calleeBoundReadSet);
1449 Set<NTuple<Descriptor>> calleeBoundOverWriteSet =
1450 bindSet(calleeOverWriteSet, mapParamIdx2ParamTempDesc, mapArgIdx2CallerArgHeapPath);
1451 // intersection of the current overwrite set and the current
1454 merge(calleeIntersectBoundOverWriteSet, calleeBoundOverWriteSet);
1456 Set<NTuple<Descriptor>> boundWriteSetFromCallee =
1457 bindSet(calleeWriteSet, mapParamIdx2ParamTempDesc, mapArgIdx2CallerArgHeapPath);
1458 calleeBoundWriteSet.addAll(boundWriteSetFromCallee);
1465 private void checkFlag(boolean booleanValue, FlatNode fn, NTuple<Descriptor> hp) {
1467 // the definitely written analysis only takes care about locations that
1468 // are written to inside of the SSJava loop
1469 for (Iterator iterator = calleeBoundWriteSet.iterator(); iterator.hasNext();) {
1470 NTuple<Descriptor> write = (NTuple<Descriptor>) iterator.next();
1471 if (hp.startsWith(write)) {
1472 // it has write effect!
1476 + "There is a variable, which is reachable through references "
1478 + ", who comes back to the same read statement without being overwritten at the out-most iteration at "
1479 + methodContainingSSJavaLoop.getClassDesc().getSourceFileName() + "::"
1487 private void merge(Hashtable<NTuple<Descriptor>, Hashtable<FlatNode, Boolean>> curr,
1488 Hashtable<NTuple<Descriptor>, Hashtable<FlatNode, Boolean>> in) {
1490 Set<NTuple<Descriptor>> inKeySet = in.keySet();
1491 for (Iterator iterator = inKeySet.iterator(); iterator.hasNext();) {
1492 NTuple<Descriptor> inKey = (NTuple<Descriptor>) iterator.next();
1493 Hashtable<FlatNode, Boolean> inPair = in.get(inKey);
1495 Set<FlatNode> pairKeySet = inPair.keySet();
1496 for (Iterator iterator2 = pairKeySet.iterator(); iterator2.hasNext();) {
1497 FlatNode pairKey = (FlatNode) iterator2.next();
1498 Boolean inFlag = inPair.get(pairKey);
1500 Hashtable<FlatNode, Boolean> currPair = curr.get(inKey);
1501 if (currPair == null) {
1502 currPair = new Hashtable<FlatNode, Boolean>();
1503 curr.put(inKey, currPair);
1506 Boolean currFlag = currPair.get(pairKey);
1507 // by default, flag is set by false
1508 if (currFlag == null) {
1509 currFlag = Boolean.FALSE;
1511 currFlag = Boolean.valueOf(inFlag.booleanValue() | currFlag.booleanValue());
1512 currPair.put(pairKey, currFlag);
1519 private void methodReadOverWriteAnalysis() {
1520 // perform method READ/OVERWRITE analysis
1521 Set<MethodDescriptor> methodDescriptorsToAnalyze = new HashSet<MethodDescriptor>();
1522 methodDescriptorsToAnalyze.addAll(ssjava.getAnnotationRequireSet());
1524 sortedDescriptors = topologicalSort(methodDescriptorsToAnalyze);
1526 LinkedList<MethodDescriptor> descriptorListToAnalyze =
1527 (LinkedList<MethodDescriptor>) sortedDescriptors.clone();
1529 // no need to analyze method having ssjava loop
1530 // methodContainingSSJavaLoop = descriptorListToAnalyze.removeFirst();
1531 methodContainingSSJavaLoop = ssjava.getMethodContainingSSJavaLoop();
1533 // current descriptors to visit in fixed-point interprocedural analysis,
1535 // dependency in the call graph
1536 methodDescriptorsToVisitStack.clear();
1538 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
1539 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
1541 while (!descriptorListToAnalyze.isEmpty()) {
1542 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
1543 methodDescriptorsToVisitStack.add(md);
1546 // analyze scheduled methods until there are no more to visit
1547 while (!methodDescriptorsToVisitStack.isEmpty()) {
1548 // start to analyze leaf node
1549 MethodDescriptor md = methodDescriptorsToVisitStack.pop();
1550 FlatMethod fm = state.getMethodFlat(md);
1552 Set<NTuple<Descriptor>> readSet = new HashSet<NTuple<Descriptor>>();
1553 Set<NTuple<Descriptor>> overWriteSet = new HashSet<NTuple<Descriptor>>();
1554 Set<NTuple<Descriptor>> writeSet = new HashSet<NTuple<Descriptor>>();
1556 methodReadOverWrite_analyzeMethod(fm, readSet, overWriteSet, writeSet);
1558 Set<NTuple<Descriptor>> prevRead = mapFlatMethodToRead.get(fm);
1559 Set<NTuple<Descriptor>> prevOverWrite = mapFlatMethodToOverWrite.get(fm);
1560 Set<NTuple<Descriptor>> prevWrite = mapFlatMethodToWrite.get(fm);
1562 if (!(readSet.equals(prevRead) && overWriteSet.equals(prevOverWrite) && writeSet
1563 .equals(prevWrite))) {
1564 mapFlatMethodToRead.put(fm, readSet);
1565 mapFlatMethodToOverWrite.put(fm, overWriteSet);
1566 mapFlatMethodToWrite.put(fm, writeSet);
1568 // results for callee changed, so enqueue dependents caller for
1571 Iterator<MethodDescriptor> depsItr = getDependents(md).iterator();
1572 while (depsItr.hasNext()) {
1573 MethodDescriptor methodNext = depsItr.next();
1574 if (!methodDescriptorsToVisitStack.contains(methodNext)
1575 && methodDescriptorToVistSet.contains(methodNext)) {
1576 methodDescriptorsToVisitStack.add(methodNext);
1587 private void methodReadOverWrite_analyzeMethod(FlatMethod fm, Set<NTuple<Descriptor>> readSet,
1588 Set<NTuple<Descriptor>> overWriteSet, Set<NTuple<Descriptor>> writeSet) {
1589 if (state.SSJAVADEBUG) {
1590 System.out.println("Definitely written Analyzing: " + fm);
1593 // intraprocedural analysis
1594 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
1595 flatNodesToVisit.add(fm);
1597 while (!flatNodesToVisit.isEmpty()) {
1598 FlatNode fn = flatNodesToVisit.iterator().next();
1599 flatNodesToVisit.remove(fn);
1601 Set<NTuple<Descriptor>> curr = new HashSet<NTuple<Descriptor>>();
1603 for (int i = 0; i < fn.numPrev(); i++) {
1604 FlatNode prevFn = fn.getPrev(i);
1605 Set<NTuple<Descriptor>> in = mapFlatNodeToWrittenSet.get(prevFn);
1611 methodReadOverWrite_nodeActions(fn, curr, readSet, overWriteSet, writeSet);
1613 Set<NTuple<Descriptor>> writtenSetPrev = mapFlatNodeToWrittenSet.get(fn);
1614 if (!curr.equals(writtenSetPrev)) {
1615 mapFlatNodeToWrittenSet.put(fn, curr);
1616 for (int i = 0; i < fn.numNext(); i++) {
1617 FlatNode nn = fn.getNext(i);
1618 flatNodesToVisit.add(nn);
1626 private void methodReadOverWrite_nodeActions(FlatNode fn, Set<NTuple<Descriptor>> writtenSet,
1627 Set<NTuple<Descriptor>> readSet, Set<NTuple<Descriptor>> overWriteSet,
1628 Set<NTuple<Descriptor>> writeSet) {
1631 FieldDescriptor fld;
1633 switch (fn.kind()) {
1634 case FKind.FlatMethod: {
1636 // set up initial heap paths for method parameters
1637 FlatMethod fm = (FlatMethod) fn;
1638 for (int i = 0; i < fm.numParameters(); i++) {
1639 TempDescriptor param = fm.getParameter(i);
1640 NTuple<Descriptor> heapPath = new NTuple<Descriptor>();
1641 heapPath.add(param);
1642 mapHeapPath.put(param, heapPath);
1647 case FKind.FlatOpNode: {
1648 FlatOpNode fon = (FlatOpNode) fn;
1649 // for a normal assign node, need to propagate lhs's heap path to
1651 if (fon.getOp().getOp() == Operation.ASSIGN) {
1652 rhs = fon.getLeft();
1653 lhs = fon.getDest();
1655 NTuple<Descriptor> rhsHeapPath = mapHeapPath.get(rhs);
1656 if (rhsHeapPath != null) {
1657 mapHeapPath.put(lhs, mapHeapPath.get(rhs));
1664 case FKind.FlatElementNode:
1665 case FKind.FlatFieldNode: {
1669 if (fn.kind() == FKind.FlatFieldNode) {
1670 FlatFieldNode ffn = (FlatFieldNode) fn;
1673 fld = ffn.getField();
1675 FlatElementNode fen = (FlatElementNode) fn;
1678 TypeDescriptor td = rhs.getType().dereference();
1679 fld = getArrayField(td);
1682 if (fld.isFinal() /* && fld.isStatic() */) {
1683 // if field is final and static, no need to check
1688 NTuple<Descriptor> srcHeapPath = mapHeapPath.get(rhs);
1689 if (srcHeapPath != null) {
1690 // if lhs srcHeapPath is null, it means that it is not reachable from
1691 // callee's parameters. so just ignore it
1693 NTuple<Descriptor> readingHeapPath = new NTuple<Descriptor>(srcHeapPath.getList());
1694 readingHeapPath.add(fld);
1695 mapHeapPath.put(lhs, readingHeapPath);
1698 if (fld.getType().isImmutable()) {
1699 // if WT doesnot have hp(x.f), add hp(x.f) to READ
1700 if (!writtenSet.contains(readingHeapPath)) {
1701 readSet.add(readingHeapPath);
1705 // no need to kill hp(x.f) from WT
1711 case FKind.FlatSetFieldNode:
1712 case FKind.FlatSetElementNode: {
1716 if (fn.kind() == FKind.FlatSetFieldNode) {
1717 FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
1718 lhs = fsfn.getDst();
1719 fld = fsfn.getField();
1720 rhs = fsfn.getSrc();
1722 FlatSetElementNode fsen = (FlatSetElementNode) fn;
1723 lhs = fsen.getDst();
1724 rhs = fsen.getSrc();
1725 TypeDescriptor td = lhs.getType().dereference();
1726 fld = getArrayField(td);
1730 NTuple<Descriptor> lhsHeapPath = mapHeapPath.get(lhs);
1731 if (lhsHeapPath != null) {
1732 // if lhs heap path is null, it means that it is not reachable from
1733 // callee's parameters. so just ignore it
1734 NTuple<Descriptor> newHeapPath = new NTuple<Descriptor>(lhsHeapPath.getList());
1735 newHeapPath.add(fld);
1736 mapHeapPath.put(fld, newHeapPath);
1739 // need to add hp(y) to WT
1740 writtenSet.add(newHeapPath);
1742 writeSet.add(newHeapPath);
1748 case FKind.FlatCall: {
1750 FlatCall fc = (FlatCall) fn;
1752 bindHeapPathCallerArgWithCaleeParam(fc);
1754 // add heap path, which is an element of READ_bound set and is not
1756 // element of WT set, to the caller's READ set
1757 for (Iterator iterator = calleeUnionBoundReadSet.iterator(); iterator.hasNext();) {
1758 NTuple<Descriptor> read = (NTuple<Descriptor>) iterator.next();
1759 if (!writtenSet.contains(read)) {
1764 // add heap path, which is an element of OVERWRITE_bound set, to the
1766 for (Iterator iterator = calleeIntersectBoundOverWriteSet.iterator(); iterator.hasNext();) {
1767 NTuple<Descriptor> write = (NTuple<Descriptor>) iterator.next();
1768 writtenSet.add(write);
1771 // add heap path, which is an element of WRITE_BOUND set, to the
1772 // caller's writeSet
1773 for (Iterator iterator = calleeBoundWriteSet.iterator(); iterator.hasNext();) {
1774 NTuple<Descriptor> write = (NTuple<Descriptor>) iterator.next();
1775 writeSet.add(write);
1781 case FKind.FlatExit: {
1782 // merge the current written set with OVERWRITE set
1783 merge(overWriteSet, writtenSet);
1791 static public FieldDescriptor getArrayField(TypeDescriptor td) {
1792 FieldDescriptor fd = mapTypeToArrayField.get(td);
1795 new FieldDescriptor(new Modifiers(Modifiers.PUBLIC), td, arrayElementFieldName, null,
1797 mapTypeToArrayField.put(td, fd);
1802 private void mergeSharedLocationAnaylsis(ClearingSummary curr, Set<ClearingSummary> inSet) {
1803 if (inSet.size() == 0) {
1806 Hashtable<Pair<NTuple<Descriptor>, Location>, Boolean> mapHeapPathLoc2Flag =
1807 new Hashtable<Pair<NTuple<Descriptor>, Location>, Boolean>();
1809 for (Iterator inIterator = inSet.iterator(); inIterator.hasNext();) {
1811 ClearingSummary inTable = (ClearingSummary) inIterator.next();
1813 Set<NTuple<Descriptor>> keySet = inTable.keySet();
1815 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1816 NTuple<Descriptor> hpKey = (NTuple<Descriptor>) iterator.next();
1817 SharedStatus inState = inTable.get(hpKey);
1818 SharedStatus currState = curr.get(hpKey);
1819 if (currState == null) {
1820 currState = new SharedStatus();
1821 curr.put(hpKey, currState);
1824 currState.merge(inState);
1826 Set<Location> locSet = inState.getMap().keySet();
1827 for (Iterator iterator2 = locSet.iterator(); iterator2.hasNext();) {
1828 Location loc = (Location) iterator2.next();
1829 Pair<Set<Descriptor>, Boolean> pair = inState.getMap().get(loc);
1830 boolean inFlag = pair.getSecond().booleanValue();
1832 Pair<NTuple<Descriptor>, Location> flagKey =
1833 new Pair<NTuple<Descriptor>, Location>(hpKey, loc);
1834 Boolean current = mapHeapPathLoc2Flag.get(flagKey);
1835 if (current == null) {
1836 current = new Boolean(true);
1838 boolean newInFlag = current.booleanValue() & inFlag;
1839 mapHeapPathLoc2Flag.put(flagKey, Boolean.valueOf(newInFlag));
1846 // merge flag status
1847 Set<NTuple<Descriptor>> hpKeySet = curr.keySet();
1848 for (Iterator iterator = hpKeySet.iterator(); iterator.hasNext();) {
1849 NTuple<Descriptor> hpKey = (NTuple<Descriptor>) iterator.next();
1850 SharedStatus currState = curr.get(hpKey);
1851 Set<Location> locKeySet = currState.getMap().keySet();
1852 for (Iterator iterator2 = locKeySet.iterator(); iterator2.hasNext();) {
1853 Location locKey = (Location) iterator2.next();
1854 Pair<Set<Descriptor>, Boolean> pair = currState.getMap().get(locKey);
1855 boolean currentFlag = pair.getSecond().booleanValue();
1856 Boolean inFlag = mapHeapPathLoc2Flag.get(new Pair(hpKey, locKey));
1857 if (inFlag != null) {
1858 boolean newFlag = currentFlag | inFlag.booleanValue();
1859 if (currentFlag != newFlag) {
1860 currState.getMap().put(locKey, new Pair(pair.getFirst(), new Boolean(newFlag)));
1868 private void merge(Set<NTuple<Descriptor>> curr, Set<NTuple<Descriptor>> in) {
1869 if (curr.isEmpty()) {
1870 // WrittenSet has a special initial value which covers all possible
1872 // For the first time of intersection, we can take all previous set
1875 // otherwise, current set is the intersection of the two sets
1881 // combine two heap path
1882 private NTuple<Descriptor> combine(NTuple<Descriptor> callerIn, NTuple<Descriptor> calleeIn) {
1883 NTuple<Descriptor> combined = new NTuple<Descriptor>();
1885 for (int i = 0; i < callerIn.size(); i++) {
1886 combined.add(callerIn.get(i));
1889 // the first element of callee's heap path represents parameter
1890 // so we skip the first one since it is already added from caller's heap
1892 for (int i = 1; i < calleeIn.size(); i++) {
1893 combined.add(calleeIn.get(i));
1899 private Set<NTuple<Descriptor>> bindSet(Set<NTuple<Descriptor>> calleeSet,
1900 Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc,
1901 Hashtable<Integer, NTuple<Descriptor>> mapCallerArgIdx2HeapPath) {
1903 Set<NTuple<Descriptor>> boundedCalleeSet = new HashSet<NTuple<Descriptor>>();
1905 Set<Integer> keySet = mapCallerArgIdx2HeapPath.keySet();
1906 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1907 Integer idx = (Integer) iterator.next();
1909 NTuple<Descriptor> callerArgHeapPath = mapCallerArgIdx2HeapPath.get(idx);
1910 TempDescriptor calleeParam = mapParamIdx2ParamTempDesc.get(idx);
1911 for (Iterator iterator2 = calleeSet.iterator(); iterator2.hasNext();) {
1912 NTuple<Descriptor> element = (NTuple<Descriptor>) iterator2.next();
1913 if (element.startsWith(calleeParam)) {
1914 NTuple<Descriptor> boundElement = combine(callerArgHeapPath, element);
1915 boundedCalleeSet.add(boundElement);
1921 return boundedCalleeSet;
1925 // Borrowed it from disjoint analysis
1926 private LinkedList<MethodDescriptor> topologicalSort(Set<MethodDescriptor> toSort) {
1928 Set<MethodDescriptor> discovered = new HashSet<MethodDescriptor>();
1930 LinkedList<MethodDescriptor> sorted = new LinkedList<MethodDescriptor>();
1932 Iterator<MethodDescriptor> itr = toSort.iterator();
1933 while (itr.hasNext()) {
1934 MethodDescriptor d = itr.next();
1936 if (!discovered.contains(d)) {
1937 dfsVisit(d, toSort, sorted, discovered);
1944 // While we're doing DFS on call graph, remember
1945 // dependencies for efficient queuing of methods
1946 // during interprocedural analysis:
1948 // a dependent of a method decriptor d for this analysis is:
1949 // 1) a method or task that invokes d
1950 // 2) in the descriptorsToAnalyze set
1951 private void dfsVisit(MethodDescriptor md, Set<MethodDescriptor> toSort,
1952 LinkedList<MethodDescriptor> sorted, Set<MethodDescriptor> discovered) {
1956 Iterator itr = callGraph.getCallerSet(md).iterator();
1957 while (itr.hasNext()) {
1958 MethodDescriptor dCaller = (MethodDescriptor) itr.next();
1959 // only consider callers in the original set to analyze
1960 if (!toSort.contains(dCaller)) {
1963 if (!discovered.contains(dCaller)) {
1964 addDependent(md, // callee
1968 dfsVisit(dCaller, toSort, sorted, discovered);
1972 // for leaf-nodes last now!
1976 // a dependent of a method decriptor d for this analysis is:
1977 // 1) a method or task that invokes d
1978 // 2) in the descriptorsToAnalyze set
1979 private void addDependent(MethodDescriptor callee, MethodDescriptor caller) {
1980 Set<MethodDescriptor> deps = mapDescriptorToSetDependents.get(callee);
1982 deps = new HashSet<MethodDescriptor>();
1985 mapDescriptorToSetDependents.put(callee, deps);
1988 private Set<MethodDescriptor> getDependents(MethodDescriptor callee) {
1989 Set<MethodDescriptor> deps = mapDescriptorToSetDependents.get(callee);
1991 deps = new HashSet<MethodDescriptor>();
1992 mapDescriptorToSetDependents.put(callee, deps);
1997 private NTuple<Descriptor> computePath(TempDescriptor td) {
1998 // generate proper path fot input td
1999 // if td is local variable, it just generate one element tuple path
2000 if (mapHeapPath.containsKey(td)) {
2001 return mapHeapPath.get(td);
2003 NTuple<Descriptor> path = new NTuple<Descriptor>();