1 package Analysis.SSJava;
3 import java.util.HashMap;
4 import java.util.HashSet;
5 import java.util.Iterator;
9 import IR.ClassDescriptor;
11 import IR.MethodDescriptor;
12 import IR.NameDescriptor;
15 public class BuildLattice {
17 private LocationInference infer;
18 private Map<HNode, TripleItem> mapSharedNodeToTripleItem;
19 private Map<HNode, Integer> mapHNodeToHighestIndex;
21 private Map<Descriptor, Map<TripleItem, String>> mapDescToIntermediateLocMap;
23 private Map<Pair<HNode, HNode>, Integer> mapItemToHighestIndex;
25 public BuildLattice(LocationInference infer) {
27 this.mapSharedNodeToTripleItem = new HashMap<HNode, TripleItem>();
28 this.mapHNodeToHighestIndex = new HashMap<HNode, Integer>();
29 this.mapItemToHighestIndex = new HashMap<Pair<HNode, HNode>, Integer>();
30 this.mapDescToIntermediateLocMap = new HashMap<Descriptor, Map<TripleItem, String>>();
33 public SSJavaLattice<String> buildLattice(Descriptor desc) {
35 HierarchyGraph inputGraph = infer.getSkeletonCombinationHierarchyGraph(desc);
36 LocationSummary locSummary = infer.getLocationSummary(desc);
38 HierarchyGraph naiveGraph = infer.getSimpleHierarchyGraph(desc);
40 // I don't think we need to keep the below if statement anymore
41 // because hierarchy graph does not have any composite location
42 Set<HNode> nodeSetWithCompositeLocation = new HashSet<HNode>();
43 if (desc instanceof MethodDescriptor) {
44 FlowGraph flowGraph = infer.getFlowGraph((MethodDescriptor) desc);
46 for (Iterator iterator = inputGraph.getNodeSet().iterator(); iterator.hasNext();) {
47 HNode hnode = (HNode) iterator.next();
48 Descriptor hnodeDesc = hnode.getDescriptor();
49 if (hnodeDesc != null) {
50 NTuple<Descriptor> descTuple = new NTuple<Descriptor>();
51 descTuple.add(hnodeDesc);
53 if (flowGraph.contains(descTuple)) {
54 FlowNode flowNode = flowGraph.getFlowNode(descTuple);
55 if (flowNode.getCompositeLocation() != null) {
56 nodeSetWithCompositeLocation.add(hnode);
65 // lattice generation for the native approach
66 BasisSet naiveBasisSet = naiveGraph.computeBasisSet(nodeSetWithCompositeLocation);
67 // debug_print(inputGraph);
69 Family naiveFamily = generateFamily(naiveBasisSet);
70 Map<Set<Integer>, Set<Set<Integer>>> naive_mapImSucc =
71 coveringGraph(naiveBasisSet, naiveFamily);
73 SSJavaLattice<String> naive_lattice =
74 buildLattice(desc, naiveBasisSet, naiveGraph, null, naive_mapImSucc);
75 LocationInference.numLocationsNaive += naive_lattice.getKeySet().size();
77 // lattice generation for the proposed approach
78 BasisSet basisSet = inputGraph.computeBasisSet(nodeSetWithCompositeLocation);
79 // debug_print(inputGraph);
81 Family family = generateFamily(basisSet);
82 Map<Set<Integer>, Set<Set<Integer>>> mapImSucc = coveringGraph(basisSet, family);
84 SSJavaLattice<String> lattice = buildLattice(desc, basisSet, inputGraph, locSummary, mapImSucc);
89 public void setIntermediateLocMap(Descriptor desc, Map<TripleItem, String> map) {
90 mapDescToIntermediateLocMap.put(desc, map);
93 public Map<TripleItem, String> getIntermediateLocMap(Descriptor desc) {
94 if (!mapDescToIntermediateLocMap.containsKey(desc)) {
95 mapDescToIntermediateLocMap.put(desc, new HashMap<TripleItem, String>());
97 return mapDescToIntermediateLocMap.get(desc);
100 private Descriptor getParent(Descriptor desc) {
101 if (desc instanceof MethodDescriptor) {
102 MethodDescriptor md = (MethodDescriptor) desc;
103 ClassDescriptor cd = md.getClassDesc();
104 return infer.getParentMethodDesc(cd, md);
106 return ((ClassDescriptor) desc).getSuperDesc();
110 private SSJavaLattice<String> buildLattice(Descriptor desc, BasisSet basisSet,
111 HierarchyGraph inputGraph, LocationSummary locSummary,
112 Map<Set<Integer>, Set<Set<Integer>>> mapImSucc) {
114 System.out.println("\nBuild Lattice:" + inputGraph.getName());
116 SSJavaLattice<String> lattice =
117 new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM);
119 Map<Set<Integer>, String> mapFToLocName = new HashMap<Set<Integer>, String>();
121 Set<Set<Integer>> keySet = mapImSucc.keySet();
122 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
123 Set<Integer> higher = (Set<Integer>) iterator.next();
125 String higherName = generateElementName(basisSet, inputGraph, mapFToLocName, higher);
127 HNode higherNode = inputGraph.getHNode(higherName);
129 if (higherNode == null) {
130 NameDescriptor d = new NameDescriptor(higherName);
131 higherNode = inputGraph.getHNode(d);
132 higherNode.setSkeleton(true);
135 if (higherNode != null && higherNode.isSharedNode()) {
136 lattice.addSharedLoc(higherName);
138 Set<Descriptor> descSet = inputGraph.getDescSetOfNode(higherNode);
139 // System.out.println("higherName=" + higherName + " higherNode=" + higherNode + " descSet="
142 if (locSummary != null) {
143 for (Iterator iterator2 = descSet.iterator(); iterator2.hasNext();) {
144 Descriptor d = (Descriptor) iterator2.next();
145 locSummary.addMapHNodeNameToLocationName(d.getSymbol(), higherName);
149 // locSummary.addMapHNodeNameToLocationName(higherName, higherName);
151 Set<Set<Integer>> lowerSet = mapImSucc.get(higher);
152 for (Iterator iterator2 = lowerSet.iterator(); iterator2.hasNext();) {
153 Set<Integer> lower = (Set<Integer>) iterator2.next();
155 String lowerName = generateElementName(basisSet, inputGraph, mapFToLocName, lower);
156 HNode lowerNode = inputGraph.getHNode(lowerName);
158 if (lowerNode == null && !lowerName.equals(SSJavaAnalysis.BOTTOM)) {
159 NameDescriptor d = new NameDescriptor(lowerName);
160 lowerNode = inputGraph.getHNode(d);
161 lowerNode.setSkeleton(true);
164 if (lowerNode != null && !inputGraph.isDirectlyConnectedTo(higherNode, lowerNode)) {
165 inputGraph.addEdge(higherNode, lowerNode);
168 if (lowerNode != null && lowerNode.isSharedNode()) {
169 lattice.addSharedLoc(lowerName);
172 Set<Descriptor> lowerDescSet = inputGraph.getDescSetOfNode(lowerNode);
173 // System.out.println("lowerName=" + lowerName + " lowerNode=" + lowerNode + " descSet="
175 if (locSummary != null) {
176 for (Iterator iterator3 = lowerDescSet.iterator(); iterator3.hasNext();) {
177 Descriptor d = (Descriptor) iterator3.next();
178 locSummary.addMapHNodeNameToLocationName(d.getSymbol(), lowerName);
181 // locSummary.addMapHNodeNameToLocationName(lowerName, lowerName);
183 if (higher.size() == 0) {
185 lattice.put(lowerName);
187 lattice.addRelationHigherToLower(higherName, lowerName);
194 inputGraph.removeRedundantEdges();
198 public HNode getCombinationNodeInSCGraph(Descriptor desc, HNode nodeFromSimpleGraph) {
200 HierarchyGraph scGraph = infer.getSkeletonCombinationHierarchyGraph(desc);
202 if (nodeFromSimpleGraph.isSkeleton()) {
203 return scGraph.getCurrentHNode(nodeFromSimpleGraph);
206 Set<HNode> combineSkeletonNodeSet =
207 infer.getSimpleHierarchyGraph(desc).getCombineSetByCombinationNode(nodeFromSimpleGraph);
208 HNode combinationNodeInSCGraph =
209 infer.getSkeletonCombinationHierarchyGraph(desc).getMapCombineNodeSetToCombinationNode()
210 .get(combineSkeletonNodeSet);
212 // Set<HNode> combineSkeletonNodeSet =
213 // infer.getSimpleHierarchyGraph(desc).getCombineSetByCombinationNode(simpleGraphNode);
214 // HNode combinationNodeInSCGraph =
215 // infer.getSkeletonCombinationHierarchyGraph(desc).getCombinationNode(combineSkeletonNodeSet);
216 return combinationNodeInSCGraph;
219 public SSJavaLattice<String> insertIntermediateNodesToStraightLine(Descriptor desc,
220 SSJavaLattice<String> skeletonLattice) {
221 // copy nodes/edges from the parent method/class if possible
222 SSJavaLattice<String> lattice = skeletonLattice.clone();
224 Descriptor parentDesc = getParent(desc);
225 if (parentDesc != null) {
226 SSJavaLattice<String> parentLattice = infer.getLattice(parentDesc);
228 Map<String, Set<String>> parentMap = parentLattice.getTable();
229 Set<String> parentKeySet = parentMap.keySet();
230 for (Iterator iterator = parentKeySet.iterator(); iterator.hasNext();) {
231 String parentKey = (String) iterator.next();
232 Set<String> parentValueSet = parentMap.get(parentKey);
233 for (Iterator iterator2 = parentValueSet.iterator(); iterator2.hasNext();) {
234 String value = (String) iterator2.next();
235 lattice.put(parentKey, value);
239 Set<String> parentSharedLocSet = parentLattice.getSharedLocSet();
240 for (Iterator iterator = parentSharedLocSet.iterator(); iterator.hasNext();) {
241 String parentSharedLoc = (String) iterator.next();
242 lattice.addSharedLoc(parentSharedLoc);
248 // perform DFS that starts from each skeleton/combination node and ends by another
249 // skeleton/combination node
251 mapSharedNodeToTripleItem.clear();
253 HierarchyGraph simpleGraph = infer.getSimpleHierarchyGraph(desc);
254 HierarchyGraph scGraph = infer.getSkeletonCombinationHierarchyGraph(desc);
255 LocationSummary locSummary = infer.getLocationSummary(desc);
257 Set<HNode> visited = new HashSet<HNode>();
259 Set<HNode> nodeSet = simpleGraph.getNodeSet();
261 Map<TripleItem, String> mapIntermediateLoc = getIntermediateLocMap(desc);
262 // Map<TripleItem, String> mapIntermediateLoc = new HashMap<TripleItem, String>();
264 // System.out.println("*insert=" + desc);
265 // System.out.println("***nodeSet=" + nodeSet);
266 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
267 HNode node = (HNode) iterator.next();
268 // System.out.println("node=" + node);
270 if (node.isSkeleton() && (!visited.contains(node))) {
273 Set<HNode> outSet = simpleGraph.getOutgoingNodeSet(node);
274 for (Iterator iterator2 = outSet.iterator(); iterator2.hasNext();) {
275 HNode outNode = (HNode) iterator2.next();
277 if (!outNode.isSkeleton()) {
278 if (outNode.isCombinationNode()) {
279 if (visited.containsAll(simpleGraph.getIncomingNodeSet(outNode))) {
280 // if (needToExpandCombinationNode(desc, outNode)) {
281 expandCombinationNode(desc, lattice, visited, mapIntermediateLoc, locSummary,
286 // we have a node that is neither combination or skeleton node
287 // System.out.println("%%%skeleton node=" + node + " outNode=" + outNode);
288 HNode startNode = scGraph.getCurrentHNode(node);
290 // if (node.getDescriptor() != null) {
291 // // node is a skeleton node and it might be merged into another node in the SC
293 // startNode = scGraph.getHNode(node.getDescriptor());
295 // // this node has already been merged before the SC graph.
300 // Set<HNode> endNodeSetFromSimpleGraph =
301 // simpleGraph.getDirectlyReachableSkeletonCombinationNodeFrom(outNode, null);
302 // Set<HNode> endCombNodeSet = new HashSet<HNode>();
303 // for (Iterator iterator3 = endNodeSetFromSimpleGraph.iterator();
304 // iterator3.hasNext();) {
305 // HNode endNode = (HNode) iterator3.next();
306 // endCombNodeSet.add(getCombinationNodeInSCGraph(desc, endNode));
309 Set<HNode> endCombNodeSet = scGraph.getOutgoingNodeSet(startNode);
311 // System.out.println("endCombNodeSet=" + endCombNodeSet);
312 visited.add(outNode);
313 if (endCombNodeSet.size() > 0) {
314 // follows the straight line up to another skeleton/combination node
315 endCombNodeSet = removeTransitivelyReachToNode(desc, startNode, endCombNodeSet);
316 } else if (endCombNodeSet.size() == 0) {
317 // the outNode is (directly/transitively) connected to the bottom node
318 // therefore, we just add a dummy bottom HNode to the endCombNodeSet.
319 endCombNodeSet.add(LocationInference.BOTTOMHNODE);
322 recurDFSNormalNode(desc, lattice, startNode, endCombNodeSet, visited,
323 mapIntermediateLoc, 1, locSummary, outNode);
329 } else if (!node.isSkeleton() && !node.isCombinationNode() && !node.isMergeNode()
330 && !visited.contains(node)) {
332 System.out.println("n=" + node);
334 // an intermediate node 'node' may be located between "TOP" location and a skeleton node
335 if (simpleGraph.getIncomingNodeSet(node).size() == 0) {
337 // this node will be directly connected to the TOP location
338 // start adding the following nodes from this node
340 Set<HNode> endNodeSetFromSimpleGraph =
341 simpleGraph.getDirectlyReachableSkeletonCombinationNodeFrom(node, null);
343 Set<HNode> endCombNodeSet = new HashSet<HNode>();
344 for (Iterator iterator3 = endNodeSetFromSimpleGraph.iterator(); iterator3.hasNext();) {
345 HNode endNode = (HNode) iterator3.next();
346 endCombNodeSet.add(getCombinationNodeInSCGraph(desc, endNode));
349 System.out.println("endCombNodeSet=" + endCombNodeSet);
350 HNode startNode = LocationInference.TOPHNODE;
351 visited.add(startNode);
352 if (endCombNodeSet.size() > 0) {
353 // follows the straight line up to another skeleton/combination node
354 // endCombNodeSet = removeTransitivelyReachToNode(desc, node, endCombNodeSet);
355 recurDFSNormalNode(desc, lattice, startNode, endCombNodeSet, visited,
356 mapIntermediateLoc, 1, locSummary, node);
364 // add shared locations
365 Set<HNode> sharedNodeSet = mapSharedNodeToTripleItem.keySet();
366 for (Iterator iterator = sharedNodeSet.iterator(); iterator.hasNext();) {
367 HNode sharedNode = (HNode) iterator.next();
368 TripleItem item = mapSharedNodeToTripleItem.get(sharedNode);
369 String nonSharedLocName = mapIntermediateLoc.get(item);
371 System.out.println("sharedNode=" + sharedNode + " locName=" + nonSharedLocName);
374 if (locSummary.getHNodeNameSetByLatticeLoationName(nonSharedLocName) != null
375 && !lattice.isSharedLoc(nonSharedLocName)) {
376 // need to generate a new shared location in the lattice, which is one level lower than the
377 // 'locName' location
378 newLocName = "ILOC" + (LocationInference.locSeed++);
380 // Set<String> aboveElementSet = getAboveElementSet(lattice, locName);
381 Set<String> belowElementSet = new HashSet<String>();
382 belowElementSet.addAll(lattice.get(nonSharedLocName));
384 System.out.println("nonSharedLocName=" + nonSharedLocName + " belowElementSet="
385 + belowElementSet + " newLocName=" + newLocName);
387 lattice.insertNewLocationBetween(nonSharedLocName, belowElementSet, newLocName);
389 newLocName = nonSharedLocName;
392 lattice.addSharedLoc(newLocName);
393 HierarchyGraph graph = infer.getSimpleHierarchyGraph(desc);
394 Set<Descriptor> descSet = graph.getDescSetOfNode(sharedNode);
395 for (Iterator iterator2 = descSet.iterator(); iterator2.hasNext();) {
396 Descriptor d = (Descriptor) iterator2.next();
397 locSummary.addMapHNodeNameToLocationName(d.getSymbol(), newLocName);
399 locSummary.addMapHNodeNameToLocationName(sharedNode.getName(), newLocName);
407 private Set<String> getAboveElementSet(SSJavaLattice<String> lattice, String loc) {
409 Set<String> aboveSet = new HashSet<String>();
411 Map<String, Set<String>> latticeMap = lattice.getTable();
412 Set<String> keySet = latticeMap.keySet();
413 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
414 String key = (String) iterator.next();
415 if (latticeMap.get(key).contains(loc)) {
423 private boolean needToExpandCombinationNode(Descriptor desc, HNode cnode) {
425 System.out.println("needToExpandCombinationNode?=" + cnode);
427 HierarchyGraph simpleGraph = infer.getSimpleHierarchyGraph(desc);
428 // HNode combinationNodeInSCGraph = getCombinationNodeInSCGraph(desc, cnode);
429 Set<HNode> combineSkeletonNodeSet = simpleGraph.getCombineSetByCombinationNode(cnode);
430 Set<HNode> combinationNodeSetInSimpleGraph =
431 simpleGraph.getCombinationNodeSetByCombineNodeSet(combineSkeletonNodeSet);
432 System.out.println("---combinationNodeSetInSimpleGraph=" + combinationNodeSetInSimpleGraph);
433 Set<HNode> inNodeSetToCNode = simpleGraph.getIncomingNodeSet(cnode);
434 System.out.println("------inNodeSetToCNode=" + inNodeSetToCNode);
435 for (Iterator iterator = combinationNodeSetInSimpleGraph.iterator(); iterator.hasNext();) {
436 HNode nodeBelongToTheSameCombinationNode = (HNode) iterator.next();
437 if (inNodeSetToCNode.contains(nodeBelongToTheSameCombinationNode)) {
438 // the combination node 'cnode' is not the highest location among the same combination node
446 private void expandCombinationNode(Descriptor desc, SSJavaLattice<String> lattice,
447 Set<HNode> visited, Map<TripleItem, String> mapIntermediateLoc, LocationSummary locSummary,
450 // expand the combination node 'outNode'
451 // here we need to expand the corresponding combination location in the lattice
452 HNode combinationNodeInSCGraph = getCombinationNodeInSCGraph(desc, cnode);
454 System.out.println("expandCombinationNode=" + cnode + " cnode in scgraph="
455 + combinationNodeInSCGraph);
457 if (combinationNodeInSCGraph == null) {
461 HierarchyGraph simpleGraph = infer.getSimpleHierarchyGraph(desc);
462 HierarchyGraph scGraph = infer.getSkeletonCombinationHierarchyGraph(desc);
464 Set<HNode> combineSkeletonNodeSet = simpleGraph.getCombineSetByCombinationNode(cnode);
466 // System.out.println("combineSkeletonNodeSet=" + combineSkeletonNodeSet);
468 Set<HNode> combinationNodeSet =
469 simpleGraph.getCombinationNodeSetByCombineNodeSet(combineSkeletonNodeSet);
471 // System.out.println("combinationNodeSet=" + combinationNodeSet);
474 // Set<HNode> endNodeSetFromSimpleGraph =
475 // simpleGraph.getDirectlyReachableSkeletonCombinationNodeFrom(cnode, combinationNodeSet);
476 // System.out.println("-endNodeSetFromSimpleGraph=" + endNodeSetFromSimpleGraph);
477 // Set<HNode> endCombNodeSet = new HashSet<HNode>();
478 // for (Iterator iterator3 = endNodeSetFromSimpleGraph.iterator(); iterator3.hasNext();) {
479 // HNode endNode = (HNode) iterator3.next();
480 // endCombNodeSet.add(getCombinationNodeInSCGraph(desc, endNode));
483 Set<HNode> endCombNodeSet = scGraph.getOutgoingNodeSet(combinationNodeInSCGraph);
486 // follows the straight line up to another skeleton/combination node
487 if (endCombNodeSet.size() > 0) {
488 // System.out.println("---endCombNodeSet=" + endCombNodeSet);
490 removeTransitivelyReachToNode(desc, combinationNodeInSCGraph, endCombNodeSet);
492 recurDFS(desc, lattice, combinationNodeInSCGraph, endCombNodeSet, visited,
493 mapIntermediateLoc, 1, locSummary, cnode);
495 endCombNodeSet.add(LocationInference.BOTTOMHNODE);
496 // System.out.println("---endCombNodeSet is zero");
497 // System.out.println("---endNodeSetFromSimpleGraph=" + endNodeSetFromSimpleGraph);
498 // System.out.println("---incoming=" + simpleGraph.getIncomingNodeSet(cnode));
499 recurDFS(desc, lattice, combinationNodeInSCGraph, endCombNodeSet, visited,
500 mapIntermediateLoc, 1, locSummary, cnode);
506 private Set<HNode> removeTransitivelyReachToNode(Descriptor desc, HNode startNode,
507 Set<HNode> endNodeSet) {
509 // if an end node is not directly connected to the start node in the SC graph
510 // replace it with a directly connected one which transitively reaches to it.
512 HierarchyGraph scGraph = infer.getSkeletonCombinationHierarchyGraph(desc);
514 Set<HNode> newEndNodeSet = new HashSet<HNode>();
515 for (Iterator iterator = endNodeSet.iterator(); iterator.hasNext();) {
516 HNode endNode = (HNode) iterator.next();
517 if (scGraph.isDirectlyConnectedTo(startNode, endNode)) {
518 newEndNodeSet.add(endNode);
521 getDirectlyReachableNodeFromStartNodeReachToEndNode(scGraph, startNode, endNode);
522 // System.out.println("#### old END NODE=" + endNode + " --->" + newEndNode);
523 newEndNodeSet.add(newEndNode);
527 // System.out.println("removeTransitivelyReachToNode=" + endNodeSet + " newSet=" +
530 return newEndNodeSet;
534 private HNode getDirectlyReachableSCNodeFromEndNode(HierarchyGraph scGraph, HNode startNode,
535 Set<HNode> endNodeSet) {
537 // System.out.println("getDirectlyReachableSCNodeFromEndNode start=" + startNode +
540 Set<HNode> newStartNodeSet = new HashSet<HNode>();
542 for (Iterator iterator = endNodeSet.iterator(); iterator.hasNext();) {
543 HNode endNode = (HNode) iterator.next();
544 Set<HNode> connectedToEndNodeSet = scGraph.getIncomingNodeSet(endNode);
546 for (Iterator iterator2 = connectedToEndNodeSet.iterator(); iterator2.hasNext();) {
547 HNode curNode = (HNode) iterator2.next();
548 if (recurConnectedFromStartNode(scGraph, startNode, curNode, new HashSet<HNode>())) {
549 newStartNodeSet.add(curNode);
554 // System.out.println("newStartNodeSet=" + newStartNodeSet);
556 if (newStartNodeSet.size() == 0) {
557 newStartNodeSet.add(startNode);
560 return newStartNodeSet.iterator().next();
563 private boolean recurConnectedFromStartNode(HierarchyGraph scGraph, HNode startNode,
564 HNode curNode, Set<HNode> visited) {
565 // return true if curNode is transitively connected from the startNode
567 boolean isConnected = false;
568 Set<HNode> inNodeSet = scGraph.getIncomingNodeSet(curNode);
569 for (Iterator iterator = inNodeSet.iterator(); iterator.hasNext();) {
570 HNode in = (HNode) iterator.next();
571 if (in.equals(startNode)) {
575 isConnected |= recurConnectedFromStartNode(scGraph, startNode, in, visited);
582 private HNode getDirectlyReachableNodeFromStartNodeReachToEndNode(HierarchyGraph scGraph,
583 HNode startNode, HNode endNode) {
584 // System.out.println("getDirectlyReachableNodeFromStartNodeReachToEndNode start=" + startNode
585 // + " end=" + endNode);
586 Set<HNode> connected = new HashSet<HNode>();
587 recurDirectlyReachableNodeFromStartNodeReachToEndNode(scGraph, startNode, endNode, connected);
588 if (connected.size() == 0) {
589 connected.add(endNode);
591 // System.out.println("connected=" + connected);
593 return connected.iterator().next();
596 private void recurDirectlyReachableNodeFromStartNodeReachToEndNode(HierarchyGraph scGraph,
597 HNode startNode, HNode curNode, Set<HNode> connected) {
599 Set<HNode> inNodeSet = scGraph.getIncomingNodeSet(curNode);
600 for (Iterator iterator = inNodeSet.iterator(); iterator.hasNext();) {
601 HNode inNode = (HNode) iterator.next();
602 if (inNode.equals(startNode)) {
603 connected.add(curNode);
605 recurDirectlyReachableNodeFromStartNodeReachToEndNode(scGraph, startNode, inNode, connected);
611 private void recurDFSNormalNode(Descriptor desc, SSJavaLattice<String> lattice, HNode startNode,
612 Set<HNode> endNodeSet, Set<HNode> visited, Map<TripleItem, String> mapIntermediateLoc,
613 int idx, LocationSummary locSummary, HNode curNode) {
615 TripleItem item = new TripleItem(startNode, endNodeSet, idx);
616 if (!mapIntermediateLoc.containsKey(item)) {
617 // need to create a new intermediate location in the lattice
618 String newLocName = "ILOC" + (LocationInference.locSeed++);
621 above = startNode.getName();
623 int prevIdx = idx - 1;
624 TripleItem prevItem = new TripleItem(startNode, endNodeSet, prevIdx);
625 above = mapIntermediateLoc.get(prevItem);
628 Set<String> belowSet = new HashSet<String>();
629 for (Iterator iterator = endNodeSet.iterator(); iterator.hasNext();) {
630 HNode endNode = (HNode) iterator.next();
632 if (locSummary.getMapHNodeNameToLocationName().containsKey(endNode.getName())) {
633 locName = locSummary.getLocationName(endNode.getName());
635 locName = endNode.getName();
637 belowSet.add(locName);
639 lattice.insertNewLocationBetween(above, belowSet, newLocName);
641 mapIntermediateLoc.put(item, newLocName);
644 String locName = mapIntermediateLoc.get(item);
645 HierarchyGraph simpleHierarchyGraph = infer.getSimpleHierarchyGraph(desc);
647 if (curNode.isSharedNode()) {
648 // if the current node is shared location, add a shared location to the lattice later
649 System.out.println("###SHARED ITEM=" + item);
650 mapSharedNodeToTripleItem.put(curNode, item);
652 Set<Descriptor> descSet = simpleHierarchyGraph.getDescSetOfNode(curNode);
653 for (Iterator iterator = descSet.iterator(); iterator.hasNext();) {
654 Descriptor d = (Descriptor) iterator.next();
655 locSummary.addMapHNodeNameToLocationName(d.getSymbol(), locName);
657 locSummary.addMapHNodeNameToLocationName(curNode.getName(), locName);
660 System.out.println("-TripleItem normal=" + item);
661 System.out.println("-curNode=" + curNode.getName() + " S=" + curNode.isSharedNode()
662 + " locName=" + locName + " isC=" + curNode.isCombinationNode());
664 Set<HNode> outSet = simpleHierarchyGraph.getOutgoingNodeSet(curNode);
665 for (Iterator iterator2 = outSet.iterator(); iterator2.hasNext();) {
666 HNode outNode = (HNode) iterator2.next();
668 Set<HNode> incomingHNodeSetToOutNode = simpleHierarchyGraph.getIncomingNodeSet(outNode);
669 System.out.println("outNode=" + outNode);
670 System.out.println("---incomingHNodeSetToOutNode=" + incomingHNodeSetToOutNode);
672 if (!outNode.isSkeleton() && !outNode.isCombinationNode() && !visited.contains(outNode)) {
673 Pair<HNode, HNode> pair = new Pair(startNode, outNode);
674 if (visited.containsAll(simpleHierarchyGraph.getIncomingNodeSet(outNode))) {
675 visited.add(outNode);
676 int newidx = getCurrentHighestIndex(pair, idx + 1);
677 // int newidx = getCurrentHighestIndex(outNode, idx + 1);
678 recurDFSNormalNode(desc, lattice, startNode, endNodeSet, visited, mapIntermediateLoc,
679 newidx, locSummary, outNode);
680 // recurDFSNormalNode(desc, lattice, startNode, endNodeSet, visited, mapIntermediateLoc,
681 // idx + 1, locSummary, outNode);
683 updateHighestIndex(pair, idx + 1);
684 // updateHighestIndex(outNode, idx + 1);
685 System.out.println("NOT RECUR");
687 } else if (!outNode.isSkeleton() && outNode.isCombinationNode() && !visited.contains(outNode)) {
688 if (needToExpandCombinationNode(desc, outNode)) {
689 System.out.println("NEED TO");
690 expandCombinationNode(desc, lattice, visited, mapIntermediateLoc, locSummary, outNode);
692 System.out.println("NOT NEED TO");
700 private void recurDFS(Descriptor desc, SSJavaLattice<String> lattice,
701 HNode combinationNodeInSCGraph, Set<HNode> endNodeSet, Set<HNode> visited,
702 Map<TripleItem, String> mapIntermediateLoc, int idx, LocationSummary locSummary, HNode curNode) {
704 TripleItem item = new TripleItem(combinationNodeInSCGraph, endNodeSet, idx);
706 if (!mapIntermediateLoc.containsKey(item)) {
707 // need to create a new intermediate location in the lattice
710 String newLocName = combinationNodeInSCGraph.getName();
711 mapIntermediateLoc.put(item, newLocName);
713 String newLocName = "ILOC" + (LocationInference.locSeed++);
714 int prevIdx = idx - 1;
715 TripleItem prevItem = new TripleItem(combinationNodeInSCGraph, endNodeSet, prevIdx);
716 above = mapIntermediateLoc.get(prevItem);
718 Set<String> belowSet = new HashSet<String>();
719 for (Iterator iterator = endNodeSet.iterator(); iterator.hasNext();) {
720 HNode endNode = (HNode) iterator.next();
721 belowSet.add(endNode.getName());
723 lattice.insertNewLocationBetween(above, belowSet, newLocName);
724 mapIntermediateLoc.put(item, newLocName);
730 // Do we need to skip the combination node and assign a shared location to the next node?
731 // if (idx == 1 && curNode.isSharedNode()) {
732 // System.out.println("THE FIRST COMBINATION NODE EXPANSION IS SHARED!");
733 // recurDFS(desc, lattice, combinationNodeInSCGraph, endNodeSet, visited, mapIntermediateLoc,
734 // idx + 1, locSummary, curNode);
738 HierarchyGraph simpleHierarchyGraph = infer.getSimpleHierarchyGraph(desc);
739 String locName = mapIntermediateLoc.get(item);
740 if (curNode.isSharedNode()) {
741 // if the current node is shared location, add a shared location to the lattice later
742 System.out.println("###SHARED ITEM=" + item);
743 mapSharedNodeToTripleItem.put(curNode, item);
745 Set<Descriptor> descSet = simpleHierarchyGraph.getDescSetOfNode(curNode);
746 for (Iterator iterator = descSet.iterator(); iterator.hasNext();) {
747 Descriptor d = (Descriptor) iterator.next();
748 locSummary.addMapHNodeNameToLocationName(d.getSymbol(), locName);
750 locSummary.addMapHNodeNameToLocationName(curNode.getName(), locName);
753 System.out.println("-TripleItem=" + item);
754 System.out.println("-curNode=" + curNode.getName() + " S=" + curNode.isSharedNode()
755 + " locName=" + locName);
757 Set<HNode> outSet = simpleHierarchyGraph.getOutgoingNodeSet(curNode);
758 for (Iterator iterator2 = outSet.iterator(); iterator2.hasNext();) {
759 HNode outNode = (HNode) iterator2.next();
760 System.out.println("---recurDFS outNode=" + outNode);
761 System.out.println("---cur combinationNodeInSCGraph=" + combinationNodeInSCGraph);
762 System.out.println("---outNode combinationNodeInSCGraph="
763 + getCombinationNodeInSCGraph(desc, outNode));
765 if (!outNode.isSkeleton() && !visited.contains(outNode)) {
766 if (outNode.isCombinationNode()) {
768 Set<HNode> combineSkeletonNodeSet =
769 simpleHierarchyGraph.getCombineSetByCombinationNode(outNode);
770 Set<HNode> incomingHNodeSetToOutNode = simpleHierarchyGraph.getIncomingNodeSet(outNode);
771 // extract nodes belong to the same combine node
772 Set<HNode> incomingCombinedHNodeSet = new HashSet<HNode>();
773 for (Iterator iterator = incomingHNodeSetToOutNode.iterator(); iterator.hasNext();) {
774 HNode inNode = (HNode) iterator.next();
775 if (combineSkeletonNodeSet.contains(inNode)) {
776 incomingCombinedHNodeSet.add(inNode);
779 System.out.println("-----incomingCombinedHNodeSet=" + incomingCombinedHNodeSet);
781 // check whether the next combination node is different from the current node
782 if (combinationNodeInSCGraph.equals(getCombinationNodeInSCGraph(desc, outNode))) {
783 Pair<HNode, HNode> pair = new Pair(combinationNodeInSCGraph, outNode);
784 if (visited.containsAll(incomingCombinedHNodeSet)) {
785 visited.add(outNode);
786 System.out.println("-------curIdx=" + (idx + 1));
788 int newIdx = getCurrentHighestIndex(pair, idx + 1);
789 // int newIdx = getCurrentHighestIndex(outNode, idx + 1);
790 System.out.println("-------newIdx=" + newIdx);
791 recurDFS(desc, lattice, combinationNodeInSCGraph, endNodeSet, visited,
792 mapIntermediateLoc, newIdx, locSummary, outNode);
793 // recurDFS(desc, lattice, combinationNodeInSCGraph, endNodeSet, visited,
794 // mapIntermediateLoc, idx + 1, locSummary, outNode);
796 updateHighestIndex(pair, idx + 1);
797 // updateHighestIndex(outNode, idx + 1);
798 System.out.println("-----NOT RECUR!");
801 if (needToExpandCombinationNode(desc, outNode)) {
802 System.out.println("NEED TO");
803 expandCombinationNode(desc, lattice, visited, mapIntermediateLoc, locSummary, outNode);
805 System.out.println("NOT NEED TO");
817 private int getCurrentHighestIndex(Pair<HNode, HNode> pair, int curIdx) {
818 int recordedIdx = getCurrentHighestIndex(pair);
819 if (recordedIdx > curIdx) {
826 private int getCurrentHighestIndex(HNode node, int curIdx) {
827 int recordedIdx = getCurrentHighestIndex(node);
828 if (recordedIdx > curIdx) {
835 private int getCurrentHighestIndex(Pair<HNode, HNode> pair) {
836 if (!mapItemToHighestIndex.containsKey(pair)) {
837 mapItemToHighestIndex.put(pair, new Integer(-1));
839 return mapItemToHighestIndex.get(pair).intValue();
842 private void updateHighestIndex(Pair<HNode, HNode> pair, int idx) {
843 if (idx > getCurrentHighestIndex(pair)) {
844 mapItemToHighestIndex.put(pair, new Integer(idx));
848 private int getCurrentHighestIndex(HNode node) {
849 if (!mapHNodeToHighestIndex.containsKey(node)) {
850 mapHNodeToHighestIndex.put(node, new Integer(-1));
852 return mapHNodeToHighestIndex.get(node).intValue();
855 private void updateHighestIndex(HNode node, int idx) {
856 if (idx > getCurrentHighestIndex(node)) {
857 mapHNodeToHighestIndex.put(node, new Integer(idx));
861 private String generateElementName(BasisSet basisSet, HierarchyGraph inputGraph,
862 Map<Set<Integer>, String> mapF2LocName, Set<Integer> F) {
864 if (mapF2LocName.containsKey(F)) {
865 return mapF2LocName.get(F);
868 HNode node = basisSet.getHNode(F);
870 mapF2LocName.put(F, node.getName());
871 return node.getName();
873 if (inputGraph.BASISTOPELEMENT.equals(F)) {
874 return SSJavaAnalysis.BOTTOM;
876 String str = "LOC" + (LocationInference.locSeed++);
877 mapF2LocName.put(F, str);
883 private void resetCount(Map<Set<Integer>, Integer> mapFtoCount, Family family) {
884 for (Iterator<Set<Integer>> iter = family.FIterator(); iter.hasNext();) {
885 Set<Integer> F = iter.next();
886 mapFtoCount.put(F, 0);
890 private Map<Set<Integer>, Set<Set<Integer>>> coveringGraph(BasisSet basisSet, Family family) {
892 Map<Set<Integer>, Integer> mapFtoCount = new HashMap<Set<Integer>, Integer>();
893 Map<Set<Integer>, Set<Set<Integer>>> mapImSucc = new HashMap<Set<Integer>, Set<Set<Integer>>>();
895 // initialize COUNT(F) to 0 for all elements of the family
896 resetCount(mapFtoCount, family);
898 for (Iterator<Set<Integer>> iter = family.FIterator(); iter.hasNext();) {
899 Set<Integer> F = iter.next();
900 Set<HNode> gammaF = family.getGamma(F);
902 Set<HNode> curHNodeSet = basisSet.getHNodeSet();
903 curHNodeSet.removeAll(gammaF);
904 Set<Set<Integer>> Bset = basisSet.getBasisSetByHNodeSet(curHNodeSet);
906 for (Iterator iterator = Bset.iterator(); iterator.hasNext();) {
907 Set<Integer> B = (Set<Integer>) iterator.next();
909 Set<Integer> Fprime = new HashSet<Integer>();
914 mapFtoCount.put(Fprime, mapFtoCount.get(Fprime) + 1);
916 // if |gamma(F')|==COUNT(F') + |gamma(F)|
917 int numGammaFprime = family.getGamma(Fprime).size();
918 int countFprime = mapFtoCount.get(Fprime);
919 int numGammaF = family.getGamma(F).size();
920 if (numGammaFprime == (countFprime + numGammaF)) {
921 // ImSucc(F)=IMSucc(F) union F'
922 addImSucc(mapImSucc, F, Fprime);
926 resetCount(mapFtoCount, family);
929 // System.out.println("mapImSucc=" + mapImSucc);
934 private Set<Set<Integer>> getImSucc(Map<Set<Integer>, Set<Set<Integer>>> mapImSucc, Set<Integer> F) {
935 if (!mapImSucc.containsKey(F)) {
936 mapImSucc.put(F, new HashSet<Set<Integer>>());
938 return mapImSucc.get(F);
941 private void addImSucc(Map<Set<Integer>, Set<Set<Integer>>> mapImSucc, Set<Integer> F,
942 Set<Integer> Fprime) {
944 if (!mapImSucc.containsKey(F)) {
945 mapImSucc.put(F, new HashSet<Set<Integer>>());
948 mapImSucc.get(F).add(Fprime);
952 private Family generateFamily(BasisSet basisSet) {
954 Family family = new Family();
956 for (Iterator<Set<Integer>> iterator = basisSet.basisIterator(); iterator.hasNext();) {
957 Set<Integer> B = iterator.next();
959 Set<Pair<Set<Integer>, Set<HNode>>> tobeadded = new HashSet<Pair<Set<Integer>, Set<HNode>>>();
961 for (Iterator<Set<Integer>> iterator2 = family.FIterator(); iterator2.hasNext();) {
962 Set<Integer> F = iterator2.next();
964 Set<Integer> Fprime = new HashSet<Integer>();
968 Set<HNode> gammaFPrimeSet = new HashSet<HNode>();
969 gammaFPrimeSet.addAll(family.getGamma(F));
970 gammaFPrimeSet.add(basisSet.getHNode(B));
972 if (!family.containsF(Fprime)) {
973 Pair<Set<Integer>, Set<HNode>> pair =
974 new Pair<Set<Integer>, Set<HNode>>(Fprime, gammaFPrimeSet);
977 family.updateGammaF(Fprime, gammaFPrimeSet);
981 for (Iterator<Pair<Set<Integer>, Set<HNode>>> iterator2 = tobeadded.iterator(); iterator2
983 Pair<Set<Integer>, Set<HNode>> pair = iterator2.next();
984 family.addFElement(pair.getFirst());
985 family.updateGammaF(pair.getFirst(), pair.getSecond());
992 private void debug_print(HierarchyGraph inputGraph) {
993 System.out.println("\nBuild Lattice:" + inputGraph.getName());
994 System.out.println("Node2Index:\n" + inputGraph.getMapHNodeToUniqueIndex());
995 System.out.println("Node2Basis:\n" + inputGraph.getMapHNodeToBasis());
1004 public Identifier(HNode n, int i) {
1009 public int hashCode() {
1010 return node.hashCode() + idx;
1013 public boolean equals(Object obj) {
1015 if (obj instanceof Identifier) {
1016 Identifier in = (Identifier) obj;
1017 if (node.equals(in.node) && idx == in.idx) {
1028 public HNode higherNode;
1029 public Set<HNode> lowerNodeSet;
1031 public boolean isShared;
1033 public TripleItem(HNode h, Set<HNode> l, int i) {
1040 public void setShared(boolean in) {
1044 public boolean isShared() {
1048 public int hashCode() {
1051 if (higherNode != null) {
1052 h = higherNode.hashCode();
1059 return h + lowerNodeSet.hashCode() + idx;
1062 public boolean equals(Object obj) {
1064 if (obj instanceof TripleItem) {
1065 TripleItem in = (TripleItem) obj;
1066 if ((higherNode == null || (higherNode != null && higherNode.equals(in.higherNode)))
1067 && lowerNodeSet.equals(in.lowerNodeSet) && idx == in.idx && isShared == in.isShared()) {
1075 public String toString() {
1076 String rtr = higherNode + "-" + idx + "->" + lowerNodeSet;