1 //===- CallGraph.cpp - Build a Module's call graph ------------------------===//
3 // This interface is used to build and manipulate a call graph, which is a very
4 // useful tool for interprocedural optimization.
6 // Every method in a module is represented as a node in the call graph. The
7 // callgraph node keeps track of which methods the are called by the method
8 // corresponding to the node.
10 // A call graph will contain nodes where the method that they correspond to is
11 // null. This 'external' node is used to represent control flow that is not
12 // represented (or analyzable) in the module. As such, the external node will
13 // have edges to methods with the following properties:
14 // 1. All methods in the module without internal linkage, since they could
15 // be called by methods outside of the our analysis capability.
16 // 2. All methods whose address is used for something more than a direct call,
17 // for example being stored into a memory location. Since they may be
18 // called by an unknown caller later, they must be tracked as such.
20 // Similarly, methods have a call edge to the external node iff:
21 // 1. The method is external, reflecting the fact that they could call
22 // anything without internal linkage or that has its address taken.
23 // 2. The method contains an indirect method call.
25 // As an extension in the future, there may be multiple nodes with a null
26 // method. These will be used when we can prove (through pointer analysis) that
27 // an indirect call site can call only a specific set of methods.
29 // Because of these properties, the CallGraph captures a conservative superset
30 // of all of the caller-callee relationships, which is useful for
33 // The CallGraph class also attempts to figure out what the root of the
34 // CallGraph is, which is currently does by looking for a method named 'main'.
35 // If no method named 'main' is found, the external node is used as the entry
36 // node, reflecting the fact that any method without internal linkage could
37 // be called into (which is common for libraries).
39 //===----------------------------------------------------------------------===//
41 #ifndef LLVM_ANALYSIS_CALLGRAPH_H
42 #define LLVM_ANALYSIS_CALLGRAPH_H
44 #include "Support/GraphTraits.h"
45 #include "llvm/Pass.h"
50 //===----------------------------------------------------------------------===//
51 // CallGraph class definition
53 class CallGraph : public Pass {
54 Module *Mod; // The module this call graph represents
56 typedef std::map<const Method *, CallGraphNode *> MethodMapTy;
57 MethodMapTy MethodMap; // Map from a method to its node
59 // Root is root of the call graph, or the external node if a 'main' function
60 // couldn't be found. ExternalNode is equivalent to (*this)[0].
62 CallGraphNode *Root, *ExternalNode;
65 //===---------------------------------------------------------------------
68 typedef MethodMapTy::iterator iterator;
69 typedef MethodMapTy::const_iterator const_iterator;
71 inline CallGraphNode *getRoot() { return Root; }
72 inline const CallGraphNode *getRoot() const { return Root; }
73 inline iterator begin() { return MethodMap.begin(); }
74 inline iterator end() { return MethodMap.end(); }
75 inline const_iterator begin() const { return MethodMap.begin(); }
76 inline const_iterator end() const { return MethodMap.end(); }
79 // Subscripting operators, return the call graph node for the provided method
80 inline const CallGraphNode *operator[](const Method *M) const {
81 const_iterator I = MethodMap.find(M);
82 assert(I != MethodMap.end() && "Method not in callgraph!");
85 inline CallGraphNode *operator[](const Method *M) {
86 const_iterator I = MethodMap.find(M);
87 assert(I != MethodMap.end() && "Method not in callgraph!");
91 //===---------------------------------------------------------------------
92 // Methods to keep a call graph up to date with a method that has been
95 void addMethodToModule(Method *Meth);
98 // removeMethodFromModule - Unlink the method from this module, returning it.
99 // Because this removes the method from the module, the call graph node is
100 // destroyed. This is only valid if the method does not call any other
101 // methods (ie, there are no edges in it's CGN). The easiest way to do this
102 // is to dropAllReferences before calling this.
104 Method *removeMethodFromModule(CallGraphNode *CGN);
105 Method *removeMethodFromModule(Method *Meth) {
106 return removeMethodFromModule((*this)[Meth]);
110 //===---------------------------------------------------------------------
111 // Pass infrastructure interface glue code...
113 static AnalysisID ID; // We are an analysis, we must have an ID
115 CallGraph(AnalysisID AID) : Root(0) { assert(AID == ID); }
116 ~CallGraph() { destroy(); }
118 // run - Compute the call graph for the specified module.
119 virtual bool run(Module *TheModule);
121 // getAnalysisUsageInfo - This obviously provides a call graph
122 virtual void getAnalysisUsageInfo(AnalysisSet &Required,
123 AnalysisSet &Destroyed,
124 AnalysisSet &Provided) {
125 Provided.push_back(ID);
128 // releaseMemory - Data structures can be large, so free memory agressively.
129 virtual void releaseMemory() {
134 //===---------------------------------------------------------------------
135 // Implementation of CallGraph construction
138 // getNodeFor - Return the node for the specified method or create one if it
139 // does not already exist.
141 CallGraphNode *getNodeFor(Method *M);
143 // addToCallGraph - Add a method to the call graph, and link the node to all
144 // of the methods that it calls.
146 void addToCallGraph(Method *M);
148 // destroy - Release memory for the call graph
153 //===----------------------------------------------------------------------===//
154 // CallGraphNode class definition
156 class CallGraphNode {
158 std::vector<CallGraphNode*> CalledMethods;
160 CallGraphNode(const CallGraphNode &); // Do not implement
162 //===---------------------------------------------------------------------
163 // Accessor methods...
166 typedef std::vector<CallGraphNode*>::iterator iterator;
167 typedef std::vector<CallGraphNode*>::const_iterator const_iterator;
169 // getMethod - Return the method that this call graph node represents...
170 Method *getMethod() const { return Meth; }
172 inline iterator begin() { return CalledMethods.begin(); }
173 inline iterator end() { return CalledMethods.end(); }
174 inline const_iterator begin() const { return CalledMethods.begin(); }
175 inline const_iterator end() const { return CalledMethods.end(); }
176 inline unsigned size() const { return CalledMethods.size(); }
178 // Subscripting operator - Return the i'th called method...
180 inline CallGraphNode *operator[](unsigned i) const { return CalledMethods[i];}
183 //===---------------------------------------------------------------------
184 // Methods to keep a call graph up to date with a method that has been
188 void removeAllCalledMethods() {
189 CalledMethods.clear();
192 private: // Stuff to construct the node, used by CallGraph
193 friend class CallGraph;
195 // CallGraphNode ctor - Create a node for the specified method...
196 inline CallGraphNode(Method *M) : Meth(M) {}
198 // addCalledMethod add a method to the list of methods called by this one
199 void addCalledMethod(CallGraphNode *M) {
200 CalledMethods.push_back(M);
206 //===----------------------------------------------------------------------===//
207 // GraphTraits specializations for call graphs so that they can be treated as
208 // graphs by the generic graph algorithms...
211 // Provide graph traits for tranversing call graphs using standard graph
213 template <> struct GraphTraits<CallGraphNode*> {
214 typedef CallGraphNode NodeType;
215 typedef NodeType::iterator ChildIteratorType;
217 static NodeType *getEntryNode(CallGraphNode *CGN) { return CGN; }
218 static inline ChildIteratorType child_begin(NodeType *N) { return N->begin();}
219 static inline ChildIteratorType child_end (NodeType *N) { return N->end(); }
222 template <> struct GraphTraits<const CallGraphNode*> {
223 typedef const CallGraphNode NodeType;
224 typedef NodeType::const_iterator ChildIteratorType;
226 static NodeType *getEntryNode(const CallGraphNode *CGN) { return CGN; }
227 static inline ChildIteratorType child_begin(NodeType *N) { return N->begin();}
228 static inline ChildIteratorType child_end (NodeType *N) { return N->end(); }
232 template<> struct GraphTraits<CallGraph*> :
233 public GraphTraits<CallGraphNode*> {
234 static NodeType *getEntryNode(CallGraph *CGN) {
235 return CGN->getRoot();
238 template<> struct GraphTraits<const CallGraph*> :
239 public GraphTraits<const CallGraphNode*> {
240 static NodeType *getEntryNode(const CallGraph *CGN) {
241 return CGN->getRoot();
246 //===----------------------------------------------------------------------===//
247 // Printing support for Call Graphs
250 // Stuff for printing out a callgraph...
252 void WriteToOutput(const CallGraph &, std::ostream &o);
253 inline std::ostream &operator <<(std::ostream &o, const CallGraph &CG) {
254 WriteToOutput(CG, o); return o;
257 void WriteToOutput(const CallGraphNode *, std::ostream &o);
258 inline std::ostream &operator <<(std::ostream &o, const CallGraphNode *CGN) {
259 WriteToOutput(CGN, o); return o;