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