more code changes
[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       int startindex=0;
67       if (ppoint.getIndex()==-1) {
68         //Build base graph for entrance to this basic block
69         delta=applyInitDelta(delta, bblock);
70       } else {
71         startindex=ppoint.getIndex()+1;
72         delta=applyCallDelta(delta, bblock);
73       }
74
75       Graph graph=bbgraphMap.get(bblock);
76       Graph nodeGraph=null;
77       //Compute delta at exit of each node
78       for(int i=startindex; i<nodes.size();i++) {
79         FlatNode currNode=nodes.get(i);
80         if (!graphMap.containsKey(currNode)) {
81           graphMap.put(currNode, new Graph(graph));
82         }
83         nodeGraph=graphMap.get(currNode);
84         delta=processNode(bblock, i, currNode, delta, nodeGraph);
85       }
86       generateFinalDelta(bblock, delta, nodeGraph);
87     }
88   }
89
90   void buildInitDelta(Graph graph, Delta newDelta) {
91     //First compute the set of temps
92     HashSet<TempDescriptor> tmpSet=new HashSet<TempDescriptor>();
93     tmpSet.addAll(graph.varMap.keySet());
94     tmpSet.addAll(graph.parent.varMap.keySet());
95     
96     //Next build the temp map part of the delta
97     for(TempDescriptor tmp:tmpSet) {
98       MySet<Edge> edgeSet=new MySet<Edge>();
99       /* Get target set */
100       if (graph.varMap.containsKey(tmp))
101         edgeSet.addAll(graph.varMap.get(tmp));
102       else
103         edgeSet.addAll(graph.parent.varMap.get(tmp));
104       newDelta.varedgeadd.put(tmp, edgeSet);
105     }
106     
107     //Next compute the set of src allocnodes
108     HashSet<AllocNode> nodeSet=new HashSet<AllocNode>();
109     nodeSet.addAll(graph.nodeMap.keySet());
110     nodeSet.addAll(graph.parent.nodeMap.keySet());
111     
112     for(AllocNode node:nodeSet) {
113       MySet<Edge> edgeSet=new MySet<Edge>();
114       /* Get edge set */
115       if (graph.nodeMap.containsKey(node))
116         edgeSet.addAll(graph.nodeMap.get(node));
117       else
118         edgeSet.addAll(graph.parent.nodeMap.get(node));
119       newDelta.heapedgeadd.put(node, edgeSet);
120       
121       /* Compute ages */
122       if (graph.nodeAges.contains(node))
123         newDelta.addNodeAges.add(node);
124       else if (graph.parent.nodeAges.contains(node))
125         newDelta.addNodeAges.add(node);
126
127       /* Compute ages */
128       if (graph.oldNodes.containsKey(node)) {
129         if (graph.oldNodes.get(node).booleanValue())
130           newDelta.addOldNodes.put(node, Boolean.TRUE);
131       } else if (graph.parent.oldNodes.containsKey(node)) {
132         //parent graphs only contain true...no need to check
133         newDelta.addOldNodes.put(node, Boolean.TRUE);
134       }
135     }
136   }
137
138   void generateFinalDelta(BBlock bblock, Delta delta, Graph graph) {
139     Delta newDelta=new Delta(null, false);
140     if (delta.getInit()) {
141       buildInitDelta(graph, newDelta);
142     } else {
143       /* We can break the old delta...it is done being used */
144       /* First we will build variable edges */
145       HashSet<TempDescriptor> tmpSet=new HashSet<TempDescriptor>();
146       tmpSet.addAll(delta.basevaredge.keySet());
147       tmpSet.addAll(delta.varedgeadd.keySet());
148       for(TempDescriptor tmp:tmpSet) {
149         /* Start with the new incoming edges */
150         MySet<Edge> newbaseedge=delta.basevaredge.get(tmp);
151         /* Remove the remove set */
152         newbaseedge.removeAll(delta.varedgeremove.get(tmp));
153         /* Add in the new set*/
154         newbaseedge.addAll(delta.varedgeadd.get(tmp));
155         /* Store the results */
156         newDelta.varedgeadd.put(tmp, newbaseedge);
157       }
158
159       /* Next we build heap edges */
160       HashSet<AllocNode> nodeSet=new HashSet<AllocNode>();
161       nodeSet.addAll(delta.baseheapedge.keySet());
162       nodeSet.addAll(delta.heapedgeadd.keySet());
163       nodeSet.addAll(delta.heapedgeremove.keySet());
164       for(AllocNode node:nodeSet) {
165         /* Start with the new incoming edges */
166         MySet<Edge> newheapedge=(MySet<Edge>) delta.baseheapedge.get(node).clone();
167         /* Remove the remove set */
168         newheapedge.removeAll(delta.heapedgeremove.get(node));
169         /* Add in the add set */
170         newheapedge.addAll(delta.heapedgeadd.get(node));
171         newDelta.heapedgeadd.put(node, newheapedge);
172
173         /* Also need to subtract off some edges */
174         MySet<Edge> removeset=delta.heapedgeremove.get(node);
175
176         /* Remove the newly created edges..no need to propagate a diff for those */
177         removeset.removeAll(delta.baseheapedge.get(node));
178         newDelta.heapedgeremove.put(node, removeset);
179       }
180
181       /* Compute new ages */
182       newDelta.addNodeAges.addAll(delta.baseNodeAges);
183       newDelta.addNodeAges.addAll(delta.addNodeAges);
184       HashSet<AllocNode> oldNodes=new HashSet<AllocNode>();
185
186       /* Compute whether old nodes survive */
187       oldNodes.addAll(delta.baseOldNodes.keySet());
188       oldNodes.addAll(delta.addOldNodes.keySet());
189       for(AllocNode node:oldNodes) {
190         if (delta.addOldNodes.containsKey(node)) {
191           if (delta.addOldNodes.get(node).booleanValue()) {
192             newDelta.addOldNodes.put(node, Boolean.TRUE);
193           }
194         } else {
195           if (delta.baseOldNodes.get(node).booleanValue()) {
196             newDelta.addOldNodes.put(node, Boolean.TRUE);
197           }
198         }
199       }
200     }
201
202     /* Now we need to propagate newdelta */
203     if (!newDelta.heapedgeadd.isEmpty()||!newDelta.heapedgeremove.isEmpty()||!newDelta.varedgeadd.isEmpty()||!newDelta.addNodeAges.isEmpty()||!newDelta.addOldNodes.isEmpty()) {
204       /* We have a delta to propagate */
205       Vector<BBlock> blockvector=bblock.next();
206       for(int i=0;i<blockvector.size();i++) {
207         if (i==0) {
208           newDelta.setBlock(new PPoint(blockvector.get(i)));
209           toprocess.add(newDelta);
210         } else {
211           toprocess.add(newDelta.diffBlock(new PPoint(blockvector.get(i))));
212         }
213       }
214     }
215   }
216
217   Delta processNode(BBlock bblock, int index, FlatNode node, Delta delta, Graph newgraph) {
218     switch(node.kind()) {
219     case FKind.FlatNew:
220       return processNewNode((FlatNew)node, delta, newgraph);
221     case FKind.FlatFieldNode:
222     case FKind.FlatElementNode:
223       return processFieldElementNode(node, delta, newgraph);
224     case FKind.FlatCastNode:
225     case FKind.FlatOpNode:
226     case FKind.FlatReturnNode:
227       return processCopyNode(node, delta, newgraph);
228     case FKind.FlatSetFieldNode:
229     case FKind.FlatSetElementNode:
230       return processSetFieldElementNode(node, delta, newgraph);
231     case FKind.FlatMethod:
232     case FKind.FlatExit:
233       return processFlatNop(node, delta, newgraph);
234     case FKind.FlatCall:
235       return processFlatCall(bblock, index, (FlatCall) node, delta, newgraph);
236     case FKind.FlatSESEEnterNode:
237     case FKind.FlatSESEExitNode:
238       throw new Error("Unimplemented node:"+node);
239     default:
240       throw new Error("Unrecognized node:"+node);
241     }
242   }
243
244
245   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) {
246     //Handle the this temp
247     if (tmpthis!=null) {
248       MySet<Edge> edges=(oldnodeset!=null)?GraphManip.getDiffEdges(delta, tmpthis):GraphManip.getEdges(graph, delta, tmpthis);
249       newDelta.varedgeadd.put(tmpthis, (MySet<Edge>) edges.clone());
250       edgeset.addAll(edges);
251       for(Edge e:edges) {
252         AllocNode dstnode=e.dst;
253         if (!nodeset.contains(dstnode)&&(oldnodeset==null||!oldnodeset.contains(dstnode))) {
254           TypeDescriptor type=dstnode.getType();
255           if (!type.isArray()) {
256             targetSet.add(type.getClassDesc());
257           } else {
258             //arrays don't have code
259             targetSet.add(typeUtil.getClass(TypeUtil.ObjectClass));
260           }
261           nodeset.add(dstnode);
262           tovisit.add(dstnode);
263         }
264       }
265     }
266   }
267
268   void processParams(Graph graph, Delta delta, Delta newDelta, HashSet<AllocNode> nodeset, Stack<AllocNode> tovisit, MySet<Edge> edgeset, FlatCall fcall, boolean diff) {
269     //Go through each temp
270     for(int i=0;i<fcall.numArgs();i++) {
271       TempDescriptor tmp=fcall.getArg(i);
272       MySet<Edge> edges=diff?GraphManip.getDiffEdges(delta, tmp):GraphManip.getEdges(graph, delta, tmp);
273       newDelta.varedgeadd.put(tmp, (MySet<Edge>) edges.clone());
274       edgeset.addAll(edges);
275       for(Edge e:edges) {
276         if (!nodeset.contains(e.dst)) {
277           nodeset.add(e.dst);
278           tovisit.add(e.dst);
279         }
280       }
281     }
282   }
283
284   void computeReachableNodes(Graph graph, Delta delta, Delta newDelta, HashSet<AllocNode> nodeset, Stack<AllocNode> tovisit, MySet<Edge> edgeset, HashSet<AllocNode> oldnodeset) {
285       while(!tovisit.isEmpty()) {
286         AllocNode node=tovisit.pop();
287         MySet<Edge> edges=GraphManip.getEdges(graph, delta, node);
288         newDelta.heapedgeadd.put(node, edges);
289         edgeset.addAll(edges);
290         for(Edge e:edges) {
291           if (!nodeset.contains(e.dst)&&(oldnodeset==null||!oldnodeset.contains(e.dst))) {
292             nodeset.add(e.dst);
293             tovisit.add(e.dst);
294           }
295         }
296       }
297   }
298
299   HashSet<MethodDescriptor> computeTargets(FlatCall fcall, Delta newDelta) {
300     TempDescriptor tmpthis=fcall.getThis();
301     MethodDescriptor md=fcall.getMethod();
302     HashSet<MethodDescriptor> targets=new HashSet<MethodDescriptor>();
303     if (md.isStatic()) {
304       targets.add(md);
305     } else {
306       //Compute Edges
307       for(Edge e:newDelta.varedgeadd.get(tmpthis)) {
308         AllocNode node=e.dst;
309         ClassDescriptor cd=node.getType().getClassDesc();
310         //Figure out exact method called and add to set
311         targets.add(cd.getCalledMethod(md));
312       }
313     }
314     return targets;
315   }
316
317       
318
319
320   void fixMapping(FlatCall fcall, HashSet<MethodDescriptor> targets, MySet<Edge> oldedgeset, Delta newDelta, BBlock callblock, int callindex) {
321     Delta basedelta=null;
322     TempDescriptor tmpthis=fcall.getThis();
323
324     for(MethodDescriptor calledmd:targets) {
325       FlatMethod fm=state.getMethodFlat(calledmd);
326       boolean newmethod=false;
327       
328       //Build tmpMap
329       HashMap<TempDescriptor, TempDescriptor> tmpMap=new HashMap<TempDescriptor, TempDescriptor>();
330       int offset=0;
331       if(tmpthis!=null) {
332         tmpMap.put(tmpthis, fm.getParameter(offset++));
333       }
334       for(int i=0;i<fcall.numArgs();i++) {
335         TempDescriptor tmp=fcall.getArg(i);
336         tmpMap.put(tmp,fm.getParameter(i+offset));
337       }
338
339       //Get basicblock for the method
340       BasicBlock block=getBBlock(fm);
341       
342       //Hook up exits
343       if (!callMap.containsKey(fcall)) {
344         callMap.put(fcall, new HashSet<BBlock>());
345       }
346       
347       Delta returnDelta=null;
348       if (!callMap.get(fcall).contains(block.getStart())) {
349         callMap.get(fcall).add(block.getStart());
350         newmethod=true;
351         
352         //Hook up return
353         if (!returnMap.containsKey(block.getExit())) {
354           returnMap.put(block.getExit(), new HashSet<PPoint>());
355         }
356         returnMap.get(block.getExit()).add(new PPoint(callblock, callindex));
357         
358         if (bbgraphMap.containsKey(block.getExit())) {
359           //Need to push existing results to current node
360           if (returnDelta==null) {
361             returnDelta=new Delta(null, false);
362             buildInitDelta(bbgraphMap.get(block.getExit()), returnDelta);
363             if (!returnDelta.heapedgeadd.isEmpty()||!returnDelta.heapedgeremove.isEmpty()||!returnDelta.varedgeadd.isEmpty()) {
364               returnDelta.setBlock(new PPoint(callblock, callindex));
365               toprocess.add(returnDelta);
366             }
367           } else {
368             if (!returnDelta.heapedgeadd.isEmpty()||!returnDelta.heapedgeremove.isEmpty()||!returnDelta.varedgeadd.isEmpty()) {
369               toprocess.add(returnDelta.diffBlock(new PPoint(callblock, callindex)));
370             }
371           }
372         }
373       }
374       
375       if (oldedgeset==null) {
376         //First build of this graph
377         //Build and enqueue delta...safe to just use existing delta
378         Delta d=newDelta.changeParams(tmpMap, new PPoint(block.getStart()));
379         toprocess.add(d);
380       } else if (newmethod) {
381         if (basedelta==null) {
382           basedelta=newDelta.buildBase(oldedgeset);
383         }
384         //Build and enqueue delta
385         Delta d=basedelta.changeParams(tmpMap, new PPoint(block.getStart()));
386         toprocess.add(d);
387       } else  {
388         //Build and enqueue delta
389         Delta d=newDelta.changeParams(tmpMap, new PPoint(block.getStart()));
390         toprocess.add(d);
391       }
392     }
393   }
394
395
396   /* This function computes all edges that start outside of the callee context and go into the callee context */
397
398   void computeExternalEdges(Graph graph, Delta delta, HashSet<AllocNode> nodeset, HashSet<AllocNode> deltaset, MySet<Edge> externaledgeset) {
399     //Do heap edges first
400     HashSet<AllocNode> externalnodes=new HashSet<AllocNode>();
401     externalnodes.addAll(delta.baseheapedge.keySet());
402     externalnodes.addAll(delta.heapedgeadd.keySet());
403     externalnodes.addAll(delta.heapedgeremove.keySet());
404     //remove allinternal nodes
405     externalnodes.removeAll(nodeset);
406     for(AllocNode extNode:externalnodes) {
407       //Compute set of edges from given node
408       MySet<Edge> edges=new MySet<Edge>(delta.baseheapedge.get(extNode));
409       edges.removeAll(delta.heapedgeremove.get(extNode));
410       edges.addAll(delta.heapedgeadd.get(extNode));
411       
412       for(Edge e:edges) {
413         if (nodeset.contains(e.dst))
414           externaledgeset.add(e);
415       }
416     }
417
418     //Do heap edges first
419     HashSet<TempDescriptor> temps=new HashSet<TempDescriptor>();
420     temps.addAll(delta.basevaredge.keySet());
421     temps.addAll(delta.varedgeadd.keySet());
422     temps.addAll(delta.varedgeremove.keySet());
423     //remove allinternal nodes
424     temps.removeAll(nodeset);
425     
426     for(TempDescriptor tmp:temps) {
427       //Compute set of edges from given node
428       MySet<Edge> edges=new MySet<Edge>(delta.basevaredge.get(tmp));
429       edges.removeAll(delta.varedgeremove.get(tmp));
430       edges.addAll(delta.varedgeadd.get(tmp));
431       
432       for(Edge e:edges) {
433         if (nodeset.contains(e.dst))
434           externaledgeset.add(e);
435       }
436     }
437   }
438   
439   void removeEdges(Delta delta, HashSet<AllocNode> nodeset, MySet<Edge> edgeset, MySet<Edge> externaledgeset) {
440     //Want to remove the set of internal edges
441     for(Edge e:edgeset) {
442       if (e.src!=null) {
443         if (delta.heapedgeadd.containsKey(e.src)&&delta.heapedgeadd.get(e.src).contains(e)) {
444           //remove edge if it is in the add set
445           delta.heapedgeadd.get(e.src).remove(e);
446         } else {
447           //add edge to to the remove set
448           if (!delta.heapedgeremove.containsKey(e.src))
449             delta.heapedgeremove.put(e.src, new MySet<Edge>());
450           delta.heapedgeremove.get(e.src).add(e);
451         }
452       }
453     }
454
455     //Want to remove the set of external edges
456     for(Edge e:externaledgeset) {
457       //want to remove the set of internal edges
458       if (e.src!=null) {
459         if (delta.heapedgeadd.containsKey(e.src)&&delta.heapedgeadd.get(e.src).contains(e)) {
460           //remove edge if it is in the add set
461           delta.heapedgeadd.get(e.src).remove(e);
462         } else {
463           //add edge to to the remove set
464           if (!delta.heapedgeremove.containsKey(e.src))
465             delta.heapedgeremove.put(e.src, new MySet<Edge>());
466           delta.heapedgeremove.get(e.src).add(e);
467         }
468       } else {
469         if (delta.varedgeadd.containsKey(e.srcvar)&&delta.varedgeadd.get(e.srcvar).contains(e)) {
470           //remove edge if it is in the add set
471           delta.varedgeadd.get(e.srcvar).remove(e);
472         } else {
473           //add edge to to the remove set
474           if (!delta.varedgeremove.containsKey(e.srcvar))
475             delta.varedgeremove.put(e.srcvar,new MySet<Edge>());
476           delta.varedgeremove.get(e.srcvar).add(e);
477         }
478       }
479     }
480   }
481   
482
483   Delta processFlatCall(BBlock callblock, int callindex, FlatCall fcall, Delta delta, Graph graph) {
484     Delta newDelta=new Delta(null, false);
485
486     if (delta.getInit()) {
487       MySet<Edge> edgeset=new MySet<Edge>();
488       MySet<Edge> externaledgeset=new MySet<Edge>();
489       HashSet<AllocNode> nodeset=new HashSet<AllocNode>();
490       HashSet<ClassDescriptor> targetSet=new HashSet<ClassDescriptor>();
491       Stack<AllocNode> tovisit=new Stack<AllocNode>();
492       TempDescriptor tmpthis=fcall.getThis();
493
494       //Handle the this temp
495       processThisTargets(targetSet, graph, delta, newDelta, nodeset, tovisit, edgeset, tmpthis, null);
496
497       //Go through each temp
498       processParams(graph, delta, newDelta, nodeset, tovisit, edgeset, fcall, false);
499       
500       //Traverse all reachable nodes
501       computeReachableNodes(graph, delta, newDelta, nodeset, tovisit, edgeset, null);
502
503       //Compute call targets
504       HashSet<MethodDescriptor> targets=computeTargets(fcall, newDelta);
505
506       //Fix mapping
507       fixMapping(fcall, targets, null, newDelta, callblock, callindex);
508
509       //Compute edges into region to splice out
510       computeExternalEdges(graph, delta, nodeset, null, externaledgeset);
511
512       //Splice out internal edges
513       removeEdges(delta, nodeset, edgeset, externaledgeset);
514
515       graph.reachNode=nodeset;
516       graph.reachEdge=edgeset;
517       
518       graph.callNodeAges=new HashSet<AllocNode>();
519       graph.callOldNodes=new HashSet<AllocNode>();
520
521       //Apply diffs to graph
522       applyDiffs(graph, delta, true);
523     } else {
524       MySet<Edge> edgeset=new MySet<Edge>();
525       MySet<Edge> externaledgeset=new MySet<Edge>();
526       HashSet<AllocNode> nodeset=new HashSet<AllocNode>();
527       MySet<Edge> oldedgeset=graph.reachEdge;
528       HashSet<AllocNode> oldnodeset=graph.reachNode;
529
530       HashSet<ClassDescriptor> targetSet=new HashSet<ClassDescriptor>();
531       Stack<AllocNode> tovisit=new Stack<AllocNode>();
532       TempDescriptor tmpthis=fcall.getThis();
533
534       //Handle the this temp
535       processThisTargets(targetSet, graph, delta, newDelta, nodeset, tovisit, edgeset, tmpthis, oldnodeset);
536
537       //Go through each temp
538       processParams(graph, delta, newDelta, nodeset, tovisit, edgeset, fcall, true);
539
540       //Go through each new heap edge that starts from old node
541       MySet<Edge> newedges=GraphManip.getDiffEdges(delta, oldnodeset);
542       edgeset.addAll(newedges);
543       for(Edge e:newedges) {
544         if (!nodeset.contains(e.dst)&&!oldnodeset.contains(e.dst)) {
545           nodeset.add(e.dst);
546           tovisit.add(e.dst);
547         }
548       }
549       
550       //Traverse all reachable nodes
551       computeReachableNodes(graph, delta, newDelta, nodeset, tovisit, edgeset, oldnodeset);
552
553       //Compute call targets
554       HashSet<MethodDescriptor> targets=computeTargets(fcall, newDelta);
555
556       //add in new nodeset and edgeset
557       oldnodeset.addAll(nodeset);
558       oldedgeset.addAll(edgeset);
559
560       //Fix mapping
561       fixMapping(fcall, targets, oldedgeset, newDelta, callblock, callindex);
562
563       //Compute edges into region to splice out
564       computeExternalEdges(graph, delta, oldnodeset, nodeset, externaledgeset);
565
566       //Splice out internal edges
567       removeEdges(delta, nodeset, edgeset, externaledgeset);
568
569       //Apply diffs to graph
570       applyDiffs(graph, delta);
571     }
572     return delta;
573   }
574
575   
576
577   Delta applyCallDelta(Delta delta, BBlock bblock) {
578     Delta newDelta=new Delta(null, false);
579     Vector<FlatNode> nodes=bblock.nodes();
580     PPoint ppoint=delta.getBlock();
581     FlatCall fcall=(FlatCall)nodes.get(ppoint.getIndex());
582     Graph graph=graphMap.get(fcall);
583     Graph oldgraph=(ppoint.getIndex()==0)?
584       bbgraphMap.get(bblock):
585       graphMap.get(nodes.get(ppoint.getIndex()-1));
586     
587     //Age outside edges if necessary
588     for(Iterator<AllocNode> nodeit=delta.addNodeAges.iterator();nodeit.hasNext();) {
589       AllocNode node=nodeit.next();
590       if (!graph.callNodeAges.contains(node)) {
591         graph.callNodeAges.add(node);
592       } else {
593         nodeit.remove();
594       }
595       if (!graph.reachNode.contains(node)&&!node.isSummary()) {
596         /* Need to age node in existing graph*/
597         summarizeInGraph(graph, newDelta, node);
598       }
599     }
600     
601     //Add heap edges in
602     for(Map.Entry<AllocNode, MySet<Edge>> entry:delta.heapedgeadd.entrySet()) {
603       for(Edge e:entry.getValue()) {
604         boolean addedge=false;
605         Edge edgetoadd=null;
606         if (e.statuspredicate==Edge.NEW) {
607           edgetoadd=e;
608         } else {
609           Edge origEdgeKey=e.makeStatus(allocFactory);
610           if (oldgraph.nodeMap.containsKey(origEdgeKey.src)&&
611               oldgraph.nodeMap.get(origEdgeKey.src).contains(origEdgeKey)) {
612             Edge origEdge=oldgraph.nodeMap.get(origEdgeKey.src).get(origEdgeKey);
613             //copy the predicate
614             origEdgeKey.statuspredicate=origEdge.statuspredicate;
615             edgetoadd=origEdgeKey;
616           }
617         }
618         if (edgetoadd!=null) {
619           if (newDelta.heapedgeadd.containsKey(edgetoadd.src)) 
620             newDelta.heapedgeadd.put(edgetoadd.src, new MySet<Edge>(edgetoadd));
621           else
622             newDelta.heapedgeadd.get(edgetoadd.src).add(edgetoadd);
623         }
624       }
625     }
626     
627     
628     return newDelta;
629   }
630
631   /* Summarizes out of context nodes in graph */
632   void summarizeInGraph(Graph graph, Delta newDelta, AllocNode singleNode) {
633     AllocNode summaryNode=allocFactory.getAllocNode(singleNode, true);
634
635     //Handle outgoing heap edges
636     MySet<Edge> edgeset=graph.getEdges(singleNode);
637
638     for(Edge e:edgeset) {
639       Edge rewrite=e.rewrite(singleNode, summaryNode);
640       //Remove old edge
641       if (!newDelta.heapedgeremove.containsKey(singleNode))
642         newDelta.heapedgeremove.put(singleNode, new MySet<Edge>(e));
643       else
644         newDelta.heapedgeremove.get(singleNode).add(e);
645
646       //Add new edge
647       if (!newDelta.heapedgeremove.containsKey(summaryNode))
648         newDelta.heapedgeremove.put(summaryNode, new MySet<Edge>(rewrite));
649       else
650         newDelta.heapedgeremove.get(summaryNode).add(rewrite);
651     }
652     
653     //Handle incoming edges
654     MySet<Edge> backedges=graph.getBackEdges(singleNode);
655     for(Edge e:backedges) {
656       if (e.dst==singleNode) {
657         Edge rewrite=e.rewrite(singleNode, summaryNode);
658         if (e.src!=null) {
659           //Have heap edge
660           if (!newDelta.heapedgeremove.containsKey(e.src))
661             newDelta.heapedgeremove.put(e.src, new MySet<Edge>(e));
662           else
663             newDelta.heapedgeremove.get(e.src).add(e);
664
665           if (!newDelta.heapedgeadd.containsKey(summaryNode))
666             newDelta.heapedgeadd.put(summaryNode, new MySet<Edge>(rewrite));
667           else
668             newDelta.heapedgeadd.get(summaryNode).add(rewrite);
669
670         } else {
671           //Have var edge
672           if (!newDelta.varedgeremove.containsKey(e.srcvar))
673             newDelta.varedgeremove.put(e.srcvar, new MySet<Edge>(e));
674           else
675             newDelta.varedgeremove.get(e.srcvar).add(e);
676
677           if (!newDelta.varedgeadd.containsKey(e.srcvar))
678             newDelta.varedgeadd.put(e.srcvar, new MySet<Edge>(rewrite));
679           else
680             newDelta.varedgeadd.get(e.srcvar).add(rewrite);
681         }
682       }
683     }
684   }
685
686   void applyDiffs(Graph graph, Delta delta) {
687     applyDiffs(graph, delta, false);
688   }
689
690   void applyDiffs(Graph graph, Delta delta, boolean genbackwards) {
691     //build backwards map if requested
692     if (genbackwards&&graph.backMap==null) {
693       graph.backMap=new HashMap<AllocNode, MySet<Edge>>();
694       if (graph.parent.backMap==null) {
695         graph.parent.backMap=new HashMap<AllocNode, MySet<Edge>>();
696         for(Map.Entry<AllocNode, MySet<Edge>> entry:graph.nodeMap.entrySet()) {
697           for(Edge e:entry.getValue()) {
698             if (!graph.parent.backMap.containsKey(e.dst))
699               graph.parent.backMap.put(e.dst, new MySet<Edge>());
700             graph.parent.backMap.get(e.dst).add(e);
701           }
702         }
703         for(Map.Entry<TempDescriptor, MySet<Edge>> entry:graph.varMap.entrySet()) {
704           for(Edge e:entry.getValue()) {
705             if (!graph.parent.backMap.containsKey(e.dst))
706               graph.parent.backMap.put(e.dst, new MySet<Edge>());
707             graph.parent.backMap.get(e.dst).add(e);
708           }
709         }
710       }
711     }
712
713     //Add hidden base edges
714     for(Map.Entry<AllocNode, MySet<Edge>> e: delta.baseheapedge.entrySet()) {
715       AllocNode node=e.getKey();
716       MySet<Edge> edges=e.getValue();
717       if (graph.nodeMap.containsKey(node)) {
718         MySet<Edge> nodeEdges=graph.nodeMap.get(node);
719         nodeEdges.addAll(edges);
720       }
721     }
722
723     //Remove heap edges
724     for(Map.Entry<AllocNode, MySet<Edge>> e: delta.heapedgeremove.entrySet()) {
725       AllocNode node=e.getKey();
726       MySet<Edge> edgestoremove=e.getValue();
727       if (graph.nodeMap.containsKey(node)) {
728         //Just apply diff to current map
729         graph.nodeMap.get(node).removeAll(edgestoremove);
730       } else {
731         //Generate diff from parent graph
732         MySet<Edge> parentedges=graph.parent.nodeMap.get(node);
733         MySet<Edge> newedgeset=Util.setSubtract(parentedges, edgestoremove);
734         graph.nodeMap.put(node, newedgeset);
735       }
736     }
737
738     //Add heap edges
739     for(Map.Entry<AllocNode, MySet<Edge>> e: delta.heapedgeadd.entrySet()) {
740       AllocNode node=e.getKey();
741       MySet<Edge> edgestoadd=e.getValue();
742       //If we have not done a subtract, then 
743       if (!graph.nodeMap.containsKey(node)) {
744         //Copy the parent entry
745         graph.nodeMap.put(node, (MySet<Edge>)graph.parent.nodeMap.get(node).clone());
746       }
747       graph.nodeMap.get(node).addAll(edgestoadd);
748       if (genbackwards) {
749         for(Edge eadd:edgestoadd) {
750           if (!graph.backMap.containsKey(eadd.dst))
751             graph.backMap.put(eadd.dst, new MySet<Edge>());
752           graph.backMap.get(eadd.dst).add(eadd);
753         }
754       }
755     }
756
757     //Remove var edges
758     for(Map.Entry<TempDescriptor, MySet<Edge>> e: delta.varedgeremove.entrySet()) {
759       TempDescriptor tmp=e.getKey();
760       MySet<Edge> edgestoremove=e.getValue();
761
762       if (graph.varMap.containsKey(tmp)) {
763         //Just apply diff to current map
764         graph.varMap.get(tmp).removeAll(edgestoremove);
765       } else {
766         //Generate diff from parent graph
767         MySet<Edge> parentedges=graph.parent.varMap.get(tmp);
768         MySet<Edge> newedgeset=Util.setSubtract(parentedges, edgestoremove);
769         graph.varMap.put(tmp, newedgeset);
770       }
771     }
772
773     //Add var edges
774     for(Map.Entry<TempDescriptor, MySet<Edge>> e: delta.varedgeadd.entrySet()) {
775       TempDescriptor tmp=e.getKey();
776       MySet<Edge> edgestoadd=e.getValue();
777       graph.varMap.put(tmp, (MySet<Edge>) edgestoadd.clone());
778       if (genbackwards) {
779         for(Edge eadd:edgestoadd) {
780           if (!graph.backMap.containsKey(eadd.dst))
781             graph.backMap.put(eadd.dst, new MySet<Edge>());
782           graph.backMap.get(eadd.dst).add(eadd);
783         }
784       }
785     }
786
787     //Add node additions
788     for(AllocNode node:delta.addNodeAges) {
789       graph.nodeAges.add(node);
790     }
791     
792     for(Map.Entry<AllocNode, Boolean> nodeentry:delta.addOldNodes.entrySet()) {
793       AllocNode node=nodeentry.getKey();
794       Boolean ispresent=nodeentry.getValue();
795       graph.oldNodes.put(node, ispresent);
796     }
797   }
798
799   Delta processSetFieldElementNode(FlatNode node, Delta delta, Graph graph) {
800     TempDescriptor src;
801     FieldDescriptor fd;
802     TempDescriptor dst;
803     if (node.kind()==FKind.FlatSetElementNode) {
804       FlatSetElementNode fen=(FlatSetElementNode) node;
805       src=fen.getSrc();
806       fd=null;
807       dst=fen.getDst();
808     } else {
809       FlatSetFieldNode ffn=(FlatSetFieldNode) node;
810       src=ffn.getSrc();
811       fd=ffn.getField();
812       dst=ffn.getDst();
813     }
814     if (delta.getInit()) {
815       HashSet<AllocNode> srcNodes=GraphManip.getNodes(graph, delta, src);
816       HashSet<AllocNode> dstNodes=GraphManip.getNodes(graph, delta, dst);
817       MySet<Edge> edgesToAdd=GraphManip.genEdges(srcNodes, fd, dstNodes);
818       MySet<Edge> edgesToRemove=null;
819       if (dstNodes.size()==1&&!dstNodes.iterator().next().isSummary()) {
820         /* Can do a strong update */
821         edgesToRemove=GraphManip.getEdges(graph, delta, dstNodes, fd);
822       }
823       /* Update diff */
824       updateHeapDelta(graph, delta, edgesToAdd, edgesToRemove);
825       applyDiffs(graph, delta);
826     } else {
827       /* First look at new sources */
828       MySet<Edge> edgesToAdd=new MySet<Edge>();
829       HashSet<AllocNode> newSrcNodes=GraphManip.getDiffNodes(delta, src);
830       HashSet<AllocNode> dstNodes=GraphManip.getDiffNodes(delta, dst);
831       edgesToAdd.addAll(GraphManip.genEdges(newSrcNodes, fd, dstNodes));
832       HashSet<AllocNode> newDstNodes=GraphManip.getDiffNodes(delta, dst);
833       MySet<Edge> edgesToRemove=null;
834       if (newDstNodes.size()!=0) {
835         if (dstNodes.size()==1&&!dstNodes.iterator().next().isSummary()) {
836           /* Need to undo strong update */
837           if (graph.strongUpdateSet!=null) {
838             edgesToAdd.addAll(graph.strongUpdateSet);
839             graph.strongUpdateSet.clear();
840           }
841         } else if (dstNodes.size()==0&&newDstNodes.size()==1&&!newDstNodes.iterator().next().isSummary()&&graph.strongUpdateSet==null) {
842           edgesToRemove=GraphManip.getEdges(graph, delta, dstNodes, fd);
843         }
844         HashSet<AllocNode> srcNodes=GraphManip.getDiffNodes(delta, src);
845         edgesToAdd.addAll(GraphManip.genEdges(srcNodes, fd, newDstNodes));
846       }
847       /* Update diff */
848       updateHeapDelta(graph, delta, edgesToAdd, edgesToRemove);
849       applyDiffs(graph, delta);
850     }
851     return delta;
852   }
853
854   Delta processCopyNode(FlatNode node, Delta delta, Graph graph) {
855     TempDescriptor src;
856     TempDescriptor dst;
857     if (node.kind()==FKind.FlatOpNode) {
858       FlatOpNode fon=(FlatOpNode) node;
859       src=fon.getLeft();
860       dst=fon.getDest();
861     } else if (node.kind()==FKind.FlatReturnNode) {
862       FlatReturnNode frn=(FlatReturnNode)node;
863       src=frn.getReturnTemp();
864       dst=returntmp;
865     } else {
866       FlatCastNode fcn=(FlatCastNode) node;
867       src=fcn.getSrc();
868       dst=fcn.getDst();
869     }
870     if (delta.getInit()) {
871       HashSet<AllocNode> srcnodes=GraphManip.getNodes(graph, delta, src);
872       MySet<Edge> edgesToAdd=GraphManip.genEdges(src, srcnodes);
873       MySet<Edge> edgesToRemove=GraphManip.getEdges(graph, delta, dst);
874       updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
875       applyDiffs(graph, delta);
876     } else {
877       /* First compute new src nodes */
878       HashSet<AllocNode> newSrcNodes=GraphManip.getDiffNodes(delta, src);
879
880       /* Compute the union, and then the set of edges */
881       MySet<Edge> edgesToAdd=GraphManip.genEdges(src, newSrcNodes);
882       
883       /* Compute set of edges to remove */
884       MySet<Edge> edgesToRemove=GraphManip.getDiffEdges(delta, dst);      
885
886       /* Update diff */
887       updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
888       applyDiffs(graph, delta);
889     }
890     return delta;
891   }
892
893   Delta processFieldElementNode(FlatNode node, Delta delta, Graph graph) {
894     TempDescriptor src;
895     FieldDescriptor fd;
896     TempDescriptor dst;
897     if (node.kind()==FKind.FlatElementNode) {
898       FlatElementNode fen=(FlatElementNode) node;
899       src=fen.getSrc();
900       fd=null;
901       dst=fen.getDst();
902     } else {
903       FlatFieldNode ffn=(FlatFieldNode) node;
904       src=ffn.getSrc();
905       fd=ffn.getField();
906       dst=ffn.getDst();
907     }
908     if (delta.getInit()) {
909       HashSet<AllocNode> srcnodes=GraphManip.getNodes(graph, delta, src);
910       HashSet<AllocNode> fdnodes=GraphManip.getNodes(graph, delta, srcnodes, fd);
911       MySet<Edge> edgesToAdd=GraphManip.genEdges(src, fdnodes);
912       MySet<Edge> edgesToRemove=GraphManip.getEdges(graph, delta, dst);
913       updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
914       applyDiffs(graph, delta);
915     } else {
916       /* First compute new objects we read fields of */
917       HashSet<AllocNode> allsrcnodes=GraphManip.getNodes(graph, delta, src);
918       HashSet<AllocNode> difffdnodes=GraphManip.getDiffNodes(delta, allsrcnodes, fd);     
919       /* Next compute new targets of fields */
920       HashSet<AllocNode> newsrcnodes=GraphManip.getDiffNodes(delta, src);
921       HashSet<AllocNode> newfdnodes=GraphManip.getNodes(graph, delta, newsrcnodes, fd);
922       /* Compute the union, and then the set of edges */
923       HashSet<AllocNode> newTargets=new HashSet<AllocNode>();
924       newTargets.addAll(newfdnodes);
925       newTargets.addAll(difffdnodes);
926       MySet<Edge> edgesToAdd=GraphManip.genEdges(src, newTargets);      
927       
928       /* Compute set of edges to remove */
929       MySet<Edge> edgesToRemove=GraphManip.getDiffEdges(delta, dst);      
930
931       /* Update diff */
932       updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
933       applyDiffs(graph, delta);
934     }
935     return delta;
936   }
937
938   static void updateVarDelta(Graph graph, Delta delta, TempDescriptor tmp, MySet<Edge> edgestoAdd, MySet<Edge> edgestoRemove) {
939     MySet<Edge> edgeAdd=delta.varedgeadd.get(tmp);
940     MySet<Edge> edgeRemove=delta.varedgeremove.get(tmp);
941     MySet<Edge> existingEdges=graph.getEdges(tmp);
942     for(Edge e: edgestoRemove) {
943       //remove edge from delta
944       edgeAdd.remove(e);
945       //if the edge is already in the graph, add an explicit remove to the delta
946       if (existingEdges.contains(e))
947         edgeRemove.add(e);
948     }
949     for(Edge e: edgestoAdd) {
950       //Remove the edge from the remove set
951       edgeRemove.remove(e);
952       //Explicitly add it to the add set unless it is already in the graph
953       if (!existingEdges.contains(e))
954         edgeAdd.add(e);
955     }
956   }
957
958   static void updateHeapDelta(Graph graph, Delta delta, MySet<Edge> edgestoAdd, MySet<Edge> edgestoRemove) {
959     for(Edge e: edgestoRemove) {
960       AllocNode src=e.src;
961       MySet<Edge> edgeAdd=delta.heapedgeadd.get(src);
962       MySet<Edge> existingEdges=graph.getEdges(src);
963       //remove edge from delta
964       edgeAdd.remove(e);
965       //if the edge is already in the graph, add an explicit remove to the delta
966       if (existingEdges.contains(e)) {
967         MySet<Edge> edgeRemove=delta.heapedgeremove.get(src);
968         edgeRemove.add(e);
969       }
970     }
971     for(Edge e: edgestoAdd) {
972       AllocNode src=e.src;
973       MySet<Edge> edgeRemove=delta.heapedgeremove.get(src);
974       MySet<Edge> existingEdges=graph.getEdges(src);
975       //Remove the edge from the remove set
976       edgeRemove.remove(e);
977       //Explicitly add it to the add set unless it is already in the graph
978       if (!existingEdges.contains(e)) {
979         MySet<Edge> edgeAdd=delta.heapedgeadd.get(src);
980         edgeAdd.add(e);
981       }
982     }
983   }
984
985   Delta processFlatNop(FlatNode node, Delta delta, Graph graph) {
986     applyDiffs(graph, delta);
987     return delta;
988   }
989   
990   Delta processNewNode(FlatNew node, Delta delta, Graph graph) {
991     AllocNode summary=allocFactory.getAllocNode(node, true);
992     AllocNode single=allocFactory.getAllocNode(node, false);
993     TempDescriptor tmp=node.getDst();
994
995     if (delta.getInit()) {
996       /* We don't have to deal with summarization here...  The
997        * intuition is that this is the only place where we generate
998        * nodes for this allocation site and this is the first time
999        * we've analyzed this site */
1000
1001       //Build new Edge
1002       Edge e=new Edge(tmp, single);
1003       //Build new Edge set
1004       MySet<Edge> newedges=new MySet<Edge>();
1005       newedges.add(e);
1006       //Add it into the diffs
1007       delta.varedgeadd.put(tmp, newedges);
1008       //Remove the old edges
1009       delta.varedgeremove.put(tmp, graph.getEdges(tmp));
1010       //Apply incoming diffs to graph
1011       applyDiffs(graph, delta);
1012       //Note that we create a single node
1013       delta.addNodeAges.add(single);
1014       //Kill the old node
1015       if (delta.addOldNodes.containsKey(single)||delta.baseOldNodes.containsKey(single)) {
1016         delta.addOldNodes.put(single, Boolean.FALSE);
1017       }
1018     } else {
1019       /* 1. Fix up the variable edge additions */
1020
1021       for(Iterator<Map.Entry<TempDescriptor, MySet<Edge>>> entryIt=delta.varedgeadd.entrySet().iterator();entryIt.hasNext();) {
1022         Map.Entry<TempDescriptor, MySet<Edge>> entry=entryIt.next();
1023
1024         if (entry.getKey()==tmp) {
1025           /* Check if this is the tmp we overwrite */
1026           entryIt.remove();
1027         } else {
1028           /* Otherwise, check if the target of the edge is changed... */
1029           summarizeSet(entry.getValue(), graph.varMap.get(entry.getKey()), single, summary);
1030         }
1031       }
1032       
1033       /* 2. Fix up the base variable edges */
1034
1035       for(Iterator<Map.Entry<TempDescriptor, MySet<Edge>>> entryIt=delta.basevaredge.entrySet().iterator();entryIt.hasNext();) {
1036         Map.Entry<TempDescriptor, MySet<Edge>> entry=entryIt.next();
1037         TempDescriptor entrytmp=entry.getKey();
1038         if (entrytmp==tmp) {
1039           /* Check is this is the tmp we overwrite, if so add to remove set */
1040           Util.relationUpdate(delta.varedgeremove, tmp, null, entry.getValue());
1041         } else {
1042           /* Check if the target of the edge is changed */ 
1043           MySet<Edge> newset=(MySet<Edge>)entry.getValue().clone();
1044           MySet<Edge> removeset=shrinkSet(newset, graph.varMap.get(entrytmp), single, summary);
1045           Util.relationUpdate(delta.varedgeremove, entrytmp, newset, removeset);
1046           Util.relationUpdate(delta.varedgeadd, entrytmp, null, newset);
1047         }
1048       }
1049
1050
1051       /* 3. Fix up heap edge additions */
1052
1053       HashMap<AllocNode, MySet<Edge>> addheapedge=new HashMap<AllocNode, MySet<Edge>>();
1054       for(Iterator<Map.Entry<AllocNode, MySet<Edge>>> entryIt=delta.heapedgeadd.entrySet().iterator();entryIt.hasNext();) {
1055         Map.Entry<AllocNode, MySet<Edge>> entry=entryIt.next();
1056         MySet<Edge> edgeset=entry.getValue();
1057         AllocNode allocnode=entry.getKey();
1058         if (allocnode==single) {
1059           entryIt.remove();
1060           summarizeSet(edgeset, graph.nodeMap.get(summary), single, summary);
1061           addheapedge.put(summary, edgeset);
1062         } else {
1063           summarizeSet(edgeset, graph.nodeMap.get(allocnode), single, summary);
1064         }
1065       }
1066       
1067       /* Merge in diffs */
1068
1069       for(Map.Entry<AllocNode, MySet<Edge>> entry:addheapedge.entrySet()) {
1070         AllocNode allocnode=entry.getKey();
1071         Util.relationUpdate(delta.heapedgeadd, allocnode, null, entry.getValue());
1072       }
1073
1074       /* 4. Fix up the base heap edges */
1075
1076       for(Iterator<Map.Entry<AllocNode, MySet<Edge>>> entryIt=delta.baseheapedge.entrySet().iterator();entryIt.hasNext();) {
1077         Map.Entry<AllocNode, MySet<Edge>> entry=entryIt.next();
1078         MySet<Edge> edgeset=entry.getValue();
1079         AllocNode allocnode=entry.getKey();
1080         if (allocnode==single) {
1081           entryIt.remove();
1082         }
1083         AllocNode addnode=(allocnode==single)?summary:allocnode;
1084
1085         MySet<Edge> newset=(MySet<Edge>) edgeset.clone();
1086         MySet<Edge> removeset=shrinkSet(newset, graph.nodeMap.get(addnode), single, summary);
1087         Util.relationUpdate(delta.heapedgeadd, addnode, null, newset);
1088         Util.relationUpdate(delta.heapedgeremove, allocnode, null, removeset);
1089       }
1090
1091       /* Update Node Ages...If the base or addNodeAges set contains a
1092        * single node, it now should also contain a summary node...  No
1093        * need to generate a single node as that has already been
1094        * done. */
1095       if (delta.baseNodeAges.contains(single)||delta.addNodeAges.contains(single)) {
1096         delta.addNodeAges.add(summary);
1097       }
1098
1099       //Kill the old node if someone tries to add it
1100       if (delta.addOldNodes.containsKey(single)||delta.baseOldNodes.containsKey(single)) {
1101         delta.addOldNodes.put(single, Boolean.FALSE);
1102       }
1103       
1104       //Apply incoming diffs to graph
1105       applyDiffs(graph, delta);      
1106     }
1107     return delta;
1108   }
1109
1110   /* This function builds a new edge set where oldnode is summarized into new node */
1111
1112   void summarizeSet(MySet<Edge> edgeset, MySet<Edge> oldedgeset, AllocNode oldnode, AllocNode sumnode) {
1113     MySet<Edge> newSet=null;
1114     for(Iterator<Edge> edgeit=edgeset.iterator();edgeit.hasNext();) {
1115       Edge e=edgeit.next();
1116       if (e.dst==oldnode||e.src==oldnode) {
1117         if (newSet==null) {
1118           newSet=new MySet<Edge>();
1119         }
1120         edgeit.remove();
1121         e=e.copy();
1122
1123         if (e.dst==oldnode) {
1124           e.dst=sumnode;
1125         }
1126         if (e.src==oldnode) {
1127           e.src=sumnode;
1128         }
1129         if (oldedgeset==null||!oldedgeset.contains(e))
1130           newSet.add(e);
1131       }
1132     }
1133     if (newSet!=null)
1134       edgeset.addAll(newSet);
1135   }
1136
1137   /* Shrinks the incoming set to just include rewritten values.
1138    * Returns a set of the original rewritten values */
1139
1140   MySet<Edge> shrinkSet(MySet<Edge> edgeset, MySet<Edge> oldedgeset, AllocNode oldnode, AllocNode newnode) {
1141     MySet<Edge> newSet=null;
1142     MySet<Edge> removeSet=null;
1143     for(Iterator<Edge> edgeit=edgeset.iterator();edgeit.hasNext();) {
1144       Edge e=edgeit.next();
1145       edgeit.remove();
1146       if (e.dst==oldnode||e.src==oldnode) {
1147         if (newSet==null) {
1148           newSet=new MySet<Edge>();
1149           removeSet=new MySet<Edge>();
1150         }
1151
1152         removeSet.add(e.copy());
1153         if (e.dst==oldnode)
1154           e.dst=newnode;
1155         if (e.src==oldnode)
1156           e.src=newnode;
1157         if (oldedgeset==null||!oldedgeset.contains(e))
1158           newSet.add(e);
1159       }
1160     }
1161     if (newSet!=null)
1162       edgeset.addAll(newSet);
1163     return removeSet;
1164   } 
1165
1166   /* This function returns a completely new Delta...  It is safe to
1167    * modify this */
1168
1169   Delta applyInitDelta(Delta delta, BBlock block) {
1170     //Apply delta to graph
1171     boolean newGraph=false;
1172     if (!bbgraphMap.containsKey(block)) {
1173       bbgraphMap.put(block, new Graph(null));
1174       newGraph=true;
1175     }
1176     Delta newdelta;
1177     Graph graph=bbgraphMap.get(block);
1178
1179     if (newGraph) {
1180       newdelta=new Delta(null, true);
1181       //Add in heap edges and throw away original diff
1182       graph.nodeMap.putAll(delta.heapedgeadd);
1183       //Add in var edges and throw away original diff
1184       graph.varMap.putAll(delta.varedgeadd);
1185       //Record that this is initial set...
1186       graph.nodeAges.addAll(delta.addNodeAges);
1187       //Add old nodes
1188       for(Map.Entry<AllocNode, Boolean> oldentry:delta.addOldNodes.entrySet()) {
1189         if (oldentry.getValue().booleanValue()) {
1190           graph.oldNodes.put(oldentry.getKey(), Boolean.TRUE);
1191         }
1192       }
1193     } else {
1194       newdelta=new Delta(null, false);
1195       //merge in heap edges and variables
1196       mergeHeapEdges(graph, delta, newdelta);
1197       mergeVarEdges(graph, delta, newdelta);
1198       mergeAges(graph, delta, newdelta);
1199     }
1200     return newdelta;
1201   }
1202
1203   /* This function merges in the heap edges.  It updates delta to be
1204    * the difference */
1205
1206   void mergeHeapEdges(Graph graph, Delta delta, Delta newdelta) {
1207     //Merge in edges
1208     for(Map.Entry<AllocNode, MySet<Edge>> heapedge:delta.heapedgeadd.entrySet()) {
1209       AllocNode nsrc=heapedge.getKey();
1210       MySet<Edge> edges=heapedge.getValue();
1211
1212       if (graph.backMap!=null) {
1213         for(Edge e:edges) {
1214           if (!graph.backMap.containsKey(e.dst))
1215             graph.backMap.put(e.dst, new MySet<Edge>());
1216           graph.backMap.get(e.dst).add(e);
1217         }
1218       }
1219
1220       if (!graph.nodeMap.containsKey(nsrc)) {
1221         graph.nodeMap.put(nsrc, new MySet<Edge>());
1222       }
1223       MySet<Edge> dstedges=graph.nodeMap.get(nsrc);
1224       MySet<Edge> diffedges=new MySet<Edge>();
1225       for(Edge e:edges) {
1226         if (!dstedges.contains(e)) {
1227           //We have a new edge
1228           diffedges.add(e);
1229           dstedges.add(e);
1230         }
1231       }
1232       //Done with edge set...
1233       if (diffedges.size()>0) {
1234         //completely new
1235         newdelta.baseheapedge.put(nsrc, diffedges);
1236       }
1237     }
1238   }
1239
1240   /* This function merges in the var edges.  It updates delta to be
1241    * the difference */
1242
1243   void mergeVarEdges(Graph graph, Delta delta, Delta newdelta) {
1244     //Merge in edges
1245     for(Map.Entry<TempDescriptor, MySet<Edge>> varedge:delta.varedgeadd.entrySet()) {
1246       TempDescriptor tmpsrc=varedge.getKey();
1247       MySet<Edge> edges=varedge.getValue();
1248       if (graph.backMap!=null) {
1249         for(Edge e:edges) {
1250           if (!graph.backMap.containsKey(e.dst))
1251             graph.backMap.put(e.dst, new MySet<Edge>());
1252           graph.backMap.get(e.dst).add(e);
1253         }
1254       }
1255
1256       if (!graph.varMap.containsKey(tmpsrc)) {
1257         graph.varMap.put(tmpsrc, new MySet<Edge>());
1258       }
1259       MySet<Edge> dstedges=graph.varMap.get(tmpsrc);
1260       MySet<Edge> diffedges=new MySet<Edge>();
1261       for(Edge e:edges) {
1262         if (!dstedges.contains(e)) {
1263           //We have a new edge
1264           diffedges.add(e);
1265           dstedges.add(e);
1266         }
1267       }
1268       //Done with edge set...
1269       if (diffedges.size()>=0) {
1270         //completely new
1271         newdelta.basevaredge.put(tmpsrc,diffedges);
1272       }
1273     }
1274   }
1275
1276   void mergeAges(Graph graph, Delta delta, Delta newDelta) {
1277     //Merge in edges
1278     for(AllocNode node:delta.addNodeAges) {
1279       if (!graph.nodeAges.contains(node)) {
1280         graph.nodeAges.add(node);
1281         newDelta.baseNodeAges.add(node);
1282       }
1283     }
1284     for(Map.Entry<AllocNode, Boolean> oldentry:delta.addOldNodes.entrySet()) {
1285       AllocNode node=oldentry.getKey();
1286       boolean ispresent=oldentry.getValue().booleanValue();
1287       if (ispresent&&!graph.oldNodes.containsKey(node)) {
1288         graph.oldNodes.put(node, Boolean.TRUE);
1289         newDelta.baseOldNodes.put(node, Boolean.TRUE);
1290       }
1291     }
1292   }
1293 }