fef9f2115596e8a1865ec0b5be4bcc5424ebbcb9
[oota-llvm.git] / include / llvm / Analysis / CallGraph.h
1 //===- CallGraph.cpp - Build a Module's call graph ------------------------===//
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 // 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.
9 //
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.
19 //
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.
24 //
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.
28 //
29 // Because of these properties, the CallGraph captures a conservative superset
30 // of all of the caller-callee relationships, which is useful for
31 // transformations.
32 //
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).
38 //
39 //===----------------------------------------------------------------------===//
40
41 #ifndef LLVM_ANALYSIS_CALLGRAPH_H
42 #define LLVM_ANALYSIS_CALLGRAPH_H
43
44 #include "Support/GraphTraits.h"
45 #include "llvm/Pass.h"
46 class Method;
47 class Module;
48 class CallGraphNode;
49
50 //===----------------------------------------------------------------------===//
51 // CallGraph class definition
52 //
53 class CallGraph : public Pass {
54   Module *Mod;              // The module this call graph represents
55
56   typedef std::map<const Method *, CallGraphNode *> MethodMapTy;
57   MethodMapTy MethodMap;    // Map from a method to its node
58
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].
61   //
62   CallGraphNode *Root, *ExternalNode;
63 public:
64
65   //===---------------------------------------------------------------------
66   // Accessors...
67   //
68   typedef MethodMapTy::iterator iterator;
69   typedef MethodMapTy::const_iterator const_iterator;
70
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();   }
77
78
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!");
83     return I->second;
84   }
85   inline CallGraphNode *operator[](const Method *M) {
86     const_iterator I = MethodMap.find(M);
87     assert(I != MethodMap.end() && "Method not in callgraph!");
88     return I->second;
89   }
90
91   //===---------------------------------------------------------------------
92   // Methods to keep a call graph up to date with a method that has been
93   // modified
94   //
95   void addMethodToModule(Method *Meth);
96
97
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.
103   //
104   Method *removeMethodFromModule(CallGraphNode *CGN);
105   Method *removeMethodFromModule(Method *Meth) {
106     return removeMethodFromModule((*this)[Meth]);
107   }
108
109
110   //===---------------------------------------------------------------------
111   // Pass infrastructure interface glue code...
112   //
113   static AnalysisID ID;    // We are an analysis, we must have an ID
114
115   CallGraph(AnalysisID AID) : Root(0) { assert(AID == ID); }
116   ~CallGraph() { destroy(); }
117
118   // run - Compute the call graph for the specified module.
119   virtual bool run(Module *TheModule);
120
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);
126   }
127
128   // releaseMemory - Data structures can be large, so free memory agressively.
129   virtual void releaseMemory() {
130     destroy();
131   }
132
133 private:
134   //===---------------------------------------------------------------------
135   // Implementation of CallGraph construction
136   //
137
138   // getNodeFor - Return the node for the specified method or create one if it
139   // does not already exist.
140   //
141   CallGraphNode *getNodeFor(Method *M);
142
143   // addToCallGraph - Add a method to the call graph, and link the node to all
144   // of the methods that it calls.
145   //
146   void addToCallGraph(Method *M);
147
148   // destroy - Release memory for the call graph
149   void destroy();
150 };
151
152
153 //===----------------------------------------------------------------------===//
154 // CallGraphNode class definition
155 //
156 class CallGraphNode {
157   Method *Meth;
158   std::vector<CallGraphNode*> CalledMethods;
159
160   CallGraphNode(const CallGraphNode &);           // Do not implement
161 public:
162   //===---------------------------------------------------------------------
163   // Accessor methods...
164   //
165
166   typedef std::vector<CallGraphNode*>::iterator iterator;
167   typedef std::vector<CallGraphNode*>::const_iterator const_iterator;
168
169   // getMethod - Return the method that this call graph node represents...
170   Method *getMethod() const { return Meth; }
171
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(); }
177
178   // Subscripting operator - Return the i'th called method...
179   //
180   inline CallGraphNode *operator[](unsigned i) const { return CalledMethods[i];}
181
182
183   //===---------------------------------------------------------------------
184   // Methods to keep a call graph up to date with a method that has been
185   // modified
186   //
187
188   void removeAllCalledMethods() {
189     CalledMethods.clear();
190   }
191
192 private:                    // Stuff to construct the node, used by CallGraph
193   friend class CallGraph;
194
195   // CallGraphNode ctor - Create a node for the specified method...
196   inline CallGraphNode(Method *M) : Meth(M) {}
197   
198   // addCalledMethod add a method to the list of methods called by this one
199   void addCalledMethod(CallGraphNode *M) {
200     CalledMethods.push_back(M);
201   }
202 };
203
204
205
206 //===----------------------------------------------------------------------===//
207 // GraphTraits specializations for call graphs so that they can be treated as
208 // graphs by the generic graph algorithms...
209 //
210
211 // Provide graph traits for tranversing call graphs using standard graph
212 // traversals.
213 template <> struct GraphTraits<CallGraphNode*> {
214   typedef CallGraphNode NodeType;
215   typedef NodeType::iterator ChildIteratorType;
216
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(); }
220 };
221
222 template <> struct GraphTraits<const CallGraphNode*> {
223   typedef const CallGraphNode NodeType;
224   typedef NodeType::const_iterator ChildIteratorType;
225
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(); }
229 };
230
231
232 template<> struct GraphTraits<CallGraph*> :
233   public GraphTraits<CallGraphNode*> {
234   static NodeType *getEntryNode(CallGraph *CGN) {
235     return CGN->getRoot();
236   }
237 };
238 template<> struct GraphTraits<const CallGraph*> :
239   public GraphTraits<const CallGraphNode*> {
240   static NodeType *getEntryNode(const CallGraph *CGN) {
241     return CGN->getRoot();
242   }
243 };
244
245
246 //===----------------------------------------------------------------------===//
247 // Printing support for Call Graphs
248 //
249
250 // Stuff for printing out a callgraph...
251
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;
255 }
256   
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;
260 }
261
262 #endif