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;
12 import IR.FieldDescriptor;
13 import IR.MethodDescriptor;
16 import IR.TypeDescriptor;
18 import IR.Flat.FlatCall;
19 import IR.Flat.FlatFieldNode;
20 import IR.Flat.FlatLiteralNode;
21 import IR.Flat.FlatMethod;
22 import IR.Flat.FlatNode;
23 import IR.Flat.FlatOpNode;
24 import IR.Flat.FlatSetFieldNode;
25 import IR.Flat.TempDescriptor;
27 public class DefinitelyWrittenCheck {
29 SSJavaAnalysis ssjava;
33 // maps a descriptor to its known dependents: namely
34 // methods or tasks that call the descriptor's method
35 // AND are part of this analysis (reachable from main)
36 private Hashtable<Descriptor, Set<MethodDescriptor>> mapDescriptorToSetDependents;
38 // maps a flat node to its WrittenSet: this keeps all heap path overwritten
40 private Hashtable<FlatNode, Set<NTuple<Descriptor>>> mapFlatNodeToWrittenSet;
42 // maps a temp descriptor to its heap path
43 // each temp descriptor has a unique heap path since we do not allow any
45 private Hashtable<Descriptor, NTuple<Descriptor>> mapHeapPath;
47 // maps a flat method to the READ that is the set of heap path that is
48 // expected to be written before method invocation
49 private Hashtable<FlatMethod, Set<NTuple<Descriptor>>> mapFlatMethodToRead;
51 // maps a flat method to the OVERWRITE that is the set of heap path that is
52 // overwritten on every possible path during method invocation
53 private Hashtable<FlatMethod, Set<NTuple<Descriptor>>> mapFlatMethodToOverWrite;
55 // points to method containing SSJAVA Loop
56 private MethodDescriptor methodContainingSSJavaLoop;
58 // maps a flatnode to definitely written analysis mapping M
59 private Hashtable<FlatNode, Hashtable<NTuple<Descriptor>, Hashtable<FlatNode, Boolean>>> definitelyWrittenResults;
61 private Set<NTuple<Descriptor>> calleeUnionBoundReadSet;
62 private Set<NTuple<Descriptor>> calleeIntersectBoundOverWriteSet;
64 public DefinitelyWrittenCheck(SSJavaAnalysis ssjava, State state) {
67 this.callGraph = ssjava.getCallGraph();
68 this.mapFlatNodeToWrittenSet = new Hashtable<FlatNode, Set<NTuple<Descriptor>>>();
69 this.mapDescriptorToSetDependents = new Hashtable<Descriptor, Set<MethodDescriptor>>();
70 this.mapHeapPath = new Hashtable<Descriptor, NTuple<Descriptor>>();
71 this.mapFlatMethodToRead = new Hashtable<FlatMethod, Set<NTuple<Descriptor>>>();
72 this.mapFlatMethodToOverWrite = new Hashtable<FlatMethod, Set<NTuple<Descriptor>>>();
73 this.definitelyWrittenResults =
74 new Hashtable<FlatNode, Hashtable<NTuple<Descriptor>, Hashtable<FlatNode, Boolean>>>();
75 this.calleeUnionBoundReadSet = new HashSet<NTuple<Descriptor>>();
76 this.calleeIntersectBoundOverWriteSet = new HashSet<NTuple<Descriptor>>();
79 public void definitelyWrittenCheck() {
80 methodReadOverWriteAnalysis();
84 private void writtenAnalyis() {
85 // perform second stage analysis: intraprocedural analysis ensure that
87 // variables are definitely written in-between the same read
89 // First, identify ssjava loop entrace
90 FlatMethod fm = state.getMethodFlat(methodContainingSSJavaLoop);
91 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
92 flatNodesToVisit.add(fm);
94 FlatNode entrance = null;
96 while (!flatNodesToVisit.isEmpty()) {
97 FlatNode fn = flatNodesToVisit.iterator().next();
98 flatNodesToVisit.remove(fn);
100 String label = (String) state.fn2labelMap.get(fn);
103 if (label.equals(ssjava.SSJAVA)) {
109 for (int i = 0; i < fn.numNext(); i++) {
110 FlatNode nn = fn.getNext(i);
111 flatNodesToVisit.add(nn);
115 assert entrance != null;
117 writtenAnalysis_analyzeLoop(entrance);
121 private void writtenAnalysis_analyzeLoop(FlatNode entrance) {
123 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
124 flatNodesToVisit.add(entrance);
126 while (!flatNodesToVisit.isEmpty()) {
127 FlatNode fn = (FlatNode) flatNodesToVisit.iterator().next();
128 flatNodesToVisit.remove(fn);
130 Hashtable<NTuple<Descriptor>, Hashtable<FlatNode, Boolean>> prev =
131 definitelyWrittenResults.get(fn);
133 Hashtable<NTuple<Descriptor>, Hashtable<FlatNode, Boolean>> curr =
134 new Hashtable<NTuple<Descriptor>, Hashtable<FlatNode, Boolean>>();
135 for (int i = 0; i < fn.numPrev(); i++) {
136 FlatNode nn = fn.getPrev(i);
137 Hashtable<NTuple<Descriptor>, Hashtable<FlatNode, Boolean>> dwIn =
138 definitelyWrittenResults.get(nn);
144 writtenAnalysis_nodeAction(fn, curr, entrance);
146 // if a new result, schedule forward nodes for analysis
147 if (!curr.equals(prev)) {
148 definitelyWrittenResults.put(fn, curr);
150 for (int i = 0; i < fn.numNext(); i++) {
151 FlatNode nn = fn.getNext(i);
152 flatNodesToVisit.add(nn);
158 private void writtenAnalysis_nodeAction(FlatNode fn,
159 Hashtable<NTuple<Descriptor>, Hashtable<FlatNode, Boolean>> curr, FlatNode loopEntrance) {
160 if (fn.equals(loopEntrance)) {
161 // it reaches loop entrance: changes all flag to true
162 Set<NTuple<Descriptor>> keySet = curr.keySet();
163 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
164 NTuple<Descriptor> key = (NTuple<Descriptor>) iterator.next();
165 Hashtable<FlatNode, Boolean> pair = curr.get(key);
167 Set<FlatNode> pairKeySet = pair.keySet();
168 for (Iterator iterator2 = pairKeySet.iterator(); iterator2.hasNext();) {
169 FlatNode pairKey = (FlatNode) iterator2.next();
170 pair.put(pairKey, Boolean.TRUE);
180 case FKind.FlatOpNode: {
181 FlatOpNode fon = (FlatOpNode) fn;
185 NTuple<Descriptor> rhsHeapPath = computePath(rhs);
186 if (!rhs.getType().isImmutable()) {
187 mapHeapPath.put(lhs, rhsHeapPath);
190 if (fon.getOp().getOp() == Operation.ASSIGN) {
192 Hashtable<FlatNode, Boolean> gen = curr.get(rhsHeapPath);
195 gen = new Hashtable<FlatNode, Boolean>();
196 curr.put(rhsHeapPath, gen);
198 Boolean currentStatus = gen.get(fn);
199 if (currentStatus == null) {
200 gen.put(fn, Boolean.FALSE);
202 if (!rhs.getType().isClass()) {
203 checkFlag(currentStatus.booleanValue(), fn);
209 NTuple<Descriptor> lhsHeapPath = computePath(lhs);
210 removeHeapPath(curr, lhsHeapPath);
211 // curr.put(lhsHeapPath, new Hashtable<FlatNode, Boolean>());
215 case FKind.FlatLiteralNode: {
216 FlatLiteralNode fln = (FlatLiteralNode) fn;
220 NTuple<Descriptor> lhsHeapPath = computePath(lhs);
221 removeHeapPath(curr, lhsHeapPath);
226 case FKind.FlatFieldNode:
227 case FKind.FlatElementNode: {
229 FlatFieldNode ffn = (FlatFieldNode) fn;
231 fld = ffn.getField();
234 NTuple<Descriptor> srcHeapPath = mapHeapPath.get(lhs);
235 NTuple<Descriptor> fldHeapPath = new NTuple<Descriptor>(srcHeapPath.getList());
236 fldHeapPath.add(fld);
237 Hashtable<FlatNode, Boolean> gen = curr.get(fldHeapPath);
240 gen = new Hashtable<FlatNode, Boolean>();
241 curr.put(fldHeapPath, gen);
244 Boolean currentStatus = gen.get(fn);
245 if (currentStatus == null) {
246 gen.put(fn, Boolean.FALSE);
248 checkFlag(currentStatus.booleanValue(), fn);
254 case FKind.FlatSetFieldNode:
255 case FKind.FlatSetElementNode: {
257 FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
259 fld = fsfn.getField();
262 NTuple<Descriptor> lhsHeapPath = mapHeapPath.get(lhs);
263 NTuple<Descriptor> fldHeapPath = new NTuple<Descriptor>(lhsHeapPath.getList());
264 fldHeapPath.add(fld);
265 removeHeapPath(curr, fldHeapPath);
266 // curr.put(fldHeapPath, new Hashtable<FlatNode, Boolean>());
271 case FKind.FlatCall: {
273 FlatCall fc = (FlatCall) fn;
275 bindHeapPathCallerArgWithCaleeParam(fc);
277 // add <hp,statement,false> in which hp is an element of
279 // of callee: callee has 'read' requirement!
280 for (Iterator iterator = calleeUnionBoundReadSet.iterator(); iterator.hasNext();) {
281 NTuple<Descriptor> read = (NTuple<Descriptor>) iterator.next();
283 Hashtable<FlatNode, Boolean> gen = curr.get(read);
285 gen = new Hashtable<FlatNode, Boolean>();
288 Boolean currentStatus = gen.get(fn);
289 if (currentStatus == null) {
290 gen.put(fn, Boolean.FALSE);
292 checkFlag(currentStatus.booleanValue(), fn);
296 // removes <hp,statement,flag> if hp is an element of
298 // set of callee. it means that callee will overwrite it
299 for (Iterator iterator = calleeIntersectBoundOverWriteSet.iterator(); iterator.hasNext();) {
300 NTuple<Descriptor> write = (NTuple<Descriptor>) iterator.next();
301 removeHeapPath(curr, write);
302 // curr.put(write, new Hashtable<FlatNode, Boolean>());
313 private void removeHeapPath(Hashtable<NTuple<Descriptor>, Hashtable<FlatNode, Boolean>> curr,
314 NTuple<Descriptor> hp) {
316 // removes all of heap path that starts with prefix 'hp'
317 // since any reference overwrite along heap path gives overwriting side
318 // effects on the value
320 Set<NTuple<Descriptor>> keySet = curr.keySet();
321 for (Iterator<NTuple<Descriptor>> iter = keySet.iterator(); iter.hasNext();) {
322 NTuple<Descriptor> key = iter.next();
323 if (key.startsWith(hp)) {
324 curr.put(key, new Hashtable<FlatNode, Boolean>());
330 private void bindHeapPathCallerArgWithCaleeParam(FlatCall fc) {
331 // compute all possible callee set
332 // transform all READ/OVERWRITE set from the any possible
335 MethodDescriptor mdCallee = fc.getMethod();
336 FlatMethod fmCallee = state.getMethodFlat(mdCallee);
337 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
338 TypeDescriptor typeDesc = fc.getThis().getType();
339 setPossibleCallees.addAll(callGraph.getMethods(mdCallee, typeDesc));
341 // create mapping from arg idx to its heap paths
342 Hashtable<Integer, NTuple<Descriptor>> mapArgIdx2CallerArgHeapPath =
343 new Hashtable<Integer, NTuple<Descriptor>>();
345 // arg idx is starting from 'this' arg
346 NTuple<Descriptor> thisHeapPath = new NTuple<Descriptor>();
347 thisHeapPath.add(fc.getThis());
348 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(0), thisHeapPath);
350 for (int i = 0; i < fc.numArgs(); i++) {
351 TempDescriptor arg = fc.getArg(i);
352 NTuple<Descriptor> argHeapPath = computePath(arg);
353 mapArgIdx2CallerArgHeapPath.put(Integer.valueOf(i + 1), argHeapPath);
356 for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
357 MethodDescriptor callee = (MethodDescriptor) iterator.next();
358 FlatMethod calleeFlatMethod = state.getMethodFlat(callee);
360 // binding caller's args and callee's params
361 Set<NTuple<Descriptor>> calleeReadSet = mapFlatMethodToRead.get(calleeFlatMethod);
362 if (calleeReadSet == null) {
363 calleeReadSet = new HashSet<NTuple<Descriptor>>();
364 mapFlatMethodToRead.put(calleeFlatMethod, calleeReadSet);
366 Set<NTuple<Descriptor>> calleeOverWriteSet = mapFlatMethodToOverWrite.get(calleeFlatMethod);
367 if (calleeOverWriteSet == null) {
368 calleeOverWriteSet = new HashSet<NTuple<Descriptor>>();
369 mapFlatMethodToOverWrite.put(calleeFlatMethod, calleeOverWriteSet);
372 Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc =
373 new Hashtable<Integer, TempDescriptor>();
374 for (int i = 0; i < calleeFlatMethod.numParameters(); i++) {
375 TempDescriptor param = calleeFlatMethod.getParameter(i);
376 mapParamIdx2ParamTempDesc.put(Integer.valueOf(i), param);
379 Set<NTuple<Descriptor>> calleeBoundReadSet =
380 bindSet(calleeReadSet, mapParamIdx2ParamTempDesc, mapArgIdx2CallerArgHeapPath);
381 // union of the current read set and the current callee's
383 calleeUnionBoundReadSet.addAll(calleeBoundReadSet);
384 Set<NTuple<Descriptor>> calleeBoundWriteSet =
385 bindSet(calleeOverWriteSet, mapParamIdx2ParamTempDesc, mapArgIdx2CallerArgHeapPath);
386 // intersection of the current overwrite set and the current
389 merge(calleeIntersectBoundOverWriteSet, calleeBoundWriteSet);
394 private void checkFlag(boolean booleanValue, FlatNode fn) {
397 "There is a variable who comes back to the same read statement at the out-most iteration at "
398 + methodContainingSSJavaLoop.getClassDesc().getSourceFileName() + "::"
403 private void merge(Hashtable<NTuple<Descriptor>, Hashtable<FlatNode, Boolean>> curr,
404 Hashtable<NTuple<Descriptor>, Hashtable<FlatNode, Boolean>> in) {
406 Set<NTuple<Descriptor>> inKeySet = in.keySet();
407 for (Iterator iterator = inKeySet.iterator(); iterator.hasNext();) {
408 NTuple<Descriptor> inKey = (NTuple<Descriptor>) iterator.next();
409 Hashtable<FlatNode, Boolean> inPair = in.get(inKey);
411 Set<FlatNode> pairKeySet = inPair.keySet();
412 for (Iterator iterator2 = pairKeySet.iterator(); iterator2.hasNext();) {
413 FlatNode pairKey = (FlatNode) iterator2.next();
414 Boolean inFlag = inPair.get(pairKey);
416 Hashtable<FlatNode, Boolean> currPair = curr.get(inKey);
417 if (currPair == null) {
418 currPair = new Hashtable<FlatNode, Boolean>();
419 curr.put(inKey, currPair);
422 Boolean currFlag = currPair.get(pairKey);
423 // by default, flag is set by false
424 if (currFlag == null) {
425 currFlag = Boolean.FALSE;
427 currFlag = Boolean.valueOf(inFlag.booleanValue() | currFlag.booleanValue());
428 currPair.put(pairKey, currFlag);
435 private void methodReadOverWriteAnalysis() {
436 // perform method READ/OVERWRITE analysis
437 Set<MethodDescriptor> methodDescriptorsToAnalyze = new HashSet<MethodDescriptor>();
438 methodDescriptorsToAnalyze.addAll(ssjava.getAnnotationRequireSet());
440 LinkedList<MethodDescriptor> sortedDescriptors = topologicalSort(methodDescriptorsToAnalyze);
442 // no need to analyze method having ssjava loop
443 methodContainingSSJavaLoop = sortedDescriptors.removeFirst();
445 // current descriptors to visit in fixed-point interprocedural analysis,
447 // dependency in the call graph
448 Stack<MethodDescriptor> methodDescriptorsToVisitStack = new Stack<MethodDescriptor>();
450 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
451 methodDescriptorToVistSet.addAll(sortedDescriptors);
453 while (!sortedDescriptors.isEmpty()) {
454 MethodDescriptor md = sortedDescriptors.removeFirst();
455 methodDescriptorsToVisitStack.add(md);
458 // analyze scheduled methods until there are no more to visit
459 while (!methodDescriptorsToVisitStack.isEmpty()) {
460 // start to analyze leaf node
461 MethodDescriptor md = methodDescriptorsToVisitStack.pop();
462 FlatMethod fm = state.getMethodFlat(md);
464 Set<NTuple<Descriptor>> readSet = new HashSet<NTuple<Descriptor>>();
465 Set<NTuple<Descriptor>> overWriteSet = new HashSet<NTuple<Descriptor>>();
467 methodReadOverWrite_analyzeMethod(fm, readSet, overWriteSet);
469 Set<NTuple<Descriptor>> prevRead = mapFlatMethodToRead.get(fm);
470 Set<NTuple<Descriptor>> prevOverWrite = mapFlatMethodToOverWrite.get(fm);
472 if (!(readSet.equals(prevRead) && overWriteSet.equals(prevOverWrite))) {
473 mapFlatMethodToRead.put(fm, readSet);
474 mapFlatMethodToOverWrite.put(fm, overWriteSet);
476 // results for callee changed, so enqueue dependents caller for
479 Iterator<MethodDescriptor> depsItr = getDependents(md).iterator();
480 while (depsItr.hasNext()) {
481 MethodDescriptor methodNext = depsItr.next();
482 if (!methodDescriptorsToVisitStack.contains(methodNext)
483 && methodDescriptorToVistSet.contains(methodNext)) {
484 methodDescriptorsToVisitStack.add(methodNext);
495 private void methodReadOverWrite_analyzeMethod(FlatMethod fm, Set<NTuple<Descriptor>> readSet,
496 Set<NTuple<Descriptor>> overWriteSet) {
497 if (state.SSJAVADEBUG) {
498 System.out.println("Definitely written Analyzing: " + fm);
501 // intraprocedural analysis
502 Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
503 flatNodesToVisit.add(fm);
505 while (!flatNodesToVisit.isEmpty()) {
506 FlatNode fn = flatNodesToVisit.iterator().next();
507 flatNodesToVisit.remove(fn);
509 Set<NTuple<Descriptor>> curr = new HashSet<NTuple<Descriptor>>();
511 for (int i = 0; i < fn.numPrev(); i++) {
512 FlatNode prevFn = fn.getPrev(i);
513 Set<NTuple<Descriptor>> in = mapFlatNodeToWrittenSet.get(prevFn);
519 methodReadOverWrite_nodeActions(fn, curr, readSet, overWriteSet);
521 mapFlatNodeToWrittenSet.put(fn, curr);
523 for (int i = 0; i < fn.numNext(); i++) {
524 FlatNode nn = fn.getNext(i);
525 flatNodesToVisit.add(nn);
532 private void methodReadOverWrite_nodeActions(FlatNode fn, Set<NTuple<Descriptor>> writtenSet,
533 Set<NTuple<Descriptor>> readSet, Set<NTuple<Descriptor>> overWriteSet) {
539 case FKind.FlatMethod: {
541 // set up initial heap paths for method parameters
542 FlatMethod fm = (FlatMethod) fn;
543 for (int i = 0; i < fm.numParameters(); i++) {
544 TempDescriptor param = fm.getParameter(i);
545 NTuple<Descriptor> heapPath = new NTuple<Descriptor>();
547 mapHeapPath.put(param, heapPath);
552 case FKind.FlatOpNode: {
553 FlatOpNode fon = (FlatOpNode) fn;
554 // for a normal assign node, need to propagate lhs's heap path to
556 if (fon.getOp().getOp() == Operation.ASSIGN) {
560 NTuple<Descriptor> rhsHeapPath = mapHeapPath.get(rhs);
561 if (rhsHeapPath != null) {
562 mapHeapPath.put(lhs, mapHeapPath.get(rhs));
569 case FKind.FlatFieldNode:
570 case FKind.FlatElementNode: {
574 FlatFieldNode ffn = (FlatFieldNode) fn;
577 fld = ffn.getField();
580 NTuple<Descriptor> srcHeapPath = mapHeapPath.get(rhs);
581 NTuple<Descriptor> readingHeapPath = new NTuple<Descriptor>(srcHeapPath.getList());
582 readingHeapPath.add(fld);
583 mapHeapPath.put(lhs, readingHeapPath);
586 // if WT doesnot have hp(x.f), add hp(x.f) to READ
587 if (!writtenSet.contains(readingHeapPath)) {
588 readSet.add(readingHeapPath);
591 // need to kill hp(x.f) from WT
592 writtenSet.remove(readingHeapPath);
597 case FKind.FlatSetFieldNode:
598 case FKind.FlatSetElementNode: {
601 FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
603 fld = fsfn.getField();
607 NTuple<Descriptor> lhsHeapPath = mapHeapPath.get(lhs);
608 NTuple<Descriptor> newHeapPath = new NTuple<Descriptor>(lhsHeapPath.getList());
609 newHeapPath.add(fld);
610 mapHeapPath.put(fld, newHeapPath);
613 // need to add hp(y) to WT
614 writtenSet.add(newHeapPath);
619 case FKind.FlatCall: {
621 FlatCall fc = (FlatCall) fn;
623 bindHeapPathCallerArgWithCaleeParam(fc);
625 // add heap path, which is an element of READ_bound set and is not
627 // element of WT set, to the caller's READ set
628 for (Iterator iterator = calleeUnionBoundReadSet.iterator(); iterator.hasNext();) {
629 NTuple<Descriptor> read = (NTuple<Descriptor>) iterator.next();
630 if (!writtenSet.contains(read)) {
634 writtenSet.removeAll(calleeUnionBoundReadSet);
636 // add heap path, which is an element of OVERWRITE_bound set, to the
638 for (Iterator iterator = calleeIntersectBoundOverWriteSet.iterator(); iterator.hasNext();) {
639 NTuple<Descriptor> write = (NTuple<Descriptor>) iterator.next();
640 writtenSet.add(write);
646 case FKind.FlatExit: {
647 // merge the current written set with OVERWRITE set
648 merge(overWriteSet, writtenSet);
656 private void merge(Set<NTuple<Descriptor>> curr, Set<NTuple<Descriptor>> in) {
658 if (curr.isEmpty()) {
659 // WrittenSet has a special initial value which covers all possible
661 // For the first time of intersection, we can take all previous set
664 // otherwise, current set is the intersection of the two sets
670 // combine two heap path
671 private NTuple<Descriptor> combine(NTuple<Descriptor> callerIn, NTuple<Descriptor> calleeIn) {
672 NTuple<Descriptor> combined = new NTuple<Descriptor>();
674 for (int i = 0; i < callerIn.size(); i++) {
675 combined.add(callerIn.get(i));
678 // the first element of callee's heap path represents parameter
679 // so we skip the first one since it is already added from caller's heap
681 for (int i = 1; i < calleeIn.size(); i++) {
682 combined.add(calleeIn.get(i));
688 private Set<NTuple<Descriptor>> bindSet(Set<NTuple<Descriptor>> calleeSet,
689 Hashtable<Integer, TempDescriptor> mapParamIdx2ParamTempDesc,
690 Hashtable<Integer, NTuple<Descriptor>> mapCallerArgIdx2HeapPath) {
692 Set<NTuple<Descriptor>> boundedCalleeSet = new HashSet<NTuple<Descriptor>>();
694 Set<Integer> keySet = mapCallerArgIdx2HeapPath.keySet();
695 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
696 Integer idx = (Integer) iterator.next();
698 NTuple<Descriptor> callerArgHeapPath = mapCallerArgIdx2HeapPath.get(idx);
699 TempDescriptor calleeParam = mapParamIdx2ParamTempDesc.get(idx);
701 for (Iterator iterator2 = calleeSet.iterator(); iterator2.hasNext();) {
702 NTuple<Descriptor> element = (NTuple<Descriptor>) iterator2.next();
703 if (element.startsWith(calleeParam)) {
704 NTuple<Descriptor> boundElement = combine(callerArgHeapPath, element);
705 boundedCalleeSet.add(boundElement);
711 return boundedCalleeSet;
715 // Borrowed it from disjoint analysis
716 private LinkedList<MethodDescriptor> topologicalSort(Set<MethodDescriptor> toSort) {
718 Set<MethodDescriptor> discovered = new HashSet<MethodDescriptor>();
720 LinkedList<MethodDescriptor> sorted = new LinkedList<MethodDescriptor>();
722 Iterator<MethodDescriptor> itr = toSort.iterator();
723 while (itr.hasNext()) {
724 MethodDescriptor d = itr.next();
726 if (!discovered.contains(d)) {
727 dfsVisit(d, toSort, sorted, discovered);
734 // While we're doing DFS on call graph, remember
735 // dependencies for efficient queuing of methods
736 // during interprocedural analysis:
738 // a dependent of a method decriptor d for this analysis is:
739 // 1) a method or task that invokes d
740 // 2) in the descriptorsToAnalyze set
741 private void dfsVisit(MethodDescriptor md, Set<MethodDescriptor> toSort,
742 LinkedList<MethodDescriptor> sorted, Set<MethodDescriptor> discovered) {
746 // otherwise call graph guides DFS
747 Iterator itr = callGraph.getCallerSet(md).iterator();
748 while (itr.hasNext()) {
749 MethodDescriptor dCaller = (MethodDescriptor) itr.next();
751 // only consider callers in the original set to analyze
752 if (!toSort.contains(dCaller)) {
756 if (!discovered.contains(dCaller)) {
757 addDependent(md, // callee
761 dfsVisit(dCaller, toSort, sorted, discovered);
765 // for leaf-nodes last now!
769 // a dependent of a method decriptor d for this analysis is:
770 // 1) a method or task that invokes d
771 // 2) in the descriptorsToAnalyze set
772 private void addDependent(MethodDescriptor callee, MethodDescriptor caller) {
773 Set<MethodDescriptor> deps = mapDescriptorToSetDependents.get(callee);
775 deps = new HashSet<MethodDescriptor>();
778 mapDescriptorToSetDependents.put(callee, deps);
781 private Set<MethodDescriptor> getDependents(MethodDescriptor callee) {
782 Set<MethodDescriptor> deps = mapDescriptorToSetDependents.get(callee);
784 deps = new HashSet<MethodDescriptor>();
785 mapDescriptorToSetDependents.put(callee, deps);
790 private NTuple<Descriptor> computePath(TempDescriptor td) {
791 // generate proper path fot input td
792 // if td is local variable, it just generate one element tuple path
793 if (mapHeapPath.containsKey(td)) {
794 return mapHeapPath.get(td);
796 NTuple<Descriptor> path = new NTuple<Descriptor>();