changes towards making this work
[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   Delta processFlatCall(BBlock callblock, int callindex, FlatCall fcall, Delta delta, Graph graph) {
215     Delta newDelta=new Delta(null, false);
216
217     if (delta.getInit()) {
218       MySet<Edge> edgeset=new MySet<Edge>();
219       HashSet<AllocNode> nodeset=new HashSet<AllocNode>();
220       HashSet<ClassDescriptor> targetSet=new HashSet<ClassDescriptor>();
221       Stack<AllocNode> tovisit=new Stack<AllocNode>();
222       TempDescriptor tmpthis=fcall.getThis();
223
224       //Handle the this temp
225       if (tmpthis!=null) {
226         MySet<Edge> edges=GraphManip.getEdges(graph, delta, tmpthis);
227         newDelta.varedgeadd.put(tmpthis, (MySet<Edge>) edges.clone());
228         edgeset.addAll(edges);
229         for(Edge e:edges) {
230           AllocNode dstnode=e.dst;
231           if (!nodeset.contains(dstnode)) {
232             TypeDescriptor type=dstnode.getType();
233             if (!type.isArray()) {
234               targetSet.add(type.getClassDesc());
235             } else {
236               //arrays don't have code
237               targetSet.add(typeUtil.getClass(TypeUtil.ObjectClass));
238             }
239             nodeset.add(dstnode);
240             tovisit.add(dstnode);
241           }
242         }
243       }
244
245       //Go through each temp
246       for(int i=0;i<fcall.numArgs();i++) {
247         TempDescriptor tmp=fcall.getArg(i);
248         MySet<Edge> edges=GraphManip.getEdges(graph, delta, tmp);
249         newDelta.varedgeadd.put(tmp, (MySet<Edge>) edges.clone());
250         edgeset.addAll(edges);
251         for(Edge e:edges) {
252           if (!nodeset.contains(e.dst)) {
253             nodeset.add(e.dst);
254             tovisit.add(e.dst);
255           }
256         }
257       }
258       
259       //Traverse all reachable nodes
260       while(!tovisit.isEmpty()) {
261         AllocNode node=tovisit.pop();
262         MySet<Edge> edges=GraphManip.getEdges(graph, delta, node);
263         newDelta.heapedgeadd.put(node, edges);
264         edgeset.addAll(edges);
265         for(Edge e:edges) {
266           if (!nodeset.contains(e.dst)) {
267             nodeset.add(e.dst);
268             tovisit.add(e.dst);
269           }
270         }
271       }
272
273       //Compute call targets
274       MethodDescriptor md=fcall.getMethod();
275       HashSet<MethodDescriptor> targets=new HashSet<MethodDescriptor>();
276       if (md.isStatic()) {
277         targets.add(md);
278       } else {
279         //Compute Edges
280         for(Edge e:newDelta.varedgeadd.get(tmpthis)) {
281           AllocNode node=e.dst;
282           ClassDescriptor cd=node.getType().getClassDesc();
283           //Figure out exact method called and add to set
284           targets.add(cd.getCalledMethod(md));
285         }
286       }
287
288
289       //Fix mapping
290       for(MethodDescriptor calledmd:targets) {
291         FlatMethod fm=state.getMethodFlat(calledmd);
292
293         //Build tmpMap
294         HashMap<TempDescriptor, TempDescriptor> tmpMap=new HashMap<TempDescriptor, TempDescriptor>();
295         int offset=0;
296         if(tmpthis!=null) {
297           tmpMap.put(tmpthis, fm.getParameter(offset++));
298         }
299         for(int i=0;i<fcall.numArgs();i++) {
300           TempDescriptor tmp=fcall.getArg(i);
301           tmpMap.put(tmp,fm.getParameter(i+offset));
302         }
303
304         //Get basicblock for the method
305         BasicBlock block=getBBlock(fm);
306
307         //Build and enqueue delta
308         Delta d=newDelta.changeParams(tmpMap, new PPoint(block.getStart()));
309         toprocess.add(d);
310
311         //Hook up exits
312         if (!callMap.containsKey(fcall)) {
313           callMap.put(fcall, new HashSet<BBlock>());
314         }
315         callMap.get(fcall).add(block.getStart());
316         
317         //If we have an existing exit, build delta
318         Delta returnDelta=null;
319
320         //Hook up return
321         if (!returnMap.containsKey(block.getExit())) {
322           returnMap.put(block.getExit(), new HashSet<PPoint>());
323         }
324         returnMap.get(block.getExit()).add(new PPoint(callblock, callindex));
325         
326         if (bbgraphMap.containsKey(block.getExit())) {
327           //Need to push existing results to current node
328           if (returnDelta==null) {
329             returnDelta=new Delta(null, false);
330             buildInitDelta(bbgraphMap.get(block.getExit()), returnDelta);
331             if (!returnDelta.heapedgeadd.isEmpty()||!returnDelta.heapedgeremove.isEmpty()||!returnDelta.varedgeadd.isEmpty()) {
332               returnDelta.setBlock(new PPoint(callblock, callindex));
333               toprocess.add(returnDelta);
334             }
335           } else {
336             if (!returnDelta.heapedgeadd.isEmpty()||!returnDelta.heapedgeremove.isEmpty()||!returnDelta.varedgeadd.isEmpty()) {
337               toprocess.add(returnDelta.diffBlock(new PPoint(callblock, callindex)));
338             }
339           }
340         }
341       }
342       graph.reachNode=nodeset;
343       graph.reachEdge=edgeset;
344       
345       //Apply diffs to graph
346       applyDiffs(graph, delta);
347     } else {
348       MySet<Edge> edgeset=new MySet<Edge>();
349       HashSet<AllocNode> nodeset=new HashSet<AllocNode>();
350       MySet<Edge> oldedgeset=graph.reachEdge;
351       HashSet<AllocNode> oldnodeset=graph.reachNode;
352
353       HashSet<ClassDescriptor> targetSet=new HashSet<ClassDescriptor>();
354       Stack<AllocNode> tovisit=new Stack<AllocNode>();
355       TempDescriptor tmpthis=fcall.getThis();
356
357       //Handle the this temp
358       if (tmpthis!=null) {
359         MySet<Edge> edges=GraphManip.getDiffEdges(delta, tmpthis);
360         newDelta.varedgeadd.put(tmpthis, (MySet<Edge>) edges.clone());
361         edgeset.addAll(edges);
362         for(Edge e:edges) {
363           AllocNode dstnode=e.dst;
364           if (!nodeset.contains(dstnode)&&!oldnodeset.contains(dstnode)) {
365             TypeDescriptor type=dstnode.getType();
366             if (!type.isArray()) {
367               targetSet.add(type.getClassDesc());
368             } else {
369               //arrays don't have code
370               targetSet.add(typeUtil.getClass(TypeUtil.ObjectClass));
371             }
372             nodeset.add(dstnode);
373             tovisit.add(dstnode);
374           }
375         }
376       }
377
378       //Go through each temp
379       for(int i=0;i<fcall.numArgs();i++) {
380         TempDescriptor tmp=fcall.getArg(i);
381         MySet<Edge> edges=GraphManip.getDiffEdges(delta, tmp);
382         newDelta.varedgeadd.put(tmp, (MySet<Edge>) edges.clone());
383         edgeset.addAll(edges);
384         for(Edge e:edges) {
385           if (!nodeset.contains(e.dst)&&!oldnodeset.contains(e.dst)) {
386             nodeset.add(e.dst);
387             tovisit.add(e.dst);
388           }
389         }
390       }
391
392       //Go through each new heap edge that starts from old node
393       MySet<Edge> newedges=GraphManip.getDiffEdges(delta, oldnodeset);
394       edgeset.addAll(newedges);
395       for(Edge e:newedges) {
396         if (!nodeset.contains(e.dst)&&!oldnodeset.contains(e.dst)) {
397           nodeset.add(e.dst);
398           tovisit.add(e.dst);
399         }
400       }
401       
402       //Traverse all reachable nodes
403       while(!tovisit.isEmpty()) {
404         AllocNode node=tovisit.pop();
405         MySet<Edge> edges=GraphManip.getEdges(graph, delta, node);
406
407         newDelta.heapedgeadd.put(node, edges);
408         edgeset.addAll(edges);
409         for(Edge e:edges) {
410           if (!nodeset.contains(e.dst)&&!oldnodeset.contains(e.dst)) {
411             nodeset.add(e.dst);
412             tovisit.add(e.dst);
413           }
414         }
415       }
416
417       //Compute call targets
418       MethodDescriptor md=fcall.getMethod();
419       HashSet<MethodDescriptor> targets=new HashSet<MethodDescriptor>();
420       if (md.isStatic()) {
421         targets.add(md);
422       } else {
423         //Compute Edges
424         for(Edge e:newDelta.varedgeadd.get(tmpthis)) {
425           AllocNode node=e.dst;
426           ClassDescriptor cd=node.getType().getClassDesc();
427           //Figure out exact method called and add to set
428           targets.add(cd.getCalledMethod(md));
429         }
430       }
431
432       //add in new nodeset and edgeset
433       oldnodeset.addAll(nodeset);
434       oldedgeset.addAll(edgeset);
435       Delta basedelta=null;
436
437       //Fix mapping
438       for(MethodDescriptor calledmd:targets) {
439         FlatMethod fm=state.getMethodFlat(calledmd);
440         boolean newmethod=false;
441
442         //Get basicblock for the method
443         BasicBlock block=getBBlock(fm);
444
445         //Hook up exits
446         if (!callMap.containsKey(fcall)) {
447           callMap.put(fcall, new HashSet<BBlock>());
448         }
449
450         Delta returnDelta=null;
451
452         if (!callMap.get(fcall).contains(block.getStart())) {
453           callMap.get(fcall).add(block.getStart());
454           newmethod=true;
455
456           //Hook up exits
457           if (!returnMap.containsKey(block.getExit())) {
458             returnMap.put(block.getExit(), new HashSet<PPoint>());
459           }
460           returnMap.get(block.getExit()).add(new PPoint(callblock, callindex));
461           
462           if (bbgraphMap.containsKey(block.getExit())) {
463             //Need to push existing results to current node
464             if (returnDelta==null) {
465               returnDelta=new Delta(null, false);
466               buildInitDelta(bbgraphMap.get(block.getExit()), returnDelta);
467               if (!returnDelta.heapedgeadd.isEmpty()||!returnDelta.heapedgeremove.isEmpty()||!returnDelta.varedgeadd.isEmpty()) {
468                 returnDelta.setBlock(new PPoint(callblock, callindex));
469                 toprocess.add(returnDelta);
470               }
471             } else {
472               if (!returnDelta.heapedgeadd.isEmpty()||!returnDelta.heapedgeremove.isEmpty()||!returnDelta.varedgeadd.isEmpty()) {
473                 toprocess.add(returnDelta.diffBlock(new PPoint(callblock, callindex)));
474               }
475             }
476           }
477         }
478         
479         //Build tmpMap
480         HashMap<TempDescriptor, TempDescriptor> tmpMap=new HashMap<TempDescriptor, TempDescriptor>();
481         int offset=0;
482         if(tmpthis!=null) {
483           tmpMap.put(tmpthis, fm.getParameter(offset++));
484         }
485         for(int i=0;i<fcall.numArgs();i++) {
486           TempDescriptor tmp=fcall.getArg(i);
487           tmpMap.put(tmp,fm.getParameter(i+offset));
488         }
489
490         if (newmethod) {
491           if (basedelta==null) {
492             basedelta=newDelta.buildBase(oldedgeset);
493           }
494           //Build and enqueue delta
495           Delta d=basedelta.changeParams(tmpMap, new PPoint(block.getStart()));
496           toprocess.add(d);
497         } else  {
498           //Build and enqueue delta
499           Delta d=newDelta.changeParams(tmpMap, new PPoint(block.getStart()));
500           toprocess.add(d);
501         }
502       }
503
504       //Apply diffs to graph
505       applyDiffs(graph, delta);
506     }
507     return newDelta;
508   }
509
510   void applyDiffs(Graph graph, Delta delta) {
511     //Add hidden base edges
512     for(Map.Entry<AllocNode, MySet<Edge>> e: delta.baseheapedge.entrySet()) {
513       AllocNode node=e.getKey();
514       MySet<Edge> edges=e.getValue();
515       if (graph.nodeMap.containsKey(node)) {
516         MySet<Edge> nodeEdges=graph.nodeMap.get(node);
517         nodeEdges.addAll(edges);
518       }
519     }
520
521     //Remove heap edges
522     for(Map.Entry<AllocNode, MySet<Edge>> e: delta.heapedgeremove.entrySet()) {
523       AllocNode node=e.getKey();
524       MySet<Edge> edgestoremove=e.getValue();
525       if (graph.nodeMap.containsKey(node)) {
526         //Just apply diff to current map
527         graph.nodeMap.get(node).removeAll(edgestoremove);
528       } else {
529         //Generate diff from parent graph
530         MySet<Edge> parentedges=graph.parent.nodeMap.get(node);
531         MySet<Edge> newedgeset=Util.setSubtract(parentedges, edgestoremove);
532         graph.nodeMap.put(node, newedgeset);
533       }
534     }
535
536     //Add heap edges
537     for(Map.Entry<AllocNode, MySet<Edge>> e: delta.heapedgeadd.entrySet()) {
538       AllocNode node=e.getKey();
539       MySet<Edge> edgestoadd=e.getValue();
540       //If we have not done a subtract, then 
541       if (!graph.nodeMap.containsKey(node)) {
542         //Copy the parent entry
543         graph.nodeMap.put(node, (MySet<Edge>)graph.parent.nodeMap.get(node).clone());
544       }
545       graph.nodeMap.get(node).addAll(edgestoadd);
546     }
547
548     //Remove var edges
549     for(Map.Entry<TempDescriptor, MySet<Edge>> e: delta.varedgeremove.entrySet()) {
550       TempDescriptor tmp=e.getKey();
551       MySet<Edge> edgestoremove=e.getValue();
552
553       if (graph.varMap.containsKey(tmp)) {
554         //Just apply diff to current map
555         graph.varMap.get(tmp).removeAll(edgestoremove);
556       } else {
557         //Generate diff from parent graph
558         MySet<Edge> parentedges=graph.parent.varMap.get(tmp);
559         MySet<Edge> newedgeset=Util.setSubtract(parentedges, edgestoremove);
560         graph.varMap.put(tmp, newedgeset);
561       }
562     }
563
564     //Add var edges
565     for(Map.Entry<TempDescriptor, MySet<Edge>> e: delta.varedgeadd.entrySet()) {
566       TempDescriptor tmp=e.getKey();
567       MySet<Edge> edgestoadd=e.getValue();
568       graph.varMap.put(tmp, (MySet<Edge>) edgestoadd.clone());
569     }
570
571     //Add node additions
572     for(AllocNode node:delta.addNodeAges) {
573       graph.nodeAges.add(node);
574     }
575   }
576
577   Delta processSetFieldElementNode(FlatNode node, Delta delta, Graph graph) {
578     TempDescriptor src;
579     FieldDescriptor fd;
580     TempDescriptor dst;
581     if (node.kind()==FKind.FlatSetElementNode) {
582       FlatSetElementNode fen=(FlatSetElementNode) node;
583       src=fen.getSrc();
584       fd=null;
585       dst=fen.getDst();
586     } else {
587       FlatSetFieldNode ffn=(FlatSetFieldNode) node;
588       src=ffn.getSrc();
589       fd=ffn.getField();
590       dst=ffn.getDst();
591     }
592     if (delta.getInit()) {
593       HashSet<AllocNode> srcNodes=GraphManip.getNodes(graph, delta, src);
594       HashSet<AllocNode> dstNodes=GraphManip.getNodes(graph, delta, dst);
595       MySet<Edge> edgesToAdd=GraphManip.genEdges(srcNodes, fd, dstNodes);
596       MySet<Edge> edgesToRemove=null;
597       if (dstNodes.size()==1&&!dstNodes.iterator().next().isSummary()) {
598         /* Can do a strong update */
599         edgesToRemove=GraphManip.getEdges(graph, delta, dstNodes, fd);
600       }
601       /* Update diff */
602       updateHeapDelta(graph, delta, edgesToAdd, edgesToRemove);
603       applyDiffs(graph, delta);
604     } else {
605       /* First look at new sources */
606       MySet<Edge> edgesToAdd=new MySet<Edge>();
607       HashSet<AllocNode> newSrcNodes=GraphManip.getDiffNodes(delta, src);
608       HashSet<AllocNode> dstNodes=GraphManip.getDiffNodes(delta, dst);
609       edgesToAdd.addAll(GraphManip.genEdges(newSrcNodes, fd, dstNodes));
610       HashSet<AllocNode> newDstNodes=GraphManip.getDiffNodes(delta, dst);
611       MySet<Edge> edgesToRemove=null;
612       if (newDstNodes.size()!=0) {
613         if (dstNodes.size()==1&&!dstNodes.iterator().next().isSummary()) {
614           /* Need to undo strong update */
615           if (graph.strongUpdateSet!=null) {
616             edgesToAdd.addAll(graph.strongUpdateSet);
617             graph.strongUpdateSet.clear();
618           }
619         } else if (dstNodes.size()==0&&newDstNodes.size()==1&&!newDstNodes.iterator().next().isSummary()&&graph.strongUpdateSet==null) {
620           edgesToRemove=GraphManip.getEdges(graph, delta, dstNodes, fd);
621         }
622         HashSet<AllocNode> srcNodes=GraphManip.getDiffNodes(delta, src);
623         edgesToAdd.addAll(GraphManip.genEdges(srcNodes, fd, newDstNodes));
624       }
625       /* Update diff */
626       updateHeapDelta(graph, delta, edgesToAdd, edgesToRemove);
627       applyDiffs(graph, delta);
628     }
629     return delta;
630   }
631
632   Delta processCopyNode(FlatNode node, Delta delta, Graph graph) {
633     TempDescriptor src;
634     TempDescriptor dst;
635     if (node.kind()==FKind.FlatOpNode) {
636       FlatOpNode fon=(FlatOpNode) node;
637       src=fon.getLeft();
638       dst=fon.getDest();
639     } else if (node.kind()==FKind.FlatReturnNode) {
640       FlatReturnNode frn=(FlatReturnNode)node;
641       src=frn.getReturnTemp();
642       dst=returntmp;
643     } else {
644       FlatCastNode fcn=(FlatCastNode) node;
645       src=fcn.getSrc();
646       dst=fcn.getDst();
647     }
648     if (delta.getInit()) {
649       HashSet<AllocNode> srcnodes=GraphManip.getNodes(graph, delta, src);
650       MySet<Edge> edgesToAdd=GraphManip.genEdges(src, srcnodes);
651       MySet<Edge> edgesToRemove=GraphManip.getEdges(graph, delta, dst);
652       updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
653       applyDiffs(graph, delta);
654     } else {
655       /* First compute new src nodes */
656       HashSet<AllocNode> newSrcNodes=GraphManip.getDiffNodes(delta, src);
657
658       /* Compute the union, and then the set of edges */
659       MySet<Edge> edgesToAdd=GraphManip.genEdges(src, newSrcNodes);
660       
661       /* Compute set of edges to remove */
662       MySet<Edge> edgesToRemove=GraphManip.getDiffEdges(delta, dst);      
663
664       /* Update diff */
665       updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
666       applyDiffs(graph, delta);
667     }
668     return delta;
669   }
670
671   Delta processFieldElementNode(FlatNode node, Delta delta, Graph graph) {
672     TempDescriptor src;
673     FieldDescriptor fd;
674     TempDescriptor dst;
675     if (node.kind()==FKind.FlatElementNode) {
676       FlatElementNode fen=(FlatElementNode) node;
677       src=fen.getSrc();
678       fd=null;
679       dst=fen.getDst();
680     } else {
681       FlatFieldNode ffn=(FlatFieldNode) node;
682       src=ffn.getSrc();
683       fd=ffn.getField();
684       dst=ffn.getDst();
685     }
686     if (delta.getInit()) {
687       HashSet<AllocNode> srcnodes=GraphManip.getNodes(graph, delta, src);
688       HashSet<AllocNode> fdnodes=GraphManip.getNodes(graph, delta, srcnodes, fd);
689       MySet<Edge> edgesToAdd=GraphManip.genEdges(src, fdnodes);
690       MySet<Edge> edgesToRemove=GraphManip.getEdges(graph, delta, dst);
691       updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
692       applyDiffs(graph, delta);
693     } else {
694       /* First compute new objects we read fields of */
695       HashSet<AllocNode> allsrcnodes=GraphManip.getNodes(graph, delta, src);
696       HashSet<AllocNode> difffdnodes=GraphManip.getDiffNodes(delta, allsrcnodes, fd);     
697       /* Next compute new targets of fields */
698       HashSet<AllocNode> newsrcnodes=GraphManip.getDiffNodes(delta, src);
699       HashSet<AllocNode> newfdnodes=GraphManip.getNodes(graph, delta, newsrcnodes, fd);
700       /* Compute the union, and then the set of edges */
701       HashSet<AllocNode> newTargets=new HashSet<AllocNode>();
702       newTargets.addAll(newfdnodes);
703       newTargets.addAll(difffdnodes);
704       MySet<Edge> edgesToAdd=GraphManip.genEdges(src, newTargets);      
705       
706       /* Compute set of edges to remove */
707       MySet<Edge> edgesToRemove=GraphManip.getDiffEdges(delta, dst);      
708
709       /* Update diff */
710       updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
711       applyDiffs(graph, delta);
712     }
713     return delta;
714   }
715
716   static void updateVarDelta(Graph graph, Delta delta, TempDescriptor tmp, MySet<Edge> edgestoAdd, MySet<Edge> edgestoRemove) {
717     MySet<Edge> edgeAdd=delta.varedgeadd.get(tmp);
718     MySet<Edge> edgeRemove=delta.varedgeremove.get(tmp);
719     MySet<Edge> existingEdges=graph.getEdges(tmp);
720     for(Edge e: edgestoRemove) {
721       //remove edge from delta
722       edgeAdd.remove(e);
723       //if the edge is already in the graph, add an explicit remove to the delta
724       if (existingEdges.contains(e))
725         edgeRemove.add(e);
726     }
727     for(Edge e: edgestoAdd) {
728       //Remove the edge from the remove set
729       edgeRemove.remove(e);
730       //Explicitly add it to the add set unless it is already in the graph
731       if (!existingEdges.contains(e))
732         edgeAdd.add(e);
733     }
734   }
735
736   static void updateHeapDelta(Graph graph, Delta delta, MySet<Edge> edgestoAdd, MySet<Edge> edgestoRemove) {
737     for(Edge e: edgestoRemove) {
738       AllocNode src=e.src;
739       MySet<Edge> edgeAdd=delta.heapedgeadd.get(src);
740       MySet<Edge> existingEdges=graph.getEdges(src);
741       //remove edge from delta
742       edgeAdd.remove(e);
743       //if the edge is already in the graph, add an explicit remove to the delta
744       if (existingEdges.contains(e)) {
745         MySet<Edge> edgeRemove=delta.heapedgeremove.get(src);
746         edgeRemove.add(e);
747       }
748     }
749     for(Edge e: edgestoAdd) {
750       AllocNode src=e.src;
751       MySet<Edge> edgeRemove=delta.heapedgeremove.get(src);
752       MySet<Edge> existingEdges=graph.getEdges(src);
753       //Remove the edge from the remove set
754       edgeRemove.remove(e);
755       //Explicitly add it to the add set unless it is already in the graph
756       if (!existingEdges.contains(e)) {
757         MySet<Edge> edgeAdd=delta.heapedgeadd.get(src);
758         edgeAdd.add(e);
759       }
760     }
761   }
762
763   Delta processFlatNop(FlatNode node, Delta delta, Graph graph) {
764     applyDiffs(graph, delta);
765     return delta;
766   }
767
768   Delta processNewNode(FlatNew node, Delta delta, Graph graph) {
769     AllocNode summary=allocFactory.getAllocNode(node, true);
770     AllocNode single=allocFactory.getAllocNode(node, false);
771     TempDescriptor tmp=node.getDst();
772
773     if (delta.getInit()) {
774       /* We don't have to deal with summarization here...  The
775        * intuition is that this is the only place where we generate
776        * nodes for this allocation site and this is the first time
777        * we've analyzed this site */
778
779       //Build new Edge
780       Edge e=new Edge(tmp, single);
781       //Build new Edge set
782       MySet<Edge> newedges=new MySet<Edge>();
783       newedges.add(e);
784       //Add it into the diffs
785       delta.varedgeadd.put(tmp, newedges);
786       //Remove the old edges
787       delta.varedgeremove.put(tmp, graph.getEdges(tmp));
788       //Apply incoming diffs to graph
789       applyDiffs(graph, delta);
790       //Note that we create a single node
791       delta.addNodeAges.add(single);
792     } else {
793       /* 1. Fix up the variable edge additions */
794
795       for(Iterator<Map.Entry<TempDescriptor, MySet<Edge>>> entryIt=delta.varedgeadd.entrySet().iterator();entryIt.hasNext();) {
796         Map.Entry<TempDescriptor, MySet<Edge>> entry=entryIt.next();
797
798         if (entry.getKey()==tmp) {
799           /* Check if this is the tmp we overwrite */
800           entryIt.remove();
801         } else {
802           /* Otherwise, check if the target of the edge is changed... */
803           summarizeSet(entry.getValue(), graph.varMap.get(entry.getKey()), single, summary);
804         }
805       }
806       
807       /* 2. Fix up the base variable edges */
808
809       for(Iterator<Map.Entry<TempDescriptor, MySet<Edge>>> entryIt=delta.basevaredge.entrySet().iterator();entryIt.hasNext();) {
810         Map.Entry<TempDescriptor, MySet<Edge>> entry=entryIt.next();
811         TempDescriptor entrytmp=entry.getKey();
812         if (entrytmp==tmp) {
813           /* Check is this is the tmp we overwrite, if so add to remove set */
814           Util.relationUpdate(delta.varedgeremove, tmp, null, entry.getValue());
815         } else {
816           /* Check if the target of the edge is changed */ 
817           MySet<Edge> newset=(MySet<Edge>)entry.getValue().clone();
818           MySet<Edge> removeset=shrinkSet(newset, graph.varMap.get(entrytmp), single, summary);
819           Util.relationUpdate(delta.varedgeremove, entrytmp, newset, removeset);
820           Util.relationUpdate(delta.varedgeadd, entrytmp, null, newset);
821         }
822       }
823
824
825       /* 3. Fix up heap edge additions */
826
827       HashMap<AllocNode, MySet<Edge>> addheapedge=new HashMap<AllocNode, MySet<Edge>>();
828       for(Iterator<Map.Entry<AllocNode, MySet<Edge>>> entryIt=delta.heapedgeadd.entrySet().iterator();entryIt.hasNext();) {
829         Map.Entry<AllocNode, MySet<Edge>> entry=entryIt.next();
830         MySet<Edge> edgeset=entry.getValue();
831         AllocNode allocnode=entry.getKey();
832         if (allocnode==single) {
833           entryIt.remove();
834           summarizeSet(edgeset, graph.nodeMap.get(summary), single, summary);
835           addheapedge.put(summary, edgeset);
836         } else {
837           summarizeSet(edgeset, graph.nodeMap.get(allocnode), single, summary);
838         }
839       }
840       
841       /* Merge in diffs */
842
843       for(Map.Entry<AllocNode, MySet<Edge>> entry:addheapedge.entrySet()) {
844         AllocNode allocnode=entry.getKey();
845         Util.relationUpdate(delta.heapedgeadd, allocnode, null, entry.getValue());
846       }
847
848       /* 4. Fix up the base heap edges */
849
850       for(Iterator<Map.Entry<AllocNode, MySet<Edge>>> entryIt=delta.baseheapedge.entrySet().iterator();entryIt.hasNext();) {
851         Map.Entry<AllocNode, MySet<Edge>> entry=entryIt.next();
852         MySet<Edge> edgeset=entry.getValue();
853         AllocNode allocnode=entry.getKey();
854         if (allocnode==single) {
855           entryIt.remove();
856         }
857         AllocNode addnode=(allocnode==single)?summary:allocnode;
858
859         MySet<Edge> newset=(MySet<Edge>) edgeset.clone();
860         MySet<Edge> removeset=shrinkSet(newset, graph.nodeMap.get(addnode), single, summary);
861         Util.relationUpdate(delta.heapedgeadd, addnode, null, newset);
862         Util.relationUpdate(delta.heapedgeremove, allocnode, null, removeset);
863       }
864
865       /* Update Node Ages...If the base or addNodeAges set contains a
866        * single node, it now should also contain a summary node...  No
867        * need to generate a single node as that has already been
868        * done. */
869       if (delta.baseNodeAges.contains(single)||delta.addNodeAges.contains(single)) {
870         delta.addNodeAges.add(summary);
871       }
872
873       //Apply incoming diffs to graph
874       applyDiffs(graph, delta);      
875     }
876     return delta;
877   }
878
879   /* This function builds a new edge set where oldnode is summarized into new node */
880
881   void summarizeSet(MySet<Edge> edgeset, MySet<Edge> oldedgeset, AllocNode oldnode, AllocNode sumnode) {
882     MySet<Edge> newSet=null;
883     for(Iterator<Edge> edgeit=edgeset.iterator();edgeit.hasNext();) {
884       Edge e=edgeit.next();
885       if (e.dst==oldnode||e.src==oldnode) {
886         if (newSet==null) {
887           newSet=new MySet<Edge>();
888         }
889         edgeit.remove();
890         e=e.copy();
891
892         if (e.dst==oldnode) {
893           e.dst=sumnode;
894         }
895         if (e.src==oldnode) {
896           e.src=sumnode;
897         }
898         if (oldedgeset==null||!oldedgeset.contains(e))
899           newSet.add(e);
900       }
901     }
902     if (newSet!=null)
903       edgeset.addAll(newSet);
904   }
905
906   /* Shrinks the incoming set to just include rewritten values.
907    * Returns a set of the original rewritten values */
908
909   MySet<Edge> shrinkSet(MySet<Edge> edgeset, MySet<Edge> oldedgeset, AllocNode oldnode, AllocNode newnode) {
910     MySet<Edge> newSet=null;
911     MySet<Edge> removeSet=null;
912     for(Iterator<Edge> edgeit=edgeset.iterator();edgeit.hasNext();) {
913       Edge e=edgeit.next();
914       edgeit.remove();
915       if (e.dst==oldnode||e.src==oldnode) {
916         if (newSet==null) {
917           newSet=new MySet<Edge>();
918           removeSet=new MySet<Edge>();
919         }
920
921         removeSet.add(e.copy());
922         if (e.dst==oldnode)
923           e.dst=newnode;
924         if (e.src==oldnode)
925           e.src=newnode;
926         if (oldedgeset==null||!oldedgeset.contains(e))
927           newSet.add(e);
928       }
929     }
930     if (newSet!=null)
931       edgeset.addAll(newSet);
932     return removeSet;
933   } 
934
935   /* This function returns a completely new Delta...  It is safe to
936    * modify this */
937
938   Delta applyInitDelta(Delta delta, BBlock block) {
939     //Apply delta to graph
940     boolean newGraph=false;
941     if (!bbgraphMap.containsKey(block)) {
942       bbgraphMap.put(block, new Graph(null));
943       newGraph=true;
944     }
945     Delta newdelta;
946     Graph graph=bbgraphMap.get(block);
947
948     if (newGraph) {
949       newdelta=new Delta(null, true);
950       //Add in heap edges and throw away original diff
951       graph.nodeMap.putAll(delta.heapedgeadd);
952       //Add in var edges and throw away original diff
953       graph.varMap.putAll(delta.varedgeadd);
954       //Record that this is initial set...
955       graph.nodeAges.addAll(delta.addNodeAges);
956     } else {
957       newdelta=new Delta(null, false);
958       //merge in heap edges and variables
959       mergeHeapEdges(graph, delta, newdelta);
960       mergeVarEdges(graph, delta, newdelta);
961       mergeAges(graph, delta, newdelta);
962     }
963     return newdelta;
964   }
965
966   /* This function merges in the heap edges.  It updates delta to be
967    * the difference */
968
969   void mergeHeapEdges(Graph graph, Delta delta, Delta newdelta) {
970     //Merge in edges
971     for(Map.Entry<AllocNode, MySet<Edge>> heapedge:delta.heapedgeadd.entrySet()) {
972       AllocNode nsrc=heapedge.getKey();
973       MySet<Edge> edges=heapedge.getValue();
974       if (!graph.nodeMap.containsKey(nsrc)) {
975         graph.nodeMap.put(nsrc, new MySet<Edge>());
976       }
977       MySet<Edge> dstedges=graph.nodeMap.get(nsrc);
978       MySet<Edge> diffedges=new MySet<Edge>();
979       for(Edge e:edges) {
980         if (!dstedges.contains(e)) {
981           //We have a new edge
982           diffedges.add(e);
983           dstedges.add(e);
984         }
985       }
986       //Done with edge set...
987       if (diffedges.size()>0) {
988         //completely new
989         newdelta.baseheapedge.put(nsrc, diffedges);
990       }
991     }
992   }
993
994   /* This function merges in the var edges.  It updates delta to be
995    * the difference */
996
997   void mergeVarEdges(Graph graph, Delta delta, Delta newdelta) {
998     //Merge in edges
999     for(Map.Entry<TempDescriptor, MySet<Edge>> varedge:delta.varedgeadd.entrySet()) {
1000       TempDescriptor tmpsrc=varedge.getKey();
1001       MySet<Edge> edges=varedge.getValue();
1002       if (!graph.varMap.containsKey(tmpsrc)) {
1003         graph.varMap.put(tmpsrc, new MySet<Edge>());
1004       }
1005       MySet<Edge> dstedges=graph.varMap.get(tmpsrc);
1006       MySet<Edge> diffedges=new MySet<Edge>();
1007       for(Edge e:edges) {
1008         if (!dstedges.contains(e)) {
1009           //We have a new edge
1010           diffedges.add(e);
1011           dstedges.add(e);
1012         }
1013       }
1014       //Done with edge set...
1015       if (diffedges.size()>=0) {
1016         //completely new
1017         newdelta.basevaredge.put(tmpsrc,diffedges);
1018       }
1019     }
1020   }
1021
1022   void mergeAges(Graph graph, Delta delta, Delta newDelta) {
1023     //Merge in edges
1024     for(AllocNode node:delta.addNodeAges) {
1025       if (!graph.nodeAges.contains(node)) {
1026         graph.nodeAges.add(node);
1027         newDelta.baseNodeAges.add(node);
1028       }
1029     }
1030   }
1031 }