//===- CallGraph.cpp - Build a Module's call graph ------------------------===//
-//
+//
// 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.
-//
+//
//===----------------------------------------------------------------------===//
//
// This file implements the CallGraph class.
//===----------------------------------------------------------------------===//
#include "llvm/Analysis/CallGraph.h"
-#include "llvm/Constants.h" // Remove when ConstantPointerRefs are gone
#include "llvm/Module.h"
-#include "llvm/iOther.h"
-#include "llvm/iTerminators.h"
+#include "llvm/Instructions.h"
#include "llvm/Support/CallSite.h"
-#include "Support/STLExtras.h"
+#include "llvm/ADT/STLExtras.h"
+#include <iostream>
using namespace llvm;
static RegisterAnalysis<CallGraph> X("callgraph", "Call Graph Construction");
Root = Node; // Found a main, keep track of it!
}
}
-
+
// If this function is not defined in this translation unit, it could call
// anything.
if (F->isExternal() && !F->getIntrinsicID())
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();
+ } 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)) {
if (isOnlyADirectCall(F, CallSite::get(Inst)))
}
}
-bool CallGraph::run(Module &M) {
+bool CallGraph::runOnModule(Module &M) {
destroy();
Mod = &M;
// If we didn't find a main function, use the external call graph node
if (Root == 0) Root = ExternalCallingNode;
-
+
return false;
}
delete I->second;
FunctionMap.clear();
delete CallsExternalNode;
+ CallsExternalNode = 0;
}
-static void WriteToOutput(const CallGraphNode *CGN, std::ostream &o) {
- if (CGN->getFunction())
- o << "Call graph node for function: '"
- << CGN->getFunction()->getName() <<"'\n";
+void CallGraphNode::print(std::ostream &OS) const {
+ if (Function *F = getFunction())
+ OS << "Call graph node for function: '" << F->getName() <<"'\n";
else
- o << "Call graph node <<null function: 0x" << CGN << ">>:\n";
+ OS << "Call graph node <<null function: 0x" << this << ">>:\n";
- for (unsigned i = 0; i < CGN->size(); ++i)
- if ((*CGN)[i]->getFunction())
- o << " Calls function '" << (*CGN)[i]->getFunction()->getName() << "'\n";
+ for (const_iterator I = begin(), E = end(); I != E; ++I)
+ if ((*I)->getFunction())
+ OS << " Calls function '" << (*I)->getFunction()->getName() << "'\n";
else
- o << " Calls external node\n";
- o << "\n";
+ OS << " Calls external node\n";
+ OS << "\n";
}
-void CallGraph::print(std::ostream &o, const Module *M) const {
- o << "CallGraph Root is: ";
- if (getRoot()->getFunction())
- o << getRoot()->getFunction()->getName() << "\n";
+void CallGraphNode::dump() const { print(std::cerr); }
+
+void CallGraph::print(std::ostream &OS, const Module *M) const {
+ OS << "CallGraph Root is: ";
+ if (Function *F = getRoot()->getFunction())
+ OS << F->getName() << "\n";
else
- o << "<<null function: 0x" << getRoot() << ">>\n";
-
+ OS << "<<null function: 0x" << getRoot() << ">>\n";
+
for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I)
- WriteToOutput(I->second, o);
+ I->second->print(OS);
+}
+
+void CallGraph::dump() const {
+ print(std::cerr, 0);
}
// Implementations of public modification methods
//
-// Functions to keep a call graph up to date with a function that has been
-// modified
-//
-void CallGraph::addFunctionToModule(Function *F) {
- assert(0 && "not implemented");
- abort();
-}
-
// 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
return F;
}
+// changeFunction - This method changes the function associated with this
+// CallGraphNode, for use by transformations that need to change the prototype
+// of a Function (thus they must create a new Function and move the old code
+// over).
+void CallGraph::changeFunction(Function *OldF, Function *NewF) {
+ iterator I = FunctionMap.find(OldF);
+ CallGraphNode *&New = FunctionMap[NewF];
+ assert(I != FunctionMap.end() && I->second && !New &&
+ "OldF didn't exist in CG or NewF already does!");
+ New = I->second;
+ New->F = NewF;
+ FunctionMap.erase(I);
+}
+
+
void CallGraph::stub() {}
void CallGraphNode::removeCallEdgeTo(CallGraphNode *Callee) {
}
}
}
+
+// 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.
+void CallGraphNode::removeAnyCallEdgeTo(CallGraphNode *Callee) {
+ for (unsigned i = 0, e = CalledFunctions.size(); i != e; ++i)
+ if (CalledFunctions[i] == Callee) {
+ CalledFunctions[i] = CalledFunctions.back();
+ CalledFunctions.pop_back();
+ --i; --e;
+ }
+}