changes to give compiler run times for phases....
[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 (false) {
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.dummyNode, 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.dummyNode, 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);
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     //Do nothing for non pointers
1303     if (!src.getType().isPtr())
1304       return delta;
1305
1306     if (delta.getInit()) {
1307       MySet<Edge> srcEdges=GraphManip.getEdges(graph, delta, src);
1308       MySet<Edge> dstEdges=GraphManip.getEdges(graph, delta, dst);
1309
1310       if (OoOJava&&!accessible.isAccessible(node, src)) {
1311         Taint srcStallTaint=Taint.factory(node,  src, AllocFactory.dummyNode, node, ReachGraph.predsEmpty);
1312         srcEdges=Edge.taintAll(srcEdges, srcStallTaint);
1313         updateVarDelta(graph, delta, src, srcEdges, null);
1314       }
1315
1316       if (OoOJava&&!accessible.isAccessible(node, dst)) {
1317         Taint dstStallTaint=Taint.factory(node,  dst, AllocFactory.dummyNode, node, ReachGraph.predsEmpty);
1318         dstEdges=Edge.taintAll(dstEdges, dstStallTaint);
1319         updateVarDelta(graph, delta, dst, dstEdges, null);
1320       }
1321
1322       MySet<Edge> edgesToAdd=GraphManip.genEdges(dstEdges, fd, srcEdges);
1323       MySet<Edge> edgesToRemove=null;
1324       if (dstEdges.size()==1&&!dstEdges.iterator().next().dst.isSummary()&&fd!=null) {
1325         /* Can do a strong update */
1326         edgesToRemove=GraphManip.getEdges(graph, delta, dstEdges, fd);
1327         graph.strongUpdateSet=edgesToRemove;
1328       } else
1329         graph.strongUpdateSet=new MySet<Edge>();
1330
1331       if (OoOJava) {
1332         effectsAnalysis.analyzeFlatSetFieldNode(dstEdges, fd, node);
1333       }
1334
1335       /* Update diff */
1336       updateHeapDelta(graph, delta, edgesToAdd, edgesToRemove);
1337       applyDiffs(graph, delta);
1338     } else {
1339       /* First look at new sources */
1340       MySet<Edge> edgesToAdd=new MySet<Edge>();
1341       MySet<Edge> newSrcEdges=GraphManip.getDiffEdges(delta, src);
1342       MySet<Edge> srcEdges=GraphManip.getEdges(graph, delta, src);
1343       HashSet<AllocNode> dstNodes=GraphManip.getNodes(graph, delta, dst);
1344       MySet<Edge> newDstEdges=GraphManip.getDiffEdges(delta, dst);
1345
1346       if (OoOJava&&!accessible.isAccessible(node, src)) {
1347         Taint srcStallTaint=Taint.factory(node,  src, AllocFactory.dummyNode, node, ReachGraph.predsEmpty);
1348         newSrcEdges=Edge.taintAll(newSrcEdges, srcStallTaint);
1349         updateVarDelta(graph, delta, src, newSrcEdges, null);
1350       }
1351
1352       if (OoOJava&&!accessible.isAccessible(node, dst)) {
1353         Taint dstStallTaint=Taint.factory(node,  dst, AllocFactory.dummyNode, node, ReachGraph.predsEmpty);
1354         newDstEdges=Edge.taintAll(newDstEdges, dstStallTaint);
1355         updateVarDelta(graph, delta, dst, newDstEdges, null);
1356       }
1357
1358       if (OoOJava) {
1359         effectsAnalysis.analyzeFlatSetFieldNode(newDstEdges, fd, node);
1360       }
1361
1362       MySet<Edge> edgesToRemove=null;
1363       if (newDstEdges.size()!=0) {
1364         if (dstNodes.size()>1&&!dstNodes.iterator().next().isSummary()&&fd!=null) {
1365           /* Need to undo strong update */
1366           if (graph.strongUpdateSet!=null) {
1367             edgesToAdd.addAll(graph.strongUpdateSet);
1368             graph.strongUpdateSet=null; //Prevent future strong updates
1369           }
1370         } else if (dstNodes.size()==1&&newDstEdges.size()==1&&!newDstEdges.iterator().next().dst.isSummary()&&graph.strongUpdateSet!=null&&fd!=null) {
1371           edgesToRemove=GraphManip.getEdges(graph, delta, dstNodes, fd);
1372           graph.strongUpdateSet.addAll(edgesToRemove);
1373         }
1374         Edge.mergeEdgesInto(edgesToAdd, GraphManip.genEdges(newDstEdges, fd, srcEdges));
1375       }
1376
1377       //Kill new edges
1378       if (graph.strongUpdateSet!=null&&fd!=null) {
1379         MySet<Edge> otherEdgesToRemove=GraphManip.getDiffEdges(delta, dstNodes, fd);
1380         if (edgesToRemove!=null)
1381           edgesToRemove.addAll(otherEdgesToRemove);
1382         else
1383           edgesToRemove=otherEdgesToRemove;
1384         graph.strongUpdateSet.addAll(otherEdgesToRemove);
1385       }
1386
1387       //Next look at new destinations
1388       Edge.mergeEdgesInto(edgesToAdd, GraphManip.genEdges(dstNodes, fd, newSrcEdges));
1389
1390       /* Update diff */
1391       updateHeapDelta(graph, delta, edgesToAdd, edgesToRemove);
1392       applyDiffs(graph, delta);
1393     }
1394     return delta;
1395   }
1396
1397   Delta processCopyNode(FlatNode node, Delta delta, Graph graph) {
1398     TempDescriptor src;
1399     TempDescriptor dst;
1400     if (node.kind()==FKind.FlatOpNode) {
1401       FlatOpNode fon=(FlatOpNode) node;
1402       src=fon.getLeft();
1403       dst=fon.getDest();
1404     } else if (node.kind()==FKind.FlatReturnNode) {
1405       FlatReturnNode frn=(FlatReturnNode)node;
1406       src=frn.getReturnTemp();
1407       dst=returntmp;
1408       if (src==null||!src.getType().isPtr()) {
1409         //This is a NOP
1410         applyDiffs(graph, delta);
1411         return delta;
1412       }
1413     } else {
1414       FlatCastNode fcn=(FlatCastNode) node;
1415       src=fcn.getSrc();
1416       dst=fcn.getDst();
1417     }
1418     if (delta.getInit()) {
1419       MySet<Edge> srcedges=GraphManip.getEdges(graph, delta, src);
1420       MySet<Edge> edgesToAdd=GraphManip.genEdges(dst, srcedges);
1421       MySet<Edge> edgesToRemove=GraphManip.getEdges(graph, delta, dst);
1422       updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
1423       applyDiffs(graph, delta);
1424     } else {
1425       /* First compute new src nodes */
1426       MySet<Edge> newSrcEdges=GraphManip.getDiffEdges(delta, src);
1427
1428       /* Compute the union, and then the set of edges */
1429       MySet<Edge> edgesToAdd=GraphManip.genEdges(dst, newSrcEdges);
1430       
1431       /* Compute set of edges to remove */
1432       MySet<Edge> edgesToRemove=GraphManip.getDiffEdges(delta, dst);      
1433
1434       /* Update diff */
1435       updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
1436       applyDiffs(graph, delta);
1437     }
1438     return delta;
1439   }
1440
1441   Delta processFieldElementNode(FlatNode node, Delta delta, Graph graph) {
1442     TempDescriptor src;
1443     FieldDescriptor fd;
1444     TempDescriptor dst;
1445     TaintSet taint=null;
1446
1447     if (node.kind()==FKind.FlatElementNode) {
1448       FlatElementNode fen=(FlatElementNode) node;
1449       src=fen.getSrc();
1450       fd=null;
1451       dst=fen.getDst();
1452     } else {
1453       FlatFieldNode ffn=(FlatFieldNode) node;
1454       src=ffn.getSrc();
1455       fd=ffn.getField();
1456       dst=ffn.getDst();
1457     }
1458     if (OoOJava&&!accessible.isAccessible(node, src)) {
1459       taint=TaintSet.factory(Taint.factory(node,  src, AllocFactory.dummyNode, node, ReachGraph.predsEmpty));
1460     }
1461
1462     //Do nothing for non pointers
1463     if (!dst.getType().isPtr())
1464       return delta;
1465     if (delta.getInit()) {
1466       MySet<Edge> srcedges=GraphManip.getEdges(graph, delta, src);
1467       MySet<Edge> edgesToAdd=GraphManip.dereference(graph, delta, dst, srcedges, fd, node, taint);
1468       MySet<Edge> edgesToRemove=GraphManip.getEdges(graph, delta, dst);
1469       if (OoOJava)
1470         effectsAnalysis.analyzeFlatFieldNode(srcedges, fd, node);
1471
1472       updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
1473       applyDiffs(graph, delta);
1474     } else {
1475       /* First compute new objects we read fields of */
1476       MySet<Edge> allsrcedges=GraphManip.getEdges(graph, delta, src);
1477       MySet<Edge> edgesToAdd=GraphManip.diffDereference(delta, dst, allsrcedges, fd, node, taint);
1478       /* Next compute new targets of fields */
1479       MySet<Edge> newsrcedges=GraphManip.getDiffEdges(delta, src);
1480       MySet<Edge> newfdedges=GraphManip.dereference(graph, delta, dst, newsrcedges, fd, node, taint);
1481
1482       /* Compute the union, and then the set of edges */
1483       Edge.mergeEdgesInto(edgesToAdd, newfdedges);
1484       
1485       /* Compute set of edges to remove */
1486       MySet<Edge> edgesToRemove=GraphManip.getDiffEdges(delta, dst);      
1487
1488       if (OoOJava)
1489         effectsAnalysis.analyzeFlatFieldNode(newsrcedges, fd, node);
1490       
1491       /* Update diff */
1492       updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
1493       applyDiffs(graph, delta);
1494     }
1495
1496     return delta;
1497   }
1498
1499   void updateVarDelta(Graph graph, Delta delta, TempDescriptor tmp, MySet<Edge> edgestoAdd, MySet<Edge> edgestoRemove) {
1500     MySet<Edge> edgeAdd=delta.varedgeadd.get(tmp);
1501     MySet<Edge> edgeRemove=delta.varedgeremove.get(tmp);
1502     MySet<Edge> existingEdges=graph.getEdges(tmp);
1503     if (edgestoRemove!=null)
1504       for(Edge e: edgestoRemove) {
1505         //remove edge from delta
1506         if (edgeAdd!=null)
1507           edgeAdd.remove(e);
1508         //if the edge is already in the graph, add an explicit remove to the delta
1509         if (existingEdges.contains(e))
1510           delta.removeVarEdge(e);
1511       }
1512     for(Edge e: edgestoAdd) {
1513       //Remove the edge from the remove set
1514       if (edgeRemove!=null)
1515         edgeRemove.remove(e);
1516       //Explicitly add it to the add set unless it is already in the graph
1517       if (typeUtil.isSuperorType(tmp.getType(), e.dst.getType())) {
1518         if (!existingEdges.contains(e)) {
1519           delta.addVarEdge(e);
1520         } else {
1521           //See if the old edge subsumes the new one
1522           Edge olde=existingEdges.get(e);
1523           if (!olde.subsumes(e)) {
1524             delta.addVarEdge(olde.merge(e));
1525           }
1526         }
1527       }
1528     }
1529   }
1530
1531   void updateHeapDelta(Graph graph, Delta delta, MySet<Edge> edgestoAdd, MySet<Edge> edgestoRemove) {
1532     if (edgestoRemove!=null)
1533       for(Edge e: edgestoRemove) {
1534         AllocNode src=e.src;
1535         MySet<Edge> edgeAdd=delta.heapedgeadd.get(src);
1536         MySet<Edge> existingEdges=graph.getEdges(src);
1537         //remove edge from delta
1538         if (edgeAdd!=null)
1539           edgeAdd.remove(e);
1540         //if the edge is already in the graph, add an explicit remove to the delta
1541         if (existingEdges.contains(e)) {
1542           delta.removeHeapEdge(e);
1543         }
1544       }
1545     if (edgestoAdd!=null)
1546       for(Edge e: edgestoAdd) {
1547         AllocNode src=e.src;
1548         MySet<Edge> edgeRemove=delta.heapedgeremove.get(src);
1549         MySet<Edge> existingEdges=graph.getEdges(src);
1550         //Remove the edge from the remove set
1551         if (edgeRemove!=null)
1552           edgeRemove.remove(e);
1553         //Explicitly add it to the add set unless it is already in the graph
1554         if (!existingEdges.contains(e)) {
1555           delta.addHeapEdge(e);
1556         } else {
1557           //See if the old edge subsumes the new one
1558           Edge olde=existingEdges.get(e);
1559           if (!olde.subsumes(e)) {
1560             delta.addHeapEdge(olde.merge(e));
1561           }
1562         }
1563       }
1564   }
1565
1566   Delta processFlatNop(FlatNode node, Delta delta, Graph graph) {
1567     applyDiffs(graph, delta);
1568     return delta;
1569   }
1570   
1571   Delta processNewNode(FlatNew node, Delta delta, Graph graph) {
1572     AllocNode summary=allocFactory.getAllocNode(node, true);
1573     AllocNode single=allocFactory.getAllocNode(node, false);
1574     TempDescriptor tmp=node.getDst();
1575
1576     if (delta.getInit()) {
1577       /* We don't have to deal with summarization here...  The
1578        * intuition is that this is the only place where we generate
1579        * nodes for this allocation site and this is the first time
1580        * we've analyzed this site */
1581
1582       //Build new Edge
1583       Edge e=new Edge(tmp, single);
1584       //Build new Edge set
1585       MySet<Edge> newedges=new MySet<Edge>();
1586       newedges.add(e);
1587       //Add it into the diffs
1588       delta.varedgeadd.put(tmp, newedges);
1589       //Remove the old edges
1590       MySet<Edge> oldedges=graph.getEdges(tmp);
1591       if (!oldedges.isEmpty())
1592         delta.varedgeremove.put(tmp, (MySet<Edge>) oldedges);
1593       //Apply incoming diffs to graph
1594       applyDiffs(graph, delta);
1595       //Note that we create a single node
1596       delta.addNodeAges.add(single);
1597       //Kill the old node
1598       if (delta.addOldNodes.containsKey(single)||delta.baseOldNodes.containsKey(single)) {
1599         delta.addOldNodes.put(single, Boolean.FALSE);
1600       }
1601     } else {
1602       /* 1. Fix up the variable edge additions */
1603
1604       for(Iterator<Map.Entry<TempDescriptor, MySet<Edge>>> entryIt=delta.varedgeadd.entrySet().iterator();entryIt.hasNext();) {
1605         Map.Entry<TempDescriptor, MySet<Edge>> entry=entryIt.next();
1606
1607         if (entry.getKey()==tmp) {
1608           /* Check if this is the tmp we overwrite */
1609           entryIt.remove();
1610         } else {
1611           /* Otherwise, check if the target of the edge is changed... */
1612           summarizeSet(entry.getValue(), graph.varMap.get(entry.getKey()), single, summary);
1613         }
1614       }
1615       
1616       /* 2. Fix up the base variable edges */
1617
1618       for(Iterator<Map.Entry<TempDescriptor, MySet<Edge>>> entryIt=delta.basevaredge.entrySet().iterator();entryIt.hasNext();) {
1619         Map.Entry<TempDescriptor, MySet<Edge>> entry=entryIt.next();
1620         TempDescriptor entrytmp=entry.getKey();
1621         if (entrytmp==tmp) {
1622           /* Check is this is the tmp we overwrite, if so add to remove set */
1623           Util.relationUpdate(delta.varedgeremove, tmp, null, entry.getValue());
1624         } else {
1625           /* Check if the target of the edge is changed */ 
1626           MySet<Edge> newset=(MySet<Edge>)entry.getValue().clone();
1627           MySet<Edge> removeset=shrinkSet(newset, graph.varMap.get(entrytmp), single, summary);
1628           Util.relationUpdate(delta.varedgeremove, entrytmp, newset, removeset);
1629           Util.relationUpdate(delta.varedgeadd, entrytmp, null, newset);
1630         }
1631       }
1632
1633
1634       /* 3. Fix up heap edge additions */
1635
1636       HashMap<AllocNode, MySet<Edge>> addheapedge=new HashMap<AllocNode, MySet<Edge>>();
1637       for(Iterator<Map.Entry<AllocNode, MySet<Edge>>> entryIt=delta.heapedgeadd.entrySet().iterator();entryIt.hasNext();) {
1638         Map.Entry<AllocNode, MySet<Edge>> entry=entryIt.next();
1639         MySet<Edge> edgeset=entry.getValue();
1640         AllocNode allocnode=entry.getKey();
1641         if (allocnode==single) {
1642           entryIt.remove();
1643           summarizeSet(edgeset, graph.nodeMap.get(summary), single, summary);
1644           addheapedge.put(summary, edgeset);
1645         } else {
1646           summarizeSet(edgeset, graph.nodeMap.get(allocnode), single, summary);
1647         }
1648       }
1649       
1650       /* Merge in diffs */
1651
1652       for(Map.Entry<AllocNode, MySet<Edge>> entry:addheapedge.entrySet()) {
1653         AllocNode allocnode=entry.getKey();
1654         Util.relationUpdate(delta.heapedgeadd, allocnode, null, entry.getValue());
1655       }
1656
1657       /* 4. Fix up the base heap edges */
1658
1659       for(Iterator<Map.Entry<AllocNode, MySet<Edge>>> entryIt=delta.baseheapedge.entrySet().iterator();entryIt.hasNext();) {
1660         Map.Entry<AllocNode, MySet<Edge>> entry=entryIt.next();
1661         MySet<Edge> edgeset=entry.getValue();
1662         AllocNode allocnode=entry.getKey();
1663         if (allocnode==single) {
1664           entryIt.remove();
1665         }
1666         AllocNode addnode=(allocnode==single)?summary:allocnode;
1667
1668         MySet<Edge> newset=(MySet<Edge>) edgeset.clone();
1669         MySet<Edge> removeset=shrinkSet(newset, graph.nodeMap.get(addnode), single, summary);
1670         Util.relationUpdate(delta.heapedgeadd, addnode, null, newset);
1671         Util.relationUpdate(delta.heapedgeremove, allocnode, null, removeset);
1672       }
1673
1674       /* Update Node Ages...If the base or addNodeAges set contains a
1675        * single node, it now should also contain a summary node...  No
1676        * need to generate a single node as that has already been
1677        * done. */
1678       if (delta.baseNodeAges.contains(single)||delta.addNodeAges.contains(single)) {
1679         delta.addNodeAges.add(summary);
1680       }
1681
1682       //Kill the old node if someone tries to add it
1683       if (delta.addOldNodes.containsKey(single)||delta.baseOldNodes.containsKey(single)) {
1684         delta.addOldNodes.put(single, Boolean.FALSE);
1685       }
1686       
1687       //Apply incoming diffs to graph
1688       applyDiffs(graph, delta);      
1689     }
1690     return delta;
1691   }
1692
1693   /* This function builds a new edge set where oldnode is summarized into new node */
1694
1695   void summarizeSet(MySet<Edge> edgeset, MySet<Edge> oldedgeset, AllocNode oldnode, AllocNode sumnode) {
1696     MySet<Edge> newSet=null;
1697     for(Iterator<Edge> edgeit=edgeset.iterator();edgeit.hasNext();) {
1698       Edge e=edgeit.next();
1699       if (e.dst==oldnode||e.src==oldnode) {
1700         if (newSet==null) {
1701           newSet=new MySet<Edge>();
1702         }
1703         edgeit.remove();
1704         e=e.copy();
1705
1706         if (e.dst==oldnode) {
1707           e.dst=sumnode;
1708         }
1709         if (e.src==oldnode) {
1710           e.src=sumnode;
1711         }
1712         if (oldedgeset==null||!oldedgeset.contains(e))
1713           newSet.add(e);
1714       }
1715     }
1716     if (newSet!=null)
1717       edgeset.addAll(newSet);
1718   }
1719
1720   /* Shrinks the incoming set to just include rewritten values.
1721    * Returns a set of the original rewritten values */
1722
1723   MySet<Edge> shrinkSet(MySet<Edge> edgeset, MySet<Edge> oldedgeset, AllocNode oldnode, AllocNode newnode) {
1724     MySet<Edge> newSet=null;
1725     MySet<Edge> removeSet=null;
1726     for(Iterator<Edge> edgeit=edgeset.iterator();edgeit.hasNext();) {
1727       Edge e=edgeit.next();
1728       edgeit.remove();
1729       if (e.dst==oldnode||e.src==oldnode) {
1730         if (newSet==null) {
1731           newSet=new MySet<Edge>();
1732           removeSet=new MySet<Edge>();
1733         }
1734
1735         removeSet.add(e);
1736         e=e.copy();
1737         if (e.dst==oldnode)
1738           e.dst=newnode;
1739         if (e.src==oldnode)
1740           e.src=newnode;
1741         if (oldedgeset==null||!oldedgeset.contains(e))
1742           newSet.add(e);
1743       }
1744     }
1745     if (newSet!=null)
1746       edgeset.addAll(newSet);
1747     return removeSet;
1748   } 
1749
1750   /* This function returns a completely new Delta...  It is safe to
1751    * modify this */
1752
1753   Delta applyInitDelta(Delta delta, BBlock block) {
1754     //Apply delta to graph
1755     boolean newGraph=false;
1756     if (!bbgraphMap.containsKey(block)) {
1757       bbgraphMap.put(block, new Graph(null));
1758       newGraph=true;
1759     }
1760     Graph graph=bbgraphMap.get(block);
1761
1762     if (newGraph) {
1763       Delta newdelta=new Delta(null, true);
1764       //Add in heap edges and throw away original diff
1765
1766       for(Map.Entry<AllocNode, MySet<Edge>> entry:delta.heapedgeadd.entrySet()) {
1767         graph.nodeMap.put(entry.getKey(), new MySet<Edge>(entry.getValue()));
1768       }
1769       //Add in var edges and throw away original diff
1770       Set<TempDescriptor> livetemps=bblivetemps.get(block);
1771
1772       for(Map.Entry<TempDescriptor, MySet<Edge>> entry:delta.varedgeadd.entrySet()) {
1773         if (livetemps.contains(entry.getKey()))
1774           graph.varMap.put(entry.getKey(), new MySet<Edge>(entry.getValue()));
1775       }
1776       //Record that this is initial set...
1777       graph.nodeAges.addAll(delta.addNodeAges);
1778       //Add old nodes
1779       for(Map.Entry<AllocNode, Boolean> oldentry:delta.addOldNodes.entrySet()) {
1780         if (oldentry.getValue().booleanValue()) {
1781           graph.oldNodes.put(oldentry.getKey(), Boolean.TRUE);
1782         }
1783       }
1784       return newdelta;
1785     } else {
1786       Delta newdelta=new Delta(null, false);
1787       //merge in heap edges and variables
1788       mergeHeapEdges(graph, delta, newdelta);
1789       mergeVarEdges(graph, delta, newdelta, block);
1790       mergeAges(graph, delta, newdelta);
1791       return newdelta;
1792     }
1793   }
1794
1795   /* This function merges in the heap edges.  It updates delta to be
1796    * the difference */
1797
1798   void mergeHeapEdges(Graph graph, Delta delta, Delta newdelta) {
1799     //Merge in edges
1800     for(Map.Entry<AllocNode, MySet<Edge>> heapedge:delta.heapedgeadd.entrySet()) {
1801       AllocNode nsrc=heapedge.getKey();
1802       MySet<Edge> edges=heapedge.getValue();
1803
1804       if (graph.backMap!=null) {
1805         for(Edge e:edges) {
1806           if (!graph.backMap.containsKey(e.dst))
1807             graph.backMap.put(e.dst, new MySet<Edge>());
1808           graph.backMap.get(e.dst).add(e);
1809         }
1810       }
1811
1812       if (!graph.nodeMap.containsKey(nsrc)) {
1813         graph.nodeMap.put(nsrc, new MySet<Edge>());
1814       }
1815       MySet<Edge> dstedges=graph.nodeMap.get(nsrc);
1816       MySet<Edge> diffedges=new MySet<Edge>();
1817       for(Edge e:edges) {
1818         if (!dstedges.contains(e)) {
1819           //We have a new edge
1820           diffedges.add(e);
1821           dstedges.add(e);
1822         } else {
1823           Edge origedge=dstedges.get(e);
1824           if (!origedge.subsumes(e)) {
1825             Edge mergededge=origedge.merge(e);
1826             diffedges.add(mergededge);
1827             dstedges.add(mergededge);
1828           }
1829         }
1830       }
1831       //Done with edge set...
1832       if (diffedges.size()>0) {
1833         //completely new
1834         newdelta.baseheapedge.put(nsrc, diffedges);
1835       }
1836     }
1837   }
1838
1839   /* This function merges in the var edges.  It updates delta to be
1840    * the difference */
1841
1842   void mergeVarEdges(Graph graph, Delta delta, Delta newdelta, BBlock block) {
1843     //Merge in edges
1844     Set<TempDescriptor> livetemps=bblivetemps.get(block);
1845     
1846     for(Map.Entry<TempDescriptor, MySet<Edge>> varedge:delta.varedgeadd.entrySet()) {
1847       TempDescriptor tmpsrc=varedge.getKey();
1848       if (livetemps.contains(tmpsrc)) {
1849         MySet<Edge> edges=varedge.getValue();
1850         if (graph.backMap!=null) {
1851           for(Edge e:edges) {
1852             if (!graph.backMap.containsKey(e.dst))
1853               graph.backMap.put(e.dst, new MySet<Edge>());
1854             graph.backMap.get(e.dst).add(e);
1855           }
1856         }
1857         
1858         if (!graph.varMap.containsKey(tmpsrc)) {
1859           graph.varMap.put(tmpsrc, new MySet<Edge>());
1860         }
1861         MySet<Edge> dstedges=graph.varMap.get(tmpsrc);
1862         MySet<Edge> diffedges=new MySet<Edge>();
1863         for(Edge e:edges) {
1864           if (!dstedges.contains(e)) {
1865             //We have a new edge
1866             diffedges.add(e);
1867             dstedges.add(e);
1868           } else {
1869             Edge origedge=dstedges.get(e);
1870             if (!origedge.subsumes(e)) {
1871               Edge mergededge=origedge.merge(e);
1872               diffedges.add(mergededge);
1873               dstedges.add(mergededge);
1874             }
1875           }
1876         }
1877         //Done with edge set...
1878         if (diffedges.size()>0) {
1879           //completely new
1880           newdelta.basevaredge.put(tmpsrc,diffedges);
1881         }
1882       }
1883     }
1884   }
1885
1886   void mergeAges(Graph graph, Delta delta, Delta newDelta) {
1887     //Merge in edges
1888     for(AllocNode node:delta.addNodeAges) {
1889       if (!graph.nodeAges.contains(node)) {
1890         graph.nodeAges.add(node);
1891         newDelta.baseNodeAges.add(node);
1892       }
1893     }
1894     for(Map.Entry<AllocNode, Boolean> oldentry:delta.addOldNodes.entrySet()) {
1895       AllocNode node=oldentry.getKey();
1896       boolean ispresent=oldentry.getValue().booleanValue();
1897       if (ispresent&&!graph.oldNodes.containsKey(node)) {
1898         graph.oldNodes.put(node, Boolean.TRUE);
1899         newDelta.baseOldNodes.put(node, Boolean.TRUE);
1900       }
1901     }
1902   }
1903 }