Allow to declare a composite location for the initial program counter location.
[IRC.git] / Robust / src / Analysis / SSJava / SSJavaAnalysis.java
1 package Analysis.SSJava;
2
3 import java.io.BufferedWriter;
4 import java.io.FileWriter;
5 import java.io.IOException;
6 import java.util.ArrayList;
7 import java.util.Collections;
8 import java.util.Comparator;
9 import java.util.HashMap;
10 import java.util.HashSet;
11 import java.util.Hashtable;
12 import java.util.Iterator;
13 import java.util.LinkedList;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Set;
17 import java.util.StringTokenizer;
18 import java.util.Vector;
19
20 import Analysis.CallGraph.CallGraph;
21 import Analysis.Loops.GlobalFieldType;
22 import Analysis.Loops.LoopOptimize;
23 import Analysis.Loops.LoopTerminate;
24 import IR.AnnotationDescriptor;
25 import IR.ClassDescriptor;
26 import IR.Descriptor;
27 import IR.FieldDescriptor;
28 import IR.MethodDescriptor;
29 import IR.State;
30 import IR.SymbolTable;
31 import IR.TypeUtil;
32 import IR.Flat.BuildFlat;
33 import IR.Flat.FlatMethod;
34 import IR.Flat.FlatNode;
35 import Util.Pair;
36
37 public class SSJavaAnalysis {
38
39   public static final String SSJAVA = "SSJAVA";
40   public static final String LATTICE = "LATTICE";
41   public static final String METHODDEFAULT = "METHODDEFAULT";
42   public static final String THISLOC = "THISLOC";
43   public static final String GLOBALLOC = "GLOBALLOC";
44   public static final String RETURNLOC = "RETURNLOC";
45   public static final String PCLOC = "PCLOC";
46   public static final String LOC = "LOC";
47   public static final String DELTA = "DELTA";
48   public static final String TERMINATE = "TERMINATE";
49   public static final String DELEGATE = "DELEGATE";
50   public static final String DELEGATETHIS = "DELEGATETHIS";
51   public static final String TRUST = "TRUST";
52
53   public static final String TOP = "_top_";
54   public static final String BOTTOM = "_bottom_";
55
56   State state;
57   TypeUtil tu;
58   FlowDownCheck flowDownChecker;
59   MethodAnnotationCheck methodAnnotationChecker;
60   BuildFlat bf;
61
62   // set containing method requires to be annoated
63   Set<MethodDescriptor> annotationRequireSet;
64
65   // class -> field lattice
66   Hashtable<ClassDescriptor, SSJavaLattice<String>> cd2lattice;
67
68   // class -> default local variable lattice
69   Hashtable<ClassDescriptor, MethodLattice<String>> cd2methodDefault;
70
71   // method -> local variable lattice
72   Hashtable<MethodDescriptor, MethodLattice<String>> md2lattice;
73
74   // method set that does not want to have loop termination analysis
75   Hashtable<MethodDescriptor, Integer> skipLoopTerminate;
76
77   // map shared location to its descriptors
78   Hashtable<Location, Set<Descriptor>> mapSharedLocation2DescriptorSet;
79
80   // set containing a class that has at least one annoated method
81   Set<ClassDescriptor> annotationRequireClassSet;
82
83   // the set of method descriptor required to check the linear type property
84   Set<MethodDescriptor> linearTypeCheckMethodSet;
85
86   // the set of method descriptors annotated as "TRUST"
87   Set<MethodDescriptor> trustWorthyMDSet;
88
89   // method -> the initial program counter location
90   Map<MethodDescriptor, CompositeLocation> md2pcLoc;
91
92   // points to method containing SSJAVA Loop
93   private MethodDescriptor methodContainingSSJavaLoop;
94
95   private FlatNode ssjavaLoopEntrance;
96
97   // keep the field ownership from the linear type checking
98   Hashtable<MethodDescriptor, Set<FieldDescriptor>> mapMethodToOwnedFieldSet;
99
100   Set<FlatNode> sameHeightWriteFlatNodeSet;
101
102   CallGraph callgraph;
103
104   LinearTypeCheck checker;
105
106   // maps a descriptor to its known dependents: namely
107   // methods or tasks that call the descriptor's method
108   // AND are part of this analysis (reachable from main)
109   private Hashtable<Descriptor, Set<MethodDescriptor>> mapDescriptorToSetDependents;
110
111   private LinkedList<MethodDescriptor> sortedDescriptors;
112
113   public SSJavaAnalysis(State state, TypeUtil tu, BuildFlat bf, CallGraph callgraph) {
114     this.state = state;
115     this.tu = tu;
116     this.callgraph = callgraph;
117     this.cd2lattice = new Hashtable<ClassDescriptor, SSJavaLattice<String>>();
118     this.cd2methodDefault = new Hashtable<ClassDescriptor, MethodLattice<String>>();
119     this.md2lattice = new Hashtable<MethodDescriptor, MethodLattice<String>>();
120     this.annotationRequireSet = new HashSet<MethodDescriptor>();
121     this.annotationRequireClassSet = new HashSet<ClassDescriptor>();
122     this.skipLoopTerminate = new Hashtable<MethodDescriptor, Integer>();
123     this.mapSharedLocation2DescriptorSet = new Hashtable<Location, Set<Descriptor>>();
124     this.linearTypeCheckMethodSet = new HashSet<MethodDescriptor>();
125     this.bf = bf;
126     this.trustWorthyMDSet = new HashSet<MethodDescriptor>();
127     this.mapMethodToOwnedFieldSet = new Hashtable<MethodDescriptor, Set<FieldDescriptor>>();
128     this.sameHeightWriteFlatNodeSet = new HashSet<FlatNode>();
129     this.mapDescriptorToSetDependents = new Hashtable<Descriptor, Set<MethodDescriptor>>();
130     this.sortedDescriptors = new LinkedList<MethodDescriptor>();
131     this.md2pcLoc = new HashMap<MethodDescriptor, CompositeLocation>();
132   }
133
134   public void doCheck() {
135     doMethodAnnotationCheck();
136     computeLinearTypeCheckMethodSet();
137     doLinearTypeCheck();
138
139     init();
140
141     if (state.SSJAVADEBUG) {
142       debug_printAnnotationRequiredSet();
143     }
144     if (state.SSJAVAINFER) {
145       inference();
146     } else {
147       parseLocationAnnotation();
148       doFlowDownCheck();
149       doDefinitelyWrittenCheck();
150       doLoopCheck();
151     }
152   }
153
154   private void init() {
155     // perform topological sort over the set of methods accessed by the main
156     // event loop
157     Set<MethodDescriptor> methodDescriptorsToAnalyze = new HashSet<MethodDescriptor>();
158     methodDescriptorsToAnalyze.addAll(getAnnotationRequireSet());
159     sortedDescriptors = topologicalSort(methodDescriptorsToAnalyze);
160   }
161
162   public LinkedList<MethodDescriptor> getSortedDescriptors() {
163     return (LinkedList<MethodDescriptor>) sortedDescriptors.clone();
164   }
165
166   private void inference() {
167     LocationInference inferEngine = new LocationInference(this, state);
168     inferEngine.inference();
169   }
170
171   private void doLoopCheck() {
172     GlobalFieldType gft = new GlobalFieldType(callgraph, state, tu.getMain());
173     LoopOptimize lo = new LoopOptimize(gft, tu);
174
175     SymbolTable classtable = state.getClassSymbolTable();
176
177     List<ClassDescriptor> toanalyzeList = new ArrayList<ClassDescriptor>();
178     List<MethodDescriptor> toanalyzeMethodList = new ArrayList<MethodDescriptor>();
179
180     toanalyzeList.addAll(classtable.getValueSet());
181     Collections.sort(toanalyzeList, new Comparator<ClassDescriptor>() {
182       public int compare(ClassDescriptor o1, ClassDescriptor o2) {
183         return o1.getClassName().compareTo(o2.getClassName());
184       }
185     });
186
187     for (int i = 0; i < toanalyzeList.size(); i++) {
188       ClassDescriptor cd = toanalyzeList.get(i);
189
190       SymbolTable methodtable = cd.getMethodTable();
191       toanalyzeMethodList.clear();
192       toanalyzeMethodList.addAll(methodtable.getValueSet());
193       Collections.sort(toanalyzeMethodList, new Comparator<MethodDescriptor>() {
194         public int compare(MethodDescriptor o1, MethodDescriptor o2) {
195           return o1.getSymbol().compareTo(o2.getSymbol());
196         }
197       });
198
199       for (int mdIdx = 0; mdIdx < toanalyzeMethodList.size(); mdIdx++) {
200         MethodDescriptor md = toanalyzeMethodList.get(mdIdx);
201         if (needTobeAnnotated(md)) {
202           lo.analyze(state.getMethodFlat(md));
203           doLoopTerminationCheck(lo, state.getMethodFlat(md));
204         }
205       }
206
207     }
208
209   }
210
211   public void addTrustMethod(MethodDescriptor md) {
212     trustWorthyMDSet.add(md);
213   }
214
215   public boolean isTrustMethod(MethodDescriptor md) {
216     return trustWorthyMDSet.contains(md);
217   }
218
219   private void computeLinearTypeCheckMethodSet() {
220
221     Set<MethodDescriptor> allCalledSet = callgraph.getMethodCalls(tu.getMain());
222     linearTypeCheckMethodSet.addAll(allCalledSet);
223
224     Set<MethodDescriptor> trustedSet = new HashSet<MethodDescriptor>();
225
226     for (Iterator iterator = trustWorthyMDSet.iterator(); iterator.hasNext();) {
227       MethodDescriptor trustMethod = (MethodDescriptor) iterator.next();
228       Set<MethodDescriptor> calledFromTrustMethodSet = callgraph.getMethodCalls(trustMethod);
229       trustedSet.add(trustMethod);
230       trustedSet.addAll(calledFromTrustMethodSet);
231     }
232
233     linearTypeCheckMethodSet.removeAll(trustedSet);
234
235     // if a method is called only by trusted method, no need to check linear
236     // type & flow down rule
237     for (Iterator iterator = trustedSet.iterator(); iterator.hasNext();) {
238       MethodDescriptor md = (MethodDescriptor) iterator.next();
239       Set<MethodDescriptor> callerSet = callgraph.getCallerSet(md);
240       if (!trustedSet.containsAll(callerSet) && !trustWorthyMDSet.contains(md)) {
241         linearTypeCheckMethodSet.add(md);
242       }
243     }
244
245   }
246
247   private void doLinearTypeCheck() {
248     LinearTypeCheck checker = new LinearTypeCheck(this, state);
249     checker.linearTypeCheck();
250   }
251
252   public void debug_printAnnotationRequiredSet() {
253     System.out.println("SSJAVA: SSJava is checking the following methods:");
254     for (Iterator<MethodDescriptor> iterator = annotationRequireSet.iterator(); iterator.hasNext();) {
255       MethodDescriptor md = iterator.next();
256       System.out.println(md);
257     }
258     System.out.println();
259   }
260
261   private void doMethodAnnotationCheck() {
262     methodAnnotationChecker = new MethodAnnotationCheck(this, state, tu);
263     methodAnnotationChecker.methodAnnoatationCheck();
264     methodAnnotationChecker.methodAnnoataionInheritanceCheck();
265     state.setAnnotationRequireSet(annotationRequireSet);
266   }
267
268   public void doFlowDownCheck() {
269     flowDownChecker = new FlowDownCheck(this, state);
270     flowDownChecker.flowDownCheck();
271   }
272
273   public void doDefinitelyWrittenCheck() {
274     DefinitelyWrittenCheck checker = new DefinitelyWrittenCheck(this, state);
275     checker.definitelyWrittenCheck();
276   }
277
278   private void parseLocationAnnotation() {
279     Iterator it = state.getClassSymbolTable().getDescriptorsIterator();
280     while (it.hasNext()) {
281       ClassDescriptor cd = (ClassDescriptor) it.next();
282       // parsing location hierarchy declaration for the class
283       Vector<AnnotationDescriptor> classAnnotations = cd.getModifier().getAnnotations();
284       for (int i = 0; i < classAnnotations.size(); i++) {
285         AnnotationDescriptor an = classAnnotations.elementAt(i);
286         String marker = an.getMarker();
287         if (marker.equals(LATTICE)) {
288           SSJavaLattice<String> locOrder =
289               new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM);
290           cd2lattice.put(cd, locOrder);
291           parseClassLatticeDefinition(cd, an.getValue(), locOrder);
292
293           if (state.SSJAVADEBUG) {
294             // generate lattice dot file
295             writeLatticeDotFile(cd, null, locOrder);
296           }
297
298         } else if (marker.equals(METHODDEFAULT)) {
299           MethodLattice<String> locOrder =
300               new MethodLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM);
301           cd2methodDefault.put(cd, locOrder);
302           parseMethodDefaultLatticeDefinition(cd, an.getValue(), locOrder);
303         }
304       }
305
306       for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
307         MethodDescriptor md = (MethodDescriptor) method_it.next();
308         // parsing location hierarchy declaration for the method
309
310         if (needTobeAnnotated(md)) {
311           Vector<AnnotationDescriptor> methodAnnotations = md.getModifiers().getAnnotations();
312           if (methodAnnotations != null) {
313             for (int i = 0; i < methodAnnotations.size(); i++) {
314               AnnotationDescriptor an = methodAnnotations.elementAt(i);
315               if (an.getMarker().equals(LATTICE)) {
316                 // developer explicitly defines method lattice
317                 MethodLattice<String> locOrder =
318                     new MethodLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM);
319                 md2lattice.put(md, locOrder);
320                 parseMethodDefaultLatticeDefinition(cd, an.getValue(), locOrder);
321               } else if (an.getMarker().equals(TERMINATE)) {
322                 // developer explicitly wants to skip loop termination analysis
323                 String value = an.getValue();
324                 int maxIteration = 0;
325                 if (value != null) {
326                   maxIteration = Integer.parseInt(value);
327                 }
328                 skipLoopTerminate.put(md, new Integer(maxIteration));
329               }
330             }
331           }
332         }
333
334       }
335
336     }
337   }
338
339   public <T> void writeLatticeDotFile(ClassDescriptor cd, MethodDescriptor md,
340       SSJavaLattice<T> locOrder) {
341
342     String fileName = "lattice_";
343     if (md != null) {
344       fileName +=
345           cd.getSymbol().replaceAll("[\\W_]", "") + "_" + md.getSymbol().replaceAll("[\\W_]", "");
346     } else {
347       fileName += cd.getSymbol().replaceAll("[\\W_]", "");
348     }
349
350     Set<Pair<T, T>> pairSet = locOrder.getOrderingPairSet();
351
352     if (pairSet.size() > 0) {
353       try {
354         BufferedWriter bw = new BufferedWriter(new FileWriter(fileName + ".dot"));
355
356         bw.write("digraph " + fileName + " {\n");
357
358         for (Iterator iterator = pairSet.iterator(); iterator.hasNext();) {
359           // pair is in the form of <higher, lower>
360           Pair<T, T> pair = (Pair<T, T>) iterator.next();
361
362           T highLocId = pair.getFirst();
363           String highLocStr, lowLocStr;
364           if (locOrder.isSharedLoc(highLocId)) {
365             highLocStr = "\"" + highLocId + "*\"";
366           } else {
367             highLocStr = highLocId.toString();
368           }
369           T lowLocId = pair.getSecond();
370           if (locOrder.isSharedLoc(lowLocId)) {
371             lowLocStr = "\"" + lowLocId + "*\"";
372           } else {
373             lowLocStr = lowLocId.toString();
374           }
375           bw.write(highLocId + " -> " + lowLocId + ";\n");
376         }
377         bw.write("}\n");
378         bw.close();
379
380       } catch (IOException e) {
381         e.printStackTrace();
382       }
383
384     }
385
386   }
387
388   private void parseMethodDefaultLatticeDefinition(ClassDescriptor cd, String value,
389       MethodLattice<String> locOrder) {
390
391     value = value.replaceAll(" ", ""); // remove all blank spaces
392
393     StringTokenizer tokenizer = new StringTokenizer(value, ",");
394
395     while (tokenizer.hasMoreTokens()) {
396       String orderElement = tokenizer.nextToken();
397       int idx = orderElement.indexOf("<");
398       if (idx > 0) {// relative order element
399         String lowerLoc = orderElement.substring(0, idx);
400         String higherLoc = orderElement.substring(idx + 1);
401         locOrder.put(higherLoc, lowerLoc);
402         if (locOrder.isIntroducingCycle(higherLoc)) {
403           throw new Error("Error: the order relation " + lowerLoc + " < " + higherLoc
404               + " introduces a cycle.");
405         }
406       } else if (orderElement.startsWith(THISLOC + "=")) {
407         String thisLoc = orderElement.substring(8);
408         locOrder.setThisLoc(thisLoc);
409       } else if (orderElement.startsWith(GLOBALLOC + "=")) {
410         String globalLoc = orderElement.substring(10);
411         locOrder.setGlobalLoc(globalLoc);
412       } else if (orderElement.startsWith(RETURNLOC + "=")) {
413         String returnLoc = orderElement.substring(10);
414         locOrder.setReturnLoc(returnLoc);
415       } else if (orderElement.endsWith("*")) {
416         // spin loc definition
417         locOrder.addSharedLoc(orderElement.substring(0, orderElement.length() - 1));
418       } else {
419         // single element
420         locOrder.put(orderElement);
421       }
422     }
423
424     // sanity checks
425     if (locOrder.getThisLoc() != null && !locOrder.containsKey(locOrder.getThisLoc())) {
426       throw new Error("Variable 'this' location '" + locOrder.getThisLoc()
427           + "' is not defined in the local variable lattice at " + cd.getSourceFileName());
428     }
429
430     if (locOrder.getGlobalLoc() != null && !locOrder.containsKey(locOrder.getGlobalLoc())) {
431       throw new Error("Variable global location '" + locOrder.getGlobalLoc()
432           + "' is not defined in the local variable lattice at " + cd.getSourceFileName());
433     }
434   }
435
436   private void parseClassLatticeDefinition(ClassDescriptor cd, String value,
437       SSJavaLattice<String> locOrder) {
438
439     value = value.replaceAll(" ", ""); // remove all blank spaces
440
441     StringTokenizer tokenizer = new StringTokenizer(value, ",");
442
443     while (tokenizer.hasMoreTokens()) {
444       String orderElement = tokenizer.nextToken();
445       int idx = orderElement.indexOf("<");
446
447       if (idx > 0) {// relative order element
448         String lowerLoc = orderElement.substring(0, idx);
449         String higherLoc = orderElement.substring(idx + 1);
450         locOrder.put(higherLoc, lowerLoc);
451         if (locOrder.isIntroducingCycle(higherLoc)) {
452           throw new Error("Error: the order relation " + lowerLoc + " < " + higherLoc
453               + " introduces a cycle.");
454         }
455       } else if (orderElement.contains("*")) {
456         // spin loc definition
457         locOrder.addSharedLoc(orderElement.substring(0, orderElement.length() - 1));
458       } else {
459         // single element
460         locOrder.put(orderElement);
461       }
462     }
463
464     // sanity check
465     Set<String> spinLocSet = locOrder.getSharedLocSet();
466     for (Iterator iterator = spinLocSet.iterator(); iterator.hasNext();) {
467       String spinLoc = (String) iterator.next();
468       if (!locOrder.containsKey(spinLoc)) {
469         throw new Error("Spin location '" + spinLoc
470             + "' is not defined in the default local variable lattice at " + cd.getSourceFileName());
471       }
472     }
473   }
474
475   public Hashtable<ClassDescriptor, SSJavaLattice<String>> getCd2lattice() {
476     return cd2lattice;
477   }
478
479   public Hashtable<ClassDescriptor, MethodLattice<String>> getCd2methodDefault() {
480     return cd2methodDefault;
481   }
482
483   public Hashtable<MethodDescriptor, MethodLattice<String>> getMd2lattice() {
484     return md2lattice;
485   }
486
487   public SSJavaLattice<String> getClassLattice(ClassDescriptor cd) {
488     return cd2lattice.get(cd);
489   }
490
491   public MethodLattice<String> getMethodDefaultLattice(ClassDescriptor cd) {
492     return cd2methodDefault.get(cd);
493   }
494
495   public MethodLattice<String> getMethodLattice(MethodDescriptor md) {
496     if (md2lattice.containsKey(md)) {
497       return md2lattice.get(md);
498     } else {
499
500       if (cd2methodDefault.containsKey(md.getClassDesc())) {
501         return cd2methodDefault.get(md.getClassDesc());
502       } else {
503         throw new Error("Method Lattice of " + md + " is not defined.");
504       }
505
506     }
507   }
508
509   public CompositeLocation getPCLocation(MethodDescriptor md) {
510     if (!md2pcLoc.containsKey(md)) {
511       // by default, the initial pc location is TOP
512       CompositeLocation pcLoc = new CompositeLocation(new Location(md, Location.TOP));
513       md2pcLoc.put(md, pcLoc);
514     }
515     return md2pcLoc.get(md);
516   }
517
518   public void setPCLocation(MethodDescriptor md, CompositeLocation pcLoc) {
519     md2pcLoc.put(md, pcLoc);
520   }
521
522   public boolean needToCheckLinearType(MethodDescriptor md) {
523     return linearTypeCheckMethodSet.contains(md);
524   }
525
526   public boolean needTobeAnnotated(MethodDescriptor md) {
527     return annotationRequireSet.contains(md);
528   }
529
530   public boolean needToBeAnnoated(ClassDescriptor cd) {
531     return annotationRequireClassSet.contains(cd);
532   }
533
534   public void addAnnotationRequire(ClassDescriptor cd) {
535     annotationRequireClassSet.add(cd);
536   }
537
538   public void addAnnotationRequire(MethodDescriptor md) {
539
540     ClassDescriptor cd = md.getClassDesc();
541     // if a method requires to be annotated, class containg that method also
542     // requires to be annotated
543     if (!isSSJavaUtil(cd)) {
544       annotationRequireClassSet.add(cd);
545       annotationRequireSet.add(md);
546     }
547   }
548
549   public Set<MethodDescriptor> getAnnotationRequireSet() {
550     return annotationRequireSet;
551   }
552
553   public void doLoopTerminationCheck(LoopOptimize lo, FlatMethod fm) {
554     LoopTerminate lt = new LoopTerminate(this, state);
555     if (needTobeAnnotated(fm.getMethod())) {
556       lt.terminateAnalysis(fm, lo.getLoopInvariant(fm));
557     }
558   }
559
560   public CallGraph getCallGraph() {
561     return callgraph;
562   }
563
564   public SSJavaLattice<String> getLattice(Descriptor d) {
565
566     if (d instanceof MethodDescriptor) {
567       return getMethodLattice((MethodDescriptor) d);
568     } else {
569       return getClassLattice((ClassDescriptor) d);
570     }
571
572   }
573
574   public boolean isSharedLocation(Location loc) {
575     SSJavaLattice<String> lattice = getLattice(loc.getDescriptor());
576     return lattice.getSharedLocSet().contains(loc.getLocIdentifier());
577   }
578
579   public void mapSharedLocation2Descriptor(Location loc, Descriptor d) {
580     Set<Descriptor> set = mapSharedLocation2DescriptorSet.get(loc);
581     if (set == null) {
582       set = new HashSet<Descriptor>();
583       mapSharedLocation2DescriptorSet.put(loc, set);
584     }
585     set.add(d);
586   }
587
588   public BuildFlat getBuildFlat() {
589     return bf;
590   }
591
592   public MethodDescriptor getMethodContainingSSJavaLoop() {
593     return methodContainingSSJavaLoop;
594   }
595
596   public void setMethodContainingSSJavaLoop(MethodDescriptor methodContainingSSJavaLoop) {
597     this.methodContainingSSJavaLoop = methodContainingSSJavaLoop;
598   }
599
600   public boolean isSSJavaUtil(ClassDescriptor cd) {
601     if (cd.getSymbol().equals("SSJAVA")) {
602       return true;
603     }
604     return false;
605   }
606
607   public void setFieldOnwership(MethodDescriptor md, FieldDescriptor field) {
608
609     Set<FieldDescriptor> fieldSet = mapMethodToOwnedFieldSet.get(md);
610     if (fieldSet == null) {
611       fieldSet = new HashSet<FieldDescriptor>();
612       mapMethodToOwnedFieldSet.put(md, fieldSet);
613     }
614     fieldSet.add(field);
615   }
616
617   public boolean isOwnedByMethod(MethodDescriptor md, FieldDescriptor field) {
618     Set<FieldDescriptor> fieldSet = mapMethodToOwnedFieldSet.get(md);
619     if (fieldSet != null) {
620       return fieldSet.contains(field);
621     }
622     return false;
623   }
624
625   public FlatNode getSSJavaLoopEntrance() {
626     return ssjavaLoopEntrance;
627   }
628
629   public void setSSJavaLoopEntrance(FlatNode ssjavaLoopEntrance) {
630     this.ssjavaLoopEntrance = ssjavaLoopEntrance;
631   }
632
633   public void addSameHeightWriteFlatNode(FlatNode fn) {
634     this.sameHeightWriteFlatNodeSet.add(fn);
635   }
636
637   public boolean isSameHeightWrite(FlatNode fn) {
638     return this.sameHeightWriteFlatNodeSet.contains(fn);
639   }
640
641   public LinkedList<MethodDescriptor> topologicalSort(Set<MethodDescriptor> toSort) {
642
643     Set<MethodDescriptor> discovered = new HashSet<MethodDescriptor>();
644
645     LinkedList<MethodDescriptor> sorted = new LinkedList<MethodDescriptor>();
646
647     Iterator<MethodDescriptor> itr = toSort.iterator();
648     while (itr.hasNext()) {
649       MethodDescriptor d = itr.next();
650
651       if (!discovered.contains(d)) {
652         dfsVisit(d, toSort, sorted, discovered);
653       }
654     }
655
656     return sorted;
657   }
658
659   // While we're doing DFS on call graph, remember
660   // dependencies for efficient queuing of methods
661   // during interprocedural analysis:
662   //
663   // a dependent of a method decriptor d for this analysis is:
664   // 1) a method or task that invokes d
665   // 2) in the descriptorsToAnalyze set
666   private void dfsVisit(MethodDescriptor md, Set<MethodDescriptor> toSort,
667       LinkedList<MethodDescriptor> sorted, Set<MethodDescriptor> discovered) {
668
669     discovered.add(md);
670
671     Iterator itr2 = callgraph.getCalleeSet(md).iterator();
672     while (itr2.hasNext()) {
673       MethodDescriptor dCallee = (MethodDescriptor) itr2.next();
674       addDependent(dCallee, md);
675     }
676
677     Iterator itr = callgraph.getCallerSet(md).iterator();
678     while (itr.hasNext()) {
679       MethodDescriptor dCaller = (MethodDescriptor) itr.next();
680       // only consider callers in the original set to analyze
681       if (!toSort.contains(dCaller)) {
682         continue;
683       }
684       if (!discovered.contains(dCaller)) {
685         addDependent(md, // callee
686             dCaller // caller
687         );
688
689         dfsVisit(dCaller, toSort, sorted, discovered);
690       }
691     }
692
693     // for leaf-nodes last now!
694     sorted.addLast(md);
695   }
696
697   public void addDependent(MethodDescriptor callee, MethodDescriptor caller) {
698     Set<MethodDescriptor> deps = mapDescriptorToSetDependents.get(callee);
699     if (deps == null) {
700       deps = new HashSet<MethodDescriptor>();
701     }
702     deps.add(caller);
703     mapDescriptorToSetDependents.put(callee, deps);
704   }
705
706   public Set<MethodDescriptor> getDependents(MethodDescriptor callee) {
707     Set<MethodDescriptor> deps = mapDescriptorToSetDependents.get(callee);
708     if (deps == null) {
709       deps = new HashSet<MethodDescriptor>();
710       mapDescriptorToSetDependents.put(callee, deps);
711     }
712     return deps;
713   }
714
715 }