blob: 713c181fbccd7298c5ca0cf2b623c0123051457e [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 Lattner221d6882002-02-12 21:07:25 +000019#include "llvm/Support/InstIterator.h"// FIXME: CallGraph should use method uses
Chris Lattnercee8f9a2001-11-27 00:03:19 +000020#include "Support/STLExtras.h"
Chris Lattner41fbf302001-09-28 00:08:15 +000021#include <algorithm>
22
Chris Lattner4ce0f8a2002-03-06 17:16:43 +000023AnalysisID CallGraph::ID(AnalysisID::create<CallGraph>());
24//AnalysisID CallGraph::ID(AnalysisID::template AnalysisID<CallGraph>());
Chris Lattner93193f82002-01-31 00:42:27 +000025
Chris Lattner41fbf302001-09-28 00:08:15 +000026// getNodeFor - Return the node for the specified method or create one if it
27// does not already exist.
28//
Chris Lattner4ce0f8a2002-03-06 17:16:43 +000029CallGraphNode *CallGraph::getNodeFor(Method *M) {
Chris Lattner41fbf302001-09-28 00:08:15 +000030 iterator I = MethodMap.find(M);
31 if (I != MethodMap.end()) return I->second;
32
33 assert(M->getParent() == Mod && "Method not in current module!");
34 CallGraphNode *New = new CallGraphNode(M);
35
Chris Lattner697954c2002-01-20 22:54:45 +000036 MethodMap.insert(std::make_pair(M, New));
Chris Lattner41fbf302001-09-28 00:08:15 +000037 return New;
38}
39
40// addToCallGraph - Add a method to the call graph, and link the node to all of
41// the methods that it calls.
42//
Chris Lattner4ce0f8a2002-03-06 17:16:43 +000043void CallGraph::addToCallGraph(Method *M) {
Chris Lattner41fbf302001-09-28 00:08:15 +000044 CallGraphNode *Node = getNodeFor(M);
45
Chris Lattner25e9cad2001-11-26 18:51:25 +000046 // If this method has external linkage,
47 if (!M->hasInternalLinkage())
48 Root->addCalledMethod(Node);
49
Chris Lattner221d6882002-02-12 21:07:25 +000050 for (inst_iterator I = inst_begin(M), E = inst_end(M); I != E; ++I) {
Chris Lattner9f9e2be2001-10-13 06:33:19 +000051 // 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 Lattner4ce0f8a2002-03-06 17:16:43 +000059bool CallGraph::run(Module *TheModule) {
Chris Lattner93193f82002-01-31 00:42:27 +000060 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 Lattner4ce0f8a2002-03-06 17:16:43 +000073void 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 Lattner4ce0f8a2002-03-06 17:16:43 +000082void 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 Lattner4ce0f8a2002-03-06 17:16:43 +000093void 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//
Chris Lattner4ce0f8a2002-03-06 17:16:43 +0000107void CallGraph::addMethodToModule(Method *Meth) {
Chris Lattner25e9cad2001-11-26 18:51:25 +0000108 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//
Chris Lattner4ce0f8a2002-03-06 17:16:43 +0000118Method *CallGraph::removeMethodFromModule(CallGraphNode *CGN) {
Chris Lattner25e9cad2001-11-26 18:51:25 +0000119 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//
Chris Lattner4ce0f8a2002-03-06 17:16:43 +0000135bool IsLeafMethod(const Method* M, const CallGraph* CG) {
Vikram S. Advea7edb182001-10-22 13:55:46 +0000136 if (CG) {
Chris Lattner4ce0f8a2002-03-06 17:16:43 +0000137 const CallGraphNode *cgn = (*CG)[M];
Vikram S. Advea7edb182001-10-22 13:55:46 +0000138 return (cgn->begin() == cgn->end());
139 }
Chris Lattner25e9cad2001-11-26 18:51:25 +0000140
Chris Lattner221d6882002-02-12 21:07:25 +0000141 for (const_inst_iterator I = inst_begin(M), E = inst_end(M); I != E; ++I)
Chris Lattner25e9cad2001-11-26 18:51:25 +0000142 if ((*I)->getOpcode() == Instruction::Call)
143 return false;
144 return true;
Vikram S. Advea7edb182001-10-22 13:55:46 +0000145}
146
147