2002-03-06 18:00:49 +00:00
|
|
|
//===- CallGraph.cpp - Build a Module's call graph ------------------------===//
|
2001-09-28 00:08:15 +00:00
|
|
|
//
|
|
|
|
// This file implements call graph construction (from a module), and will
|
|
|
|
// eventually implement call graph serialization and deserialization for
|
|
|
|
// annotation support.
|
|
|
|
//
|
2001-10-13 06:33:19 +00:00
|
|
|
// This call graph represents a dynamic method invocation as a null method node.
|
|
|
|
// A call graph may only have up to one null method node that represents all of
|
|
|
|
// the dynamic method invocations.
|
|
|
|
//
|
2001-09-28 00:08:15 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Analysis/CallGraph.h"
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Method.h"
|
|
|
|
#include "llvm/iOther.h"
|
2001-10-13 06:33:19 +00:00
|
|
|
#include "llvm/iTerminators.h"
|
2002-02-12 21:07:25 +00:00
|
|
|
#include "llvm/Support/InstIterator.h"// FIXME: CallGraph should use method uses
|
2001-11-27 00:03:19 +00:00
|
|
|
#include "Support/STLExtras.h"
|
2001-09-28 00:08:15 +00:00
|
|
|
#include <algorithm>
|
2002-03-06 18:00:49 +00:00
|
|
|
#include <iostream>
|
2001-09-28 00:08:15 +00:00
|
|
|
|
2002-03-06 17:16:43 +00:00
|
|
|
AnalysisID CallGraph::ID(AnalysisID::create<CallGraph>());
|
2002-01-31 00:42:27 +00:00
|
|
|
|
2001-09-28 00:08:15 +00:00
|
|
|
// getNodeFor - Return the node for the specified method or create one if it
|
|
|
|
// does not already exist.
|
|
|
|
//
|
2002-03-06 17:16:43 +00:00
|
|
|
CallGraphNode *CallGraph::getNodeFor(Method *M) {
|
2001-09-28 00:08:15 +00:00
|
|
|
iterator I = MethodMap.find(M);
|
|
|
|
if (I != MethodMap.end()) return I->second;
|
|
|
|
|
|
|
|
assert(M->getParent() == Mod && "Method not in current module!");
|
|
|
|
CallGraphNode *New = new CallGraphNode(M);
|
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
MethodMap.insert(std::make_pair(M, New));
|
2001-09-28 00:08:15 +00:00
|
|
|
return New;
|
|
|
|
}
|
|
|
|
|
|
|
|
// addToCallGraph - Add a method to the call graph, and link the node to all of
|
|
|
|
// the methods that it calls.
|
|
|
|
//
|
2002-03-06 17:16:43 +00:00
|
|
|
void CallGraph::addToCallGraph(Method *M) {
|
2001-09-28 00:08:15 +00:00
|
|
|
CallGraphNode *Node = getNodeFor(M);
|
|
|
|
|
2001-11-26 18:51:25 +00:00
|
|
|
// If this method has external linkage,
|
|
|
|
if (!M->hasInternalLinkage())
|
|
|
|
Root->addCalledMethod(Node);
|
|
|
|
|
2002-02-12 21:07:25 +00:00
|
|
|
for (inst_iterator I = inst_begin(M), E = inst_end(M); I != E; ++I) {
|
2001-10-13 06:33:19 +00:00
|
|
|
// Dynamic calls will cause Null nodes to be created
|
|
|
|
if (CallInst *CI = dyn_cast<CallInst>(*I))
|
2001-09-28 00:08:15 +00:00
|
|
|
Node->addCalledMethod(getNodeFor(CI->getCalledMethod()));
|
2001-10-13 06:33:19 +00:00
|
|
|
else if (InvokeInst *II = dyn_cast<InvokeInst>(*I))
|
|
|
|
Node->addCalledMethod(getNodeFor(II->getCalledMethod()));
|
2001-09-28 00:08:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-03-06 17:16:43 +00:00
|
|
|
bool CallGraph::run(Module *TheModule) {
|
2002-01-31 00:42:27 +00:00
|
|
|
destroy();
|
|
|
|
|
2001-09-28 00:08:15 +00:00
|
|
|
Mod = TheModule;
|
|
|
|
|
2001-11-26 18:51:25 +00:00
|
|
|
// Create the root node of the module...
|
|
|
|
Root = new CallGraphNode(0);
|
|
|
|
|
2001-09-28 00:08:15 +00:00
|
|
|
// Add every method to the call graph...
|
|
|
|
for_each(Mod->begin(), Mod->end(), bind_obj(this,&CallGraph::addToCallGraph));
|
2002-01-31 00:42:27 +00:00
|
|
|
|
|
|
|
return false;
|
2001-09-28 00:08:15 +00:00
|
|
|
}
|
|
|
|
|
2002-03-06 17:16:43 +00:00
|
|
|
void CallGraph::destroy() {
|
2001-11-26 18:51:25 +00:00
|
|
|
for (MethodMapTy::iterator I = MethodMap.begin(), E = MethodMap.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
delete I->second;
|
|
|
|
}
|
2002-01-31 00:42:27 +00:00
|
|
|
MethodMap.clear();
|
2001-11-26 18:51:25 +00:00
|
|
|
}
|
|
|
|
|
2001-09-28 00:08:15 +00:00
|
|
|
|
2002-03-06 17:16:43 +00:00
|
|
|
void WriteToOutput(const CallGraphNode *CGN, std::ostream &o) {
|
2001-11-26 18:51:25 +00:00
|
|
|
if (CGN->getMethod())
|
|
|
|
o << "Call graph node for method: '" << CGN->getMethod()->getName() <<"'\n";
|
|
|
|
else
|
|
|
|
o << "Call graph node null method:\n";
|
|
|
|
|
2001-09-28 00:08:15 +00:00
|
|
|
for (unsigned i = 0; i < CGN->size(); ++i)
|
|
|
|
o << " Calls method '" << (*CGN)[i]->getMethod()->getName() << "'\n";
|
2002-01-20 22:54:45 +00:00
|
|
|
o << "\n";
|
2001-09-28 00:08:15 +00:00
|
|
|
}
|
|
|
|
|
2002-03-06 17:16:43 +00:00
|
|
|
void WriteToOutput(const CallGraph &CG, std::ostream &o) {
|
2001-11-26 18:51:25 +00:00
|
|
|
WriteToOutput(CG.getRoot(), o);
|
2001-09-28 00:08:15 +00:00
|
|
|
for (CallGraph::const_iterator I = CG.begin(), E = CG.end(); I != E; ++I)
|
|
|
|
o << I->second;
|
|
|
|
}
|
2001-10-22 13:55:46 +00:00
|
|
|
|
|
|
|
|
2001-11-26 18:51:25 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Implementations of public modification methods
|
|
|
|
//
|
|
|
|
|
|
|
|
// Methods to keep a call graph up to date with a method that has been
|
|
|
|
// modified
|
|
|
|
//
|
2002-03-06 17:16:43 +00:00
|
|
|
void CallGraph::addMethodToModule(Method *Meth) {
|
2001-11-26 18:51:25 +00:00
|
|
|
assert(0 && "not implemented");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
// removeMethodFromModule - Unlink the method from this module, returning it.
|
|
|
|
// Because this removes the method from the module, the call graph node is
|
|
|
|
// destroyed. This is only valid if the method does not call any other
|
|
|
|
// methods (ie, there are no edges in it's CGN). The easiest way to do this
|
|
|
|
// is to dropAllReferences before calling this.
|
|
|
|
//
|
2002-03-06 17:16:43 +00:00
|
|
|
Method *CallGraph::removeMethodFromModule(CallGraphNode *CGN) {
|
2001-11-26 18:51:25 +00:00
|
|
|
assert(CGN->CalledMethods.empty() && "Cannot remove method from call graph"
|
|
|
|
" if it references other methods!");
|
|
|
|
Method *M = CGN->getMethod(); // Get the method for the call graph node
|
|
|
|
delete CGN; // Delete the call graph node for this method
|
|
|
|
MethodMap.erase(M); // Remove the call graph node from the map
|
|
|
|
|
|
|
|
Mod->getMethodList().remove(M);
|
|
|
|
return M;
|
|
|
|
}
|
|
|
|
|