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"
29 std::vector<CallGraphNode*> CalledMethods;
31 CallGraphNode(const CallGraphNode &); // Do not implement
33 typedef std::vector<CallGraphNode*>::iterator iterator;
34 typedef std::vector<CallGraphNode*>::const_iterator const_iterator;
36 // getMethod - Return the method that this call graph node represents...
37 Method *getMethod() const { return Meth; }
39 inline iterator begin() { return CalledMethods.begin(); }
40 inline iterator end() { return CalledMethods.end(); }
41 inline const_iterator begin() const { return CalledMethods.begin(); }
42 inline const_iterator end() const { return CalledMethods.end(); }
43 inline unsigned size() const { return CalledMethods.size(); }
45 inline CallGraphNode *operator[](unsigned i) const { return CalledMethods[i];}
47 void removeAllCalledMethods() {
48 CalledMethods.clear();
51 private: // Stuff to construct the node, used by CallGraph
52 friend class CallGraph;
54 // CallGraphNode ctor - Create a node for the specified method...
55 inline CallGraphNode(Method *M) : Meth(M) {}
57 // addCalledMethod add a method to the list of methods called by this one
58 void addCalledMethod(CallGraphNode *M) {
59 CalledMethods.push_back(M);
64 class CallGraph : public Pass {
65 Module *Mod; // The module this call graph represents
67 typedef std::map<const Method *, CallGraphNode *> MethodMapTy;
68 MethodMapTy MethodMap; // Map from a method to its node
72 static AnalysisID ID; // We are an analysis, we must have an ID
74 CallGraph(AnalysisID AID) : Root(0) { assert(AID == ID); }
75 ~CallGraph() { destroy(); }
77 typedef MethodMapTy::iterator iterator;
78 typedef MethodMapTy::const_iterator const_iterator;
80 inline CallGraphNode *getRoot() { return Root; }
81 inline const CallGraphNode *getRoot() const { return Root; }
82 inline iterator begin() { return MethodMap.begin(); }
83 inline iterator end() { return MethodMap.end(); }
84 inline const_iterator begin() const { return MethodMap.begin(); }
85 inline const_iterator end() const { return MethodMap.end(); }
87 inline const CallGraphNode *operator[](const Method *M) const {
88 const_iterator I = MethodMap.find(M);
89 assert(I != MethodMap.end() && "Method not in callgraph!");
92 inline CallGraphNode *operator[](const Method *M) {
93 const_iterator I = MethodMap.find(M);
94 assert(I != MethodMap.end() && "Method not in callgraph!");
98 // Methods to keep a call graph up to date with a method that has been
101 void addMethodToModule(Method *Meth); // TODO IMPLEMENT
104 // removeMethodFromModule - Unlink the method from this module, returning it.
105 // Because this removes the method from the module, the call graph node is
106 // destroyed. This is only valid if the method does not call any other
107 // methods (ie, there are no edges in it's CGN). The easiest way to do this
108 // is to dropAllReferences before calling this.
110 Method *removeMethodFromModule(CallGraphNode *CGN);
111 Method *removeMethodFromModule(Method *Meth) {
112 return removeMethodFromModule((*this)[Meth]);
115 // run - Compute the call graph for the specified module.
116 virtual bool run(Module *TheModule);
118 // getAnalysisUsageInfo - This obviously provides a call graph
119 virtual void getAnalysisUsageInfo(AnalysisSet &Required,
120 AnalysisSet &Destroyed,
121 AnalysisSet &Provided) {
122 Provided.push_back(ID);
125 // releaseMemory - Data structures can be large, so free memory agressively.
126 virtual void releaseMemory() {
130 private: // Implementation of CallGraph construction
133 // getNodeFor - Return the node for the specified method or create one if it
134 // does not already exist.
136 CallGraphNode *getNodeFor(Method *M);
138 // addToCallGraph - Add a method to the call graph, and link the node to all
139 // of the methods that it calls.
141 void addToCallGraph(Method *M);
144 } // end namespace cfg
149 // Provide graph traits for tranversing call graphs using standard graph
151 template <> struct GraphTraits<cfg::CallGraphNode*> {
152 typedef cfg::CallGraphNode NodeType;
153 typedef NodeType::iterator ChildIteratorType;
155 static NodeType *getEntryNode(cfg::CallGraphNode *CGN) { return CGN; }
156 static inline ChildIteratorType child_begin(NodeType *N) { return N->begin();}
157 static inline ChildIteratorType child_end (NodeType *N) { return N->end(); }
160 template <> struct GraphTraits<const cfg::CallGraphNode*> {
161 typedef const cfg::CallGraphNode NodeType;
162 typedef NodeType::const_iterator ChildIteratorType;
164 static NodeType *getEntryNode(const cfg::CallGraphNode *CGN) { return CGN; }
165 static inline ChildIteratorType child_begin(NodeType *N) { return N->begin();}
166 static inline ChildIteratorType child_end (NodeType *N) { return N->end(); }
170 template<> struct GraphTraits<cfg::CallGraph*> :
171 public GraphTraits<cfg::CallGraphNode*> {
172 static NodeType *getEntryNode(cfg::CallGraph *CGN) {
173 return CGN->getRoot();
176 template<> struct GraphTraits<const cfg::CallGraph*> :
177 public GraphTraits<const cfg::CallGraphNode*> {
178 static NodeType *getEntryNode(const cfg::CallGraph *CGN) {
179 return CGN->getRoot();
184 // Checks if a method contains any call instructions.
185 // Note that this uses the call graph only if one is provided.
186 // It does not build the call graph.
188 bool isLeafMethod(const Method* method, const cfg::CallGraph *callGraph = 0);