9b4e57799ecb5800ad640312be957f927742c718
[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   State state;
13   TypeUtil typeUtil;
14   AllocFactory allocFactory;
15   LinkedList<Delta> toprocess;
16   TempDescriptor returntmp;
17
18   public Pointer(State state, TypeUtil typeUtil) {
19     this.state=state;
20     this.blockMap=new HashMap<FlatMethod, BasicBlock>();
21     this.bbgraphMap=new HashMap<BBlock, Graph>();
22     this.graphMap=new HashMap<FlatNode, Graph>();
23     this.typeUtil=typeUtil;
24     this.allocFactory=new AllocFactory(state, typeUtil);
25     this.toprocess=new LinkedList<Delta>();
26     ClassDescriptor stringcd=typeUtil.getClass(TypeUtil.ObjectClass);
27     this.returntmp=new TempDescriptor("RETURNVAL", stringcd);
28   }
29
30   public BasicBlock getBBlock(FlatMethod fm) {
31     if (!blockMap.containsKey(fm))
32       blockMap.put(fm, BasicBlock.getBBlock(fm, true));
33     return blockMap.get(fm);
34   }
35   
36   Delta buildInitialContext() {
37     MethodDescriptor md=typeUtil.getMain();
38     FlatMethod fm=state.getMethodFlat(md);
39     BasicBlock bb=getBBlock(fm);
40     BBlock start=bb.getStart();
41     Delta delta=new Delta(start, true);
42     HashSet<Edge> arrayset=new HashSet<Edge>();
43     HashSet<Edge> varset=new HashSet<Edge>();
44     arrayset.add(new Edge(allocFactory.StringArray, null, allocFactory.Strings));
45     varset.add(new Edge(fm.getParameter(0), allocFactory.StringArray));
46     delta.heapedgeadd.put(allocFactory.StringArray, arrayset);
47     delta.varedgeadd.put(fm.getParameter(0), varset);
48     return delta;
49   }
50
51   public void doAnalysis() {
52     toprocess.add(buildInitialContext());
53
54     while(!toprocess.isEmpty()) {
55       Delta delta=toprocess.remove();
56       BBlock bblock=delta.getBlock();
57       Vector<FlatNode> nodes=bblock.nodes();
58
59       //Build base graph for entrance to this basic block
60       delta=applyInitDelta(delta, bblock);
61       Graph graph=bbgraphMap.get(bblock);
62
63       Graph nodeGraph=null;
64       //Compute delta at exit of each node
65       for(int i=0; i<nodes.size();i++) {
66         FlatNode currNode=nodes.get(i);
67         if (!graphMap.containsKey(currNode)) {
68           graphMap.put(currNode, new Graph(graph));
69         }
70         nodeGraph=graphMap.get(currNode);
71         delta=processNode(currNode, delta, nodeGraph);
72       }
73       generateFinalDelta(bblock, delta, nodeGraph);
74     }    
75   }
76
77   void generateFinalDelta(BBlock bblock, Delta delta, Graph graph) {
78     Delta newDelta=new Delta(null, false);
79     if (delta.getInit()) {
80       //First compute the set of temps
81       HashSet<TempDescriptor> tmpSet=new HashSet<TempDescriptor>();
82       tmpSet.addAll(graph.varMap.keySet());
83       tmpSet.addAll(graph.parent.varMap.keySet());
84
85       //Next build the temp map part of the delta
86       for(TempDescriptor tmp:tmpSet) {
87         HashSet<Edge> edgeSet=new HashSet<Edge>();
88         /* Get target set */
89         if (graph.varMap.containsKey(tmp))
90           edgeSet.addAll(graph.varMap.get(tmp));
91         else
92           edgeSet.addAll(graph.parent.varMap.get(tmp));
93         newDelta.varedgeadd.put(tmp, edgeSet);
94       }
95
96       //Next compute the set of src allocnodes
97       HashSet<AllocNode> nodeSet=new HashSet<AllocNode>();
98       nodeSet.addAll(graph.nodeMap.keySet());
99       nodeSet.addAll(graph.parent.nodeMap.keySet());
100
101       for(AllocNode node:nodeSet) {
102         HashSet<Edge> edgeSet=new HashSet<Edge>();
103         /* Get edge set */
104         if (graph.nodeMap.containsKey(node))
105           edgeSet.addAll(graph.nodeMap.get(node));
106         else
107           edgeSet.addAll(graph.parent.nodeMap.get(node));
108         newDelta.heapedgeadd.put(node, edgeSet);
109       }
110     } else {
111       /* We can break the old delta...it is done being used */
112       /* First we will build variable edges */
113       HashSet<TempDescriptor> tmpSet=new HashSet<TempDescriptor>();
114       tmpSet.addAll(delta.basevaredge.keySet());
115       tmpSet.addAll(delta.varedgeadd.keySet());
116       for(TempDescriptor tmp:tmpSet) {
117         /* Start with the new incoming edges */
118         HashSet<Edge> newbaseedge=delta.basevaredge.get(tmp);
119         /* Remove the remove set */
120         newbaseedge.removeAll(delta.varedgeremove.get(tmp));
121         /* Add in the new set*/
122         newbaseedge.addAll(delta.varedgeadd.get(tmp));
123         /* Store the results */
124         newDelta.varedgeadd.put(tmp, newbaseedge);
125       }
126
127       /* Next we build heap edges */
128       HashSet<AllocNode> nodeSet=new HashSet<AllocNode>();
129       nodeSet.addAll(delta.baseheapedge.keySet());
130       nodeSet.addAll(delta.heapedgeadd.keySet());
131       nodeSet.addAll(delta.heapedgeremove.keySet());
132       for(AllocNode node:nodeSet) {
133         /* Start with the new incoming edges */
134         HashSet<Edge> newheapedge=(HashSet<Edge>) delta.baseheapedge.get(node).clone();
135         /* Remove the remove set */
136         newheapedge.removeAll(delta.heapedgeremove.get(node));
137         /* Add in the add set */
138         newheapedge.addAll(delta.heapedgeadd.get(node));
139         newDelta.heapedgeadd.put(node, newheapedge);
140
141         /* Also need to subtract off some edges */
142         HashSet<Edge> removeset=delta.heapedgeremove.get(node);
143         /* Remove the newly created edges..no need to propagate a diff for those */
144         removeset.removeAll(delta.baseheapedge.get(node));
145         newDelta.heapedgeremove.put(node, removeset);
146       }
147     }
148     /* Now we need to propagate newdelta */
149     if (!newDelta.heapedgeadd.isEmpty()||!newDelta.heapedgeremove.isEmpty()||!newDelta.varedgeadd.isEmpty()) {
150       /* We have a delta to propagate */
151       Vector<BBlock> blockvector=bblock.next();
152       for(int i=0;i<blockvector.size();i++) {
153         if (i==0) {
154           newDelta.setBlock(blockvector.get(i));
155           toprocess.add(newDelta);
156         } else {
157           toprocess.add(newDelta.diffBlock(blockvector.get(i)));
158         }
159       }
160     }
161   }
162
163   Delta processNode(FlatNode node, Delta delta, Graph newgraph) {
164     switch(node.kind()) {
165     case FKind.FlatNew:
166       return processNewNode((FlatNew)node, delta, newgraph);
167     case FKind.FlatFieldNode:
168     case FKind.FlatElementNode:
169       return processFieldElementNode(node, delta, newgraph);
170     case FKind.FlatCastNode:
171     case FKind.FlatOpNode:
172     case FKind.FlatReturnNode:
173       return processCopyNode(node, delta, newgraph);
174     case FKind.FlatSetFieldNode:
175     case FKind.FlatSetElementNode:
176       return processSetFieldElementNode(node, delta, newgraph);
177     case FKind.FlatMethod:
178     case FKind.FlatExit:
179       return processFlatNop(node, delta, newgraph);
180     case FKind.FlatCall:
181       return processFlatCall((FlatCall) node, delta, newgraph);
182     case FKind.FlatSESEEnterNode:
183     case FKind.FlatSESEExitNode:
184       throw new Error("Unimplemented node:"+node);
185     default:
186       throw new Error("Unrecognized node:"+node);
187     }
188   }
189
190   Delta processFlatCall(FlatCall fcall, Delta delta, Graph newgraph) {
191     Delta newDelta=new Delta(null, false);
192
193     if (delta.getInit()) {
194       HashSet<Edge> edgeset=new HashSet<Edge>();
195       HashSet<AllocNode> nodeset=new HashSet<AllocNode>();
196       HashSet<ClassDescriptor> targetSet=new HashSet<ClassDescriptor>();
197       Stack<AllocNode> tovisit=new Stack<AllocNode>();
198       TempDescriptor tmpthis=fc.getThis();
199
200       //Handle the this temp
201       if (tmpthis!=null) {
202         HashSet<Edge> edges=GraphManip.getEdges(graph, delta, tmpthis);
203         newdelta.varedgeadd.put(tmpthis, (HashSet<Edge>) edges.clone());
204         edgeset.addAll(edges);
205         for(Edge e:edges) {
206           AllocNode dstnode=e.dst;
207           if (!nodeset.contains(dstnode)) {
208             TypeDescriptor type=dstnode.getType();
209             if (!type.isArray()) {
210               targetSet.add(type.getClassDesc());
211             } else {
212               //arrays don't have code
213               targetSet.add(typeUtil.getClass(TypeUtil.ObjectClass));
214             }
215             nodeset.add(dstnode);
216             tovisit.add(dstnode);
217           }
218         }
219       }
220
221       //Go through each temp
222       for(int i=0;i<fc.numArgs();i++) {
223         TempDescriptor tmp=fc.getArg(i);
224         HashSet<Edge> edges=GraphManip.getEdges(graph, delta, tmp);
225         newdelta.varedgeadd.put(tmp, (HashSet<Edge>) edges.clone());
226         edgeset.addAll(edges);
227         for(Edge e:edges) {
228           if (!nodeset.contains(e.dst)) {
229             nodeset.add(e.dst);
230             tovisit.add(e.dst);
231           }
232         }
233       }
234       
235       //Traverse all reachable nodes
236       while(!tovisit.isEmpty()) {
237         AllocNode node=tovisit.pop();
238         HashSet<Edge> edges=GraphManip.getEdges(graph, delta, node);
239         newdelta.heapedgeadd.put(node, (HashSet<Edge>) edges.clone());
240         edgeset.addAll(edges);
241         for(Edge e:edges) {
242           if (!nodeset.contains(e.dst)) {
243             nodeset.add(e.dst);
244             tovisit.add(e.dst);
245           }
246         }
247       }
248       
249       //Apply diffs to graph
250       applyDiffs(graph, delta);
251     } else {
252
253       //Apply diffs to graph
254       applyDiffs(graph, delta);
255     }
256   }
257
258   void applyDiffs(Graph graph, Delta delta) {
259     //Add hidden base edges
260     for(Map.Entry<AllocNode, HashSet<Edge>> e: delta.baseheapedge.entrySet()) {
261       AllocNode node=e.getKey();
262       HashSet<Edge> edges=e.getValue();
263       if (graph.nodeMap.containsKey(node)) {
264         HashSet<Edge> nodeEdges=graph.nodeMap.get(node);
265         nodeEdges.addAll(edges);
266       }
267     }
268
269     //Remove heap edges
270     for(Map.Entry<AllocNode, HashSet<Edge>> e: delta.heapedgeremove.entrySet()) {
271       AllocNode node=e.getKey();
272       HashSet<Edge> edgestoremove=e.getValue();
273       if (graph.nodeMap.containsKey(node)) {
274         //Just apply diff to current map
275         graph.nodeMap.get(node).removeAll(edgestoremove);
276       } else {
277         //Generate diff from parent graph
278         HashSet<Edge> parentedges=graph.parent.nodeMap.get(node);
279         HashSet<Edge> newedgeset=Util.setSubtract(parentedges, edgestoremove);
280         graph.nodeMap.put(node, newedgeset);
281       }
282     }
283
284     //Add heap edges
285     for(Map.Entry<AllocNode, HashSet<Edge>> e: delta.heapedgeadd.entrySet()) {
286       AllocNode node=e.getKey();
287       HashSet<Edge> edgestoadd=e.getValue();
288       //If we have not done a subtract, then 
289       if (!graph.nodeMap.containsKey(node)) {
290         //Copy the parent entry
291         graph.nodeMap.put(node, (HashSet<Edge>)graph.parent.nodeMap.get(node).clone());
292       }
293       graph.nodeMap.get(node).addAll(edgestoadd);
294     }
295
296     //Remove var edges
297     for(Map.Entry<TempDescriptor, HashSet<Edge>> e: delta.varedgeremove.entrySet()) {
298       TempDescriptor tmp=e.getKey();
299       HashSet<Edge> edgestoremove=e.getValue();
300
301       if (graph.varMap.containsKey(tmp)) {
302         //Just apply diff to current map
303         graph.varMap.get(tmp).removeAll(edgestoremove);
304       } else {
305         //Generate diff from parent graph
306         HashSet<Edge> parentedges=graph.parent.varMap.get(tmp);
307         HashSet<Edge> newedgeset=Util.setSubtract(parentedges, edgestoremove);
308         graph.varMap.put(tmp, newedgeset);
309       }
310     }
311
312     //Add var edges
313     for(Map.Entry<TempDescriptor, HashSet<Edge>> e: delta.varedgeadd.entrySet()) {
314       TempDescriptor tmp=e.getKey();
315       HashSet<Edge> edgestoadd=e.getValue();
316       graph.varMap.put(tmp, (HashSet<Edge>) edgestoadd.clone());
317     }
318   }
319
320   Delta processSetFieldElementNode(FlatNode node, Delta delta, Graph graph) {
321     TempDescriptor src;
322     FieldDescriptor fd;
323     TempDescriptor dst;
324     if (node.kind()==FKind.FlatSetElementNode) {
325       FlatSetElementNode fen=(FlatSetElementNode) node;
326       src=fen.getSrc();
327       fd=null;
328       dst=fen.getDst();
329     } else {
330       FlatSetFieldNode ffn=(FlatSetFieldNode) node;
331       src=ffn.getSrc();
332       fd=ffn.getField();
333       dst=ffn.getDst();
334     }
335     if (delta.getInit()) {
336       HashSet<AllocNode> srcNodes=GraphManip.getNodes(graph, delta, src);
337       HashSet<AllocNode> dstNodes=GraphManip.getNodes(graph, delta, dst);
338       HashSet<Edge> edgesToAdd=GraphManip.genEdges(srcNodes, fd, dstNodes);
339       HashSet<Edge> edgesToRemove=null;
340       if (dstNodes.size()==1&&!dstNodes.iterator().next().isSummary()) {
341         /* Can do a strong update */
342         edgesToRemove=GraphManip.getEdges(graph, delta, dstNodes, fd);
343       }
344       /* Update diff */
345       updateHeapDelta(graph, delta, edgesToAdd, edgesToRemove);
346       applyDiffs(graph, delta);
347     } else {
348       /* First look at new sources */
349       HashSet<Edge> edgesToAdd=new HashSet<Edge>();
350       HashSet<AllocNode> newSrcNodes=GraphManip.getDiffNodes(delta, src);
351       HashSet<AllocNode> dstNodes=GraphManip.getDiffNodes(delta, dst);
352       edgesToAdd.addAll(GraphManip.genEdges(newSrcNodes, fd, dstNodes));
353       HashSet<AllocNode> newDstNodes=GraphManip.getDiffNodes(delta, dst);
354       HashSet<Edge> edgesToRemove=null;
355       if (newDstNodes.size()!=0) {
356         if (dstNodes.size()==1&&!dstNodes.iterator().next().isSummary()) {
357           /* Need to undo strong update */
358           if (graph.strongUpdateSet!=null) {
359             edgesToAdd.addAll(graph.strongUpdateSet);
360             graph.strongUpdateSet.clear();
361           }
362         } else if (dstNodes.size()==0&&newDstNodes.size()==1&&!newDstNodes.iterator().next().isSummary()&&graph.strongUpdateSet==null) {
363           edgesToRemove=GraphManip.getEdges(graph, delta, dstNodes, fd);
364         }
365         HashSet<AllocNode> srcNodes=GraphManip.getDiffNodes(delta, src);
366         edgesToAdd.addAll(GraphManip.genEdges(srcNodes, fd, newDstNodes));
367       }
368       /* Update diff */
369       updateHeapDelta(graph, delta, edgesToAdd, edgesToRemove);
370       applyDiffs(graph, delta);
371     }
372     return delta;
373   }
374
375   Delta processCopyNode(FlatNode node, Delta delta, Graph graph) {
376     TempDescriptor src;
377     TempDescriptor dst;
378     if (node.kind()==FKind.FlatOpNode) {
379       FlatOpNode fon=(FlatOpNode) node;
380       src=fon.getLeft();
381       dst=fon.getDest();
382     } else if (node.kind()==FKind.FlatReturnNode) {
383       FlatReturnNode frn=(FlatReturnNode)node;
384       src=frn.getReturnTemp();
385       dst=returntmp;
386     } else {
387       FlatCastNode fcn=(FlatCastNode) node;
388       src=fcn.getSrc();
389       dst=fcn.getDst();
390     }
391     if (delta.getInit()) {
392       HashSet<AllocNode> srcnodes=GraphManip.getNodes(graph, delta, src);
393       HashSet<Edge> edgesToAdd=GraphManip.genEdges(src, srcnodes);
394       HashSet<Edge> edgesToRemove=GraphManip.getEdges(graph, delta, dst);
395       updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
396       applyDiffs(graph, delta);
397     } else {
398       /* First compute new src nodes */
399       HashSet<AllocNode> newSrcNodes=GraphManip.getDiffNodes(delta, src);
400
401       /* Compute the union, and then the set of edges */
402       HashSet<Edge> edgesToAdd=GraphManip.genEdges(src, newSrcNodes);
403       
404       /* Compute set of edges to remove */
405       HashSet<Edge> edgesToRemove=GraphManip.getDiffEdges(delta, dst);      
406
407       /* Update diff */
408       updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
409       applyDiffs(graph, delta);
410     }
411     return delta;
412   }
413
414   Delta processFieldElementNode(FlatNode node, Delta delta, Graph graph) {
415     TempDescriptor src;
416     FieldDescriptor fd;
417     TempDescriptor dst;
418     if (node.kind()==FKind.FlatElementNode) {
419       FlatElementNode fen=(FlatElementNode) node;
420       src=fen.getSrc();
421       fd=null;
422       dst=fen.getDst();
423     } else {
424       FlatFieldNode ffn=(FlatFieldNode) node;
425       src=ffn.getSrc();
426       fd=ffn.getField();
427       dst=ffn.getDst();
428     }
429     if (delta.getInit()) {
430       HashSet<AllocNode> srcnodes=GraphManip.getNodes(graph, delta, src);
431       HashSet<AllocNode> fdnodes=GraphManip.getNodes(graph, delta, srcnodes, fd);
432       HashSet<Edge> edgesToAdd=GraphManip.genEdges(src, fdnodes);
433       HashSet<Edge> edgesToRemove=GraphManip.getEdges(graph, delta, dst);
434       updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
435       applyDiffs(graph, delta);
436     } else {
437       /* First compute new objects we read fields of */
438       HashSet<AllocNode> allsrcnodes=GraphManip.getNodes(graph, delta, src);
439       HashSet<AllocNode> difffdnodes=GraphManip.getDiffNodes(delta, allsrcnodes, fd);     
440       /* Next compute new targets of fields */
441       HashSet<AllocNode> newsrcnodes=GraphManip.getDiffNodes(delta, src);
442       HashSet<AllocNode> newfdnodes=GraphManip.getNodes(graph, delta, newsrcnodes, fd);
443       /* Compute the union, and then the set of edges */
444       HashSet<AllocNode> newTargets=new HashSet<AllocNode>();
445       newTargets.addAll(newfdnodes);
446       newTargets.addAll(difffdnodes);
447       HashSet<Edge> edgesToAdd=GraphManip.genEdges(src, newTargets);      
448       
449       /* Compute set of edges to remove */
450       HashSet<Edge> edgesToRemove=GraphManip.getDiffEdges(delta, dst);      
451
452       /* Update diff */
453       updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
454       applyDiffs(graph, delta);
455     }
456     return delta;
457   }
458
459   static void updateVarDelta(Graph graph, Delta delta, TempDescriptor tmp, HashSet<Edge> edgestoAdd, HashSet<Edge> edgestoRemove) {
460     HashSet<Edge> edgeAdd=delta.varedgeadd.get(tmp);
461     HashSet<Edge> edgeRemove=delta.varedgeremove.get(tmp);
462     HashSet<Edge> existingEdges=graph.getEdges(tmp);
463     for(Edge e: edgestoRemove) {
464       //remove edge from delta
465       edgeAdd.remove(e);
466       //if the edge is already in the graph, add an explicit remove to the delta
467       if (existingEdges.contains(e))
468         edgeRemove.add(e);
469     }
470     for(Edge e: edgestoAdd) {
471       //Remove the edge from the remove set
472       edgeRemove.remove(e);
473       //Explicitly add it to the add set unless it is already in the graph
474       if (!existingEdges.contains(e))
475         edgeAdd.add(e);
476     }
477   }
478
479   static void updateHeapDelta(Graph graph, Delta delta, HashSet<Edge> edgestoAdd, HashSet<Edge> edgestoRemove) {
480     for(Edge e: edgestoRemove) {
481       AllocNode src=e.src;
482       HashSet<Edge> edgeAdd=delta.heapedgeadd.get(src);
483       HashSet<Edge> existingEdges=graph.getEdges(src);
484       //remove edge from delta
485       edgeAdd.remove(e);
486       //if the edge is already in the graph, add an explicit remove to the delta
487       if (existingEdges.contains(e)) {
488         HashSet<Edge> edgeRemove=delta.heapedgeremove.get(src);
489         edgeRemove.add(e);
490       }
491     }
492     for(Edge e: edgestoAdd) {
493       AllocNode src=e.src;
494       HashSet<Edge> edgeRemove=delta.heapedgeremove.get(src);
495       HashSet<Edge> existingEdges=graph.getEdges(src);
496       //Remove the edge from the remove set
497       edgeRemove.remove(e);
498       //Explicitly add it to the add set unless it is already in the graph
499       if (!existingEdges.contains(e)) {
500         HashSet<Edge> edgeAdd=delta.heapedgeadd.get(src);
501         edgeAdd.add(e);
502       }
503     }
504   }
505
506   Delta processFlatNop(FlatNode node, Delta delta, Graph graph) {
507     applyDiffs(graph, delta);
508     return delta;
509   }
510
511   Delta processNewNode(FlatNew node, Delta delta, Graph graph) {
512     AllocNode summary=allocFactory.getAllocNode(node, true);
513     AllocNode single=allocFactory.getAllocNode(node, false);
514     TempDescriptor tmp=node.getDst();
515       
516     if (delta.getInit()) {
517       //Build new Edge
518       Edge e=new Edge(tmp, single);
519       //Build new Edge set
520       HashSet<Edge> newedges=new HashSet<Edge>();
521       newedges.add(e);
522       //Add it into the diffs
523       delta.varedgeadd.put(tmp, newedges);
524       //Remove the old edges
525       delta.varedgeremove.put(tmp, graph.getEdges(tmp));
526       //Apply incoming diffs to graph
527       applyDiffs(graph, delta);
528     } else {
529       /* 1. Fix up the variable edge additions */
530
531       for(Iterator<Map.Entry<TempDescriptor, HashSet<Edge>>> entryIt=delta.varedgeadd.entrySet().iterator();entryIt.hasNext();) {
532         Map.Entry<TempDescriptor, HashSet<Edge>> entry=entryIt.next();
533
534         if (entry.getKey()==tmp) {
535           /* Check if this is the tmp we overwrite */
536           entryIt.remove();
537         } else {
538           /* Otherwise, check if the target of the edge is changed... */
539           rewriteSet(entry.getValue(), graph.varMap.get(entry.getKey()), single, summary);
540         }
541       }
542       
543       /* 2. Fix up the base variable edges */
544
545       for(Iterator<Map.Entry<TempDescriptor, HashSet<Edge>>> entryIt=delta.basevaredge.entrySet().iterator();entryIt.hasNext();) {
546         Map.Entry<TempDescriptor, HashSet<Edge>> entry=entryIt.next();
547         TempDescriptor entrytmp=entry.getKey();
548         if (entrytmp==tmp) {
549           /* Check is this is the tmp we overwrite, if so add to remove set */
550           Util.relationUpdate(delta.varedgeremove, tmp, null, entry.getValue());
551         } else {
552           /* Check if the target of the edge is changed */ 
553           HashSet<Edge> newset=(HashSet<Edge>)entry.getValue().clone();
554           HashSet<Edge> removeset=shrinkSet(newset, graph.varMap.get(entrytmp), single, summary);
555           Util.relationUpdate(delta.varedgeremove, entrytmp, newset, removeset);
556           Util.relationUpdate(delta.varedgeadd, entrytmp, null, newset);
557         }
558       }
559
560
561       /* 3. Fix up heap edge additions */
562
563       HashMap<AllocNode, HashSet<Edge>> addheapedge=new HashMap<AllocNode, HashSet<Edge>>();
564       for(Iterator<Map.Entry<AllocNode, HashSet<Edge>>> entryIt=delta.heapedgeadd.entrySet().iterator();entryIt.hasNext();) {
565         Map.Entry<AllocNode, HashSet<Edge>> entry=entryIt.next();
566         HashSet<Edge> edgeset=entry.getValue();
567         AllocNode allocnode=entry.getKey();
568         if (allocnode==single) {
569           entryIt.remove();
570           rewriteSet(edgeset, graph.nodeMap.get(summary), single, summary);
571           addheapedge.put(summary, edgeset);
572         } else {
573           rewriteSet(edgeset, graph.nodeMap.get(allocnode), single, summary);
574         }
575       }
576       
577       /* Merge in diffs */
578
579       for(Map.Entry<AllocNode, HashSet<Edge>> entry:addheapedge.entrySet()) {
580         AllocNode allocnode=entry.getKey();
581         Util.relationUpdate(delta.heapedgeadd, allocnode, null, entry.getValue());
582       }
583
584       /* 4. Fix up the base heap edges */
585
586       for(Iterator<Map.Entry<AllocNode, HashSet<Edge>>> entryIt=delta.baseheapedge.entrySet().iterator();entryIt.hasNext();) {
587         Map.Entry<AllocNode, HashSet<Edge>> entry=entryIt.next();
588         HashSet<Edge> edgeset=entry.getValue();
589         AllocNode allocnode=entry.getKey();
590         if (allocnode==single) {
591           entryIt.remove();
592         }
593         AllocNode addnode=(allocnode==single)?summary:allocnode;
594
595         HashSet<Edge> newset=(HashSet<Edge>) edgeset.clone();
596         HashSet<Edge> removeset=shrinkSet(newset, graph.nodeMap.get(addnode), single, summary);
597         Util.relationUpdate(delta.heapedgeadd, addnode, null, newset);
598         Util.relationUpdate(delta.heapedgeremove, allocnode, null, removeset);
599       }
600       
601       //Apply incoming diffs to graph
602       applyDiffs(graph, delta);      
603     }
604     return delta;
605   }
606
607   void rewriteSet(HashSet<Edge> edgeset, HashSet<Edge> oldedgeset, AllocNode oldnode, AllocNode newnode) {
608     HashSet<Edge> newSet=null;
609     for(Iterator<Edge> edgeit=edgeset.iterator();edgeit.hasNext();) {
610       Edge e=edgeit.next();
611       if (e.dst==oldnode||e.src==oldnode) {
612         if (newSet==null) {
613           newSet=new HashSet<Edge>();
614         }
615         edgeit.remove();
616         if (e.dst==oldnode)
617           e.dst=newnode;
618         if (e.src==oldnode)
619           e.src=newnode;
620         if (oldedgeset==null||!oldedgeset.contains(e))
621           newSet.add(e);
622       }
623     }
624     if (newSet!=null)
625       edgeset.addAll(newSet);
626   }
627
628   /* Shrinks the incoming set to just include rewritten values.
629    * Returns a set of the original rewritten values */
630
631   HashSet<Edge> shrinkSet(HashSet<Edge> edgeset, HashSet<Edge> oldedgeset, AllocNode oldnode, AllocNode newnode) {
632     HashSet<Edge> newSet=null;
633     HashSet<Edge> removeSet=null;
634     for(Iterator<Edge> edgeit=edgeset.iterator();edgeit.hasNext();) {
635       Edge e=edgeit.next();
636       edgeit.remove();
637       if (e.dst==oldnode||e.src==oldnode) {
638         if (newSet==null) {
639           newSet=new HashSet<Edge>();
640           removeSet=new HashSet<Edge>();
641         }
642
643         removeSet.add(e.copy());
644         if (e.dst==oldnode)
645           e.dst=newnode;
646         if (e.src==oldnode)
647           e.src=newnode;
648         if (oldedgeset==null||!oldedgeset.contains(e))
649           newSet.add(e);
650       }
651     }
652     if (newSet!=null)
653       edgeset.addAll(newSet);
654     return removeSet;
655   } 
656
657   Delta applyInitDelta(Delta delta, BBlock block) {
658     //Apply delta to graph
659     boolean newGraph=false;
660     if (!bbgraphMap.containsKey(block)) {
661       bbgraphMap.put(block, new Graph(null));
662       newGraph=true;
663     }
664     Delta newdelta;
665     Graph graph=bbgraphMap.get(block);
666
667     if (newGraph) {
668       newdelta=new Delta(null, true);
669       //Add in heap edges and throw away original diff
670       graph.nodeMap.putAll(delta.heapedgeadd);
671       //Add in var edges and throw away original diff
672       graph.varMap.putAll(delta.varedgeadd);
673       //Record that this is initial set...
674     } else {
675       newdelta=new Delta(null, false);
676       //merge in heap edges and variables
677       mergeHeapEdges(graph, delta, newdelta);
678       mergeVarEdges(graph, delta, newdelta);
679       //Record that this is a diff
680       newdelta.setInit(false);
681     }
682     return newdelta;
683   }
684
685   /* This function merges in the heap edges.  It updates delta to be
686    * the difference */
687
688   void mergeHeapEdges(Graph graph, Delta delta, Delta newdelta) {
689     //Merge in edges
690     for(Map.Entry<AllocNode, HashSet<Edge>> heapedge:delta.heapedgeadd.entrySet()) {
691       AllocNode nsrc=heapedge.getKey();
692       HashSet<Edge> edges=heapedge.getValue();
693       if (!graph.nodeMap.containsKey(nsrc)) {
694         graph.nodeMap.put(nsrc, new HashSet<Edge>());
695       }
696       HashSet<Edge> dstedges=graph.nodeMap.get(nsrc);
697       HashSet<Edge> diffedges=new HashSet<Edge>();
698       for(Edge e:edges) {
699         if (!dstedges.contains(e)) {
700           //We have a new edge
701           diffedges.add(e);
702           dstedges.add(e);
703         }
704       }
705       //Done with edge set...
706       if (diffedges.size()>0) {
707         //completely new
708         newdelta.baseheapedge.put(nsrc, diffedges);
709       }
710     }
711   }
712
713   /* This function merges in the var edges.  It updates delta to be
714    * the difference */
715
716   void mergeVarEdges(Graph graph, Delta delta, Delta newdelta) {
717     //Merge in edges
718     for(Map.Entry<TempDescriptor, HashSet<Edge>> varedge:delta.varedgeadd.entrySet()) {
719       TempDescriptor tmpsrc=varedge.getKey();
720       HashSet<Edge> edges=varedge.getValue();
721       if (!graph.varMap.containsKey(tmpsrc)) {
722         graph.varMap.put(tmpsrc, new HashSet<Edge>());
723       }
724       HashSet<Edge> dstedges=graph.varMap.get(tmpsrc);
725       HashSet<Edge> diffedges=new HashSet<Edge>();
726       for(Edge e:edges) {
727         if (!dstedges.contains(e)) {
728           //We have a new edge
729           diffedges.add(e);
730           dstedges.add(e);
731         }
732       }
733       //Done with edge set...
734       if (diffedges.size()>=0) {
735         //completely new
736         newdelta.basevaredge.put(tmpsrc,diffedges);
737       }
738     }
739   }
740 }