Make non-local memdep not be recursive, and fix a bug on 403.gcc that this exposed.
[oota-llvm.git] / include / llvm / Analysis / CallGraph.h
index 4edb60235624efffb5fcf71e5f71c802572cd98e..4f4ce0e9f1a3b841c0258bbff118362f85134351 100644 (file)
@@ -54,6 +54,7 @@
 #include "llvm/ADT/GraphTraits.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/Pass.h"
+#include "llvm/Support/CallSite.h"
 
 namespace llvm {
 
@@ -72,6 +73,7 @@ protected:
   FunctionMapTy FunctionMap;    // Map from a function to its node
 
 public:
+  static char ID; // Class identification, replacement for typeinfo
   //===---------------------------------------------------------------------
   // Accessors...
   //
@@ -132,6 +134,11 @@ public:
   /// old code over).
   void changeFunction(Function *OldF, Function *NewF);
 
+  /// getOrInsertFunction - This method is identical to calling operator[], but
+  /// it will insert a new CallGraphNode for the specified function if one does
+  /// not already exist.
+  CallGraphNode *getOrInsertFunction(const Function *F);
+  
   //===---------------------------------------------------------------------
   // Pass infrastructure interface glue code...
   //
@@ -147,9 +154,11 @@ public:
   void initialize(Module &M);
 
   virtual void print(std::ostream &o, const Module *M) const;
-
+  void print(std::ostream *o, const Module *M) const { if (o) print(*o, M); }
+  void dump() const;
+  
   // stub - dummy function, just ignore it
-  static void stub();
+  static int stub;
 protected:
 
   // destroy - Release memory for the call graph
@@ -161,7 +170,8 @@ protected:
 //
 class CallGraphNode {
   Function *F;
-  std::vector<CallGraphNode*> CalledFunctions;
+  typedef std::pair<CallSite,CallGraphNode*> CallRecord;
+  std::vector<CallRecord> CalledFunctions;
 
   CallGraphNode(const CallGraphNode &);           // Do not implement
 public:
@@ -169,8 +179,8 @@ public:
   // Accessor methods...
   //
 
-  typedef std::vector<CallGraphNode*>::iterator iterator;
-  typedef std::vector<CallGraphNode*>::const_iterator const_iterator;
+  typedef std::vector<CallRecord>::iterator iterator;
+  typedef std::vector<CallRecord>::const_iterator const_iterator;
 
   // getFunction - Return the function that this call graph node represents...
   Function *getFunction() const { return F; }
@@ -183,12 +193,15 @@ public:
 
   // Subscripting operator - Return the i'th called function...
   //
-  CallGraphNode *operator[](unsigned i) const { return CalledFunctions[i];}
+  CallGraphNode *operator[](unsigned i) const {
+    return CalledFunctions[i].second;
+  }
 
   /// dump - Print out this call graph node.
   ///
   void dump() const;
   void print(std::ostream &OS) const;
+  void print(std::ostream *OS) const { if (OS) print(*OS); }
 
   //===---------------------------------------------------------------------
   // Methods to keep a call graph up to date with a function that has been
@@ -203,8 +216,8 @@ public:
 
   /// addCalledFunction add a function to the list of functions called by this
   /// one.
-  void addCalledFunction(CallGraphNode *M) {
-    CalledFunctions.push_back(M);
+  void addCalledFunction(CallSite CS, CallGraphNode *M) {
+    CalledFunctions.push_back(std::make_pair(CS, M));
   }
 
   /// removeCallEdgeTo - This method removes a *single* edge to the specified
@@ -219,24 +232,38 @@ public:
 
   friend class CallGraph;
 
-  // CallGraphNode ctor - Create a node for the specified function...
+  // CallGraphNode ctor - Create a node for the specified function.
   inline CallGraphNode(Function *f) : F(f) {}
 };
 
 //===----------------------------------------------------------------------===//
 // GraphTraits specializations for call graphs so that they can be treated as
-// graphs by the generic graph algorithms...
+// graphs by the generic graph algorithms.
 //
 
 // Provide graph traits for tranversing call graphs using standard graph
 // traversals.
 template <> struct GraphTraits<CallGraphNode*> {
   typedef CallGraphNode NodeType;
-  typedef NodeType::iterator ChildIteratorType;
 
+  typedef std::pair<CallSite, CallGraphNode*> CGNPairTy;
+  typedef std::pointer_to_unary_function<CGNPairTy, CallGraphNode*> CGNDerefFun;
+  
   static NodeType *getEntryNode(CallGraphNode *CGN) { return CGN; }
-  static inline ChildIteratorType child_begin(NodeType *N) { return N->begin();}
-  static inline ChildIteratorType child_end  (NodeType *N) { return N->end(); }
+  
+  typedef mapped_iterator<NodeType::iterator, CGNDerefFun> ChildIteratorType;
+  
+  static inline ChildIteratorType child_begin(NodeType *N) {
+    return map_iterator(N->begin(), CGNDerefFun(CGNDeref));
+  }
+  static inline ChildIteratorType child_end  (NodeType *N) {
+    return map_iterator(N->end(), CGNDerefFun(CGNDeref));
+  }
+  
+  static CallGraphNode *CGNDeref(CGNPairTy P) {
+    return P.second;
+  }
+  
 };
 
 template <> struct GraphTraits<const CallGraphNode*> {
@@ -264,8 +291,7 @@ template<> struct GraphTraits<CallGraph*> : public GraphTraits<CallGraphNode*> {
     return map_iterator(CG->end(), DerefFun(CGdereference));
   }
 
-  static CallGraphNode &CGdereference (std::pair<const Function*,
-                                       CallGraphNode*> P) {
+  static CallGraphNode &CGdereference(PairTy P) {
     return *P.second;
   }
 };
@@ -281,13 +307,9 @@ template<> struct GraphTraits<const CallGraph*> :
   static nodes_iterator nodes_end  (const CallGraph *CG) { return CG->end(); }
 };
 
-// Make sure that any clients of this file link in CallGraph.cpp
-static IncludeFile
-CALLGRAPH_INCLUDE_FILE((void*)&CallGraph::stub);
-
-extern void BasicCallGraphStub();
-static IncludeFile HDR_INCLUDE_CALLGRAPH_CPP((void*)&BasicCallGraphStub);
-
 } // End llvm namespace
 
+// Make sure that any clients of this file link in CallGraph.cpp
+FORCE_DEFINING_FILE_TO_BE_LINKED(CallGraph)
+
 #endif