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