2002-03-06 18:00:49 +00:00
|
|
|
//===- CallGraph.cpp - Build a Module's call graph ------------------------===//
|
2003-10-20 19:43:21 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2001-09-28 00:08:15 +00:00
|
|
|
//
|
2004-04-12 05:36:32 +00:00
|
|
|
// This file implements the CallGraph class.
|
2001-10-13 06:33:19 +00:00
|
|
|
//
|
2001-09-28 00:08:15 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Analysis/CallGraph.h"
|
2003-10-31 21:05:12 +00:00
|
|
|
#include "llvm/Constants.h" // Remove when ConstantPointerRefs are gone
|
2001-09-28 00:08:15 +00:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/iOther.h"
|
2001-10-13 06:33:19 +00:00
|
|
|
#include "llvm/iTerminators.h"
|
2003-10-31 21:05:12 +00:00
|
|
|
#include "llvm/Support/CallSite.h"
|
2001-11-27 00:03:19 +00:00
|
|
|
#include "Support/STLExtras.h"
|
2004-04-12 05:36:32 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2002-07-26 21:12:44 +00:00
|
|
|
static RegisterAnalysis<CallGraph> X("callgraph", "Call Graph Construction");
|
2002-01-31 00:42:27 +00:00
|
|
|
|
2002-07-18 04:43:16 +00:00
|
|
|
// getNodeFor - Return the node for the specified function or create one if it
|
2001-09-28 00:08:15 +00:00
|
|
|
// does not already exist.
|
|
|
|
//
|
2002-03-26 17:55:33 +00:00
|
|
|
CallGraphNode *CallGraph::getNodeFor(Function *F) {
|
2002-07-18 04:43:16 +00:00
|
|
|
CallGraphNode *&CGN = FunctionMap[F];
|
2002-03-06 20:19:35 +00:00
|
|
|
if (CGN) return CGN;
|
2001-09-28 00:08:15 +00:00
|
|
|
|
2002-03-26 17:55:33 +00:00
|
|
|
assert((!F || F->getParent() == Mod) && "Function not in current module!");
|
|
|
|
return CGN = new CallGraphNode(F);
|
2001-09-28 00:08:15 +00:00
|
|
|
}
|
|
|
|
|
2003-10-31 21:05:12 +00:00
|
|
|
static bool isOnlyADirectCall(Function *F, CallSite CS) {
|
|
|
|
if (!CS.getInstruction()) return false;
|
|
|
|
for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); I != E; ++I)
|
|
|
|
if (*I == F) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2002-07-18 04:43:16 +00:00
|
|
|
// addToCallGraph - Add a function to the call graph, and link the node to all
|
|
|
|
// of the functions that it calls.
|
2001-09-28 00:08:15 +00:00
|
|
|
//
|
2003-08-31 20:36:52 +00:00
|
|
|
void CallGraph::addToCallGraph(Function *F) {
|
|
|
|
CallGraphNode *Node = getNodeFor(F);
|
2001-09-28 00:08:15 +00:00
|
|
|
|
2003-09-15 04:35:16 +00:00
|
|
|
// If this function has external linkage, anything could call it...
|
2003-08-31 20:36:52 +00:00
|
|
|
if (!F->hasInternalLinkage()) {
|
2004-04-12 05:36:32 +00:00
|
|
|
ExternalCallingNode->addCalledFunction(Node);
|
2002-03-06 20:19:35 +00:00
|
|
|
|
|
|
|
// Found the entry point?
|
2003-08-31 20:36:52 +00:00
|
|
|
if (F->getName() == "main") {
|
2004-04-12 05:36:32 +00:00
|
|
|
if (Root) // Found multiple external mains? Don't pick one.
|
|
|
|
Root = ExternalCallingNode;
|
2002-03-06 20:19:35 +00:00
|
|
|
else
|
|
|
|
Root = Node; // Found a main, keep track of it!
|
|
|
|
}
|
|
|
|
}
|
2003-09-15 04:35:16 +00:00
|
|
|
|
|
|
|
// If this function is not defined in this translation unit, it could call
|
|
|
|
// anything.
|
2004-04-12 05:36:32 +00:00
|
|
|
if (F->isExternal() && !F->getIntrinsicID())
|
|
|
|
Node->addCalledFunction(CallsExternalNode);
|
2002-03-06 20:19:35 +00:00
|
|
|
|
2002-07-18 04:43:16 +00:00
|
|
|
// Loop over all of the users of the function... looking for callers...
|
2002-03-06 20:19:35 +00:00
|
|
|
//
|
2003-10-31 21:05:12 +00:00
|
|
|
bool isUsedExternally = false;
|
2003-08-31 20:36:52 +00:00
|
|
|
for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; ++I) {
|
2003-10-31 21:05:12 +00:00
|
|
|
if (Instruction *Inst = dyn_cast<Instruction>(*I)) {
|
|
|
|
if (isOnlyADirectCall(F, CallSite::get(Inst)))
|
|
|
|
getNodeFor(Inst->getParent()->getParent())->addCalledFunction(Node);
|
|
|
|
else
|
|
|
|
isUsedExternally = true;
|
|
|
|
} else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(*I)) {
|
|
|
|
// THIS IS A DISGUSTING HACK. Brought to you by the power of
|
|
|
|
// ConstantPointerRefs!
|
|
|
|
for (Value::use_iterator I = CPR->use_begin(), E = CPR->use_end();
|
|
|
|
I != E; ++I)
|
|
|
|
if (Instruction *Inst = dyn_cast<Instruction>(*I)) {
|
|
|
|
if (isOnlyADirectCall(F, CallSite::get(Inst)))
|
|
|
|
getNodeFor(Inst->getParent()->getParent())->addCalledFunction(Node);
|
|
|
|
else
|
|
|
|
isUsedExternally = true;
|
|
|
|
} else {
|
|
|
|
isUsedExternally = true;
|
|
|
|
}
|
|
|
|
} else { // Can't classify the user!
|
|
|
|
isUsedExternally = true;
|
|
|
|
}
|
2002-03-06 20:19:35 +00:00
|
|
|
}
|
2003-10-31 21:05:12 +00:00
|
|
|
if (isUsedExternally)
|
2004-04-12 05:36:32 +00:00
|
|
|
ExternalCallingNode->addCalledFunction(Node);
|
2002-03-06 20:19:35 +00:00
|
|
|
|
2002-07-18 04:43:16 +00:00
|
|
|
// Look for an indirect function call...
|
2003-08-31 20:36:52 +00:00
|
|
|
for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
|
2002-03-06 20:19:35 +00:00
|
|
|
for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II){
|
2003-10-31 21:05:12 +00:00
|
|
|
CallSite CS = CallSite::get(II);
|
|
|
|
if (CS.getInstruction() && !CS.getCalledFunction())
|
2004-04-12 05:36:32 +00:00
|
|
|
Node->addCalledFunction(CallsExternalNode);
|
2002-03-06 20:19:35 +00:00
|
|
|
}
|
2001-09-28 00:08:15 +00:00
|
|
|
}
|
|
|
|
|
2002-06-25 16:13:24 +00:00
|
|
|
bool CallGraph::run(Module &M) {
|
2002-01-31 00:42:27 +00:00
|
|
|
destroy();
|
|
|
|
|
2002-06-25 16:13:24 +00:00
|
|
|
Mod = &M;
|
2004-04-12 05:36:32 +00:00
|
|
|
ExternalCallingNode = getNodeFor(0);
|
|
|
|
CallsExternalNode = new CallGraphNode(0);
|
2002-03-06 20:19:35 +00:00
|
|
|
Root = 0;
|
2001-11-26 18:51:25 +00:00
|
|
|
|
2002-07-18 04:43:16 +00:00
|
|
|
// Add every function to the call graph...
|
2002-06-25 16:13:24 +00:00
|
|
|
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
|
|
|
|
addToCallGraph(I);
|
2002-03-06 20:19:35 +00:00
|
|
|
|
2002-07-18 04:43:16 +00:00
|
|
|
// If we didn't find a main function, use the external call graph node
|
2004-04-12 05:36:32 +00:00
|
|
|
if (Root == 0) Root = ExternalCallingNode;
|
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() {
|
2002-07-18 04:43:16 +00:00
|
|
|
for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
|
2002-03-06 20:19:35 +00:00
|
|
|
I != E; ++I)
|
2001-11-26 18:51:25 +00:00
|
|
|
delete I->second;
|
2002-07-18 04:43:16 +00:00
|
|
|
FunctionMap.clear();
|
2004-05-02 07:31:34 +00:00
|
|
|
delete CallsExternalNode;
|
2004-05-02 16:06:18 +00:00
|
|
|
CallsExternalNode = 0;
|
2001-11-26 18:51:25 +00:00
|
|
|
}
|
|
|
|
|
2002-11-04 00:21:19 +00:00
|
|
|
static void WriteToOutput(const CallGraphNode *CGN, std::ostream &o) {
|
2002-07-18 04:43:16 +00:00
|
|
|
if (CGN->getFunction())
|
|
|
|
o << "Call graph node for function: '"
|
|
|
|
<< CGN->getFunction()->getName() <<"'\n";
|
2001-11-26 18:51:25 +00:00
|
|
|
else
|
2003-09-15 04:29:37 +00:00
|
|
|
o << "Call graph node <<null function: 0x" << CGN << ">>:\n";
|
2001-11-26 18:51:25 +00:00
|
|
|
|
2001-09-28 00:08:15 +00:00
|
|
|
for (unsigned i = 0; i < CGN->size(); ++i)
|
2002-07-18 04:43:16 +00:00
|
|
|
if ((*CGN)[i]->getFunction())
|
|
|
|
o << " Calls function '" << (*CGN)[i]->getFunction()->getName() << "'\n";
|
2002-03-06 20:19:35 +00:00
|
|
|
else
|
|
|
|
o << " Calls external node\n";
|
2002-01-20 22:54:45 +00:00
|
|
|
o << "\n";
|
2001-09-28 00:08:15 +00:00
|
|
|
}
|
|
|
|
|
2002-11-04 00:21:19 +00:00
|
|
|
void CallGraph::print(std::ostream &o, const Module *M) const {
|
2003-09-15 04:29:37 +00:00
|
|
|
o << "CallGraph Root is: ";
|
|
|
|
if (getRoot()->getFunction())
|
|
|
|
o << getRoot()->getFunction()->getName() << "\n";
|
|
|
|
else
|
|
|
|
o << "<<null function: 0x" << getRoot() << ">>\n";
|
|
|
|
|
2002-11-04 00:21:19 +00:00
|
|
|
for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I)
|
|
|
|
WriteToOutput(I->second, o);
|
2001-09-28 00:08:15 +00:00
|
|
|
}
|
2001-10-22 13:55:46 +00:00
|
|
|
|
|
|
|
|
2001-11-26 18:51:25 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Implementations of public modification methods
|
|
|
|
//
|
|
|
|
|
2002-07-18 04:43:16 +00:00
|
|
|
// Functions to keep a call graph up to date with a function that has been
|
2001-11-26 18:51:25 +00:00
|
|
|
// modified
|
|
|
|
//
|
2004-04-12 05:36:32 +00:00
|
|
|
void CallGraph::addFunctionToModule(Function *F) {
|
2001-11-26 18:51:25 +00:00
|
|
|
assert(0 && "not implemented");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2002-07-18 04:43:16 +00:00
|
|
|
// removeFunctionFromModule - Unlink the function from this module, returning
|
|
|
|
// it. Because this removes the function from the module, the call graph node
|
|
|
|
// is destroyed. This is only valid if the function does not call any other
|
|
|
|
// functions (ie, there are no edges in it's CGN). The easiest way to do this
|
2001-11-26 18:51:25 +00:00
|
|
|
// is to dropAllReferences before calling this.
|
|
|
|
//
|
2002-07-18 04:43:16 +00:00
|
|
|
Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
|
|
|
|
assert(CGN->CalledFunctions.empty() && "Cannot remove function from call "
|
|
|
|
"graph if it references other functions!");
|
2003-08-31 20:36:52 +00:00
|
|
|
Function *F = CGN->getFunction(); // Get the function for the call graph node
|
2002-07-18 04:43:16 +00:00
|
|
|
delete CGN; // Delete the call graph node for this func
|
2003-08-31 20:36:52 +00:00
|
|
|
FunctionMap.erase(F); // Remove the call graph node from the map
|
2001-11-26 18:51:25 +00:00
|
|
|
|
2003-08-31 20:36:52 +00:00
|
|
|
Mod->getFunctionList().remove(F);
|
|
|
|
return F;
|
2001-11-26 18:51:25 +00:00
|
|
|
}
|
|
|
|
|
2003-10-30 05:17:30 +00:00
|
|
|
void CallGraph::stub() {}
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2004-04-12 05:36:32 +00:00
|
|
|
void CallGraphNode::removeCallEdgeTo(CallGraphNode *Callee) {
|
|
|
|
for (unsigned i = CalledFunctions.size(); ; --i) {
|
|
|
|
assert(i && "Cannot find callee to remove!");
|
|
|
|
if (CalledFunctions[i-1] == Callee) {
|
|
|
|
CalledFunctions.erase(CalledFunctions.begin()+i-1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|