bb31a9c005dbe4d473ba1301b0da7c0e70153985
[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.Pointer.BasicBlock.BBlock;
6 import Analysis.Pointer.AllocFactory.AllocNode;
7
8 public class Pointer {
9   HashMap<FlatMethod, BasicBlock> blockMap;
10   HashMap<BBlock, Graph> bbgraphMap;
11   HashMap<FlatNode, Graph> graphMap;
12   HashMap<FlatCall, Set<BBlock>> callMap;
13   HashMap<BBlock, Set<PPoint>> returnMap;
14
15   State state;
16   TypeUtil typeUtil;
17   AllocFactory allocFactory;
18   LinkedList<Delta> toprocess;
19   TempDescriptor returntmp;
20
21   public Pointer(State state, TypeUtil typeUtil) {
22     this.state=state;
23     this.blockMap=new HashMap<FlatMethod, BasicBlock>();
24     this.bbgraphMap=new HashMap<BBlock, Graph>();
25     this.graphMap=new HashMap<FlatNode, Graph>();
26     this.callMap=new HashMap<FlatCall, Set<BBlock>>();
27     this.returnMap=new HashMap<BBlock, Set<PPoint>>();
28     this.typeUtil=typeUtil;
29     this.allocFactory=new AllocFactory(state, typeUtil);
30     this.toprocess=new LinkedList<Delta>();
31     ClassDescriptor stringcd=typeUtil.getClass(TypeUtil.ObjectClass);
32     this.returntmp=new TempDescriptor("RETURNVAL", stringcd);
33   }
34
35   public BasicBlock getBBlock(FlatMethod fm) {
36     if (!blockMap.containsKey(fm))
37       blockMap.put(fm, BasicBlock.getBBlock(fm));
38     return blockMap.get(fm);
39   }
40   
41   Delta buildInitialContext() {
42     MethodDescriptor md=typeUtil.getMain();
43     FlatMethod fm=state.getMethodFlat(md);
44     BasicBlock bb=getBBlock(fm);
45     BBlock start=bb.getStart();
46     Delta delta=new Delta(new PPoint(start), true);
47     MySet<Edge> arrayset=new MySet<Edge>();
48     MySet<Edge> varset=new MySet<Edge>();
49     Edge arrayedge=new Edge(allocFactory.StringArray, null, allocFactory.Strings);
50     arrayset.add(arrayedge);
51     Edge stringedge=new Edge(fm.getParameter(0), allocFactory.StringArray);
52     varset.add(stringedge);
53     delta.heapedgeadd.put(allocFactory.StringArray, arrayset);
54     delta.varedgeadd.put(fm.getParameter(0), varset);
55     return delta;
56   }
57
58   public void doAnalysis() {
59     toprocess.add(buildInitialContext());
60
61     while(!toprocess.isEmpty()) {
62       Delta delta=toprocess.remove();
63       PPoint ppoint=delta.getBlock();
64       BBlock bblock=ppoint.getBBlock();
65       Vector<FlatNode> nodes=bblock.nodes();
66
67       //Build base graph for entrance to this basic block
68       delta=applyInitDelta(delta, bblock);
69       Graph graph=bbgraphMap.get(bblock);
70
71       Graph nodeGraph=null;
72       //Compute delta at exit of each node
73       for(int i=0; i<nodes.size();i++) {
74         FlatNode currNode=nodes.get(i);
75         if (!graphMap.containsKey(currNode)) {
76           graphMap.put(currNode, new Graph(graph));
77         }
78         nodeGraph=graphMap.get(currNode);
79         delta=processNode(bblock, i, currNode, delta, nodeGraph);
80       }
81       generateFinalDelta(bblock, delta, nodeGraph);
82     }
83   }
84
85   void buildInitDelta(Graph graph, Delta newDelta) {
86     //First compute the set of temps
87     HashSet<TempDescriptor> tmpSet=new HashSet<TempDescriptor>();
88     tmpSet.addAll(graph.varMap.keySet());
89     tmpSet.addAll(graph.parent.varMap.keySet());
90     
91     //Next build the temp map part of the delta
92     for(TempDescriptor tmp:tmpSet) {
93       MySet<Edge> edgeSet=new MySet<Edge>();
94       /* Get target set */
95       if (graph.varMap.containsKey(tmp))
96         edgeSet.addAll(graph.varMap.get(tmp));
97       else
98         edgeSet.addAll(graph.parent.varMap.get(tmp));
99       newDelta.varedgeadd.put(tmp, edgeSet);
100     }
101     
102     //Next compute the set of src allocnodes
103     HashSet<AllocNode> nodeSet=new HashSet<AllocNode>();
104     nodeSet.addAll(graph.nodeMap.keySet());
105     nodeSet.addAll(graph.parent.nodeMap.keySet());
106     
107     for(AllocNode node:nodeSet) {
108       MySet<Edge> edgeSet=new MySet<Edge>();
109       /* Get edge set */
110       if (graph.nodeMap.containsKey(node))
111         edgeSet.addAll(graph.nodeMap.get(node));
112       else
113         edgeSet.addAll(graph.parent.nodeMap.get(node));
114       newDelta.heapedgeadd.put(node, edgeSet);
115       
116       /* Compute ages */
117       if (graph.nodeAges.contains(node))
118         newDelta.addNodeAges.add(node);
119       else if (graph.parent.nodeAges.contains(node))
120         newDelta.addNodeAges.add(node);
121     }
122   }
123
124   void generateFinalDelta(BBlock bblock, Delta delta, Graph graph) {
125     Delta newDelta=new Delta(null, false);
126     if (delta.getInit()) {
127       buildInitDelta(graph, newDelta);
128     } else {
129       /* We can break the old delta...it is done being used */
130       /* First we will build variable edges */
131       HashSet<TempDescriptor> tmpSet=new HashSet<TempDescriptor>();
132       tmpSet.addAll(delta.basevaredge.keySet());
133       tmpSet.addAll(delta.varedgeadd.keySet());
134       for(TempDescriptor tmp:tmpSet) {
135         /* Start with the new incoming edges */
136         MySet<Edge> newbaseedge=delta.basevaredge.get(tmp);
137         /* Remove the remove set */
138         newbaseedge.removeAll(delta.varedgeremove.get(tmp));
139         /* Add in the new set*/
140         newbaseedge.addAll(delta.varedgeadd.get(tmp));
141         /* Store the results */
142         newDelta.varedgeadd.put(tmp, newbaseedge);
143       }
144
145       /* Next we build heap edges */
146       HashSet<AllocNode> nodeSet=new HashSet<AllocNode>();
147       nodeSet.addAll(delta.baseheapedge.keySet());
148       nodeSet.addAll(delta.heapedgeadd.keySet());
149       nodeSet.addAll(delta.heapedgeremove.keySet());
150       for(AllocNode node:nodeSet) {
151         /* Start with the new incoming edges */
152         MySet<Edge> newheapedge=(MySet<Edge>) delta.baseheapedge.get(node).clone();
153         /* Remove the remove set */
154         newheapedge.removeAll(delta.heapedgeremove.get(node));
155         /* Add in the add set */
156         newheapedge.addAll(delta.heapedgeadd.get(node));
157         newDelta.heapedgeadd.put(node, newheapedge);
158
159         /* Also need to subtract off some edges */
160         MySet<Edge> removeset=delta.heapedgeremove.get(node);
161
162         /* Remove the newly created edges..no need to propagate a diff for those */
163         removeset.removeAll(delta.baseheapedge.get(node));
164         newDelta.heapedgeremove.put(node, removeset);
165       }
166
167       /* Compute new ages */
168       newDelta.addNodeAges.addAll(delta.baseNodeAges);
169       newDelta.addNodeAges.addAll(delta.addNodeAges);
170     }
171
172     /* Now we need to propagate newdelta */
173     if (!newDelta.heapedgeadd.isEmpty()||!newDelta.heapedgeremove.isEmpty()||!newDelta.varedgeadd.isEmpty()||!newDelta.addNodeAges.isEmpty()) {
174       /* We have a delta to propagate */
175       Vector<BBlock> blockvector=bblock.next();
176       for(int i=0;i<blockvector.size();i++) {
177         if (i==0) {
178           newDelta.setBlock(new PPoint(blockvector.get(i)));
179           toprocess.add(newDelta);
180         } else {
181           toprocess.add(newDelta.diffBlock(new PPoint(blockvector.get(i))));
182         }
183       }
184     }
185   }
186
187   Delta processNode(BBlock bblock, int index, FlatNode node, Delta delta, Graph newgraph) {
188     switch(node.kind()) {
189     case FKind.FlatNew:
190       return processNewNode((FlatNew)node, delta, newgraph);
191     case FKind.FlatFieldNode:
192     case FKind.FlatElementNode:
193       return processFieldElementNode(node, delta, newgraph);
194     case FKind.FlatCastNode:
195     case FKind.FlatOpNode:
196     case FKind.FlatReturnNode:
197       return processCopyNode(node, delta, newgraph);
198     case FKind.FlatSetFieldNode:
199     case FKind.FlatSetElementNode:
200       return processSetFieldElementNode(node, delta, newgraph);
201     case FKind.FlatMethod:
202     case FKind.FlatExit:
203       return processFlatNop(node, delta, newgraph);
204     case FKind.FlatCall:
205       return processFlatCall(bblock, index, (FlatCall) node, delta, newgraph);
206     case FKind.FlatSESEEnterNode:
207     case FKind.FlatSESEExitNode:
208       throw new Error("Unimplemented node:"+node);
209     default:
210       throw new Error("Unrecognized node:"+node);
211     }
212   }
213
214
215   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) {
216     //Handle the this temp
217     if (tmpthis!=null) {
218       MySet<Edge> edges=(oldnodeset!=null)?GraphManip.getDiffEdges(delta, tmpthis):GraphManip.getEdges(graph, delta, tmpthis);
219       newDelta.varedgeadd.put(tmpthis, (MySet<Edge>) edges.clone());
220       edgeset.addAll(edges);
221       for(Edge e:edges) {
222         AllocNode dstnode=e.dst;
223         if (!nodeset.contains(dstnode)&&(oldnodeset==null||!oldnodeset.contains(dstnode))) {
224           TypeDescriptor type=dstnode.getType();
225           if (!type.isArray()) {
226             targetSet.add(type.getClassDesc());
227           } else {
228             //arrays don't have code
229             targetSet.add(typeUtil.getClass(TypeUtil.ObjectClass));
230           }
231           nodeset.add(dstnode);
232           tovisit.add(dstnode);
233         }
234       }
235     }
236   }
237
238   void processParams(Graph graph, Delta delta, Delta newDelta, HashSet<AllocNode> nodeset, Stack<AllocNode> tovisit, MySet<Edge> edgeset, FlatCall fcall, boolean diff) {
239     //Go through each temp
240     for(int i=0;i<fcall.numArgs();i++) {
241       TempDescriptor tmp=fcall.getArg(i);
242       MySet<Edge> edges=diff?GraphManip.getDiffEdges(delta, tmp):GraphManip.getEdges(graph, delta, tmp);
243       newDelta.varedgeadd.put(tmp, (MySet<Edge>) edges.clone());
244       edgeset.addAll(edges);
245       for(Edge e:edges) {
246         if (!nodeset.contains(e.dst)) {
247           nodeset.add(e.dst);
248           tovisit.add(e.dst);
249         }
250       }
251     }
252   }
253
254   void computeReachableNodes(Graph graph, Delta delta, Delta newDelta, HashSet<AllocNode> nodeset, Stack<AllocNode> tovisit, MySet<Edge> edgeset, HashSet<AllocNode> oldnodeset) {
255       while(!tovisit.isEmpty()) {
256         AllocNode node=tovisit.pop();
257         MySet<Edge> edges=GraphManip.getEdges(graph, delta, node);
258         newDelta.heapedgeadd.put(node, edges);
259         edgeset.addAll(edges);
260         for(Edge e:edges) {
261           if (!nodeset.contains(e.dst)&&(oldnodeset==null||!oldnodeset.contains(e.dst))) {
262             nodeset.add(e.dst);
263             tovisit.add(e.dst);
264           }
265         }
266       }
267   }
268
269   HashSet<MethodDescriptor> computeTargets(FlatCall fcall, Delta newDelta) {
270     TempDescriptor tmpthis=fcall.getThis();
271     MethodDescriptor md=fcall.getMethod();
272     HashSet<MethodDescriptor> targets=new HashSet<MethodDescriptor>();
273     if (md.isStatic()) {
274       targets.add(md);
275     } else {
276       //Compute Edges
277       for(Edge e:newDelta.varedgeadd.get(tmpthis)) {
278         AllocNode node=e.dst;
279         ClassDescriptor cd=node.getType().getClassDesc();
280         //Figure out exact method called and add to set
281         targets.add(cd.getCalledMethod(md));
282       }
283     }
284     return targets;
285   }
286
287
288   void fixMapping(FlatCall fcall, HashSet<MethodDescriptor> targets, MySet<Edge> oldedgeset, Delta newDelta, BBlock callblock, int callindex) {
289     Delta basedelta=null;
290     TempDescriptor tmpthis=fcall.getThis();
291
292     for(MethodDescriptor calledmd:targets) {
293       FlatMethod fm=state.getMethodFlat(calledmd);
294       boolean newmethod=false;
295       
296       //Build tmpMap
297       HashMap<TempDescriptor, TempDescriptor> tmpMap=new HashMap<TempDescriptor, TempDescriptor>();
298       int offset=0;
299       if(tmpthis!=null) {
300         tmpMap.put(tmpthis, fm.getParameter(offset++));
301       }
302       for(int i=0;i<fcall.numArgs();i++) {
303         TempDescriptor tmp=fcall.getArg(i);
304         tmpMap.put(tmp,fm.getParameter(i+offset));
305       }
306
307       //Get basicblock for the method
308       BasicBlock block=getBBlock(fm);
309       
310       //Hook up exits
311       if (!callMap.containsKey(fcall)) {
312         callMap.put(fcall, new HashSet<BBlock>());
313       }
314       
315       Delta returnDelta=null;
316       
317       if (!callMap.get(fcall).contains(block.getStart())) {
318         callMap.get(fcall).add(block.getStart());
319         newmethod=true;
320         
321         //Hook up return
322         if (!returnMap.containsKey(block.getExit())) {
323           returnMap.put(block.getExit(), new HashSet<PPoint>());
324         }
325         returnMap.get(block.getExit()).add(new PPoint(callblock, callindex));
326         
327         if (bbgraphMap.containsKey(block.getExit())) {
328           //Need to push existing results to current node
329           if (returnDelta==null) {
330             returnDelta=new Delta(null, false);
331             buildInitDelta(bbgraphMap.get(block.getExit()), returnDelta);
332             if (!returnDelta.heapedgeadd.isEmpty()||!returnDelta.heapedgeremove.isEmpty()||!returnDelta.varedgeadd.isEmpty()) {
333               returnDelta.setBlock(new PPoint(callblock, callindex));
334               toprocess.add(returnDelta);
335             }
336           } else {
337             if (!returnDelta.heapedgeadd.isEmpty()||!returnDelta.heapedgeremove.isEmpty()||!returnDelta.varedgeadd.isEmpty()) {
338               toprocess.add(returnDelta.diffBlock(new PPoint(callblock, callindex)));
339             }
340           }
341         }
342       }
343       
344       if (oldedgeset==null) {
345         //First build of this graph
346         //Build and enqueue delta...safe to just use existing delta
347         Delta d=newDelta.changeParams(tmpMap, new PPoint(block.getStart()));
348         toprocess.add(d);
349       } else if (newmethod) {
350         if (basedelta==null) {
351           basedelta=newDelta.buildBase(oldedgeset);
352         }
353         //Build and enqueue delta
354         Delta d=basedelta.changeParams(tmpMap, new PPoint(block.getStart()));
355         toprocess.add(d);
356       } else  {
357         //Build and enqueue delta
358         Delta d=newDelta.changeParams(tmpMap, new PPoint(block.getStart()));
359         toprocess.add(d);
360       }
361     }
362   }
363   
364
365   Delta processFlatCall(BBlock callblock, int callindex, FlatCall fcall, Delta delta, Graph graph) {
366     Delta newDelta=new Delta(null, false);
367
368     if (delta.getInit()) {
369       MySet<Edge> edgeset=new MySet<Edge>();
370       HashSet<AllocNode> nodeset=new HashSet<AllocNode>();
371       HashSet<ClassDescriptor> targetSet=new HashSet<ClassDescriptor>();
372       Stack<AllocNode> tovisit=new Stack<AllocNode>();
373       TempDescriptor tmpthis=fcall.getThis();
374
375       //Handle the this temp
376       processThisTargets(targetSet, graph, delta, newDelta, nodeset, tovisit, edgeset, tmpthis, null);
377
378       //Go through each temp
379       processParams(graph, delta, newDelta, nodeset, tovisit, edgeset, fcall, false);
380       
381       //Traverse all reachable nodes
382       computeReachableNodes(graph, delta, newDelta, nodeset, tovisit, edgeset, null);
383
384       //Compute call targets
385       HashSet<MethodDescriptor> targets=computeTargets(fcall, newDelta);
386
387       //Fix mapping
388       fixMapping(fcall, targets, null, newDelta, callblock, callindex);
389
390       graph.reachNode=nodeset;
391       graph.reachEdge=edgeset;
392       
393       //Apply diffs to graph
394       applyDiffs(graph, delta);
395     } else {
396       MySet<Edge> edgeset=new MySet<Edge>();
397       HashSet<AllocNode> nodeset=new HashSet<AllocNode>();
398       MySet<Edge> oldedgeset=graph.reachEdge;
399       HashSet<AllocNode> oldnodeset=graph.reachNode;
400
401       HashSet<ClassDescriptor> targetSet=new HashSet<ClassDescriptor>();
402       Stack<AllocNode> tovisit=new Stack<AllocNode>();
403       TempDescriptor tmpthis=fcall.getThis();
404
405       //Handle the this temp
406       processThisTargets(targetSet, graph, delta, newDelta, nodeset, tovisit, edgeset, tmpthis, oldnodeset);
407
408       //Go through each temp
409       processParams(graph, delta, newDelta, nodeset, tovisit, edgeset, fcall, true);
410
411       //Go through each new heap edge that starts from old node
412       MySet<Edge> newedges=GraphManip.getDiffEdges(delta, oldnodeset);
413       edgeset.addAll(newedges);
414       for(Edge e:newedges) {
415         if (!nodeset.contains(e.dst)&&!oldnodeset.contains(e.dst)) {
416           nodeset.add(e.dst);
417           tovisit.add(e.dst);
418         }
419       }
420       
421       //Traverse all reachable nodes
422       computeReachableNodes(graph, delta, newDelta, nodeset, tovisit, edgeset, oldnodeset);
423
424       //Compute call targets
425       HashSet<MethodDescriptor> targets=computeTargets(fcall, newDelta);
426
427       //add in new nodeset and edgeset
428       oldnodeset.addAll(nodeset);
429       oldedgeset.addAll(edgeset);
430
431       //Fix mapping
432       fixMapping(fcall, targets, oldedgeset, newDelta, callblock, callindex);
433
434       //Apply diffs to graph
435       applyDiffs(graph, delta);
436     }
437     return newDelta;
438   }
439
440   void applyDiffs(Graph graph, Delta delta) {
441     //Add hidden base edges
442     for(Map.Entry<AllocNode, MySet<Edge>> e: delta.baseheapedge.entrySet()) {
443       AllocNode node=e.getKey();
444       MySet<Edge> edges=e.getValue();
445       if (graph.nodeMap.containsKey(node)) {
446         MySet<Edge> nodeEdges=graph.nodeMap.get(node);
447         nodeEdges.addAll(edges);
448       }
449     }
450
451     //Remove heap edges
452     for(Map.Entry<AllocNode, MySet<Edge>> e: delta.heapedgeremove.entrySet()) {
453       AllocNode node=e.getKey();
454       MySet<Edge> edgestoremove=e.getValue();
455       if (graph.nodeMap.containsKey(node)) {
456         //Just apply diff to current map
457         graph.nodeMap.get(node).removeAll(edgestoremove);
458       } else {
459         //Generate diff from parent graph
460         MySet<Edge> parentedges=graph.parent.nodeMap.get(node);
461         MySet<Edge> newedgeset=Util.setSubtract(parentedges, edgestoremove);
462         graph.nodeMap.put(node, newedgeset);
463       }
464     }
465
466     //Add heap edges
467     for(Map.Entry<AllocNode, MySet<Edge>> e: delta.heapedgeadd.entrySet()) {
468       AllocNode node=e.getKey();
469       MySet<Edge> edgestoadd=e.getValue();
470       //If we have not done a subtract, then 
471       if (!graph.nodeMap.containsKey(node)) {
472         //Copy the parent entry
473         graph.nodeMap.put(node, (MySet<Edge>)graph.parent.nodeMap.get(node).clone());
474       }
475       graph.nodeMap.get(node).addAll(edgestoadd);
476     }
477
478     //Remove var edges
479     for(Map.Entry<TempDescriptor, MySet<Edge>> e: delta.varedgeremove.entrySet()) {
480       TempDescriptor tmp=e.getKey();
481       MySet<Edge> edgestoremove=e.getValue();
482
483       if (graph.varMap.containsKey(tmp)) {
484         //Just apply diff to current map
485         graph.varMap.get(tmp).removeAll(edgestoremove);
486       } else {
487         //Generate diff from parent graph
488         MySet<Edge> parentedges=graph.parent.varMap.get(tmp);
489         MySet<Edge> newedgeset=Util.setSubtract(parentedges, edgestoremove);
490         graph.varMap.put(tmp, newedgeset);
491       }
492     }
493
494     //Add var edges
495     for(Map.Entry<TempDescriptor, MySet<Edge>> e: delta.varedgeadd.entrySet()) {
496       TempDescriptor tmp=e.getKey();
497       MySet<Edge> edgestoadd=e.getValue();
498       graph.varMap.put(tmp, (MySet<Edge>) edgestoadd.clone());
499     }
500
501     //Add node additions
502     for(AllocNode node:delta.addNodeAges) {
503       graph.nodeAges.add(node);
504     }
505   }
506
507   Delta processSetFieldElementNode(FlatNode node, Delta delta, Graph graph) {
508     TempDescriptor src;
509     FieldDescriptor fd;
510     TempDescriptor dst;
511     if (node.kind()==FKind.FlatSetElementNode) {
512       FlatSetElementNode fen=(FlatSetElementNode) node;
513       src=fen.getSrc();
514       fd=null;
515       dst=fen.getDst();
516     } else {
517       FlatSetFieldNode ffn=(FlatSetFieldNode) node;
518       src=ffn.getSrc();
519       fd=ffn.getField();
520       dst=ffn.getDst();
521     }
522     if (delta.getInit()) {
523       HashSet<AllocNode> srcNodes=GraphManip.getNodes(graph, delta, src);
524       HashSet<AllocNode> dstNodes=GraphManip.getNodes(graph, delta, dst);
525       MySet<Edge> edgesToAdd=GraphManip.genEdges(srcNodes, fd, dstNodes);
526       MySet<Edge> edgesToRemove=null;
527       if (dstNodes.size()==1&&!dstNodes.iterator().next().isSummary()) {
528         /* Can do a strong update */
529         edgesToRemove=GraphManip.getEdges(graph, delta, dstNodes, fd);
530       }
531       /* Update diff */
532       updateHeapDelta(graph, delta, edgesToAdd, edgesToRemove);
533       applyDiffs(graph, delta);
534     } else {
535       /* First look at new sources */
536       MySet<Edge> edgesToAdd=new MySet<Edge>();
537       HashSet<AllocNode> newSrcNodes=GraphManip.getDiffNodes(delta, src);
538       HashSet<AllocNode> dstNodes=GraphManip.getDiffNodes(delta, dst);
539       edgesToAdd.addAll(GraphManip.genEdges(newSrcNodes, fd, dstNodes));
540       HashSet<AllocNode> newDstNodes=GraphManip.getDiffNodes(delta, dst);
541       MySet<Edge> edgesToRemove=null;
542       if (newDstNodes.size()!=0) {
543         if (dstNodes.size()==1&&!dstNodes.iterator().next().isSummary()) {
544           /* Need to undo strong update */
545           if (graph.strongUpdateSet!=null) {
546             edgesToAdd.addAll(graph.strongUpdateSet);
547             graph.strongUpdateSet.clear();
548           }
549         } else if (dstNodes.size()==0&&newDstNodes.size()==1&&!newDstNodes.iterator().next().isSummary()&&graph.strongUpdateSet==null) {
550           edgesToRemove=GraphManip.getEdges(graph, delta, dstNodes, fd);
551         }
552         HashSet<AllocNode> srcNodes=GraphManip.getDiffNodes(delta, src);
553         edgesToAdd.addAll(GraphManip.genEdges(srcNodes, fd, newDstNodes));
554       }
555       /* Update diff */
556       updateHeapDelta(graph, delta, edgesToAdd, edgesToRemove);
557       applyDiffs(graph, delta);
558     }
559     return delta;
560   }
561
562   Delta processCopyNode(FlatNode node, Delta delta, Graph graph) {
563     TempDescriptor src;
564     TempDescriptor dst;
565     if (node.kind()==FKind.FlatOpNode) {
566       FlatOpNode fon=(FlatOpNode) node;
567       src=fon.getLeft();
568       dst=fon.getDest();
569     } else if (node.kind()==FKind.FlatReturnNode) {
570       FlatReturnNode frn=(FlatReturnNode)node;
571       src=frn.getReturnTemp();
572       dst=returntmp;
573     } else {
574       FlatCastNode fcn=(FlatCastNode) node;
575       src=fcn.getSrc();
576       dst=fcn.getDst();
577     }
578     if (delta.getInit()) {
579       HashSet<AllocNode> srcnodes=GraphManip.getNodes(graph, delta, src);
580       MySet<Edge> edgesToAdd=GraphManip.genEdges(src, srcnodes);
581       MySet<Edge> edgesToRemove=GraphManip.getEdges(graph, delta, dst);
582       updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
583       applyDiffs(graph, delta);
584     } else {
585       /* First compute new src nodes */
586       HashSet<AllocNode> newSrcNodes=GraphManip.getDiffNodes(delta, src);
587
588       /* Compute the union, and then the set of edges */
589       MySet<Edge> edgesToAdd=GraphManip.genEdges(src, newSrcNodes);
590       
591       /* Compute set of edges to remove */
592       MySet<Edge> edgesToRemove=GraphManip.getDiffEdges(delta, dst);      
593
594       /* Update diff */
595       updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
596       applyDiffs(graph, delta);
597     }
598     return delta;
599   }
600
601   Delta processFieldElementNode(FlatNode node, Delta delta, Graph graph) {
602     TempDescriptor src;
603     FieldDescriptor fd;
604     TempDescriptor dst;
605     if (node.kind()==FKind.FlatElementNode) {
606       FlatElementNode fen=(FlatElementNode) node;
607       src=fen.getSrc();
608       fd=null;
609       dst=fen.getDst();
610     } else {
611       FlatFieldNode ffn=(FlatFieldNode) node;
612       src=ffn.getSrc();
613       fd=ffn.getField();
614       dst=ffn.getDst();
615     }
616     if (delta.getInit()) {
617       HashSet<AllocNode> srcnodes=GraphManip.getNodes(graph, delta, src);
618       HashSet<AllocNode> fdnodes=GraphManip.getNodes(graph, delta, srcnodes, fd);
619       MySet<Edge> edgesToAdd=GraphManip.genEdges(src, fdnodes);
620       MySet<Edge> edgesToRemove=GraphManip.getEdges(graph, delta, dst);
621       updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
622       applyDiffs(graph, delta);
623     } else {
624       /* First compute new objects we read fields of */
625       HashSet<AllocNode> allsrcnodes=GraphManip.getNodes(graph, delta, src);
626       HashSet<AllocNode> difffdnodes=GraphManip.getDiffNodes(delta, allsrcnodes, fd);     
627       /* Next compute new targets of fields */
628       HashSet<AllocNode> newsrcnodes=GraphManip.getDiffNodes(delta, src);
629       HashSet<AllocNode> newfdnodes=GraphManip.getNodes(graph, delta, newsrcnodes, fd);
630       /* Compute the union, and then the set of edges */
631       HashSet<AllocNode> newTargets=new HashSet<AllocNode>();
632       newTargets.addAll(newfdnodes);
633       newTargets.addAll(difffdnodes);
634       MySet<Edge> edgesToAdd=GraphManip.genEdges(src, newTargets);      
635       
636       /* Compute set of edges to remove */
637       MySet<Edge> edgesToRemove=GraphManip.getDiffEdges(delta, dst);      
638
639       /* Update diff */
640       updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
641       applyDiffs(graph, delta);
642     }
643     return delta;
644   }
645
646   static void updateVarDelta(Graph graph, Delta delta, TempDescriptor tmp, MySet<Edge> edgestoAdd, MySet<Edge> edgestoRemove) {
647     MySet<Edge> edgeAdd=delta.varedgeadd.get(tmp);
648     MySet<Edge> edgeRemove=delta.varedgeremove.get(tmp);
649     MySet<Edge> existingEdges=graph.getEdges(tmp);
650     for(Edge e: edgestoRemove) {
651       //remove edge from delta
652       edgeAdd.remove(e);
653       //if the edge is already in the graph, add an explicit remove to the delta
654       if (existingEdges.contains(e))
655         edgeRemove.add(e);
656     }
657     for(Edge e: edgestoAdd) {
658       //Remove the edge from the remove set
659       edgeRemove.remove(e);
660       //Explicitly add it to the add set unless it is already in the graph
661       if (!existingEdges.contains(e))
662         edgeAdd.add(e);
663     }
664   }
665
666   static void updateHeapDelta(Graph graph, Delta delta, MySet<Edge> edgestoAdd, MySet<Edge> edgestoRemove) {
667     for(Edge e: edgestoRemove) {
668       AllocNode src=e.src;
669       MySet<Edge> edgeAdd=delta.heapedgeadd.get(src);
670       MySet<Edge> existingEdges=graph.getEdges(src);
671       //remove edge from delta
672       edgeAdd.remove(e);
673       //if the edge is already in the graph, add an explicit remove to the delta
674       if (existingEdges.contains(e)) {
675         MySet<Edge> edgeRemove=delta.heapedgeremove.get(src);
676         edgeRemove.add(e);
677       }
678     }
679     for(Edge e: edgestoAdd) {
680       AllocNode src=e.src;
681       MySet<Edge> edgeRemove=delta.heapedgeremove.get(src);
682       MySet<Edge> existingEdges=graph.getEdges(src);
683       //Remove the edge from the remove set
684       edgeRemove.remove(e);
685       //Explicitly add it to the add set unless it is already in the graph
686       if (!existingEdges.contains(e)) {
687         MySet<Edge> edgeAdd=delta.heapedgeadd.get(src);
688         edgeAdd.add(e);
689       }
690     }
691   }
692
693   Delta processFlatNop(FlatNode node, Delta delta, Graph graph) {
694     applyDiffs(graph, delta);
695     return delta;
696   }
697
698   Delta processNewNode(FlatNew node, Delta delta, Graph graph) {
699     AllocNode summary=allocFactory.getAllocNode(node, true);
700     AllocNode single=allocFactory.getAllocNode(node, false);
701     TempDescriptor tmp=node.getDst();
702
703     if (delta.getInit()) {
704       /* We don't have to deal with summarization here...  The
705        * intuition is that this is the only place where we generate
706        * nodes for this allocation site and this is the first time
707        * we've analyzed this site */
708
709       //Build new Edge
710       Edge e=new Edge(tmp, single);
711       //Build new Edge set
712       MySet<Edge> newedges=new MySet<Edge>();
713       newedges.add(e);
714       //Add it into the diffs
715       delta.varedgeadd.put(tmp, newedges);
716       //Remove the old edges
717       delta.varedgeremove.put(tmp, graph.getEdges(tmp));
718       //Apply incoming diffs to graph
719       applyDiffs(graph, delta);
720       //Note that we create a single node
721       delta.addNodeAges.add(single);
722     } else {
723       /* 1. Fix up the variable edge additions */
724
725       for(Iterator<Map.Entry<TempDescriptor, MySet<Edge>>> entryIt=delta.varedgeadd.entrySet().iterator();entryIt.hasNext();) {
726         Map.Entry<TempDescriptor, MySet<Edge>> entry=entryIt.next();
727
728         if (entry.getKey()==tmp) {
729           /* Check if this is the tmp we overwrite */
730           entryIt.remove();
731         } else {
732           /* Otherwise, check if the target of the edge is changed... */
733           summarizeSet(entry.getValue(), graph.varMap.get(entry.getKey()), single, summary);
734         }
735       }
736       
737       /* 2. Fix up the base variable edges */
738
739       for(Iterator<Map.Entry<TempDescriptor, MySet<Edge>>> entryIt=delta.basevaredge.entrySet().iterator();entryIt.hasNext();) {
740         Map.Entry<TempDescriptor, MySet<Edge>> entry=entryIt.next();
741         TempDescriptor entrytmp=entry.getKey();
742         if (entrytmp==tmp) {
743           /* Check is this is the tmp we overwrite, if so add to remove set */
744           Util.relationUpdate(delta.varedgeremove, tmp, null, entry.getValue());
745         } else {
746           /* Check if the target of the edge is changed */ 
747           MySet<Edge> newset=(MySet<Edge>)entry.getValue().clone();
748           MySet<Edge> removeset=shrinkSet(newset, graph.varMap.get(entrytmp), single, summary);
749           Util.relationUpdate(delta.varedgeremove, entrytmp, newset, removeset);
750           Util.relationUpdate(delta.varedgeadd, entrytmp, null, newset);
751         }
752       }
753
754
755       /* 3. Fix up heap edge additions */
756
757       HashMap<AllocNode, MySet<Edge>> addheapedge=new HashMap<AllocNode, MySet<Edge>>();
758       for(Iterator<Map.Entry<AllocNode, MySet<Edge>>> entryIt=delta.heapedgeadd.entrySet().iterator();entryIt.hasNext();) {
759         Map.Entry<AllocNode, MySet<Edge>> entry=entryIt.next();
760         MySet<Edge> edgeset=entry.getValue();
761         AllocNode allocnode=entry.getKey();
762         if (allocnode==single) {
763           entryIt.remove();
764           summarizeSet(edgeset, graph.nodeMap.get(summary), single, summary);
765           addheapedge.put(summary, edgeset);
766         } else {
767           summarizeSet(edgeset, graph.nodeMap.get(allocnode), single, summary);
768         }
769       }
770       
771       /* Merge in diffs */
772
773       for(Map.Entry<AllocNode, MySet<Edge>> entry:addheapedge.entrySet()) {
774         AllocNode allocnode=entry.getKey();
775         Util.relationUpdate(delta.heapedgeadd, allocnode, null, entry.getValue());
776       }
777
778       /* 4. Fix up the base heap edges */
779
780       for(Iterator<Map.Entry<AllocNode, MySet<Edge>>> entryIt=delta.baseheapedge.entrySet().iterator();entryIt.hasNext();) {
781         Map.Entry<AllocNode, MySet<Edge>> entry=entryIt.next();
782         MySet<Edge> edgeset=entry.getValue();
783         AllocNode allocnode=entry.getKey();
784         if (allocnode==single) {
785           entryIt.remove();
786         }
787         AllocNode addnode=(allocnode==single)?summary:allocnode;
788
789         MySet<Edge> newset=(MySet<Edge>) edgeset.clone();
790         MySet<Edge> removeset=shrinkSet(newset, graph.nodeMap.get(addnode), single, summary);
791         Util.relationUpdate(delta.heapedgeadd, addnode, null, newset);
792         Util.relationUpdate(delta.heapedgeremove, allocnode, null, removeset);
793       }
794
795       /* Update Node Ages...If the base or addNodeAges set contains a
796        * single node, it now should also contain a summary node...  No
797        * need to generate a single node as that has already been
798        * done. */
799       if (delta.baseNodeAges.contains(single)||delta.addNodeAges.contains(single)) {
800         delta.addNodeAges.add(summary);
801       }
802
803       //Apply incoming diffs to graph
804       applyDiffs(graph, delta);      
805     }
806     return delta;
807   }
808
809   /* This function builds a new edge set where oldnode is summarized into new node */
810
811   void summarizeSet(MySet<Edge> edgeset, MySet<Edge> oldedgeset, AllocNode oldnode, AllocNode sumnode) {
812     MySet<Edge> newSet=null;
813     for(Iterator<Edge> edgeit=edgeset.iterator();edgeit.hasNext();) {
814       Edge e=edgeit.next();
815       if (e.dst==oldnode||e.src==oldnode) {
816         if (newSet==null) {
817           newSet=new MySet<Edge>();
818         }
819         edgeit.remove();
820         e=e.copy();
821
822         if (e.dst==oldnode) {
823           e.dst=sumnode;
824         }
825         if (e.src==oldnode) {
826           e.src=sumnode;
827         }
828         if (oldedgeset==null||!oldedgeset.contains(e))
829           newSet.add(e);
830       }
831     }
832     if (newSet!=null)
833       edgeset.addAll(newSet);
834   }
835
836   /* Shrinks the incoming set to just include rewritten values.
837    * Returns a set of the original rewritten values */
838
839   MySet<Edge> shrinkSet(MySet<Edge> edgeset, MySet<Edge> oldedgeset, AllocNode oldnode, AllocNode newnode) {
840     MySet<Edge> newSet=null;
841     MySet<Edge> removeSet=null;
842     for(Iterator<Edge> edgeit=edgeset.iterator();edgeit.hasNext();) {
843       Edge e=edgeit.next();
844       edgeit.remove();
845       if (e.dst==oldnode||e.src==oldnode) {
846         if (newSet==null) {
847           newSet=new MySet<Edge>();
848           removeSet=new MySet<Edge>();
849         }
850
851         removeSet.add(e.copy());
852         if (e.dst==oldnode)
853           e.dst=newnode;
854         if (e.src==oldnode)
855           e.src=newnode;
856         if (oldedgeset==null||!oldedgeset.contains(e))
857           newSet.add(e);
858       }
859     }
860     if (newSet!=null)
861       edgeset.addAll(newSet);
862     return removeSet;
863   } 
864
865   /* This function returns a completely new Delta...  It is safe to
866    * modify this */
867
868   Delta applyInitDelta(Delta delta, BBlock block) {
869     //Apply delta to graph
870     boolean newGraph=false;
871     if (!bbgraphMap.containsKey(block)) {
872       bbgraphMap.put(block, new Graph(null));
873       newGraph=true;
874     }
875     Delta newdelta;
876     Graph graph=bbgraphMap.get(block);
877
878     if (newGraph) {
879       newdelta=new Delta(null, true);
880       //Add in heap edges and throw away original diff
881       graph.nodeMap.putAll(delta.heapedgeadd);
882       //Add in var edges and throw away original diff
883       graph.varMap.putAll(delta.varedgeadd);
884       //Record that this is initial set...
885       graph.nodeAges.addAll(delta.addNodeAges);
886     } else {
887       newdelta=new Delta(null, false);
888       //merge in heap edges and variables
889       mergeHeapEdges(graph, delta, newdelta);
890       mergeVarEdges(graph, delta, newdelta);
891       mergeAges(graph, delta, newdelta);
892     }
893     return newdelta;
894   }
895
896   /* This function merges in the heap edges.  It updates delta to be
897    * the difference */
898
899   void mergeHeapEdges(Graph graph, Delta delta, Delta newdelta) {
900     //Merge in edges
901     for(Map.Entry<AllocNode, MySet<Edge>> heapedge:delta.heapedgeadd.entrySet()) {
902       AllocNode nsrc=heapedge.getKey();
903       MySet<Edge> edges=heapedge.getValue();
904       if (!graph.nodeMap.containsKey(nsrc)) {
905         graph.nodeMap.put(nsrc, new MySet<Edge>());
906       }
907       MySet<Edge> dstedges=graph.nodeMap.get(nsrc);
908       MySet<Edge> diffedges=new MySet<Edge>();
909       for(Edge e:edges) {
910         if (!dstedges.contains(e)) {
911           //We have a new edge
912           diffedges.add(e);
913           dstedges.add(e);
914         }
915       }
916       //Done with edge set...
917       if (diffedges.size()>0) {
918         //completely new
919         newdelta.baseheapedge.put(nsrc, diffedges);
920       }
921     }
922   }
923
924   /* This function merges in the var edges.  It updates delta to be
925    * the difference */
926
927   void mergeVarEdges(Graph graph, Delta delta, Delta newdelta) {
928     //Merge in edges
929     for(Map.Entry<TempDescriptor, MySet<Edge>> varedge:delta.varedgeadd.entrySet()) {
930       TempDescriptor tmpsrc=varedge.getKey();
931       MySet<Edge> edges=varedge.getValue();
932       if (!graph.varMap.containsKey(tmpsrc)) {
933         graph.varMap.put(tmpsrc, new MySet<Edge>());
934       }
935       MySet<Edge> dstedges=graph.varMap.get(tmpsrc);
936       MySet<Edge> diffedges=new MySet<Edge>();
937       for(Edge e:edges) {
938         if (!dstedges.contains(e)) {
939           //We have a new edge
940           diffedges.add(e);
941           dstedges.add(e);
942         }
943       }
944       //Done with edge set...
945       if (diffedges.size()>=0) {
946         //completely new
947         newdelta.basevaredge.put(tmpsrc,diffedges);
948       }
949     }
950   }
951
952   void mergeAges(Graph graph, Delta delta, Delta newDelta) {
953     //Merge in edges
954     for(AllocNode node:delta.addNodeAges) {
955       if (!graph.nodeAges.contains(node)) {
956         graph.nodeAges.add(node);
957         newDelta.baseNodeAges.add(node);
958       }
959     }
960   }
961 }