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 flat method to the READ that is the set of heap path that is
61 // expected to be written before method invocation
62 private Hashtable<FlatMethod, Set<NTuple<Descriptor>>> mapFlatMethodToReadSet;
64 // maps a flat method to the must-write set that is the set of heap path that
65 // is overwritten on every possible path during method invocation
66 private Hashtable<FlatMethod, Set<NTuple<Descriptor>>> mapFlatMethodToMustWriteSet;
68 // maps a flat method to the may-wirte set that is the set of heap path that
69 // might be written to
70 private Hashtable<FlatMethod, Set<NTuple<Descriptor>>> mapFlatMethodToMayWriteSet;
72 // maps a call site to the read set contributed by all callees
73 private Hashtable<FlatNode, Set<NTuple<Descriptor>>> mapFlatNodeToBoundReadSet;
75 // maps a call site to the must write set contributed by all callees
76 private Hashtable<FlatNode, Set<NTuple<Descriptor>>> mapFlatNodeToBoundMustWriteSet;
78 // maps a call site to the may read set contributed by all callees
79 private Hashtable<FlatNode, Set<NTuple<Descriptor>>> mapFlatNodeToBoundMayWriteSet;
81 // points to method containing SSJAVA Loop
82 private MethodDescriptor methodContainingSSJavaLoop;
84 // maps a flatnode to definitely written analysis mapping M
85 private Hashtable<FlatNode, Hashtable<NTuple<Descriptor>, Set<WriteAge>>> mapFlatNodetoEventLoopMap;
87 // maps a method descriptor to its current summary during the analysis
88 // then analysis reaches fixed-point, this mapping will have the final summary
89 // for each method descriptor
90 private Hashtable<MethodDescriptor, ClearingSummary> mapMethodDescriptorToCompleteClearingSummary;
92 // maps a method descriptor to the merged incoming caller's current
94 private Hashtable<MethodDescriptor, ClearingSummary> mapMethodDescriptorToInitialClearingSummary;
96 // maps a flat node to current partial results
97 private Hashtable<FlatNode, ClearingSummary> mapFlatNodeToClearingSummary;
99 // maps shared location to the set of descriptors which belong to the shared
102 // keep current descriptors to visit in fixed-point interprocedural analysis,
103 private Stack<MethodDescriptor> methodDescriptorsToVisitStack;
105 // when analyzing flatcall, need to re-schedule set of callee
106 private Set<MethodDescriptor> calleesToEnqueue;
108 private Set<ReadSummary> possibleCalleeReadSummarySetToCaller;
110 public static final String arrayElementFieldName = "___element_";
111 static protected Hashtable<TypeDescriptor, FieldDescriptor> mapTypeToArrayField;
113 private Set<ClearingSummary> possibleCalleeCompleteSummarySetToCaller;
115 // maps a method descriptor to the merged incoming caller's current
117 // it is for setting clearance flag when all read set is overwritten
118 private Hashtable<MethodDescriptor, ReadSummary> mapMethodDescriptorToReadSummary;
120 private Hashtable<Location, Set<Descriptor>> mapSharedLocationToCoverSet;
122 private LinkedList<MethodDescriptor> sortedDescriptors;
124 private FlatNode ssjavaLoopEntrance;
125 private LoopFinder ssjavaLoop;
126 private Set<FlatNode> loopIncElements;
128 private Set<NTuple<Descriptor>> calleeUnionBoundReadSet;
129 private Set<NTuple<Descriptor>> calleeIntersectBoundMustWriteSet;
130 private Set<NTuple<Descriptor>> calleeUnionBoundMayWriteSet;
132 private Hashtable<Descriptor, Location> mapDescToLocation;
134 private TempDescriptor LOCAL;
136 public static int MAXAGE = 1;
138 public DefinitelyWrittenCheck(SSJavaAnalysis ssjava, State state) {
140 this.ssjava = ssjava;
141 this.callGraph = ssjava.getCallGraph();
142 this.mapFlatNodeToMustWriteSet = new Hashtable<FlatNode, Set<NTuple<Descriptor>>>();
143 this.mapDescriptorToSetDependents = new Hashtable<Descriptor, Set<MethodDescriptor>>();
144 this.mapHeapPath = new Hashtable<Descriptor, NTuple<Descriptor>>();
145 this.mapFlatMethodToReadSet = new Hashtable<FlatMethod, Set<NTuple<Descriptor>>>();
146 this.mapFlatMethodToMustWriteSet = new Hashtable<FlatMethod, Set<NTuple<Descriptor>>>();
147 this.mapFlatMethodToMayWriteSet = new Hashtable<FlatMethod, Set<NTuple<Descriptor>>>();
148 this.mapFlatNodetoEventLoopMap =
149 new Hashtable<FlatNode, Hashtable<NTuple<Descriptor>, Set<WriteAge>>>();
150 this.calleeUnionBoundReadSet = new HashSet<NTuple<Descriptor>>();
151 this.calleeIntersectBoundMustWriteSet = new HashSet<NTuple<Descriptor>>();
152 this.calleeUnionBoundMayWriteSet = new HashSet<NTuple<Descriptor>>();
154 this.mapMethodDescriptorToCompleteClearingSummary =
155 new Hashtable<MethodDescriptor, ClearingSummary>();
156 this.mapMethodDescriptorToInitialClearingSummary =
157 new Hashtable<MethodDescriptor, ClearingSummary>();
158 this.methodDescriptorsToVisitStack = new Stack<MethodDescriptor>();
159 this.calleesToEnqueue = new HashSet<MethodDescriptor>();
160 this.possibleCalleeCompleteSummarySetToCaller = new HashSet<ClearingSummary>();
161 this.mapTypeToArrayField = new Hashtable<TypeDescriptor, FieldDescriptor>();
162 this.LOCAL = new TempDescriptor("LOCAL");
163 this.mapDescToLocation = new Hashtable<Descriptor, Location>();
164 this.possibleCalleeReadSummarySetToCaller = new HashSet<ReadSummary>();
165 this.mapMethodDescriptorToReadSummary = new Hashtable<MethodDescriptor, ReadSummary>();
166 this.mapFlatNodeToBoundReadSet = new Hashtable<FlatNode, Set<NTuple<Descriptor>>>();
167 this.mapFlatNodeToBoundMustWriteSet = new Hashtable<FlatNode, Set<NTuple<Descriptor>>>();
168 this.mapFlatNodeToBoundMayWriteSet = new Hashtable<FlatNode, Set<NTuple<Descriptor>>>();
169 this.mapSharedLocationToCoverSet = new Hashtable<Location, Set<Descriptor>>();
172 public void definitelyWrittenCheck() {
173 if (!ssjava.getAnnotationRequireSet().isEmpty()) {
175 methodReadWriteSetAnalysis();
176 methodReadWriteSetAnalysisToEventLoopBody();
178 computeSharedCoverSet();
180 System.out.println("#");
181 System.out.println(mapSharedLocationToCoverSet);
183 // methodReadWriteSetAnalysis();
184 // methodReadWriteSetAnalysisToEventLoopBody();
185 // eventLoopAnalysis();
187 // sharedLocationAnalysis();
188 // checkSharedLocationResult();
192 private void checkSharedLocationResult() {
194 // mapping of method containing ssjava loop has the final result of
195 // shared location analysis
197 ClearingSummary result =
198 mapMethodDescriptorToCompleteClearingSummary.get(methodContainingSSJavaLoop);
200 String str = generateNotClearedResult(result);
201 if (str.length() > 0) {
203 "Following concrete locations of the shared abstract location are not cleared at the same time:\n"
209 private String generateNotClearedResult(ClearingSummary result) {
210 Set<NTuple<Descriptor>> keySet = result.keySet();
212 StringBuffer str = new StringBuffer();
213 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
214 NTuple<Descriptor> hpKey = (NTuple<Descriptor>) iterator.next();
215 SharedStatus status = result.get(hpKey);
216 Hashtable<Location, Pair<Set<Descriptor>, Boolean>> map = status.getMap();
217 Set<Location> locKeySet = map.keySet();
218 for (Iterator iterator2 = locKeySet.iterator(); iterator2.hasNext();) {
219 Location locKey = (Location) iterator2.next();
220 if (status.haveWriteEffect(locKey)) {
221 Pair<Set<Descriptor>, Boolean> pair = map.get(locKey);
222 if (!pair.getSecond().booleanValue()) {
224 str.append("- Concrete locations of the shared location '" + locKey
225 + "' are not cleared out, which are reachable through the heap path '" + hpKey
232 return str.toString();
236 private void writeReadMapFile() {
238 String fileName = "SharedLocationReadMap";
241 BufferedWriter bw = new BufferedWriter(new FileWriter(fileName + ".txt"));
243 Set<MethodDescriptor> keySet = mapMethodDescriptorToReadSummary.keySet();
244 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
245 MethodDescriptor mdKey = (MethodDescriptor) iterator.next();
246 ReadSummary summary = mapMethodDescriptorToReadSummary.get(mdKey);
247 bw.write("Method " + mdKey + "::\n");
248 bw.write(summary + "\n\n");
251 } catch (IOException e) {
257 private void sharedLocationAnalysis() {
258 // verify that all concrete locations of shared location are cleared out at
259 // the same time once per the out-most loop
261 computeSharedCoverSet();
263 if (state.SSJAVADEBUG) {
267 // methodDescriptorsToVisitStack.clear();
268 // methodDescriptorsToVisitStack.add(sortedDescriptors.peekFirst());
270 LinkedList<MethodDescriptor> descriptorListToAnalyze =
271 (LinkedList<MethodDescriptor>) sortedDescriptors.clone();
273 // current descriptors to visit in fixed-point interprocedural analysis,
275 // dependency in the call graph
276 methodDescriptorsToVisitStack.clear();
278 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
279 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
281 while (!descriptorListToAnalyze.isEmpty()) {
282 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
283 methodDescriptorsToVisitStack.add(md);
286 // analyze scheduled methods until there are no more to visit
287 while (!methodDescriptorsToVisitStack.isEmpty()) {
288 MethodDescriptor md = methodDescriptorsToVisitStack.pop();
290 ClearingSummary completeSummary =
291 sharedLocation_analyzeMethod(md, (md.equals(methodContainingSSJavaLoop)));
293 ClearingSummary prevCompleteSummary = mapMethodDescriptorToCompleteClearingSummary.get(md);
295 if (!completeSummary.equals(prevCompleteSummary)) {
297 mapMethodDescriptorToCompleteClearingSummary.put(md, completeSummary);
299 // results for callee changed, so enqueue dependents caller for
301 Iterator<MethodDescriptor> depsItr = getDependents(md).iterator();
302 while (depsItr.hasNext()) {
303 MethodDescriptor methodNext = depsItr.next();
304 if (!methodDescriptorsToVisitStack.contains(methodNext)) {
305 methodDescriptorsToVisitStack.add(methodNext);
309 // if there is set of callee to be analyzed,
310 // add this set into the top of stack
311 Iterator<MethodDescriptor> calleeIter = calleesToEnqueue.iterator();
312 while (calleeIter.hasNext()) {
313 MethodDescriptor mdNext = calleeIter.next();
314 if (!methodDescriptorsToVisitStack.contains(mdNext)) {
315 methodDescriptorsToVisitStack.add(mdNext);
318 calleesToEnqueue.clear();
326 private ClearingSummary sharedLocation_analyzeMethod(MethodDescriptor md,
327 boolean onlyVisitSSJavaLoop) {
329 if (state.SSJAVADEBUG) {
330 System.out.println("SSJAVA: Definite clearance for shared locations Analyzing: " + md);
333 FlatMethod fm = state.getMethodFlat(md);
335 // intraprocedural analysis
336 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
338 // start a new mapping of partial results for each flat node
339 mapFlatNodeToClearingSummary = new Hashtable<FlatNode, ClearingSummary>();
341 if (onlyVisitSSJavaLoop) {
342 flatNodesToVisit.add(ssjavaLoopEntrance);
344 flatNodesToVisit.add(fm);
347 Set<FlatNode> returnNodeSet = new HashSet<FlatNode>();
349 while (!flatNodesToVisit.isEmpty()) {
350 FlatNode fn = flatNodesToVisit.iterator().next();
351 flatNodesToVisit.remove(fn);
353 ClearingSummary curr = new ClearingSummary();
355 Set<ClearingSummary> prevSet = new HashSet<ClearingSummary>();
356 for (int i = 0; i < fn.numPrev(); i++) {
357 FlatNode prevFn = fn.getPrev(i);
358 ClearingSummary in = mapFlatNodeToClearingSummary.get(prevFn);
363 mergeSharedLocationAnaylsis(curr, prevSet);
365 sharedLocation_nodeActions(md, fn, curr, returnNodeSet, onlyVisitSSJavaLoop);
366 ClearingSummary clearingPrev = mapFlatNodeToClearingSummary.get(fn);
368 if (!curr.equals(clearingPrev)) {
369 mapFlatNodeToClearingSummary.put(fn, curr);
371 for (int i = 0; i < fn.numNext(); i++) {
372 FlatNode nn = fn.getNext(i);
374 if (!onlyVisitSSJavaLoop || (onlyVisitSSJavaLoop && loopIncElements.contains(nn))) {
375 flatNodesToVisit.add(nn);
383 ClearingSummary completeSummary = new ClearingSummary();
384 Set<ClearingSummary> summarySet = new HashSet<ClearingSummary>();
386 if (onlyVisitSSJavaLoop) {
387 // when analyzing ssjava loop,
388 // complete summary is merging of all previous nodes of ssjava loop
390 for (int i = 0; i < ssjavaLoopEntrance.numPrev(); i++) {
391 ClearingSummary frnSummary =
392 mapFlatNodeToClearingSummary.get(ssjavaLoopEntrance.getPrev(i));
393 if (frnSummary != null) {
394 summarySet.add(frnSummary);
398 // merging all exit node summary into the complete summary
399 if (!returnNodeSet.isEmpty()) {
400 for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
401 FlatNode frn = (FlatNode) iterator.next();
402 ClearingSummary frnSummary = mapFlatNodeToClearingSummary.get(frn);
403 summarySet.add(frnSummary);
407 mergeSharedLocationAnaylsis(completeSummary, summarySet);
409 return completeSummary;
412 private void sharedLocation_nodeActions(MethodDescriptor md, FlatNode fn, ClearingSummary curr,
413 Set<FlatNode> returnNodeSet, boolean isSSJavaLoop) {
420 case FKind.FlatMethod: {
421 FlatMethod fm = (FlatMethod) fn;
423 ClearingSummary summaryFromCaller =
424 mapMethodDescriptorToInitialClearingSummary.get(fm.getMethod());
426 Set<ClearingSummary> inSet = new HashSet<ClearingSummary>();
427 if (summaryFromCaller != null) {
428 inSet.add(summaryFromCaller);
429 mergeSharedLocationAnaylsis(curr, inSet);
435 case FKind.FlatOpNode: {
436 FlatOpNode fon = (FlatOpNode) fn;
440 if (fon.getOp().getOp() == Operation.ASSIGN) {
441 if (rhs.getType().isImmutable() && isSSJavaLoop) {
442 // in ssjavaloop, we need to take care about reading local variables!
443 NTuple<Descriptor> rhsHeapPath = new NTuple<Descriptor>();
444 NTuple<Descriptor> lhsHeapPath = new NTuple<Descriptor>();
445 rhsHeapPath.add(LOCAL);
446 lhsHeapPath.add(LOCAL);
447 if (!lhs.getSymbol().startsWith("neverused")) {
448 readLocation(md, curr, rhsHeapPath, getLocation(rhs), rhs);
449 writeLocation(md, curr, lhsHeapPath, getLocation(lhs), lhs);
457 case FKind.FlatFieldNode:
458 case FKind.FlatElementNode: {
460 if (fn.kind() == FKind.FlatFieldNode) {
461 FlatFieldNode ffn = (FlatFieldNode) fn;
464 fld = ffn.getField();
466 FlatElementNode fen = (FlatElementNode) fn;
469 TypeDescriptor td = rhs.getType().dereference();
470 fld = getArrayField(td);
474 NTuple<Descriptor> srcHeapPath = mapHeapPath.get(rhs);
476 if (srcHeapPath != null) {
477 // if lhs srcHeapPath is null, it means that it is not reachable from
478 // callee's parameters. so just ignore it
479 NTuple<Descriptor> fldHeapPath = new NTuple<Descriptor>(srcHeapPath.getList());
481 if (!fld.getType().isArray() && fld.getType().isImmutable()) {
483 if (fn.kind() == FKind.FlatElementNode) {
484 // array element read case
485 NTuple<Descriptor> newHeapPath = new NTuple<Descriptor>();
486 for (int i = 0; i < fldHeapPath.size() - 1; i++) {
487 newHeapPath.add(fldHeapPath.get(i));
490 Descriptor desc = fldHeapPath.get(fldHeapPath.size() - 1);
491 if (desc instanceof FieldDescriptor) {
492 fld = (FieldDescriptor) desc;
493 fldHeapPath = newHeapPath;
494 loc = getLocation(fld);
495 readLocation(md, curr, fldHeapPath, loc, fld);
498 loc = getLocation(fld);
499 readLocation(md, curr, fldHeapPath, loc, fld);
503 if (fn.kind() != FKind.FlatElementNode) {
504 // if it is multi dimensional array, do not need to add heap path
505 // because all accesses from the same array is represented by
507 fldHeapPath.add(fld);
509 mapHeapPath.put(lhs, fldHeapPath);
519 case FKind.FlatSetFieldNode:
520 case FKind.FlatSetElementNode: {
522 if (fn.kind() == FKind.FlatSetFieldNode) {
523 FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
525 fld = fsfn.getField();
528 FlatSetElementNode fsen = (FlatSetElementNode) fn;
531 TypeDescriptor td = lhs.getType().dereference();
532 fld = getArrayField(td);
536 NTuple<Descriptor> lhsHeapPath = computePath(lhs);
537 NTuple<Descriptor> fldHeapPath = new NTuple<Descriptor>(lhsHeapPath.getList());
538 if (fld.getType().isImmutable()) {
539 writeLocation(md, curr, fldHeapPath, getLocation(fld), fld);
541 Descriptor desc = fldHeapPath.get(fldHeapPath.size() - 1);
542 if (desc instanceof FieldDescriptor) {
543 NTuple<Descriptor> arrayPath = new NTuple<Descriptor>();
544 for (int i = 0; i < fldHeapPath.size() - 1; i++) {
545 arrayPath.add(fldHeapPath.get(i));
547 SharedStatus state = getState(curr, arrayPath);
548 state.setWriteEffect(getLocation(desc));
552 // updates reference field case:
553 fldHeapPath.add(fld);
554 updateWriteEffectOnReferenceField(curr, fldHeapPath);
560 case FKind.FlatCall: {
562 FlatCall fc = (FlatCall) fn;
564 if (ssjava.isSSJavaUtil(fc.getMethod().getClassDesc())) {
566 // have write effects on the first argument
568 if (fc.getArg(0).getType().isArray()) {
569 // updates reference field case:
570 // 2. if there exists a tuple t in sharing summary that starts with
571 // hp(x) then, set flag of tuple t to 'true'
572 NTuple<Descriptor> argHeapPath = computePath(fc.getArg(0));
574 Location loc = getLocation(fc.getArg(0));
575 NTuple<Descriptor> newHeapPath = new NTuple<Descriptor>();
576 for (int i = 0; i < argHeapPath.size() - 1; i++) {
577 newHeapPath.add(argHeapPath.get(i));
579 fld = (FieldDescriptor) argHeapPath.get(argHeapPath.size() - 1);
580 argHeapPath = newHeapPath;
582 writeLocation(md, curr, argHeapPath, loc, fld);
586 // find out the set of callees
587 MethodDescriptor mdCallee = fc.getMethod();
588 FlatMethod fmCallee = state.getMethodFlat(mdCallee);
589 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
590 setPossibleCallees.addAll(callGraph.getMethods(mdCallee));
592 possibleCalleeCompleteSummarySetToCaller.clear();
594 for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
595 MethodDescriptor mdPossibleCallee = (MethodDescriptor) iterator.next();
596 FlatMethod calleeFlatMethod = state.getMethodFlat(mdPossibleCallee);
598 addDependent(mdPossibleCallee, // callee
601 calleesToEnqueue.add(mdPossibleCallee);
603 // updates possible callee's initial summary using caller's current
605 ClearingSummary prevCalleeInitSummary =
606 mapMethodDescriptorToInitialClearingSummary.get(mdPossibleCallee);
608 ClearingSummary calleeInitSummary =
609 bindHeapPathOfCalleeCallerEffects(fc, calleeFlatMethod, curr);
611 Set<ClearingSummary> inSet = new HashSet<ClearingSummary>();
612 if (prevCalleeInitSummary != null) {
613 inSet.add(prevCalleeInitSummary);
614 mergeSharedLocationAnaylsis(calleeInitSummary, inSet);
617 // if changes, update the init summary
618 // and reschedule the callee for analysis
619 if (!calleeInitSummary.equals(prevCalleeInitSummary)) {
621 if (!methodDescriptorsToVisitStack.contains(mdPossibleCallee)) {
622 methodDescriptorsToVisitStack.add(mdPossibleCallee);
625 mapMethodDescriptorToInitialClearingSummary.put(mdPossibleCallee, calleeInitSummary);
630 // contribute callee's writing effects to the caller
631 mergeSharedLocationAnaylsis(curr, possibleCalleeCompleteSummarySetToCaller);
638 case FKind.FlatReturnNode: {
639 returnNodeSet.add(fn);
647 private void updateWriteEffectOnReferenceField(ClearingSummary curr, NTuple<Descriptor> heapPath) {
649 // 2. if there exists a tuple t in sharing summary that starts with
650 // hp(x) then, set flag of tuple t to 'true'
651 Set<NTuple<Descriptor>> hpKeySet = curr.keySet();
652 for (Iterator iterator = hpKeySet.iterator(); iterator.hasNext();) {
653 NTuple<Descriptor> hpKey = (NTuple<Descriptor>) iterator.next();
654 if (hpKey.startsWith(heapPath)) {
655 curr.get(hpKey).updateFlag(true);
661 private ClearingSummary bindHeapPathOfCalleeCallerEffects(FlatCall fc,
662 FlatMethod calleeFlatMethod, ClearingSummary curr) {
664 ClearingSummary boundSet = new ClearingSummary();
666 // create mapping from arg idx to its heap paths
667 Hashtable<Integer, NTuple<Descriptor>> mapArgIdx2CallerArgHeapPath =
668 new Hashtable<Integer, NTuple<Descriptor>>();
670 if (fc.getThis() != null) {
671 // arg idx is starting from 'this' arg
672 NTuple<Descriptor> thisHeapPath = mapHeapPath.get(fc.getThis());
673 if (thisHeapPath == null) {
674 // method is called without creating new flat node representing 'this'
675 thisHeapPath = new NTuple<Descriptor>();
676 thisHeapPath.add(fc.getThis());
679 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(0), thisHeapPath);
682 for (int i = 0; i < fc.numArgs(); i++) {
683 TempDescriptor arg = fc.getArg(i);
684 NTuple<Descriptor> argHeapPath = computePath(arg);
685 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(i + 1), argHeapPath);
688 Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc =
689 new Hashtable<Integer, TempDescriptor>();
691 if (calleeFlatMethod.getMethod().isStatic()) {
692 // static method does not have implicit 'this' arg
695 for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
696 TempDescriptor param = calleeFlatMethod.getParameter(i);
697 mapParamIdx2ParamTempDesc.put(Integer.valueOf(i + offset), param);
700 // binding caller's writing effects to callee's params
701 for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
702 NTuple<Descriptor> argHeapPath = mapArgIdx2CallerArgHeapPath.get(Integer.valueOf(i));
704 if (argHeapPath != null) {
705 // if method is static, the first argument is nulll because static
706 // method does not have implicit "THIS" arg
707 TempDescriptor calleeParamHeapPath = mapParamIdx2ParamTempDesc.get(Integer.valueOf(i));
709 // iterate over caller's writing effect set
710 Set<NTuple<Descriptor>> hpKeySet = curr.keySet();
711 for (Iterator iterator = hpKeySet.iterator(); iterator.hasNext();) {
712 NTuple<Descriptor> hpKey = (NTuple<Descriptor>) iterator.next();
713 // current element is reachable caller's arg
714 // so need to bind it to the caller's side and add it to the
717 if (hpKey.startsWith(argHeapPath)) {
718 NTuple<Descriptor> boundHeapPath = replace(hpKey, argHeapPath, calleeParamHeapPath);
719 boundSet.put(boundHeapPath, curr.get(hpKey).clone());
727 // contribute callee's complete summary into the caller's current summary
728 ClearingSummary calleeCompleteSummary =
729 mapMethodDescriptorToCompleteClearingSummary.get(calleeFlatMethod.getMethod());
730 if (calleeCompleteSummary != null) {
731 ClearingSummary boundCalleeEfffects = new ClearingSummary();
732 for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
733 NTuple<Descriptor> argHeapPath = mapArgIdx2CallerArgHeapPath.get(Integer.valueOf(i));
735 if (argHeapPath != null) {
736 // if method is static, the first argument is nulll because static
737 // method does not have implicit "THIS" arg
738 TempDescriptor calleeParamHeapPath = mapParamIdx2ParamTempDesc.get(Integer.valueOf(i));
740 // iterate over callee's writing effect set
741 Set<NTuple<Descriptor>> hpKeySet = calleeCompleteSummary.keySet();
742 for (Iterator iterator = hpKeySet.iterator(); iterator.hasNext();) {
743 NTuple<Descriptor> hpKey = (NTuple<Descriptor>) iterator.next();
744 // current element is reachable caller's arg
745 // so need to bind it to the caller's side and add it to the
748 if (hpKey.startsWith(calleeParamHeapPath)) {
750 NTuple<Descriptor> boundHeapPathForCaller = replace(hpKey, argHeapPath);
752 boundCalleeEfffects.put(boundHeapPathForCaller, calleeCompleteSummary.get(hpKey)
761 possibleCalleeCompleteSummarySetToCaller.add(boundCalleeEfffects);
767 private NTuple<Descriptor> replace(NTuple<Descriptor> hpKey, NTuple<Descriptor> argHeapPath) {
769 // replace the head of heap path with caller's arg path
770 // for example, heap path 'param.a.b' in callee's side will be replaced with
771 // (corresponding arg heap path).a.b for caller's side
773 NTuple<Descriptor> bound = new NTuple<Descriptor>();
775 for (int i = 0; i < argHeapPath.size(); i++) {
776 bound.add(argHeapPath.get(i));
779 for (int i = 1; i < hpKey.size(); i++) {
780 bound.add(hpKey.get(i));
786 private NTuple<Descriptor> replace(NTuple<Descriptor> effectHeapPath,
787 NTuple<Descriptor> argHeapPath, TempDescriptor calleeParamHeapPath) {
788 // replace the head of caller's heap path with callee's param heap path
790 NTuple<Descriptor> boundHeapPath = new NTuple<Descriptor>();
791 boundHeapPath.add(calleeParamHeapPath);
793 for (int i = argHeapPath.size(); i < effectHeapPath.size(); i++) {
794 boundHeapPath.add(effectHeapPath.get(i));
797 return boundHeapPath;
800 private void computeSharedCoverSet() {
801 LinkedList<MethodDescriptor> descriptorListToAnalyze =
802 (LinkedList<MethodDescriptor>) sortedDescriptors.clone();
804 // current descriptors to visit in fixed-point interprocedural analysis,
806 // dependency in the call graph
807 methodDescriptorsToVisitStack.clear();
809 descriptorListToAnalyze.removeFirst();
811 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
812 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
814 while (!descriptorListToAnalyze.isEmpty()) {
815 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
816 methodDescriptorsToVisitStack.add(md);
819 // analyze scheduled methods until there are no more to visit
820 while (!methodDescriptorsToVisitStack.isEmpty()) {
821 MethodDescriptor md = methodDescriptorsToVisitStack.pop();
823 FlatMethod fm = state.getMethodFlat(md);
825 computeSharedCoverSet_analyzeMethod(fm, md.equals(methodContainingSSJavaLoop));
831 private void computeSharedCoverSet_analyzeMethod(FlatMethod fm, boolean onlyVisitSSJavaLoop) {
833 MethodDescriptor md = fm.getMethod();
834 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
836 Set<FlatNode> visited = new HashSet<FlatNode>();
838 if (onlyVisitSSJavaLoop) {
839 flatNodesToVisit.add(ssjavaLoopEntrance);
841 flatNodesToVisit.add(fm);
844 while (!flatNodesToVisit.isEmpty()) {
845 FlatNode fn = flatNodesToVisit.iterator().next();
846 flatNodesToVisit.remove(fn);
849 computeSharedCoverSet_nodeActions(md, fn);
851 for (int i = 0; i < fn.numNext(); i++) {
852 FlatNode nn = fn.getNext(i);
854 if (!visited.contains(nn)) {
855 if (!onlyVisitSSJavaLoop || (onlyVisitSSJavaLoop && loopIncElements.contains(nn))) {
856 flatNodesToVisit.add(nn);
866 private void computeSharedCoverSet_nodeActions(MethodDescriptor md, FlatNode fn) {
873 case FKind.FlatLiteralNode: {
874 FlatLiteralNode fln = (FlatLiteralNode) fn;
877 if (lhs.getType().isPrimitive() && !lhs.getSymbol().startsWith("neverused")
878 && !lhs.getSymbol().startsWith("srctmp")) {
879 // only need to care about composite location case here
880 if (lhs.getType().getExtension() instanceof SSJavaType) {
881 CompositeLocation compLoc = ((SSJavaType) lhs.getType().getExtension()).getCompLoc();
882 Location lastLocElement = compLoc.get(compLoc.getSize() - 1);
883 // check if the last one is shared loc
884 if (ssjava.isSharedLocation(lastLocElement)) {
885 addSharedLocDescriptor(lastLocElement, lhs);
893 case FKind.FlatOpNode: {
894 FlatOpNode fon = (FlatOpNode) fn;
895 // for a normal assign node, need to propagate lhs's heap path to
897 if (fon.getOp().getOp() == Operation.ASSIGN) {
901 if (lhs.getType().isPrimitive() && !lhs.getSymbol().startsWith("neverused")
902 && !lhs.getSymbol().startsWith("srctmp")) {
903 // only need to care about composite location case here
904 if (lhs.getType().getExtension() instanceof SSJavaType) {
905 CompositeLocation compLoc = ((SSJavaType) lhs.getType().getExtension()).getCompLoc();
906 Location lastLocElement = compLoc.get(compLoc.getSize() - 1);
907 // check if the last one is shared loc
908 if (ssjava.isSharedLocation(lastLocElement)) {
909 addSharedLocDescriptor(lastLocElement, lhs);
918 case FKind.FlatSetFieldNode:
919 case FKind.FlatSetElementNode: {
923 if (fn.kind() == FKind.FlatSetFieldNode) {
924 FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
926 fld = fsfn.getField();
929 FlatSetElementNode fsen = (FlatSetElementNode) fn;
932 TypeDescriptor td = lhs.getType().dereference();
933 fld = getArrayField(td);
936 Location fieldLocation = (Location) fld.getType().getExtension();
937 if (ssjava.isSharedLocation(fieldLocation)) {
938 addSharedLocDescriptor(fieldLocation, fld);
947 private void addSharedLocDescriptor(Location sharedLoc, Descriptor desc) {
949 Set<Descriptor> descSet = mapSharedLocationToCoverSet.get(sharedLoc);
950 if (descSet == null) {
951 descSet = new HashSet<Descriptor>();
952 mapSharedLocationToCoverSet.put(sharedLoc, descSet);
955 System.out.println("add " + desc + " to shared loc" + sharedLoc);
960 private void mergeReadLocationAnaylsis(ReadSummary curr, Set<ReadSummary> inSet) {
962 if (inSet.size() == 0) {
966 for (Iterator inIterator = inSet.iterator(); inIterator.hasNext();) {
967 ReadSummary inSummary = (ReadSummary) inIterator.next();
968 curr.merge(inSummary);
973 private boolean hasReadingEffectOnSharedLocation(MethodDescriptor md, NTuple<Descriptor> hp,
974 Location loc, Descriptor d) {
976 ReadSummary summary = mapMethodDescriptorToReadSummary.get(md);
978 if (summary != null) {
979 Hashtable<Location, Set<Descriptor>> map = summary.get(hp);
981 Set<Descriptor> descSec = map.get(loc);
982 if (descSec != null) {
983 return descSec.contains(d);
991 private Location getLocation(Descriptor d) {
993 if (d instanceof FieldDescriptor) {
994 TypeExtension te = ((FieldDescriptor) d).getType().getExtension();
996 return (Location) te;
999 assert d instanceof TempDescriptor;
1000 TempDescriptor td = (TempDescriptor) d;
1002 TypeExtension te = td.getType().getExtension();
1004 if (te instanceof SSJavaType) {
1005 SSJavaType ssType = (SSJavaType) te;
1006 CompositeLocation comp = ssType.getCompLoc();
1007 return comp.get(comp.getSize() - 1);
1009 return (Location) te;
1014 return mapDescToLocation.get(d);
1017 private void writeLocation(MethodDescriptor md, ClearingSummary curr, NTuple<Descriptor> hp,
1018 Location loc, Descriptor d) {
1020 SharedStatus state = getState(curr, hp);
1021 if (loc != null && hasReadingEffectOnSharedLocation(md, hp, loc, d)) {
1022 // 1. add field x to the clearing set
1024 state.addVar(loc, d);
1026 // 3. if the set v contains all of variables belonging to the shared
1027 // location, set flag to true
1028 if (isOverWrittenAllDescsOfSharedLoc(md, hp, loc, state.getVarSet(loc))) {
1029 state.updateFlag(loc, true);
1032 state.setWriteEffect(loc);
1036 private boolean isOverWrittenAllDescsOfSharedLoc(MethodDescriptor md, NTuple<Descriptor> hp,
1037 Location loc, Set<Descriptor> writtenSet) {
1039 ReadSummary summary = mapMethodDescriptorToReadSummary.get(md);
1041 if (summary != null) {
1042 Hashtable<Location, Set<Descriptor>> map = summary.get(hp);
1044 Set<Descriptor> descSet = map.get(loc);
1045 if (descSet != null) {
1046 return writtenSet.containsAll(descSet);
1053 private void readLocation(MethodDescriptor md, ClearingSummary curr, NTuple<Descriptor> hp,
1054 Location loc, Descriptor d) {
1055 // remove reading var x from written set
1056 if (loc != null && hasReadingEffectOnSharedLocation(md, hp, loc, d)) {
1057 SharedStatus state = getState(curr, hp);
1058 state.removeVar(loc, d);
1062 private SharedStatus getState(ClearingSummary curr, NTuple<Descriptor> hp) {
1063 SharedStatus state = curr.get(hp);
1064 if (state == null) {
1065 state = new SharedStatus();
1066 curr.put(hp, state);
1071 private void eventLoopAnalysis() {
1072 // perform second stage analysis: intraprocedural analysis ensure that
1074 // variables are definitely written in-between the same read
1076 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
1077 flatNodesToVisit.add(ssjavaLoopEntrance);
1079 while (!flatNodesToVisit.isEmpty()) {
1080 FlatNode fn = (FlatNode) flatNodesToVisit.iterator().next();
1081 flatNodesToVisit.remove(fn);
1083 Hashtable<NTuple<Descriptor>, Set<WriteAge>> prev = mapFlatNodetoEventLoopMap.get(fn);
1085 Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr =
1086 new Hashtable<NTuple<Descriptor>, Set<WriteAge>>();
1087 for (int i = 0; i < fn.numPrev(); i++) {
1088 FlatNode nn = fn.getPrev(i);
1089 Hashtable<NTuple<Descriptor>, Set<WriteAge>> in = mapFlatNodetoEventLoopMap.get(nn);
1095 eventLoopAnalysis_nodeAction(fn, curr, ssjavaLoopEntrance);
1097 // if a new result, schedule forward nodes for analysis
1098 if (!curr.equals(prev)) {
1099 mapFlatNodetoEventLoopMap.put(fn, curr);
1101 for (int i = 0; i < fn.numNext(); i++) {
1102 FlatNode nn = fn.getNext(i);
1103 if (loopIncElements.contains(nn)) {
1104 flatNodesToVisit.add(nn);
1112 private void union(Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr,
1113 Hashtable<NTuple<Descriptor>, Set<WriteAge>> in) {
1115 Set<NTuple<Descriptor>> inKeySet = in.keySet();
1116 for (Iterator iterator = inKeySet.iterator(); iterator.hasNext();) {
1117 NTuple<Descriptor> inKey = (NTuple<Descriptor>) iterator.next();
1118 Set<WriteAge> inSet = in.get(inKey);
1120 Set<WriteAge> currSet = curr.get(inKey);
1122 if (currSet == null) {
1123 currSet = new HashSet<WriteAge>();
1124 curr.put(inKey, currSet);
1126 currSet.addAll(inSet);
1131 private void eventLoopAnalysis_nodeAction(FlatNode fn,
1132 Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr, FlatNode loopEntrance) {
1134 Hashtable<NTuple<Descriptor>, Set<WriteAge>> KILLSet =
1135 new Hashtable<NTuple<Descriptor>, Set<WriteAge>>();
1136 Hashtable<NTuple<Descriptor>, Set<WriteAge>> GENSet =
1137 new Hashtable<NTuple<Descriptor>, Set<WriteAge>>();
1139 if (fn.equals(loopEntrance)) {
1140 // it reaches loop entrance: changes all flag to true
1141 Set<NTuple<Descriptor>> keySet = curr.keySet();
1142 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1143 NTuple<Descriptor> key = (NTuple<Descriptor>) iterator.next();
1144 Set<WriteAge> writeAgeSet = curr.get(key);
1146 Set<WriteAge> incSet = new HashSet<WriteAge>();
1147 incSet.addAll(writeAgeSet);
1148 writeAgeSet.clear();
1150 for (Iterator iterator2 = incSet.iterator(); iterator2.hasNext();) {
1151 WriteAge writeAge = (WriteAge) iterator2.next();
1152 WriteAge newWriteAge = writeAge.copy();
1154 writeAgeSet.add(newWriteAge);
1158 // System.out.println("EVENT LOOP ENTRY=" + curr);
1163 FieldDescriptor fld;
1165 switch (fn.kind()) {
1167 case FKind.FlatOpNode: {
1168 FlatOpNode fon = (FlatOpNode) fn;
1169 lhs = fon.getDest();
1170 rhs = fon.getLeft();
1172 if (!lhs.getSymbol().startsWith("neverused")) {
1173 NTuple<Descriptor> rhsHeapPath = computePath(rhs);
1174 if (!rhs.getType().isImmutable()) {
1175 mapHeapPath.put(lhs, rhsHeapPath);
1178 // NTuple<Descriptor> lhsHeapPath = computePath(lhs);
1179 NTuple<Descriptor> path = new NTuple<Descriptor>();
1182 // System.out.println("WRITE VARIABLE=" + path + " from=" + lhs);
1184 computeKILLSetForWrite(curr, path, KILLSet);
1185 computeGENSetForWrite(path, GENSet);
1187 // System.out.println("#VARIABLE WRITE:" + fn);
1188 // System.out.println("#KILLSET=" + KILLSet);
1189 // System.out.println("#GENSet=" + GENSet);
1197 case FKind.FlatFieldNode:
1198 case FKind.FlatElementNode: {
1200 if (fn.kind() == FKind.FlatFieldNode) {
1201 FlatFieldNode ffn = (FlatFieldNode) fn;
1204 fld = ffn.getField();
1206 FlatElementNode fen = (FlatElementNode) fn;
1209 TypeDescriptor td = rhs.getType().dereference();
1210 fld = getArrayField(td);
1214 NTuple<Descriptor> srcHeapPath = mapHeapPath.get(rhs);
1215 NTuple<Descriptor> fldHeapPath;
1216 if (srcHeapPath != null) {
1217 fldHeapPath = new NTuple<Descriptor>(srcHeapPath.getList());
1219 // if srcHeapPath is null, it is static reference
1220 fldHeapPath = new NTuple<Descriptor>();
1221 fldHeapPath.add(rhs);
1223 fldHeapPath.add(fld);
1225 Set<WriteAge> writeAgeSet = curr.get(fldHeapPath);
1226 checkWriteAgeSet(writeAgeSet, fldHeapPath, fn);
1231 case FKind.FlatSetFieldNode:
1232 case FKind.FlatSetElementNode: {
1234 if (fn.kind() == FKind.FlatSetFieldNode) {
1235 FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
1236 lhs = fsfn.getDst();
1237 fld = fsfn.getField();
1239 FlatSetElementNode fsen = (FlatSetElementNode) fn;
1240 lhs = fsen.getDst();
1241 rhs = fsen.getSrc();
1242 TypeDescriptor td = lhs.getType().dereference();
1243 fld = getArrayField(td);
1247 NTuple<Descriptor> lhsHeapPath = computePath(lhs);
1248 NTuple<Descriptor> fldHeapPath = new NTuple<Descriptor>(lhsHeapPath.getList());
1249 fldHeapPath.add(fld);
1251 computeKILLSetForWrite(curr, fldHeapPath, KILLSet);
1252 computeGENSetForWrite(fldHeapPath, GENSet);
1254 // System.out.println("FIELD WRITE:" + fn);
1255 // System.out.println("KILLSET=" + KILLSet);
1256 // System.out.println("GENSet=" + GENSet);
1261 case FKind.FlatCall: {
1262 FlatCall fc = (FlatCall) fn;
1264 generateKILLSetForFlatCall(fc, curr, KILLSet);
1265 generateGENSetForFlatCall(fc, GENSet);
1267 // System.out.println("FLATCALL:" + fn);
1268 // System.out.println("KILLSET=" + KILLSet);
1269 // System.out.println("GENSet=" + GENSet);
1276 computeNewMapping(curr, KILLSet, GENSet);
1277 // System.out.println("#######" + curr);
1283 private void checkWriteAgeSet(Set<WriteAge> writeAgeSet, NTuple<Descriptor> path, FlatNode fn) {
1284 if (writeAgeSet != null) {
1285 for (Iterator iterator = writeAgeSet.iterator(); iterator.hasNext();) {
1286 WriteAge writeAge = (WriteAge) iterator.next();
1287 if (writeAge.getAge() >= MAXAGE) {
1289 "Memory location, which is reachable through references "
1291 + ", who comes back to the same read statement without being overwritten at the out-most iteration at "
1292 + methodContainingSSJavaLoop.getClassDesc().getSourceFileName() + "::"
1299 private void generateGENSetForFlatCall(FlatCall fc,
1300 Hashtable<NTuple<Descriptor>, Set<WriteAge>> GENSet) {
1302 Set<NTuple<Descriptor>> boundMayWriteSet = mapFlatNodeToBoundMayWriteSet.get(fc);
1304 for (Iterator iterator = boundMayWriteSet.iterator(); iterator.hasNext();) {
1305 NTuple<Descriptor> key = (NTuple<Descriptor>) iterator.next();
1306 // TODO: shared location
1307 Set<WriteAge> set = new HashSet<WriteAge>();
1308 set.add(new WriteAge(0));
1309 GENSet.put(key, set);
1314 private void generateKILLSetForFlatCall(FlatCall fc,
1315 Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr,
1316 Hashtable<NTuple<Descriptor>, Set<WriteAge>> KILLSet) {
1318 Set<NTuple<Descriptor>> boundMustWriteSet = mapFlatNodeToBoundMustWriteSet.get(fc);
1320 for (Iterator iterator = boundMustWriteSet.iterator(); iterator.hasNext();) {
1321 NTuple<Descriptor> key = (NTuple<Descriptor>) iterator.next();
1322 // TODO: shared location
1323 if (curr.get(key) != null) {
1324 KILLSet.put(key, curr.get(key));
1330 private void computeNewMapping(Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr,
1331 Hashtable<NTuple<Descriptor>, Set<WriteAge>> KILLSet,
1332 Hashtable<NTuple<Descriptor>, Set<WriteAge>> GENSet) {
1334 for (Enumeration<NTuple<Descriptor>> e = KILLSet.keys(); e.hasMoreElements();) {
1335 NTuple<Descriptor> key = e.nextElement();
1337 Set<WriteAge> writeAgeSet = curr.get(key);
1338 if (writeAgeSet == null) {
1339 writeAgeSet = new HashSet<WriteAge>();
1340 curr.put(key, writeAgeSet);
1342 writeAgeSet.removeAll(KILLSet.get(key));
1345 for (Enumeration<NTuple<Descriptor>> e = GENSet.keys(); e.hasMoreElements();) {
1346 NTuple<Descriptor> key = e.nextElement();
1347 curr.put(key, GENSet.get(key));
1352 private void computeGENSetForWrite(NTuple<Descriptor> fldHeapPath,
1353 Hashtable<NTuple<Descriptor>, Set<WriteAge>> GENSet) {
1355 // generate write age 0 for the field being written to
1356 Set<WriteAge> writeAgeSet = new HashSet<WriteAge>();
1357 writeAgeSet.add(new WriteAge(0));
1358 GENSet.put(fldHeapPath, writeAgeSet);
1362 private void readValue(FlatNode fn, NTuple<Descriptor> hp,
1363 Hashtable<NTuple<Descriptor>, Hashtable<FlatNode, Boolean>> curr) {
1364 Hashtable<FlatNode, Boolean> gen = curr.get(hp);
1366 gen = new Hashtable<FlatNode, Boolean>();
1369 Boolean currentStatus = gen.get(fn);
1370 if (currentStatus == null) {
1371 gen.put(fn, Boolean.FALSE);
1373 checkFlag(currentStatus.booleanValue(), fn, hp);
1378 private void computeKILLSetForWrite(Hashtable<NTuple<Descriptor>, Set<WriteAge>> curr,
1379 NTuple<Descriptor> hp, Hashtable<NTuple<Descriptor>, Set<WriteAge>> KILLSet) {
1381 // removes all of heap path that starts with prefix 'hp'
1382 // since any reference overwrite along heap path gives overwriting side
1383 // effects on the value
1385 Set<NTuple<Descriptor>> keySet = curr.keySet();
1386 for (Iterator<NTuple<Descriptor>> iter = keySet.iterator(); iter.hasNext();) {
1387 NTuple<Descriptor> key = iter.next();
1388 if (key.startsWith(hp)) {
1389 KILLSet.put(key, curr.get(key));
1395 private void bindHeapPathCallerArgWithCaleeParam(FlatCall fc) {
1396 // compute all possible callee set
1397 // transform all READ/WRITE set from the any possible
1398 // callees to the caller
1399 calleeUnionBoundReadSet.clear();
1400 calleeIntersectBoundMustWriteSet.clear();
1401 calleeUnionBoundMayWriteSet.clear();
1403 if (ssjava.isSSJavaUtil(fc.getMethod().getClassDesc())) {
1404 // ssjava util case!
1405 // have write effects on the first argument
1406 TempDescriptor arg = fc.getArg(0);
1407 NTuple<Descriptor> argHeapPath = computePath(arg);
1408 calleeIntersectBoundMustWriteSet.add(argHeapPath);
1409 calleeUnionBoundMayWriteSet.add(argHeapPath);
1411 MethodDescriptor mdCallee = fc.getMethod();
1412 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
1413 setPossibleCallees.addAll(callGraph.getMethods(mdCallee));
1415 // create mapping from arg idx to its heap paths
1416 Hashtable<Integer, NTuple<Descriptor>> mapArgIdx2CallerArgHeapPath =
1417 new Hashtable<Integer, NTuple<Descriptor>>();
1419 // arg idx is starting from 'this' arg
1420 if (fc.getThis() != null) {
1421 NTuple<Descriptor> thisHeapPath = mapHeapPath.get(fc.getThis());
1422 if (thisHeapPath == null) {
1423 // method is called without creating new flat node representing 'this'
1424 thisHeapPath = new NTuple<Descriptor>();
1425 thisHeapPath.add(fc.getThis());
1428 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(0), thisHeapPath);
1431 for (int i = 0; i < fc.numArgs(); i++) {
1432 TempDescriptor arg = fc.getArg(i);
1433 NTuple<Descriptor> argHeapPath = computePath(arg);
1434 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(i + 1), argHeapPath);
1437 for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
1438 MethodDescriptor callee = (MethodDescriptor) iterator.next();
1439 FlatMethod calleeFlatMethod = state.getMethodFlat(callee);
1441 // binding caller's args and callee's params
1443 Set<NTuple<Descriptor>> calleeReadSet = mapFlatMethodToReadSet.get(calleeFlatMethod);
1444 if (calleeReadSet == null) {
1445 calleeReadSet = new HashSet<NTuple<Descriptor>>();
1446 mapFlatMethodToReadSet.put(calleeFlatMethod, calleeReadSet);
1449 Set<NTuple<Descriptor>> calleeMustWriteSet =
1450 mapFlatMethodToMustWriteSet.get(calleeFlatMethod);
1452 if (calleeMustWriteSet == null) {
1453 calleeMustWriteSet = new HashSet<NTuple<Descriptor>>();
1454 mapFlatMethodToMustWriteSet.put(calleeFlatMethod, calleeMustWriteSet);
1457 Set<NTuple<Descriptor>> calleeMayWriteSet =
1458 mapFlatMethodToMayWriteSet.get(calleeFlatMethod);
1460 if (calleeMayWriteSet == null) {
1461 calleeMayWriteSet = new HashSet<NTuple<Descriptor>>();
1462 mapFlatMethodToMayWriteSet.put(calleeFlatMethod, calleeMayWriteSet);
1465 Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc =
1466 new Hashtable<Integer, TempDescriptor>();
1468 if (calleeFlatMethod.getMethod().isStatic()) {
1469 // static method does not have implicit 'this' arg
1472 for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
1473 TempDescriptor param = calleeFlatMethod.getParameter(i);
1474 mapParamIdx2ParamTempDesc.put(Integer.valueOf(i + offset), param);
1477 Set<NTuple<Descriptor>> calleeBoundReadSet =
1478 bindSet(calleeReadSet, mapParamIdx2ParamTempDesc, mapArgIdx2CallerArgHeapPath);
1479 // union of the current read set and the current callee's
1481 calleeUnionBoundReadSet.addAll(calleeBoundReadSet);
1483 Set<NTuple<Descriptor>> calleeBoundMustWriteSet =
1484 bindSet(calleeMustWriteSet, mapParamIdx2ParamTempDesc, mapArgIdx2CallerArgHeapPath);
1485 // intersection of the current overwrite set and the current
1488 merge(calleeIntersectBoundMustWriteSet, calleeBoundMustWriteSet);
1490 Set<NTuple<Descriptor>> boundWriteSetFromCallee =
1491 bindSet(calleeMayWriteSet, mapParamIdx2ParamTempDesc, mapArgIdx2CallerArgHeapPath);
1492 calleeUnionBoundMayWriteSet.addAll(boundWriteSetFromCallee);
1499 private void checkFlag(boolean booleanValue, FlatNode fn, NTuple<Descriptor> hp) {
1501 // the definitely written analysis only takes care about locations that
1502 // are written to inside of the SSJava loop
1503 for (Iterator iterator = calleeUnionBoundMayWriteSet.iterator(); iterator.hasNext();) {
1504 NTuple<Descriptor> write = (NTuple<Descriptor>) iterator.next();
1505 if (hp.startsWith(write)) {
1506 // it has write effect!
1510 + "There is a variable, which is reachable through references "
1512 + ", who comes back to the same read statement without being overwritten at the out-most iteration at "
1513 + methodContainingSSJavaLoop.getClassDesc().getSourceFileName() + "::"
1521 private void initialize() {
1522 // First, identify ssjava loop entrace
1524 // no need to analyze method having ssjava loop
1525 methodContainingSSJavaLoop = ssjava.getMethodContainingSSJavaLoop();
1527 FlatMethod fm = state.getMethodFlat(methodContainingSSJavaLoop);
1528 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
1529 flatNodesToVisit.add(fm);
1531 LoopFinder loopFinder = new LoopFinder(fm);
1533 while (!flatNodesToVisit.isEmpty()) {
1534 FlatNode fn = flatNodesToVisit.iterator().next();
1535 flatNodesToVisit.remove(fn);
1537 String label = (String) state.fn2labelMap.get(fn);
1538 if (label != null) {
1540 if (label.equals(ssjava.SSJAVA)) {
1541 ssjavaLoopEntrance = fn;
1546 for (int i = 0; i < fn.numNext(); i++) {
1547 FlatNode nn = fn.getNext(i);
1548 flatNodesToVisit.add(nn);
1552 assert ssjavaLoopEntrance != null;
1554 // assume that ssjava loop is top-level loop in method, not nested loop
1555 Set nestedLoop = loopFinder.nestedLoops();
1556 for (Iterator loopIter = nestedLoop.iterator(); loopIter.hasNext();) {
1557 LoopFinder lf = (LoopFinder) loopIter.next();
1558 if (lf.loopEntrances().iterator().next().equals(ssjavaLoopEntrance)) {
1563 assert ssjavaLoop != null;
1565 loopIncElements = (Set<FlatNode>) ssjavaLoop.loopIncElements();
1567 // perform topological sort over the set of methods accessed by the main
1569 Set<MethodDescriptor> methodDescriptorsToAnalyze = new HashSet<MethodDescriptor>();
1570 methodDescriptorsToAnalyze.addAll(ssjava.getAnnotationRequireSet());
1571 sortedDescriptors = topologicalSort(methodDescriptorsToAnalyze);
1574 private void methodReadWriteSetAnalysis() {
1575 // perform method READ/OVERWRITE analysis
1576 LinkedList<MethodDescriptor> descriptorListToAnalyze =
1577 (LinkedList<MethodDescriptor>) sortedDescriptors.clone();
1579 // current descriptors to visit in fixed-point interprocedural analysis,
1581 // dependency in the call graph
1582 methodDescriptorsToVisitStack.clear();
1584 descriptorListToAnalyze.removeFirst();
1586 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
1587 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
1589 while (!descriptorListToAnalyze.isEmpty()) {
1590 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
1591 methodDescriptorsToVisitStack.add(md);
1594 // analyze scheduled methods until there are no more to visit
1595 while (!methodDescriptorsToVisitStack.isEmpty()) {
1596 // start to analyze leaf node
1597 MethodDescriptor md = methodDescriptorsToVisitStack.pop();
1598 FlatMethod fm = state.getMethodFlat(md);
1600 Set<NTuple<Descriptor>> readSet = new HashSet<NTuple<Descriptor>>();
1601 Set<NTuple<Descriptor>> mustWriteSet = new HashSet<NTuple<Descriptor>>();
1602 Set<NTuple<Descriptor>> mayWriteSet = new HashSet<NTuple<Descriptor>>();
1604 methodReadWriteSet_analyzeMethod(fm, readSet, mustWriteSet, mayWriteSet);
1606 Set<NTuple<Descriptor>> prevRead = mapFlatMethodToReadSet.get(fm);
1607 Set<NTuple<Descriptor>> prevMustWrite = mapFlatMethodToMustWriteSet.get(fm);
1608 Set<NTuple<Descriptor>> prevMayWrite = mapFlatMethodToMayWriteSet.get(fm);
1610 if (!(readSet.equals(prevRead) && mustWriteSet.equals(prevMustWrite) && mayWriteSet
1611 .equals(prevMayWrite))) {
1612 mapFlatMethodToReadSet.put(fm, readSet);
1613 mapFlatMethodToMustWriteSet.put(fm, mustWriteSet);
1614 mapFlatMethodToMayWriteSet.put(fm, mayWriteSet);
1616 // results for callee changed, so enqueue dependents caller for
1619 Iterator<MethodDescriptor> depsItr = getDependents(md).iterator();
1620 while (depsItr.hasNext()) {
1621 MethodDescriptor methodNext = depsItr.next();
1622 if (!methodDescriptorsToVisitStack.contains(methodNext)
1623 && methodDescriptorToVistSet.contains(methodNext)) {
1624 methodDescriptorsToVisitStack.add(methodNext);
1635 private void methodReadWriteSet_analyzeMethod(FlatMethod fm, Set<NTuple<Descriptor>> readSet,
1636 Set<NTuple<Descriptor>> mustWriteSet, Set<NTuple<Descriptor>> mayWriteSet) {
1637 if (state.SSJAVADEBUG) {
1638 System.out.println("SSJAVA: Definitely written Analyzing: " + fm);
1641 methodReadWriteSet_analyzeBody(fm, readSet, mustWriteSet, mayWriteSet, null);
1645 private void methodReadWriteSetAnalysisToEventLoopBody() {
1647 // perform method read/write analysis for Event Loop Body
1649 FlatMethod flatMethodContainingSSJavaLoop = state.getMethodFlat(methodContainingSSJavaLoop);
1651 if (state.SSJAVADEBUG) {
1652 System.out.println("SSJAVA: Definitely written Event Loop Analyzing: "
1653 + flatMethodContainingSSJavaLoop);
1656 Set<NTuple<Descriptor>> readSet = new HashSet<NTuple<Descriptor>>();
1657 Set<NTuple<Descriptor>> mustWriteSet = new HashSet<NTuple<Descriptor>>();
1658 Set<NTuple<Descriptor>> mayWriteSet = new HashSet<NTuple<Descriptor>>();
1660 mapFlatMethodToReadSet.put(flatMethodContainingSSJavaLoop, readSet);
1661 mapFlatMethodToMustWriteSet.put(flatMethodContainingSSJavaLoop, mustWriteSet);
1662 mapFlatMethodToMayWriteSet.put(flatMethodContainingSSJavaLoop, mayWriteSet);
1664 methodReadWriteSet_analyzeBody(ssjavaLoopEntrance, readSet, mustWriteSet, mayWriteSet,
1669 private void methodReadWriteSet_analyzeBody(FlatNode startNode, Set<NTuple<Descriptor>> readSet,
1670 Set<NTuple<Descriptor>> mustWriteSet, Set<NTuple<Descriptor>> mayWriteSet,
1671 Set<FlatNode> bodyNodeSet) {
1673 // intraprocedural analysis
1674 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
1675 flatNodesToVisit.add(startNode);
1677 while (!flatNodesToVisit.isEmpty()) {
1678 FlatNode fn = flatNodesToVisit.iterator().next();
1679 flatNodesToVisit.remove(fn);
1681 Set<NTuple<Descriptor>> currMustWriteSet = new HashSet<NTuple<Descriptor>>();
1683 for (int i = 0; i < fn.numPrev(); i++) {
1684 FlatNode prevFn = fn.getPrev(i);
1685 Set<NTuple<Descriptor>> in = mapFlatNodeToMustWriteSet.get(prevFn);
1687 merge(currMustWriteSet, in);
1691 methodReadWriteSet_nodeActions(fn, currMustWriteSet, readSet, mustWriteSet, mayWriteSet);
1693 Set<NTuple<Descriptor>> mustSetPrev = mapFlatNodeToMustWriteSet.get(fn);
1694 if (!currMustWriteSet.equals(mustSetPrev)) {
1695 mapFlatNodeToMustWriteSet.put(fn, currMustWriteSet);
1696 for (int i = 0; i < fn.numNext(); i++) {
1697 FlatNode nn = fn.getNext(i);
1698 if (bodyNodeSet == null || bodyNodeSet.contains(nn)) {
1699 flatNodesToVisit.add(nn);
1709 private void methodReadWriteSet_nodeActions(FlatNode fn,
1710 Set<NTuple<Descriptor>> currMustWriteSet, Set<NTuple<Descriptor>> readSet,
1711 Set<NTuple<Descriptor>> mustWriteSet, Set<NTuple<Descriptor>> mayWriteSet) {
1714 FieldDescriptor fld;
1716 switch (fn.kind()) {
1717 case FKind.FlatMethod: {
1719 // set up initial heap paths for method parameters
1720 FlatMethod fm = (FlatMethod) fn;
1721 for (int i = 0; i < fm.numParameters(); i++) {
1722 TempDescriptor param = fm.getParameter(i);
1723 NTuple<Descriptor> heapPath = new NTuple<Descriptor>();
1724 heapPath.add(param);
1725 mapHeapPath.put(param, heapPath);
1730 case FKind.FlatOpNode: {
1731 FlatOpNode fon = (FlatOpNode) fn;
1732 // for a normal assign node, need to propagate lhs's heap path to
1734 if (fon.getOp().getOp() == Operation.ASSIGN) {
1735 rhs = fon.getLeft();
1736 lhs = fon.getDest();
1738 NTuple<Descriptor> rhsHeapPath = mapHeapPath.get(rhs);
1739 if (rhsHeapPath != null) {
1740 mapHeapPath.put(lhs, mapHeapPath.get(rhs));
1742 NTuple<Descriptor> heapPath = new NTuple<Descriptor>();
1744 mapHeapPath.put(lhs, heapPath);
1751 case FKind.FlatElementNode:
1752 case FKind.FlatFieldNode: {
1756 if (fn.kind() == FKind.FlatFieldNode) {
1757 FlatFieldNode ffn = (FlatFieldNode) fn;
1760 fld = ffn.getField();
1762 FlatElementNode fen = (FlatElementNode) fn;
1765 TypeDescriptor td = rhs.getType().dereference();
1766 fld = getArrayField(td);
1769 if (fld.isFinal()) {
1770 // if field is final no need to check
1775 NTuple<Descriptor> srcHeapPath = mapHeapPath.get(rhs);
1776 if (srcHeapPath != null) {
1777 // if lhs srcHeapPath is null, it means that it is not reachable from
1778 // callee's parameters. so just ignore it
1780 NTuple<Descriptor> readingHeapPath = new NTuple<Descriptor>(srcHeapPath.getList());
1781 readingHeapPath.add(fld);
1782 mapHeapPath.put(lhs, readingHeapPath);
1785 if (fld.getType().isImmutable()) {
1786 // if WT doesnot have hp(x.f), add hp(x.f) to READ
1787 if (!currMustWriteSet.contains(readingHeapPath)) {
1788 readSet.add(readingHeapPath);
1792 // no need to kill hp(x.f) from WT
1798 case FKind.FlatSetFieldNode:
1799 case FKind.FlatSetElementNode: {
1803 if (fn.kind() == FKind.FlatSetFieldNode) {
1804 FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
1805 lhs = fsfn.getDst();
1806 fld = fsfn.getField();
1807 rhs = fsfn.getSrc();
1809 FlatSetElementNode fsen = (FlatSetElementNode) fn;
1810 lhs = fsen.getDst();
1811 rhs = fsen.getSrc();
1812 TypeDescriptor td = lhs.getType().dereference();
1813 fld = getArrayField(td);
1817 NTuple<Descriptor> lhsHeapPath = mapHeapPath.get(lhs);
1819 if (lhsHeapPath != null) {
1820 // if lhs heap path is null, it means that it is not reachable from
1821 // callee's parameters. so just ignore it
1822 NTuple<Descriptor> newHeapPath = new NTuple<Descriptor>(lhsHeapPath.getList());
1823 newHeapPath.add(fld);
1824 mapHeapPath.put(fld, newHeapPath);
1827 // need to add hp(y) to WT
1828 currMustWriteSet.add(newHeapPath);
1829 mayWriteSet.add(newHeapPath);
1836 case FKind.FlatCall: {
1838 FlatCall fc = (FlatCall) fn;
1840 bindHeapPathCallerArgWithCaleeParam(fc);
1842 mapFlatNodeToBoundReadSet.put(fn, calleeUnionBoundReadSet);
1843 mapFlatNodeToBoundMustWriteSet.put(fn, calleeIntersectBoundMustWriteSet);
1844 mapFlatNodeToBoundMayWriteSet.put(fn, calleeUnionBoundMayWriteSet);
1846 // add heap path, which is an element of READ_bound set and is not
1848 // element of WT set, to the caller's READ set
1849 for (Iterator iterator = calleeUnionBoundReadSet.iterator(); iterator.hasNext();) {
1850 NTuple<Descriptor> read = (NTuple<Descriptor>) iterator.next();
1851 if (!currMustWriteSet.contains(read)) {
1856 // add heap path, which is an element of OVERWRITE_bound set, to the
1858 for (Iterator iterator = calleeIntersectBoundMustWriteSet.iterator(); iterator.hasNext();) {
1859 NTuple<Descriptor> write = (NTuple<Descriptor>) iterator.next();
1860 currMustWriteSet.add(write);
1863 // add heap path, which is an element of WRITE_BOUND set, to the
1864 // caller's writeSet
1865 for (Iterator iterator = calleeUnionBoundMayWriteSet.iterator(); iterator.hasNext();) {
1866 NTuple<Descriptor> write = (NTuple<Descriptor>) iterator.next();
1867 mayWriteSet.add(write);
1873 case FKind.FlatExit: {
1874 // merge the current written set with OVERWRITE set
1875 merge(mustWriteSet, currMustWriteSet);
1883 static public FieldDescriptor getArrayField(TypeDescriptor td) {
1884 FieldDescriptor fd = mapTypeToArrayField.get(td);
1887 new FieldDescriptor(new Modifiers(Modifiers.PUBLIC), td, arrayElementFieldName, null,
1889 mapTypeToArrayField.put(td, fd);
1894 private void mergeSharedLocationAnaylsis(ClearingSummary curr, Set<ClearingSummary> inSet) {
1895 if (inSet.size() == 0) {
1898 Hashtable<Pair<NTuple<Descriptor>, Location>, Boolean> mapHeapPathLoc2Flag =
1899 new Hashtable<Pair<NTuple<Descriptor>, Location>, Boolean>();
1901 for (Iterator inIterator = inSet.iterator(); inIterator.hasNext();) {
1903 ClearingSummary inTable = (ClearingSummary) inIterator.next();
1905 Set<NTuple<Descriptor>> keySet = inTable.keySet();
1907 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1908 NTuple<Descriptor> hpKey = (NTuple<Descriptor>) iterator.next();
1909 SharedStatus inState = inTable.get(hpKey);
1910 SharedStatus currState = curr.get(hpKey);
1911 if (currState == null) {
1912 currState = new SharedStatus();
1913 curr.put(hpKey, currState);
1916 currState.merge(inState);
1918 Set<Location> locSet = inState.getMap().keySet();
1919 for (Iterator iterator2 = locSet.iterator(); iterator2.hasNext();) {
1920 Location loc = (Location) iterator2.next();
1921 Pair<Set<Descriptor>, Boolean> pair = inState.getMap().get(loc);
1922 boolean inFlag = pair.getSecond().booleanValue();
1924 Pair<NTuple<Descriptor>, Location> flagKey =
1925 new Pair<NTuple<Descriptor>, Location>(hpKey, loc);
1926 Boolean current = mapHeapPathLoc2Flag.get(flagKey);
1927 if (current == null) {
1928 current = new Boolean(true);
1930 boolean newInFlag = current.booleanValue() & inFlag;
1931 mapHeapPathLoc2Flag.put(flagKey, Boolean.valueOf(newInFlag));
1938 // merge flag status
1939 Set<NTuple<Descriptor>> hpKeySet = curr.keySet();
1940 for (Iterator iterator = hpKeySet.iterator(); iterator.hasNext();) {
1941 NTuple<Descriptor> hpKey = (NTuple<Descriptor>) iterator.next();
1942 SharedStatus currState = curr.get(hpKey);
1943 Set<Location> locKeySet = currState.getMap().keySet();
1944 for (Iterator iterator2 = locKeySet.iterator(); iterator2.hasNext();) {
1945 Location locKey = (Location) iterator2.next();
1946 Pair<Set<Descriptor>, Boolean> pair = currState.getMap().get(locKey);
1947 boolean currentFlag = pair.getSecond().booleanValue();
1948 Boolean inFlag = mapHeapPathLoc2Flag.get(new Pair(hpKey, locKey));
1949 if (inFlag != null) {
1950 boolean newFlag = currentFlag | inFlag.booleanValue();
1951 if (currentFlag != newFlag) {
1952 currState.getMap().put(locKey, new Pair(pair.getFirst(), new Boolean(newFlag)));
1960 private void merge(Set<NTuple<Descriptor>> curr, Set<NTuple<Descriptor>> in) {
1961 if (curr.isEmpty()) {
1962 // set has a special initial value which covers all possible
1964 // For the first time of intersection, we can take all previous set
1967 // otherwise, current set is the intersection of the two sets
1973 // combine two heap path
1974 private NTuple<Descriptor> combine(NTuple<Descriptor> callerIn, NTuple<Descriptor> calleeIn) {
1975 NTuple<Descriptor> combined = new NTuple<Descriptor>();
1977 for (int i = 0; i < callerIn.size(); i++) {
1978 combined.add(callerIn.get(i));
1981 // the first element of callee's heap path represents parameter
1982 // so we skip the first one since it is already added from caller's heap
1984 for (int i = 1; i < calleeIn.size(); i++) {
1985 combined.add(calleeIn.get(i));
1991 private Set<NTuple<Descriptor>> bindSet(Set<NTuple<Descriptor>> calleeSet,
1992 Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc,
1993 Hashtable<Integer, NTuple<Descriptor>> mapCallerArgIdx2HeapPath) {
1995 Set<NTuple<Descriptor>> boundedCalleeSet = new HashSet<NTuple<Descriptor>>();
1997 Set<Integer> keySet = mapCallerArgIdx2HeapPath.keySet();
1998 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1999 Integer idx = (Integer) iterator.next();
2001 NTuple<Descriptor> callerArgHeapPath = mapCallerArgIdx2HeapPath.get(idx);
2002 TempDescriptor calleeParam = mapParamIdx2ParamTempDesc.get(idx);
2003 for (Iterator iterator2 = calleeSet.iterator(); iterator2.hasNext();) {
2004 NTuple<Descriptor> element = (NTuple<Descriptor>) iterator2.next();
2005 if (element.startsWith(calleeParam)) {
2006 NTuple<Descriptor> boundElement = combine(callerArgHeapPath, element);
2007 boundedCalleeSet.add(boundElement);
2013 return boundedCalleeSet;
2017 // Borrowed it from disjoint analysis
2018 private LinkedList<MethodDescriptor> topologicalSort(Set<MethodDescriptor> toSort) {
2020 Set<MethodDescriptor> discovered = new HashSet<MethodDescriptor>();
2022 LinkedList<MethodDescriptor> sorted = new LinkedList<MethodDescriptor>();
2024 Iterator<MethodDescriptor> itr = toSort.iterator();
2025 while (itr.hasNext()) {
2026 MethodDescriptor d = itr.next();
2028 if (!discovered.contains(d)) {
2029 dfsVisit(d, toSort, sorted, discovered);
2036 // While we're doing DFS on call graph, remember
2037 // dependencies for efficient queuing of methods
2038 // during interprocedural analysis:
2040 // a dependent of a method decriptor d for this analysis is:
2041 // 1) a method or task that invokes d
2042 // 2) in the descriptorsToAnalyze set
2043 private void dfsVisit(MethodDescriptor md, Set<MethodDescriptor> toSort,
2044 LinkedList<MethodDescriptor> sorted, Set<MethodDescriptor> discovered) {
2048 Iterator itr = callGraph.getCallerSet(md).iterator();
2049 while (itr.hasNext()) {
2050 MethodDescriptor dCaller = (MethodDescriptor) itr.next();
2051 // only consider callers in the original set to analyze
2052 if (!toSort.contains(dCaller)) {
2055 if (!discovered.contains(dCaller)) {
2056 addDependent(md, // callee
2060 dfsVisit(dCaller, toSort, sorted, discovered);
2064 // for leaf-nodes last now!
2068 // a dependent of a method decriptor d for this analysis is:
2069 // 1) a method or task that invokes d
2070 // 2) in the descriptorsToAnalyze set
2071 private void addDependent(MethodDescriptor callee, MethodDescriptor caller) {
2072 Set<MethodDescriptor> deps = mapDescriptorToSetDependents.get(callee);
2074 deps = new HashSet<MethodDescriptor>();
2077 mapDescriptorToSetDependents.put(callee, deps);
2080 private Set<MethodDescriptor> getDependents(MethodDescriptor callee) {
2081 Set<MethodDescriptor> deps = mapDescriptorToSetDependents.get(callee);
2083 deps = new HashSet<MethodDescriptor>();
2084 mapDescriptorToSetDependents.put(callee, deps);
2089 private NTuple<Descriptor> computePath(TempDescriptor td) {
2090 // generate proper path fot input td
2091 // if td is local variable, it just generate one element tuple path
2092 if (mapHeapPath.containsKey(td)) {
2093 return mapHeapPath.get(td);
2095 NTuple<Descriptor> path = new NTuple<Descriptor>();