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