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