Chris Lattner | 41fbf30 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 1 | //===- CallGraph.cpp - Build a Module's call graph --------------------------=// |
| 2 | // |
| 3 | // This file implements call graph construction (from a module), and will |
| 4 | // eventually implement call graph serialization and deserialization for |
| 5 | // annotation support. |
| 6 | // |
Chris Lattner | 9f9e2be | 2001-10-13 06:33:19 +0000 | [diff] [blame^] | 7 | // This call graph represents a dynamic method invocation as a null method node. |
| 8 | // A call graph may only have up to one null method node that represents all of |
| 9 | // the dynamic method invocations. |
| 10 | // |
Chris Lattner | 41fbf30 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "llvm/Analysis/CallGraph.h" |
| 14 | #include "llvm/Analysis/Writer.h" |
| 15 | #include "llvm/Support/STLExtras.h" |
| 16 | #include "llvm/Module.h" |
| 17 | #include "llvm/Method.h" |
| 18 | #include "llvm/iOther.h" |
Chris Lattner | 9f9e2be | 2001-10-13 06:33:19 +0000 | [diff] [blame^] | 19 | #include "llvm/iTerminators.h" |
Chris Lattner | 41fbf30 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 20 | #include <algorithm> |
| 21 | |
| 22 | using namespace cfg; |
| 23 | |
| 24 | // getNodeFor - Return the node for the specified method or create one if it |
| 25 | // does not already exist. |
| 26 | // |
| 27 | CallGraphNode *CallGraph::getNodeFor(Method *M) { |
| 28 | iterator I = MethodMap.find(M); |
| 29 | if (I != MethodMap.end()) return I->second; |
| 30 | |
| 31 | assert(M->getParent() == Mod && "Method not in current module!"); |
| 32 | CallGraphNode *New = new CallGraphNode(M); |
| 33 | |
| 34 | MethodMap.insert(pair<const Method*, CallGraphNode*>(M, New)); |
| 35 | return New; |
| 36 | } |
| 37 | |
| 38 | // addToCallGraph - Add a method to the call graph, and link the node to all of |
| 39 | // the methods that it calls. |
| 40 | // |
| 41 | void CallGraph::addToCallGraph(Method *M) { |
| 42 | CallGraphNode *Node = getNodeFor(M); |
| 43 | |
Chris Lattner | 9f9e2be | 2001-10-13 06:33:19 +0000 | [diff] [blame^] | 44 | for (Method::inst_iterator I = M->inst_begin(), E = M->inst_end(); |
| 45 | I != E; ++I) { |
| 46 | // Dynamic calls will cause Null nodes to be created |
| 47 | if (CallInst *CI = dyn_cast<CallInst>(*I)) |
Chris Lattner | 41fbf30 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 48 | Node->addCalledMethod(getNodeFor(CI->getCalledMethod())); |
Chris Lattner | 9f9e2be | 2001-10-13 06:33:19 +0000 | [diff] [blame^] | 49 | else if (InvokeInst *II = dyn_cast<InvokeInst>(*I)) |
| 50 | Node->addCalledMethod(getNodeFor(II->getCalledMethod())); |
Chris Lattner | 41fbf30 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
| 54 | CallGraph::CallGraph(Module *TheModule) { |
| 55 | Mod = TheModule; |
| 56 | |
| 57 | // Add every method to the call graph... |
| 58 | for_each(Mod->begin(), Mod->end(), bind_obj(this,&CallGraph::addToCallGraph)); |
| 59 | } |
| 60 | |
| 61 | |
| 62 | void cfg::WriteToOutput(const CallGraphNode *CGN, ostream &o) { |
| 63 | o << "Call graph node for method: '" << CGN->getMethod()->getName() << "'\n"; |
| 64 | for (unsigned i = 0; i < CGN->size(); ++i) |
| 65 | o << " Calls method '" << (*CGN)[i]->getMethod()->getName() << "'\n"; |
| 66 | o << endl; |
| 67 | } |
| 68 | |
| 69 | void cfg::WriteToOutput(const CallGraph &CG, ostream &o) { |
| 70 | for (CallGraph::const_iterator I = CG.begin(), E = CG.end(); I != E; ++I) |
| 71 | o << I->second; |
| 72 | } |