bug fixes
[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.Liveness;
6 import Analysis.Pointer.BasicBlock.BBlock;
7 import Analysis.Pointer.AllocFactory.AllocNode;
8 import Analysis.Disjoint.Alloc;
9 import Analysis.Disjoint.Taint;
10 import Analysis.Disjoint.TaintSet;
11 import Analysis.Disjoint.Canonical;
12 import Analysis.Disjoint.HeapAnalysis;
13 import Analysis.CallGraph.CallGraph;
14 import Analysis.OoOJava.RBlockRelationAnalysis;
15 import Analysis.OoOJava.Accessible;
16 import Analysis.Disjoint.ExistPred;
17 import Analysis.Disjoint.ReachGraph;
18 import Analysis.Disjoint.EffectsAnalysis;
19 import Analysis.Disjoint.BuildStateMachines;
20 import java.io.*;
21
22
23 public class Pointer implements HeapAnalysis{
24   HashMap<FlatMethod, BasicBlock> blockMap;
25   HashMap<BBlock, Graph> bbgraphMap;
26   HashMap<FlatNode, Graph> graphMap;
27   HashMap<FlatCall, Set<BBlock>> callMap;
28   HashMap<BBlock, Set<PPoint>> returnMap;
29   HashMap<BBlock, Set<TempDescriptor>> bblivetemps;
30
31   private boolean OoOJava=false;
32   CallGraph callGraph;
33   State state;
34   TypeUtil typeUtil;
35   AllocFactory allocFactory;
36   LinkedList<Delta> toprocess;
37   TempDescriptor returntmp;
38   RBlockRelationAnalysis taskAnalysis;
39   EffectsAnalysis effectsAnalysis;
40   Accessible accessible;
41
42   public Pointer(State state, TypeUtil typeUtil, CallGraph callGraph, RBlockRelationAnalysis taskAnalysis, Liveness liveness) {
43     this(state, typeUtil);
44     this.callGraph=callGraph;
45     this.OoOJava=true;
46     this.taskAnalysis=taskAnalysis;
47     this.effectsAnalysis=new EffectsAnalysis();
48     effectsAnalysis.state=state;
49     effectsAnalysis.buildStateMachines=new BuildStateMachines();
50     accessible=new Accessible(state, callGraph, taskAnalysis, liveness);
51     accessible.doAnalysis();
52     State.logEvent("Done Writing Accessible Analysis");
53   }
54
55   public Pointer(State state, TypeUtil typeUtil) {
56     this.state=state;
57     this.blockMap=new HashMap<FlatMethod, BasicBlock>();
58     this.bbgraphMap=new HashMap<BBlock, Graph>();
59     this.bblivetemps=new HashMap<BBlock, Set<TempDescriptor>>();
60     this.graphMap=new HashMap<FlatNode, Graph>();
61     this.callMap=new HashMap<FlatCall, Set<BBlock>>();
62     this.returnMap=new HashMap<BBlock, Set<PPoint>>();
63     this.typeUtil=typeUtil;
64     this.allocFactory=new AllocFactory(state, typeUtil);
65     this.toprocess=new LinkedList<Delta>();
66     ClassDescriptor stringcd=typeUtil.getClass(TypeUtil.ObjectClass);
67     this.returntmp=new TempDescriptor("RETURNVAL", stringcd);
68   }
69
70   public EffectsAnalysis getEffectsAnalysis() {
71     return effectsAnalysis;
72   }
73
74   public BasicBlock getBBlock(FlatMethod fm) {
75     if (!blockMap.containsKey(fm)) {
76       blockMap.put(fm, BasicBlock.getBBlock(fm));
77       Hashtable<FlatNode, Set<TempDescriptor>> livemap=Liveness.computeLiveTemps(fm);
78       for(BBlock bblock:blockMap.get(fm).getBlocks()) {
79         FlatNode fn=bblock.nodes.get(0);
80         if (fn==fm) {
81           HashSet<TempDescriptor> fmset=new HashSet<TempDescriptor>();
82           fmset.addAll((List<TempDescriptor>)Arrays.asList(fm.writesTemps()));
83           bblivetemps.put(bblock, fmset);
84         } else {
85           Set<TempDescriptor> livetemps=livemap.get(fn);
86           bblivetemps.put(bblock, livetemps);
87           livetemps.add(returntmp);
88         }
89       }
90     }
91     return blockMap.get(fm);
92   }
93   
94   Delta buildInitialContext() {
95     MethodDescriptor md=typeUtil.getMain();
96     FlatMethod fm=state.getMethodFlat(md);
97     BasicBlock bb=getBBlock(fm);
98     BBlock start=bb.getStart();
99     Delta delta=new Delta(new PPoint(start), true);
100     MySet<Edge> arrayset=new MySet<Edge>();
101     MySet<Edge> varset=new MySet<Edge>();
102     Edge arrayedge=new Edge(allocFactory.StringArray, null, allocFactory.Strings);
103     Edge stringedge=new Edge(fm.getParameter(0), allocFactory.StringArray);
104     delta.addHeapEdge(arrayedge);
105     delta.addVarEdge(stringedge);
106
107     return delta;
108   }
109
110
111   public Graph getGraph(FlatNode fn) {
112     return graphMap.get(fn);
113   }
114
115   public void doAnalysis() {
116
117     toprocess.add(buildInitialContext());
118     nextdelta:
119     while(!toprocess.isEmpty()) {
120       Delta delta=toprocess.remove();
121       PPoint ppoint=delta.getBlock();
122       BBlock bblock=ppoint.getBBlock();
123       Vector<FlatNode> nodes=bblock.nodes();
124       int startindex=0;
125
126       if (ppoint.getIndex()==-1) {
127         //Build base graph for entrance to this basic block
128         //System.out.println("Processing "+bblock.nodes.get(0).toString().replace(' ','_'));
129         //delta.print();
130         delta=applyInitDelta(delta, bblock);
131         //System.out.println("Generating:");
132         //delta.print();
133       } else {
134         //System.out.println("Processing Call "+bblock.nodes.get(ppoint.getIndex()).toString().replace(' ','_'));
135         //delta.print();
136
137         startindex=ppoint.getIndex()+1;
138         delta=applyCallDelta(delta, bblock);
139         //System.out.println("Generating:");
140         //delta.print();
141       }
142       Graph graph=bbgraphMap.get(bblock);
143       Graph nodeGraph=null;
144       boolean init=delta.getInit();
145       if (!init&&delta.isEmpty())
146         continue nextdelta;
147       
148       //Compute delta at exit of each node
149       for(int i=startindex; i<nodes.size();i++) {
150         FlatNode currNode=nodes.get(i);
151         //System.out.println("Start Processing "+currNode);
152         if (!graphMap.containsKey(currNode)) {
153           if (isNEEDED(currNode))
154             graphMap.put(currNode, new Graph(graph));
155           else {
156             if (i==0) {
157               //base graph works for us
158               graphMap.put(currNode, new Graph(graph));
159             } else {
160               //just use previous graph
161               graphMap.put(currNode, graphMap.get(nodes.get(i-1)));
162             }
163           }
164         }
165         nodeGraph=graphMap.get(currNode);
166         delta=processNode(bblock, i, currNode, delta, nodeGraph);
167         //System.out.println("Processing "+currNode+" and generating delta:");
168         //delta.print();
169       }
170       generateFinalDelta(bblock, delta, nodeGraph);
171     }
172
173     //DEBUG
174     if (true) {
175       int debugindex=0;
176       for(Map.Entry<BBlock, Graph> e:bbgraphMap.entrySet()) {
177         Graph g=e.getValue();
178         plotGraph(g,"BB"+e.getKey().nodes.get(0).toString().replace(' ','_'));
179         debugindex++;
180       }
181       
182       for(FlatMethod fm:blockMap.keySet()) {
183         System.out.println(fm.printMethod());
184       }
185       for(Map.Entry<FlatNode, Graph> e:graphMap.entrySet()) {
186         FlatNode fn=e.getKey();
187         Graph g=e.getValue();
188         plotGraph(g,"FN"+fn.toString()+debugindex);
189         debugindex++;
190       } 
191     }
192
193     State.logEvent("Done With Pointer Analysis");
194
195
196     if (OoOJava) {
197       effectsAnalysis.buildStateMachines.writeStateMachines();
198       State.logEvent("Done Writing State Machines");
199     }
200   }
201
202   void plotGraph(Graph g, String name) {
203     try {
204       PrintWriter pw=new PrintWriter(new FileWriter(name.toString().replace(' ','_')+".dot"));
205       g.printGraph(pw, name);
206       pw.close();
207     } catch (Exception ex) {
208       ex.printStackTrace();
209     }
210   }
211   
212
213   /* This function builds the last delta for a basic block.  It
214    * handles the case for the first time the basic block is
215    * evaluated.*/
216
217   void buildInitDelta(Graph graph, Delta newDelta) {
218     //First compute the set of temps
219     HashSet<TempDescriptor> tmpSet=new HashSet<TempDescriptor>();
220     tmpSet.addAll(graph.varMap.keySet());
221     tmpSet.addAll(graph.parent.varMap.keySet());
222     
223     //Next build the temp map part of the delta
224     for(TempDescriptor tmp:tmpSet) {
225       MySet<Edge> edgeSet=new MySet<Edge>();
226       /* Get target set */
227       if (graph.varMap.containsKey(tmp))
228         edgeSet.addAll(graph.varMap.get(tmp));
229       else
230         edgeSet.addAll(graph.parent.varMap.get(tmp));
231       newDelta.varedgeadd.put(tmp, edgeSet);
232     }
233     
234     //Next compute the set of src allocnodes
235     HashSet<AllocNode> nodeSet=new HashSet<AllocNode>();
236     nodeSet.addAll(graph.nodeMap.keySet());
237     nodeSet.addAll(graph.parent.nodeMap.keySet());
238     
239     for(AllocNode node:nodeSet) {
240       MySet<Edge> edgeSet=new MySet<Edge>();
241       /* Get edge set */
242       if (graph.nodeMap.containsKey(node))
243         edgeSet.addAll(graph.nodeMap.get(node));
244       else
245         edgeSet.addAll(graph.parent.nodeMap.get(node));
246       newDelta.heapedgeadd.put(node, edgeSet);
247       
248       /* Compute ages */
249       if (graph.oldNodes.containsKey(node)) {
250
251       } else if (graph.parent.oldNodes.containsKey(node)) {
252         //parent graphs only contain true...no need to check
253         newDelta.addOldNodes.put(node, Boolean.TRUE);
254       }
255     }
256     
257     for(AllocNode node:graph.oldNodes.keySet()) {
258       if (graph.oldNodes.get(node).booleanValue())
259         newDelta.addOldNodes.put(node, Boolean.TRUE);
260     }
261
262     for(AllocNode node:graph.parent.oldNodes.keySet()) {
263       //make sure child doesn't override
264       if (!graph.oldNodes.containsKey(node))
265         newDelta.addOldNodes.put(node, Boolean.TRUE);
266     }
267
268     newDelta.addNodeAges.addAll(graph.nodeAges);
269     newDelta.addNodeAges.addAll(graph.parent.nodeAges);
270   }
271
272   /* This function build the delta for the exit of a basic block. */
273
274   void generateFinalDelta(BBlock bblock, Delta delta, Graph graph) {
275     Delta newDelta=new Delta(null, false);
276     if (delta.getInit()) {
277       buildInitDelta(graph, newDelta);
278     } else {
279       /* We can break the old delta...it is done being used */
280       /* First we will build variable edges */
281       HashSet<TempDescriptor> tmpSet=new HashSet<TempDescriptor>();
282       tmpSet.addAll(delta.basevaredge.keySet());
283       tmpSet.addAll(delta.varedgeadd.keySet());
284       for(TempDescriptor tmp:tmpSet) {
285         /* Start with the new incoming edges */
286         MySet<Edge> newbaseedge=delta.basevaredge.get(tmp);
287         /* Remove the remove set */
288         if (newbaseedge==null)
289           newbaseedge=new MySet<Edge>();
290         newbaseedge.removeAll(delta.varedgeremove.get(tmp));
291         /* Add in the new set*/
292         newbaseedge.addAll(delta.varedgeadd.get(tmp));
293         /* Store the results */
294         newDelta.varedgeadd.put(tmp, newbaseedge);
295       }
296       delta.basevaredge.clear();
297
298       /* Next we build heap edges */
299       HashSet<AllocNode> nodeSet=new HashSet<AllocNode>();
300       nodeSet.addAll(delta.baseheapedge.keySet());
301       nodeSet.addAll(delta.heapedgeadd.keySet());
302       nodeSet.addAll(delta.heapedgeremove.keySet());
303       for(AllocNode node:nodeSet) {
304         /* Start with the new incoming edges */
305         MySet<Edge> newheapedge=new MySet<Edge>(delta.baseheapedge.get(node));
306         /* Remove the remove set */
307         MySet<Edge> removeset=delta.heapedgeremove.get(node);
308
309         if (removeset!=null)
310           newheapedge.removeAll(removeset);
311
312         /* Add in the add set */
313         MySet<Edge> settoadd=delta.heapedgeadd.get(node);
314         if (settoadd!=null)
315           newheapedge.addAll(settoadd);
316         newDelta.heapedgeadd.put(node, newheapedge);
317
318         /* Remove the newly created edges..no need to propagate a diff for those */
319         if (removeset!=null) {
320           removeset.removeAll(delta.baseheapedge.get(node));
321           newDelta.heapedgeremove.put(node, removeset);
322         }
323       }
324
325       /* Compute new ages */
326       newDelta.addNodeAges.addAll(delta.baseNodeAges);
327       newDelta.addNodeAges.addAll(delta.addNodeAges);
328       HashSet<AllocNode> oldNodes=new HashSet<AllocNode>();
329
330       /* Compute whether old nodes survive */
331       oldNodes.addAll(delta.baseOldNodes.keySet());
332       oldNodes.addAll(delta.addOldNodes.keySet());
333       for(AllocNode node:oldNodes) {
334         if (delta.addOldNodes.containsKey(node)) {
335           if (delta.addOldNodes.get(node).booleanValue()) {
336             newDelta.addOldNodes.put(node, Boolean.TRUE);
337           }
338         } else {
339           if (delta.baseOldNodes.get(node).booleanValue()) {
340             newDelta.addOldNodes.put(node, Boolean.TRUE);
341           }
342         }
343       }
344     }
345
346     /* Now we need to propagate newdelta */
347     if (!newDelta.heapedgeadd.isEmpty()||!newDelta.heapedgeremove.isEmpty()||!newDelta.varedgeadd.isEmpty()||!newDelta.addNodeAges.isEmpty()||!newDelta.addOldNodes.isEmpty()) {
348       /* We have a delta to propagate */
349       if (returnMap.containsKey(bblock)) {
350         //exit of call block
351         boolean first=true;
352
353         for(PPoint caller:returnMap.get(bblock)) {
354           //System.out.println("Sending Return BBlock to "+caller.getBBlock().nodes.get(caller.getIndex()).toString().replace(' ','_'));
355           //newDelta.print();
356           if (first) {
357             newDelta.setBlock(caller);
358             toprocess.add(newDelta);
359             first=false;
360           } else {
361             Delta d=newDelta.diffBlock(caller);
362             toprocess.add(d);
363           }
364         }
365       } else {
366         //normal block
367         Vector<BBlock> blockvector=bblock.next();
368         for(int i=0;i<blockvector.size();i++) {
369           //System.out.println("Sending BBlock to "+blockvector.get(i).nodes.get(0).toString().replace(' ','_'));
370           //newDelta.print();
371           if (i==0) {
372             newDelta.setBlock(new PPoint(blockvector.get(i)));
373             toprocess.add(newDelta);
374           } else {
375             Delta d=newDelta.diffBlock(new PPoint(blockvector.get(i)));
376             toprocess.add(d);
377           }
378         }
379       }
380     } else {
381       //System.out.println("EMPTY DELTA");
382       //System.out.println("delta");
383       //delta.print();
384       //System.out.println("newDelta");
385       //newDelta.print();
386     }
387   }
388
389   boolean isNEEDED(FlatNode node) {
390     switch(node.kind()) {
391     case FKind.FlatSetFieldNode: {
392       FlatSetFieldNode n=(FlatSetFieldNode)node;
393       return n.getSrc().getType().isPtr();
394     }
395     case FKind.FlatSetElementNode: {
396       FlatSetElementNode n=(FlatSetElementNode)node;
397       return n.getSrc().getType().isPtr();
398     }
399     case FKind.FlatFieldNode: {
400       FlatFieldNode n=(FlatFieldNode)node;
401       return n.getDst().getType().isPtr();
402     }
403     case FKind.FlatElementNode: {
404       FlatElementNode n=(FlatElementNode)node;
405       return n.getDst().getType().isPtr();
406     }
407     }
408     return true;
409   }
410
411   Delta processNode(BBlock bblock, int index, FlatNode node, Delta delta, Graph newgraph) {
412     switch(node.kind()) {
413     case FKind.FlatNew:
414       return processNewNode((FlatNew)node, delta, newgraph);
415     case FKind.FlatFieldNode:
416     case FKind.FlatElementNode:
417       return processFieldElementNode(node, delta, newgraph);
418     case FKind.FlatCastNode:
419     case FKind.FlatOpNode:
420     case FKind.FlatReturnNode:
421       return processCopyNode(node, delta, newgraph);
422     case FKind.FlatSetFieldNode:
423     case FKind.FlatSetElementNode:
424       return processSetFieldElementNode(node, delta, newgraph);
425     case FKind.FlatSESEEnterNode:
426       return processSESEEnterNode((FlatSESEEnterNode) node, delta, newgraph);
427     case FKind.FlatSESEExitNode:
428       return processSESEExitNode((FlatSESEExitNode) node, delta, newgraph);
429     case FKind.FlatMethod:
430     case FKind.FlatExit:
431     case FKind.FlatBackEdge:
432     case FKind.FlatGenReachNode:
433       return processFlatNop(node, delta, newgraph);
434     case FKind.FlatCall:
435       return processFlatCall(bblock, index, (FlatCall) node, delta, newgraph);
436     default:
437       throw new Error("Unrecognized node:"+node);
438     }
439   }
440
441   Delta processSESEEnterNode(FlatSESEEnterNode sese, Delta delta, Graph graph) {
442     if (!OoOJava)
443       return processFlatNop(sese, delta, graph);
444     if (delta.getInit()) {
445       removeInitTaints(null, delta, graph);
446       for (TempDescriptor tmp:sese.getInVarSet()) {
447         Taint taint=Taint.factory(sese,  null, tmp, AllocFactory.dummySite, sese, ReachGraph.predsEmpty);
448         MySet<Edge> edges=GraphManip.getEdges(graph, delta, tmp);
449         for(Edge e:edges) {
450           Edge newe=e.addTaint(taint);
451           delta.addVarEdge(newe);
452         }
453       }
454     } else {
455       removeDiffTaints(null, delta);
456       for (TempDescriptor tmp:sese.getInVarSet()) {
457         Taint taint=Taint.factory(sese,  null, tmp, AllocFactory.dummySite, sese, ReachGraph.predsEmpty);
458         MySet<Edge> edges=GraphManip.getDiffEdges(delta, tmp);
459         for(Edge e:edges) {
460           Edge newe=e.addTaint(taint);
461           delta.addVarEdge(newe);
462         }
463       }
464     }
465
466
467     applyDiffs(graph, delta);
468     return delta;
469   }
470   
471   private boolean isRecursive(FlatSESEEnterNode sese) {
472     MethodDescriptor md=sese.getmdEnclosing();
473     boolean isrecursive=callGraph.getCalleeSet(md).contains(md);
474     return isrecursive;
475   }
476
477   Delta processSESEExitNode(FlatSESEExitNode seseexit, Delta delta, Graph graph) {
478     if (!OoOJava)
479       return processFlatNop(seseexit, delta, graph);
480     FlatSESEEnterNode sese=seseexit.getFlatEnter();
481     //Strip Taints from this SESE
482     if (delta.getInit()) {
483       removeInitTaints(isRecursive(sese)?null:sese, delta, graph);
484     } else {
485       removeDiffTaints(isRecursive(sese)?null:sese, delta);
486     }
487     applyDiffs(graph, delta);
488     return delta;
489   }
490   
491   void removeDiffTaints(FlatSESEEnterNode sese, Delta delta) {
492     //Start with variable edges
493     {
494       MySet<Edge> edgestoadd=new MySet<Edge>();
495       MySet<Edge> edgestoremove=new MySet<Edge>();
496       
497       //Process base diff edges
498       processEdgeMap(sese, delta.basevaredge, null, delta.varedgeremove, edgestoremove, edgestoadd); 
499       //Process delta edges
500       processEdgeMap(sese, delta.varedgeadd, null, null, edgestoremove, edgestoadd); 
501       for(Edge e:edgestoremove) {
502         delta.removeVarEdge(e);
503       }
504       for(Edge e:edgestoadd) {
505         delta.addVarEdge(e);
506       }
507     }
508
509     //Now do heap edges
510     {
511       MySet<Edge> edgestoadd=new MySet<Edge>();
512       MySet<Edge> edgestoremove=new MySet<Edge>();
513
514       //Process base diff edges
515       processEdgeMap(sese, delta.baseheapedge, null, delta.heapedgeremove, edgestoremove, edgestoadd); 
516       //Process delta edges
517       processEdgeMap(sese, delta.heapedgeadd, null, null, edgestoremove, edgestoadd); 
518       for(Edge e:edgestoremove) {
519         delta.removeHeapEdge(e);
520       }
521       for(Edge e:edgestoadd) {
522         delta.addHeapEdge(e);
523       }
524     }
525   }
526
527   void removeInitTaints(FlatSESEEnterNode sese, Delta delta, Graph graph) {
528     //Start with variable edges
529     {
530       MySet<Edge> edgestoadd=new MySet<Edge>();
531       MySet<Edge> edgestoremove=new MySet<Edge>();
532       
533       //Process parent edges
534       processEdgeMap(sese, graph.parent.varMap, graph.varMap, delta.varedgeremove, edgestoremove, edgestoadd);
535       //Process graph edges
536       processEdgeMap(sese, graph.varMap, null, delta.varedgeremove, edgestoremove, edgestoadd); 
537       //Process delta edges
538       processEdgeMap(sese, delta.varedgeadd, null, null, edgestoremove, edgestoadd); 
539       for(Edge e:edgestoremove) {
540         delta.removeVarEdge(e);
541       }
542       for(Edge e:edgestoadd) {
543         delta.addVarEdge(e);
544       }
545     }
546
547     //Now do heap edges
548     {
549       MySet<Edge> edgestoadd=new MySet<Edge>();
550       MySet<Edge> edgestoremove=new MySet<Edge>();
551
552       //Process parent edges
553       processEdgeMap(sese, graph.parent.nodeMap, graph.nodeMap, delta.heapedgeremove, edgestoremove, edgestoadd);
554       //Process graph edges
555       processEdgeMap(sese, graph.nodeMap, null, delta.heapedgeremove, edgestoremove, edgestoadd); 
556       //Process delta edges
557       processEdgeMap(sese, delta.heapedgeadd, null, null, edgestoremove, edgestoadd); 
558       for(Edge e:edgestoremove) {
559         delta.removeHeapEdge(e);
560       }
561       for(Edge e:edgestoadd) {
562         delta.addHeapEdge(e);
563       }
564     }
565   }
566
567   void processEdgeMap(FlatSESEEnterNode sese, HashMap<?, MySet<Edge>> edgemap, HashMap<?, MySet<Edge>> childmap, HashMap<?, MySet<Edge>> removemap, MySet<Edge> edgestoremove, MySet<Edge> edgestoadd) {
568     for(Map.Entry<?, MySet<Edge>> entry:edgemap.entrySet()) {
569       //If the parent map exists and overrides this entry, skip it
570       if (childmap!=null&&childmap.containsKey(entry.getKey()))
571         continue;
572       for(Edge e:entry.getValue()) {
573         //check whether this edge has been removed
574         if (removemap!=null&&removemap.containsKey(entry.getKey())&&
575             removemap.get(entry.getKey()).contains(e))
576           continue;
577         //have real edge
578         TaintSet ts=e.getTaints();
579         TaintSet newts=null;
580         //update non-null taint set
581         if (ts!=null)
582           newts=Canonical.removeInContextTaintsNP(ts, sese);
583         if (newts!=null) {
584           edgestoremove.add(e);
585           edgestoadd.add(e.changeTaintSet(newts));
586         }
587       }
588     }
589   }
590
591   /* This function compute the edges for the this variable for a
592    * callee if it exists. */
593
594   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) {
595     //Handle the this temp
596     if (tmpthis!=null) {
597       MySet<Edge> edges=(oldnodeset!=null)?GraphManip.getDiffEdges(delta, tmpthis):GraphManip.getEdges(graph, delta, tmpthis);
598       newDelta.varedgeadd.put(tmpthis, (MySet<Edge>) edges.clone());
599       edgeset.addAll(edges);
600       for(Edge e:edges) {
601         AllocNode dstnode=e.dst;
602         if (!nodeset.contains(dstnode)&&(oldnodeset==null||!oldnodeset.contains(dstnode))) {
603           TypeDescriptor type=dstnode.getType();
604           if (!type.isArray()) {
605             targetSet.add(type.getClassDesc());
606           } else {
607             //arrays don't have code
608             targetSet.add(typeUtil.getClass(TypeUtil.ObjectClass));
609           }
610           nodeset.add(dstnode);
611           tovisit.add(dstnode);
612         }
613       }
614     }
615   }
616
617   /* This function compute the edges for a call's parameters. */
618
619   void processParams(Graph graph, Delta delta, Delta newDelta, HashSet<AllocNode> nodeset, Stack<AllocNode> tovisit, MySet<Edge> edgeset, FlatCall fcall, boolean diff) {
620     //Go through each temp
621     for(int i=0;i<fcall.numArgs();i++) {
622       TempDescriptor tmp=fcall.getArg(i);
623       MySet<Edge> edges=diff?GraphManip.getDiffEdges(delta, tmp):GraphManip.getEdges(graph, delta, tmp);
624       newDelta.varedgeadd.put(tmp, (MySet<Edge>) edges.clone());
625       edgeset.addAll(edges);
626       for(Edge e:edges) {
627         if (!nodeset.contains(e.dst)) {
628           nodeset.add(e.dst);
629           tovisit.add(e.dst);
630         }
631       }
632     }
633   }
634
635   /* This function computes the reachable nodes for a callee. */
636
637   void computeReachableNodes(Graph graph, Delta delta, Delta newDelta, HashSet<AllocNode> nodeset, Stack<AllocNode> tovisit, MySet<Edge> edgeset, HashSet<AllocNode> oldnodeset) {
638       while(!tovisit.isEmpty()) {
639         AllocNode node=tovisit.pop();
640         MySet<Edge> edges=GraphManip.getEdges(graph, delta, node);
641         if (!edges.isEmpty()) {
642           newDelta.heapedgeadd.put(node, edges);
643           edgeset.addAll(edges);
644           for(Edge e:edges) {
645             if (!nodeset.contains(e.dst)&&(oldnodeset==null||!oldnodeset.contains(e.dst))) {
646               nodeset.add(e.dst);
647               tovisit.add(e.dst);
648             }
649           }
650         }
651       }
652   }
653
654   HashSet<MethodDescriptor> computeTargets(FlatCall fcall, Delta newDelta) {
655     TempDescriptor tmpthis=fcall.getThis();
656     MethodDescriptor md=fcall.getMethod();
657     HashSet<MethodDescriptor> targets=new HashSet<MethodDescriptor>();
658     if (md.isStatic()) {
659       targets.add(md);
660     } else {
661       //Compute Edges
662       for(Edge e:newDelta.varedgeadd.get(tmpthis)) {
663         AllocNode node=e.dst;
664         ClassDescriptor cd=node.getType().getClassDesc();
665         //Figure out exact method called and add to set
666         MethodDescriptor calledmd=cd.getCalledMethod(md);
667         targets.add(calledmd);
668       }
669     }
670     return targets;
671   }
672
673   void fixMapping(FlatCall fcall, HashSet<MethodDescriptor> targets, MySet<Edge> oldedgeset, Delta newDelta, BBlock callblock, int callindex) {
674     Delta basedelta=null;
675     TempDescriptor tmpthis=fcall.getThis();
676
677     for(MethodDescriptor calledmd:targets) {
678       FlatMethod fm=state.getMethodFlat(calledmd);
679       boolean newmethod=false;
680       
681       //Build tmpMap
682       HashMap<TempDescriptor, TempDescriptor> tmpMap=new HashMap<TempDescriptor, TempDescriptor>();
683       int offset=0;
684       if(tmpthis!=null) {
685         tmpMap.put(tmpthis, fm.getParameter(offset++));
686       }
687       for(int i=0;i<fcall.numArgs();i++) {
688         TempDescriptor tmp=fcall.getArg(i);
689         tmpMap.put(tmp,fm.getParameter(i+offset));
690       }
691
692       //Get basicblock for the method
693       BasicBlock block=getBBlock(fm);
694       
695       //Hook up exits
696       if (!callMap.containsKey(fcall)) {
697         callMap.put(fcall, new HashSet<BBlock>());
698       }
699       
700       Delta returnDelta=null;
701       if (!callMap.get(fcall).contains(block.getStart())) {
702         callMap.get(fcall).add(block.getStart());
703         newmethod=true;
704         
705         //Hook up return
706         if (!returnMap.containsKey(block.getExit())) {
707           returnMap.put(block.getExit(), new HashSet<PPoint>());
708         }
709         returnMap.get(block.getExit()).add(new PPoint(callblock, callindex));
710         
711         if (bbgraphMap.containsKey(block.getExit())) {
712           //Need to push existing results to current node
713           if (returnDelta==null) {
714             returnDelta=new Delta(null, false);
715             Vector<FlatNode> exitblocknodes=block.getExit().nodes();
716             FlatExit fexit=(FlatExit)exitblocknodes.get(exitblocknodes.size()-1);
717             buildInitDelta(graphMap.get(fexit), returnDelta);
718             if (!returnDelta.heapedgeadd.isEmpty()||!returnDelta.heapedgeremove.isEmpty()||!returnDelta.varedgeadd.isEmpty()) {
719               returnDelta.setBlock(new PPoint(callblock, callindex));
720               toprocess.add(returnDelta);
721             }
722           } else {
723             if (!returnDelta.heapedgeadd.isEmpty()||!returnDelta.heapedgeremove.isEmpty()||!returnDelta.varedgeadd.isEmpty()) {
724               toprocess.add(returnDelta.diffBlock(new PPoint(callblock, callindex)));
725             }
726           }
727         }
728       }
729       
730       if (oldedgeset==null) {
731         //First build of this graph
732         //Build and enqueue delta...safe to just use existing delta
733         Delta d=newDelta.changeParams(tmpMap, new PPoint(block.getStart()));
734         //System.out.println("AProcessing "+block.getStart().nodes.get(0).toString().replace(' ','_'));
735         //d.print();
736         toprocess.add(d);
737       } else if (newmethod) {
738         if (basedelta==null) {
739           basedelta=newDelta.buildBase(oldedgeset);
740         }
741         //Build and enqueue delta
742         Delta d=basedelta.changeParams(tmpMap, new PPoint(block.getStart()));
743         //System.out.println("BProcessing "+block.getStart().nodes.get(0).toString().replace(' ','_'));
744         //d.print();
745         toprocess.add(d);
746       } else  {
747         //Build and enqueue delta
748         Delta d=newDelta.changeParams(tmpMap, new PPoint(block.getStart()));
749         //System.out.println("CProcessing "+block.getStart().nodes.get(0).toString().replace(' ','_'));
750         //d.print();
751         toprocess.add(d);
752       }
753     }
754   }
755
756
757   /* This function computes all edges that start outside of the callee
758    * context and go into the callee context */
759
760   void computeExternalEdges(Graph graph, Delta delta, HashSet<AllocNode> nodeset, HashSet<AllocNode> deltaset, MySet<Edge> externaledgeset) {
761     //Do heap edges first
762     HashSet<AllocNode> externalnodes=new HashSet<AllocNode>();
763     externalnodes.addAll(delta.baseheapedge.keySet());
764     externalnodes.addAll(delta.heapedgeadd.keySet());
765     externalnodes.addAll(delta.heapedgeremove.keySet());
766     //remove allinternal nodes
767     externalnodes.removeAll(nodeset);
768     for(AllocNode extNode:externalnodes) {
769       //Compute set of edges from given node
770       MySet<Edge> edges=new MySet<Edge>(delta.baseheapedge.get(extNode));
771       edges.removeAll(delta.heapedgeremove.get(extNode));
772       edges.addAll(delta.heapedgeadd.get(extNode));
773       
774       for(Edge e:edges) {
775         if (nodeset.contains(e.dst))
776           externaledgeset.add(e);
777       }
778     }
779
780     //Do var edges now
781     HashSet<TempDescriptor> temps=new HashSet<TempDescriptor>();
782     temps.addAll(delta.basevaredge.keySet());
783     temps.addAll(delta.varedgeadd.keySet());
784     temps.addAll(delta.varedgeremove.keySet());
785     //remove allinternal nodes
786     temps.removeAll(nodeset);
787     
788     for(TempDescriptor tmp:temps) {
789       //Compute set of edges from given node
790       MySet<Edge> edges=new MySet<Edge>(delta.basevaredge.get(tmp));
791       
792       edges.removeAll(delta.varedgeremove.get(tmp));
793       edges.addAll(delta.varedgeadd.get(tmp));
794       
795       for(Edge e:edges) {
796         if (nodeset.contains(e.dst))
797           externaledgeset.add(e);
798       }
799     }
800   }
801
802   /* This function removes the caller reachable edges from the
803    * callee's heap. */
804   
805   void removeEdges(Graph graph, Delta delta, HashSet<AllocNode> nodeset, MySet<Edge> edgeset, MySet<Edge> externaledgeset) {
806     //Want to remove the set of internal edges
807     for(Edge e:edgeset) {
808       if (e.src!=null&&!graph.callerEdges.contains(e)) {
809         delta.removeHeapEdge(e);
810       }
811     }
812
813     //Want to remove the set of external edges
814     for(Edge e:externaledgeset) {
815       //want to remove the set of internal edges
816       if (!graph.callerEdges.contains(e))
817         delta.removeEdge(e);
818     }
819   }
820
821   Delta processFlatCall(BBlock callblock, int callindex, FlatCall fcall, Delta delta, Graph graph) {
822     Delta newDelta=new Delta(null, false);
823
824     if (delta.getInit()) {
825       MySet<Edge> edgeset=new MySet<Edge>();
826       MySet<Edge> externaledgeset=new MySet<Edge>();
827       HashSet<AllocNode> nodeset=new HashSet<AllocNode>();
828       HashSet<ClassDescriptor> targetSet=new HashSet<ClassDescriptor>();
829       Stack<AllocNode> tovisit=new Stack<AllocNode>();
830       TempDescriptor tmpthis=fcall.getThis();
831       graph.callerEdges=new MySet<Edge>();
832
833       //Handle the this temp
834       processThisTargets(targetSet, graph, delta, newDelta, nodeset, tovisit, edgeset, tmpthis, null);
835
836       //Go through each temp
837       processParams(graph, delta, newDelta, nodeset, tovisit, edgeset, fcall, false);
838       
839       //Traverse all reachable nodes
840       computeReachableNodes(graph, delta, newDelta, nodeset, tovisit, edgeset, null);
841
842       //Compute call targets
843       HashSet<MethodDescriptor> newtargets=computeTargets(fcall, newDelta);
844
845       //Fix mapping
846       fixMapping(fcall, newtargets, null, newDelta, callblock, callindex);
847
848       //Compute edges into region to splice out
849       computeExternalEdges(graph, delta, nodeset, null, externaledgeset);
850
851       //Splice out internal edges
852       removeEdges(graph, delta, nodeset, edgeset, externaledgeset);
853
854       //store data structures
855       graph.externalEdgeSet=externaledgeset;
856       graph.reachNode=nodeset;
857       graph.reachEdge=edgeset;
858       
859       graph.callTargets=newtargets;
860       graph.callNodeAges=new HashSet<AllocNode>();
861       graph.callOldNodes=new HashSet<AllocNode>();
862
863       //Apply diffs to graph
864       applyDiffs(graph, delta, true);
865     } else {
866       MySet<Edge> edgeset=new MySet<Edge>();
867       MySet<Edge> externaledgeset=new MySet<Edge>();
868       HashSet<AllocNode> nodeset=new HashSet<AllocNode>();
869       MySet<Edge> oldedgeset=graph.reachEdge;
870       HashSet<AllocNode> oldnodeset=graph.reachNode;
871
872       HashSet<ClassDescriptor> targetSet=new HashSet<ClassDescriptor>();
873       Stack<AllocNode> tovisit=new Stack<AllocNode>();
874       TempDescriptor tmpthis=fcall.getThis();
875       //Fix up delta to get rid of unnecessary heap edge removals
876       for(Map.Entry<AllocNode, MySet<Edge>> entry:delta.heapedgeremove.entrySet()) {
877         for(Iterator<Edge> eit=entry.getValue().iterator();eit.hasNext();) {
878           Edge e=eit.next();
879           if (graph.callerEdges.contains(e))
880             eit.remove();
881         }
882       }
883
884       //Fix up delta to get rid of unnecessary var edge removals
885       for(Map.Entry<TempDescriptor, MySet<Edge>> entry:delta.varedgeremove.entrySet()) {
886         for(Iterator<Edge> eit=entry.getValue().iterator();eit.hasNext();) {
887           Edge e=eit.next();
888           if (graph.callerEdges.contains(e))
889             eit.remove();
890         }
891       }
892       
893       //Handle the this temp
894       processThisTargets(targetSet, graph, delta, newDelta, nodeset, tovisit, edgeset, tmpthis, oldnodeset);
895
896       //Go through each temp
897       processParams(graph, delta, newDelta, nodeset, tovisit, edgeset, fcall, true);
898       //Go through each new heap edge that starts from old node
899       MySet<Edge> newedges=GraphManip.getDiffEdges(delta, oldnodeset);
900       edgeset.addAll(newedges);
901       for(Edge e:newedges) {
902         //Add new edges that start from old node to newDelta
903         AllocNode src=e.src;
904         if (!newDelta.heapedgeadd.containsKey(src)) {
905           newDelta.heapedgeadd.put(src, new MySet<Edge>());
906         }
907         newDelta.heapedgeadd.get(src).add(e);
908         if (!nodeset.contains(e.dst)&&!oldnodeset.contains(e.dst)) {
909           nodeset.add(e.dst);
910           tovisit.add(e.dst);
911         }
912       }
913
914       //Traverse all reachable nodes
915       computeReachableNodes(graph, delta, newDelta, nodeset, tovisit, edgeset, oldnodeset);
916       //Compute call targets
917       HashSet<MethodDescriptor> newtargets=computeTargets(fcall, newDelta);
918       graph.callTargets.addAll(newtargets);
919       //add in new nodeset and edgeset
920       oldnodeset.addAll(nodeset);
921       oldedgeset.addAll(edgeset);
922       //Fix mapping
923       fixMapping(fcall, graph.callTargets, oldedgeset, newDelta, callblock, callindex);
924       //Compute edges into region to splice out
925       computeExternalEdges(graph, delta, oldnodeset, nodeset, externaledgeset);
926
927       //Splice out internal edges
928       removeEdges(graph, delta, nodeset, edgeset, externaledgeset);
929
930       //Add external edges back in
931       processCallExternal(graph, delta, externaledgeset);
932
933       //Move new edges that should be summarized
934       processSummarization(graph, delta);
935       
936       //Add in new external edges
937       graph.externalEdgeSet.addAll(externaledgeset);
938       //Apply diffs to graph
939       applyDiffs(graph, delta);
940     }
941     return delta;
942   }
943
944   void processSummarization(Graph graph, Delta delta) {
945     processSumHeapEdgeSet(delta.heapedgeadd, delta, graph);
946     processSumHeapEdgeSet(delta.baseheapedge, delta, graph);
947     processSumVarEdgeSet(delta.varedgeadd, delta, graph);
948     processSumVarEdgeSet(delta.basevaredge, delta, graph);
949   }
950
951   void processSumVarEdgeSet(HashMap<TempDescriptor, MySet<Edge>> map, Delta delta, Graph graph) {
952     MySet<Edge> edgestoadd=new MySet<Edge>();
953     MySet<Edge> edgestoremove=new MySet<Edge>();
954     for(Iterator<Map.Entry<TempDescriptor, MySet<Edge>>> eit=map.entrySet().iterator();eit.hasNext();) {
955       Map.Entry<TempDescriptor, MySet<Edge>> entry=eit.next();
956       MySet<Edge> edgeset=entry.getValue();
957
958       for(Edge e:edgeset) {
959         Edge copy=e.copy();
960         boolean rewrite=false;
961         if (copy.dst!=null&&graph.callNodeAges.contains(copy.dst)) {
962           copy.dst=allocFactory.getAllocNode(copy.dst, true);
963           rewrite=true;
964         }
965         if (rewrite) {
966           edgestoremove.add(e);
967           edgestoadd.add(copy);
968         }
969       }
970     }
971     for(Edge e:edgestoremove) {
972       delta.removeVarEdge(e);
973     }
974     for(Edge e:edgestoadd) {
975       delta.addVarEdge(e);
976     }
977   }
978   
979   public Alloc getAllocationSiteFromFlatNew(FlatNew node) {
980     return allocFactory.getAllocNode(node, false).getAllocSite();
981   }
982  
983   void processSumHeapEdgeSet(HashMap<AllocNode, MySet<Edge>> map, Delta delta, Graph graph) {
984     MySet<Edge> edgestoadd=new MySet<Edge>();
985     MySet<Edge> edgestoremove=new MySet<Edge>();
986     for(Iterator<Map.Entry<AllocNode, MySet<Edge>>> eit=map.entrySet().iterator();eit.hasNext();) {
987       Map.Entry<AllocNode, MySet<Edge>> entry=eit.next();
988       AllocNode node=entry.getKey();
989       MySet<Edge> edgeset=entry.getValue();
990
991       for(Edge e:edgeset) {
992         Edge copy=e.copy();
993         boolean rewrite=false;
994         if (copy.src!=null&&graph.callNodeAges.contains(copy.src)) {
995           copy.src=allocFactory.getAllocNode(copy.src, true);
996           rewrite=true;
997         }
998         if (copy.dst!=null&&graph.callNodeAges.contains(copy.dst)) {
999           copy.dst=allocFactory.getAllocNode(copy.dst, true);
1000           rewrite=true;
1001         }
1002         if (rewrite) {
1003           edgestoremove.add(e);
1004           edgestoadd.add(copy);
1005         }
1006       }
1007     }
1008     for(Edge e:edgestoremove) {
1009       delta.removeHeapEdge(e);
1010     }
1011     for(Edge e:edgestoadd) {
1012       delta.addHeapEdge(e);
1013     }
1014   }
1015
1016   //Handle external edges
1017   void processCallExternal(Graph graph, Delta newDelta, MySet<Edge> externalEdgeSet) {
1018     //Add external edges in
1019     for(Edge e:externalEdgeSet) {
1020       //First did we age the source
1021       Edge newedge=e.copy();
1022       if (newedge.src!=null&&!e.src.isSummary()&&graph.callNodeAges.contains(e.src)) {
1023         AllocNode summaryNode=allocFactory.getAllocNode(newedge.src, true);
1024         newedge.src=summaryNode;
1025       }
1026       //Compute target
1027       if (graph.callNodeAges.contains(e.dst)&&!e.dst.isSummary()) {
1028         if (graph.callOldNodes.contains(e.dst)) {
1029           //Need two edges
1030           Edge copy=newedge.copy();
1031           mergeEdge(graph, newDelta, copy);
1032         }
1033         //Now add summarized node
1034         newedge.dst=allocFactory.getAllocNode(newedge.dst, true);
1035         mergeCallEdge(graph, newDelta, newedge);
1036       } else {
1037         //Add edge to single node
1038         mergeEdge(graph, newDelta, newedge);
1039       }
1040     }
1041   }
1042
1043   /* This function applies callee deltas to the caller heap. */
1044
1045   Delta applyCallDelta(Delta delta, BBlock bblock) {
1046     Delta newDelta=new Delta(null, false);
1047     Vector<FlatNode> nodes=bblock.nodes();
1048     PPoint ppoint=delta.getBlock();
1049     FlatCall fcall=(FlatCall)nodes.get(ppoint.getIndex());
1050     Graph graph=graphMap.get(fcall);
1051     Graph oldgraph=(ppoint.getIndex()==0)?
1052       bbgraphMap.get(bblock):
1053       graphMap.get(nodes.get(ppoint.getIndex()-1));
1054     Set<FlatSESEEnterNode> seseCallers=OoOJava?taskAnalysis.getTransitiveExecutingRBlocks(fcall):null;
1055
1056     //Age outside nodes if necessary
1057     for(Iterator<AllocNode> nodeit=delta.addNodeAges.iterator();nodeit.hasNext();) {
1058       AllocNode node=nodeit.next();
1059       if (!graph.callNodeAges.contains(node)) {
1060         graph.callNodeAges.add(node);
1061         newDelta.addNodeAges.add(node);
1062       }
1063       if (!graph.reachNode.contains(node)&&!node.isSummary()) {
1064         /* Need to age node in existing graph*/
1065         summarizeInGraph(graph, newDelta, node);
1066       }
1067     }
1068     //Add heap edges in
1069     for(Map.Entry<AllocNode, MySet<Edge>> entry:delta.heapedgeadd.entrySet()) {
1070       for(Edge e:entry.getValue()) {
1071         boolean addedge=false;
1072         Edge edgetoadd=null;
1073         if (e.statuspredicate==Edge.NEW) {
1074           edgetoadd=e;
1075         } else {
1076           Edge origEdgeKey=e.makeStatus(allocFactory);
1077           if (oldgraph.nodeMap.containsKey(origEdgeKey.src)&&
1078               oldgraph.nodeMap.get(origEdgeKey.src).contains(origEdgeKey)) {
1079             Edge origEdge=oldgraph.nodeMap.get(origEdgeKey.src).get(origEdgeKey);
1080             //copy the predicate
1081             origEdgeKey.statuspredicate=origEdge.statuspredicate;
1082             edgetoadd=origEdgeKey;
1083           }
1084         }
1085         if (seseCallers!=null&&edgetoadd!=null)
1086           edgetoadd.taintModify(seseCallers);
1087         mergeCallEdge(graph, newDelta, edgetoadd);
1088       }
1089     }
1090     
1091     processCallExternal(graph, newDelta, graph.externalEdgeSet);
1092
1093     //Add edge for return value
1094     if (fcall.getReturnTemp()!=null) {
1095       MySet<Edge> returnedge=delta.varedgeadd.get(returntmp);
1096       if (returnedge!=null)
1097         for(Edge e:returnedge) {
1098           Edge newedge=e.copy();
1099           newedge.srcvar=fcall.getReturnTemp();
1100           if (seseCallers!=null)
1101             newedge.taintModify(seseCallers);
1102           if (graph.getEdges(fcall.getReturnTemp())==null||!graph.getEdges(fcall.getReturnTemp()).contains(newedge))
1103             newDelta.addEdge(newedge);
1104         }
1105     }
1106     applyDiffs(graph, newDelta);
1107     return newDelta;
1108   }
1109   
1110   public void mergeEdge(Graph graph, Delta newDelta, Edge edgetoadd) {
1111     if (edgetoadd!=null) {
1112       Edge match=graph.getMatch(edgetoadd);
1113
1114       if (match==null||!match.subsumes(edgetoadd)) {
1115         Edge mergededge=edgetoadd.merge(match);
1116         newDelta.addEdge(mergededge);
1117       }
1118     }
1119   }
1120
1121   /* This is a call produced edge...need to remember this */
1122
1123   public void mergeCallEdge(Graph graph, Delta newDelta, Edge edgetoadd) {
1124     if (edgetoadd!=null) {
1125       Edge match=graph.getMatch(edgetoadd);
1126
1127       if (match==null||!match.subsumes(edgetoadd)) {
1128         Edge mergededge=edgetoadd.merge(match);
1129         newDelta.addEdge(mergededge);
1130         graph.callerEdges.add(mergededge);
1131         //System.out.println("ADDING: "+ mergededge);
1132       }
1133     }
1134   }
1135
1136
1137   /* Summarizes out of context nodes in graph */
1138   void summarizeInGraph(Graph graph, Delta newDelta, AllocNode singleNode) {
1139     AllocNode summaryNode=allocFactory.getAllocNode(singleNode, true);
1140
1141     //Handle outgoing heap edges
1142     MySet<Edge> edgeset=graph.getEdges(singleNode);
1143
1144     for(Edge e:edgeset) {
1145       Edge rewrite=e.rewrite(singleNode, summaryNode);
1146       //Remove old edge
1147       newDelta.removeHeapEdge(e);
1148       mergeCallEdge(graph, newDelta, rewrite);
1149     }
1150     
1151     //Handle incoming edges
1152     MySet<Edge> backedges=graph.getBackEdges(singleNode);
1153     for(Edge e:backedges) {
1154       if (e.dst==singleNode) {
1155         //Need to get original edge so that predicate will be correct
1156         Edge match=graph.getMatch(e);
1157         if (match!=null) {
1158           Edge rewrite=match.rewrite(singleNode, summaryNode);
1159           newDelta.removeEdge(match);
1160           mergeCallEdge(graph, newDelta, rewrite);
1161         }
1162       }
1163     }
1164   }
1165
1166   void applyDiffs(Graph graph, Delta delta) {
1167     applyDiffs(graph, delta, false);
1168   }
1169
1170   void applyDiffs(Graph graph, Delta delta, boolean genbackwards) {
1171     //build backwards map if requested
1172     if (genbackwards&&graph.backMap==null) {
1173       graph.backMap=new HashMap<AllocNode, MySet<Edge>>();
1174       if (graph.parent.backMap==null) {
1175         graph.parent.backMap=new HashMap<AllocNode, MySet<Edge>>();
1176         for(Map.Entry<AllocNode, MySet<Edge>> entry:graph.nodeMap.entrySet()) {
1177           for(Edge e:entry.getValue()) {
1178             if (!graph.parent.backMap.containsKey(e.dst))
1179               graph.parent.backMap.put(e.dst, new MySet<Edge>());
1180             graph.parent.backMap.get(e.dst).add(e);
1181           }
1182         }
1183         for(Map.Entry<TempDescriptor, MySet<Edge>> entry:graph.varMap.entrySet()) {
1184           for(Edge e:entry.getValue()) {
1185             if (!graph.parent.backMap.containsKey(e.dst))
1186               graph.parent.backMap.put(e.dst, new MySet<Edge>());
1187             graph.parent.backMap.get(e.dst).add(e);
1188           }
1189         }
1190       }
1191     }
1192
1193     //Add hidden base edges
1194     for(Map.Entry<AllocNode, MySet<Edge>> e: delta.baseheapedge.entrySet()) {
1195       AllocNode node=e.getKey();
1196       MySet<Edge> edges=e.getValue();
1197       if (graph.nodeMap.containsKey(node)) {
1198         MySet<Edge> nodeEdges=graph.nodeMap.get(node);
1199         nodeEdges.addAll(edges);
1200       }
1201     }
1202
1203     //Remove heap edges
1204     for(Map.Entry<AllocNode, MySet<Edge>> e: delta.heapedgeremove.entrySet()) {
1205       AllocNode node=e.getKey();
1206       MySet<Edge> edgestoremove=e.getValue();
1207       if (graph.nodeMap.containsKey(node)) {
1208         //Just apply diff to current map
1209         graph.nodeMap.get(node).removeAll(edgestoremove);
1210       } else {
1211         //Generate diff from parent graph
1212         MySet<Edge> parentedges=graph.parent.nodeMap.get(node);
1213         if (parentedges!=null) {
1214           MySet<Edge> newedgeset=Util.setSubtract(parentedges, edgestoremove);
1215           graph.nodeMap.put(node, newedgeset);
1216         }
1217       }
1218     }
1219
1220     //Add heap edges
1221     for(Map.Entry<AllocNode, MySet<Edge>> e: delta.heapedgeadd.entrySet()) {
1222       AllocNode node=e.getKey();
1223       MySet<Edge> edgestoadd=e.getValue();
1224       //If we have not done a subtract, then 
1225       if (!graph.nodeMap.containsKey(node)) {
1226         //Copy the parent entry
1227         if (graph.parent.nodeMap.containsKey(node))
1228           graph.nodeMap.put(node, (MySet<Edge>)graph.parent.nodeMap.get(node).clone());
1229         else
1230           graph.nodeMap.put(node, new MySet<Edge>());
1231       }
1232       Edge.mergeEdgesInto(graph.nodeMap.get(node),edgestoadd);
1233       if (genbackwards) {
1234         for(Edge eadd:edgestoadd) {
1235           if (!graph.backMap.containsKey(eadd.dst))
1236             graph.backMap.put(eadd.dst, new MySet<Edge>());
1237           graph.backMap.get(eadd.dst).add(eadd);
1238         }
1239       }
1240     }
1241
1242     //Remove var edges
1243     for(Map.Entry<TempDescriptor, MySet<Edge>> e: delta.varedgeremove.entrySet()) {
1244       TempDescriptor tmp=e.getKey();
1245       MySet<Edge> edgestoremove=e.getValue();
1246
1247       if (graph.varMap.containsKey(tmp)) {
1248         //Just apply diff to current map
1249         graph.varMap.get(tmp).removeAll(edgestoremove);
1250       } else if (graph.parent.varMap.containsKey(tmp)) {
1251         //Generate diff from parent graph
1252         MySet<Edge> parentedges=graph.parent.varMap.get(tmp);
1253         MySet<Edge> newedgeset=Util.setSubtract(parentedges, edgestoremove);
1254         graph.varMap.put(tmp, newedgeset);
1255       }
1256     }
1257
1258     //Add var edges
1259     for(Map.Entry<TempDescriptor, MySet<Edge>> e: delta.varedgeadd.entrySet()) {
1260       TempDescriptor tmp=e.getKey();
1261       MySet<Edge> edgestoadd=e.getValue();
1262       if (graph.varMap.containsKey(tmp)) {
1263         Edge.mergeEdgesInto(graph.varMap.get(tmp), edgestoadd);
1264       } else 
1265         graph.varMap.put(tmp, (MySet<Edge>) edgestoadd.clone());
1266       if (genbackwards) {
1267         for(Edge eadd:edgestoadd) {
1268           if (!graph.backMap.containsKey(eadd.dst))
1269             graph.backMap.put(eadd.dst, new MySet<Edge>());
1270           graph.backMap.get(eadd.dst).add(eadd);
1271         }
1272       }
1273     }
1274
1275     //Add node additions
1276     for(AllocNode node:delta.addNodeAges) {
1277       graph.nodeAges.add(node);
1278     }
1279     
1280     for(Map.Entry<AllocNode, Boolean> nodeentry:delta.addOldNodes.entrySet()) {
1281       AllocNode node=nodeentry.getKey();
1282       Boolean ispresent=nodeentry.getValue();
1283       graph.oldNodes.put(node, ispresent);
1284     }
1285   }
1286
1287   Delta processSetFieldElementNode(FlatNode node, Delta delta, Graph graph) {
1288     TempDescriptor src;
1289     FieldDescriptor fd;
1290     TempDescriptor dst;
1291     if (node.kind()==FKind.FlatSetElementNode) {
1292       FlatSetElementNode fen=(FlatSetElementNode) node;
1293       src=fen.getSrc();
1294       fd=null;
1295       dst=fen.getDst();
1296     } else {
1297       FlatSetFieldNode ffn=(FlatSetFieldNode) node;
1298       src=ffn.getSrc();
1299       fd=ffn.getField();
1300       dst=ffn.getDst();
1301     }
1302
1303     if (delta.getInit()) {
1304       MySet<Edge> dstEdges=GraphManip.getEdges(graph, delta, dst);
1305
1306       if (OoOJava&&!accessible.isAccessible(node, dst)) {
1307         Taint dstStallTaint=Taint.factory(node,  dst, AllocFactory.dummySite, node, ReachGraph.predsEmpty);
1308         dstEdges=Edge.taintAll(dstEdges, dstStallTaint);
1309         updateVarDelta(graph, delta, dst, dstEdges, null);
1310       }
1311       if (OoOJava) {
1312         effectsAnalysis.analyzeFlatSetFieldNode(dstEdges, fd, node);
1313       }
1314
1315       //Do nothing for non pointers
1316       if (!src.getType().isPtr()) {
1317         return delta;
1318       }
1319
1320       MySet<Edge> srcEdges=GraphManip.getEdges(graph, delta, src);
1321       if (OoOJava&&!accessible.isAccessible(node, src)) {
1322         Taint srcStallTaint=Taint.factory(node,  src, AllocFactory.dummySite, node, ReachGraph.predsEmpty);
1323         srcEdges=Edge.taintAll(srcEdges, srcStallTaint);
1324         updateVarDelta(graph, delta, src, srcEdges, null);
1325       }
1326
1327       MySet<Edge> edgesToAdd=GraphManip.genEdges(dstEdges, fd, srcEdges);
1328       MySet<Edge> edgesToRemove=null;
1329       if (dstEdges.size()==1&&!dstEdges.iterator().next().dst.isSummary()&&fd!=null) {
1330         /* Can do a strong update */
1331         edgesToRemove=GraphManip.getEdges(graph, delta, dstEdges, fd);
1332         graph.strongUpdateSet=edgesToRemove;
1333       } else
1334         graph.strongUpdateSet=new MySet<Edge>();
1335
1336       /* Update diff */
1337       updateHeapDelta(graph, delta, edgesToAdd, edgesToRemove);
1338       applyDiffs(graph, delta);
1339     } else {
1340       MySet<Edge> newDstEdges=GraphManip.getDiffEdges(delta, dst);
1341
1342       if (OoOJava&&!accessible.isAccessible(node, dst)) {
1343         Taint dstStallTaint=Taint.factory(node,  dst, AllocFactory.dummySite, node, ReachGraph.predsEmpty);
1344         newDstEdges=Edge.taintAll(newDstEdges, dstStallTaint);
1345         updateVarDelta(graph, delta, dst, newDstEdges, null);
1346       }
1347
1348       if (OoOJava) {
1349         effectsAnalysis.analyzeFlatSetFieldNode(newDstEdges, fd, node);
1350       }
1351
1352       if (!src.getType().isPtr()) {
1353         return delta;
1354       }
1355
1356       /* Next look at new sources */
1357
1358       MySet<Edge> edgesToAdd=new MySet<Edge>();
1359       MySet<Edge> newSrcEdges=GraphManip.getDiffEdges(delta, src);
1360       MySet<Edge> srcEdges=GraphManip.getEdges(graph, delta, src);
1361       HashSet<AllocNode> dstNodes=GraphManip.getNodes(graph, delta, dst);
1362
1363       if (OoOJava&&!accessible.isAccessible(node, src)) {
1364         Taint srcStallTaint=Taint.factory(node,  src, AllocFactory.dummySite, node, ReachGraph.predsEmpty);
1365         newSrcEdges=Edge.taintAll(newSrcEdges, srcStallTaint);
1366         updateVarDelta(graph, delta, src, newSrcEdges, null);
1367       }
1368
1369       MySet<Edge> edgesToRemove=null;
1370       if (newDstEdges.size()!=0) {
1371         if (dstNodes.size()>1&&!dstNodes.iterator().next().isSummary()&&fd!=null) {
1372           /* Need to undo strong update */
1373           if (graph.strongUpdateSet!=null) {
1374             edgesToAdd.addAll(graph.strongUpdateSet);
1375             graph.strongUpdateSet=null; //Prevent future strong updates
1376           }
1377         } else if (dstNodes.size()==1&&newDstEdges.size()==1&&!newDstEdges.iterator().next().dst.isSummary()&&graph.strongUpdateSet!=null&&fd!=null) {
1378           edgesToRemove=GraphManip.getEdges(graph, delta, dstNodes, fd);
1379           graph.strongUpdateSet.addAll(edgesToRemove);
1380         }
1381         Edge.mergeEdgesInto(edgesToAdd, GraphManip.genEdges(newDstEdges, fd, srcEdges));
1382       }
1383
1384       //Kill new edges
1385       if (graph.strongUpdateSet!=null&&fd!=null) {
1386         MySet<Edge> otherEdgesToRemove=GraphManip.getDiffEdges(delta, dstNodes, fd);
1387         if (edgesToRemove!=null)
1388           edgesToRemove.addAll(otherEdgesToRemove);
1389         else
1390           edgesToRemove=otherEdgesToRemove;
1391         graph.strongUpdateSet.addAll(otherEdgesToRemove);
1392       }
1393
1394       //Next look at new destinations
1395       Edge.mergeEdgesInto(edgesToAdd, GraphManip.genEdges(dstNodes, fd, newSrcEdges));
1396
1397       /* Update diff */
1398       updateHeapDelta(graph, delta, edgesToAdd, edgesToRemove);
1399       applyDiffs(graph, delta);
1400     }
1401     return delta;
1402   }
1403
1404   Delta processCopyNode(FlatNode node, Delta delta, Graph graph) {
1405     TempDescriptor src;
1406     TempDescriptor dst;
1407     if (node.kind()==FKind.FlatOpNode) {
1408       FlatOpNode fon=(FlatOpNode) node;
1409       src=fon.getLeft();
1410       dst=fon.getDest();
1411     } else if (node.kind()==FKind.FlatReturnNode) {
1412       FlatReturnNode frn=(FlatReturnNode)node;
1413       src=frn.getReturnTemp();
1414       dst=returntmp;
1415       if (src==null||!src.getType().isPtr()) {
1416         //This is a NOP
1417         applyDiffs(graph, delta);
1418         return delta;
1419       }
1420     } else {
1421       FlatCastNode fcn=(FlatCastNode) node;
1422       src=fcn.getSrc();
1423       dst=fcn.getDst();
1424     }
1425     if (delta.getInit()) {
1426       MySet<Edge> srcedges=GraphManip.getEdges(graph, delta, src);
1427       MySet<Edge> edgesToAdd=GraphManip.genEdges(dst, srcedges);
1428       MySet<Edge> edgesToRemove=GraphManip.getEdges(graph, delta, dst);
1429       updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
1430       applyDiffs(graph, delta);
1431     } else {
1432       /* First compute new src nodes */
1433       MySet<Edge> newSrcEdges=GraphManip.getDiffEdges(delta, src);
1434
1435       /* Compute the union, and then the set of edges */
1436       MySet<Edge> edgesToAdd=GraphManip.genEdges(dst, newSrcEdges);
1437       
1438       /* Compute set of edges to remove */
1439       MySet<Edge> edgesToRemove=GraphManip.getDiffEdges(delta, dst);      
1440
1441       /* Update diff */
1442       updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
1443       applyDiffs(graph, delta);
1444     }
1445     return delta;
1446   }
1447
1448   Delta processFieldElementNode(FlatNode node, Delta delta, Graph graph) {
1449     TempDescriptor src;
1450     FieldDescriptor fd;
1451     TempDescriptor dst;
1452     TaintSet taint=null;
1453
1454     if (node.kind()==FKind.FlatElementNode) {
1455       FlatElementNode fen=(FlatElementNode) node;
1456       src=fen.getSrc();
1457       fd=null;
1458       dst=fen.getDst();
1459     } else {
1460       FlatFieldNode ffn=(FlatFieldNode) node;
1461       src=ffn.getSrc();
1462       fd=ffn.getField();
1463       dst=ffn.getDst();
1464     }
1465     if (OoOJava&&!accessible.isAccessible(node, src)) {
1466       taint=TaintSet.factory(Taint.factory(node,  src, AllocFactory.dummySite, node, ReachGraph.predsEmpty));
1467     }
1468
1469     //Do nothing for non pointers
1470     if (delta.getInit()) {
1471       MySet<Edge> srcedges=GraphManip.getEdges(graph, delta, src);
1472       if (OoOJava) {
1473         if (taint!=null) {
1474           srcedges=Edge.taintAll(srcedges, taint);
1475           updateVarDelta(graph, delta, src, srcedges, null);
1476         }
1477         effectsAnalysis.analyzeFlatFieldNode(srcedges, fd, node);
1478       }
1479       if (!dst.getType().isPtr())
1480         return delta;
1481
1482       MySet<Edge> edgesToAdd=GraphManip.dereference(graph, delta, dst, srcedges, fd, node);
1483       MySet<Edge> edgesToRemove=GraphManip.getEdges(graph, delta, dst);
1484
1485       updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
1486       applyDiffs(graph, delta);
1487     } else {
1488       MySet<Edge> newsrcedges=GraphManip.getDiffEdges(delta, src);
1489       if (OoOJava) {
1490         if (taint!=null) {
1491           newsrcedges=Edge.taintAll(newsrcedges, taint);
1492           updateVarDelta(graph, delta, src, newsrcedges, null);
1493         }
1494         effectsAnalysis.analyzeFlatFieldNode(newsrcedges, fd, node);
1495       }
1496       if (!dst.getType().isPtr())
1497         return delta;
1498
1499       /* First compute new objects we read fields of */
1500       MySet<Edge> allsrcedges=GraphManip.getEdges(graph, delta, src);
1501       MySet<Edge> edgesToAdd=GraphManip.diffDereference(delta, dst, allsrcedges, fd, node);
1502       /* Next compute new targets of fields */
1503       MySet<Edge> newfdedges=GraphManip.dereference(graph, delta, dst, newsrcedges, fd, node);
1504
1505       /* Compute the union, and then the set of edges */
1506       Edge.mergeEdgesInto(edgesToAdd, newfdedges);
1507       
1508       /* Compute set of edges to remove */
1509       MySet<Edge> edgesToRemove=GraphManip.getDiffEdges(delta, dst);      
1510
1511       
1512       /* Update diff */
1513       updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
1514       applyDiffs(graph, delta);
1515     }
1516
1517     return delta;
1518   }
1519
1520   void updateVarDelta(Graph graph, Delta delta, TempDescriptor tmp, MySet<Edge> edgestoAdd, MySet<Edge> edgestoRemove) {
1521     MySet<Edge> edgeAdd=delta.varedgeadd.get(tmp);
1522     MySet<Edge> edgeRemove=delta.varedgeremove.get(tmp);
1523     MySet<Edge> existingEdges=graph.getEdges(tmp);
1524     if (edgestoRemove!=null)
1525       for(Edge e: edgestoRemove) {
1526         //remove edge from delta
1527         if (edgeAdd!=null)
1528           edgeAdd.remove(e);
1529         //if the edge is already in the graph, add an explicit remove to the delta
1530         if (existingEdges.contains(e))
1531           delta.removeVarEdge(e);
1532       }
1533     for(Edge e: edgestoAdd) {
1534       //Remove the edge from the remove set
1535       if (edgeRemove!=null)
1536         edgeRemove.remove(e);
1537       //Explicitly add it to the add set unless it is already in the graph
1538       if (typeUtil.isSuperorType(tmp.getType(), e.dst.getType())) {
1539         if (!existingEdges.contains(e)) {
1540           delta.addVarEdge(e);
1541         } else {
1542           //See if the old edge subsumes the new one
1543           Edge olde=existingEdges.get(e);
1544           if (!olde.subsumes(e)) {
1545             delta.addVarEdge(olde.merge(e));
1546           }
1547         }
1548       }
1549     }
1550   }
1551
1552   void updateHeapDelta(Graph graph, Delta delta, MySet<Edge> edgestoAdd, MySet<Edge> edgestoRemove) {
1553     if (edgestoRemove!=null)
1554       for(Edge e: edgestoRemove) {
1555         AllocNode src=e.src;
1556         MySet<Edge> edgeAdd=delta.heapedgeadd.get(src);
1557         MySet<Edge> existingEdges=graph.getEdges(src);
1558         //remove edge from delta
1559         if (edgeAdd!=null)
1560           edgeAdd.remove(e);
1561         //if the edge is already in the graph, add an explicit remove to the delta
1562         if (existingEdges.contains(e)) {
1563           delta.removeHeapEdge(e);
1564         }
1565       }
1566     if (edgestoAdd!=null)
1567       for(Edge e: edgestoAdd) {
1568         AllocNode src=e.src;
1569         MySet<Edge> edgeRemove=delta.heapedgeremove.get(src);
1570         MySet<Edge> existingEdges=graph.getEdges(src);
1571         //Remove the edge from the remove set
1572         if (edgeRemove!=null)
1573           edgeRemove.remove(e);
1574         //Explicitly add it to the add set unless it is already in the graph
1575         if (!existingEdges.contains(e)) {
1576           delta.addHeapEdge(e);
1577         } else {
1578           //See if the old edge subsumes the new one
1579           Edge olde=existingEdges.get(e);
1580           if (!olde.subsumes(e)) {
1581             delta.addHeapEdge(olde.merge(e));
1582           }
1583         }
1584       }
1585   }
1586
1587   Delta processFlatNop(FlatNode node, Delta delta, Graph graph) {
1588     applyDiffs(graph, delta);
1589     return delta;
1590   }
1591   
1592   Delta processNewNode(FlatNew node, Delta delta, Graph graph) {
1593     AllocNode summary=allocFactory.getAllocNode(node, true);
1594     AllocNode single=allocFactory.getAllocNode(node, false);
1595     TempDescriptor tmp=node.getDst();
1596
1597     if (delta.getInit()) {
1598       /* We don't have to deal with summarization here...  The
1599        * intuition is that this is the only place where we generate
1600        * nodes for this allocation site and this is the first time
1601        * we've analyzed this site */
1602
1603       //Build new Edge
1604       Edge e=new Edge(tmp, single);
1605       //Build new Edge set
1606       MySet<Edge> newedges=new MySet<Edge>();
1607       newedges.add(e);
1608       //Add it into the diffs
1609       delta.varedgeadd.put(tmp, newedges);
1610       //Remove the old edges
1611       MySet<Edge> oldedges=graph.getEdges(tmp);
1612       if (!oldedges.isEmpty())
1613         delta.varedgeremove.put(tmp, (MySet<Edge>) oldedges);
1614       //Apply incoming diffs to graph
1615       applyDiffs(graph, delta);
1616       //Note that we create a single node
1617       delta.addNodeAges.add(single);
1618       //Kill the old node
1619       if (delta.addOldNodes.containsKey(single)||delta.baseOldNodes.containsKey(single)) {
1620         delta.addOldNodes.put(single, Boolean.FALSE);
1621       }
1622     } else {
1623       /* 1. Fix up the variable edge additions */
1624
1625       for(Iterator<Map.Entry<TempDescriptor, MySet<Edge>>> entryIt=delta.varedgeadd.entrySet().iterator();entryIt.hasNext();) {
1626         Map.Entry<TempDescriptor, MySet<Edge>> entry=entryIt.next();
1627
1628         if (entry.getKey()==tmp) {
1629           /* Check if this is the tmp we overwrite */
1630           entryIt.remove();
1631         } else {
1632           /* Otherwise, check if the target of the edge is changed... */
1633           summarizeSet(entry.getValue(), graph.varMap.get(entry.getKey()), single, summary);
1634         }
1635       }
1636       
1637       /* 2. Fix up the base variable edges */
1638
1639       for(Iterator<Map.Entry<TempDescriptor, MySet<Edge>>> entryIt=delta.basevaredge.entrySet().iterator();entryIt.hasNext();) {
1640         Map.Entry<TempDescriptor, MySet<Edge>> entry=entryIt.next();
1641         TempDescriptor entrytmp=entry.getKey();
1642         if (entrytmp==tmp) {
1643           /* Check is this is the tmp we overwrite, if so add to remove set */
1644           Util.relationUpdate(delta.varedgeremove, tmp, null, entry.getValue());
1645         } else {
1646           /* Check if the target of the edge is changed */ 
1647           MySet<Edge> newset=(MySet<Edge>)entry.getValue().clone();
1648           MySet<Edge> removeset=shrinkSet(newset, graph.varMap.get(entrytmp), single, summary);
1649           Util.relationUpdate(delta.varedgeremove, entrytmp, newset, removeset);
1650           Util.relationUpdate(delta.varedgeadd, entrytmp, null, newset);
1651         }
1652       }
1653
1654
1655       /* 3. Fix up heap edge additions */
1656
1657       HashMap<AllocNode, MySet<Edge>> addheapedge=new HashMap<AllocNode, MySet<Edge>>();
1658       for(Iterator<Map.Entry<AllocNode, MySet<Edge>>> entryIt=delta.heapedgeadd.entrySet().iterator();entryIt.hasNext();) {
1659         Map.Entry<AllocNode, MySet<Edge>> entry=entryIt.next();
1660         MySet<Edge> edgeset=entry.getValue();
1661         AllocNode allocnode=entry.getKey();
1662         if (allocnode==single) {
1663           entryIt.remove();
1664           summarizeSet(edgeset, graph.nodeMap.get(summary), single, summary);
1665           addheapedge.put(summary, edgeset);
1666         } else {
1667           summarizeSet(edgeset, graph.nodeMap.get(allocnode), single, summary);
1668         }
1669       }
1670       
1671       /* Merge in diffs */
1672
1673       for(Map.Entry<AllocNode, MySet<Edge>> entry:addheapedge.entrySet()) {
1674         AllocNode allocnode=entry.getKey();
1675         Util.relationUpdate(delta.heapedgeadd, allocnode, null, entry.getValue());
1676       }
1677
1678       /* 4. Fix up the base heap edges */
1679
1680       for(Iterator<Map.Entry<AllocNode, MySet<Edge>>> entryIt=delta.baseheapedge.entrySet().iterator();entryIt.hasNext();) {
1681         Map.Entry<AllocNode, MySet<Edge>> entry=entryIt.next();
1682         MySet<Edge> edgeset=entry.getValue();
1683         AllocNode allocnode=entry.getKey();
1684         if (allocnode==single) {
1685           entryIt.remove();
1686         }
1687         AllocNode addnode=(allocnode==single)?summary:allocnode;
1688
1689         MySet<Edge> newset=(MySet<Edge>) edgeset.clone();
1690         MySet<Edge> removeset=shrinkSet(newset, graph.nodeMap.get(addnode), single, summary);
1691         Util.relationUpdate(delta.heapedgeadd, addnode, null, newset);
1692         Util.relationUpdate(delta.heapedgeremove, allocnode, null, removeset);
1693       }
1694
1695       /* Update Node Ages...If the base or addNodeAges set contains a
1696        * single node, it now should also contain a summary node...  No
1697        * need to generate a single node as that has already been
1698        * done. */
1699       if (delta.baseNodeAges.contains(single)||delta.addNodeAges.contains(single)) {
1700         delta.addNodeAges.add(summary);
1701       }
1702
1703       //Kill the old node if someone tries to add it
1704       if (delta.addOldNodes.containsKey(single)||delta.baseOldNodes.containsKey(single)) {
1705         delta.addOldNodes.put(single, Boolean.FALSE);
1706       }
1707       
1708       //Apply incoming diffs to graph
1709       applyDiffs(graph, delta);      
1710     }
1711     return delta;
1712   }
1713
1714   /* This function builds a new edge set where oldnode is summarized into new node */
1715
1716   void summarizeSet(MySet<Edge> edgeset, MySet<Edge> oldedgeset, AllocNode oldnode, AllocNode sumnode) {
1717     MySet<Edge> newSet=null;
1718     for(Iterator<Edge> edgeit=edgeset.iterator();edgeit.hasNext();) {
1719       Edge e=edgeit.next();
1720       if (e.dst==oldnode||e.src==oldnode) {
1721         if (newSet==null) {
1722           newSet=new MySet<Edge>();
1723         }
1724         edgeit.remove();
1725         e=e.copy();
1726
1727         if (e.dst==oldnode) {
1728           e.dst=sumnode;
1729         }
1730         if (e.src==oldnode) {
1731           e.src=sumnode;
1732         }
1733         if (oldedgeset==null||!oldedgeset.contains(e))
1734           newSet.add(e);
1735       }
1736     }
1737     if (newSet!=null)
1738       edgeset.addAll(newSet);
1739   }
1740
1741   /* Shrinks the incoming set to just include rewritten values.
1742    * Returns a set of the original rewritten values */
1743
1744   MySet<Edge> shrinkSet(MySet<Edge> edgeset, MySet<Edge> oldedgeset, AllocNode oldnode, AllocNode newnode) {
1745     MySet<Edge> newSet=null;
1746     MySet<Edge> removeSet=null;
1747     for(Iterator<Edge> edgeit=edgeset.iterator();edgeit.hasNext();) {
1748       Edge e=edgeit.next();
1749       edgeit.remove();
1750       if (e.dst==oldnode||e.src==oldnode) {
1751         if (newSet==null) {
1752           newSet=new MySet<Edge>();
1753           removeSet=new MySet<Edge>();
1754         }
1755
1756         removeSet.add(e);
1757         e=e.copy();
1758         if (e.dst==oldnode)
1759           e.dst=newnode;
1760         if (e.src==oldnode)
1761           e.src=newnode;
1762         if (oldedgeset==null||!oldedgeset.contains(e))
1763           newSet.add(e);
1764       }
1765     }
1766     if (newSet!=null)
1767       edgeset.addAll(newSet);
1768     return removeSet;
1769   } 
1770
1771   /* This function returns a completely new Delta...  It is safe to
1772    * modify this */
1773
1774   Delta applyInitDelta(Delta delta, BBlock block) {
1775     //Apply delta to graph
1776     boolean newGraph=false;
1777     if (!bbgraphMap.containsKey(block)) {
1778       bbgraphMap.put(block, new Graph(null));
1779       newGraph=true;
1780     }
1781     Graph graph=bbgraphMap.get(block);
1782
1783     if (newGraph) {
1784       Delta newdelta=new Delta(null, true);
1785       //Add in heap edges and throw away original diff
1786
1787       for(Map.Entry<AllocNode, MySet<Edge>> entry:delta.heapedgeadd.entrySet()) {
1788         graph.nodeMap.put(entry.getKey(), new MySet<Edge>(entry.getValue()));
1789       }
1790       //Add in var edges and throw away original diff
1791       Set<TempDescriptor> livetemps=bblivetemps.get(block);
1792
1793       for(Map.Entry<TempDescriptor, MySet<Edge>> entry:delta.varedgeadd.entrySet()) {
1794         if (livetemps.contains(entry.getKey()))
1795           graph.varMap.put(entry.getKey(), new MySet<Edge>(entry.getValue()));
1796       }
1797       //Record that this is initial set...
1798       graph.nodeAges.addAll(delta.addNodeAges);
1799       //Add old nodes
1800       for(Map.Entry<AllocNode, Boolean> oldentry:delta.addOldNodes.entrySet()) {
1801         if (oldentry.getValue().booleanValue()) {
1802           graph.oldNodes.put(oldentry.getKey(), Boolean.TRUE);
1803         }
1804       }
1805       return newdelta;
1806     } else {
1807       Delta newdelta=new Delta(null, false);
1808       //merge in heap edges and variables
1809       mergeHeapEdges(graph, delta, newdelta);
1810       mergeVarEdges(graph, delta, newdelta, block);
1811       mergeAges(graph, delta, newdelta);
1812       return newdelta;
1813     }
1814   }
1815
1816   /* This function merges in the heap edges.  It updates delta to be
1817    * the difference */
1818
1819   void mergeHeapEdges(Graph graph, Delta delta, Delta newdelta) {
1820     //Merge in edges
1821     for(Map.Entry<AllocNode, MySet<Edge>> heapedge:delta.heapedgeadd.entrySet()) {
1822       AllocNode nsrc=heapedge.getKey();
1823       MySet<Edge> edges=heapedge.getValue();
1824
1825       if (graph.backMap!=null) {
1826         for(Edge e:edges) {
1827           if (!graph.backMap.containsKey(e.dst))
1828             graph.backMap.put(e.dst, new MySet<Edge>());
1829           graph.backMap.get(e.dst).add(e);
1830         }
1831       }
1832
1833       if (!graph.nodeMap.containsKey(nsrc)) {
1834         graph.nodeMap.put(nsrc, new MySet<Edge>());
1835       }
1836       MySet<Edge> dstedges=graph.nodeMap.get(nsrc);
1837       MySet<Edge> diffedges=new MySet<Edge>();
1838       for(Edge e:edges) {
1839         if (!dstedges.contains(e)) {
1840           //We have a new edge
1841           diffedges.add(e);
1842           dstedges.add(e);
1843         } else {
1844           Edge origedge=dstedges.get(e);
1845           if (!origedge.subsumes(e)) {
1846             Edge mergededge=origedge.merge(e);
1847             diffedges.add(mergededge);
1848             dstedges.add(mergededge);
1849           }
1850         }
1851       }
1852       //Done with edge set...
1853       if (diffedges.size()>0) {
1854         //completely new
1855         newdelta.baseheapedge.put(nsrc, diffedges);
1856       }
1857     }
1858   }
1859
1860   /* This function merges in the var edges.  It updates delta to be
1861    * the difference */
1862
1863   void mergeVarEdges(Graph graph, Delta delta, Delta newdelta, BBlock block) {
1864     //Merge in edges
1865     Set<TempDescriptor> livetemps=bblivetemps.get(block);
1866     
1867     for(Map.Entry<TempDescriptor, MySet<Edge>> varedge:delta.varedgeadd.entrySet()) {
1868       TempDescriptor tmpsrc=varedge.getKey();
1869       if (livetemps.contains(tmpsrc)) {
1870         MySet<Edge> edges=varedge.getValue();
1871         if (graph.backMap!=null) {
1872           for(Edge e:edges) {
1873             if (!graph.backMap.containsKey(e.dst))
1874               graph.backMap.put(e.dst, new MySet<Edge>());
1875             graph.backMap.get(e.dst).add(e);
1876           }
1877         }
1878         
1879         if (!graph.varMap.containsKey(tmpsrc)) {
1880           graph.varMap.put(tmpsrc, new MySet<Edge>());
1881         }
1882         MySet<Edge> dstedges=graph.varMap.get(tmpsrc);
1883         MySet<Edge> diffedges=new MySet<Edge>();
1884         for(Edge e:edges) {
1885           if (!dstedges.contains(e)) {
1886             //We have a new edge
1887             diffedges.add(e);
1888             dstedges.add(e);
1889           } else {
1890             Edge origedge=dstedges.get(e);
1891             if (!origedge.subsumes(e)) {
1892               Edge mergededge=origedge.merge(e);
1893               diffedges.add(mergededge);
1894               dstedges.add(mergededge);
1895             }
1896           }
1897         }
1898         //Done with edge set...
1899         if (diffedges.size()>0) {
1900           //completely new
1901           newdelta.basevaredge.put(tmpsrc,diffedges);
1902         }
1903       }
1904     }
1905   }
1906
1907   void mergeAges(Graph graph, Delta delta, Delta newDelta) {
1908     //Merge in edges
1909     for(AllocNode node:delta.addNodeAges) {
1910       if (!graph.nodeAges.contains(node)) {
1911         graph.nodeAges.add(node);
1912         newDelta.baseNodeAges.add(node);
1913       }
1914     }
1915     for(Map.Entry<AllocNode, Boolean> oldentry:delta.addOldNodes.entrySet()) {
1916       AllocNode node=oldentry.getKey();
1917       boolean ispresent=oldentry.getValue().booleanValue();
1918       if (ispresent&&!graph.oldNodes.containsKey(node)) {
1919         graph.oldNodes.put(node, Boolean.TRUE);
1920         newDelta.baseOldNodes.put(node, Boolean.TRUE);
1921       }
1922     }
1923   }
1924 }