blob: 244c35ea46ec037db0a29cdef5a55ec2af689edf [file] [log] [blame]
Chris Lattner41fbf302001-09-28 00:08:15 +00001//===- 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 Lattner9f9e2be2001-10-13 06:33:19 +00007// 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 Lattner41fbf302001-09-28 00:08:15 +000011//===----------------------------------------------------------------------===//
12
13#include "llvm/Analysis/CallGraph.h"
14#include "llvm/Analysis/Writer.h"
Chris Lattner41fbf302001-09-28 00:08:15 +000015#include "llvm/Module.h"
16#include "llvm/Method.h"
17#include "llvm/iOther.h"
Chris Lattner9f9e2be2001-10-13 06:33:19 +000018#include "llvm/iTerminators.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000019#include "Support/STLExtras.h"
Chris Lattner41fbf302001-09-28 00:08:15 +000020#include <algorithm>
21
Chris Lattner93193f82002-01-31 00:42:27 +000022AnalysisID cfg::CallGraph::ID(AnalysisID::create<cfg::CallGraph>());
23//AnalysisID cfg::CallGraph::ID(AnalysisID::template AnalysisID<cfg::CallGraph>());
24
Chris Lattner41fbf302001-09-28 00:08:15 +000025// getNodeFor - Return the node for the specified method or create one if it
26// does not already exist.
27//
Chris Lattner25e9cad2001-11-26 18:51:25 +000028cfg::CallGraphNode *cfg::CallGraph::getNodeFor(Method *M) {
Chris Lattner41fbf302001-09-28 00:08:15 +000029 iterator I = MethodMap.find(M);
30 if (I != MethodMap.end()) return I->second;
31
32 assert(M->getParent() == Mod && "Method not in current module!");
33 CallGraphNode *New = new CallGraphNode(M);
34
Chris Lattner697954c2002-01-20 22:54:45 +000035 MethodMap.insert(std::make_pair(M, New));
Chris Lattner41fbf302001-09-28 00:08:15 +000036 return New;
37}
38
39// addToCallGraph - Add a method to the call graph, and link the node to all of
40// the methods that it calls.
41//
Chris Lattner25e9cad2001-11-26 18:51:25 +000042void cfg::CallGraph::addToCallGraph(Method *M) {
Chris Lattner41fbf302001-09-28 00:08:15 +000043 CallGraphNode *Node = getNodeFor(M);
44
Chris Lattner25e9cad2001-11-26 18:51:25 +000045 // If this method has external linkage,
46 if (!M->hasInternalLinkage())
47 Root->addCalledMethod(Node);
48
Chris Lattner9f9e2be2001-10-13 06:33:19 +000049 for (Method::inst_iterator I = M->inst_begin(), E = M->inst_end();
50 I != E; ++I) {
51 // Dynamic calls will cause Null nodes to be created
52 if (CallInst *CI = dyn_cast<CallInst>(*I))
Chris Lattner41fbf302001-09-28 00:08:15 +000053 Node->addCalledMethod(getNodeFor(CI->getCalledMethod()));
Chris Lattner9f9e2be2001-10-13 06:33:19 +000054 else if (InvokeInst *II = dyn_cast<InvokeInst>(*I))
55 Node->addCalledMethod(getNodeFor(II->getCalledMethod()));
Chris Lattner41fbf302001-09-28 00:08:15 +000056 }
57}
58
Chris Lattner93193f82002-01-31 00:42:27 +000059bool cfg::CallGraph::run(Module *TheModule) {
60 destroy();
61
Chris Lattner41fbf302001-09-28 00:08:15 +000062 Mod = TheModule;
63
Chris Lattner25e9cad2001-11-26 18:51:25 +000064 // Create the root node of the module...
65 Root = new CallGraphNode(0);
66
Chris Lattner41fbf302001-09-28 00:08:15 +000067 // Add every method to the call graph...
68 for_each(Mod->begin(), Mod->end(), bind_obj(this,&CallGraph::addToCallGraph));
Chris Lattner93193f82002-01-31 00:42:27 +000069
70 return false;
Chris Lattner41fbf302001-09-28 00:08:15 +000071}
72
Chris Lattner93193f82002-01-31 00:42:27 +000073void cfg::CallGraph::destroy() {
Chris Lattner25e9cad2001-11-26 18:51:25 +000074 for (MethodMapTy::iterator I = MethodMap.begin(), E = MethodMap.end();
75 I != E; ++I) {
76 delete I->second;
77 }
Chris Lattner93193f82002-01-31 00:42:27 +000078 MethodMap.clear();
Chris Lattner25e9cad2001-11-26 18:51:25 +000079}
80
Chris Lattner41fbf302001-09-28 00:08:15 +000081
Chris Lattner697954c2002-01-20 22:54:45 +000082void cfg::WriteToOutput(const CallGraphNode *CGN, std::ostream &o) {
Chris Lattner25e9cad2001-11-26 18:51:25 +000083 if (CGN->getMethod())
84 o << "Call graph node for method: '" << CGN->getMethod()->getName() <<"'\n";
85 else
86 o << "Call graph node null method:\n";
87
Chris Lattner41fbf302001-09-28 00:08:15 +000088 for (unsigned i = 0; i < CGN->size(); ++i)
89 o << " Calls method '" << (*CGN)[i]->getMethod()->getName() << "'\n";
Chris Lattner697954c2002-01-20 22:54:45 +000090 o << "\n";
Chris Lattner41fbf302001-09-28 00:08:15 +000091}
92
Chris Lattner697954c2002-01-20 22:54:45 +000093void cfg::WriteToOutput(const CallGraph &CG, std::ostream &o) {
Chris Lattner25e9cad2001-11-26 18:51:25 +000094 WriteToOutput(CG.getRoot(), o);
Chris Lattner41fbf302001-09-28 00:08:15 +000095 for (CallGraph::const_iterator I = CG.begin(), E = CG.end(); I != E; ++I)
96 o << I->second;
97}
Vikram S. Advea7edb182001-10-22 13:55:46 +000098
99
Chris Lattner25e9cad2001-11-26 18:51:25 +0000100//===----------------------------------------------------------------------===//
101// Implementations of public modification methods
102//
103
104// Methods to keep a call graph up to date with a method that has been
105// modified
106//
107void cfg::CallGraph::addMethodToModule(Method *Meth) {
108 assert(0 && "not implemented");
109 abort();
110}
111
112// removeMethodFromModule - Unlink the method from this module, returning it.
113// Because this removes the method from the module, the call graph node is
114// destroyed. This is only valid if the method does not call any other
115// methods (ie, there are no edges in it's CGN). The easiest way to do this
116// is to dropAllReferences before calling this.
117//
118Method *cfg::CallGraph::removeMethodFromModule(CallGraphNode *CGN) {
119 assert(CGN->CalledMethods.empty() && "Cannot remove method from call graph"
120 " if it references other methods!");
121 Method *M = CGN->getMethod(); // Get the method for the call graph node
122 delete CGN; // Delete the call graph node for this method
123 MethodMap.erase(M); // Remove the call graph node from the map
124
125 Mod->getMethodList().remove(M);
126 return M;
127}
128
Vikram S. Advea7edb182001-10-22 13:55:46 +0000129
130//
131// Checks if a method contains any call instructions.
132// Note that this uses the call graph only if one is provided.
133// It does not build the call graph.
134//
135bool IsLeafMethod(const Method* M, const cfg::CallGraph* CG) {
136 if (CG) {
137 const cfg::CallGraphNode *cgn = (*CG)[M];
138 return (cgn->begin() == cgn->end());
139 }
Chris Lattner25e9cad2001-11-26 18:51:25 +0000140
Chris Lattner7a176752001-12-04 00:03:30 +0000141 for (Method::const_inst_iterator I = M->inst_begin(), E = M->inst_end();
Chris Lattner25e9cad2001-11-26 18:51:25 +0000142 I != E; ++I)
143 if ((*I)->getOpcode() == Instruction::Call)
144 return false;
145 return true;
Vikram S. Advea7edb182001-10-22 13:55:46 +0000146}
147
148