//
// 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 is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
#include "llvm/Module.h"
#include "llvm/Instructions.h"
#include "llvm/Support/CallSite.h"
-#include <iostream>
+#include "llvm/Support/Compiler.h"
+#include "llvm/Support/Streams.h"
+#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;
+ return !CS.hasArgument(F);
}
namespace {
//===----------------------------------------------------------------------===//
// BasicCallGraph class definition
//
-class BasicCallGraph : public CallGraph, public ModulePass {
+class VISIBILITY_HIDDEN BasicCallGraph : public CallGraph, public ModulePass {
// Root is root of the call graph, or the external node if a 'main' function
// couldn't be found.
//
CallGraphNode *CallsExternalNode;
public:
- BasicCallGraph() : Root(0), ExternalCallingNode(0), CallsExternalNode(0) {}
- ~BasicCallGraph() { destroy(); }
+ static char ID; // Class identification, replacement for typeinfo
+ BasicCallGraph() : ModulePass((intptr_t)&ID), Root(0),
+ ExternalCallingNode(0), CallsExternalNode(0) {}
// runOnModule - Compute the call graph for the specified module.
virtual bool runOnModule(Module &M) {
- destroy();
CallGraph::initialize(M);
ExternalCallingNode = getOrInsertFunction(0);
AU.setPreservesAll();
}
+ void print(std::ostream *o, const Module *M) const {
+ if (o) print(*o, M);
+ }
+
virtual void print(std::ostream &o, const Module *M) const {
o << "CallGraph Root is: ";
if (Function *F = getRoot()->getFunction())
/// dump - Print out this call graph.
///
inline void dump() const {
- print(std::cerr, Mod);
+ print(cerr, Mod);
}
CallGraphNode* getExternalCallingNode() const { return ExternalCallingNode; }
// If this function is not defined in this translation unit, it could call
// anything.
- if (F->isExternal() && !F->getIntrinsicID())
+ if (F->isDeclaration() && !F->isIntrinsic())
Node->addCalledFunction(CallSite(), CallsExternalNode);
// Loop over all of the users of the function... looking for callers...
//
// destroy - Release memory for the call graph
virtual void destroy() {
- if (!CallsExternalNode) {
- delete CallsExternalNode;
- CallsExternalNode = 0;
- }
+ /// CallsExternalNode is not in the function map, delete it explicitly.
+ delete CallsExternalNode;
+ CallsExternalNode = 0;
+ CallGraph::destroy();
}
};
-RegisterAnalysisGroup<CallGraph> X("Call Graph");
-RegisterOpt<BasicCallGraph> Y("basiccg", "Basic CallGraph Construction");
-RegisterAnalysisGroup<CallGraph, BasicCallGraph, true> Z;
-
} //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 CallGraph::initialize(Module &M) {
- destroy();
Mod = &M;
}
void CallGraph::destroy() {
- if(!FunctionMap.size()) {
+ if (!FunctionMap.empty()) {
for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
I != E; ++I)
delete I->second;
}
void CallGraph::dump() const {
- print(std::cerr, 0);
+ print(cerr, 0);
}
//===----------------------------------------------------------------------===//
OS << "\n";
}
-void CallGraphNode::dump() const { print(std::cerr); }
+void CallGraphNode::dump() const { print(cerr); }
void CallGraphNode::removeCallEdgeTo(CallGraphNode *Callee) {
for (unsigned i = CalledFunctions.size(); ; --i) {
}
}
+/// 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].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.