00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 #ifndef LLVM_ANALYSIS_CALLGRAPH_H
00052 #define LLVM_ANALYSIS_CALLGRAPH_H
00053
00054 #include "llvm/Function.h"
00055 #include "llvm/Pass.h"
00056 #include "llvm/ADT/GraphTraits.h"
00057 #include "llvm/ADT/STLExtras.h"
00058 #include "llvm/Support/CallSite.h"
00059 #include "llvm/Support/ValueHandle.h"
00060 #include "llvm/System/IncludeFile.h"
00061 #include <map>
00062
00063 namespace llvm {
00064
00065 class Function;
00066 class Module;
00067 class CallGraphNode;
00068
00069
00070
00071
00072 class CallGraph {
00073 protected:
00074 Module *Mod;
00075
00076 typedef std::map<const Function *, CallGraphNode *> FunctionMapTy;
00077 FunctionMapTy FunctionMap;
00078
00079 public:
00080 static char ID;
00081
00082
00083
00084 typedef FunctionMapTy::iterator iterator;
00085 typedef FunctionMapTy::const_iterator const_iterator;
00086
00089 Module &getModule() const { return *Mod; }
00090
00091 inline iterator begin() { return FunctionMap.begin(); }
00092 inline iterator end() { return FunctionMap.end(); }
00093 inline const_iterator begin() const { return FunctionMap.begin(); }
00094 inline const_iterator end() const { return FunctionMap.end(); }
00095
00096
00097
00098 inline const CallGraphNode *operator[](const Function *F) const {
00099 const_iterator I = FunctionMap.find(F);
00100 assert(I != FunctionMap.end() && "Function not in callgraph!");
00101 return I->second;
00102 }
00103 inline CallGraphNode *operator[](const Function *F) {
00104 const_iterator I = FunctionMap.find(F);
00105 assert(I != FunctionMap.end() && "Function not in callgraph!");
00106 return I->second;
00107 }
00108
00111 virtual CallGraphNode* getExternalCallingNode() const { return 0; }
00112 virtual CallGraphNode* getCallsExternalNode() const { return 0; }
00113
00117 virtual CallGraphNode* getRoot() { return 0; }
00118 virtual const CallGraphNode* getRoot() const { return 0; }
00119
00120
00121
00122
00123
00124
00131 Function *removeFunctionFromModule(CallGraphNode *CGN);
00132 Function *removeFunctionFromModule(Function *F) {
00133 return removeFunctionFromModule((*this)[F]);
00134 }
00135
00139 CallGraphNode *getOrInsertFunction(const Function *F);
00140
00141
00142
00143
00144 protected:
00145 CallGraph() {}
00146
00147 public:
00148 virtual ~CallGraph() { destroy(); }
00149
00153 void initialize(Module &M);
00154
00155 void print(raw_ostream &o, Module *) const;
00156 void dump() const;
00157 protected:
00158
00159 virtual void destroy();
00160 };
00161
00162
00163
00164
00165 class CallGraphNode {
00166 AssertingVH<Function> F;
00167
00168
00169
00170 public:
00171 typedef std::pair<WeakVH, CallGraphNode*> CallRecord;
00172 private:
00173 std::vector<CallRecord> CalledFunctions;
00174
00177 unsigned NumReferences;
00178
00179 CallGraphNode(const CallGraphNode &);
00180 void operator=(const CallGraphNode &);
00181
00182 void DropRef() { --NumReferences; }
00183 void AddRef() { ++NumReferences; }
00184 public:
00185 typedef std::vector<CallRecord> CalledFunctionsVector;
00186
00187
00188
00189 inline CallGraphNode(Function *f) : F(f), NumReferences(0) {}
00190
00191
00192
00193
00194
00195 typedef std::vector<CallRecord>::iterator iterator;
00196 typedef std::vector<CallRecord>::const_iterator const_iterator;
00197
00198
00199 Function *getFunction() const { return F; }
00200
00201 inline iterator begin() { return CalledFunctions.begin(); }
00202 inline iterator end() { return CalledFunctions.end(); }
00203 inline const_iterator begin() const { return CalledFunctions.begin(); }
00204 inline const_iterator end() const { return CalledFunctions.end(); }
00205 inline bool empty() const { return CalledFunctions.empty(); }
00206 inline unsigned size() const { return (unsigned)CalledFunctions.size(); }
00207
00210 unsigned getNumReferences() const { return NumReferences; }
00211
00212
00213
00214 CallGraphNode *operator[](unsigned i) const {
00215 assert(i < CalledFunctions.size() && "Invalid index");
00216 return CalledFunctions[i].second;
00217 }
00218
00221 void dump() const;
00222 void print(raw_ostream &OS) const;
00223
00224
00225
00226
00227
00228
00231 void removeAllCalledFunctions() {
00232 while (!CalledFunctions.empty()) {
00233 CalledFunctions.back().second->DropRef();
00234 CalledFunctions.pop_back();
00235 }
00236 }
00237
00240 void stealCalledFunctionsFrom(CallGraphNode *N) {
00241 assert(CalledFunctions.empty() &&
00242 "Cannot steal callsite information if I already have some");
00243 std::swap(CalledFunctions, N->CalledFunctions);
00244 }
00245
00246
00249 void addCalledFunction(CallSite CS, CallGraphNode *M) {
00250 CalledFunctions.push_back(std::make_pair(CS.getInstruction(), M));
00251 M->AddRef();
00252 }
00253
00254 void removeCallEdge(iterator I) {
00255 I->second->DropRef();
00256 *I = CalledFunctions.back();
00257 CalledFunctions.pop_back();
00258 }
00259
00260
00264 void removeCallEdgeFor(CallSite CS);
00265
00269 void removeAnyCallEdgeTo(CallGraphNode *Callee);
00270
00273 void removeOneAbstractEdgeTo(CallGraphNode *Callee);
00274
00278 void replaceCallEdge(CallSite CS, CallSite NewCS, CallGraphNode *NewNode);
00279
00280 };
00281
00282
00283
00284
00285
00286
00287
00288
00289 template <> struct GraphTraits<CallGraphNode*> {
00290 typedef CallGraphNode NodeType;
00291
00292 typedef CallGraphNode::CallRecord CGNPairTy;
00293 typedef std::pointer_to_unary_function<CGNPairTy, CallGraphNode*> CGNDerefFun;
00294
00295 static NodeType *getEntryNode(CallGraphNode *CGN) { return CGN; }
00296
00297 typedef mapped_iterator<NodeType::iterator, CGNDerefFun> ChildIteratorType;
00298
00299 static inline ChildIteratorType child_begin(NodeType *N) {
00300 return map_iterator(N->begin(), CGNDerefFun(CGNDeref));
00301 }
00302 static inline ChildIteratorType child_end (NodeType *N) {
00303 return map_iterator(N->end(), CGNDerefFun(CGNDeref));
00304 }
00305
00306 static CallGraphNode *CGNDeref(CGNPairTy P) {
00307 return P.second;
00308 }
00309
00310 };
00311
00312 template <> struct GraphTraits<const CallGraphNode*> {
00313 typedef const CallGraphNode NodeType;
00314 typedef NodeType::const_iterator ChildIteratorType;
00315
00316 static NodeType *getEntryNode(const CallGraphNode *CGN) { return CGN; }
00317 static inline ChildIteratorType child_begin(NodeType *N) { return N->begin();}
00318 static inline ChildIteratorType child_end (NodeType *N) { return N->end(); }
00319 };
00320
00321 template<> struct GraphTraits<CallGraph*> : public GraphTraits<CallGraphNode*> {
00322 static NodeType *getEntryNode(CallGraph *CGN) {
00323 return CGN->getExternalCallingNode();
00324 }
00325 typedef std::pair<const Function*, CallGraphNode*> PairTy;
00326 typedef std::pointer_to_unary_function<PairTy, CallGraphNode&> DerefFun;
00327
00328
00329 typedef mapped_iterator<CallGraph::iterator, DerefFun> nodes_iterator;
00330 static nodes_iterator nodes_begin(CallGraph *CG) {
00331 return map_iterator(CG->begin(), DerefFun(CGdereference));
00332 }
00333 static nodes_iterator nodes_end (CallGraph *CG) {
00334 return map_iterator(CG->end(), DerefFun(CGdereference));
00335 }
00336
00337 static CallGraphNode &CGdereference(PairTy P) {
00338 return *P.second;
00339 }
00340 };
00341
00342 template<> struct GraphTraits<const CallGraph*> :
00343 public GraphTraits<const CallGraphNode*> {
00344 static NodeType *getEntryNode(const CallGraph *CGN) {
00345 return CGN->getExternalCallingNode();
00346 }
00347
00348 typedef CallGraph::const_iterator nodes_iterator;
00349 static nodes_iterator nodes_begin(const CallGraph *CG) { return CG->begin(); }
00350 static nodes_iterator nodes_end (const CallGraph *CG) { return CG->end(); }
00351 };
00352
00353 }
00354
00355
00356 FORCE_DEFINING_FILE_TO_BE_LINKED(CallGraph)
00357
00358 #endif