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 private Map<SSJavaLattice<String>, Set<String>> mapLatticeToLocalLocSet;
27 public BuildLattice(LocationInference infer) {
29 this.mapSharedNodeToTripleItem = new HashMap<HNode, TripleItem>();
30 this.mapHNodeToHighestIndex = new HashMap<HNode, Integer>();
31 this.mapItemToHighestIndex = new HashMap<Pair<HNode, HNode>, Integer>();
32 this.mapDescToIntermediateLocMap = new HashMap<Descriptor, Map<TripleItem, String>>();
33 this.mapLatticeToLocalLocSet = new HashMap<SSJavaLattice<String>, Set<String>>();
36 public SSJavaLattice<String> buildLattice(Descriptor desc) {
38 HierarchyGraph inputGraph = infer.getSkeletonCombinationHierarchyGraph(desc);
39 LocationSummary locSummary = infer.getLocationSummary(desc);
41 HierarchyGraph naiveGraph = infer.getSimpleHierarchyGraph(desc);
43 // I don't think we need to keep the below if statement anymore
44 // because hierarchy graph does not have any composite location
45 Set<HNode> nodeSetWithCompositeLocation = new HashSet<HNode>();
46 if (desc instanceof MethodDescriptor) {
47 FlowGraph flowGraph = infer.getFlowGraph((MethodDescriptor) desc);
49 for (Iterator iterator = inputGraph.getNodeSet().iterator(); iterator.hasNext();) {
50 HNode hnode = (HNode) iterator.next();
51 Descriptor hnodeDesc = hnode.getDescriptor();
52 if (hnodeDesc != null) {
53 NTuple<Descriptor> descTuple = new NTuple<Descriptor>();
54 descTuple.add(hnodeDesc);
56 if (flowGraph.contains(descTuple)) {
57 FlowNode flowNode = flowGraph.getFlowNode(descTuple);
58 if (flowNode.getCompositeLocation() != null) {
59 nodeSetWithCompositeLocation.add(hnode);
68 // /////////////////////////////////////////////////////////////////////////////////////
69 // lattice generation for the native approach
71 if (infer.state.SSJAVA_INFER_NAIVE_WRITEDOTS) {
72 BasisSet naiveBasisSet = naiveGraph.computeBasisSet(nodeSetWithCompositeLocation);
74 Family naiveFamily = generateFamily(naiveBasisSet);
75 Map<Set<Integer>, Set<Set<Integer>>> naive_mapImSucc =
76 coveringGraph(naiveBasisSet, naiveFamily);
78 SSJavaLattice<String> naive_lattice =
79 buildLattice(desc, naiveBasisSet, naiveGraph, null, naive_mapImSucc);
80 LocationInference.numLocationsNaive += naive_lattice.getKeySet().size();
81 infer.addNaiveLattice(desc, naive_lattice);
84 // /////////////////////////////////////////////////////////////////////////////////////
86 // lattice generation for the proposed approach
87 BasisSet basisSet = inputGraph.computeBasisSet(nodeSetWithCompositeLocation);
88 // debug_print(inputGraph);
90 Family family = generateFamily(basisSet);
91 Map<Set<Integer>, Set<Set<Integer>>> mapImSucc = coveringGraph(basisSet, family);
93 SSJavaLattice<String> lattice = buildLattice(desc, basisSet, inputGraph, locSummary, mapImSucc);
98 public void setIntermediateLocMap(Descriptor desc, Map<TripleItem, String> map) {
99 mapDescToIntermediateLocMap.put(desc, map);
102 public Map<TripleItem, String> getIntermediateLocMap(Descriptor desc) {
103 if (!mapDescToIntermediateLocMap.containsKey(desc)) {
104 mapDescToIntermediateLocMap.put(desc, new HashMap<TripleItem, String>());
106 return mapDescToIntermediateLocMap.get(desc);
109 private Descriptor getParent(Descriptor desc) {
110 if (desc instanceof MethodDescriptor) {
111 MethodDescriptor md = (MethodDescriptor) desc;
112 ClassDescriptor cd = md.getClassDesc();
113 return infer.getParentMethodDesc(cd, md);
115 return ((ClassDescriptor) desc).getSuperDesc();
119 private SSJavaLattice<String> buildLattice(Descriptor desc, BasisSet basisSet,
120 HierarchyGraph inputGraph, LocationSummary locSummary,
121 Map<Set<Integer>, Set<Set<Integer>>> mapImSucc) {
123 System.out.println("\nBuild Lattice:" + inputGraph.getName());
125 SSJavaLattice<String> lattice =
126 new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM);
128 Map<Set<Integer>, String> mapFToLocName = new HashMap<Set<Integer>, String>();
130 Set<Set<Integer>> keySet = mapImSucc.keySet();
131 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
132 Set<Integer> higher = (Set<Integer>) iterator.next();
134 String higherName = generateElementName(basisSet, inputGraph, mapFToLocName, higher);
136 HNode higherNode = inputGraph.getHNode(higherName);
138 if (higherNode == null) {
139 NameDescriptor d = new NameDescriptor(higherName);
140 higherNode = inputGraph.getHNode(d);
141 higherNode.setSkeleton(true);
144 if (higherNode != null && higherNode.isSharedNode()) {
145 lattice.addSharedLoc(higherName);
147 Set<Descriptor> descSet = inputGraph.getDescSetOfNode(higherNode);
148 // System.out.println("higherName=" + higherName + " higherNode=" + higherNode + " descSet="
151 if (locSummary != null) {
152 for (Iterator iterator2 = descSet.iterator(); iterator2.hasNext();) {
153 Descriptor d = (Descriptor) iterator2.next();
154 locSummary.addMapHNodeNameToLocationName(d.getSymbol(), higherName);
158 // locSummary.addMapHNodeNameToLocationName(higherName, higherName);
160 Set<Set<Integer>> lowerSet = mapImSucc.get(higher);
161 for (Iterator iterator2 = lowerSet.iterator(); iterator2.hasNext();) {
162 Set<Integer> lower = (Set<Integer>) iterator2.next();
164 String lowerName = generateElementName(basisSet, inputGraph, mapFToLocName, lower);
165 HNode lowerNode = inputGraph.getHNode(lowerName);
167 if (lowerNode == null && !lowerName.equals(SSJavaAnalysis.BOTTOM)) {
168 NameDescriptor d = new NameDescriptor(lowerName);
169 lowerNode = inputGraph.getHNode(d);
170 lowerNode.setSkeleton(true);
173 if (lowerNode != null && !inputGraph.isDirectlyConnectedTo(higherNode, lowerNode)) {
174 inputGraph.addEdge(higherNode, lowerNode);
177 if (lowerNode != null && lowerNode.isSharedNode()) {
178 lattice.addSharedLoc(lowerName);
181 Set<Descriptor> lowerDescSet = inputGraph.getDescSetOfNode(lowerNode);
182 // System.out.println("lowerName=" + lowerName + " lowerNode=" + lowerNode + " descSet="
184 if (locSummary != null) {
185 for (Iterator iterator3 = lowerDescSet.iterator(); iterator3.hasNext();) {
186 Descriptor d = (Descriptor) iterator3.next();
187 locSummary.addMapHNodeNameToLocationName(d.getSymbol(), lowerName);
190 // locSummary.addMapHNodeNameToLocationName(lowerName, lowerName);
192 if (higher.size() == 0) {
194 lattice.put(lowerName);
196 lattice.addRelationHigherToLower(higherName, lowerName);
203 inputGraph.removeRedundantEdges();
207 public HNode getCombinationNodeInSCGraph(Descriptor desc, HNode nodeFromSimpleGraph) {
209 HierarchyGraph scGraph = infer.getSkeletonCombinationHierarchyGraph(desc);
211 if (nodeFromSimpleGraph.isSkeleton()) {
212 return scGraph.getCurrentHNode(nodeFromSimpleGraph);
215 Set<HNode> combineSkeletonNodeSet =
216 infer.getSimpleHierarchyGraph(desc).getCombineSetByCombinationNode(nodeFromSimpleGraph);
217 HNode combinationNodeInSCGraph =
218 infer.getSkeletonCombinationHierarchyGraph(desc).getMapCombineNodeSetToCombinationNode()
219 .get(combineSkeletonNodeSet);
221 // Set<HNode> combineSkeletonNodeSet =
222 // infer.getSimpleHierarchyGraph(desc).getCombineSetByCombinationNode(simpleGraphNode);
223 // HNode combinationNodeInSCGraph =
224 // infer.getSkeletonCombinationHierarchyGraph(desc).getCombinationNode(combineSkeletonNodeSet);
225 return combinationNodeInSCGraph;
228 public SSJavaLattice<String> insertIntermediateNodesToStraightLine(Descriptor desc,
229 SSJavaLattice<String> skeletonLattice) {
231 SSJavaLattice<String> lattice = skeletonLattice.clone();
232 LocationSummary locSummary = infer.getLocationSummary(desc);
234 Descriptor parentDesc = getParent(desc);
235 if (parentDesc != null) {
236 SSJavaLattice<String> parentLattice = infer.getLattice(parentDesc);
238 Map<String, Set<String>> parentMap = parentLattice.getTable();
239 Set<String> parentKeySet = parentMap.keySet();
240 for (Iterator iterator = parentKeySet.iterator(); iterator.hasNext();) {
241 String parentKey = (String) iterator.next();
242 Set<String> parentValueSet = parentMap.get(parentKey);
243 for (Iterator iterator2 = parentValueSet.iterator(); iterator2.hasNext();) {
244 String value = (String) iterator2.next();
245 lattice.put(parentKey, value);
249 Set<String> parentSharedLocSet = parentLattice.getSharedLocSet();
250 for (Iterator iterator = parentSharedLocSet.iterator(); iterator.hasNext();) {
251 String parentSharedLoc = (String) iterator.next();
252 lattice.addSharedLoc(parentSharedLoc);
256 HierarchyGraph hierarchyGraph = infer.getSimpleHierarchyGraph(desc);
257 HierarchyGraph scGraph = infer.getSkeletonCombinationHierarchyGraph(desc);
259 Set<HNode> hierarchyGraphNodeSet = hierarchyGraph.getNodeSet();
260 for (Iterator iterator = hierarchyGraphNodeSet.iterator(); iterator.hasNext();) {
261 HNode hNode = (HNode) iterator.next();
262 if (!hNode.isSkeleton()) {
263 // here we need to insert an intermediate node for the hNode
264 System.out.println("\n#local node=" + hNode);
266 // 1) find the lowest node m in the lattice that is above hnode in the lattice
267 // 2) count the number of non-shared nodes d between the hnode and the node m
268 // int numNonSharedNodes;
272 Set<HNode> combineSkeletonNodeSet = null;
273 if (hNode.isDirectCombinationNode()) {
274 // this node itself is the lowest node m. it is the first node of the chain
275 Set<HNode> combineSet = hierarchyGraph.getCombineSetByCombinationNode(hNode);
277 System.out.println(" # direct combine node::combineSkeletonNodeSet=" + combineSet);
279 SCNode = scGraph.getCombinationNode(combineSet);
280 // numNonSharedNodes = -1;
284 Set<HNode> aboveSet = new HashSet<HNode>();
285 if (hNode.isCombinationNode()) {
286 // the current node is a combination node
287 combineSkeletonNodeSet = hierarchyGraph.getCombineSetByCombinationNode(hNode);
288 System.out.println(" combineSkeletonNodeSet=" + combineSkeletonNodeSet
289 + " combinationNode=" + scGraph.getCombinationNode(combineSkeletonNodeSet));
291 scGraph.getCombinationNode(combineSkeletonNodeSet);
293 System.out.println(" firstnodeOfSimpleGraph="
294 + hierarchyGraph.getFirstNodeOfCombinationNodeChainSet(combineSkeletonNodeSet));
295 aboveSet.addAll(hierarchyGraph
296 .getFirstNodeOfCombinationNodeChainSet(combineSkeletonNodeSet));
298 SCNode = scGraph.getCombinationNode(combineSkeletonNodeSet);
301 // the current node is not a combination node
302 // there is only one parent node which should be skeleton node.
304 System.out.println(" hierarchyGraph.getSkeleteNodeSetReachTo(" + hNode + ")="
305 + hierarchyGraph.getSkeleteNodeSetReachTo(hNode));
306 aboveSet.addAll(hierarchyGraph.getSkeleteNodeSetReachTo(hNode));
307 System.out.println(" aboveset of " + hNode + "=" + aboveSet);
308 // assert aboveSet.size() == 1;
309 SCNode = aboveSet.iterator().next();
312 // update above set w.r.t the hierarchy graph with SC nodes
313 // because the skeleton nodes in the original hierarchy graph may be merged to a new node
314 Set<HNode> endSet = new HashSet<HNode>();
315 for (Iterator iterator2 = aboveSet.iterator(); iterator2.hasNext();) {
316 HNode aboveNode = (HNode) iterator2.next();
317 endSet.add(hierarchyGraph.getCurrentHNode(aboveNode));
320 dist = hierarchyGraph.computeDistance(hNode, endSet, combineSkeletonNodeSet);
321 System.out.println("##### " + hNode + "::dist=" + dist);
323 // numNonSharedNodes = hierarchyGraph.countNonSharedNode(hNode, endSet);
325 System.out.println(" COUNT-RESULT::node=" + hNode + " above=" + endSet + " distance="
326 + dist + " SCNode=" + SCNode);
329 // 3) convert the node m into a chain of nodes with the last node in the chain having m’s
331 Set<HNode> outgoingSCNodeSet = scGraph.getOutgoingNodeSet(SCNode);
332 System.out.println(" outgoing scnode set from " + SCNode + "=" + outgoingSCNodeSet);
334 // convert hnodes to location names
335 String startLocName = locSummary.getLocationName(SCNode.getName());
336 Set<String> outgoingLocNameSet = new HashSet<String>();
337 for (Iterator iterator2 = outgoingSCNodeSet.iterator(); iterator2.hasNext();) {
338 HNode outSCNode = (HNode) iterator2.next();
339 String locName = locSummary.getLocationName(outSCNode.getName());
340 if (!locName.equals(outSCNode.getName())) {
341 System.out.println(" outSCNode=" + outSCNode + " -> locName="
344 outgoingLocNameSet.add(locName);
347 if (outgoingLocNameSet.isEmpty()) {
348 outgoingLocNameSet.add(lattice.getBottomItem());
351 // 4) If hnode is not a shared location, check if there already exists a local variable
352 // node that has distance d below m along this chain. If such a node
353 // does not exist, insert it.
355 getNewLocation(lattice, startLocName, outgoingLocNameSet, dist, hNode.isSharedNode());
356 System.out.println(" ###hNode=" + hNode + "---->locName=" + locName);
357 locSummary.addMapHNodeNameToLocationName(hNode.getName(), locName);
365 private void addLocalLocation(SSJavaLattice<String> lattice, String localLoc) {
366 if (!mapLatticeToLocalLocSet.containsKey(lattice)) {
367 mapLatticeToLocalLocSet.put(lattice, new HashSet<String>());
369 mapLatticeToLocalLocSet.get(lattice).add(localLoc);
372 private boolean isLocalLocation(SSJavaLattice<String> lattice, String localLoc) {
373 if (mapLatticeToLocalLocSet.containsKey(lattice)) {
374 return mapLatticeToLocalLocSet.get(lattice).contains(localLoc);
379 public String getNewLocation(SSJavaLattice<String> lattice, String start, Set<String> endSet,
380 int dist, boolean isShared) {
381 System.out.println(" #GETNEWLOCATION:: start=" + start + " endSet=" + endSet + " dist="
382 + dist + " isShared=" + isShared);
383 return recur_getNewLocation(lattice, start, start, endSet, dist, isShared);
386 private String recur_getNewLocation(SSJavaLattice<String> lattice, String start, String cur,
387 Set<String> endSet, int dist, boolean isShared) {
389 System.out.println(" recur_getNewLocation cur=" + cur + " dist=" + dist);
393 // first check if there already exists a non-shared node at distance d
394 if (!isLocalLocation(lattice, cur) && !start.equals(cur)) {
395 // if not, need to insert a new SHARED local location at this point
396 System.out.println("if not, need to insert a new SHARED local location at this point");
397 String newLocName = "ILOC" + (LocationInference.locSeed++);
398 Set<String> lowerSet = new HashSet<String>();
399 lowerSet.addAll(lattice.get(cur));
400 lattice.insertNewLocationBetween(cur, lowerSet, newLocName);
401 lattice.addSharedLoc(newLocName);
402 addLocalLocation(lattice, newLocName);
405 // if there exists a non-shared node at distance d
406 // then try to add a new SHARED loc at distance d+1
408 Set<String> connectedSet = lattice.get(cur);
409 if (connectedSet == null) {
410 connectedSet = new HashSet<String>();
412 System.out.println("cur=" + cur + " connectedSet=" + connectedSet);
414 // check if there already exists a shared node that has distance d + 1 on the chain
415 boolean needToInsertSharedNode = false;
416 if (connectedSet.equals(endSet)) {
417 needToInsertSharedNode = true;
419 // in this case, the current node is in the middle of the chain
420 assert connectedSet.size() == 1;
421 String below = connectedSet.iterator().next();
422 if (lattice.isSharedLoc(below)) {
425 needToInsertSharedNode = true;
429 if (needToInsertSharedNode) {
430 // no shared local location at d+1, need to insert it!
431 String newSharedLocName = "ILOC" + (LocationInference.locSeed++);
432 Set<String> lowerSet = new HashSet<String>();
433 lowerSet.addAll(connectedSet);
434 lattice.insertNewLocationBetween(cur, lowerSet, newSharedLocName);
435 lattice.addSharedLoc(newSharedLocName);
436 addLocalLocation(lattice, newSharedLocName);
437 System.out.println(" INSERT NEW SHARED LOC=" + newSharedLocName);
438 cur = newSharedLocName;
444 // if the node is not a shared one,
445 // check if the cur node is a shared node
446 if (lattice.isSharedLoc(cur)) {
447 // here, we need to add a new local NONSHARED node above cur
449 String newLocName = "ILOC" + (LocationInference.locSeed++);
450 lattice.insertNewLocationAtOneLevelHigher(cur, newLocName);
451 addLocalLocation(lattice, newLocName);
452 System.out.println(" INSERT NEW LOC=" + newLocName + " ABOVE=" + cur);
455 // if cur is not shared, return it!
461 Set<String> connectedSet = lattice.get(cur);
462 if (connectedSet == null) {
463 connectedSet = new HashSet<String>();
466 System.out.println("cur=" + cur + " connected set=" + connectedSet);
467 if (cur.equals(lattice.getTopItem()) || connectedSet.equals(endSet)) {
468 // if not, need to insert a new local location at this point
469 System.out.println("NEED TO INSERT A NEW LOCAL LOC FOR NEXT connectedSet=" + connectedSet);
470 String newLocName = "ILOC" + (LocationInference.locSeed++);
471 Set<String> lowerSet = new HashSet<String>();
472 lowerSet.addAll(connectedSet);
473 lattice.insertNewLocationBetween(cur, lowerSet, newLocName);
474 addLocalLocation(lattice, newLocName);
477 // in this case, the current node is in the middle of the chain
478 assert connectedSet.size() == 1;
479 cur = connectedSet.iterator().next();
482 if (!lattice.isSharedLoc(cur)) {
485 return recur_getNewLocation(lattice, start, cur, endSet, dist, isShared);
489 private Set<String> getAboveElementSet(SSJavaLattice<String> lattice, String loc) {
491 Set<String> aboveSet = new HashSet<String>();
493 Map<String, Set<String>> latticeMap = lattice.getTable();
494 Set<String> keySet = latticeMap.keySet();
495 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
496 String key = (String) iterator.next();
497 if (latticeMap.get(key).contains(loc)) {
505 private boolean needToExpandCombinationNode(Descriptor desc, HNode cnode) {
507 System.out.println("needToExpandCombinationNode?=" + cnode);
509 HierarchyGraph simpleGraph = infer.getSimpleHierarchyGraph(desc);
510 // HNode combinationNodeInSCGraph = getCombinationNodeInSCGraph(desc, cnode);
511 Set<HNode> combineSkeletonNodeSet = simpleGraph.getCombineSetByCombinationNode(cnode);
512 Set<HNode> combinationNodeSetInSimpleGraph =
513 simpleGraph.getCombinationNodeSetByCombineNodeSet(combineSkeletonNodeSet);
514 System.out.println("---combinationNodeSetInSimpleGraph=" + combinationNodeSetInSimpleGraph);
515 Set<HNode> inNodeSetToCNode = simpleGraph.getIncomingNodeSet(cnode);
516 System.out.println("------inNodeSetToCNode=" + inNodeSetToCNode);
517 for (Iterator iterator = combinationNodeSetInSimpleGraph.iterator(); iterator.hasNext();) {
518 HNode nodeBelongToTheSameCombinationNode = (HNode) iterator.next();
519 if (inNodeSetToCNode.contains(nodeBelongToTheSameCombinationNode)) {
520 // the combination node 'cnode' is not the highest location among the same combination node
528 private void expandCombinationNode(Descriptor desc, SSJavaLattice<String> lattice,
529 Set<HNode> visited, Map<TripleItem, String> mapIntermediateLoc, LocationSummary locSummary,
532 // expand the combination node 'outNode'
533 // here we need to expand the corresponding combination location in the lattice
534 HNode combinationNodeInSCGraph = getCombinationNodeInSCGraph(desc, cnode);
536 System.out.println("expandCombinationNode=" + cnode + " cnode in scgraph="
537 + combinationNodeInSCGraph);
539 if (combinationNodeInSCGraph == null) {
543 HierarchyGraph simpleGraph = infer.getSimpleHierarchyGraph(desc);
544 HierarchyGraph scGraph = infer.getSkeletonCombinationHierarchyGraph(desc);
546 Set<HNode> combineSkeletonNodeSet = simpleGraph.getCombineSetByCombinationNode(cnode);
548 // System.out.println("combineSkeletonNodeSet=" + combineSkeletonNodeSet);
550 Set<HNode> combinationNodeSet =
551 simpleGraph.getCombinationNodeSetByCombineNodeSet(combineSkeletonNodeSet);
553 // System.out.println("combinationNodeSet=" + combinationNodeSet);
556 // Set<HNode> endNodeSetFromSimpleGraph =
557 // simpleGraph.getDirectlyReachableSkeletonCombinationNodeFrom(cnode, combinationNodeSet);
558 // System.out.println("-endNodeSetFromSimpleGraph=" + endNodeSetFromSimpleGraph);
559 // Set<HNode> endCombNodeSet = new HashSet<HNode>();
560 // for (Iterator iterator3 = endNodeSetFromSimpleGraph.iterator(); iterator3.hasNext();) {
561 // HNode endNode = (HNode) iterator3.next();
562 // endCombNodeSet.add(getCombinationNodeInSCGraph(desc, endNode));
565 Set<HNode> endCombNodeSet = scGraph.getOutgoingNodeSet(combinationNodeInSCGraph);
568 // follows the straight line up to another skeleton/combination node
569 if (endCombNodeSet.size() > 0) {
570 // System.out.println("---endCombNodeSet=" + endCombNodeSet);
572 removeTransitivelyReachToNode(desc, combinationNodeInSCGraph, endCombNodeSet);
574 recurDFS(desc, lattice, combinationNodeInSCGraph, endCombNodeSet, visited,
575 mapIntermediateLoc, 1, locSummary, cnode);
577 endCombNodeSet.add(LocationInference.BOTTOMHNODE);
578 // System.out.println("---endCombNodeSet is zero");
579 // System.out.println("---endNodeSetFromSimpleGraph=" + endNodeSetFromSimpleGraph);
580 // System.out.println("---incoming=" + simpleGraph.getIncomingNodeSet(cnode));
581 recurDFS(desc, lattice, combinationNodeInSCGraph, endCombNodeSet, visited,
582 mapIntermediateLoc, 1, locSummary, cnode);
588 private Set<HNode> removeTransitivelyReachToNode(Descriptor desc, HNode startNode,
589 Set<HNode> endNodeSet) {
591 // if an end node is not directly connected to the start node in the SC graph
592 // replace it with a directly connected one which transitively reaches to it.
594 HierarchyGraph scGraph = infer.getSkeletonCombinationHierarchyGraph(desc);
596 Set<HNode> newEndNodeSet = new HashSet<HNode>();
597 for (Iterator iterator = endNodeSet.iterator(); iterator.hasNext();) {
598 HNode endNode = (HNode) iterator.next();
599 if (scGraph.isDirectlyConnectedTo(startNode, endNode)) {
600 newEndNodeSet.add(endNode);
603 getDirectlyReachableNodeFromStartNodeReachToEndNode(scGraph, startNode, endNode);
604 // System.out.println("#### old END NODE=" + endNode + " --->" + newEndNode);
605 newEndNodeSet.add(newEndNode);
609 // System.out.println("removeTransitivelyReachToNode=" + endNodeSet + " newSet=" +
612 return newEndNodeSet;
616 private HNode getDirectlyReachableSCNodeFromEndNode(HierarchyGraph scGraph, HNode startNode,
617 Set<HNode> endNodeSet) {
619 // System.out.println("getDirectlyReachableSCNodeFromEndNode start=" + startNode +
622 Set<HNode> newStartNodeSet = new HashSet<HNode>();
624 for (Iterator iterator = endNodeSet.iterator(); iterator.hasNext();) {
625 HNode endNode = (HNode) iterator.next();
626 Set<HNode> connectedToEndNodeSet = scGraph.getIncomingNodeSet(endNode);
628 for (Iterator iterator2 = connectedToEndNodeSet.iterator(); iterator2.hasNext();) {
629 HNode curNode = (HNode) iterator2.next();
630 if (recurConnectedFromStartNode(scGraph, startNode, curNode, new HashSet<HNode>())) {
631 newStartNodeSet.add(curNode);
636 // System.out.println("newStartNodeSet=" + newStartNodeSet);
638 if (newStartNodeSet.size() == 0) {
639 newStartNodeSet.add(startNode);
642 return newStartNodeSet.iterator().next();
645 private boolean recurConnectedFromStartNode(HierarchyGraph scGraph, HNode startNode,
646 HNode curNode, Set<HNode> visited) {
647 // return true if curNode is transitively connected from the startNode
649 boolean isConnected = false;
650 Set<HNode> inNodeSet = scGraph.getIncomingNodeSet(curNode);
651 for (Iterator iterator = inNodeSet.iterator(); iterator.hasNext();) {
652 HNode in = (HNode) iterator.next();
653 if (in.equals(startNode)) {
657 isConnected |= recurConnectedFromStartNode(scGraph, startNode, in, visited);
664 private HNode getDirectlyReachableNodeFromStartNodeReachToEndNode(HierarchyGraph scGraph,
665 HNode startNode, HNode endNode) {
666 // System.out.println("getDirectlyReachableNodeFromStartNodeReachToEndNode start=" + startNode
667 // + " end=" + endNode);
668 Set<HNode> connected = new HashSet<HNode>();
669 recurDirectlyReachableNodeFromStartNodeReachToEndNode(scGraph, startNode, endNode, connected);
670 if (connected.size() == 0) {
671 connected.add(endNode);
673 // System.out.println("connected=" + connected);
675 return connected.iterator().next();
678 private void recurDirectlyReachableNodeFromStartNodeReachToEndNode(HierarchyGraph scGraph,
679 HNode startNode, HNode curNode, Set<HNode> connected) {
681 Set<HNode> inNodeSet = scGraph.getIncomingNodeSet(curNode);
682 for (Iterator iterator = inNodeSet.iterator(); iterator.hasNext();) {
683 HNode inNode = (HNode) iterator.next();
684 if (inNode.equals(startNode)) {
685 connected.add(curNode);
687 recurDirectlyReachableNodeFromStartNodeReachToEndNode(scGraph, startNode, inNode, connected);
693 private void recurDFSNormalNode(Descriptor desc, SSJavaLattice<String> lattice, HNode startNode,
694 Set<HNode> endNodeSet, Set<HNode> visited, Map<TripleItem, String> mapIntermediateLoc,
695 int idx, LocationSummary locSummary, HNode curNode) {
697 TripleItem item = new TripleItem(startNode, endNodeSet, idx);
698 if (!mapIntermediateLoc.containsKey(item)) {
699 // need to create a new intermediate location in the lattice
700 String newLocName = "ILOC" + (LocationInference.locSeed++);
703 above = startNode.getName();
705 int prevIdx = idx - 1;
706 TripleItem prevItem = new TripleItem(startNode, endNodeSet, prevIdx);
707 above = mapIntermediateLoc.get(prevItem);
710 Set<String> belowSet = new HashSet<String>();
711 for (Iterator iterator = endNodeSet.iterator(); iterator.hasNext();) {
712 HNode endNode = (HNode) iterator.next();
714 if (locSummary.getMapHNodeNameToLocationName().containsKey(endNode.getName())) {
715 locName = locSummary.getLocationName(endNode.getName());
717 locName = endNode.getName();
719 belowSet.add(locName);
721 lattice.insertNewLocationBetween(above, belowSet, newLocName);
723 mapIntermediateLoc.put(item, newLocName);
726 String locName = mapIntermediateLoc.get(item);
727 HierarchyGraph simpleHierarchyGraph = infer.getSimpleHierarchyGraph(desc);
729 if (curNode.isSharedNode()) {
730 // if the current node is shared location, add a shared location to the lattice later
731 System.out.println("###SHARED ITEM=" + item);
732 mapSharedNodeToTripleItem.put(curNode, item);
734 Set<Descriptor> descSet = simpleHierarchyGraph.getDescSetOfNode(curNode);
735 for (Iterator iterator = descSet.iterator(); iterator.hasNext();) {
736 Descriptor d = (Descriptor) iterator.next();
737 locSummary.addMapHNodeNameToLocationName(d.getSymbol(), locName);
739 locSummary.addMapHNodeNameToLocationName(curNode.getName(), locName);
742 System.out.println("-TripleItem normal=" + item);
743 System.out.println("-curNode=" + curNode.getName() + " S=" + curNode.isSharedNode()
744 + " locName=" + locName + " isC=" + curNode.isCombinationNode());
746 Set<HNode> outSet = simpleHierarchyGraph.getOutgoingNodeSet(curNode);
747 for (Iterator iterator2 = outSet.iterator(); iterator2.hasNext();) {
748 HNode outNode = (HNode) iterator2.next();
750 Set<HNode> incomingHNodeSetToOutNode = simpleHierarchyGraph.getIncomingNodeSet(outNode);
751 System.out.println("outNode=" + outNode);
752 System.out.println("---incomingHNodeSetToOutNode=" + incomingHNodeSetToOutNode);
754 if (!outNode.isSkeleton() && !outNode.isCombinationNode() && !visited.contains(outNode)) {
755 Pair<HNode, HNode> pair = new Pair(startNode, outNode);
756 if (visited.containsAll(simpleHierarchyGraph.getIncomingNodeSet(outNode))) {
757 visited.add(outNode);
758 int newidx = getCurrentHighestIndex(pair, idx + 1);
759 // int newidx = getCurrentHighestIndex(outNode, idx + 1);
760 recurDFSNormalNode(desc, lattice, startNode, endNodeSet, visited, mapIntermediateLoc,
761 newidx, locSummary, outNode);
762 // recurDFSNormalNode(desc, lattice, startNode, endNodeSet, visited, mapIntermediateLoc,
763 // idx + 1, locSummary, outNode);
765 updateHighestIndex(pair, idx + 1);
766 // updateHighestIndex(outNode, idx + 1);
767 System.out.println("NOT RECUR");
769 } else if (!outNode.isSkeleton() && outNode.isCombinationNode() && !visited.contains(outNode)) {
770 if (needToExpandCombinationNode(desc, outNode)) {
771 System.out.println("NEED TO");
772 expandCombinationNode(desc, lattice, visited, mapIntermediateLoc, locSummary, outNode);
774 System.out.println("NOT NEED TO");
782 private void recurDFS(Descriptor desc, SSJavaLattice<String> lattice,
783 HNode combinationNodeInSCGraph, Set<HNode> endNodeSet, Set<HNode> visited,
784 Map<TripleItem, String> mapIntermediateLoc, int idx, LocationSummary locSummary, HNode curNode) {
786 TripleItem item = new TripleItem(combinationNodeInSCGraph, endNodeSet, idx);
788 if (!mapIntermediateLoc.containsKey(item)) {
789 // need to create a new intermediate location in the lattice
792 String newLocName = combinationNodeInSCGraph.getName();
793 mapIntermediateLoc.put(item, newLocName);
795 String newLocName = "ILOC" + (LocationInference.locSeed++);
796 int prevIdx = idx - 1;
797 TripleItem prevItem = new TripleItem(combinationNodeInSCGraph, endNodeSet, prevIdx);
798 above = mapIntermediateLoc.get(prevItem);
800 Set<String> belowSet = new HashSet<String>();
801 for (Iterator iterator = endNodeSet.iterator(); iterator.hasNext();) {
802 HNode endNode = (HNode) iterator.next();
803 belowSet.add(endNode.getName());
805 lattice.insertNewLocationBetween(above, belowSet, newLocName);
806 mapIntermediateLoc.put(item, newLocName);
812 // Do we need to skip the combination node and assign a shared location to the next node?
813 // if (idx == 1 && curNode.isSharedNode()) {
814 // System.out.println("THE FIRST COMBINATION NODE EXPANSION IS SHARED!");
815 // recurDFS(desc, lattice, combinationNodeInSCGraph, endNodeSet, visited, mapIntermediateLoc,
816 // idx + 1, locSummary, curNode);
820 HierarchyGraph simpleHierarchyGraph = infer.getSimpleHierarchyGraph(desc);
821 String locName = mapIntermediateLoc.get(item);
822 if (curNode.isSharedNode()) {
823 // if the current node is shared location, add a shared location to the lattice later
824 System.out.println("###SHARED ITEM=" + item);
825 mapSharedNodeToTripleItem.put(curNode, item);
827 Set<Descriptor> descSet = simpleHierarchyGraph.getDescSetOfNode(curNode);
828 for (Iterator iterator = descSet.iterator(); iterator.hasNext();) {
829 Descriptor d = (Descriptor) iterator.next();
830 locSummary.addMapHNodeNameToLocationName(d.getSymbol(), locName);
832 locSummary.addMapHNodeNameToLocationName(curNode.getName(), locName);
835 System.out.println("-TripleItem=" + item);
836 System.out.println("-curNode=" + curNode.getName() + " S=" + curNode.isSharedNode()
837 + " locName=" + locName);
839 Set<HNode> outSet = simpleHierarchyGraph.getOutgoingNodeSet(curNode);
840 for (Iterator iterator2 = outSet.iterator(); iterator2.hasNext();) {
841 HNode outNode = (HNode) iterator2.next();
842 System.out.println("---recurDFS outNode=" + outNode);
843 System.out.println("---cur combinationNodeInSCGraph=" + combinationNodeInSCGraph);
844 System.out.println("---outNode combinationNodeInSCGraph="
845 + getCombinationNodeInSCGraph(desc, outNode));
847 if (!outNode.isSkeleton() && !visited.contains(outNode)) {
848 if (outNode.isCombinationNode()) {
850 Set<HNode> combineSkeletonNodeSet =
851 simpleHierarchyGraph.getCombineSetByCombinationNode(outNode);
852 Set<HNode> incomingHNodeSetToOutNode = simpleHierarchyGraph.getIncomingNodeSet(outNode);
853 // extract nodes belong to the same combine node
854 Set<HNode> incomingCombinedHNodeSet = new HashSet<HNode>();
855 for (Iterator iterator = incomingHNodeSetToOutNode.iterator(); iterator.hasNext();) {
856 HNode inNode = (HNode) iterator.next();
857 if (combineSkeletonNodeSet.contains(inNode)) {
858 incomingCombinedHNodeSet.add(inNode);
861 System.out.println("-----incomingCombinedHNodeSet=" + incomingCombinedHNodeSet);
863 // check whether the next combination node is different from the current node
864 if (combinationNodeInSCGraph.equals(getCombinationNodeInSCGraph(desc, outNode))) {
865 Pair<HNode, HNode> pair = new Pair(combinationNodeInSCGraph, outNode);
866 if (visited.containsAll(incomingCombinedHNodeSet)) {
867 visited.add(outNode);
868 System.out.println("-------curIdx=" + (idx + 1));
870 int newIdx = getCurrentHighestIndex(pair, idx + 1);
871 // int newIdx = getCurrentHighestIndex(outNode, idx + 1);
872 System.out.println("-------newIdx=" + newIdx);
873 recurDFS(desc, lattice, combinationNodeInSCGraph, endNodeSet, visited,
874 mapIntermediateLoc, newIdx, locSummary, outNode);
875 // recurDFS(desc, lattice, combinationNodeInSCGraph, endNodeSet, visited,
876 // mapIntermediateLoc, idx + 1, locSummary, outNode);
878 updateHighestIndex(pair, idx + 1);
879 // updateHighestIndex(outNode, idx + 1);
880 System.out.println("-----NOT RECUR!");
883 if (needToExpandCombinationNode(desc, outNode)) {
884 System.out.println("NEED TO");
885 expandCombinationNode(desc, lattice, visited, mapIntermediateLoc, locSummary, outNode);
887 System.out.println("NOT NEED TO");
899 private int getCurrentHighestIndex(Pair<HNode, HNode> pair, int curIdx) {
900 int recordedIdx = getCurrentHighestIndex(pair);
901 if (recordedIdx > curIdx) {
908 private int getCurrentHighestIndex(HNode node, int curIdx) {
909 int recordedIdx = getCurrentHighestIndex(node);
910 if (recordedIdx > curIdx) {
917 private int getCurrentHighestIndex(Pair<HNode, HNode> pair) {
918 if (!mapItemToHighestIndex.containsKey(pair)) {
919 mapItemToHighestIndex.put(pair, new Integer(-1));
921 return mapItemToHighestIndex.get(pair).intValue();
924 private void updateHighestIndex(Pair<HNode, HNode> pair, int idx) {
925 if (idx > getCurrentHighestIndex(pair)) {
926 mapItemToHighestIndex.put(pair, new Integer(idx));
930 private int getCurrentHighestIndex(HNode node) {
931 if (!mapHNodeToHighestIndex.containsKey(node)) {
932 mapHNodeToHighestIndex.put(node, new Integer(-1));
934 return mapHNodeToHighestIndex.get(node).intValue();
937 private void updateHighestIndex(HNode node, int idx) {
938 if (idx > getCurrentHighestIndex(node)) {
939 mapHNodeToHighestIndex.put(node, new Integer(idx));
943 private String generateElementName(BasisSet basisSet, HierarchyGraph inputGraph,
944 Map<Set<Integer>, String> mapF2LocName, Set<Integer> F) {
946 if (mapF2LocName.containsKey(F)) {
947 return mapF2LocName.get(F);
950 HNode node = basisSet.getHNode(F);
952 mapF2LocName.put(F, node.getName());
953 return node.getName();
955 if (inputGraph.BASISTOPELEMENT.equals(F)) {
956 return SSJavaAnalysis.BOTTOM;
958 String str = "LOC" + (LocationInference.locSeed++);
959 mapF2LocName.put(F, str);
965 private void resetCount(Map<Set<Integer>, Integer> mapFtoCount, Family family) {
966 for (Iterator<Set<Integer>> iter = family.FIterator(); iter.hasNext();) {
967 Set<Integer> F = iter.next();
968 mapFtoCount.put(F, 0);
972 private Map<Set<Integer>, Set<Set<Integer>>> coveringGraph(BasisSet basisSet, Family family) {
974 Map<Set<Integer>, Integer> mapFtoCount = new HashMap<Set<Integer>, Integer>();
975 Map<Set<Integer>, Set<Set<Integer>>> mapImSucc = new HashMap<Set<Integer>, Set<Set<Integer>>>();
977 // initialize COUNT(F) to 0 for all elements of the family
978 resetCount(mapFtoCount, family);
980 for (Iterator<Set<Integer>> iter = family.FIterator(); iter.hasNext();) {
981 Set<Integer> F = iter.next();
982 Set<HNode> gammaF = family.getGamma(F);
984 Set<HNode> curHNodeSet = basisSet.getHNodeSet();
985 curHNodeSet.removeAll(gammaF);
986 Set<Set<Integer>> Bset = basisSet.getBasisSetByHNodeSet(curHNodeSet);
988 for (Iterator iterator = Bset.iterator(); iterator.hasNext();) {
989 Set<Integer> B = (Set<Integer>) iterator.next();
991 Set<Integer> Fprime = new HashSet<Integer>();
996 mapFtoCount.put(Fprime, mapFtoCount.get(Fprime) + 1);
998 // if |gamma(F')|==COUNT(F') + |gamma(F)|
999 int numGammaFprime = family.getGamma(Fprime).size();
1000 int countFprime = mapFtoCount.get(Fprime);
1001 int numGammaF = family.getGamma(F).size();
1002 if (numGammaFprime == (countFprime + numGammaF)) {
1003 // ImSucc(F)=IMSucc(F) union F'
1004 addImSucc(mapImSucc, F, Fprime);
1008 resetCount(mapFtoCount, family);
1011 // System.out.println("mapImSucc=" + mapImSucc);
1016 private Set<Set<Integer>> getImSucc(Map<Set<Integer>, Set<Set<Integer>>> mapImSucc, Set<Integer> F) {
1017 if (!mapImSucc.containsKey(F)) {
1018 mapImSucc.put(F, new HashSet<Set<Integer>>());
1020 return mapImSucc.get(F);
1023 private void addImSucc(Map<Set<Integer>, Set<Set<Integer>>> mapImSucc, Set<Integer> F,
1024 Set<Integer> Fprime) {
1026 if (!mapImSucc.containsKey(F)) {
1027 mapImSucc.put(F, new HashSet<Set<Integer>>());
1030 mapImSucc.get(F).add(Fprime);
1034 private Family generateFamily(BasisSet basisSet) {
1036 Family family = new Family();
1038 for (Iterator<Set<Integer>> iterator = basisSet.basisIterator(); iterator.hasNext();) {
1039 Set<Integer> B = iterator.next();
1041 Set<Pair<Set<Integer>, Set<HNode>>> tobeadded = new HashSet<Pair<Set<Integer>, Set<HNode>>>();
1043 for (Iterator<Set<Integer>> iterator2 = family.FIterator(); iterator2.hasNext();) {
1044 Set<Integer> F = iterator2.next();
1046 Set<Integer> Fprime = new HashSet<Integer>();
1050 Set<HNode> gammaFPrimeSet = new HashSet<HNode>();
1051 gammaFPrimeSet.addAll(family.getGamma(F));
1052 gammaFPrimeSet.add(basisSet.getHNode(B));
1054 if (!family.containsF(Fprime)) {
1055 Pair<Set<Integer>, Set<HNode>> pair =
1056 new Pair<Set<Integer>, Set<HNode>>(Fprime, gammaFPrimeSet);
1057 tobeadded.add(pair);
1059 family.updateGammaF(Fprime, gammaFPrimeSet);
1063 for (Iterator<Pair<Set<Integer>, Set<HNode>>> iterator2 = tobeadded.iterator(); iterator2
1065 Pair<Set<Integer>, Set<HNode>> pair = iterator2.next();
1066 family.addFElement(pair.getFirst());
1067 family.updateGammaF(pair.getFirst(), pair.getSecond());
1074 private void debug_print(HierarchyGraph inputGraph) {
1075 System.out.println("\nBuild Lattice:" + inputGraph.getName());
1076 System.out.println("Node2Index:\n" + inputGraph.getMapHNodeToUniqueIndex());
1077 System.out.println("Node2Basis:\n" + inputGraph.getMapHNodeToBasis());
1086 public Identifier(HNode n, int i) {
1091 public int hashCode() {
1092 return node.hashCode() + idx;
1095 public boolean equals(Object obj) {
1097 if (obj instanceof Identifier) {
1098 Identifier in = (Identifier) obj;
1099 if (node.equals(in.node) && idx == in.idx) {
1110 public HNode higherNode;
1111 public Set<HNode> lowerNodeSet;
1113 public boolean isShared;
1115 public TripleItem(HNode h, Set<HNode> l, int i) {
1122 public void setShared(boolean in) {
1126 public boolean isShared() {
1130 public int hashCode() {
1133 if (higherNode != null) {
1134 h = higherNode.hashCode();
1141 return h + lowerNodeSet.hashCode() + idx;
1144 public boolean equals(Object obj) {
1146 if (obj instanceof TripleItem) {
1147 TripleItem in = (TripleItem) obj;
1148 if ((higherNode == null || (higherNode != null && higherNode.equals(in.higherNode)))
1149 && lowerNodeSet.equals(in.lowerNodeSet) && idx == in.idx && isShared == in.isShared()) {
1157 public String toString() {
1158 String rtr = higherNode + "-" + idx + "->" + lowerNodeSet;