Add special case handling for calloc and realloc
[oota-llvm.git] / lib / Analysis / DataStructure / DataStructure.cpp
index fe38152f31cbc68df4582ff2ba5be6c8bea55293..0a6fa57aa07e4b145c00babf2ae5c4a45b52806a 100644 (file)
@@ -10,6 +10,7 @@
 #include "llvm/DerivedTypes.h"
 #include "llvm/Target/TargetData.h"
 #include "llvm/Assembly/Writer.h"
+#include "Support/Debug.h"
 #include "Support/STLExtras.h"
 #include "Support/Statistic.h"
 #include "Support/Timer.h"
@@ -708,7 +709,7 @@ void DSNode::mergeWith(const DSNodeHandle &NH, unsigned Offset) {
 
 // Define here to avoid including iOther.h and BasicBlock.h in DSGraph.h
 Function &DSCallSite::getCaller() const {
-  return *Inst->getParent()->getParent();
+  return *Site.getInstruction()->getParent()->getParent();
 }
 
 
@@ -959,7 +960,7 @@ void DSGraph::cloneInto(const DSGraph &G, ScalarMapTy &OldValMap,
   }
 
   if (!(CloneFlags & DontCloneAuxCallNodes)) {
-    // Copy the auxillary function calls list...
+    // Copy the auxiliary function calls list...
     unsigned FC = AuxFunctionCalls.size();  // FirstCall
     AuxFunctionCalls.reserve(FC+G.AuxFunctionCalls.size());
     for (unsigned i = 0, ei = G.AuxFunctionCalls.size(); i != ei; ++i)
@@ -1043,7 +1044,7 @@ DSCallSite DSGraph::getCallSiteForArguments(Function &F) const {
     if (isPointerType(I->getType()))
       Args.push_back(getScalarMap().find(I)->second);
 
-  return DSCallSite(*(CallInst*)0, getReturnNodeFor(F), &F, Args);
+  return DSCallSite(CallSite(), getReturnNodeFor(F), &F, Args);
 }
 
 
@@ -1061,7 +1062,7 @@ static void markIncompleteNode(DSNode *N) {
   // Actually mark the node
   N->setIncompleteMarker();
 
-  // Recusively process children...
+  // Recursively process children...
   for (unsigned i = 0, e = N->getSize(); i < e; i += DS::PointerSize)
     if (DSNode *DSN = N->getLink(i).getNode())
       markIncompleteNode(DSN);
@@ -1385,7 +1386,7 @@ void DSGraph::removeDeadNodes(unsigned Flags) {
   // merging...
   removeTriviallyDeadNodes();
 
-  // FIXME: Merge nontrivially identical call nodes...
+  // FIXME: Merge non-trivially identical call nodes...
 
   // Alive - a set that holds all nodes found to be reachable/alive.
   hash_set<DSNode*> Alive;
@@ -1529,9 +1530,9 @@ void DSGraph::removeDeadNodes(unsigned Flags) {
   GlobalNodeMap.clear();
   GlobalsGraph->removeTriviallyDeadNodes();
 
-  // At this point, any nodes which are visited, but not alive, are nodes which
-  // should be moved to the globals graph.  Loop over all nodes, eliminating
-  // completely unreachable nodes, and moving visited nodes to the globals graph
+  // At this point, any nodes which are visited, but not alive, are nodes
+  // which can be removed.  Loop over all nodes, eliminating completely
+  // unreachable nodes.
   //
   std::vector<DSNode*> DeadNodes;
   DeadNodes.reserve(Nodes.size());
@@ -1581,3 +1582,31 @@ void DSGraph::AssertGraphOK() const {
   AssertAuxCallNodesInGraph();
 }
 
+/// mergeInGlobalsGraph - This method is useful for clients to incorporate the
+/// globals graph into the DS, BU or TD graph for a function.  This code retains
+/// all globals, i.e., does not delete unreachable globals after they are
+/// inlined.
+///
+void DSGraph::mergeInGlobalsGraph() {
+  NodeMapTy GlobalNodeMap;
+  ScalarMapTy OldValMap;
+  ReturnNodesTy OldRetNodes;
+  cloneInto(*GlobalsGraph, OldValMap, OldRetNodes, GlobalNodeMap,
+            DSGraph::KeepAllocaBit | DSGraph::DontCloneCallNodes |
+            DSGraph::DontCloneAuxCallNodes);
+  
+  // Now merge existing global nodes in the GlobalsGraph with their copies
+  for (ScalarMapTy::iterator I = ScalarMap.begin(), E = ScalarMap.end(); 
+       I != E; ++I)
+    if (isa<GlobalValue>(I->first)) {             // Found a global node
+      DSNodeHandle &GH = I->second;
+      DSNodeHandle &GGNodeH = GlobalsGraph->getScalarMap()[I->first];
+      GH.mergeWith(GlobalNodeMap[GGNodeH.getNode()]);
+    }
+  
+  // Merging leaves behind unused nodes: get rid of them now.
+  GlobalNodeMap.clear();
+  OldValMap.clear();
+  OldRetNodes.clear();
+  removeTriviallyDeadNodes();
+}