changes: generated annotated code but it still causes type errors + re-formatting...
[IRC.git] / Robust / src / Analysis / SSJava / FlowGraph.java
index 44a859ba84e9480e743bc08d95c33d2b93a296f9..c2a8f8ecc98f1b34288a25729b7bc5a739e43966 100644 (file)
@@ -15,29 +15,34 @@ import IR.FieldDescriptor;
 import IR.MethodDescriptor;
 import IR.NameDescriptor;
 import IR.VarDescriptor;
+import IR.Tree.MethodInvokeNode;
 
 public class FlowGraph {
 
   MethodDescriptor md;
 
-  Set<FlowNode> nodeSet;
   Set<FlowNode> returnNodeSet;
   FlowNode thisVarNode;
 
+  Map<FlowNode, Set<FlowEdge>> mapFlowNodeToInEdgeSet;
+  Map<FlowNode, Set<FlowEdge>> mapFlowNodeToOutEdgeSet;
+
   Map<NTuple<Location>, FlowNode> mapLocTupleToFlowNode;
   Map<FlowNode, NTuple<Location>> mapFlowNodeToLocTuple;
 
   // maps the composite representation of field/var descriptors to infer nodes
   Map<NTuple<Descriptor>, FlowNode> mapDescTupleToInferNode;
 
-  // maps an infer node to the set of neighbors which is pointed by the node
-  Map<NTuple<Descriptor>, Set<FlowNode>> mapNodeToNeighborSet;
-
   // maps a paramter descriptor to its index
   Map<Descriptor, Integer> mapParamDescToIdx;
 
+  // DS for the lattice generation
   Map<Integer, FlowNode> mapIdxToFlowNode;
 
+  Map<MethodInvokeNode, FlowReturnNode> mapMethodInvokeNodeToFlowReturnNode;
+
+  Map<Descriptor, ClassDescriptor> mapIntersectionDescToEnclosingDescriptor;
+
   public static int interseed = 0;
 
   boolean debug = true;
@@ -46,13 +51,15 @@ public class FlowGraph {
     this.md = md;
     this.mapFlowNodeToLocTuple = new HashMap<FlowNode, NTuple<Location>>();
     this.mapLocTupleToFlowNode = new HashMap<NTuple<Location>, FlowNode>();
-    this.nodeSet = new HashSet<FlowNode>();
     this.mapDescTupleToInferNode = new HashMap<NTuple<Descriptor>, FlowNode>();
-    this.mapNodeToNeighborSet = new HashMap<NTuple<Descriptor>, Set<FlowNode>>();
     this.mapParamDescToIdx = new HashMap<Descriptor, Integer>();
     this.mapParamDescToIdx.putAll(mapParamDescToIdx);
     this.returnNodeSet = new HashSet<FlowNode>();
     this.mapIdxToFlowNode = new HashMap<Integer, FlowNode>();
+    this.mapFlowNodeToOutEdgeSet = new HashMap<FlowNode, Set<FlowEdge>>();
+    this.mapFlowNodeToInEdgeSet = new HashMap<FlowNode, Set<FlowEdge>>();
+    this.mapMethodInvokeNodeToFlowReturnNode = new HashMap<MethodInvokeNode, FlowReturnNode>();
+    this.mapIntersectionDescToEnclosingDescriptor = new HashMap<Descriptor, ClassDescriptor>();
 
     if (!md.isStatic()) {
       // create a node for 'this' varialbe
@@ -70,6 +77,39 @@ public class FlowGraph {
 
   }
 
+  public void addMapInterLocNodeToEnclosingDescriptor(Descriptor interDesc, ClassDescriptor desc) {
+    System.out.println("##### INTERLOC=" + interDesc + "    enclosing desc=" + desc);
+    mapIntersectionDescToEnclosingDescriptor.put(interDesc, desc);
+  }
+
+  public ClassDescriptor getEnclosingDescriptor(Descriptor interDesc) {
+    return mapIntersectionDescToEnclosingDescriptor.get(interDesc);
+  }
+
+  public Map<NTuple<Descriptor>, FlowNode> getMapDescTupleToInferNode() {
+    return mapDescTupleToInferNode;
+  }
+
+  public void setMapDescTupleToInferNode(Map<NTuple<Descriptor>, FlowNode> in) {
+    this.mapDescTupleToInferNode.putAll(in);
+  }
+
+  public Map<NTuple<Location>, FlowNode> getMapLocTupleToFlowNode() {
+    return mapLocTupleToFlowNode;
+  }
+
+  public void setMapLocTupleToFlowNode(Map<NTuple<Location>, FlowNode> in) {
+    this.mapLocTupleToFlowNode.putAll(in);
+  }
+
+  public void setReturnNodeSet(Set<FlowNode> in) {
+    this.returnNodeSet.addAll(in);
+  }
+
+  public void setThisVarNode(FlowNode thisVarNode) {
+    this.thisVarNode = thisVarNode;
+  }
+
   public Map<Descriptor, Integer> getMapParamDescToIdx() {
     return mapParamDescToIdx;
   }
@@ -84,13 +124,32 @@ public class FlowGraph {
     newNode.setIntermediate(true);
 
     mapDescTupleToInferNode.put(tuple, newNode);
-    nodeSet.add(newNode);
+    // nodeSet.add(newNode);
 
     System.out.println("create new intermediate node= " + newNode);
 
     return newNode;
   }
 
+  public FlowReturnNode getFlowReturnNode(MethodInvokeNode min) {
+    return mapMethodInvokeNodeToFlowReturnNode.get(min);
+  }
+
+  public FlowReturnNode createReturnNode(MethodInvokeNode min) {
+    NTuple<Descriptor> tuple = new NTuple<Descriptor>();
+    NameDescriptor n = new NameDescriptor("RETURNLOC" + (LocationInference.locSeed++));
+    tuple.add(n);
+
+    FlowReturnNode newNode = new FlowReturnNode(tuple, min);
+    mapDescTupleToInferNode.put(tuple, newNode);
+    mapMethodInvokeNodeToFlowReturnNode.put(min, newNode);
+    // nodeSet.add(newNode);
+
+    System.out.println("create new set node= " + newNode);
+
+    return newNode;
+  }
+
   private void setupMapIdxToDesc() {
 
     Set<Descriptor> descSet = mapParamDescToIdx.keySet();
@@ -114,22 +173,32 @@ public class FlowGraph {
     return mapIdxToFlowNode.get(idx);
   }
 
-  public Set<FlowNode> getNodeSet() {
-    return nodeSet;
+  public Set<FlowEdge> getEdgeSet() {
+    Set<FlowEdge> edgeSet = new HashSet<FlowEdge>();
+
+    Set<FlowNode> nodeSet = getNodeSet();
+    for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
+      FlowNode flowNode = (FlowNode) iterator.next();
+      edgeSet.addAll(getOutEdgeSet(flowNode));
+    }
+
+    return edgeSet;
   }
 
-  public MethodDescriptor getMethodDescriptor() {
-    return md;
+  public Set<FlowNode> getParamFlowNodeSet() {
+    Set<FlowNode> setParamFlowNode = new HashSet<FlowNode>();
+    setParamFlowNode.addAll(mapIdxToFlowNode.values());
+    return setParamFlowNode;
   }
 
-  public void addNeighbor(FlowNode node, FlowNode neighbor) {
-    Set<FlowNode> set = mapNodeToNeighborSet.get(node);
-    if (set == null) {
-      set = new HashSet<FlowNode>();
-    }
-    set.add(neighbor);
+  public Set<FlowNode> getNodeSet() {
+    Set<FlowNode> set = new HashSet<FlowNode>();
+    set.addAll(mapDescTupleToInferNode.values());
+    return set;
+  }
 
-    // System.out.println("add a new neighbor " + neighbor + " to " + node);
+  public MethodDescriptor getMethodDescriptor() {
+    return md;
   }
 
   public boolean isParamDesc(Descriptor desc) {
@@ -150,7 +219,7 @@ public class FlowGraph {
     FlowNode fromNode = mapDescTupleToInferNode.get(fromDescTuple);
     FlowNode toNode = mapDescTupleToInferNode.get(toDescTuple);
 
-    Set<FlowEdge> fromNodeOutEdgeSet = fromNode.getOutEdgeSet();
+    Set<FlowEdge> fromNodeOutEdgeSet = getOutEdgeSet(fromNode);
     for (Iterator iterator = fromNodeOutEdgeSet.iterator(); iterator.hasNext();) {
       FlowEdge flowEdge = (FlowEdge) iterator.next();
       if (flowEdge.getDst().equals(toNode)) {
@@ -165,12 +234,50 @@ public class FlowGraph {
     return false;
   }
 
+  public Set<FlowEdge> getOutEdgeSetStartingFrom(FlowNode startNode) {
+
+    Descriptor prefixDesc = startNode.getCurrentDescTuple().get(0);
+
+    // returns the set of edges that share the same prefix of startNode
+    Set<FlowEdge> edgeSet = new HashSet<FlowEdge>();
+
+    for (Iterator<Set<FlowEdge>> iter = mapFlowNodeToOutEdgeSet.values().iterator(); iter.hasNext();) {
+      Set<FlowEdge> nodeEdgeSet = iter.next();
+      for (Iterator<FlowEdge> iter2 = nodeEdgeSet.iterator(); iter2.hasNext();) {
+        FlowEdge edge = iter2.next();
+        if (edge.getInitTuple().get(0).equals(prefixDesc)) {
+          edgeSet.add(edge);
+        }
+      }
+    }
+
+    return edgeSet;
+  }
+
+  public Set<FlowEdge> getOutEdgeSet(FlowNode node) {
+    if (!mapFlowNodeToOutEdgeSet.containsKey(node)) {
+      mapFlowNodeToOutEdgeSet.put(node, new HashSet<FlowEdge>());
+    }
+    return mapFlowNodeToOutEdgeSet.get(node);
+  }
+
+  public Set<FlowEdge> getInEdgeSet(FlowNode node) {
+    if (!mapFlowNodeToInEdgeSet.containsKey(node)) {
+      mapFlowNodeToInEdgeSet.put(node, new HashSet<FlowEdge>());
+    }
+    return mapFlowNodeToInEdgeSet.get(node);
+  }
+
   public void addValueFlowEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
 
     FlowNode fromNode = getFlowNode(fromDescTuple);
     FlowNode toNode = getFlowNode(toDescTuple);
 
-    System.out.println("create an edge from " + fromNode + " to " + toNode);
+    if (toNode.getDescTuple().get(0).equals(LocationInference.LITERALDESC)) {
+      return;
+    }
+
+    // System.out.println("create an edge from " + fromNode + " to " + toNode);
 
     int fromTupleSize = fromDescTuple.size();
     NTuple<Descriptor> curFromTuple = new NTuple<Descriptor>();
@@ -208,11 +315,27 @@ public class FlowGraph {
       NTuple<Descriptor> endTuple) {
 
     FlowEdge edge = new FlowEdge(fromNode, toNode, initTuple, endTuple);
-    fromNode.addOutEdge(edge);
+    addOutEdge(fromNode, edge);
+    addInEdge(toNode, edge);
 
     System.out.println("add a new edge=" + edge);
   }
 
+  private void addInEdge(FlowNode toNode, FlowEdge edge) {
+    getInEdgeSet(toNode).add(edge);
+  }
+
+  private void addOutEdge(FlowNode fromNode, FlowEdge edge) {
+    if (!mapFlowNodeToOutEdgeSet.containsKey(fromNode)) {
+      mapFlowNodeToOutEdgeSet.put(fromNode, new HashSet<FlowEdge>());
+    }
+    mapFlowNodeToOutEdgeSet.get(fromNode).add(edge);
+  }
+
+  public boolean contains(NTuple<Descriptor> descTuple) {
+    return mapDescTupleToInferNode.containsKey(descTuple);
+  }
+
   public FlowNode getFlowNode(NTuple<Descriptor> descTuple) {
     if (!mapDescTupleToInferNode.containsKey(descTuple)) {
       FlowNode node = createNewFlowNode(descTuple);
@@ -227,10 +350,11 @@ public class FlowGraph {
 
   public FlowNode createNewFlowNode(NTuple<Descriptor> tuple) {
 
+    // System.out.println("createNewFlowNode=" + tuple);
     if (!mapDescTupleToInferNode.containsKey(tuple)) {
       FlowNode node = new FlowNode(tuple);
       mapDescTupleToInferNode.put(tuple, node);
-      nodeSet.add(node);
+      // nodeSet.add(node);
 
       mapLocTupleToFlowNode.put(getLocationTuple(node), node);
 
@@ -254,8 +378,6 @@ public class FlowGraph {
     }
 
     FlowNode node = mapDescTupleToInferNode.get(tuple);
-    node.setReturn(true);
-
     returnNodeSet.add(node);
   }
 
@@ -271,21 +393,40 @@ public class FlowGraph {
 
   private void recurLocalReachFlowNodeSet(FlowNode fn, Set<FlowNode> visited) {
 
-    for (Iterator iterator = fn.getOutEdgeSet().iterator(); iterator.hasNext();) {
+    Set<FlowEdge> outEdgeSet = getOutEdgeSet(fn);
+    for (Iterator iterator = outEdgeSet.iterator(); iterator.hasNext();) {
       FlowEdge edge = (FlowEdge) iterator.next();
-      FlowNode dstNode = edge.getDst();
+      FlowNode originalDstNode = edge.getDst();
+
+      Set<FlowNode> dstNodeSet = new HashSet<FlowNode>();
+      if (originalDstNode instanceof FlowReturnNode) {
+        FlowReturnNode rnode = (FlowReturnNode) originalDstNode;
+        Set<NTuple<Descriptor>> rtupleSetFromRNode = rnode.getReturnTupleSet();
+        Set<NTuple<Descriptor>> rtupleSet = getReturnTupleSet(rtupleSetFromRNode);
+        for (Iterator iterator2 = rtupleSet.iterator(); iterator2.hasNext();) {
+          NTuple<Descriptor> rtuple = (NTuple<Descriptor>) iterator2.next();
+          dstNodeSet.add(getFlowNode(rtuple));
+        }
+      } else {
+        dstNodeSet.add(originalDstNode);
+      }
 
-      if (!visited.contains(dstNode)) {
-        visited.add(dstNode);
-        recurLocalReachFlowNodeSet(dstNode, visited);
+      for (Iterator iterator2 = dstNodeSet.iterator(); iterator2.hasNext();) {
+        FlowNode dstNode = (FlowNode) iterator2.next();
+        if (!visited.contains(dstNode)) {
+          visited.add(dstNode);
+          recurLocalReachFlowNodeSet(dstNode, visited);
+        }
       }
+
     }
 
   }
 
   private void getReachFlowNodeSetFrom(FlowNode fn, Set<FlowNode> visited) {
 
-    for (Iterator iterator = fn.getOutEdgeSet().iterator(); iterator.hasNext();) {
+    Set<FlowEdge> outEdgeSet = getOutEdgeSet(fn);
+    for (Iterator iterator = outEdgeSet.iterator(); iterator.hasNext();) {
       FlowEdge edge = (FlowEdge) iterator.next();
 
       if (fn.equals(getFlowNode(edge.getInitTuple()))) {
@@ -305,6 +446,52 @@ public class FlowGraph {
     return set;
   }
 
+  public Set<FlowNode> getReachableSetFrom(NTuple<Descriptor> prefix) {
+    Set<FlowNode> reachableSet = new HashSet<FlowNode>();
+
+    Set<FlowNode> nodeSet = getNodeSet();
+    for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
+      FlowNode originalSrcNode = (FlowNode) iterator.next();
+
+      Set<FlowNode> srcNodeSet = new HashSet<FlowNode>();
+      if (originalSrcNode instanceof FlowReturnNode) {
+        FlowReturnNode rnode = (FlowReturnNode) originalSrcNode;
+        Set<NTuple<Descriptor>> rtupleSetFromRNode = rnode.getReturnTupleSet();
+        Set<NTuple<Descriptor>> rtupleSet = getReturnTupleSet(rtupleSetFromRNode);
+        System.out.println("#rnode=" + rnode + "  rtupleSet=" + rtupleSet);
+        for (Iterator iterator2 = rtupleSet.iterator(); iterator2.hasNext();) {
+          NTuple<Descriptor> rtuple = (NTuple<Descriptor>) iterator2.next();
+          if (rtuple.startsWith(prefix)) {
+            // System.out.println("rtuple=" + rtuple + "   give it to recur=" + originalSrcNode);
+            recurReachableSetFrom(originalSrcNode, reachableSet);
+          }
+        }
+      } else {
+        if (originalSrcNode.getCurrentDescTuple().startsWith(prefix)) {
+          recurReachableSetFrom(originalSrcNode, reachableSet);
+        }
+      }
+
+    }
+
+    return reachableSet;
+  }
+
+  public Set<NTuple<Descriptor>> getReturnTupleSet(Set<NTuple<Descriptor>> in) {
+
+    Set<NTuple<Descriptor>> normalTupleSet = new HashSet<NTuple<Descriptor>>();
+    for (Iterator iterator2 = in.iterator(); iterator2.hasNext();) {
+      NTuple<Descriptor> tuple = (NTuple<Descriptor>) iterator2.next();
+      FlowNode tupleNode = getFlowNode(tuple);
+      if (tupleNode instanceof FlowReturnNode) {
+        normalTupleSet.addAll(getReturnTupleSet(((FlowReturnNode) tupleNode).getReturnTupleSet()));
+      } else {
+        normalTupleSet.add(tuple);
+      }
+    }
+    return normalTupleSet;
+  }
+
   // private void getReachFlowNodeSetFrom(FlowNode fn, Set<FlowNode> visited) {
   //
   // for (Iterator iterator = fn.getOutEdgeSet().iterator();
@@ -324,8 +511,25 @@ public class FlowGraph {
   //
   // }
 
+  private void recurReachableSetFrom(FlowNode curNode, Set<FlowNode> reachableSet) {
+
+    Set<FlowEdge> edgeSet = getOutEdgeSet(curNode);
+    for (Iterator iterator = edgeSet.iterator(); iterator.hasNext();) {
+      FlowEdge edge = (FlowEdge) iterator.next();
+      FlowNode dstNode = getFlowNode(edge.getEndTuple());
+      if (!reachableSet.contains(dstNode)) {
+        reachableSet.add(dstNode);
+        recurReachableSetFrom(dstNode, reachableSet);
+      }
+    }
+
+  }
+
   public Set<NTuple<Location>> getReachableFlowTupleSet(Set<NTuple<Location>> visited, FlowNode fn) {
-    for (Iterator iterator = fn.getOutEdgeSet().iterator(); iterator.hasNext();) {
+
+    Set<FlowEdge> outEdgeSet = getOutEdgeSet(fn);
+
+    for (Iterator iterator = outEdgeSet.iterator(); iterator.hasNext();) {
       FlowEdge edge = (FlowEdge) iterator.next();
 
       if (fn.getDescTuple().equals(edge.getInitTuple())) {
@@ -355,11 +559,11 @@ public class FlowGraph {
 
       Descriptor localDesc = fn.getDescTuple().get(0);
 
-      if (fn.isIntermediate()) {
+      if (fn.isIntermediate() && fn.getDescTuple().size() == 1) {
         Location interLoc = new Location(md, localDesc.getSymbol());
         interLoc.setLocDescriptor(localDesc);
         locTuple.add(interLoc);
-      } else if (localDesc.getSymbol().equals(LocationInference.TOPLOC)) {
+      } else if (localDesc.getSymbol().equals(SSJavaAnalysis.TOP)) {
         Location topLoc = new Location(md, Location.TOP);
         topLoc.setLocDescriptor(LocationInference.TOPDESC);
         locTuple.add(topLoc);
@@ -375,11 +579,27 @@ public class FlowGraph {
           if (i == 0) {
             loc = new Location(md, curDesc.getSymbol());
             loc.setLocDescriptor(curDesc);
-            cd = ((VarDescriptor) curDesc).getType().getClassDesc();
+            if (curDesc instanceof VarDescriptor) {
+              cd = ((VarDescriptor) curDesc).getType().getClassDesc();
+            } else if (curDesc instanceof InterDescriptor) {
+              cd = mapIntersectionDescToEnclosingDescriptor.get(curDesc);
+            } else {
+              // otherwise it should be the last element
+              cd = null;
+            }
           } else {
             loc = new Location(cd, curDesc.getSymbol());
             loc.setLocDescriptor(curDesc);
-            cd = ((FieldDescriptor) curDesc).getType().getClassDesc();
+
+            if (curDesc instanceof FieldDescriptor) {
+              cd = ((FieldDescriptor) curDesc).getType().getClassDesc();
+            } else if (curDesc instanceof LocationDescriptor) {
+              cd = ((LocationDescriptor) curDesc).getEnclosingClassDesc();
+            } else {
+              // otherwise it should be the last element of the tuple
+              cd = null;
+            }
+
           }
           locTuple.add(loc);
         }
@@ -398,9 +618,9 @@ public class FlowGraph {
 
   public void getIncomingFlowNodeSet(FlowNode node, Set<FlowNode> visited) {
 
-    for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
+    for (Iterator iterator = getNodeSet().iterator(); iterator.hasNext();) {
       FlowNode curNode = (FlowNode) iterator.next();
-      Set<FlowEdge> edgeSet = curNode.getOutEdgeSet();
+      Set<FlowEdge> edgeSet = getOutEdgeSet(curNode);
 
       for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
         FlowEdge flowEdge = (FlowEdge) iterator2.next();
@@ -408,61 +628,123 @@ public class FlowGraph {
         if (node.equals(getFlowNode(flowEdge.getEndTuple()))) {
           FlowNode incomingNode = getFlowNode(flowEdge.getInitTuple());
 
-          if (!visited.contains(incomingNode)) {
-            visited.add(incomingNode);
-            getIncomingFlowNodeSet(incomingNode, visited);
+          if (incomingNode instanceof FlowReturnNode) {
+            FlowReturnNode rnode = (FlowReturnNode) incomingNode;
+            Set<NTuple<Descriptor>> nodeTupleSet = rnode.getReturnTupleSet();
+            Set<NTuple<Descriptor>> rtupleSet = getReturnTupleSet(nodeTupleSet);
+            for (Iterator iterator3 = rtupleSet.iterator(); iterator3.hasNext();) {
+              NTuple<Descriptor> nodeTuple = (NTuple<Descriptor>) iterator3.next();
+              FlowNode fn = getFlowNode(nodeTuple);
+              if (!visited.contains(fn)) {
+                visited.add(fn);
+                getIncomingFlowNodeSet(fn, visited);
+              }
+            }
+          } else {
+            if (!visited.contains(incomingNode)) {
+              visited.add(incomingNode);
+              getIncomingFlowNodeSet(incomingNode, visited);
+            }
           }
+
         }
       }
     }
 
   }
 
-  public Set<NTuple<Location>> getIncomingFlowTupleSet(FlowNode fn) {
+  public boolean isParameter(NTuple<Descriptor> tuple) {
+    // return true if a descriptor tuple is started with a parameter descriptor
+    Descriptor firstIdxDesc = tuple.get(0);
+    return mapParamDescToIdx.containsKey(firstIdxDesc);
+  }
 
-    NTuple<Descriptor> dstTuple = fn.getDescTuple();
+  public int getParamIdx(NTuple<Descriptor> tuple) {
+    Descriptor firstIdxDesc = tuple.get(0);
+    return mapParamDescToIdx.get(firstIdxDesc);
+  }
 
-    Set<NTuple<Location>> set = new HashSet<NTuple<Location>>();
+  public FlowGraph clone() {
+    FlowGraph clone = new FlowGraph(md, mapParamDescToIdx);
 
-    ClassDescriptor cd = null;
+    // clone.setNodeSet(getNodeSet());
+    clone.setMapLocTupleToFlowNode(getMapLocTupleToFlowNode());
+    clone.setMapFlowNodeToLocTuple(getMapFlowNodeToLocTuple());
+    clone.setMapDescTupleToInferNode(getMapDescTupleToInferNode());
+
+    clone.setMapFlowNodeToOutEdgeSet(getMapFlowNodeToOutEdgeSet());
+    clone.setReturnNodeSet(getReturnNodeSet());
+    clone.setThisVarNode(getThisVarNode());
+
+    return clone;
+  }
+
+  public Map<FlowNode, NTuple<Location>> getMapFlowNodeToLocTuple() {
+    return mapFlowNodeToLocTuple;
+  }
+
+  public void setMapFlowNodeToLocTuple(Map<FlowNode, NTuple<Location>> in) {
+    this.mapFlowNodeToLocTuple.putAll(in);
+  }
+
+  public Map<FlowNode, Set<FlowEdge>> getMapFlowNodeToOutEdgeSet() {
+    return mapFlowNodeToOutEdgeSet;
+  }
+
+  public Set<FlowNode> getIncomingNodeSetByPrefix(Descriptor prefix) {
+
+    Set<FlowNode> incomingNodeSet = new HashSet<FlowNode>();
+
+    for (Iterator iterator = getNodeSet().iterator(); iterator.hasNext();) {
+      FlowNode curNode = (FlowNode) iterator.next();
+      Set<FlowEdge> outEdgeSet = getOutEdgeSet(curNode);
+
+      for (Iterator iterator2 = outEdgeSet.iterator(); iterator2.hasNext();) {
+        FlowEdge outEdge = (FlowEdge) iterator2.next();
+
+        if (outEdge.getEndTuple().startsWith(prefix)) {
+          incomingNodeSet.add(curNode);
+          recurIncomingNodeSetByPrefix(prefix, curNode, incomingNodeSet);
 
-    for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
-      FlowNode node = (FlowNode) iterator.next();
-      Set<FlowEdge> edgeSet = node.getOutEdgeSet();
-      for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
-        FlowEdge flowEdge = (FlowEdge) iterator2.next();
-        if (dstTuple.equals(flowEdge.getEndTuple())) {
-          NTuple<Descriptor> initTuple = flowEdge.getInitTuple();
-          NTuple<Location> locTuple = new NTuple<Location>();
-          for (int i = 0; i < initTuple.size(); i++) {
-            Descriptor d = initTuple.get(i);
-            Location loc;
-            if (i == 0) {
-              loc = new Location(md, d.getSymbol());
-              cd = ((VarDescriptor) d).getType().getClassDesc();
-            } else {
-              loc = new Location(cd, d.getSymbol());
-              cd = ((FieldDescriptor) d).getType().getClassDesc();
-            }
-            locTuple.add(loc);
-          }
-          set.add(locTuple);
         }
+
       }
     }
-    return set;
+
+    return incomingNodeSet;
+
   }
 
-  public boolean isParameter(NTuple<Descriptor> tuple) {
-    // return true if a descriptor tuple is started with a parameter descriptor
-    Descriptor firstIdxDesc = tuple.get(0);
-    return mapParamDescToIdx.containsKey(firstIdxDesc);
+  private void recurIncomingNodeSetByPrefix(Descriptor prefix, FlowNode node, Set<FlowNode> visited) {
+
+    Set<FlowEdge> inEdgeSet = getInEdgeSet(node);
+
+    for (Iterator iterator = inEdgeSet.iterator(); iterator.hasNext();) {
+      FlowEdge inEdge = (FlowEdge) iterator.next();
+
+      FlowNode inNode = getFlowNode(inEdge.getInitTuple());
+      if (!inEdge.getInitTuple().startsWith(prefix) && !visited.contains(inNode)) {
+        visited.add(inNode);
+        recurIncomingNodeSetByPrefix(prefix, inNode, visited);
+      }
+    }
+
+  }
+
+  public void setMapFlowNodeToOutEdgeSet(Map<FlowNode, Set<FlowEdge>> inMap) {
+    Set<FlowNode> keySet = inMap.keySet();
+    for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
+      FlowNode key = (FlowNode) iterator.next();
+      Set<FlowEdge> newEdgeSet = new HashSet<FlowEdge>();
+      newEdgeSet.addAll(inMap.get(key));
+      mapFlowNodeToOutEdgeSet.put(key, newEdgeSet);
+    }
   }
 
   private void drawEdges(FlowNode node, BufferedWriter bw, Set<FlowNode> addedNodeSet,
       Set<FlowEdge> addedEdgeSet) throws IOException {
 
-    Set<FlowEdge> edgeSet = node.getOutEdgeSet();
+    Set<FlowEdge> edgeSet = getOutEdgeSet(node);
 
     for (Iterator<FlowEdge> iterator = edgeSet.iterator(); iterator.hasNext();) {
       FlowEdge flowEdge = iterator.next();
@@ -495,12 +777,22 @@ public class FlowGraph {
   }
 
   private void drawNode(FlowNode node, BufferedWriter bw) throws IOException {
-    bw.write(node.getID() + " [label=\"" + node.getPrettyID() + "\"]" + ";\n");
+    if (node instanceof FlowReturnNode) {
+      FlowReturnNode rnode = (FlowReturnNode) node;
+      bw.write(node.getID() + " [label=\"" + node.getPrettyID() + "\"]" + ";\n");
+    } else {
+      bw.write(node.getID() + " [label=\"" + node.getPrettyID() + "\"]" + ";\n");
+    }
+
   }
 
   public void writeGraph() throws java.io.IOException {
+    writeGraph("");
+  }
 
-    String graphName = "flowgraph_" + md.toString();
+  public void writeGraph(String suffix) throws java.io.IOException {
+
+    String graphName = "flowgraph_" + md.toString() + suffix;
     graphName = graphName.replaceAll("[\\W]", "");
 
     BufferedWriter bw = new BufferedWriter(new FileWriter(graphName + ".dot"));
@@ -509,7 +801,8 @@ public class FlowGraph {
 
     // then visit every flow node
 
-    Iterator<FlowNode> iter = nodeSet.iterator();
+    // Iterator<FlowNode> iter = nodeSet.iterator();
+    Iterator<FlowNode> iter = getNodeSet().iterator();
 
     Set<FlowEdge> addedEdgeSet = new HashSet<FlowEdge>();
     Set<FlowNode> addedNodeSet = new HashSet<FlowNode>();
@@ -533,7 +826,7 @@ public class FlowGraph {
   }
 
   public boolean constainsNode(FlowNode node) {
-    return nodeSet.contains(node);
+    return getNodeSet().contains(node);
   }
 
   private void drawSubgraph(FlowNode node, BufferedWriter bw, Set<FlowEdge> addedSet)
@@ -565,4 +858,5 @@ public class FlowGraph {
 
     bw.write("}\n");
   }
+
 }
\ No newline at end of file