Add support for a top-down propagation pass.
authorVikram S. Adve <vadve@cs.uiuc.edu>
Thu, 18 Jul 2002 16:12:08 +0000 (16:12 +0000)
committerVikram S. Adve <vadve@cs.uiuc.edu>
Thu, 18 Jul 2002 16:12:08 +0000 (16:12 +0000)
Each DSGraph now keeps a list of pending callers that have not
been inlined into the function represented by that graph.
It also keeps a copy of the original call nodes before the BU pass
eliminates some of them.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@2965 91177308-0d34-0410-b5e6-96231b3b80d8

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

index d129aab0ad8a09877525b343e6a6e3192f824f15..b4493602c5343204059abfe7c1699695e7465e68 100644 (file)
@@ -24,6 +24,7 @@ class DSGraph;                 // A graph for a function
 class DSNodeIterator;          // Data structure graph traversal iterator
 class LocalDataStructures;     // A collection of local graphs for a program
 class BUDataStructures;        // A collection of bu graphs for a program
+class TDDataStructures;        // A collection of td graphs for a program
 
 //===----------------------------------------------------------------------===//
 // DSNodeHandle - Implement a "handle" to a data structure node that takes care
@@ -196,16 +197,47 @@ class DSGraph {
   // call, the second is the function scalar being invoked, and the rest are
   // pointer arguments to the function.
   //
+  // OrigFunctionCalls - This vector retains a copy of the original function
+  // calls of the current graph.  This is needed to support top-down inlining
+  // after bottom-up inlining is complete, since the latter deletes call nodes.
+  // 
   std::vector<std::vector<DSNodeHandle> > FunctionCalls;
-
+  std::vector<std::vector<DSNodeHandle> > OrigFunctionCalls;
+
+  // PendingCallers - This vector records all unresolved callers of the
+  // current function, i.e., ones whose graphs have not been inlined into
+  // the current graph.  As long as there are unresolved callers, the nodes
+  // for formal arguments in the current graph cannot be eliminated, and
+  // nodes in the graph reachable from the formal argument nodes or
+  // global variable nodes must be considered incomplete. 
+  std::vector<Function*> PendingCallers;
+  
 private:
   // Define the interface only accessable to DataStructure
   friend class LocalDataStructures;
   friend class BUDataStructures;
+  friend class TDDataStructures;
   DSGraph(Function &F);            // Compute the local DSGraph
   DSGraph(const DSGraph &DSG);     // Copy ctor
   ~DSGraph();
 
+  // clone all the call nodes and save the copies in OrigFunctionCalls
+  void saveOrigFunctionCalls() {
+    assert(OrigFunctionCalls.size() == 0 && "Do this only once!");
+    OrigFunctionCalls.reserve(FunctionCalls.size());
+    for (unsigned i = 0, ei = FunctionCalls.size(); i != ei; ++i) {
+      OrigFunctionCalls.push_back(std::vector<DSNodeHandle>());
+      OrigFunctionCalls[i].reserve(FunctionCalls[i].size());
+      for (unsigned j = 0, ej = FunctionCalls[i].size(); j != ej; ++j)
+        OrigFunctionCalls[i].push_back(FunctionCalls[i][j]);
+    }
+  }
+  
+  // get the saved copies of the original function call nodes
+  std::vector<std::vector<DSNodeHandle> >& getOrigFunctionCalls() {
+    return OrigFunctionCalls;
+  }
+
   void operator=(const DSGraph &); // DO NOT IMPLEMENT
 public:
 
@@ -236,7 +268,6 @@ public:
   void maskNodeTypes(unsigned char Mask);
   void maskIncompleteMarkers() { maskNodeTypes(~DSNode::Incomplete); }
 
-
   // markIncompleteNodes - Traverse the graph, identifying nodes that may be
   // modified by other functions that have not been resolved yet.  This marks
   // nodes that are reachable through three sources of "unknownness":
@@ -254,6 +285,18 @@ public:
   //
   void removeDeadNodes();
 
+  // AddCaller - add a known caller node into the graph and mark it pending.
+  // getCallers - get a vector of the functions that call this one
+  // getCallersPending - get a matching vector of bools indicating if each
+  //                     caller's DSGraph has been resolved into this one.
+  // 
+  void addCaller(Function& caller) {
+    PendingCallers.push_back(&caller);
+  }
+  std::vector<Function*>& getPendingCallers() {
+    return PendingCallers;
+  }
+  
   // cloneInto - Clone the specified DSGraph into the current graph, returning
   // the Return node of the graph.  The translated ValueMap for the old function
   // is filled into the OldValMap member.  If StripLocals is set to true, Scalar
@@ -261,6 +304,7 @@ public:
   // into a calling function's graph.
   //
   DSNode *cloneInto(const DSGraph &G, std::map<Value*, DSNodeHandle> &OldValMap,
+                    std::map<const DSNode*, DSNode*>& OldNodeMap,
                     bool StripLocals = true);
 private:
   bool isNodeDead(DSNode *N);
@@ -335,7 +379,7 @@ public:
     assert(I != DSInfo.end() && "Function not in module!");
     return *I->second;
   }
-
+  
   // print - Print out the analysis results...
   void print(std::ostream &O, Module *M) const;
 
@@ -352,4 +396,49 @@ private:
   DSGraph &calculateGraph(Function &F);
 };
 
+
+// TDDataStructures - Analysis that computes new data structure graphs
+// for each function using the closed graphs for the callers computed
+// by the bottom-up pass.
+//
+class TDDataStructures : public Pass {
+  // DSInfo, one graph for each function
+  std::map<Function*, DSGraph*> DSInfo;
+public:
+  static AnalysisID ID;            // TDDataStructure Analysis ID 
+
+  TDDataStructures(AnalysisID id) { assert(id == ID); }
+  ~TDDataStructures() { releaseMemory(); }
+
+  virtual const char *getPassName() const {
+    return "Top-downData Structure Analysis Closure";
+  }
+
+  virtual bool run(Module &M);
+
+  // getDSGraph - Return the data structure graph for the specified function.
+  DSGraph &getDSGraph(Function &F) const {
+    std::map<Function*, DSGraph*>::const_iterator I = DSInfo.find(&F);
+    assert(I != DSInfo.end() && "Function not in module!");
+    return *I->second;
+  }
+
+  // print - Print out the analysis results...
+  void print(std::ostream &O, Module *M) const;
+
+  // If the pass pipeline is done with this pass, we can release our memory...
+  virtual void releaseMemory();
+
+  // getAnalysisUsage - This obviously provides a data structure graph.
+  virtual void getAnalysisUsage(AnalysisUsage &AU) const {
+    AU.setPreservesAll();
+    AU.addProvided(ID);
+    AU.addRequired(BUDataStructures::ID);
+  }
+private:
+  DSGraph &calculateGraph(Function &F);
+  void pushGraphIntoCallee(DSGraph &callerGraph, DSGraph &calleeGraph,
+                           std::map<Value*, DSNodeHandle> &OldValMap,
+                           std::map<const DSNode*, DSNode*> &OldNodeMap);
+};
 #endif
index d129aab0ad8a09877525b343e6a6e3192f824f15..b4493602c5343204059abfe7c1699695e7465e68 100644 (file)
@@ -24,6 +24,7 @@ class DSGraph;                 // A graph for a function
 class DSNodeIterator;          // Data structure graph traversal iterator
 class LocalDataStructures;     // A collection of local graphs for a program
 class BUDataStructures;        // A collection of bu graphs for a program
+class TDDataStructures;        // A collection of td graphs for a program
 
 //===----------------------------------------------------------------------===//
 // DSNodeHandle - Implement a "handle" to a data structure node that takes care
@@ -196,16 +197,47 @@ class DSGraph {
   // call, the second is the function scalar being invoked, and the rest are
   // pointer arguments to the function.
   //
+  // OrigFunctionCalls - This vector retains a copy of the original function
+  // calls of the current graph.  This is needed to support top-down inlining
+  // after bottom-up inlining is complete, since the latter deletes call nodes.
+  // 
   std::vector<std::vector<DSNodeHandle> > FunctionCalls;
-
+  std::vector<std::vector<DSNodeHandle> > OrigFunctionCalls;
+
+  // PendingCallers - This vector records all unresolved callers of the
+  // current function, i.e., ones whose graphs have not been inlined into
+  // the current graph.  As long as there are unresolved callers, the nodes
+  // for formal arguments in the current graph cannot be eliminated, and
+  // nodes in the graph reachable from the formal argument nodes or
+  // global variable nodes must be considered incomplete. 
+  std::vector<Function*> PendingCallers;
+  
 private:
   // Define the interface only accessable to DataStructure
   friend class LocalDataStructures;
   friend class BUDataStructures;
+  friend class TDDataStructures;
   DSGraph(Function &F);            // Compute the local DSGraph
   DSGraph(const DSGraph &DSG);     // Copy ctor
   ~DSGraph();
 
+  // clone all the call nodes and save the copies in OrigFunctionCalls
+  void saveOrigFunctionCalls() {
+    assert(OrigFunctionCalls.size() == 0 && "Do this only once!");
+    OrigFunctionCalls.reserve(FunctionCalls.size());
+    for (unsigned i = 0, ei = FunctionCalls.size(); i != ei; ++i) {
+      OrigFunctionCalls.push_back(std::vector<DSNodeHandle>());
+      OrigFunctionCalls[i].reserve(FunctionCalls[i].size());
+      for (unsigned j = 0, ej = FunctionCalls[i].size(); j != ej; ++j)
+        OrigFunctionCalls[i].push_back(FunctionCalls[i][j]);
+    }
+  }
+  
+  // get the saved copies of the original function call nodes
+  std::vector<std::vector<DSNodeHandle> >& getOrigFunctionCalls() {
+    return OrigFunctionCalls;
+  }
+
   void operator=(const DSGraph &); // DO NOT IMPLEMENT
 public:
 
@@ -236,7 +268,6 @@ public:
   void maskNodeTypes(unsigned char Mask);
   void maskIncompleteMarkers() { maskNodeTypes(~DSNode::Incomplete); }
 
-
   // markIncompleteNodes - Traverse the graph, identifying nodes that may be
   // modified by other functions that have not been resolved yet.  This marks
   // nodes that are reachable through three sources of "unknownness":
@@ -254,6 +285,18 @@ public:
   //
   void removeDeadNodes();
 
+  // AddCaller - add a known caller node into the graph and mark it pending.
+  // getCallers - get a vector of the functions that call this one
+  // getCallersPending - get a matching vector of bools indicating if each
+  //                     caller's DSGraph has been resolved into this one.
+  // 
+  void addCaller(Function& caller) {
+    PendingCallers.push_back(&caller);
+  }
+  std::vector<Function*>& getPendingCallers() {
+    return PendingCallers;
+  }
+  
   // cloneInto - Clone the specified DSGraph into the current graph, returning
   // the Return node of the graph.  The translated ValueMap for the old function
   // is filled into the OldValMap member.  If StripLocals is set to true, Scalar
@@ -261,6 +304,7 @@ public:
   // into a calling function's graph.
   //
   DSNode *cloneInto(const DSGraph &G, std::map<Value*, DSNodeHandle> &OldValMap,
+                    std::map<const DSNode*, DSNode*>& OldNodeMap,
                     bool StripLocals = true);
 private:
   bool isNodeDead(DSNode *N);
@@ -335,7 +379,7 @@ public:
     assert(I != DSInfo.end() && "Function not in module!");
     return *I->second;
   }
-
+  
   // print - Print out the analysis results...
   void print(std::ostream &O, Module *M) const;
 
@@ -352,4 +396,49 @@ private:
   DSGraph &calculateGraph(Function &F);
 };
 
+
+// TDDataStructures - Analysis that computes new data structure graphs
+// for each function using the closed graphs for the callers computed
+// by the bottom-up pass.
+//
+class TDDataStructures : public Pass {
+  // DSInfo, one graph for each function
+  std::map<Function*, DSGraph*> DSInfo;
+public:
+  static AnalysisID ID;            // TDDataStructure Analysis ID 
+
+  TDDataStructures(AnalysisID id) { assert(id == ID); }
+  ~TDDataStructures() { releaseMemory(); }
+
+  virtual const char *getPassName() const {
+    return "Top-downData Structure Analysis Closure";
+  }
+
+  virtual bool run(Module &M);
+
+  // getDSGraph - Return the data structure graph for the specified function.
+  DSGraph &getDSGraph(Function &F) const {
+    std::map<Function*, DSGraph*>::const_iterator I = DSInfo.find(&F);
+    assert(I != DSInfo.end() && "Function not in module!");
+    return *I->second;
+  }
+
+  // print - Print out the analysis results...
+  void print(std::ostream &O, Module *M) const;
+
+  // If the pass pipeline is done with this pass, we can release our memory...
+  virtual void releaseMemory();
+
+  // getAnalysisUsage - This obviously provides a data structure graph.
+  virtual void getAnalysisUsage(AnalysisUsage &AU) const {
+    AU.setPreservesAll();
+    AU.addProvided(ID);
+    AU.addRequired(BUDataStructures::ID);
+  }
+private:
+  DSGraph &calculateGraph(Function &F);
+  void pushGraphIntoCallee(DSGraph &callerGraph, DSGraph &calleeGraph,
+                           std::map<Value*, DSNodeHandle> &OldValMap,
+                           std::map<const DSNode*, DSNode*> &OldNodeMap);
+};
 #endif
index 6bc5a716fc9c140d829fadd0e986c8750e718dac..89904c1019ea1fb891bcd6e701a6aab8b177ffa6 100644 (file)
@@ -125,11 +125,13 @@ void DSNode::mergeWith(DSNode *N) {
 //===----------------------------------------------------------------------===//
 
 DSGraph::DSGraph(const DSGraph &G) : Func(G.Func) {
-  RetNode = cloneInto(G, ValueMap, false);
+  std::map<const DSNode*, DSNode*> NodeMap; // ignored
+  RetNode = cloneInto(G, ValueMap, NodeMap, false);
 }
 
 DSGraph::~DSGraph() {
   FunctionCalls.clear();
+  OrigFunctionCalls.clear();
   ValueMap.clear();
   RetNode = 0;
 
@@ -146,6 +148,25 @@ DSGraph::~DSGraph() {
 // dump - Allow inspection of graph in a debugger.
 void DSGraph::dump() const { print(std::cerr); }
 
+
+// Helper function used to clone a function list.
+// Each call really shd have an explicit representation as a separate class. 
+void
+CopyFunctionCallsList(const std::vector<std::vector<DSNodeHandle> >& fromCalls,
+                      std::vector<std::vector<DSNodeHandle> >& 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(std::vector<DSNodeHandle>());
+    toCalls[FC+i].reserve(fromCalls[i].size());
+    for (unsigned j = 0, ej = fromCalls[i].size(); j != ej; ++j)
+      toCalls[FC+i].push_back(NodeMap[fromCalls[i][j]]);
+  }
+}
+
+
 // cloneInto - Clone the specified DSGraph into the current graph, returning the
 // Return node of the graph.  The translated ValueMap for the old function is
 // filled into the OldValMap member.  If StripLocals is set to true, Scalar and
@@ -154,9 +175,12 @@ void DSGraph::dump() const { print(std::cerr); }
 //
 DSNode *DSGraph::cloneInto(const DSGraph &G, 
                            std::map<Value*, DSNodeHandle> &OldValMap,
+                           std::map<const DSNode*, DSNode*>& OldNodeMap,
                            bool StripLocals) {
-  std::map<const DSNode*, DSNode*> NodeMap;
-  NodeMap[0] = 0;  // Null pointer maps to null
+
+  assert(OldNodeMap.size()==0 && "Return argument OldNodeMap should be empty");
+
+  OldNodeMap[0] = 0;  // Null pointer maps to null
 
   unsigned FN = Nodes.size();  // FirstNode...
 
@@ -165,13 +189,13 @@ DSNode *DSGraph::cloneInto(const DSGraph &G,
   for (unsigned i = 0, e = G.Nodes.size(); i != e; ++i) {
     DSNode *Old = G.Nodes[i], *New = new DSNode(*Old);
     Nodes.push_back(New);
-    NodeMap[Old] = New;
+    OldNodeMap[Old] = New;
   }
 
   // Rewrite the links in the nodes to point into the current graph now.
   for (unsigned i = FN, e = Nodes.size(); i != e; ++i)
     for (unsigned j = 0, e = Nodes[i]->getNumLinks(); j != e; ++j)
-      Nodes[i]->setLink(j, NodeMap[Nodes[i]->getLink(j)]);
+      Nodes[i]->setLink(j, OldNodeMap[Nodes[i]->getLink(j)]);
 
   // If we are inlining this graph into the called function graph, remove local
   // markers.
@@ -182,20 +206,18 @@ DSNode *DSGraph::cloneInto(const DSGraph &G,
   // Copy the value map...
   for (std::map<Value*, DSNodeHandle>::const_iterator I = G.ValueMap.begin(),
          E = G.ValueMap.end(); I != E; ++I)
-    OldValMap[I->first] = NodeMap[I->second];
-
-  // Copy the function calls list...
-  unsigned FC = FunctionCalls.size();  // FirstCall
-  FunctionCalls.reserve(FC+G.FunctionCalls.size());
-  for (unsigned i = 0, e = G.FunctionCalls.size(); i != e; ++i) {
-    FunctionCalls.push_back(std::vector<DSNodeHandle>());
-    FunctionCalls[FC+i].reserve(G.FunctionCalls[i].size());
-    for (unsigned j = 0, e = G.FunctionCalls[i].size(); j != e; ++j)
-      FunctionCalls[FC+i].push_back(NodeMap[G.FunctionCalls[i][j]]);
-  }
+    OldValMap[I->first] = OldNodeMap[I->second];
+
+  // Copy the current function calls list and the orig function calls list ...
+  CopyFunctionCallsList(G.FunctionCalls, FunctionCalls, OldNodeMap);
+  CopyFunctionCallsList(G.OrigFunctionCalls, OrigFunctionCalls, OldNodeMap);
+
+  // Copy the list of unresolved callers
+  PendingCallers.insert(PendingCallers.end(),
+                        G.PendingCallers.begin(), G.PendingCallers.end());
 
   // Return the returned node pointer...
-  return NodeMap[G.RetNode];
+  return OldNodeMap[G.RetNode];
 }