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