running on some benchmarks....
[IRC.git] / Robust / src / Analysis / Pointer / Pointer.java
index 4afeae91e9b977f58265ef40d07775caedb85c88..98e0512aa83e5fa7ae6617ad5b81dc72869633a9 100644 (file)
@@ -57,13 +57,13 @@ public class Pointer {
 
   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));
        }
@@ -87,33 +87,36 @@ public class Pointer {
     }
 
     //DEBUG
-    int debugindex=0;
-    for(Map.Entry<BBlock, Graph> e:bbgraphMap.entrySet()) {
-      Graph g=e.getValue();
-      try {
-       PrintWriter pw=new PrintWriter(new FileWriter("BB"+debugindex+".dot"));
-       g.printGraph(pw, "BB");
-       pw.close();
-      } catch (Exception ex) {
-       ex.printStackTrace();
+    if (false) {
+      int debugindex=0;
+      for(Map.Entry<BBlock, Graph> e:bbgraphMap.entrySet()) {
+       Graph g=e.getValue();
+       plotGraph(g,"BB"+debugindex);
+       debugindex++;
       }
-      debugindex++;
-    }
-
-    for(Map.Entry<FlatNode, Graph> e:graphMap.entrySet()) {
-      FlatNode fn=e.getKey();
-      Graph g=e.getValue();
-      try {
-       PrintWriter pw=new PrintWriter(new FileWriter("FN"+fn.toString().replace(' ','_')+".dot"));
-       g.printGraph(pw, fn.toString());
-       pw.close();
-      } catch (Exception ex) {
-       ex.printStackTrace();
+      
+      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();
       }
-      debugindex++;
     }
+  }
 
+  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
@@ -183,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>();
@@ -197,7 +203,7 @@ 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 */
        MySet<Edge> removeset=delta.heapedgeremove.get(node);
 
@@ -241,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);
+         }
        }
       }
     }
@@ -269,12 +293,13 @@ 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);
     }
@@ -401,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);
@@ -468,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));
       
@@ -495,7 +523,6 @@ public class Pointer {
       delta.removeEdge(e);
     }
   }
-  
 
   Delta processFlatCall(BBlock callblock, int callindex, FlatCall fcall, Delta delta, Graph graph) {
     Delta newDelta=new Delta(null, false);
@@ -605,7 +632,7 @@ public class Pointer {
     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();
@@ -619,7 +646,6 @@ public class Pointer {
        summarizeInGraph(graph, newDelta, node);
       }
     }
-    
     //Add heap edges in
     for(Map.Entry<AllocNode, MySet<Edge>> entry:delta.heapedgeadd.entrySet()) {
       for(Edge e:entry.getValue()) {
@@ -640,7 +666,6 @@ public class Pointer {
        mergeEdge(graph, newDelta, edgetoadd);
       }
     }
-
     //Add external edges in
     for(Edge e:graph.externalEdgeSet) {
       //First did we age the source
@@ -666,15 +691,15 @@ public class Pointer {
     }
     //Add edge for return value
     if (fcall.getReturnTemp()!=null) {
-      MySet<Edge> returnedge=newDelta.varedgeadd.get(returntmp);
-      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);
-      }
+      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;
   }
@@ -685,7 +710,7 @@ public class Pointer {
 
       if (match==null||!match.subsumes(edgetoadd)) {
        Edge mergededge=edgetoadd.merge(match);
-       newDelta.addHeapEdge(mergededge);
+       newDelta.addEdge(mergededge);
       }
     }
   }
@@ -765,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);
+       }
       }
     }
 
@@ -852,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 (srcNodes.size()==1&&!srcNodes.iterator().next().isSummary()) {
+      if (dstNodes.size()==1&&!dstNodes.iterator().next().isSummary()&&fd!=null) {
        /* Can do a strong update */
-       edgesToRemove=GraphManip.getEdges(graph, delta, srcNodes, fd);
-      }
+       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);
@@ -871,21 +900,32 @@ public class Pointer {
 
 
       MySet<Edge> edgesToRemove=null;
-      if (newSrcNodes.size()!=0) {
-       if (srcNodes.size()==1&&!srcNodes.iterator().next().isSummary()) {
+      if (newDstNodes.size()!=0) {
+       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 (srcNodes.size()==0&&newSrcNodes.size()==1&&!newSrcNodes.iterator().next().isSummary()&&graph.strongUpdateSet==null) {
-         edgesToRemove=GraphManip.getEdges(graph, delta, srcNodes, fd);
+       } 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);
        }
-       edgesToAdd.addAll(GraphManip.genEdges(newSrcNodes, fd, dstNodes));
+       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(srcNodes, fd, newDstNodes));
+      edgesToAdd.addAll(GraphManip.genEdges(dstNodes, fd, newSrcNodes));
 
       /* Update diff */
       updateHeapDelta(graph, delta, edgesToAdd, edgesToRemove);
@@ -905,7 +945,7 @@ public class Pointer {
       FlatReturnNode frn=(FlatReturnNode)node;
       src=frn.getReturnTemp();
       dst=returntmp;
-      if (src==null) {
+      if (src==null||!src.getType().isPtr()) {
        //This is a NOP
        applyDiffs(graph, delta);
        return delta;
@@ -917,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);
@@ -926,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);      
@@ -956,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);
@@ -971,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);      
@@ -1012,7 +1052,8 @@ public class Pointer {
        MySet<Edge> edgeAdd=delta.heapedgeadd.get(src);
        MySet<Edge> existingEdges=graph.getEdges(src);
        //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)) {
          delta.removeHeapEdge(e);
@@ -1057,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
@@ -1200,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)
@@ -1248,6 +1290,7 @@ public class Pointer {
       mergeVarEdges(graph, delta, newdelta);
       mergeAges(graph, delta, newdelta);
     }
+
     return newdelta;
   }
 
@@ -1324,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);
       }