Add initial support for a globals graph
authorChris Lattner <sabre@nondot.org>
Sat, 9 Nov 2002 20:01:01 +0000 (20:01 +0000)
committerChris Lattner <sabre@nondot.org>
Sat, 9 Nov 2002 20:01:01 +0000 (20:01 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@4656 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Analysis/DSGraph.h
include/llvm/Analysis/DataStructure.h
include/llvm/Analysis/DataStructure/DSGraph.h
include/llvm/Analysis/DataStructure/DataStructure.h
lib/Analysis/DataStructure/DataStructure.cpp
lib/Analysis/DataStructure/Local.cpp

index 26dee954be1f143eb865e5918b95eb02e3c9502c..6afce63e1b412debfea856e6e56b259d064d84eb 100644 (file)
 /// DSGraph - The graph that represents a function.
 ///
 class DSGraph {
-  Function *Func;
+  Function *Func;          // Func - The LLVM function this graph corresponds to
+  DSGraph *GlobalsGraph;   // Pointer to the common graph of global objects
+
+  DSNodeHandle RetNode;    // The node that gets returned...
   std::vector<DSNode*> Nodes;
-  DSNodeHandle RetNode;                          // Node that gets returned...
   std::map<Value*, DSNodeHandle> ScalarMap;
 
-#if 0
-  // GlobalsGraph -- Reference to the common graph of globally visible objects.
-  // This includes GlobalValues, New nodes, Cast nodes, and Calls.
-  // 
-  GlobalDSGraph* GlobalsGraph;
-#endif
-
   // FunctionCalls - This vector maintains a single entry for each call
   // instruction in the current graph.  The first entry in the vector is the
   // scalar that holds the return value for the call, the second is the function
@@ -41,12 +36,17 @@ class DSGraph {
 
   void operator=(const DSGraph &); // DO NOT IMPLEMENT
 public:
-  DSGraph() : Func(0) {}           // Create a new, empty, DSGraph.
-  DSGraph(Function &F);            // Compute the local DSGraph
+  DSGraph() : Func(0), GlobalsGraph(0) {}      // Create a new, empty, DSGraph.
+  DSGraph(Function &F, DSGraph *GlobalsGraph); // Compute the local DSGraph
 
   // Copy ctor - If you want to capture the node mapping between the source and
   // destination graph, you may optionally do this by specifying a map to record
   // this into.
+  //
+  // Note that a copied graph does not retain the GlobalsGraph pointer of the
+  // source.  You need to set a new GlobalsGraph with the setGlobalsGraph
+  // method.
+  //
   DSGraph(const DSGraph &DSG);
   DSGraph(const DSGraph &DSG, std::map<const DSNode*, DSNodeHandle> &NodeMap);
   ~DSGraph();
@@ -54,6 +54,9 @@ public:
   bool hasFunction() const { return Func != 0; }
   Function &getFunction() const { return *Func; }
 
+  DSGraph *getGlobalsGraph() const { return GlobalsGraph; }
+  void setGlobalsGraph(DSGraph *G) { GlobalsGraph = G; }
+
   /// getNodes - Get a vector of all the nodes in the graph
   /// 
   const std::vector<DSNode*> &getNodes() const { return Nodes; }
index 610fa712e8e98964cd0783470db6d63e36cf924c..71bf5128eca6a45cff287178d0781a4ea04e0d26 100644 (file)
@@ -33,6 +33,7 @@ namespace DataStructureAnalysis {
 class LocalDataStructures : public Pass {
   // DSInfo, one graph for each function
   std::map<const Function*, DSGraph*> DSInfo;
+  DSGraph *GlobalsGraph;
 public:
   ~LocalDataStructures() { releaseMemory(); }
 
index 26dee954be1f143eb865e5918b95eb02e3c9502c..6afce63e1b412debfea856e6e56b259d064d84eb 100644 (file)
 /// DSGraph - The graph that represents a function.
 ///
 class DSGraph {
-  Function *Func;
+  Function *Func;          // Func - The LLVM function this graph corresponds to
+  DSGraph *GlobalsGraph;   // Pointer to the common graph of global objects
+
+  DSNodeHandle RetNode;    // The node that gets returned...
   std::vector<DSNode*> Nodes;
-  DSNodeHandle RetNode;                          // Node that gets returned...
   std::map<Value*, DSNodeHandle> ScalarMap;
 
-#if 0
-  // GlobalsGraph -- Reference to the common graph of globally visible objects.
-  // This includes GlobalValues, New nodes, Cast nodes, and Calls.
-  // 
-  GlobalDSGraph* GlobalsGraph;
-#endif
-
   // FunctionCalls - This vector maintains a single entry for each call
   // instruction in the current graph.  The first entry in the vector is the
   // scalar that holds the return value for the call, the second is the function
@@ -41,12 +36,17 @@ class DSGraph {
 
   void operator=(const DSGraph &); // DO NOT IMPLEMENT
 public:
-  DSGraph() : Func(0) {}           // Create a new, empty, DSGraph.
-  DSGraph(Function &F);            // Compute the local DSGraph
+  DSGraph() : Func(0), GlobalsGraph(0) {}      // Create a new, empty, DSGraph.
+  DSGraph(Function &F, DSGraph *GlobalsGraph); // Compute the local DSGraph
 
   // Copy ctor - If you want to capture the node mapping between the source and
   // destination graph, you may optionally do this by specifying a map to record
   // this into.
+  //
+  // Note that a copied graph does not retain the GlobalsGraph pointer of the
+  // source.  You need to set a new GlobalsGraph with the setGlobalsGraph
+  // method.
+  //
   DSGraph(const DSGraph &DSG);
   DSGraph(const DSGraph &DSG, std::map<const DSNode*, DSNodeHandle> &NodeMap);
   ~DSGraph();
@@ -54,6 +54,9 @@ public:
   bool hasFunction() const { return Func != 0; }
   Function &getFunction() const { return *Func; }
 
+  DSGraph *getGlobalsGraph() const { return GlobalsGraph; }
+  void setGlobalsGraph(DSGraph *G) { GlobalsGraph = G; }
+
   /// getNodes - Get a vector of all the nodes in the graph
   /// 
   const std::vector<DSNode*> &getNodes() const { return Nodes; }
index 610fa712e8e98964cd0783470db6d63e36cf924c..71bf5128eca6a45cff287178d0781a4ea04e0d26 100644 (file)
@@ -33,6 +33,7 @@ namespace DataStructureAnalysis {
 class LocalDataStructures : public Pass {
   // DSInfo, one graph for each function
   std::map<const Function*, DSGraph*> DSInfo;
+  DSGraph *GlobalsGraph;
 public:
   ~LocalDataStructures() { releaseMemory(); }
 
index 6380a4967ee02bf88ed1c7f7aa0094d99dd98f73..584031bade3325a2f9e281a378e11aa05d55fc3f 100644 (file)
@@ -501,14 +501,14 @@ Function &DSCallSite::getCaller() const {
 // DSGraph Implementation
 //===----------------------------------------------------------------------===//
 
-DSGraph::DSGraph(const DSGraph &G) : Func(G.Func) {
+DSGraph::DSGraph(const DSGraph &G) : Func(G.Func), GlobalsGraph(0) {
   std::map<const DSNode*, DSNodeHandle> NodeMap;
   RetNode = cloneInto(G, ScalarMap, NodeMap);
 }
 
 DSGraph::DSGraph(const DSGraph &G,
                  std::map<const DSNode*, DSNodeHandle> &NodeMap)
-  : Func(G.Func) {
+  : Func(G.Func), GlobalsGraph(0) {
   RetNode = cloneInto(G, ScalarMap, NodeMap);
 }
 
@@ -1056,28 +1056,6 @@ void DSGraph::maskNodeTypes(unsigned char Mask) {
 // GlobalDSGraph Implementation
 //===----------------------------------------------------------------------===//
 
-GlobalDSGraph::GlobalDSGraph() : DSGraph(*(Function*)0, this) {
-}
-
-GlobalDSGraph::~GlobalDSGraph() {
-  assert(Referrers.size() == 0 &&
-         "Deleting global graph while references from other graphs exist");
-}
-
-void GlobalDSGraph::addReference(const DSGraph* referrer) {
-  if (referrer != this)
-    Referrers.insert(referrer);
-}
-
-void GlobalDSGraph::removeReference(const DSGraph* referrer) {
-  if (referrer != this) {
-    assert(Referrers.find(referrer) != Referrers.end() && "This is very bad!");
-    Referrers.erase(referrer);
-    if (Referrers.size() == 0)
-      delete this;
-  }
-}
-
 #if 0
 // Bits used in the next function
 static const char ExternalTypeBits = DSNode::GlobalNode | DSNode::HeapNode;
index 224ec4ab9c3f99ccb6660477a4f9d8f7c540119d..0c0c6afd8888db63352605e63f2742ea890476a2 100644 (file)
@@ -131,7 +131,7 @@ namespace {
 //===----------------------------------------------------------------------===//
 // DSGraph constructor - Simply use the GraphBuilder to construct the local
 // graph.
-DSGraph::DSGraph(Function &F) : Func(&F) {
+DSGraph::DSGraph(Function &F, DSGraph *GG) : Func(&F), GlobalsGraph(GG) {
   // Use the graph builder to construct the local version of the graph
   GraphBuilder B(*this, Nodes, RetNode, ScalarMap, FunctionCalls);
   markIncompleteNodes();
@@ -416,12 +416,16 @@ void LocalDataStructures::releaseMemory() {
   // Empty map so next time memory is released, data structures are not
   // re-deleted.
   DSInfo.clear();
+  delete GlobalsGraph;
+  GlobalsGraph = 0;
 }
 
 bool LocalDataStructures::run(Module &M) {
+  GlobalsGraph = new DSGraph();
+
   // Calculate all of the graphs...
   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
     if (!I->isExternal())
-      DSInfo.insert(std::make_pair(I, new DSGraph(*I)));
+      DSInfo.insert(std::make_pair(I, new DSGraph(*I, GlobalsGraph)));
   return false;
 }