1 package Analysis.Pointer;
5 import Analysis.Liveness;
6 import Analysis.Pointer.BasicBlock.BBlock;
7 import Analysis.Pointer.AllocFactory.AllocNode;
8 import Analysis.Disjoint.Taint;
11 public class Pointer {
12 HashMap<FlatMethod, BasicBlock> blockMap;
13 HashMap<BBlock, Graph> bbgraphMap;
14 HashMap<FlatNode, Graph> graphMap;
15 HashMap<FlatCall, Set<BBlock>> callMap;
16 HashMap<BBlock, Set<PPoint>> returnMap;
17 HashMap<BBlock, Set<TempDescriptor>> bblivetemps;
21 AllocFactory allocFactory;
22 LinkedList<Delta> toprocess;
23 TempDescriptor returntmp;
25 public Pointer(State state, TypeUtil typeUtil) {
27 this.blockMap=new HashMap<FlatMethod, BasicBlock>();
28 this.bbgraphMap=new HashMap<BBlock, Graph>();
29 this.bblivetemps=new HashMap<BBlock, Set<TempDescriptor>>();
30 this.graphMap=new HashMap<FlatNode, Graph>();
31 this.callMap=new HashMap<FlatCall, Set<BBlock>>();
32 this.returnMap=new HashMap<BBlock, Set<PPoint>>();
33 this.typeUtil=typeUtil;
34 this.allocFactory=new AllocFactory(state, typeUtil);
35 this.toprocess=new LinkedList<Delta>();
36 ClassDescriptor stringcd=typeUtil.getClass(TypeUtil.ObjectClass);
37 this.returntmp=new TempDescriptor("RETURNVAL", stringcd);
40 public BasicBlock getBBlock(FlatMethod fm) {
41 if (!blockMap.containsKey(fm)) {
42 blockMap.put(fm, BasicBlock.getBBlock(fm));
43 Hashtable<FlatNode, Set<TempDescriptor>> livemap=Liveness.computeLiveTemps(fm);
44 for(BBlock bblock:blockMap.get(fm).getBlocks()) {
45 FlatNode fn=bblock.nodes.get(0);
47 HashSet<TempDescriptor> fmset=new HashSet<TempDescriptor>();
48 fmset.addAll((List<TempDescriptor>)Arrays.asList(fm.writesTemps()));
49 bblivetemps.put(bblock, fmset);
51 Set<TempDescriptor> livetemps=livemap.get(fn);
52 bblivetemps.put(bblock, livetemps);
53 livetemps.add(returntmp);
57 return blockMap.get(fm);
60 Delta buildInitialContext() {
61 MethodDescriptor md=typeUtil.getMain();
62 FlatMethod fm=state.getMethodFlat(md);
63 BasicBlock bb=getBBlock(fm);
64 BBlock start=bb.getStart();
65 Delta delta=new Delta(new PPoint(start), true);
66 MySet<Edge> arrayset=new MySet<Edge>();
67 MySet<Edge> varset=new MySet<Edge>();
68 Edge arrayedge=new Edge(allocFactory.StringArray, null, allocFactory.Strings);
69 Edge stringedge=new Edge(fm.getParameter(0), allocFactory.StringArray);
70 delta.addHeapEdge(arrayedge);
71 delta.addVarEdge(stringedge);
76 public void doAnalysis() {
77 toprocess.add(buildInitialContext());
79 while(!toprocess.isEmpty()) {
80 Delta delta=toprocess.remove();
81 PPoint ppoint=delta.getBlock();
82 BBlock bblock=ppoint.getBBlock();
83 Vector<FlatNode> nodes=bblock.nodes();
86 if (ppoint.getIndex()==-1) {
87 //Build base graph for entrance to this basic block
88 //System.out.println("Processing "+bblock.nodes.get(0).toString().replace(' ','_'));
90 delta=applyInitDelta(delta, bblock);
91 //System.out.println("Generating:");
94 //System.out.println("Processing Call "+bblock.nodes.get(ppoint.getIndex()).toString().replace(' ','_'));
97 startindex=ppoint.getIndex()+1;
98 delta=applyCallDelta(delta, bblock);
99 //System.out.println("Generating:");
102 Graph graph=bbgraphMap.get(bblock);
103 Graph nodeGraph=null;
104 boolean init=delta.getInit();
105 if (!init&&delta.isEmpty())
108 //Compute delta at exit of each node
109 for(int i=startindex; i<nodes.size();i++) {
110 FlatNode currNode=nodes.get(i);
111 //System.out.println("Start Processing "+currNode);
112 if (!graphMap.containsKey(currNode)) {
113 if (isNEEDED(currNode))
114 graphMap.put(currNode, new Graph(graph));
117 //base graph works for us
118 graphMap.put(currNode, new Graph(graph));
120 //just use previous graph
121 graphMap.put(currNode, graphMap.get(nodes.get(i-1)));
125 nodeGraph=graphMap.get(currNode);
126 delta=processNode(bblock, i, currNode, delta, nodeGraph);
127 //System.out.println("Processing "+currNode+" and generating delta:");
130 generateFinalDelta(bblock, delta, nodeGraph);
136 for(Map.Entry<BBlock, Graph> e:bbgraphMap.entrySet()) {
137 Graph g=e.getValue();
138 plotGraph(g,"BB"+e.getKey().nodes.get(0).toString().replace(' ','_'));
142 for(FlatMethod fm:blockMap.keySet()) {
143 System.out.println(fm.printMethod());
145 for(Map.Entry<FlatNode, Graph> e:graphMap.entrySet()) {
146 FlatNode fn=e.getKey();
147 Graph g=e.getValue();
148 plotGraph(g,"FN"+fn.toString()+debugindex);
154 void plotGraph(Graph g, String name) {
156 PrintWriter pw=new PrintWriter(new FileWriter(name.toString().replace(' ','_')+".dot"));
157 g.printGraph(pw, name);
159 } catch (Exception ex) {
160 ex.printStackTrace();
165 /* This function builds the last delta for a basic block. It
166 * handles the case for the first time the basic block is
169 void buildInitDelta(Graph graph, Delta newDelta) {
170 //First compute the set of temps
171 HashSet<TempDescriptor> tmpSet=new HashSet<TempDescriptor>();
172 tmpSet.addAll(graph.varMap.keySet());
173 tmpSet.addAll(graph.parent.varMap.keySet());
175 //Next build the temp map part of the delta
176 for(TempDescriptor tmp:tmpSet) {
177 MySet<Edge> edgeSet=new MySet<Edge>();
179 if (graph.varMap.containsKey(tmp))
180 edgeSet.addAll(graph.varMap.get(tmp));
182 edgeSet.addAll(graph.parent.varMap.get(tmp));
183 newDelta.varedgeadd.put(tmp, edgeSet);
186 //Next compute the set of src allocnodes
187 HashSet<AllocNode> nodeSet=new HashSet<AllocNode>();
188 nodeSet.addAll(graph.nodeMap.keySet());
189 nodeSet.addAll(graph.parent.nodeMap.keySet());
191 for(AllocNode node:nodeSet) {
192 MySet<Edge> edgeSet=new MySet<Edge>();
194 if (graph.nodeMap.containsKey(node))
195 edgeSet.addAll(graph.nodeMap.get(node));
197 edgeSet.addAll(graph.parent.nodeMap.get(node));
198 newDelta.heapedgeadd.put(node, edgeSet);
201 if (graph.oldNodes.containsKey(node)) {
203 } else if (graph.parent.oldNodes.containsKey(node)) {
204 //parent graphs only contain true...no need to check
205 newDelta.addOldNodes.put(node, Boolean.TRUE);
209 for(AllocNode node:graph.oldNodes.keySet()) {
210 if (graph.oldNodes.get(node).booleanValue())
211 newDelta.addOldNodes.put(node, Boolean.TRUE);
214 for(AllocNode node:graph.parent.oldNodes.keySet()) {
215 //make sure child doesn't override
216 if (!graph.oldNodes.containsKey(node))
217 newDelta.addOldNodes.put(node, Boolean.TRUE);
220 newDelta.addNodeAges.addAll(graph.nodeAges);
221 newDelta.addNodeAges.addAll(graph.parent.nodeAges);
224 /* This function build the delta for the exit of a basic block. */
226 void generateFinalDelta(BBlock bblock, Delta delta, Graph graph) {
227 Delta newDelta=new Delta(null, false);
228 if (delta.getInit()) {
229 buildInitDelta(graph, newDelta);
231 /* We can break the old delta...it is done being used */
232 /* First we will build variable edges */
233 HashSet<TempDescriptor> tmpSet=new HashSet<TempDescriptor>();
234 tmpSet.addAll(delta.basevaredge.keySet());
235 tmpSet.addAll(delta.varedgeadd.keySet());
236 for(TempDescriptor tmp:tmpSet) {
237 /* Start with the new incoming edges */
238 MySet<Edge> newbaseedge=delta.basevaredge.get(tmp);
239 /* Remove the remove set */
240 if (newbaseedge==null)
241 newbaseedge=new MySet<Edge>();
242 newbaseedge.removeAll(delta.varedgeremove.get(tmp));
243 /* Add in the new set*/
244 newbaseedge.addAll(delta.varedgeadd.get(tmp));
245 /* Store the results */
246 newDelta.varedgeadd.put(tmp, newbaseedge);
248 delta.basevaredge.clear();
250 /* Next we build heap edges */
251 HashSet<AllocNode> nodeSet=new HashSet<AllocNode>();
252 nodeSet.addAll(delta.baseheapedge.keySet());
253 nodeSet.addAll(delta.heapedgeadd.keySet());
254 nodeSet.addAll(delta.heapedgeremove.keySet());
255 for(AllocNode node:nodeSet) {
256 /* Start with the new incoming edges */
257 MySet<Edge> newheapedge=new MySet<Edge>(delta.baseheapedge.get(node));
258 /* Remove the remove set */
259 MySet<Edge> removeset=delta.heapedgeremove.get(node);
262 newheapedge.removeAll(removeset);
264 /* Add in the add set */
265 MySet<Edge> settoadd=delta.heapedgeadd.get(node);
267 newheapedge.addAll(settoadd);
268 newDelta.heapedgeadd.put(node, newheapedge);
270 /* Remove the newly created edges..no need to propagate a diff for those */
271 if (removeset!=null) {
272 removeset.removeAll(delta.baseheapedge.get(node));
273 newDelta.heapedgeremove.put(node, removeset);
277 /* Compute new ages */
278 newDelta.addNodeAges.addAll(delta.baseNodeAges);
279 newDelta.addNodeAges.addAll(delta.addNodeAges);
280 HashSet<AllocNode> oldNodes=new HashSet<AllocNode>();
282 /* Compute whether old nodes survive */
283 oldNodes.addAll(delta.baseOldNodes.keySet());
284 oldNodes.addAll(delta.addOldNodes.keySet());
285 for(AllocNode node:oldNodes) {
286 if (delta.addOldNodes.containsKey(node)) {
287 if (delta.addOldNodes.get(node).booleanValue()) {
288 newDelta.addOldNodes.put(node, Boolean.TRUE);
291 if (delta.baseOldNodes.get(node).booleanValue()) {
292 newDelta.addOldNodes.put(node, Boolean.TRUE);
298 /* Now we need to propagate newdelta */
299 if (!newDelta.heapedgeadd.isEmpty()||!newDelta.heapedgeremove.isEmpty()||!newDelta.varedgeadd.isEmpty()||!newDelta.addNodeAges.isEmpty()||!newDelta.addOldNodes.isEmpty()) {
300 /* We have a delta to propagate */
301 if (returnMap.containsKey(bblock)) {
305 for(PPoint caller:returnMap.get(bblock)) {
306 //System.out.println("Sending Return BBlock to "+caller.getBBlock().nodes.get(caller.getIndex()).toString().replace(' ','_'));
309 newDelta.setBlock(caller);
310 toprocess.add(newDelta);
313 Delta d=newDelta.diffBlock(caller);
319 Vector<BBlock> blockvector=bblock.next();
320 for(int i=0;i<blockvector.size();i++) {
321 //System.out.println("Sending BBlock to "+blockvector.get(i).nodes.get(0).toString().replace(' ','_'));
324 newDelta.setBlock(new PPoint(blockvector.get(i)));
325 toprocess.add(newDelta);
327 Delta d=newDelta.diffBlock(new PPoint(blockvector.get(i)));
333 //System.out.println("EMPTY DELTA");
334 //System.out.println("delta");
336 //System.out.println("newDelta");
341 boolean isNEEDED(FlatNode node) {
342 switch(node.kind()) {
343 case FKind.FlatSetFieldNode: {
344 FlatSetFieldNode n=(FlatSetFieldNode)node;
345 return n.getSrc().getType().isPtr();
347 case FKind.FlatSetElementNode: {
348 FlatSetElementNode n=(FlatSetElementNode)node;
349 return n.getSrc().getType().isPtr();
351 case FKind.FlatFieldNode: {
352 FlatFieldNode n=(FlatFieldNode)node;
353 return n.getDst().getType().isPtr();
355 case FKind.FlatElementNode: {
356 FlatElementNode n=(FlatElementNode)node;
357 return n.getDst().getType().isPtr();
363 Delta processNode(BBlock bblock, int index, FlatNode node, Delta delta, Graph newgraph) {
364 switch(node.kind()) {
366 return processNewNode((FlatNew)node, delta, newgraph);
367 case FKind.FlatFieldNode:
368 case FKind.FlatElementNode:
369 return processFieldElementNode(node, delta, newgraph);
370 case FKind.FlatCastNode:
371 case FKind.FlatOpNode:
372 case FKind.FlatReturnNode:
373 return processCopyNode(node, delta, newgraph);
374 case FKind.FlatSetFieldNode:
375 case FKind.FlatSetElementNode:
376 return processSetFieldElementNode(node, delta, newgraph);
377 case FKind.FlatMethod:
379 case FKind.FlatBackEdge:
380 case FKind.FlatGenReachNode:
381 case FKind.FlatSESEEnterNode:
382 case FKind.FlatSESEExitNode:
383 return processFlatNop(node, delta, newgraph);
385 return processFlatCall(bblock, index, (FlatCall) node, delta, newgraph);
387 throw new Error("Unrecognized node:"+node);
391 Delta processSESEEnter(FlatSESEEnterNode sese, Delta delta, Graph graph) {
392 for (TempDescriptor tmp:sese.getInVarSet()) {
393 Taint taint=Taint.factory(sese, null, tmp, null, sese, null);
394 if (delta.getInit()) {
395 MySet<Edge> edges=GraphManip.getEdges(graph, delta, tmp);
397 Edge newe=e.addTaint(taint);
398 delta.addVarEdge(newe);
401 MySet<Edge> edges=GraphManip.getDiffEdges(delta, tmp);
403 Edge newe=e.addTaint(taint);
404 delta.addVarEdge(newe);
408 applyDiffs(graph, delta);
413 /* This function compute the edges for the this variable for a
414 * callee if it exists. */
416 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) {
417 //Handle the this temp
419 MySet<Edge> edges=(oldnodeset!=null)?GraphManip.getDiffEdges(delta, tmpthis):GraphManip.getEdges(graph, delta, tmpthis);
420 newDelta.varedgeadd.put(tmpthis, (MySet<Edge>) edges.clone());
421 edgeset.addAll(edges);
423 AllocNode dstnode=e.dst;
424 if (!nodeset.contains(dstnode)&&(oldnodeset==null||!oldnodeset.contains(dstnode))) {
425 TypeDescriptor type=dstnode.getType();
426 if (!type.isArray()) {
427 targetSet.add(type.getClassDesc());
429 //arrays don't have code
430 targetSet.add(typeUtil.getClass(TypeUtil.ObjectClass));
432 nodeset.add(dstnode);
433 tovisit.add(dstnode);
439 /* This function compute the edges for a call's parameters. */
441 void processParams(Graph graph, Delta delta, Delta newDelta, HashSet<AllocNode> nodeset, Stack<AllocNode> tovisit, MySet<Edge> edgeset, FlatCall fcall, boolean diff) {
442 //Go through each temp
443 for(int i=0;i<fcall.numArgs();i++) {
444 TempDescriptor tmp=fcall.getArg(i);
445 MySet<Edge> edges=diff?GraphManip.getDiffEdges(delta, tmp):GraphManip.getEdges(graph, delta, tmp);
446 newDelta.varedgeadd.put(tmp, (MySet<Edge>) edges.clone());
447 edgeset.addAll(edges);
449 if (!nodeset.contains(e.dst)) {
457 /* This function computes the reachable nodes for a callee. */
459 void computeReachableNodes(Graph graph, Delta delta, Delta newDelta, HashSet<AllocNode> nodeset, Stack<AllocNode> tovisit, MySet<Edge> edgeset, HashSet<AllocNode> oldnodeset) {
460 while(!tovisit.isEmpty()) {
461 AllocNode node=tovisit.pop();
462 MySet<Edge> edges=GraphManip.getEdges(graph, delta, node);
463 if (!edges.isEmpty()) {
464 newDelta.heapedgeadd.put(node, edges);
465 edgeset.addAll(edges);
467 if (!nodeset.contains(e.dst)&&(oldnodeset==null||!oldnodeset.contains(e.dst))) {
476 HashSet<MethodDescriptor> computeTargets(FlatCall fcall, Delta newDelta) {
477 TempDescriptor tmpthis=fcall.getThis();
478 MethodDescriptor md=fcall.getMethod();
479 HashSet<MethodDescriptor> targets=new HashSet<MethodDescriptor>();
484 for(Edge e:newDelta.varedgeadd.get(tmpthis)) {
485 AllocNode node=e.dst;
486 ClassDescriptor cd=node.getType().getClassDesc();
487 //Figure out exact method called and add to set
488 MethodDescriptor calledmd=cd.getCalledMethod(md);
489 targets.add(calledmd);
495 void fixMapping(FlatCall fcall, HashSet<MethodDescriptor> targets, MySet<Edge> oldedgeset, Delta newDelta, BBlock callblock, int callindex) {
496 Delta basedelta=null;
497 TempDescriptor tmpthis=fcall.getThis();
499 for(MethodDescriptor calledmd:targets) {
500 FlatMethod fm=state.getMethodFlat(calledmd);
501 boolean newmethod=false;
504 HashMap<TempDescriptor, TempDescriptor> tmpMap=new HashMap<TempDescriptor, TempDescriptor>();
507 tmpMap.put(tmpthis, fm.getParameter(offset++));
509 for(int i=0;i<fcall.numArgs();i++) {
510 TempDescriptor tmp=fcall.getArg(i);
511 tmpMap.put(tmp,fm.getParameter(i+offset));
514 //Get basicblock for the method
515 BasicBlock block=getBBlock(fm);
518 if (!callMap.containsKey(fcall)) {
519 callMap.put(fcall, new HashSet<BBlock>());
522 Delta returnDelta=null;
523 if (!callMap.get(fcall).contains(block.getStart())) {
524 callMap.get(fcall).add(block.getStart());
528 if (!returnMap.containsKey(block.getExit())) {
529 returnMap.put(block.getExit(), new HashSet<PPoint>());
531 returnMap.get(block.getExit()).add(new PPoint(callblock, callindex));
533 if (bbgraphMap.containsKey(block.getExit())) {
534 //Need to push existing results to current node
535 if (returnDelta==null) {
536 returnDelta=new Delta(null, false);
537 Vector<FlatNode> exitblocknodes=block.getExit().nodes();
538 FlatExit fexit=(FlatExit)exitblocknodes.get(exitblocknodes.size()-1);
539 buildInitDelta(graphMap.get(fexit), returnDelta);
540 if (!returnDelta.heapedgeadd.isEmpty()||!returnDelta.heapedgeremove.isEmpty()||!returnDelta.varedgeadd.isEmpty()) {
541 returnDelta.setBlock(new PPoint(callblock, callindex));
542 toprocess.add(returnDelta);
545 if (!returnDelta.heapedgeadd.isEmpty()||!returnDelta.heapedgeremove.isEmpty()||!returnDelta.varedgeadd.isEmpty()) {
546 toprocess.add(returnDelta.diffBlock(new PPoint(callblock, callindex)));
552 if (oldedgeset==null) {
553 //First build of this graph
554 //Build and enqueue delta...safe to just use existing delta
555 Delta d=newDelta.changeParams(tmpMap, new PPoint(block.getStart()));
556 //System.out.println("AProcessing "+block.getStart().nodes.get(0).toString().replace(' ','_'));
559 } else if (newmethod) {
560 if (basedelta==null) {
561 basedelta=newDelta.buildBase(oldedgeset);
563 //Build and enqueue delta
564 Delta d=basedelta.changeParams(tmpMap, new PPoint(block.getStart()));
565 //System.out.println("BProcessing "+block.getStart().nodes.get(0).toString().replace(' ','_'));
569 //Build and enqueue delta
570 Delta d=newDelta.changeParams(tmpMap, new PPoint(block.getStart()));
571 //System.out.println("CProcessing "+block.getStart().nodes.get(0).toString().replace(' ','_'));
579 /* This function computes all edges that start outside of the callee
580 * context and go into the callee context */
582 void computeExternalEdges(Graph graph, Delta delta, HashSet<AllocNode> nodeset, HashSet<AllocNode> deltaset, MySet<Edge> externaledgeset) {
583 //Do heap edges first
584 HashSet<AllocNode> externalnodes=new HashSet<AllocNode>();
585 externalnodes.addAll(delta.baseheapedge.keySet());
586 externalnodes.addAll(delta.heapedgeadd.keySet());
587 externalnodes.addAll(delta.heapedgeremove.keySet());
588 //remove allinternal nodes
589 externalnodes.removeAll(nodeset);
590 for(AllocNode extNode:externalnodes) {
591 //Compute set of edges from given node
592 MySet<Edge> edges=new MySet<Edge>(delta.baseheapedge.get(extNode));
593 edges.removeAll(delta.heapedgeremove.get(extNode));
594 edges.addAll(delta.heapedgeadd.get(extNode));
597 if (nodeset.contains(e.dst))
598 externaledgeset.add(e);
603 HashSet<TempDescriptor> temps=new HashSet<TempDescriptor>();
604 temps.addAll(delta.basevaredge.keySet());
605 temps.addAll(delta.varedgeadd.keySet());
606 temps.addAll(delta.varedgeremove.keySet());
607 //remove allinternal nodes
608 temps.removeAll(nodeset);
610 for(TempDescriptor tmp:temps) {
611 //Compute set of edges from given node
612 MySet<Edge> edges=new MySet<Edge>(delta.basevaredge.get(tmp));
614 edges.removeAll(delta.varedgeremove.get(tmp));
615 edges.addAll(delta.varedgeadd.get(tmp));
618 if (nodeset.contains(e.dst))
619 externaledgeset.add(e);
624 /* This function removes the caller reachable edges from the
627 void removeEdges(Graph graph, Delta delta, HashSet<AllocNode> nodeset, MySet<Edge> edgeset, MySet<Edge> externaledgeset) {
628 //Want to remove the set of internal edges
629 for(Edge e:edgeset) {
630 if (e.src!=null&&!graph.callerEdges.contains(e)) {
631 delta.removeHeapEdge(e);
635 //Want to remove the set of external edges
636 for(Edge e:externaledgeset) {
637 //want to remove the set of internal edges
638 if (!graph.callerEdges.contains(e))
643 Delta processFlatCall(BBlock callblock, int callindex, FlatCall fcall, Delta delta, Graph graph) {
644 Delta newDelta=new Delta(null, false);
646 if (delta.getInit()) {
647 MySet<Edge> edgeset=new MySet<Edge>();
648 MySet<Edge> externaledgeset=new MySet<Edge>();
649 HashSet<AllocNode> nodeset=new HashSet<AllocNode>();
650 HashSet<ClassDescriptor> targetSet=new HashSet<ClassDescriptor>();
651 Stack<AllocNode> tovisit=new Stack<AllocNode>();
652 TempDescriptor tmpthis=fcall.getThis();
653 graph.callerEdges=new MySet<Edge>();
655 //Handle the this temp
656 processThisTargets(targetSet, graph, delta, newDelta, nodeset, tovisit, edgeset, tmpthis, null);
658 //Go through each temp
659 processParams(graph, delta, newDelta, nodeset, tovisit, edgeset, fcall, false);
661 //Traverse all reachable nodes
662 computeReachableNodes(graph, delta, newDelta, nodeset, tovisit, edgeset, null);
664 //Compute call targets
665 HashSet<MethodDescriptor> newtargets=computeTargets(fcall, newDelta);
668 fixMapping(fcall, newtargets, null, newDelta, callblock, callindex);
670 //Compute edges into region to splice out
671 computeExternalEdges(graph, delta, nodeset, null, externaledgeset);
673 //Splice out internal edges
674 removeEdges(graph, delta, nodeset, edgeset, externaledgeset);
676 //store data structures
677 graph.externalEdgeSet=externaledgeset;
678 graph.reachNode=nodeset;
679 graph.reachEdge=edgeset;
681 graph.callTargets=newtargets;
682 graph.callNodeAges=new HashSet<AllocNode>();
683 graph.callOldNodes=new HashSet<AllocNode>();
685 //Apply diffs to graph
686 applyDiffs(graph, delta, true);
688 MySet<Edge> edgeset=new MySet<Edge>();
689 MySet<Edge> externaledgeset=new MySet<Edge>();
690 HashSet<AllocNode> nodeset=new HashSet<AllocNode>();
691 MySet<Edge> oldedgeset=graph.reachEdge;
692 HashSet<AllocNode> oldnodeset=graph.reachNode;
694 HashSet<ClassDescriptor> targetSet=new HashSet<ClassDescriptor>();
695 Stack<AllocNode> tovisit=new Stack<AllocNode>();
696 TempDescriptor tmpthis=fcall.getThis();
697 //Fix up delta to get rid of unnecessary heap edge removals
698 for(Map.Entry<AllocNode, MySet<Edge>> entry:delta.heapedgeremove.entrySet()) {
699 for(Iterator<Edge> eit=entry.getValue().iterator();eit.hasNext();) {
701 if (graph.callerEdges.contains(e))
706 //Fix up delta to get rid of unnecessary var edge removals
707 for(Map.Entry<TempDescriptor, MySet<Edge>> entry:delta.varedgeremove.entrySet()) {
708 for(Iterator<Edge> eit=entry.getValue().iterator();eit.hasNext();) {
710 if (graph.callerEdges.contains(e))
715 //Handle the this temp
716 processThisTargets(targetSet, graph, delta, newDelta, nodeset, tovisit, edgeset, tmpthis, oldnodeset);
718 //Go through each temp
719 processParams(graph, delta, newDelta, nodeset, tovisit, edgeset, fcall, true);
720 //Go through each new heap edge that starts from old node
721 MySet<Edge> newedges=GraphManip.getDiffEdges(delta, oldnodeset);
722 edgeset.addAll(newedges);
723 for(Edge e:newedges) {
724 //Add new edges that start from old node to newDelta
726 if (!newDelta.heapedgeadd.containsKey(src)) {
727 newDelta.heapedgeadd.put(src, new MySet<Edge>());
729 newDelta.heapedgeadd.get(src).add(e);
730 if (!nodeset.contains(e.dst)&&!oldnodeset.contains(e.dst)) {
736 //Traverse all reachable nodes
737 computeReachableNodes(graph, delta, newDelta, nodeset, tovisit, edgeset, oldnodeset);
738 //Compute call targets
739 HashSet<MethodDescriptor> newtargets=computeTargets(fcall, newDelta);
740 graph.callTargets.addAll(newtargets);
741 //add in new nodeset and edgeset
742 oldnodeset.addAll(nodeset);
743 oldedgeset.addAll(edgeset);
745 fixMapping(fcall, graph.callTargets, oldedgeset, newDelta, callblock, callindex);
746 //Compute edges into region to splice out
747 computeExternalEdges(graph, delta, oldnodeset, nodeset, externaledgeset);
749 //Splice out internal edges
750 removeEdges(graph, delta, nodeset, edgeset, externaledgeset);
752 //Add external edges back in
753 processCallExternal(graph, delta, externaledgeset);
755 //Move new edges that should be summarized
756 processSummarization(graph, delta);
758 //Add in new external edges
759 graph.externalEdgeSet.addAll(externaledgeset);
760 //Apply diffs to graph
761 applyDiffs(graph, delta);
766 void processSummarization(Graph graph, Delta delta) {
767 processSumHeapEdgeSet(delta.heapedgeadd, delta, graph);
768 processSumHeapEdgeSet(delta.baseheapedge, delta, graph);
769 processSumVarEdgeSet(delta.varedgeadd, delta, graph);
770 processSumVarEdgeSet(delta.basevaredge, delta, graph);
773 void processSumVarEdgeSet(HashMap<TempDescriptor, MySet<Edge>> map, Delta delta, Graph graph) {
774 MySet<Edge> edgestoadd=new MySet<Edge>();
775 MySet<Edge> edgestoremove=new MySet<Edge>();
776 for(Iterator<Map.Entry<TempDescriptor, MySet<Edge>>> eit=map.entrySet().iterator();eit.hasNext();) {
777 Map.Entry<TempDescriptor, MySet<Edge>> entry=eit.next();
778 MySet<Edge> edgeset=entry.getValue();
780 for(Edge e:edgeset) {
782 boolean rewrite=false;
783 if (copy.dst!=null&&graph.callNodeAges.contains(copy.dst)) {
784 copy.dst=allocFactory.getAllocNode(copy.dst, true);
788 edgestoremove.add(e);
789 edgestoadd.add(copy);
793 for(Edge e:edgestoremove) {
794 delta.removeVarEdge(e);
796 for(Edge e:edgestoadd) {
801 void processSumHeapEdgeSet(HashMap<AllocNode, MySet<Edge>> map, Delta delta, Graph graph) {
802 MySet<Edge> edgestoadd=new MySet<Edge>();
803 MySet<Edge> edgestoremove=new MySet<Edge>();
804 for(Iterator<Map.Entry<AllocNode, MySet<Edge>>> eit=map.entrySet().iterator();eit.hasNext();) {
805 Map.Entry<AllocNode, MySet<Edge>> entry=eit.next();
806 AllocNode node=entry.getKey();
807 MySet<Edge> edgeset=entry.getValue();
809 for(Edge e:edgeset) {
811 boolean rewrite=false;
812 if (copy.src!=null&&graph.callNodeAges.contains(copy.src)) {
813 copy.src=allocFactory.getAllocNode(copy.src, true);
816 if (copy.dst!=null&&graph.callNodeAges.contains(copy.dst)) {
817 copy.dst=allocFactory.getAllocNode(copy.dst, true);
821 edgestoremove.add(e);
822 edgestoadd.add(copy);
826 for(Edge e:edgestoremove) {
827 delta.removeHeapEdge(e);
829 for(Edge e:edgestoadd) {
830 delta.addHeapEdge(e);
834 //Handle external edges
835 void processCallExternal(Graph graph, Delta newDelta, MySet<Edge> externalEdgeSet) {
836 //Add external edges in
837 for(Edge e:externalEdgeSet) {
838 //First did we age the source
839 Edge newedge=e.copy();
840 if (newedge.src!=null&&!e.src.isSummary()&&graph.callNodeAges.contains(e.src)) {
841 AllocNode summaryNode=allocFactory.getAllocNode(newedge.src, true);
842 newedge.src=summaryNode;
845 if (graph.callNodeAges.contains(e.dst)&&!e.dst.isSummary()) {
846 if (graph.callOldNodes.contains(e.dst)) {
848 Edge copy=newedge.copy();
849 mergeEdge(graph, newDelta, copy);
851 //Now add summarized node
852 newedge.dst=allocFactory.getAllocNode(newedge.dst, true);
853 mergeCallEdge(graph, newDelta, newedge);
855 //Add edge to single node
856 mergeEdge(graph, newDelta, newedge);
861 /* This function applies callee deltas to the caller heap. */
863 Delta applyCallDelta(Delta delta, BBlock bblock) {
864 Delta newDelta=new Delta(null, false);
865 Vector<FlatNode> nodes=bblock.nodes();
866 PPoint ppoint=delta.getBlock();
867 FlatCall fcall=(FlatCall)nodes.get(ppoint.getIndex());
868 Graph graph=graphMap.get(fcall);
869 Graph oldgraph=(ppoint.getIndex()==0)?
870 bbgraphMap.get(bblock):
871 graphMap.get(nodes.get(ppoint.getIndex()-1));
873 //Age outside nodes if necessary
874 for(Iterator<AllocNode> nodeit=delta.addNodeAges.iterator();nodeit.hasNext();) {
875 AllocNode node=nodeit.next();
876 if (!graph.callNodeAges.contains(node)) {
877 graph.callNodeAges.add(node);
878 newDelta.addNodeAges.add(node);
880 if (!graph.reachNode.contains(node)&&!node.isSummary()) {
881 /* Need to age node in existing graph*/
882 summarizeInGraph(graph, newDelta, node);
886 for(Map.Entry<AllocNode, MySet<Edge>> entry:delta.heapedgeadd.entrySet()) {
887 for(Edge e:entry.getValue()) {
888 boolean addedge=false;
890 if (e.statuspredicate==Edge.NEW) {
893 Edge origEdgeKey=e.makeStatus(allocFactory);
894 if (oldgraph.nodeMap.containsKey(origEdgeKey.src)&&
895 oldgraph.nodeMap.get(origEdgeKey.src).contains(origEdgeKey)) {
896 Edge origEdge=oldgraph.nodeMap.get(origEdgeKey.src).get(origEdgeKey);
898 origEdgeKey.statuspredicate=origEdge.statuspredicate;
899 edgetoadd=origEdgeKey;
902 mergeCallEdge(graph, newDelta, edgetoadd);
906 processCallExternal(graph, newDelta, graph.externalEdgeSet);
908 //Add edge for return value
909 if (fcall.getReturnTemp()!=null) {
910 MySet<Edge> returnedge=delta.varedgeadd.get(returntmp);
911 if (returnedge!=null)
912 for(Edge e:returnedge) {
913 Edge newedge=e.copy();
914 newedge.srcvar=fcall.getReturnTemp();
915 if (graph.getEdges(fcall.getReturnTemp())==null||!graph.getEdges(fcall.getReturnTemp()).contains(newedge))
916 newDelta.addEdge(newedge);
919 applyDiffs(graph, newDelta);
923 public void mergeEdge(Graph graph, Delta newDelta, Edge edgetoadd) {
924 if (edgetoadd!=null) {
925 Edge match=graph.getMatch(edgetoadd);
927 if (match==null||!match.subsumes(edgetoadd)) {
928 Edge mergededge=edgetoadd.merge(match);
929 newDelta.addEdge(mergededge);
934 /* This is a call produced edge...need to remember this */
936 public void mergeCallEdge(Graph graph, Delta newDelta, Edge edgetoadd) {
937 if (edgetoadd!=null) {
938 Edge match=graph.getMatch(edgetoadd);
940 if (match==null||!match.subsumes(edgetoadd)) {
941 Edge mergededge=edgetoadd.merge(match);
942 newDelta.addEdge(mergededge);
943 graph.callerEdges.add(mergededge);
944 //System.out.println("ADDING: "+ mergededge);
950 /* Summarizes out of context nodes in graph */
951 void summarizeInGraph(Graph graph, Delta newDelta, AllocNode singleNode) {
952 AllocNode summaryNode=allocFactory.getAllocNode(singleNode, true);
954 //Handle outgoing heap edges
955 MySet<Edge> edgeset=graph.getEdges(singleNode);
957 for(Edge e:edgeset) {
958 Edge rewrite=e.rewrite(singleNode, summaryNode);
960 newDelta.removeHeapEdge(e);
961 mergeCallEdge(graph, newDelta, rewrite);
964 //Handle incoming edges
965 MySet<Edge> backedges=graph.getBackEdges(singleNode);
966 for(Edge e:backedges) {
967 if (e.dst==singleNode) {
968 //Need to get original edge so that predicate will be correct
969 Edge match=graph.getMatch(e);
971 Edge rewrite=match.rewrite(singleNode, summaryNode);
972 newDelta.removeEdge(match);
973 mergeCallEdge(graph, newDelta, rewrite);
979 void applyDiffs(Graph graph, Delta delta) {
980 applyDiffs(graph, delta, false);
983 void applyDiffs(Graph graph, Delta delta, boolean genbackwards) {
984 //build backwards map if requested
985 if (genbackwards&&graph.backMap==null) {
986 graph.backMap=new HashMap<AllocNode, MySet<Edge>>();
987 if (graph.parent.backMap==null) {
988 graph.parent.backMap=new HashMap<AllocNode, MySet<Edge>>();
989 for(Map.Entry<AllocNode, MySet<Edge>> entry:graph.nodeMap.entrySet()) {
990 for(Edge e:entry.getValue()) {
991 if (!graph.parent.backMap.containsKey(e.dst))
992 graph.parent.backMap.put(e.dst, new MySet<Edge>());
993 graph.parent.backMap.get(e.dst).add(e);
996 for(Map.Entry<TempDescriptor, MySet<Edge>> entry:graph.varMap.entrySet()) {
997 for(Edge e:entry.getValue()) {
998 if (!graph.parent.backMap.containsKey(e.dst))
999 graph.parent.backMap.put(e.dst, new MySet<Edge>());
1000 graph.parent.backMap.get(e.dst).add(e);
1006 //Add hidden base edges
1007 for(Map.Entry<AllocNode, MySet<Edge>> e: delta.baseheapedge.entrySet()) {
1008 AllocNode node=e.getKey();
1009 MySet<Edge> edges=e.getValue();
1010 if (graph.nodeMap.containsKey(node)) {
1011 MySet<Edge> nodeEdges=graph.nodeMap.get(node);
1012 nodeEdges.addAll(edges);
1017 for(Map.Entry<AllocNode, MySet<Edge>> e: delta.heapedgeremove.entrySet()) {
1018 AllocNode node=e.getKey();
1019 MySet<Edge> edgestoremove=e.getValue();
1020 if (graph.nodeMap.containsKey(node)) {
1021 //Just apply diff to current map
1022 graph.nodeMap.get(node).removeAll(edgestoremove);
1024 //Generate diff from parent graph
1025 MySet<Edge> parentedges=graph.parent.nodeMap.get(node);
1026 if (parentedges!=null) {
1027 MySet<Edge> newedgeset=Util.setSubtract(parentedges, edgestoremove);
1028 graph.nodeMap.put(node, newedgeset);
1034 for(Map.Entry<AllocNode, MySet<Edge>> e: delta.heapedgeadd.entrySet()) {
1035 AllocNode node=e.getKey();
1036 MySet<Edge> edgestoadd=e.getValue();
1037 //If we have not done a subtract, then
1038 if (!graph.nodeMap.containsKey(node)) {
1039 //Copy the parent entry
1040 if (graph.parent.nodeMap.containsKey(node))
1041 graph.nodeMap.put(node, (MySet<Edge>)graph.parent.nodeMap.get(node).clone());
1043 graph.nodeMap.put(node, new MySet<Edge>());
1045 graph.nodeMap.get(node).addAll(edgestoadd);
1047 for(Edge eadd:edgestoadd) {
1048 if (!graph.backMap.containsKey(eadd.dst))
1049 graph.backMap.put(eadd.dst, new MySet<Edge>());
1050 graph.backMap.get(eadd.dst).add(eadd);
1056 for(Map.Entry<TempDescriptor, MySet<Edge>> e: delta.varedgeremove.entrySet()) {
1057 TempDescriptor tmp=e.getKey();
1058 MySet<Edge> edgestoremove=e.getValue();
1060 if (graph.varMap.containsKey(tmp)) {
1061 //Just apply diff to current map
1062 graph.varMap.get(tmp).removeAll(edgestoremove);
1063 } else if (graph.parent.varMap.containsKey(tmp)) {
1064 //Generate diff from parent graph
1065 MySet<Edge> parentedges=graph.parent.varMap.get(tmp);
1066 MySet<Edge> newedgeset=Util.setSubtract(parentedges, edgestoremove);
1067 graph.varMap.put(tmp, newedgeset);
1072 for(Map.Entry<TempDescriptor, MySet<Edge>> e: delta.varedgeadd.entrySet()) {
1073 TempDescriptor tmp=e.getKey();
1074 MySet<Edge> edgestoadd=e.getValue();
1075 if (graph.varMap.containsKey(tmp)) {
1076 graph.varMap.get(tmp).addAll(edgestoadd);
1078 graph.varMap.put(tmp, (MySet<Edge>) edgestoadd.clone());
1080 for(Edge eadd:edgestoadd) {
1081 if (!graph.backMap.containsKey(eadd.dst))
1082 graph.backMap.put(eadd.dst, new MySet<Edge>());
1083 graph.backMap.get(eadd.dst).add(eadd);
1088 //Add node additions
1089 for(AllocNode node:delta.addNodeAges) {
1090 graph.nodeAges.add(node);
1093 for(Map.Entry<AllocNode, Boolean> nodeentry:delta.addOldNodes.entrySet()) {
1094 AllocNode node=nodeentry.getKey();
1095 Boolean ispresent=nodeentry.getValue();
1096 graph.oldNodes.put(node, ispresent);
1100 Delta processSetFieldElementNode(FlatNode node, Delta delta, Graph graph) {
1104 if (node.kind()==FKind.FlatSetElementNode) {
1105 FlatSetElementNode fen=(FlatSetElementNode) node;
1110 FlatSetFieldNode ffn=(FlatSetFieldNode) node;
1115 //Do nothing for non pointers
1116 if (!src.getType().isPtr())
1119 if (delta.getInit()) {
1120 HashSet<AllocNode> srcNodes=GraphManip.getNodes(graph, delta, src);
1121 HashSet<AllocNode> dstNodes=GraphManip.getNodes(graph, delta, dst);
1122 MySet<Edge> edgesToAdd=GraphManip.genEdges(dstNodes, fd, srcNodes);
1123 MySet<Edge> edgesToRemove=null;
1124 if (dstNodes.size()==1&&!dstNodes.iterator().next().isSummary()&&fd!=null) {
1125 /* Can do a strong update */
1126 edgesToRemove=GraphManip.getEdges(graph, delta, dstNodes, fd);
1127 graph.strongUpdateSet=edgesToRemove;
1129 graph.strongUpdateSet=new MySet<Edge>();
1131 updateHeapDelta(graph, delta, edgesToAdd, edgesToRemove);
1132 applyDiffs(graph, delta);
1134 /* First look at new sources */
1135 MySet<Edge> edgesToAdd=new MySet<Edge>();
1136 HashSet<AllocNode> newSrcNodes=GraphManip.getDiffNodes(delta, src);
1137 HashSet<AllocNode> srcNodes=GraphManip.getNodes(graph, delta, src);
1138 HashSet<AllocNode> dstNodes=GraphManip.getNodes(graph, delta, dst);
1139 HashSet<AllocNode> newDstNodes=GraphManip.getDiffNodes(delta, dst);
1142 MySet<Edge> edgesToRemove=null;
1143 if (newDstNodes.size()!=0) {
1144 if (dstNodes.size()>1&&!dstNodes.iterator().next().isSummary()&&fd!=null) {
1145 /* Need to undo strong update */
1146 if (graph.strongUpdateSet!=null) {
1147 edgesToAdd.addAll(graph.strongUpdateSet);
1148 graph.strongUpdateSet=null; //Prevent future strong updates
1150 } else if (dstNodes.size()==1&&newDstNodes.size()==1&&!newDstNodes.iterator().next().isSummary()&&graph.strongUpdateSet!=null&&fd!=null) {
1151 edgesToRemove=GraphManip.getEdges(graph, delta, dstNodes, fd);
1152 graph.strongUpdateSet.addAll(edgesToRemove);
1154 edgesToAdd.addAll(GraphManip.genEdges(newDstNodes, fd, srcNodes));
1158 if (graph.strongUpdateSet!=null&&fd!=null) {
1159 MySet<Edge> otherEdgesToRemove=GraphManip.getDiffEdges(delta, dstNodes, fd);
1160 if (edgesToRemove!=null)
1161 edgesToRemove.addAll(otherEdgesToRemove);
1163 edgesToRemove=otherEdgesToRemove;
1164 graph.strongUpdateSet.addAll(otherEdgesToRemove);
1167 //Next look at new destinations
1168 edgesToAdd.addAll(GraphManip.genEdges(dstNodes, fd, newSrcNodes));
1171 updateHeapDelta(graph, delta, edgesToAdd, edgesToRemove);
1172 applyDiffs(graph, delta);
1177 Delta processCopyNode(FlatNode node, Delta delta, Graph graph) {
1180 if (node.kind()==FKind.FlatOpNode) {
1181 FlatOpNode fon=(FlatOpNode) node;
1184 } else if (node.kind()==FKind.FlatReturnNode) {
1185 FlatReturnNode frn=(FlatReturnNode)node;
1186 src=frn.getReturnTemp();
1188 if (src==null||!src.getType().isPtr()) {
1190 applyDiffs(graph, delta);
1194 FlatCastNode fcn=(FlatCastNode) node;
1198 if (delta.getInit()) {
1199 HashSet<AllocNode> srcnodes=GraphManip.getNodes(graph, delta, src);
1200 MySet<Edge> edgesToAdd=GraphManip.genEdges(dst, srcnodes);
1201 MySet<Edge> edgesToRemove=GraphManip.getEdges(graph, delta, dst);
1202 updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
1203 applyDiffs(graph, delta);
1205 /* First compute new src nodes */
1206 HashSet<AllocNode> newSrcNodes=GraphManip.getDiffNodes(delta, src);
1208 /* Compute the union, and then the set of edges */
1209 MySet<Edge> edgesToAdd=GraphManip.genEdges(dst, newSrcNodes);
1211 /* Compute set of edges to remove */
1212 MySet<Edge> edgesToRemove=GraphManip.getDiffEdges(delta, dst);
1215 updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
1216 applyDiffs(graph, delta);
1221 Delta processFieldElementNode(FlatNode node, Delta delta, Graph graph) {
1225 if (node.kind()==FKind.FlatElementNode) {
1226 FlatElementNode fen=(FlatElementNode) node;
1231 FlatFieldNode ffn=(FlatFieldNode) node;
1236 //Do nothing for non pointers
1237 if (!dst.getType().isPtr())
1239 if (delta.getInit()) {
1240 HashSet<AllocNode> srcnodes=GraphManip.getNodes(graph, delta, src);
1241 HashSet<AllocNode> fdnodes=GraphManip.getNodes(graph, delta, srcnodes, fd);
1242 MySet<Edge> edgesToAdd=GraphManip.genEdges(dst, fdnodes);
1243 MySet<Edge> edgesToRemove=GraphManip.getEdges(graph, delta, dst);
1244 updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
1245 applyDiffs(graph, delta);
1247 /* First compute new objects we read fields of */
1248 HashSet<AllocNode> allsrcnodes=GraphManip.getNodes(graph, delta, src);
1249 HashSet<AllocNode> difffdnodes=GraphManip.getDiffNodes(delta, allsrcnodes, fd);
1250 /* Next compute new targets of fields */
1251 HashSet<AllocNode> newsrcnodes=GraphManip.getDiffNodes(delta, src);
1252 HashSet<AllocNode> newfdnodes=GraphManip.getNodes(graph, delta, newsrcnodes, fd);
1253 /* Compute the union, and then the set of edges */
1254 HashSet<AllocNode> newTargets=new HashSet<AllocNode>();
1255 newTargets.addAll(newfdnodes);
1256 newTargets.addAll(difffdnodes);
1257 MySet<Edge> edgesToAdd=GraphManip.genEdges(dst, newTargets);
1259 /* Compute set of edges to remove */
1260 MySet<Edge> edgesToRemove=GraphManip.getDiffEdges(delta, dst);
1263 updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
1264 applyDiffs(graph, delta);
1269 void updateVarDelta(Graph graph, Delta delta, TempDescriptor tmp, MySet<Edge> edgestoAdd, MySet<Edge> edgestoRemove) {
1270 MySet<Edge> edgeAdd=delta.varedgeadd.get(tmp);
1271 MySet<Edge> edgeRemove=delta.varedgeremove.get(tmp);
1272 MySet<Edge> existingEdges=graph.getEdges(tmp);
1273 for(Edge e: edgestoRemove) {
1274 //remove edge from delta
1277 //if the edge is already in the graph, add an explicit remove to the delta
1278 if (existingEdges.contains(e))
1279 delta.removeVarEdge(e);
1281 for(Edge e: edgestoAdd) {
1282 //Remove the edge from the remove set
1283 if (edgeRemove!=null)
1284 edgeRemove.remove(e);
1285 //Explicitly add it to the add set unless it is already in the graph
1286 if (!existingEdges.contains(e)&&typeUtil.isSuperorType(tmp.getType(),e.dst.getType()))
1287 delta.addVarEdge(e);
1291 void updateHeapDelta(Graph graph, Delta delta, MySet<Edge> edgestoAdd, MySet<Edge> edgestoRemove) {
1292 if (edgestoRemove!=null)
1293 for(Edge e: edgestoRemove) {
1294 AllocNode src=e.src;
1295 MySet<Edge> edgeAdd=delta.heapedgeadd.get(src);
1296 MySet<Edge> existingEdges=graph.getEdges(src);
1297 //remove edge from delta
1300 //if the edge is already in the graph, add an explicit remove to the delta
1301 if (existingEdges.contains(e)) {
1302 delta.removeHeapEdge(e);
1305 if (edgestoAdd!=null)
1306 for(Edge e: edgestoAdd) {
1307 AllocNode src=e.src;
1308 MySet<Edge> edgeRemove=delta.heapedgeremove.get(src);
1309 MySet<Edge> existingEdges=graph.getEdges(src);
1310 //Remove the edge from the remove set
1311 if (edgeRemove!=null)
1312 edgeRemove.remove(e);
1313 //Explicitly add it to the add set unless it is already in the graph
1314 if (!existingEdges.contains(e)||!existingEdges.get(e).isNew()) {
1315 delta.addHeapEdge(e);
1320 Delta processFlatNop(FlatNode node, Delta delta, Graph graph) {
1321 applyDiffs(graph, delta);
1325 Delta processNewNode(FlatNew node, Delta delta, Graph graph) {
1326 AllocNode summary=allocFactory.getAllocNode(node, true);
1327 AllocNode single=allocFactory.getAllocNode(node, false);
1328 TempDescriptor tmp=node.getDst();
1330 if (delta.getInit()) {
1331 /* We don't have to deal with summarization here... The
1332 * intuition is that this is the only place where we generate
1333 * nodes for this allocation site and this is the first time
1334 * we've analyzed this site */
1337 Edge e=new Edge(tmp, single);
1338 //Build new Edge set
1339 MySet<Edge> newedges=new MySet<Edge>();
1341 //Add it into the diffs
1342 delta.varedgeadd.put(tmp, newedges);
1343 //Remove the old edges
1344 MySet<Edge> oldedges=graph.getEdges(tmp);
1345 if (!oldedges.isEmpty())
1346 delta.varedgeremove.put(tmp, (MySet<Edge>) oldedges);
1347 //Apply incoming diffs to graph
1348 applyDiffs(graph, delta);
1349 //Note that we create a single node
1350 delta.addNodeAges.add(single);
1352 if (delta.addOldNodes.containsKey(single)||delta.baseOldNodes.containsKey(single)) {
1353 delta.addOldNodes.put(single, Boolean.FALSE);
1356 /* 1. Fix up the variable edge additions */
1358 for(Iterator<Map.Entry<TempDescriptor, MySet<Edge>>> entryIt=delta.varedgeadd.entrySet().iterator();entryIt.hasNext();) {
1359 Map.Entry<TempDescriptor, MySet<Edge>> entry=entryIt.next();
1361 if (entry.getKey()==tmp) {
1362 /* Check if this is the tmp we overwrite */
1365 /* Otherwise, check if the target of the edge is changed... */
1366 summarizeSet(entry.getValue(), graph.varMap.get(entry.getKey()), single, summary);
1370 /* 2. Fix up the base variable edges */
1372 for(Iterator<Map.Entry<TempDescriptor, MySet<Edge>>> entryIt=delta.basevaredge.entrySet().iterator();entryIt.hasNext();) {
1373 Map.Entry<TempDescriptor, MySet<Edge>> entry=entryIt.next();
1374 TempDescriptor entrytmp=entry.getKey();
1375 if (entrytmp==tmp) {
1376 /* Check is this is the tmp we overwrite, if so add to remove set */
1377 Util.relationUpdate(delta.varedgeremove, tmp, null, entry.getValue());
1379 /* Check if the target of the edge is changed */
1380 MySet<Edge> newset=(MySet<Edge>)entry.getValue().clone();
1381 MySet<Edge> removeset=shrinkSet(newset, graph.varMap.get(entrytmp), single, summary);
1382 Util.relationUpdate(delta.varedgeremove, entrytmp, newset, removeset);
1383 Util.relationUpdate(delta.varedgeadd, entrytmp, null, newset);
1388 /* 3. Fix up heap edge additions */
1390 HashMap<AllocNode, MySet<Edge>> addheapedge=new HashMap<AllocNode, MySet<Edge>>();
1391 for(Iterator<Map.Entry<AllocNode, MySet<Edge>>> entryIt=delta.heapedgeadd.entrySet().iterator();entryIt.hasNext();) {
1392 Map.Entry<AllocNode, MySet<Edge>> entry=entryIt.next();
1393 MySet<Edge> edgeset=entry.getValue();
1394 AllocNode allocnode=entry.getKey();
1395 if (allocnode==single) {
1397 summarizeSet(edgeset, graph.nodeMap.get(summary), single, summary);
1398 addheapedge.put(summary, edgeset);
1400 summarizeSet(edgeset, graph.nodeMap.get(allocnode), single, summary);
1404 /* Merge in diffs */
1406 for(Map.Entry<AllocNode, MySet<Edge>> entry:addheapedge.entrySet()) {
1407 AllocNode allocnode=entry.getKey();
1408 Util.relationUpdate(delta.heapedgeadd, allocnode, null, entry.getValue());
1411 /* 4. Fix up the base heap edges */
1413 for(Iterator<Map.Entry<AllocNode, MySet<Edge>>> entryIt=delta.baseheapedge.entrySet().iterator();entryIt.hasNext();) {
1414 Map.Entry<AllocNode, MySet<Edge>> entry=entryIt.next();
1415 MySet<Edge> edgeset=entry.getValue();
1416 AllocNode allocnode=entry.getKey();
1417 if (allocnode==single) {
1420 AllocNode addnode=(allocnode==single)?summary:allocnode;
1422 MySet<Edge> newset=(MySet<Edge>) edgeset.clone();
1423 MySet<Edge> removeset=shrinkSet(newset, graph.nodeMap.get(addnode), single, summary);
1424 Util.relationUpdate(delta.heapedgeadd, addnode, null, newset);
1425 Util.relationUpdate(delta.heapedgeremove, allocnode, null, removeset);
1428 /* Update Node Ages...If the base or addNodeAges set contains a
1429 * single node, it now should also contain a summary node... No
1430 * need to generate a single node as that has already been
1432 if (delta.baseNodeAges.contains(single)||delta.addNodeAges.contains(single)) {
1433 delta.addNodeAges.add(summary);
1436 //Kill the old node if someone tries to add it
1437 if (delta.addOldNodes.containsKey(single)||delta.baseOldNodes.containsKey(single)) {
1438 delta.addOldNodes.put(single, Boolean.FALSE);
1441 //Apply incoming diffs to graph
1442 applyDiffs(graph, delta);
1447 /* This function builds a new edge set where oldnode is summarized into new node */
1449 void summarizeSet(MySet<Edge> edgeset, MySet<Edge> oldedgeset, AllocNode oldnode, AllocNode sumnode) {
1450 MySet<Edge> newSet=null;
1451 for(Iterator<Edge> edgeit=edgeset.iterator();edgeit.hasNext();) {
1452 Edge e=edgeit.next();
1453 if (e.dst==oldnode||e.src==oldnode) {
1455 newSet=new MySet<Edge>();
1460 if (e.dst==oldnode) {
1463 if (e.src==oldnode) {
1466 if (oldedgeset==null||!oldedgeset.contains(e))
1471 edgeset.addAll(newSet);
1474 /* Shrinks the incoming set to just include rewritten values.
1475 * Returns a set of the original rewritten values */
1477 MySet<Edge> shrinkSet(MySet<Edge> edgeset, MySet<Edge> oldedgeset, AllocNode oldnode, AllocNode newnode) {
1478 MySet<Edge> newSet=null;
1479 MySet<Edge> removeSet=null;
1480 for(Iterator<Edge> edgeit=edgeset.iterator();edgeit.hasNext();) {
1481 Edge e=edgeit.next();
1483 if (e.dst==oldnode||e.src==oldnode) {
1485 newSet=new MySet<Edge>();
1486 removeSet=new MySet<Edge>();
1495 if (oldedgeset==null||!oldedgeset.contains(e))
1500 edgeset.addAll(newSet);
1504 /* This function returns a completely new Delta... It is safe to
1507 Delta applyInitDelta(Delta delta, BBlock block) {
1508 //Apply delta to graph
1509 boolean newGraph=false;
1510 if (!bbgraphMap.containsKey(block)) {
1511 bbgraphMap.put(block, new Graph(null));
1514 Graph graph=bbgraphMap.get(block);
1517 Delta newdelta=new Delta(null, true);
1518 //Add in heap edges and throw away original diff
1520 for(Map.Entry<AllocNode, MySet<Edge>> entry:delta.heapedgeadd.entrySet()) {
1521 graph.nodeMap.put(entry.getKey(), new MySet<Edge>(entry.getValue()));
1523 //Add in var edges and throw away original diff
1524 Set<TempDescriptor> livetemps=bblivetemps.get(block);
1526 for(Map.Entry<TempDescriptor, MySet<Edge>> entry:delta.varedgeadd.entrySet()) {
1527 if (livetemps.contains(entry.getKey()))
1528 graph.varMap.put(entry.getKey(), new MySet<Edge>(entry.getValue()));
1530 //Record that this is initial set...
1531 graph.nodeAges.addAll(delta.addNodeAges);
1533 for(Map.Entry<AllocNode, Boolean> oldentry:delta.addOldNodes.entrySet()) {
1534 if (oldentry.getValue().booleanValue()) {
1535 graph.oldNodes.put(oldentry.getKey(), Boolean.TRUE);
1540 Delta newdelta=new Delta(null, false);
1541 //merge in heap edges and variables
1542 mergeHeapEdges(graph, delta, newdelta);
1543 mergeVarEdges(graph, delta, newdelta, block);
1544 mergeAges(graph, delta, newdelta);
1549 /* This function merges in the heap edges. It updates delta to be
1552 void mergeHeapEdges(Graph graph, Delta delta, Delta newdelta) {
1554 for(Map.Entry<AllocNode, MySet<Edge>> heapedge:delta.heapedgeadd.entrySet()) {
1555 AllocNode nsrc=heapedge.getKey();
1556 MySet<Edge> edges=heapedge.getValue();
1558 if (graph.backMap!=null) {
1560 if (!graph.backMap.containsKey(e.dst))
1561 graph.backMap.put(e.dst, new MySet<Edge>());
1562 graph.backMap.get(e.dst).add(e);
1566 if (!graph.nodeMap.containsKey(nsrc)) {
1567 graph.nodeMap.put(nsrc, new MySet<Edge>());
1569 MySet<Edge> dstedges=graph.nodeMap.get(nsrc);
1570 MySet<Edge> diffedges=new MySet<Edge>();
1572 if (!dstedges.contains(e)) {
1573 //We have a new edge
1577 Edge origedge=dstedges.get(e);
1578 if (!origedge.subsumes(e)) {
1579 Edge mergededge=origedge.merge(e);
1580 diffedges.add(mergededge);
1581 dstedges.add(mergededge);
1585 //Done with edge set...
1586 if (diffedges.size()>0) {
1588 newdelta.baseheapedge.put(nsrc, diffedges);
1593 /* This function merges in the var edges. It updates delta to be
1596 void mergeVarEdges(Graph graph, Delta delta, Delta newdelta, BBlock block) {
1598 Set<TempDescriptor> livetemps=bblivetemps.get(block);
1600 for(Map.Entry<TempDescriptor, MySet<Edge>> varedge:delta.varedgeadd.entrySet()) {
1601 TempDescriptor tmpsrc=varedge.getKey();
1602 if (livetemps.contains(tmpsrc)) {
1603 MySet<Edge> edges=varedge.getValue();
1604 if (graph.backMap!=null) {
1606 if (!graph.backMap.containsKey(e.dst))
1607 graph.backMap.put(e.dst, new MySet<Edge>());
1608 graph.backMap.get(e.dst).add(e);
1612 if (!graph.varMap.containsKey(tmpsrc)) {
1613 graph.varMap.put(tmpsrc, new MySet<Edge>());
1615 MySet<Edge> dstedges=graph.varMap.get(tmpsrc);
1616 MySet<Edge> diffedges=new MySet<Edge>();
1618 if (!dstedges.contains(e)) {
1619 //We have a new edge
1624 //Done with edge set...
1625 if (diffedges.size()>0) {
1627 newdelta.basevaredge.put(tmpsrc,diffedges);
1633 void mergeAges(Graph graph, Delta delta, Delta newDelta) {
1635 for(AllocNode node:delta.addNodeAges) {
1636 if (!graph.nodeAges.contains(node)) {
1637 graph.nodeAges.add(node);
1638 newDelta.baseNodeAges.add(node);
1641 for(Map.Entry<AllocNode, Boolean> oldentry:delta.addOldNodes.entrySet()) {
1642 AllocNode node=oldentry.getKey();
1643 boolean ispresent=oldentry.getValue().booleanValue();
1644 if (ispresent&&!graph.oldNodes.containsKey(node)) {
1645 graph.oldNodes.put(node, Boolean.TRUE);
1646 newDelta.baseOldNodes.put(node, Boolean.TRUE);