bug fix
[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, Edge.makeOld(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       graph.callNewEdges=new HashMap<AllocNode, MySet<Edge>>();
863       graph.callOldEdges=new HashMap<Edge,MySet<Edge>>();
864
865       //Apply diffs to graph
866       applyDiffs(graph, delta, true);
867     } else {
868       MySet<Edge> edgeset=new MySet<Edge>();
869       MySet<Edge> externaledgeset=new MySet<Edge>();
870       HashSet<AllocNode> nodeset=new HashSet<AllocNode>();
871       MySet<Edge> oldedgeset=graph.reachEdge;
872       HashSet<AllocNode> oldnodeset=graph.reachNode;
873
874       HashSet<ClassDescriptor> targetSet=new HashSet<ClassDescriptor>();
875       Stack<AllocNode> tovisit=new Stack<AllocNode>();
876       TempDescriptor tmpthis=fcall.getThis();
877       //Fix up delta to get rid of unnecessary heap edge removals
878       for(Map.Entry<AllocNode, MySet<Edge>> entry:delta.heapedgeremove.entrySet()) {
879         for(Iterator<Edge> eit=entry.getValue().iterator();eit.hasNext();) {
880           Edge e=eit.next();
881           if (graph.callerEdges.contains(e))
882             eit.remove();
883         }
884       }
885
886       //Fix up delta to get rid of unnecessary var edge removals
887       for(Map.Entry<TempDescriptor, MySet<Edge>> entry:delta.varedgeremove.entrySet()) {
888         for(Iterator<Edge> eit=entry.getValue().iterator();eit.hasNext();) {
889           Edge e=eit.next();
890           if (graph.callerEdges.contains(e))
891             eit.remove();
892         }
893       }
894       
895       //Handle the this temp
896       processThisTargets(targetSet, graph, delta, newDelta, nodeset, tovisit, edgeset, tmpthis, oldnodeset);
897
898       //Go through each temp
899       processParams(graph, delta, newDelta, nodeset, tovisit, edgeset, fcall, true);
900       //Go through each new heap edge that starts from old node
901       MySet<Edge> newedges=GraphManip.getDiffEdges(delta, oldnodeset);
902       edgeset.addAll(newedges);
903       for(Edge e:newedges) {
904         //Add new edges that start from old node to newDelta
905         AllocNode src=e.src;
906         if (!newDelta.heapedgeadd.containsKey(src)) {
907           newDelta.heapedgeadd.put(src, new MySet<Edge>());
908         }
909         newDelta.heapedgeadd.get(src).add(e.makeOld());
910         if (!nodeset.contains(e.dst)&&!oldnodeset.contains(e.dst)) {
911           nodeset.add(e.dst);
912           tovisit.add(e.dst);
913         }
914       }
915
916       //Traverse all reachable nodes
917       computeReachableNodes(graph, delta, newDelta, nodeset, tovisit, edgeset, oldnodeset);
918       //Compute call targets
919       HashSet<MethodDescriptor> newtargets=computeTargets(fcall, newDelta);
920       graph.callTargets.addAll(newtargets);
921
922       //add in new nodeset and edgeset
923       oldnodeset.addAll(nodeset);
924       oldedgeset.addAll(edgeset);
925       //Fix mapping
926       fixMapping(fcall, graph.callTargets, oldedgeset, newDelta, callblock, callindex);
927       //Compute edges into region to splice out
928       computeExternalEdges(graph, delta, oldnodeset, nodeset, externaledgeset);
929
930       //Splice out internal edges
931       removeEdges(graph, delta, nodeset, edgeset, externaledgeset);
932
933       //Add external edges back in
934       processCallExternal(graph, delta, externaledgeset);
935
936       //Move new edges that should be summarized
937       processSummarization(graph, delta);
938
939       Set<FlatSESEEnterNode> seseCallers=OoOJava?taskAnalysis.getTransitiveExecutingRBlocks(fcall):null;
940       //Check if the new nodes allow us to insert a new edge
941       for(AllocNode node:nodeset) {
942         if (graph.callNewEdges.containsKey(node)) {
943           for(Iterator<Edge> eit=graph.callNewEdges.get(node).iterator();eit.hasNext();) {
944             Edge e=eit.next();
945             if ((graph.callNodeAges.contains(e.src)||graph.reachNode.contains(e.src))&&
946                 (graph.callNodeAges.contains(e.dst)||graph.reachNode.contains(e.dst))) {
947               Edge edgetoadd=e.copy();//we need our own copy to modify below
948               eit.remove();
949               if (seseCallers!=null)
950                 edgetoadd.taintModify(seseCallers);
951               mergeCallEdge(graph, delta, edgetoadd);
952             }
953           }
954         }
955       }
956
957       for(Edge e:edgeset) {
958         //See if these edges would allow an old edge to be added
959         if (graph.callOldEdges.containsKey(e)) {
960           for(Edge adde:graph.callOldEdges.get(e)) {
961             Edge ecopy=adde.copy();
962             ecopy.statuspredicate=e.statuspredicate;
963             mergeCallEdge(graph, delta, ecopy);
964           }
965         }
966       }
967
968       //Add in new external edges
969       graph.externalEdgeSet.addAll(externaledgeset);
970       //Apply diffs to graph
971       applyDiffs(graph, delta);
972     }
973     return delta;
974   }
975
976   void processSummarization(Graph graph, Delta delta) {
977     processSumHeapEdgeSet(delta.heapedgeadd, delta, graph);
978     processSumHeapEdgeSet(delta.baseheapedge, delta, graph);
979     processSumVarEdgeSet(delta.varedgeadd, delta, graph);
980     processSumVarEdgeSet(delta.basevaredge, delta, graph);
981   }
982
983   void processSumVarEdgeSet(HashMap<TempDescriptor, 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<TempDescriptor, MySet<Edge>>> eit=map.entrySet().iterator();eit.hasNext();) {
987       Map.Entry<TempDescriptor, MySet<Edge>> entry=eit.next();
988       MySet<Edge> edgeset=entry.getValue();
989
990       for(Edge e:edgeset) {
991         Edge copy=e.copy();
992         boolean rewrite=false;
993         if (copy.dst!=null&&graph.callNodeAges.contains(copy.dst)) {
994           copy.dst=allocFactory.getAllocNode(copy.dst, true);
995           rewrite=true;
996         }
997         if (rewrite) {
998           edgestoremove.add(e);
999           edgestoadd.add(copy);
1000         }
1001       }
1002     }
1003     for(Edge e:edgestoremove) {
1004       delta.removeVarEdge(e);
1005     }
1006     for(Edge e:edgestoadd) {
1007       delta.addVarEdge(e);
1008     }
1009   }
1010   
1011   public Alloc getAllocationSiteFromFlatNew(FlatNew node) {
1012     return allocFactory.getAllocNode(node, false).getAllocSite();
1013   }
1014  
1015   void processSumHeapEdgeSet(HashMap<AllocNode, MySet<Edge>> map, Delta delta, Graph graph) {
1016     MySet<Edge> edgestoadd=new MySet<Edge>();
1017     MySet<Edge> edgestoremove=new MySet<Edge>();
1018     for(Iterator<Map.Entry<AllocNode, MySet<Edge>>> eit=map.entrySet().iterator();eit.hasNext();) {
1019       Map.Entry<AllocNode, MySet<Edge>> entry=eit.next();
1020       AllocNode node=entry.getKey();
1021       MySet<Edge> edgeset=entry.getValue();
1022
1023       for(Edge e:edgeset) {
1024         Edge copy=e.copy();
1025         boolean rewrite=false;
1026         if (copy.src!=null&&graph.callNodeAges.contains(copy.src)) {
1027           copy.src=allocFactory.getAllocNode(copy.src, true);
1028           rewrite=true;
1029         }
1030         if (copy.dst!=null&&graph.callNodeAges.contains(copy.dst)) {
1031           copy.dst=allocFactory.getAllocNode(copy.dst, true);
1032           rewrite=true;
1033         }
1034         if (rewrite) {
1035           edgestoremove.add(e);
1036           edgestoadd.add(copy);
1037         }
1038       }
1039     }
1040     for(Edge e:edgestoremove) {
1041       delta.removeHeapEdge(e);
1042     }
1043     for(Edge e:edgestoadd) {
1044       delta.addHeapEdge(e);
1045     }
1046   }
1047
1048   //Handle external edges
1049   void processCallExternal(Graph graph, Delta newDelta, MySet<Edge> externalEdgeSet) {
1050     //Add external edges in
1051     for(Edge e:externalEdgeSet) {
1052       //First did we age the source
1053       Edge newedge=e.copy();
1054       if (newedge.src!=null&&!e.src.isSummary()&&graph.callNodeAges.contains(e.src)) {
1055         AllocNode summaryNode=allocFactory.getAllocNode(newedge.src, true);
1056         newedge.src=summaryNode;
1057       }
1058       //Compute target
1059       if (graph.callNodeAges.contains(e.dst)&&!e.dst.isSummary()) {
1060         if (graph.callOldNodes.contains(e.dst)) {
1061           //Need two edges
1062           Edge copy=newedge.copy();
1063           mergeEdge(graph, newDelta, copy);
1064         }
1065         //Now add summarized node
1066         newedge.dst=allocFactory.getAllocNode(newedge.dst, true);
1067         mergeCallEdge(graph, newDelta, newedge);
1068       } else {
1069         //Add edge to single node
1070         mergeEdge(graph, newDelta, newedge);
1071       }
1072     }
1073   }
1074
1075   /* This function applies callee deltas to the caller heap. */
1076
1077   Delta applyCallDelta(Delta delta, BBlock bblock) {
1078     Delta newDelta=new Delta(null, false);
1079     Vector<FlatNode> nodes=bblock.nodes();
1080     PPoint ppoint=delta.getBlock();
1081     FlatCall fcall=(FlatCall)nodes.get(ppoint.getIndex());
1082     Graph graph=graphMap.get(fcall);
1083     Graph oldgraph=(ppoint.getIndex()==0)?
1084       bbgraphMap.get(bblock):
1085       graphMap.get(nodes.get(ppoint.getIndex()-1));
1086     Set<FlatSESEEnterNode> seseCallers=OoOJava?taskAnalysis.getTransitiveExecutingRBlocks(fcall):null;
1087
1088     //Age outside nodes if necessary
1089     for(Iterator<AllocNode> nodeit=delta.addNodeAges.iterator();nodeit.hasNext();) {
1090       AllocNode node=nodeit.next();
1091       if (!graph.callNodeAges.contains(node)) {
1092         graph.callNodeAges.add(node);
1093         newDelta.addNodeAges.add(node);
1094       }
1095       if (!graph.reachNode.contains(node)&&!node.isSummary()) {
1096         /* Need to age node in existing graph*/
1097         summarizeInGraph(graph, newDelta, node);
1098       }
1099       if (graph.callNewEdges.containsKey(node)) {
1100         for(Iterator<Edge> eit=graph.callNewEdges.get(node).iterator();eit.hasNext();) {
1101           Edge e=eit.next();
1102           if ((graph.callNodeAges.contains(e.src)||graph.reachNode.contains(e.src))&&
1103               (graph.callNodeAges.contains(e.dst)||graph.reachNode.contains(e.dst))) {
1104             Edge edgetoadd=e.copy();//we need our own copy to modify below
1105             eit.remove();
1106             if (seseCallers!=null)
1107               edgetoadd.taintModify(seseCallers);
1108             mergeCallEdge(graph, newDelta, edgetoadd);
1109           }
1110         }
1111       }
1112     }
1113
1114     //Add heap edges in
1115     for(Map.Entry<AllocNode, MySet<Edge>> entry:delta.heapedgeadd.entrySet()) {
1116       for(Edge e:entry.getValue()) {
1117         boolean addedge=false;
1118         Edge edgetoadd=null;
1119         if (e.statuspredicate==Edge.NEW) {
1120           if ((graph.callNodeAges.contains(e.src)||graph.reachNode.contains(e.src))&&
1121               (graph.callNodeAges.contains(e.dst)||graph.reachNode.contains(e.dst))) {
1122             edgetoadd=e.copy();//we need our own copy to modify below
1123           } else {
1124             graph.addCallEdge(e);
1125           }
1126         } else {
1127           Edge[] edgeArray=e.makeStatus(allocFactory);
1128
1129           int statuspredicate=0;
1130           for(int i=0;i<edgeArray.length;i++) {
1131             Edge origEdgeKey=edgeArray[i];
1132             if (graph.reachEdge.contains(origEdgeKey)) {
1133               Edge origEdge=graph.reachEdge.get(origEdgeKey);
1134               //copy the predicate
1135               statuspredicate=statuspredicate|origEdge.statuspredicate;
1136             }
1137             if (!graph.callOldEdges.containsKey(origEdgeKey)) {
1138               graph.callOldEdges.put(origEdgeKey, new MySet<Edge>());
1139             }
1140             if (graph.callOldEdges.get(origEdgeKey).contains(e)) {
1141               Edge olde=graph.callOldEdges.get(origEdgeKey).get(e);
1142               graph.callOldEdges.get(origEdgeKey).add(olde.merge(e));
1143             } else {
1144               graph.callOldEdges.get(origEdgeKey).add(e);
1145             }
1146           }
1147           if (statuspredicate!=0) {
1148             Edge newe=e.copy();
1149             newe.statuspredicate=statuspredicate;
1150             edgetoadd=newe;
1151           }
1152         }
1153         if (seseCallers!=null&&edgetoadd!=null)
1154           edgetoadd.taintModify(seseCallers);
1155         mergeCallEdge(graph, newDelta, edgetoadd);
1156       }
1157     }
1158     
1159     processCallExternal(graph, newDelta, graph.externalEdgeSet);
1160
1161     //Add edge for return value
1162     if (fcall.getReturnTemp()!=null) {
1163       MySet<Edge> returnedge=delta.varedgeadd.get(returntmp);
1164       if (returnedge!=null)
1165         for(Edge e:returnedge) {
1166           Edge newedge=e.copy();
1167           newedge.srcvar=fcall.getReturnTemp();
1168           if (seseCallers!=null)
1169             newedge.taintModify(seseCallers);
1170           if (graph.getEdges(fcall.getReturnTemp())==null||!graph.getEdges(fcall.getReturnTemp()).contains(newedge))
1171             newDelta.addEdge(newedge);
1172         }
1173     }
1174     applyDiffs(graph, newDelta);
1175     return newDelta;
1176   }
1177   
1178   public void mergeEdge(Graph graph, Delta newDelta, Edge edgetoadd) {
1179     if (edgetoadd!=null) {
1180       Edge match=graph.getMatch(edgetoadd);
1181
1182       if (match==null||!match.subsumes(edgetoadd)) {
1183         Edge mergededge=edgetoadd.merge(match);
1184         newDelta.addEdge(mergededge);
1185       }
1186     }
1187   }
1188
1189   /* This is a call produced edge...need to remember this */
1190
1191   public void mergeCallEdge(Graph graph, Delta newDelta, Edge edgetoadd) {
1192     if (edgetoadd!=null) {
1193       newDelta.addEdgeClear(edgetoadd);
1194
1195       Edge match=graph.getMatch(edgetoadd);
1196       
1197       if (match==null||!match.subsumes(edgetoadd)) {
1198         Edge mergededge=edgetoadd.merge(match);
1199         newDelta.addEdge(mergededge);
1200         graph.callerEdges.add(mergededge);
1201       }
1202     }
1203   }
1204
1205
1206   /* Summarizes out of context nodes in graph */
1207   void summarizeInGraph(Graph graph, Delta newDelta, AllocNode singleNode) {
1208     AllocNode summaryNode=allocFactory.getAllocNode(singleNode, true);
1209
1210     //Handle outgoing heap edges
1211     MySet<Edge> edgeset=graph.getEdges(singleNode);
1212
1213     for(Edge e:edgeset) {
1214       Edge rewrite=e.rewrite(singleNode, summaryNode);
1215       //Remove old edge
1216       newDelta.removeHeapEdge(e);
1217       mergeCallEdge(graph, newDelta, rewrite);
1218     }
1219     
1220     //Handle incoming edges
1221     MySet<Edge> backedges=graph.getBackEdges(singleNode);
1222     for(Edge e:backedges) {
1223       if (e.dst==singleNode) {
1224         //Need to get original edge so that predicate will be correct
1225         Edge match=graph.getMatch(e);
1226         if (match!=null) {
1227           Edge rewrite=match.rewrite(singleNode, summaryNode);
1228           newDelta.removeEdge(match);
1229           mergeCallEdge(graph, newDelta, rewrite);
1230         }
1231       }
1232     }
1233   }
1234
1235   void applyDiffs(Graph graph, Delta delta) {
1236     applyDiffs(graph, delta, false);
1237   }
1238
1239   void applyDiffs(Graph graph, Delta delta, boolean genbackwards) {
1240     //build backwards map if requested
1241     if (genbackwards&&graph.backMap==null) {
1242       graph.backMap=new HashMap<AllocNode, MySet<Edge>>();
1243       if (graph.parent.backMap==null) {
1244         graph.parent.backMap=new HashMap<AllocNode, MySet<Edge>>();
1245         for(Map.Entry<AllocNode, MySet<Edge>> entry:graph.nodeMap.entrySet()) {
1246           for(Edge e:entry.getValue()) {
1247             if (!graph.parent.backMap.containsKey(e.dst))
1248               graph.parent.backMap.put(e.dst, new MySet<Edge>());
1249             graph.parent.backMap.get(e.dst).add(e);
1250           }
1251         }
1252         for(Map.Entry<TempDescriptor, MySet<Edge>> entry:graph.varMap.entrySet()) {
1253           for(Edge e:entry.getValue()) {
1254             if (!graph.parent.backMap.containsKey(e.dst))
1255               graph.parent.backMap.put(e.dst, new MySet<Edge>());
1256             graph.parent.backMap.get(e.dst).add(e);
1257           }
1258         }
1259       }
1260     }
1261
1262     //Add hidden base edges
1263     for(Map.Entry<AllocNode, MySet<Edge>> e: delta.baseheapedge.entrySet()) {
1264       AllocNode node=e.getKey();
1265       MySet<Edge> edges=e.getValue();
1266       if (graph.nodeMap.containsKey(node)) {
1267         MySet<Edge> nodeEdges=graph.nodeMap.get(node);
1268         nodeEdges.addAll(edges);
1269       }
1270     }
1271
1272     //Remove heap edges
1273     for(Map.Entry<AllocNode, MySet<Edge>> e: delta.heapedgeremove.entrySet()) {
1274       AllocNode node=e.getKey();
1275       MySet<Edge> edgestoremove=e.getValue();
1276       if (graph.nodeMap.containsKey(node)) {
1277         //Just apply diff to current map
1278         graph.nodeMap.get(node).removeAll(edgestoremove);
1279       } else {
1280         //Generate diff from parent graph
1281         MySet<Edge> parentedges=graph.parent.nodeMap.get(node);
1282         if (parentedges!=null) {
1283           MySet<Edge> newedgeset=Util.setSubtract(parentedges, edgestoremove);
1284           graph.nodeMap.put(node, newedgeset);
1285         }
1286       }
1287     }
1288
1289     //Add heap edges
1290     for(Map.Entry<AllocNode, MySet<Edge>> e: delta.heapedgeadd.entrySet()) {
1291       AllocNode node=e.getKey();
1292       MySet<Edge> edgestoadd=e.getValue();
1293       //If we have not done a subtract, then 
1294       if (!graph.nodeMap.containsKey(node)) {
1295         //Copy the parent entry
1296         if (graph.parent.nodeMap.containsKey(node))
1297           graph.nodeMap.put(node, (MySet<Edge>)graph.parent.nodeMap.get(node).clone());
1298         else
1299           graph.nodeMap.put(node, new MySet<Edge>());
1300       }
1301       Edge.mergeEdgesInto(graph.nodeMap.get(node),edgestoadd);
1302       if (genbackwards) {
1303         for(Edge eadd:edgestoadd) {
1304           if (!graph.backMap.containsKey(eadd.dst))
1305             graph.backMap.put(eadd.dst, new MySet<Edge>());
1306           graph.backMap.get(eadd.dst).add(eadd);
1307         }
1308       }
1309     }
1310
1311     //Remove var edges
1312     for(Map.Entry<TempDescriptor, MySet<Edge>> e: delta.varedgeremove.entrySet()) {
1313       TempDescriptor tmp=e.getKey();
1314       MySet<Edge> edgestoremove=e.getValue();
1315
1316       if (graph.varMap.containsKey(tmp)) {
1317         //Just apply diff to current map
1318         graph.varMap.get(tmp).removeAll(edgestoremove);
1319       } else if (graph.parent.varMap.containsKey(tmp)) {
1320         //Generate diff from parent graph
1321         MySet<Edge> parentedges=graph.parent.varMap.get(tmp);
1322         MySet<Edge> newedgeset=Util.setSubtract(parentedges, edgestoremove);
1323         graph.varMap.put(tmp, newedgeset);
1324       }
1325     }
1326
1327     //Add var edges
1328     for(Map.Entry<TempDescriptor, MySet<Edge>> e: delta.varedgeadd.entrySet()) {
1329       TempDescriptor tmp=e.getKey();
1330       MySet<Edge> edgestoadd=e.getValue();
1331       if (graph.varMap.containsKey(tmp)) {
1332         Edge.mergeEdgesInto(graph.varMap.get(tmp), edgestoadd);
1333       } else 
1334         graph.varMap.put(tmp, (MySet<Edge>) edgestoadd.clone());
1335       if (genbackwards) {
1336         for(Edge eadd:edgestoadd) {
1337           if (!graph.backMap.containsKey(eadd.dst))
1338             graph.backMap.put(eadd.dst, new MySet<Edge>());
1339           graph.backMap.get(eadd.dst).add(eadd);
1340         }
1341       }
1342     }
1343
1344     //Add node additions
1345     for(AllocNode node:delta.addNodeAges) {
1346       graph.nodeAges.add(node);
1347     }
1348     
1349     for(Map.Entry<AllocNode, Boolean> nodeentry:delta.addOldNodes.entrySet()) {
1350       AllocNode node=nodeentry.getKey();
1351       Boolean ispresent=nodeentry.getValue();
1352       graph.oldNodes.put(node, ispresent);
1353     }
1354   }
1355   Delta processSetFieldElementNode(FlatNode node, Delta delta, Graph graph) {
1356     TempDescriptor src;
1357     FieldDescriptor fd;
1358     TempDescriptor dst;
1359     if (node.kind()==FKind.FlatSetElementNode) {
1360       FlatSetElementNode fen=(FlatSetElementNode) node;
1361       src=fen.getSrc();
1362       fd=null;
1363       dst=fen.getDst();
1364     } else {
1365       FlatSetFieldNode ffn=(FlatSetFieldNode) node;
1366       src=ffn.getSrc();
1367       fd=ffn.getField();
1368       dst=ffn.getDst();
1369     }
1370
1371     if (delta.getInit()) {
1372       MySet<Edge> dstEdges=GraphManip.getEdges(graph, delta, dst);
1373
1374       if (OoOJava&&!accessible.isAccessible(node, dst)) {
1375         Taint dstStallTaint=Taint.factory(node,  dst, AllocFactory.dummySite, node, ReachGraph.predsEmpty);
1376         dstEdges=Edge.taintAll(dstEdges, dstStallTaint);
1377         updateVarDelta(graph, delta, dst, dstEdges, null);
1378       }
1379       if (OoOJava) {
1380         effectsAnalysis.analyzeFlatSetFieldNode(dstEdges, fd, node);
1381       }
1382
1383       //Do nothing for non pointers
1384       if (!src.getType().isPtr()) {
1385         return delta;
1386       }
1387
1388       MySet<Edge> srcEdges=GraphManip.getEdges(graph, delta, src);
1389       if (OoOJava&&!accessible.isAccessible(node, src)) {
1390         Taint srcStallTaint=Taint.factory(node,  src, AllocFactory.dummySite, node, ReachGraph.predsEmpty);
1391         srcEdges=Edge.taintAll(srcEdges, srcStallTaint);
1392         updateVarDelta(graph, delta, src, srcEdges, null);
1393       }
1394
1395       MySet<Edge> edgesToAdd=GraphManip.genEdges(dstEdges, fd, srcEdges);
1396       MySet<Edge> edgesToRemove=null;
1397       if (dstEdges.size()==1&&!dstEdges.iterator().next().dst.isSummary()&&fd!=null) {
1398         /* Can do a strong update */
1399         edgesToRemove=GraphManip.getEdges(graph, delta, dstEdges, fd);
1400         graph.strongUpdateSet=edgesToRemove;
1401       } else
1402         graph.strongUpdateSet=new MySet<Edge>();
1403
1404       /* Update diff */
1405       updateHeapDelta(graph, delta, edgesToAdd, edgesToRemove);
1406       applyDiffs(graph, delta);
1407     } else {
1408       MySet<Edge> newDstEdges=GraphManip.getDiffEdges(delta, dst);
1409
1410       if (OoOJava&&!accessible.isAccessible(node, dst)) {
1411         Taint dstStallTaint=Taint.factory(node,  dst, AllocFactory.dummySite, node, ReachGraph.predsEmpty);
1412         newDstEdges=Edge.taintAll(newDstEdges, dstStallTaint);
1413         updateVarDelta(graph, delta, dst, newDstEdges, null);
1414       }
1415
1416       if (OoOJava) {
1417         effectsAnalysis.analyzeFlatSetFieldNode(newDstEdges, fd, node);
1418       }
1419
1420       if (!src.getType().isPtr()) {
1421         return delta;
1422       }
1423
1424       /* Next look at new sources */
1425
1426       MySet<Edge> edgesToAdd=new MySet<Edge>();
1427       MySet<Edge> newSrcEdges=GraphManip.getDiffEdges(delta, src);
1428       MySet<Edge> srcEdges=GraphManip.getEdges(graph, delta, src);
1429       HashSet<AllocNode> dstNodes=GraphManip.getNodes(graph, delta, dst);
1430
1431       if (OoOJava&&!accessible.isAccessible(node, src)) {
1432         Taint srcStallTaint=Taint.factory(node,  src, AllocFactory.dummySite, node, ReachGraph.predsEmpty);
1433         newSrcEdges=Edge.taintAll(newSrcEdges, srcStallTaint);
1434         updateVarDelta(graph, delta, src, newSrcEdges, null);
1435       }
1436
1437       MySet<Edge> edgesToRemove=null;
1438       if (newDstEdges.size()!=0) {
1439         if (dstNodes.size()>1&&!dstNodes.iterator().next().isSummary()&&fd!=null) {
1440           /* Need to undo strong update */
1441           if (graph.strongUpdateSet!=null) {
1442             edgesToAdd.addAll(graph.strongUpdateSet);
1443             graph.strongUpdateSet=null; //Prevent future strong updates
1444           }
1445         } else if (dstNodes.size()==1&&newDstEdges.size()==1&&!newDstEdges.iterator().next().dst.isSummary()&&graph.strongUpdateSet!=null&&fd!=null) {
1446           edgesToRemove=GraphManip.getEdges(graph, delta, dstNodes, fd);
1447           graph.strongUpdateSet.addAll(edgesToRemove);
1448         }
1449         Edge.mergeEdgesInto(edgesToAdd, GraphManip.genEdges(newDstEdges, fd, srcEdges));
1450       }
1451
1452       //Kill new edges
1453       if (graph.strongUpdateSet!=null&&fd!=null) {
1454         MySet<Edge> otherEdgesToRemove=GraphManip.getDiffEdges(delta, dstNodes, fd);
1455         if (edgesToRemove!=null)
1456           edgesToRemove.addAll(otherEdgesToRemove);
1457         else
1458           edgesToRemove=otherEdgesToRemove;
1459         graph.strongUpdateSet.addAll(otherEdgesToRemove);
1460       }
1461
1462       //Next look at new destinations
1463       Edge.mergeEdgesInto(edgesToAdd, GraphManip.genEdges(dstNodes, fd, newSrcEdges));
1464
1465       /* Update diff */
1466       updateHeapDelta(graph, delta, edgesToAdd, edgesToRemove);
1467       applyDiffs(graph, delta);
1468     }
1469     return delta;
1470   }
1471
1472   Delta processCopyNode(FlatNode node, Delta delta, Graph graph) {
1473     TempDescriptor src;
1474     TempDescriptor dst;
1475     if (node.kind()==FKind.FlatOpNode) {
1476       FlatOpNode fon=(FlatOpNode) node;
1477       src=fon.getLeft();
1478       dst=fon.getDest();
1479     } else if (node.kind()==FKind.FlatReturnNode) {
1480       FlatReturnNode frn=(FlatReturnNode)node;
1481       src=frn.getReturnTemp();
1482       dst=returntmp;
1483       if (src==null||!src.getType().isPtr()) {
1484         //This is a NOP
1485         applyDiffs(graph, delta);
1486         return delta;
1487       }
1488     } else {
1489       FlatCastNode fcn=(FlatCastNode) node;
1490       src=fcn.getSrc();
1491       dst=fcn.getDst();
1492     }
1493     if (delta.getInit()) {
1494       MySet<Edge> srcedges=GraphManip.getEdges(graph, delta, src);
1495       MySet<Edge> edgesToAdd=GraphManip.genEdges(dst, srcedges);
1496       MySet<Edge> edgesToRemove=GraphManip.getEdges(graph, delta, dst);
1497       updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
1498       applyDiffs(graph, delta);
1499     } else {
1500       /* First compute new src nodes */
1501       MySet<Edge> newSrcEdges=GraphManip.getDiffEdges(delta, src);
1502
1503       /* Compute the union, and then the set of edges */
1504       MySet<Edge> edgesToAdd=GraphManip.genEdges(dst, newSrcEdges);
1505       
1506       /* Compute set of edges to remove */
1507       MySet<Edge> edgesToRemove=GraphManip.getDiffEdges(delta, dst);      
1508
1509       /* Update diff */
1510       updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
1511       applyDiffs(graph, delta);
1512     }
1513     return delta;
1514   }
1515
1516   Delta processFieldElementNode(FlatNode node, Delta delta, Graph graph) {
1517     TempDescriptor src;
1518     FieldDescriptor fd;
1519     TempDescriptor dst;
1520     TaintSet taint=null;
1521
1522     if (node.kind()==FKind.FlatElementNode) {
1523       FlatElementNode fen=(FlatElementNode) node;
1524       src=fen.getSrc();
1525       fd=null;
1526       dst=fen.getDst();
1527     } else {
1528       FlatFieldNode ffn=(FlatFieldNode) node;
1529       src=ffn.getSrc();
1530       fd=ffn.getField();
1531       dst=ffn.getDst();
1532     }
1533     if (OoOJava&&!accessible.isAccessible(node, src)) {
1534       taint=TaintSet.factory(Taint.factory(node,  src, AllocFactory.dummySite, node, ReachGraph.predsEmpty));
1535     }
1536
1537     //Do nothing for non pointers
1538     if (delta.getInit()) {
1539       MySet<Edge> srcedges=GraphManip.getEdges(graph, delta, src);
1540       if (OoOJava) {
1541         if (taint!=null) {
1542           srcedges=Edge.taintAll(srcedges, taint);
1543           updateVarDelta(graph, delta, src, srcedges, null);
1544         }
1545         effectsAnalysis.analyzeFlatFieldNode(srcedges, fd, node);
1546       }
1547       if (!dst.getType().isPtr())
1548         return delta;
1549
1550       MySet<Edge> edgesToAdd=GraphManip.dereference(graph, delta, dst, srcedges, fd, node);
1551       MySet<Edge> edgesToRemove=GraphManip.getEdges(graph, delta, dst);
1552
1553       updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
1554       applyDiffs(graph, delta);
1555     } else {
1556       MySet<Edge> newsrcedges=GraphManip.getDiffEdges(delta, src);
1557       if (OoOJava) {
1558         if (taint!=null) {
1559           newsrcedges=Edge.taintAll(newsrcedges, taint);
1560           updateVarDelta(graph, delta, src, newsrcedges, null);
1561         }
1562         effectsAnalysis.analyzeFlatFieldNode(newsrcedges, fd, node);
1563       }
1564       if (!dst.getType().isPtr())
1565         return delta;
1566
1567       /* First compute new objects we read fields of */
1568       MySet<Edge> allsrcedges=GraphManip.getEdges(graph, delta, src);
1569       MySet<Edge> edgesToAdd=GraphManip.diffDereference(delta, dst, allsrcedges, fd, node);
1570       /* Next compute new targets of fields */
1571       MySet<Edge> newfdedges=GraphManip.dereference(graph, delta, dst, newsrcedges, fd, node);
1572
1573       /* Compute the union, and then the set of edges */
1574       Edge.mergeEdgesInto(edgesToAdd, newfdedges);
1575       
1576       /* Compute set of edges to remove */
1577       MySet<Edge> edgesToRemove=GraphManip.getDiffEdges(delta, dst);      
1578
1579       
1580       /* Update diff */
1581       updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
1582       applyDiffs(graph, delta);
1583     }
1584
1585     return delta;
1586   }
1587
1588   void updateVarDelta(Graph graph, Delta delta, TempDescriptor tmp, MySet<Edge> edgestoAdd, MySet<Edge> edgestoRemove) {
1589     MySet<Edge> edgeAdd=delta.varedgeadd.get(tmp);
1590     MySet<Edge> edgeRemove=delta.varedgeremove.get(tmp);
1591     MySet<Edge> existingEdges=graph.getEdges(tmp);
1592     if (edgestoRemove!=null)
1593       for(Edge e: edgestoRemove) {
1594         //remove edge from delta
1595         if (edgeAdd!=null)
1596           edgeAdd.remove(e);
1597         //if the edge is already in the graph, add an explicit remove to the delta
1598         if (existingEdges.contains(e))
1599           delta.removeVarEdge(e);
1600       }
1601     for(Edge e: edgestoAdd) {
1602       //Remove the edge from the remove set
1603       if (edgeRemove!=null)
1604         edgeRemove.remove(e);
1605       //Explicitly add it to the add set unless it is already in the graph
1606       if (typeUtil.isSuperorType(tmp.getType(), e.dst.getType())) {
1607         if (!existingEdges.contains(e)) {
1608           delta.addVarEdge(e);
1609         } else {
1610           //See if the old edge subsumes the new one
1611           Edge olde=existingEdges.get(e);
1612           if (!olde.subsumes(e)) {
1613             delta.addVarEdge(olde.merge(e));
1614           }
1615         }
1616       }
1617     }
1618   }
1619
1620   void updateHeapDelta(Graph graph, Delta delta, MySet<Edge> edgestoAdd, MySet<Edge> edgestoRemove) {
1621     if (edgestoRemove!=null)
1622       for(Edge e: edgestoRemove) {
1623         AllocNode src=e.src;
1624         MySet<Edge> edgeAdd=delta.heapedgeadd.get(src);
1625         MySet<Edge> existingEdges=graph.getEdges(src);
1626         //remove edge from delta
1627         if (edgeAdd!=null)
1628           edgeAdd.remove(e);
1629         //if the edge is already in the graph, add an explicit remove to the delta
1630         if (existingEdges.contains(e)) {
1631           delta.removeHeapEdge(e);
1632         }
1633       }
1634     if (edgestoAdd!=null)
1635       for(Edge e: edgestoAdd) {
1636         AllocNode src=e.src;
1637         MySet<Edge> edgeRemove=delta.heapedgeremove.get(src);
1638         MySet<Edge> existingEdges=graph.getEdges(src);
1639         //Remove the edge from the remove set
1640         if (edgeRemove!=null)
1641           edgeRemove.remove(e);
1642         //Explicitly add it to the add set unless it is already in the graph
1643         if (!existingEdges.contains(e)) {
1644           delta.addHeapEdge(e);
1645         } else {
1646           //See if the old edge subsumes the new one
1647           Edge olde=existingEdges.get(e);
1648           if (!olde.subsumes(e)) {
1649             delta.addHeapEdge(olde.merge(e));
1650           }
1651         }
1652       }
1653   }
1654
1655   Delta processFlatNop(FlatNode node, Delta delta, Graph graph) {
1656     applyDiffs(graph, delta);
1657     return delta;
1658   }
1659   
1660   Delta processNewNode(FlatNew node, Delta delta, Graph graph) {
1661     AllocNode summary=allocFactory.getAllocNode(node, true);
1662     AllocNode single=allocFactory.getAllocNode(node, false);
1663     TempDescriptor tmp=node.getDst();
1664
1665     if (delta.getInit()) {
1666       /* We don't have to deal with summarization here...  The
1667        * intuition is that this is the only place where we generate
1668        * nodes for this allocation site and this is the first time
1669        * we've analyzed this site */
1670
1671       //Build new Edge
1672       Edge e=new Edge(tmp, single);
1673       //Build new Edge set
1674       MySet<Edge> newedges=new MySet<Edge>();
1675       newedges.add(e);
1676       //Add it into the diffs
1677       delta.varedgeadd.put(tmp, newedges);
1678       //Remove the old edges
1679       MySet<Edge> oldedges=graph.getEdges(tmp);
1680       if (!oldedges.isEmpty())
1681         delta.varedgeremove.put(tmp, (MySet<Edge>) oldedges);
1682       //Apply incoming diffs to graph
1683       applyDiffs(graph, delta);
1684       //Note that we create a single node
1685       delta.addNodeAges.add(single);
1686       //Kill the old node
1687       if (delta.addOldNodes.containsKey(single)||delta.baseOldNodes.containsKey(single)) {
1688         delta.addOldNodes.put(single, Boolean.FALSE);
1689       }
1690     } else {
1691       /* 1. Fix up the variable edge additions */
1692
1693       for(Iterator<Map.Entry<TempDescriptor, MySet<Edge>>> entryIt=delta.varedgeadd.entrySet().iterator();entryIt.hasNext();) {
1694         Map.Entry<TempDescriptor, MySet<Edge>> entry=entryIt.next();
1695
1696         if (entry.getKey()==tmp) {
1697           /* Check if this is the tmp we overwrite */
1698           entryIt.remove();
1699         } else {
1700           /* Otherwise, check if the target of the edge is changed... */
1701           summarizeSet(entry.getValue(), graph.varMap.get(entry.getKey()), single, summary);
1702         }
1703       }
1704       
1705       /* 2. Fix up the base variable edges */
1706
1707       for(Iterator<Map.Entry<TempDescriptor, MySet<Edge>>> entryIt=delta.basevaredge.entrySet().iterator();entryIt.hasNext();) {
1708         Map.Entry<TempDescriptor, MySet<Edge>> entry=entryIt.next();
1709         TempDescriptor entrytmp=entry.getKey();
1710         if (entrytmp==tmp) {
1711           /* Check is this is the tmp we overwrite, if so add to remove set */
1712           Util.relationUpdate(delta.varedgeremove, tmp, null, entry.getValue());
1713         } else {
1714           /* Check if the target of the edge is changed */ 
1715           MySet<Edge> newset=(MySet<Edge>)entry.getValue().clone();
1716           MySet<Edge> removeset=shrinkSet(newset, graph.varMap.get(entrytmp), single, summary);
1717           Util.relationUpdate(delta.varedgeremove, entrytmp, newset, removeset);
1718           Util.relationUpdate(delta.varedgeadd, entrytmp, null, newset);
1719         }
1720       }
1721
1722
1723       /* 3. Fix up heap edge additions */
1724
1725       HashMap<AllocNode, MySet<Edge>> addheapedge=new HashMap<AllocNode, MySet<Edge>>();
1726       for(Iterator<Map.Entry<AllocNode, MySet<Edge>>> entryIt=delta.heapedgeadd.entrySet().iterator();entryIt.hasNext();) {
1727         Map.Entry<AllocNode, MySet<Edge>> entry=entryIt.next();
1728         MySet<Edge> edgeset=entry.getValue();
1729         AllocNode allocnode=entry.getKey();
1730         if (allocnode==single) {
1731           entryIt.remove();
1732           summarizeSet(edgeset, graph.nodeMap.get(summary), single, summary);
1733           addheapedge.put(summary, edgeset);
1734         } else {
1735           summarizeSet(edgeset, graph.nodeMap.get(allocnode), single, summary);
1736         }
1737       }
1738       
1739       /* Merge in diffs */
1740
1741       for(Map.Entry<AllocNode, MySet<Edge>> entry:addheapedge.entrySet()) {
1742         AllocNode allocnode=entry.getKey();
1743         Util.relationUpdate(delta.heapedgeadd, allocnode, null, entry.getValue());
1744       }
1745
1746       /* 4. Fix up the base heap edges */
1747
1748       for(Iterator<Map.Entry<AllocNode, MySet<Edge>>> entryIt=delta.baseheapedge.entrySet().iterator();entryIt.hasNext();) {
1749         Map.Entry<AllocNode, MySet<Edge>> entry=entryIt.next();
1750         MySet<Edge> edgeset=entry.getValue();
1751         AllocNode allocnode=entry.getKey();
1752         if (allocnode==single) {
1753           entryIt.remove();
1754         }
1755         AllocNode addnode=(allocnode==single)?summary:allocnode;
1756
1757         MySet<Edge> newset=(MySet<Edge>) edgeset.clone();
1758         MySet<Edge> removeset=shrinkSet(newset, graph.nodeMap.get(addnode), single, summary);
1759         Util.relationUpdate(delta.heapedgeadd, addnode, null, newset);
1760         Util.relationUpdate(delta.heapedgeremove, allocnode, null, removeset);
1761       }
1762
1763       /* Update Node Ages...If the base or addNodeAges set contains a
1764        * single node, it now should also contain a summary node...  No
1765        * need to generate a single node as that has already been
1766        * done. */
1767       if (delta.baseNodeAges.contains(single)||delta.addNodeAges.contains(single)) {
1768         delta.addNodeAges.add(summary);
1769       }
1770
1771       //Kill the old node if someone tries to add it
1772       if (delta.addOldNodes.containsKey(single)||delta.baseOldNodes.containsKey(single)) {
1773         delta.addOldNodes.put(single, Boolean.FALSE);
1774       }
1775       
1776       //Apply incoming diffs to graph
1777       applyDiffs(graph, delta);      
1778     }
1779     return delta;
1780   }
1781
1782   /* This function builds a new edge set where oldnode is summarized into new node */
1783
1784   void summarizeSet(MySet<Edge> edgeset, MySet<Edge> oldedgeset, AllocNode oldnode, AllocNode sumnode) {
1785     MySet<Edge> newSet=null;
1786     for(Iterator<Edge> edgeit=edgeset.iterator();edgeit.hasNext();) {
1787       Edge e=edgeit.next();
1788       if (e.dst==oldnode||e.src==oldnode) {
1789         if (newSet==null) {
1790           newSet=new MySet<Edge>();
1791         }
1792         edgeit.remove();
1793         e=e.copy();
1794
1795         if (e.dst==oldnode) {
1796           e.dst=sumnode;
1797         }
1798         if (e.src==oldnode) {
1799           e.src=sumnode;
1800         }
1801         if (oldedgeset==null||!oldedgeset.contains(e))
1802           newSet.add(e);
1803       }
1804     }
1805     if (newSet!=null)
1806       edgeset.addAll(newSet);
1807   }
1808
1809   /* Shrinks the incoming set to just include rewritten values.
1810    * Returns a set of the original rewritten values */
1811
1812   MySet<Edge> shrinkSet(MySet<Edge> edgeset, MySet<Edge> oldedgeset, AllocNode oldnode, AllocNode newnode) {
1813     MySet<Edge> newSet=null;
1814     MySet<Edge> removeSet=null;
1815     for(Iterator<Edge> edgeit=edgeset.iterator();edgeit.hasNext();) {
1816       Edge e=edgeit.next();
1817       edgeit.remove();
1818       if (e.dst==oldnode||e.src==oldnode) {
1819         if (newSet==null) {
1820           newSet=new MySet<Edge>();
1821           removeSet=new MySet<Edge>();
1822         }
1823
1824         removeSet.add(e);
1825         e=e.copy();
1826         if (e.dst==oldnode)
1827           e.dst=newnode;
1828         if (e.src==oldnode)
1829           e.src=newnode;
1830         if (oldedgeset==null||!oldedgeset.contains(e))
1831           newSet.add(e);
1832       }
1833     }
1834     if (newSet!=null)
1835       edgeset.addAll(newSet);
1836     return removeSet;
1837   } 
1838
1839   /* This function returns a completely new Delta...  It is safe to
1840    * modify this */
1841
1842   Delta applyInitDelta(Delta delta, BBlock block) {
1843     //Apply delta to graph
1844     boolean newGraph=false;
1845     if (!bbgraphMap.containsKey(block)) {
1846       bbgraphMap.put(block, new Graph(null));
1847       newGraph=true;
1848     }
1849     Graph graph=bbgraphMap.get(block);
1850
1851     if (newGraph) {
1852       Delta newdelta=new Delta(null, true);
1853       //Add in heap edges and throw away original diff
1854
1855       for(Map.Entry<AllocNode, MySet<Edge>> entry:delta.heapedgeadd.entrySet()) {
1856         graph.nodeMap.put(entry.getKey(), new MySet<Edge>(entry.getValue()));
1857       }
1858       //Add in var edges and throw away original diff
1859       Set<TempDescriptor> livetemps=bblivetemps.get(block);
1860
1861       for(Map.Entry<TempDescriptor, MySet<Edge>> entry:delta.varedgeadd.entrySet()) {
1862         if (livetemps.contains(entry.getKey()))
1863           graph.varMap.put(entry.getKey(), new MySet<Edge>(entry.getValue()));
1864       }
1865       //Record that this is initial set...
1866       graph.nodeAges.addAll(delta.addNodeAges);
1867       //Add old nodes
1868       for(Map.Entry<AllocNode, Boolean> oldentry:delta.addOldNodes.entrySet()) {
1869         if (oldentry.getValue().booleanValue()) {
1870           graph.oldNodes.put(oldentry.getKey(), Boolean.TRUE);
1871         }
1872       }
1873       return newdelta;
1874     } else {
1875       Delta newdelta=new Delta(null, false);
1876       //merge in heap edges and variables
1877       mergeHeapEdges(graph, delta, newdelta);
1878       mergeVarEdges(graph, delta, newdelta, block);
1879       mergeAges(graph, delta, newdelta);
1880       return newdelta;
1881     }
1882   }
1883
1884   /* This function merges in the heap edges.  It updates delta to be
1885    * the difference */
1886
1887   void mergeHeapEdges(Graph graph, Delta delta, Delta newdelta) {
1888     //Merge in edges
1889     for(Map.Entry<AllocNode, MySet<Edge>> heapedge:delta.heapedgeadd.entrySet()) {
1890       AllocNode nsrc=heapedge.getKey();
1891       MySet<Edge> edges=heapedge.getValue();
1892
1893       if (graph.backMap!=null) {
1894         for(Edge e:edges) {
1895           if (!graph.backMap.containsKey(e.dst))
1896             graph.backMap.put(e.dst, new MySet<Edge>());
1897           graph.backMap.get(e.dst).add(e);
1898         }
1899       }
1900
1901       if (!graph.nodeMap.containsKey(nsrc)) {
1902         graph.nodeMap.put(nsrc, new MySet<Edge>());
1903       }
1904       MySet<Edge> dstedges=graph.nodeMap.get(nsrc);
1905       MySet<Edge> diffedges=new MySet<Edge>();
1906       for(Edge e:edges) {
1907         if (!dstedges.contains(e)) {
1908           //We have a new edge
1909           diffedges.add(e);
1910           dstedges.add(e);
1911         } else {
1912           Edge origedge=dstedges.get(e);
1913           if (!origedge.subsumes(e)) {
1914             Edge mergededge=origedge.merge(e);
1915             diffedges.add(mergededge);
1916             dstedges.add(mergededge);
1917           }
1918         }
1919       }
1920       //Done with edge set...
1921       if (diffedges.size()>0) {
1922         //completely new
1923         newdelta.baseheapedge.put(nsrc, diffedges);
1924       }
1925     }
1926   }
1927
1928   /* This function merges in the var edges.  It updates delta to be
1929    * the difference */
1930
1931   void mergeVarEdges(Graph graph, Delta delta, Delta newdelta, BBlock block) {
1932     //Merge in edges
1933     Set<TempDescriptor> livetemps=bblivetemps.get(block);
1934     
1935     for(Map.Entry<TempDescriptor, MySet<Edge>> varedge:delta.varedgeadd.entrySet()) {
1936       TempDescriptor tmpsrc=varedge.getKey();
1937       if (livetemps.contains(tmpsrc)) {
1938         MySet<Edge> edges=varedge.getValue();
1939         if (graph.backMap!=null) {
1940           for(Edge e:edges) {
1941             if (!graph.backMap.containsKey(e.dst))
1942               graph.backMap.put(e.dst, new MySet<Edge>());
1943             graph.backMap.get(e.dst).add(e);
1944           }
1945         }
1946         
1947         if (!graph.varMap.containsKey(tmpsrc)) {
1948           graph.varMap.put(tmpsrc, new MySet<Edge>());
1949         }
1950         MySet<Edge> dstedges=graph.varMap.get(tmpsrc);
1951         MySet<Edge> diffedges=new MySet<Edge>();
1952         for(Edge e:edges) {
1953           if (!dstedges.contains(e)) {
1954             //We have a new edge
1955             diffedges.add(e);
1956             dstedges.add(e);
1957           } else {
1958             Edge origedge=dstedges.get(e);
1959             if (!origedge.subsumes(e)) {
1960               Edge mergededge=origedge.merge(e);
1961               diffedges.add(mergededge);
1962               dstedges.add(mergededge);
1963             }
1964           }
1965         }
1966         //Done with edge set...
1967         if (diffedges.size()>0) {
1968           //completely new
1969           newdelta.basevaredge.put(tmpsrc,diffedges);
1970         }
1971       }
1972     }
1973   }
1974
1975   void mergeAges(Graph graph, Delta delta, Delta newDelta) {
1976     //Merge in edges
1977     for(AllocNode node:delta.addNodeAges) {
1978       if (!graph.nodeAges.contains(node)) {
1979         graph.nodeAges.add(node);
1980         newDelta.baseNodeAges.add(node);
1981       }
1982     }
1983     for(Map.Entry<AllocNode, Boolean> oldentry:delta.addOldNodes.entrySet()) {
1984       AllocNode node=oldentry.getKey();
1985       boolean ispresent=oldentry.getValue().booleanValue();
1986       if (ispresent&&!graph.oldNodes.containsKey(node)) {
1987         graph.oldNodes.put(node, Boolean.TRUE);
1988         newDelta.baseOldNodes.put(node, Boolean.TRUE);
1989       }
1990     }
1991   }
1992 }