1 //===- llvm/Analysis/CallGraph.h - Build a Module's call graph ---*- C++ -*--=//
3 // This interface is used to build and manipulate a call graph, which is a very
4 // useful tool for interprocedural optimization.
6 // This call graph represents a dynamic method invocation as a null method node.
7 // A call graph may only have up to one null method node that represents all of
8 // the dynamic method invocations.
10 // Additionally, the 'root' node of a call graph represents the "entry point"
11 // node of the graph, which has an edge to every external method in the graph.
12 // This node has a null method pointer.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_ANALYSIS_CALLGRAPH_H
17 #define LLVM_ANALYSIS_CALLGRAPH_H
19 #include "Support/GraphTraits.h"
20 #include "llvm/Pass.h"
25 //===----------------------------------------------------------------------===//
26 // CallGraphNode class definition
30 std::vector<CallGraphNode*> CalledMethods;
32 CallGraphNode(const CallGraphNode &); // Do not implement
34 typedef std::vector<CallGraphNode*>::iterator iterator;
35 typedef std::vector<CallGraphNode*>::const_iterator const_iterator;
37 // getMethod - Return the method that this call graph node represents...
38 Method *getMethod() const { return Meth; }
40 inline iterator begin() { return CalledMethods.begin(); }
41 inline iterator end() { return CalledMethods.end(); }
42 inline const_iterator begin() const { return CalledMethods.begin(); }
43 inline const_iterator end() const { return CalledMethods.end(); }
44 inline unsigned size() const { return CalledMethods.size(); }
46 inline CallGraphNode *operator[](unsigned i) const { return CalledMethods[i];}
48 void removeAllCalledMethods() {
49 CalledMethods.clear();
52 private: // Stuff to construct the node, used by CallGraph
53 friend class CallGraph;
55 // CallGraphNode ctor - Create a node for the specified method...
56 inline CallGraphNode(Method *M) : Meth(M) {}
58 // addCalledMethod add a method to the list of methods called by this one
59 void addCalledMethod(CallGraphNode *M) {
60 CalledMethods.push_back(M);
65 //===----------------------------------------------------------------------===//
66 // CallGraph class definition
68 class CallGraph : public Pass {
69 Module *Mod; // The module this call graph represents
71 typedef std::map<const Method *, CallGraphNode *> MethodMapTy;
72 MethodMapTy MethodMap; // Map from a method to its node
76 static AnalysisID ID; // We are an analysis, we must have an ID
78 CallGraph(AnalysisID AID) : Root(0) { assert(AID == ID); }
79 ~CallGraph() { destroy(); }
81 typedef MethodMapTy::iterator iterator;
82 typedef MethodMapTy::const_iterator const_iterator;
84 inline CallGraphNode *getRoot() { return Root; }
85 inline const CallGraphNode *getRoot() const { return Root; }
86 inline iterator begin() { return MethodMap.begin(); }
87 inline iterator end() { return MethodMap.end(); }
88 inline const_iterator begin() const { return MethodMap.begin(); }
89 inline const_iterator end() const { return MethodMap.end(); }
91 inline const CallGraphNode *operator[](const Method *M) const {
92 const_iterator I = MethodMap.find(M);
93 assert(I != MethodMap.end() && "Method not in callgraph!");
96 inline CallGraphNode *operator[](const Method *M) {
97 const_iterator I = MethodMap.find(M);
98 assert(I != MethodMap.end() && "Method not in callgraph!");
102 // Methods to keep a call graph up to date with a method that has been
105 void addMethodToModule(Method *Meth); // TODO IMPLEMENT
108 // removeMethodFromModule - Unlink the method from this module, returning it.
109 // Because this removes the method from the module, the call graph node is
110 // destroyed. This is only valid if the method does not call any other
111 // methods (ie, there are no edges in it's CGN). The easiest way to do this
112 // is to dropAllReferences before calling this.
114 Method *removeMethodFromModule(CallGraphNode *CGN);
115 Method *removeMethodFromModule(Method *Meth) {
116 return removeMethodFromModule((*this)[Meth]);
119 // run - Compute the call graph for the specified module.
120 virtual bool run(Module *TheModule);
122 // getAnalysisUsageInfo - This obviously provides a call graph
123 virtual void getAnalysisUsageInfo(AnalysisSet &Required,
124 AnalysisSet &Destroyed,
125 AnalysisSet &Provided) {
126 Provided.push_back(ID);
129 // releaseMemory - Data structures can be large, so free memory agressively.
130 virtual void releaseMemory() {
134 private: // Implementation of CallGraph construction
137 // getNodeFor - Return the node for the specified method or create one if it
138 // does not already exist.
140 CallGraphNode *getNodeFor(Method *M);
142 // addToCallGraph - Add a method to the call graph, and link the node to all
143 // of the methods that it calls.
145 void addToCallGraph(Method *M);
149 //===----------------------------------------------------------------------===//
150 // GraphTraits specializations for call graphs so that they can be treated as
151 // graphs by the generic graph algorithms...
154 // Provide graph traits for tranversing call graphs using standard graph
156 template <> struct GraphTraits<CallGraphNode*> {
157 typedef CallGraphNode NodeType;
158 typedef NodeType::iterator ChildIteratorType;
160 static NodeType *getEntryNode(CallGraphNode *CGN) { return CGN; }
161 static inline ChildIteratorType child_begin(NodeType *N) { return N->begin();}
162 static inline ChildIteratorType child_end (NodeType *N) { return N->end(); }
165 template <> struct GraphTraits<const CallGraphNode*> {
166 typedef const CallGraphNode NodeType;
167 typedef NodeType::const_iterator ChildIteratorType;
169 static NodeType *getEntryNode(const CallGraphNode *CGN) { return CGN; }
170 static inline ChildIteratorType child_begin(NodeType *N) { return N->begin();}
171 static inline ChildIteratorType child_end (NodeType *N) { return N->end(); }
175 template<> struct GraphTraits<CallGraph*> :
176 public GraphTraits<CallGraphNode*> {
177 static NodeType *getEntryNode(CallGraph *CGN) {
178 return CGN->getRoot();
181 template<> struct GraphTraits<const CallGraph*> :
182 public GraphTraits<const CallGraphNode*> {
183 static NodeType *getEntryNode(const CallGraph *CGN) {
184 return CGN->getRoot();
189 //===----------------------------------------------------------------------===//
190 // Printing support for Call Graphs
193 // Stuff for printing out a callgraph...
195 void WriteToOutput(const CallGraph &, std::ostream &o);
196 inline std::ostream &operator <<(std::ostream &o, const CallGraph &CG) {
197 WriteToOutput(CG, o); return o;
200 void WriteToOutput(const CallGraphNode *, std::ostream &o);
201 inline std::ostream &operator <<(std::ostream &o, const CallGraphNode *CGN) {
202 WriteToOutput(CGN, o); return o;