1 package Analysis.Pointer;
5 import Analysis.Pointer.BasicBlock.BBlock;
6 import Analysis.Pointer.AllocFactory.AllocNode;
9 HashMap<FlatMethod, BasicBlock> blockMap;
10 HashMap<BBlock, Graph> bbgraphMap;
11 HashMap<FlatNode, Graph> graphMap;
12 HashMap<FlatCall, Set<BBlock>> callMap;
13 HashMap<BBlock, Set<PPoint>> returnMap;
17 AllocFactory allocFactory;
18 LinkedList<Delta> toprocess;
19 TempDescriptor returntmp;
21 public Pointer(State state, TypeUtil typeUtil) {
23 this.blockMap=new HashMap<FlatMethod, BasicBlock>();
24 this.bbgraphMap=new HashMap<BBlock, Graph>();
25 this.graphMap=new HashMap<FlatNode, Graph>();
26 this.callMap=new HashMap<FlatCall, Set<BBlock>>();
27 this.returnMap=new HashMap<BBlock, Set<PPoint>>();
28 this.typeUtil=typeUtil;
29 this.allocFactory=new AllocFactory(state, typeUtil);
30 this.toprocess=new LinkedList<Delta>();
31 ClassDescriptor stringcd=typeUtil.getClass(TypeUtil.ObjectClass);
32 this.returntmp=new TempDescriptor("RETURNVAL", stringcd);
35 public BasicBlock getBBlock(FlatMethod fm) {
36 if (!blockMap.containsKey(fm))
37 blockMap.put(fm, BasicBlock.getBBlock(fm));
38 return blockMap.get(fm);
41 Delta buildInitialContext() {
42 MethodDescriptor md=typeUtil.getMain();
43 FlatMethod fm=state.getMethodFlat(md);
44 BasicBlock bb=getBBlock(fm);
45 BBlock start=bb.getStart();
46 Delta delta=new Delta(new PPoint(start), true);
47 MySet<Edge> arrayset=new MySet<Edge>();
48 MySet<Edge> varset=new MySet<Edge>();
49 Edge arrayedge=new Edge(allocFactory.StringArray, null, allocFactory.Strings);
50 arrayset.add(arrayedge);
51 Edge stringedge=new Edge(fm.getParameter(0), allocFactory.StringArray);
52 varset.add(stringedge);
53 delta.heapedgeadd.put(allocFactory.StringArray, arrayset);
54 delta.varedgeadd.put(fm.getParameter(0), varset);
58 public void doAnalysis() {
59 toprocess.add(buildInitialContext());
61 while(!toprocess.isEmpty()) {
62 Delta delta=toprocess.remove();
63 PPoint ppoint=delta.getBlock();
64 BBlock bblock=ppoint.getBBlock();
65 Vector<FlatNode> nodes=bblock.nodes();
67 //Build base graph for entrance to this basic block
68 delta=applyInitDelta(delta, bblock);
69 Graph graph=bbgraphMap.get(bblock);
72 //Compute delta at exit of each node
73 for(int i=0; i<nodes.size();i++) {
74 FlatNode currNode=nodes.get(i);
75 if (!graphMap.containsKey(currNode)) {
76 graphMap.put(currNode, new Graph(graph));
78 nodeGraph=graphMap.get(currNode);
79 delta=processNode(bblock, i, currNode, delta, nodeGraph);
81 generateFinalDelta(bblock, delta, nodeGraph);
85 void buildInitDelta(Graph graph, Delta newDelta) {
86 //First compute the set of temps
87 HashSet<TempDescriptor> tmpSet=new HashSet<TempDescriptor>();
88 tmpSet.addAll(graph.varMap.keySet());
89 tmpSet.addAll(graph.parent.varMap.keySet());
91 //Next build the temp map part of the delta
92 for(TempDescriptor tmp:tmpSet) {
93 MySet<Edge> edgeSet=new MySet<Edge>();
95 if (graph.varMap.containsKey(tmp))
96 edgeSet.addAll(graph.varMap.get(tmp));
98 edgeSet.addAll(graph.parent.varMap.get(tmp));
99 newDelta.varedgeadd.put(tmp, edgeSet);
102 //Next compute the set of src allocnodes
103 HashSet<AllocNode> nodeSet=new HashSet<AllocNode>();
104 nodeSet.addAll(graph.nodeMap.keySet());
105 nodeSet.addAll(graph.parent.nodeMap.keySet());
107 for(AllocNode node:nodeSet) {
108 MySet<Edge> edgeSet=new MySet<Edge>();
110 if (graph.nodeMap.containsKey(node))
111 edgeSet.addAll(graph.nodeMap.get(node));
113 edgeSet.addAll(graph.parent.nodeMap.get(node));
114 newDelta.heapedgeadd.put(node, edgeSet);
117 if (graph.nodeAges.contains(node))
118 newDelta.addNodeAges.add(node);
119 else if (graph.parent.nodeAges.contains(node))
120 newDelta.addNodeAges.add(node);
124 void generateFinalDelta(BBlock bblock, Delta delta, Graph graph) {
125 Delta newDelta=new Delta(null, false);
126 if (delta.getInit()) {
127 buildInitDelta(graph, newDelta);
129 /* We can break the old delta...it is done being used */
130 /* First we will build variable edges */
131 HashSet<TempDescriptor> tmpSet=new HashSet<TempDescriptor>();
132 tmpSet.addAll(delta.basevaredge.keySet());
133 tmpSet.addAll(delta.varedgeadd.keySet());
134 for(TempDescriptor tmp:tmpSet) {
135 /* Start with the new incoming edges */
136 MySet<Edge> newbaseedge=delta.basevaredge.get(tmp);
137 /* Remove the remove set */
138 newbaseedge.removeAll(delta.varedgeremove.get(tmp));
139 /* Add in the new set*/
140 newbaseedge.addAll(delta.varedgeadd.get(tmp));
141 /* Store the results */
142 newDelta.varedgeadd.put(tmp, newbaseedge);
145 /* Next we build heap edges */
146 HashSet<AllocNode> nodeSet=new HashSet<AllocNode>();
147 nodeSet.addAll(delta.baseheapedge.keySet());
148 nodeSet.addAll(delta.heapedgeadd.keySet());
149 nodeSet.addAll(delta.heapedgeremove.keySet());
150 for(AllocNode node:nodeSet) {
151 /* Start with the new incoming edges */
152 MySet<Edge> newheapedge=(MySet<Edge>) delta.baseheapedge.get(node).clone();
153 /* Remove the remove set */
154 newheapedge.removeAll(delta.heapedgeremove.get(node));
155 /* Add in the add set */
156 newheapedge.addAll(delta.heapedgeadd.get(node));
157 newDelta.heapedgeadd.put(node, newheapedge);
159 /* Also need to subtract off some edges */
160 MySet<Edge> removeset=delta.heapedgeremove.get(node);
162 /* Remove the newly created edges..no need to propagate a diff for those */
163 removeset.removeAll(delta.baseheapedge.get(node));
164 newDelta.heapedgeremove.put(node, removeset);
167 /* Compute new ages */
168 newDelta.addNodeAges.addAll(delta.baseNodeAges);
169 newDelta.addNodeAges.addAll(delta.addNodeAges);
172 /* Now we need to propagate newdelta */
173 if (!newDelta.heapedgeadd.isEmpty()||!newDelta.heapedgeremove.isEmpty()||!newDelta.varedgeadd.isEmpty()||!newDelta.addNodeAges.isEmpty()) {
174 /* We have a delta to propagate */
175 Vector<BBlock> blockvector=bblock.next();
176 for(int i=0;i<blockvector.size();i++) {
178 newDelta.setBlock(new PPoint(blockvector.get(i)));
179 toprocess.add(newDelta);
181 toprocess.add(newDelta.diffBlock(new PPoint(blockvector.get(i))));
187 Delta processNode(BBlock bblock, int index, FlatNode node, Delta delta, Graph newgraph) {
188 switch(node.kind()) {
190 return processNewNode((FlatNew)node, delta, newgraph);
191 case FKind.FlatFieldNode:
192 case FKind.FlatElementNode:
193 return processFieldElementNode(node, delta, newgraph);
194 case FKind.FlatCastNode:
195 case FKind.FlatOpNode:
196 case FKind.FlatReturnNode:
197 return processCopyNode(node, delta, newgraph);
198 case FKind.FlatSetFieldNode:
199 case FKind.FlatSetElementNode:
200 return processSetFieldElementNode(node, delta, newgraph);
201 case FKind.FlatMethod:
203 return processFlatNop(node, delta, newgraph);
205 return processFlatCall(bblock, index, (FlatCall) node, delta, newgraph);
206 case FKind.FlatSESEEnterNode:
207 case FKind.FlatSESEExitNode:
208 throw new Error("Unimplemented node:"+node);
210 throw new Error("Unrecognized node:"+node);
215 void processThisTargets(HashSet<ClassDescriptor> targetSet, Graph graph, Delta delta, Delta newDelta, HashSet<AllocNode> nodeset, Stack<AllocNode> tovisit, MySet<Edge> edgeset, TempDescriptor tmpthis, HashSet<AllocNode> oldnodeset) {
216 //Handle the this temp
218 MySet<Edge> edges=(oldnodeset!=null)?GraphManip.getDiffEdges(delta, tmpthis):GraphManip.getEdges(graph, delta, tmpthis);
219 newDelta.varedgeadd.put(tmpthis, (MySet<Edge>) edges.clone());
220 edgeset.addAll(edges);
222 AllocNode dstnode=e.dst;
223 if (!nodeset.contains(dstnode)&&(oldnodeset==null||!oldnodeset.contains(dstnode))) {
224 TypeDescriptor type=dstnode.getType();
225 if (!type.isArray()) {
226 targetSet.add(type.getClassDesc());
228 //arrays don't have code
229 targetSet.add(typeUtil.getClass(TypeUtil.ObjectClass));
231 nodeset.add(dstnode);
232 tovisit.add(dstnode);
238 void processParams(Graph graph, Delta delta, Delta newDelta, HashSet<AllocNode> nodeset, Stack<AllocNode> tovisit, MySet<Edge> edgeset, FlatCall fcall, boolean diff) {
239 //Go through each temp
240 for(int i=0;i<fcall.numArgs();i++) {
241 TempDescriptor tmp=fcall.getArg(i);
242 MySet<Edge> edges=diff?GraphManip.getDiffEdges(delta, tmp):GraphManip.getEdges(graph, delta, tmp);
243 newDelta.varedgeadd.put(tmp, (MySet<Edge>) edges.clone());
244 edgeset.addAll(edges);
246 if (!nodeset.contains(e.dst)) {
254 void computeReachableNodes(Graph graph, Delta delta, Delta newDelta, HashSet<AllocNode> nodeset, Stack<AllocNode> tovisit, MySet<Edge> edgeset, HashSet<AllocNode> oldnodeset) {
255 while(!tovisit.isEmpty()) {
256 AllocNode node=tovisit.pop();
257 MySet<Edge> edges=GraphManip.getEdges(graph, delta, node);
258 newDelta.heapedgeadd.put(node, edges);
259 edgeset.addAll(edges);
261 if (!nodeset.contains(e.dst)&&(oldnodeset==null||!oldnodeset.contains(e.dst))) {
269 HashSet<MethodDescriptor> computeTargets(FlatCall fcall, Delta newDelta) {
270 TempDescriptor tmpthis=fcall.getThis();
271 MethodDescriptor md=fcall.getMethod();
272 HashSet<MethodDescriptor> targets=new HashSet<MethodDescriptor>();
277 for(Edge e:newDelta.varedgeadd.get(tmpthis)) {
278 AllocNode node=e.dst;
279 ClassDescriptor cd=node.getType().getClassDesc();
280 //Figure out exact method called and add to set
281 targets.add(cd.getCalledMethod(md));
288 void fixMapping(FlatCall fcall, HashSet<MethodDescriptor> targets, MySet<Edge> oldedgeset, Delta newDelta, BBlock callblock, int callindex) {
289 Delta basedelta=null;
290 TempDescriptor tmpthis=fcall.getThis();
292 for(MethodDescriptor calledmd:targets) {
293 FlatMethod fm=state.getMethodFlat(calledmd);
294 boolean newmethod=false;
297 HashMap<TempDescriptor, TempDescriptor> tmpMap=new HashMap<TempDescriptor, TempDescriptor>();
300 tmpMap.put(tmpthis, fm.getParameter(offset++));
302 for(int i=0;i<fcall.numArgs();i++) {
303 TempDescriptor tmp=fcall.getArg(i);
304 tmpMap.put(tmp,fm.getParameter(i+offset));
307 //Get basicblock for the method
308 BasicBlock block=getBBlock(fm);
311 if (!callMap.containsKey(fcall)) {
312 callMap.put(fcall, new HashSet<BBlock>());
315 Delta returnDelta=null;
317 if (!callMap.get(fcall).contains(block.getStart())) {
318 callMap.get(fcall).add(block.getStart());
322 if (!returnMap.containsKey(block.getExit())) {
323 returnMap.put(block.getExit(), new HashSet<PPoint>());
325 returnMap.get(block.getExit()).add(new PPoint(callblock, callindex));
327 if (bbgraphMap.containsKey(block.getExit())) {
328 //Need to push existing results to current node
329 if (returnDelta==null) {
330 returnDelta=new Delta(null, false);
331 buildInitDelta(bbgraphMap.get(block.getExit()), returnDelta);
332 if (!returnDelta.heapedgeadd.isEmpty()||!returnDelta.heapedgeremove.isEmpty()||!returnDelta.varedgeadd.isEmpty()) {
333 returnDelta.setBlock(new PPoint(callblock, callindex));
334 toprocess.add(returnDelta);
337 if (!returnDelta.heapedgeadd.isEmpty()||!returnDelta.heapedgeremove.isEmpty()||!returnDelta.varedgeadd.isEmpty()) {
338 toprocess.add(returnDelta.diffBlock(new PPoint(callblock, callindex)));
344 if (oldedgeset==null) {
345 //First build of this graph
346 //Build and enqueue delta...safe to just use existing delta
347 Delta d=newDelta.changeParams(tmpMap, new PPoint(block.getStart()));
349 } else if (newmethod) {
350 if (basedelta==null) {
351 basedelta=newDelta.buildBase(oldedgeset);
353 //Build and enqueue delta
354 Delta d=basedelta.changeParams(tmpMap, new PPoint(block.getStart()));
357 //Build and enqueue delta
358 Delta d=newDelta.changeParams(tmpMap, new PPoint(block.getStart()));
365 Delta processFlatCall(BBlock callblock, int callindex, FlatCall fcall, Delta delta, Graph graph) {
366 Delta newDelta=new Delta(null, false);
368 if (delta.getInit()) {
369 MySet<Edge> edgeset=new MySet<Edge>();
370 HashSet<AllocNode> nodeset=new HashSet<AllocNode>();
371 HashSet<ClassDescriptor> targetSet=new HashSet<ClassDescriptor>();
372 Stack<AllocNode> tovisit=new Stack<AllocNode>();
373 TempDescriptor tmpthis=fcall.getThis();
375 //Handle the this temp
376 processThisTargets(targetSet, graph, delta, newDelta, nodeset, tovisit, edgeset, tmpthis, null);
378 //Go through each temp
379 processParams(graph, delta, newDelta, nodeset, tovisit, edgeset, fcall, false);
381 //Traverse all reachable nodes
382 computeReachableNodes(graph, delta, newDelta, nodeset, tovisit, edgeset, null);
384 //Compute call targets
385 HashSet<MethodDescriptor> targets=computeTargets(fcall, newDelta);
388 fixMapping(fcall, targets, null, newDelta, callblock, callindex);
390 graph.reachNode=nodeset;
391 graph.reachEdge=edgeset;
393 //Apply diffs to graph
394 applyDiffs(graph, delta);
396 MySet<Edge> edgeset=new MySet<Edge>();
397 HashSet<AllocNode> nodeset=new HashSet<AllocNode>();
398 MySet<Edge> oldedgeset=graph.reachEdge;
399 HashSet<AllocNode> oldnodeset=graph.reachNode;
401 HashSet<ClassDescriptor> targetSet=new HashSet<ClassDescriptor>();
402 Stack<AllocNode> tovisit=new Stack<AllocNode>();
403 TempDescriptor tmpthis=fcall.getThis();
405 //Handle the this temp
406 processThisTargets(targetSet, graph, delta, newDelta, nodeset, tovisit, edgeset, tmpthis, oldnodeset);
408 //Go through each temp
409 processParams(graph, delta, newDelta, nodeset, tovisit, edgeset, fcall, true);
411 //Go through each new heap edge that starts from old node
412 MySet<Edge> newedges=GraphManip.getDiffEdges(delta, oldnodeset);
413 edgeset.addAll(newedges);
414 for(Edge e:newedges) {
415 if (!nodeset.contains(e.dst)&&!oldnodeset.contains(e.dst)) {
421 //Traverse all reachable nodes
422 computeReachableNodes(graph, delta, newDelta, nodeset, tovisit, edgeset, oldnodeset);
424 //Compute call targets
425 HashSet<MethodDescriptor> targets=computeTargets(fcall, newDelta);
427 //add in new nodeset and edgeset
428 oldnodeset.addAll(nodeset);
429 oldedgeset.addAll(edgeset);
432 fixMapping(fcall, targets, oldedgeset, newDelta, callblock, callindex);
434 //Apply diffs to graph
435 applyDiffs(graph, delta);
440 void applyDiffs(Graph graph, Delta delta) {
441 //Add hidden base edges
442 for(Map.Entry<AllocNode, MySet<Edge>> e: delta.baseheapedge.entrySet()) {
443 AllocNode node=e.getKey();
444 MySet<Edge> edges=e.getValue();
445 if (graph.nodeMap.containsKey(node)) {
446 MySet<Edge> nodeEdges=graph.nodeMap.get(node);
447 nodeEdges.addAll(edges);
452 for(Map.Entry<AllocNode, MySet<Edge>> e: delta.heapedgeremove.entrySet()) {
453 AllocNode node=e.getKey();
454 MySet<Edge> edgestoremove=e.getValue();
455 if (graph.nodeMap.containsKey(node)) {
456 //Just apply diff to current map
457 graph.nodeMap.get(node).removeAll(edgestoremove);
459 //Generate diff from parent graph
460 MySet<Edge> parentedges=graph.parent.nodeMap.get(node);
461 MySet<Edge> newedgeset=Util.setSubtract(parentedges, edgestoremove);
462 graph.nodeMap.put(node, newedgeset);
467 for(Map.Entry<AllocNode, MySet<Edge>> e: delta.heapedgeadd.entrySet()) {
468 AllocNode node=e.getKey();
469 MySet<Edge> edgestoadd=e.getValue();
470 //If we have not done a subtract, then
471 if (!graph.nodeMap.containsKey(node)) {
472 //Copy the parent entry
473 graph.nodeMap.put(node, (MySet<Edge>)graph.parent.nodeMap.get(node).clone());
475 graph.nodeMap.get(node).addAll(edgestoadd);
479 for(Map.Entry<TempDescriptor, MySet<Edge>> e: delta.varedgeremove.entrySet()) {
480 TempDescriptor tmp=e.getKey();
481 MySet<Edge> edgestoremove=e.getValue();
483 if (graph.varMap.containsKey(tmp)) {
484 //Just apply diff to current map
485 graph.varMap.get(tmp).removeAll(edgestoremove);
487 //Generate diff from parent graph
488 MySet<Edge> parentedges=graph.parent.varMap.get(tmp);
489 MySet<Edge> newedgeset=Util.setSubtract(parentedges, edgestoremove);
490 graph.varMap.put(tmp, newedgeset);
495 for(Map.Entry<TempDescriptor, MySet<Edge>> e: delta.varedgeadd.entrySet()) {
496 TempDescriptor tmp=e.getKey();
497 MySet<Edge> edgestoadd=e.getValue();
498 graph.varMap.put(tmp, (MySet<Edge>) edgestoadd.clone());
502 for(AllocNode node:delta.addNodeAges) {
503 graph.nodeAges.add(node);
507 Delta processSetFieldElementNode(FlatNode node, Delta delta, Graph graph) {
511 if (node.kind()==FKind.FlatSetElementNode) {
512 FlatSetElementNode fen=(FlatSetElementNode) node;
517 FlatSetFieldNode ffn=(FlatSetFieldNode) node;
522 if (delta.getInit()) {
523 HashSet<AllocNode> srcNodes=GraphManip.getNodes(graph, delta, src);
524 HashSet<AllocNode> dstNodes=GraphManip.getNodes(graph, delta, dst);
525 MySet<Edge> edgesToAdd=GraphManip.genEdges(srcNodes, fd, dstNodes);
526 MySet<Edge> edgesToRemove=null;
527 if (dstNodes.size()==1&&!dstNodes.iterator().next().isSummary()) {
528 /* Can do a strong update */
529 edgesToRemove=GraphManip.getEdges(graph, delta, dstNodes, fd);
532 updateHeapDelta(graph, delta, edgesToAdd, edgesToRemove);
533 applyDiffs(graph, delta);
535 /* First look at new sources */
536 MySet<Edge> edgesToAdd=new MySet<Edge>();
537 HashSet<AllocNode> newSrcNodes=GraphManip.getDiffNodes(delta, src);
538 HashSet<AllocNode> dstNodes=GraphManip.getDiffNodes(delta, dst);
539 edgesToAdd.addAll(GraphManip.genEdges(newSrcNodes, fd, dstNodes));
540 HashSet<AllocNode> newDstNodes=GraphManip.getDiffNodes(delta, dst);
541 MySet<Edge> edgesToRemove=null;
542 if (newDstNodes.size()!=0) {
543 if (dstNodes.size()==1&&!dstNodes.iterator().next().isSummary()) {
544 /* Need to undo strong update */
545 if (graph.strongUpdateSet!=null) {
546 edgesToAdd.addAll(graph.strongUpdateSet);
547 graph.strongUpdateSet.clear();
549 } else if (dstNodes.size()==0&&newDstNodes.size()==1&&!newDstNodes.iterator().next().isSummary()&&graph.strongUpdateSet==null) {
550 edgesToRemove=GraphManip.getEdges(graph, delta, dstNodes, fd);
552 HashSet<AllocNode> srcNodes=GraphManip.getDiffNodes(delta, src);
553 edgesToAdd.addAll(GraphManip.genEdges(srcNodes, fd, newDstNodes));
556 updateHeapDelta(graph, delta, edgesToAdd, edgesToRemove);
557 applyDiffs(graph, delta);
562 Delta processCopyNode(FlatNode node, Delta delta, Graph graph) {
565 if (node.kind()==FKind.FlatOpNode) {
566 FlatOpNode fon=(FlatOpNode) node;
569 } else if (node.kind()==FKind.FlatReturnNode) {
570 FlatReturnNode frn=(FlatReturnNode)node;
571 src=frn.getReturnTemp();
574 FlatCastNode fcn=(FlatCastNode) node;
578 if (delta.getInit()) {
579 HashSet<AllocNode> srcnodes=GraphManip.getNodes(graph, delta, src);
580 MySet<Edge> edgesToAdd=GraphManip.genEdges(src, srcnodes);
581 MySet<Edge> edgesToRemove=GraphManip.getEdges(graph, delta, dst);
582 updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
583 applyDiffs(graph, delta);
585 /* First compute new src nodes */
586 HashSet<AllocNode> newSrcNodes=GraphManip.getDiffNodes(delta, src);
588 /* Compute the union, and then the set of edges */
589 MySet<Edge> edgesToAdd=GraphManip.genEdges(src, newSrcNodes);
591 /* Compute set of edges to remove */
592 MySet<Edge> edgesToRemove=GraphManip.getDiffEdges(delta, dst);
595 updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
596 applyDiffs(graph, delta);
601 Delta processFieldElementNode(FlatNode node, Delta delta, Graph graph) {
605 if (node.kind()==FKind.FlatElementNode) {
606 FlatElementNode fen=(FlatElementNode) node;
611 FlatFieldNode ffn=(FlatFieldNode) node;
616 if (delta.getInit()) {
617 HashSet<AllocNode> srcnodes=GraphManip.getNodes(graph, delta, src);
618 HashSet<AllocNode> fdnodes=GraphManip.getNodes(graph, delta, srcnodes, fd);
619 MySet<Edge> edgesToAdd=GraphManip.genEdges(src, fdnodes);
620 MySet<Edge> edgesToRemove=GraphManip.getEdges(graph, delta, dst);
621 updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
622 applyDiffs(graph, delta);
624 /* First compute new objects we read fields of */
625 HashSet<AllocNode> allsrcnodes=GraphManip.getNodes(graph, delta, src);
626 HashSet<AllocNode> difffdnodes=GraphManip.getDiffNodes(delta, allsrcnodes, fd);
627 /* Next compute new targets of fields */
628 HashSet<AllocNode> newsrcnodes=GraphManip.getDiffNodes(delta, src);
629 HashSet<AllocNode> newfdnodes=GraphManip.getNodes(graph, delta, newsrcnodes, fd);
630 /* Compute the union, and then the set of edges */
631 HashSet<AllocNode> newTargets=new HashSet<AllocNode>();
632 newTargets.addAll(newfdnodes);
633 newTargets.addAll(difffdnodes);
634 MySet<Edge> edgesToAdd=GraphManip.genEdges(src, newTargets);
636 /* Compute set of edges to remove */
637 MySet<Edge> edgesToRemove=GraphManip.getDiffEdges(delta, dst);
640 updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
641 applyDiffs(graph, delta);
646 static void updateVarDelta(Graph graph, Delta delta, TempDescriptor tmp, MySet<Edge> edgestoAdd, MySet<Edge> edgestoRemove) {
647 MySet<Edge> edgeAdd=delta.varedgeadd.get(tmp);
648 MySet<Edge> edgeRemove=delta.varedgeremove.get(tmp);
649 MySet<Edge> existingEdges=graph.getEdges(tmp);
650 for(Edge e: edgestoRemove) {
651 //remove edge from delta
653 //if the edge is already in the graph, add an explicit remove to the delta
654 if (existingEdges.contains(e))
657 for(Edge e: edgestoAdd) {
658 //Remove the edge from the remove set
659 edgeRemove.remove(e);
660 //Explicitly add it to the add set unless it is already in the graph
661 if (!existingEdges.contains(e))
666 static void updateHeapDelta(Graph graph, Delta delta, MySet<Edge> edgestoAdd, MySet<Edge> edgestoRemove) {
667 for(Edge e: edgestoRemove) {
669 MySet<Edge> edgeAdd=delta.heapedgeadd.get(src);
670 MySet<Edge> existingEdges=graph.getEdges(src);
671 //remove edge from delta
673 //if the edge is already in the graph, add an explicit remove to the delta
674 if (existingEdges.contains(e)) {
675 MySet<Edge> edgeRemove=delta.heapedgeremove.get(src);
679 for(Edge e: edgestoAdd) {
681 MySet<Edge> edgeRemove=delta.heapedgeremove.get(src);
682 MySet<Edge> existingEdges=graph.getEdges(src);
683 //Remove the edge from the remove set
684 edgeRemove.remove(e);
685 //Explicitly add it to the add set unless it is already in the graph
686 if (!existingEdges.contains(e)) {
687 MySet<Edge> edgeAdd=delta.heapedgeadd.get(src);
693 Delta processFlatNop(FlatNode node, Delta delta, Graph graph) {
694 applyDiffs(graph, delta);
698 Delta processNewNode(FlatNew node, Delta delta, Graph graph) {
699 AllocNode summary=allocFactory.getAllocNode(node, true);
700 AllocNode single=allocFactory.getAllocNode(node, false);
701 TempDescriptor tmp=node.getDst();
703 if (delta.getInit()) {
704 /* We don't have to deal with summarization here... The
705 * intuition is that this is the only place where we generate
706 * nodes for this allocation site and this is the first time
707 * we've analyzed this site */
710 Edge e=new Edge(tmp, single);
712 MySet<Edge> newedges=new MySet<Edge>();
714 //Add it into the diffs
715 delta.varedgeadd.put(tmp, newedges);
716 //Remove the old edges
717 delta.varedgeremove.put(tmp, graph.getEdges(tmp));
718 //Apply incoming diffs to graph
719 applyDiffs(graph, delta);
720 //Note that we create a single node
721 delta.addNodeAges.add(single);
723 /* 1. Fix up the variable edge additions */
725 for(Iterator<Map.Entry<TempDescriptor, MySet<Edge>>> entryIt=delta.varedgeadd.entrySet().iterator();entryIt.hasNext();) {
726 Map.Entry<TempDescriptor, MySet<Edge>> entry=entryIt.next();
728 if (entry.getKey()==tmp) {
729 /* Check if this is the tmp we overwrite */
732 /* Otherwise, check if the target of the edge is changed... */
733 summarizeSet(entry.getValue(), graph.varMap.get(entry.getKey()), single, summary);
737 /* 2. Fix up the base variable edges */
739 for(Iterator<Map.Entry<TempDescriptor, MySet<Edge>>> entryIt=delta.basevaredge.entrySet().iterator();entryIt.hasNext();) {
740 Map.Entry<TempDescriptor, MySet<Edge>> entry=entryIt.next();
741 TempDescriptor entrytmp=entry.getKey();
743 /* Check is this is the tmp we overwrite, if so add to remove set */
744 Util.relationUpdate(delta.varedgeremove, tmp, null, entry.getValue());
746 /* Check if the target of the edge is changed */
747 MySet<Edge> newset=(MySet<Edge>)entry.getValue().clone();
748 MySet<Edge> removeset=shrinkSet(newset, graph.varMap.get(entrytmp), single, summary);
749 Util.relationUpdate(delta.varedgeremove, entrytmp, newset, removeset);
750 Util.relationUpdate(delta.varedgeadd, entrytmp, null, newset);
755 /* 3. Fix up heap edge additions */
757 HashMap<AllocNode, MySet<Edge>> addheapedge=new HashMap<AllocNode, MySet<Edge>>();
758 for(Iterator<Map.Entry<AllocNode, MySet<Edge>>> entryIt=delta.heapedgeadd.entrySet().iterator();entryIt.hasNext();) {
759 Map.Entry<AllocNode, MySet<Edge>> entry=entryIt.next();
760 MySet<Edge> edgeset=entry.getValue();
761 AllocNode allocnode=entry.getKey();
762 if (allocnode==single) {
764 summarizeSet(edgeset, graph.nodeMap.get(summary), single, summary);
765 addheapedge.put(summary, edgeset);
767 summarizeSet(edgeset, graph.nodeMap.get(allocnode), single, summary);
773 for(Map.Entry<AllocNode, MySet<Edge>> entry:addheapedge.entrySet()) {
774 AllocNode allocnode=entry.getKey();
775 Util.relationUpdate(delta.heapedgeadd, allocnode, null, entry.getValue());
778 /* 4. Fix up the base heap edges */
780 for(Iterator<Map.Entry<AllocNode, MySet<Edge>>> entryIt=delta.baseheapedge.entrySet().iterator();entryIt.hasNext();) {
781 Map.Entry<AllocNode, MySet<Edge>> entry=entryIt.next();
782 MySet<Edge> edgeset=entry.getValue();
783 AllocNode allocnode=entry.getKey();
784 if (allocnode==single) {
787 AllocNode addnode=(allocnode==single)?summary:allocnode;
789 MySet<Edge> newset=(MySet<Edge>) edgeset.clone();
790 MySet<Edge> removeset=shrinkSet(newset, graph.nodeMap.get(addnode), single, summary);
791 Util.relationUpdate(delta.heapedgeadd, addnode, null, newset);
792 Util.relationUpdate(delta.heapedgeremove, allocnode, null, removeset);
795 /* Update Node Ages...If the base or addNodeAges set contains a
796 * single node, it now should also contain a summary node... No
797 * need to generate a single node as that has already been
799 if (delta.baseNodeAges.contains(single)||delta.addNodeAges.contains(single)) {
800 delta.addNodeAges.add(summary);
803 //Apply incoming diffs to graph
804 applyDiffs(graph, delta);
809 /* This function builds a new edge set where oldnode is summarized into new node */
811 void summarizeSet(MySet<Edge> edgeset, MySet<Edge> oldedgeset, AllocNode oldnode, AllocNode sumnode) {
812 MySet<Edge> newSet=null;
813 for(Iterator<Edge> edgeit=edgeset.iterator();edgeit.hasNext();) {
814 Edge e=edgeit.next();
815 if (e.dst==oldnode||e.src==oldnode) {
817 newSet=new MySet<Edge>();
822 if (e.dst==oldnode) {
825 if (e.src==oldnode) {
828 if (oldedgeset==null||!oldedgeset.contains(e))
833 edgeset.addAll(newSet);
836 /* Shrinks the incoming set to just include rewritten values.
837 * Returns a set of the original rewritten values */
839 MySet<Edge> shrinkSet(MySet<Edge> edgeset, MySet<Edge> oldedgeset, AllocNode oldnode, AllocNode newnode) {
840 MySet<Edge> newSet=null;
841 MySet<Edge> removeSet=null;
842 for(Iterator<Edge> edgeit=edgeset.iterator();edgeit.hasNext();) {
843 Edge e=edgeit.next();
845 if (e.dst==oldnode||e.src==oldnode) {
847 newSet=new MySet<Edge>();
848 removeSet=new MySet<Edge>();
851 removeSet.add(e.copy());
856 if (oldedgeset==null||!oldedgeset.contains(e))
861 edgeset.addAll(newSet);
865 /* This function returns a completely new Delta... It is safe to
868 Delta applyInitDelta(Delta delta, BBlock block) {
869 //Apply delta to graph
870 boolean newGraph=false;
871 if (!bbgraphMap.containsKey(block)) {
872 bbgraphMap.put(block, new Graph(null));
876 Graph graph=bbgraphMap.get(block);
879 newdelta=new Delta(null, true);
880 //Add in heap edges and throw away original diff
881 graph.nodeMap.putAll(delta.heapedgeadd);
882 //Add in var edges and throw away original diff
883 graph.varMap.putAll(delta.varedgeadd);
884 //Record that this is initial set...
885 graph.nodeAges.addAll(delta.addNodeAges);
887 newdelta=new Delta(null, false);
888 //merge in heap edges and variables
889 mergeHeapEdges(graph, delta, newdelta);
890 mergeVarEdges(graph, delta, newdelta);
891 mergeAges(graph, delta, newdelta);
896 /* This function merges in the heap edges. It updates delta to be
899 void mergeHeapEdges(Graph graph, Delta delta, Delta newdelta) {
901 for(Map.Entry<AllocNode, MySet<Edge>> heapedge:delta.heapedgeadd.entrySet()) {
902 AllocNode nsrc=heapedge.getKey();
903 MySet<Edge> edges=heapedge.getValue();
904 if (!graph.nodeMap.containsKey(nsrc)) {
905 graph.nodeMap.put(nsrc, new MySet<Edge>());
907 MySet<Edge> dstedges=graph.nodeMap.get(nsrc);
908 MySet<Edge> diffedges=new MySet<Edge>();
910 if (!dstedges.contains(e)) {
916 //Done with edge set...
917 if (diffedges.size()>0) {
919 newdelta.baseheapedge.put(nsrc, diffedges);
924 /* This function merges in the var edges. It updates delta to be
927 void mergeVarEdges(Graph graph, Delta delta, Delta newdelta) {
929 for(Map.Entry<TempDescriptor, MySet<Edge>> varedge:delta.varedgeadd.entrySet()) {
930 TempDescriptor tmpsrc=varedge.getKey();
931 MySet<Edge> edges=varedge.getValue();
932 if (!graph.varMap.containsKey(tmpsrc)) {
933 graph.varMap.put(tmpsrc, new MySet<Edge>());
935 MySet<Edge> dstedges=graph.varMap.get(tmpsrc);
936 MySet<Edge> diffedges=new MySet<Edge>();
938 if (!dstedges.contains(e)) {
944 //Done with edge set...
945 if (diffedges.size()>=0) {
947 newdelta.basevaredge.put(tmpsrc,diffedges);
952 void mergeAges(Graph graph, Delta delta, Delta newDelta) {
954 for(AllocNode node:delta.addNodeAges) {
955 if (!graph.nodeAges.contains(node)) {
956 graph.nodeAges.add(node);
957 newDelta.baseNodeAges.add(node);