running on some benchmarks....
[IRC.git] / Robust / src / Analysis / Pointer / Pointer.java
index ceca50bd3ace2691875b64ad857e8eb9b8ff64fd..98e0512aa83e5fa7ae6617ad5b81dc72869633a9 100644 (file)
@@ -4,6 +4,7 @@ import IR.Flat.*;
 import IR.*;
 import Analysis.Pointer.BasicBlock.BBlock;
 import Analysis.Pointer.AllocFactory.AllocNode;
+import java.io.*;
 
 public class Pointer {
   HashMap<FlatMethod, BasicBlock> blockMap;
@@ -47,23 +48,22 @@ public class Pointer {
     MySet<Edge> arrayset=new MySet<Edge>();
     MySet<Edge> varset=new MySet<Edge>();
     Edge arrayedge=new Edge(allocFactory.StringArray, null, allocFactory.Strings);
-    arrayset.add(arrayedge);
     Edge stringedge=new Edge(fm.getParameter(0), allocFactory.StringArray);
-    varset.add(stringedge);
-    delta.heapedgeadd.put(allocFactory.StringArray, arrayset);
-    delta.varedgeadd.put(fm.getParameter(0), varset);
+    delta.addHeapEdge(arrayedge);
+    delta.addVarEdge(stringedge);
+
     return delta;
   }
 
   public void doAnalysis() {
     toprocess.add(buildInitialContext());
-
     while(!toprocess.isEmpty()) {
       Delta delta=toprocess.remove();
       PPoint ppoint=delta.getBlock();
       BBlock bblock=ppoint.getBBlock();
       Vector<FlatNode> nodes=bblock.nodes();
       int startindex=0;
+
       if (ppoint.getIndex()==-1) {
        //Build base graph for entrance to this basic block
        delta=applyInitDelta(delta, bblock);
@@ -71,12 +71,12 @@ public class Pointer {
        startindex=ppoint.getIndex()+1;
        delta=applyCallDelta(delta, bblock);
       }
-
       Graph graph=bbgraphMap.get(bblock);
       Graph nodeGraph=null;
       //Compute delta at exit of each node
       for(int i=startindex; i<nodes.size();i++) {
        FlatNode currNode=nodes.get(i);
+
        if (!graphMap.containsKey(currNode)) {
          graphMap.put(currNode, new Graph(graph));
        }
@@ -85,8 +85,43 @@ public class Pointer {
       }
       generateFinalDelta(bblock, delta, nodeGraph);
     }
+
+    //DEBUG
+    if (false) {
+      int debugindex=0;
+      for(Map.Entry<BBlock, Graph> e:bbgraphMap.entrySet()) {
+       Graph g=e.getValue();
+       plotGraph(g,"BB"+debugindex);
+       debugindex++;
+      }
+      
+      for(Map.Entry<FlatNode, Graph> e:graphMap.entrySet()) {
+       FlatNode fn=e.getKey();
+       Graph g=e.getValue();
+       plotGraph(g,"FN"+fn.toString()+debugindex);
+       debugindex++;
+      }
+      for(FlatMethod fm:blockMap.keySet()) {
+       fm.printMethod();
+      }
+    }
   }
 
+  void plotGraph(Graph g, String name) {
+    try {
+      PrintWriter pw=new PrintWriter(new FileWriter(name.toString().replace(' ','_')+".dot"));
+      g.printGraph(pw, name);
+      pw.close();
+    } catch (Exception ex) {
+      ex.printStackTrace();
+    }
+  }
+  
+
+  /* This function builds the last delta for a basic block.  It
+   * handles the case for the first time the basic block is
+   * evaluated.*/
+
   void buildInitDelta(Graph graph, Delta newDelta) {
     //First compute the set of temps
     HashSet<TempDescriptor> tmpSet=new HashSet<TempDescriptor>();
@@ -135,6 +170,8 @@ public class Pointer {
     }
   }
 
+  /* This function build the delta for the exit of a basic block. */
+
   void generateFinalDelta(BBlock bblock, Delta delta, Graph graph) {
     Delta newDelta=new Delta(null, false);
     if (delta.getInit()) {
@@ -149,12 +186,15 @@ public class Pointer {
        /* Start with the new incoming edges */
        MySet<Edge> newbaseedge=delta.basevaredge.get(tmp);
        /* Remove the remove set */
+       if (newbaseedge==null)
+         newbaseedge=new MySet<Edge>();
        newbaseedge.removeAll(delta.varedgeremove.get(tmp));
        /* Add in the new set*/
        newbaseedge.addAll(delta.varedgeadd.get(tmp));
        /* Store the results */
        newDelta.varedgeadd.put(tmp, newbaseedge);
       }
+      delta.basevaredge.clear();
 
       /* Next we build heap edges */
       HashSet<AllocNode> nodeSet=new HashSet<AllocNode>();
@@ -163,19 +203,24 @@ public class Pointer {
       nodeSet.addAll(delta.heapedgeremove.keySet());
       for(AllocNode node:nodeSet) {
        /* Start with the new incoming edges */
-       MySet<Edge> newheapedge=(MySet<Edge>) delta.baseheapedge.get(node).clone();
+       MySet<Edge> newheapedge=new MySet<Edge>(delta.baseheapedge.get(node));
        /* Remove the remove set */
-       newheapedge.removeAll(delta.heapedgeremove.get(node));
+       MySet<Edge> removeset=delta.heapedgeremove.get(node);
+
+       if (removeset!=null)
+         newheapedge.removeAll(removeset);
+
        /* Add in the add set */
-       newheapedge.addAll(delta.heapedgeadd.get(node));
+       MySet<Edge> settoadd=delta.heapedgeadd.get(node);
+       if (settoadd!=null)
+         newheapedge.addAll(settoadd);
        newDelta.heapedgeadd.put(node, newheapedge);
 
-       /* Also need to subtract off some edges */
-       MySet<Edge> removeset=delta.heapedgeremove.get(node);
-
        /* Remove the newly created edges..no need to propagate a diff for those */
-       removeset.removeAll(delta.baseheapedge.get(node));
-       newDelta.heapedgeremove.put(node, removeset);
+       if (removeset!=null) {
+         removeset.removeAll(delta.baseheapedge.get(node));
+         newDelta.heapedgeremove.put(node, removeset);
+       }
       }
 
       /* Compute new ages */
@@ -202,13 +247,31 @@ public class Pointer {
     /* Now we need to propagate newdelta */
     if (!newDelta.heapedgeadd.isEmpty()||!newDelta.heapedgeremove.isEmpty()||!newDelta.varedgeadd.isEmpty()||!newDelta.addNodeAges.isEmpty()||!newDelta.addOldNodes.isEmpty()) {
       /* We have a delta to propagate */
-      Vector<BBlock> blockvector=bblock.next();
-      for(int i=0;i<blockvector.size();i++) {
-       if (i==0) {
-         newDelta.setBlock(new PPoint(blockvector.get(i)));
-         toprocess.add(newDelta);
-       } else {
-         toprocess.add(newDelta.diffBlock(new PPoint(blockvector.get(i))));
+      if (returnMap.containsKey(bblock)) {
+       //exit of call block
+       boolean first=true;
+
+       for(PPoint caller:returnMap.get(bblock)) {
+         if (first) {
+           newDelta.setBlock(caller);
+           toprocess.add(newDelta);
+           first=false;
+         } else {
+           Delta d=newDelta.diffBlock(caller);
+           toprocess.add(d);
+         }
+       }
+      } else {
+       //normal block
+       Vector<BBlock> blockvector=bblock.next();
+       for(int i=0;i<blockvector.size();i++) {
+         if (i==0) {
+           newDelta.setBlock(new PPoint(blockvector.get(i)));
+           toprocess.add(newDelta);
+         } else {
+           Delta d=newDelta.diffBlock(new PPoint(blockvector.get(i)));
+           toprocess.add(d);
+         }
        }
       }
     }
@@ -230,17 +293,20 @@ public class Pointer {
       return processSetFieldElementNode(node, delta, newgraph);
     case FKind.FlatMethod:
     case FKind.FlatExit:
+    case FKind.FlatBackEdge:
+    case FKind.FlatGenReachNode:
+    case FKind.FlatSESEEnterNode:
+    case FKind.FlatSESEExitNode:
       return processFlatNop(node, delta, newgraph);
     case FKind.FlatCall:
       return processFlatCall(bblock, index, (FlatCall) node, delta, newgraph);
-    case FKind.FlatSESEEnterNode:
-    case FKind.FlatSESEExitNode:
-      throw new Error("Unimplemented node:"+node);
     default:
       throw new Error("Unrecognized node:"+node);
     }
   }
 
+  /* This function compute the edges for the this variable for a
+   * callee if it exists. */
 
   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) {
     //Handle the this temp
@@ -265,6 +331,8 @@ public class Pointer {
     }
   }
 
+  /* This function compute the edges for a call's parameters. */
+
   void processParams(Graph graph, Delta delta, Delta newDelta, HashSet<AllocNode> nodeset, Stack<AllocNode> tovisit, MySet<Edge> edgeset, FlatCall fcall, boolean diff) {
     //Go through each temp
     for(int i=0;i<fcall.numArgs();i++) {
@@ -281,6 +349,8 @@ public class Pointer {
     }
   }
 
+  /* This function computes the reachable nodes for a callee. */
+
   void computeReachableNodes(Graph graph, Delta delta, Delta newDelta, HashSet<AllocNode> nodeset, Stack<AllocNode> tovisit, MySet<Edge> edgeset, HashSet<AllocNode> oldnodeset) {
       while(!tovisit.isEmpty()) {
        AllocNode node=tovisit.pop();
@@ -314,7 +384,6 @@ public class Pointer {
     return targets;
   }
 
-
   void fixMapping(FlatCall fcall, HashSet<MethodDescriptor> targets, MySet<Edge> oldedgeset, Delta newDelta, BBlock callblock, int callindex) {
     Delta basedelta=null;
     TempDescriptor tmpthis=fcall.getThis();
@@ -357,7 +426,9 @@ public class Pointer {
          //Need to push existing results to current node
          if (returnDelta==null) {
            returnDelta=new Delta(null, false);
-           buildInitDelta(bbgraphMap.get(block.getExit()), returnDelta);
+           Vector<FlatNode> exitblocknodes=block.getExit().nodes();
+           FlatExit fexit=(FlatExit)exitblocknodes.get(exitblocknodes.size()-1);
+           buildInitDelta(graphMap.get(fexit), returnDelta);
            if (!returnDelta.heapedgeadd.isEmpty()||!returnDelta.heapedgeremove.isEmpty()||!returnDelta.varedgeadd.isEmpty()) {
              returnDelta.setBlock(new PPoint(callblock, callindex));
              toprocess.add(returnDelta);
@@ -424,6 +495,7 @@ public class Pointer {
     for(TempDescriptor tmp:temps) {
       //Compute set of edges from given node
       MySet<Edge> edges=new MySet<Edge>(delta.basevaredge.get(tmp));
+      
       edges.removeAll(delta.varedgeremove.get(tmp));
       edges.addAll(delta.varedgeadd.get(tmp));
       
@@ -433,50 +505,24 @@ public class Pointer {
       }
     }
   }
+
+  /* This function removes the caller reachable edges from the
+   * callee's heap. */
   
   void removeEdges(Delta delta, HashSet<AllocNode> nodeset, MySet<Edge> edgeset, MySet<Edge> externaledgeset) {
     //Want to remove the set of internal edges
     for(Edge e:edgeset) {
       if (e.src!=null) {
-       if (delta.heapedgeadd.containsKey(e.src)&&delta.heapedgeadd.get(e.src).contains(e)) {
-         //remove edge if it is in the add set
-         delta.heapedgeadd.get(e.src).remove(e);
-       } else {
-         //add edge to to the remove set
-         if (!delta.heapedgeremove.containsKey(e.src))
-           delta.heapedgeremove.put(e.src, new MySet<Edge>());
-         delta.heapedgeremove.get(e.src).add(e);
-       }
+       delta.removeHeapEdge(e);
       }
     }
 
     //Want to remove the set of external edges
     for(Edge e:externaledgeset) {
       //want to remove the set of internal edges
-      if (e.src!=null) {
-       if (delta.heapedgeadd.containsKey(e.src)&&delta.heapedgeadd.get(e.src).contains(e)) {
-         //remove edge if it is in the add set
-         delta.heapedgeadd.get(e.src).remove(e);
-       } else {
-         //add edge to to the remove set
-         if (!delta.heapedgeremove.containsKey(e.src))
-           delta.heapedgeremove.put(e.src, new MySet<Edge>());
-         delta.heapedgeremove.get(e.src).add(e);
-       }
-      } else {
-       if (delta.varedgeadd.containsKey(e.srcvar)&&delta.varedgeadd.get(e.srcvar).contains(e)) {
-         //remove edge if it is in the add set
-         delta.varedgeadd.get(e.srcvar).remove(e);
-       } else {
-         //add edge to to the remove set
-         if (!delta.varedgeremove.containsKey(e.srcvar))
-           delta.varedgeremove.put(e.srcvar,new MySet<Edge>());
-         delta.varedgeremove.get(e.srcvar).add(e);
-       }
-      }
+      delta.removeEdge(e);
     }
   }
-  
 
   Delta processFlatCall(BBlock callblock, int callindex, FlatCall fcall, Delta delta, Graph graph) {
     Delta newDelta=new Delta(null, false);
@@ -510,9 +556,14 @@ public class Pointer {
       //Splice out internal edges
       removeEdges(delta, nodeset, edgeset, externaledgeset);
 
+      //store data structures
+      graph.externalEdgeSet=externaledgeset;
       graph.reachNode=nodeset;
       graph.reachEdge=edgeset;
       
+      graph.callNodeAges=new HashSet<AllocNode>();
+      graph.callOldNodes=new HashSet<AllocNode>();
+
       //Apply diffs to graph
       applyDiffs(graph, delta, true);
     } else {
@@ -561,21 +612,135 @@ public class Pointer {
       //Splice out internal edges
       removeEdges(delta, nodeset, edgeset, externaledgeset);
 
+      //Add in new external edges
+      graph.externalEdgeSet.addAll(externaledgeset);
+
       //Apply diffs to graph
       applyDiffs(graph, delta);
     }
     return delta;
   }
 
+  /* This function applies callee deltas to the caller heap. */
+
   Delta applyCallDelta(Delta delta, BBlock bblock) {
+    Delta newDelta=new Delta(null, false);
     Vector<FlatNode> nodes=bblock.nodes();
     PPoint ppoint=delta.getBlock();
     FlatCall fcall=(FlatCall)nodes.get(ppoint.getIndex());
     Graph graph=graphMap.get(fcall);
-    
+    Graph oldgraph=(ppoint.getIndex()==0)?
+      bbgraphMap.get(bblock):
+      graphMap.get(nodes.get(ppoint.getIndex()-1));
+
+    //Age outside nodes if necessary
+    for(Iterator<AllocNode> nodeit=delta.addNodeAges.iterator();nodeit.hasNext();) {
+      AllocNode node=nodeit.next();
+      if (!graph.callNodeAges.contains(node)) {
+       graph.callNodeAges.add(node);
+      } else {
+       nodeit.remove();
+      }
+      if (!graph.reachNode.contains(node)&&!node.isSummary()) {
+       /* Need to age node in existing graph*/
+       summarizeInGraph(graph, newDelta, node);
+      }
+    }
+    //Add heap edges in
+    for(Map.Entry<AllocNode, MySet<Edge>> entry:delta.heapedgeadd.entrySet()) {
+      for(Edge e:entry.getValue()) {
+       boolean addedge=false;
+       Edge edgetoadd=null;
+       if (e.statuspredicate==Edge.NEW) {
+         edgetoadd=e;
+       } else {
+         Edge origEdgeKey=e.makeStatus(allocFactory);
+         if (oldgraph.nodeMap.containsKey(origEdgeKey.src)&&
+             oldgraph.nodeMap.get(origEdgeKey.src).contains(origEdgeKey)) {
+           Edge origEdge=oldgraph.nodeMap.get(origEdgeKey.src).get(origEdgeKey);
+           //copy the predicate
+           origEdgeKey.statuspredicate=origEdge.statuspredicate;
+           edgetoadd=origEdgeKey;
+         }
+       }
+       mergeEdge(graph, newDelta, edgetoadd);
+      }
+    }
+    //Add external edges in
+    for(Edge e:graph.externalEdgeSet) {
+      //First did we age the source
+      Edge newedge=e.copy();
+      if (newedge.src!=null&&!e.src.isSummary()&&graph.callNodeAges.contains(e.src)) {
+       AllocNode summaryNode=allocFactory.getAllocNode(newedge.src, true);
+       newedge.src=summaryNode;
+      }
+      //Compute target
+      if (graph.callNodeAges.contains(e.dst)&&!e.dst.isSummary()) {
+       if (graph.callOldNodes.contains(e.dst)) {
+         //Need two edges
+         Edge copy=newedge.copy();
+         mergeEdge(graph, newDelta, copy);
+       }
+       //Now add summarized node
+       newedge.dst=allocFactory.getAllocNode(newedge.dst, true);
+       mergeEdge(graph, newDelta, newedge);
+      } else {
+       //Add edge to single node
+       mergeEdge(graph, newDelta, newedge);
+      }
+    }
+    //Add edge for return value
+    if (fcall.getReturnTemp()!=null) {
+      MySet<Edge> returnedge=delta.varedgeadd.get(returntmp);
+      if (returnedge!=null)
+       for(Edge e:returnedge) {
+         Edge newedge=e.copy();
+         newedge.srcvar=fcall.getReturnTemp();
+         if (graph.getEdges(fcall.getReturnTemp())==null||!graph.getEdges(fcall.getReturnTemp()).contains(newedge))
+           newDelta.addEdge(newedge);
+       }
+    }
+    applyDiffs(graph, newDelta);
+    return newDelta;
+  }
+  
+  public void mergeEdge(Graph graph, Delta newDelta, Edge edgetoadd) {
+    if (edgetoadd!=null) {
+      Edge match=graph.getMatch(edgetoadd);
 
+      if (match==null||!match.subsumes(edgetoadd)) {
+       Edge mergededge=edgetoadd.merge(match);
+       newDelta.addEdge(mergededge);
+      }
+    }
+  }
+
+
+  /* Summarizes out of context nodes in graph */
+  void summarizeInGraph(Graph graph, Delta newDelta, AllocNode singleNode) {
+    AllocNode summaryNode=allocFactory.getAllocNode(singleNode, true);
 
-    return null;
+    //Handle outgoing heap edges
+    MySet<Edge> edgeset=graph.getEdges(singleNode);
+
+    for(Edge e:edgeset) {
+      Edge rewrite=e.rewrite(singleNode, summaryNode);
+      //Remove old edge
+      newDelta.removeHeapEdge(e);
+      mergeEdge(graph, newDelta, rewrite);
+    }
+    
+    //Handle incoming edges
+    MySet<Edge> backedges=graph.getBackEdges(singleNode);
+    for(Edge e:backedges) {
+      if (e.dst==singleNode) {
+       //Need to get original edge so that predicate will be correct
+       Edge match=graph.getMatch(e);
+       Edge rewrite=match.rewrite(singleNode, summaryNode);
+       newDelta.removeEdge(match);
+       mergeEdge(graph, newDelta, rewrite);
+      }
+    }
   }
 
   void applyDiffs(Graph graph, Delta delta) {
@@ -625,8 +790,10 @@ public class Pointer {
       } else {
        //Generate diff from parent graph
        MySet<Edge> parentedges=graph.parent.nodeMap.get(node);
-       MySet<Edge> newedgeset=Util.setSubtract(parentedges, edgestoremove);
-       graph.nodeMap.put(node, newedgeset);
+       if (parentedges!=null) {
+         MySet<Edge> newedgeset=Util.setSubtract(parentedges, edgestoremove);
+         graph.nodeMap.put(node, newedgeset);
+       }
       }
     }
 
@@ -637,7 +804,10 @@ public class Pointer {
       //If we have not done a subtract, then 
       if (!graph.nodeMap.containsKey(node)) {
        //Copy the parent entry
-       graph.nodeMap.put(node, (MySet<Edge>)graph.parent.nodeMap.get(node).clone());
+       if (graph.parent.nodeMap.containsKey(node))
+         graph.nodeMap.put(node, (MySet<Edge>)graph.parent.nodeMap.get(node).clone());
+       else
+         graph.nodeMap.put(node, new MySet<Edge>());
       }
       graph.nodeMap.get(node).addAll(edgestoadd);
       if (genbackwards) {
@@ -657,7 +827,7 @@ public class Pointer {
       if (graph.varMap.containsKey(tmp)) {
        //Just apply diff to current map
        graph.varMap.get(tmp).removeAll(edgestoremove);
-      } else {
+      } else if (graph.parent.varMap.containsKey(tmp)) {
        //Generate diff from parent graph
        MySet<Edge> parentedges=graph.parent.varMap.get(tmp);
        MySet<Edge> newedgeset=Util.setSubtract(parentedges, edgestoremove);
@@ -709,12 +879,14 @@ public class Pointer {
     if (delta.getInit()) {
       HashSet<AllocNode> srcNodes=GraphManip.getNodes(graph, delta, src);
       HashSet<AllocNode> dstNodes=GraphManip.getNodes(graph, delta, dst);
-      MySet<Edge> edgesToAdd=GraphManip.genEdges(srcNodes, fd, dstNodes);
+      MySet<Edge> edgesToAdd=GraphManip.genEdges(dstNodes, fd, srcNodes);
       MySet<Edge> edgesToRemove=null;
-      if (dstNodes.size()==1&&!dstNodes.iterator().next().isSummary()) {
+      if (dstNodes.size()==1&&!dstNodes.iterator().next().isSummary()&&fd!=null) {
        /* Can do a strong update */
        edgesToRemove=GraphManip.getEdges(graph, delta, dstNodes, fd);
-      }
+       graph.strongUpdateSet=edgesToRemove;
+      } else
+       graph.strongUpdateSet=new MySet<Edge>();
       /* Update diff */
       updateHeapDelta(graph, delta, edgesToAdd, edgesToRemove);
       applyDiffs(graph, delta);
@@ -722,23 +894,39 @@ public class Pointer {
       /* First look at new sources */
       MySet<Edge> edgesToAdd=new MySet<Edge>();
       HashSet<AllocNode> newSrcNodes=GraphManip.getDiffNodes(delta, src);
-      HashSet<AllocNode> dstNodes=GraphManip.getDiffNodes(delta, dst);
-      edgesToAdd.addAll(GraphManip.genEdges(newSrcNodes, fd, dstNodes));
+      HashSet<AllocNode> srcNodes=GraphManip.getNodes(graph, delta, src);
+      HashSet<AllocNode> dstNodes=GraphManip.getNodes(graph, delta, dst);
       HashSet<AllocNode> newDstNodes=GraphManip.getDiffNodes(delta, dst);
+
+
       MySet<Edge> edgesToRemove=null;
       if (newDstNodes.size()!=0) {
-       if (dstNodes.size()==1&&!dstNodes.iterator().next().isSummary()) {
+       if (dstNodes.size()>1&&!dstNodes.iterator().next().isSummary()&&fd!=null) {
          /* Need to undo strong update */
          if (graph.strongUpdateSet!=null) {
            edgesToAdd.addAll(graph.strongUpdateSet);
-           graph.strongUpdateSet.clear();
+           graph.strongUpdateSet=null; //Prevent future strong updates
          }
-       } else if (dstNodes.size()==0&&newDstNodes.size()==1&&!newDstNodes.iterator().next().isSummary()&&graph.strongUpdateSet==null) {
+       } else if (dstNodes.size()==1&&newDstNodes.size()==1&&!newDstNodes.iterator().next().isSummary()&&graph.strongUpdateSet!=null&&fd!=null) {
          edgesToRemove=GraphManip.getEdges(graph, delta, dstNodes, fd);
+         graph.strongUpdateSet.addAll(edgesToRemove);
        }
-       HashSet<AllocNode> srcNodes=GraphManip.getDiffNodes(delta, src);
-       edgesToAdd.addAll(GraphManip.genEdges(srcNodes, fd, newDstNodes));
+       edgesToAdd.addAll(GraphManip.genEdges(newDstNodes, fd, srcNodes));
       }
+
+      //Kill new edges
+      if (graph.strongUpdateSet!=null&&fd!=null) {
+       MySet<Edge> otherEdgesToRemove=GraphManip.getDiffEdges(delta, dstNodes);
+       if (edgesToRemove!=null)
+         edgesToRemove.addAll(otherEdgesToRemove);
+       else
+         edgesToRemove=otherEdgesToRemove;
+       graph.strongUpdateSet.addAll(otherEdgesToRemove);
+      }
+
+      //Next look at new destinations
+      edgesToAdd.addAll(GraphManip.genEdges(dstNodes, fd, newSrcNodes));
+
       /* Update diff */
       updateHeapDelta(graph, delta, edgesToAdd, edgesToRemove);
       applyDiffs(graph, delta);
@@ -757,6 +945,11 @@ public class Pointer {
       FlatReturnNode frn=(FlatReturnNode)node;
       src=frn.getReturnTemp();
       dst=returntmp;
+      if (src==null||!src.getType().isPtr()) {
+       //This is a NOP
+       applyDiffs(graph, delta);
+       return delta;
+      }
     } else {
       FlatCastNode fcn=(FlatCastNode) node;
       src=fcn.getSrc();
@@ -764,7 +957,7 @@ public class Pointer {
     }
     if (delta.getInit()) {
       HashSet<AllocNode> srcnodes=GraphManip.getNodes(graph, delta, src);
-      MySet<Edge> edgesToAdd=GraphManip.genEdges(src, srcnodes);
+      MySet<Edge> edgesToAdd=GraphManip.genEdges(dst, srcnodes);
       MySet<Edge> edgesToRemove=GraphManip.getEdges(graph, delta, dst);
       updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
       applyDiffs(graph, delta);
@@ -773,7 +966,7 @@ public class Pointer {
       HashSet<AllocNode> newSrcNodes=GraphManip.getDiffNodes(delta, src);
 
       /* Compute the union, and then the set of edges */
-      MySet<Edge> edgesToAdd=GraphManip.genEdges(src, newSrcNodes);
+      MySet<Edge> edgesToAdd=GraphManip.genEdges(dst, newSrcNodes);
       
       /* Compute set of edges to remove */
       MySet<Edge> edgesToRemove=GraphManip.getDiffEdges(delta, dst);      
@@ -803,7 +996,7 @@ public class Pointer {
     if (delta.getInit()) {
       HashSet<AllocNode> srcnodes=GraphManip.getNodes(graph, delta, src);
       HashSet<AllocNode> fdnodes=GraphManip.getNodes(graph, delta, srcnodes, fd);
-      MySet<Edge> edgesToAdd=GraphManip.genEdges(src, fdnodes);
+      MySet<Edge> edgesToAdd=GraphManip.genEdges(dst, fdnodes);
       MySet<Edge> edgesToRemove=GraphManip.getEdges(graph, delta, dst);
       updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
       applyDiffs(graph, delta);
@@ -818,7 +1011,7 @@ public class Pointer {
       HashSet<AllocNode> newTargets=new HashSet<AllocNode>();
       newTargets.addAll(newfdnodes);
       newTargets.addAll(difffdnodes);
-      MySet<Edge> edgesToAdd=GraphManip.genEdges(src, newTargets);      
+      MySet<Edge> edgesToAdd=GraphManip.genEdges(dst, newTargets);      
       
       /* Compute set of edges to remove */
       MySet<Edge> edgesToRemove=GraphManip.getDiffEdges(delta, dst);      
@@ -836,45 +1029,49 @@ public class Pointer {
     MySet<Edge> existingEdges=graph.getEdges(tmp);
     for(Edge e: edgestoRemove) {
       //remove edge from delta
-      edgeAdd.remove(e);
+      if (edgeAdd!=null)
+       edgeAdd.remove(e);
       //if the edge is already in the graph, add an explicit remove to the delta
       if (existingEdges.contains(e))
-       edgeRemove.add(e);
+       delta.removeVarEdge(e);
     }
     for(Edge e: edgestoAdd) {
       //Remove the edge from the remove set
-      edgeRemove.remove(e);
+      if (edgeRemove!=null)
+       edgeRemove.remove(e);
       //Explicitly add it to the add set unless it is already in the graph
       if (!existingEdges.contains(e))
-       edgeAdd.add(e);
+       delta.addVarEdge(e);
     }
   }
 
   static void updateHeapDelta(Graph graph, Delta delta, MySet<Edge> edgestoAdd, MySet<Edge> edgestoRemove) {
-    for(Edge e: edgestoRemove) {
-      AllocNode src=e.src;
-      MySet<Edge> edgeAdd=delta.heapedgeadd.get(src);
-      MySet<Edge> existingEdges=graph.getEdges(src);
-      //remove edge from delta
-      edgeAdd.remove(e);
-      //if the edge is already in the graph, add an explicit remove to the delta
-      if (existingEdges.contains(e)) {
-       MySet<Edge> edgeRemove=delta.heapedgeremove.get(src);
-       edgeRemove.add(e);
-      }
-    }
-    for(Edge e: edgestoAdd) {
-      AllocNode src=e.src;
-      MySet<Edge> edgeRemove=delta.heapedgeremove.get(src);
-      MySet<Edge> existingEdges=graph.getEdges(src);
-      //Remove the edge from the remove set
-      edgeRemove.remove(e);
-      //Explicitly add it to the add set unless it is already in the graph
-      if (!existingEdges.contains(e)) {
+    if (edgestoRemove!=null)
+      for(Edge e: edgestoRemove) {
+       AllocNode src=e.src;
        MySet<Edge> edgeAdd=delta.heapedgeadd.get(src);
-       edgeAdd.add(e);
+       MySet<Edge> existingEdges=graph.getEdges(src);
+       //remove edge from delta
+       if (edgeAdd!=null)
+         edgeAdd.remove(e);
+       //if the edge is already in the graph, add an explicit remove to the delta
+       if (existingEdges.contains(e)) {
+         delta.removeHeapEdge(e);
+       }
+      }
+    if (edgestoAdd!=null)
+      for(Edge e: edgestoAdd) {
+       AllocNode src=e.src;
+       MySet<Edge> edgeRemove=delta.heapedgeremove.get(src);
+       MySet<Edge> existingEdges=graph.getEdges(src);
+       //Remove the edge from the remove set
+       if (edgeRemove!=null)
+         edgeRemove.remove(e);
+       //Explicitly add it to the add set unless it is already in the graph
+       if (!existingEdges.contains(e)||!existingEdges.get(e).isNew()) {
+         delta.addHeapEdge(e);
+       }
       }
-    }
   }
 
   Delta processFlatNop(FlatNode node, Delta delta, Graph graph) {
@@ -901,7 +1098,7 @@ public class Pointer {
       //Add it into the diffs
       delta.varedgeadd.put(tmp, newedges);
       //Remove the old edges
-      delta.varedgeremove.put(tmp, graph.getEdges(tmp));
+      delta.varedgeremove.put(tmp, (MySet<Edge>) graph.getEdges(tmp).clone());
       //Apply incoming diffs to graph
       applyDiffs(graph, delta);
       //Note that we create a single node
@@ -1044,7 +1241,8 @@ public class Pointer {
          removeSet=new MySet<Edge>();
        }
 
-       removeSet.add(e.copy());
+       removeSet.add(e);
+       e=e.copy();
        if (e.dst==oldnode)
          e.dst=newnode;
        if (e.src==oldnode)
@@ -1092,6 +1290,7 @@ public class Pointer {
       mergeVarEdges(graph, delta, newdelta);
       mergeAges(graph, delta, newdelta);
     }
+
     return newdelta;
   }
 
@@ -1122,6 +1321,13 @@ public class Pointer {
          //We have a new edge
          diffedges.add(e);
          dstedges.add(e);
+       } else {
+         Edge origedge=dstedges.get(e);
+         if (!origedge.subsumes(e)) {
+           Edge mergededge=origedge.merge(e);
+           diffedges.add(mergededge);
+           dstedges.add(mergededge);
+         }
        }
       }
       //Done with edge set...
@@ -1161,7 +1367,7 @@ public class Pointer {
        }
       }
       //Done with edge set...
-      if (diffedges.size()>=0) {
+      if (diffedges.size()>0) {
        //completely new
        newdelta.basevaredge.put(tmpsrc,diffedges);
       }