0683623454c3242e5e5d05aed94a25cacdb25eee
[oota-llvm.git] / include / llvm / Analysis / CallGraph.h
1 //===- llvm/Analysis/CallGraph.h - Build a Module's call graph ---*- C++ -*--=//
2 //
3 // This interface is used to build and manipulate a call graph, which is a very 
4 // useful tool for interprocedural optimization.
5 //
6 //===----------------------------------------------------------------------===//
7
8 #ifndef LLVM_ANALYSIS_CALLGRAPH_H
9 #define LLVM_ANALYSIS_CALLGRAPH_H
10
11 #include <map>
12 #include <vector>
13 class Method;
14 class Module;
15
16 namespace cfg {
17
18 class CallGraph;
19 class CallGraphNode {
20   Method *Meth;
21   vector<CallGraphNode*> CalledMethods;
22
23   CallGraphNode(const CallGraphNode &);           // Do not implement
24 public:
25   typedef vector<CallGraphNode*>::iterator iterator;
26   typedef vector<CallGraphNode*>::const_iterator const_iterator;
27
28   // getMethod - Return the method that this call graph node represents...
29   Method *getMethod() const { return Meth; }
30
31   inline iterator begin() { return CalledMethods.begin(); }
32   inline iterator end()   { return CalledMethods.end();   }
33   inline const_iterator begin() const { return CalledMethods.begin(); }
34   inline const_iterator end()   const { return CalledMethods.end();   }
35   inline unsigned size() const { return CalledMethods.size(); }
36
37   inline CallGraphNode *operator[](unsigned i) const { return CalledMethods[i];}
38
39
40 private:                    // Stuff to construct the node, used by CallGraph
41   friend class CallGraph;
42
43   // CallGraphNode ctor - Create a node for the specified method...
44   inline CallGraphNode(Method *M) : Meth(M) {}
45   
46   // addCalledMethod add a method to the list of methods called by this one
47   void addCalledMethod(CallGraphNode *M) {
48     CalledMethods.push_back(M);
49   }
50 };
51
52
53 class CallGraph {
54   Module *Mod;
55   typedef map<const Method *, CallGraphNode *> MethodMapTy;
56   MethodMapTy MethodMap;
57 public:
58   CallGraph(Module *TheModule);
59
60   typedef MethodMapTy::iterator iterator;
61   typedef MethodMapTy::const_iterator const_iterator;
62
63   inline const_iterator begin() const { return MethodMap.begin(); }
64   inline const_iterator end()   const { return MethodMap.end();   }
65
66   inline const CallGraphNode *operator[](const Method *M) const {
67     const_iterator I = MethodMap.find(M);
68     assert(I != MethodMap.end() && "Method not in callgraph!");
69     return I->second;
70   }
71
72 private:   // Implementation of CallGraph construction
73
74   // getNodeFor - Return the node for the specified method or create one if it
75   // does not already exist.
76   //
77   CallGraphNode *getNodeFor(Method *M);
78
79   // addToCallGraph - Add a method to the call graph, and link the node to all
80   // of the methods that it calls.
81   //
82   void addToCallGraph(Method *M);
83 };
84
85
86 }  // end namespace cfg
87
88 #endif