1 //===- CallGraph.cpp - Build a Module's call graph --------------------------=//
3 // This file implements call graph construction (from a module), and will
4 // eventually implement call graph serialization and deserialization for
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Analysis/CallGraph.h"
10 #include "llvm/Analysis/Writer.h"
11 #include "llvm/Support/STLExtras.h"
12 #include "llvm/Module.h"
13 #include "llvm/Method.h"
14 #include "llvm/iOther.h"
19 // getNodeFor - Return the node for the specified method or create one if it
20 // does not already exist.
22 CallGraphNode *CallGraph::getNodeFor(Method *M) {
23 iterator I = MethodMap.find(M);
24 if (I != MethodMap.end()) return I->second;
26 assert(M->getParent() == Mod && "Method not in current module!");
27 CallGraphNode *New = new CallGraphNode(M);
29 MethodMap.insert(pair<const Method*, CallGraphNode*>(M, New));
33 // addToCallGraph - Add a method to the call graph, and link the node to all of
34 // the methods that it calls.
36 void CallGraph::addToCallGraph(Method *M) {
37 CallGraphNode *Node = getNodeFor(M);
39 for (Method::inst_iterator II = M->inst_begin(), IE = M->inst_end();
41 if (CallInst *CI = dyn_cast<CallInst>(*II))
42 Node->addCalledMethod(getNodeFor(CI->getCalledMethod()));
46 CallGraph::CallGraph(Module *TheModule) {
49 // Add every method to the call graph...
50 for_each(Mod->begin(), Mod->end(), bind_obj(this,&CallGraph::addToCallGraph));
54 void cfg::WriteToOutput(const CallGraphNode *CGN, ostream &o) {
55 o << "Call graph node for method: '" << CGN->getMethod()->getName() << "'\n";
56 for (unsigned i = 0; i < CGN->size(); ++i)
57 o << " Calls method '" << (*CGN)[i]->getMethod()->getName() << "'\n";
61 void cfg::WriteToOutput(const CallGraph &CG, ostream &o) {
62 for (CallGraph::const_iterator I = CG.begin(), E = CG.end(); I != E; ++I)