more changes
[IRC.git] / Robust / src / Analysis / Pointer / Pointer.java
1 package Analysis.Pointer;
2 import java.util.*;
3 import IR.Flat.*;
4 import IR.*;
5 import Analysis.Pointer.BasicBlock.BBlock;
6 import Analysis.Pointer.AllocFactory.AllocNode;
7
8 public class Pointer {
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;
14
15   State state;
16   TypeUtil typeUtil;
17   AllocFactory allocFactory;
18   LinkedList<Delta> toprocess;
19   TempDescriptor returntmp;
20
21   public Pointer(State state, TypeUtil typeUtil) {
22     this.state=state;
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);
33   }
34
35   public BasicBlock getBBlock(FlatMethod fm) {
36     if (!blockMap.containsKey(fm))
37       blockMap.put(fm, BasicBlock.getBBlock(fm));
38     return blockMap.get(fm);
39   }
40   
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);
55     return delta;
56   }
57
58   public void doAnalysis() {
59     toprocess.add(buildInitialContext());
60
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();
66
67       //Build base graph for entrance to this basic block
68       delta=applyInitDelta(delta, bblock);
69       Graph graph=bbgraphMap.get(bblock);
70
71       Graph nodeGraph=null;
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));
77         }
78         nodeGraph=graphMap.get(currNode);
79         delta=processNode(bblock, i, currNode, delta, nodeGraph);
80       }
81       generateFinalDelta(bblock, delta, nodeGraph);
82     }
83   }
84
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());
90     
91     //Next build the temp map part of the delta
92     for(TempDescriptor tmp:tmpSet) {
93       MySet<Edge> edgeSet=new MySet<Edge>();
94       /* Get target set */
95       if (graph.varMap.containsKey(tmp))
96         edgeSet.addAll(graph.varMap.get(tmp));
97       else
98         edgeSet.addAll(graph.parent.varMap.get(tmp));
99       newDelta.varedgeadd.put(tmp, edgeSet);
100     }
101     
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());
106     
107     for(AllocNode node:nodeSet) {
108       MySet<Edge> edgeSet=new MySet<Edge>();
109       /* Get edge set */
110       if (graph.nodeMap.containsKey(node))
111         edgeSet.addAll(graph.nodeMap.get(node));
112       else
113         edgeSet.addAll(graph.parent.nodeMap.get(node));
114       newDelta.heapedgeadd.put(node, edgeSet);
115       
116       /* Compute ages */
117       if (graph.nodeAges.contains(node))
118         newDelta.addNodeAges.add(node);
119       else if (graph.parent.nodeAges.contains(node))
120         newDelta.addNodeAges.add(node);
121
122       /* Compute ages */
123       if (graph.oldNodes.containsKey(node)) {
124         if (graph.oldNodes.get(node).booleanValue())
125           newDelta.addOldNodes.put(node, Boolean.TRUE);
126       } else if (graph.parent.oldNodes.containsKey(node)) {
127         //parent graphs only contain true...no need to check
128         newDelta.addOldNodes.put(node, Boolean.TRUE);
129       }
130     }
131   }
132
133   void generateFinalDelta(BBlock bblock, Delta delta, Graph graph) {
134     Delta newDelta=new Delta(null, false);
135     if (delta.getInit()) {
136       buildInitDelta(graph, newDelta);
137     } else {
138       /* We can break the old delta...it is done being used */
139       /* First we will build variable edges */
140       HashSet<TempDescriptor> tmpSet=new HashSet<TempDescriptor>();
141       tmpSet.addAll(delta.basevaredge.keySet());
142       tmpSet.addAll(delta.varedgeadd.keySet());
143       for(TempDescriptor tmp:tmpSet) {
144         /* Start with the new incoming edges */
145         MySet<Edge> newbaseedge=delta.basevaredge.get(tmp);
146         /* Remove the remove set */
147         newbaseedge.removeAll(delta.varedgeremove.get(tmp));
148         /* Add in the new set*/
149         newbaseedge.addAll(delta.varedgeadd.get(tmp));
150         /* Store the results */
151         newDelta.varedgeadd.put(tmp, newbaseedge);
152       }
153
154       /* Next we build heap edges */
155       HashSet<AllocNode> nodeSet=new HashSet<AllocNode>();
156       nodeSet.addAll(delta.baseheapedge.keySet());
157       nodeSet.addAll(delta.heapedgeadd.keySet());
158       nodeSet.addAll(delta.heapedgeremove.keySet());
159       for(AllocNode node:nodeSet) {
160         /* Start with the new incoming edges */
161         MySet<Edge> newheapedge=(MySet<Edge>) delta.baseheapedge.get(node).clone();
162         /* Remove the remove set */
163         newheapedge.removeAll(delta.heapedgeremove.get(node));
164         /* Add in the add set */
165         newheapedge.addAll(delta.heapedgeadd.get(node));
166         newDelta.heapedgeadd.put(node, newheapedge);
167
168         /* Also need to subtract off some edges */
169         MySet<Edge> removeset=delta.heapedgeremove.get(node);
170
171         /* Remove the newly created edges..no need to propagate a diff for those */
172         removeset.removeAll(delta.baseheapedge.get(node));
173         newDelta.heapedgeremove.put(node, removeset);
174       }
175
176       /* Compute new ages */
177       newDelta.addNodeAges.addAll(delta.baseNodeAges);
178       newDelta.addNodeAges.addAll(delta.addNodeAges);
179       HashSet<AllocNode> oldNodes=new HashSet<AllocNode>();
180
181       /* Compute whether old nodes survive */
182       oldNodes.addAll(delta.baseOldNodes.keySet());
183       oldNodes.addAll(delta.addOldNodes.keySet());
184       for(AllocNode node:oldNodes) {
185         if (delta.addOldNodes.containsKey(node)) {
186           if (delta.addOldNodes.get(node).booleanValue()) {
187             newDelta.addOldNodes.put(node, Boolean.TRUE);
188           }
189         } else {
190           if (delta.baseOldNodes.get(node).booleanValue()) {
191             newDelta.addOldNodes.put(node, Boolean.TRUE);
192           }
193         }
194       }
195     }
196
197     /* Now we need to propagate newdelta */
198     if (!newDelta.heapedgeadd.isEmpty()||!newDelta.heapedgeremove.isEmpty()||!newDelta.varedgeadd.isEmpty()||!newDelta.addNodeAges.isEmpty()||!newDelta.addOldNodes.isEmpty()) {
199       /* We have a delta to propagate */
200       Vector<BBlock> blockvector=bblock.next();
201       for(int i=0;i<blockvector.size();i++) {
202         if (i==0) {
203           newDelta.setBlock(new PPoint(blockvector.get(i)));
204           toprocess.add(newDelta);
205         } else {
206           toprocess.add(newDelta.diffBlock(new PPoint(blockvector.get(i))));
207         }
208       }
209     }
210   }
211
212   Delta processNode(BBlock bblock, int index, FlatNode node, Delta delta, Graph newgraph) {
213     switch(node.kind()) {
214     case FKind.FlatNew:
215       return processNewNode((FlatNew)node, delta, newgraph);
216     case FKind.FlatFieldNode:
217     case FKind.FlatElementNode:
218       return processFieldElementNode(node, delta, newgraph);
219     case FKind.FlatCastNode:
220     case FKind.FlatOpNode:
221     case FKind.FlatReturnNode:
222       return processCopyNode(node, delta, newgraph);
223     case FKind.FlatSetFieldNode:
224     case FKind.FlatSetElementNode:
225       return processSetFieldElementNode(node, delta, newgraph);
226     case FKind.FlatMethod:
227     case FKind.FlatExit:
228       return processFlatNop(node, delta, newgraph);
229     case FKind.FlatCall:
230       return processFlatCall(bblock, index, (FlatCall) node, delta, newgraph);
231     case FKind.FlatSESEEnterNode:
232     case FKind.FlatSESEExitNode:
233       throw new Error("Unimplemented node:"+node);
234     default:
235       throw new Error("Unrecognized node:"+node);
236     }
237   }
238
239
240   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) {
241     //Handle the this temp
242     if (tmpthis!=null) {
243       MySet<Edge> edges=(oldnodeset!=null)?GraphManip.getDiffEdges(delta, tmpthis):GraphManip.getEdges(graph, delta, tmpthis);
244       newDelta.varedgeadd.put(tmpthis, (MySet<Edge>) edges.clone());
245       edgeset.addAll(edges);
246       for(Edge e:edges) {
247         AllocNode dstnode=e.dst;
248         if (!nodeset.contains(dstnode)&&(oldnodeset==null||!oldnodeset.contains(dstnode))) {
249           TypeDescriptor type=dstnode.getType();
250           if (!type.isArray()) {
251             targetSet.add(type.getClassDesc());
252           } else {
253             //arrays don't have code
254             targetSet.add(typeUtil.getClass(TypeUtil.ObjectClass));
255           }
256           nodeset.add(dstnode);
257           tovisit.add(dstnode);
258         }
259       }
260     }
261   }
262
263   void processParams(Graph graph, Delta delta, Delta newDelta, HashSet<AllocNode> nodeset, Stack<AllocNode> tovisit, MySet<Edge> edgeset, FlatCall fcall, boolean diff) {
264     //Go through each temp
265     for(int i=0;i<fcall.numArgs();i++) {
266       TempDescriptor tmp=fcall.getArg(i);
267       MySet<Edge> edges=diff?GraphManip.getDiffEdges(delta, tmp):GraphManip.getEdges(graph, delta, tmp);
268       newDelta.varedgeadd.put(tmp, (MySet<Edge>) edges.clone());
269       edgeset.addAll(edges);
270       for(Edge e:edges) {
271         if (!nodeset.contains(e.dst)) {
272           nodeset.add(e.dst);
273           tovisit.add(e.dst);
274         }
275       }
276     }
277   }
278
279   void computeReachableNodes(Graph graph, Delta delta, Delta newDelta, HashSet<AllocNode> nodeset, Stack<AllocNode> tovisit, MySet<Edge> edgeset, HashSet<AllocNode> oldnodeset) {
280       while(!tovisit.isEmpty()) {
281         AllocNode node=tovisit.pop();
282         MySet<Edge> edges=GraphManip.getEdges(graph, delta, node);
283         newDelta.heapedgeadd.put(node, edges);
284         edgeset.addAll(edges);
285         for(Edge e:edges) {
286           if (!nodeset.contains(e.dst)&&(oldnodeset==null||!oldnodeset.contains(e.dst))) {
287             nodeset.add(e.dst);
288             tovisit.add(e.dst);
289           }
290         }
291       }
292   }
293
294   HashSet<MethodDescriptor> computeTargets(FlatCall fcall, Delta newDelta) {
295     TempDescriptor tmpthis=fcall.getThis();
296     MethodDescriptor md=fcall.getMethod();
297     HashSet<MethodDescriptor> targets=new HashSet<MethodDescriptor>();
298     if (md.isStatic()) {
299       targets.add(md);
300     } else {
301       //Compute Edges
302       for(Edge e:newDelta.varedgeadd.get(tmpthis)) {
303         AllocNode node=e.dst;
304         ClassDescriptor cd=node.getType().getClassDesc();
305         //Figure out exact method called and add to set
306         targets.add(cd.getCalledMethod(md));
307       }
308     }
309     return targets;
310   }
311
312
313   void fixMapping(FlatCall fcall, HashSet<MethodDescriptor> targets, MySet<Edge> oldedgeset, Delta newDelta, BBlock callblock, int callindex) {
314     Delta basedelta=null;
315     TempDescriptor tmpthis=fcall.getThis();
316
317     for(MethodDescriptor calledmd:targets) {
318       FlatMethod fm=state.getMethodFlat(calledmd);
319       boolean newmethod=false;
320       
321       //Build tmpMap
322       HashMap<TempDescriptor, TempDescriptor> tmpMap=new HashMap<TempDescriptor, TempDescriptor>();
323       int offset=0;
324       if(tmpthis!=null) {
325         tmpMap.put(tmpthis, fm.getParameter(offset++));
326       }
327       for(int i=0;i<fcall.numArgs();i++) {
328         TempDescriptor tmp=fcall.getArg(i);
329         tmpMap.put(tmp,fm.getParameter(i+offset));
330       }
331
332       //Get basicblock for the method
333       BasicBlock block=getBBlock(fm);
334       
335       //Hook up exits
336       if (!callMap.containsKey(fcall)) {
337         callMap.put(fcall, new HashSet<BBlock>());
338       }
339       
340       Delta returnDelta=null;
341       if (!callMap.get(fcall).contains(block.getStart())) {
342         callMap.get(fcall).add(block.getStart());
343         newmethod=true;
344         
345         //Hook up return
346         if (!returnMap.containsKey(block.getExit())) {
347           returnMap.put(block.getExit(), new HashSet<PPoint>());
348         }
349         returnMap.get(block.getExit()).add(new PPoint(callblock, callindex));
350         
351         if (bbgraphMap.containsKey(block.getExit())) {
352           //Need to push existing results to current node
353           if (returnDelta==null) {
354             returnDelta=new Delta(null, false);
355             buildInitDelta(bbgraphMap.get(block.getExit()), returnDelta);
356             if (!returnDelta.heapedgeadd.isEmpty()||!returnDelta.heapedgeremove.isEmpty()||!returnDelta.varedgeadd.isEmpty()) {
357               returnDelta.setBlock(new PPoint(callblock, callindex));
358               toprocess.add(returnDelta);
359             }
360           } else {
361             if (!returnDelta.heapedgeadd.isEmpty()||!returnDelta.heapedgeremove.isEmpty()||!returnDelta.varedgeadd.isEmpty()) {
362               toprocess.add(returnDelta.diffBlock(new PPoint(callblock, callindex)));
363             }
364           }
365         }
366       }
367       
368       if (oldedgeset==null) {
369         //First build of this graph
370         //Build and enqueue delta...safe to just use existing delta
371         Delta d=newDelta.changeParams(tmpMap, new PPoint(block.getStart()));
372         toprocess.add(d);
373       } else if (newmethod) {
374         if (basedelta==null) {
375           basedelta=newDelta.buildBase(oldedgeset);
376         }
377         //Build and enqueue delta
378         Delta d=basedelta.changeParams(tmpMap, new PPoint(block.getStart()));
379         toprocess.add(d);
380       } else  {
381         //Build and enqueue delta
382         Delta d=newDelta.changeParams(tmpMap, new PPoint(block.getStart()));
383         toprocess.add(d);
384       }
385     }
386   }
387
388
389   /* This function computes all edges that start outside of the callee context and go into the callee context */
390
391   void computeExternalEdges(Graph graph, Delta delta, HashSet<AllocNode> nodeset, HashSet<AllocNode> deltaset, MySet<Edge> externaledgeset) {
392     //Do heap edges first
393     HashSet<AllocNode> externalnodes=new HashSet<AllocNode>();
394     externalnodes.addAll(delta.baseheapedge.keySet());
395     externalnodes.addAll(delta.heapedgeadd.keySet());
396     externalnodes.addAll(delta.heapedgeremove.keySet());
397     //remove allinternal nodes
398     externalnodes.removeAll(nodeset);
399     for(AllocNode extNode:externalnodes) {
400       //Compute set of edges from given node
401       MySet<Edge> edges=new MySet<Edge>(delta.baseheapedge.get(extNode));
402       edges.removeAll(delta.heapedgeremove.get(extNode));
403       edges.addAll(delta.heapedgeadd.get(extNode));
404       
405       for(Edge e:edges) {
406         if (nodeset.contains(e.dst))
407           externaledgeset.add(e);
408       }
409     }
410
411     //Do heap edges first
412     HashSet<TempDescriptor> temps=new HashSet<TempDescriptor>();
413     temps.addAll(delta.basevaredge.keySet());
414     temps.addAll(delta.varedgeadd.keySet());
415     temps.addAll(delta.varedgeremove.keySet());
416     //remove allinternal nodes
417     temps.removeAll(nodeset);
418     
419     for(TempDescriptor tmp:temps) {
420       //Compute set of edges from given node
421       MySet<Edge> edges=new MySet<Edge>(delta.basevaredge.get(tmp));
422       edges.removeAll(delta.varedgeremove.get(tmp));
423       edges.addAll(delta.varedgeadd.get(tmp));
424       
425       for(Edge e:edges) {
426         if (nodeset.contains(e.dst))
427           externaledgeset.add(e);
428       }
429     }
430   }
431   
432   void removeEdges(Delta delta, HashSet<AllocNode> nodeset, MySet<Edge> edgeset, MySet<Edge> externaledgeset) {
433     //Want to remove the set of internal edges
434     for(Edge e:edgeset) {
435       if (e.src!=null) {
436         if (delta.heapedgeadd.containsKey(e.src)&&delta.heapedgeadd.get(e.src).contains(e)) {
437           //remove edge if it is in the add set
438           delta.heapedgeadd.get(e.src).remove(e);
439         } else {
440           //add edge to to the remove set
441           if (!delta.heapedgeremove.containsKey(e.src))
442             delta.heapedgeremove.put(e.src, new MySet<Edge>());
443           delta.heapedgeremove.get(e.src).add(e);
444         }
445       }
446     }
447
448     //Want to remove the set of external edges
449     for(Edge e:externaledgeset) {
450       //want to remove the set of internal edges
451       if (e.src!=null) {
452         if (delta.heapedgeadd.containsKey(e.src)&&delta.heapedgeadd.get(e.src).contains(e)) {
453           //remove edge if it is in the add set
454           delta.heapedgeadd.get(e.src).remove(e);
455         } else {
456           //add edge to to the remove set
457           if (!delta.heapedgeremove.containsKey(e.src))
458             delta.heapedgeremove.put(e.src, new MySet<Edge>());
459           delta.heapedgeremove.get(e.src).add(e);
460         }
461       } else {
462         if (delta.varedgeadd.containsKey(e.srcvar)&&delta.varedgeadd.get(e.srcvar).contains(e)) {
463           //remove edge if it is in the add set
464           delta.varedgeadd.get(e.srcvar).remove(e);
465         } else {
466           //add edge to to the remove set
467           if (!delta.varedgeremove.containsKey(e.srcvar))
468             delta.varedgeremove.put(e.srcvar,new MySet<Edge>());
469           delta.varedgeremove.get(e.srcvar).add(e);
470         }
471       }
472     }
473   }
474   
475
476   Delta processFlatCall(BBlock callblock, int callindex, FlatCall fcall, Delta delta, Graph graph) {
477     Delta newDelta=new Delta(null, false);
478
479     if (delta.getInit()) {
480       MySet<Edge> edgeset=new MySet<Edge>();
481       MySet<Edge> externaledgeset=new MySet<Edge>();
482       HashSet<AllocNode> nodeset=new HashSet<AllocNode>();
483       HashSet<ClassDescriptor> targetSet=new HashSet<ClassDescriptor>();
484       Stack<AllocNode> tovisit=new Stack<AllocNode>();
485       TempDescriptor tmpthis=fcall.getThis();
486
487       //Handle the this temp
488       processThisTargets(targetSet, graph, delta, newDelta, nodeset, tovisit, edgeset, tmpthis, null);
489
490       //Go through each temp
491       processParams(graph, delta, newDelta, nodeset, tovisit, edgeset, fcall, false);
492       
493       //Traverse all reachable nodes
494       computeReachableNodes(graph, delta, newDelta, nodeset, tovisit, edgeset, null);
495
496       //Compute call targets
497       HashSet<MethodDescriptor> targets=computeTargets(fcall, newDelta);
498
499       //Fix mapping
500       fixMapping(fcall, targets, null, newDelta, callblock, callindex);
501
502       //Compute edges into region to splice out
503       computeExternalEdges(graph, delta, nodeset, null, externaledgeset);
504
505       //Splice out internal edges
506       removeEdges(delta, nodeset, edgeset, externaledgeset);
507
508       graph.reachNode=nodeset;
509       graph.reachEdge=edgeset;
510       
511       //Apply diffs to graph
512       applyDiffs(graph, delta, true);
513     } else {
514       MySet<Edge> edgeset=new MySet<Edge>();
515       MySet<Edge> externaledgeset=new MySet<Edge>();
516       HashSet<AllocNode> nodeset=new HashSet<AllocNode>();
517       MySet<Edge> oldedgeset=graph.reachEdge;
518       HashSet<AllocNode> oldnodeset=graph.reachNode;
519
520       HashSet<ClassDescriptor> targetSet=new HashSet<ClassDescriptor>();
521       Stack<AllocNode> tovisit=new Stack<AllocNode>();
522       TempDescriptor tmpthis=fcall.getThis();
523
524       //Handle the this temp
525       processThisTargets(targetSet, graph, delta, newDelta, nodeset, tovisit, edgeset, tmpthis, oldnodeset);
526
527       //Go through each temp
528       processParams(graph, delta, newDelta, nodeset, tovisit, edgeset, fcall, true);
529
530       //Go through each new heap edge that starts from old node
531       MySet<Edge> newedges=GraphManip.getDiffEdges(delta, oldnodeset);
532       edgeset.addAll(newedges);
533       for(Edge e:newedges) {
534         if (!nodeset.contains(e.dst)&&!oldnodeset.contains(e.dst)) {
535           nodeset.add(e.dst);
536           tovisit.add(e.dst);
537         }
538       }
539       
540       //Traverse all reachable nodes
541       computeReachableNodes(graph, delta, newDelta, nodeset, tovisit, edgeset, oldnodeset);
542
543       //Compute call targets
544       HashSet<MethodDescriptor> targets=computeTargets(fcall, newDelta);
545
546       //add in new nodeset and edgeset
547       oldnodeset.addAll(nodeset);
548       oldedgeset.addAll(edgeset);
549
550       //Fix mapping
551       fixMapping(fcall, targets, oldedgeset, newDelta, callblock, callindex);
552
553       //Compute edges into region to splice out
554       computeExternalEdges(graph, delta, oldnodeset, nodeset, externaledgeset);
555
556       //Splice out internal edges
557       removeEdges(delta, nodeset, edgeset, externaledgeset);
558
559       //Apply diffs to graph
560       applyDiffs(graph, delta);
561     }
562     return delta;
563   }
564
565   void applyDiffs(Graph graph, Delta delta) {
566     applyDiffs(graph, delta, false);
567   }
568
569   void applyDiffs(Graph graph, Delta delta, boolean genbackwards) {
570     //build backwards map if requested
571     if (genbackwards&&graph.backMap==null) {
572       graph.backMap=new HashMap<AllocNode, MySet<Edge>>();
573       if (graph.parent.backMap==null) {
574         graph.parent.backMap=new HashMap<AllocNode, MySet<Edge>>();
575         for(Map.Entry<AllocNode, MySet<Edge>> entry:graph.nodeMap.entrySet()) {
576           for(Edge e:entry.getValue()) {
577             if (!graph.parent.backMap.containsKey(e.dst))
578               graph.parent.backMap.put(e.dst, new MySet<Edge>());
579             graph.parent.backMap.get(e.dst).add(e);
580           }
581         }
582         for(Map.Entry<TempDescriptor, MySet<Edge>> entry:graph.varMap.entrySet()) {
583           for(Edge e:entry.getValue()) {
584             if (!graph.parent.backMap.containsKey(e.dst))
585               graph.parent.backMap.put(e.dst, new MySet<Edge>());
586             graph.parent.backMap.get(e.dst).add(e);
587           }
588         }
589       }
590     }
591
592     //Add hidden base edges
593     for(Map.Entry<AllocNode, MySet<Edge>> e: delta.baseheapedge.entrySet()) {
594       AllocNode node=e.getKey();
595       MySet<Edge> edges=e.getValue();
596       if (graph.nodeMap.containsKey(node)) {
597         MySet<Edge> nodeEdges=graph.nodeMap.get(node);
598         nodeEdges.addAll(edges);
599       }
600     }
601
602     //Remove heap edges
603     for(Map.Entry<AllocNode, MySet<Edge>> e: delta.heapedgeremove.entrySet()) {
604       AllocNode node=e.getKey();
605       MySet<Edge> edgestoremove=e.getValue();
606       if (graph.nodeMap.containsKey(node)) {
607         //Just apply diff to current map
608         graph.nodeMap.get(node).removeAll(edgestoremove);
609       } else {
610         //Generate diff from parent graph
611         MySet<Edge> parentedges=graph.parent.nodeMap.get(node);
612         MySet<Edge> newedgeset=Util.setSubtract(parentedges, edgestoremove);
613         graph.nodeMap.put(node, newedgeset);
614       }
615     }
616
617     //Add heap edges
618     for(Map.Entry<AllocNode, MySet<Edge>> e: delta.heapedgeadd.entrySet()) {
619       AllocNode node=e.getKey();
620       MySet<Edge> edgestoadd=e.getValue();
621       //If we have not done a subtract, then 
622       if (!graph.nodeMap.containsKey(node)) {
623         //Copy the parent entry
624         graph.nodeMap.put(node, (MySet<Edge>)graph.parent.nodeMap.get(node).clone());
625       }
626       graph.nodeMap.get(node).addAll(edgestoadd);
627       if (genbackwards) {
628         for(Edge eadd:edgestoadd) {
629           if (!graph.backMap.containsKey(eadd.dst))
630             graph.backMap.put(eadd.dst, new MySet<Edge>());
631           graph.backMap.get(eadd.dst).add(eadd);
632         }
633       }
634     }
635
636     //Remove var edges
637     for(Map.Entry<TempDescriptor, MySet<Edge>> e: delta.varedgeremove.entrySet()) {
638       TempDescriptor tmp=e.getKey();
639       MySet<Edge> edgestoremove=e.getValue();
640
641       if (graph.varMap.containsKey(tmp)) {
642         //Just apply diff to current map
643         graph.varMap.get(tmp).removeAll(edgestoremove);
644       } else {
645         //Generate diff from parent graph
646         MySet<Edge> parentedges=graph.parent.varMap.get(tmp);
647         MySet<Edge> newedgeset=Util.setSubtract(parentedges, edgestoremove);
648         graph.varMap.put(tmp, newedgeset);
649       }
650     }
651
652     //Add var edges
653     for(Map.Entry<TempDescriptor, MySet<Edge>> e: delta.varedgeadd.entrySet()) {
654       TempDescriptor tmp=e.getKey();
655       MySet<Edge> edgestoadd=e.getValue();
656       graph.varMap.put(tmp, (MySet<Edge>) edgestoadd.clone());
657       if (genbackwards) {
658         for(Edge eadd:edgestoadd) {
659           if (!graph.backMap.containsKey(eadd.dst))
660             graph.backMap.put(eadd.dst, new MySet<Edge>());
661           graph.backMap.get(eadd.dst).add(eadd);
662         }
663       }
664     }
665
666     //Add node additions
667     for(AllocNode node:delta.addNodeAges) {
668       graph.nodeAges.add(node);
669     }
670     
671     for(Map.Entry<AllocNode, Boolean> nodeentry:delta.addOldNodes.entrySet()) {
672       AllocNode node=nodeentry.getKey();
673       Boolean ispresent=nodeentry.getValue();
674       graph.oldNodes.put(node, ispresent);
675     }
676   }
677
678   Delta processSetFieldElementNode(FlatNode node, Delta delta, Graph graph) {
679     TempDescriptor src;
680     FieldDescriptor fd;
681     TempDescriptor dst;
682     if (node.kind()==FKind.FlatSetElementNode) {
683       FlatSetElementNode fen=(FlatSetElementNode) node;
684       src=fen.getSrc();
685       fd=null;
686       dst=fen.getDst();
687     } else {
688       FlatSetFieldNode ffn=(FlatSetFieldNode) node;
689       src=ffn.getSrc();
690       fd=ffn.getField();
691       dst=ffn.getDst();
692     }
693     if (delta.getInit()) {
694       HashSet<AllocNode> srcNodes=GraphManip.getNodes(graph, delta, src);
695       HashSet<AllocNode> dstNodes=GraphManip.getNodes(graph, delta, dst);
696       MySet<Edge> edgesToAdd=GraphManip.genEdges(srcNodes, fd, dstNodes);
697       MySet<Edge> edgesToRemove=null;
698       if (dstNodes.size()==1&&!dstNodes.iterator().next().isSummary()) {
699         /* Can do a strong update */
700         edgesToRemove=GraphManip.getEdges(graph, delta, dstNodes, fd);
701       }
702       /* Update diff */
703       updateHeapDelta(graph, delta, edgesToAdd, edgesToRemove);
704       applyDiffs(graph, delta);
705     } else {
706       /* First look at new sources */
707       MySet<Edge> edgesToAdd=new MySet<Edge>();
708       HashSet<AllocNode> newSrcNodes=GraphManip.getDiffNodes(delta, src);
709       HashSet<AllocNode> dstNodes=GraphManip.getDiffNodes(delta, dst);
710       edgesToAdd.addAll(GraphManip.genEdges(newSrcNodes, fd, dstNodes));
711       HashSet<AllocNode> newDstNodes=GraphManip.getDiffNodes(delta, dst);
712       MySet<Edge> edgesToRemove=null;
713       if (newDstNodes.size()!=0) {
714         if (dstNodes.size()==1&&!dstNodes.iterator().next().isSummary()) {
715           /* Need to undo strong update */
716           if (graph.strongUpdateSet!=null) {
717             edgesToAdd.addAll(graph.strongUpdateSet);
718             graph.strongUpdateSet.clear();
719           }
720         } else if (dstNodes.size()==0&&newDstNodes.size()==1&&!newDstNodes.iterator().next().isSummary()&&graph.strongUpdateSet==null) {
721           edgesToRemove=GraphManip.getEdges(graph, delta, dstNodes, fd);
722         }
723         HashSet<AllocNode> srcNodes=GraphManip.getDiffNodes(delta, src);
724         edgesToAdd.addAll(GraphManip.genEdges(srcNodes, fd, newDstNodes));
725       }
726       /* Update diff */
727       updateHeapDelta(graph, delta, edgesToAdd, edgesToRemove);
728       applyDiffs(graph, delta);
729     }
730     return delta;
731   }
732
733   Delta processCopyNode(FlatNode node, Delta delta, Graph graph) {
734     TempDescriptor src;
735     TempDescriptor dst;
736     if (node.kind()==FKind.FlatOpNode) {
737       FlatOpNode fon=(FlatOpNode) node;
738       src=fon.getLeft();
739       dst=fon.getDest();
740     } else if (node.kind()==FKind.FlatReturnNode) {
741       FlatReturnNode frn=(FlatReturnNode)node;
742       src=frn.getReturnTemp();
743       dst=returntmp;
744     } else {
745       FlatCastNode fcn=(FlatCastNode) node;
746       src=fcn.getSrc();
747       dst=fcn.getDst();
748     }
749     if (delta.getInit()) {
750       HashSet<AllocNode> srcnodes=GraphManip.getNodes(graph, delta, src);
751       MySet<Edge> edgesToAdd=GraphManip.genEdges(src, srcnodes);
752       MySet<Edge> edgesToRemove=GraphManip.getEdges(graph, delta, dst);
753       updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
754       applyDiffs(graph, delta);
755     } else {
756       /* First compute new src nodes */
757       HashSet<AllocNode> newSrcNodes=GraphManip.getDiffNodes(delta, src);
758
759       /* Compute the union, and then the set of edges */
760       MySet<Edge> edgesToAdd=GraphManip.genEdges(src, newSrcNodes);
761       
762       /* Compute set of edges to remove */
763       MySet<Edge> edgesToRemove=GraphManip.getDiffEdges(delta, dst);      
764
765       /* Update diff */
766       updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
767       applyDiffs(graph, delta);
768     }
769     return delta;
770   }
771
772   Delta processFieldElementNode(FlatNode node, Delta delta, Graph graph) {
773     TempDescriptor src;
774     FieldDescriptor fd;
775     TempDescriptor dst;
776     if (node.kind()==FKind.FlatElementNode) {
777       FlatElementNode fen=(FlatElementNode) node;
778       src=fen.getSrc();
779       fd=null;
780       dst=fen.getDst();
781     } else {
782       FlatFieldNode ffn=(FlatFieldNode) node;
783       src=ffn.getSrc();
784       fd=ffn.getField();
785       dst=ffn.getDst();
786     }
787     if (delta.getInit()) {
788       HashSet<AllocNode> srcnodes=GraphManip.getNodes(graph, delta, src);
789       HashSet<AllocNode> fdnodes=GraphManip.getNodes(graph, delta, srcnodes, fd);
790       MySet<Edge> edgesToAdd=GraphManip.genEdges(src, fdnodes);
791       MySet<Edge> edgesToRemove=GraphManip.getEdges(graph, delta, dst);
792       updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
793       applyDiffs(graph, delta);
794     } else {
795       /* First compute new objects we read fields of */
796       HashSet<AllocNode> allsrcnodes=GraphManip.getNodes(graph, delta, src);
797       HashSet<AllocNode> difffdnodes=GraphManip.getDiffNodes(delta, allsrcnodes, fd);     
798       /* Next compute new targets of fields */
799       HashSet<AllocNode> newsrcnodes=GraphManip.getDiffNodes(delta, src);
800       HashSet<AllocNode> newfdnodes=GraphManip.getNodes(graph, delta, newsrcnodes, fd);
801       /* Compute the union, and then the set of edges */
802       HashSet<AllocNode> newTargets=new HashSet<AllocNode>();
803       newTargets.addAll(newfdnodes);
804       newTargets.addAll(difffdnodes);
805       MySet<Edge> edgesToAdd=GraphManip.genEdges(src, newTargets);      
806       
807       /* Compute set of edges to remove */
808       MySet<Edge> edgesToRemove=GraphManip.getDiffEdges(delta, dst);      
809
810       /* Update diff */
811       updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
812       applyDiffs(graph, delta);
813     }
814     return delta;
815   }
816
817   static void updateVarDelta(Graph graph, Delta delta, TempDescriptor tmp, MySet<Edge> edgestoAdd, MySet<Edge> edgestoRemove) {
818     MySet<Edge> edgeAdd=delta.varedgeadd.get(tmp);
819     MySet<Edge> edgeRemove=delta.varedgeremove.get(tmp);
820     MySet<Edge> existingEdges=graph.getEdges(tmp);
821     for(Edge e: edgestoRemove) {
822       //remove edge from delta
823       edgeAdd.remove(e);
824       //if the edge is already in the graph, add an explicit remove to the delta
825       if (existingEdges.contains(e))
826         edgeRemove.add(e);
827     }
828     for(Edge e: edgestoAdd) {
829       //Remove the edge from the remove set
830       edgeRemove.remove(e);
831       //Explicitly add it to the add set unless it is already in the graph
832       if (!existingEdges.contains(e))
833         edgeAdd.add(e);
834     }
835   }
836
837   static void updateHeapDelta(Graph graph, Delta delta, MySet<Edge> edgestoAdd, MySet<Edge> edgestoRemove) {
838     for(Edge e: edgestoRemove) {
839       AllocNode src=e.src;
840       MySet<Edge> edgeAdd=delta.heapedgeadd.get(src);
841       MySet<Edge> existingEdges=graph.getEdges(src);
842       //remove edge from delta
843       edgeAdd.remove(e);
844       //if the edge is already in the graph, add an explicit remove to the delta
845       if (existingEdges.contains(e)) {
846         MySet<Edge> edgeRemove=delta.heapedgeremove.get(src);
847         edgeRemove.add(e);
848       }
849     }
850     for(Edge e: edgestoAdd) {
851       AllocNode src=e.src;
852       MySet<Edge> edgeRemove=delta.heapedgeremove.get(src);
853       MySet<Edge> existingEdges=graph.getEdges(src);
854       //Remove the edge from the remove set
855       edgeRemove.remove(e);
856       //Explicitly add it to the add set unless it is already in the graph
857       if (!existingEdges.contains(e)) {
858         MySet<Edge> edgeAdd=delta.heapedgeadd.get(src);
859         edgeAdd.add(e);
860       }
861     }
862   }
863
864   Delta processFlatNop(FlatNode node, Delta delta, Graph graph) {
865     applyDiffs(graph, delta);
866     return delta;
867   }
868   
869   Delta processNewNode(FlatNew node, Delta delta, Graph graph) {
870     AllocNode summary=allocFactory.getAllocNode(node, true);
871     AllocNode single=allocFactory.getAllocNode(node, false);
872     TempDescriptor tmp=node.getDst();
873
874     if (delta.getInit()) {
875       /* We don't have to deal with summarization here...  The
876        * intuition is that this is the only place where we generate
877        * nodes for this allocation site and this is the first time
878        * we've analyzed this site */
879
880       //Build new Edge
881       Edge e=new Edge(tmp, single);
882       //Build new Edge set
883       MySet<Edge> newedges=new MySet<Edge>();
884       newedges.add(e);
885       //Add it into the diffs
886       delta.varedgeadd.put(tmp, newedges);
887       //Remove the old edges
888       delta.varedgeremove.put(tmp, graph.getEdges(tmp));
889       //Apply incoming diffs to graph
890       applyDiffs(graph, delta);
891       //Note that we create a single node
892       delta.addNodeAges.add(single);
893       //Kill the old node
894       if (delta.addOldNodes.containsKey(single)||delta.baseOldNodes.containsKey(single)) {
895         delta.addOldNodes.put(single, Boolean.FALSE);
896       }
897     } else {
898       /* 1. Fix up the variable edge additions */
899
900       for(Iterator<Map.Entry<TempDescriptor, MySet<Edge>>> entryIt=delta.varedgeadd.entrySet().iterator();entryIt.hasNext();) {
901         Map.Entry<TempDescriptor, MySet<Edge>> entry=entryIt.next();
902
903         if (entry.getKey()==tmp) {
904           /* Check if this is the tmp we overwrite */
905           entryIt.remove();
906         } else {
907           /* Otherwise, check if the target of the edge is changed... */
908           summarizeSet(entry.getValue(), graph.varMap.get(entry.getKey()), single, summary);
909         }
910       }
911       
912       /* 2. Fix up the base variable edges */
913
914       for(Iterator<Map.Entry<TempDescriptor, MySet<Edge>>> entryIt=delta.basevaredge.entrySet().iterator();entryIt.hasNext();) {
915         Map.Entry<TempDescriptor, MySet<Edge>> entry=entryIt.next();
916         TempDescriptor entrytmp=entry.getKey();
917         if (entrytmp==tmp) {
918           /* Check is this is the tmp we overwrite, if so add to remove set */
919           Util.relationUpdate(delta.varedgeremove, tmp, null, entry.getValue());
920         } else {
921           /* Check if the target of the edge is changed */ 
922           MySet<Edge> newset=(MySet<Edge>)entry.getValue().clone();
923           MySet<Edge> removeset=shrinkSet(newset, graph.varMap.get(entrytmp), single, summary);
924           Util.relationUpdate(delta.varedgeremove, entrytmp, newset, removeset);
925           Util.relationUpdate(delta.varedgeadd, entrytmp, null, newset);
926         }
927       }
928
929
930       /* 3. Fix up heap edge additions */
931
932       HashMap<AllocNode, MySet<Edge>> addheapedge=new HashMap<AllocNode, MySet<Edge>>();
933       for(Iterator<Map.Entry<AllocNode, MySet<Edge>>> entryIt=delta.heapedgeadd.entrySet().iterator();entryIt.hasNext();) {
934         Map.Entry<AllocNode, MySet<Edge>> entry=entryIt.next();
935         MySet<Edge> edgeset=entry.getValue();
936         AllocNode allocnode=entry.getKey();
937         if (allocnode==single) {
938           entryIt.remove();
939           summarizeSet(edgeset, graph.nodeMap.get(summary), single, summary);
940           addheapedge.put(summary, edgeset);
941         } else {
942           summarizeSet(edgeset, graph.nodeMap.get(allocnode), single, summary);
943         }
944       }
945       
946       /* Merge in diffs */
947
948       for(Map.Entry<AllocNode, MySet<Edge>> entry:addheapedge.entrySet()) {
949         AllocNode allocnode=entry.getKey();
950         Util.relationUpdate(delta.heapedgeadd, allocnode, null, entry.getValue());
951       }
952
953       /* 4. Fix up the base heap edges */
954
955       for(Iterator<Map.Entry<AllocNode, MySet<Edge>>> entryIt=delta.baseheapedge.entrySet().iterator();entryIt.hasNext();) {
956         Map.Entry<AllocNode, MySet<Edge>> entry=entryIt.next();
957         MySet<Edge> edgeset=entry.getValue();
958         AllocNode allocnode=entry.getKey();
959         if (allocnode==single) {
960           entryIt.remove();
961         }
962         AllocNode addnode=(allocnode==single)?summary:allocnode;
963
964         MySet<Edge> newset=(MySet<Edge>) edgeset.clone();
965         MySet<Edge> removeset=shrinkSet(newset, graph.nodeMap.get(addnode), single, summary);
966         Util.relationUpdate(delta.heapedgeadd, addnode, null, newset);
967         Util.relationUpdate(delta.heapedgeremove, allocnode, null, removeset);
968       }
969
970       /* Update Node Ages...If the base or addNodeAges set contains a
971        * single node, it now should also contain a summary node...  No
972        * need to generate a single node as that has already been
973        * done. */
974       if (delta.baseNodeAges.contains(single)||delta.addNodeAges.contains(single)) {
975         delta.addNodeAges.add(summary);
976       }
977
978       //Kill the old node if someone tries to add it
979       if (delta.addOldNodes.containsKey(single)||delta.baseOldNodes.containsKey(single)) {
980         delta.addOldNodes.put(single, Boolean.FALSE);
981       }
982       
983       //Apply incoming diffs to graph
984       applyDiffs(graph, delta);      
985     }
986     return delta;
987   }
988
989   /* This function builds a new edge set where oldnode is summarized into new node */
990
991   void summarizeSet(MySet<Edge> edgeset, MySet<Edge> oldedgeset, AllocNode oldnode, AllocNode sumnode) {
992     MySet<Edge> newSet=null;
993     for(Iterator<Edge> edgeit=edgeset.iterator();edgeit.hasNext();) {
994       Edge e=edgeit.next();
995       if (e.dst==oldnode||e.src==oldnode) {
996         if (newSet==null) {
997           newSet=new MySet<Edge>();
998         }
999         edgeit.remove();
1000         e=e.copy();
1001
1002         if (e.dst==oldnode) {
1003           e.dst=sumnode;
1004         }
1005         if (e.src==oldnode) {
1006           e.src=sumnode;
1007         }
1008         if (oldedgeset==null||!oldedgeset.contains(e))
1009           newSet.add(e);
1010       }
1011     }
1012     if (newSet!=null)
1013       edgeset.addAll(newSet);
1014   }
1015
1016   /* Shrinks the incoming set to just include rewritten values.
1017    * Returns a set of the original rewritten values */
1018
1019   MySet<Edge> shrinkSet(MySet<Edge> edgeset, MySet<Edge> oldedgeset, AllocNode oldnode, AllocNode newnode) {
1020     MySet<Edge> newSet=null;
1021     MySet<Edge> removeSet=null;
1022     for(Iterator<Edge> edgeit=edgeset.iterator();edgeit.hasNext();) {
1023       Edge e=edgeit.next();
1024       edgeit.remove();
1025       if (e.dst==oldnode||e.src==oldnode) {
1026         if (newSet==null) {
1027           newSet=new MySet<Edge>();
1028           removeSet=new MySet<Edge>();
1029         }
1030
1031         removeSet.add(e.copy());
1032         if (e.dst==oldnode)
1033           e.dst=newnode;
1034         if (e.src==oldnode)
1035           e.src=newnode;
1036         if (oldedgeset==null||!oldedgeset.contains(e))
1037           newSet.add(e);
1038       }
1039     }
1040     if (newSet!=null)
1041       edgeset.addAll(newSet);
1042     return removeSet;
1043   } 
1044
1045   /* This function returns a completely new Delta...  It is safe to
1046    * modify this */
1047
1048   Delta applyInitDelta(Delta delta, BBlock block) {
1049     //Apply delta to graph
1050     boolean newGraph=false;
1051     if (!bbgraphMap.containsKey(block)) {
1052       bbgraphMap.put(block, new Graph(null));
1053       newGraph=true;
1054     }
1055     Delta newdelta;
1056     Graph graph=bbgraphMap.get(block);
1057
1058     if (newGraph) {
1059       newdelta=new Delta(null, true);
1060       //Add in heap edges and throw away original diff
1061       graph.nodeMap.putAll(delta.heapedgeadd);
1062       //Add in var edges and throw away original diff
1063       graph.varMap.putAll(delta.varedgeadd);
1064       //Record that this is initial set...
1065       graph.nodeAges.addAll(delta.addNodeAges);
1066       //Add old nodes
1067       for(Map.Entry<AllocNode, Boolean> oldentry:delta.addOldNodes.entrySet()) {
1068         if (oldentry.getValue().booleanValue()) {
1069           graph.oldNodes.put(oldentry.getKey(), Boolean.TRUE);
1070         }
1071       }
1072     } else {
1073       newdelta=new Delta(null, false);
1074       //merge in heap edges and variables
1075       mergeHeapEdges(graph, delta, newdelta);
1076       mergeVarEdges(graph, delta, newdelta);
1077       mergeAges(graph, delta, newdelta);
1078     }
1079     return newdelta;
1080   }
1081
1082   /* This function merges in the heap edges.  It updates delta to be
1083    * the difference */
1084
1085   void mergeHeapEdges(Graph graph, Delta delta, Delta newdelta) {
1086     //Merge in edges
1087     for(Map.Entry<AllocNode, MySet<Edge>> heapedge:delta.heapedgeadd.entrySet()) {
1088       AllocNode nsrc=heapedge.getKey();
1089       MySet<Edge> edges=heapedge.getValue();
1090
1091       if (graph.backMap!=null) {
1092         for(Edge e:edges) {
1093           if (!graph.backMap.containsKey(e.dst))
1094             graph.backMap.put(e.dst, new MySet<Edge>());
1095           graph.backMap.get(e.dst).add(e);
1096         }
1097       }
1098
1099       if (!graph.nodeMap.containsKey(nsrc)) {
1100         graph.nodeMap.put(nsrc, new MySet<Edge>());
1101       }
1102       MySet<Edge> dstedges=graph.nodeMap.get(nsrc);
1103       MySet<Edge> diffedges=new MySet<Edge>();
1104       for(Edge e:edges) {
1105         if (!dstedges.contains(e)) {
1106           //We have a new edge
1107           diffedges.add(e);
1108           dstedges.add(e);
1109         }
1110       }
1111       //Done with edge set...
1112       if (diffedges.size()>0) {
1113         //completely new
1114         newdelta.baseheapedge.put(nsrc, diffedges);
1115       }
1116     }
1117   }
1118
1119   /* This function merges in the var edges.  It updates delta to be
1120    * the difference */
1121
1122   void mergeVarEdges(Graph graph, Delta delta, Delta newdelta) {
1123     //Merge in edges
1124     for(Map.Entry<TempDescriptor, MySet<Edge>> varedge:delta.varedgeadd.entrySet()) {
1125       TempDescriptor tmpsrc=varedge.getKey();
1126       MySet<Edge> edges=varedge.getValue();
1127       if (graph.backMap!=null) {
1128         for(Edge e:edges) {
1129           if (!graph.backMap.containsKey(e.dst))
1130             graph.backMap.put(e.dst, new MySet<Edge>());
1131           graph.backMap.get(e.dst).add(e);
1132         }
1133       }
1134
1135       if (!graph.varMap.containsKey(tmpsrc)) {
1136         graph.varMap.put(tmpsrc, new MySet<Edge>());
1137       }
1138       MySet<Edge> dstedges=graph.varMap.get(tmpsrc);
1139       MySet<Edge> diffedges=new MySet<Edge>();
1140       for(Edge e:edges) {
1141         if (!dstedges.contains(e)) {
1142           //We have a new edge
1143           diffedges.add(e);
1144           dstedges.add(e);
1145         }
1146       }
1147       //Done with edge set...
1148       if (diffedges.size()>=0) {
1149         //completely new
1150         newdelta.basevaredge.put(tmpsrc,diffedges);
1151       }
1152     }
1153   }
1154
1155   void mergeAges(Graph graph, Delta delta, Delta newDelta) {
1156     //Merge in edges
1157     for(AllocNode node:delta.addNodeAges) {
1158       if (!graph.nodeAges.contains(node)) {
1159         graph.nodeAges.add(node);
1160         newDelta.baseNodeAges.add(node);
1161       }
1162     }
1163     for(Map.Entry<AllocNode, Boolean> oldentry:delta.addOldNodes.entrySet()) {
1164       AllocNode node=oldentry.getKey();
1165       boolean ispresent=oldentry.getValue().booleanValue();
1166       if (ispresent&&!graph.oldNodes.containsKey(node)) {
1167         graph.oldNodes.put(node, Boolean.TRUE);
1168         newDelta.baseOldNodes.put(node, Boolean.TRUE);
1169       }
1170     }
1171   }
1172 }