#include <ostream>
using namespace llvm;
-/// isOnlyADirectCall - Return true if this callsite is *just* a direct call to
-/// the specified function. Specifically return false if the callsite also
-/// takes the address of the function.
-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;
-}
-
namespace {
//===----------------------------------------------------------------------===//
public:
static char ID; // Class identification, replacement for typeinfo
- BasicCallGraph() : ModulePass((intptr_t)&ID), Root(0),
+ BasicCallGraph() : ModulePass(&ID), Root(0),
ExternalCallingNode(0), CallsExternalNode(0) {}
// runOnModule - Compute the call graph for the specified module.
if (F->isDeclaration() && !F->isIntrinsic())
Node->addCalledFunction(CallSite(), CallsExternalNode);
- // Loop over all of the users of the function... looking for callers...
- //
+ // Loop over all of the users of the function, looking for non-call uses.
bool isUsedExternally = false;
- for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; ++I){
+ for (Value::use_iterator I = F->use_begin(), E = F->use_end();
+ I != E && !isUsedExternally; ++I) {
if (Instruction *Inst = dyn_cast<Instruction>(*I)) {
CallSite CS = CallSite::get(Inst);
- if (isOnlyADirectCall(F, CS))
- getOrInsertFunction(Inst->getParent()->getParent())
- ->addCalledFunction(CS, Node);
- else
- isUsedExternally = true;
- } else if (GlobalValue *GV = dyn_cast<GlobalValue>(*I)) {
- for (Value::use_iterator I = GV->use_begin(), E = GV->use_end();
- I != E; ++I)
- if (Instruction *Inst = dyn_cast<Instruction>(*I)) {
- CallSite CS = CallSite::get(Inst);
- if (isOnlyADirectCall(F, CS))
- getOrInsertFunction(Inst->getParent()->getParent())
- ->addCalledFunction(CS, Node);
- else
- isUsedExternally = true;
- } else {
- isUsedExternally = true;
- }
- } else { // Can't classify the user!
+ // Not a call? Or F being passed as a parameter not as the callee?
+ isUsedExternally = !CS.getInstruction() || I.getOperandNo();
+ } else { // User is not a direct call!
isUsedExternally = true;
}
}
if (isUsedExternally)
ExternalCallingNode->addCalledFunction(CallSite(), Node);
- // Look for an indirect function call.
+ // Look for calls by this function.
for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
for (BasicBlock::iterator II = BB->begin(), IE = BB->end();
II != IE; ++II) {
- CallSite CS = CallSite::get(II);
- if (CS.getInstruction() && !CS.getCalledFunction())
- Node->addCalledFunction(CS, CallsExternalNode);
+ CallSite CS = CallSite::get(II);
+ if (CS.getInstruction()) {
+ const Function *Callee = CS.getCalledFunction();
+ if (Callee)
+ Node->addCalledFunction(CS, getOrInsertFunction(Callee));
+ else
+ Node->addCalledFunction(CS, CallsExternalNode);
+ }
}
}
}
};
-RegisterAnalysisGroup<CallGraph> X("Call Graph");
-RegisterPass<BasicCallGraph> Y("basiccg", "Basic CallGraph Construction", false, true);
-RegisterAnalysisGroup<CallGraph, true> Z(Y);
-
} //End anonymous namespace
+static RegisterAnalysisGroup<CallGraph> X("Call Graph");
+static RegisterPass<BasicCallGraph>
+Y("basiccg", "Basic CallGraph Construction", false, true);
+static RegisterAnalysisGroup<CallGraph, true> Z(Y);
+
char CallGraph::ID = 0;
char BasicCallGraph::ID = 0;
void CallGraphNode::dump() const { print(cerr); }
-void CallGraphNode::removeCallEdgeTo(CallGraphNode *Callee) {
+/// removeCallEdgeFor - This method removes the edge in the node for the
+/// specified call site. Note that this method takes linear time, so it
+/// should be used sparingly.
+void CallGraphNode::removeCallEdgeFor(CallSite CS) {
for (unsigned i = CalledFunctions.size(); ; --i) {
- assert(i && "Cannot find callee to remove!");
- if (CalledFunctions[i-1].second == Callee) {
+ assert(i && "Cannot find callsite to remove!");
+ if (CalledFunctions[i-1].first == CS) {
CalledFunctions.erase(CalledFunctions.begin()+i-1);
return;
}
}
}
+
// removeAnyCallEdgeTo - This method removes any call edges from this node to
// the specified callee function. This takes more time to execute than
// removeCallEdgeTo, so it should not be used unless necessary.
}
}
+/// replaceCallSite - Make the edge in the node for Old CallSite be for
+/// New CallSite instead. Note that this method takes linear time, so it
+/// should be used sparingly.
+void CallGraphNode::replaceCallSite(CallSite Old, CallSite New) {
+ for (unsigned i = CalledFunctions.size(); ; --i) {
+ assert(i && "Cannot find callsite to replace!");
+ if (CalledFunctions[i-1].first == Old) {
+ CalledFunctions[i-1].first = New;
+ return;
+ }
+ }
+}
+
// Enuse that users of CallGraph.h also link with this file
DEFINING_FILE_FOR(CallGraph)