Add flush


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@4619 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Analysis/DataStructure/DataStructure.cpp b/lib/Analysis/DataStructure/DataStructure.cpp
index 31ff0a9..8d798e3 100644
--- a/lib/Analysis/DataStructure/DataStructure.cpp
+++ b/lib/Analysis/DataStructure/DataStructure.cpp
@@ -17,7 +17,8 @@
 using std::vector;
 
 namespace {
-  Statistic<> NumFolds("dsnode", "Number of nodes completely folded");
+  Statistic<> NumFolds          ("dsnode", "Number of nodes completely folded");
+  Statistic<> NumCallNodesMerged("dsnode", "Number of call nodes merged");
 };
 
 namespace DS {   // TODO: FIXME
@@ -443,7 +444,7 @@
 
   // Merge the node types
   NodeType |= N->NodeType;
-  N->NodeType = 0;   // N is now a dead node.
+  N->NodeType = DEAD;   // N is now a dead node.
 
   // Merge the globals list...
   if (!N->Globals.empty()) {
@@ -497,17 +498,6 @@
 void DSGraph::dump() const { print(std::cerr); }
 
 
-// Helper function used to clone a function list.
-// 
-static void CopyFunctionCallsList(const vector<DSCallSite>& fromCalls,
-                                  vector<DSCallSite> &toCalls,
-                                  std::map<const DSNode*, DSNode*> &NodeMap) {
-  unsigned FC = toCalls.size();  // FirstCall
-  toCalls.reserve(FC+fromCalls.size());
-  for (unsigned i = 0, ei = fromCalls.size(); i != ei; ++i)
-    toCalls.push_back(DSCallSite(fromCalls[i], NodeMap));
-}
-
 /// remapLinks - Change all of the Links in the current node according to the
 /// specified mapping.
 ///
@@ -528,6 +518,7 @@
                                 std::map<const DSNode*, DSNode*> &OldNodeMap,
                                 AllocaBit StripAllocas) {
   assert(OldNodeMap.empty() && "Returned OldNodeMap should be empty!");
+  assert(&G != this && "Cannot clone graph into itself!");
 
   unsigned FN = Nodes.size();           // First new node...
 
@@ -560,14 +551,18 @@
       std::map<Value*, DSNodeHandle>::iterator GVI = ScalarMap.find(I->first);
       if (GVI != ScalarMap.end()) {   // Is the global value in this fn already?
         GVI->second.mergeWith(H);
+        OldNodeMap[I->second.getNode()] = H.getNode();
       } else {
         ScalarMap[I->first] = H;      // Add global pointer to this graph
       }
     }
   }
-  // Copy the function calls list...
-  CopyFunctionCallsList(G.FunctionCalls, FunctionCalls, OldNodeMap);
 
+  // Copy the function calls list...
+  unsigned FC = FunctionCalls.size();  // FirstCall
+  FunctionCalls.reserve(FC+G.FunctionCalls.size());
+  for (unsigned i = 0, ei = G.FunctionCalls.size(); i != ei; ++i)
+    FunctionCalls.push_back(DSCallSite(G.FunctionCalls[i], OldNodeMap));
 
   // Return the returned node pointer...
   return DSNodeHandle(OldNodeMap[G.RetNode.getNode()], G.RetNode.getOffset());
@@ -729,7 +724,8 @@
 //
 bool DSGraph::isNodeDead(DSNode *N) {
   // Is it a trivially dead shadow node...
-  if (N->getReferrers().empty() && N->NodeType == 0)
+  if (N->getReferrers().empty() &&
+      (N->NodeType == 0 || N->NodeType == DSNode::DEAD))
     return true;
 
   // Is it a function node or some other trivially unused global?
@@ -754,6 +750,9 @@
   Calls.erase(std::unique(Calls.begin(), Calls.end()),
               Calls.end());
 
+  // Track the number of call nodes merged away...
+  NumCallNodesMerged += NumFns-Calls.size();
+
   DEBUG(if (NumFns != Calls.size())
           std::cerr << "Merged " << (NumFns-Calls.size())
                     << " call nodes in " << where << "\n";);
@@ -936,7 +935,7 @@
 // inlining graphs.
 //
 void DSGraph::removeDeadNodes(bool KeepAllGlobals, bool KeepCalls) {
-  assert((!KeepAllGlobals || KeepCalls) &&
+  assert((!KeepAllGlobals || KeepCalls) &&  // FIXME: This should be an enum!
          "KeepAllGlobals without KeepCalls is meaningless");
 
   // Reduce the amount of work we have to do...
@@ -961,22 +960,13 @@
          E = ScalarMap.end(); I != E; ++I)
     markAlive(I->second.getNode(), Alive);
 
-#if 0
-  // Marge all nodes reachable by global nodes, as alive.  Isn't this covered by
-  // the ScalarMap?
-  //
-  if (KeepAllGlobals)
-    for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
-      if (Nodes[i]->NodeType & DSNode::GlobalNode)
-        markAlive(Nodes[i], Alive);
-#endif
-
   // The return value is alive as well...
   markAlive(RetNode.getNode(), Alive);
 
   // Mark all globals or cast nodes that can reach a live node as alive.
   // This also marks all nodes reachable from such nodes as alive.
   // Of course, if KeepAllGlobals is specified, they would be live already.
+
   if (!KeepAllGlobals)
     markGlobalsAlive(*this, Alive, !KeepCalls);